From 4c1173b7f3dad1275d559f14431db033243602d0 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sat, 19 May 2007 02:31:53 +0000 Subject: [PATCH] Add gtk_entry_[gs]et_cursor_hadjustment() to allow automatic scrolling in 2007-05-18 Matthias Clasen * 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 | 7 ++ docs/reference/ChangeLog | 4 ++ docs/reference/gtk/gtk-sections.txt | 2 + gtk/gtk.symbols | 2 + gtk/gtkentry.c | 99 ++++++++++++++++++++++++++++- gtk/gtkentry.h | 5 ++ 6 files changed, 117 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 64fb8dc0a5..358a269101 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2007-05-18 Matthias Clasen + + * 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 * gtk/gtktextview.c: Add a toggle-cursor-visibility keybinding diff --git a/docs/reference/ChangeLog b/docs/reference/ChangeLog index dd99c7aa83..9413dbe2ea 100644 --- a/docs/reference/ChangeLog +++ b/docs/reference/ChangeLog @@ -1,3 +1,7 @@ +2007-05-18 Matthias Clasen + + * gtk/gtk-sections.txt: Add gtk_entry_[gs]et_cursor_hadjustment. + 2007-05-18 Matthias Clasen * gtk/gtk-sections.txt: Add generic icon lookup function diff --git a/docs/reference/gtk/gtk-sections.txt b/docs/reference/gtk/gtk-sections.txt index 161c2c4be3..30e367a4fe 100644 --- a/docs/reference/gtk/gtk-sections.txt +++ b/docs/reference/gtk/gtk-sections.txt @@ -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 GTK_ENTRY GTK_IS_ENTRY diff --git a/gtk/gtk.symbols b/gtk/gtk.symbols index c278ae7f93..ba34e5ab0a 100644 --- a/gtk/gtk.symbols +++ b/gtk/gtk.symbols @@ -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 diff --git a/gtk/gtkentry.c b/gtk/gtkentry.c index f9744662ab..da5e1905c4 100644 --- a/gtk/gtkentry.c +++ b/gtk/gtkentry.c @@ -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" diff --git a/gtk/gtkentry.h b/gtk/gtkentry.h index 6f2a81af11..24bf28db7c 100644 --- a/gtk/gtkentry.h +++ b/gtk/gtkentry.h @@ -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 */