Add gtk_entry_[gs]et_cursor_hadjustment() to allow automatic scrolling in

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
This commit is contained in:
Matthias Clasen 2007-05-19 02:31:53 +00:00 committed by Matthias Clasen
parent 5d5e545924
commit 4c1173b7f3
6 changed files with 117 additions and 2 deletions

View File

@ -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

View 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

View 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

View 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

View 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"

View 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
*/