parasite: Use template for css editor

This commit is contained in:
Matthias Clasen 2014-05-04 14:53:17 -04:00
parent 3c22fc4ef6
commit 0eb330613b
5 changed files with 162 additions and 166 deletions

View File

@ -23,15 +23,14 @@
#include "css-editor.h" #include "css-editor.h"
#include "parasite.h" #include "parasite.h"
#define PARASITE_CSSEDITOR_TEXT "parasite-csseditor-text" #define PARASITE_CSS_EDITOR_TEXT "parasite-css-editor-text"
#define PARASITE_CSSEDITOR_PROVIDER "parasite-csseditor-provider" #define PARASITE_CSS_EDITOR_PROVIDER "parasite-css-editor-provider"
enum enum
{ {
COLUMN_ENABLED, COLUMN_ENABLED,
COLUMN_NAME, COLUMN_NAME,
COLUMN_USER, COLUMN_USER
NUM_COLUMNS
}; };
enum enum
@ -49,6 +48,7 @@ typedef struct
struct _ParasiteCssEditorPrivate struct _ParasiteCssEditorPrivate
{ {
GtkWidget *toolbar; GtkWidget *toolbar;
GtkWidget *view;
GtkTextBuffer *text; GtkTextBuffer *text;
GtkCssProvider *provider; GtkCssProvider *provider;
gboolean global; gboolean global;
@ -56,82 +56,69 @@ struct _ParasiteCssEditorPrivate
GtkToggleToolButton *disable_button; GtkToggleToolButton *disable_button;
}; };
G_DEFINE_TYPE_WITH_PRIVATE (ParasiteCssEditor, parasite_csseditor, GTK_TYPE_BOX) G_DEFINE_TYPE_WITH_PRIVATE (ParasiteCssEditor, parasite_css_editor, GTK_TYPE_BOX)
static const gchar *initial_text_global =
"/*\n"
"You can type here any CSS rule recognized by GTK+.\n"
"You can temporarily disable this custom CSS by clicking on the \"Pause\" button above.\n\n"
"Changes are applied instantly and globally, for the whole application.\n"
"*/\n\n";
static const gchar *initial_text_widget =
"/*\n"
"You can type here any CSS rule recognized by GTK+.\n"
"You can temporarily disable this custom CSS by clicking on the \"Pause\" button above.\n\n"
"Changes are applied instantly, only for this selected widget.\n"
"*/\n\n";
static void static void
disable_toggled (GtkToggleToolButton *button, ParasiteCssEditor *editor) set_initial_text (ParasiteCssEditor *editor)
{
const gchar *initial_text_global =
"/*\n"
"You can type here any CSS rule recognized by GTK+.\n"
"You can temporarily disable this custom CSS by clicking on the \"Pause\" button above.\n\n"
"Changes are applied instantly and globally, for the whole application.\n"
"*/\n\n";
const gchar *initial_text_widget =
"/*\n"
"You can type here any CSS rule recognized by GTK+.\n"
"You can temporarily disable this custom CSS by clicking on the \"Pause\" button above.\n\n"
"Changes are applied instantly, only for this selected widget.\n"
"*/\n\n";
const gchar *text = NULL;
if (editor->priv->selected_context)
text = g_object_get_data (G_OBJECT (editor->priv->selected_context), PARASITE_CSS_EDITOR_TEXT);
if (text)
gtk_text_buffer_set_text (GTK_TEXT_BUFFER (editor->priv->text), text, -1);
else if (editor->priv->global)
gtk_text_buffer_set_text (GTK_TEXT_BUFFER (editor->priv->text), initial_text_global, -1);
else
gtk_text_buffer_set_text (GTK_TEXT_BUFFER (editor->priv->text), initial_text_widget, -1);
}
static void
disable_toggled (GtkToggleToolButton *button,
ParasiteCssEditor *editor)
{ {
if (gtk_toggle_tool_button_get_active (button)) if (gtk_toggle_tool_button_get_active (button))
{ {
if (editor->priv->global) if (editor->priv->global)
{ gtk_style_context_remove_provider_for_screen (gdk_screen_get_default (),
gtk_style_context_remove_provider_for_screen (gdk_screen_get_default (), GTK_STYLE_PROVIDER (editor->priv->provider));
GTK_STYLE_PROVIDER (editor->priv->provider));
}
else if (editor->priv->selected_context) else if (editor->priv->selected_context)
{ gtk_style_context_remove_provider (editor->priv->selected_context,
gtk_style_context_remove_provider (editor->priv->selected_context, GTK_STYLE_PROVIDER (g_object_get_data (G_OBJECT (editor->priv->selected_context), PARASITE_CSS_EDITOR_PROVIDER)));
GTK_STYLE_PROVIDER (g_object_get_data (G_OBJECT (editor->priv->selected_context), PARASITE_CSSEDITOR_PROVIDER)));
}
} }
else else
{ {
if (editor->priv->global) if (editor->priv->global)
{ gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
gtk_style_context_add_provider_for_screen (gdk_screen_get_default (), GTK_STYLE_PROVIDER (editor->priv->provider),
GTK_STYLE_PROVIDER (editor->priv->provider), GTK_STYLE_PROVIDER_PRIORITY_USER);
GTK_STYLE_PROVIDER_PRIORITY_USER);
}
else if (editor->priv->selected_context) else if (editor->priv->selected_context)
{ gtk_style_context_add_provider (editor->priv->selected_context,
gtk_style_context_add_provider (editor->priv->selected_context, GTK_STYLE_PROVIDER (g_object_get_data (G_OBJECT (editor->priv->selected_context), PARASITE_CSS_EDITOR_PROVIDER)),
GTK_STYLE_PROVIDER (g_object_get_data (G_OBJECT (editor->priv->selected_context), PARASITE_CSSEDITOR_PROVIDER)), G_MAXUINT);
G_MAXUINT);
}
} }
} }
static void static void
create_toolbar (ParasiteCssEditor *editor) apply_system_font (ParasiteCssEditor *editor)
{
editor->priv->toolbar = g_object_new (GTK_TYPE_TOOLBAR,
"icon-size", GTK_ICON_SIZE_SMALL_TOOLBAR,
NULL);
gtk_container_add (GTK_CONTAINER (editor), editor->priv->toolbar);
editor->priv->disable_button = g_object_new (GTK_TYPE_TOGGLE_TOOL_BUTTON,
"icon-name", "media-playback-pause",
"tooltip-text", "Disable this custom css",
NULL);
g_signal_connect (editor->priv->disable_button,
"toggled",
G_CALLBACK (disable_toggled),
editor);
gtk_container_add (GTK_CONTAINER (editor->priv->toolbar),
GTK_WIDGET (editor->priv->disable_button));
}
static void
apply_system_font (GtkWidget *widget)
{ {
GSettings *s = g_settings_new ("org.gnome.desktop.interface"); GSettings *s = g_settings_new ("org.gnome.desktop.interface");
gchar *font_name = g_settings_get_string (s, "monospace-font-name"); gchar *font_name = g_settings_get_string (s, "monospace-font-name");
PangoFontDescription *font_desc = pango_font_description_from_string (font_name); PangoFontDescription *font_desc = pango_font_description_from_string (font_name);
gtk_widget_override_font (widget, font_desc); gtk_widget_override_font (editor->priv->view, font_desc);
pango_font_description_free (font_desc); pango_font_description_free (font_desc);
g_free (font_name); g_free (font_name);
@ -151,15 +138,16 @@ get_current_text (GtkTextBuffer *buffer)
} }
static void static void
text_changed (GtkTextBuffer *buffer, ParasiteCssEditor *editor) text_changed (GtkTextBuffer *buffer,
ParasiteCssEditor *editor)
{ {
GtkCssProvider *provider; GtkCssProvider *provider;
char *text; gchar *text;
if (editor->priv->global) if (editor->priv->global)
provider = editor->priv->provider; provider = editor->priv->provider;
else if (editor->priv->selected_context) else if (editor->priv->selected_context)
provider = g_object_get_data (G_OBJECT (editor->priv->selected_context), PARASITE_CSSEDITOR_PROVIDER); provider = g_object_get_data (G_OBJECT (editor->priv->selected_context), PARASITE_CSS_EDITOR_PROVIDER);
else else
return; return;
@ -197,42 +185,6 @@ show_parsing_error (GtkCssProvider *provider,
gtk_text_buffer_apply_tag_by_name (buffer, tag_name, &start, &end); gtk_text_buffer_apply_tag_by_name (buffer, tag_name, &start, &end);
} }
static void
create_text_widget (ParasiteCssEditor *editor)
{
GtkWidget *sw, *view;
editor->priv->text = gtk_text_buffer_new (NULL);
if (editor->priv->global)
gtk_text_buffer_set_text (GTK_TEXT_BUFFER (editor->priv->text), initial_text_global, -1);
else
gtk_text_buffer_set_text (GTK_TEXT_BUFFER (editor->priv->text), initial_text_widget, -1);
g_signal_connect (editor->priv->text, "changed", G_CALLBACK (text_changed), editor);
gtk_text_buffer_create_tag (GTK_TEXT_BUFFER (editor->priv->text),
"warning",
"underline", PANGO_UNDERLINE_SINGLE,
NULL);
gtk_text_buffer_create_tag (GTK_TEXT_BUFFER (editor->priv->text),
"error",
"underline", PANGO_UNDERLINE_ERROR,
NULL);
sw = g_object_new (GTK_TYPE_SCROLLED_WINDOW,
"expand", TRUE,
NULL);
gtk_container_add (GTK_CONTAINER (editor), sw);
view = g_object_new (GTK_TYPE_TEXT_VIEW,
"buffer", editor->priv->text,
"wrap-mode", GTK_WRAP_WORD,
NULL);
apply_system_font (view);
gtk_container_add (GTK_CONTAINER (sw), view);
}
static void static void
create_provider (ParasiteCssEditor *editor) create_provider (ParasiteCssEditor *editor)
{ {
@ -251,35 +203,29 @@ create_provider (ParasiteCssEditor *editor)
GTK_STYLE_PROVIDER (provider), GTK_STYLE_PROVIDER (provider),
G_MAXUINT); G_MAXUINT);
g_object_set_data (G_OBJECT (editor->priv->selected_context), g_object_set_data (G_OBJECT (editor->priv->selected_context),
PARASITE_CSSEDITOR_PROVIDER, PARASITE_CSS_EDITOR_PROVIDER, provider);
provider);
} }
g_signal_connect (provider, g_signal_connect (provider, "parsing-error",
"parsing-error", G_CALLBACK (show_parsing_error), editor);
G_CALLBACK (show_parsing_error),
editor);
} }
static void static void
parasite_csseditor_init (ParasiteCssEditor *editor) parasite_css_editor_init (ParasiteCssEditor *editor)
{ {
editor->priv = parasite_csseditor_get_instance_private (editor); editor->priv = parasite_css_editor_get_instance_private (editor);
gtk_widget_init_template (GTK_WIDGET (editor));
} }
static void static void
constructed (GObject *object) constructed (GObject *object)
{ {
ParasiteCssEditor *editor = PARASITE_CSSEDITOR (object); ParasiteCssEditor *editor = PARASITE_CSS_EDITOR (object);
g_object_set (editor, gtk_widget_set_sensitive (GTK_WIDGET (editor), editor->priv->global);
"orientation", GTK_ORIENTATION_VERTICAL,
"sensitive", editor->priv->global,
NULL);
create_toolbar (editor);
create_provider (editor); create_provider (editor);
create_text_widget (editor); apply_system_font (editor);
set_initial_text (editor);
} }
static void static void
@ -288,7 +234,7 @@ get_property (GObject *object,
GValue *value, GValue *value,
GParamSpec *pspec) GParamSpec *pspec)
{ {
ParasiteCssEditor *editor = PARASITE_CSSEDITOR (object); ParasiteCssEditor *editor = PARASITE_CSS_EDITOR (object);
switch (param_id) switch (param_id)
{ {
@ -308,7 +254,7 @@ set_property (GObject *object,
const GValue *value, const GValue *value,
GParamSpec *pspec) GParamSpec *pspec)
{ {
ParasiteCssEditor *editor = PARASITE_CSSEDITOR (object); ParasiteCssEditor *editor = PARASITE_CSS_EDITOR (object);
switch (param_id) switch (param_id)
{ {
@ -323,38 +269,44 @@ set_property (GObject *object,
} }
static void static void
parasite_csseditor_class_init (ParasiteCssEditorClass *klass) parasite_css_editor_class_init (ParasiteCssEditorClass *klass)
{ {
GObjectClass *object_class = G_OBJECT_CLASS (klass); GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
object_class->get_property = get_property; object_class->get_property = get_property;
object_class->set_property = set_property; object_class->set_property = set_property;
object_class->constructed = constructed; object_class->constructed = constructed;
g_object_class_install_property (object_class, g_object_class_install_property (object_class, PROP_GLOBAL,
PROP_GLOBAL, g_param_spec_boolean ("global", "Global", "Whether this editor changes the whole application or just the selected widget",
g_param_spec_boolean ("global", TRUE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
"Global",
"Whether this editor changes the whole application or just the selected widget", gtk_widget_class_set_template_from_resource (widget_class, "/org/gtk/parasite/css-editor.ui");
TRUE, gtk_widget_class_bind_template_child_private (widget_class, ParasiteCssEditor, toolbar);
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); gtk_widget_class_bind_template_child_private (widget_class, ParasiteCssEditor, text);
gtk_widget_class_bind_template_child_private (widget_class, ParasiteCssEditor, view);
gtk_widget_class_bind_template_child_private (widget_class, ParasiteCssEditor, disable_button);
gtk_widget_class_bind_template_callback (widget_class, disable_toggled);
gtk_widget_class_bind_template_callback (widget_class, text_changed);
} }
GtkWidget * GtkWidget *
parasite_csseditor_new (gboolean global) parasite_css_editor_new (gboolean global)
{ {
return GTK_WIDGET (g_object_new (PARASITE_TYPE_CSSEDITOR, return GTK_WIDGET (g_object_new (PARASITE_TYPE_CSS_EDITOR,
"global", global, "global", global,
NULL)); NULL));
} }
void void
parasite_csseditor_set_widget (ParasiteCssEditor *editor, GtkWidget *widget) parasite_css_editor_set_widget (ParasiteCssEditor *editor,
GtkWidget *widget)
{ {
gchar *text; gchar *text;
GtkCssProvider *provider; GtkCssProvider *provider;
g_return_if_fail (PARASITE_IS_CSSEDITOR (editor)); g_return_if_fail (PARASITE_IS_CSS_EDITOR (editor));
g_return_if_fail (!editor->priv->global); g_return_if_fail (!editor->priv->global);
gtk_widget_set_sensitive (GTK_WIDGET (editor), TRUE); gtk_widget_set_sensitive (GTK_WIDGET (editor), TRUE);
@ -363,26 +315,19 @@ parasite_csseditor_set_widget (ParasiteCssEditor *editor, GtkWidget *widget)
{ {
text = get_current_text (GTK_TEXT_BUFFER (editor->priv->text)); text = get_current_text (GTK_TEXT_BUFFER (editor->priv->text));
g_object_set_data_full (G_OBJECT (editor->priv->selected_context), g_object_set_data_full (G_OBJECT (editor->priv->selected_context),
PARASITE_CSSEDITOR_TEXT, PARASITE_CSS_EDITOR_TEXT,
text, text,
g_free); g_free);
} }
editor->priv->selected_context = gtk_widget_get_style_context (widget); editor->priv->selected_context = gtk_widget_get_style_context (widget);
provider = g_object_get_data (G_OBJECT (editor->priv->selected_context), PARASITE_CSSEDITOR_PROVIDER); provider = g_object_get_data (G_OBJECT (editor->priv->selected_context), PARASITE_CSS_EDITOR_PROVIDER);
if (!provider) if (!provider)
{ create_provider (editor);
create_provider (editor);
}
text = g_object_get_data (G_OBJECT (editor->priv->selected_context), PARASITE_CSSEDITOR_TEXT);
if (text)
gtk_text_buffer_set_text (GTK_TEXT_BUFFER (editor->priv->text), text, -1);
else
gtk_text_buffer_set_text (GTK_TEXT_BUFFER (editor->priv->text), initial_text_widget, -1);
set_initial_text (editor);
disable_toggled (editor->priv->disable_button, editor); disable_toggled (editor->priv->disable_button, editor);
} }
// vim: set et sw=4 ts=4: // vim: set et sw=2 ts=2:

View File

@ -20,40 +20,41 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#ifndef _GTKPARASITE_CSSEDITOR_H_ #ifndef _GTKPARASITE_CSS_EDITOR_H_
#define _GTKPARASITE_CSSEDITOR_H_ #define _GTKPARASITE_CSS_EDITOR_H_
#include <gtk/gtk.h> #include <gtk/gtk.h>
#define PARASITE_TYPE_CSSEDITOR (parasite_csseditor_get_type()) #define PARASITE_TYPE_CSS_EDITOR (parasite_css_editor_get_type())
#define PARASITE_CSSEDITOR(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), PARASITE_TYPE_CSSEDITOR, ParasiteCssEditor)) #define PARASITE_CSS_EDITOR(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), PARASITE_TYPE_CSS_EDITOR, ParasiteCssEditor))
#define PARASITE_CSSEDITOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), PARASITE_TYPE_CSSEDITOR, ParasiteCssEditorClass)) #define PARASITE_CSS_EDITOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), PARASITE_TYPE_CSS_EDITOR, ParasiteCssEditorClass))
#define PARASITE_IS_CSSEDITOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), PARASITE_TYPE_CSSEDITOR)) #define PARASITE_IS_CSS_EDITOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), PARASITE_TYPE_CSS_EDITOR))
#define PARASITE_IS_CSSEDITOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), PARASITE_TYPE_CSSEDITOR)) #define PARASITE_IS_CSS_EDITOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), PARASITE_TYPE_CSS_EDITOR))
#define PARASITE_CSSEDITOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), PARASITE_TYPE_CSSEDITOR, ParasiteCssEditorClass)) #define PARASITE_CSS_EDITOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), PARASITE_TYPE_CSS_EDITOR, ParasiteCssEditorClass))
typedef struct _ParasiteCssEditorPrivate ParasiteCssEditorPrivate; typedef struct _ParasiteCssEditorPrivate ParasiteCssEditorPrivate;
typedef struct _ParasiteCssEditor { typedef struct _ParasiteCssEditor
GtkBox parent; {
ParasiteCssEditorPrivate *priv; GtkBox parent;
ParasiteCssEditorPrivate *priv;
} ParasiteCssEditor; } ParasiteCssEditor;
typedef struct _ParasiteCssEditorClass { typedef struct _ParasiteCssEditorClass
GtkBoxClass parent; {
GtkBoxClass parent;
} ParasiteCssEditorClass; } ParasiteCssEditorClass;
G_BEGIN_DECLS G_BEGIN_DECLS
GType parasite_csseditor_get_type (); GType parasite_css_editor_get_type (void);
GtkWidget *parasite_css_editor_new (gboolean global);
GtkWidget *parasite_csseditor_new (gboolean global); void parasite_css_editor_set_widget (ParasiteCssEditor *editor,
void parasite_csseditor_set_widget (ParasiteCssEditor *editor, GtkWidget *widget);
GtkWidget *widget);
G_END_DECLS G_END_DECLS
#endif // _GTKPARASITE_CSSEDITOR_H_ #endif // _GTKPARASITE_CSS_EDITOR_H_
// vim: set et sw=4 ts=4: // vim: set et sw=2 ts=2:

View File

@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<object class="GtkTextTagTable" id="tags">
<child type="tag">
<object class="GtkTextTag">
<property name="name">warning</property>
<property name="underline">single</property>
</object>
<object class="GtkTextTag">
<property name="name">error</property>
<property name="underline">error</property>
</object>
</child>
</object>
<object class="GtkTextBuffer" id="text">
<property name="tag-table">tags</property>
<signal name="changed" handler="text_changed"/>
</object>
<template class="ParasiteCssEditor" parent="GtkBox">
<property name="orientation">vertical</property>
<child>
<object class="GtkToolbar" id="toolbar">
<property name="visible">True</property>
<property name="icon-size">small-toolbar</property>
<child>
<object class="GtkToggleToolButton" id="disable_button">
<property name="visible">True</property>
<property name="icon-name">media-playback-pause</property>
<property name="tooltip-text">Disable this custom CSS</property>
<signal name="toggled" handler="disable_toggled"/>
</object>
</child>
</object>
</child>
<child>
<object class="GtkScrolledWindow">
<property name="visible">True</property>
<property name="expand">True</property>
<child>
<object class="GtkTextView" id="view">
<property name="visible">True</property>
<property name="buffer">text</property>
<property name="wrap-mode">word</property>
</object>
</child>
</object>
</child>
</template>
</interface>

View File

@ -3,5 +3,6 @@
<gresource prefix="/org/gtk/parasite"> <gresource prefix="/org/gtk/parasite">
<file>button-path.ui</file> <file>button-path.ui</file>
<file>object-hierarchy.ui</file> <file>object-hierarchy.ui</file>
<file>css-editor.ui</file>
</gresource> </gresource>
</gresources> </gresources>

View File

@ -55,7 +55,7 @@ on_widget_tree_selection_changed (ParasiteWidgetTree *widget_tree,
gtkparasite_flash_widget(parasite, widget); gtkparasite_flash_widget(parasite, widget);
parasite_button_path_set_widget (PARASITE_BUTTON_PATH (parasite->button_path), widget); parasite_button_path_set_widget (PARASITE_BUTTON_PATH (parasite->button_path), widget);
parasite_classeslist_set_widget (PARASITE_CLASSESLIST (parasite->classes_list), widget); parasite_classeslist_set_widget (PARASITE_CLASSESLIST (parasite->classes_list), widget);
parasite_csseditor_set_widget (PARASITE_CSSEDITOR (parasite->widget_css_editor), widget); parasite_css_editor_set_widget (PARASITE_CSS_EDITOR (parasite->widget_css_editor), widget);
} }
else else
{ {
@ -263,7 +263,7 @@ gtkparasite_window_create()
gtk_label_new ("Themes")); gtk_label_new ("Themes"));
gtk_notebook_append_page (GTK_NOTEBOOK (nb), gtk_notebook_append_page (GTK_NOTEBOOK (nb),
parasite_csseditor_new (TRUE), parasite_css_editor_new (TRUE),
gtk_label_new ("Custom CSS")); gtk_label_new ("Custom CSS"));
window->button_path = parasite_button_path_new (); window->button_path = parasite_button_path_new ();
@ -298,7 +298,7 @@ gtkparasite_window_create()
window->classes_list, window->classes_list,
gtk_label_new ("CSS Classes")); gtk_label_new ("CSS Classes"));
window->widget_css_editor = parasite_csseditor_new (FALSE); window->widget_css_editor = parasite_css_editor_new (FALSE);
gtk_notebook_append_page (GTK_NOTEBOOK (nb), gtk_notebook_append_page (GTK_NOTEBOOK (nb),
window->widget_css_editor, window->widget_css_editor,
gtk_label_new ("Custom CSS")); gtk_label_new ("Custom CSS"));