この章では、各種ダイアログを説明します。
#include
void activate();
void show_info();
void show_warn();
void show_ques();
void show_erro();
int main(int argc, char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("jp.vivacocoa.messagedialog", 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 *grid;
GtkWidget *info;
GtkWidget *warn;
GtkWidget *ques;
GtkWidget *erro;
/* Window */
window =
gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW(window),
"MessageDialog" );
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 */
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 );
/* Buttons */
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" );
/* Attach to grid */
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);
/* Signals */
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 );
gtk_widget_show_all (window);
}
void show_info(GtkWidget *widget, gpointer window)
{
GtkWidget *dialog
= gtk_message_dialog_new (window, 2, 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
= gtk_message_dialog_new (window, 2, 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
= gtk_message_dialog_new (window, 2, 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
= gtk_message_dialog_new (window, 2, 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);
}
GTK_DIALOG_MODAL | 値は1です |
GTK_DIALOG_DESTROY_WITH_PARENT | 値は2です |
GTK_DIALOG_USE_HEADER_BAR | 値は4です |
#include <gtk/gtk.h>
GtkWidget *label;
void activate();
void select_file();
int main(int argc, char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("jp.vivacocoa.filedialog", 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 *menubar;
GtkWidget *filemenu;
GtkWidget *fileitem;
GtkWidget *openitem;
/* Window */
window =
gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW(window),
"File dialog" );
gtk_window_set_default_size (GTK_WINDOW(window), 300, 200);
gtk_window_set_position (GTK_WINDOW(window), 1 );
/* Container */
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0 );
gtk_container_add (GTK_CONTAINER(window), vbox);
/* MenuBar */
menubar = gtk_menu_bar_new ();
filemenu
= gtk_menu_new ();
fileitem
= gtk_menu_item_new_with_label("File");
openitem
= gtk_menu_item_new_with_label("Open");
gtk_menu_shell_append (GTK_MENU_SHELL(menubar),
fileitem);
gtk_menu_shell_append (GTK_MENU_SHELL(filemenu),
openitem);
gtk_menu_item_set_submenu (GTK_MENU_ITEM(fileitem),
filemenu);
/* Label */
label = gtk_label_new ("Unselected");
gtk_label_set_justify (GTK_LABEL(label),
GTK_JUSTIFY_CENTER);
/* Pack to box */
gtk_box_pack_start (GTK_BOX(vbox), menubar, 0, 0, 0);
gtk_box_pack_end (GTK_BOX(vbox), label , 1, 0, 0);
/* Signal */
g_signal_connect (openitem, "activate",
G_CALLBACK(select_file), window);
gtk_widget_show_all (window);
}
void select_file(GtkWidget *widget, gpointer window)
{
gint result;
GtkWidget *dialog
= gtk_file_chooser_dialog_new ("Selec File",
window,
GTK_FILE_CHOOSER_ACTION_OPEN,
("Cancel"),
GTK_RESPONSE_CANCEL,
("Open"),
GTK_RESPONSE_ACCEPT,
NULL );
result = gtk_dialog_run (GTK_DIALOG(dialog) );
if (result == GTK_RESPONSE_ACCEPT)
{
char *filename;
char *basename;
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), "Unselected");
}
gtk_widget_destroy (dialog);
}
#include <gtk/gtk.h>
GtkWidget *window;
void activate();
void select_font();
int main(int argc, char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("jp.vivacocoa.fontdialog", 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 *vbox;
GtkWidget *menubar;
GtkWidget *editmenu;
GtkWidget *edititem;
GtkWidget *fontitem;
GtkWidget *label;
/* Window */
window =
gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW(window),
"Font dialog" );
gtk_window_set_default_size (GTK_WINDOW(window), 300, 200);
gtk_window_set_position (GTK_WINDOW(window), 1 );
/* Container */
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0 );
gtk_container_add (GTK_CONTAINER(window), vbox);
/* MenuBar */
menubar = gtk_menu_bar_new ();
editmenu
= gtk_menu_new ();
edititem
= gtk_menu_item_new_with_label("Edit");
fontitem
= gtk_menu_item_new_with_label("Font");
gtk_menu_shell_append (GTK_MENU_SHELL(menubar),
edititem);
gtk_menu_shell_append (GTK_MENU_SHELL(editmenu),
fontitem);
gtk_menu_item_set_submenu (GTK_MENU_ITEM(edititem),
editmenu);
/* Label */
label = gtk_label_new ("Gtk Font Dialog");
gtk_label_set_justify (GTK_LABEL(label),
GTK_JUSTIFY_CENTER);
/* Pack to box */
gtk_box_pack_start (GTK_BOX(vbox), menubar, 0, 0, 0);
gtk_box_pack_end (GTK_BOX(vbox), label , 1, 0, 0);
/* Signal */
g_signal_connect (fontitem, "activate",
G_CALLBACK(select_font), label );
/* Show window */
gtk_widget_show_all (window);
}
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)
{
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);
}
Comic SansというフォントはmacOSだけだと思います。
"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>
GdkRGBA color;
void activate();
void draw_callback();
void select_color();
int main(int argc, char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("jp.vivacocoa.colordialog", 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 *menubar;
GtkWidget *editmenu;
GtkWidget *edititem;
GtkWidget *coloritem;
GtkWidget *darea;
/* Window */
window =
gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW(window),
"Color dialog" );
gtk_window_set_default_size (GTK_WINDOW(window), 300, 200);
gtk_window_set_position (GTK_WINDOW(window), 1 );
/* Container */
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0 );
gtk_container_add (GTK_CONTAINER(window), vbox);
/* Menubar */
menubar = gtk_menu_bar_new ();
editmenu
= gtk_menu_new ();
edititem
= gtk_menu_item_new_with_label("Edit");
coloritem
= gtk_menu_item_new_with_label("Color");
gtk_menu_shell_append (GTK_MENU_SHELL(menubar),
edititem);
gtk_menu_shell_append (GTK_MENU_SHELL(editmenu),
coloritem);
gtk_menu_item_set_submenu (GTK_MENU_ITEM(edititem),
editmenu);
/* Drawing area */
darea = gtk_drawing_area_new ();
gdk_rgba_parse (&color, "rgb(127, 127, 127)" );
/* Pack to box */
gtk_box_pack_start (GTK_BOX(vbox), menubar, 0, 0, 0);
gtk_box_pack_end (GTK_BOX(vbox), darea , 1, 1, 0);
/* Signals */
g_signal_connect (darea, "draw",
G_CALLBACK(draw_callback), NULL);
g_signal_connect (coloritem, "activate",
G_CALLBACK(select_color), window);
/* Show Window */
gtk_widget_show_all (window);
}
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", 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);
}