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 "parasite.h"
#define PARASITE_CSSEDITOR_TEXT "parasite-csseditor-text"
#define PARASITE_CSSEDITOR_PROVIDER "parasite-csseditor-provider"
#define PARASITE_CSS_EDITOR_TEXT "parasite-css-editor-text"
#define PARASITE_CSS_EDITOR_PROVIDER "parasite-css-editor-provider"
enum
{
COLUMN_ENABLED,
COLUMN_NAME,
COLUMN_USER,
NUM_COLUMNS
COLUMN_USER
};
enum
@ -49,6 +48,7 @@ typedef struct
struct _ParasiteCssEditorPrivate
{
GtkWidget *toolbar;
GtkWidget *view;
GtkTextBuffer *text;
GtkCssProvider *provider;
gboolean global;
@ -56,82 +56,69 @@ struct _ParasiteCssEditorPrivate
GtkToggleToolButton *disable_button;
};
G_DEFINE_TYPE_WITH_PRIVATE (ParasiteCssEditor, parasite_csseditor, 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";
G_DEFINE_TYPE_WITH_PRIVATE (ParasiteCssEditor, parasite_css_editor, GTK_TYPE_BOX)
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 (editor->priv->global)
{
gtk_style_context_remove_provider_for_screen (gdk_screen_get_default (),
GTK_STYLE_PROVIDER (editor->priv->provider));
}
gtk_style_context_remove_provider_for_screen (gdk_screen_get_default (),
GTK_STYLE_PROVIDER (editor->priv->provider));
else if (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_CSSEDITOR_PROVIDER)));
}
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)));
}
else
{
if (editor->priv->global)
{
gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
GTK_STYLE_PROVIDER (editor->priv->provider),
GTK_STYLE_PROVIDER_PRIORITY_USER);
}
gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
GTK_STYLE_PROVIDER (editor->priv->provider),
GTK_STYLE_PROVIDER_PRIORITY_USER);
else if (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_CSSEDITOR_PROVIDER)),
G_MAXUINT);
}
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)),
G_MAXUINT);
}
}
static void
create_toolbar (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)
apply_system_font (ParasiteCssEditor *editor)
{
GSettings *s = g_settings_new ("org.gnome.desktop.interface");
gchar *font_name = g_settings_get_string (s, "monospace-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);
g_free (font_name);
@ -151,15 +138,16 @@ get_current_text (GtkTextBuffer *buffer)
}
static void
text_changed (GtkTextBuffer *buffer, ParasiteCssEditor *editor)
text_changed (GtkTextBuffer *buffer,
ParasiteCssEditor *editor)
{
GtkCssProvider *provider;
char *text;
gchar *text;
if (editor->priv->global)
provider = editor->priv->provider;
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
return;
@ -197,42 +185,6 @@ show_parsing_error (GtkCssProvider *provider,
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
create_provider (ParasiteCssEditor *editor)
{
@ -251,35 +203,29 @@ create_provider (ParasiteCssEditor *editor)
GTK_STYLE_PROVIDER (provider),
G_MAXUINT);
g_object_set_data (G_OBJECT (editor->priv->selected_context),
PARASITE_CSSEDITOR_PROVIDER,
provider);
PARASITE_CSS_EDITOR_PROVIDER, provider);
}
g_signal_connect (provider,
"parsing-error",
G_CALLBACK (show_parsing_error),
editor);
g_signal_connect (provider, "parsing-error",
G_CALLBACK (show_parsing_error), editor);
}
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
constructed (GObject *object)
{
ParasiteCssEditor *editor = PARASITE_CSSEDITOR (object);
ParasiteCssEditor *editor = PARASITE_CSS_EDITOR (object);
g_object_set (editor,
"orientation", GTK_ORIENTATION_VERTICAL,
"sensitive", editor->priv->global,
NULL);
create_toolbar (editor);
gtk_widget_set_sensitive (GTK_WIDGET (editor), editor->priv->global);
create_provider (editor);
create_text_widget (editor);
apply_system_font (editor);
set_initial_text (editor);
}
static void
@ -288,7 +234,7 @@ get_property (GObject *object,
GValue *value,
GParamSpec *pspec)
{
ParasiteCssEditor *editor = PARASITE_CSSEDITOR (object);
ParasiteCssEditor *editor = PARASITE_CSS_EDITOR (object);
switch (param_id)
{
@ -308,7 +254,7 @@ set_property (GObject *object,
const GValue *value,
GParamSpec *pspec)
{
ParasiteCssEditor *editor = PARASITE_CSSEDITOR (object);
ParasiteCssEditor *editor = PARASITE_CSS_EDITOR (object);
switch (param_id)
{
@ -323,38 +269,44 @@ set_property (GObject *object,
}
static void
parasite_csseditor_class_init (ParasiteCssEditorClass *klass)
parasite_css_editor_class_init (ParasiteCssEditorClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
object_class->get_property = get_property;
object_class->set_property = set_property;
object_class->constructed = constructed;
object_class->constructed = constructed;
g_object_class_install_property (object_class,
PROP_GLOBAL,
g_param_spec_boolean ("global",
"Global",
"Whether this editor changes the whole application or just the selected widget",
TRUE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class, PROP_GLOBAL,
g_param_spec_boolean ("global", "Global", "Whether this editor changes the whole application or just the selected widget",
TRUE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
gtk_widget_class_set_template_from_resource (widget_class, "/org/gtk/parasite/css-editor.ui");
gtk_widget_class_bind_template_child_private (widget_class, ParasiteCssEditor, toolbar);
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 *
parasite_csseditor_new (gboolean global)
parasite_css_editor_new (gboolean global)
{
return GTK_WIDGET (g_object_new (PARASITE_TYPE_CSSEDITOR,
"global", global,
NULL));
return GTK_WIDGET (g_object_new (PARASITE_TYPE_CSS_EDITOR,
"global", global,
NULL));
}
void
parasite_csseditor_set_widget (ParasiteCssEditor *editor, GtkWidget *widget)
parasite_css_editor_set_widget (ParasiteCssEditor *editor,
GtkWidget *widget)
{
gchar *text;
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);
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));
g_object_set_data_full (G_OBJECT (editor->priv->selected_context),
PARASITE_CSSEDITOR_TEXT,
PARASITE_CSS_EDITOR_TEXT,
text,
g_free);
}
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)
{
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);
create_provider (editor);
set_initial_text (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.
*/
#ifndef _GTKPARASITE_CSSEDITOR_H_
#define _GTKPARASITE_CSSEDITOR_H_
#ifndef _GTKPARASITE_CSS_EDITOR_H_
#define _GTKPARASITE_CSS_EDITOR_H_
#include <gtk/gtk.h>
#define PARASITE_TYPE_CSSEDITOR (parasite_csseditor_get_type())
#define PARASITE_CSSEDITOR(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), PARASITE_TYPE_CSSEDITOR, ParasiteCssEditor))
#define PARASITE_CSSEDITOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), PARASITE_TYPE_CSSEDITOR, ParasiteCssEditorClass))
#define PARASITE_IS_CSSEDITOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), PARASITE_TYPE_CSSEDITOR))
#define PARASITE_IS_CSSEDITOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), PARASITE_TYPE_CSSEDITOR))
#define PARASITE_CSSEDITOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), PARASITE_TYPE_CSSEDITOR, ParasiteCssEditorClass))
#define PARASITE_TYPE_CSS_EDITOR (parasite_css_editor_get_type())
#define PARASITE_CSS_EDITOR(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), PARASITE_TYPE_CSS_EDITOR, ParasiteCssEditor))
#define PARASITE_CSS_EDITOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), PARASITE_TYPE_CSS_EDITOR, ParasiteCssEditorClass))
#define PARASITE_IS_CSS_EDITOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), PARASITE_TYPE_CSS_EDITOR))
#define PARASITE_IS_CSS_EDITOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), PARASITE_TYPE_CSS_EDITOR))
#define PARASITE_CSS_EDITOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), PARASITE_TYPE_CSS_EDITOR, ParasiteCssEditorClass))
typedef struct _ParasiteCssEditorPrivate ParasiteCssEditorPrivate;
typedef struct _ParasiteCssEditor {
GtkBox parent;
ParasiteCssEditorPrivate *priv;
typedef struct _ParasiteCssEditor
{
GtkBox parent;
ParasiteCssEditorPrivate *priv;
} ParasiteCssEditor;
typedef struct _ParasiteCssEditorClass {
GtkBoxClass parent;
typedef struct _ParasiteCssEditorClass
{
GtkBoxClass parent;
} ParasiteCssEditorClass;
G_BEGIN_DECLS
GType parasite_csseditor_get_type ();
GtkWidget *parasite_csseditor_new (gboolean global);
void parasite_csseditor_set_widget (ParasiteCssEditor *editor,
GtkWidget *widget);
GType parasite_css_editor_get_type (void);
GtkWidget *parasite_css_editor_new (gboolean global);
void parasite_css_editor_set_widget (ParasiteCssEditor *editor,
GtkWidget *widget);
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">
<file>button-path.ui</file>
<file>object-hierarchy.ui</file>
<file>css-editor.ui</file>
</gresource>
</gresources>

View File

@ -55,7 +55,7 @@ on_widget_tree_selection_changed (ParasiteWidgetTree *widget_tree,
gtkparasite_flash_widget(parasite, widget);
parasite_button_path_set_widget (PARASITE_BUTTON_PATH (parasite->button_path), 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
{
@ -263,7 +263,7 @@ gtkparasite_window_create()
gtk_label_new ("Themes"));
gtk_notebook_append_page (GTK_NOTEBOOK (nb),
parasite_csseditor_new (TRUE),
parasite_css_editor_new (TRUE),
gtk_label_new ("Custom CSS"));
window->button_path = parasite_button_path_new ();
@ -298,7 +298,7 @@ gtkparasite_window_create()
window->classes_list,
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),
window->widget_css_editor,
gtk_label_new ("Custom CSS"));