]> Pileus Git - ~andy/gtk/commitdiff
Add gtk_entry_[gs]et_cursor_hadjustment() to allow automatic scrolling in
authorMatthias Clasen <mclasen@redhat.com>
Sat, 19 May 2007 02:31:53 +0000 (02:31 +0000)
committerMatthias Clasen <matthiasc@src.gnome.org>
Sat, 19 May 2007 02:31:53 +0000 (02:31 +0000)
2007-05-18  Matthias Clasen  <mclasen@redhat.com>

        * gtk/gtk.symbols:
        * gtk/gtkentry.[hc]: Add gtk_entry_[gs]et_cursor_hadjustment()
        to allow automatic scrolling in response to cursor movements
        in the entry.  (#438651, Nate Nielsen)

svn path=/trunk/; revision=17871

ChangeLog
docs/reference/ChangeLog
docs/reference/gtk/gtk-sections.txt
gtk/gtk.symbols
gtk/gtkentry.c
gtk/gtkentry.h

index 64fb8dc0a5b5c3eab9b6ebd9bc42cb1bb30d4a07..358a26910108021f95d78fb4376be5d56cf26c48 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2007-05-18  Matthias Clasen  <mclasen@redhat.com>
+
+       * gtk/gtk.symbols:
+       * gtk/gtkentry.[hc]: Add gtk_entry_[gs]et_cursor_hadjustment()
+       to allow automatic scrolling in response to cursor movements
+       in the entry.  (#438651, Nate Nielsen)
+
 2007-05-18  Matthias Clasen  <mclasen@redhat.com>
 
        * gtk/gtktextview.c: Add a toggle-cursor-visibility keybinding
index dd99c7aa83eb94d889f452b961492475a7a955f8..9413dbe2ea2299250a9df94d2d91aef5362190a3 100644 (file)
@@ -1,3 +1,7 @@
+2007-05-18  Matthias Clasen <mclasen@redhat.com>
+
+       * gtk/gtk-sections.txt: Add gtk_entry_[gs]et_cursor_hadjustment.
+
 2007-05-18  Matthias Clasen <mclasen@redhat.com>
 
        * gtk/gtk-sections.txt: Add generic icon lookup function
index 161c2c4be3fbae14991206a3a3162bf0a1dbd924..30e367a4fecf76970423116b136f3927d8ecd731 100644 (file)
@@ -1138,6 +1138,8 @@ gtk_entry_get_max_length
 gtk_entry_get_visibility
 gtk_entry_set_completion
 gtk_entry_get_completion
+gtk_entry_set_cursor_hadjustment
+gtk_entry_get_cursor_hadjustment
 <SUBSECTION Standard>
 GTK_ENTRY
 GTK_IS_ENTRY
index c278ae7f934bc05c5baf5a2a9c9679cb7e347f7d..ba34e5ab0a766f9147803953d1cb7e4c44bb6f1d 100644 (file)
@@ -1211,6 +1211,8 @@ gtk_entry_set_text
 gtk_entry_set_visibility
 gtk_entry_set_width_chars
 gtk_entry_text_index_to_layout_index
+gtk_entry_set_cursor_hadjustment
+gtk_entry_get_cursor_hadjustment
 #endif
 #endif
 
index f9744662abc85e3158a3af0381a66e83501cf133..da5e1905c4b18431d98dd853b84e3f2b83168025 100644 (file)
@@ -89,6 +89,8 @@ struct _GtkEntryPrivate
   gint focus_width;
   gboolean interior_focus;
   GtkShadowType shadow_type;
+
+  GtkAdjustment *cursor_hadjustment;
 };
 
 typedef struct _GtkEntryPasswordHint GtkEntryPasswordHint;
@@ -352,6 +354,7 @@ static void         get_widget_window_size             (GtkEntry       *entry,
                                                        gint           *y,
                                                        gint           *width,
                                                        gint           *height);
+static void         gtk_entry_move_adjustments         (GtkEntry       *entry);
 
 /* Completion */
 static gint         gtk_entry_completion_timeout       (gpointer            data);
@@ -3129,8 +3132,11 @@ gtk_entry_set_positions (GtkEntry *entry,
 
   g_object_thaw_notify (G_OBJECT (entry));
 
-  if (changed)
-    gtk_entry_recompute (entry);
+  if (changed) 
+    {
+      gtk_entry_move_adjustments (entry);
+      gtk_entry_recompute (entry);
+    }
 }
 
 static void
@@ -3841,6 +3847,37 @@ gtk_entry_adjust_scroll (GtkEntry *entry)
   g_object_notify (G_OBJECT (entry), "scroll-offset");
 }
 
+static void
+gtk_entry_move_adjustments (GtkEntry *entry)
+{
+  GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
+  PangoContext *context;
+  PangoFontMetrics *metrics;
+  gint x, layout_x, border_x, border_y;
+  gint char_width;
+
+  if (!priv->cursor_hadjustment)
+    return;
+
+  /* Cursor position, layout offset, border width, and widget allocation */
+  gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &x, NULL);
+  get_layout_position (entry, &layout_x, NULL);
+  _gtk_entry_get_borders (entry, &border_x, &border_y);
+  x += entry->widget.allocation.x + layout_x + border_x;
+
+  /* Approximate width of a char, so user can see what is ahead/behind */
+  context = gtk_widget_get_pango_context (GTK_WIDGET (entry));
+  metrics = pango_context_get_metrics (context, 
+                                       entry->widget.style->font_desc,
+                                      pango_context_get_language (context));
+  char_width = pango_font_metrics_get_approximate_char_width (metrics) / PANGO_SCALE;
+
+  /* Scroll it */
+  gtk_adjustment_clamp_page (priv->cursor_hadjustment, 
+                            x - (char_width + 1),   /* one char + one pixel before */
+                            x + (char_width + 2));  /* one char + cursor + one pixel after */
+}
+
 static gint
 gtk_entry_move_visually (GtkEntry *entry,
                         gint      start,
@@ -6068,5 +6105,63 @@ gtk_entry_get_completion (GtkEntry *entry)
   return completion;
 }
 
+/**
+ * gtk_entry_set_cursor_hadjustment:
+ * @entry: a #GtkEntry
+ * @adjustment: an adjustment which should be adjusted when the cursor is moved,
+ *              or %NULL
+ *
+ * Hooks up an adjustment to the cursor position in an entry, so that when 
+ * the cursor is moved, the adjustment is scrolled to show that position. 
+ * See gtk_scrolled_window_get_hadjustment() for a typical way of obtaining 
+ * the adjustment.
+ *
+ * The adjustment has to be in pixel units and in the same coordinate system 
+ * as the entry. 
+ * 
+ * Since: 2.12
+ */
+void
+gtk_entry_set_cursor_hadjustment (GtkEntry      *entry,
+                                  GtkAdjustment *adjustment)
+{
+  GtkEntryPrivate *priv;
+
+  g_return_if_fail (GTK_IS_ENTRY (entry));
+  if (adjustment)
+    g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
+
+  priv = GTK_ENTRY_GET_PRIVATE (entry);
+  if (priv->cursor_hadjustment)
+    g_object_unref (priv->cursor_hadjustment);
+  if (adjustment)
+    g_object_ref (adjustment);
+  priv->cursor_hadjustment = adjustment;
+}
+
+/**
+ * gtk_entry_get_cursor_hadjustment:
+ * @entry: a #GtkEntry
+ *
+ * Retrieves the horizontal cursor adjustment for the entry. 
+ * See gtk_entry_set_cursor_hadjustment().
+ *
+ * Return value: the horizontal cursor adjustment, or %NULL 
+ *   if none has been set.
+ * 
+ * Since: 2.12
+ */
+GtkAdjustment*
+gtk_entry_get_cursor_hadjustment (GtkEntry *entry)
+{
+  GtkEntryPrivate *priv;
+    
+  g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
+
+  priv = GTK_ENTRY_GET_PRIVATE (entry);
+
+  return priv->cursor_hadjustment;
+}
+
 #define __GTK_ENTRY_C__
 #include "gtkaliasdef.c"
index 6f2a81af11bdd4957da167cd818bbccbee219706..24bf28db7c2c3bf2f647e33d759b8cb66d7729a3 100644 (file)
@@ -191,6 +191,11 @@ gint       gtk_entry_layout_index_to_text_index (GtkEntry      *entry,
 gint       gtk_entry_text_index_to_layout_index (GtkEntry      *entry,
                                                  gint           text_index);
 
+/* For scrolling cursor appropriately 
+ */
+void           gtk_entry_set_cursor_hadjustment (GtkEntry      *entry,
+                                                 GtkAdjustment *adjustment);
+GtkAdjustment* gtk_entry_get_cursor_hadjustment (GtkEntry      *entry);
 
 /* Deprecated compatibility functions
  */