[gimp] app: new gimp_display_grab_focus() function.



commit 086ae77929de01b9d88970e703f5b06cf22fe61d
Author: Jehan <jehan girinstud io>
Date:   Mon Jul 5 15:24:12 2021 +0200

    app: new gimp_display_grab_focus() function.
    
    The goal of this function is to give the focus to the active image
    display. This is implemented as a core GimpDisplay virtual function
    (with the actual implementation in GimpDisplayImpl), allowing to be used
    even in core code, without actual GUI code (this was not necessary right
    now, but I think it will be useful in future use).
    
    This function is now called from the toolbox code (cf. 2 commits
    earlier), avoiding code duplication. I also added a usage at the end of
    toolbox_paste_received() so that a newly opened image by middle-click
    paste in the toolbox directly gains focus.

 app/core/gimpdisplay.c       | 11 +++++++++++
 app/core/gimpdisplay.h       |  6 ++++--
 app/display/gimpdisplay.c    | 22 ++++++++++++++++++++++
 app/widgets/gimptoolbox.c    | 36 ++++++++++++++++--------------------
 app/widgets/gimptoolbutton.c | 12 +-----------
 5 files changed, 54 insertions(+), 33 deletions(-)
---
diff --git a/app/core/gimpdisplay.c b/app/core/gimpdisplay.c
index 8f8da42a2c..2d82dbf80d 100644
--- a/app/core/gimpdisplay.c
+++ b/app/core/gimpdisplay.c
@@ -197,6 +197,17 @@ gimp_display_present (GimpDisplay *display)
   return FALSE;
 }
 
+gboolean
+gimp_display_grab_focus (GimpDisplay *display)
+{
+  g_return_val_if_fail (GIMP_IS_DISPLAY (display), FALSE);
+
+  if (GIMP_DISPLAY_GET_CLASS (display)->grab_focus)
+    return GIMP_DISPLAY_GET_CLASS (display)->grab_focus (display);
+
+  return FALSE;
+}
+
 Gimp *
 gimp_display_get_gimp (GimpDisplay *display)
 {
diff --git a/app/core/gimpdisplay.h b/app/core/gimpdisplay.h
index e40d8d65eb..56ccedd79a 100644
--- a/app/core/gimpdisplay.h
+++ b/app/core/gimpdisplay.h
@@ -47,7 +47,8 @@ struct _GimpDisplayClass
 {
   GimpObjectClass     parent_class;
 
-  gboolean         (* present) (GimpDisplay *display);
+  gboolean         (* present)    (GimpDisplay *display);
+  gboolean         (* grab_focus) (GimpDisplay *display);
 };
 
 
@@ -57,7 +58,8 @@ gint          gimp_display_get_id    (GimpDisplay *display);
 GimpDisplay * gimp_display_get_by_id (Gimp        *gimp,
                                       gint         id);
 
-gboolean      gimp_display_present   (GimpDisplay *display);
+gboolean      gimp_display_present    (GimpDisplay *display);
+gboolean      gimp_display_grab_focus (GimpDisplay *display);
 
 Gimp        * gimp_display_get_gimp  (GimpDisplay *display);
 
diff --git a/app/display/gimpdisplay.c b/app/display/gimpdisplay.c
index 1712999546..0232a564e3 100644
--- a/app/display/gimpdisplay.c
+++ b/app/display/gimpdisplay.c
@@ -94,6 +94,7 @@ static void     gimp_display_get_property           (GObject             *object
                                                      GParamSpec          *pspec);
 
 static gboolean gimp_display_impl_present           (GimpDisplay         *display);
+static gboolean gimp_display_impl_grab_focus        (GimpDisplay         *display);
 
 static GimpProgress * gimp_display_progress_start   (GimpProgress        *progress,
                                                      gboolean             cancellable,
@@ -141,6 +142,7 @@ gimp_display_impl_class_init (GimpDisplayImplClass *klass)
   object_class->get_property = gimp_display_get_property;
 
   display_class->present     = gimp_display_impl_present;
+  display_class->grab_focus  = gimp_display_impl_grab_focus;
 
   g_object_class_install_property (object_class, PROP_IMAGE,
                                    g_param_spec_object ("image",
@@ -221,6 +223,26 @@ gimp_display_impl_present (GimpDisplay *display)
   return TRUE;
 }
 
+static gboolean
+gimp_display_impl_grab_focus (GimpDisplay *display)
+{
+  GimpDisplayImpl *display_impl = GIMP_DISPLAY_IMPL (display);
+
+  if (display_impl->priv->shell && gimp_display_get_image (display))
+    {
+      GimpImageWindow *image_window;
+
+      image_window = gimp_display_shell_get_window (gimp_display_get_shell (display));
+
+      gimp_display_present (display);
+      gtk_widget_grab_focus (gimp_window_get_primary_focus_widget (GIMP_WINDOW (image_window)));
+
+      return TRUE;
+    }
+
+  return FALSE;
+}
+
 static GimpProgress *
 gimp_display_progress_start (GimpProgress *progress,
                              gboolean      cancellable,
diff --git a/app/widgets/gimptoolbox.c b/app/widgets/gimptoolbox.c
index 6cb27dbf83..a0e68c7292 100644
--- a/app/widgets/gimptoolbox.c
+++ b/app/widgets/gimptoolbox.c
@@ -33,10 +33,6 @@
 #include "core/gimpcontext.h"
 #include "core/gimpdisplay.h"
 
-#include "display/display-types.h"
-#include "display/gimpdisplay.h"
-#include "display/gimpdisplayshell.h"
-
 #include "file/file-open.h"
 
 #include "gimpcairo-wilber.h"
@@ -353,20 +349,7 @@ gimp_toolbox_button_press_event (GtkWidget      *widget,
 {
   GimpToolbox *toolbox = GIMP_TOOLBOX (widget);
   GimpDisplay *display;
-
-  if ((display = gimp_context_get_display (toolbox->p->context)) &&
-      gimp_context_get_image (toolbox->p->context))
-    {
-      /* Any button event in empty spaces or the Wilber area gives focus
-       * to the top image.
-       */
-      GimpImageWindow *image_window;
-
-      image_window = gimp_display_shell_get_window (gimp_display_get_shell (display));
-
-      gimp_display_present (display);
-      gtk_widget_grab_focus (gimp_window_get_primary_focus_widget (GIMP_WINDOW (image_window)));
-    }
+  gboolean     stop_event = GDK_EVENT_PROPAGATE;
 
   if (event->type == GDK_BUTTON_PRESS && event->button == 2)
     {
@@ -377,10 +360,17 @@ gimp_toolbox_button_press_event (GtkWidget      *widget,
                                   toolbox_paste_received,
                                   g_object_ref (toolbox));
 
-      return TRUE;
+      stop_event = GDK_EVENT_STOP;
+    }
+  else if ((display = gimp_context_get_display (toolbox->p->context)))
+    {
+      /* Any button event in empty spaces or the Wilber area gives focus
+       * to the top image.
+       */
+      gimp_display_grab_focus (display);
     }
 
-  return FALSE;
+  return stop_event;
 }
 
 static void
@@ -686,6 +676,7 @@ toolbox_paste_received (GtkClipboard *clipboard,
         {
           GtkWidget         *widget = GTK_WIDGET (toolbox);
           GimpImage         *image;
+          GimpDisplay       *display;
           GimpPDBStatusType  status;
           GError            *error = NULL;
 
@@ -701,6 +692,11 @@ toolbox_paste_received (GtkClipboard *clipboard,
                             gimp_file_get_utf8_name (file), error->message);
               g_clear_error (&error);
             }
+          else if ((display = gimp_context_get_display (context)))
+            {
+              /* Giving focus to newly opened image. */
+              gimp_display_grab_focus (display);
+            }
 
           g_object_unref (file);
         }
diff --git a/app/widgets/gimptoolbutton.c b/app/widgets/gimptoolbutton.c
index 815f3ef9e9..0322f7383a 100644
--- a/app/widgets/gimptoolbutton.c
+++ b/app/widgets/gimptoolbutton.c
@@ -39,10 +39,6 @@
 #include "core/gimptoolgroup.h"
 #include "core/gimptoolinfo.h"
 
-#include "display/display-types.h"
-#include "display/gimpdisplay.h"
-#include "display/gimpdisplayshell.h"
-
 #include "actions/tools-commands.h"
 
 #include "gimpaccellabel.h"
@@ -52,7 +48,6 @@
 #include "gimptoolbutton.h"
 #include "gimpuimanager.h"
 #include "gimpwidgets-utils.h"
-#include "gimpwindow.h"
 #include "gimpwindowstrategy.h"
 
 #include "gimp-intl.h"
@@ -616,12 +611,7 @@ gimp_tool_button_toggled (GtkToggleToolButton *toggle_tool_button)
       if ((display = gimp_context_get_display (context)) &&
           gimp_context_get_image (context))
         {
-          GimpImageWindow *image_window;
-
-          image_window = gimp_display_shell_get_window (gimp_display_get_shell (display));
-
-          gimp_display_present (display);
-          gtk_widget_grab_focus (gimp_window_get_primary_focus_widget (GIMP_WINDOW (image_window)));
+          gimp_display_grab_focus (display);
         }
     }
   else


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