[gtkmm] Add Gtk::ShortcutController and ShortcutManager



commit e47b71e93addb0c193e9f75a7f17fef004683f91
Author: Kjell Ahlstedt <kjellahlstedt gmail com>
Date:   Sun Jun 7 10:06:00 2020 +0200

    Add Gtk::ShortcutController and ShortcutManager
    
    These classes are probably not very useful without C++ bindings of
    GtkShortcut, GtkShortcutAction and GtkShortcutTrigger.
    I add them now because ShortcutManager is an interface, implemented by
    Window and Popover. New classes can be added in the future. An implemented
    interface can't be added to an existing class after the first stable
    version of gtkmm4 has been released.

 .gitignore                     |   4 ++
 gtk/gtkmm.h                    |   1 +
 gtk/gtkmm/meson.build          |   2 +
 gtk/src/enums.hg               |   1 +
 gtk/src/filelist.am            |   4 +-
 gtk/src/gtk_docs_override.xml  |   1 +
 gtk/src/gtk_extra_objects.defs |   6 +++
 gtk/src/gtk_vfuncs.defs        |  18 +++++++
 gtk/src/layoutmanager.hg       |   2 +-
 gtk/src/popover.hg             |   6 +--
 gtk/src/root.hg                |   1 -
 gtk/src/shortcutcontroller.ccg |  30 +++++++++++
 gtk/src/shortcutcontroller.hg  | 114 +++++++++++++++++++++++++++++++++++++++++
 gtk/src/shortcutmanager.ccg    |  18 +++++++
 gtk/src/shortcutmanager.hg     |  60 ++++++++++++++++++++++
 gtk/src/window.hg              |  13 +++--
 tools/m4/convert_gtk.m4        |   2 +
 17 files changed, 270 insertions(+), 13 deletions(-)
---
diff --git a/.gitignore b/.gitignore
index 86f17a08..9a08d553 100644
--- a/.gitignore
+++ b/.gitignore
@@ -467,8 +467,12 @@ gtk/gtkmm/separator.cc
 gtk/gtkmm/separator.h
 gtk/gtkmm/settings.cc
 gtk/gtkmm/settings.h
+gtk/gtkmm/shortcutcontroller.cc
+gtk/gtkmm/shortcutcontroller.h
 gtk/gtkmm/shortcutlabel.cc
 gtk/gtkmm/shortcutlabel.h
+gtk/gtkmm/shortcutmanager.cc
+gtk/gtkmm/shortcutmanager.h
 gtk/gtkmm/shortcutsgroup.cc
 gtk/gtkmm/shortcutsgroup.h
 gtk/gtkmm/shortcutssection.cc
diff --git a/gtk/gtkmm.h b/gtk/gtkmm.h
index c4077a65..4fff12c1 100644
--- a/gtk/gtkmm.h
+++ b/gtk/gtkmm.h
@@ -239,6 +239,7 @@ extern const int gtkmm_micro_version;
 #include <gtkmm/searchentry.h>
 #include <gtkmm/separator.h>
 #include <gtkmm/settings.h>
+#include <gtkmm/shortcutcontroller.h>
 #include <gtkmm/shortcutlabel.h>
 #include <gtkmm/shortcutsgroup.h>
 #include <gtkmm/shortcutssection.h>
diff --git a/gtk/gtkmm/meson.build b/gtk/gtkmm/meson.build
index ac5461c4..9d7dcb9a 100644
--- a/gtk/gtkmm/meson.build
+++ b/gtk/gtkmm/meson.build
@@ -185,7 +185,9 @@ gtkmm_any_hg_ccg_basenames = [
   'selectionmodel',
   'separator',
   'settings',
+  'shortcutcontroller',
   'shortcutlabel',
+  'shortcutmanager',
   'shortcutsgroup',
   'shortcutssection',
   'shortcutsshortcut',
diff --git a/gtk/src/enums.hg b/gtk/src/enums.hg
index 5dc71170..0f5f67dc 100644
--- a/gtk/src/enums.hg
+++ b/gtk/src/enums.hg
@@ -57,6 +57,7 @@ _WRAP_ENUM(StateFlags, GtkStateFlags)
 _WRAP_ENUM(InputPurpose, GtkInputPurpose)
 _WRAP_ENUM(InputHints, GtkInputHints)
 _WRAP_ENUM(BaselinePosition, GtkBaselinePosition)
+_WRAP_ENUM(ShortcutScope, GtkShortcutScope)
 
 #ifndef DOXYGEN_SHOULD_SKIP_THIS
 //We need this because we can't just use floats for enum value.
diff --git a/gtk/src/filelist.am b/gtk/src/filelist.am
index e3b7875d..52cd0888 100644
--- a/gtk/src/filelist.am
+++ b/gtk/src/filelist.am
@@ -170,7 +170,9 @@ gtkmm_files_any_hg =                \
        selectionmodel.hg \
        separator.hg            \
        settings.hg             \
-       shortcutlabel.hg                \
+       shortcutcontroller.hg \
+       shortcutlabel.hg \
+       shortcutmanager.hg \
        shortcutsgroup.hg               \
        shortcutssection.hg             \
        shortcutsshortcut.hg            \
diff --git a/gtk/src/gtk_docs_override.xml b/gtk/src/gtk_docs_override.xml
index 755fc29b..e43d4c52 100644
--- a/gtk/src/gtk_docs_override.xml
+++ b/gtk/src/gtk_docs_override.xml
@@ -134,6 +134,7 @@
 <substitute_enumerator_name from="G_CONNECT_AFTER" to="connect(..., true)" />
 <!-- Some enums have enumerators without unique prefixes. Handle these enumerators individually. -->
 <substitute_enumerator_name from="GDK_CONTROL_MASK" to="Gdk::ModifierType::CONTROL_MASK" />
+<substitute_enumerator_name from="GDK_ALT_MASK" to="Gdk::ModifierType::ALT_MASK" />
 <substitute_enumerator_name from="GDK_HYPER_MASK" to="Gdk::ModifierType::HYPER_MASK" />
 <substitute_enumerator_name from="GDK_META_MASK" to="Gdk::ModifierType::META_MASK" />
 <substitute_enumerator_name from="GDK_MOD1_MASK" to="Gdk::ModifierType::MOD1_MASK" />
diff --git a/gtk/src/gtk_extra_objects.defs b/gtk/src/gtk_extra_objects.defs
index a109c8f3..79bda648 100644
--- a/gtk/src/gtk_extra_objects.defs
+++ b/gtk/src/gtk_extra_objects.defs
@@ -472,6 +472,12 @@
   (gtype-id "GTK_TYPE_SIZE_GROUP")
 )
 
+(define-object ShortcutController
+  (in-module "Gtk")
+  (c-name "GtkShortcutController")
+  (gtype-id "GTK_TYPE_SHORTCUT_CONTROLLER")
+)
+
 (define-object Snapshot
   (in-module "Gtk")
   (c-name "GtkSnapshot")
diff --git a/gtk/src/gtk_vfuncs.defs b/gtk/src/gtk_vfuncs.defs
index ab04e4c0..dcc8b393 100644
--- a/gtk/src/gtk_vfuncs.defs
+++ b/gtk/src/gtk_vfuncs.defs
@@ -547,6 +547,24 @@
   )
 )
 
+; GtkShortcutManager
+
+(define-vfunc add_controller
+  (of-object "GtkShortcutManager")
+  (return-type "void")
+  (parameters
+    '("GtkShortcutController*" "controller")
+  )
+)
+
+(define-vfunc remove_controller
+  (of-object "GtkShortcutManager")
+  (return-type "void")
+  (parameters
+    '("GtkShortcutController*" "controller")
+  )
+)
+
 ; GtkToolShell
 
 (define-vfunc get_orientation
diff --git a/gtk/src/layoutmanager.hg b/gtk/src/layoutmanager.hg
index 59abc88d..a768219f 100644
--- a/gtk/src/layoutmanager.hg
+++ b/gtk/src/layoutmanager.hg
@@ -39,7 +39,7 @@ class Widget;
  * at any given time; it is possible, though, to replace the layout manager
  * instance using Gtk::Widget::set_layout_manager().
  *
- * ## %Layout properties
+ * ## Layout properties
  *
  * A layout manager can expose properties for controlling the layout of
  * each child, by creating an object type derived from Gtk::LayoutChild
diff --git a/gtk/src/popover.hg b/gtk/src/popover.hg
index 277d67e2..13d0d41d 100644
--- a/gtk/src/popover.hg
+++ b/gtk/src/popover.hg
@@ -18,6 +18,7 @@
 
 #include <gtkmm/widget.h>
 #include <gtkmm/native.h>
+#include <gtkmm/shortcutmanager.h>
 
 _DEFS(gtkmm,gtk)
 _PINCLUDE(gtkmm/private/widget_p.h)
@@ -46,12 +47,11 @@ namespace Gtk
  * @ingroup Widgets
  * @newin{3,12}
  */
-class Popover : public Widget, public Native
+class Popover : public Widget, public ShortcutManager, public Native
 {
   _CLASS_GTKOBJECT(Popover, GtkPopover, GTK_POPOVER, Gtk::Widget, GtkWidget)
+  _IMPLEMENTS_INTERFACE(ShortcutManager)
   _IMPLEMENTS_INTERFACE(Native)
-  // GtkPopover also implements GtkShortcutManager, but that interface is not
-  // (yet) wrapped in a C++ class.
 public:
   _WRAP_ENUM(Constraint, GtkPopoverConstraint)
 
diff --git a/gtk/src/root.hg b/gtk/src/root.hg
index ef5318b8..095ee128 100644
--- a/gtk/src/root.hg
+++ b/gtk/src/root.hg
@@ -18,7 +18,6 @@
 
 _DEFS(gtkmm,gtk)
 _PINCLUDE(glibmm/private/interface_p.h)
-_PINCLUDE(gdk/gdk.h)
 
 #ifndef DOXYGEN_SHOULD_SKIP_THIS
 extern "C"
diff --git a/gtk/src/shortcutcontroller.ccg b/gtk/src/shortcutcontroller.ccg
new file mode 100644
index 00000000..606da665
--- /dev/null
+++ b/gtk/src/shortcutcontroller.ccg
@@ -0,0 +1,30 @@
+/* Copyright (C) 2020 The gtkmm Development Team
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <gtk/gtk.h>
+
+namespace Gtk
+{
+#if 0 // TODO: Activate when there is a Gtk::Shortcut class.
+void ShortcutController::add_shortcut(const Glib::RefPtr<Shortcut>& shortcut)
+{
+  // gtk_shortcut_controller_add_shortcut() does not take a ref (transfer full).
+  if (shortcut)
+    shortcut->reference();
+  gtk_shortcut_controller_add_shortcut(gobj(), Glib::unwrap(shortcut));
+}
+#endif
+} // namespace Gtk
diff --git a/gtk/src/shortcutcontroller.hg b/gtk/src/shortcutcontroller.hg
new file mode 100644
index 00000000..3e4104d2
--- /dev/null
+++ b/gtk/src/shortcutcontroller.hg
@@ -0,0 +1,114 @@
+/* Copyright (C) 2020 The gtkmm Development Team
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <gtkmm/eventcontroller.h>
+#include <gtkmm/buildable.h>
+#include <giomm/listmodel.h>
+#include <gtkmm/enums.h>
+#include <gdkmm/types.h>
+
+_DEFS(gtkmm,gtk)
+_PINCLUDE(gtkmm/private/eventcontroller_p.h)
+
+namespace Gtk
+{
+/** Event controller for shortcuts.
+ *
+ * %Gtk::ShortcutController is an event controller that manages shortcuts.
+ *
+ * Most common shortcuts are using this controller implicitly, e.g. by
+ * adding a mnemonic underline to a Gtk::Label, or by installing a key
+ * binding using gtk_widget_class_add_binding(), or by adding accelerators
+ * to global actions using Gtk::Application::set_accels_for_action().
+ *
+ * But it is possible to create your own shortcut controller, and add
+ * shortcuts to it.
+ *
+ * %Gtk::ShortcutController implements Gio::ListModel for querying the shortcuts that
+ * have been added to it.
+ *
+ * ## ShortcutController as a Buildable
+ *
+ * ShortcutControllers can be created in ui files to set up shortcuts
+ * in the same place as the widgets.
+ *
+ * An example of a UI definition fragment with GtkShortcutController:
+ * @code
+ *   <object class='GtkButton'>
+ *     <child>
+ *       <object class='GtkShortcutController'>
+ *         <property name='scope'>managed</property>
+ *         <child>
+ *           <object class='GtkShortcut'>
+ *             <property name='trigger'>&lt;Control&gt;k</property>
+ *             <property name='action'>activate</property>
+ *           </object>
+ *         </child>
+ *       </object>
+ *     </child>
+ *   </object>
+ * @endcode
+ *
+ * @newin{3,98}
+ *
+ * @ingroup Gestures
+ */
+class ShortcutController : public EventController, public Gio::ListModel, public Buildable
+{
+  _CLASS_GOBJECT(ShortcutController, GtkShortcutController, GTK_SHORTCUT_CONTROLLER,
+    EventController, GtkEventController)
+  _IMPLEMENTS_INTERFACE(Gio::ListModel)
+  _IMPLEMENTS_INTERFACE(Buildable)
+
+protected:
+  _CTOR_DEFAULT
+  _WRAP_CTOR(ShortcutController(const Glib::RefPtr<Gio::ListModel>& model), 
gtk_shortcut_controller_new_for_model)
+
+public:
+  _WRAP_CREATE()
+
+  /** Creates a new shortcut controller that takes its shortcuts from
+   * the given list model.
+   *
+   * A controller created by this function does not let you add or
+   * remove individual shortcuts using the shortcut controller api,
+   * but you can change the contents of the model.
+   *
+   * @param model A ListModel containing shortcuts.
+   * @return A newly created shortcut controller.
+   */
+  _WRAP_CREATE(const Glib::RefPtr<Gio::ListModel>& model)
+
+  _WRAP_METHOD(void set_mnemonics_modifiers(Gdk::ModifierType modifiers),
+    gtk_shortcut_controller_set_mnemonics_modifiers)
+  _WRAP_METHOD(Gdk::ModifierType get_mnemonics_modifiers() const,
+    gtk_shortcut_controller_get_mnemonics_modifiers)
+
+  _WRAP_METHOD(void set_scope(ShortcutScope scope), gtk_shortcut_controller_set_scope)
+  _WRAP_METHOD(ShortcutScope set_scope() const, gtk_shortcut_controller_get_scope)
+
+  // TODO: Activate when there is a Gtk::Shortcut class.
+  //_WRAP_METHOD_DOCS_ONLY(gtk_shortcut_controller_add_shortcut)
+  //void add_shortcut(const Glib::RefPtr<Shortcut>& shortcut);
+  //_WRAP_METHOD(void remove_shortcut(const Glib::RefPtr<const Shortcut>& shortcut),
+  //  gtk_shortcut_controller_remove_shortcut)
+
+  _WRAP_PROPERTY("mnemonic-modifiers", Gdk::ModifierType)
+  _WRAP_PROPERTY("scope", ShortcutScope)
+  // The "model" property is write-pnly, construct-only. Can't be wrapped.
+};
+
+} // namespace Gtk
diff --git a/gtk/src/shortcutmanager.ccg b/gtk/src/shortcutmanager.ccg
new file mode 100644
index 00000000..80938b6a
--- /dev/null
+++ b/gtk/src/shortcutmanager.ccg
@@ -0,0 +1,18 @@
+/* Copyright (C) 2020 The gtkmm Development Team
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <gtk/gtk.h>
+#include <gtkmm/shortcutcontroller.h>
diff --git a/gtk/src/shortcutmanager.hg b/gtk/src/shortcutmanager.hg
new file mode 100644
index 00000000..f2ce5254
--- /dev/null
+++ b/gtk/src/shortcutmanager.hg
@@ -0,0 +1,60 @@
+/* Copyright (C) 2020 The gtkmm Development Team
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <glibmm/interface.h>
+
+_DEFS(gtkmm,gtk)
+_PINCLUDE(glibmm/private/interface_p.h)
+_PINCLUDE(gtk/gtk.h)
+
+#ifndef DOXYGEN_SHOULD_SKIP_THIS
+extern "C"
+{
+typedef struct _GtkShortcutManagerInterface GtkShortcutManagerInterface;
+}
+#endif /* DOXYGEN_SHOULD_SKIP_THIS */
+
+namespace Gtk
+{
+class ShortcutController;
+
+/** Interface for managing shortcuts.
+ *
+ * The %Gtk::ShortcutManager interface is used to implement
+ * shortcut scopes.
+ *
+ * This is important for Gtk::Native widgets that have their own surface,
+ * since the event controllers that are used to implement managed and
+ * global scopes are limited to the same native.
+ *
+ * Examples of widgets implementing %Gtk::ShortcutManager are Gtk::Window
+ * and Gtk::Popover.
+ *
+ * @newin{3,98}
+ */
+class ShortcutManager : public Glib::Interface
+{
+  _CLASS_INTERFACE(ShortcutManager, GtkShortcutManager, GTK_SHORTCUT_MANAGER, GtkShortcutManagerInterface)
+
+public:
+
+protected:
+#m4 _CONVERSION(`GtkShortcutController*',`const Glib::RefPtr<ShortcutController>&',`Glib::wrap($3, true)')
+  _WRAP_VFUNC(void add_controller(const Glib::RefPtr<ShortcutController>& controller), "add_controller")
+  _WRAP_VFUNC(void remove_controller(const Glib::RefPtr<ShortcutController>& controller), 
"remove_controller")
+};
+
+} // namespace Gtk
diff --git a/gtk/src/window.hg b/gtk/src/window.hg
index 84b0716a..765e7ce8 100644
--- a/gtk/src/window.hg
+++ b/gtk/src/window.hg
@@ -24,6 +24,7 @@
 #include <gtkmm/widget.h>
 #include <gtkmm/native.h>
 #include <gtkmm/root.h>
+#include <gtkmm/shortcutmanager.h>
 #include <gtkmm/application.h>
 #include <gtkmm/windowgroup.h>
 
@@ -44,27 +45,25 @@ class WindowGroup;
  * This represents all widgets which are physical windows controlled
  * by the window manager.
  *
- * The window will be hidden when the window manager's close button is clicked.
- * Override on_delete_event() to stop this.
+ * Gtk::manage() has no effect on Windows because they have no parent containers.
  *
- * Gtk::manage() has no effect on Windows because they have no parent Containers.
+ * The window will be destroyed when the window manager's close button is clicked.
+ * Call set_hide_on_close() if you want it to be hidden instead.
  *
  * When a window is closed (hidden), it's removed from its application.
  * This can cause Application::run() to return, if it is the last window.
  *
  * @ingroup Widgets
  */
-class Window : public Widget, public Native, public Root
+class Window : public Widget, public Native, public ShortcutManager, public Root
 {
   _CLASS_GTKOBJECT(Window, GtkWindow, GTK_WINDOW, Gtk::Widget, GtkWidget)
   _IMPLEMENTS_INTERFACE(Native)
+  _IMPLEMENTS_INTERFACE(ShortcutManager)
   _IMPLEMENTS_INTERFACE(Root)
   _UNMANAGEABLE
   _IGNORE(gtk_window_destroy, gtk_window_set_destroy_with_parent)
 
-  // GtkWindow also implements GtkShortcutManager, but that interface is not
-  // (yet) wrapped in a C++ class.
-
   //TODO: Use gtk_window_set_destroy_with_parent() to allow use of Gtk::manage() with top-level windows, 
using the transient-parent?
 
 public:
diff --git a/tools/m4/convert_gtk.m4 b/tools/m4/convert_gtk.m4
index 22cf17e8..f1ccba39 100644
--- a/tools/m4/convert_gtk.m4
+++ b/tools/m4/convert_gtk.m4
@@ -74,6 +74,7 @@ _CONV_ENUM(Gtk,PropagationPhase)
 _CONV_ENUM(Gtk,RevealerTransitionType)
 _CONV_ENUM(Gtk,ScrollType)
 _CONV_ENUM(Gtk,SelectionMode)
+_CONV_ENUM(Gtk,ShortcutScope)
 _CONV_ENUM(Gtk,ShortcutType)
 _CONV_INCLASS_ENUM(Gtk,SizeGroup,Mode)
 _CONV_ENUM(Gtk,SortType)
@@ -414,6 +415,7 @@ _CONVERSION(`GtkShortcutsWindow*',`ShortcutsWindow*',__RP2P)
 _CONVERSION(`GtkShortcutsWindow*',`const ShortcutsWindow*',__RP2P)
 _CONVERSION(`ShortcutsWindow&',`GtkShortcutsWindow*',__FR2P)
 
+_CONVERSION(`const Glib::RefPtr<ShortcutController>&', `GtkShortcutController*', __CONVERT_REFPTR_TO_P)
 _CONVERSION(`const Glib::RefPtr<Gtk::Snapshot>&', `GtkSnapshot*', __CONVERT_REFPTR_TO_P)
 
 #FlowBoxChild


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