#include <gtk/gtk.h>
#include <string.h>
#include <stdlib.h>
int window_width = 500;
int window_height = 200;
void button_click(GtkButton *button,gpointer data)
{
printf("button clicked!\n");
}
void button_enter(GtkButton *button,gpointer data)
{
printf("button enter!\n");
}
void button_leave(GtkButton *button,gpointer data)
{
printf("button leave!\n");
}
void button_pressed(GtkButton *button,gpointer data)
{
printf("button pressed!\n");
}
void button_released(GtkButton *button,gpointer data)
{
printf("button released!\n");
}
void button_toggled(GtkToggleButton *button,gpointer data)
{
gboolean state = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
if(state)
{
printf("toggle button pressed!\n");
}
else
{
printf("toggle button unpressed!\n");
}
}
void button_checked(GtkToggleButton *button,gpointer data)
{
gboolean state = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
if(state)
{
printf("check button checked!\n");
}
else
{
printf("check button unchecked!\n");
}
}
void button_switch(GtkSwitch *button,gpointer data)
{
gboolean state = gtk_switch_get_active(GTK_SWITCH(button));
if(state)
{
printf("switch on!\n");
}
else
{
printf("switch off!\n");
}
}
void button_volume_value_change(GtkScaleButton *button,gpointer data)
{
gdouble value = gtk_scale_button_get_value(GTK_SCALE_BUTTON(button));
printf("volume value changed:%lf\n",value);
}
void file_chooser_selection_changed(GtkFileChooser *chooser,gpointer data)
{
gchar* file_name = gtk_file_chooser_get_filename(chooser);
printf("selected file is:%s\n",file_name);
}
void font_choose(GtkFontButton *button,gpointer data)
{
gchar* font_name = gtk_font_button_get_font_name(button);
printf("selected font is:%s\n",font_name);
}
void color_choose(GtkColorButton *button,gpointer data)
{
GdkRGBA *color;
color = (GdkRGBA *)malloc(sizeof(GdkRGBA *));
gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(button),color);
printf("color rgb(%lf,%lf,%lf)\n",color->red,color->green,color->blue);
free(color);
}
void spin_value_change(GtkSpinButton *button,gpointer data)
{
gint value = gtk_spin_button_get_value_as_int(button);
printf("spin value is:%d\n", value);
}
int main(int argc,char *argv[])
{
int row = 5,column = 4,i,j;
GtkWidget *window,*grid;
GtkWidget *normal_button,*toggle_button,*check_button,*switch_button;
GtkWidget *link_button,*volume_button,*radio_button_1,*radio_button_2;
GtkWidget *file_choose_button,*font_button,*color_button,*spin_button;
GtkWidget *image_button,*image;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request(window,window_width,window_height);
grid = gtk_grid_new();
gtk_container_add(GTK_CONTAINER(window), grid);
gtk_grid_set_row_spacing (GTK_GRID(grid), 20);
gtk_grid_set_column_spacing(GTK_GRID(grid),30);
for(i=0;i<row;i++)
{
gtk_grid_insert_row(GTK_GRID(grid),0);
}
for(j=0;j<column;j++)
{
gtk_grid_insert_column(GTK_GRID(grid),0);
}
normal_button = gtk_button_new_with_label("normal button");
g_signal_connect(GTK_WIDGET(normal_button),"clicked",G_CALLBACK(button_click),NULL);
g_signal_connect(GTK_WIDGET(normal_button),"enter",G_CALLBACK(button_enter),NULL);
g_signal_connect(GTK_WIDGET(normal_button),"leave",G_CALLBACK(button_leave),NULL);
g_signal_connect(GTK_WIDGET(normal_button),"pressed",G_CALLBACK(button_pressed),NULL);
g_signal_connect(GTK_WIDGET(normal_button),"released",G_CALLBACK(button_released),NULL);
toggle_button = gtk_toggle_button_new_with_label("toggle button");
g_signal_connect(GTK_WIDGET(toggle_button),"toggled",G_CALLBACK(button_toggled),NULL);
check_button = gtk_check_button_new_with_label("choose it");
g_signal_connect(GTK_WIDGET(check_button),"toggled",G_CALLBACK(button_checked),NULL);
switch_button = gtk_switch_new();
g_signal_connect(GTK_WIDGET(switch_button),"notify::active",G_CALLBACK(button_switch),NULL);
link_button = gtk_link_button_new_with_label("http://www.gnome.org","link button");
volume_button = gtk_volume_button_new();
g_signal_connect(GTK_WIDGET(volume_button),"value-changed",G_CALLBACK(button_volume_value_change),NULL);
radio_button_1 = gtk_radio_button_new_with_label(NULL,"radio 1");
radio_button_2 = gtk_radio_button_new_with_label_from_widget(radio_button_1,"radio 2");
file_choose_button = gtk_file_chooser_button_new("file choose",GTK_FILE_CHOOSER_ACTION_OPEN);
g_signal_connect(GTK_WIDGET(file_choose_button),"selection-changed",G_CALLBACK(file_chooser_selection_changed),NULL);
font_button = gtk_font_button_new();
g_signal_connect(GTK_WIDGET(font_button),"font-set",G_CALLBACK(font_choose),NULL);
color_button = gtk_color_button_new();
g_signal_connect(GTK_WIDGET(color_button),"color-set",G_CALLBACK(color_choose),NULL);
spin_button = gtk_spin_button_new_with_range(0,100,2);
g_signal_connect(GTK_WIDGET(spin_button),"value-changed",G_CALLBACK(spin_value_change),NULL);
image_button = gtk_button_new();
image = gtk_image_new_from_file("1.png");
gtk_button_set_image(GTK_BUTTON(image_button), image);
gtk_grid_attach(GTK_GRID(grid),normal_button,1,1,1,1);
gtk_grid_attach(GTK_GRID(grid),toggle_button,2,1,1,1);
gtk_grid_attach(GTK_GRID(grid),check_button,3,1,1,1);
gtk_grid_attach(GTK_GRID(grid),switch_button,4,1,1,1);
gtk_grid_attach(GTK_GRID(grid),link_button,1,2,1,1);
gtk_grid_attach(GTK_GRID(grid),volume_button,2,2,1,1);
gtk_grid_attach(GTK_GRID(grid),radio_button_1,3,2,1,1);
gtk_grid_attach(GTK_GRID(grid),radio_button_2,4,2,1,1);
gtk_grid_attach(GTK_GRID(grid),file_choose_button,1,3,1,1);
gtk_grid_attach(GTK_GRID(grid),font_button,2,3,1,1);
gtk_grid_attach(GTK_GRID(grid),color_button,3,3,1,1);
gtk_grid_attach(GTK_GRID(grid),spin_button,4,3,1,1);
gtk_grid_attach(GTK_GRID(grid),image_button,1,4,1,1);
gtk_window_set_title(GTK_WINDOW(window),"hello fishc - GtkButton");
gtk_widget_show_all(window);
gtk_main ();
return 0;
}