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-window
27 * @short_description: Widget representing a top-level window in the Hildon framework.
29 * The HildonWindow is a GTK widget which represents a top-level
30 * window in the Hildon framework. It is derived from the GtkWindow
31 * and provides additional commodities specific to the Hildon
34 * Among these windows in the Hildon framework can have a single menu
35 * attached, which is toggled with a hardware key or by tapping
36 * a custom button in the window frame. This menu can be set
37 * by providing a GtkMenu to the hildon_window_set_menu() method.
39 * Similarly a window in the Hildon framework can have several toolbars
40 * attached. These can be added to the HildonWindow with
41 * hildon_window_add_toolbar()..
44 * <title>Creating a HildonWindow</title>
46 * HildonWindow *window;
47 * GtkToolbar *toolbar;
49 * GdkPixbuf *icon_pixbuf;
51 * window = HILDON_WINDOW (hildon_window_new());
53 * toolbar = create_toolbar();
55 * menu = create_menu();
57 * icon_pixbuf = create_icon();
59 * hildon_window_set_menu (window, menu);
61 * hildon_window_add_toolbar (window, toolbar);
63 * // Can be used to set the window fullscreen
64 * gtk_window_fullscreen (GTK_WINDOW (window));
66 * // Used to trigger the blinking of the window's icon in the task navigator
67 * gtk_window_set_urgency_hint (GTK_WINDOW (window), TRUE);
69 * // Change the window's icon in the task navigator
70 * gtk_window_set_icon (GTK_WINDOW (window), icon_pixbuf);
76 #include "hildon-window.h"
81 #include "hildon-program.h"
82 #include "hildon-window-private.h"
83 #include "hildon-find-toolbar.h"
85 #include <gtk/gtkmenu.h>
86 #include <gtk/gtkimcontext.h>
87 #include <gtk/gtkmenuitem.h>
88 #include <gtk/gtkcheckmenuitem.h>
89 #include <gtk/gtkmenushell.h>
90 #include <gtk/gtkwindow.h>
91 #include <gtk/gtkwidget.h>
92 #include <gtk/gtkvbox.h>
93 #include <gtk/gtklabel.h>
94 #include <gtk/gtkentry.h>
95 #include <gtk/gtktextview.h>
96 #include <gtk/gtkscrolledwindow.h>
97 #include <gtk/gtkmain.h>
98 #include <gdk/gdkkeysyms.h>
100 #include <gtk/gtkprivate.h>
102 #include <X11/Xlib.h>
103 #include <X11/Xatom.h>
106 #define _(String) gettext(String)
108 #define TOOLBAR_HEIGHT 40
110 #define TOOLBAR_MIDDLE 10
113 #define CAN_HIBERNATE "CANKILL"
115 #define CAN_HIBERNATE_LENGTH 7
117 #define CAN_HIBERNATE_PROPERTY "_HILDON_ABLE_TO_HIBERNATE"
119 #define TITLE_SEPARATOR " - "
121 static GtkWindowClass *parent_class;
123 typedef void (*HildonWindowSignal) (HildonWindow *, gint, gpointer);
126 hildon_window_init (HildonWindow * self);
129 hildon_window_class_init (HildonWindowClass * window_class);
132 hildon_window_menu_popup_func (GtkMenu *menu,
138 hildon_window_menu_popup_func_full (GtkMenu *menu,
144 hildon_window_expose (GtkWidget *widget,
145 GdkEventExpose *event);
147 hildon_window_forall (GtkContainer *container,
148 gboolean include_internals,
149 GtkCallback callback,
150 gpointer callback_data);
152 hildon_window_show_all (GtkWidget *widget);
155 hildon_window_size_allocate (GtkWidget * widget,
156 GtkAllocation *allocation);
158 hildon_window_size_request (GtkWidget * widget,
159 GtkRequisition *requisition);
161 hildon_window_finalize (GObject *obj_self);
164 hildon_window_get_property (GObject *object,
170 hildon_window_destroy (GtkObject *obj);
173 hildon_window_realize (GtkWidget *widget);
176 hildon_window_unrealize (GtkWidget *widget);
179 hildon_window_key_press_event (GtkWidget *widget,
183 hildon_window_key_release_event (GtkWidget *widget,
186 hildon_window_window_state_event (GtkWidget *widget,
187 GdkEventWindowState *event);
190 hildon_window_notify (GObject *gobject,
194 hildon_window_is_topmost_notify (HildonWindow *window);
197 hildon_window_toggle_menu (HildonWindow * self);
200 hildon_window_escape_timeout (gpointer data);
202 static GdkFilterReturn
203 hildon_window_event_filter (GdkXEvent *xevent,
207 static GdkFilterReturn
208 hildon_window_root_window_event_filter (GdkXEvent *xevent,
213 hildon_window_get_borders (HildonWindow *window);
216 visible_toolbar (gpointer data,
220 paint_toolbar (GtkWidget *widget,
222 GdkEventExpose * event,
223 gboolean fullscreen);
239 * hildon_window_get_type:
241 * Initializes and returns the type of a hildon window.
243 * @Returns: GType of #HildonWindow
246 hildon_window_get_type (void)
248 static GType window_type = 0;
251 static const GTypeInfo window_info = {
252 sizeof(HildonWindowClass),
253 NULL, /* base_init */
254 NULL, /* base_finalize */
255 (GClassInitFunc) hildon_window_class_init,
256 NULL, /* class_finalize */
257 NULL, /* class_data */
258 sizeof(HildonWindow),
260 (GInstanceInitFunc) hildon_window_init,
262 window_type = g_type_register_static(GTK_TYPE_WINDOW,
270 hildon_window_class_init (HildonWindowClass * window_class)
272 /* Get convenience variables */
273 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (window_class);
274 GObjectClass *object_class = G_OBJECT_CLASS (window_class);
275 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (window_class);
277 /* Set the global parent_class here */
278 parent_class = g_type_class_peek_parent (window_class);
280 object_class->get_property = hildon_window_get_property;
281 object_class->notify = hildon_window_notify;
282 widget_class->size_allocate = hildon_window_size_allocate;
283 widget_class->size_request = hildon_window_size_request;
284 widget_class->expose_event = hildon_window_expose;
285 widget_class->show_all = hildon_window_show_all;
286 widget_class->realize = hildon_window_realize;
287 widget_class->unrealize = hildon_window_unrealize;
288 widget_class->key_press_event = hildon_window_key_press_event;
289 widget_class->key_release_event = hildon_window_key_release_event;
290 widget_class->window_state_event = hildon_window_window_state_event;
292 /* now the object stuff */
293 object_class->finalize = hildon_window_finalize;
295 /* To the container */
296 container_class->forall = hildon_window_forall;
299 GTK_OBJECT_CLASS (window_class)->destroy = hildon_window_destroy;
301 g_type_class_add_private (window_class,
302 sizeof (struct _HildonWindowPrivate));
304 /* Install properties */
307 * HildonWindow:is-topmost:
309 * Whether the window is currently activated by the window manager.
311 g_object_class_install_property (object_class, PROP_IS_TOPMOST,
312 g_param_spec_boolean ("is-topmost",
314 "Whether the window is currently activated by the window "
319 gtk_widget_class_install_style_property (widget_class,
320 g_param_spec_boxed ("borders",
322 "Size of graphical window borders",
326 gtk_widget_class_install_style_property (widget_class,
327 g_param_spec_boxed ("toolbar-borders",
328 "Graphical toolbar borders",
329 "Size of graphical toolbar borders",
333 /* opera hack, install clip operation signal */
334 g_signal_new ("clipboard_operation",
335 G_OBJECT_CLASS_TYPE (object_class),
337 G_STRUCT_OFFSET (HildonWindowClass, clipboard_operation),
339 g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1,
344 hildon_window_init (HildonWindow *self)
346 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
347 g_assert (priv != NULL);
349 priv->vbox = gtk_vbox_new (TRUE, TOOLBAR_MIDDLE);
350 gtk_widget_set_parent (priv->vbox, GTK_WIDGET(self));
352 priv->visible_toolbars = 0;
353 priv->is_topmost = FALSE;
354 priv->borders = NULL;
355 priv->toolbar_borders = NULL;
356 priv->escape_timeout = 0;
358 priv->fullscreen = FALSE;
360 priv->program = NULL;
362 /* We need to track the root window _MB_CURRENT_APP_WINDOW property */
363 gdk_window_set_events (gdk_get_default_root_window (),
364 gdk_window_get_events (gdk_get_default_root_window ()) | GDK_PROPERTY_CHANGE_MASK);
366 gdk_window_add_filter (gdk_get_default_root_window (),
367 hildon_window_root_window_event_filter, self);
371 hildon_window_finalize (GObject * obj_self)
374 HildonWindowPrivate *priv;
376 g_return_if_fail (HILDON_WINDOW (obj_self));
378 priv = HILDON_WINDOW_GET_PRIVATE (obj_self);
379 g_assert (priv != NULL);
381 self = HILDON_WINDOW (obj_self);
383 g_free (priv->borders);
384 g_free (priv->toolbar_borders);
386 if (G_OBJECT_CLASS (parent_class)->finalize)
387 G_OBJECT_CLASS (parent_class)->finalize (obj_self);
392 hildon_window_realize (GtkWidget *widget)
394 Atom *old_atoms, *new_atoms;
398 Window active_window;
399 HildonWindowPrivate *priv;
401 GTK_WIDGET_CLASS (parent_class)->realize (widget);
403 priv = HILDON_WINDOW_GET_PRIVATE (widget);
404 g_assert (priv != NULL);
406 gtk_widget_realize (GTK_WIDGET (priv->vbox));
408 /* catch the custom button signal from mb to display the menu */
409 gdk_window_add_filter (widget->window, hildon_window_event_filter, widget);
411 window = GDK_WINDOW_XID (widget->window);
412 disp = GDK_WINDOW_XDISPLAY (widget->window);
414 /* Enable custom button that is used for menu */
415 XGetWMProtocols (disp, window, &old_atoms, &atom_count);
416 new_atoms = g_new (Atom, atom_count + 1);
418 memcpy (new_atoms, old_atoms, sizeof(Atom) * atom_count);
420 new_atoms[atom_count++] =
421 XInternAtom (disp, "_NET_WM_CONTEXT_CUSTOM", False);
423 XSetWMProtocols (disp, window, new_atoms, atom_count);
428 /* rely on GDK to set the window group to its default */
429 gdk_window_set_group (widget->window, NULL);
432 gboolean can_hibernate = hildon_program_get_can_hibernate (priv->program);
434 hildon_window_set_can_hibernate_property (HILDON_WINDOW (widget),
438 /* Update the topmost status */
439 active_window = hildon_window_get_active_window();
440 hildon_window_update_topmost (HILDON_WINDOW (widget), active_window);
442 /* Update the window title */
443 hildon_window_update_title(HILDON_WINDOW (widget));
447 hildon_window_unrealize (GtkWidget *widget)
449 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (widget);
450 g_assert (priv != NULL);
452 gdk_window_remove_filter (widget->window, hildon_window_event_filter,
455 gtk_widget_unrealize (GTK_WIDGET (priv->vbox));
456 GTK_WIDGET_CLASS(parent_class)->unrealize(widget);
460 hildon_window_get_property (GObject *object,
465 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (object);
466 g_assert (priv != NULL);
468 switch (property_id) {
470 case PROP_IS_TOPMOST:
471 g_value_set_boolean (value, priv->is_topmost);
475 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
481 * Retrieve the graphical borders size used by the themes
484 hildon_window_get_borders (HildonWindow *window)
486 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (window);
489 g_free (priv->borders);
490 g_free (priv->toolbar_borders);
492 gtk_widget_style_get (GTK_WIDGET (window), "borders",&priv->borders,
493 "toolbar-borders", &priv->toolbar_borders,
497 priv->borders = (GtkBorder *) g_malloc0 (sizeof (GtkBorder));
499 if (! priv->toolbar_borders)
500 priv->toolbar_borders = (GtkBorder *) g_malloc0 (sizeof (GtkBorder));
504 visible_toolbars (gpointer data,
507 if (GTK_WIDGET_VISIBLE (GTK_WIDGET (((GtkBoxChild *)data)->widget)))
508 (*((gint *)user_data)) ++;
512 hildon_window_expose (GtkWidget *widget,
513 GdkEventExpose * event)
515 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (widget);
518 GtkWidget *bx = priv->vbox;
519 GtkBox *box = GTK_BOX(bx);
520 GtkBorder *b = priv->borders;
521 GtkBorder *tb = priv->toolbar_borders;
523 gint currently_visible_toolbars = 0;
525 if (! priv->borders) {
526 hildon_window_get_borders (HILDON_WINDOW (widget));
528 tb = priv->toolbar_borders;
531 tb_height = bx->allocation.height + tb->top + tb->bottom;
533 g_list_foreach (box->children, visible_toolbars,
534 ¤tly_visible_toolbars);
536 paint_toolbar (widget, box,
537 event, priv->fullscreen);
539 if (! priv->fullscreen) {
541 /* Draw the left and right window border */
542 gint side_borders_height = widget->allocation.height - b->top;
544 if (currently_visible_toolbars)
545 side_borders_height -= tb_height;
547 side_borders_height -= b->bottom;
551 gtk_paint_box (widget->style, widget->window,
552 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
553 &event->area, widget, "left-border",
554 widget->allocation.x, widget->allocation.y +
555 b->top, b->left, side_borders_height);
560 gtk_paint_box (widget->style, widget->window,
561 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
562 &event->area, widget, "right-border",
563 widget->allocation.x + widget->allocation.width -
564 b->right, widget->allocation.y + b->top,
565 b->right, side_borders_height);
568 /* If no toolbar, draw the bottom window border */
569 if (!currently_visible_toolbars && b->bottom > 0)
571 gtk_paint_box (widget->style, widget->window,
572 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
573 &event->area, widget, "bottom-border",
574 widget->allocation.x, widget->allocation.y +
575 (widget->allocation.height - b->bottom),
576 widget->allocation.width, b->bottom);
579 /* Draw the top border */
582 gtk_paint_box (widget->style, widget->window,
583 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
584 &event->area, widget, "top-border",
585 widget->allocation.x, widget->allocation.y,
586 widget->allocation.width, b->top);
592 /* don't draw the window stuff as it overwrites our borders with a blank
593 * rectangle. Instead start with the drawing of the GtkBin */
594 GTK_WIDGET_CLASS (g_type_class_peek_parent (parent_class))->expose_event (widget, event);
596 /* FIXME Not sure why this is commented out
597 * GTK_WIDGET_CLASS (parent_class))->
598 * expose_event (widget, event);
605 hildon_window_size_request (GtkWidget *widget,
606 GtkRequisition *requisition)
608 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (widget);
611 GtkWidget *child = GTK_BIN (widget)->child;
613 gint border_width = GTK_CONTAINER(widget)->border_width;
617 hildon_window_get_borders (HILDON_WINDOW (widget));
621 gtk_widget_size_request (child, requisition);
623 if (priv->vbox != NULL)
624 gtk_widget_size_request (priv->vbox, &req2);
626 requisition->height += req2.height;
627 requisition->width = (requisition->width < req2.width) ?
628 req2.width : requisition->width;
630 requisition->width += 2 * border_width;
631 requisition->height += 2 * border_width;
633 if (! priv->fullscreen)
635 requisition->height += priv->borders->top;
636 if (req2.height == 0)
637 requisition->height += priv->borders->bottom;
638 requisition->width += priv->borders->left + priv->borders->right;
643 hildon_window_size_allocate (GtkWidget *widget,
644 GtkAllocation *allocation)
646 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (widget);
649 GtkAllocation box_alloc;
650 GtkAllocation alloc = *allocation;
652 gint border_width = GTK_CONTAINER(widget)->border_width;
654 GtkWidget *box = priv->vbox;
655 GtkBin *bin = GTK_BIN(widget);
656 GtkBorder *b = priv->borders;
657 GtkBorder *tb = priv->toolbar_borders;
661 hildon_window_get_borders (HILDON_WINDOW (widget));
663 tb = priv->toolbar_borders;
666 widget->allocation = *allocation;
668 gtk_widget_get_child_requisition (box, &req);
670 box_alloc.width = allocation->width - tb->left - tb->right;
671 box_alloc.height = ( (req.height < allocation->height) ?
672 req.height : allocation->height );
673 box_alloc.x = allocation->x + tb->left;
674 box_alloc.y = allocation->y + allocation->height - box_alloc.height - tb->bottom;
676 if (bin->child != NULL && GTK_IS_WIDGET (bin->child)
677 && GTK_WIDGET_VISIBLE (bin->child))
679 alloc.x += border_width;
680 alloc.y += border_width;
681 alloc.width -= (border_width * 2);
682 alloc.height -= (border_width * 2) + box_alloc.height;
684 if (! priv->fullscreen)
687 alloc.width -= (b->left + b->right);
690 alloc.height -= b->top;
692 if (box_alloc.height <= 0)
693 alloc.height -= b->bottom;
695 alloc.height -= (tb->top + tb->bottom);
699 if (!(box_alloc.height <= 0))
700 alloc.height -= (tb->top + tb->bottom);
703 gtk_widget_size_allocate (bin->child, &alloc);
706 gtk_widget_size_allocate (box, &box_alloc);
708 if (priv->previous_vbox_y != box_alloc.y)
710 /* The size of the VBox has changed, we need to redraw part
711 * of the window borders */
712 gint draw_from_y = priv->previous_vbox_y < box_alloc.y?
713 priv->previous_vbox_y - tb->top:
714 box_alloc.y - tb->top;
716 gtk_widget_queue_draw_area (widget, 0, draw_from_y,
717 widget->allocation.width,
718 widget->allocation.height - draw_from_y);
720 priv->previous_vbox_y = box_alloc.y;
726 hildon_window_forall (GtkContainer *container,
727 gboolean include_internals,
728 GtkCallback callback,
729 gpointer callback_data)
731 HildonWindow *self = HILDON_WINDOW (container);
732 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
734 g_return_if_fail (callback != NULL);
737 GTK_CONTAINER_CLASS (parent_class)->forall (container, include_internals,
738 callback, callback_data);
739 if (include_internals && priv->vbox != NULL)
740 (* callback)(GTK_WIDGET (priv->vbox), callback_data);
744 hildon_window_show_all (GtkWidget *widget)
746 HildonWindow *self = HILDON_WINDOW (widget);
747 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
749 g_assert (priv != NULL);
751 GTK_WIDGET_CLASS (parent_class)->show_all (widget);
752 gtk_widget_show_all (priv->vbox);
756 hildon_window_destroy (GtkObject *obj)
758 HildonWindow *self = HILDON_WINDOW (obj);
759 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (obj);
762 g_assert (priv != NULL);
764 if (priv->vbox != NULL)
768 GtkWidget * common_toolbar =
769 GTK_WIDGET (hildon_program_get_common_toolbar (priv->program));
770 if (common_toolbar && common_toolbar->parent == priv->vbox)
772 gtk_container_remove (GTK_CONTAINER (priv->vbox),
777 gtk_widget_unparent (priv->vbox);
782 menu_list = g_list_copy (gtk_menu_get_for_attach_widget (GTK_WIDGET (obj)));
786 if (GTK_IS_MENU(menu_list->data))
788 if (GTK_WIDGET_VISIBLE (GTK_WIDGET (menu_list->data)))
790 gtk_menu_popdown (GTK_MENU (menu_list->data));
791 gtk_menu_shell_deactivate (GTK_MENU_SHELL (menu_list->data));
793 gtk_menu_detach (GTK_MENU (menu_list->data));
795 menu_list = menu_list->next;
798 g_list_free (menu_list);
802 hildon_program_remove_window (priv->program, self);
805 gdk_window_remove_filter (gdk_get_default_root_window(),
806 hildon_window_root_window_event_filter,
809 gtk_widget_set_events (GTK_WIDGET(obj), 0);
811 GTK_OBJECT_CLASS (parent_class)->destroy (obj);
816 hildon_window_notify (GObject *gobject,
819 HildonWindow *window = HILDON_WINDOW (gobject);
821 if (g_str_equal (param->name, "title"))
824 hildon_window_update_title (window);
826 else if (g_str_equal (param->name, "is-topmost"))
828 hildon_window_is_topmost_notify (window);
831 if (G_OBJECT_CLASS(parent_class)->notify)
832 G_OBJECT_CLASS(parent_class)->notify (gobject, param);
837 visible_toolbar (gpointer data,
840 if (GTK_WIDGET_VISIBLE (((GtkBoxChild *)data)->widget))
841 (*((gint *)user_data))++;
845 find_findtoolbar_index (gpointer data,
848 gint *pass_bundle = (gint *)user_data;
850 if(((GtkBoxChild *)data)->widget->allocation.y < pass_bundle[0]
851 && GTK_WIDGET_VISIBLE (((GtkBoxChild *)data)->widget))
856 find_findtoolbar (gpointer data,
859 if(HILDON_IS_FIND_TOOLBAR (((GtkBoxChild *)data)->widget)
860 && GTK_WIDGET_VISIBLE (((GtkBoxChild *)data)->widget))
861 (*((GtkWidget **)user_data)) = ((GtkBoxChild *)data)->widget;
865 paint_toolbar (GtkWidget *widget,
867 GdkEventExpose * event,
870 gint toolbar_num = 0;
873 GtkWidget *findtoolbar = NULL;
874 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (widget);
875 gchar toolbar_mode[40];
876 GtkBorder *tb = priv->toolbar_borders;
878 g_assert (priv != NULL);
880 /* collect info to help on painting the boxes */
881 g_list_foreach (box->children, visible_toolbar,
882 (gpointer) &toolbar_num);
887 g_list_foreach (box->children, find_findtoolbar, (gpointer) &findtoolbar);
889 if (findtoolbar != NULL)
891 gint pass_bundle[2];/* an array for convient data passing
892 the first member contains the y allocation
893 of the find toolbar, and the second allocation
894 contains the index(how many toolbars are above
896 pass_bundle[0] = findtoolbar->allocation.y;
897 pass_bundle[1] = ftb_index;
898 g_list_foreach(box->children, find_findtoolbar_index,
899 (gpointer) pass_bundle);
900 ftb_index = pass_bundle[1];
904 sprintf (toolbar_mode, "toolbar%sframe-top",
905 fullscreen ? "-fullscreen-" : "-");
906 gtk_paint_box (widget->style, widget->window,
907 GTK_WIDGET_STATE (widget), GTK_SHADOW_OUT,
908 &event->area, widget, toolbar_mode,
909 widget->allocation.x,
910 GTK_WIDGET (box)->allocation.y - tb->top,
911 widget->allocation.width, tb->top);
913 /*top most toolbar painting*/
914 if (findtoolbar != NULL && ftb_index == 0 )
916 sprintf (toolbar_mode, "findtoolbar%s",
917 fullscreen ? "-fullscreen" : "");
919 gtk_paint_box (widget->style, widget->window,
920 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
921 &event->area, widget, toolbar_mode,
922 widget->allocation.x,
923 GTK_WIDGET(box)->allocation.y,
924 widget->allocation.width,
929 sprintf (toolbar_mode, "toolbar%s",
930 fullscreen ? "-fullscreen" : "");
932 gtk_paint_box (widget->style, widget->window,
933 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
934 &event->area, widget, toolbar_mode,
935 widget->allocation.x,
936 GTK_WIDGET(box)->allocation.y,
937 widget->allocation.width,
940 /*multi toolbar painting*/
941 for (count = 0; count < toolbar_num - 1; count++)
943 sprintf (toolbar_mode, "toolbar%sframe-middle",
944 fullscreen ? "-fullscreen-" : "-");
946 gtk_paint_box (widget->style, widget->window,
947 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
948 &event->area, widget, toolbar_mode,
949 widget->allocation.x,
950 GTK_WIDGET(box)->allocation.y +
951 (1 + count) * TOOLBAR_HEIGHT +
952 count * TOOLBAR_MIDDLE,
953 widget->allocation.width,
956 if (findtoolbar != NULL && count + 1 == ftb_index)
959 sprintf (toolbar_mode, "findtoolbar%s",
960 fullscreen ? "-fullscreen" : "");
962 gtk_paint_box (widget->style, widget->window,
963 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
964 &event->area, widget, toolbar_mode,
965 widget->allocation.x,
966 GTK_WIDGET(box)->allocation.y +
967 (1 + count) * (TOOLBAR_HEIGHT + TOOLBAR_MIDDLE),
968 widget->allocation.width,
973 sprintf (toolbar_mode, "toolbar%s",
974 fullscreen ? "-fullscreen" : "");
976 gtk_paint_box (widget->style, widget->window,
977 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
978 &event->area, widget, toolbar_mode,
979 widget->allocation.x,
980 GTK_WIDGET(box)->allocation.y +
981 (1 + count) * (TOOLBAR_HEIGHT + TOOLBAR_MIDDLE),
982 widget->allocation.width,
986 sprintf (toolbar_mode, "toolbar%sframe-bottom",
987 fullscreen ? "-fullscreen-" : "-");
989 gtk_paint_box (widget->style, widget->window,
990 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
991 &event->area, widget, toolbar_mode,
992 widget->allocation.x,
993 GTK_WIDGET(box)->allocation.y +
994 GTK_WIDGET(box)->allocation.height,
995 widget->allocation.width, tb->bottom);
999 * Checks the root window to know which is the topped window
1002 hildon_window_get_active_window (void)
1009 unsigned long extra;
1013 unsigned char *char_pointer;
1015 Atom active_app_atom =
1016 XInternAtom (GDK_DISPLAY (), "_MB_CURRENT_APP_WINDOW", False);
1020 status = XGetWindowProperty (GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1021 active_app_atom, 0L, 16L,
1022 0, XA_WINDOW, &realtype, &format,
1023 &n, &extra, &win.char_pointer);
1024 if (!(status == Success && realtype == XA_WINDOW && format == 32
1025 && n == 1 && win.win != NULL))
1027 if (win.win != NULL)
1028 XFree (win.char_pointer);
1034 if (win.win != NULL)
1035 XFree(win.char_pointer);
1041 xclient_message_type_check (XClientMessageEvent *cm,
1044 return cm->message_type == XInternAtom(GDK_DISPLAY(), name, FALSE);
1048 * Handle the window border custom button, which toggles the menu,
1049 * and the Hildon input method copy paste messages
1051 static GdkFilterReturn
1052 hildon_window_event_filter (GdkXEvent *xevent,
1056 XAnyEvent *eventti = xevent;
1058 if (eventti->type == ClientMessage)
1060 XClientMessageEvent *cm = xevent;
1062 if (xclient_message_type_check (cm, "_MB_GRAB_TRANSFER"))
1064 hildon_window_toggle_menu (HILDON_WINDOW ( data ));
1065 return GDK_FILTER_REMOVE;
1067 /* opera hack clipboard client message */
1068 else if (xclient_message_type_check (cm, "_HILDON_IM_CLIPBOARD_COPY"))
1070 g_signal_emit_by_name(G_OBJECT(data), "clipboard_operation",
1071 HILDON_WINDOW_CO_COPY);
1072 return GDK_FILTER_REMOVE;
1074 else if (xclient_message_type_check(cm, "_HILDON_IM_CLIPBOARD_CUT"))
1076 g_signal_emit_by_name(G_OBJECT(data), "clipboard_operation",
1077 HILDON_WINDOW_CO_CUT);
1078 return GDK_FILTER_REMOVE;
1080 else if (xclient_message_type_check(cm, "_HILDON_IM_CLIPBOARD_PASTE"))
1082 g_signal_emit_by_name(G_OBJECT(data), "clipboard_operation",
1083 HILDON_WINDOW_CO_PASTE);
1084 return GDK_FILTER_REMOVE;
1088 return GDK_FILTER_CONTINUE;
1092 * Here we keep track of changes in the _MB_CURRENT_APP_WINDOW,
1093 * to know when we acquire/lose topmost status
1095 static GdkFilterReturn
1096 hildon_window_root_window_event_filter (GdkXEvent *xevent,
1100 XAnyEvent *eventti = xevent;
1101 HildonWindow *hwindow = HILDON_WINDOW (data);
1103 if (eventti->type == PropertyNotify)
1105 XPropertyEvent *pevent = xevent;
1106 Atom active_app_atom =
1107 XInternAtom (GDK_DISPLAY (), "_MB_CURRENT_APP_WINDOW", False);
1109 if (pevent->atom == active_app_atom)
1111 Window active_window = hildon_window_get_active_window();
1113 hildon_window_update_topmost (hwindow, active_window);
1117 return GDK_FILTER_CONTINUE;
1121 * Handle the menu hardware key here
1124 hildon_window_key_press_event (GtkWidget *widget,
1127 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (widget);
1129 g_return_val_if_fail (HILDON_IS_WINDOW (widget),FALSE);
1132 switch (event->keyval)
1134 case HILDON_HARDKEY_MENU:
1135 if (hildon_window_toggle_menu (HILDON_WINDOW (widget)))
1138 case HILDON_HARDKEY_ESC:
1139 if (!priv->escape_timeout)
1141 priv->escape_timeout = g_timeout_add
1142 (HILDON_WINDOW_LONG_PRESS_TIME,
1143 hildon_window_escape_timeout, widget);
1148 return GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event);
1152 hildon_window_key_release_event (GtkWidget *widget,
1155 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (widget);
1157 g_return_val_if_fail (HILDON_IS_WINDOW (widget), FALSE);
1160 switch (event->keyval)
1162 case HILDON_HARDKEY_ESC:
1163 if (priv->escape_timeout)
1165 g_source_remove (priv->escape_timeout);
1166 priv->escape_timeout = 0;
1171 return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
1176 * We keep track of the window state changes, because the drawing
1177 * (borders) differs whether we are in fullscreen mode or not
1180 hildon_window_window_state_event (GtkWidget *widget,
1181 GdkEventWindowState *event)
1183 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (widget);
1184 g_assert (priv != NULL);
1186 if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
1187 priv->fullscreen = event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN;
1189 if (GTK_WIDGET_CLASS (parent_class)->window_state_event)
1191 return GTK_WIDGET_CLASS (parent_class)->window_state_event (
1202 * The menu popuping needs a menu popup-function
1205 hildon_window_menu_popup_func (GtkMenu *menu,
1213 GdkWindow *window = GTK_WIDGET(widget)->window;
1217 gdk_window_get_origin (window, &window_x, &window_y);
1220 gtk_widget_style_get (GTK_WIDGET (menu), "horizontal-offset", x,
1221 "vertical-offset", y, NULL);
1229 hildon_window_menu_popup_func_full (GtkMenu *menu,
1235 gtk_widget_style_get (GTK_WIDGET (menu), "horizontal-offset", x,
1236 "vertical-offset", y, NULL);
1244 * Takes the common toolbar when we acquire the top-most status
1247 hildon_window_is_topmost_notify (HildonWindow *window)
1249 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (window);
1253 if (priv->is_topmost)
1255 hildon_window_take_common_toolbar (window);
1260 /* If the window lost focus while the user started to press
1261 * the ESC key, we won't get the release event. We need to
1262 * stop the timeout*/
1263 if (priv->escape_timeout)
1265 g_source_remove (priv->escape_timeout);
1266 priv->escape_timeout = 0;
1272 * Sets the program to which the window belongs. This should only be called
1273 * by hildon_program_add_window
1275 void G_GNUC_INTERNAL
1276 hildon_window_set_program (HildonWindow *self,
1279 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
1281 g_return_if_fail (HILDON_IS_WINDOW (self));
1282 g_assert (priv != NULL);
1286 g_object_unref (priv->program);
1289 /* Now that we are bound to a program, we can rely on it to track the
1291 gdk_window_remove_filter (gdk_get_default_root_window(),
1292 hildon_window_root_window_event_filter,
1295 priv->program = HILDON_PROGRAM (program);
1296 g_object_ref (program);
1300 * Unsets the program to which the window belongs. This should only be called
1301 * by hildon_program_add_window
1303 void G_GNUC_INTERNAL
1304 hildon_window_unset_program (HildonWindow *self)
1306 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
1308 g_return_if_fail(HILDON_IS_WINDOW (self));
1309 g_assert (priv != NULL);
1313 g_object_unref (priv->program);
1314 priv->program = NULL;
1316 /* We need to start tacking the root window again */
1317 gdk_window_set_events (gdk_get_default_root_window (),
1318 gdk_window_get_events (gdk_get_default_root_window ())
1319 | GDK_PROPERTY_CHANGE_MASK);
1321 gdk_window_add_filter (gdk_get_default_root_window (),
1322 hildon_window_root_window_event_filter, self );
1325 priv->program = NULL;
1329 * Sets whether or not the program to which this window belongs is
1330 * killable. This is used by the HildonProgram to signify to the
1331 * Task Navigator whether or not it can hibernate in memory-low situations
1333 void G_GNUC_INTERNAL
1334 hildon_window_set_can_hibernate_property (HildonWindow *self,
1335 gpointer _can_hibernate)
1337 GdkAtom killable_atom;
1338 gboolean can_hibernate;
1340 g_return_if_fail(self && HILDON_IS_WINDOW (self));
1342 if (!GTK_WIDGET_REALIZED ((GTK_WIDGET (self))))
1347 can_hibernate = * ((gboolean *)_can_hibernate);
1349 killable_atom = gdk_atom_intern (CAN_HIBERNATE_PROPERTY, FALSE);
1353 gdk_property_change (GTK_WIDGET (self)->window, killable_atom,
1354 (GdkAtom)31/* XA_STRING */, 8,
1355 GDK_PROP_MODE_REPLACE, (const guchar *)CAN_HIBERNATE,
1356 CAN_HIBERNATE_LENGTH);
1360 gdk_property_delete (GTK_WIDGET (self)->window, killable_atom);
1366 * If a common toolbar was set to the program, reparent it to
1369 void G_GNUC_INTERNAL
1370 hildon_window_take_common_toolbar (HildonWindow *self)
1372 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
1374 g_return_if_fail(HILDON_IS_WINDOW (self));
1379 GtkWidget *common_toolbar =
1380 GTK_WIDGET (hildon_program_get_common_toolbar (priv->program));
1382 if (common_toolbar && common_toolbar->parent != priv->vbox)
1384 g_object_ref (common_toolbar);
1385 if (common_toolbar->parent)
1387 gtk_container_remove (GTK_CONTAINER (common_toolbar->parent),
1391 gtk_box_pack_end (GTK_BOX(priv->vbox), common_toolbar,
1393 g_object_unref (common_toolbar);
1395 gtk_widget_set_size_request (common_toolbar, -1, TOOLBAR_HEIGHT);
1397 gtk_widget_show (priv->vbox);
1404 * Compare the window that was last topped, and act consequently
1407 hildon_window_update_topmost (HildonWindow *self,
1410 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
1414 g_return_if_fail (HILDON_IS_WINDOW (self));
1417 my_window = GDK_WINDOW_XID (GTK_WIDGET (self)->window);
1419 if (window_id == my_window)
1421 if (! priv->is_topmost)
1423 priv->is_topmost = TRUE;
1424 hildon_window_is_topmost_notify (self);
1425 g_object_notify (G_OBJECT (self), "is-topmost");
1428 else if (priv->is_topmost)
1430 /* Should this go in the signal handler? */
1431 GtkWidget *focus = gtk_window_get_focus (GTK_WINDOW (self));
1433 if (GTK_IS_ENTRY (focus))
1434 gtk_im_context_focus_out (GTK_ENTRY (focus)->im_context);
1435 if (GTK_IS_TEXT_VIEW (focus))
1436 gtk_im_context_focus_out (GTK_TEXT_VIEW (focus)->im_context);
1438 priv->is_topmost = FALSE;
1439 hildon_window_is_topmost_notify (self);
1440 g_object_notify (G_OBJECT (self), "is-topmost");
1445 * If the application
1446 * was given a name (with g_set_application_name(), set
1447 * "ProgramName - WindowTitle" as the displayed
1450 void G_GNUC_INTERNAL
1451 hildon_window_update_title (HildonWindow *window)
1453 const gchar * application_name;
1455 g_return_if_fail (HILDON_IS_WINDOW (window));
1457 if (!GTK_WIDGET_REALIZED (window))
1462 application_name = g_get_application_name ();
1464 if (application_name && application_name[0])
1466 const gchar *old_title = gtk_window_get_title (GTK_WINDOW (window));
1468 if (old_title && old_title[0])
1470 gchar *title = NULL;
1472 title = g_strjoin (TITLE_SEPARATOR, application_name,
1475 gdk_window_set_title (GTK_WIDGET (window)->window, title);
1484 detach_menu_func (GtkWidget *attach_widget,
1487 /* FIXME Why is this even needed here? */
1491 * Toggles the display of the HildonWindow menu.
1492 * Returns whether or not something was done (whether or not we had a menu
1496 hildon_window_toggle_menu (HildonWindow * self)
1498 GtkMenu *menu_to_use = NULL;
1499 GList *menu_children = NULL;
1500 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
1502 g_return_val_if_fail (HILDON_IS_WINDOW (self), FALSE);
1503 g_assert (priv != NULL);
1505 /* Select which menu to use, Window specific has highest priority,
1506 * then program specific */
1509 menu_to_use = GTK_MENU (priv->menu);
1511 else if (priv->program)
1513 menu_to_use = hildon_program_get_common_menu (priv->program);
1514 if (menu_to_use && gtk_menu_get_attach_widget (menu_to_use) !=
1517 g_object_ref (menu_to_use);
1518 if (gtk_menu_get_attach_widget (menu_to_use))
1520 gtk_menu_detach (menu_to_use);
1523 gtk_menu_attach_to_widget (menu_to_use, GTK_WIDGET (self),
1525 g_object_unref (menu_to_use);
1535 if (GTK_WIDGET_MAPPED (GTK_WIDGET (menu_to_use)))
1537 gtk_menu_popdown (menu_to_use);
1538 gtk_menu_shell_deactivate (GTK_MENU_SHELL (menu_to_use));
1542 /* Check if the menu has items */
1543 menu_children = gtk_container_get_children (GTK_CONTAINER (menu_to_use));
1547 g_list_free (menu_children);
1549 /* Apply right theming */
1550 gtk_widget_set_name (GTK_WIDGET (menu_to_use),
1551 "menu_force_with_corners");
1553 if (priv->fullscreen)
1555 gtk_menu_popup (menu_to_use, NULL, NULL,
1556 (GtkMenuPositionFunc)
1557 hildon_window_menu_popup_func_full,
1559 gtk_get_current_event_time ());
1563 gtk_menu_popup (menu_to_use, NULL, NULL,
1564 (GtkMenuPositionFunc)
1565 hildon_window_menu_popup_func,
1567 gtk_get_current_event_time ());
1569 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu_to_use), TRUE);
1577 * If the ESC key was not released when the timeout expires,
1581 hildon_window_escape_timeout (gpointer data)
1583 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (data);
1588 GDK_THREADS_ENTER ();
1590 /* Send fake event, simulation a situation that user
1591 pressed 'x' from the corner */
1592 event = gdk_event_new(GDK_DELETE);
1593 ((GdkEventAny *)event)->window = GDK_WINDOW (g_object_ref (GTK_WIDGET(data)->window));
1594 gtk_main_do_event(event);
1596 /* That unrefs the window, so we're reffing it above */
1597 gdk_event_free(event);
1599 priv->escape_timeout = 0;
1601 GDK_THREADS_LEAVE ();
1607 * hildon_window_new:
1609 * Creates a new HildonWindow.
1611 * Return value: A @HildonWindow.
1614 hildon_window_new (void)
1616 HildonWindow *newwindow = g_object_new (HILDON_TYPE_WINDOW, NULL);
1618 return GTK_WIDGET (newwindow);
1622 * hildon_window_add_with_scrollbar
1623 * @self : A @HildonWindow
1624 * @child : A @GtkWidget
1626 * Adds the @child to the HildonWindow and creates a scrollbar
1627 * for it. Similar as adding first a @GtkScrolledWindow and then the
1631 hildon_window_add_with_scrollbar (HildonWindow *self,
1634 GtkScrolledWindow *scrolledw;
1636 g_return_if_fail (HILDON_IS_WINDOW (self));
1637 g_return_if_fail (GTK_IS_WIDGET (child));
1638 g_return_if_fail (child->parent == NULL);
1640 scrolledw = GTK_SCROLLED_WINDOW (gtk_scrolled_window_new (NULL, NULL));
1641 gtk_scrolled_window_set_policy (scrolledw, GTK_POLICY_NEVER,
1642 GTK_POLICY_AUTOMATIC);
1643 gtk_scrolled_window_set_shadow_type (scrolledw, GTK_SHADOW_NONE);
1645 if (GTK_IS_VIEWPORT (child))
1646 gtk_container_add (GTK_CONTAINER (scrolledw), child);
1649 if (GTK_IS_CONTAINER (child) )
1650 gtk_container_set_focus_vadjustment (GTK_CONTAINER(child),
1651 gtk_scrolled_window_get_vadjustment (scrolledw) );
1652 gtk_scrolled_window_add_with_viewport (scrolledw, child);
1655 gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (scrolledw));
1659 * hildon_window_add_toolbar:
1660 * @self: A @HildonWindow
1661 * @toolbar: A #GtkToolbar to add to the HildonWindow
1663 * Adds a toolbar to the window.
1666 hildon_window_add_toolbar (HildonWindow *self,
1667 GtkToolbar *toolbar)
1670 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
1672 g_return_if_fail (HILDON_IS_WINDOW (self));
1673 g_return_if_fail (toolbar && GTK_IS_TOOLBAR (toolbar));
1676 vbox = GTK_BOX (priv->vbox);
1678 gtk_box_pack_start (vbox, GTK_WIDGET(toolbar), TRUE, TRUE, 0);
1679 gtk_box_reorder_child (vbox, GTK_WIDGET(toolbar), 0);
1680 gtk_widget_set_size_request (GTK_WIDGET (toolbar), -1, TOOLBAR_HEIGHT);
1682 gtk_widget_queue_resize (GTK_WIDGET(self));
1686 * hildon_window_remove_toolbar:
1687 * @self: A @HildonWindow
1688 * @toolbar: A #GtkToolbar to remove from the HildonWindow
1690 * Removes a toolbar from the window.
1693 hildon_window_remove_toolbar (HildonWindow *self,
1694 GtkToolbar *toolbar)
1696 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
1698 g_return_if_fail (HILDON_IS_WINDOW (self));
1701 gtk_container_remove (GTK_CONTAINER (priv->vbox), GTK_WIDGET(toolbar));
1705 * hildon_window_get_menu:
1706 * @self : #HildonWindow
1708 * Gets the #GtMenu assigned to the #HildonAppview.
1710 * Return value: The #GtkMenu assigned to this application view.
1713 hildon_window_get_menu (HildonWindow * self)
1715 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
1717 g_return_val_if_fail (HILDON_IS_WINDOW (self), NULL);
1720 return GTK_MENU (priv->menu);
1724 * hildon_window_set_menu:
1725 * @self: A #HildonWindow
1726 * @menu: The #GtkMenu to be used for this #HildonWindow
1728 * Sets the menu to be used for this window. This menu overrides
1729 * a program-wide menu that may have been set with
1730 * hildon_program_set_common_menu. Pass NULL to remove the current
1734 hildon_window_set_menu (HildonWindow *self,
1737 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
1739 g_return_if_fail (HILDON_IS_WINDOW (self));
1742 if (priv->menu != NULL) {
1743 gtk_menu_detach (GTK_MENU (priv->menu));
1744 g_object_unref (priv->menu);
1747 priv->menu = (menu != NULL) ? GTK_WIDGET (menu) : NULL;
1748 if (priv->menu != NULL) {
1749 gtk_widget_set_name (priv->menu, "menu_force_with_corners");
1750 gtk_menu_attach_to_widget (GTK_MENU (priv->menu), GTK_WIDGET (self), &detach_menu_func);
1751 g_object_ref (GTK_MENU (priv->menu));
1752 gtk_widget_show_all (GTK_WIDGET (priv->menu));
1757 * hildon_window_get_is_topmost:
1758 * @self: A #HildonWindow
1760 * Return value: Whether or not the #HildonWindow is currenltly activated
1761 * by the window manager.
1764 hildon_window_get_is_topmost (HildonWindow *self)
1766 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
1768 g_return_val_if_fail (HILDON_IS_WINDOW (self), FALSE);
1771 return priv->is_topmost;