GtkEditable and GtkTextView



Here's the first cut of a patch (against the gtk-im-new branch), to make
GtkTextView support the GtkEditable interface.

It works for me, but it seems very short, what obvious things did I forget
to do? 

-- 
I speak for myself, not my employer
Index: gtktextview.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtktextview.c,v
retrieving revision 1.14.2.3
diff -u -r1.14.2.3 gtktextview.c
--- gtktextview.c	2000/09/19 15:17:12	1.14.2.3
+++ gtktextview.c	2000/10/16 19:54:14
@@ -4,6 +4,7 @@
  * Copyright (c) 1994-1996 Sun Microsystems, Inc.
  * Copyright (c) 1999 by Scriptics Corporation.
  * Copyright (c) 2000      Red Hat, Inc.
+ * Copyright (c) 2000      SuSE Linux Ltd
  * Tk -> Gtk port by Havoc Pennington <hp redhat com>
  *
  * This software is copyrighted by the Regents of the University of
@@ -92,6 +93,7 @@
 
 static void gtk_text_view_init                 (GtkTextView      *text_view);
 static void gtk_text_view_class_init           (GtkTextViewClass *klass);
+static void gtk_text_view_editable_init        (GtkEditableIface *iface);
 static void gtk_text_view_destroy              (GtkObject        *object);
 static void gtk_text_view_finalize             (GObject          *object);
 static void gtk_text_view_set_arg              (GtkObject        *object,
@@ -234,6 +236,28 @@
 static void gtk_text_view_popup_menu (GtkTextView    *text_view,
 				      GdkEventButton *event);
 
+/* GtkEditable method implementations */
+
+static void gtk_text_view_insert_text	(GtkEditable *editable,
+					 const gchar *new_text,
+					 gint new_text_length,
+					 gint *position);
+static void     gtk_text_view_delete_text          (GtkEditable *editable,
+						gint         start_pos,
+						gint         end_pos);
+static gchar *  gtk_text_view_get_chars            (GtkEditable *editable,
+						gint         start_pos,
+						gint         end_pos);
+static void     gtk_text_view_set_position    (GtkEditable *editable,
+						gint         position);
+static gint     gtk_text_view_get_position         (GtkEditable *editable);
+static void     gtk_text_view_set_selection_bounds (GtkEditable *editable,
+						gint         start,
+						gint         end);
+static gboolean gtk_text_view_get_selection_bounds (GtkEditable *editable,
+						gint        *start,
+						    gint    *end);
+
 enum {
   TARGET_STRING,
   TARGET_TEXT,
@@ -271,7 +295,17 @@
         (GtkClassInitFunc) NULL
       };
 
+      static const GInterfaceInfo editable_info =
+      {
+	(GInterfaceInitFunc) gtk_text_view_editable_init, /* interface_init */
+	NULL,			                          /* interface_finalize */
+	NULL			                          /* interface_data */
+      };
+
     our_type = gtk_type_unique (GTK_TYPE_CONTAINER, &our_info);
+    g_type_add_interface_static (our_type,
+			         GTK_TYPE_EDITABLE,
+			         &editable_info);
   }
 
   return our_type;
@@ -602,6 +636,18 @@
   klass->set_scroll_adjustments = gtk_text_view_set_scroll_adjustments;
 }
 
+static void
+gtk_text_view_editable_init (GtkEditableIface *iface)
+{
+  iface->insert_text = gtk_text_view_insert_text;
+  iface->delete_text = gtk_text_view_delete_text;
+  iface->get_chars = gtk_text_view_get_chars;
+  iface->set_position = gtk_text_view_set_position;
+  iface->get_position = gtk_text_view_get_position;
+  iface->set_selection_bounds = gtk_text_view_set_selection_bounds;
+  iface->get_selection_bounds = gtk_text_view_get_selection_bounds;
+}
+
 void
 gtk_text_view_init (GtkTextView *text_view)
 {
@@ -3096,4 +3142,131 @@
   gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
 		  NULL, NULL,
 		  event->button, event->time);
+}
+
+/* GtkEditable method implementations */
+
+static void gtk_text_view_insert_text	(GtkEditable *editable,
+					 const gchar *new_text,
+					 gint new_text_length,
+					 gint *position) {
+  GtkTextView *text_view = GTK_TEXT_VIEW (editable);
+  GtkTextBuffer *buffer = text_view->buffer;
+  GtkTextIter iter;
+
+  int charcount = gtk_text_buffer_get_char_count(buffer);
+  if (*position < 0 || *position > charcount) 
+    *position = charcount;
+  
+  g_object_ref (G_OBJECT (editable));
+
+  gtk_text_buffer_get_iter_at_offset (buffer, &iter, *position);
+  gtk_text_buffer_insert (buffer, &iter, new_text, new_text_length);
+  
+  g_object_unref (G_OBJECT (editable));
+}
+
+static void gtk_text_view_delete_text	(GtkEditable *editable,
+					 gint start_pos,
+					 gint end_pos) {
+  GtkTextView *text_view = GTK_TEXT_VIEW (editable);
+  GtkTextBuffer *buffer = text_view->buffer;
+  GtkTextIter start, end;
+
+  g_object_ref (G_OBJECT (editable));
+  
+  gtk_text_buffer_get_iter_at_offset (buffer, &start, start_pos);
+  gtk_text_buffer_get_iter_at_offset (buffer, &end, end_pos);
+  gtk_text_buffer_delete (buffer, &start, &end);
+  
+  g_object_unref (G_OBJECT (editable));
+}
+
+static gchar *gtk_text_view_get_chars	(GtkEditable *editable,
+					 gint start_pos,
+					 gint end_pos) {
+  GtkTextView *text_view = GTK_TEXT_VIEW (editable);
+  GtkTextBuffer *buffer = text_view->buffer;
+  GtkTextIter start, end;
+  char *chars;
+
+  g_object_ref (G_OBJECT (editable));
+  
+  gtk_text_buffer_get_iter_at_offset (buffer, &start, start_pos);
+  gtk_text_buffer_get_iter_at_offset (buffer, &end, end_pos);
+  
+  chars = gtk_text_buffer_get_text (buffer, &start, &end, TRUE);
+
+  g_object_unref (G_OBJECT (editable));
+
+  return chars;
+}
+
+static int gtk_text_view_get_position (GtkEditable *editable)
+{
+  GtkTextView *text_view = GTK_TEXT_VIEW (editable);
+  GtkTextBuffer *buffer = text_view->buffer;
+  GtkTextIter iter;
+
+  g_object_ref (G_OBJECT (editable));
+
+  gtk_text_buffer_get_iter_at_mark(buffer, &iter,
+                                   gtk_text_buffer_get_mark (buffer,
+                                                             "insert"));
+
+  g_object_unref (G_OBJECT (editable));
+
+  return gtk_text_iter_get_offset(&iter);
+}
+
+static void gtk_text_view_set_position (GtkEditable *editable, gint pos)
+{
+  GtkTextView *text_view = GTK_TEXT_VIEW (editable);
+  GtkTextBuffer *buffer = text_view->buffer;
+  GtkTextIter iter;
+
+  g_object_ref (G_OBJECT (editable));
+
+  gtk_text_buffer_get_iter_at_offset (buffer, &iter, pos);
+  gtk_text_buffer_place_cursor (buffer, &iter);
+
+  g_object_unref (G_OBJECT (editable));
+}
+
+static void     gtk_text_view_set_selection_bounds (GtkEditable *editable,
+						    gint         start,
+						    gint         end) 
+{
+  GtkTextView *text_view = GTK_TEXT_VIEW (editable);
+  GtkTextBuffer *buffer = text_view->buffer;
+  GtkTextIter iter;
+  GtkTextMark *insert_mark;
+
+  g_object_ref (G_OBJECT (editable));
+
+  gtk_text_buffer_get_iter_at_offset (buffer, &iter, start);
+  gtk_text_buffer_place_cursor (buffer, &iter);
+
+  gtk_text_buffer_get_iter_at_offset (buffer, &iter, end);
+  insert_mark = gtk_text_buffer_get_mark (text_view->buffer, "insert");
+  gtk_text_buffer_move_mark (text_view->buffer,
+			     insert_mark,
+			     &iter);
+
+  g_object_unref (G_OBJECT (editable));
+}
+
+static gboolean gtk_text_view_get_selection_bounds (GtkEditable *editable,
+						    gint        *start,
+						    gint    *end)
+{
+  GtkTextView *text_view = GTK_TEXT_VIEW (editable);
+  GtkTextBuffer *buffer = text_view->buffer;
+  GtkTextIter start_i, end_i;
+  gboolean r_val;
+
+  r_val = gtk_text_buffer_get_selection_bounds (buffer, &start_i, &end_i);
+
+  *start = gtk_text_iter_get_offset (&start_i);
+  *end = gtk_text_iter_get_offset (&end_i);
 }


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