]> www.fi.muni.cz Git - evince.git/blobdiff - shell/ev-view.c
Added Kurdish (ku) translation
[evince.git] / shell / ev-view.c
index 4b684190963623e21f591b93d92082466cb37f4e..6f87b6a63cb176e2c3c1d8d29de9c8dba77769fa 100644 (file)
@@ -19,6 +19,7 @@
  */
 
 #include <math.h>
  */
 
 #include <math.h>
+#include <string.h>
 #include <gtk/gtkalignment.h>
 #include <glib/gi18n.h>
 #include <gtk/gtkbindings.h>
 #include <gtk/gtkalignment.h>
 #include <glib/gi18n.h>
 #include <gtk/gtkbindings.h>
@@ -55,6 +56,7 @@ enum {
        PROP_SIZING_MODE,
        PROP_ZOOM,
        PROP_ROTATION,
        PROP_SIZING_MODE,
        PROP_ZOOM,
        PROP_ROTATION,
+       PROP_HAS_SELECTION,
 };
 
 enum {
 };
 
 enum {
@@ -112,6 +114,7 @@ typedef struct {
 /* Information for handling selection */
 typedef struct {
        gboolean in_selection;
 /* Information for handling selection */
 typedef struct {
        gboolean in_selection;
+       gboolean in_drag;
        GdkPoint start;
        GList *selections;
 } SelectionInfo;
        GdkPoint start;
        GList *selections;
 } SelectionInfo;
@@ -1097,6 +1100,32 @@ ev_view_get_height (EvView *view)
        return GTK_WIDGET (view)->allocation.height;
 }
 
        return GTK_WIDGET (view)->allocation.height;
 }
 
+static gboolean
+location_in_selected_text (EvView  *view,
+                          gdouble  x,
+                          gdouble  y)
+{
+       gint page = -1;
+       gint x_offset = 0, y_offset = 0;
+       EvViewSelection *selection;
+       GList *l = NULL;
+
+       for (l = view->selection_info.selections; l != NULL; l = l->next) {
+               selection = (EvViewSelection *)l->data;
+               
+               find_page_at_location (view, x, y, &page, &x_offset, &y_offset);
+               
+               if (page != selection->page)
+                       continue;
+               
+               if (selection->covered_region &&
+                   gdk_region_point_in (selection->covered_region, x_offset, y_offset))
+                       return TRUE;
+       }
+
+       return FALSE;
+}
+
 /*** Hyperref ***/
 static EvLink *
 get_link_at_location (EvView  *view,
 /*** Hyperref ***/
 static EvLink *
 get_link_at_location (EvView  *view,
@@ -1336,9 +1365,13 @@ handle_link_over_xy (EvView *view, gint x, gint y)
         if (link) {
                char *msg = tip_from_link (view, link);
 
         if (link) {
                char *msg = tip_from_link (view, link);
 
-               ev_tooltip_set_position (EV_TOOLTIP (view->link_tooltip), x, y);
-               ev_tooltip_set_text (EV_TOOLTIP (view->link_tooltip), msg);
-               ev_tooltip_activate (EV_TOOLTIP (view->link_tooltip));
+               if (msg && g_utf8_validate (msg, -1, NULL)) {
+                       EvTooltip *tooltip = EV_TOOLTIP (view->link_tooltip);
+
+                       ev_tooltip_set_position (tooltip, x, y);
+                       ev_tooltip_set_text (tooltip, msg);
+                       ev_tooltip_activate (tooltip);
+               }
                g_free (msg);
 
                ev_view_set_cursor (view, EV_VIEW_CURSOR_LINK);
                g_free (msg);
 
                ev_view_set_cursor (view, EV_VIEW_CURSOR_LINK);
@@ -1680,22 +1713,31 @@ ev_view_button_press_event (GtkWidget      *widget,
                            GdkEventButton *event)
 {
        EvView *view = EV_VIEW (widget);
                            GdkEventButton *event)
 {
        EvView *view = EV_VIEW (widget);
-
+       
        if (!GTK_WIDGET_HAS_FOCUS (widget)) {
                gtk_widget_grab_focus (widget);
        }
        if (!GTK_WIDGET_HAS_FOCUS (widget)) {
                gtk_widget_grab_focus (widget);
        }
-
+       
        view->pressed_button = event->button;
        view->pressed_button = event->button;
-
+       view->selection_info.in_drag = FALSE;
+       
        switch (event->button) {
        switch (event->button) {
-               case 1:
+               case 1:
                        if (view->selection_info.selections) {
                        if (view->selection_info.selections) {
-                               clear_selection (view);
+                               if (location_in_selected_text (view,
+                                                              event->x + view->scroll_x,
+                                                              event->y + view->scroll_y)) {
+                                       view->selection_info.in_drag = TRUE;
+                               } else {
+                                       clear_selection (view);
+                               }
+                               
                                gtk_widget_queue_draw (widget);
                        }
 
                        view->selection_info.start.x = event->x + view->scroll_x;
                        view->selection_info.start.y = event->y + view->scroll_y;
                                gtk_widget_queue_draw (widget);
                        }
 
                        view->selection_info.start.x = event->x + view->scroll_x;
                        view->selection_info.start.y = event->y + view->scroll_y;
+                       
                        return TRUE;
                case 2:
                        /* use root coordinates as reference point because
                        return TRUE;
                case 2:
                        /* use root coordinates as reference point because
@@ -1709,10 +1751,30 @@ ev_view_button_press_event (GtkWidget      *widget,
 
                        return TRUE;
        }
 
                        return TRUE;
        }
-
+       
        return FALSE;
 }
 
        return FALSE;
 }
 
+static void
+ev_view_drag_data_get (GtkWidget        *widget,
+                      GdkDragContext   *context,
+                      GtkSelectionData *selection_data,
+                      guint             info,
+                      guint             time)
+{
+       EvView *view = EV_VIEW (widget);
+
+       if (view->selection_info.selections &&
+           ev_document_can_get_text (view->document)) {
+               gchar *text;
+
+               text = get_selected_text (view);
+
+               gtk_selection_data_set_text (selection_data, text, strlen (text));
+
+               g_free (text);
+       }
+}
 
 static gboolean
 selection_update_idle_cb (EvView *view)
 
 static gboolean
 selection_update_idle_cb (EvView *view)
@@ -1735,19 +1797,40 @@ ev_view_motion_notify_event (GtkWidget      *widget,
        if (!view->document)
                return FALSE;
 
        if (!view->document)
                return FALSE;
 
+       if (view->selection_info.in_drag) {
+               if (gtk_drag_check_threshold (widget,
+                                             view->selection_info.start.x,
+                                             view->selection_info.start.y,
+                                             event->x, event->y)) {
+                       GdkDragContext *context;
+                       GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
+
+                       gtk_target_list_add_text_targets (target_list, 0);
+
+                       context = gtk_drag_begin (widget, target_list,
+                                                 GDK_ACTION_COPY,
+                                                 1, (GdkEvent *)event);
+
+                       view->selection_info.in_drag = FALSE;
+
+                       gtk_target_list_unref (target_list);
+
+                       return TRUE;
+               }
+       }
+       
        /* For the Evince 0.4.x release, we limit selection to un-rotated
         * documents only.
         */
        /* For the Evince 0.4.x release, we limit selection to un-rotated
         * documents only.
         */
-       if (view->pressed_button == 1 &&
-           view->rotation == 0) {
+       if (view->pressed_button == 1 && view->rotation == 0) {
                view->selection_info.in_selection = TRUE;
                view->motion_x = event->x + view->scroll_x;
                view->motion_y = event->y + view->scroll_y;
 
                view->selection_info.in_selection = TRUE;
                view->motion_x = event->x + view->scroll_x;
                view->motion_y = event->y + view->scroll_y;
 
-               /* Queue an idle to handle the motion.  We do this because
-                * handling any selection events in the motion could be slower
-                * than new motion events reach us.  We always put it in the
-                * idle to make sure we catch up and don't visibly lag the
+               /* Queue an idle to handle the motion.  We do this because      
+                * handling any selection events in the motion could be slower  
+                * than new motion events reach us.  We always put it in the    
+                * idle to make sure we catch up and don't visibly lag the      
                 * mouse. */
                if (! view->selection_update_id)
                        view->selection_update_id = g_idle_add ((GSourceFunc)selection_update_idle_cb, view);
                 * mouse. */
                if (! view->selection_update_id)
                        view->selection_update_id = g_idle_add ((GSourceFunc)selection_update_idle_cb, view);
@@ -1824,6 +1907,13 @@ ev_view_button_release_event (GtkWidget      *widget,
 
        if (view->selection_info.selections) {
                ev_view_update_primary_selection (view);
 
        if (view->selection_info.selections) {
                ev_view_update_primary_selection (view);
+               
+               if (view->selection_info.in_drag) {
+                       clear_selection (view);
+                       gtk_widget_queue_draw (widget);
+               }
+               
+               view->selection_info.in_drag = FALSE;
        } else if (link) {
                ev_view_goto_link (view, link);
        } else if (view->presentation) {
        } else if (link) {
                ev_view_goto_link (view, link);
        } else if (view->presentation) {
@@ -2244,6 +2334,10 @@ ev_view_get_property (GObject *object,
        case PROP_ROTATION:
                g_value_set_int (value, view->rotation);
                break;
        case PROP_ROTATION:
                g_value_set_int (value, view->rotation);
                break;
+       case PROP_HAS_SELECTION:
+               g_value_set_boolean (value,
+                                    view->selection_info.selections != NULL);
+               break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        }
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        }
@@ -2274,6 +2368,7 @@ ev_view_class_init (EvViewClass *class)
        widget_class->enter_notify_event = ev_view_enter_notify_event;
        widget_class->leave_notify_event = ev_view_leave_notify_event;
        widget_class->style_set = ev_view_style_set;
        widget_class->enter_notify_event = ev_view_enter_notify_event;
        widget_class->leave_notify_event = ev_view_leave_notify_event;
        widget_class->style_set = ev_view_style_set;
+       widget_class->drag_data_get = ev_view_drag_data_get;
        gtk_object_class->destroy = ev_view_destroy;
 
        class->set_scroll_adjustments = ev_view_set_scroll_adjustments;
        gtk_object_class->destroy = ev_view_destroy;
 
        class->set_scroll_adjustments = ev_view_set_scroll_adjustments;
@@ -2389,6 +2484,13 @@ ev_view_class_init (EvViewClass *class)
                                                               360,
                                                               0,
                                                               G_PARAM_READWRITE));
                                                               360,
                                                               0,
                                                               G_PARAM_READWRITE));
+       g_object_class_install_property (object_class,
+                                        PROP_HAS_SELECTION,
+                                        g_param_spec_boolean ("has-selection",
+                                                              "Has selection",
+                                                              "The view has selections",
+                                                              FALSE,
+                                                              G_PARAM_READABLE));
 
        binding_set = gtk_binding_set_by_class (class);
 
 
        binding_set = gtk_binding_set_by_class (class);
 
@@ -2409,7 +2511,9 @@ ev_view_init (EvView *view)
        view->pressed_button = -1;
        view->cursor = EV_VIEW_CURSOR_NORMAL;
        view->drag_info.in_drag = FALSE;
        view->pressed_button = -1;
        view->cursor = EV_VIEW_CURSOR_NORMAL;
        view->drag_info.in_drag = FALSE;
+       view->selection_info.selections = NULL;
        view->selection_info.in_selection = FALSE;
        view->selection_info.in_selection = FALSE;
+       view->selection_info.in_drag = FALSE;
        view->selection_mode = EV_VIEW_SELECTION_TEXT;
        view->continuous = TRUE;
        view->dual_page = FALSE;
        view->selection_mode = EV_VIEW_SELECTION_TEXT;
        view->continuous = TRUE;
        view->dual_page = FALSE;
@@ -3441,6 +3545,7 @@ merge_selection_region (EvView *view,
        g_list_foreach (view->selection_info.selections, (GFunc)selection_free, NULL);
        view->selection_info.selections = new_list;
        ev_pixbuf_cache_set_selection_list (view->pixbuf_cache, new_list);
        g_list_foreach (view->selection_info.selections, (GFunc)selection_free, NULL);
        view->selection_info.selections = new_list;
        ev_pixbuf_cache_set_selection_list (view->pixbuf_cache, new_list);
+       g_object_notify (G_OBJECT (view), "has-selection");
 
        new_list_ptr = new_list;
        old_list_ptr = old_list;
 
        new_list_ptr = new_list;
        old_list_ptr = old_list;
@@ -3571,6 +3676,7 @@ clear_selection (EvView *view)
        g_list_foreach (view->selection_info.selections, (GFunc)selection_free, NULL);
        view->selection_info.selections = NULL;
        view->selection_info.in_selection = FALSE;
        g_list_foreach (view->selection_info.selections, (GFunc)selection_free, NULL);
        view->selection_info.selections = NULL;
        view->selection_info.in_selection = FALSE;
+       g_object_notify (G_OBJECT (view), "has-selection");
 }
 
 
 }
 
 
@@ -3605,9 +3711,16 @@ ev_view_select_all (EvView *view)
        }
 
        ev_pixbuf_cache_set_selection_list (view->pixbuf_cache, view->selection_info.selections);
        }
 
        ev_pixbuf_cache_set_selection_list (view->pixbuf_cache, view->selection_info.selections);
+       g_object_notify (G_OBJECT (view), "has-selection");
        gtk_widget_queue_draw (GTK_WIDGET (view));
 }
 
        gtk_widget_queue_draw (GTK_WIDGET (view));
 }
 
+gboolean
+ev_view_get_has_selection (EvView *view)
+{
+       return view->selection_info.selections != NULL;
+}
+
 static char *
 get_selected_text (EvView *ev_view)
 {
 static char *
 get_selected_text (EvView *ev_view)
 {