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 <gtkmm.h>
class ExampleApplication : public Gtk::Application
{
protected:
ExampleApplication();
public:
static Glib::RefPtr<ExampleApplication> 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<Gtk::Builder> 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 <gtkmm.h>
class ExampleWindow : public Gtk::ApplicationWindow
{
public:
ExampleWindow();
virtual ~ExampleWindow();
protected:
//Signal handlers:
void on_menu_others();
void on_menu_choices(const Glib::ustring& parameter);
void on_menu_choices_other(int parameter);
void on_menu_toggle();
//Child widgets:
Gtk::Box m_Box;
Glib::RefPtr<Gtk::Builder> m_refBuilder;
//Two sets of choices:
Glib::RefPtr<Gio::SimpleAction> m_refChoice;
Glib::RefPtr<Gio::SimpleAction> m_refChoiceOther;
Glib::RefPtr<Gio::SimpleAction> 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 <iostream>
ExampleApplication::ExampleApplication()
: Gtk::Application("org.gtkmm.example.main_menu")
{
Glib::set_application_name("Main Menu Example");
}
Glib::RefPtr<ExampleApplication> ExampleApplication::create()
{
return Glib::make_refptr_for_instance<ExampleApplication>(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, &ExampleApplication::on_menu_file_new_generic));
add_action("newfoo",
sigc::mem_fun(*this, &ExampleApplication::on_menu_file_new_generic));
add_action("newgoo",
sigc::mem_fun(*this, &ExampleApplication::on_menu_file_new_generic));
//File menu:
add_action("quit", sigc::mem_fun(*this, &ExampleApplication::on_menu_file_quit));
//Help menu:
add_action("about", sigc::mem_fun(*this, &ExampleApplication::on_menu_help_about));
// Set accelerator keys:
set_accel_for_action("app.newstandard", "<Primary>n");
set_accel_for_action("app.quit", "<Primary>q");
set_accel_for_action("win.copy", "<Primary>c");
set_accel_for_action("win.paste", "<Primary>v");
m_refBuilder = Gtk::Builder::create();
//Layout the actions in a menubar and a menu:
Glib::ustring ui_info =
"<interface>"
" <!-- menubar -->"
" <menu id='menu-example'>"
" <submenu>"
" <attribute name='label' translatable='yes'>_File</attribute>"
" <section>"
" <item>"
" <attribute name='label' translatable='yes'>New _Standard</attribute>"
" <attribute name='action'>app.newstandard</attribute>"
" <attribute name='accel'>&lt;Primary&gt;n</attribute>"
" </item>"
" <item>"
" <attribute name='label' translatable='yes'>New _Foo</attribute>"
" <attribute name='action'>app.newfoo</attribute>"
" </item>"
" <item>"
" <attribute name='label' translatable='yes'>New _Goo</attribute>"
" <attribute name='action'>app.newgoo</attribute>"
" </item>"
" </section>"
" <section>"
" <item>"
" <attribute name='label' translatable='yes'>_Quit</attribute>"
" <attribute name='action'>app.quit</attribute>"
" <attribute name='accel'>&lt;Primary&gt;q</attribute>"
" </item>"
" </section>"
" </submenu>"
" <submenu>"
" <attribute name='label' translatable='yes'>_Edit</attribute>"
" <section>"
" <item>"
" <attribute name='label' translatable='yes'>_Copy</attribute>"
" <attribute name='action'>win.copy</attribute>"
" <attribute name='accel'>&lt;Primary&gt;c</attribute>"
" </item>"
" <item>"
" <attribute name='label' translatable='yes'>_Paste</attribute>"
" <attribute name='action'>win.paste</attribute>"
" <attribute name='accel'>&lt;Primary&gt;v</attribute>"
" </item>"
" <item>"
" <attribute name='label' translatable='yes'>_Something</attribute>"
" <attribute name='action'>win.something</attribute>"
" </item>"
" </section>"
" </submenu>"
" <submenu>"
" <attribute name='label' translatable='yes'>_Choices</attribute>"
" <section>"
" <item>"
" <attribute name='label' translatable='yes'>Choice _A</attribute>"
" <attribute name='action'>win.choice</attribute>"
" <attribute name='target'>a</attribute>"
" </item>"
" <item>"
" <attribute name='label' translatable='yes'>Choice _B</attribute>"
" <attribute name='action'>win.choice</attribute>"
" <attribute name='target'>b</attribute>"
" </item>"
" </section>"
" </submenu>"
" <submenu>"
" <attribute name='label' translatable='yes'>_Other Choices</attribute>"
" <section>"
" <item>"
" <attribute name='label' translatable='yes'>Choice 1</attribute>"
" <attribute name='action'>win.choiceother</attribute>"
" <attribute name='target' type='i'>1</attribute>"
" </item>"
" <item>"
" <attribute name='label' translatable='yes'>Choice 2</attribute>"
" <attribute name='action'>win.choiceother</attribute>"
" <attribute name='target' type='i'>2</attribute>"
" </item>"
" </section>"
" <section>"
" <item>"
" <attribute name='label' translatable='yes'>Some Toggle</attribute>"
" <attribute name='action'>win.sometoggle</attribute>"
" </item>"
" </section>"
" </submenu>"
" <submenu>"
" <attribute name='label' translatable='yes'>_Help</attribute>"
" <section>"
" <item>"
" <attribute name='label' translatable='yes'>_About Window</attribute>"
" <attribute name='action'>win.about</attribute>"
" </item>"
" <item>"
" <attribute name='label' translatable='yes'>_About App</attribute>"
" <attribute name='action'>app.about</attribute>"
" </item>"
" </section>"
" </submenu>"
" </menu>"
"</interface>";
try
{
m_refBuilder->add_from_string(ui_info);
}
catch (const Glib::Error& ex)
{
std::cerr << "Building menus failed: " << ex.what();
}
//Get the menubar and the app menu, and add them to the application:
auto gmenu = m_refBuilder->get_object<Gio::Menu>("menu-example");
if (!gmenu)
{
g_warning("GMenu not found");
}
else
{
set_menubar(gmenu);
}
}
void ExampleApplication::on_activate()
{
//std::cout << "debug1: " << G_STRFUNC << 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->signal_hide().connect([win]() { delete win; });
win->set_show_menubar();
win->set_visible(true);
}
void ExampleApplication::on_menu_file_new_generic()
{
std::cout << "A File|New menu item was selected." << std::endl;
}
void ExampleApplication::on_menu_file_quit()
{
std::cout << G_STRFUNC << 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<Gtk::Window*> windows = get_windows();
if (windows.size() > 0)
windows[0]->set_visible(false); // In this simple case, we know there is only one window.
}
void ExampleApplication::on_menu_help_about()
{
std::cout << "Help|About App was selected." << 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 <iostream>
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, &ExampleWindow::on_menu_others));
add_action("paste", sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
add_action("something", sigc::mem_fun(*this, &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, &ExampleWindow::on_menu_choices), "a");
m_refChoiceOther = add_action_radio_integer("choiceother",
sigc::mem_fun(*this, &ExampleWindow::on_menu_choices_other), 1);
m_refToggle = add_action_bool("sometoggle",
sigc::mem_fun(*this, &ExampleWindow::on_menu_toggle), false);
//Help menu:
add_action("about", sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
//Create the toolbar and add it to a container widget:
m_refBuilder = Gtk::Builder::create();
Glib::ustring ui_info =
"<!-- Generated with glade 3.18.3 and then changed manually -->"
"<interface>"
" <object class='GtkBox' id='toolbar'>"
" <property name='can_focus'>False</property>"
" <child>"
" <object class='GtkButton' id='toolbutton_new'>"
" <property name='can_focus'>False</property>"
" <property name='tooltip_text' translatable='yes'>New Standard</property>"
" <property name='action_name'>app.newstandard</property>"
" <property name='icon_name'>document-new</property>"
" <property name='hexpand'>False</property>"
" <property name='vexpand'>False</property>"
" </object>"
" </child>"
" <child>"
" <object class='GtkButton' id='toolbutton_quit'>"
" <property name='can_focus'>False</property>"
" <property name='tooltip_text' translatable='yes'>Quit</property>"
" <property name='action_name'>app.quit</property>"
" <property name='icon_name'>application-exit</property>"
" <property name='hexpand'>False</property>"
" <property name='vexpand'>False</property>"
" </object>"
" </child>"
" </object>"
"</interface>";
try
{
m_refBuilder->add_from_string(ui_info);
}
catch (const Glib::Error& ex)
{
std::cerr << "Building toolbar failed: " << ex.what();
}
auto toolbar = m_refBuilder->get_widget<Gtk::Box>("toolbar");
if (!toolbar)
g_warning("toolbar not found");
else
m_Box.append(*toolbar);
}
ExampleWindow::~ExampleWindow()
{
}
void ExampleWindow::on_menu_others()
{
std::cout << "A menu item was selected." << std::endl;
}
void ExampleWindow::on_menu_choices(const Glib::ustring& parameter)
{
//The radio action's state does not change automatically:
m_refChoice->change_state(parameter);
Glib::ustring message;
if (parameter == "a")
message = "Choice a was selected.";
else
message = "Choice b was selected.";
std::cout << message << std::endl;
}
void ExampleWindow::on_menu_choices_other(int parameter)
{
//The radio action's state does not change automatically:
m_refChoiceOther->change_state(parameter);
Glib::ustring message;
if (parameter == 1)
message = "Choice 1 was selected.";
else
message = "Choice 2 was selected.";
std::cout << message << std::endl;
}
void ExampleWindow::on_menu_toggle()
{
bool active = false;
m_refToggle->get_state(active);
//The toggle action's state does not change automatically:
active = !active;
m_refToggle->change_state(active);
Glib::ustring message;
if (active)
message = "Toggle is active.";
else
message = "Toggle is not active.";
std::cout << message << 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->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 <gtkmm.h>
class ExampleWindow : public Gtk::Window
{
public:
ExampleWindow(const Glib::RefPtr<Gtk::Application>& 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<Gtk::Builder> m_refBuilder;
Glib::RefPtr<Gio::SimpleActionGroup> m_refActionGroup;
Glib::RefPtr<Gio::SimpleAction> 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 <gtkmm.h>
#include <iostream>
ExampleWindow::ExampleWindow(const Glib::RefPtr<Gtk::Application>& 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->add_action("new",
[] { std::cout << "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->add_action("open",
sigc::mem_fun(*this, &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->add_action_bool("rain",
[this] { on_action_toggle(); }, false);
m_refActionGroup->add_action("quit",
sigc::mem_fun(*this, &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 >= 4
m_refActionGroup->add_action("cut",
sigc::track_object([this] { on_action_others(); }, *this));
#else
m_refActionGroup->add_action("cut",
sigc::track_obj([this] { on_action_others(); }, *this));
#endif
m_refActionGroup->add_action("copy",
sigc::mem_fun(*this, &ExampleWindow::on_action_others) );
m_refActionGroup->add_action("paste",
sigc::mem_fun(*this, &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 =
"<interface>"
" <menu id='menubar'>"
" <submenu>"
" <attribute name='label' translatable='yes'>_File</attribute>"
" <section>"
" <item>"
" <attribute name='label' translatable='yes'>_New</attribute>"
" <attribute name='action'>example.new</attribute>"
" </item>"
" <item>"
" <attribute name='label' translatable='yes'>_Open</attribute>"
" <attribute name='action'>example.open</attribute>"
" </item>"
" </section>"
" <section>"
" <item>"
" <attribute name='label' translatable='yes'>Rain</attribute>"
" <attribute name='action'>example.rain</attribute>"
" </item>"
" </section>"
" <section>"
" <item>"
" <attribute name='label' translatable='yes'>_Quit</attribute>"
" <attribute name='action'>example.quit</attribute>"
" </item>"
" </section>"
" </submenu>"
" <submenu>"
" <attribute name='label' translatable='yes'>_Edit</attribute>"
" <item>"
" <attribute name='label' translatable='yes'>_Cut</attribute>"
" <attribute name='action'>example.cut</attribute>"
" </item>"
" <item>"
" <attribute name='label' translatable='yes'>_Copy</attribute>"
" <attribute name='action'>example.copy</attribute>"
" </item>"
" <item>"
" <attribute name='label' translatable='yes'>_Paste</attribute>"
" <attribute name='action'>example.paste</attribute>"
" </item>"
" </submenu>"
" </menu>"
"</interface>";
// Set accelerator keys:
app->set_accel_for_action("example.new", "<Primary>n");
app->set_accel_for_action("example.open", "<Primary>o");
app->set_accel_for_action("example.quit", "<Primary>q");
app->set_accel_for_action("example.cut", "<Primary>x");
app->set_accel_for_action("example.copy", "<Primary>c");
app->set_accel_for_action("example.paste", "<Primary>v");
try
{
m_refBuilder->add_from_string(ui_info);
m_refBuilder->add_from_resource("/toolbar/toolbar.ui");
}
catch(const Glib::Error& ex)
{
std::cerr << "Building menus and toolbar failed: " << ex.what();
}
//Get the menubar:
auto gmenu = m_refBuilder->get_object<Gio::Menu>("menubar");
if (!gmenu)
g_warning("GMenu not found");
else
{
auto pMenuBar = Gtk::make_managed<Gtk::PopoverMenuBar>(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->get_widget<Gtk::Box>("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->make_window_and_run().
}
//void ExampleWindow::on_action_file_new()
//{
// std::cout << "A File|New menu item was selected." << std::endl;
//}
void ExampleWindow::on_action_others()
{
std::cout << "A menu item was selected." << std::endl;
}
void ExampleWindow::on_action_toggle()
{
std::cout << "The toggle menu item was selected." << std::endl;
bool active = false;
m_refActionRain->get_state(active);
//The toggle action's state does not change automatically:
active = !active;
m_refActionRain->change_state(active);
Glib::ustring message;
if(active)
message = "Toggle is active.";
else
message = "Toggle is not active";
std::cout << message << 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 <gtkmm.h>
#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->make_window_and_run<ExampleWindow>(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"><?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/toolbar">
<file preprocess="xml-stripblanks">toolbar.ui</file>
<file>rain.png</file>
</gresource>
</gresources>
</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 <gtkmm.h>
#include <memory>
class ExampleWindow : public Gtk::Window
{
public:
ExampleWindow(const Glib::RefPtr<Gtk::Application>& 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<Gtk::Builder> m_refBuilder;
Glib::RefPtr<Gtk::GestureClick> 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 <iostream>
ExampleWindow::ExampleWindow(const Glib::RefPtr<Gtk::Application>& 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->set_button(GDK_BUTTON_SECONDARY);
m_refGesture->signal_pressed().connect(
sigc::mem_fun(*this, &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->add_action("edit",
sigc::mem_fun(*this, &ExampleWindow::on_menu_file_popup_generic));
refActionGroup->add_action("process",
sigc::mem_fun(*this, &ExampleWindow::on_menu_file_popup_generic));
refActionGroup->add_action("remove",
sigc::mem_fun(*this, &ExampleWindow::on_menu_file_popup_generic));
insert_action_group("examplepopup", refActionGroup);
// Set accelerator keys:
app->set_accel_for_action("examplepopup.edit", "<Primary>e");
app->set_accel_for_action("examplepopup.process", "<Primary>p");
app->set_accel_for_action("examplepopup.remove", "<Primary>r");
m_refBuilder = Gtk::Builder::create();
//Layout the actions in a menubar and toolbar:
Glib::ustring ui_info =
"<interface>"
" <menu id='menu-examplepopup'>"
" <section>"
" <item>"
" <attribute name='label' translatable='yes'>Edit</attribute>"
" <attribute name='action'>examplepopup.edit</attribute>"
" </item>"
" <item>"
" <attribute name='label' translatable='yes'>Process</attribute>"
" <attribute name='action'>examplepopup.process</attribute>"
" </item>"
" <item>"
" <attribute name='label' translatable='yes'>Remove</attribute>"
" <attribute name='action'>examplepopup.remove</attribute>"
" </item>"
" </section>"
" </menu>"
"</interface>";
try
{
m_refBuilder->add_from_string(ui_info);
}
catch(const Glib::Error& ex)
{
std::cerr << "building menus failed: " << ex.what();
}
//Get the menu:
auto gmenu = m_refBuilder->get_object<Gio::Menu>("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 << "A popup menu item was selected." << 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 <gtkmm/application.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->make_window_and_run<ExampleWindow>(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>
|