[gtkmm-documentation/gmenu] gmenu: Also demonstrate a toggle action



commit a6a96844e61b52606d2daf6988ea1c322e2d4bac
Author: Murray Cumming <murrayc murrayc com>
Date:   Wed Aug 7 15:13:12 2013 +0200

    gmenu: Also demonstrate a toggle action

 examples/book/menus/main_menu/examplewindow.cc |   30 ++++++++++++++++++++++++
 examples/book/menus/main_menu/examplewindow.h  |    3 ++
 2 files changed, 33 insertions(+), 0 deletions(-)
---
diff --git a/examples/book/menus/main_menu/examplewindow.cc b/examples/book/menus/main_menu/examplewindow.cc
index 7debfc7..24b02ed 100644
--- a/examples/book/menus/main_menu/examplewindow.cc
+++ b/examples/book/menus/main_menu/examplewindow.cc
@@ -65,6 +65,10 @@ ExampleWindow::ExampleWindow()
     sigc::mem_fun(*this, &ExampleWindow::on_menu_choices_other),
     1);
 
+  m_refToggle = refActionGroup->add_action_bool("sometoggle",
+    sigc::mem_fun(*this, &ExampleWindow::on_menu_toggle),
+    false);
+
 
   //Help menu:
   refActionGroup->add_action("about",
@@ -149,6 +153,12 @@ ExampleWindow::ExampleWindow()
     "          <attribute name='target' type='i'>2</attribute>"
     "        </item>"
     "      </section>"
+    "      <section>"
+    "        <item>"
+    "          <attribute name='label' translatable='yes'>Some Toggle</attribute>"
+    "          <attribute name='action'>example.sometoggle</attribute>"
+    "        </item>"
+    "      </section>"
     "    </submenu>"
     "    <submenu>"
     "      <attribute name='label' translatable='yes'>_Help</attribute>"
@@ -235,6 +245,7 @@ void ExampleWindow::on_menu_choices(const Glib::ustring& parameter)
 
 void ExampleWindow::on_menu_choices_other(int parameter)
 {
+  //The radio action's state does not change automatically:
   m_refChoice->set_state(
    Glib::Variant<int>::create(parameter) );
 
@@ -247,3 +258,22 @@ void ExampleWindow::on_menu_choices_other(int parameter)
   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:
+  m_refToggle->set_state(
+   Glib::Variant<bool>::create(!active) );
+  active = !active;
+
+  Glib::ustring message;
+  if(active)
+    message = "Toggle is active.";
+  else
+    message = "Toggle is not active";
+
+  std::cout << message << std::endl;
+}
+
diff --git a/examples/book/menus/main_menu/examplewindow.h b/examples/book/menus/main_menu/examplewindow.h
index b676ded..9bd84c5 100644
--- a/examples/book/menus/main_menu/examplewindow.h
+++ b/examples/book/menus/main_menu/examplewindow.h
@@ -35,6 +35,7 @@ protected:
 
   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;
@@ -43,6 +44,8 @@ protected:
 
   //Two sets of choices:
   Glib::RefPtr<Gio::SimpleAction> m_refChoice, m_refChoiceOther;
+
+  Glib::RefPtr<Gio::SimpleAction> m_refToggle;
 };
 
 #endif //GTKMM_EXAMPLEWINDOW_H


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]