]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkapplicationwindow.c
Add GtkBubbleWindow
[~andy/gtk] / gtk / gtkapplicationwindow.c
index 3660b2c73911b1d763f0b929ba9a555d90889e86..271054b109cfa90d98391023e10b7c70b3fabaf8 100644 (file)
@@ -12,9 +12,7 @@
  * 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, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  *
  * Author: Ryan Lortie <desrt@desrt.ca>
  */
 
 #include "gtkapplicationwindow.h"
 
-#include "gtkmodelmenu.h"
-#include "gactionmuxer.h"
+#include "gtkapplicationprivate.h"
+#include "gtkwidgetprivate.h"
+#include "gtkwindowprivate.h"
 #include "gtkaccelgroup.h"
 #include "gtkaccelmap.h"
+#include "gtkmenubar.h"
 #include "gtkintl.h"
+#include "gtksettings.h"
+
+#include <gdk/gdk.h>
+#ifdef GDK_WINDOWING_X11
+#include <gdk/x11/gdkx.h>
+#endif
+
+#ifdef HAVE_GIO_UNIX
+#include <gio/gdesktopappinfo.h>
+#endif
 
 /**
  * SECTION:gtkapplicationwindow
@@ -37,8 +47,8 @@
  * GtkApplicationWindow is a #GtkWindow subclass that offers some
  * extra functionality for better integration with #GtkApplication
  * features.  Notably, it can handle both the application menu as well
- * as the menubar. See g_application_set_app_menu() and
- * g_application_set_menubar().
+ * as the menubar. See gtk_application_set_app_menu() and
+ * gtk_application_set_menubar().
  *
  * This class implements the #GActionGroup and #GActionMap interfaces,
  * to let you add window-specific actions that will be exported by the
  * prefix.  Actions must be addressed with the prefixed name when
  * referring to them from a #GMenuModel.
  *
+ * Note that widgets that are placed inside a GtkApplicationWindow
+ * can also activate these actions, if they implement the
+ * GtkActionable interface.
+ *
+ * As with #GtkApplication, the GDK lock will be acquired when
+ * processing actions arriving from other processes and should therefore
+ * be held when activating actions locally (if GDK threads are enabled).
+ *
  * The settings #GtkSettings:gtk-shell-shows-app-menu and
  * #GtkSettings:gtk-shell-shows-menubar tell GTK+ whether the
  * desktop environment is showing the application menu and menubar
  * will display the application menu, but not the menubar.
  *
  * If the desktop environment does not display the menubar, then
- * #GApplicationWindow will automatically show a #GtkMenubar for it.
+ * #GtkApplicationWindow will automatically show a #GtkMenuBar for it.
+ * (see the #GtkApplication docs for some screenshots of how this
+ * looks on different platforms).
  * This behaviour can be overridden with the #GtkApplicationWindow:show-menubar
  * property. If the desktop environment does not display the application
- * menu, then it will automatilly be included in the menubar.
+ * menu, then it will automatically be included in the menubar.
  *
  * <example><title>A GtkApplicationWindow with a menubar</title>
  * <programlisting><![CDATA[
@@ -76,8 +96,8 @@
  *     "    </submenu>"
  *     "  </menu>"
  *     "</interface>");
- * g_application_set_menubar (G_APPLICATION (app),
- *                            G_MENU_MODEL (gtk_builder_get_object (builder, "menubar")));
+ * gtk_application_set_menubar (G_APPLICATION (app),
+ *                              G_MENU_MODEL (gtk_builder_get_object (builder, "menubar")));
  * g_object_unref (builder);
  *
  * ...
  * ]]>
  * </programlisting>
  * </example>
+ *
+ * <example><title>Handling fallback yourself</title>
+ * <programlisting>
+ * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../examples/sunny.c">
+ *  <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
+ * </xi:include>
+ * </programlisting>
+ * </example>
+ *
+ * The XML format understood by #GtkBuilder for #GMenuModel consists
+ * of a toplevel <tag class="starttag">menu</tag> element, which contains
+ * one or more <tag class="starttag">item</tag> elements. Each
+ * <tag class="starttag">item</tag> element contains
+ * <tag class="starttag">attribute</tag> and <tag class="starttag">link</tag>
+ * elements with a mandatory name attribute.
+ * <tag class="starttag">link</tag> elements have the same content
+ * model as <tag class="starttag">menu</tag>.
+ *
+ * Attribute values can be translated using gettext, like other #GtkBuilder
+ * content. <tag class="starttag">attribute</tag> elements can be marked for
+ * translation with a <literal>translatable="yes"</literal> attribute.
+ * It is also possible to specify message context and translator comments,
+ * using the context and comments attributes. To make use of this, the
+ * #GtkBuilder must have been given the gettext domain to use.
  */
+
+typedef GSimpleActionGroupClass GtkApplicationWindowActionsClass;
+typedef struct
+{
+  GSimpleActionGroup parent_instance;
+  GtkWindow *window;
+} GtkApplicationWindowActions;
+
+static GType gtk_application_window_actions_get_type   (void);
+static void  gtk_application_window_actions_iface_init (GRemoteActionGroupInterface *iface);
+G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindowActions, gtk_application_window_actions, G_TYPE_SIMPLE_ACTION_GROUP,
+                         G_IMPLEMENT_INTERFACE (G_TYPE_REMOTE_ACTION_GROUP, gtk_application_window_actions_iface_init))
+
+static void
+gtk_application_window_actions_activate_action_full (GRemoteActionGroup *remote,
+                                                     const gchar        *action_name,
+                                                     GVariant           *parameter,
+                                                     GVariant           *platform_data)
+{
+  GtkApplicationWindowActions *actions = (GtkApplicationWindowActions *) remote;
+  GApplication *application;
+  GApplicationClass *class;
+
+  application = G_APPLICATION (gtk_window_get_application (actions->window));
+  class = G_APPLICATION_GET_CLASS (application);
+
+  class->before_emit (application, platform_data);
+  g_action_group_activate_action (G_ACTION_GROUP (actions), action_name, parameter);
+  class->after_emit (application, platform_data);
+}
+
+static void
+gtk_application_window_actions_change_action_state_full (GRemoteActionGroup *remote,
+                                                         const gchar        *action_name,
+                                                         GVariant           *value,
+                                                         GVariant           *platform_data)
+{
+  GtkApplicationWindowActions *actions = (GtkApplicationWindowActions *) remote;
+  GApplication *application;
+  GApplicationClass *class;
+
+  application = G_APPLICATION (gtk_window_get_application (actions->window));
+  class = G_APPLICATION_GET_CLASS (application);
+
+  class->before_emit (application, platform_data);
+  g_action_group_change_action_state (G_ACTION_GROUP (actions), action_name, value);
+  class->after_emit (application, platform_data);
+}
+
+static void
+gtk_application_window_actions_init (GtkApplicationWindowActions *actions)
+{
+}
+
+static void
+gtk_application_window_actions_iface_init (GRemoteActionGroupInterface *iface)
+{
+  iface->activate_action_full = gtk_application_window_actions_activate_action_full;
+  iface->change_action_state_full = gtk_application_window_actions_change_action_state_full;
+}
+
+static void
+gtk_application_window_actions_class_init (GtkApplicationWindowActionsClass *class)
+{
+}
+
+static GSimpleActionGroup *
+gtk_application_window_actions_new (GtkApplicationWindow *window)
+{
+  GtkApplicationWindowActions *actions;
+
+  actions = g_object_new (gtk_application_window_actions_get_type (), NULL);
+  actions->window = GTK_WINDOW (window);
+
+  return G_SIMPLE_ACTION_GROUP (actions);
+}
+
+/* Now onto GtkApplicationWindow... */
+
 struct _GtkApplicationWindowPrivate
 {
   GSimpleActionGroup *actions;
@@ -97,6 +220,12 @@ struct _GtkApplicationWindowPrivate
   GMenu *app_menu_section;
   GMenu *menubar_section;
   gboolean show_menubar;
+
+  GDBusConnection *session;
+  gchar           *object_path;
+  guint            export_id;
+
+  guint            id;
 };
 
 static void
@@ -121,27 +250,47 @@ gtk_application_window_update_menubar (GtkApplicationWindow *window)
 
   if (!have_menubar && should_have_menubar)
     {
-      GActionMuxer *muxer;
       GMenu *combined;
 
-      muxer = g_action_muxer_new ();
-      g_action_muxer_insert (muxer, "app", G_ACTION_GROUP (gtk_window_get_application (GTK_WINDOW (window))));
-      g_action_muxer_insert (muxer, "win", G_ACTION_GROUP (window));
-
       combined = g_menu_new ();
       g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->app_menu_section));
       g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->menubar_section));
 
-      window->priv->menubar = gtk_model_menu_create_menu_bar (G_MENU_MODEL (combined), G_ACTION_OBSERVABLE (muxer), window->priv->accels);
+      window->priv->menubar = gtk_menu_bar_new_from_model (G_MENU_MODEL (combined));
       gtk_widget_set_parent (window->priv->menubar, GTK_WIDGET (window));
       gtk_widget_show_all (window->priv->menubar);
       g_object_unref (combined);
-      g_object_unref (muxer);
 
       gtk_widget_queue_resize (GTK_WIDGET (window));
     }
 }
 
+static gchar *
+gtk_application_window_get_app_desktop_name (void)
+{
+  gchar *retval = NULL;
+
+#ifdef HAVE_GIO_UNIX
+  GDesktopAppInfo *app_info;
+  const gchar *app_name = NULL;
+  gchar *desktop_file;
+
+  desktop_file = g_strconcat (g_get_prgname (), ".desktop", NULL);
+  app_info = g_desktop_app_info_new (desktop_file);
+  g_free (desktop_file);
+
+  if (app_info != NULL)
+    app_name = g_app_info_get_name (G_APP_INFO (app_info));
+
+  if (app_name != NULL)
+    retval = g_strdup (app_name);
+
+  g_clear_object (&app_info);
+#endif /* HAVE_GIO_UNIX */
+
+  return retval;
+}
+
 static void
 gtk_application_window_update_shell_shows_app_menu (GtkApplicationWindow *window,
                                                     GtkSettings          *settings)
@@ -163,10 +312,30 @@ gtk_application_window_update_shell_shows_app_menu (GtkApplicationWindow *window
         {
           GMenuModel *app_menu;
 
-          app_menu = g_application_get_app_menu (G_APPLICATION (gtk_window_get_application (GTK_WINDOW (window))));
+          app_menu = gtk_application_get_app_menu (gtk_window_get_application (GTK_WINDOW (window)));
 
           if (app_menu != NULL)
-            g_menu_append_submenu (window->priv->app_menu_section, _("Application"), app_menu);
+            {
+              const gchar *app_name;
+              gchar *name;
+
+              app_name = g_get_application_name ();
+              if (app_name != g_get_prgname ())
+                {
+                  /* the app has set its application name, use it */
+                  name = g_strdup (app_name);
+                }
+              else
+                {
+                  /* get the name from .desktop file */
+                  name = gtk_application_window_get_app_desktop_name ();
+                  if (name == NULL)
+                    name = g_strdup (_("Application"));
+                }
+
+              g_menu_append_submenu (window->priv->app_menu_section, name, app_menu);
+              g_free (name);
+            }
         }
     }
 }
@@ -192,7 +361,7 @@ gtk_application_window_update_shell_shows_menubar (GtkApplicationWindow *window,
         {
           GMenuModel *menubar;
 
-          menubar = g_application_get_menubar (G_APPLICATION (gtk_window_get_application (GTK_WINDOW (window))));
+          menubar = gtk_application_get_menubar (gtk_window_get_application (GTK_WINDOW (window)));
 
           if (menubar != NULL)
             g_menu_append_section (window->priv->menubar_section, NULL, menubar);
@@ -249,11 +418,6 @@ free_accel_closures (GtkApplicationWindow *window)
   window->priv->accel_closures = NULL;
 }
 
-typedef struct {
-  GtkApplicationWindow *window;
-  GActionGroup *actions;
-} AccelData;
-
 /* Hack. We iterate over the accel map instead of the actions,
  * in order to pull the parameters out of accel map entries
  */
@@ -264,9 +428,8 @@ add_accel_closure (gpointer         data,
                    GdkModifierType  accel_mods,
                    gboolean         changed)
 {
-  AccelData *d = data;
-  GtkApplicationWindow *window = d->window;
-  GActionGroup *actions = d->actions;
+  GtkApplicationWindow *window = data;
+  GActionGroup *actions;
   const gchar *path;
   const gchar *p;
   gchar *action_name;
@@ -276,10 +439,10 @@ add_accel_closure (gpointer         data,
   if (accel_key == 0)
     return;
 
-  if (!g_str_has_prefix (accel_path, "<Actions>/"))
+  if (!g_str_has_prefix (accel_path, "<GAction>/"))
     return;
 
-  path = accel_path + strlen ("<Actions>/");
+  path = accel_path + strlen ("<GAction>/");
   p = strchr (path, '/');
   if (p)
     {
@@ -294,6 +457,7 @@ add_accel_closure (gpointer         data,
       parameter = NULL;
     }
 
+  actions = G_ACTION_GROUP (_gtk_widget_get_action_muxer (GTK_WIDGET (window)));
   if (g_action_group_has_action (actions, action_name))
     {
       closure = (AccelClosure*) g_closure_new_object (sizeof (AccelClosure), g_object_ref (actions));
@@ -307,6 +471,10 @@ add_accel_closure (gpointer         data,
 
       gtk_accel_group_connect_by_path (window->priv->accels, accel_path, &closure->closure);
     }
+  else if (parameter)
+    {
+      g_variant_unref (parameter);
+    }
 
   g_free (action_name);
 }
@@ -314,27 +482,9 @@ add_accel_closure (gpointer         data,
 static void
 gtk_application_window_update_accels (GtkApplicationWindow *window)
 {
-  GtkApplication *application;
-  AccelData data;
-  GActionMuxer *muxer;
-
-  /* FIXME: we should keep the muxer around, and listen for changes.
-   * Also, we should listen for accel map changes
-   */
-  application = gtk_window_get_application (GTK_WINDOW (window));
-
   free_accel_closures (window);
 
-  muxer = g_action_muxer_new ();
-  g_action_muxer_insert (muxer, "app", G_ACTION_GROUP (application));
-  g_action_muxer_insert (muxer, "win", G_ACTION_GROUP (window));
-
-  data.window = window;
-  data.actions = G_ACTION_GROUP (muxer);
-
-  gtk_accel_map_foreach (&data, add_accel_closure);
-
-  g_object_unref (muxer);
+  gtk_accel_map_foreach (window, add_accel_closure);
 }
 
 static void
@@ -525,15 +675,21 @@ gtk_application_window_real_get_preferred_width_for_height (GtkWidget *widget,
                                                             gint      *natural_width)
 {
   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
+  gint menubar_height;
+
+  if (window->priv->menubar != NULL)
+    gtk_widget_get_preferred_height (window->priv->menubar, &menubar_height, NULL);
+  else
+    menubar_height = 0;
 
   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
-    ->get_preferred_width_for_height (widget, height, minimum_width, natural_width);
+    ->get_preferred_width_for_height (widget, height - menubar_height, minimum_width, natural_width);
 
   if (window->priv->menubar != NULL)
     {
       gint menubar_min_width, menubar_nat_width;
 
-      gtk_widget_get_preferred_width_for_height (window->priv->menubar, height, &menubar_min_width, &menubar_nat_width);
+      gtk_widget_get_preferred_width_for_height (window->priv->menubar, menubar_height, &menubar_min_width, &menubar_nat_width);
       *minimum_width = MAX (*minimum_width, menubar_min_width);
       *natural_width = MAX (*natural_width, menubar_nat_width);
     }
@@ -548,12 +704,14 @@ gtk_application_window_real_size_allocate (GtkWidget     *widget,
   if (window->priv->menubar != NULL)
     {
       GtkAllocation menubar_allocation = *allocation;
-      gint menubar_min_height, menubar_nat_height;
+      gint menubar_height;
       GtkWidget *child;
 
-      gtk_widget_get_preferred_height_for_width (window->priv->menubar, allocation->width, &menubar_min_height, &menubar_nat_height);
+      _gtk_window_set_allocation (GTK_WINDOW (widget), allocation);
+
+      gtk_widget_get_preferred_height_for_width (window->priv->menubar, allocation->width, &menubar_height, NULL);
 
-      menubar_allocation.height = menubar_min_height;
+      menubar_allocation.height = menubar_height;
       gtk_widget_size_allocate (window->priv->menubar, &menubar_allocation);
 
       child = gtk_bin_get_child (GTK_BIN (window));
@@ -562,17 +720,14 @@ gtk_application_window_real_size_allocate (GtkWidget     *widget,
           GtkAllocation child_allocation = *allocation;
           gint border_width;
 
-          child_allocation.height = MAX (1, child_allocation.height - menubar_min_height);
-
           border_width = gtk_container_get_border_width (GTK_CONTAINER (window));
           child_allocation.x += border_width;
-          child_allocation.y += border_width + menubar_min_height;
-          child_allocation.width -= border_width * 2;
-          child_allocation.height -= border_width * 2 - menubar_min_height;
+          child_allocation.y += border_width + menubar_height;
+          child_allocation.width = MAX (1, child_allocation.width - border_width * 2);
+          child_allocation.height = MAX (1, child_allocation.height - border_width * 2 - menubar_height);
+
           gtk_widget_size_allocate (child, &child_allocation);
         }
-
-      gtk_widget_set_allocation (widget, allocation);
     }
   else
     GTK_WIDGET_CLASS (gtk_application_window_parent_class)
@@ -583,8 +738,10 @@ static void
 gtk_application_window_real_realize (GtkWidget *widget)
 {
   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
+  GtkApplication *application;
   GtkSettings *settings;
 
+  application = gtk_window_get_application (GTK_WINDOW (window));
   settings = gtk_widget_get_settings (widget);
 
   g_signal_connect (settings, "notify::gtk-shell-shows-app-menu",
@@ -599,6 +756,35 @@ gtk_application_window_real_realize (GtkWidget *widget)
 
   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
     ->realize (widget);
+
+#ifdef GDK_WINDOWING_X11
+  {
+    GdkWindow *gdkwindow;
+
+    gdkwindow = gtk_widget_get_window (GTK_WIDGET (window));
+
+    if (GDK_IS_X11_WINDOW (gdkwindow) && window->priv->session)
+      {
+        gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APPLICATION_ID",
+                                          g_application_get_application_id (G_APPLICATION (application)));
+
+        gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_UNIQUE_BUS_NAME",
+                                          g_dbus_connection_get_unique_name (window->priv->session));
+
+        gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APPLICATION_OBJECT_PATH",
+                                          g_application_get_dbus_object_path (G_APPLICATION (application)));
+
+        gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_WINDOW_OBJECT_PATH",
+                                          window->priv->object_path);
+
+        gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APP_MENU_OBJECT_PATH",
+                                          gtk_application_get_app_menu_object_path (application));
+
+        gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_MENUBAR_OBJECT_PATH",
+                                          gtk_application_get_menubar_object_path (application));
+      }
+  }
+#endif
 }
 
 static void
@@ -615,6 +801,48 @@ gtk_application_window_real_unrealize (GtkWidget *widget)
     ->unrealize (widget);
 }
 
+gboolean
+gtk_application_window_publish (GtkApplicationWindow *window,
+                                GDBusConnection      *session,
+                                const gchar          *object_path,
+                                guint                 object_id)
+{
+  g_assert (window->priv->session == NULL);
+  g_assert (window->priv->export_id == 0);
+  g_assert (window->priv->object_path == NULL);
+  g_assert (window->priv->id == 0);
+
+  window->priv->export_id = g_dbus_connection_export_action_group (session, object_path,
+                                                                   G_ACTION_GROUP (window->priv->actions),
+                                                                   NULL);
+
+  if (window->priv->export_id == 0)
+    return FALSE;
+
+  window->priv->session = session;
+  window->priv->object_path = g_strdup (object_path);
+  window->priv->id = object_id;
+
+  return TRUE;
+}
+
+void
+gtk_application_window_unpublish (GtkApplicationWindow *window)
+{
+  g_assert (window->priv->session != NULL);
+  g_assert (window->priv->export_id != 0);
+  g_assert (window->priv->object_path != NULL);
+  g_assert (window->priv->id != 0);
+
+  g_dbus_connection_unexport_action_group (window->priv->session, window->priv->export_id);
+  window->priv->session = NULL;
+  window->priv->export_id = 0;
+  window->priv->id = 0;
+
+  g_free (window->priv->object_path);
+  window->priv->object_path = NULL;
+}
+
 static void
 gtk_application_window_real_map (GtkWidget *widget)
 {
@@ -643,7 +871,6 @@ gtk_application_window_real_forall_internal (GtkContainer *container,
     ->forall (container, include_internal, callback, user_data);
 }
 
-
 static void
 gtk_application_window_get_property (GObject    *object,
                                      guint       prop_id,
@@ -709,12 +936,14 @@ gtk_application_window_init (GtkApplicationWindow *window)
 {
   window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window, GTK_TYPE_APPLICATION_WINDOW, GtkApplicationWindowPrivate);
 
-  window->priv->actions = g_simple_action_group_new ();
+  window->priv->actions = gtk_application_window_actions_new (window);
   window->priv->app_menu_section = g_menu_new ();
   window->priv->menubar_section = g_menu_new ();
   window->priv->accels = gtk_accel_group_new ();
   gtk_window_add_accel_group (GTK_WINDOW (window), window->priv->accels);
 
+  gtk_widget_insert_action_group (GTK_WIDGET (window), "win", G_ACTION_GROUP (window->priv->actions));
+
   /* window->priv->actions is the one and only ref on the group, so when
    * we dispose, the action group will die, disconnecting all signals.
    */
@@ -753,8 +982,8 @@ gtk_application_window_class_init (GtkApplicationWindowClass *class)
    *
    * If this property is %TRUE, the window will display a menubar
    * that includes the app menu and menubar, unless these are
-   * shown by the desktop shell. See g_application_set_app_menu()
-   * and g_application_set_menubar().
+   * shown by the desktop shell. See gtk_application_set_app_menu()
+   * and gtk_application_set_menubar().
    *
    * If %FALSE, the window will not display a menubar, regardless
    * of whether the desktop shell is showing the menus or not.
@@ -833,3 +1062,29 @@ gtk_application_window_set_show_menubar (GtkApplicationWindow *window,
       g_object_notify_by_pspec (G_OBJECT (window), gtk_application_window_properties[PROP_SHOW_MENUBAR]);
     }
 }
+
+GtkAccelGroup *
+gtk_application_window_get_accel_group (GtkApplicationWindow *window)
+{
+  return window->priv->accels;
+}
+
+/**
+ * gtk_application_window_get_id:
+ * @window: a #GtkApplicationWindow
+ *
+ * Returns the unique ID of the window. If the window has not yet been added to
+ * a #GtkApplication, returns <literal>0</literal>.
+ *
+ * Returns: the unique ID for @window, or <literal>0</literal> if the window
+ *   has not yet been added to a #GtkApplication
+ *
+ * Since: 3.6
+ */
+guint
+gtk_application_window_get_id (GtkApplicationWindow *window)
+{
+  g_return_val_if_fail (GTK_IS_APPLICATION_WINDOW (window), 0);
+
+  return window->priv->id;
+}