[evince/wip/gpoo/gtk4-rebase-port: 46/96] libview: Port EvAnnotationWindow to gtk4




commit 182b3abfb5930404544089f229fe566d925bf0dc
Author: Qiu Wenbo <qiuwenbo kylinos com cn>
Date:   Wed Aug 11 12:24:45 2021 +0800

    libview: Port EvAnnotationWindow to gtk4

 libview/ev-annotation-window.c | 192 +++++++++++++++--------------------------
 libview/meson.build            |   2 +-
 2 files changed, 72 insertions(+), 122 deletions(-)
---
diff --git a/libview/ev-annotation-window.c b/libview/ev-annotation-window.c
index 92cd25fc8..ccf651401 100644
--- a/libview/ev-annotation-window.c
+++ b/libview/ev-annotation-window.c
@@ -35,6 +35,10 @@
 #include <gspell/gspell.h>
 #endif
 
+#ifdef GDK_WINDOWING_X11
+#include <gdk/x11/gdkx.h>
+#endif
+
 enum {
        PROP_0,
        PROP_ANNOTATION,
@@ -87,24 +91,6 @@ static guint signals[N_SIGNALS];
 
 G_DEFINE_TYPE (EvAnnotationWindow, ev_annotation_window, GTK_TYPE_WINDOW)
 
-/* Cut and paste from gtkwindow.c */
-static void
-send_focus_change (GtkWidget *widget,
-                  gboolean   in)
-{
-       GdkEvent *fevent = gdk_event_new (GDK_FOCUS_CHANGE);
-
-       fevent->focus_change.type = GDK_FOCUS_CHANGE;
-       fevent->focus_change.window = gtk_widget_get_window (widget);
-       fevent->focus_change.in = in;
-       if (fevent->focus_change.window)
-               g_object_ref (fevent->focus_change.window);
-
-       gtk_widget_send_focus_change (widget, fevent);
-
-       gdk_event_free (fevent);
-}
-
 static void
 ev_annotation_window_sync_contents (EvAnnotationWindow *window)
 {
@@ -133,12 +119,11 @@ ev_annotation_window_set_color (EvAnnotationWindow *window,
        css_data = g_strdup_printf ("button {border-color: %1$s; color: %2$s; -gtk-icon-shadow:0 0; 
box-shadow:0 0;}\n"
                                    "button:hover {background: lighter(%1$s); border-color: darker(%1$s);}\n"
                                    "button:active {background: darker(%1$s);}\n"
-                                   "evannotationwindow.background { color: %2$s; }\n"
-                                   "evannotationwindow.background:backdrop { color: alpha(%2$s, .75); }\n"
-                                   "evannotationwindow.background, button {background: %1$s}",
+                                   "evannotationwindow.background, button {background: %1$s;}"
+                                   "evannotationwindow {padding-left: 2px; padding-right: 2px;}",
                                    rgba_str, icon_color_str);
 
-       gtk_css_provider_load_from_data (css_provider, css_data, strlen (css_data), &error);
+       gtk_css_provider_load_from_data (css_provider, css_data, strlen (css_data));
        if (error != NULL)
                g_error ("%s", error->message);
 
@@ -223,7 +208,7 @@ ev_annotation_window_set_property (GObject      *object,
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        }
 }
-
+#if 0
 static gboolean
 ev_annotation_window_resize (EvAnnotationWindow *window,
                             GdkEventButton     *event,
@@ -265,6 +250,7 @@ ev_annotation_window_set_resize_cursor (GtkWidget          *widget,
                gdk_window_set_cursor (gdk_window, NULL);
        }
 }
+#endif
 
 static void
 text_view_state_flags_changed (GtkWidget     *widget,
@@ -272,8 +258,8 @@ text_view_state_flags_changed (GtkWidget     *widget,
 {
        GtkStateFlags current_flags = gtk_widget_get_state_flags (widget);
 
-       if (current_flags & GTK_STATE_FLAG_BACKDROP)
-               gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (widget), FALSE);
+       gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (widget),
+                       !(current_flags & GTK_STATE_FLAG_BACKDROP));
 }
 
 static void
@@ -283,25 +269,24 @@ ev_annotation_window_close (EvAnnotationWindow *window)
        g_signal_emit (window, signals[CLOSED], 0);
 }
 
-static gboolean
-ev_annotation_window_button_press_event (GtkWidget      *widget,
-                                        GdkEventButton *event)
+static void
+ev_annotation_window_button_press_event (GtkGestureClick       *self,
+                                        gint                    n_press,
+                                        gdouble                 x,
+                                        gdouble                 y,
+                                        gpointer                user_data)
 {
-       EvAnnotationWindow *window = EV_ANNOTATION_WINDOW (widget);
+       EvAnnotationWindow *window = EV_ANNOTATION_WINDOW (user_data);
+       GtkEventController *controller = GTK_EVENT_CONTROLLER (self);
+       GtkNative *native = gtk_widget_get_native (GTK_WIDGET (window));
+       GdkSurface *toplevel = gtk_native_get_surface (native);
+       GdkDevice *device = gtk_event_controller_get_current_event_device (controller);
+       guint32 timestamp = gtk_event_controller_get_current_event_time (controller);
 
-       if (event->type == GDK_BUTTON_PRESS && event->button == 1) {
-               window->in_move = TRUE;
-               window->x = event->x_root - event->x;
-               window->y = event->y_root - event->y;
-               gtk_window_begin_move_drag (GTK_WINDOW (widget),
-                                           event->button,
-                                           event->x_root,
-                                           event->y_root,
-                                           event->time);
-               return TRUE;
-       }
+       window->in_move = TRUE;
 
-       return FALSE;
+       gdk_toplevel_begin_move (GDK_TOPLEVEL (toplevel), device, GDK_BUTTON_PRIMARY,
+                       x, y, timestamp);
 }
 
 static void
@@ -310,11 +295,7 @@ ev_annotation_window_init (EvAnnotationWindow *window)
        GtkWidget    *vbox, *hbox;
        GtkWidget    *icon;
        GtkWidget    *swindow;
-       GtkWidget    *header;
-       GtkIconTheme *icon_theme;
-       GdkPixbuf    *pixbuf;
-
-       icon_theme = gtk_icon_theme_get_default ();
+       GtkEventController *controller;
 
        gtk_widget_set_can_focus (GTK_WIDGET (window), TRUE);
 
@@ -323,36 +304,32 @@ ev_annotation_window_init (EvAnnotationWindow *window)
        /* Title bar */
        hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
 
-       icon = gtk_image_new (); /* FIXME: use the annot icon */
-       gtk_box_pack_start (GTK_BOX (hbox), icon, FALSE, FALSE, 0);
-       gtk_widget_show (icon);
+       icon = gtk_image_new_from_icon_name ("document-edit-symbolic");
+       gtk_box_prepend (GTK_BOX (hbox), icon);
+
+       window->title = gtk_label_new (NULL);
+       gtk_widget_set_halign (window->title, GTK_ALIGN_FILL);
+       gtk_widget_set_hexpand (window->title, TRUE);
 
-       header = gtk_event_box_new ();
-       gtk_widget_add_events (header, GDK_BUTTON_PRESS_MASK);
-       g_signal_connect_swapped (header, "button-press-event",
+       controller = GTK_EVENT_CONTROLLER (gtk_gesture_click_new ());
+       g_signal_connect (controller, "pressed",
                                  G_CALLBACK (ev_annotation_window_button_press_event),
                                  window);
+       gtk_widget_add_controller (window->title, controller);
 
-       window->title = gtk_label_new (NULL);
-       gtk_container_add (GTK_CONTAINER (header), window->title);
-       gtk_widget_show (window->title);
-
-       gtk_box_pack_start (GTK_BOX (hbox), header, TRUE, TRUE, 0);
-       gtk_widget_show (header);
+       gtk_box_append (GTK_BOX (hbox), window->title);
 
-       window->close_button = gtk_button_new_from_icon_name ("window-close-symbolic", GTK_ICON_SIZE_BUTTON);
+       window->close_button = gtk_button_new_from_icon_name ("window-close-symbolic");
        g_signal_connect_swapped (window->close_button, "clicked",
                                  G_CALLBACK (ev_annotation_window_close),
                                  window);
 
-       gtk_box_pack_start (GTK_BOX (hbox), window->close_button, FALSE, FALSE, 0);
-       gtk_widget_show (window->close_button);
+       gtk_box_append (GTK_BOX (hbox), window->close_button);
 
-       gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
-       gtk_widget_show (hbox);
+       gtk_window_set_titlebar (GTK_WINDOW (window), hbox);
 
        /* Contents */
-       swindow = gtk_scrolled_window_new (NULL, NULL);
+       swindow = gtk_scrolled_window_new ();
        window->text_view = gtk_text_view_new ();
 
 #if WITH_GSPELL
@@ -361,71 +338,45 @@ ev_annotation_window_init (EvAnnotationWindow *window)
        gspell_text_view_basic_setup (window->spellcheck_view);
 #endif
 
-       gtk_container_set_border_width (GTK_CONTAINER (window->text_view), 6);
+       // gtk_container_set_border_width (GTK_CONTAINER (window->text_view), 6);
        gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (window->text_view), GTK_WRAP_WORD);
        g_signal_connect (window->text_view, "state-flags-changed",
                          G_CALLBACK (text_view_state_flags_changed),
                          window);
-       gtk_container_add (GTK_CONTAINER (swindow), window->text_view);
-       gtk_widget_show (window->text_view);
+       gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (swindow), window->text_view);
+       gtk_widget_set_valign (swindow, GTK_ALIGN_FILL);
+       gtk_widget_set_vexpand (swindow, TRUE);
 
-       gtk_box_pack_start (GTK_BOX (vbox), swindow, TRUE, TRUE, 0);
-       gtk_widget_show (swindow);
+       gtk_box_append (GTK_BOX (vbox), swindow);
 
        /* Resize bar */
        hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
 
-       window->resize_sw = gtk_event_box_new ();
-       gtk_widget_add_events (window->resize_sw, GDK_BUTTON_PRESS_MASK);
-       g_signal_connect_swapped (window->resize_sw, "button-press-event",
-                                 G_CALLBACK (ev_annotation_window_resize),
-                                 window);
-       g_signal_connect (window->resize_sw, "realize",
-                         G_CALLBACK (ev_annotation_window_set_resize_cursor),
-                         window);
-
-       pixbuf = gtk_icon_theme_load_icon (icon_theme, EV_STOCK_RESIZE_SW, 8,
-                                          GTK_ICON_LOOKUP_FORCE_SIZE, NULL);
-       icon = gtk_image_new_from_pixbuf (pixbuf);
-       g_object_unref (pixbuf);
-       gtk_container_add (GTK_CONTAINER (window->resize_sw), icon);
-       gtk_widget_show (icon);
-       gtk_box_pack_start (GTK_BOX (hbox), window->resize_sw, FALSE, FALSE, 0);
-       gtk_widget_show (window->resize_sw);
-
-       window->resize_se = gtk_event_box_new ();
-       gtk_widget_add_events (window->resize_se, GDK_BUTTON_PRESS_MASK);
-       g_signal_connect_swapped (window->resize_se, "button-press-event",
-                                 G_CALLBACK (ev_annotation_window_resize),
-                                 window);
-       g_signal_connect (window->resize_se, "realize",
-                         G_CALLBACK (ev_annotation_window_set_resize_cursor),
-                         window);
-
-       pixbuf = gtk_icon_theme_load_icon (icon_theme, EV_STOCK_RESIZE_SE, 8,
-                                          GTK_ICON_LOOKUP_FORCE_SIZE, NULL);
-       icon = gtk_image_new_from_pixbuf (pixbuf);
-       g_object_unref (pixbuf);
-       gtk_container_add (GTK_CONTAINER (window->resize_se), icon);
-       gtk_widget_show (icon);
-       gtk_box_pack_end (GTK_BOX (hbox), window->resize_se, FALSE, FALSE, 0);
-       gtk_widget_show (window->resize_se);
+       icon = gtk_image_new_from_icon_name (EV_STOCK_RESIZE_SW);
+       gtk_image_set_pixel_size (GTK_IMAGE (icon), 8);
+       gtk_box_prepend (GTK_BOX (hbox), icon);
+       icon = gtk_image_new_from_icon_name (EV_STOCK_RESIZE_SE);
+       gtk_image_set_pixel_size (GTK_IMAGE (icon), 8);
+       gtk_widget_set_hexpand (icon, TRUE);
+       gtk_widget_set_halign (icon, GTK_ALIGN_END);
+       gtk_box_append (GTK_BOX (hbox), icon);
 
-       gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
-       gtk_widget_show (hbox);
+       gtk_box_append (GTK_BOX (vbox), hbox);
 
-       gtk_container_add (GTK_CONTAINER (window), vbox);
-       gtk_widget_show (vbox);
+       gtk_window_set_child (GTK_WINDOW (window), vbox);
 
-       gtk_widget_add_events (GTK_WIDGET (window),
-                              GDK_BUTTON_PRESS_MASK |
-                              GDK_KEY_PRESS_MASK);
+#ifdef GDK_WINDOWING_X11
+       {
+               GtkNative *native = gtk_widget_get_native (GTK_WIDGET (window));
+               GdkSurface *surface = gtk_native_get_surface (native);
 
-       gtk_container_set_border_width (GTK_CONTAINER (window), 2);
+               if (GDK_IS_X11_SURFACE (surface)) {
+                       gdk_x11_surface_set_skip_taskbar_hint (GDK_X11_SURFACE (surface), TRUE);
+                       gdk_x11_surface_set_skip_pager_hint (GDK_X11_SURFACE (surface), TRUE);
+               }
+       }
+#endif
 
-       gtk_window_set_decorated (GTK_WINDOW (window), FALSE);
-       gtk_window_set_skip_taskbar_hint (GTK_WINDOW (window), TRUE);
-       gtk_window_set_skip_pager_hint (GTK_WINDOW (window), TRUE);
        gtk_window_set_resizable (GTK_WINDOW (window), TRUE);
 }
 
@@ -463,7 +414,7 @@ ev_annotation_window_constructor (GType                  type,
 
        /* Rectangle is at doc resolution (72.0) */
        scale = ev_document_misc_get_widget_dpi (GTK_WIDGET (window)) / 72.0;
-       gtk_window_resize (GTK_WINDOW (window),
+       gtk_window_set_default_size (GTK_WINDOW (window),
                           (gint)((rect->x2 - rect->x1) * scale),
                           (gint)((rect->y2 - rect->y1) * scale));
 
@@ -501,6 +452,7 @@ ev_annotation_window_constructor (GType                  type,
        return object;
 }
 
+#if 0
 static gboolean
 ev_annotation_window_configure_event (GtkWidget         *widget,
                                      GdkEventConfigure *event)
@@ -560,6 +512,7 @@ ev_annotation_window_key_press_event (GtkWidget   *widget,
 
         return GTK_WIDGET_CLASS (ev_annotation_window_parent_class)->key_press_event (widget, event);
 }
+#endif
 
 static void
 ev_annotation_window_class_init (EvAnnotationWindowClass *klass)
@@ -571,10 +524,12 @@ ev_annotation_window_class_init (EvAnnotationWindowClass *klass)
        g_object_class->set_property = ev_annotation_window_set_property;
        g_object_class->dispose = ev_annotation_window_dispose;
 
+#if 0
        gtk_widget_class->configure_event = ev_annotation_window_configure_event;
        gtk_widget_class->focus_in_event = ev_annotation_window_focus_in_event;
        gtk_widget_class->focus_out_event = ev_annotation_window_focus_out_event;
         gtk_widget_class->key_press_event = ev_annotation_window_key_press_event;
+#endif
 
        gtk_widget_class_set_css_name (gtk_widget_class, "evannotationwindow");
        g_object_class_install_property (g_object_class,
@@ -690,7 +645,6 @@ ev_annotation_window_grab_focus (EvAnnotationWindow *window)
 
        if (!gtk_widget_has_focus (window->text_view)) {
                gtk_widget_grab_focus (GTK_WIDGET (window));
-               send_focus_change (window->text_view, TRUE);
        }
 }
 
@@ -699,10 +653,6 @@ ev_annotation_window_ungrab_focus (EvAnnotationWindow *window)
 {
        g_return_if_fail (EV_IS_ANNOTATION_WINDOW (window));
 
-       if (gtk_widget_has_focus (window->text_view)) {
-               send_focus_change (window->text_view, FALSE);
-       }
-
        ev_annotation_window_sync_contents (window);
 }
 
diff --git a/libview/meson.build b/libview/meson.build
index 3e314de45..838cb739a 100644
--- a/libview/meson.build
+++ b/libview/meson.build
@@ -18,7 +18,7 @@ install_headers(
 )
 
 sources = files(
-  #'ev-annotation-window.c',
+  'ev-annotation-window.c',
   'ev-color-contrast.c',
   'ev-document-model.c',
   #'ev-form-field-accessible.c',


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