ラジオボタンで使うイベントは "group_changed" ではなく、 "toggled" でした。2019/12/21に訂正しました。
このサンプルはGTK3対応です。GTK2ではコンパイルできません。
#include <gtk/gtk.h>
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *button;
gtk_init(&argc, &argv);
window = gtk_window_new (0);
gtk_window_set_title (GTK_WINDOW(window), "Button");
gtk_window_set_default_size (GTK_WINDOW(window), 320, 240);
gtk_window_set_position (GTK_WINDOW(window), 1);
gtk_container_set_border_width (GTK_CONTAINER(window), 15);
button = gtk_button_new_with_label ("Quit");
gtk_widget_set_size_request (button, 100, 25);
gtk_widget_set_halign (button, GTK_ALIGN_CENTER);
gtk_widget_set_valign (button, GTK_ALIGN_CENTER);
gtk_container_add (GTK_CONTAINER(window), button);
g_signal_connect (G_OBJECT(button), "clicked",
G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect (G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
このサンプルはGTK3に対応しています。GTK2ではコンパイルできません。
#include <gtk/gtk.h>
void toggle_title(GtkWidget *widget, gpointer window);
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *check;
gtk_init(&argc, &argv);
window = gtk_window_new (0);
gtk_window_set_title (GTK_WINDOW(window),
"CheckButton");
gtk_window_set_default_size (GTK_WINDOW(window), 320, 240);
gtk_window_set_position (GTK_WINDOW(window), 1);
gtk_container_set_border_width (GTK_CONTAINER(window), 15);
check = gtk_check_button_new_with_label ("Show title");
gtk_widget_set_halign (check, GTK_ALIGN_CENTER);
gtk_widget_set_valign (check, GTK_ALIGN_CENTER);
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(check), TRUE);
gtk_container_add (GTK_CONTAINER(window), check);
g_signal_connect (G_OBJECT(check), "clicked",
G_CALLBACK(toggle_title), window);
g_signal_connect (G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
void toggle_title(GtkWidget *widget, gpointer window)
{
if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
{
gtk_window_set_title(window, "CheckButton");
} else {
gtk_window_set_title(window, "");
}
}
#include <gtk/gtk.h>
void value_changed(GtkRange *range, gpointer label);
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *hbox;
GtkWidget *scale;
GtkWidget *label;
gtk_init(&argc, &argv);
window = gtk_window_new (0);
gtk_window_set_title (GTK_WINDOW(window), "Scale");
gtk_window_set_default_size (GTK_WINDOW(window), 320, 240);
gtk_window_set_position (GTK_WINDOW(window), 1);
gtk_container_set_border_width (GTK_CONTAINER(window), 15);
scale = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
0, 100, 1);
gtk_scale_set_draw_value (GTK_SCALE(scale), FALSE);
label = gtk_label_new ("0");
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
gtk_box_pack_start (GTK_BOX(hbox),scale, 1, 1, 0);
gtk_box_pack_start (GTK_BOX(hbox),label, 0, 0, 0);
gtk_widget_set_valign (hbox, GTK_ALIGN_CENTER);
gtk_container_add (GTK_CONTAINER(window), hbox);
g_signal_connect (scale, "value-changed",
G_CALLBACK(value_changed), label);
g_signal_connect (window, "destroy",
gtk_main_quit, NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
void value_changed(GtkRange *range, gpointer label)
{
gdouble val = gtk_range_get_value(range);
gchar *str = g_strdup_printf("%.f", val);
gtk_label_set_text(GTK_LABEL(label), str);
g_free(str);
}
#include <gtk/gtk.h>
guchar r = 0;
guchar g = 0;
guchar b = 0;
void draw_callback ();
void togglered ();
void togglegreen ();
void toggleblue ();
/*
void draw_callback (GtkWidget *da, cairo_t *cr);
void togglered (GtkToggleButton *red, GtkWidget *da);
void togglegreen (GtkToggleButton *green, GtkWidget *da);
void toggleblue (GtkToggleButton *blue, GtkWidget *da);
*/
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *da;
GtkWidget *red;
GtkWidget *green;
GtkWidget *blue;
GtkWidget *frame;
GtkWidget *fixed;
gtk_init(&argc, &argv);
window = gtk_window_new (0);
gtk_window_set_title (GTK_WINDOW(window),
"ToggleButton");
gtk_window_set_default_size (GTK_WINDOW(window), 320, 240);
gtk_window_set_position (GTK_WINDOW(window), 1);
gtk_container_set_border_width (GTK_CONTAINER(window), 15);
red = gtk_toggle_button_new_with_label("Red");
green = gtk_toggle_button_new_with_label("Green");
blue = gtk_toggle_button_new_with_label("Blue");
da = gtk_drawing_area_new ();
frame = gtk_frame_new (NULL);
gtk_container_add (GTK_CONTAINER(frame), da);
gtk_widget_set_size_request (red, 100, 25);
gtk_widget_set_size_request (green, 100, 25);
gtk_widget_set_size_request (blue, 100, 25);
gtk_widget_set_size_request (da, 110, 110);
fixed = gtk_fixed_new ();
gtk_container_add (GTK_CONTAINER(window), fixed);
gtk_fixed_put (GTK_FIXED(fixed), red, 20, 20);
gtk_fixed_put (GTK_FIXED(fixed), green, 20, 70);
gtk_fixed_put (GTK_FIXED(fixed), blue, 20, 120);
gtk_fixed_put (GTK_FIXED(fixed), frame,150, 20);
g_signal_connect (G_OBJECT(da), "draw",
G_CALLBACK(draw_callback), NULL);
g_signal_connect (G_OBJECT(red), "toggled",
G_CALLBACK(togglered), da);
g_signal_connect (G_OBJECT(green), "toggled",
G_CALLBACK(togglegreen), da);
g_signal_connect (G_OBJECT(blue), "toggled",
G_CALLBACK(toggleblue), da);
g_signal_connect (window, "destroy",
gtk_main_quit, NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
void draw_callback (GtkWidget *da, cairo_t *cr)
{
guint width, height;
GdkRGBA color;
gchar *rgb;
width = gtk_widget_get_allocated_width (da);
height = gtk_widget_get_allocated_height (da);
cairo_rectangle(cr, 0, 0, width, height);
rgb = g_strdup_printf("rgb(%d,%d,%d)",r, g, b);
gdk_rgba_parse(&color, rgb);
g_free(rgb);
gdk_cairo_set_source_rgba (cr, &color);
cairo_fill (cr);
//printf("%s\n", gdk_rgba_to_string(&color));
}
void togglered(GtkToggleButton *red, GtkWidget *da)
{
if (gtk_toggle_button_get_active(red))
{
r = 255;
} else {
r = 0;
}
gtk_widget_queue_draw(da);
}
void togglegreen(GtkToggleButton *green, GtkWidget *da)
{
if (gtk_toggle_button_get_active(green))
{
g = 255;
} else {
g = 0;
}
gtk_widget_queue_draw(da);
}
void toggleblue(GtkToggleButton *blue, GtkWidget *da)
{
if (gtk_toggle_button_get_active(blue))
{
b = 255;
} else {
b = 0;
}
gtk_widget_queue_draw(da);
}
ラジオボタンで使うイベントは "group_changed" ではなく、 "toggled" でした。2019/12/21に訂正しました。
#include <gtk/gtk.h>
//#include <glib.h> //環境によっては必要な場合があります。
void on_toggle();
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *frame;
GtkWidget *vbox;;
GtkWidget *hbox;
GtkWidget *red;
GtkWidget *green;
GtkWidget *blue;
GSList *group = NULL;
gtk_init(&argc, &argv);
window = gtk_window_new (0);
gtk_window_set_title (GTK_WINDOW(window),
"RadioButton");
gtk_window_set_default_size (GTK_WINDOW(window), 320, 240);
gtk_container_set_border_width (GTK_CONTAINER(window), 15);
frame = gtk_frame_new ("Red");
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
gtk_container_add (GTK_CONTAINER(window), frame);
gtk_container_add (GTK_CONTAINER(frame), vbox);
gtk_box_pack_start (GTK_BOX(vbox), hbox, 1, 0, 0);
red = gtk_radio_button_new_with_label
(group, "Red");
green = gtk_radio_button_new_with_label_from_widget
(GTK_RADIO_BUTTON(red), "Green");
blue = gtk_radio_button_new_with_label_from_widget
(GTK_RADIO_BUTTON(red), "Blue");
gtk_box_pack_start (GTK_BOX(hbox), red, 1, 0, 0);
gtk_box_pack_start (GTK_BOX(hbox), green, 1, 0, 0);
gtk_box_pack_start (GTK_BOX(hbox), blue, 1, 0, 0);
g_signal_connect (red, "toggled",
G_CALLBACK(on_toggle), frame);
g_signal_connect (green, "toggled",
G_CALLBACK(on_toggle), frame);
g_signal_connect (blue, "toggled",
G_CALLBACK(on_toggle), frame);
g_signal_connect (window, "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
void on_toggle(GtkButton *button, GtkFrame *frame)
{
const gchar * label = gtk_button_get_label(button);
gtk_frame_set_label(frame, label);
}
環境によっては次のような警告が出ることがありますが、 結果には影響ありません。
(a.out:2816): Gtk-WARNING **: 20:56:56.165: Could not load a pixbuf from /org/gtk/libgtk/theme/Adwaita/assets/bullet-symbolic.svg.
This may indicate that pixbuf loaders or the mime database could not be found.
#include <gtk/gtk.h>
void combo_selected();
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *combo;
GtkWidget *label;
GtkWidget *vbox;
gtk_init(&argc, &argv);
window = gtk_window_new (0);
/* GTK_WINDOW_TOPLEVELは 0 です */
gtk_window_set_title (GTK_WINDOW(window),
"ComboBoxText");
gtk_window_set_default_size (GTK_WINDOW(window), 320, 240);
gtk_window_set_position (GTK_WINDOW(window), 1);
/* GTK_WIN_POS_CENTERは 1 です */
gtk_container_set_border_width (GTK_CONTAINER(window), 15);
combo = gtk_combo_box_text_new();
gtk_combo_box_text_append (GTK_COMBO_BOX_TEXT(combo), NULL,
"Windows");
gtk_combo_box_text_append (GTK_COMBO_BOX_TEXT(combo), NULL,
"macOS");
gtk_combo_box_text_append (GTK_COMBO_BOX_TEXT(combo), NULL,
"Ubuntu");
gtk_combo_box_text_append (GTK_COMBO_BOX_TEXT(combo), NULL,
"Fedora");
label = gtk_label_new ("未選択");
vbox = gtk_box_new (1, 0);
/*GTK_ORIENTATION_HORIZONTALは 0、GTK_ORIENTATION_VERTICALは 1です*/
gtk_box_pack_start (GTK_BOX(vbox), combo, 0, 0, 0);
/* 第3と第4引数はTRUEを1、FALSEを0と記述しています */
gtk_box_pack_end (GTK_BOX(vbox), label, 1, 0, 0);
/* gtk_box_pack_end関数で、後ろから配置することもできます */
gtk_container_add (GTK_CONTAINER(window), vbox);
g_signal_connect (combo, "changed",
G_CALLBACK(combo_selected),label);
g_signal_connect (window, "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
void combo_selected(GtkComboBoxText *combo, GtkLabel *label)
{
gchar *os;
os = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(combo));
gtk_label_set_text(label, os);
/* label引数をGtkWidget型とし、GTK_LABEL(label)とキャストすることも可 */
//printf("%s\n", os);
}
#include <gtk/gtk.h>
void list_selected();
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *listbox;
GtkWidget *label;
GtkWidget *vbox;
gtk_init(&argc, &argv);
window = gtk_window_new (0);
/* GTK_WINDOW_TOPLEVEL定数は 0 です */
gtk_window_set_title (GTK_WINDOW(window),
"ListBox");
gtk_window_set_default_size (GTK_WINDOW(window), 320, 240);
gtk_window_set_position (GTK_WINDOW(window), 1);
/* GTK_WIN_POS_CENTER定数は 1 です */
gtk_container_set_border_width (GTK_CONTAINER(window), 15);
listbox = gtk_list_box_new ();
gtk_list_box_insert (GTK_LIST_BOX(listbox),
gtk_label_new("Windows"), -1);
gtk_list_box_insert (GTK_LIST_BOX(listbox),
gtk_label_new("macOS"), -1);
gtk_list_box_insert (GTK_LIST_BOX(listbox),
gtk_label_new("Ubuntu"), -1);
gtk_list_box_insert (GTK_LIST_BOX(listbox),
gtk_label_new("Fedora"), -1);
label = gtk_label_new (NULL);
vbox = gtk_box_new (1, 15);
/*GTK_ORIENTATION_HORIZONTALは 0、GTK_ORIENTATION_VERTICALは 1です*/
gtk_box_pack_start (GTK_BOX(vbox), listbox, 1, 1, 0);
/* 第3と第4引数はTRUEを1、FALSEを0と記述しています */
gtk_box_pack_end (GTK_BOX(vbox), label, 0, 0, 0);
/* gtk_box_pack_end関数で、後ろから配置することもできます */
gtk_container_add (GTK_CONTAINER(window), vbox);
g_signal_connect (listbox, "row_selected",
G_CALLBACK(list_selected), label);
/* "row-activated"というイベントも使えます */
g_signal_connect (window, "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
void
list_selected(GtkListBox *list, GtkListBoxRow * row, GtkLabel *label)
{
GtkWidget *child = gtk_bin_get_child(GTK_BIN(row));
gtk_label_set_text(label, gtk_label_get_text(GTK_LABEL(child)));
}