2 * This file is a part of hildon
4 * Copyright (C) 2006 Nokia Corporation, all rights reserved.
6 * Contact: Rodrigo Novo <rodrigo.novo@nokia.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; version 2.1 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
26 * SECTION:hildon-program
27 * @short_description: An object that represents an application running in the Hildon framework.
28 * @see_also: #HildonWindow, #HildonStackableWindow
30 * #HildonProgram is an object used to represent an application running
31 * in the Hildon framework.
33 * Applications can have one or more #HildonWindow<!-- -->s. These
34 * can be registered in the #HildonProgram with hildon_program_add_window(),
35 * and can be unregistered similarly with hildon_program_remove_window().
37 * #HildonProgram provides the programmer with commodities such
38 * as applying a common toolbar and menu to all registered
39 * #HildonWindow<!-- -->s. This is done with hildon_program_set_common_menu(),
40 * hildon_program_set_common_app_menu() and hildon_program_set_common_toolbar().
42 * #HildonProgram is also used to apply program-wide properties that
43 * are specific to the Hildon framework. For instance
44 * hildon_program_set_can_hibernate() sets whether or not an application
45 * can be set to hibernate by the Hildon task navigator, in situations of
50 * HildonProgram *program;
51 * HildonWindow *window1;
52 * HildonWindow *window2;
53 * GtkToolbar *common_toolbar, *window_specific_toolbar;
54 * HildonAppMenu *menu;
56 * program = HILDON_PROGRAM (hildon_program_get_instance ());
58 * window1 = HILDON_WINDOW (hildon_window_new ());
59 * window2 = HILDON_WINDOW (hildon_window_new ());
61 * common_toolbar = create_common_toolbar ();
62 * window_specific_toolbar = create_window_specific_toolbar ();
64 * menu = create_menu ();
66 * hildon_program_add_window (program, window1);
67 * hildon_program_add_window (program, window2);
69 * hildon_program_set_common_app_menu (program, menu);
71 * hildon_program_set_common_toolbar (program, common_toolbar);
72 * hildon_window_add_toolbar (window1, window_specific_toolbar);
74 * hildon_program_set_can_hibernate (program, TRUE);
79 #undef HILDON_DISABLE_DEPRECATED
85 #include <X11/Xatom.h>
87 #include "hildon-program.h"
88 #include "hildon-program-private.h"
89 #include "hildon-window-private.h"
90 #include "hildon-window-stack.h"
91 #include "hildon-app-menu-private.h"
94 hildon_program_init (HildonProgram *self);
97 hildon_program_finalize (GObject *self);
100 hildon_program_class_init (HildonProgramClass *self);
103 hildon_program_get_property (GObject *object,
108 hildon_program_set_property (GObject *object,
121 hildon_program_get_type (void)
123 static GType program_type = 0;
127 static const GTypeInfo program_info =
129 sizeof (HildonProgramClass),
130 NULL, /* base_init */
131 NULL, /* base_finalize */
132 (GClassInitFunc) hildon_program_class_init,
133 NULL, /* class_finalize */
134 NULL, /* class_data */
135 sizeof (HildonProgram),
137 (GInstanceInitFunc) hildon_program_init,
139 program_type = g_type_register_static(G_TYPE_OBJECT,
140 "HildonProgram", &program_info, 0);
146 hildon_program_init (HildonProgram *self)
148 HildonProgramPrivate *priv = HILDON_PROGRAM_GET_PRIVATE (self);
151 priv->killable = FALSE;
152 priv->window_count = 0;
153 priv->is_topmost = FALSE;
154 priv->window_group = GDK_WINDOW_XID (gdk_display_get_default_group (gdk_display_get_default()));
155 priv->common_menu = NULL;
156 priv->common_app_menu = NULL;
157 priv->common_toolbar = NULL;
158 priv->windows = NULL;
162 hildon_program_finalize (GObject *self)
164 HildonProgramPrivate *priv = HILDON_PROGRAM_GET_PRIVATE (HILDON_PROGRAM (self));
167 if (priv->common_toolbar)
169 g_object_unref (priv->common_toolbar);
170 priv->common_toolbar = NULL;
173 if (priv->common_menu)
175 g_object_unref (priv->common_menu);
176 priv->common_menu = NULL;
181 hildon_program_class_init (HildonProgramClass *self)
183 GObjectClass *object_class = G_OBJECT_CLASS (self);
185 g_type_class_add_private (self, sizeof (HildonProgramPrivate));
187 /* Set up object virtual functions */
188 object_class->finalize = hildon_program_finalize;
189 object_class->set_property = hildon_program_set_property;
190 object_class->get_property = hildon_program_get_property;
192 /* Install properties */
195 * HildonProgram:is-topmost:
197 * Whether one of the program's window or dialog currently
198 * is activated by window manager.
200 g_object_class_install_property (object_class, PROP_IS_TOPMOST,
201 g_param_spec_boolean ("is-topmost",
203 "Whether one of the program's window or dialog currently "
204 "is activated by window manager",
209 * HildonProgram:can-hibernate:
211 * Whether the program should be set to hibernate by the Task
212 * Navigator in low memory situation.
214 g_object_class_install_property (object_class, PROP_KILLABLE,
215 g_param_spec_boolean ("can-hibernate",
217 "Whether the program should be set to hibernate by the Task "
218 "Navigator in low memory situation",
225 hildon_program_set_property (GObject *object,
230 switch (property_id) {
233 hildon_program_set_can_hibernate (HILDON_PROGRAM (object), g_value_get_boolean (value));
237 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
244 hildon_program_get_property (GObject *object,
249 HildonProgramPrivate *priv = HILDON_PROGRAM_GET_PRIVATE (object);
255 g_value_set_boolean (value, priv->killable);
258 case PROP_IS_TOPMOST:
259 g_value_set_boolean (value, priv->is_topmost);
263 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
269 * hildon_program_pop_window_stack:
270 * @self: A #HildonProgram
272 * Pops a window from the stack.
274 * Deprecated: Use hildon_window_stack_pop() instead
276 * Returns: A #HildonStackableWindow, or %NULL
278 HildonStackableWindow *
279 hildon_program_pop_window_stack (HildonProgram *self)
281 HildonWindowStack *stack = hildon_window_stack_get_default ();
282 GtkWidget *win = hildon_window_stack_pop_1 (stack);
283 g_warning ("%s: this function is deprecated. Use hildon_window_stack_pop() instead", __FUNCTION__);
284 return win ? HILDON_STACKABLE_WINDOW (win) : NULL;
288 * hildon_program_peek_window_stack:
289 * @self: A #HildonProgram
291 * Deprecated: Use hildon_window_stack_peek() instead
293 * Returns: A #HildonStackableWindow, or %NULL
295 HildonStackableWindow *
296 hildon_program_peek_window_stack (HildonProgram *self)
298 HildonWindowStack *stack = hildon_window_stack_get_default ();
299 GtkWidget *win = hildon_window_stack_peek (stack);
300 g_warning ("%s: this function is deprecated. Use hildon_window_stack_peek() instead", __FUNCTION__);
301 return win ? HILDON_STACKABLE_WINDOW (win) : NULL;
306 hildon_program_window_list_compare (gconstpointer window_a,
307 gconstpointer window_b)
309 g_return_val_if_fail (HILDON_IS_WINDOW(window_a) &&
310 HILDON_IS_WINDOW(window_b), 1);
312 return window_a != window_b;
316 * foreach function, checks if a window is topmost and acts consequently
319 hildon_program_window_list_is_is_topmost (gpointer data,
322 if (data && HILDON_IS_WINDOW (data))
324 HildonWindow *window = HILDON_WINDOW (data);
325 Window window_id = * (Window*)window_id_;
327 hildon_window_update_topmost (window, window_id);
332 * Check the _MB_CURRENT_APP_WINDOW on the root window, and update
333 * the top_most status accordingly
336 hildon_program_update_top_most (HildonProgram *program)
339 Window active_window;
340 HildonProgramPrivate *priv;
342 priv = HILDON_PROGRAM_GET_PRIVATE (program);
345 active_window = hildon_window_get_active_window();
353 gdk_error_trap_push ();
354 wm_hints = XGetWMHints (GDK_DISPLAY (), active_window);
355 xerror = gdk_error_trap_pop ();
361 is_topmost = (wm_hints->window_group == priv->window_group);
366 /* Send notification if is_topmost has changed */
367 if (!priv->is_topmost != !is_topmost)
369 priv->is_topmost = is_topmost;
370 g_object_notify (G_OBJECT (program), "is-topmost");
373 /* Check each window if it was is_topmost */
374 g_slist_foreach (priv->windows,
375 (GFunc)hildon_program_window_list_is_is_topmost, &active_window);
379 * We keep track of the _MB_CURRENT_APP_WINDOW property on the root window,
380 * to detect when a window belonging to this program was is_topmost. This
381 * is based on the window group WM hint.
383 static GdkFilterReturn
384 hildon_program_root_window_event_filter (GdkXEvent *xevent,
388 XAnyEvent *eventti = xevent;
389 HildonProgram *program = HILDON_PROGRAM (data);
390 Atom active_app_atom =
391 XInternAtom (GDK_DISPLAY (), "_MB_CURRENT_APP_WINDOW", False);
393 if (eventti->type == PropertyNotify)
395 XPropertyEvent *pevent = xevent;
397 if (pevent->atom == active_app_atom)
399 hildon_program_update_top_most (program);
403 return GDK_FILTER_CONTINUE;
407 * Checks if the window is the topmost window of the program and in
408 * that case forces the window to take the common toolbar.
411 hildon_program_common_toolbar_topmost_window (gpointer window,
414 if (HILDON_IS_WINDOW (window) && hildon_window_get_is_topmost (HILDON_WINDOW (window)))
415 hildon_window_take_common_toolbar (HILDON_WINDOW (window));
419 * hildon_program_get_instance:
421 * Returns the #HildonProgram for the current process. The object is
422 * created on the first call. Note that you're not supposed to unref
423 * the returned object since it's not reffed in the first place.
425 * Return value: the #HildonProgram.
428 hildon_program_get_instance (void)
430 static HildonProgram *program = NULL;
434 program = g_object_new (HILDON_TYPE_PROGRAM, NULL);
441 * hildon_program_add_window:
442 * @self: The #HildonProgram to which the window should be registered
443 * @window: A #HildonWindow to be added
445 * Registers a #HildonWindow as belonging to a given #HildonProgram. This
446 * allows to apply program-wide settings as all the registered windows,
447 * such as hildon_program_set_common_menu(), hildon_program_set_common_app_menu()
448 * and hildon_program_set_common_toolbar().
451 hildon_program_add_window (HildonProgram *self,
452 HildonWindow *window)
454 HildonProgramPrivate *priv;
456 g_return_if_fail (HILDON_IS_PROGRAM (self));
457 g_return_if_fail (HILDON_IS_WINDOW (window));
459 priv = HILDON_PROGRAM_GET_PRIVATE (self);
462 if (g_slist_find_custom (priv->windows, window,
463 hildon_program_window_list_compare) )
465 /* We already have that window */
469 if (!priv->window_count)
471 hildon_program_update_top_most (self);
473 /* Now that we have a window we should start keeping track of
475 gdk_window_set_events (gdk_get_default_root_window (),
476 gdk_window_get_events (gdk_get_default_root_window ()) | GDK_PROPERTY_CHANGE_MASK);
478 gdk_window_add_filter (gdk_get_default_root_window (),
479 hildon_program_root_window_event_filter, self );
482 hildon_window_set_can_hibernate_property (window, &priv->killable);
484 hildon_window_set_program (window, G_OBJECT (self));
486 priv->windows = g_slist_append (priv->windows, window);
487 priv->window_count ++;
491 * hildon_program_remove_window:
492 * @self: The #HildonProgram to which the window should be unregistered
493 * @window: The #HildonWindow to unregister
495 * Used to unregister a window from the program. Subsequent calls to
496 * hildon_program_set_common_menu(), hildon_program_set_common_app_menu()
497 * and hildon_program_set_common_toolbar() will not affect the window.
500 hildon_program_remove_window (HildonProgram *self,
501 HildonWindow *window)
503 HildonProgramPrivate *priv;
505 g_return_if_fail (HILDON_IS_PROGRAM (self));
506 g_return_if_fail (HILDON_IS_WINDOW (window));
508 priv = HILDON_PROGRAM_GET_PRIVATE (self);
511 g_return_if_fail (g_slist_find (priv->windows, window));
513 hildon_window_unset_program (window);
515 priv->windows = g_slist_remove (priv->windows, window);
517 priv->window_count --;
519 if (! priv->window_count)
520 gdk_window_remove_filter (gdk_get_default_root_window(),
521 hildon_program_root_window_event_filter,
526 * hildon_program_set_can_hibernate:
527 * @self: The #HildonProgram which can hibernate or not
528 * @can_hibernate: whether or not the #HildonProgram can hibernate
530 * Used to set whether or not the Hildon task navigator should
531 * be able to set the program to hibernation in case of low memory
534 hildon_program_set_can_hibernate (HildonProgram *self,
535 gboolean can_hibernate)
537 HildonProgramPrivate *priv;
539 g_return_if_fail (HILDON_IS_PROGRAM (self));
541 priv = HILDON_PROGRAM_GET_PRIVATE (self);
544 if (priv->killable != can_hibernate)
546 g_slist_foreach (priv->windows,
547 (GFunc) hildon_window_set_can_hibernate_property, &can_hibernate);
550 priv->killable = can_hibernate;
554 * hildon_program_get_can_hibernate:
555 * @self: The #HildonProgram which can hibernate or not
557 * Returns whether the #HildonProgram is set to be support hibernation
558 * from the Hildon task navigator
560 * Return value: %TRUE if the program can hibernate, %FALSE otherwise.
563 hildon_program_get_can_hibernate (HildonProgram *self)
565 HildonProgramPrivate *priv;
567 g_return_val_if_fail (HILDON_IS_PROGRAM (self), FALSE);
569 priv = HILDON_PROGRAM_GET_PRIVATE (self);
572 return priv->killable;
576 * hildon_program_set_common_menu:
577 * @self: The #HildonProgram in which the common menu should be used
578 * @menu: A #GtkMenu to use as common menu for the program
580 * Sets a #GtkMenu that will appear in all #HildonWindow<!-- -->s
581 * registered with the #HildonProgram. Only one common #GtkMenu can be
582 * set, further calls will detach the previous common #GtkMenu. A
583 * #HildonWindow can use its own #GtkMenu with
584 * hildon_window_set_menu()
586 * This method does not support #HildonAppMenu<!-- -->s. See
587 * hildon_program_set_common_app_menu() for that.
592 hildon_program_set_common_menu (HildonProgram *self,
595 HildonProgramPrivate *priv;
597 g_return_if_fail (HILDON_IS_PROGRAM (self));
599 priv = HILDON_PROGRAM_GET_PRIVATE (self);
602 if (priv->common_menu)
604 if (GTK_WIDGET_VISIBLE (priv->common_menu))
606 gtk_menu_popdown (priv->common_menu);
607 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->common_menu));
610 if (gtk_menu_get_attach_widget (priv->common_menu))
612 gtk_menu_detach (priv->common_menu);
616 g_object_unref (priv->common_menu);
620 priv->common_menu = menu;
622 if (priv->common_menu)
625 gtk_object_sink (GTK_OBJECT (menu));
626 gtk_widget_show_all (GTK_WIDGET (menu));
631 * hildon_program_get_common_menu:
632 * @self: The #HildonProgram from which to retrieve the common menu
634 * Returns the #GtkMenu that was set as common menu for this
637 * Return value: the #GtkMenu or %NULL of no common menu was set.
640 hildon_program_get_common_menu (HildonProgram *self)
642 HildonProgramPrivate *priv;
644 g_return_val_if_fail (HILDON_IS_PROGRAM (self), NULL);
646 priv = HILDON_PROGRAM_GET_PRIVATE (self);
649 return priv->common_menu;
653 * hildon_program_set_common_app_menu:
654 * @self: The #HildonProgram in which the common menu should be used
655 * @menu: A #HildonAppMenu to use as common menu for the program
657 * Sets a #HildonAppMenu that will appear in all
658 * #HildonWindow<!-- -->s registered with the #HildonProgram. Only
659 * one common #HildonAppMenu can be set, further calls will detach the
660 * previous common #HildonAppMenu. A #HildonWindow can use its own
661 * #HildonAppMenu with hildon_window_set_app_menu()
663 * This method does not support #GtkMenu<!-- -->s. See
664 * hildon_program_set_common_menu() for that.
669 hildon_program_set_common_app_menu (HildonProgram *self,
672 HildonProgramPrivate *priv;
673 HildonAppMenu *old_menu;
675 g_return_if_fail (HILDON_IS_PROGRAM (self));
676 g_return_if_fail (menu == NULL || HILDON_IS_APP_MENU (menu));
678 priv = HILDON_PROGRAM_GET_PRIVATE (self);
681 old_menu = priv->common_app_menu;
684 priv->common_app_menu = menu;
686 g_object_ref_sink (menu);
688 /* Hide and unref old menu */
690 hildon_app_menu_set_parent_window (old_menu, NULL);
691 g_object_unref (old_menu);
696 * hildon_program_get_common_app_menu:
697 * @self: The #HildonProgram from which to retrieve the common app menu
699 * Returns the #HildonAppMenu that was set as common menu for this
702 * Return value: the #HildonAppMenu or %NULL of no common app menu was
708 hildon_program_get_common_app_menu (HildonProgram *self)
710 HildonProgramPrivate *priv;
712 g_return_val_if_fail (HILDON_IS_PROGRAM (self), NULL);
714 priv = HILDON_PROGRAM_GET_PRIVATE (self);
717 return priv->common_app_menu;
721 * hildon_program_set_common_toolbar:
722 * @self: The #HildonProgram in which the common toolbar should be used
723 * @toolbar: A #GtkToolbar to use as common toolbar for the program
725 * Sets a #GtkToolbar that will appear in all the #HildonWindow registered
726 * to the #HildonProgram. Only one common #GtkToolbar can be set, further
727 * call will detach the previous common #GtkToolbar. A #HildonWindow
728 * can use its own #GtkToolbar with hildon_window_add_toolbar(). Both
729 * #HildonProgram and #HildonWindow specific toolbars will be shown
732 hildon_program_set_common_toolbar (HildonProgram *self,
735 HildonProgramPrivate *priv;
737 g_return_if_fail (HILDON_IS_PROGRAM (self));
739 priv = HILDON_PROGRAM_GET_PRIVATE (self);
742 if (priv->common_toolbar)
744 if (priv->common_toolbar->parent)
746 gtk_container_remove (GTK_CONTAINER (priv->common_toolbar->parent),
747 priv->common_toolbar);
750 g_object_unref (priv->common_toolbar);
753 priv->common_toolbar = GTK_WIDGET (toolbar);
755 if (priv->common_toolbar)
757 g_object_ref (priv->common_toolbar);
758 gtk_object_sink (GTK_OBJECT (priv->common_toolbar) );
761 /* if the program is the topmost we have to update the common
762 toolbar right now for the topmost window */
763 if (priv->is_topmost)
765 g_slist_foreach (priv->windows,
766 (GFunc) hildon_program_common_toolbar_topmost_window, NULL);
771 * hildon_program_get_common_toolbar:
772 * @self: The #HildonProgram from which to retrieve the common toolbar
774 * Returns the #GtkToolbar that was set as common toolbar for this
777 * Return value: the #GtkToolbar or %NULL of no common toolbar was
781 hildon_program_get_common_toolbar (HildonProgram *self)
783 HildonProgramPrivate *priv;
785 g_return_val_if_fail (HILDON_IS_PROGRAM (self), NULL);
787 priv = HILDON_PROGRAM_GET_PRIVATE (self);
790 return priv->common_toolbar ? GTK_TOOLBAR (priv->common_toolbar) : NULL;
794 * hildon_program_get_is_topmost:
795 * @self: A #HildonWindow
797 * Returns whether one of the program's windows or dialogs is
798 * currently activated by the window manager.
800 * Return value: %TRUE if a window or dialog is topmost, %FALSE
804 hildon_program_get_is_topmost (HildonProgram *self)
806 HildonProgramPrivate *priv;
808 g_return_val_if_fail (HILDON_IS_PROGRAM (self), FALSE);
810 priv = HILDON_PROGRAM_GET_PRIVATE (self);
813 return priv->is_topmost;
817 * hildon_program_go_to_root_window:
818 * @self: A #HildonProgram
820 * Goes to the root window of the stack.
822 * Deprecated: See #HildonWindowStack
825 hildon_program_go_to_root_window (HildonProgram *self)
827 HildonWindowStack *stack = hildon_window_stack_get_default ();
828 gint n = hildon_window_stack_size (stack);
829 g_warning ("%s: this function is deprecated. Use hildon_window_stack_pop() instead.", __FUNCTION__);
831 hildon_window_stack_pop (stack, n-1, NULL);