[gimp] app: add GimpTextBuffer, a GtkTextBuffer subclass



commit 0b4bdc5613f9496db40b2669a598dce779972d97
Author: Michael Natterer <mitch gimp org>
Date:   Thu Feb 25 17:41:10 2010 +0100

    app: add GimpTextBuffer, a GtkTextBuffer subclass
    
    Pull all text buffer utility functions as methods and use
    GimpTextBuffer all over the place instead of GtkTextBuffer.
    Some actually usefuly features coming soon...

 app/actions/error-console-commands.c |    5 +-
 app/actions/text-editor-commands.c   |    6 +-
 app/actions/text-tool-commands.c     |    9 +-
 app/tools/gimptextoptions.c          |    4 +-
 app/tools/gimptextoptions.h          |    2 +-
 app/tools/gimptexttool-editor.c      |   66 +++------
 app/tools/gimptexttool-editor.h      |    5 -
 app/tools/gimptexttool.c             |   43 +++--
 app/tools/gimptexttool.h             |    2 +-
 app/widgets/Makefile.am              |    2 +
 app/widgets/gimperrorconsole.c       |    3 +-
 app/widgets/gimptextbuffer.c         |  288 ++++++++++++++++++++++++++++++++++
 app/widgets/gimptextbuffer.h         |   65 ++++++++
 app/widgets/gimptexteditor.c         |   12 +-
 app/widgets/gimptexteditor.h         |    2 +-
 app/widgets/gimpwidgets-utils.c      |  134 ----------------
 app/widgets/gimpwidgets-utils.h      |    7 -
 app/widgets/widgets-types.h          |    3 +-
 18 files changed, 427 insertions(+), 231 deletions(-)
---
diff --git a/app/actions/error-console-commands.c b/app/actions/error-console-commands.c
index fb975d2..585a9ff 100644
--- a/app/actions/error-console-commands.c
+++ b/app/actions/error-console-commands.c
@@ -28,7 +28,7 @@
 
 #include "widgets/gimperrorconsole.h"
 #include "widgets/gimphelp-ids.h"
-#include "widgets/gimpwidgets-utils.h"
+#include "widgets/gimptextbuffer.h"
 
 #include "error-console-commands.h"
 
@@ -149,7 +149,8 @@ error_console_save_response (GtkWidget        *dialog,
 
       filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
 
-      if (! gimp_text_buffer_save (console->text_buffer, filename,
+      if (! gimp_text_buffer_save (GIMP_TEXT_BUFFER (console->text_buffer),
+                                   filename,
                                    console->save_selection, &error))
         {
           gimp_message (console->gimp, G_OBJECT (dialog), GIMP_MESSAGE_ERROR,
diff --git a/app/actions/text-editor-commands.c b/app/actions/text-editor-commands.c
index 8e575a6..3a02d5b 100644
--- a/app/actions/text-editor-commands.c
+++ b/app/actions/text-editor-commands.c
@@ -26,10 +26,10 @@
 
 #include "core/gimp.h"
 
-#include "widgets/gimptexteditor.h"
 #include "widgets/gimphelp-ids.h"
+#include "widgets/gimptextbuffer.h"
+#include "widgets/gimptexteditor.h"
 #include "widgets/gimpuimanager.h"
-#include "widgets/gimpwidgets-utils.h"
 
 #include "text-editor-commands.h"
 
@@ -136,7 +136,7 @@ text_editor_load_response (GtkWidget      *dialog,
       buffer   = gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->view));
       filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
 
-      if (! gimp_text_buffer_load (buffer, filename, &error))
+      if (! gimp_text_buffer_load (GIMP_TEXT_BUFFER (buffer), filename, &error))
         {
           gimp_message (editor->ui_manager->gimp, G_OBJECT (dialog),
                         GIMP_MESSAGE_ERROR,
diff --git a/app/actions/text-tool-commands.c b/app/actions/text-tool-commands.c
index 6637bf0..38b981b 100644
--- a/app/actions/text-tool-commands.c
+++ b/app/actions/text-tool-commands.c
@@ -154,11 +154,12 @@ void
 text_tool_clear_cmd_callback (GtkAction *action,
                               gpointer   data)
 {
-  GimpTextTool *text_tool = GIMP_TEXT_TOOL (data);
-  GtkTextIter   start, end;
+  GimpTextTool  *text_tool = GIMP_TEXT_TOOL (data);
+  GtkTextBuffer *buffer    = GTK_TEXT_BUFFER (text_tool->text_buffer);
+  GtkTextIter    start, end;
 
-  gtk_text_buffer_get_bounds (text_tool->text_buffer, &start, &end);
-  gtk_text_buffer_select_range (text_tool->text_buffer, &start, &end);
+  gtk_text_buffer_get_bounds (buffer, &start, &end);
+  gtk_text_buffer_select_range (buffer, &start, &end);
   gimp_text_tool_delete_selection (text_tool);
 }
 
diff --git a/app/tools/gimptextoptions.c b/app/tools/gimptextoptions.c
index a8f85f4..8aa578e 100644
--- a/app/tools/gimptextoptions.c
+++ b/app/tools/gimptextoptions.c
@@ -36,6 +36,7 @@
 #include "widgets/gimpdock.h"
 #include "widgets/gimpmenufactory.h"
 #include "widgets/gimppropwidgets.h"
+#include "widgets/gimptextbuffer.h"
 #include "widgets/gimptexteditor.h"
 #include "widgets/gimpviewablebox.h"
 #include "widgets/gimpwidgets-utils.h"
@@ -600,7 +601,7 @@ gimp_text_options_editor_new (GtkWindow       *parent,
                               GimpTextOptions *options,
                               GimpMenuFactory *menu_factory,
                               const gchar     *title,
-                              GtkTextBuffer   *text_buffer)
+                              GimpTextBuffer  *text_buffer)
 {
   GtkWidget   *editor;
   const gchar *font_name;
@@ -608,6 +609,7 @@ gimp_text_options_editor_new (GtkWindow       *parent,
   g_return_val_if_fail (GIMP_IS_TEXT_OPTIONS (options), NULL);
   g_return_val_if_fail (GIMP_IS_MENU_FACTORY (menu_factory), NULL);
   g_return_val_if_fail (title != NULL, NULL);
+  g_return_val_if_fail (GIMP_IS_TEXT_BUFFER (text_buffer), NULL);
 
   editor = gimp_text_editor_new (title, parent, menu_factory, text_buffer);
 
diff --git a/app/tools/gimptextoptions.h b/app/tools/gimptextoptions.h
index 71328dc..63602b2 100644
--- a/app/tools/gimptextoptions.h
+++ b/app/tools/gimptextoptions.h
@@ -70,7 +70,7 @@ GtkWidget * gimp_text_options_editor_new   (GtkWindow       *parent,
                                             GimpTextOptions *options,
                                             GimpMenuFactory *menu_factory,
                                             const gchar     *title,
-                                            GtkTextBuffer   *text_buffer);
+                                            GimpTextBuffer  *text_buffer);
 
 
 #endif /* __GIMP_TEXT_OPTIONS_H__ */
diff --git a/app/tools/gimptexttool-editor.c b/app/tools/gimptexttool-editor.c
index 7002085..c03bb50 100644
--- a/app/tools/gimptexttool-editor.c
+++ b/app/tools/gimptexttool-editor.c
@@ -35,6 +35,7 @@
 
 #include "widgets/gimpdialogfactory.h"
 #include "widgets/gimpoverlaybox.h"
+#include "widgets/gimptextbuffer.h"
 #include "widgets/gimptexteditor.h"
 #include "widgets/gimptextproxy.h"
 
@@ -187,7 +188,7 @@ gimp_text_tool_editor_button_press (GimpTextTool        *text_tool,
                                     gdouble              y,
                                     GimpButtonPressType  press_type)
 {
-  GtkTextBuffer *buffer = text_tool->text_buffer;
+  GtkTextBuffer *buffer = GTK_TEXT_BUFFER (text_tool->text_buffer);
   GtkTextIter    cursor;
   GtkTextIter    selection;
   gint           offset;
@@ -235,7 +236,9 @@ gimp_text_tool_editor_button_press (GimpTextTool        *text_tool,
 void
 gimp_text_tool_editor_button_release (GimpTextTool *text_tool)
 {
-  if (gtk_text_buffer_get_has_selection (text_tool->text_buffer))
+  GtkTextBuffer *buffer = GTK_TEXT_BUFFER (text_tool->text_buffer);
+
+  if (gtk_text_buffer_get_has_selection (buffer))
     {
       GimpTool         *tool  = GIMP_TOOL (text_tool);
       GimpDisplayShell *shell = gimp_display_get_shell (tool->display);
@@ -244,7 +247,7 @@ gimp_text_tool_editor_button_release (GimpTextTool *text_tool)
       clipboard = gtk_widget_get_clipboard (GTK_WIDGET (shell),
                                             GDK_SELECTION_PRIMARY);
 
-      gtk_text_buffer_copy_clipboard (text_tool->text_buffer, clipboard);
+      gtk_text_buffer_copy_clipboard (buffer, clipboard);
     }
 }
 
@@ -253,7 +256,7 @@ gimp_text_tool_editor_motion (GimpTextTool *text_tool,
                               gdouble       x,
                               gdouble       y)
 {
-  GtkTextBuffer *buffer = text_tool->text_buffer;
+  GtkTextBuffer *buffer = GTK_TEXT_BUFFER (text_tool->text_buffer);
   GtkTextIter    cursor;
   GtkTextIter    selection;
   gint           cursor_offset;
@@ -341,7 +344,7 @@ gboolean
 gimp_text_tool_editor_key_press (GimpTextTool *text_tool,
                                  GdkEventKey  *kevent)
 {
-  GtkTextBuffer *buffer = text_tool->text_buffer;
+  GtkTextBuffer *buffer = GTK_TEXT_BUFFER (text_tool->text_buffer);
   GtkTextIter    cursor;
   GtkTextIter    selection;
   gint           x_pos  = -1;
@@ -443,42 +446,13 @@ gimp_text_tool_reset_im_context (GimpTextTool *text_tool)
     }
 }
 
-gchar *
-gimp_text_tool_editor_get_text (GimpTextTool *text_tool)
-{
-  GtkTextIter start, end;
-
-  gtk_text_buffer_get_bounds (text_tool->text_buffer, &start, &end);
-
-  return gtk_text_buffer_get_text (text_tool->text_buffer,
-                                   &start, &end, TRUE);
-}
-
-gint
-gimp_text_tool_editor_get_iter_index (GimpTextTool *text_tool,
-                                      GtkTextIter  *iter)
-{
-  GtkTextBuffer *buffer = text_tool->text_buffer;
-  GtkTextIter    start;
-  gchar         *string;
-  gint           index;
-
-  gtk_text_buffer_get_start_iter (buffer, &start);
-
-  string = gtk_text_buffer_get_text (buffer, &start, iter, TRUE);
-  index = strlen (string);
-  g_free (string);
-
-  return index;
-}
-
 void
 gimp_text_tool_editor_get_cursor_rect (GimpTextTool   *text_tool,
                                        PangoRectangle *cursor_rect,
                                        gint           *logical_off_x,
                                        gint           *logical_off_y)
 {
-  GtkTextBuffer  *buffer = text_tool->text_buffer;
+  GtkTextBuffer  *buffer = GTK_TEXT_BUFFER (text_tool->text_buffer);
   PangoLayout    *layout;
   PangoRectangle  ink_extents;
   PangoRectangle  logical_extents;
@@ -507,7 +481,8 @@ gimp_text_tool_editor_get_cursor_rect (GimpTextTool   *text_tool,
 
   gtk_text_buffer_get_iter_at_mark (buffer, &cursor,
                                     gtk_text_buffer_get_insert (buffer));
-  cursor_index = gimp_text_tool_editor_get_iter_index (text_tool, &cursor);
+  cursor_index = gimp_text_buffer_get_iter_index (text_tool->text_buffer,
+                                                  &cursor);
 
   pango_layout_index_to_pos (layout, cursor_index, cursor_rect);
   gimp_text_layout_transform_rect (text_tool->layout, cursor_rect);
@@ -584,7 +559,7 @@ gimp_text_tool_move_cursor (GimpTextTool    *text_tool,
                             gint             count,
                             gboolean         extend_selection)
 {
-  GtkTextBuffer *buffer = text_tool->text_buffer;
+  GtkTextBuffer *buffer = GTK_TEXT_BUFFER (text_tool->text_buffer);
   GtkTextIter    cursor;
   GtkTextIter    selection;
   GtkTextIter   *sel_start;
@@ -672,8 +647,8 @@ gimp_text_tool_move_cursor (GimpTextTool    *text_tool,
 
         gtk_text_buffer_get_bounds (buffer, &start, &end);
 
-        cursor_index = gimp_text_tool_editor_get_iter_index (text_tool,
-                                                             &cursor);
+        cursor_index = gimp_text_buffer_get_iter_index (text_tool->text_buffer,
+                                                        &cursor);
 
         gimp_text_tool_ensure_layout (text_tool);
 
@@ -795,7 +770,8 @@ gimp_text_tool_insert_at_cursor (GimpTextTool *text_tool,
 {
   gimp_draw_tool_pause (GIMP_DRAW_TOOL (text_tool));
 
-  gtk_text_buffer_insert_at_cursor (text_tool->text_buffer, str, -1);
+  gtk_text_buffer_insert_at_cursor (GTK_TEXT_BUFFER (text_tool->text_buffer),
+                                    str, -1);
 
   gimp_draw_tool_resume (GIMP_DRAW_TOOL (text_tool));
 }
@@ -836,7 +812,7 @@ gimp_text_tool_delete_from_cursor (GimpTextTool  *text_tool,
                                    GtkDeleteType  type,
                                    gint           count)
 {
-  GtkTextBuffer *buffer = text_tool->text_buffer;
+  GtkTextBuffer *buffer = GTK_TEXT_BUFFER (text_tool->text_buffer);
   GtkTextIter    cursor;
   GtkTextIter    end;
 
@@ -933,7 +909,7 @@ gimp_text_tool_delete_from_cursor (GimpTextTool  *text_tool,
 static void
 gimp_text_tool_backspace (GimpTextTool *text_tool)
 {
-  GtkTextBuffer *buffer = text_tool->text_buffer;
+  GtkTextBuffer *buffer = GTK_TEXT_BUFFER (text_tool->text_buffer);
 
   gimp_text_tool_reset_im_context (text_tool);
 
@@ -970,7 +946,7 @@ static void
 gimp_text_tool_select_all (GimpTextTool *text_tool,
                            gboolean      select)
 {
-  GtkTextBuffer *buffer = text_tool->text_buffer;
+  GtkTextBuffer *buffer = GTK_TEXT_BUFFER (text_tool->text_buffer);
 
   gimp_draw_tool_pause (GIMP_DRAW_TOOL (text_tool));
 
@@ -1073,7 +1049,7 @@ static void
 gimp_text_tool_enter_text (GimpTextTool *text_tool,
                            const gchar  *str)
 {
-  GtkTextBuffer *buffer = text_tool->text_buffer;
+  GtkTextBuffer *buffer = GTK_TEXT_BUFFER (text_tool->text_buffer);
   gboolean       had_selection;
 
   had_selection = gtk_text_buffer_get_has_selection (buffer);
@@ -1123,7 +1099,7 @@ gimp_text_tool_xy_to_offset (GimpTextTool *text_tool,
   if (ink_extents.y < 0)
     y += ink_extents.y;
 
-  string = gimp_text_tool_editor_get_text (text_tool);
+  string = gimp_text_buffer_get_text (text_tool->text_buffer);
 
   pango_layout_xy_to_index (layout,
                             x * PANGO_SCALE,
diff --git a/app/tools/gimptexttool-editor.h b/app/tools/gimptexttool-editor.h
index 7726f03..69cffa2 100644
--- a/app/tools/gimptexttool-editor.h
+++ b/app/tools/gimptexttool-editor.h
@@ -45,11 +45,6 @@ gboolean   gimp_text_tool_editor_key_release     (GimpTextTool        *text_tool
 
 void       gimp_text_tool_reset_im_context       (GimpTextTool        *text_tool);
 
-gchar    * gimp_text_tool_editor_get_text        (GimpTextTool        *text_tool);
-
-gint       gimp_text_tool_editor_get_iter_index  (GimpTextTool        *text_tool,
-                                                  GtkTextIter         *iter);
-
 void       gimp_text_tool_editor_get_cursor_rect (GimpTextTool        *text_tool,
                                                   PangoRectangle      *cursor_rect,
                                                   gint                *logical_off_x,
diff --git a/app/tools/gimptexttool.c b/app/tools/gimptexttool.c
index b99474c..a0dc4c8 100644
--- a/app/tools/gimptexttool.c
+++ b/app/tools/gimptexttool.c
@@ -51,6 +51,7 @@
 #include "widgets/gimpdialogfactory.h"
 #include "widgets/gimphelp-ids.h"
 #include "widgets/gimpmenufactory.h"
+#include "widgets/gimptextbuffer.h"
 #include "widgets/gimpuimanager.h"
 #include "widgets/gimpviewabledialog.h"
 
@@ -240,8 +241,7 @@ gimp_text_tool_init (GimpTextTool *text_tool)
   text_tool->image   = NULL;
   text_tool->layout  = NULL;
 
-  text_tool->text_buffer = gtk_text_buffer_new (NULL);
-  gtk_text_buffer_set_text (text_tool->text_buffer, "", -1);
+  text_tool->text_buffer = gimp_text_buffer_new ();
 
   g_signal_connect (text_tool->text_buffer, "changed",
                     G_CALLBACK (gimp_text_tool_buffer_changed),
@@ -699,7 +699,7 @@ static void
 gimp_text_tool_draw (GimpDrawTool *draw_tool)
 {
   GimpTextTool   *text_tool = GIMP_TEXT_TOOL (draw_tool);
-  GtkTextBuffer  *buffer    = text_tool->text_buffer;
+  GtkTextBuffer  *buffer    = GTK_TEXT_BUFFER (text_tool->text_buffer);
   PangoRectangle  cursor_rect;
   gint            logical_offset_x;
   gint            logical_offset_y;
@@ -750,7 +750,7 @@ gimp_text_tool_draw_selection (GimpDrawTool *draw_tool,
                                gint          logical_off_y)
 {
   GimpTextTool    *text_tool = GIMP_TEXT_TOOL (draw_tool);
-  GtkTextBuffer   *buffer    = text_tool->text_buffer;
+  GtkTextBuffer   *buffer    = GTK_TEXT_BUFFER (text_tool->text_buffer);
   PangoLayout     *layout;
   PangoLayoutIter *iter;
   GtkTextIter      sel_start, sel_end;
@@ -759,8 +759,8 @@ gimp_text_tool_draw_selection (GimpDrawTool *draw_tool,
 
   gtk_text_buffer_get_selection_bounds (buffer, &sel_start, &sel_end);
 
-  min = gimp_text_tool_editor_get_iter_index (text_tool, &sel_start);
-  max = gimp_text_tool_editor_get_iter_index (text_tool, &sel_end);
+  min = gimp_text_buffer_get_iter_index (text_tool->text_buffer, &sel_start);
+  max = gimp_text_buffer_get_iter_index (text_tool->text_buffer, &sel_end);
 
   layout = gimp_text_layout_get_pango_layout (text_tool->layout);
 
@@ -922,7 +922,7 @@ gimp_text_tool_connect (GimpTextTool  *text_tool,
           text_tool->text = NULL;
 
           g_object_set (text_tool->proxy, "text", NULL, NULL);
-          gtk_text_buffer_set_text (text_tool->text_buffer, "", -1);
+          gimp_text_buffer_set_text (text_tool->text_buffer, NULL);
 
           gimp_text_tool_clear_layout (text_tool);
         }
@@ -934,7 +934,7 @@ gimp_text_tool_connect (GimpTextTool  *text_tool,
       if (text)
         {
           gimp_config_sync (G_OBJECT (text), G_OBJECT (text_tool->proxy), 0);
-          gtk_text_buffer_set_text (text_tool->text_buffer, text->text, -1);
+          gimp_text_buffer_set_text (text_tool->text_buffer, text->text);
 
           gimp_text_tool_clear_layout (text_tool);
 
@@ -1042,7 +1042,7 @@ gimp_text_tool_text_notify (GimpText     *text,
                                        gimp_text_tool_buffer_changed,
                                        text_tool);
 
-      gtk_text_buffer_set_text (text_tool->text_buffer, text->text, -1);
+      gimp_text_buffer_set_text (text_tool->text_buffer, text->text);
 
       g_signal_handlers_unblock_by_func (text_tool->text_buffer,
                                          gimp_text_tool_buffer_changed,
@@ -1224,7 +1224,7 @@ gimp_text_tool_create_layer (GimpTextTool *text_tool,
     }
   else
     {
-      gchar *string = gimp_text_tool_editor_get_text (text_tool);
+      gchar *string = gimp_text_buffer_get_text (text_tool->text_buffer);
 
       g_object_set (text_tool->proxy,
                     "text",     string,
@@ -1505,12 +1505,12 @@ gimp_text_tool_set_drawable (GimpTextTool *text_tool,
 }
 
 static void
-gimp_text_tool_buffer_changed (GtkTextBuffer *text_buffer,
+gimp_text_tool_buffer_changed (GtkTextBuffer *buffer,
                                GimpTextTool  *text_tool)
 {
   if (text_tool->text)
     {
-      gchar *string = gimp_text_tool_editor_get_text (text_tool);
+      gchar *string = gimp_text_buffer_get_text (GIMP_TEXT_BUFFER (buffer));
 
       g_object_set (text_tool->proxy,
                     "text", string,
@@ -1611,17 +1611,21 @@ gimp_text_tool_set_layer (GimpTextTool *text_tool,
 gboolean
 gimp_text_tool_get_has_text_selection (GimpTextTool *text_tool)
 {
-  return gtk_text_buffer_get_has_selection (text_tool->text_buffer);
+  GtkTextBuffer *buffer = GTK_TEXT_BUFFER (text_tool->text_buffer);
+
+  return gtk_text_buffer_get_has_selection (buffer);
 }
 
 void
 gimp_text_tool_delete_selection (GimpTextTool *text_tool)
 {
-  if (gtk_text_buffer_get_has_selection (text_tool->text_buffer))
+  GtkTextBuffer *buffer = GTK_TEXT_BUFFER (text_tool->text_buffer);
+
+  if (gtk_text_buffer_get_has_selection (buffer))
     {
       gimp_draw_tool_pause (GIMP_DRAW_TOOL (text_tool));
 
-      gtk_text_buffer_delete_selection (text_tool->text_buffer, TRUE, TRUE);
+      gtk_text_buffer_delete_selection (buffer, TRUE, TRUE);
 
       gimp_draw_tool_resume (GIMP_DRAW_TOOL (text_tool));
     }
@@ -1642,7 +1646,8 @@ gimp_text_tool_cut_clipboard (GimpTextTool *text_tool)
 
   gimp_draw_tool_pause (GIMP_DRAW_TOOL (text_tool));
 
-  gtk_text_buffer_cut_clipboard (text_tool->text_buffer, clipboard, TRUE);
+  gtk_text_buffer_cut_clipboard (GTK_TEXT_BUFFER (text_tool->text_buffer),
+                                 clipboard, TRUE);
 
   gimp_draw_tool_resume (GIMP_DRAW_TOOL (text_tool));
 }
@@ -1660,7 +1665,8 @@ gimp_text_tool_copy_clipboard (GimpTextTool *text_tool)
   clipboard = gtk_widget_get_clipboard (GTK_WIDGET (shell),
                                         GDK_SELECTION_CLIPBOARD);
 
-  gtk_text_buffer_copy_clipboard (text_tool->text_buffer, clipboard);
+  gtk_text_buffer_copy_clipboard (GTK_TEXT_BUFFER (text_tool->text_buffer),
+                                  clipboard);
 }
 
 void
@@ -1678,7 +1684,8 @@ gimp_text_tool_paste_clipboard (GimpTextTool *text_tool)
 
   gimp_draw_tool_pause (GIMP_DRAW_TOOL (text_tool));
 
-  gtk_text_buffer_paste_clipboard (text_tool->text_buffer, clipboard, NULL, TRUE);
+  gtk_text_buffer_paste_clipboard (GTK_TEXT_BUFFER (text_tool->text_buffer),
+                                   clipboard, NULL, TRUE);
 
   gimp_draw_tool_resume (GIMP_DRAW_TOOL (text_tool));
 }
diff --git a/app/tools/gimptexttool.h b/app/tools/gimptexttool.h
index a61d257..09023fc 100644
--- a/app/tools/gimptexttool.h
+++ b/app/tools/gimptexttool.h
@@ -49,7 +49,7 @@ struct _GimpTextTool
 
   gboolean        moving;
 
-  GtkTextBuffer  *text_buffer;
+  GimpTextBuffer *text_buffer;
 
   GimpText       *text;
   GimpTextLayer  *layer;
diff --git a/app/widgets/Makefile.am b/app/widgets/Makefile.am
index ace0d1d..86f8bf9 100644
--- a/app/widgets/Makefile.am
+++ b/app/widgets/Makefile.am
@@ -296,6 +296,8 @@ libappwidgets_a_sources = \
 	gimptemplateeditor.h		\
 	gimptemplateview.c		\
 	gimptemplateview.h		\
+	gimptextbuffer.c		\
+	gimptextbuffer.h		\
 	gimptexteditor.c		\
 	gimptexteditor.h		\
 	gimptextproxy.c			\
diff --git a/app/widgets/gimperrorconsole.c b/app/widgets/gimperrorconsole.c
index 680dca4..d55ba7f 100644
--- a/app/widgets/gimperrorconsole.c
+++ b/app/widgets/gimperrorconsole.c
@@ -34,6 +34,7 @@
 
 #include "gimperrorconsole.h"
 #include "gimpmenufactory.h"
+#include "gimptextbuffer.h"
 #include "gimpwidgets-utils.h"
 
 #include "gimp-intl.h"
@@ -73,7 +74,7 @@ gimp_error_console_init (GimpErrorConsole *console)
 {
   GtkWidget *scrolled_window;
 
-  console->text_buffer = gtk_text_buffer_new (NULL);
+  console->text_buffer = GTK_TEXT_BUFFER (gimp_text_buffer_new ());
 
   gtk_text_buffer_create_tag (console->text_buffer, "title",
                               "scale",  PANGO_SCALE_LARGE,
diff --git a/app/widgets/gimptextbuffer.c b/app/widgets/gimptextbuffer.c
new file mode 100644
index 0000000..36d5a22
--- /dev/null
+++ b/app/widgets/gimptextbuffer.c
@@ -0,0 +1,288 @@
+/* GIMP - The GNU Image Manipulation Program
+ * Copyright (C) 1995 Spencer Kimball and Peter Mattis
+ *
+ * GimpTextBuffer
+ * Copyright (C) 2010  Michael Natterer <mitch gimp org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <string.h>
+#include <sys/types.h>
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#include <glib/gstdio.h>
+
+#include <glib.h>
+
+#ifdef G_OS_WIN32
+#include "libgimpbase/gimpwin32-io.h"
+#endif
+
+#include <gegl.h>
+#include <gtk/gtk.h>
+
+#include "libgimpbase/gimpbase.h"
+
+#include "widgets-types.h"
+
+#include "gimptextbuffer.h"
+
+#include "gimp-intl.h"
+
+
+/*  local function prototypes  */
+
+static GObject * gimp_text_buffer_constructor (GType                  type,
+                                               guint                  n_params,
+                                               GObjectConstructParam *params);
+static void      gimp_text_buffer_dispose     (GObject               *object);
+static void      gimp_text_buffer_finalize    (GObject               *object);
+
+
+G_DEFINE_TYPE (GimpTextBuffer, gimp_text_buffer, GTK_TYPE_TEXT_BUFFER)
+
+#define parent_class gimp_text_buffer_parent_class
+
+
+static void
+gimp_text_buffer_class_init (GimpTextBufferClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->constructor = gimp_text_buffer_constructor;
+  object_class->dispose     = gimp_text_buffer_dispose;
+  object_class->finalize    = gimp_text_buffer_finalize;
+}
+
+static void
+gimp_text_buffer_init (GimpTextBuffer *buffer)
+{
+}
+
+static GObject *
+gimp_text_buffer_constructor (GType                  type,
+                              guint                  n_params,
+                              GObjectConstructParam *params)
+{
+  GObject        *object;
+  GimpTextBuffer *buffer;
+
+  object = G_OBJECT_CLASS (parent_class)->constructor (type, n_params, params);
+
+  buffer = GIMP_TEXT_BUFFER (object);
+
+  gtk_text_buffer_set_text (GTK_TEXT_BUFFER (buffer), "", -1);
+
+  return object;
+}
+
+static void
+gimp_text_buffer_dispose (GObject *object)
+{
+  /* GimpTextBuffer *buffer = GIMP_TEXT_BUFFER (object); */
+
+  G_OBJECT_CLASS (parent_class)->dispose (object);
+}
+
+static void
+gimp_text_buffer_finalize (GObject *object)
+{
+  /* GimpTextBuffer *buffer = GIMP_TEXT_BUFFER (object); */
+
+  G_OBJECT_CLASS (parent_class)->finalize (object);
+}
+
+
+/*  public functions  */
+
+GimpTextBuffer *
+gimp_text_buffer_new (void)
+{
+  return g_object_new (GIMP_TYPE_TEXT_BUFFER, NULL);
+}
+
+void
+gimp_text_buffer_set_text (GimpTextBuffer *buffer,
+                           const gchar    *text)
+{
+  g_return_if_fail (GIMP_IS_TEXT_BUFFER (buffer));
+
+  if (text == NULL)
+    text = "";
+
+  gtk_text_buffer_set_text (GTK_TEXT_BUFFER (buffer), text, -1);
+}
+
+gchar *
+gimp_text_buffer_get_text (GimpTextBuffer *buffer)
+{
+  GtkTextIter start, end;
+
+  g_return_val_if_fail (GIMP_IS_TEXT_BUFFER (buffer), NULL);
+
+  gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (buffer), &start, &end);
+
+  return gtk_text_buffer_get_text (GTK_TEXT_BUFFER (buffer),
+                                   &start, &end, TRUE);
+}
+
+gint
+gimp_text_buffer_get_iter_index (GimpTextBuffer *buffer,
+                                 GtkTextIter    *iter)
+{
+  GtkTextIter  start;
+  gchar       *string;
+  gint         index;
+
+  g_return_val_if_fail (GIMP_IS_TEXT_BUFFER (buffer), 0);
+
+  gtk_text_buffer_get_start_iter (GTK_TEXT_BUFFER (buffer), &start);
+
+  string = gtk_text_buffer_get_text (GTK_TEXT_BUFFER (buffer),
+                                     &start, iter, TRUE);
+  index = strlen (string);
+  g_free (string);
+
+  return index;
+}
+
+gboolean
+gimp_text_buffer_load (GimpTextBuffer *buffer,
+                       const gchar    *filename,
+                       GError        **error)
+{
+  FILE        *file;
+  gchar        buf[2048];
+  gint         remaining = 0;
+  GtkTextIter  iter;
+
+  g_return_val_if_fail (GIMP_IS_TEXT_BUFFER (buffer), FALSE);
+  g_return_val_if_fail (filename != NULL, FALSE);
+  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+  file = g_fopen (filename, "r");
+
+  if (! file)
+    {
+      g_set_error_literal (error, G_FILE_ERROR,
+                           g_file_error_from_errno (errno),
+                           g_strerror (errno));
+      return FALSE;
+    }
+
+  gimp_text_buffer_set_text (buffer, NULL);
+  gtk_text_buffer_get_end_iter (GTK_TEXT_BUFFER (buffer), &iter);
+
+  while (! feof (file))
+    {
+      const char *leftover;
+      gint        count;
+      gint        to_read = sizeof (buf) - remaining - 1;
+
+      count = fread (buf + remaining, 1, to_read, file);
+      buf[count + remaining] = '\0';
+
+      g_utf8_validate (buf, count + remaining, &leftover);
+
+      gtk_text_buffer_insert (GTK_TEXT_BUFFER (buffer), &iter,
+                              buf, leftover - buf);
+      gtk_text_buffer_get_end_iter (GTK_TEXT_BUFFER (buffer), &iter);
+
+      remaining = (buf + remaining + count) - leftover;
+      g_memmove (buf, leftover, remaining);
+
+      if (remaining > 6 || count < to_read)
+        break;
+    }
+
+  if (remaining)
+    g_message (_("Invalid UTF-8 data in file '%s'."),
+               gimp_filename_to_utf8 (filename));
+
+  fclose (file);
+
+  return TRUE;
+}
+
+gboolean
+gimp_text_buffer_save (GimpTextBuffer *buffer,
+                       const gchar    *filename,
+                       gboolean        selection_only,
+                       GError        **error)
+{
+  GtkTextIter  start_iter;
+  GtkTextIter  end_iter;
+  gint         fd;
+  gchar       *text_contents;
+
+  g_return_val_if_fail (GIMP_IS_TEXT_BUFFER (buffer), FALSE);
+  g_return_val_if_fail (filename != NULL, FALSE);
+  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+  fd = g_open (filename, O_WRONLY | O_CREAT | O_APPEND, 0666);
+
+  if (fd == -1)
+    {
+      g_set_error_literal (error, G_FILE_ERROR,
+                           g_file_error_from_errno (errno),
+                           g_strerror (errno));
+      return FALSE;
+    }
+
+  if (selection_only)
+    gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (buffer),
+                                          &start_iter, &end_iter);
+  else
+    gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (buffer),
+                                &start_iter, &end_iter);
+
+  text_contents = gtk_text_buffer_get_text (GTK_TEXT_BUFFER (buffer),
+                                            &start_iter, &end_iter, TRUE);
+
+  if (text_contents)
+    {
+      gint text_length = strlen (text_contents);
+
+      if (text_length > 0)
+        {
+          gint bytes_written;
+
+          bytes_written = write (fd, text_contents, text_length);
+
+          if (bytes_written != text_length)
+            {
+              g_free (text_contents);
+              close (fd);
+              g_set_error_literal (error, G_FILE_ERROR,
+                                   g_file_error_from_errno (errno),
+                                   g_strerror (errno));
+              return FALSE;
+            }
+        }
+
+      g_free (text_contents);
+    }
+
+  close (fd);
+
+  return TRUE;
+}
diff --git a/app/widgets/gimptextbuffer.h b/app/widgets/gimptextbuffer.h
new file mode 100644
index 0000000..a7d463f
--- /dev/null
+++ b/app/widgets/gimptextbuffer.h
@@ -0,0 +1,65 @@
+/* GIMP - The GNU Image Manipulation Program
+ * Copyright (C) 1995 Spencer Kimball and Peter Mattis
+ *
+ * GimpTextBuffer
+ * Copyright (C) 2010  Michael Natterer <mitch gimp org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __GIMP_TEXT_BUFFER_H__
+#define __GIMP_TEXT_BUFFER_H__
+
+
+#define GIMP_TYPE_TEXT_BUFFER            (gimp_text_buffer_get_type ())
+#define GIMP_TEXT_BUFFER(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIMP_TYPE_TEXT_BUFFER, GimpTextBuffer))
+#define GIMP_IS_TEXT_BUFFER(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GIMP_TYPE_TEXT_BUFFER))
+#define GIMP_TEXT_BUFFER_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GIMP_TYPE_TEXT_BUFFER, GimpTextBufferClass))
+#define GIMP_IS_TEXT_BUFFER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GIMP_TYPE_TEXT_BUFFER))
+
+
+typedef struct _GimpTextBufferClass  GimpTextBufferClass;
+
+struct _GimpTextBuffer
+{
+  GtkTextBuffer  parent_instance;
+};
+
+struct _GimpTextBufferClass
+{
+  GtkTextBufferClass  parent_class;
+};
+
+
+GType            gimp_text_buffer_get_type       (void) G_GNUC_CONST;
+
+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);
+
+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);
+
+
+#endif /* __GIMP_TEXT_BUFFER_H__ */
diff --git a/app/widgets/gimptexteditor.c b/app/widgets/gimptexteditor.c
index 9c53c6d..b3fdff9 100644
--- a/app/widgets/gimptexteditor.c
+++ b/app/widgets/gimptexteditor.c
@@ -30,6 +30,7 @@
 
 #include "gimphelp-ids.h"
 #include "gimpmenufactory.h"
+#include "gimptextbuffer.h"
 #include "gimptexteditor.h"
 #include "gimpuimanager.h"
 
@@ -128,7 +129,7 @@ GtkWidget *
 gimp_text_editor_new (const gchar     *title,
                       GtkWindow       *parent,
                       GimpMenuFactory *menu_factory,
-                      GtkTextBuffer   *text_buffer)
+                      GimpTextBuffer  *text_buffer)
 {
   GimpTextEditor *editor;
   GtkWidget      *content_area;
@@ -138,6 +139,7 @@ gimp_text_editor_new (const gchar     *title,
   g_return_val_if_fail (title != NULL, NULL);
   g_return_val_if_fail (parent == NULL || GTK_IS_WINDOW (parent), NULL);
   g_return_val_if_fail (GIMP_IS_MENU_FACTORY (menu_factory), NULL);
+  g_return_val_if_fail (GIMP_IS_TEXT_BUFFER (text_buffer), NULL);
 
   editor = g_object_new (GIMP_TYPE_TEXT_EDITOR,
                          "title",         title,
@@ -181,7 +183,7 @@ gimp_text_editor_new (const gchar     *title,
   gtk_box_pack_start (GTK_BOX (content_area), scrolled_window, TRUE, TRUE, 0);
   gtk_widget_show (scrolled_window);
 
-  editor->view = gtk_text_view_new_with_buffer (text_buffer);
+  editor->view = gtk_text_view_new_with_buffer (GTK_TEXT_BUFFER (text_buffer));
   gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (editor->view),
                                GTK_WRAP_WORD_CHAR);
   gtk_container_add (GTK_CONTAINER (scrolled_window), editor->view);
@@ -238,16 +240,12 @@ gchar *
 gimp_text_editor_get_text (GimpTextEditor *editor)
 {
   GtkTextBuffer *buffer;
-  GtkTextIter    start_iter;
-  GtkTextIter    end_iter;
 
   g_return_val_if_fail (GIMP_IS_TEXT_EDITOR (editor), NULL);
 
   buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->view));
 
-  gtk_text_buffer_get_bounds (buffer, &start_iter, &end_iter);
-
-  return gtk_text_buffer_get_text (buffer, &start_iter, &end_iter, FALSE);
+  return gimp_text_buffer_get_text (GIMP_TEXT_BUFFER (buffer));
 }
 
 void
diff --git a/app/widgets/gimptexteditor.h b/app/widgets/gimptexteditor.h
index 02d2fc3..25b7a79 100644
--- a/app/widgets/gimptexteditor.h
+++ b/app/widgets/gimptexteditor.h
@@ -56,7 +56,7 @@ GType               gimp_text_editor_get_type      (void) G_GNUC_CONST;
 GtkWidget         * gimp_text_editor_new           (const gchar       *title,
                                                     GtkWindow         *parent,
                                                     GimpMenuFactory   *menu_factory,
-                                                    GtkTextBuffer     *text_buffer);
+                                                    GimpTextBuffer    *text_buffer);
 
 void                gimp_text_editor_set_text      (GimpTextEditor    *editor,
                                                     const gchar       *text,
diff --git a/app/widgets/gimpwidgets-utils.c b/app/widgets/gimpwidgets-utils.c
index 762a069..559ad00 100644
--- a/app/widgets/gimpwidgets-utils.c
+++ b/app/widgets/gimpwidgets-utils.c
@@ -22,22 +22,7 @@
 
 #undef GSEAL_ENABLE
 
-#include <errno.h>
-#include <fcntl.h>
 #include <string.h>
-#include <sys/types.h>
-
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-
-#include <glib/gstdio.h>
-
-#include <glib.h>
-
-#ifdef G_OS_WIN32
-#include "libgimpbase/gimpwin32-io.h"
-#endif
 
 #include <gtk/gtk.h>
 
@@ -887,125 +872,6 @@ gimp_window_set_transient_for (GtkWindow *window,
 #endif
 }
 
-gboolean
-gimp_text_buffer_load (GtkTextBuffer  *buffer,
-                       const gchar    *filename,
-                       GError        **error)
-{
-  FILE        *file;
-  gchar        buf[2048];
-  gint         remaining = 0;
-  GtkTextIter  iter;
-
-  g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
-  g_return_val_if_fail (filename != NULL, FALSE);
-  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
-
-  file = g_fopen (filename, "r");
-
-  if (! file)
-    {
-      g_set_error_literal (error, G_FILE_ERROR,
-                           g_file_error_from_errno (errno),
-                           g_strerror (errno));
-      return FALSE;
-    }
-
-  gtk_text_buffer_set_text (buffer, "", 0);
-  gtk_text_buffer_get_iter_at_offset (buffer, &iter, 0);
-
-  while (! feof (file))
-    {
-      const char *leftover;
-      gint        count;
-      gint        to_read = sizeof (buf) - remaining - 1;
-
-      count = fread (buf + remaining, 1, to_read, file);
-      buf[count + remaining] = '\0';
-
-      g_utf8_validate (buf, count + remaining, &leftover);
-
-      gtk_text_buffer_insert (buffer, &iter, buf, leftover - buf);
-      gtk_text_buffer_get_iter_at_offset (buffer, &iter, -1);
-
-      remaining = (buf + remaining + count) - leftover;
-      g_memmove (buf, leftover, remaining);
-
-      if (remaining > 6 || count < to_read)
-        break;
-    }
-
-  if (remaining)
-    g_message (_("Invalid UTF-8 data in file '%s'."),
-               gimp_filename_to_utf8 (filename));
-
-  fclose (file);
-
-  return TRUE;
-}
-
-gboolean
-gimp_text_buffer_save (GtkTextBuffer  *buffer,
-                       const gchar    *filename,
-                       gboolean        selection_only,
-                       GError        **error)
-{
-  GtkTextIter  start_iter;
-  GtkTextIter  end_iter;
-  gint         fd;
-  gchar       *text_contents;
-
-  g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
-  g_return_val_if_fail (filename != NULL, FALSE);
-  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
-
-  fd = g_open (filename, O_WRONLY | O_CREAT | O_APPEND, 0666);
-
-  if (fd == -1)
-    {
-      g_set_error_literal (error, G_FILE_ERROR,
-                           g_file_error_from_errno (errno),
-                           g_strerror (errno));
-      return FALSE;
-    }
-
-  if (selection_only)
-    gtk_text_buffer_get_selection_bounds (buffer, &start_iter, &end_iter);
-  else
-    gtk_text_buffer_get_bounds (buffer, &start_iter, &end_iter);
-
-  text_contents = gtk_text_buffer_get_text (buffer,
-                                            &start_iter, &end_iter, TRUE);
-
-  if (text_contents)
-    {
-      gint text_length = strlen (text_contents);
-
-      if (text_length > 0)
-        {
-          gint bytes_written;
-
-          bytes_written = write (fd, text_contents, text_length);
-
-          if (bytes_written != text_length)
-            {
-              g_free (text_contents);
-              close (fd);
-              g_set_error_literal (error, G_FILE_ERROR,
-                                   g_file_error_from_errno (errno),
-                                   g_strerror (errno));
-              return FALSE;
-            }
-        }
-
-      g_free (text_contents);
-    }
-
-  close (fd);
-
-  return TRUE;
-}
-
 void
 gimp_toggle_button_set_visible (GtkToggleButton *toggle,
                                 GtkWidget       *widget)
diff --git a/app/widgets/gimpwidgets-utils.h b/app/widgets/gimpwidgets-utils.h
index 94a0262..c6184d1 100644
--- a/app/widgets/gimpwidgets-utils.h
+++ b/app/widgets/gimpwidgets-utils.h
@@ -71,13 +71,6 @@ void              gimp_window_set_hint             (GtkWindow            *window
 GdkNativeWindow   gimp_window_get_native           (GtkWindow            *window);
 void              gimp_window_set_transient_for    (GtkWindow            *window,
                                                     guint32               parent_ID);
-gboolean          gimp_text_buffer_load            (GtkTextBuffer        *buffer,
-                                                    const gchar          *filename,
-                                                    GError              **error);
-gboolean          gimp_text_buffer_save            (GtkTextBuffer        *buffer,
-                                                    const gchar          *filename,
-                                                    gboolean              selection_only,
-                                                    GError              **error);
 void              gimp_toggle_button_set_visible   (GtkToggleButton      *toggle,
                                                     GtkWidget            *widget);
 void              gimp_widget_set_accel_help       (GtkWidget            *widget,
diff --git a/app/widgets/widgets-types.h b/app/widgets/widgets-types.h
index 27064a2..a31bf93 100644
--- a/app/widgets/widgets-types.h
+++ b/app/widgets/widgets-types.h
@@ -224,9 +224,10 @@ typedef struct _GimpCellRendererDashes       GimpCellRendererDashes;
 typedef struct _GimpCellRendererViewable     GimpCellRendererViewable;
 
 
-/*  misc utilities & constructors  */
+/*  misc objects  */
 
 typedef struct _GimpDialogFactory            GimpDialogFactory;
+typedef struct _GimpTextBuffer               GimpTextBuffer;
 typedef struct _GimpUIConfigurer             GimpUIConfigurer;
 
 



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