[gimp] app: add function gimp_text_tool_editor_get_iter_index()



commit 97db8b9b420ecb53498d91922a517fa607b68ef3
Author: Michael Natterer <mitch gimp org>
Date:   Wed Feb 24 00:18:50 2010 +0100

    app: add function gimp_text_tool_editor_get_iter_index()
    
    which returns the byte index at a GtkTextIter, and use it all over the
    place.

 app/tools/gimptexttool-editor.c |   31 +++++++++++++++++++++----------
 app/tools/gimptexttool-editor.h |    3 +++
 app/tools/gimptexttool.c        |   12 ++----------
 3 files changed, 26 insertions(+), 20 deletions(-)
---
diff --git a/app/tools/gimptexttool-editor.c b/app/tools/gimptexttool-editor.c
index b134cf0..95c0e7b 100644
--- a/app/tools/gimptexttool-editor.c
+++ b/app/tools/gimptexttool-editor.c
@@ -453,6 +453,24 @@ gimp_text_tool_editor_get_text (GimpTextTool *text_tool)
                                    &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,
@@ -463,10 +481,8 @@ gimp_text_tool_editor_get_cursor_rect (GimpTextTool   *text_tool,
   PangoLayout    *layout;
   PangoRectangle  ink_extents;
   PangoRectangle  logical_extents;
-  GtkTextIter     start;
   GtkTextIter     cursor;
   gint            cursor_index;
-  gchar          *string;
 
   g_return_if_fail (GIMP_IS_TEXT_TOOL (text_tool));
   g_return_if_fail (cursor_rect != NULL);
@@ -488,13 +504,9 @@ gimp_text_tool_editor_get_cursor_rect (GimpTextTool   *text_tool,
   else
     *logical_off_y = 0;
 
-  gtk_text_buffer_get_start_iter (buffer, &start);
   gtk_text_buffer_get_iter_at_mark (buffer, &cursor,
                                     gtk_text_buffer_get_insert (buffer));
-
-  string = gtk_text_buffer_get_text (buffer, &start, &cursor, TRUE);
-  cursor_index = strlen (string);
-  g_free (string);
+  cursor_index = gimp_text_tool_editor_get_iter_index (text_tool, &cursor);
 
   pango_layout_index_to_pos (layout, cursor_index, cursor_rect);
   gimp_text_layout_transform_rect (text_tool->layout, cursor_rect);
@@ -659,9 +671,8 @@ gimp_text_tool_move_cursor (GimpTextTool    *text_tool,
 
         gtk_text_buffer_get_bounds (buffer, &start, &end);
 
-        string = gtk_text_buffer_get_text (buffer, &start, &cursor, TRUE);
-        cursor_index = strlen (string);
-        g_free (string);
+        cursor_index = gimp_text_tool_editor_get_iter_index (text_tool,
+                                                             &cursor);
 
         gimp_text_tool_ensure_layout (text_tool);
 
diff --git a/app/tools/gimptexttool-editor.h b/app/tools/gimptexttool-editor.h
index 31e6b55..7726f03 100644
--- a/app/tools/gimptexttool-editor.h
+++ b/app/tools/gimptexttool-editor.h
@@ -47,6 +47,9 @@ 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 de80d52..b99474c 100644
--- a/app/tools/gimptexttool.c
+++ b/app/tools/gimptexttool.c
@@ -753,22 +753,14 @@ gimp_text_tool_draw_selection (GimpDrawTool *draw_tool,
   GtkTextBuffer   *buffer    = text_tool->text_buffer;
   PangoLayout     *layout;
   PangoLayoutIter *iter;
-  GtkTextIter      start;
   GtkTextIter      sel_start, sel_end;
   gint             min, max;
-  gchar           *string;
   gint             i;
 
-  gtk_text_buffer_get_start_iter (buffer, &start);
   gtk_text_buffer_get_selection_bounds (buffer, &sel_start, &sel_end);
 
-  string = gtk_text_buffer_get_text (buffer, &start, &sel_start, FALSE);
-  min = strlen (string);
-  g_free (string);
-
-  string = gtk_text_buffer_get_text (buffer, &start, &sel_end, FALSE);
-  max = strlen (string);
-  g_free (string);
+  min = gimp_text_tool_editor_get_iter_index (text_tool, &sel_start);
+  max = gimp_text_tool_editor_get_iter_index (text_tool, &sel_end);
 
   layout = gimp_text_layout_get_pango_layout (text_tool->layout);
 



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