#include <gtk/gtk.h>
void activate();
int main(int argc, char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("jp.vivacocoa.button", 0 );
g_signal_connect (app, "activate",
G_CALLBACK( activate), NULL);
status = g_application_run (G_APPLICATION(app), argc, argv);
g_object_unref (app);
return status;
}
void activate(GtkApplication *app, gpointer data)
{
GtkWidget *window;
GtkWidget *button;
window =
gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW(window), "Button");
gtk_window_set_default_size (GTK_WINDOW(window), 300, 200);
gtk_window_set_position (GTK_WINDOW(window), 1 );
button =
gtk_button_new_with_label ("Quit");
gtk_widget_set_size_request (button, 100, -1 );
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_swapped (button, "clicked",
G_CALLBACK(gtk_widget_destroy),
window);
gtk_widget_show_all (window);
}
#include <gtk/gtk.h>
void activate();
void toggle_title();
int main(int argc, char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("jp.vivacocoa.chekcbutton", 0 );
g_signal_connect (app, "activate",
G_CALLBACK( activate), NULL);
status = g_application_run (G_APPLICATION(app), argc, argv);
g_object_unref (app);
return status;
}
void activate(GtkApplication *app, gpointer data)
{
GtkWidget *window;
GtkWidget *check;
window =
gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW(window),
"CheckButton" );
gtk_window_set_default_size (GTK_WINDOW(window), 300, 200);
gtk_window_set_position (GTK_WINDOW(window), 1 );
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 (check, "clicked",
G_CALLBACK(toggle_title),
window );
gtk_widget_show_all (window);
}
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, "" );
}
macOSの場合、チェックボタンとラジオボタンを使った場合に、 実行時に次のような警告が出ることがありますが、結果には影響ありません。
(a.out:713): Gtk-WARNING **: 07:26:23.142: Could not load a pixbuf
from /org/gtk/libgtk/theme/Adwaita/assets/check-symbolic.svg.
This may indicate that pixbuf loaders or the mime database could not be
found.
#include <gtk/gtk.h>
void activate();
void value_changed();
int main(int argc, char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("jp.vivacocoa.scale" , 0 );
g_signal_connect (app, "activate",
G_CALLBACK( activate), NULL);
status = g_application_run (G_APPLICATION(app), argc, argv);
g_object_unref (app);
return status;
}
void activate(GtkApplication *app, gpointer data)
{
GtkWidget *window;
GtkWidget *hbox;
GtkWidget *scale;
GtkWidget *label;
window =
gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW(window), "Scale" );
gtk_window_set_default_size (GTK_WINDOW(window), 300, 200);
gtk_window_set_position (GTK_WINDOW(window), 1 );
gtk_container_set_border_width (GTK_CONTAINER(window), 15 );
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0 );
gtk_container_add (GTK_CONTAINER(window), hbox);
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");
gtk_box_pack_start (GTK_BOX(hbox), scale, 1, 1, 0 );
gtk_box_pack_end (GTK_BOX(hbox), label, 0, 0, 0 );
g_signal_connect (scale, "value-changed",
G_CALLBACK( value_changed),
label);
gtk_widget_show_all (window);
}
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 (label, str);
g_free(str);
}
GTK_ORIENTATION_HORIZONTAL | 横向き | 値は0 |
GTK_ORIENTATION_VERTICAL | 縦向き | 値は1 |
#include <gtk/gtk.h>
guchar r = 0;
guchar g = 0;
guchar b = 0;
void activate ();
void draw_callback ();
void togglered ();
void togglegreen ();
void toggleblue ();
int main(int argc, char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("jp.vivacocoa.togglebutton", 0 );
g_signal_connect (app, "activate",
G_CALLBACK( activate), NULL);
status = g_application_run (G_APPLICATION(app), argc, argv);
g_object_unref (app);
return status;
}
void activate(GtkApplication *app, gpointer data)
{
GtkWidget *window;
GtkWidget *fixed;
GtkWidget *red;
GtkWidget *green;
GtkWidget *blue;
GtkWidget *darea;
/* Window */
window =
gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW(window),
"ToggleButton" );
gtk_window_set_default_size (GTK_WINDOW(window), 300, 200);
gtk_window_set_position (GTK_WINDOW(window), 1 );
/* Fixed */
fixed = gtk_fixed_new ();
gtk_container_add (GTK_CONTAINER(window), fixed );
/* Buttons */
red =
gtk_toggle_button_new_with_label("Red");
green =
gtk_toggle_button_new_with_label("Green");
blue =
gtk_toggle_button_new_with_label("Blue");
darea =
gtk_drawing_area_new ();
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 (darea, 110, 110);
/* Puts to 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), darea,150, 20);
/* Events */
g_signal_connect (darea, "draw",
G_CALLBACK(draw_callback), NULL);
g_signal_connect (red, "toggled",
G_CALLBACK(togglered), darea );
g_signal_connect (green, "toggled",
G_CALLBACK(togglegreen), darea );
g_signal_connect (blue, "toggled",
G_CALLBACK(toggleblue), darea );
gtk_widget_show_all (window);
}
void draw_callback(GtkWidget *darea, cairo_t *cr)
{
GdkRGBA color;
gchar *rgb;
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_paint (cr);
}
void togglered(GtkToggleButton *red, GtkWidget *darea)
{
if (gtk_toggle_button_get_active(red))
r = 255;
else
r = 0;
gtk_widget_queue_draw(darea);
}
void togglegreen(GtkToggleButton *green, GtkWidget *darea)
{
if (gtk_toggle_button_get_active(green))
g = 255;
else
g = 0;
gtk_widget_queue_draw(darea);
}
void toggleblue(GtkToggleButton *blue, GtkWidget *darea)
{
if (gtk_toggle_button_get_active(blue))
b = 255;
else
b = 0;
gtk_widget_queue_draw(darea);
}
#include <gtk/gtk.h>
void activate ();
void on_toggle ();
int main(int argc, char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("jp.vivacocoa.radiobutton", 0 );
g_signal_connect (app, "activate",
G_CALLBACK(activate), NULL );
status = g_application_run (G_APPLICATION(app), argc, argv );
g_object_unref (app);
return status;
}
void activate(GtkApplication *app, gpointer data)
{
GtkWidget *window;
GtkWidget *frame;
GtkWidget *vbox;
GtkWidget *hbox;
GtkWidget *red;
GtkWidget *green;
GtkWidget *blue;
GSList *group = NULL;
/* Window */
window =
gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW(window),
"RadioButton" );
gtk_window_set_default_size (GTK_WINDOW(window), 300, 200);
gtk_window_set_position (GTK_WINDOW(window), 1 );
gtk_container_set_border_width (GTK_CONTAINER(window), 15 );
/* Containers */
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 );
/* Radio buttons */
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" );
/* Pack to box */
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_end (GTK_BOX(hbox), blue , 1, 0, 0 );
/* Signals */
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 );
gtk_widget_show_all (window);
}
void on_toggle(GtkButton *button, GtkFrame *frame)
{
const gchar *label = gtk_button_get_label(button);
gtk_frame_set_label(frame, label);
}
macOSの場合、チェックボタンとラジオボタンを使った場合に、 実行時に次のような警告が出ることがありますが、結果には影響ありません。
(a.out:736): Gtk-WARNING **: 07:33:22.044: 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 activate ();
void select_combo ();
int main(int argc, char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("jp.vivacocoa.comboboxtext", 0 );
g_signal_connect (app, "activate",
G_CALLBACK( activate), NULL);
status = g_application_run (G_APPLICATION(app), argc, argv);
g_object_unref (app);
return status;
}
void activate(GtkApplication *app, gpointer data)
{
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *combo;
GtkWidget *label;
/* Window */
window =
gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW(window),
"ComboBoxText" );
gtk_window_set_default_size (GTK_WINDOW(window), 300, 200);
gtk_window_set_position (GTK_WINDOW(window), 1 );
gtk_container_set_border_width (GTK_CONTAINER(window), 15 );
/* Container */
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0 );
gtk_container_add (GTK_CONTAINER(window), vbox );
/* ComboBoxText */
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 */
label = gtk_label_new ("Unselected");
/* Pack to box */
gtk_box_pack_start (GTK_BOX(vbox), combo, 0, 0, 0 );
gtk_box_pack_end (GTK_BOX(vbox), label, 1, 0, 0 );
/* Signal */
g_signal_connect (combo, "changed",
G_CALLBACK(select_combo), label);
gtk_widget_show_all (window);
}
void select_combo(GtkComboBoxText *combo, GtkLabel *label)
{
gchar *os;
os = gtk_combo_box_text_get_active_text(combo);
gtk_label_set_text(label, os);
}
コンボボックステキストで項目を選択すると、 選択された項目の文字列がラベルに表示されます。
#include <gtk/gtk.h>
void activate ();
void select_list ();
int main(int argc, char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("jp.vivacocoa.listbox", 0 );
g_signal_connect (app, "activate",
G_CALLBACK( activate), NULL);
status = g_application_run (G_APPLICATION(app), argc, argv);
g_object_unref (app);
return status;
}
void activate(GtkApplication *app, gpointer data)
{
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *listbox;
GtkWidget *label;
/* Window */
window =
gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW(window), "ListBox" );
gtk_window_set_default_size (GTK_WINDOW(window), 300, 200);
gtk_window_set_position (GTK_WINDOW(window), 1 );
gtk_container_set_border_width (GTK_CONTAINER(window), 15 );
/* Container */
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 15 );
gtk_container_add (GTK_CONTAINER(window), vbox);
/* ListBox */
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 */
label = gtk_label_new (NULL);
/* Pack to box */
gtk_box_pack_start (GTK_BOX(vbox), listbox, 1, 1, 0);
gtk_box_pack_end (GTK_BOX(vbox), label, 0, 0, 0);
/* Signal */
g_signal_connect (listbox, "row_selected",
G_CALLBACK(select_list), label);
gtk_widget_show_all (window);
}
void select_list(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)));
}