File: sec-menus-examples.html

package info (click to toggle)
gtkmm-documentation 4.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,772 kB
  • sloc: cpp: 15,541; javascript: 1,208; makefile: 1,080; python: 401; xml: 106; perl: 67; sh: 8
file content (947 lines) | stat: -rw-r--r-- 33,345 bytes parent folder | download
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="highlight.min.css">
<script src="highlight.min.js"></script><script>
      hljs.configure({languages: ['cpp']});
      hljs.highlightAll();
    </script><title>Examples</title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
<link rel="home" href="index.html" title="Programming with gtkmm 4">
<link rel="up" href="chapter-menus-and-toolbars.html" title="Chapter 15. Menus and Toolbars">
<link rel="prev" href="sec-gio-resource.html" title="Gio::Resource and glib-compile-resources">
<link rel="next" href="chapter-adjustment.html" title="Chapter 16. Adjustments">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Examples</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="sec-gio-resource.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center">Chapter 15. Menus and Toolbars</th>
<td width="20%" align="right"> <a accesskey="n" href="chapter-adjustment.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
</table>
<hr>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="sec-menus-examples"></a>Examples</h2></div></div></div>


<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="menu-example-main"></a>Application Menu and Main Menu example</h3></div></div></div>

<p>
This program contains an application menu, a menubar and a toolbar.
Classes are derived from <code class="classname">Gtk::Application</code> and
<code class="classname">Gtk::ApplicationWindow</code>.
</p>

<div class="figure">
<a name="figure-menus-mainmenu"></a><p class="title"><b>Figure 15.1. App and Main Menu</b></p>
<div class="figure-contents">
  
  <div class="screenshot">
    <div class="mediaobject"><img src="figures/main_menu.png" alt="App and Main Menu"></div>
  </div>
</div>
</div>
<br class="figure-break">

<p><a class="ulink" href="https://gitlab.gnome.org/GNOME/gtkmm-documentation/tree/master/examples/book/menus/main_menu/" target="_top">Source Code</a></p>

<p>File: <code class="filename">exampleapplication.h</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#ifndef GTKMM_EXAMPLEAPPLICATION_H
#define GTKMM_EXAMPLEAPPLICATION_H

#include &lt;gtkmm.h&gt;

class ExampleApplication : public Gtk::Application
{
protected:
  ExampleApplication();

public:
  static Glib::RefPtr&lt;ExampleApplication&gt; create();

protected:
  //Overrides of default signal handlers:
  void on_startup() override;
  void on_activate() override;

private:
  void create_window();

  void on_menu_file_new_generic();
  void on_menu_file_quit();
  void on_menu_help_about();

  Glib::RefPtr&lt;Gtk::Builder&gt; m_refBuilder;
};

#endif /* GTKMM_EXAMPLEAPPLICATION_H */
</code></pre>
<p>File: <code class="filename">examplewindow.h</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H

#include &lt;gtkmm.h&gt;

class ExampleWindow : public Gtk::ApplicationWindow
{
public:
  ExampleWindow();
  virtual ~ExampleWindow();

protected:
  //Signal handlers:
  void on_menu_others();

  void on_menu_choices(const Glib::ustring&amp; parameter);
  void on_menu_choices_other(int parameter);
  void on_menu_toggle();

  //Child widgets:
  Gtk::Box m_Box;

  Glib::RefPtr&lt;Gtk::Builder&gt; m_refBuilder;

  //Two sets of choices:
  Glib::RefPtr&lt;Gio::SimpleAction&gt; m_refChoice;
  Glib::RefPtr&lt;Gio::SimpleAction&gt; m_refChoiceOther;

  Glib::RefPtr&lt;Gio::SimpleAction&gt; m_refToggle;
};

#endif //GTKMM_EXAMPLEWINDOW_H
</code></pre>
<p>File: <code class="filename">exampleapplication.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "exampleapplication.h"
#include "examplewindow.h"
#include &lt;iostream&gt;

ExampleApplication::ExampleApplication()
: Gtk::Application("org.gtkmm.example.main_menu")
{
  Glib::set_application_name("Main Menu Example");
}

Glib::RefPtr&lt;ExampleApplication&gt; ExampleApplication::create()
{
  return Glib::make_refptr_for_instance&lt;ExampleApplication&gt;(new ExampleApplication());
}

void ExampleApplication::on_startup()
{
  //Call the base class's implementation:
  Gtk::Application::on_startup();

  //Create actions for menus and toolbars.
  //We can use add_action() because Gtk::Application derives from Gio::ActionMap.

  //File|New sub menu:
  add_action("newstandard",
    sigc::mem_fun(*this, &amp;ExampleApplication::on_menu_file_new_generic));

  add_action("newfoo",
    sigc::mem_fun(*this, &amp;ExampleApplication::on_menu_file_new_generic));

  add_action("newgoo",
    sigc::mem_fun(*this, &amp;ExampleApplication::on_menu_file_new_generic));

  //File menu:
  add_action("quit", sigc::mem_fun(*this, &amp;ExampleApplication::on_menu_file_quit));

  //Help menu:
  add_action("about", sigc::mem_fun(*this, &amp;ExampleApplication::on_menu_help_about));

  // Set accelerator keys:
  set_accel_for_action("app.newstandard", "&lt;Primary&gt;n");
  set_accel_for_action("app.quit", "&lt;Primary&gt;q");
  set_accel_for_action("win.copy", "&lt;Primary&gt;c");
  set_accel_for_action("win.paste", "&lt;Primary&gt;v");

  m_refBuilder = Gtk::Builder::create();

  //Layout the actions in a menubar and a menu:
  Glib::ustring ui_info =
    "&lt;interface&gt;"
    "  &lt;!-- menubar --&gt;"
    "  &lt;menu id='menu-example'&gt;"
    "    &lt;submenu&gt;"
    "      &lt;attribute name='label' translatable='yes'&gt;_File&lt;/attribute&gt;"
    "      &lt;section&gt;"
    "        &lt;item&gt;"
    "          &lt;attribute name='label' translatable='yes'&gt;New _Standard&lt;/attribute&gt;"
    "          &lt;attribute name='action'&gt;app.newstandard&lt;/attribute&gt;"
    "          &lt;attribute name='accel'&gt;&amp;lt;Primary&amp;gt;n&lt;/attribute&gt;"
    "        &lt;/item&gt;"
    "        &lt;item&gt;"
    "          &lt;attribute name='label' translatable='yes'&gt;New _Foo&lt;/attribute&gt;"
    "          &lt;attribute name='action'&gt;app.newfoo&lt;/attribute&gt;"
    "        &lt;/item&gt;"
    "        &lt;item&gt;"
    "          &lt;attribute name='label' translatable='yes'&gt;New _Goo&lt;/attribute&gt;"
    "          &lt;attribute name='action'&gt;app.newgoo&lt;/attribute&gt;"
    "        &lt;/item&gt;"
    "      &lt;/section&gt;"
    "      &lt;section&gt;"
    "        &lt;item&gt;"
    "          &lt;attribute name='label' translatable='yes'&gt;_Quit&lt;/attribute&gt;"
    "          &lt;attribute name='action'&gt;app.quit&lt;/attribute&gt;"
    "          &lt;attribute name='accel'&gt;&amp;lt;Primary&amp;gt;q&lt;/attribute&gt;"
    "        &lt;/item&gt;"
    "      &lt;/section&gt;"
    "    &lt;/submenu&gt;"
    "    &lt;submenu&gt;"
    "      &lt;attribute name='label' translatable='yes'&gt;_Edit&lt;/attribute&gt;"
    "      &lt;section&gt;"
    "        &lt;item&gt;"
    "          &lt;attribute name='label' translatable='yes'&gt;_Copy&lt;/attribute&gt;"
    "          &lt;attribute name='action'&gt;win.copy&lt;/attribute&gt;"
    "          &lt;attribute name='accel'&gt;&amp;lt;Primary&amp;gt;c&lt;/attribute&gt;"
    "        &lt;/item&gt;"
    "        &lt;item&gt;"
    "          &lt;attribute name='label' translatable='yes'&gt;_Paste&lt;/attribute&gt;"
    "          &lt;attribute name='action'&gt;win.paste&lt;/attribute&gt;"
    "          &lt;attribute name='accel'&gt;&amp;lt;Primary&amp;gt;v&lt;/attribute&gt;"
    "        &lt;/item&gt;"
    "        &lt;item&gt;"
    "          &lt;attribute name='label' translatable='yes'&gt;_Something&lt;/attribute&gt;"
    "          &lt;attribute name='action'&gt;win.something&lt;/attribute&gt;"
    "        &lt;/item&gt;"
    "      &lt;/section&gt;"
    "    &lt;/submenu&gt;"
    "    &lt;submenu&gt;"
    "      &lt;attribute name='label' translatable='yes'&gt;_Choices&lt;/attribute&gt;"
    "      &lt;section&gt;"
    "        &lt;item&gt;"
    "          &lt;attribute name='label' translatable='yes'&gt;Choice _A&lt;/attribute&gt;"
    "          &lt;attribute name='action'&gt;win.choice&lt;/attribute&gt;"
    "          &lt;attribute name='target'&gt;a&lt;/attribute&gt;"
    "        &lt;/item&gt;"
    "        &lt;item&gt;"
    "          &lt;attribute name='label' translatable='yes'&gt;Choice _B&lt;/attribute&gt;"
    "          &lt;attribute name='action'&gt;win.choice&lt;/attribute&gt;"
    "          &lt;attribute name='target'&gt;b&lt;/attribute&gt;"
    "        &lt;/item&gt;"
    "      &lt;/section&gt;"
    "    &lt;/submenu&gt;"
    "    &lt;submenu&gt;"
    "      &lt;attribute name='label' translatable='yes'&gt;_Other Choices&lt;/attribute&gt;"
    "      &lt;section&gt;"
    "        &lt;item&gt;"
    "          &lt;attribute name='label' translatable='yes'&gt;Choice 1&lt;/attribute&gt;"
    "          &lt;attribute name='action'&gt;win.choiceother&lt;/attribute&gt;"
    "          &lt;attribute name='target' type='i'&gt;1&lt;/attribute&gt;"
    "        &lt;/item&gt;"
    "        &lt;item&gt;"
    "          &lt;attribute name='label' translatable='yes'&gt;Choice 2&lt;/attribute&gt;"
    "          &lt;attribute name='action'&gt;win.choiceother&lt;/attribute&gt;"
    "          &lt;attribute name='target' type='i'&gt;2&lt;/attribute&gt;"
    "        &lt;/item&gt;"
    "      &lt;/section&gt;"
    "      &lt;section&gt;"
    "        &lt;item&gt;"
    "          &lt;attribute name='label' translatable='yes'&gt;Some Toggle&lt;/attribute&gt;"
    "          &lt;attribute name='action'&gt;win.sometoggle&lt;/attribute&gt;"
    "        &lt;/item&gt;"
    "      &lt;/section&gt;"
    "    &lt;/submenu&gt;"
    "    &lt;submenu&gt;"
    "      &lt;attribute name='label' translatable='yes'&gt;_Help&lt;/attribute&gt;"
    "      &lt;section&gt;"
    "        &lt;item&gt;"
    "          &lt;attribute name='label' translatable='yes'&gt;_About Window&lt;/attribute&gt;"
    "          &lt;attribute name='action'&gt;win.about&lt;/attribute&gt;"
    "        &lt;/item&gt;"
    "        &lt;item&gt;"
    "          &lt;attribute name='label' translatable='yes'&gt;_About App&lt;/attribute&gt;"
    "          &lt;attribute name='action'&gt;app.about&lt;/attribute&gt;"
    "        &lt;/item&gt;"
    "      &lt;/section&gt;"
    "    &lt;/submenu&gt;"
    "  &lt;/menu&gt;"
    "&lt;/interface&gt;";

  try
  {
    m_refBuilder-&gt;add_from_string(ui_info);
  }
  catch (const Glib::Error&amp; ex)
  {
    std::cerr &lt;&lt; "Building menus failed: " &lt;&lt; ex.what();
  }

  //Get the menubar and the app menu, and add them to the application:
  auto gmenu = m_refBuilder-&gt;get_object&lt;Gio::Menu&gt;("menu-example");
  if (!gmenu)
  {
    g_warning("GMenu not found");
  }
  else
  {
    set_menubar(gmenu);
  }
}

void ExampleApplication::on_activate()
{
  //std::cout &lt;&lt; "debug1: " &lt;&lt; G_STRFUNC &lt;&lt; std::endl;
  // The application has been started, so let's show a window.
  // A real application might want to reuse this window in on_open(),
  // when asked to open a file, if no changes have been made yet.
  create_window();
}

void ExampleApplication::create_window()
{
  auto win = new ExampleWindow();

  //Make sure that the application runs for as long this window is still open:
  add_window(*win);

  //Delete the window when it is hidden.
  //That's enough for this simple example.
  win-&gt;signal_hide().connect([win]() { delete win; });

  win-&gt;set_show_menubar();
  win-&gt;set_visible(true);
}

void ExampleApplication::on_menu_file_new_generic()
{
  std::cout &lt;&lt; "A File|New menu item was selected." &lt;&lt; std::endl;
}

void ExampleApplication::on_menu_file_quit()
{
  std::cout &lt;&lt; G_STRFUNC &lt;&lt; std::endl;
  quit(); // Not really necessary, when Gtk::Widget::set_visible(false) is called.

  // Gio::Application::quit() will make Gio::Application::run() return,
  // but it's a crude way of ending the program. The window is not removed
  // from the application. Neither the window's nor the application's
  // destructors will be called, because there will be remaining reference
  // counts in both of them. If we want the destructors to be called, we
  // must remove the window from the application. One way of doing this
  // is to hide the window.
  std::vector&lt;Gtk::Window*&gt; windows = get_windows();
  if (windows.size() &gt; 0)
    windows[0]-&gt;set_visible(false); // In this simple case, we know there is only one window.
}

void ExampleApplication::on_menu_help_about()
{
  std::cout &lt;&lt; "Help|About App was selected." &lt;&lt; std::endl;
}
</code></pre>
<p>File: <code class="filename">examplewindow.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "examplewindow.h"
#include &lt;iostream&gt;

ExampleWindow::ExampleWindow()
: Gtk::ApplicationWindow(),
  m_Box(Gtk::Orientation::VERTICAL)
{
  set_title("Main menu example");
  set_default_size(300, 100);

  // ExampleApplication displays the menubar. Other stuff, such as a toolbar,
  // is put into the box.
  set_child(m_Box);

  // Create actions for menus and toolbars.
  // We can use add_action() because Gtk::ApplicationWindow derives from Gio::ActionMap.
  // This Action Map uses a "win." prefix for the actions.
  // Therefore, for instance, "win.copy", is used in ExampleApplication::on_startup()
  // to layout the menu.

  //Edit menu:
  add_action("copy", sigc::mem_fun(*this, &amp;ExampleWindow::on_menu_others));
  add_action("paste", sigc::mem_fun(*this, &amp;ExampleWindow::on_menu_others));
  add_action("something", sigc::mem_fun(*this, &amp;ExampleWindow::on_menu_others));

  //Choices menus, to demonstrate Radio items,
  //using our convenience methods for string and int radio values:
  m_refChoice = add_action_radio_string("choice",
    sigc::mem_fun(*this, &amp;ExampleWindow::on_menu_choices), "a");

  m_refChoiceOther = add_action_radio_integer("choiceother",
    sigc::mem_fun(*this, &amp;ExampleWindow::on_menu_choices_other), 1);

  m_refToggle = add_action_bool("sometoggle",
    sigc::mem_fun(*this, &amp;ExampleWindow::on_menu_toggle), false);

  //Help menu:
  add_action("about", sigc::mem_fun(*this, &amp;ExampleWindow::on_menu_others));

  //Create the toolbar and add it to a container widget:

  m_refBuilder = Gtk::Builder::create();

  Glib::ustring ui_info =
    "&lt;!-- Generated with glade 3.18.3 and then changed manually --&gt;"
    "&lt;interface&gt;"
    "  &lt;object class='GtkBox' id='toolbar'&gt;"
    "    &lt;property name='can_focus'&gt;False&lt;/property&gt;"
    "    &lt;child&gt;"
    "      &lt;object class='GtkButton' id='toolbutton_new'&gt;"
    "        &lt;property name='can_focus'&gt;False&lt;/property&gt;"
    "        &lt;property name='tooltip_text' translatable='yes'&gt;New Standard&lt;/property&gt;"
    "        &lt;property name='action_name'&gt;app.newstandard&lt;/property&gt;"
    "        &lt;property name='icon_name'&gt;document-new&lt;/property&gt;"
    "        &lt;property name='hexpand'&gt;False&lt;/property&gt;"
    "        &lt;property name='vexpand'&gt;False&lt;/property&gt;"
    "      &lt;/object&gt;"
    "    &lt;/child&gt;"
    "    &lt;child&gt;"
    "      &lt;object class='GtkButton' id='toolbutton_quit'&gt;"
    "        &lt;property name='can_focus'&gt;False&lt;/property&gt;"
    "        &lt;property name='tooltip_text' translatable='yes'&gt;Quit&lt;/property&gt;"
    "        &lt;property name='action_name'&gt;app.quit&lt;/property&gt;"
    "        &lt;property name='icon_name'&gt;application-exit&lt;/property&gt;"
    "        &lt;property name='hexpand'&gt;False&lt;/property&gt;"
    "        &lt;property name='vexpand'&gt;False&lt;/property&gt;"
    "      &lt;/object&gt;"
    "    &lt;/child&gt;"
    "  &lt;/object&gt;"
    "&lt;/interface&gt;";

  try
  {
    m_refBuilder-&gt;add_from_string(ui_info);
  }
  catch (const Glib::Error&amp; ex)
  {
    std::cerr &lt;&lt; "Building toolbar failed: " &lt;&lt;  ex.what();
  }

  auto toolbar = m_refBuilder-&gt;get_widget&lt;Gtk::Box&gt;("toolbar");
  if (!toolbar)
    g_warning("toolbar not found");
  else
    m_Box.append(*toolbar);
}

ExampleWindow::~ExampleWindow()
{
}

void ExampleWindow::on_menu_others()
{
  std::cout &lt;&lt; "A menu item was selected." &lt;&lt; std::endl;
}

void ExampleWindow::on_menu_choices(const Glib::ustring&amp; parameter)
{
  //The radio action's state does not change automatically:
  m_refChoice-&gt;change_state(parameter);

  Glib::ustring message;
  if (parameter == "a")
    message = "Choice a was selected.";
  else
    message = "Choice b was selected.";

  std::cout &lt;&lt; message &lt;&lt; std::endl;
}

void ExampleWindow::on_menu_choices_other(int parameter)
{
  //The radio action's state does not change automatically:
  m_refChoiceOther-&gt;change_state(parameter);

  Glib::ustring message;
  if (parameter == 1)
    message = "Choice 1 was selected.";
  else
    message = "Choice 2 was selected.";

  std::cout &lt;&lt; message &lt;&lt; std::endl;
}

void ExampleWindow::on_menu_toggle()
{
  bool active = false;
  m_refToggle-&gt;get_state(active);

  //The toggle action's state does not change automatically:
  active = !active;
  m_refToggle-&gt;change_state(active);

  Glib::ustring message;
  if (active)
    message = "Toggle is active.";
  else
    message = "Toggle is not active.";

  std::cout &lt;&lt; message &lt;&lt; std::endl;
}
</code></pre>
<p>File: <code class="filename">main.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "exampleapplication.h"

int main(int argc, char* argv[])
{
  auto application = ExampleApplication::create();

  // Start the application, showing the initial window,
  // and opening extra windows for any files that it is asked to open,
  // for instance as a command-line parameter.
  // run() will return when the last window has been closed by the user.
  const int status = application-&gt;run(argc, argv);
  return status;
}
</code></pre>


</div>

<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="menu-example-main2"></a>Main Menu example</h3></div></div></div>

<p>
This program contains a menubar and a toolbar.
A class is derived from <code class="classname">Gtk::Window</code>.
</p>

<div class="figure">
<a name="figure-menus-mainmenu2"></a><p class="title"><b>Figure 15.2. Main Menu</b></p>
<div class="figure-contents">
  
  <div class="screenshot">
    <div class="mediaobject"><img src="figures/menus_and_toolbars.png" alt="Main Menu"></div>
  </div>
</div>
</div>
<br class="figure-break">

<p><a class="ulink" href="https://gitlab.gnome.org/GNOME/gtkmm-documentation/tree/master/examples/book/menus_and_toolbars" target="_top">Source Code</a></p>

<p>File: <code class="filename">examplewindow.h</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H

#include &lt;gtkmm.h&gt;

class ExampleWindow : public Gtk::Window
{
public:
  ExampleWindow(const Glib::RefPtr&lt;Gtk::Application&gt;&amp; app);
  virtual ~ExampleWindow();

private:
  //Signal handlers:
  //void on_action_file_new();
  void on_action_file_quit();
  void on_action_others();
  void on_action_toggle();

  //Child widgets:
  Gtk::Box m_Box;

  Glib::RefPtr&lt;Gtk::Builder&gt; m_refBuilder;
  Glib::RefPtr&lt;Gio::SimpleActionGroup&gt; m_refActionGroup;
  Glib::RefPtr&lt;Gio::SimpleAction&gt; m_refActionRain;
};

#endif //GTKMM_EXAMPLEWINDOW_H
</code></pre>
<p>File: <code class="filename">examplewindow.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "examplewindow.h"
#include &lt;gtkmm.h&gt;
#include &lt;iostream&gt;

ExampleWindow::ExampleWindow(const Glib::RefPtr&lt;Gtk::Application&gt;&amp; app)
: m_Box(Gtk::Orientation::VERTICAL)
{
  set_title("main_menu example");
  set_default_size(200, 200);

  set_child(m_Box); //We can put a MenuBar at the top of the box and other stuff below it.

  //Define the actions:
  m_refActionGroup = Gio::SimpleActionGroup::create();

  // There are several ways of calling a function that takes a sigc::slot.
  // If the slot function is very short, it might be easy to skip the on_xxx()
  // method and put its contents directly in a lambda expression.
  m_refActionGroup-&gt;add_action("new",
    [] { std::cout &lt;&lt; "A File|New menu item was selected.\n"; /* on_action_file_new() */});
  // With sigc::mem_fun() or (for non-member functions and static member functions)
  // sigc::ptr_fun(). The only way before C++11 introduced lambda expressions.
  m_refActionGroup-&gt;add_action("open",
    sigc::mem_fun(*this, &amp;ExampleWindow::on_action_others) );

  // With a lambda expression. Does not disconnect automatically when ExampleWindow
  // is deleted, like sigc::mem_fun() does.
  m_refActionRain = m_refActionGroup-&gt;add_action_bool("rain",
    [this] { on_action_toggle(); }, false);

  m_refActionGroup-&gt;add_action("quit",
    sigc::mem_fun(*this, &amp;ExampleWindow::on_action_file_quit) );

  // With a lambda expression and sigc::track_obj() or sigc::track_object().
  // Disconnects automatically like sigc::mem_fun().
#if SIGCXX_MINOR_VERSION &gt;= 4
  m_refActionGroup-&gt;add_action("cut",
    sigc::track_object([this] { on_action_others(); }, *this));
#else
  m_refActionGroup-&gt;add_action("cut",
    sigc::track_obj([this] { on_action_others(); }, *this));
#endif
  m_refActionGroup-&gt;add_action("copy",
    sigc::mem_fun(*this, &amp;ExampleWindow::on_action_others) );
  m_refActionGroup-&gt;add_action("paste",
    sigc::mem_fun(*this, &amp;ExampleWindow::on_action_others) );

  insert_action_group("example", m_refActionGroup);

  //Define how the actions are presented in the menus and toolbars:
  m_refBuilder = Gtk::Builder::create();

  //Layout the actions in a menubar and toolbar:
  const Glib::ustring ui_info =
    "&lt;interface&gt;"
    "  &lt;menu id='menubar'&gt;"
    "    &lt;submenu&gt;"
    "      &lt;attribute name='label' translatable='yes'&gt;_File&lt;/attribute&gt;"
    "      &lt;section&gt;"
    "        &lt;item&gt;"
    "          &lt;attribute name='label' translatable='yes'&gt;_New&lt;/attribute&gt;"
    "          &lt;attribute name='action'&gt;example.new&lt;/attribute&gt;"
    "        &lt;/item&gt;"
    "        &lt;item&gt;"
    "          &lt;attribute name='label' translatable='yes'&gt;_Open&lt;/attribute&gt;"
    "          &lt;attribute name='action'&gt;example.open&lt;/attribute&gt;"
    "        &lt;/item&gt;"
    "      &lt;/section&gt;"
    "      &lt;section&gt;"
    "        &lt;item&gt;"
    "          &lt;attribute name='label' translatable='yes'&gt;Rain&lt;/attribute&gt;"
    "          &lt;attribute name='action'&gt;example.rain&lt;/attribute&gt;"
    "        &lt;/item&gt;"
    "      &lt;/section&gt;"
    "      &lt;section&gt;"
    "        &lt;item&gt;"
    "          &lt;attribute name='label' translatable='yes'&gt;_Quit&lt;/attribute&gt;"
    "          &lt;attribute name='action'&gt;example.quit&lt;/attribute&gt;"
    "        &lt;/item&gt;"
    "      &lt;/section&gt;"
    "    &lt;/submenu&gt;"
    "    &lt;submenu&gt;"
    "      &lt;attribute name='label' translatable='yes'&gt;_Edit&lt;/attribute&gt;"
    "      &lt;item&gt;"
    "        &lt;attribute name='label' translatable='yes'&gt;_Cut&lt;/attribute&gt;"
    "        &lt;attribute name='action'&gt;example.cut&lt;/attribute&gt;"
    "      &lt;/item&gt;"
    "      &lt;item&gt;"
    "        &lt;attribute name='label' translatable='yes'&gt;_Copy&lt;/attribute&gt;"
    "        &lt;attribute name='action'&gt;example.copy&lt;/attribute&gt;"
    "      &lt;/item&gt;"
    "      &lt;item&gt;"
    "        &lt;attribute name='label' translatable='yes'&gt;_Paste&lt;/attribute&gt;"
    "        &lt;attribute name='action'&gt;example.paste&lt;/attribute&gt;"
    "      &lt;/item&gt;"
    "    &lt;/submenu&gt;"
    "  &lt;/menu&gt;"
    "&lt;/interface&gt;";

  // Set accelerator keys:
  app-&gt;set_accel_for_action("example.new", "&lt;Primary&gt;n");
  app-&gt;set_accel_for_action("example.open", "&lt;Primary&gt;o");
  app-&gt;set_accel_for_action("example.quit", "&lt;Primary&gt;q");
  app-&gt;set_accel_for_action("example.cut", "&lt;Primary&gt;x");
  app-&gt;set_accel_for_action("example.copy", "&lt;Primary&gt;c");
  app-&gt;set_accel_for_action("example.paste", "&lt;Primary&gt;v");

  try
  {
    m_refBuilder-&gt;add_from_string(ui_info);
    m_refBuilder-&gt;add_from_resource("/toolbar/toolbar.ui");
  }
  catch(const Glib::Error&amp; ex)
  {
    std::cerr &lt;&lt; "Building menus and toolbar failed: " &lt;&lt;  ex.what();
  }

  //Get the menubar:
  auto gmenu = m_refBuilder-&gt;get_object&lt;Gio::Menu&gt;("menubar");
  if (!gmenu)
    g_warning("GMenu not found");
  else
  {
    auto pMenuBar = Gtk::make_managed&lt;Gtk::PopoverMenuBar&gt;(gmenu);

    //Add the PopoverMenuBar to the window:
    m_Box.append(*pMenuBar);
  }

  //Get the toolbar and add it to a container widget:
  auto toolbar = m_refBuilder-&gt;get_widget&lt;Gtk::Box&gt;("toolbar");
  if (!toolbar)
    g_warning("toolbar not found");
  else
    m_Box.append(*toolbar);
}

ExampleWindow::~ExampleWindow()
{
}

void ExampleWindow::on_action_file_quit()
{
  set_visible(false); //Closes the main window to stop the app-&gt;make_window_and_run().
}

//void ExampleWindow::on_action_file_new()
//{
//   std::cout &lt;&lt; "A File|New menu item was selected." &lt;&lt; std::endl;
//}

void ExampleWindow::on_action_others()
{
  std::cout &lt;&lt; "A menu item was selected." &lt;&lt; std::endl;
}

void ExampleWindow::on_action_toggle()
{
  std::cout &lt;&lt; "The toggle menu item was selected." &lt;&lt; std::endl;

  bool active = false;
  m_refActionRain-&gt;get_state(active);

  //The toggle action's state does not change automatically:
  active = !active;
  m_refActionRain-&gt;change_state(active);

  Glib::ustring message;
  if(active)
    message = "Toggle is active.";
  else
    message = "Toggle is not active";

  std::cout &lt;&lt; message &lt;&lt; std::endl;
}
</code></pre>
<p>File: <code class="filename">main.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include &lt;gtkmm.h&gt;
#include "examplewindow.h"

int main(int argc, char* argv[])
{
  auto app = Gtk::Application::create("org.gtkmm.example");

  //Shows the window and returns when it is closed.
  return app-&gt;make_window_and_run&lt;ExampleWindow&gt;(argc, argv, app);
}
</code></pre>
<p>File: <code class="filename">toolbar.gresource.xml</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;gresources&gt;
  &lt;gresource prefix="/toolbar"&gt;
    &lt;file preprocess="xml-stripblanks"&gt;toolbar.ui&lt;/file&gt;
    &lt;file&gt;rain.png&lt;/file&gt;
  &lt;/gresource&gt;
&lt;/gresources&gt;
</code></pre>


</div>

<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="menu-example-popup"></a>Popup Menu example</h3></div></div></div>


<div class="figure">
<a name="figure-menus-popup"></a><p class="title"><b>Figure 15.3. Popup Menu</b></p>
<div class="figure-contents">
  
  <div class="screenshot">
    <div class="mediaobject"><img src="figures/menu_popup.png" alt="Popup Menu"></div>
  </div>
</div>
</div>
<br class="figure-break">

<p><a class="ulink" href="https://gitlab.gnome.org/GNOME/gtkmm-documentation/tree/master/examples/book/menus/popup/" target="_top">Source Code</a></p>

<p>File: <code class="filename">examplewindow.h</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H

#include &lt;gtkmm.h&gt;

#include &lt;memory&gt;

class ExampleWindow : public Gtk::Window
{
public:
  ExampleWindow(const Glib::RefPtr&lt;Gtk::Application&gt;&amp; app);
  virtual ~ExampleWindow();

protected:
  //Signal handlers:
  void on_label_pressed(int n_press, double x, double y);

  void on_menu_file_popup_generic();

  //Child widgets:
  Gtk::Box m_Box;
  Gtk::Label m_Label;
  Gtk::PopoverMenu m_MenuPopup;

  Glib::RefPtr&lt;Gtk::Builder&gt; m_refBuilder;
  Glib::RefPtr&lt;Gtk::GestureClick&gt; m_refGesture;
};

#endif //GTKMM_EXAMPLEWINDOW_H
</code></pre>
<p>File: <code class="filename">examplewindow.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "examplewindow.h"
#include &lt;iostream&gt;

ExampleWindow::ExampleWindow(const Glib::RefPtr&lt;Gtk::Application&gt;&amp; app)
: m_Box(Gtk::Orientation::VERTICAL),
  m_Label("Right-click to see the popup menu.")
{
  set_title("popup example");
  set_default_size(200, 200);

  set_child(m_Box);

  // Catch button_press events:
  m_Box.append(m_Label);
  m_Label.set_expand();
  m_refGesture = Gtk::GestureClick::create();
  m_refGesture-&gt;set_button(GDK_BUTTON_SECONDARY);
  m_refGesture-&gt;signal_pressed().connect(
    sigc::mem_fun(*this, &amp;ExampleWindow::on_label_pressed));
  m_Label.add_controller(m_refGesture);

  //Create actions:
  auto refActionGroup = Gio::SimpleActionGroup::create();

  //File|New sub menu:
  //These menu actions would normally already exist for a main menu, because a
  //context menu should not normally contain menu items that are only available
  //via a context menu.

  refActionGroup-&gt;add_action("edit",
    sigc::mem_fun(*this, &amp;ExampleWindow::on_menu_file_popup_generic));

  refActionGroup-&gt;add_action("process",
    sigc::mem_fun(*this, &amp;ExampleWindow::on_menu_file_popup_generic));

  refActionGroup-&gt;add_action("remove",
    sigc::mem_fun(*this, &amp;ExampleWindow::on_menu_file_popup_generic));

  insert_action_group("examplepopup", refActionGroup);

  // Set accelerator keys:
  app-&gt;set_accel_for_action("examplepopup.edit", "&lt;Primary&gt;e");
  app-&gt;set_accel_for_action("examplepopup.process", "&lt;Primary&gt;p");
  app-&gt;set_accel_for_action("examplepopup.remove", "&lt;Primary&gt;r");

  m_refBuilder = Gtk::Builder::create();

  //Layout the actions in a menubar and toolbar:
  Glib::ustring ui_info =
    "&lt;interface&gt;"
    "  &lt;menu id='menu-examplepopup'&gt;"
    "    &lt;section&gt;"
    "      &lt;item&gt;"
    "        &lt;attribute name='label' translatable='yes'&gt;Edit&lt;/attribute&gt;"
    "        &lt;attribute name='action'&gt;examplepopup.edit&lt;/attribute&gt;"
    "      &lt;/item&gt;"
    "      &lt;item&gt;"
    "        &lt;attribute name='label' translatable='yes'&gt;Process&lt;/attribute&gt;"
    "        &lt;attribute name='action'&gt;examplepopup.process&lt;/attribute&gt;"
    "      &lt;/item&gt;"
    "      &lt;item&gt;"
    "        &lt;attribute name='label' translatable='yes'&gt;Remove&lt;/attribute&gt;"
    "        &lt;attribute name='action'&gt;examplepopup.remove&lt;/attribute&gt;"
    "      &lt;/item&gt;"
    "    &lt;/section&gt;"
    "  &lt;/menu&gt;"
    "&lt;/interface&gt;";

  try
  {
    m_refBuilder-&gt;add_from_string(ui_info);
  }
  catch(const Glib::Error&amp; ex)
  {
    std::cerr &lt;&lt; "building menus failed: " &lt;&lt;  ex.what();
  }

  //Get the menu:
  auto gmenu = m_refBuilder-&gt;get_object&lt;Gio::Menu&gt;("menu-examplepopup");
  if(!gmenu)
    g_warning("GMenu not found");

  m_MenuPopup.set_parent(m_Label);
  m_MenuPopup.set_menu_model(gmenu);
  m_MenuPopup.set_has_arrow(false);
}

ExampleWindow::~ExampleWindow()
{
  m_MenuPopup.unparent();
}

void ExampleWindow::on_menu_file_popup_generic()
{
   std::cout &lt;&lt; "A popup menu item was selected." &lt;&lt; std::endl;
}

void ExampleWindow::on_label_pressed(int /* n_press */, double x, double y)
{
  const Gdk::Rectangle rect(x, y, 1, 1);
  m_MenuPopup.set_pointing_to(rect);
  m_MenuPopup.popup();
}
</code></pre>
<p>File: <code class="filename">main.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "examplewindow.h"
#include &lt;gtkmm/application.h&gt;

int main(int argc, char *argv[])
{
  auto app = Gtk::Application::create("org.gtkmm.example");

  //Shows the window and returns when it is closed.
  return app-&gt;make_window_and_run&lt;ExampleWindow&gt;(argc, argv, app);
}
</code></pre>


</div>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="sec-gio-resource.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"><a accesskey="u" href="chapter-menus-and-toolbars.html"><img src="icons/up.png" alt="Up"></a></td>
<td width="40%" align="right"> <a accesskey="n" href="chapter-adjustment.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Gio::Resource and glib-compile-resources </td>
<td width="20%" align="center"><a accesskey="h" href="index.html"><img src="icons/home.png" alt="Home"></a></td>
<td width="40%" align="right" valign="top"> Chapter 16. Adjustments</td>
</tr>
</table>
</div>
</body>
</html>