From e16b92298dcbb1d3cd79afa990cac51dba6aa176 Mon Sep 17 00:00:00 2001 From: Michael Natterer Date: Sun, 26 Oct 2008 23:30:23 +0000 Subject: [PATCH] add integer x_pos member to remember the x cursor position when moving up 2008-10-27 Michael Natterer * app/tools/gimptexttool.h: add integer x_pos member to remember the x cursor position when moving up and down across shorter lines. * app/tools/gimptexttool.c (gimp_text_tool_key_press): implement moving the cursor up and down. The x_pos probably needs to be reset in a few more places but it seems to work pretty nicely already. svn path=/trunk/; revision=27430 --- ChangeLog | 11 ++++++ app/tools/gimptexttool.c | 73 ++++++++++++++++++++++++++++++++++++++++ app/tools/gimptexttool.h | 2 ++ 3 files changed, 86 insertions(+) diff --git a/ChangeLog b/ChangeLog index b9b764b459..21c4404f59 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2008-10-27 Michael Natterer + + * app/tools/gimptexttool.h: add integer x_pos member to remember + the x cursor position when moving up and down across shorter + lines. + + * app/tools/gimptexttool.c (gimp_text_tool_key_press): implement + moving the cursor up and down. The x_pos probably needs to be + reset in a few more places but it seems to work pretty nicely + already. + 2008-10-26 Michael Natterer * app/tools/gimptexttool.c: handle Tab, some more cleanup. diff --git a/app/tools/gimptexttool.c b/app/tools/gimptexttool.c index 02acf47065..3bee5892dc 100644 --- a/app/tools/gimptexttool.c +++ b/app/tools/gimptexttool.c @@ -291,6 +291,8 @@ gimp_text_tool_init (GimpTextTool *text_tool) "context/context-font-select-set"); text_tool->handle_rectangle_change_complete = TRUE; + + text_tool->x_pos = -1; } static GObject * @@ -752,11 +754,13 @@ gimp_text_tool_key_press (GimpTool *tool, GtkTextMark *selection_bound; GtkTextIter cursor, selection; GtkTextIter *sel_start; + gint x_pos = -1; gboolean retval = TRUE; if (gtk_im_context_filter_keypress (text_tool->im_context, kevent)) { text_tool->needs_im_reset = TRUE; + text_tool->x_pos = -1; return TRUE; } @@ -832,6 +836,73 @@ gimp_text_tool_key_press (GimpTool *tool, gimp_text_tool_reset_im_context (text_tool); break; + case GDK_Up: + case GDK_KP_Up: + case GDK_Down: + case GDK_KP_Down: + { + gint offset; + gint line; + gint trailing; + PangoLayoutLine *layout_line; + + offset = gtk_text_iter_get_offset (&selection); + + pango_layout_index_to_line_x (text_tool->layout->layout, + offset, + FALSE, &line, &x_pos); + + if (text_tool->x_pos != -1) + x_pos = text_tool->x_pos; + + if (kevent->keyval == GDK_Up || + kevent->keyval == GDK_KP_Up) + { + line--; + if (line < 0) + { + gtk_text_iter_set_line_offset (&selection, 0); + gtk_text_buffer_select_range (text_tool->text_buffer, sel_start, + &selection); + break; + } + } + else + { + line++; + } + + layout_line = pango_layout_get_line_readonly (text_tool->layout->layout, + line); + if (! layout_line) + { + if (kevent->keyval == GDK_Up || + kevent->keyval == GDK_KP_Up) + { + gtk_text_iter_set_line_offset (&selection, 0); + gtk_text_buffer_select_range (text_tool->text_buffer, sel_start, + &selection); + } + else + { + gtk_text_iter_forward_to_line_end (&selection); + gtk_text_buffer_select_range (text_tool->text_buffer, sel_start, + &selection); + } + break; + } + + pango_layout_line_x_to_index (layout_line, x_pos, &offset, &trailing); + + gtk_text_buffer_get_iter_at_offset (text_tool->text_buffer, + &selection, offset + trailing); + + gtk_text_buffer_place_cursor (text_tool->text_buffer, &selection); + gtk_text_buffer_select_range (text_tool->text_buffer, sel_start, + &selection); + } + break; + case GDK_Home: case GDK_KP_Home: gtk_text_iter_set_line (&selection, gtk_text_iter_get_line (&selection)); @@ -851,6 +922,8 @@ gimp_text_tool_key_press (GimpTool *tool, retval = FALSE; } + text_tool->x_pos = x_pos; + gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool)); return retval; diff --git a/app/tools/gimptexttool.h b/app/tools/gimptexttool.h index 1083aed27c..e1ccbf1c44 100644 --- a/app/tools/gimptexttool.h +++ b/app/tools/gimptexttool.h @@ -67,6 +67,8 @@ struct _GimpTextTool gboolean text_box_fixed; gboolean text_cursor_changing; + gint x_pos; + GimpTextLayout *layout; };