[gtksourceview] Bug 132780 - Added gtk_source_view_get_visual_column
- From: Garrett Regier <gregier src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtksourceview] Bug 132780 - Added gtk_source_view_get_visual_column
- Date: Thu, 20 May 2010 06:41:43 +0000 (UTC)
commit ca4c51ca73dc47e3d915ee4b1c973729d45a72b4
Author: Garrett Regier <alias301 gmail com>
Date: Wed May 19 23:41:15 2010 -0700
Bug 132780 - Added gtk_source_view_get_visual_column
Utility function to get the visual column at an iter and takes into consideration the indent width
gtksourceview/gtksourceview.c | 49 +++++++++++++++++++++++++++++++++++++++++
gtksourceview/gtksourceview.h | 7 +++--
tests/test-widget.c | 23 +++----------------
3 files changed, 57 insertions(+), 22 deletions(-)
---
diff --git a/gtksourceview/gtksourceview.c b/gtksourceview/gtksourceview.c
index c9aeae3..e6b8cc8 100644
--- a/gtksourceview/gtksourceview.c
+++ b/gtksourceview/gtksourceview.c
@@ -4956,6 +4956,55 @@ gtk_source_view_get_draw_spaces (GtkSourceView *view)
return view->priv->draw_spaces;
}
+/**
+ * gtk_source_view_get_visual_column:
+ * @view: a #GtkSourceView.
+ * @iter: a position in @view.
+ *
+ * Determines the visual column at @iter taking into
+ * consideration the indent width of @view.
+ *
+ * Return value: the visual column at @iter.
+ */
+guint
+gtk_source_view_get_visual_column (GtkSourceView *view,
+ const GtkTextIter *iter)
+{
+ gunichar tab_char;
+ GtkTextBuffer *buffer;
+ GtkTextIter position;
+ guint column, indent_width;
+
+ g_return_val_if_fail (GTK_IS_SOURCE_VIEW (view), 0);
+ g_return_val_if_fail (iter != NULL, 0);
+
+ tab_char = g_utf8_get_char ("\t");
+ buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
+
+ column = 0;
+ indent_width = get_real_indent_width (view);
+
+ position = *iter;
+ gtk_text_iter_set_visible_line_offset (&position, 0);
+
+ while (!gtk_text_iter_equal (&position, iter))
+ {
+ if (gtk_text_iter_get_char (&position) == tab_char)
+ {
+ column += (indent_width - (column % indent_width));
+ }
+ else
+ {
+ ++column;
+ }
+
+ if (!gtk_text_iter_forward_visible_cursor_position (&position))
+ break;
+ }
+
+ return column;
+}
+
static void
gtk_source_view_style_set (GtkWidget *widget, GtkStyle *previous_style)
{
diff --git a/gtksourceview/gtksourceview.h b/gtksourceview/gtksourceview.h
index df95179..6dc8aab 100644
--- a/gtksourceview/gtksourceview.h
+++ b/gtksourceview/gtksourceview.h
@@ -268,10 +268,11 @@ GtkSourceSmartHomeEndType
void gtk_source_view_set_draw_spaces (GtkSourceView *view,
GtkSourceDrawSpacesFlags flags);
GtkSourceDrawSpacesFlags
- gtk_source_view_get_draw_spaces (GtkSourceView *view);
-
+ gtk_source_view_get_draw_spaces (GtkSourceView *view);
+guint gtk_source_view_get_visual_column (GtkSourceView *view,
+ const GtkTextIter *iter);
GtkSourceCompletion *
- gtk_source_view_get_completion (GtkSourceView *view);
+ gtk_source_view_get_completion (GtkSourceView *view);
GtkSourceGutter *gtk_source_view_get_gutter (GtkSourceView *view,
GtkTextWindowType window_type);
diff --git a/tests/test-widget.c b/tests/test-widget.c
index 1d38d9c..f1154b6 100644
--- a/tests/test-widget.c
+++ b/tests/test-widget.c
@@ -1097,8 +1097,9 @@ static void
update_cursor_position (GtkTextBuffer *buffer, gpointer user_data)
{
gchar *msg;
- gint row, col, chars, tabwidth;
- GtkTextIter iter, start;
+ gint row, chars;
+ guint col;
+ GtkTextIter iter;
GtkSourceView *view;
GtkLabel *pos_label;
gchar **classes;
@@ -1108,7 +1109,6 @@ update_cursor_position (GtkTextBuffer *buffer, gpointer user_data)
g_return_if_fail (GTK_IS_SOURCE_VIEW (user_data));
view = GTK_SOURCE_VIEW (user_data);
- tabwidth = gtk_source_view_get_tab_width (view);
pos_label = GTK_LABEL (g_object_get_data (G_OBJECT (view), "pos_label"));
gtk_text_buffer_get_iter_at_mark (buffer,
@@ -1117,22 +1117,7 @@ update_cursor_position (GtkTextBuffer *buffer, gpointer user_data)
chars = gtk_text_iter_get_offset (&iter);
row = gtk_text_iter_get_line (&iter) + 1;
-
- start = iter;
- gtk_text_iter_set_line_offset (&start, 0);
- col = 0;
-
- while (!gtk_text_iter_equal (&start, &iter))
- {
- if (gtk_text_iter_get_char (&start) == '\t')
- {
- col += (tabwidth - (col % tabwidth));
- }
- else
- ++col;
-
- gtk_text_iter_forward_char (&start);
- }
+ col = gtk_source_view_get_visual_column (view, &iter) + 1;
classes = gtk_source_buffer_get_context_classes_at_iter (GTK_SOURCE_BUFFER (buffer),
&iter);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]