[gtkmm/wip/dboles/Builder-test-derived-properties] tests/builder: Test derived props declared in C++,



commit 7907faf2945d0424839443902653de736aaeb534
Author: Daniel Boles <dboles src gnome org>
Date:   Sun Nov 24 17:25:24 2019 +0000

    tests/builder: Test derived props declared in C++,
    
    by giving DerivedButton one and then setting it in the Builder .ui file.

 tests/builder/main.cc | 55 ++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 50 insertions(+), 5 deletions(-)
---
diff --git a/tests/builder/main.cc b/tests/builder/main.cc
index 3c35c952..b4c5532e 100644
--- a/tests/builder/main.cc
+++ b/tests/builder/main.cc
@@ -40,11 +40,12 @@ const char gladefile[] =
         "<property name='can_focus'>False</property>"
         "<property name='orientation'>vertical</property>"
         "<child>"
-          "<object class='GtkButton' id='derived_button'>"
+          "<object class='gtkmm__CustomObject_DerivedButton' id='derived_button'>"
             "<property name='label' translatable='yes'>DerivedButton</property>"
             "<property name='visible'>True</property>"
             "<property name='can_focus'>True</property>"
             "<property name='receives_default'>True</property>"
+            "<property name='background'>#008080</property>"
           "</object>"
           "<packing>"
             "<property name='expand'>False</property>"
@@ -91,11 +92,50 @@ void* on_orphaned_button_deleted(void* /* data */)
 class DerivedButton : public Gtk::Button
 {
 public:
+  // This ctor is mainly for instantiating a dummy instance to register w/ GType
+  // but it might as well construct a usable instance, hence construct_impl().
+  DerivedButton(const Glib::ustring& icon_name = {})
+  : Glib::ObjectBase("DerivedButton")
+  , m_property_background{*this, "background"}
+  , m_cssProvider_background{ Gtk::CssProvider::create() }
+  {
+    std::cout << "DerivedButton::ctor1" << std::endl;
+    construct_impl(icon_name);
+  }
+
   DerivedButton(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& /* refBuilder */,
                 const Glib::ustring& icon_name = {})
-  : Gtk::Button(cobject)
+  : Glib::ObjectBase("DerivedButton")
+  , Gtk::Button(cobject)
+  , m_property_background{*this, "background"}
+  , m_cssProvider_background{ Gtk::CssProvider::create() }
   {
-    std::cout << "DerivedButton::ctor" << std::endl;
+    std::cout << "DerivedButton::ctor2" << std::endl;
+    construct_impl(icon_name);
+  }
+
+  Glib::PropertyProxy_ReadOnly<Glib::ustring> property_background() const { return 
m_property_background.get_proxy(); }
+  Glib::PropertyProxy         <Glib::ustring> property_background()       { return 
m_property_background.get_proxy(); }
+  Glib::ustring get_background() { return m_property_background.get_value(); }
+  void set_background(const Glib::ustring& background){ m_property_background.set_value(background); }
+
+  virtual ~DerivedButton()
+  {
+    std::cout << "DerivedButton::dtor" << std::endl;
+  }
+
+private:
+  Glib::Property<Glib::ustring> m_property_background;
+  Glib::RefPtr<Gtk::CssProvider> m_cssProvider_background;
+
+  void construct_impl(const Glib::ustring& icon_name)
+  {
+    get_style_context()->add_provider(
+      m_cssProvider_background, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
+
+    on_background_changed();
+    property_background().signal_changed().connect(
+     sigc::mem_fun(*this, &DerivedButton::on_background_changed));
 
     if (!icon_name.empty())
     {
@@ -104,9 +144,12 @@ public:
     }
   }
 
-  virtual ~DerivedButton()
+  void on_background_changed()
   {
-    std::cout << "DerivedButton::dtor" << std::endl;
+    const auto background = get_background();
+    const auto css = background.empty() ? Glib::ustring()
+                     : Glib::ustring::compose("button { background: %1; }", get_background());
+    m_cssProvider_background->load_from_data(css);
   }
 };
 
@@ -157,6 +200,8 @@ int main(int argc, char* argv[])
 
   Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc1, argv);
 
+  // Ensure custom GType and properties are registered w/ GObject before Builder
+  static_cast<void>(DerivedButton());
   Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_string(gladefile);
 
   MainWindow* main_win = nullptr;


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