2 * This file is part of hildon-libs
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
25 #include "hildon-window.h"
30 #include "hildon-program.h"
31 #include "hildon-window-private.h"
32 #include "hildon-find-toolbar.h"
34 #include <gtk/gtkmenu.h>
35 #include <gtk/gtkimcontext.h>
36 #include <gtk/gtkmenuitem.h>
37 #include <gtk/gtkcheckmenuitem.h>
38 #include <gtk/gtkmenushell.h>
39 #include <gtk/gtkwindow.h>
40 #include <gtk/gtkwidget.h>
41 #include <gtk/gtkvbox.h>
42 #include <gtk/gtklabel.h>
43 #include <gtk/gtkentry.h>
44 #include <gtk/gtktextview.h>
45 #include <gtk/gtkscrolledwindow.h>
46 #include <gtk/gtkmain.h>
47 #include <gdk/gdkkeysyms.h>
49 #include <gtk/gtkprivate.h>
52 #include <X11/Xatom.h>
55 #define _(String) gettext(String)
57 #define TOOLBAR_HEIGHT 40
59 #define TOOLBAR_MIDDLE 10
62 #define CAN_HIBERNATE "CANKILL"
64 #define CAN_HIBERNATE_LENGTH 7
66 #define CAN_HIBERNATE_PROPERTY "_HILDON_ABLE_TO_HIBERNATE"
68 #define TITLE_SEPARATOR " - "
70 static GtkWindowClass *parent_class;
72 typedef void (*HildonWindowSignal) (HildonWindow *, gint, gpointer);
75 hildon_window_init (HildonWindow * self);
78 hildon_window_class_init (HildonWindowClass * window_class);
81 hildon_window_menu_popup_func (GtkMenu *menu,
87 hildon_window_menu_popup_func_full (GtkMenu *menu,
93 hildon_window_expose (GtkWidget *widget,
94 GdkEventExpose *event);
96 hildon_window_forall (GtkContainer *container,
97 gboolean include_internals,
99 gpointer callback_data);
101 hildon_window_show_all (GtkWidget *widget);
104 hildon_window_size_allocate (GtkWidget * widget,
105 GtkAllocation *allocation);
107 hildon_window_size_request (GtkWidget * widget,
108 GtkRequisition *requisition);
110 hildon_window_finalize (GObject *obj_self);
113 hildon_window_get_property (GObject *object,
119 hildon_window_destroy (GtkObject *obj);
122 hildon_window_realize (GtkWidget *widget);
125 hildon_window_unrealize (GtkWidget *widget);
128 hildon_window_key_press_event (GtkWidget *widget,
132 hildon_window_key_release_event (GtkWidget *widget,
135 hildon_window_window_state_event (GtkWidget *widget,
136 GdkEventWindowState *event);
139 hildon_window_notify (GObject *gobject,
143 hildon_window_is_topmost_notify (HildonWindow *window);
146 hildon_window_toggle_menu (HildonWindow * self);
149 hildon_window_escape_timeout (gpointer data);
151 static GdkFilterReturn
152 hildon_window_event_filter (GdkXEvent *xevent,
156 static GdkFilterReturn
157 hildon_window_root_window_event_filter (GdkXEvent *xevent,
162 hildon_window_get_borders (HildonWindow *window);
165 visible_toolbar (gpointer data,
169 paint_toolbar (GtkWidget *widget,
171 GdkEventExpose * event,
172 gboolean fullscreen);
188 hildon_window_get_type (void)
190 static GType window_type = 0;
193 static const GTypeInfo window_info = {
194 sizeof(HildonWindowClass),
195 NULL, /* base_init */
196 NULL, /* base_finalize */
197 (GClassInitFunc) hildon_window_class_init,
198 NULL, /* class_finalize */
199 NULL, /* class_data */
200 sizeof(HildonWindow),
202 (GInstanceInitFunc) hildon_window_init,
204 window_type = g_type_register_static(GTK_TYPE_WINDOW,
212 hildon_window_class_init (HildonWindowClass * window_class)
214 /* Get convenience variables */
215 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (window_class);
216 GObjectClass *object_class = G_OBJECT_CLASS (window_class);
217 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (window_class);
219 /* Set the global parent_class here */
220 parent_class = g_type_class_peek_parent (window_class);
222 object_class->get_property = hildon_window_get_property;
223 object_class->notify = hildon_window_notify;
225 /* Set the widgets virtual functions */
226 widget_class->size_allocate = hildon_window_size_allocate;
227 widget_class->size_request = hildon_window_size_request;
228 widget_class->expose_event = hildon_window_expose;
229 widget_class->show_all = hildon_window_show_all;
230 widget_class->realize = hildon_window_realize;
231 widget_class->unrealize = hildon_window_unrealize;
232 widget_class->key_press_event = hildon_window_key_press_event;
233 widget_class->key_release_event = hildon_window_key_release_event;
234 widget_class->window_state_event = hildon_window_window_state_event;
236 /* now the object stuff */
237 object_class->finalize = hildon_window_finalize;
239 /* To the container */
240 container_class->forall = hildon_window_forall;
243 GTK_OBJECT_CLASS (window_class)->destroy = hildon_window_destroy;
245 g_type_class_add_private (window_class,
246 sizeof (struct _HildonWindowPrivate));
248 /* Install properties */
249 g_object_class_install_property (object_class, PROP_IS_TOPMOST,
250 g_param_spec_boolean ("is-topmost",
252 "Whether the window is currently activated by the window "
257 gtk_widget_class_install_style_property (widget_class,
258 g_param_spec_boxed ("borders",
260 "Size of graphical window borders",
264 gtk_widget_class_install_style_property (widget_class,
265 g_param_spec_boxed ("toolbar-borders",
266 "Graphical toolbar borders",
267 "Size of graphical toolbar borders",
271 /* opera hack, install clip operation signal */
272 g_signal_new ("clipboard_operation",
273 G_OBJECT_CLASS_TYPE (object_class),
275 G_STRUCT_OFFSET (HildonWindowClass, clipboard_operation),
277 g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1,
282 hildon_window_init (HildonWindow *self)
284 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
285 g_assert (priv != NULL);
287 priv->vbox = gtk_vbox_new (TRUE, TOOLBAR_MIDDLE);
288 gtk_widget_set_parent (priv->vbox, GTK_WIDGET(self));
290 priv->visible_toolbars = 0;
291 priv->is_topmost = FALSE;
292 priv->borders = NULL;
293 priv->toolbar_borders = NULL;
294 priv->escape_timeout = 0;
296 priv->fullscreen = FALSE;
298 priv->program = NULL;
300 /* We need to track the root window _MB_CURRENT_APP_WINDOW property */
301 gdk_window_set_events (gdk_get_default_root_window (),
302 gdk_window_get_events (gdk_get_default_root_window ()) | GDK_PROPERTY_CHANGE_MASK);
304 gdk_window_add_filter (gdk_get_default_root_window (),
305 hildon_window_root_window_event_filter, self);
309 hildon_window_finalize (GObject * obj_self)
312 HildonWindowPrivate *priv;
314 g_return_if_fail (HILDON_WINDOW (obj_self));
316 priv = HILDON_WINDOW_GET_PRIVATE (obj_self);
317 g_assert (priv != NULL);
319 self = HILDON_WINDOW (obj_self);
321 g_free (priv->borders);
322 g_free (priv->toolbar_borders);
324 if (G_OBJECT_CLASS (parent_class)->finalize)
325 G_OBJECT_CLASS (parent_class)->finalize (obj_self);
330 hildon_window_realize (GtkWidget *widget)
332 Atom *old_atoms, *new_atoms;
336 Window active_window;
337 HildonWindowPrivate *priv;
339 GTK_WIDGET_CLASS (parent_class)->realize (widget);
341 priv = HILDON_WINDOW_GET_PRIVATE (widget);
342 g_assert (priv != NULL);
344 gtk_widget_realize (GTK_WIDGET (priv->vbox));
346 /* catch the custom button signal from mb to display the menu */
347 gdk_window_add_filter (widget->window, hildon_window_event_filter, widget);
349 window = GDK_WINDOW_XID (widget->window);
350 disp = GDK_WINDOW_XDISPLAY (widget->window);
352 /* Enable custom button that is used for menu */
353 XGetWMProtocols (disp, window, &old_atoms, &atom_count);
354 new_atoms = g_new (Atom, atom_count + 1);
356 memcpy (new_atoms, old_atoms, sizeof(Atom) * atom_count);
358 new_atoms[atom_count++] =
359 XInternAtom (disp, "_NET_WM_CONTEXT_CUSTOM", False);
361 XSetWMProtocols (disp, window, new_atoms, atom_count);
366 /* rely on GDK to set the window group to its default */
367 gdk_window_set_group (widget->window, NULL);
370 gboolean can_hibernate = hildon_program_get_can_hibernate (priv->program);
372 hildon_window_set_can_hibernate_property (HILDON_WINDOW (widget),
376 /* Update the topmost status */
377 active_window = hildon_window_get_active_window();
378 hildon_window_update_topmost (HILDON_WINDOW (widget), active_window);
380 /* Update the window title */
381 hildon_window_update_title(HILDON_WINDOW (widget));
385 hildon_window_unrealize (GtkWidget *widget)
387 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (widget);
388 g_assert (priv != NULL);
390 gdk_window_remove_filter (widget->window, hildon_window_event_filter,
393 gtk_widget_unrealize (GTK_WIDGET (priv->vbox));
394 GTK_WIDGET_CLASS(parent_class)->unrealize(widget);
398 hildon_window_get_property (GObject *object,
403 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (object);
404 g_assert (priv != NULL);
406 switch (property_id) {
408 case PROP_IS_TOPMOST:
409 g_value_set_boolean (value, priv->is_topmost);
413 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
419 * Retrieve the graphical borders size used by the themes
422 hildon_window_get_borders (HildonWindow *window)
424 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (window);
427 g_free (priv->borders);
428 g_free (priv->toolbar_borders);
430 gtk_widget_style_get (GTK_WIDGET (window), "borders",&priv->borders,
431 "toolbar-borders", &priv->toolbar_borders,
435 priv->borders = (GtkBorder *) g_malloc0 (sizeof (GtkBorder));
437 if (! priv->toolbar_borders)
438 priv->toolbar_borders = (GtkBorder *) g_malloc0 (sizeof (GtkBorder));
442 visible_toolbars (gpointer data,
445 if (GTK_WIDGET_VISIBLE (GTK_WIDGET (((GtkBoxChild *)data)->widget)))
446 (*((gint *)user_data)) ++;
450 hildon_window_expose (GtkWidget *widget,
451 GdkEventExpose * event)
453 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (widget);
456 GtkWidget *bx = priv->vbox;
457 GtkBox *box = GTK_BOX(bx);
458 GtkBorder *b = priv->borders;
459 GtkBorder *tb = priv->toolbar_borders;
461 gint currently_visible_toolbars = 0;
463 if (! priv->borders) {
464 hildon_window_get_borders (HILDON_WINDOW (widget));
466 tb = priv->toolbar_borders;
469 tb_height = bx->allocation.height + tb->top + tb->bottom;
471 g_list_foreach (box->children, visible_toolbars,
472 ¤tly_visible_toolbars);
474 paint_toolbar (widget, box,
475 event, priv->fullscreen);
477 if (! priv->fullscreen) {
479 /* Draw the left and right window border */
480 gint side_borders_height = widget->allocation.height - b->top;
482 if (currently_visible_toolbars)
483 side_borders_height -= tb_height;
485 side_borders_height -= b->bottom;
489 gtk_paint_box (widget->style, widget->window,
490 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
491 &event->area, widget, "left-border",
492 widget->allocation.x, widget->allocation.y +
493 b->top, b->left, side_borders_height);
498 gtk_paint_box (widget->style, widget->window,
499 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
500 &event->area, widget, "right-border",
501 widget->allocation.x + widget->allocation.width -
502 b->right, widget->allocation.y + b->top,
503 b->right, side_borders_height);
506 /* If no toolbar, draw the bottom window border */
507 if (!currently_visible_toolbars && b->bottom > 0)
509 gtk_paint_box (widget->style, widget->window,
510 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
511 &event->area, widget, "bottom-border",
512 widget->allocation.x, widget->allocation.y +
513 (widget->allocation.height - b->bottom),
514 widget->allocation.width, b->bottom);
517 /* Draw the top border */
520 gtk_paint_box (widget->style, widget->window,
521 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
522 &event->area, widget, "top-border",
523 widget->allocation.x, widget->allocation.y,
524 widget->allocation.width, b->top);
530 /* don't draw the window stuff as it overwrites our borders with a blank
531 * rectangle. Instead start with the drawing of the GtkBin */
532 GTK_WIDGET_CLASS (g_type_class_peek_parent (parent_class))->expose_event (widget, event);
534 /* FIXME Not sure why this is commented out
535 * GTK_WIDGET_CLASS (parent_class))->
536 * expose_event (widget, event);
543 hildon_window_size_request (GtkWidget *widget,
544 GtkRequisition *requisition)
546 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (widget);
549 GtkWidget *child = GTK_BIN (widget)->child;
551 gint border_width = GTK_CONTAINER(widget)->border_width;
555 hildon_window_get_borders (HILDON_WINDOW (widget));
559 gtk_widget_size_request (child, requisition);
561 if (priv->vbox != NULL)
562 gtk_widget_size_request (priv->vbox, &req2);
564 requisition->height += req2.height;
565 requisition->width = (requisition->width < req2.width) ?
566 req2.width : requisition->width;
568 requisition->width += 2 * border_width;
569 requisition->height += 2 * border_width;
571 if (! priv->fullscreen)
573 requisition->height += priv->borders->top;
574 if (req2.height == 0)
575 requisition->height += priv->borders->bottom;
576 requisition->width += priv->borders->left + priv->borders->right;
581 hildon_window_size_allocate (GtkWidget *widget,
582 GtkAllocation *allocation)
584 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (widget);
587 GtkAllocation box_alloc;
588 GtkAllocation alloc = *allocation;
590 gint border_width = GTK_CONTAINER(widget)->border_width;
592 GtkWidget *box = priv->vbox;
593 GtkBin *bin = GTK_BIN(widget);
594 GtkBorder *b = priv->borders;
595 GtkBorder *tb = priv->toolbar_borders;
599 hildon_window_get_borders (HILDON_WINDOW (widget));
601 tb = priv->toolbar_borders;
604 widget->allocation = *allocation;
606 gtk_widget_get_child_requisition (box, &req);
608 box_alloc.width = allocation->width - tb->left - tb->right;
609 box_alloc.height = ( (req.height < allocation->height) ?
610 req.height : allocation->height );
611 box_alloc.x = allocation->x + tb->left;
612 box_alloc.y = allocation->y + allocation->height - box_alloc.height - tb->bottom;
614 if (bin->child != NULL && GTK_IS_WIDGET (bin->child)
615 && GTK_WIDGET_VISIBLE (bin->child))
617 alloc.x += border_width;
618 alloc.y += border_width;
619 alloc.width -= (border_width * 2);
620 alloc.height -= (border_width * 2) + box_alloc.height;
622 if (! priv->fullscreen)
625 alloc.width -= (b->left + b->right);
628 alloc.height -= b->top;
630 if (box_alloc.height <= 0)
631 alloc.height -= b->bottom;
633 alloc.height -= (tb->top + tb->bottom);
637 if (!(box_alloc.height <= 0))
638 alloc.height -= (tb->top + tb->bottom);
641 gtk_widget_size_allocate (bin->child, &alloc);
644 gtk_widget_size_allocate (box, &box_alloc);
646 if (priv->previous_vbox_y != box_alloc.y)
648 /* The size of the VBox has changed, we need to redraw part
649 * of the window borders */
650 gint draw_from_y = priv->previous_vbox_y < box_alloc.y?
651 priv->previous_vbox_y - tb->top:
652 box_alloc.y - tb->top;
654 gtk_widget_queue_draw_area (widget, 0, draw_from_y,
655 widget->allocation.width,
656 widget->allocation.height - draw_from_y);
658 priv->previous_vbox_y = box_alloc.y;
664 hildon_window_forall (GtkContainer *container,
665 gboolean include_internals,
666 GtkCallback callback,
667 gpointer callback_data)
669 HildonWindow *self = HILDON_WINDOW (container);
670 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
672 g_return_if_fail (callback != NULL);
675 GTK_CONTAINER_CLASS (parent_class)->forall (container, include_internals,
676 callback, callback_data);
677 if (include_internals && priv->vbox != NULL)
678 (* callback)(GTK_WIDGET (priv->vbox), callback_data);
682 hildon_window_show_all (GtkWidget *widget)
684 HildonWindow *self = HILDON_WINDOW (widget);
685 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
687 g_assert (priv != NULL);
689 GTK_WIDGET_CLASS (parent_class)->show_all (widget);
690 gtk_widget_show_all (priv->vbox);
694 hildon_window_destroy (GtkObject *obj)
696 HildonWindow *self = HILDON_WINDOW (obj);
697 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (obj);
700 g_assert (priv != NULL);
702 if (priv->vbox != NULL)
706 GtkWidget * common_toolbar =
707 GTK_WIDGET (hildon_program_get_common_toolbar (priv->program));
708 if (common_toolbar && common_toolbar->parent == priv->vbox)
710 gtk_container_remove (GTK_CONTAINER (priv->vbox),
715 gtk_widget_unparent (priv->vbox);
720 menu_list = g_list_copy (gtk_menu_get_for_attach_widget (GTK_WIDGET (obj)));
724 if (GTK_IS_MENU(menu_list->data))
726 if (GTK_WIDGET_VISIBLE (GTK_WIDGET (menu_list->data)))
728 gtk_menu_popdown (GTK_MENU (menu_list->data));
729 gtk_menu_shell_deactivate (GTK_MENU_SHELL (menu_list->data));
731 gtk_menu_detach (GTK_MENU (menu_list->data));
733 menu_list = menu_list->next;
736 g_list_free (menu_list);
740 hildon_program_remove_window (priv->program, self);
743 gdk_window_remove_filter (gdk_get_default_root_window(),
744 hildon_window_root_window_event_filter,
747 gtk_widget_set_events (GTK_WIDGET(obj), 0);
749 GTK_OBJECT_CLASS (parent_class)->destroy (obj);
754 hildon_window_notify (GObject *gobject,
757 HildonWindow *window = HILDON_WINDOW (gobject);
759 if (strcmp (param->name, "title") == 0)
762 hildon_window_update_title (window);
764 else if (strcmp (param->name, "is-topmost"))
766 hildon_window_is_topmost_notify (window);
769 if (G_OBJECT_CLASS(parent_class)->notify)
770 G_OBJECT_CLASS(parent_class)->notify (gobject, param);
775 visible_toolbar (gpointer data,
778 if (GTK_WIDGET_VISIBLE (((GtkBoxChild *)data)->widget))
779 (*((gint *)user_data))++;
783 find_findtoolbar_index (gpointer data,
786 gint *pass_bundle = (gint *)user_data;
788 if(((GtkBoxChild *)data)->widget->allocation.y < pass_bundle[0]
789 && GTK_WIDGET_VISIBLE (((GtkBoxChild *)data)->widget))
794 find_findtoolbar (gpointer data,
797 if(HILDON_IS_FIND_TOOLBAR (((GtkBoxChild *)data)->widget)
798 && GTK_WIDGET_VISIBLE (((GtkBoxChild *)data)->widget))
799 (*((GtkWidget **)user_data)) = ((GtkBoxChild *)data)->widget;
803 paint_toolbar (GtkWidget *widget,
805 GdkEventExpose * event,
808 gint toolbar_num = 0;
811 GtkWidget *findtoolbar = NULL;
812 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (widget);
813 gchar toolbar_mode[40];
814 GtkBorder *tb = priv->toolbar_borders;
816 g_assert (priv != NULL);
818 /* collect info to help on painting the boxes */
819 g_list_foreach (box->children, visible_toolbar,
820 (gpointer) &toolbar_num);
825 g_list_foreach (box->children, find_findtoolbar, (gpointer) &findtoolbar);
827 if (findtoolbar != NULL)
829 gint pass_bundle[2];/* an array for convient data passing
830 the first member contains the y allocation
831 of the find toolbar, and the second allocation
832 contains the index(how many toolbars are above
834 pass_bundle[0] = findtoolbar->allocation.y;
835 pass_bundle[1] = ftb_index;
836 g_list_foreach(box->children, find_findtoolbar_index,
837 (gpointer) pass_bundle);
838 ftb_index = pass_bundle[1];
842 sprintf (toolbar_mode, "toolbar%sframe-top",
843 fullscreen ? "-fullscreen-" : "-");
844 gtk_paint_box (widget->style, widget->window,
845 GTK_WIDGET_STATE (widget), GTK_SHADOW_OUT,
846 &event->area, widget, toolbar_mode,
847 widget->allocation.x,
848 GTK_WIDGET (box)->allocation.y - tb->top,
849 widget->allocation.width, tb->top);
851 /*top most toolbar painting*/
852 if (findtoolbar != NULL && ftb_index == 0 )
854 sprintf (toolbar_mode, "findtoolbar%s",
855 fullscreen ? "-fullscreen" : "");
857 gtk_paint_box (widget->style, widget->window,
858 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
859 &event->area, widget, toolbar_mode,
860 widget->allocation.x,
861 GTK_WIDGET(box)->allocation.y,
862 widget->allocation.width,
867 sprintf (toolbar_mode, "toolbar%s",
868 fullscreen ? "-fullscreen" : "");
870 gtk_paint_box (widget->style, widget->window,
871 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
872 &event->area, widget, toolbar_mode,
873 widget->allocation.x,
874 GTK_WIDGET(box)->allocation.y,
875 widget->allocation.width,
878 /*multi toolbar painting*/
879 for (count = 0; count < toolbar_num - 1; count++)
881 sprintf (toolbar_mode, "toolbar%sframe-middle",
882 fullscreen ? "-fullscreen-" : "-");
884 gtk_paint_box (widget->style, widget->window,
885 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
886 &event->area, widget, toolbar_mode,
887 widget->allocation.x,
888 GTK_WIDGET(box)->allocation.y +
889 (1 + count) * TOOLBAR_HEIGHT +
890 count * TOOLBAR_MIDDLE,
891 widget->allocation.width,
894 if (findtoolbar != NULL && count + 1 == ftb_index)
897 sprintf (toolbar_mode, "findtoolbar%s",
898 fullscreen ? "-fullscreen" : "");
900 gtk_paint_box (widget->style, widget->window,
901 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
902 &event->area, widget, toolbar_mode,
903 widget->allocation.x,
904 GTK_WIDGET(box)->allocation.y +
905 (1 + count) * (TOOLBAR_HEIGHT + TOOLBAR_MIDDLE),
906 widget->allocation.width,
911 sprintf (toolbar_mode, "toolbar%s",
912 fullscreen ? "-fullscreen" : "");
914 gtk_paint_box (widget->style, widget->window,
915 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
916 &event->area, widget, toolbar_mode,
917 widget->allocation.x,
918 GTK_WIDGET(box)->allocation.y +
919 (1 + count) * (TOOLBAR_HEIGHT + TOOLBAR_MIDDLE),
920 widget->allocation.width,
924 sprintf (toolbar_mode, "toolbar%sframe-bottom",
925 fullscreen ? "-fullscreen-" : "-");
927 gtk_paint_box (widget->style, widget->window,
928 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
929 &event->area, widget, toolbar_mode,
930 widget->allocation.x,
931 GTK_WIDGET(box)->allocation.y +
932 GTK_WIDGET(box)->allocation.height,
933 widget->allocation.width, tb->bottom);
937 * Checks the root window to know which is the topped window
940 hildon_window_get_active_window (void)
951 unsigned char *char_pointer;
953 Atom active_app_atom =
954 XInternAtom (GDK_DISPLAY (), "_MB_CURRENT_APP_WINDOW", False);
958 status = XGetWindowProperty (GDK_DISPLAY(), GDK_ROOT_WINDOW(),
959 active_app_atom, 0L, 16L,
960 0, XA_WINDOW, &realtype, &format,
961 &n, &extra, &win.char_pointer);
962 if (!(status == Success && realtype == XA_WINDOW && format == 32
963 && n == 1 && win.win != NULL))
966 XFree (win.char_pointer);
973 XFree(win.char_pointer);
979 xclient_message_type_check (XClientMessageEvent *cm,
982 return cm->message_type == XInternAtom(GDK_DISPLAY(), name, FALSE);
986 * Handle the window border custom button, which toggles the menu,
987 * and the Hildon input method copy paste messages
989 static GdkFilterReturn
990 hildon_window_event_filter (GdkXEvent *xevent,
994 XAnyEvent *eventti = xevent;
996 if (eventti->type == ClientMessage)
998 XClientMessageEvent *cm = xevent;
1000 if (xclient_message_type_check (cm, "_MB_GRAB_TRANSFER"))
1002 hildon_window_toggle_menu (HILDON_WINDOW ( data ));
1003 return GDK_FILTER_REMOVE;
1005 /* opera hack clipboard client message */
1006 else if (xclient_message_type_check (cm, "_HILDON_IM_CLIPBOARD_COPY"))
1008 g_signal_emit_by_name(G_OBJECT(data), "clipboard_operation",
1009 HILDON_WINDOW_CO_COPY);
1010 return GDK_FILTER_REMOVE;
1012 else if (xclient_message_type_check(cm, "_HILDON_IM_CLIPBOARD_CUT"))
1014 g_signal_emit_by_name(G_OBJECT(data), "clipboard_operation",
1015 HILDON_WINDOW_CO_CUT);
1016 return GDK_FILTER_REMOVE;
1018 else if (xclient_message_type_check(cm, "_HILDON_IM_CLIPBOARD_PASTE"))
1020 g_signal_emit_by_name(G_OBJECT(data), "clipboard_operation",
1021 HILDON_WINDOW_CO_PASTE);
1022 return GDK_FILTER_REMOVE;
1026 return GDK_FILTER_CONTINUE;
1030 * Here we keep track of changes in the _MB_CURRENT_APP_WINDOW,
1031 * to know when we acquire/lose topmost status
1033 static GdkFilterReturn
1034 hildon_window_root_window_event_filter (GdkXEvent *xevent,
1038 XAnyEvent *eventti = xevent;
1039 HildonWindow *hwindow = HILDON_WINDOW (data);
1041 if (eventti->type == PropertyNotify)
1043 XPropertyEvent *pevent = xevent;
1044 Atom active_app_atom =
1045 XInternAtom (GDK_DISPLAY (), "_MB_CURRENT_APP_WINDOW", False);
1047 if (pevent->atom == active_app_atom)
1049 Window active_window = hildon_window_get_active_window();
1051 hildon_window_update_topmost (hwindow, active_window);
1055 return GDK_FILTER_CONTINUE;
1059 * Handle the menu hardware key here
1062 hildon_window_key_press_event (GtkWidget *widget,
1065 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (widget);
1067 g_return_val_if_fail (HILDON_IS_WINDOW (widget),FALSE);
1070 switch (event->keyval)
1072 case HILDON_HARDKEY_MENU:
1073 if (hildon_window_toggle_menu (HILDON_WINDOW (widget)))
1076 case HILDON_HARDKEY_ESC:
1077 if (!priv->escape_timeout)
1079 priv->escape_timeout = g_timeout_add
1080 (HILDON_WINDOW_LONG_PRESS_TIME,
1081 hildon_window_escape_timeout, widget);
1086 return GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event);
1090 hildon_window_key_release_event (GtkWidget *widget,
1093 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (widget);
1095 g_return_val_if_fail (HILDON_IS_WINDOW (widget), FALSE);
1098 switch (event->keyval)
1100 case HILDON_HARDKEY_ESC:
1101 if (priv->escape_timeout)
1103 g_source_remove (priv->escape_timeout);
1104 priv->escape_timeout = 0;
1109 return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
1114 * We keep track of the window state changes, because the drawing
1115 * (borders) differs whether we are in fullscreen mode or not
1118 hildon_window_window_state_event (GtkWidget *widget,
1119 GdkEventWindowState *event)
1121 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (widget);
1122 g_assert (priv != NULL);
1124 if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
1125 priv->fullscreen = event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN;
1127 if (GTK_WIDGET_CLASS (parent_class)->window_state_event)
1129 return GTK_WIDGET_CLASS (parent_class)->window_state_event (
1141 hildon_window_title_notify (GObject *gobject,
1145 HildonWindow *window = HILDON_WINDOW (gobject);
1147 hildon_window_update_title (window);
1152 * The menu popuping needs a menu popup-function
1155 hildon_window_menu_popup_func (GtkMenu *menu,
1163 GdkWindow *window = GTK_WIDGET(widget)->window;
1167 gdk_window_get_origin (window, &window_x, &window_y);
1170 gtk_widget_style_get (GTK_WIDGET (menu), "horizontal-offset", x,
1171 "vertical-offset", y, NULL);
1179 hildon_window_menu_popup_func_full (GtkMenu *menu,
1185 gtk_widget_style_get (GTK_WIDGET (menu), "horizontal-offset", x,
1186 "vertical-offset", y, NULL);
1194 * Takes the common toolbar when we acquire the top-most status
1197 hildon_window_is_topmost_notify (HildonWindow *window)
1199 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (window);
1203 if (priv->is_topmost)
1205 hildon_window_take_common_toolbar (window);
1210 /* If the window lost focus while the user started to press
1211 * the ESC key, we won't get the release event. We need to
1212 * stop the timeout*/
1213 if (priv->escape_timeout)
1215 g_source_remove (priv->escape_timeout);
1216 priv->escape_timeout = 0;
1222 * Sets the program to which the window belongs. This should only be called
1223 * by hildon_program_add_window
1225 void G_GNUC_INTERNAL
1226 hildon_window_set_program (HildonWindow *self,
1229 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
1231 g_return_if_fail (HILDON_IS_WINDOW (self));
1232 g_assert (priv != NULL);
1236 g_object_unref (priv->program);
1239 /* Now that we are bound to a program, we can rely on it to track the
1241 gdk_window_remove_filter (gdk_get_default_root_window(),
1242 hildon_window_root_window_event_filter,
1245 priv->program = HILDON_PROGRAM (program);
1246 g_object_ref (program);
1250 * Unsets the program to which the window belongs. This should only be called
1251 * by hildon_program_add_window
1253 void G_GNUC_INTERNAL
1254 hildon_window_unset_program (HildonWindow *self)
1256 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
1258 g_return_if_fail(HILDON_IS_WINDOW (self));
1259 g_assert (priv != NULL);
1263 g_object_unref (priv->program);
1264 priv->program = NULL;
1266 /* We need to start tacking the root window again */
1267 gdk_window_set_events (gdk_get_default_root_window (),
1268 gdk_window_get_events (gdk_get_default_root_window ())
1269 | GDK_PROPERTY_CHANGE_MASK);
1271 gdk_window_add_filter (gdk_get_default_root_window (),
1272 hildon_window_root_window_event_filter, self );
1275 priv->program = NULL;
1279 * Sets whether or not the program to which this window belongs is
1280 * killable. This is used by the HildonProgram to signify to the
1281 * Task Navigator whether or not it can hibernate in memory-low situations
1283 void G_GNUC_INTERNAL
1284 hildon_window_set_can_hibernate_property (HildonWindow *self,
1285 gpointer _can_hibernate)
1287 GdkAtom killable_atom;
1288 gboolean can_hibernate;
1290 g_return_if_fail(self && HILDON_IS_WINDOW (self));
1292 if (!GTK_WIDGET_REALIZED ((GTK_WIDGET (self))))
1297 can_hibernate = * ((gboolean *)_can_hibernate);
1299 killable_atom = gdk_atom_intern (CAN_HIBERNATE_PROPERTY, FALSE);
1303 gdk_property_change (GTK_WIDGET (self)->window, killable_atom,
1304 (GdkAtom)31/* XA_STRING */, 8,
1305 GDK_PROP_MODE_REPLACE, (const guchar *)CAN_HIBERNATE,
1306 CAN_HIBERNATE_LENGTH);
1310 gdk_property_delete (GTK_WIDGET (self)->window, killable_atom);
1316 * If a common toolbar was set to the program, reparent it to
1319 void G_GNUC_INTERNAL
1320 hildon_window_take_common_toolbar (HildonWindow *self)
1322 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
1324 g_return_if_fail(HILDON_IS_WINDOW (self));
1329 GtkWidget *common_toolbar =
1330 GTK_WIDGET (hildon_program_get_common_toolbar (priv->program));
1332 if (common_toolbar && common_toolbar->parent != priv->vbox)
1334 g_object_ref (common_toolbar);
1335 if (common_toolbar->parent)
1337 gtk_container_remove (GTK_CONTAINER (common_toolbar->parent),
1341 gtk_box_pack_end (GTK_BOX(priv->vbox), common_toolbar,
1343 g_object_unref (common_toolbar);
1345 gtk_widget_set_size_request (common_toolbar, -1, TOOLBAR_HEIGHT);
1347 gtk_widget_show (priv->vbox);
1354 * Compare the window that was last topped, and act consequently
1357 hildon_window_update_topmost (HildonWindow *self,
1360 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
1364 g_return_if_fail (HILDON_IS_WINDOW (self));
1367 my_window = GDK_WINDOW_XID (GTK_WIDGET (self)->window);
1369 if (window_id == my_window)
1371 if (! priv->is_topmost)
1373 priv->is_topmost = TRUE;
1374 hildon_window_is_topmost_notify (self);
1375 g_object_notify (G_OBJECT (self), "is-topmost");
1378 else if (priv->is_topmost)
1380 /* Should this go in the signal handler? */
1381 GtkWidget *focus = gtk_window_get_focus (GTK_WINDOW (self));
1383 if (GTK_IS_ENTRY (focus))
1384 gtk_im_context_focus_out (GTK_ENTRY (focus)->im_context);
1385 if (GTK_IS_TEXT_VIEW (focus))
1386 gtk_im_context_focus_out (GTK_TEXT_VIEW (focus)->im_context);
1388 priv->is_topmost = FALSE;
1389 hildon_window_is_topmost_notify (self);
1390 g_object_notify (G_OBJECT (self), "is-topmost");
1395 * If the application
1396 * was given a name (with g_set_application_name(), set
1397 * "ProgramName - WindowTitle" as the displayed
1400 void G_GNUC_INTERNAL
1401 hildon_window_update_title (HildonWindow *window)
1403 const gchar * application_name;
1405 g_return_if_fail (HILDON_IS_WINDOW (window));
1407 if (!GTK_WIDGET_REALIZED (window))
1412 application_name = g_get_application_name ();
1414 if (application_name && application_name[0])
1416 const gchar *old_title = gtk_window_get_title (GTK_WINDOW (window));
1418 if (old_title && old_title[0])
1420 gchar *title = NULL;
1422 title = g_strjoin (TITLE_SEPARATOR, application_name,
1425 gdk_window_set_title (GTK_WIDGET (window)->window, title);
1434 detach_menu_func (GtkWidget *attach_widget,
1437 /* FIXME Why is this even needed here? */
1441 * Toggles the display of the HildonWindow menu.
1442 * Returns whether or not something was done (whether or not we had a menu
1446 hildon_window_toggle_menu (HildonWindow * self)
1448 GtkMenu *menu_to_use = NULL;
1449 GList *menu_children = NULL;
1450 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
1452 g_return_val_if_fail (HILDON_IS_WINDOW (self), FALSE);
1453 g_assert (priv != NULL);
1455 /* Select which menu to use, Window specific has highest priority,
1456 * then program specific */
1459 menu_to_use = GTK_MENU (priv->menu);
1461 else if (priv->program)
1463 menu_to_use = hildon_program_get_common_menu (priv->program);
1464 if (menu_to_use && gtk_menu_get_attach_widget (menu_to_use) !=
1467 g_object_ref (menu_to_use);
1468 if (gtk_menu_get_attach_widget (menu_to_use))
1470 gtk_menu_detach (menu_to_use);
1473 gtk_menu_attach_to_widget (menu_to_use, GTK_WIDGET (self),
1475 g_object_unref (menu_to_use);
1485 if (GTK_WIDGET_MAPPED (GTK_WIDGET (menu_to_use)))
1487 gtk_menu_popdown (menu_to_use);
1488 gtk_menu_shell_deactivate (GTK_MENU_SHELL (menu_to_use));
1492 /* Check if the menu has items */
1493 menu_children = gtk_container_get_children (GTK_CONTAINER (menu_to_use));
1497 g_list_free (menu_children);
1499 /* Apply right theming */
1500 gtk_widget_set_name (GTK_WIDGET (menu_to_use),
1501 "menu_force_with_corners");
1503 if (priv->fullscreen)
1505 gtk_menu_popup (menu_to_use, NULL, NULL,
1506 (GtkMenuPositionFunc)
1507 hildon_window_menu_popup_func_full,
1509 gtk_get_current_event_time ());
1513 gtk_menu_popup (menu_to_use, NULL, NULL,
1514 (GtkMenuPositionFunc)
1515 hildon_window_menu_popup_func,
1517 gtk_get_current_event_time ());
1519 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu_to_use), TRUE);
1527 * If the ESC key was not released when the timeout expires,
1531 hildon_window_escape_timeout (gpointer data)
1533 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (data);
1538 GDK_THREADS_ENTER ();
1540 /* Send fake event, simulation a situation that user
1541 pressed 'x' from the corner */
1542 event = gdk_event_new(GDK_DELETE);
1543 ((GdkEventAny *)event)->window = GDK_WINDOW (g_object_ref (GTK_WIDGET(data)->window));
1544 gtk_main_do_event(event);
1546 /* That unrefs the window, so we're reffing it above */
1547 gdk_event_free(event);
1549 priv->escape_timeout = 0;
1551 GDK_THREADS_LEAVE ();
1558 * hildon_window_new:
1560 * Creates a new HildonWindow.
1562 * Return value: A @HildonWindow.
1565 hildon_window_new (void)
1567 HildonWindow *newwindow = g_object_new (HILDON_TYPE_WINDOW, NULL);
1569 return GTK_WIDGET (newwindow);
1573 * hildon_window_add_with_scrollbar
1574 * @self : A @HildonWindow
1575 * @child : A @GtkWidget
1577 * Adds the @child to the HildonWindow and creates a scrollbar
1578 * for it. Similar as adding first a @GtkScrolledWindow and then the
1582 hildon_window_add_with_scrollbar (HildonWindow *self,
1585 GtkScrolledWindow *scrolledw;
1587 g_return_if_fail (HILDON_IS_WINDOW (self));
1588 g_return_if_fail (GTK_IS_WIDGET (child));
1589 g_return_if_fail (child->parent == NULL);
1591 scrolledw = GTK_SCROLLED_WINDOW (gtk_scrolled_window_new (NULL, NULL));
1592 gtk_scrolled_window_set_policy (scrolledw, GTK_POLICY_NEVER,
1593 GTK_POLICY_AUTOMATIC);
1594 gtk_scrolled_window_set_shadow_type (scrolledw, GTK_SHADOW_NONE);
1596 if (GTK_IS_VIEWPORT (child))
1597 gtk_container_add (GTK_CONTAINER (scrolledw), child);
1600 if (GTK_IS_CONTAINER (child) )
1601 gtk_container_set_focus_vadjustment (GTK_CONTAINER(child),
1602 gtk_scrolled_window_get_vadjustment (scrolledw) );
1603 gtk_scrolled_window_add_with_viewport (scrolledw, child);
1606 gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (scrolledw));
1610 * hildon_window_add_toolbar:
1611 * @self: A @HildonWindow
1612 * @toolbar: A #GtkToolbar to add to the HildonWindow
1614 * Adds a toolbar to the window.
1617 hildon_window_add_toolbar (HildonWindow *self,
1618 GtkToolbar *toolbar)
1621 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
1623 g_return_if_fail (HILDON_IS_WINDOW (self));
1624 g_return_if_fail (toolbar && GTK_IS_TOOLBAR (toolbar));
1627 vbox = GTK_BOX (priv->vbox);
1629 gtk_box_pack_start (vbox, GTK_WIDGET(toolbar), TRUE, TRUE, 0);
1630 gtk_box_reorder_child (vbox, GTK_WIDGET(toolbar), 0);
1631 gtk_widget_set_size_request (GTK_WIDGET (toolbar), -1, TOOLBAR_HEIGHT);
1633 gtk_widget_queue_resize (GTK_WIDGET(self));
1637 * hildon_window_remove_toolbar:
1638 * @self: A @HildonWindow
1639 * @toolbar: A #GtkToolbar to remove from the HildonWindow
1641 * Removes a toolbar from the window.
1644 hildon_window_remove_toolbar (HildonWindow *self,
1645 GtkToolbar *toolbar)
1647 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
1649 g_return_if_fail (HILDON_IS_WINDOW (self));
1652 gtk_container_remove (GTK_CONTAINER (priv->vbox), GTK_WIDGET(toolbar));
1656 * hildon_window_get_menu:
1657 * @self : #HildonWindow
1659 * Gets the #GtMenu assigned to the #HildonAppview.
1661 * Return value: The #GtkMenu assigned to this application view.
1664 hildon_window_get_menu (HildonWindow * self)
1666 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
1668 g_return_val_if_fail (HILDON_IS_WINDOW (self), NULL);
1671 return GTK_MENU (priv->menu);
1675 * hildon_window_set_menu:
1676 * @self: A #HildonWindow
1677 * @menu: The #GtkMenu to be used for this #HildonWindow
1679 * Sets the menu to be used for this window. This menu overrides
1680 * a program-wide menu that may have been set with
1681 * hildon_program_set_common_menu. Pass NULL to remove the current
1685 hildon_window_set_menu (HildonWindow *self,
1688 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
1690 g_return_if_fail (HILDON_IS_WINDOW (self));
1693 if (priv->menu != NULL) {
1694 gtk_menu_detach (GTK_MENU (priv->menu));
1695 g_object_unref (priv->menu);
1698 priv->menu = (menu != NULL) ? GTK_WIDGET (menu) : NULL;
1699 if (priv->menu != NULL) {
1700 gtk_widget_set_name (priv->menu, "menu_force_with_corners");
1701 gtk_menu_attach_to_widget (GTK_MENU (priv->menu), GTK_WIDGET (self), &detach_menu_func);
1702 g_object_ref (GTK_MENU (priv->menu));
1703 gtk_widget_show_all (GTK_WIDGET (priv->menu));
1708 * hildon_window_get_is_topmost:
1709 * @self: A #HildonWindow
1711 * Return value: Whether or not the #HildonWindow is currenltly activated
1712 * by the window manager.
1715 hildon_window_get_is_topmost (HildonWindow *self)
1717 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
1719 g_return_val_if_fail (HILDON_IS_WINDOW (self), FALSE);
1722 return priv->is_topmost;