[gtkmm-documentation] builder/derived example: Show Gtk::Builder combined with Glib::Property



commit 5d8e8b34619e6cf0e0dbac118f203d4dc4205a93
Author: Kjell Ahlstedt <kjellahlstedt gmail com>
Date:   Thu Jun 27 18:34:38 2019 +0200

    builder/derived example: Show Gtk::Builder combined with Glib::Property
    
    Add a DerivedButton that contains Glib::Property members.

 examples/Makefile.am                           |  2 +
 examples/book/builder/derived/derived.glade    |  4 +-
 examples/book/builder/derived/derivedbutton.cc | 60 ++++++++++++++++++++++++++
 examples/book/builder/derived/derivedbutton.h  | 38 ++++++++++++++++
 examples/book/builder/derived/deriveddialog.cc | 12 ++++--
 examples/book/builder/derived/deriveddialog.h  |  5 ++-
 examples/book/builder/derived/main.cc          |  7 +++
 examples/book/meson.build                      |  2 +-
 8 files changed, 123 insertions(+), 7 deletions(-)
---
diff --git a/examples/Makefile.am b/examples/Makefile.am
index 67ee852..0c81e14 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -193,6 +193,8 @@ book_builder_basic_example_SOURCES =                \
 book_builder_derived_example_SOURCES =         \
        book/builder/derived/deriveddialog.cc   \
        book/builder/derived/deriveddialog.h    \
+       book/builder/derived/derivedbutton.cc   \
+       book/builder/derived/derivedbutton.h    \
        book/builder/derived/main.cc
 
 book_buttons_button_buttons_SOURCES =          \
diff --git a/examples/book/builder/derived/derived.glade b/examples/book/builder/derived/derived.glade
index 9f34638..d93132f 100644
--- a/examples/book/builder/derived/derived.glade
+++ b/examples/book/builder/derived/derived.glade
@@ -16,11 +16,13 @@
           <object class="GtkBox" id="dialog-action_area2">
             <property name="can_focus">False</property>
             <child>
-              <object class="GtkButton" id="quit_button">
+              <object class="gtkmm__CustomObject_MyButton" id="quit_button">
                 <property name="label">_Quit</property>
                 <property name="can_focus">True</property>
                 <property name="receives_default">False</property>
                 <property name="use_underline">True</property>
+                <property name="button-ustring">Button with extra properties</property>
+                <property name="button-int">85</property>
               </object>
             </child>
           </object>
diff --git a/examples/book/builder/derived/derivedbutton.cc b/examples/book/builder/derived/derivedbutton.cc
new file mode 100644
index 0000000..16bcc76
--- /dev/null
+++ b/examples/book/builder/derived/derivedbutton.cc
@@ -0,0 +1,60 @@
+/* gtkmm example Copyright (C) 2019 gtkmm development team
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "derivedbutton.h"
+#include <iostream>
+
+namespace
+{
+void on_ustring_changed()
+{
+  std::cout << "- ustring property changed!" << std::endl;
+}
+
+void on_int_changed()
+{
+  std::cout << "- int property changed!" << std::endl;
+}
+} // anonymous namespace
+
+// For creating a dummy object in main.cc.
+DerivedButton::DerivedButton()
+: Glib::ObjectBase("MyButton"),
+  prop_ustring(*this, "button-ustring"),
+  prop_int(*this, "button-int", 10)
+{
+}
+
+DerivedButton::DerivedButton(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& /* refGlade */)
+: // To register custom properties, you must register a custom GType.  If
+  // you don't know what that means, don't worry, just remember to add
+  // this Glib::ObjectBase constructor call to your class' constructor.
+  // The GType name will be gtkmm__CustomObject_MyButton.
+  Glib::ObjectBase("MyButton"),
+  Gtk::Button(cobject),
+  // register the properties with the object and give them names
+  prop_ustring(*this, "button-ustring"),
+  // this one has a default value
+  prop_int(*this, "button-int", 10)
+{
+  // Register some handlers that will be called when the values of the
+  // specified parameters are changed.
+  property_ustring().signal_changed().connect(sigc::ptr_fun(&on_ustring_changed));
+  property_int().signal_changed().connect(sigc::ptr_fun(&on_int_changed));
+}
+
+DerivedButton::~DerivedButton()
+{
+}
diff --git a/examples/book/builder/derived/derivedbutton.h b/examples/book/builder/derived/derivedbutton.h
new file mode 100644
index 0000000..f07d987
--- /dev/null
+++ b/examples/book/builder/derived/derivedbutton.h
@@ -0,0 +1,38 @@
+/* gtkmm example Copyright (C) 2019 gtkmm development team
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GTKMM_EXAMPLE_DERIVED_BUTTON_H
+#define GTKMM_EXAMPLE_DERIVED_BUTTON_H
+
+#include <gtkmm.h>
+
+class DerivedButton : public Gtk::Button
+{
+public:
+  DerivedButton();
+  DerivedButton(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& refGlade);
+  virtual ~DerivedButton();
+
+  // Provide proxies for the properties. The proxy allows you to connect to
+  // the 'changed' signal, etc.
+  Glib::PropertyProxy<Glib::ustring> property_ustring() { return prop_ustring.get_proxy(); }
+  Glib::PropertyProxy<int> property_int() { return prop_int.get_proxy(); }
+
+private:
+  Glib::Property<Glib::ustring> prop_ustring;
+  Glib::Property<int> prop_int;
+};
+
+#endif //GTKMM_EXAMPLE_DERIVED_BUTTON_H
diff --git a/examples/book/builder/derived/deriveddialog.cc b/examples/book/builder/derived/deriveddialog.cc
index e41e720..7019207 100644
--- a/examples/book/builder/derived/deriveddialog.cc
+++ b/examples/book/builder/derived/deriveddialog.cc
@@ -15,17 +15,23 @@
  */
 
 #include "deriveddialog.h"
+#include <iostream>
 
 DerivedDialog::DerivedDialog(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& refGlade)
 : Gtk::Dialog(cobject),
   m_refGlade(refGlade),
   m_pButton(nullptr)
 {
-  //Get the Glade-instantiated Button, and connect a signal handler:
-  m_pButton = m_refGlade->get_widget<Gtk::Button>("quit_button");
-  if(m_pButton)
+  // Get the Glade-instantiated Button, and connect a signal handler:
+  m_pButton = Gtk::Builder::get_widget_derived<DerivedButton>(m_refGlade, "quit_button");
+  if (m_pButton)
   {
     m_pButton->signal_clicked().connect( sigc::mem_fun(*this, &DerivedDialog::on_button_quit) );
+    std::cout << "ustring, int: " << m_pButton->property_ustring()
+              << ", " << m_pButton->property_int() << std::endl;
+    m_pButton->property_int() = 99;
+    std::cout << "ustring, int: " << m_pButton->property_ustring()
+              << ", " << m_pButton->property_int() << std::endl;
   }
 }
 
diff --git a/examples/book/builder/derived/deriveddialog.h b/examples/book/builder/derived/deriveddialog.h
index 252dad3..17d0a1b 100644
--- a/examples/book/builder/derived/deriveddialog.h
+++ b/examples/book/builder/derived/deriveddialog.h
@@ -18,6 +18,7 @@
 #define GTKMM_EXAMPLE_DERIVED_DIALOG_H
 
 #include <gtkmm.h>
+#include "derivedbutton.h"
 
 class DerivedDialog : public Gtk::Dialog
 {
@@ -32,7 +33,7 @@ protected:
   void on_button_quit();
 
   Glib::RefPtr<Gtk::Builder> m_refGlade;
-  Gtk::Button* m_pButton;
+  DerivedButton* m_pButton;
 };
 
-#endif //GTKMM_EXAMPLE_DERIVED_WINDOW_H
+#endif //GTKMM_EXAMPLE_DERIVED_DIALOG_H
diff --git a/examples/book/builder/derived/main.cc b/examples/book/builder/derived/main.cc
index 6ef78d2..a8f1970 100644
--- a/examples/book/builder/derived/main.cc
+++ b/examples/book/builder/derived/main.cc
@@ -18,6 +18,9 @@
 #include <iostream>
 #include <cstring>
 
+// Not really used anywhere, but force an instance to be created.
+DerivedButton* my_globally_accessible_button = nullptr;
+
 int main (int argc, char **argv)
 {
   bool show_icon = false;
@@ -41,6 +44,10 @@ int main (int argc, char **argv)
 
   auto app = Gtk::Application::create("org.gtkmm.example");
 
+  // Create a dummy instance before the call to refBuilder->add_from_file().
+  // This creation registers DerivedButton's class in the GType system.
+  my_globally_accessible_button = new DerivedButton();
+
   //Load the Glade file and instantiate its widgets:
   auto refBuilder = Gtk::Builder::create();
   try
diff --git a/examples/book/meson.build b/examples/book/meson.build
index 4ee21b5..b6e015d 100644
--- a/examples/book/meson.build
+++ b/examples/book/meson.build
@@ -22,7 +22,7 @@ examples_book = [
   [['base'], 'base', ['base.cc']],
   [['box'], 'example', exwindow_main + ['packbox.cc']],
   [['builder', 'basic'], 'example', ['main.cc']],
-  [['builder', 'derived'], 'example', ['deriveddialog.cc', 'main.cc']],
+  [['builder', 'derived'], 'example', ['deriveddialog.cc', 'derivedbutton.cc', 'main.cc']],
   [['buttons', 'button'], 'buttons', ['buttons.cc', 'main.cc']],
   [['buttons', 'checkbutton'], 'example', exwindow_main],
   [['buttons', 'filechooserbutton'], 'example', exwindow_main],


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