1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
|
/* Application main window
*
* Demonstrates a typical application window with menubar, toolbar, statusbar.
*/
#include <gtk/gtk.h>
#include "config.h"
#include "demo-common.h"
static GtkWidget *window = NULL;
static GtkWidget *infobar = NULL;
static GtkWidget *messagelabel = NULL;
static void
activate_action (GtkAction *action)
{
const gchar *name = gtk_action_get_name (action);
const gchar *typename = G_OBJECT_TYPE_NAME (action);
GtkWidget *dialog;
if (g_str_equal (name, "DarkTheme"))
{
gboolean value = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
GtkSettings *settings = gtk_settings_get_default ();
g_object_set (G_OBJECT (settings),
"gtk-application-prefer-dark-theme", value,
NULL);
return;
}
dialog = gtk_message_dialog_new (GTK_WINDOW (window),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_INFO,
GTK_BUTTONS_CLOSE,
"You activated action: \"%s\" of type \"%s\"",
name, typename);
/* Close dialog on user response */
g_signal_connect (dialog,
"response",
G_CALLBACK (gtk_widget_destroy),
NULL);
gtk_widget_show (dialog);
}
static void
activate_radio_action (GtkAction *action, GtkRadioAction *current)
{
const gchar *name = gtk_action_get_name (GTK_ACTION (current));
const gchar *typename = G_OBJECT_TYPE_NAME (GTK_ACTION (current));
gboolean active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (current));
gint value = gtk_radio_action_get_current_value (GTK_RADIO_ACTION (current));
if (active)
{
gchar *text;
text = g_strdup_printf ("You activated radio action: \"%s\" of type \"%s\".\n"
"Current value: %d",
name, typename, value);
gtk_label_set_text (GTK_LABEL (messagelabel), text);
gtk_info_bar_set_message_type (GTK_INFO_BAR (infobar), (GtkMessageType)value);
gtk_widget_show (infobar);
g_free (text);
}
}
static void
activate_email (GtkAboutDialog *about,
const gchar *link,
gpointer data)
{
gchar *text;
text = g_strdup_printf ("send mail to %s", link);
g_print ("%s\n", text);
g_free (text);
}
static void
activate_url (GtkAboutDialog *about,
const gchar *link,
gpointer data)
{
g_print ("show url %s\n", link);
}
static void
about_cb (GtkAction *action,
GtkWidget *window)
{
GdkPixbuf *pixbuf, *transparent;
gchar *filename;
const gchar *authors[] = {
"Peter Mattis",
"Spencer Kimball",
"Josh MacDonald",
"and many more...",
NULL
};
const gchar *documentors[] = {
"Owen Taylor",
"Tony Gale",
"Matthias Clasen <mclasen@redhat.com>",
"and many more...",
NULL
};
pixbuf = NULL;
transparent = NULL;
filename = demo_find_file ("gtk-logo-rgb.gif", NULL);
if (filename)
{
pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
g_free (filename);
transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff);
g_object_unref (pixbuf);
}
gtk_about_dialog_set_email_hook (activate_email, NULL, NULL);
gtk_about_dialog_set_url_hook (activate_url, NULL, NULL);
gtk_show_about_dialog (GTK_WINDOW (window),
"program-name", "GTK+ Code Demos",
"version", g_strdup_printf ("%s,\nRunning against GTK+ %d.%d.%d",
PACKAGE_VERSION,
gtk_get_major_version (),
gtk_get_minor_version (),
gtk_get_micro_version ()),
"copyright", "(C) 1997-2009 The GTK+ Team",
"license-type", GTK_LICENSE_LGPL_2_1,
"website", "http://www.gtk.org",
"comments", "Program to demonstrate GTK+ functions.",
"authors", authors,
"documenters", documentors,
"logo", transparent,
"title", "About GTK+ Code Demos",
NULL);
g_object_unref (transparent);
}
typedef struct
{
GtkAction action;
} ToolMenuAction;
typedef struct
{
GtkActionClass parent_class;
} ToolMenuActionClass;
G_DEFINE_TYPE(ToolMenuAction, tool_menu_action, GTK_TYPE_ACTION)
static void
tool_menu_action_class_init (ToolMenuActionClass *class)
{
GTK_ACTION_CLASS (class)->toolbar_item_type = GTK_TYPE_MENU_TOOL_BUTTON;
}
static void
tool_menu_action_init (ToolMenuAction *action)
{
}
static GtkActionEntry entries[] = {
{ "FileMenu", NULL, "_File" }, /* name, stock id, label */
{ "OpenMenu", NULL, "_Open" }, /* name, stock id, label */
{ "PreferencesMenu", NULL, "_Preferences" }, /* name, stock id, label */
{ "ColorMenu", NULL, "_Color" }, /* name, stock id, label */
{ "ShapeMenu", NULL, "_Shape" }, /* name, stock id, label */
{ "HelpMenu", NULL, "_Help" }, /* name, stock id, label */
{ "New", GTK_STOCK_NEW, /* name, stock id */
"_New", "<control>N", /* label, accelerator */
"Create a new file", /* tooltip */
G_CALLBACK (activate_action) },
{ "File1", NULL, /* name, stock id */
"File1", NULL, /* label, accelerator */
"Open first file", /* tooltip */
G_CALLBACK (activate_action) },
{ "Save", GTK_STOCK_SAVE, /* name, stock id */
"_Save","<control>S", /* label, accelerator */
"Save current file", /* tooltip */
G_CALLBACK (activate_action) },
{ "SaveAs", GTK_STOCK_SAVE, /* name, stock id */
"Save _As...", NULL, /* label, accelerator */
"Save to a file", /* tooltip */
G_CALLBACK (activate_action) },
{ "Quit", GTK_STOCK_QUIT, /* name, stock id */
"_Quit", "<control>Q", /* label, accelerator */
"Quit", /* tooltip */
G_CALLBACK (activate_action) },
{ "About", NULL, /* name, stock id */
"_About", "<control>A", /* label, accelerator */
"About", /* tooltip */
G_CALLBACK (about_cb) },
{ "Logo", "demo-gtk-logo", /* name, stock id */
NULL, NULL, /* label, accelerator */
"GTK+", /* tooltip */
G_CALLBACK (activate_action) },
};
static guint n_entries = G_N_ELEMENTS (entries);
static GtkToggleActionEntry toggle_entries[] = {
{ "Bold", GTK_STOCK_BOLD, /* name, stock id */
"_Bold", "<control>B", /* label, accelerator */
"Bold", /* tooltip */
G_CALLBACK (activate_action),
TRUE }, /* is_active */
{ "DarkTheme", NULL, /* name, stock id */
"_Prefer Dark Theme", NULL, /* label, accelerator */
"Prefer Dark Theme", /* tooltip */
G_CALLBACK (activate_action),
FALSE }, /* is_active */
};
static guint n_toggle_entries = G_N_ELEMENTS (toggle_entries);
enum {
COLOR_RED,
COLOR_GREEN,
COLOR_BLUE
};
static GtkRadioActionEntry color_entries[] = {
{ "Red", NULL, /* name, stock id */
"_Red", "<control>R", /* label, accelerator */
"Blood", COLOR_RED }, /* tooltip, value */
{ "Green", NULL, /* name, stock id */
"_Green", "<control>G", /* label, accelerator */
"Grass", COLOR_GREEN }, /* tooltip, value */
{ "Blue", NULL, /* name, stock id */
"_Blue", "<control>B", /* label, accelerator */
"Sky", COLOR_BLUE }, /* tooltip, value */
};
static guint n_color_entries = G_N_ELEMENTS (color_entries);
enum {
SHAPE_SQUARE,
SHAPE_RECTANGLE,
SHAPE_OVAL
};
static GtkRadioActionEntry shape_entries[] = {
{ "Square", NULL, /* name, stock id */
"_Square", "<control>S", /* label, accelerator */
"Square", SHAPE_SQUARE }, /* tooltip, value */
{ "Rectangle", NULL, /* name, stock id */
"_Rectangle", "<control>R", /* label, accelerator */
"Rectangle", SHAPE_RECTANGLE }, /* tooltip, value */
{ "Oval", NULL, /* name, stock id */
"_Oval", "<control>O", /* label, accelerator */
"Egg", SHAPE_OVAL }, /* tooltip, value */
};
static guint n_shape_entries = G_N_ELEMENTS (shape_entries);
static const gchar *ui_info =
"<ui>"
" <menubar name='MenuBar'>"
" <menu action='FileMenu'>"
" <menuitem action='New'/>"
" <menuitem action='Open'/>"
" <menuitem action='Save'/>"
" <menuitem action='SaveAs'/>"
" <separator/>"
" <menuitem action='Quit'/>"
" </menu>"
" <menu action='PreferencesMenu'>"
" <menuitem action='DarkTheme'/>"
" <menu action='ColorMenu'>"
" <menuitem action='Red'/>"
" <menuitem action='Green'/>"
" <menuitem action='Blue'/>"
" </menu>"
" <menu action='ShapeMenu'>"
" <menuitem action='Square'/>"
" <menuitem action='Rectangle'/>"
" <menuitem action='Oval'/>"
" </menu>"
" <menuitem action='Bold'/>"
" </menu>"
" <menu action='HelpMenu'>"
" <menuitem action='About'/>"
" </menu>"
" </menubar>"
" <toolbar name='ToolBar'>"
" <toolitem action='Open'>"
" <menu action='OpenMenu'>"
" <menuitem action='File1'/>"
" </menu>"
" </toolitem>"
" <toolitem action='Quit'/>"
" <separator action='Sep1'/>"
" <toolitem action='Logo'/>"
" </toolbar>"
"</ui>";
/* This function registers our custom toolbar icons, so they can be themed.
*
* It's totally optional to do this, you could just manually insert icons
* and have them not be themeable, especially if you never expect people
* to theme your app.
*/
static void
register_stock_icons (void)
{
static gboolean registered = FALSE;
if (!registered)
{
GdkPixbuf *pixbuf;
GtkIconFactory *factory;
char *filename;
static GtkStockItem items[] = {
{ "demo-gtk-logo",
"_GTK!",
0, 0, NULL }
};
registered = TRUE;
/* Register our stock items */
gtk_stock_add (items, G_N_ELEMENTS (items));
/* Add our custom icon factory to the list of defaults */
factory = gtk_icon_factory_new ();
gtk_icon_factory_add_default (factory);
/* demo_find_file() looks in the current directory first,
* so you can run gtk-demo without installing GTK, then looks
* in the location where the file is installed.
*/
pixbuf = NULL;
filename = demo_find_file ("gtk-logo-rgb.gif", NULL);
if (filename)
{
pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
g_free (filename);
}
/* Register icon to accompany stock item */
if (pixbuf != NULL)
{
GtkIconSet *icon_set;
GdkPixbuf *transparent;
/* The gtk-logo-rgb icon has a white background, make it transparent */
transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff);
icon_set = gtk_icon_set_new_from_pixbuf (transparent);
gtk_icon_factory_add (factory, "demo-gtk-logo", icon_set);
gtk_icon_set_unref (icon_set);
g_object_unref (pixbuf);
g_object_unref (transparent);
}
else
g_warning ("failed to load GTK logo for toolbar");
/* Drop our reference to the factory, GTK will hold a reference. */
g_object_unref (factory);
}
}
static void
update_statusbar (GtkTextBuffer *buffer,
GtkStatusbar *statusbar)
{
gchar *msg;
gint row, col;
gint count;
GtkTextIter iter;
gtk_statusbar_pop (statusbar, 0); /* clear any previous message,
* underflow is allowed
*/
count = gtk_text_buffer_get_char_count (buffer);
gtk_text_buffer_get_iter_at_mark (buffer,
&iter,
gtk_text_buffer_get_insert (buffer));
row = gtk_text_iter_get_line (&iter);
col = gtk_text_iter_get_line_offset (&iter);
msg = g_strdup_printf ("Cursor at row %d column %d - %d chars in document",
row, col, count);
gtk_statusbar_push (statusbar, 0, msg);
g_free (msg);
}
static void
mark_set_callback (GtkTextBuffer *buffer,
const GtkTextIter *new_location,
GtkTextMark *mark,
gpointer data)
{
update_statusbar (buffer, GTK_STATUSBAR (data));
}
static void
update_resize_grip (GtkWidget *widget,
GdkEventWindowState *event,
GtkStatusbar *statusbar)
{
if (event->changed_mask & (GDK_WINDOW_STATE_MAXIMIZED |
GDK_WINDOW_STATE_FULLSCREEN))
{
gboolean maximized;
maximized = event->new_window_state & (GDK_WINDOW_STATE_MAXIMIZED |
GDK_WINDOW_STATE_FULLSCREEN);
gtk_statusbar_set_has_resize_grip (statusbar, !maximized);
}
}
GtkWidget *
do_appwindow (GtkWidget *do_widget)
{
if (!window)
{
GtkWidget *table;
GtkWidget *statusbar;
GtkWidget *contents;
GtkWidget *sw;
GtkWidget *bar;
GtkTextBuffer *buffer;
GtkActionGroup *action_group;
GtkAction *open_action;
GtkUIManager *merge;
GError *error = NULL;
register_stock_icons ();
/* Create the toplevel window
*/
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_screen (GTK_WINDOW (window),
gtk_widget_get_screen (do_widget));
gtk_window_set_title (GTK_WINDOW (window), "Application Window");
gtk_window_set_icon_name (GTK_WINDOW (window), "document-open");
/* NULL window variable when window is closed */
g_signal_connect (window, "destroy",
G_CALLBACK (gtk_widget_destroyed),
&window);
table = gtk_table_new (1, 5, FALSE);
gtk_container_add (GTK_CONTAINER (window), table);
/* Create the menubar and toolbar
*/
action_group = gtk_action_group_new ("AppWindowActions");
open_action = g_object_new (tool_menu_action_get_type (),
"name", "Open",
"label", "_Open",
"tooltip", "Open a file",
"stock-id", GTK_STOCK_OPEN,
NULL);
gtk_action_group_add_action (action_group, open_action);
g_object_unref (open_action);
gtk_action_group_add_actions (action_group,
entries, n_entries,
window);
gtk_action_group_add_toggle_actions (action_group,
toggle_entries, n_toggle_entries,
NULL);
gtk_action_group_add_radio_actions (action_group,
color_entries, n_color_entries,
COLOR_RED,
G_CALLBACK (activate_radio_action),
NULL);
gtk_action_group_add_radio_actions (action_group,
shape_entries, n_shape_entries,
SHAPE_SQUARE,
G_CALLBACK (activate_radio_action),
NULL);
merge = gtk_ui_manager_new ();
g_object_set_data_full (G_OBJECT (window), "ui-manager", merge,
g_object_unref);
gtk_ui_manager_insert_action_group (merge, action_group, 0);
gtk_window_add_accel_group (GTK_WINDOW (window),
gtk_ui_manager_get_accel_group (merge));
if (!gtk_ui_manager_add_ui_from_string (merge, ui_info, -1, &error))
{
g_message ("building menus failed: %s", error->message);
g_error_free (error);
}
bar = gtk_ui_manager_get_widget (merge, "/MenuBar");
gtk_widget_show (bar);
gtk_table_attach (GTK_TABLE (table),
bar,
/* X direction */ /* Y direction */
0, 1, 0, 1,
GTK_EXPAND | GTK_FILL, 0,
0, 0);
bar = gtk_ui_manager_get_widget (merge, "/ToolBar");
gtk_widget_show (bar);
gtk_table_attach (GTK_TABLE (table),
bar,
/* X direction */ /* Y direction */
0, 1, 1, 2,
GTK_EXPAND | GTK_FILL, 0,
0, 0);
/* Create document
*/
infobar = gtk_info_bar_new ();
gtk_widget_set_no_show_all (infobar, TRUE);
messagelabel = gtk_label_new ("");
gtk_widget_show (messagelabel);
gtk_box_pack_start (GTK_BOX (gtk_info_bar_get_content_area (GTK_INFO_BAR (infobar))),
messagelabel,
TRUE, TRUE, 0);
gtk_info_bar_add_button (GTK_INFO_BAR (infobar),
GTK_STOCK_OK, GTK_RESPONSE_OK);
g_signal_connect (infobar, "response",
G_CALLBACK (gtk_widget_hide), NULL);
gtk_table_attach (GTK_TABLE (table),
infobar,
/* X direction */ /* Y direction */
0, 1, 2, 3,
GTK_EXPAND | GTK_FILL, 0,
0, 0);
sw = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
GTK_POLICY_AUTOMATIC,
GTK_POLICY_AUTOMATIC);
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw),
GTK_SHADOW_IN);
gtk_table_attach (GTK_TABLE (table),
sw,
/* X direction */ /* Y direction */
0, 1, 3, 4,
GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL,
0, 0);
gtk_window_set_default_size (GTK_WINDOW (window),
200, 200);
contents = gtk_text_view_new ();
gtk_widget_grab_focus (contents);
gtk_container_add (GTK_CONTAINER (sw),
contents);
/* Create statusbar */
statusbar = gtk_statusbar_new ();
gtk_table_attach (GTK_TABLE (table),
statusbar,
/* X direction */ /* Y direction */
0, 1, 4, 5,
GTK_EXPAND | GTK_FILL, 0,
0, 0);
/* Show text widget info in the statusbar */
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (contents));
g_signal_connect_object (buffer,
"changed",
G_CALLBACK (update_statusbar),
statusbar,
0);
g_signal_connect_object (buffer,
"mark_set", /* cursor moved */
G_CALLBACK (mark_set_callback),
statusbar,
0);
g_signal_connect_object (window,
"window_state_event",
G_CALLBACK (update_resize_grip),
statusbar,
0);
update_statusbar (buffer, GTK_STATUSBAR (statusbar));
}
if (!gtk_widget_get_visible (window))
{
gtk_widget_show_all (window);
}
else
{
gtk_widget_destroy (window);
window = NULL;
infobar = NULL;
messagelabel = NULL;
}
return window;
}
|