app: allow to toggle tags and then write text with that style

instead of using the toggles only to change the style of selected text.

Introduces a list of "insert tags" in GimpTextBuffer that is applied
on newly inserted text if it exists. Clear the list on each content
or cursor/selection change, so we always display wthe style at the
cursor unless the buttons were clicked explicitely.
This commit is contained in:
Michael Natterer 2010-02-26 19:53:52 +01:00
parent a4826176d8
commit 9a53cc11ca
4 changed files with 160 additions and 48 deletions

View File

@ -58,6 +58,10 @@ static GObject * gimp_text_buffer_constructor (GType type,
static void gimp_text_buffer_dispose (GObject *object);
static void gimp_text_buffer_finalize (GObject *object);
static void gimp_text_buffer_mark_set (GtkTextBuffer *buffer,
const GtkTextIter *location,
GtkTextMark *mark);
G_DEFINE_TYPE (GimpTextBuffer, gimp_text_buffer, GTK_TYPE_TEXT_BUFFER)
@ -67,11 +71,14 @@ G_DEFINE_TYPE (GimpTextBuffer, gimp_text_buffer, GTK_TYPE_TEXT_BUFFER)
static void
gimp_text_buffer_class_init (GimpTextBufferClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkTextBufferClass *buffer_class = GTK_TEXT_BUFFER_CLASS (klass);
object_class->constructor = gimp_text_buffer_constructor;
object_class->dispose = gimp_text_buffer_dispose;
object_class->finalize = gimp_text_buffer_finalize;
buffer_class->mark_set = gimp_text_buffer_mark_set;
}
static void
@ -137,11 +144,23 @@ gimp_text_buffer_dispose (GObject *object)
static void
gimp_text_buffer_finalize (GObject *object)
{
/* GimpTextBuffer *buffer = GIMP_TEXT_BUFFER (object); */
GimpTextBuffer *buffer = GIMP_TEXT_BUFFER (object);
gimp_text_buffer_clear_insert_tags (buffer);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
gimp_text_buffer_mark_set (GtkTextBuffer *buffer,
const GtkTextIter *location,
GtkTextMark *mark)
{
gimp_text_buffer_clear_insert_tags (GIMP_TEXT_BUFFER (buffer));
GTK_TEXT_BUFFER_CLASS (parent_class)->mark_set (buffer, location, mark);
}
/* public functions */
@ -161,6 +180,8 @@ gimp_text_buffer_set_text (GimpTextBuffer *buffer,
text = "";
gtk_text_buffer_set_text (GTK_TEXT_BUFFER (buffer), text, -1);
gimp_text_buffer_clear_insert_tags (buffer);
}
gchar *
@ -202,6 +223,8 @@ gimp_text_buffer_set_markup (GimpTextBuffer *buffer,
g_clear_error (&error);
}
}
gimp_text_buffer_clear_insert_tags (buffer);
}
gchar *
@ -275,14 +298,34 @@ gimp_text_buffer_name_to_tag (GimpTextBuffer *buffer,
return NULL;
}
void
gimp_text_buffer_set_insert_tags (GimpTextBuffer *buffer,
GList *style)
{
g_list_free (buffer->insert_tags);
buffer->insert_tags = style;
buffer->insert_tags_set = TRUE;
}
void
gimp_text_buffer_clear_insert_tags (GimpTextBuffer *buffer)
{
g_return_if_fail (GIMP_IS_TEXT_BUFFER (buffer));
g_list_free (buffer->insert_tags);
buffer->insert_tags = NULL;
buffer->insert_tags_set = FALSE;
}
void
gimp_text_buffer_insert (GimpTextBuffer *buffer,
const gchar *text)
{
GtkTextIter iter, start;
gint start_offset;
GSList *tags_off;
GSList *list;
GList *insert_tags;
gboolean insert_tags_set;
GSList *tags_off = NULL;
g_return_if_fail (GIMP_IS_TEXT_BUFFER (buffer));
@ -291,20 +334,46 @@ gimp_text_buffer_insert (GimpTextBuffer *buffer,
start_offset = gtk_text_iter_get_offset (&iter);
tags_off = gtk_text_iter_get_toggled_tags (&iter, FALSE);
insert_tags = buffer->insert_tags;
insert_tags_set = buffer->insert_tags_set;
buffer->insert_tags = NULL;
buffer->insert_tags_set = FALSE;
if (! insert_tags_set)
tags_off = gtk_text_iter_get_toggled_tags (&iter, FALSE);
gtk_text_buffer_insert (GTK_TEXT_BUFFER (buffer), &iter, text, -1);
gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (buffer), &start,
start_offset);
for (list = tags_off; list; list = g_slist_next (list))
if (insert_tags_set)
{
gtk_text_buffer_apply_tag (GTK_TEXT_BUFFER (buffer), list->data,
&start, &iter);
}
GList *list;
g_slist_free (tags_off);
gtk_text_buffer_remove_all_tags (GTK_TEXT_BUFFER (buffer),
&start, &iter);
for (list = insert_tags; list; list = g_list_next (list))
{
gtk_text_buffer_apply_tag (GTK_TEXT_BUFFER (buffer), list->data,
&start, &iter);
}
g_list_free (insert_tags);
}
else
{
GSList *list;
for (list = tags_off; list; list = g_slist_next (list))
{
gtk_text_buffer_apply_tag (GTK_TEXT_BUFFER (buffer), list->data,
&start, &iter);
}
g_slist_free (tags_off);
}
}
gint

View File

@ -40,6 +40,9 @@ struct _GimpTextBuffer
GtkTextTag *underline_tag;
GtkTextTag *strikethrough_tag;
GList *insert_tags;
gboolean insert_tags_set;
GdkAtom markup_atom;
};
@ -49,36 +52,39 @@ struct _GimpTextBufferClass
};
GType gimp_text_buffer_get_type (void) G_GNUC_CONST;
GType gimp_text_buffer_get_type (void) G_GNUC_CONST;
GimpTextBuffer * gimp_text_buffer_new (void);
GimpTextBuffer * gimp_text_buffer_new (void);
void gimp_text_buffer_set_text (GimpTextBuffer *buffer,
const gchar *text);
gchar * gimp_text_buffer_get_text (GimpTextBuffer *buffer);
void gimp_text_buffer_set_text (GimpTextBuffer *buffer,
const gchar *text);
gchar * gimp_text_buffer_get_text (GimpTextBuffer *buffer);
void gimp_text_buffer_set_markup (GimpTextBuffer *buffer,
const gchar *markup);
gchar * gimp_text_buffer_get_markup (GimpTextBuffer *buffer);
void gimp_text_buffer_set_markup (GimpTextBuffer *buffer,
const gchar *markup);
gchar * gimp_text_buffer_get_markup (GimpTextBuffer *buffer);
const gchar * gimp_text_buffer_tag_to_name (GimpTextBuffer *buffer,
GtkTextTag *tag);
GtkTextTag * gimp_text_buffer_name_to_tag (GimpTextBuffer *buffer,
const gchar *name);
const gchar * gimp_text_buffer_tag_to_name (GimpTextBuffer *buffer,
GtkTextTag *tag);
GtkTextTag * gimp_text_buffer_name_to_tag (GimpTextBuffer *buffer,
const gchar *name);
void gimp_text_buffer_insert (GimpTextBuffer *buffer,
const gchar *text);
void gimp_text_buffer_set_insert_tags (GimpTextBuffer *buffer,
GList *style);
void gimp_text_buffer_clear_insert_tags (GimpTextBuffer *buffer);
void gimp_text_buffer_insert (GimpTextBuffer *buffer,
const gchar *text);
gint gimp_text_buffer_get_iter_index (GimpTextBuffer *buffer,
GtkTextIter *iter);
gint gimp_text_buffer_get_iter_index (GimpTextBuffer *buffer,
GtkTextIter *iter);
gboolean gimp_text_buffer_load (GimpTextBuffer *buffer,
const gchar *filename,
GError **error);
gboolean gimp_text_buffer_save (GimpTextBuffer *buffer,
const gchar *filename,
gboolean selection_only,
GError **error);
gboolean gimp_text_buffer_load (GimpTextBuffer *buffer,
const gchar *filename,
GError **error);
gboolean gimp_text_buffer_save (GimpTextBuffer *buffer,
const gchar *filename,
gboolean selection_only,
GError **error);
#endif /* __GIMP_TEXT_BUFFER_H__ */

View File

@ -60,10 +60,7 @@ static void gimp_text_style_editor_clear_tags (GtkButton *butto
static void gimp_text_style_editor_tag_toggled (GtkToggleButton *toggle,
GimpTextStyleEditor *editor);
static void gimp_text_style_editor_mark_set (GtkTextBuffer *buffer,
GtkTextIter *location,
GtkTextMark *mark,
GimpTextStyleEditor *editor);
static void gimp_text_style_editor_update (GimpTextStyleEditor *editor);
G_DEFINE_TYPE (GimpTextStyleEditor, gimp_text_style_editor,
@ -136,9 +133,22 @@ gimp_text_style_editor_constructor (GType type,
gimp_text_style_editor_create_toggle (editor, editor->buffer->strikethrough_tag,
GTK_STOCK_STRIKETHROUGH);
g_signal_connect (editor->buffer, "mark-set",
G_CALLBACK (gimp_text_style_editor_mark_set),
editor);
g_signal_connect_data (editor->buffer, "changed",
G_CALLBACK (gimp_text_style_editor_update),
editor, 0,
G_CONNECT_AFTER | G_CONNECT_SWAPPED);
g_signal_connect_data (editor->buffer, "apply-tag",
G_CALLBACK (gimp_text_style_editor_update),
editor, 0,
G_CONNECT_AFTER | G_CONNECT_SWAPPED);
g_signal_connect_data (editor->buffer, "remove-tag",
G_CALLBACK (gimp_text_style_editor_update),
editor, 0,
G_CONNECT_AFTER | G_CONNECT_SWAPPED);
g_signal_connect_data (editor->buffer, "mark-set",
G_CALLBACK (gimp_text_style_editor_update),
editor, 0,
G_CONNECT_AFTER | G_CONNECT_SWAPPED);
return object;
}
@ -151,7 +161,7 @@ gimp_text_style_editor_dispose (GObject *object)
if (editor->buffer)
{
g_signal_handlers_disconnect_by_func (editor->buffer,
gimp_text_style_editor_mark_set,
gimp_text_style_editor_update,
editor);
}
@ -231,6 +241,26 @@ gimp_text_style_editor_new (GimpTextBuffer *buffer)
NULL);
}
GList *
gimp_text_style_editor_list_tags (GimpTextStyleEditor *editor)
{
GList *toggles;
GList *tags = NULL;
g_return_val_if_fail (GIMP_IS_TEXT_STYLE_EDITOR (editor), NULL);
for (toggles = editor->toggles; toggles; toggles = g_list_next (toggles))
{
if (gtk_toggle_button_get_active (toggles->data))
{
tags = g_list_prepend (tags,
g_object_get_data (toggles->data, "tag"));
}
}
return g_list_reverse (tags);
}
/* private functions */
@ -247,6 +277,8 @@ gimp_text_style_editor_create_toggle (GimpTextStyleEditor *editor,
gtk_box_pack_start (GTK_BOX (editor), toggle, FALSE, FALSE, 0);
gtk_widget_show (toggle);
editor->toggles = g_list_append (editor->toggles, toggle);
g_object_set_data (G_OBJECT (toggle), "tag", tag);
g_hash_table_insert (editor->tag_to_toggle_hash, tag, toggle);
@ -283,6 +315,7 @@ gimp_text_style_editor_tag_toggled (GtkToggleButton *toggle,
{
GtkTextBuffer *buffer = GTK_TEXT_BUFFER (editor->buffer);
GtkTextTag *tag = g_object_get_data (G_OBJECT (toggle), "tag");
GList *list;
if (gtk_text_buffer_get_has_selection (buffer))
{
@ -299,6 +332,9 @@ gimp_text_style_editor_tag_toggled (GtkToggleButton *toggle,
gtk_text_buffer_remove_tag (buffer, tag, &start, &end);
}
}
list = gimp_text_style_editor_list_tags (editor);
gimp_text_buffer_set_insert_tags (editor->buffer, list);
}
static void
@ -370,11 +406,10 @@ gimp_text_style_editor_update_cursor (GtkTextTag *tag,
}
static void
gimp_text_style_editor_mark_set (GtkTextBuffer *buffer,
GtkTextIter *location,
GtkTextMark *mark,
GimpTextStyleEditor *editor)
gimp_text_style_editor_update (GimpTextStyleEditor *editor)
{
GtkTextBuffer *buffer = GTK_TEXT_BUFFER (editor->buffer);
if (gtk_text_buffer_get_has_selection (buffer))
{
GtkTextIter start, end;
@ -405,7 +440,6 @@ gimp_text_style_editor_mark_set (GtkTextBuffer *buffer,
if (! data.any_active)
break;
}
}
else
{

View File

@ -44,6 +44,7 @@ struct _GimpTextStyleEditor
GtkWidget *underline_toggle;
GtkWidget *strikethrough_toggle;
GList *toggles;
GHashTable *tag_to_toggle_hash;
};
@ -53,9 +54,11 @@ struct _GimpTextStyleEditorClass
};
GType gimp_text_style_editor_get_type (void) G_GNUC_CONST;
GType gimp_text_style_editor_get_type (void) G_GNUC_CONST;
GtkWidget * gimp_text_style_editor_new (GimpTextBuffer *buffer);
GtkWidget * gimp_text_style_editor_new (GimpTextBuffer *buffer);
GList * gimp_text_style_editor_list_tags (GimpTextStyleEditor *editor);
#endif /* __GIMP_TEXT_STYLE_EDITOR_H__ */