[gtkmm-documentation] Custom widget and ListBox examples: Update use of CSS files



commit 1c20792c8ef1b6992eb816861cb57180b4ba4af1
Author: Kjell Ahlstedt <kjell ahlstedt bredband net>
Date:   Fri Feb 12 16:00:00 2016 +0100

    Custom widget and ListBox examples: Update use of CSS files
    
    * examples/book/custom/custom_widget/custom_gtk.css: Use widget instance name
    instead of class name.
    * examples/book/custom/custom_widget/mywidget.cc: Call Gtk::Widget::set_name().
    Explain the alternative names for use in the CSS file.
    CSS node names are introduced in gtk+ 3.19. They largely replace the class
    names in CSS files.
    * examples/book/listbox/examplewindow.[cc|h]: Remove the CSS data. Gtk+ now
    has reasonable style sheets for GtkListBoxRow.

 examples/book/custom/custom_widget/custom_gtk.css |    2 +-
 examples/book/custom/custom_widget/mywidget.cc    |   15 ++++++++++
 examples/book/listbox/examplewindow.cc            |   32 ---------------------
 examples/book/listbox/examplewindow.h             |    1 -
 4 files changed, 16 insertions(+), 34 deletions(-)
---
diff --git a/examples/book/custom/custom_widget/custom_gtk.css 
b/examples/book/custom/custom_widget/custom_gtk.css
index 8b8d349..dbdd7f7 100644
--- a/examples/book/custom/custom_widget/custom_gtk.css
+++ b/examples/book/custom/custom_widget/custom_gtk.css
@@ -9,7 +9,7 @@
   -gtkmm__CustomObject_mywidget-example-scale: 920;
 }
 
-gtkmm__CustomObject_mywidget {
+#my-widget {
   background-color: rgb(255,0,0);
   color:            rgb(0,0,255);
 }
diff --git a/examples/book/custom/custom_widget/mywidget.cc b/examples/book/custom/custom_widget/mywidget.cc
index 0a263f6..280400a 100644
--- a/examples/book/custom/custom_widget/mywidget.cc
+++ b/examples/book/custom/custom_widget/mywidget.cc
@@ -41,6 +41,21 @@ MyWidget::MyWidget() :
   //This shows that the GType still derives from GtkWidget:
   //std::cout << "Gtype is a GtkWidget?:" << GTK_IS_WIDGET(gobj()) << std::endl;
 
+  // Set the widget name to use in the CSS file.
+  set_name("my-widget");
+
+  // If you make a custom widget in C code, based on gtk+'s GtkWidget, there is
+  // an alternative to gtk_widget_set_name(): Set a CSS name for your custom
+  // class (instead of the widget instance) with gtk_widget_class_set_css_name()
+  // (new in gtk+ 3.19.1). That's not possible for custom widgets defined in gtkmm.
+  // gtk_widget_class_set_css_name() must be called in the class init function,
+  // which can't be customized, when the widget is based on gtkmm's Gtk::Widget.
+  //
+  // Another alternative: The custom widget inherits the CSS name "widget" from
+  // GtkWidget. That name can be used in the CSS file. This is not a very good
+  // alternative. GtkWidget's CSS name is not documented. It can probably be
+  // changed or removed in the future.
+
   m_refCssProvider = Gtk::CssProvider::create();
   auto refStyleContext = get_style_context();
   refStyleContext->add_provider(m_refCssProvider,
diff --git a/examples/book/listbox/examplewindow.cc b/examples/book/listbox/examplewindow.cc
index e984909..b16d01e 100644
--- a/examples/book/listbox/examplewindow.cc
+++ b/examples/book/listbox/examplewindow.cc
@@ -18,22 +18,6 @@
 
 namespace
 {
-const char css[] =
-  "GtkListBoxRow {"
-  " border-width: 1px;"
-  " border-style: solid;"
-  " border-color: blue;"
-  "}"
-  "GtkListBoxRow:prelight {"
-  "background-color: green;"
-  "}"
-  "GtkListBoxRow:selected {"
-  "background-color: yellow;"
-  "}"
-  "GtkListBoxRow:active {"
-  "background-color: red;"
-  "}";
-
 struct SelectionModeStruct
 {
   Gtk::SelectionMode mode;
@@ -79,22 +63,6 @@ ExampleWindow::ExampleWindow() :
   set_border_width(5);
   set_default_size(300, 300);
 
-  // Add a style sheet for GtkListBoxRow.
-  // (2013-10-01: If this is not done, ListBoxRows look dull, and selection
-  // is not marked in any way. I suppose this is necessary only temporarily.
-  // It ought to be part of gtk+.)
-  m_refCssProvider = Gtk::CssProvider::create();
-  Gtk::StyleContext::add_provider_for_screen(get_screen(), m_refCssProvider,
-    GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
-  try
-  {
-    m_refCssProvider->load_from_data(css);
-  }
-  catch (const Glib::Error& ex)
-  {
-    std::cerr << "Gtk::CssProvider::load_from_data() failed: " << ex.what() << std::endl;
-  }
-
   add(m_HBox);
   m_HBox.pack_start(m_VBox1, Gtk::PACK_SHRINK);
 
diff --git a/examples/book/listbox/examplewindow.h b/examples/book/listbox/examplewindow.h
index 8ad3933..fb3b329 100644
--- a/examples/book/listbox/examplewindow.h
+++ b/examples/book/listbox/examplewindow.h
@@ -44,7 +44,6 @@ private:
   static void update_header_func(Gtk::ListBoxRow* row, Gtk::ListBoxRow* before);
 
   // Member data.
-  Glib::RefPtr<Gtk::CssProvider> m_refCssProvider;
   Gtk::Box m_HBox;
   Gtk::Box m_VBox1;
   Gtk::Box m_VBox2;


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