[glom] Port from Gtk::Widget::override*color() to CssProvider.



commit a621388eaad431da5bdba4fe2870ff1b9789550d
Author: Murray Cumming <murrayc murrayc com>
Date:   Tue Feb 10 12:39:15 2015 +0100

    Port from Gtk::Widget::override*color() to CssProvider.
    
    Because the gtk_widget_override_*() functions are deprecated.

 glom/mode_data/datawidget/entry.cc                 |    9 ++-
 glom/mode_design/dialog_database_preferences.cc    |    3 +-
 .../layout_item_dialogs/dialog_buttonscript.cc     |    3 +-
 glom/utility_widgets/layoutwidgetbase.cc           |   20 +---
 glom/utils_ui.cc                                   |  103 ++++++++++++++++++++
 glom/utils_ui.h                                    |    5 +
 6 files changed, 125 insertions(+), 18 deletions(-)
---
diff --git a/glom/mode_data/datawidget/entry.cc b/glom/mode_data/datawidget/entry.cc
index 0f2505f..c4dfeba 100644
--- a/glom/mode_data/datawidget/entry.cc
+++ b/glom/mode_data/datawidget/entry.cc
@@ -24,6 +24,7 @@
 #include <glom/dialog_invalid_data.h>
 #include <libglom/data_structure/glomconversions.h>
 #include <glom/appwindow.h>
+#include <glom/utils_ui.h>
 #include <glibmm/i18n.h>
 //#include <sstream> //For stringstream
 
@@ -160,9 +161,13 @@ void Entry::set_value(const Gnome::Gda::Value& value)
     const Glib::ustring fg_color = 
     layout_item->get_formatting_used().get_text_format_color_foreground_to_use(value);
     if(!fg_color.empty())
-      override_color(Gdk::RGBA(fg_color));
+    {
+      UiUtils::load_color_into_css_provider(*this, fg_color);
+    }
     else
-      unset_color();
+    {
+      //TOOD: unset_color();
+    }
   }
 }
 
diff --git a/glom/mode_design/dialog_database_preferences.cc b/glom/mode_design/dialog_database_preferences.cc
index 842dba7..bbfe209 100644
--- a/glom/mode_design/dialog_database_preferences.cc
+++ b/glom/mode_design/dialog_database_preferences.cc
@@ -24,6 +24,7 @@
 #include <libglom/data_structure/glomconversions.h>
 #include <libglom/db_utils.h>
 #include <glom/bakery/busy_cursor.h>
+#include <glom/utils_ui.h>
 #include <gtksourceviewmm/languagemanager.h>
 #include <glibmm/i18n.h>
 
@@ -82,7 +83,7 @@ Dialog_Database_Preferences::Dialog_Database_Preferences(BaseObjectType* cobject
     sigc::mem_fun(*this, &Dialog_Database_Preferences::on_button_test_script) );
 
   // Set a monospace font
-  m_text_view_script->override_font(Pango::FontDescription("Monospace"));
+  UiUtils::load_font_into_css_provider(*m_text_view_script, "Monospace");
 
   //Dialog_Properties::set_modified(false);
 
diff --git a/glom/mode_design/layout/layout_item_dialogs/dialog_buttonscript.cc 
b/glom/mode_design/layout/layout_item_dialogs/dialog_buttonscript.cc
index dc646d2..8f9a89c 100644
--- a/glom/mode_design/layout/layout_item_dialogs/dialog_buttonscript.cc
+++ b/glom/mode_design/layout/layout_item_dialogs/dialog_buttonscript.cc
@@ -23,6 +23,7 @@
 #include <glom/python_embed/glom_python.h>
 #include <glom/utils_ui.h>
 #include <glom/appwindow.h>
+#include <glom/utils_ui.h>
 #include <libglom/data_structure/glomconversions.h>
 #include <gtksourceviewmm/languagemanager.h>
 
@@ -46,7 +47,7 @@ Dialog_ButtonScript::Dialog_ButtonScript(BaseObjectType* cobject, const Glib::Re
   m_button_test_script->signal_clicked().connect( sigc::mem_fun(*this, 
&Dialog_ButtonScript::on_button_test_script) );
 
   // Set a monospace font
-  m_text_view_script->override_font(Pango::FontDescription("Monospace"));
+  UiUtils::load_font_into_css_provider(*m_text_view_script, "Monospace");
   //connect_each_widget(this);
 
   //Dialog_Properties::set_modified(false);
diff --git a/glom/utility_widgets/layoutwidgetbase.cc b/glom/utility_widgets/layoutwidgetbase.cc
index 48dff1f..ed0d0c5 100644
--- a/glom/utility_widgets/layoutwidgetbase.cc
+++ b/glom/utility_widgets/layoutwidgetbase.cc
@@ -23,6 +23,7 @@
 #include <glom/appwindow.h>
 #include <glom/mode_data/datawidget/textview.h>
 #include <glom/mode_data/datawidget/label.h>
+#include <glom/utils_ui.h>
 #include <iostream>
 
 namespace Glom
@@ -158,42 +159,33 @@ void LayoutWidgetBase::apply_formatting(Gtk::Widget& widget, const sharedptr<con
   const Glib::ustring font_desc = formatting.get_text_format_font();
   if(!font_desc.empty())
   {
-    widget_to_change->override_font( Pango::FontDescription(font_desc) );
+    UiUtils::load_font_into_css_provider(*widget_to_change, font_desc);
   }
 
   
   const Glib::ustring fg = formatting.get_text_format_color_foreground();
   if(!fg.empty())
   {
-    // "text" is the text color. (Works for GtkEntry and GtkTextView, 
-    // for which override_color() doesn't seem to have any effect.
-    widget_to_change->override_color(Gdk::RGBA(fg));
-    
-    // This works for GtkLabel, for which override_color() does not.
-    widget_to_change->override_color(Gdk::RGBA(fg));
+    UiUtils::load_color_into_css_provider(*widget_to_change, fg);
   }
 
-
   const Glib::ustring bg = formatting.get_text_format_color_background();
   if(!bg.empty())
   {
     if(!labelglom && !button)
     {
-      // "base" is the background color for GtkEntry. "bg" seems to change the border:
-      widget_to_change->override_background_color(Gdk::RGBA(bg));
+      UiUtils::load_background_color_into_css_provider(*widget_to_change, bg);
     }
     //According to the gtk_widget_override_background_color() documentation, 
     //a GtkLabel can only have a background color by, for instance, placing it 
     //in a GtkEventBox. Luckily Label is actually derived from EventBox.
     else if(labelglom)
     {
-      //label->override_background_color(Gdk::RGBA("bg"));
-      labelglom->override_background_color(Gdk::RGBA(bg));
+      UiUtils::load_background_color_into_css_provider(*labelglom, bg);
     }
     else if(button)
     {
-      //button->override_background_color(Gdk::RGBA("bg"));
-      button->override_background_color(Gdk::RGBA(bg));
+      UiUtils::load_background_color_into_css_provider(*button, bg);
     }
   }
 }
diff --git a/glom/utils_ui.cc b/glom/utils_ui.cc
index 06965c0..fb022fc 100644
--- a/glom/utils_ui.cc
+++ b/glom/utils_ui.cc
@@ -60,6 +60,36 @@
 namespace
 {
 
+static void on_css_parsing_error(const Glib::RefPtr<const Gtk::CssSection>& section, const Glib::Error& 
error)
+{
+  std::cerr << G_STRFUNC << ": Parsing error: " << error.what() << std::endl;
+
+  if(section)
+  {
+    std::cerr << " URI = " << section->get_file()->get_uri() << std::endl;
+    std::cerr << " start_line = " << section->get_start_line()+1
+      << ", end_line = " << section->get_end_line()+1 << std::endl;
+    std::cerr << " start_position = " << section->get_start_position()
+      << ", end_position = " << section->get_end_position() << std::endl;
+  }
+}
+
+static Glib::RefPtr<Gtk::CssProvider> create_css_provider(Gtk::Widget& widget)
+{
+  // Add a StyleProvider so we can change the color, background color, and font.
+  // This was easier before Gtk::Widget::override_color() was deprecated.
+  Glib::RefPtr<Gtk::CssProvider> css_provider = Gtk::CssProvider::create();
+
+  Glib::RefPtr<Gtk::StyleContext> refStyleContext = widget.get_style_context();
+  if(refStyleContext)
+    refStyleContext->add_provider(css_provider, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
+
+  css_provider->signal_parsing_error().connect(
+    sigc::ptr_fun(&on_css_parsing_error));
+
+  return css_provider;
+}
+
 // Basically copied from libgnome (gnome-help.c, Copyright (C) 2001 Sid Vicious
 // Copyright (C) 2001 Jonathan Blandford <jrb alum mit edu>), but C++ified
 std::string locate_help_file(const std::string& path, const std::string& doc_name)
@@ -595,4 +625,77 @@ void UiUtils::container_remove_all(Gtk::Container& container)
   }
 }
 
+void UiUtils::load_font_into_css_provider(Gtk::Widget& widget, const Glib::ustring& font)
+{
+  Glib::RefPtr<Gtk::CssProvider> css_provider = create_css_provider(widget);
+
+  try
+  {
+    css_provider->load_from_data("* { font: " + font + "; }");
+  }
+  catch(const Gtk::CssProviderError& ex)
+  {
+    std::cerr << G_STRFUNC << ": Gtk::CssProvider::load_from_data() failed: "
+      << ex.what() << std::endl;
+  }
+  catch(const Glib::Error& ex)
+  {
+    std::cerr << G_STRFUNC << ": Gtk::CssProvider::load_from_data() failed: "
+      << ex.what() << std::endl;
+  }
+}
+
+void UiUtils::load_color_into_css_provider(Gtk::Widget& widget, const Glib::ustring& color)
+{
+  Glib::RefPtr<Gtk::CssProvider> css_provider = create_css_provider(widget);
+
+  try
+  {
+    css_provider->load_from_data("* { color: " + color + "; }");
+  }
+  catch(const Gtk::CssProviderError& ex)
+  {
+    std::cerr << G_STRFUNC << ": Gtk::CssProvider::load_from_data() failed: "
+      << ex.what() << std::endl;
+  }
+  catch(const Glib::Error& ex)
+  {
+    std::cerr << G_STRFUNC << ": Gtk::CssProvider::load_from_data() failed: "
+      << ex.what() << std::endl;
+  }
+}
+
+void UiUtils::load_background_color_into_css_provider(Gtk::Widget& widget, const Glib::ustring& color)
+{
+  Glib::RefPtr<Gtk::CssProvider> css_provider = create_css_provider(widget);
+
+  try
+  {
+    css_provider->load_from_data(
+      "* { background-color: " + color + "; }"
+      );
+/*
+      "GtkTextView {\n"
+      "  background-color: " + color + "; }\n"
+      "GtkTextView:backdrop {\n"
+      "  background-color: " + color + "; }\n"
+      "GtkTextView.view:selected {\n"
+      "  background-color: " + color + "; }\n"
+      "GtkTextView.view:insensitive {\n"
+      "  background-color: " + color + "; }"
+*/
+
+  }
+  catch(const Gtk::CssProviderError& ex)
+  {
+    std::cerr << G_STRFUNC << ": Gtk::CssProvider::load_from_data() failed: "
+      << ex.what() << std::endl;
+  }
+  catch(const Glib::Error& ex)
+  {
+    std::cerr << G_STRFUNC << ": Gtk::CssProvider::load_from_data() failed: "
+      << ex.what() << std::endl;
+  }
+}
+
 } //namespace Glom
diff --git a/glom/utils_ui.h b/glom/utils_ui.h
index cfd8630..d073bbb 100644
--- a/glom/utils_ui.h
+++ b/glom/utils_ui.h
@@ -24,6 +24,7 @@
 #include "config.h"
 #include <gtkmm/dialog.h>
 #include <gtkmm/treeview.h>
+#include <gtkmm/cssprovider.h>
 #include <libglom/data_structure/field.h>
 #include <libglom/data_structure/numeric_format.h>
 
@@ -107,6 +108,10 @@ void treeview_delete_all_columns(Gtk::TreeView* treeview);
 
 void container_remove_all(Gtk::Container& container);
 
+void load_font_into_css_provider(Gtk::Widget& widget, const Glib::ustring& font);
+void load_color_into_css_provider(Gtk::Widget& widget, const Glib::ustring& color);
+void load_background_color_into_css_provider(Gtk::Widget& widget, const Glib::ustring& color);
+
 } //namespace UiUtils
 
 } //namespace Glom


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