2009-03-12 Alberto Garcia <agarcia@igalia.com>
[hildon] / src / hildon-program.c
index 3e52eb2..53da518 100644 (file)
@@ -3,12 +3,12 @@
  *
  * Copyright (C) 2006 Nokia Corporation, all rights reserved.
  *
- * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@nokia.com>
+ * Contact: Rodrigo Novo <rodrigo.novo@nokia.com>
  *
  * 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; version 2.1 of
- * the License.
+ * 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
  *
  */
 
-/*
- * @file hildon-program.c
+/**
+ * SECTION:hildon-program
+ * @short_description: An object that represents an application running in the Hildon framework.
+ * @see_also: #HildonWindow, #HildonStackableWindow
+ *
+ * #HildonProgram is an object used to represent an application running
+ * in the Hildon framework.
  *
- * This file implements the HildonProgram object
+ * Applications can have one or more #HildonWindow<!-- -->s. These
+ * can be registered in the #HildonProgram with hildon_program_add_window(),
+ * and can be unregistered similarly with hildon_program_remove_window().
  *
+ * #HildonProgram provides the programmer with commodities such
+ * as applying a common toolbar and menu to all registered
+ * #HildonWindow<!-- -->s. This is done with hildon_program_set_common_menu(),
+ * hildon_program_set_common_app_menu() and hildon_program_set_common_toolbar().
+ *
+ * #HildonProgram is also used to apply program-wide properties that
+ * are specific to the Hildon framework. For instance
+ * hildon_program_set_can_hibernate() sets whether or not an application
+ * can be set to hibernate by the Hildon task navigator, in situations of
+ * low memory.
+ *
+ * <example>
+ * <programlisting>
+ * HildonProgram *program;
+ * HildonWindow *window1;
+ * HildonWindow *window2;
+ * GtkToolbar *common_toolbar, *window_specific_toolbar;
+ * HildonAppMenu *menu;
+ * <!-- -->
+ * program = HILDON_PROGRAM (hildon_program_get_instance ());
+ * <!-- -->
+ * window1 = HILDON_WINDOW (hildon_window_new ());
+ * window2 = HILDON_WINDOW (hildon_window_new ());
+ * <!-- -->
+ * common_toolbar = create_common_toolbar ();
+ * window_specific_toolbar = create_window_specific_toolbar ();
+ * <!-- -->
+ * menu = create_menu ();
+ * <!-- -->
+ * hildon_program_add_window (program, window1);
+ * hildon_program_add_window (program, window2);
+ * <!-- -->
+ * hildon_program_set_common_app_menu (program, menu);
+ * <!-- -->
+ * hildon_program_set_common_toolbar (program, common_toolbar);
+ * hildon_window_add_toolbar (window1, window_specific_toolbar);
+ * <!-- -->
+ * hildon_program_set_can_hibernate (program, TRUE);
+ * </programlisting>
+ * </example>
  */
 
+#undef                                          HILDON_DISABLE_DEPRECATED
+
+#ifdef                                          HAVE_CONFIG_H
 #include                                        <config.h>
+#endif
+
+#include                                        <X11/Xatom.h>
+
 #include                                        "hildon-program.h"
 #include                                        "hildon-program-private.h"
 #include                                        "hildon-window-private.h"
-#include                                        <X11/Xatom.h>
+#include                                        "hildon-window-stack.h"
+#include                                        "hildon-app-menu-private.h"
 
 static void
 hildon_program_init                             (HildonProgram *self);
@@ -62,7 +117,7 @@ enum
     PROP_KILLABLE
 };
 
-GType
+GType G_GNUC_CONST
 hildon_program_get_type                         (void)
 {
     static GType program_type = 0;
@@ -97,8 +152,10 @@ hildon_program_init                             (HildonProgram *self)
     priv->window_count = 0;
     priv->is_topmost = FALSE;
     priv->window_group = GDK_WINDOW_XID (gdk_display_get_default_group (gdk_display_get_default()));
+    priv->common_menu = NULL;
+    priv->common_app_menu = NULL;
     priv->common_toolbar = NULL;
-    priv->name = NULL;
+    priv->windows = NULL;
 }
 
 static void
@@ -118,8 +175,6 @@ hildon_program_finalize                         (GObject *self)
         g_object_unref (priv->common_menu);
         priv->common_menu = NULL;
     }
-
-    g_free (priv->name);
 }
 
 static void
@@ -135,6 +190,13 @@ hildon_program_class_init                       (HildonProgramClass *self)
     object_class->get_property  = hildon_program_get_property;
 
     /* Install properties */
+
+    /**
+     * HildonProgram:is-topmost:
+     *
+     * Whether one of the program's window or dialog currently
+     * is activated by window manager. 
+     */
     g_object_class_install_property (object_class, PROP_IS_TOPMOST,
                 g_param_spec_boolean ("is-topmost",
                 "Is top-most",
@@ -142,7 +204,13 @@ hildon_program_class_init                       (HildonProgramClass *self)
                 "is activated by window manager",
                 FALSE,
                 G_PARAM_READABLE)); 
-    
+
+    /**
+     * HildonProgram:can-hibernate:
+     *
+     * Whether the program should be set to hibernate by the Task
+     * Navigator in low memory situation.
+     */
     g_object_class_install_property (object_class, PROP_KILLABLE,
                 g_param_spec_boolean ("can-hibernate",
                 "Can hibernate",
@@ -197,6 +265,42 @@ hildon_program_get_property                     (GObject *object,
     }
 }
 
+/**
+ * hildon_program_pop_window_stack:
+ * @self: A #HildonProgram
+ *
+ * Pops a window from the stack.
+ *
+ * Deprecated: Use hildon_window_stack_pop() instead
+ *
+ * Returns: A #HildonStackableWindow, or %NULL
+ */
+HildonStackableWindow *
+hildon_program_pop_window_stack                 (HildonProgram *self)
+{
+    HildonWindowStack *stack = hildon_window_stack_get_default ();
+    GtkWidget *win = hildon_window_stack_pop_1 (stack);
+    g_warning ("%s: this function is deprecated. Use hildon_window_stack_pop() instead", __FUNCTION__);
+    return win ? HILDON_STACKABLE_WINDOW (win) : NULL;
+}
+
+/**
+ * hildon_program_peek_window_stack:
+ * @self: A #HildonProgram
+ *
+ * Deprecated: Use hildon_window_stack_peek() instead
+ *
+ * Returns: A #HildonStackableWindow, or %NULL
+ */
+HildonStackableWindow *
+hildon_program_peek_window_stack                (HildonProgram *self)
+{
+    HildonWindowStack *stack = hildon_window_stack_get_default ();
+    GtkWidget *win = hildon_window_stack_peek (stack);
+    g_warning ("%s: this function is deprecated. Use hildon_window_stack_peek() instead", __FUNCTION__);
+    return win ? HILDON_STACKABLE_WINDOW (win) : NULL;
+}
+
 /* Utilities */
 static gint 
 hildon_program_window_list_compare              (gconstpointer window_a, 
@@ -242,26 +346,27 @@ hildon_program_update_top_most                  (HildonProgram *program)
 
     if (active_window)
     {
+      gint xerror;
+      gboolean is_topmost = FALSE;
+      
+      gdk_error_trap_push ();
       wm_hints = XGetWMHints (GDK_DISPLAY (), active_window);
+      xerror = gdk_error_trap_pop ();
+      if (xerror)
+        return;
 
       if (wm_hints)
       {
+        is_topmost = (wm_hints->window_group == priv->window_group);
+        XFree (wm_hints);
+      }
 
-          if (wm_hints->window_group == priv->window_group)
-          {
-              if (!priv->is_topmost)
-              {
-                  priv->is_topmost = TRUE;
-                  g_object_notify (G_OBJECT (program), "is-topmost");
-              }
-          }
-          else if (priv->is_topmost)
-          {
-            priv->is_topmost = FALSE;
-            g_object_notify (G_OBJECT (program), "is-topmost");
-          }
+      /* Send notification if is_topmost has changed */
+      if (!priv->is_topmost != !is_topmost)
+      {
+        priv->is_topmost = is_topmost;
+        g_object_notify (G_OBJECT (program), "is-topmost");
       }
-      XFree (wm_hints);
     }
 
     /* Check each window if it was is_topmost */
@@ -312,8 +417,11 @@ hildon_program_common_toolbar_topmost_window    (gpointer window,
 /**
  * hildon_program_get_instance:
  *
- * Return value: Returns the #HildonProgram for the current process.
- * The object is created on the first call.
+ * Returns the #HildonProgram for the current process. The object is
+ * created on the first call. Note that you're not supposed to unref
+ * the returned object since it's not reffed in the first place.
+ *
+ * Return value: the #HildonProgram.
  **/
 HildonProgram*
 hildon_program_get_instance                     (void)
@@ -330,13 +438,13 @@ hildon_program_get_instance                     (void)
 
 /**
  * hildon_program_add_window:
- * @program: The @HildonProgram to which the window should be registered
- * @window: A @HildonWindow to be added
+ * @self: The #HildonProgram to which the window should be registered
+ * @window: A #HildonWindow to be added
  *
- * Registers a @HildonWindow as belonging to a given @HildonProgram. This
+ * Registers a #HildonWindow as belonging to a given #HildonProgram. This
  * allows to apply program-wide settings as all the registered windows,
- * such as hildon_program_set_common_menu() and
- * hildon_pogram_set_common_toolbar()
+ * such as hildon_program_set_common_menu(), hildon_program_set_common_app_menu()
+ * and hildon_program_set_common_toolbar().
  **/
 void
 hildon_program_add_window                       (HildonProgram *self, 
@@ -380,11 +488,11 @@ hildon_program_add_window                       (HildonProgram *self,
 /**
  * hildon_program_remove_window:
  * @self: The #HildonProgram to which the window should be unregistered
- * @window: The @HildonWindow to unregister
+ * @window: The #HildonWindow to unregister
  *
  * Used to unregister a window from the program. Subsequent calls to
- * hildon_program_set_common_menu() and hildon_pogram_set_common_toolbar()
- * will not affect the window
+ * hildon_program_set_common_menu(), hildon_program_set_common_app_menu()
+ * and hildon_program_set_common_toolbar() will not affect the window.
  **/
 void
 hildon_program_remove_window                    (HildonProgram *self, 
@@ -419,7 +527,7 @@ hildon_program_remove_window                    (HildonProgram *self,
  **/
 void
 hildon_program_set_can_hibernate                (HildonProgram *self, 
-                                                 gboolean killable)
+                                                 gboolean can_hibernate)
 {
     HildonProgramPrivate *priv;
     
@@ -428,21 +536,23 @@ hildon_program_set_can_hibernate                (HildonProgram *self,
     priv = HILDON_PROGRAM_GET_PRIVATE (self);
     g_assert (priv);
 
-    if (priv->killable != killable)
+    if (priv->killable != can_hibernate)
     {
         g_slist_foreach (priv->windows, 
-                (GFunc)hildon_window_set_can_hibernate_property, &killable);
+                (GFunc) hildon_window_set_can_hibernate_property, &can_hibernate);
     }
 
-    priv->killable = killable;
+    priv->killable = can_hibernate;
 }
 
 /**
  * hildon_program_get_can_hibernate:
  * @self: The #HildonProgram which can hibernate or not
- * 
- * Return value: Whether or not this #HildonProgram is set to be
- * support hibernation from the Hildon task navigator
+ *
+ * Returns whether the #HildonProgram is set to be support hibernation
+ * from the Hildon task navigator
+ *
+ * Return value: %TRUE if the program can hibernate, %FALSE otherwise.
  **/
 gboolean
 hildon_program_get_can_hibernate                (HildonProgram *self)
@@ -460,12 +570,18 @@ hildon_program_get_can_hibernate                (HildonProgram *self)
 /**
  * hildon_program_set_common_menu:
  * @self: The #HildonProgram in which the common menu should be used
- * @menu: A GtkMenu to use as common menu for the program
+ * @menu: A #GtkMenu to use as common menu for the program
+ *
+ * Sets a #GtkMenu that will appear in all #HildonWindow<!-- -->s
+ * registered with the #HildonProgram. Only one common #GtkMenu can be
+ * set, further calls will detach the previous common #GtkMenu. A
+ * #HildonWindow can use its own #GtkMenu with
+ * hildon_window_set_menu()
+ *
+ * This method does not support #HildonAppMenu<!-- -->s. See
+ * hildon_program_set_common_app_menu() for that.
  *
- * Sets a GtkMenu that will appear in all the @HildonWindow registered
- * to the #HildonProgram. Only one common GtkMenu can be set, further
- * call will detach the previous common GtkMenu. A @HildonWindow
- * can use it's own GtkMenu with @hildon_window_set_menu
+ * Since: 2.2
  **/
 void
 hildon_program_set_common_menu                  (HildonProgram *self, 
@@ -482,13 +598,13 @@ hildon_program_set_common_menu                  (HildonProgram *self,
     {
         if (GTK_WIDGET_VISIBLE (priv->common_menu))
         {
-            gtk_menu_popdown (GTK_MENU (priv->common_menu));
+            gtk_menu_popdown (priv->common_menu);
             gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->common_menu));
         }
 
-        if (gtk_menu_get_attach_widget (GTK_MENU (priv->common_menu)))
+        if (gtk_menu_get_attach_widget (priv->common_menu))
         {
-            gtk_menu_detach (GTK_MENU (priv->common_menu));
+            gtk_menu_detach (priv->common_menu);
         }
         else
         {
@@ -496,7 +612,7 @@ hildon_program_set_common_menu                  (HildonProgram *self,
         }
     }
 
-    priv->common_menu = GTK_WIDGET (menu);
+    priv->common_menu = menu;
 
     if (priv->common_menu)
     {
@@ -510,8 +626,10 @@ hildon_program_set_common_menu                  (HildonProgram *self,
  * hildon_program_get_common_menu:
  * @self: The #HildonProgram from which to retrieve the common menu
  *
- * Return value: the GtkMenu that was set as common menu for this
- * #HildonProgram, or NULL of no common menu was set.
+ * Returns the #GtkMenu that was set as common menu for this
+ * #HildonProgram.
+ *
+ * Return value: the #GtkMenu or %NULL of no common menu was set.
  **/
 GtkMenu*
 hildon_program_get_common_menu                  (HildonProgram *self)
@@ -523,19 +641,87 @@ hildon_program_get_common_menu                  (HildonProgram *self)
     priv = HILDON_PROGRAM_GET_PRIVATE (self);
     g_assert (priv);
 
-    return GTK_MENU (priv->common_menu);
+    return priv->common_menu;
+}
+
+/**
+ * hildon_program_set_common_app_menu:
+ * @self: The #HildonProgram in which the common menu should be used
+ * @menu: A #HildonAppMenu to use as common menu for the program
+ *
+ * Sets a #HildonAppMenu that will appear in all
+ * #HildonWindow<!-- -->s registered with the #HildonProgram. Only
+ * one common #HildonAppMenu can be set, further calls will detach the
+ * previous common #HildonAppMenu. A #HildonWindow can use its own
+ * #HildonAppMenu with hildon_window_set_app_menu()
+ *
+ * This method does not support #GtkMenu<!-- -->s. See
+ * hildon_program_set_common_menu() for that.
+ *
+ * Since: 2.2
+ **/
+void
+hildon_program_set_common_app_menu              (HildonProgram *self,
+                                                 HildonAppMenu *menu)
+{
+    HildonProgramPrivate *priv;
+    HildonAppMenu *old_menu;
+
+    g_return_if_fail (HILDON_IS_PROGRAM (self));
+    g_return_if_fail (menu == NULL || HILDON_IS_APP_MENU (menu));
+
+    priv = HILDON_PROGRAM_GET_PRIVATE (self);
+    g_assert (priv);
+
+    old_menu = priv->common_app_menu;
+
+    /* Set new menu */
+    priv->common_app_menu = menu;
+    if (menu)
+        g_object_ref_sink (menu);
+
+    /* Hide and unref old menu */
+    if (old_menu) {
+        hildon_app_menu_set_parent_window (old_menu, NULL);
+        g_object_unref (old_menu);
+    }
+}
+
+/**
+ * hildon_program_get_common_app_menu:
+ * @self: The #HildonProgram from which to retrieve the common app menu
+ *
+ * Returns the #HildonAppMenu that was set as common menu for this
+ * #HildonProgram.
+ *
+ * Return value: the #HildonAppMenu or %NULL of no common app menu was
+ * set.
+ *
+ * Since: 2.2
+ **/
+HildonAppMenu*
+hildon_program_get_common_app_menu              (HildonProgram *self)
+{
+    HildonProgramPrivate *priv;
+
+    g_return_val_if_fail (HILDON_IS_PROGRAM (self), NULL);
+
+    priv = HILDON_PROGRAM_GET_PRIVATE (self);
+    g_assert (priv);
+
+    return priv->common_app_menu;
 }
 
 /**
  * hildon_program_set_common_toolbar:
  * @self: The #HildonProgram in which the common toolbar should be used
- * @toolbar: A GtkToolbar to use as common toolbar for the program
+ * @toolbar: A #GtkToolbar to use as common toolbar for the program
  *
- * Sets a GtkToolbar that will appear in all the @HildonWindow registered
- * to the #HildonProgram. Only one common GtkToolbar can be set, further
- * call will detach the previous common GtkToolbar. A @HildonWindow
- * can use its own GtkToolbar with @hildon_window_set_toolbar. Both
- * #HildonProgram and @HildonWindow specific toolbars will be shown
+ * Sets a #GtkToolbar that will appear in all the #HildonWindow registered
+ * to the #HildonProgram. Only one common #GtkToolbar can be set, further
+ * call will detach the previous common #GtkToolbar. A #HildonWindow
+ * can use its own #GtkToolbar with hildon_window_add_toolbar(). Both
+ * #HildonProgram and #HildonWindow specific toolbars will be shown
  **/
 void
 hildon_program_set_common_toolbar               (HildonProgram *self, 
@@ -580,8 +766,11 @@ hildon_program_set_common_toolbar               (HildonProgram *self,
  * hildon_program_get_common_toolbar:
  * @self: The #HildonProgram from which to retrieve the common toolbar
  *
- * Return value: the GtkToolbar that was set as common toolbar for this
- * #HildonProgram, or NULL of no common menu was set.
+ * Returns the #GtkToolbar that was set as common toolbar for this
+ * #HildonProgram.
+ *
+ * Return value: the #GtkToolbar or %NULL of no common toolbar was
+ * set.
  **/
 GtkToolbar*
 hildon_program_get_common_toolbar               (HildonProgram *self)
@@ -600,8 +789,11 @@ hildon_program_get_common_toolbar               (HildonProgram *self)
  * hildon_program_get_is_topmost:
  * @self: A #HildonWindow
  *
- * Return value: Whether or not one of the program's window or dialog is 
- * currenltly activated by the window manager.
+ * Returns whether one of the program's windows or dialogs is
+ * currently activated by the window manager.
+ *
+ * Return value: %TRUE if a window or dialog is topmost, %FALSE
+ * otherwise.
  **/
 gboolean
 hildon_program_get_is_topmost                   (HildonProgram *self)
@@ -615,3 +807,22 @@ hildon_program_get_is_topmost                   (HildonProgram *self)
 
     return priv->is_topmost;
 }
+
+/**
+ * hildon_program_go_to_root_window:
+ * @self: A #HildonProgram
+ *
+ * Goes to the root window of the stack.
+ *
+ * Deprecated: See #HildonWindowStack
+ */
+void
+hildon_program_go_to_root_window                (HildonProgram *self)
+{
+    HildonWindowStack *stack = hildon_window_stack_get_default ();
+    gint n = hildon_window_stack_size (stack);
+    g_warning ("%s: this function is deprecated. Use hildon_window_stack_pop() instead.", __FUNCTION__);
+    if (n > 1) {
+        hildon_window_stack_pop (stack, n-1, NULL);
+    }
+}