この章では、各種ダイアログを説明します。
#include <gtk/gtk.h>
void show_info();
void show_warn();
void show_ques();
void show_erro();
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *grid;
GtkWidget *info;
GtkWidget *warn;
GtkWidget *ques;
GtkWidget *erro;
gtk_init(&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL); // 0
gtk_window_set_title (GTK_WINDOW(window),
"Message dialogs");
gtk_window_set_default_size (GTK_WINDOW(window), 320, 240);
gtk_window_set_position (GTK_WINDOW(window),
GTK_WIN_POS_CENTER); // 1
gtk_container_set_border_width (GTK_CONTAINER(window), 15);
grid = gtk_grid_new ();
gtk_grid_set_row_spacing (GTK_GRID(grid), 15);
gtk_grid_set_column_spacing (GTK_GRID(grid), 15);
gtk_grid_set_row_homogeneous (GTK_GRID(grid), TRUE);
gtk_grid_set_column_homogeneous (GTK_GRID(grid), TRUE);
gtk_container_add (GTK_CONTAINER(window), grid);
info = gtk_button_new_with_label
("Info");
warn = gtk_button_new_with_label
("Warning");
ques = gtk_button_new_with_label
("Question");
erro = gtk_button_new_with_label
("Error");
gtk_grid_attach (GTK_GRID(grid), info, 0, 0, 1, 1);
gtk_grid_attach (GTK_GRID(grid), warn, 1, 0, 1, 1);
gtk_grid_attach (GTK_GRID(grid), ques, 0, 1, 1, 1);
gtk_grid_attach (GTK_GRID(grid), erro, 1, 1, 1, 1);
g_signal_connect (info, "clicked",
G_CALLBACK(show_info), window);
g_signal_connect (warn, "clicked",
G_CALLBACK(show_warn), window);
g_signal_connect (ques, "clicked",
G_CALLBACK(show_ques), window);
g_signal_connect (erro, "clicked",
G_CALLBACK(show_erro), window);
g_signal_connect (window, "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
void show_info(GtkWidget *widget, gpointer window)
{
GtkWidget *dialog;
dialog = gtk_message_dialog_new(GTK_WINDOW(window),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_INFO,
GTK_BUTTONS_OK,
"Download Completed");
gtk_window_set_title (GTK_WINDOW(dialog),
"Information");
gtk_dialog_run (GTK_DIALOG(dialog));
gtk_widget_destroy (dialog);
}
void show_warn(GtkWidget *widget, gpointer window)
{
GtkWidget *dialog;
dialog = gtk_message_dialog_new(GTK_WINDOW(window),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_WARNING,
GTK_BUTTONS_OK,
"Unallowed operation");
gtk_window_set_title (GTK_WINDOW(dialog),
"Warning");
gtk_dialog_run (GTK_DIALOG(dialog));
gtk_widget_destroy (dialog);
}
void show_ques(GtkWidget *widget, gpointer window)
{
GtkWidget *dialog;
dialog = gtk_message_dialog_new(GTK_WINDOW(window),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_QUESTION,
GTK_BUTTONS_YES_NO,
"Are you sure to quit?");
gtk_window_set_title (GTK_WINDOW(dialog),
"Question");
gtk_dialog_run (GTK_DIALOG(dialog));
gtk_widget_destroy (dialog);
}
void show_erro(GtkWidget *widget, gpointer window)
{
GtkWidget *dialog;
dialog = gtk_message_dialog_new(GTK_WINDOW(window),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_OK,
"Error loading file");
gtk_window_set_title (GTK_WINDOW(dialog),
"Error");
gtk_dialog_run (GTK_DIALOG(dialog));
gtk_widget_destroy (dialog);
}
#include <gtk/gtk.h>
GtkWidget *window;
void select_font();
int main(int argc, char *argv[])
{
GtkWidget *vbox;
GtkWidget *menubar;
GtkWidget *editmenu;
GtkWidget *editmenuitem;
GtkWidget *fontmenuitem;
GtkWidget *label;
gtk_init(&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL); // 0
gtk_window_set_title (GTK_WINDOW(window),
"Font dialog");
gtk_window_set_default_size (GTK_WINDOW(window), 320, 240);
gtk_window_set_position (GTK_WINDOW(window),
GTK_WIN_POS_CENTER); // 1
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0); // 1
gtk_container_add (GTK_CONTAINER(window), vbox);
menubar = gtk_menu_bar_new ();
editmenu
= gtk_menu_new ();
editmenuitem
= gtk_menu_item_new_with_label("Edit");
fontmenuitem
= gtk_menu_item_new_with_label("Font");
gtk_menu_shell_append (GTK_MENU_SHELL(menubar),
editmenuitem);
gtk_menu_item_set_submenu (GTK_MENU_ITEM(editmenuitem),
editmenu);
gtk_menu_shell_append (GTK_MENU_SHELL(editmenu),
fontmenuitem);
gtk_box_pack_start (GTK_BOX(vbox), menubar, 0, 0, 0);
/* 第3引数と第4引数の 0 は、FALSE という意味です */
label = gtk_label_new ("GTK Font dialog");
gtk_label_set_justify (GTK_LABEL(label),
GTK_JUSTIFY_CENTER);
gtk_box_pack_end (GTK_BOX(vbox), label, 1, 0, 0);
/* 第3引数の 1 はTRUE、第4引数の 0 はFALSEという意味です */
g_signal_connect (fontmenuitem, "activate",
G_CALLBACK(select_font), label);
g_signal_connect (window, "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
void select_font(GtkWidget *widget, gpointer label)
{
GtkWidget *dialog;
gint result;
GtkCssProvider *provider;
PangoFontDescription *desc;
const char *family;
gint size;
PangoStyle style;
PangoWeight weight;
GtkStyleContext *context;
dialog = gtk_font_chooser_dialog_new
("Select Font",
GTK_WINDOW(window));
result = gtk_dialog_run (GTK_DIALOG(dialog));
if (result == GTK_RESPONSE_OK || result == GTK_RESPONSE_APPLY)
/* GTK_RESPONSE_OKには-5、GTK_RESPONS_APPLEには-10が定義済されています*/
{
provider = gtk_css_provider_new();
desc = gtk_font_chooser_get_font_desc
(GTK_FONT_CHOOSER(dialog));
family = pango_font_description_get_family(desc);
size = pango_font_description_get_size(desc)/1000;
weight = pango_font_description_get_weight(desc);
style = pango_font_description_get_style(desc);
gchar *str = g_strdup_printf
("window label {font-family: \"%s\";\
font-size: %dpx;\
font-weight: %d;\
font-style: %s;}",
family, size, weight,
style ? "italic" : "normal");
gtk_css_provider_load_from_data(provider,str, -1, NULL);
context = gtk_widget_get_style_context(label);
gtk_style_context_add_provider(context,
GTK_STYLE_PROVIDER(provider),
GTK_STYLE_PROVIDER_PRIORITY_USER
);
g_free (str);
}
gtk_widget_destroy(dialog);
}
"window label {font-family: \"%s\";\ | ラベルはwindow labelになります。font-family:のあとに"と"で囲んで フォント名を指定します。文字列引数内で改行する場合は\が必要です。 |
font-size: %dpx;\ | font-size:のあとにフォントのサイズを指定します。サイズのあとにpx が必要です。 |
font-weight: %d;\ | font-weight:のあとにフォントの太さを指定します。PangoWeight型は もともと整数値です。 |
font-style: %s;}", | font-style:の あとにフォントのスタイルを表す、nomal、oblique、italic のどれかの文字列を指定します。 |
style ? "italic" : "normal"); | これは三項演算子と呼ばれるものです。?の前の式がTRUEなら
:の前の式が実行され、FALSEなら:の後ろの式が実行されます。 styleにはNormalの場合0が、Obliqueの場合1が、Italicの場合2 が入っています。0はFALSEと評価されますので"normal"が返されます。 1と2の場合には"italic"が返されます。 フォントデスクリプションではObliqueとItalicはともに2と評価されます。 |
GTK_STYLE_PROVIDER_PRIORITY_FALLBACK | 1 |
GTK_STYLE_PROVIDER_PRIORITY_THEME | 200 |
GTK_STYLE_PROVIDER_PRIORITY_SETTINGS | 400 | GTK_STYLE_PROVIDER_PRIORITY_APPLICATION | 600 |
GTK_STYLE_PROVIDER_PRIORITY_USER | 800 |
#include <gtk/gtk.h>
GtkWidget *window;
void select_font();
int main(int argc, char *argv[])
{
GtkWidget *vbox;
GtkWidget *toolbar;
GtkToolItem *font;
GtkWidget *label;
gtk_init(&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL); // 0
gtk_window_set_title (GTK_WINDOW(window),
"Font dialog");
gtk_window_set_default_size (GTK_WINDOW(window), 320, 240);
gtk_window_set_position (GTK_WINDOW(window),
GTK_WIN_POS_CENTER); // 1
//gtk_container_set_border_width (GTK_CONTAINER(window), 15);
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0); // 1
gtk_container_add (GTK_CONTAINER(window), vbox);
toolbar = gtk_toolbar_new ();
gtk_toolbar_set_style (GTK_TOOLBAR(toolbar),
GTK_TOOLBAR_ICONS);
font = gtk_tool_button_new_from_stock
(GTK_STOCK_SELECT_FONT);
gtk_toolbar_insert (GTK_TOOLBAR(toolbar), font, -1);
gtk_box_pack_start (GTK_BOX(vbox), toolbar, 0, 0, 0);
/* 第3引数と第4引数の 0 は、FALSE という意味です */
label = gtk_label_new ("GTK Font dialog");
gtk_label_set_justify (GTK_LABEL(label),
GTK_JUSTIFY_CENTER);
gtk_box_pack_end (GTK_BOX(vbox), label, 1, 0, 0);
/* 第3引数の 1 はTRUE、第4引数の 0 はFALSEという意味です */
g_signal_connect (font, "clicked",
G_CALLBACK(select_font), label);
g_signal_connect (window, "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
void select_font(GtkWidget *widget, gpointer label)
{
GtkWidget *dialog;
gint result;
gchar *fontname;
PangoFontDescription *fontdesc;
dialog = gtk_font_chooser_dialog_new
("Select Font",
GTK_WINDOW(window));
result = gtk_dialog_run (GTK_DIALOG(dialog));
if (result == GTK_RESPONSE_OK || result == GTK_RESPONSE_APPLY)
/* GTK_RESPONSE_OKには-5、GTK_RESPONS_APPLEには-10が定義済されています*/
{
fontname = gtk_font_chooser_get_font
(GTK_FONT_CHOOSER(dialog));
fontdesc = pango_font_description_from_string
(fontname);
gtk_widget_override_font (GTK_WIDGET(label), fontdesc);
g_free (fontname);
}
gtk_widget_destroy(dialog);
}
#include <gtk/gtk.h>
GdkRGBA color;
void select_color();
void draw_callback();
void select_color();
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *menubar;
GtkWidget *editmenu;
GtkWidget *editmenuitem;
GtkWidget *colormenuitem;
GtkWidget *darea;
GtkWidget *frame;
gtk_init(&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL); // 0
gtk_window_set_title (GTK_WINDOW(window),
"Color dialog");
gtk_window_set_default_size (GTK_WINDOW(window), 320, 240);
gtk_window_set_position (GTK_WINDOW(window),
GTK_WIN_POS_CENTER); // 1
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
gtk_container_add (GTK_CONTAINER(window), vbox);
menubar = gtk_menu_bar_new ();
editmenu
= gtk_menu_new ();
editmenuitem
= gtk_menu_item_new_with_label("Edit");
colormenuitem
= gtk_menu_item_new_with_label("Color");
gtk_menu_shell_append (GTK_MENU_SHELL(menubar),
editmenuitem);
gtk_menu_item_set_submenu (GTK_MENU_ITEM(editmenuitem),
editmenu);
gtk_menu_shell_append (GTK_MENU_SHELL(editmenu),
colormenuitem);
darea = gtk_drawing_area_new ();
frame = gtk_frame_new (NULL);
gtk_container_set_border_width (GTK_CONTAINER(frame), 15);
gtk_container_add (GTK_CONTAINER(frame), darea);
gtk_box_pack_end (GTK_BOX(vbox), frame, 1, 1, 0);
/* 第3引数と第4引数の 1 は、TRUE の意味です。第5引数は数値の 0 です */
gtk_box_pack_start (GTK_BOX(vbox), menubar, 0, 0, 0);
/* 第3引数と第4引数の 0 は、FALSEの意味です。第5引数は数値の 0 です */
gdk_rgba_parse (&color ,"rgb(127, 127, 127)");
g_signal_connect (darea, "draw",
G_CALLBACK(draw_callback), NULL);
g_signal_connect (colormenuitem, "activate",
G_CALLBACK(select_color), window);
g_signal_connect (window, "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
void draw_callback(GtkWidget *darea, cairo_t *cr)
{
gdk_cairo_set_source_rgba (cr, &color);
cairo_paint (cr);
}
void select_color(GtkWidget *widget, gpointer window)
{
GtkWidget *dialog;
gint result;
dialog = gtk_color_chooser_dialog_new
("Select color",
GTK_WINDOW(window));
result = gtk_dialog_run (GTK_DIALOG(dialog));
//printf("%d\n", GTK_RESPONSE_OK);
if (result == GTK_RESPONSE_OK)
{
gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER(dialog),
&color);
gtk_widget_queue_draw (window);
}
gtk_widget_destroy (dialog);
}
#include <gtk/gtk.h>
GdkRGBA color;
void select_color();
void draw_callback();
void select_color();
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *toolbar;
GtkToolItem *coloritem;
GtkWidget *darea;
GtkWidget *frame;
gtk_init(&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL); // 0
gtk_window_set_title (GTK_WINDOW(window),
"Color dialog");
gtk_window_set_default_size (GTK_WINDOW(window), 320, 240);
gtk_window_set_position (GTK_WINDOW(window),
GTK_WIN_POS_CENTER); // 1
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
gtk_container_add (GTK_CONTAINER(window), vbox);
toolbar = gtk_toolbar_new ();
gtk_box_pack_start (GTK_BOX(vbox), toolbar, 0, 0, 0);
/* 第3引数と第4引数の 0 は、FALSEの意味です。第5引数は数値の 0 です */
coloritem
= gtk_tool_button_new_from_stock
(GTK_STOCK_SELECT_COLOR);
/*この関数とGTK_STOCK定数は非推奨(deprecated)です。実行に問題はありません*/
gtk_toolbar_insert (GTK_TOOLBAR(toolbar), coloritem,
-1);
darea = gtk_drawing_area_new ();
frame = gtk_frame_new (NULL);
gtk_container_set_border_width (GTK_CONTAINER(frame), 15);
gtk_container_add (GTK_CONTAINER(frame), darea);
gtk_box_pack_end (GTK_BOX(vbox), frame, 1, 1, 0);
/* 第3引数と第4引数の 1 は、TRUE の意味です。第5引数は数値の 0 です */
gdk_rgba_parse (&color ,"rgb(127, 127, 127)");
g_signal_connect (darea, "draw",
G_CALLBACK(draw_callback), NULL);
g_signal_connect (coloritem, "clicked",
G_CALLBACK(select_color), window);
g_signal_connect (window, "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
void draw_callback(GtkWidget *darea, cairo_t *cr)
{
gdk_cairo_set_source_rgba (cr, &color);
cairo_paint (cr);
}
void select_color(GtkWidget *widget, gpointer window)
{
GtkWidget *dialog;
gint result;
dialog = gtk_color_chooser_dialog_new
("Select color",
GTK_WINDOW(window));
result = gtk_dialog_run (GTK_DIALOG(dialog));
if (result == GTK_RESPONSE_OK)
{
gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER(dialog),
&color);
gtk_widget_queue_draw (window);
}
gtk_widget_destroy (dialog);
}
#include <gtk/gtk.h>
GtkWidget *label;
void select_file();
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *menubar;
GtkWidget *filemenu;
GtkWidget *filemenuitem;
GtkWidget *openmenuitem;
gtk_init(&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL); // 0
gtk_window_set_title (GTK_WINDOW(window),
"File dialog");
gtk_window_set_default_size (GTK_WINDOW(window), 320, 240);
gtk_window_set_position (GTK_WINDOW(window),
GTK_WIN_POS_CENTER); // 1
vbox = gtk_box_new (1, 0); // 1
/* GTK_ORIENTATION_VERTICAL定数の値は 1 です */
gtk_container_add (GTK_CONTAINER(window), vbox);
menubar = gtk_menu_bar_new ();
filemenu
= gtk_menu_new ();
filemenuitem
= gtk_menu_item_new_with_label("File");
openmenuitem
= gtk_menu_item_new_with_label("Open");
gtk_menu_shell_append (GTK_MENU_SHELL(menubar),
filemenuitem);
gtk_menu_item_set_submenu (GTK_MENU_ITEM(filemenuitem),
filemenu);
gtk_menu_shell_append (GTK_MENU_SHELL(filemenu),
openmenuitem);
label = gtk_label_new ("未選択");
gtk_label_set_justify (GTK_LABEL(label),
GTK_JUSTIFY_CENTER); // 2
gtk_box_pack_start (GTK_BOX(vbox), menubar, 0, 0, 0);
gtk_box_pack_end (GTK_BOX(vbox), label, 1, 0, 0);
/* 第3引数と第4引数の0はFALSE、1はTRUEの意味です。第5引数は数値の0です */
g_signal_connect (openmenuitem, "activate",
G_CALLBACK(select_file), window);
g_signal_connect (window, "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
void select_file(GtkWidget *widget, gpointer window)
{
GtkWidget *dialog;
gint result;
dialog = gtk_file_chooser_dialog_new
("Select File",
window,
GTK_FILE_CHOOSER_ACTION_OPEN,
/* 保存ダイアログの場合は、GTK_FILE_CHOOSER_ACTION_SAVE にします */
("Cancel"),
GTK_RESPONSE_CANCEL,
("Select"),
GTK_RESPONSE_ACCEPT,
NULL);
result = gtk_dialog_run (GTK_DIALOG(dialog));
if (result == GTK_RESPONSE_ACCEPT)
/* GTK_RESPONSE_ACCEPT定数は -3 です */
{
char *filename;
GtkFileChooser *chooser
= GTK_FILE_CHOOSER(dialog);
filename
= gtk_file_chooser_get_filename
(chooser);
gtk_label_set_text (GTK_LABEL(label), filename);
g_free (filename);
} else {
gtk_label_set_text (GTK_LABEL(label), "未選択");
}
gtk_widget_destroy (dialog);
}
#include <gtk/gtk.h>
GtkWidget *label;
void select_file();
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *toolbar;
GtkToolItem *fileitem;
gtk_init(&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL); // 0
gtk_window_set_title (GTK_WINDOW(window),
"File dialog");
gtk_window_set_default_size (GTK_WINDOW(window), 320, 240);
gtk_window_set_position (GTK_WINDOW(window),
GTK_WIN_POS_CENTER); // 1
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0); // 1
gtk_container_add (GTK_CONTAINER(window), vbox);
toolbar = gtk_toolbar_new ();
gtk_box_pack_start (GTK_BOX(vbox), toolbar, 0, 0, 0);
/* 第3引数と第4引数の 0 は、FALSEの意味です。第5引数は数値の 0 です */
fileitem
= gtk_tool_button_new_from_stock
(GTK_STOCK_FILE);
/*この関数とGTK_STOCK定数は非推奨(deprecated)です。実行に問題はありません*/
/*macOSでは、GTK_STOCK_FILEは読み込めません */
gtk_toolbar_insert (GTK_TOOLBAR(toolbar), fileitem,
-1);
label = gtk_label_new ("未選択");
gtk_label_set_justify (GTK_LABEL(label),
GTK_JUSTIFY_CENTER); // 2
gtk_box_pack_end (GTK_BOX(vbox), label, 1, 0, 0);
/*第3引数の 1 はTRUE、第4引数の 0 はFALSE、第5引数は数値の0という意味です*/
g_signal_connect (fileitem, "clicked",
G_CALLBACK(select_file), window);
g_signal_connect (window, "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
void select_file(GtkWidget *widget, gpointer window)
{
GtkWidget *dialog;
gint result;
dialog = gtk_file_chooser_dialog_new
("Select File",
window,
GTK_FILE_CHOOSER_ACTION_OPEN,
/* 保存ダイアログの場合は、GTK_FILE_CHOOSER_ACTION_SAVE にします */
("Cancel"),
GTK_RESPONSE_CANCEL,
("Select"),
GTK_RESPONSE_ACCEPT,
NULL);
result = gtk_dialog_run (GTK_DIALOG(dialog));
if (result == GTK_RESPONSE_ACCEPT)
/* GTK_RESPONSE_ACCEPT定数は -3 です */
{
char *filename;
GtkFileChooser *chooser
= GTK_FILE_CHOOSER(dialog);
filename
= gtk_file_chooser_get_filename
(chooser);
gtk_label_set_text (GTK_LABEL(label), filename);
g_free (filename);
} else {
gtk_label_set_text (GTK_LABEL(label), "未選択");
}
gtk_widget_destroy (dialog);
}