Commit a621388e authored by Murray Cumming's avatar Murray Cumming
Browse files

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

Because the gtk_widget_override_*() functions are deprecated.
parent b52d7657
......@@ -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();
}
}
}
......
......@@ -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);
......
......@@ -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);
......
......@@ -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);
}
}
}
......
......@@ -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
......@@ -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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment