2 * This file is a part of hildon
4 * Copyright (C) 2006 Nokia Corporation, all rights reserved.
6 * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@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
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.
29 * The HildonProgram is an object used to represent an application running
30 * in the Hildon framework.
32 * Such an application is thought to have one or more HildonWindow. These
33 * shall be registered to the HildonProgram with hildon_program_add_window,
34 * and can be unregistered similarly with hildon_program_remove_window.
36 * The HildonProgram provides the programmer with commodities such
37 * as applying a common toolbar and menu to all the HildonWindow
38 * registered to it. This is done with hildon_program_set_common_menu()
39 * and hildon_program_set_common_toolbar().
41 * The HildonProgram is also used to apply program-wide properties that
42 * are specific to the Hildon framework. For instance
43 * hildon_program_set_can_hibernate() sets whether or not an application
44 * can be set to hibernate by the Hildon task navigator, in situations of
49 * HildonProgram *program;
50 * HildonWindow *first_window;
51 * HildonWindow *second_window;
52 * GtkToolbar *common_toolbar, *window_specific_toolbar;
55 * program = HILDON_PROGRAM (hildon_program_new ());
57 * window1 = HILDON_WINDOW (hildon_window_new ());
58 * window2 = HILDON_WINDOW (hildon_window_new ());
60 * common_toolbar = create_common_toolbar ();
61 * window_specific_toolbar = create_window_specific_toolbar ();
63 * menu = create_menu ();
65 * hildon_program_add (program, window1);
66 * hildon_program_add (program, window2);
68 * hildon_program_set_common_menu (program, menu);
70 * hildon_program_set_common_toolbar (program, common_toolbar);
71 * hildon_window_add_toolbar (first_window, window_specific_toolbar);
73 * hildon_program_set_can_hibernate (program, TRUE);
82 #include "hildon-program.h"
83 #include "hildon-program-private.h"
84 #include "hildon-window-private.h"
85 #include <X11/Xatom.h>
88 hildon_program_init (HildonProgram *self);
91 hildon_program_finalize (GObject *self);
94 hildon_program_class_init (HildonProgramClass *self);
97 hildon_program_get_property (GObject *object,
102 hildon_program_set_property (GObject *object,
115 hildon_program_get_type (void)
117 static GType program_type = 0;
121 static const GTypeInfo program_info =
123 sizeof (HildonProgramClass),
124 NULL, /* base_init */
125 NULL, /* base_finalize */
126 (GClassInitFunc) hildon_program_class_init,
127 NULL, /* class_finalize */
128 NULL, /* class_data */
129 sizeof (HildonProgram),
131 (GInstanceInitFunc) hildon_program_init,
133 program_type = g_type_register_static(G_TYPE_OBJECT,
134 "HildonProgram", &program_info, 0);
140 hildon_program_init (HildonProgram *self)
142 HildonProgramPrivate *priv = HILDON_PROGRAM_GET_PRIVATE (self);
145 priv->killable = FALSE;
146 priv->window_count = 0;
147 priv->is_topmost = FALSE;
148 priv->window_group = GDK_WINDOW_XID (gdk_display_get_default_group (gdk_display_get_default()));
149 priv->common_toolbar = NULL;
154 hildon_program_finalize (GObject *self)
156 HildonProgramPrivate *priv = HILDON_PROGRAM_GET_PRIVATE (HILDON_PROGRAM (self));
159 if (priv->common_toolbar)
161 g_object_unref (priv->common_toolbar);
162 priv->common_toolbar = NULL;
165 if (priv->common_menu)
167 g_object_unref (priv->common_menu);
168 priv->common_menu = NULL;
175 hildon_program_class_init (HildonProgramClass *self)
177 GObjectClass *object_class = G_OBJECT_CLASS (self);
179 g_type_class_add_private (self, sizeof (HildonProgramPrivate));
181 /* Set up object virtual functions */
182 object_class->finalize = hildon_program_finalize;
183 object_class->set_property = hildon_program_set_property;
184 object_class->get_property = hildon_program_get_property;
186 /* Install properties */
189 * HildonProgram:is-topmost:
191 * Whether one of the program's window or dialog currently
192 * is activated by window manager.
194 g_object_class_install_property (object_class, PROP_IS_TOPMOST,
195 g_param_spec_boolean ("is-topmost",
197 "Whether one of the program's window or dialog currently "
198 "is activated by window manager",
203 * HildonProgram:can-hibernate:
205 * Whether the program should be set to hibernate by the Task
206 * Navigator in low memory situation.
208 g_object_class_install_property (object_class, PROP_KILLABLE,
209 g_param_spec_boolean ("can-hibernate",
211 "Whether the program should be set to hibernate by the Task "
212 "Navigator in low memory situation",
219 hildon_program_set_property (GObject *object,
224 switch (property_id) {
227 hildon_program_set_can_hibernate (HILDON_PROGRAM (object), g_value_get_boolean (value));
231 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
238 hildon_program_get_property (GObject *object,
243 HildonProgramPrivate *priv = HILDON_PROGRAM_GET_PRIVATE (object);
249 g_value_set_boolean (value, priv->killable);
252 case PROP_IS_TOPMOST:
253 g_value_set_boolean (value, priv->is_topmost);
257 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
264 hildon_program_window_list_compare (gconstpointer window_a,
265 gconstpointer window_b)
267 g_return_val_if_fail (HILDON_IS_WINDOW(window_a) &&
268 HILDON_IS_WINDOW(window_b), 1);
270 return window_a != window_b;
274 * foreach function, checks if a window is topmost and acts consequently
277 hildon_program_window_list_is_is_topmost (gpointer data,
280 if (data && HILDON_IS_WINDOW (data))
282 HildonWindow *window = HILDON_WINDOW (data);
283 Window window_id = * (Window*)window_id_;
285 hildon_window_update_topmost (window, window_id);
290 * Check the _MB_CURRENT_APP_WINDOW on the root window, and update
291 * the top_most status accordingly
294 hildon_program_update_top_most (HildonProgram *program)
297 Window active_window;
298 HildonProgramPrivate *priv;
300 priv = HILDON_PROGRAM_GET_PRIVATE (program);
303 active_window = hildon_window_get_active_window();
307 wm_hints = XGetWMHints (GDK_DISPLAY (), active_window);
312 if (wm_hints->window_group == priv->window_group)
314 if (!priv->is_topmost)
316 priv->is_topmost = TRUE;
317 g_object_notify (G_OBJECT (program), "is-topmost");
320 else if (priv->is_topmost)
322 priv->is_topmost = FALSE;
323 g_object_notify (G_OBJECT (program), "is-topmost");
329 /* Check each window if it was is_topmost */
330 g_slist_foreach (priv->windows,
331 (GFunc)hildon_program_window_list_is_is_topmost, &active_window);
335 * We keep track of the _MB_CURRENT_APP_WINDOW property on the root window,
336 * to detect when a window belonging to this program was is_topmost. This
337 * is based on the window group WM hint.
339 static GdkFilterReturn
340 hildon_program_root_window_event_filter (GdkXEvent *xevent,
344 XAnyEvent *eventti = xevent;
345 HildonProgram *program = HILDON_PROGRAM (data);
346 Atom active_app_atom =
347 XInternAtom (GDK_DISPLAY (), "_MB_CURRENT_APP_WINDOW", False);
349 if (eventti->type == PropertyNotify)
351 XPropertyEvent *pevent = xevent;
353 if (pevent->atom == active_app_atom)
355 hildon_program_update_top_most (program);
359 return GDK_FILTER_CONTINUE;
363 * Checks if the window is the topmost window of the program and in
364 * that case forces the window to take the common toolbar.
367 hildon_program_common_toolbar_topmost_window (gpointer window,
370 if (HILDON_IS_WINDOW (window) && hildon_window_get_is_topmost (HILDON_WINDOW (window)))
371 hildon_window_take_common_toolbar (HILDON_WINDOW (window));
375 * hildon_program_get_instance:
377 * Return value: Returns the #HildonProgram for the current process.
378 * The object is created on the first call.
381 hildon_program_get_instance (void)
383 static HildonProgram *program = NULL;
387 program = g_object_new (HILDON_TYPE_PROGRAM, NULL);
394 * hildon_program_add_window:
395 * @self: The @HildonProgram to which the window should be registered
396 * @window: A @HildonWindow to be added
398 * Registers a @HildonWindow as belonging to a given @HildonProgram. This
399 * allows to apply program-wide settings as all the registered windows,
400 * such as hildon_program_set_common_menu() and
401 * hildon_pogram_set_common_toolbar()
404 hildon_program_add_window (HildonProgram *self,
405 HildonWindow *window)
407 HildonProgramPrivate *priv;
409 g_return_if_fail (HILDON_IS_PROGRAM (self));
411 priv = HILDON_PROGRAM_GET_PRIVATE (self);
414 if (g_slist_find_custom (priv->windows, window,
415 hildon_program_window_list_compare) )
417 /* We already have that window */
421 if (!priv->window_count)
423 hildon_program_update_top_most (self);
425 /* Now that we have a window we should start keeping track of
427 gdk_window_set_events (gdk_get_default_root_window (),
428 gdk_window_get_events (gdk_get_default_root_window ()) | GDK_PROPERTY_CHANGE_MASK);
430 gdk_window_add_filter (gdk_get_default_root_window (),
431 hildon_program_root_window_event_filter, self );
434 hildon_window_set_can_hibernate_property (window, &priv->killable);
436 hildon_window_set_program (window, G_OBJECT (self));
438 priv->windows = g_slist_append (priv->windows, window);
439 priv->window_count ++;
443 * hildon_program_remove_window:
444 * @self: The #HildonProgram to which the window should be unregistered
445 * @window: The @HildonWindow to unregister
447 * Used to unregister a window from the program. Subsequent calls to
448 * hildon_program_set_common_menu() and hildon_pogram_set_common_toolbar()
449 * will not affect the window
452 hildon_program_remove_window (HildonProgram *self,
453 HildonWindow *window)
455 HildonProgramPrivate *priv;
457 g_return_if_fail (HILDON_IS_PROGRAM (self));
459 priv = HILDON_PROGRAM_GET_PRIVATE (self);
462 hildon_window_unset_program (window);
464 priv->windows = g_slist_remove (priv->windows, window);
466 priv->window_count --;
468 if (! priv->window_count)
469 gdk_window_remove_filter (gdk_get_default_root_window(),
470 hildon_program_root_window_event_filter,
475 * hildon_program_set_can_hibernate:
476 * @self: The #HildonProgram which can hibernate or not
477 * @can_hibernate: whether or not the #HildonProgram can hibernate
479 * Used to set whether or not the Hildon task navigator should
480 * be able to set the program to hibernation in case of low memory
483 hildon_program_set_can_hibernate (HildonProgram *self,
484 gboolean can_hibernate)
486 HildonProgramPrivate *priv;
488 g_return_if_fail (HILDON_IS_PROGRAM (self));
490 priv = HILDON_PROGRAM_GET_PRIVATE (self);
493 if (priv->killable != can_hibernate)
495 g_slist_foreach (priv->windows,
496 (GFunc) hildon_window_set_can_hibernate_property, &can_hibernate);
499 priv->killable = can_hibernate;
503 * hildon_program_get_can_hibernate:
504 * @self: The #HildonProgram which can hibernate or not
506 * Return value: Whether or not this #HildonProgram is set to be
507 * support hibernation from the Hildon task navigator
510 hildon_program_get_can_hibernate (HildonProgram *self)
512 HildonProgramPrivate *priv;
514 g_return_val_if_fail (HILDON_IS_PROGRAM (self), FALSE);
516 priv = HILDON_PROGRAM_GET_PRIVATE (self);
519 return priv->killable;
523 * hildon_program_set_common_menu:
524 * @self: The #HildonProgram in which the common menu should be used
525 * @menu: A GtkMenu to use as common menu for the program
527 * Sets a GtkMenu that will appear in all the @HildonWindow registered
528 * to the #HildonProgram. Only one common GtkMenu can be set, further
529 * call will detach the previous common GtkMenu. A @HildonWindow
530 * can use it's own GtkMenu with @hildon_window_set_menu
533 hildon_program_set_common_menu (HildonProgram *self,
536 HildonProgramPrivate *priv;
538 g_return_if_fail (HILDON_IS_PROGRAM (self));
540 priv = HILDON_PROGRAM_GET_PRIVATE (self);
543 if (priv->common_menu)
545 if (GTK_WIDGET_VISIBLE (priv->common_menu))
547 gtk_menu_popdown (GTK_MENU (priv->common_menu));
548 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->common_menu));
551 if (gtk_menu_get_attach_widget (GTK_MENU (priv->common_menu)))
553 gtk_menu_detach (GTK_MENU (priv->common_menu));
557 g_object_unref (priv->common_menu);
561 priv->common_menu = GTK_WIDGET (menu);
563 if (priv->common_menu)
566 gtk_object_sink (GTK_OBJECT (menu));
567 gtk_widget_show_all (GTK_WIDGET (menu));
572 * hildon_program_get_common_menu:
573 * @self: The #HildonProgram from which to retrieve the common menu
575 * Return value: the GtkMenu that was set as common menu for this
576 * #HildonProgram, or NULL of no common menu was set.
579 hildon_program_get_common_menu (HildonProgram *self)
581 HildonProgramPrivate *priv;
583 g_return_val_if_fail (HILDON_IS_PROGRAM (self), NULL);
585 priv = HILDON_PROGRAM_GET_PRIVATE (self);
588 return GTK_MENU (priv->common_menu);
592 * hildon_program_set_common_toolbar:
593 * @self: The #HildonProgram in which the common toolbar should be used
594 * @toolbar: A GtkToolbar to use as common toolbar for the program
596 * Sets a GtkToolbar that will appear in all the @HildonWindow registered
597 * to the #HildonProgram. Only one common GtkToolbar can be set, further
598 * call will detach the previous common GtkToolbar. A @HildonWindow
599 * can use its own GtkToolbar with @hildon_window_set_toolbar. Both
600 * #HildonProgram and @HildonWindow specific toolbars will be shown
603 hildon_program_set_common_toolbar (HildonProgram *self,
606 HildonProgramPrivate *priv;
608 g_return_if_fail (HILDON_IS_PROGRAM (self));
610 priv = HILDON_PROGRAM_GET_PRIVATE (self);
613 if (priv->common_toolbar)
615 if (priv->common_toolbar->parent)
617 gtk_container_remove (GTK_CONTAINER (priv->common_toolbar->parent),
618 priv->common_toolbar);
621 g_object_unref (priv->common_toolbar);
624 priv->common_toolbar = GTK_WIDGET (toolbar);
626 if (priv->common_toolbar)
628 g_object_ref (priv->common_toolbar);
629 gtk_object_sink (GTK_OBJECT (priv->common_toolbar) );
632 /* if the program is the topmost we have to update the common
633 toolbar right now for the topmost window */
634 if (priv->is_topmost)
636 g_slist_foreach (priv->windows,
637 (GFunc) hildon_program_common_toolbar_topmost_window, NULL);
642 * hildon_program_get_common_toolbar:
643 * @self: The #HildonProgram from which to retrieve the common toolbar
645 * Return value: the GtkToolbar that was set as common toolbar for this
646 * #HildonProgram, or NULL of no common menu was set.
649 hildon_program_get_common_toolbar (HildonProgram *self)
651 HildonProgramPrivate *priv;
653 g_return_val_if_fail (HILDON_IS_PROGRAM (self), NULL);
655 priv = HILDON_PROGRAM_GET_PRIVATE (self);
658 return priv->common_toolbar ? GTK_TOOLBAR (priv->common_toolbar) : NULL;
662 * hildon_program_get_is_topmost:
663 * @self: A #HildonWindow
665 * Return value: Whether or not one of the program's window or dialog is
666 * currenltly activated by the window manager.
669 hildon_program_get_is_topmost (HildonProgram *self)
671 HildonProgramPrivate *priv;
673 g_return_val_if_fail (HILDON_IS_PROGRAM (self), FALSE);
675 priv = HILDON_PROGRAM_GET_PRIVATE (self);
678 return priv->is_topmost;