[Xfce4-commits] [apps/mousepad] 01/01: Support statusbar tooltips for toolbar items too

noreply at xfce.org noreply at xfce.org
Mon Jul 14 02:37:19 CEST 2014


This is an automated email from the git hooks/post-receive script.

mbrush pushed a commit to branch master
in repository apps/mousepad.

commit 1502541dcdcaa9c1e7a41bcfbd3f64674164f0f3
Author: Matthew Brush <mbrush at codebrainz.ca>
Date:   Sun Jul 13 16:01:53 2014 -0700

    Support statusbar tooltips for toolbar items too
    
    Refactor the code so that the statusbar can itself display tooltips for
    widgets and the window code just uses this. For the proxy connections,
    tool items need a bit of special treatment since they don't have the
    "select" and "deselect" signals used by menu items, so use the Gdk
    event signals for mouse enter/leave and focus enter/leave (to cover
    mouse and keyboard events) are used on the tool items' child widgets.
    
    Finishes one of the TODOs in 2b51559.
---
 mousepad/mousepad-statusbar.c |   45 +++++++++++++++++++
 mousepad/mousepad-statusbar.h |    6 +++
 mousepad/mousepad-util.c      |   28 ++++++++++++
 mousepad/mousepad-util.h      |    2 +
 mousepad/mousepad-window.c    |  100 ++++++++++++++++++++++++++---------------
 5 files changed, 144 insertions(+), 37 deletions(-)

diff --git a/mousepad/mousepad-statusbar.c b/mousepad/mousepad-statusbar.c
index e69a494..e4abdc0 100644
--- a/mousepad/mousepad-statusbar.c
+++ b/mousepad/mousepad-statusbar.c
@@ -298,3 +298,48 @@ mousepad_statusbar_set_overwrite (MousepadStatusbar *statusbar,
 
   statusbar->overwrite_enabled = overwrite;
 }
+
+
+
+gboolean
+mousepad_statusbar_push_tooltip (MousepadStatusbar *statusbar,
+                                 GtkWidget         *widget)
+{
+  GtkAction   *action = NULL;
+  const gchar *tooltip = NULL;
+  gint         id;
+
+  /* get the widget's related action */
+  action = mousepad_util_find_related_action (widget);
+
+  /* if the action has a tooltip, use it */
+  if (G_LIKELY (action != NULL))
+    tooltip = gtk_action_get_tooltip (action);
+
+  /* if the action doesn't have a tooltip, see if the widget has one */
+  if (G_UNLIKELY (tooltip == NULL) && gtk_widget_get_has_tooltip (widget))
+    tooltip = gtk_widget_get_tooltip_text (widget);
+
+  if (G_LIKELY (tooltip != NULL))
+    {
+      /* show the tooltip */
+      id = gtk_statusbar_get_context_id (GTK_STATUSBAR (statusbar), "tooltip");
+      gtk_statusbar_push (GTK_STATUSBAR (statusbar), id, tooltip);
+      return TRUE;
+    }
+
+  return FALSE;
+}
+
+
+
+void
+mousepad_statusbar_pop_tooltip (MousepadStatusbar *statusbar,
+                                GtkWidget         *widget)
+{
+  gint id;
+
+  /* drop the widget's tooltip from the statusbar */
+  id = gtk_statusbar_get_context_id (GTK_STATUSBAR (statusbar), "tooltip");
+  gtk_statusbar_pop (GTK_STATUSBAR (statusbar), id);
+}
diff --git a/mousepad/mousepad-statusbar.h b/mousepad/mousepad-statusbar.h
index 8ebe7a6..3a54c68 100644
--- a/mousepad/mousepad-statusbar.h
+++ b/mousepad/mousepad-statusbar.h
@@ -44,6 +44,12 @@ void        mousepad_statusbar_set_overwrite        (MousepadStatusbar *statusba
 void        mousepad_statusbar_set_language         (MousepadStatusbar *statusbar,
                                                      GtkSourceLanguage *language);
 
+gboolean    mousepad_statusbar_push_tooltip         (MousepadStatusbar *statusbar,
+                                                     GtkWidget         *widget);
+
+void        mousepad_statusbar_pop_tooltip          (MousepadStatusbar *statusbar,
+                                                     GtkWidget         *widget);
+
 G_END_DECLS
 
 #endif /* !__MOUSEPAD_STATUSBAR_H__ */
diff --git a/mousepad/mousepad-util.c b/mousepad/mousepad-util.c
index c57ce71..9ca07a8 100644
--- a/mousepad/mousepad-util.c
+++ b/mousepad/mousepad-util.c
@@ -1216,3 +1216,31 @@ mousepad_util_languages_get_sorted_for_section (const gchar *section)
 
   return g_slist_sort(list, (GCompareFunc)mousepad_util_languages_name_compare);
 }
+
+
+
+/* get the related action of the widget or walk up the parents to find one.
+ * useful for example to get the related action of a tool item from its child. */
+GtkAction *
+mousepad_util_find_related_action (GtkWidget *widget)
+{
+  GtkAction *action;
+
+  g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
+
+  do
+    {
+      if (GTK_IS_ACTIVATABLE (widget))
+        action = gtk_activatable_get_related_action (GTK_ACTIVATABLE (widget));
+
+      if (G_UNLIKELY (action == NULL))
+        {
+          if (gtk_widget_is_toplevel (widget))
+            break;
+          widget = gtk_widget_get_parent (widget);
+        }
+    }
+  while (action == NULL);
+
+  return action;
+}
diff --git a/mousepad/mousepad-util.h b/mousepad/mousepad-util.h
index dbafd81..ef80c7d 100644
--- a/mousepad/mousepad-util.h
+++ b/mousepad/mousepad-util.h
@@ -133,6 +133,8 @@ GSList    *mousepad_util_language_sections_get_sorted     (void);
 
 GSList    *mousepad_util_languages_get_sorted_for_section (const gchar         *section);
 
+GtkAction *mousepad_util_find_related_action              (GtkWidget           *widget);
+
 G_END_DECLS
 
 #endif /* !__MOUSEPAD_UTIL_H__ */
diff --git a/mousepad/mousepad-window.c b/mousepad/mousepad-window.c
index b469c38..ec213e8 100644
--- a/mousepad/mousepad-window.c
+++ b/mousepad/mousepad-window.c
@@ -85,6 +85,13 @@ static void              mousepad_window_menu_item_selected           (GtkWidget
                                                                        MousepadWindow         *window);
 static void              mousepad_window_menu_item_deselected         (GtkWidget              *menu_item,
                                                                        MousepadWindow         *window);
+static gboolean          mousepad_window_tool_item_enter_event        (GtkWidget              *tool_item,
+                                                                       GdkEvent               *event,
+                                                                       MousepadWindow         *window);
+static gboolean          mousepad_window_tool_item_leave_event        (GtkWidget              *tool_item,
+                                                                       GdkEvent               *event,
+                                                                       MousepadWindow         *window);
+
 
 /* save windows geometry */
 static gboolean          mousepad_window_save_geometry_timer          (gpointer                user_data);
@@ -1112,9 +1119,26 @@ mousepad_window_connect_proxy (GtkUIManager   *manager,
 
   if (GTK_IS_MENU_ITEM (proxy))
     {
+      /* menu item's don't work with gdk window events */
       g_signal_connect_object (proxy, "select", G_CALLBACK (mousepad_window_menu_item_selected), window, 0);
       g_signal_connect_object (proxy, "deselect", G_CALLBACK (mousepad_window_menu_item_deselected), window, 0);
     }
+  else if (GTK_IS_TOOL_ITEM (proxy))
+    {
+      GtkWidget *child;
+
+      /* tool items will have GtkButton or other widgets in them, we want the child */
+      child = gtk_bin_get_child (GTK_BIN (proxy));
+
+      /* get events for mouse enter/leave and focus in/out */
+      gtk_widget_add_events (child, GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_FOCUS_CHANGE_MASK);
+
+      /* connect to signals for the events */
+      g_signal_connect_object (child, "enter-notify-event", G_CALLBACK (mousepad_window_tool_item_enter_event), window, 0);
+      g_signal_connect_object (child, "leave-notify-event", G_CALLBACK (mousepad_window_tool_item_leave_event), window, 0);
+      g_signal_connect_object (child, "focus-in-event", G_CALLBACK (mousepad_window_tool_item_enter_event), window, 0);
+      g_signal_connect_object (child, "focus-out-event", G_CALLBACK (mousepad_window_tool_item_leave_event), window, 0);
+    }
 }
 
 
@@ -1131,9 +1155,23 @@ mousepad_window_disconnect_proxy (GtkUIManager   *manager,
 
   if (GTK_IS_MENU_ITEM (proxy))
     {
+      /* disconnect from the select/deselect signals */
       g_signal_handlers_disconnect_by_func (proxy, mousepad_window_menu_item_selected, window);
       g_signal_handlers_disconnect_by_func (proxy, mousepad_window_menu_item_deselected, window);
     }
+  else if (GTK_IS_TOOL_ITEM (proxy))
+    {
+      GtkWidget *child;
+
+      /* tool items will have GtkButton or other widgets in them, we want the child */
+      child = gtk_bin_get_child (GTK_BIN (proxy));
+
+      /* disconnect from the gdk window event signals */
+      g_signal_handlers_disconnect_by_func (child, mousepad_window_tool_item_enter_event, window);
+      g_signal_handlers_disconnect_by_func (child, mousepad_window_tool_item_leave_event, window);
+      g_signal_handlers_disconnect_by_func (child, mousepad_window_tool_item_enter_event, window);
+      g_signal_handlers_disconnect_by_func (child, mousepad_window_tool_item_leave_event, window);
+    }
 }
 
 
@@ -1142,35 +1180,7 @@ static void
 mousepad_window_menu_item_selected (GtkWidget      *menu_item,
                                     MousepadWindow *window)
 {
-  GtkAction *action;
-  gchar     *tooltip;
-  gint       id;
-
-  /* we can only display tooltips if we have a statusbar */
-  if (G_LIKELY (window->statusbar != NULL))
-    {
-      /* get the action from the menu item */
-#if GTK_CHECK_VERSION (2, 16, 0)
-      action = gtk_activatable_get_related_action (GTK_ACTIVATABLE (menu_item));
-#else
-      action = gtk_widget_get_action (menu_item);
-#endif
-      if (G_LIKELY (action))
-        {
-          /* read the tooltip from the action, if there is one */
-          g_object_get (G_OBJECT (action), "tooltip", &tooltip, NULL);
-
-          if (G_LIKELY (tooltip != NULL))
-            {
-              /* show the tooltip */
-              id = gtk_statusbar_get_context_id (GTK_STATUSBAR (window->statusbar), "tooltip");
-              gtk_statusbar_push (GTK_STATUSBAR (window->statusbar), id, tooltip);
-
-              /* cleanup */
-              g_free (tooltip);
-            }
-        }
-    }
+  mousepad_statusbar_push_tooltip (MOUSEPAD_STATUSBAR (window->statusbar), menu_item);
 }
 
 
@@ -1179,15 +1189,31 @@ static void
 mousepad_window_menu_item_deselected (GtkWidget      *menu_item,
                                       MousepadWindow *window)
 {
-  gint id;
+  mousepad_statusbar_pop_tooltip (MOUSEPAD_STATUSBAR (window->statusbar), menu_item);
+}
 
-  /* we can only undisplay tooltips if we have a statusbar */
-  if (G_LIKELY (window->statusbar != NULL))
-    {
-      /* drop the last tooltip from the statusbar */
-      id = gtk_statusbar_get_context_id (GTK_STATUSBAR (window->statusbar), "tooltip");
-      gtk_statusbar_pop (GTK_STATUSBAR (window->statusbar), id);
-    }
+
+
+static gboolean
+mousepad_window_tool_item_enter_event (GtkWidget      *tool_item,
+                                       GdkEvent       *event,
+                                       MousepadWindow *window)
+{
+  mousepad_statusbar_push_tooltip (MOUSEPAD_STATUSBAR (window->statusbar), tool_item);
+
+  return FALSE;
+}
+
+
+
+static gboolean
+mousepad_window_tool_item_leave_event (GtkWidget      *tool_item,
+                                       GdkEvent       *event,
+                                       MousepadWindow *window)
+{
+  mousepad_statusbar_pop_tooltip (MOUSEPAD_STATUSBAR (window->statusbar), tool_item);
+
+  return FALSE;
 }
 
 

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Xfce4-commits mailing list