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



commit 559a7cb8744fd390f2e05df8a931b7f6b0369d80
Author: Daniel Boles <dboles src gnome org>
Date:   Mon Nov 25 06:59:49 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 | 53 +++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 51 insertions(+), 2 deletions(-)
---
diff --git a/tests/builder/main.cc b/tests/builder/main.cc
index 3c35c952..ecacd4c6 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,12 +92,27 @@ void* on_orphaned_button_deleted(void* /* data */)
 class DerivedButton : public Gtk::Button
 {
 public:
+  static void ensure_type()
+  {
+    std::cout << "DerivedButton::ensure_type()" << std::endl;
+    static_cast<void>(DerivedButton());
+  }
+
   DerivedButton(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& /* refBuilder */,
                 const Glib::ustring& icon_name = {})
-  : Gtk::Button(cobject)
+  : Glib::ObjectBase(s_type_name)
+  , Gtk::Button(cobject)
+  , m_cssProvider_background{ Gtk::CssProvider::create() }
   {
     std::cout << "DerivedButton::ctor" << std::endl;
 
+    get_style_context()->add_provider(
+      m_cssProvider_background, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
+
+    apply_background();
+    property_background().signal_changed().connect(
+     sigc::mem_fun(*this, &DerivedButton::apply_background));
+
     if (!icon_name.empty())
     {
       set_image_from_icon_name(icon_name);
@@ -104,10 +120,42 @@ public:
     }
   }
 
+  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() const { 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:
+  static constexpr auto s_type_name = "DerivedButton";
+  Glib::Property<Glib::ustring> m_property_background{*this, "background"};
+
+  const Glib::RefPtr<Gtk::CssProvider> m_cssProvider_background;
+
+  DerivedButton()
+  : Glib::ObjectBase(s_type_name)
+  {
+    std::cout << "DerivedButton::ctor to register type" << std::endl;
+  }
+
+  void apply_background()
+  {
+    const auto background = get_background();
+    std::cout << "apply_background(\"" << background << "\")" << std::endl;
+
+    const auto css = background.empty() ? Glib::ustring()
+                     : Glib::ustring::compose("button { background: %1; }", get_background());
+
+    try {
+      m_cssProvider_background->load_from_data(css);
+    } catch (const Gtk::CssProviderError&) {
+      std::cout << "invalid CSS, but I let you keep typing!" << std::endl;
+    }
+  }
 };
 
 class MainWindow : public Gtk::Window
@@ -157,6 +205,7 @@ int main(int argc, char* argv[])
 
   Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc1, argv);
 
+  DerivedButton::ensure_type();
   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]