[vte/vte-next: 216/223] Add vte_view_match_check_{event, iter} and vte_view_iter_from_event()



commit ebffcac900c79cae43d05313334e0271c6123635
Author: Christian Persch <chpe gnome org>
Date:   Wed Jun 22 20:12:54 2011 +0200

    Add vte_view_match_check_{event,iter} and vte_view_iter_from_event()
    
    Instead of having to get the event coordinates, char width/height and
    padding to pass to vte_view_match_check(), add variants that take a buffer
    iter or GdkEvent directly, as well as a function that sets an iter
    from an event.

 doc/reference/vte-sections.txt |    6 ++
 src/vte.c                      |  169 ++++++++++++++++++++++++++++++++++++++++
 src/vteapp.c                   |   16 +---
 src/vteview.h                  |   12 +++
 4 files changed, 190 insertions(+), 13 deletions(-)
---
diff --git a/doc/reference/vte-sections.txt b/doc/reference/vte-sections.txt
index 32bdcaf..8ae43cf 100644
--- a/doc/reference/vte-sections.txt
+++ b/doc/reference/vte-sections.txt
@@ -33,6 +33,8 @@ vte_view_match_add_gregex
 vte_view_match_remove
 vte_view_match_remove_all
 vte_view_match_check
+vte_view_match_check_event
+vte_view_match_check_iter
 vte_view_match_set_cursor
 vte_view_match_set_cursor_type
 vte_view_match_set_cursor_name
@@ -48,6 +50,10 @@ vte_view_get_geometry_hints
 vte_view_set_window_geometry_hints
 
 <SUBSECTION>
+vte_view_iter_from_event
+vte_view_iter_is_visible
+
+<SUBSECTION>
 VTE_STYLE_CLASS_TERMINAL
 
 <SUBSECTION>
diff --git a/src/vte.c b/src/vte.c
index e4144db..76b9f8f 100644
--- a/src/vte.c
+++ b/src/vte.c
@@ -1943,6 +1943,100 @@ vte_view_match_check(VteView *terminal, glong column, glong row,
 	return ret;
 }
 
+/**
+ * vte_view_match_check_event:
+ * @terminal: a #VteView
+ * @event: a #GdkEvent
+ * @tag: (out) (allow-none): a location to store the tag, or %NULL
+ *
+ * Checks if the text in and around the coordinates of @event matches any of the
+ * regular expressions previously set using vte_view_match_add_gregex().  If a
+ * match exists, the text string is returned and if @tag is not %NULL, the number
+ * associated with the matched regular expression will be stored in @tag.
+ *
+ * If more than one regular expression has been set with
+ * vte_view_match_add_gregex(), then expressions are checked in the order in
+ * which they were added.
+ *
+ * Returns: (transfer full): a newly allocated string which matches one of the previously
+ *   set regular expressions
+ */
+char *
+vte_view_match_check_event(VteView *view,
+                           GdkEvent *event,
+                           int *tag)
+{
+        VteBufferIter iter;
+
+        if (!vte_view_iter_from_event(view, event, &iter))
+                return NULL;
+
+        return vte_view_match_check_iter(view, &iter, tag);
+}
+
+/**
+ * vte_view_match_check_iter:
+ * @terminal: a #VteView
+ * @event: a #VteBufferIter
+ * @tag: (out) (allow-none): a location to store the tag, or %NULL
+ *
+ * Checks if the text in and around the coordinates of @iter matches any of the
+ * regular expressions previously set using vte_view_match_add_gregex().  If a
+ * match exists, the text string is returned and if @tag is not %NULL, the number
+ * associated with the matched regular expression will be stored in @tag.
+ *
+ * If more than one regular expression has been set with
+ * vte_view_match_add_gregex(), then expressions are checked in the order in
+ * which they were added.
+ *
+ * Returns: (transfer full): a newly allocated string which matches one of the previously
+ *   set regular expressions
+ */
+char *
+vte_view_match_check_iter(VteView *view,
+                          VteBufferIter *iter,
+                          int *tag)
+{
+        VteBufferIterReal *real_iter = (VteBufferIterReal *) iter;
+        VteBuffer *buffer;
+        glong row, col;
+        char *ret;
+
+        g_return_val_if_fail(VTE_IS_VIEW(view), NULL);
+
+        buffer = view->pvt->buffer;
+        if (buffer == NULL)
+                return NULL;
+
+        if (!vte_buffer_iter_is_valid(iter, view->pvt->buffer))
+                return NULL;
+
+        row = real_iter->position.row;
+        col = real_iter->position.col;
+        _vte_debug_print(VTE_DEBUG_EVENTS,
+                        "Checking for match at (%ld,%ld).\n",
+                        row, col);
+
+        if (rowcol_inside_match (view, row, col)) {
+                if (tag) {
+                        *tag = view->pvt->match_tag;
+                }
+                ret = view->pvt->match != NULL ?
+                        g_strdup (view->pvt->match) :
+                        NULL;
+        } else {
+                ret = vte_view_match_check_internal(view,
+                                                    col, row,
+                                                    tag, NULL, NULL);
+        }
+        _VTE_DEBUG_IF(VTE_DEBUG_EVENTS) {
+                if (ret != NULL)
+                        g_printerr("Matched `%s'.\n", ret);
+        }
+
+        return ret;
+}
+
 /* Emit an adjustment changed signal on our adjustment object. */
 static void
 vte_view_emit_adjustment_changed(VteView *terminal)
@@ -13394,6 +13488,81 @@ vte_view_set_window_geometry_hints(VteView *view,
                                       GDK_HINT_BASE_SIZE);
 }
 
+/**
+ * vte_view_iter_from_event:
+ * @view: a #VteView
+ * @event: a #GdkEvent
+ * @iter: (out) (allow-none): a location to store a #VteBufferIter
+ *
+ * Converts the event coordinates in @event to a #VteBufferIter
+ * on @view's buffer.
+ *
+ * If @event does not have coordinates, or @view has no buffer,
+ * or the event coordinates are outside the visible grid,
+ * returns %FALSE.
+ *
+ * Returns: %TRUE iff @iter was filled in
+ */
+gboolean
+vte_view_iter_from_event(VteView *view,
+                         GdkEvent *event,
+                         VteBufferIter *iter)
+{
+        VteBufferIterReal *real_iter = (VteBufferIterReal *) iter;
+        VteBuffer *buffer;
+        gdouble x, y;
+        glong row, col;
+
+        g_return_val_if_fail(VTE_IS_VIEW(view), FALSE);
+        g_return_val_if_fail(event != NULL, FALSE);
+
+        buffer = view->pvt->buffer;
+        if (buffer == NULL)
+                return FALSE;
+
+        if (!gdk_event_get_coords(event, &x, &y))
+                return FALSE;
+
+        if (!_vte_view_xy_to_grid(view, x, y, &col, &row))
+                return FALSE;
+
+        if (iter) {
+                _vte_buffer_iter_init(real_iter, buffer);
+
+                real_iter->position.col = col;
+                real_iter->position.row = row + buffer->pvt->screen->scroll_delta;
+        }
+
+        return TRUE;
+}
+
+/**
+ * vte_view_iter_is_visible:
+ * @view: a #VteView
+ * @iter: a valid #VteBufferIter for @view's buffer
+ *
+ * Returns: %TRUE iff the grid coordinates in @iter are within
+ *   the visible grid of @view
+ */
+gboolean
+vte_view_iter_is_visible(VteView *view,
+                         VteBufferIter *iter)
+{
+        VteBufferIterReal *real_iter = (VteBufferIterReal *) iter;
+        VteBuffer *buffer;
+        glong row;
+
+        g_return_val_if_fail(VTE_IS_VIEW(view), FALSE);
+
+        buffer = view->pvt->buffer;
+        if (vte_buffer_iter_is_valid(iter, buffer))
+                return FALSE;
+
+        row = real_iter->position.row - buffer->pvt->screen->scroll_delta;
+
+        return row >= 0 && row < buffer->pvt->row_count;
+}
+
 /* *********
  * VteBuffer
  * *********
diff --git a/src/vteapp.c b/src/vteapp.c
index 6ca16c2..924e9bc 100644
--- a/src/vteapp.c
+++ b/src/vteapp.c
@@ -132,27 +132,17 @@ status_line_changed(VteBuffer *buffer, gpointer data)
 }
 
 static int
-button_pressed(GtkWidget *widget, GdkEventButton *event, gpointer data)
+button_pressed(GtkWidget *widget, GdkEvent *event, gpointer data)
 {
 	VteView *terminal;
 	char *match;
 	int tag;
-	GtkBorder padding;
-	int char_width, char_height;
 
-	switch (event->button) {
+	switch (event->button.button) {
 	case 3:
 		terminal = VTE_VIEW(widget);
 
-                gtk_style_context_get_padding(gtk_widget_get_style_context(widget),
-                                              gtk_widget_get_state_flags(widget),
-                                              &padding);
-                char_width = vte_view_get_char_width (terminal);
-		char_height = vte_view_get_char_height (terminal);
-		match = vte_view_match_check(terminal,
-						 (event->x - padding.left) / char_width,
-						 (event->y - padding.top) / char_height,
-						 &tag);
+                match = vte_view_match_check_event(terminal, event, &tag);
 		if (match != NULL) {
 			g_print("Matched `%s' (%d).\n", match, tag);
 			g_free(match);
diff --git a/src/vteview.h b/src/vteview.h
index 9f70d42..2230c6e 100644
--- a/src/vteview.h
+++ b/src/vteview.h
@@ -160,6 +160,12 @@ void vte_view_match_remove_all(VteView *terminal);
 char *vte_view_match_check(VteView *terminal,
 			       glong column, glong row,
 			       int *tag);
+char *vte_view_match_check_event(VteView *view,
+                                 GdkEvent *event,
+                                 int *tag);
+char *vte_view_match_check_iter(VteView *view,
+                                VteBufferIter *iter,
+                                int *tag);
 
 void      vte_view_search_set_gregex      (VteView *terminal,
 					       GRegex      *regex,
@@ -181,6 +187,12 @@ void vte_view_get_geometry_hints(VteView *view,
 void vte_view_set_window_geometry_hints(VteView *view,
                                         GtkWindow *window);
 
+gboolean vte_view_iter_from_event(VteView *view,
+                                  GdkEvent *event,
+                                  VteBufferIter *iter);
+gboolean vte_view_iter_is_visible(VteView *view,
+                                  VteBufferIter *iter);
+
 G_END_DECLS
 
 #endif /* __VTE_VIEW_H__ */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]