+static void
+gtk_text_view_set_handle_position (GtkTextView *text_view,
+ GtkTextIter *iter,
+ GtkTextHandlePosition pos)
+{
+ GtkTextViewPrivate *priv;
+ GdkRectangle rect;
+ gint x, y;
+
+ priv = text_view->priv;
+ gtk_text_view_get_cursor_locations (text_view, iter, &rect, NULL);
+
+ x = rect.x - priv->xoffset;
+ y = rect.y - priv->yoffset;
+
+ if (!_gtk_text_handle_get_is_dragged (priv->text_handle, pos) &&
+ (x < 0 || x > SCREEN_WIDTH (text_view) ||
+ y < 0 || y > SCREEN_HEIGHT (text_view)))
+ {
+ /* Hide the handle if it's not being manipulated
+ * and fell outside of the visible text area.
+ */
+ _gtk_text_handle_set_visible (priv->text_handle, pos, FALSE);
+ }
+ else
+ {
+ _gtk_text_handle_set_visible (priv->text_handle, pos, TRUE);
+
+ rect.x = CLAMP (x, 0, SCREEN_WIDTH (text_view));
+ rect.y = CLAMP (y, 0, SCREEN_HEIGHT (text_view));
+ _gtk_text_handle_set_position (priv->text_handle, pos, &rect);
+ }
+}
+
+static void
+gtk_text_view_handle_dragged (GtkTextHandle *handle,
+ GtkTextHandlePosition pos,
+ gint x,
+ gint y,
+ GtkTextView *text_view)
+{
+ GtkTextViewPrivate *priv;
+ GtkTextIter old_cursor, old_bound;
+ GtkTextIter cursor, bound, iter;
+ GtkTextIter *min, *max;
+ GtkTextHandleMode mode;
+ GtkTextBuffer *buffer;
+ GtkTextHandlePosition cursor_pos;
+
+ priv = text_view->priv;
+ buffer = get_buffer (text_view);
+ mode = _gtk_text_handle_get_mode (handle);
+
+ gtk_text_view_selection_bubble_popup_unset (text_view);
+ gtk_text_layout_get_iter_at_pixel (priv->layout, &iter,
+ x + priv->xoffset,
+ y + priv->yoffset);
+ gtk_text_buffer_get_iter_at_mark (buffer, &old_cursor,
+ gtk_text_buffer_get_insert (buffer));
+ gtk_text_buffer_get_iter_at_mark (buffer, &old_bound,
+ gtk_text_buffer_get_selection_bound (buffer));
+ cursor = old_cursor;
+ bound = old_bound;
+
+ if (mode == GTK_TEXT_HANDLE_MODE_CURSOR ||
+ gtk_text_iter_compare (&cursor, &bound) >= 0)
+ {
+ cursor_pos = GTK_TEXT_HANDLE_POSITION_CURSOR;
+ max = &cursor;
+ min = &bound;
+ }
+ else
+ {
+ cursor_pos = GTK_TEXT_HANDLE_POSITION_SELECTION_START;
+ max = &bound;
+ min = &cursor;
+ }
+
+ if (pos == GTK_TEXT_HANDLE_POSITION_SELECTION_END)
+ {
+ if (mode == GTK_TEXT_HANDLE_MODE_SELECTION &&
+ gtk_text_iter_compare (&iter, min) <= 0)
+ {
+ iter = *min;
+ gtk_text_iter_forward_char (&iter);
+ }
+
+ *max = iter;
+ gtk_text_view_set_handle_position (text_view, &iter, pos);
+ }
+ else
+ {
+ if (mode == GTK_TEXT_HANDLE_MODE_SELECTION &&
+ gtk_text_iter_compare (&iter, max) >= 0)
+ {
+ iter = *max;
+ gtk_text_iter_backward_char (&iter);
+ }
+
+ *min = iter;
+ gtk_text_view_set_handle_position (text_view, &iter, pos);
+ }
+
+ if (gtk_text_iter_compare (&old_cursor, &cursor) != 0 ||
+ gtk_text_iter_compare (&old_bound, &bound) != 0)
+ {
+ if (mode == GTK_TEXT_HANDLE_MODE_CURSOR)
+ gtk_text_buffer_place_cursor (buffer, &cursor);
+ else
+ gtk_text_buffer_select_range (buffer, &cursor, &bound);
+
+ if (_gtk_text_handle_get_is_dragged (priv->text_handle, cursor_pos))
+ gtk_text_view_scroll_mark_onscreen (text_view,
+ gtk_text_buffer_get_insert (buffer));
+ else
+ gtk_text_view_scroll_mark_onscreen (text_view,
+ gtk_text_buffer_get_selection_bound (buffer));
+ }
+}
+
+static void
+gtk_text_view_handle_drag_finished (GtkTextHandle *handle,
+ GtkTextHandlePosition pos,
+ GtkTextView *text_view)
+{
+ gtk_text_view_selection_bubble_popup_set (text_view);
+}
+
+static void
+gtk_text_view_update_handles (GtkTextView *text_view,
+ GtkTextHandleMode mode)
+{
+ GtkTextViewPrivate *priv = text_view->priv;
+ GtkTextIter cursor, bound, min, max;
+ GtkTextBuffer *buffer;
+
+ buffer = get_buffer (text_view);
+
+ gtk_text_buffer_get_iter_at_mark (buffer, &cursor,
+ gtk_text_buffer_get_insert (buffer));
+ gtk_text_buffer_get_iter_at_mark (buffer, &bound,
+ gtk_text_buffer_get_selection_bound (buffer));
+
+ if (mode == GTK_TEXT_HANDLE_MODE_SELECTION &&
+ gtk_text_iter_compare (&cursor, &bound) == 0)
+ {
+ mode = GTK_TEXT_HANDLE_MODE_CURSOR;
+ }
+
+ if (mode == GTK_TEXT_HANDLE_MODE_CURSOR &&
+ (!gtk_widget_is_sensitive (GTK_WIDGET (text_view)) || !priv->cursor_visible))
+ {
+ mode = GTK_TEXT_HANDLE_MODE_NONE;
+ }
+
+ _gtk_text_handle_set_mode (priv->text_handle, mode);
+
+ if (gtk_text_iter_compare (&cursor, &bound) >= 0)
+ {
+ min = bound;
+ max = cursor;
+ }
+ else
+ {
+ min = cursor;
+ max = bound;
+ }
+
+ if (mode != GTK_TEXT_HANDLE_MODE_NONE)
+ gtk_text_view_set_handle_position (text_view, &max,
+ GTK_TEXT_HANDLE_POSITION_SELECTION_END);
+
+ if (mode == GTK_TEXT_HANDLE_MODE_SELECTION)
+ gtk_text_view_set_handle_position (text_view, &min,
+ GTK_TEXT_HANDLE_POSITION_SELECTION_START);
+}
+