2 * This file is part of hildon-libs
4 * Copyright (C) 2006 Nokia Corporation.
6 * Contact: Luc Pionchon <luc.pionchon@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; either version 2.1 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
30 #include "hildon-app.h"
31 #include <hildon-window.h>
32 #include "hildon-program.h"
33 #include "hildon-window-private.h"
34 #include <hildon-find-toolbar.h>
36 #include <gtk/gtkmenu.h>
37 #include <gtk/gtkimcontext.h>
38 #include <gtk/gtkmenuitem.h>
39 #include <gtk/gtkcheckmenuitem.h>
40 #include <gtk/gtkmenushell.h>
41 #include <gtk/gtkwindow.h>
42 #include <gtk/gtkwidget.h>
43 #include <gtk/gtkvbox.h>
44 #include <gtk/gtklabel.h>
45 #include <gtk/gtkentry.h>
46 #include <gtk/gtktextview.h>
47 #include <gtk/gtkscrolledwindow.h>
48 #include <gtk/gtkmain.h>
49 #include <gdk/gdkkeysyms.h>
52 #include<gtk/gtkprivate.h>
56 #include <X11/Xatom.h>
60 #define _(String) gettext(String)
62 /*The size of screen*/
63 #define WINDOW_HEIGHT 480
64 #define WINDOW_WIDTH 800
66 #define NAVIGATOR_HEIGHT WINDOW_HEIGHT
68 #define APPVIEW_HEIGHT 396
69 #define APPVIEW_WIDTH 672
71 #define TOOLBAR_HEIGHT 40
72 #define TOOLBAR_MIDDLE 10
73 #define TOOLBAR_WIDTH APPVIEW_WIDTH
76 #define CAN_HIBERNATE "CANKILL"
77 #define CAN_HIBERNATE_LENGTH 7
79 #define CAN_HIBERNATE_PROPERTY "_HILDON_ABLE_TO_HIBERNATE"
81 #define TITLE_SEPARATOR " - "
84 #define HILDON_WINDOW_GET_PRIVATE(obj) \
85 (G_TYPE_INSTANCE_GET_PRIVATE ((obj),\
86 HILDON_TYPE_WINDOW, HildonWindowPrivate))
88 static GtkWindowClass *parent_class;
91 hildon_window_init (HildonWindow * self);
94 hildon_window_class_init (HildonWindowClass * window_class);
97 hildon_window_menupopupfunc (GtkMenu *menu, gint *x, gint *y,
101 hildon_window_menupopupfuncfull (GtkMenu *menu, gint *x, gint *y,
105 hildon_window_expose (GtkWidget * widget, GdkEventExpose * event);
107 hildon_window_forall (GtkContainer * container,
108 gboolean include_internals,
109 GtkCallback callback,
110 gpointer callback_data);
112 hildon_window_show_all (GtkWidget *widget);
115 hildon_window_size_allocate (GtkWidget * widget,
116 GtkAllocation * allocation);
118 hildon_window_size_request (GtkWidget * widget,
119 GtkRequisition * requisition);
121 hildon_window_finalize (GObject * obj_self);
123 hildon_window_set_property (GObject * object, guint property_id,
124 const GValue * value, GParamSpec * pspec);
126 hildon_window_get_property (GObject * object, guint property_id,
127 GValue * value, GParamSpec * pspec);
129 hildon_window_destroy (GtkObject *obj);
131 hildon_window_realize (GtkWidget *widget);
133 hildon_window_unrealize (GtkWidget *widget);
135 hildon_window_key_press_event (GtkWidget *widget,
138 hildon_window_key_release_event (GtkWidget *widget,
141 hildon_window_window_state_event (GtkWidget *widget,
142 GdkEventWindowState *event,
146 hildon_window_title_notify (GObject *gobject,
150 hildon_window_is_topmost_notify (GObject *self,
151 GParamSpec *property_spec,
155 hildon_window_vbox_expose_event (GtkWidget *vbox,
156 GdkEventExpose *event,
160 hildon_window_toggle_menu (HildonWindow * self);
163 hildon_window_escape_timeout (gpointer data);
165 static GdkFilterReturn
166 hildon_window_event_filter (GdkXEvent *xevent, GdkEvent *event, gpointer data );
168 static GdkFilterReturn
169 hildon_window_root_window_event_filter (GdkXEvent *xevent,
174 hildon_window_get_borders (HildonWindow *window);
177 visible_toolbar (gpointer data, gpointer user_data);
179 paint_toolbar (GtkWidget *widget, GtkBox *box,
180 GdkEventExpose * event,
181 gboolean fullscreen);
183 typedef void (*HildonWindowSignal) (HildonWindow *, gint, gpointer);
200 struct _HildonWindowPrivate
206 GtkBorder *toolbar_borders;
208 GtkAllocation allocation;
212 guint escape_timeout;
213 gint visible_toolbars;
214 gint previous_vbox_y;
216 HildonProgram *program;
220 hildon_window_get_type (void)
222 static GType window_type = 0;
225 static const GTypeInfo window_info = {
226 sizeof(HildonWindowClass),
227 NULL, /* base_init */
228 NULL, /* base_finalize */
229 (GClassInitFunc) hildon_window_class_init,
230 NULL, /* class_finalize */
231 NULL, /* class_data */
232 sizeof(HildonWindow),
234 (GInstanceInitFunc) hildon_window_init,
236 window_type = g_type_register_static(GTK_TYPE_WINDOW,
243 /* Virtual methods */
246 hildon_window_class_init (HildonWindowClass * window_class)
248 /* Get convenience variables */
249 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (window_class);
250 GObjectClass *object_class = G_OBJECT_CLASS (window_class);
251 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (window_class);
253 /* Set the global parent_class here */
254 parent_class = g_type_class_peek_parent (window_class);
256 object_class->set_property = hildon_window_set_property;
257 object_class->get_property = hildon_window_get_property;
259 /* Set the widgets virtual functions */
260 widget_class->size_allocate = hildon_window_size_allocate;
261 widget_class->size_request = hildon_window_size_request;
262 widget_class->expose_event = hildon_window_expose;
263 widget_class->show_all = hildon_window_show_all;
264 widget_class->realize = hildon_window_realize;
265 widget_class->unrealize = hildon_window_unrealize;
266 widget_class->key_press_event = hildon_window_key_press_event;
267 widget_class->key_release_event = hildon_window_key_release_event;
269 /* now the object stuff */
270 object_class->finalize = hildon_window_finalize;
272 /* To the container */
273 container_class->forall = hildon_window_forall;
276 GTK_OBJECT_CLASS (window_class)->destroy = hildon_window_destroy;
278 g_type_class_add_private (window_class,
279 sizeof (struct _HildonWindowPrivate));
281 /* Install properties */
282 g_object_class_install_property (object_class, PROP_IS_TOPMOST,
283 g_param_spec_boolean ("is-topmost",
285 "Whether the window is currently activated by the window "
290 gtk_widget_class_install_style_property (widget_class,
291 g_param_spec_boxed ("borders",
293 "Size of graphical window borders",
297 gtk_widget_class_install_style_property (widget_class,
298 g_param_spec_boxed ("toolbar-borders",
299 "Graphical toolbar borders",
300 "Size of graphical toolbar borders",
307 hildon_window_init (HildonWindow * self)
309 HildonWindowPrivate *priv = self->priv = HILDON_WINDOW_GET_PRIVATE(self);
311 self->priv->vbox = gtk_vbox_new (TRUE, 10);
312 gtk_widget_set_parent (self->priv->vbox, GTK_WIDGET(self));
314 priv->visible_toolbars = 0;
315 priv->is_topmost = FALSE;
316 priv->borders = NULL;
317 priv->toolbar_borders = NULL;
318 priv->escape_timeout = 0;
320 priv->fullscreen = FALSE;
322 priv->program = NULL;
323 gtk_widget_set_events (GTK_WIDGET(self), GDK_BUTTON_PRESS_MASK |
324 GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK);
326 /* Handle the drawing of the vbox (add the pixmap borders) */
327 g_signal_connect (G_OBJECT (self->priv->vbox), "expose-event",
328 G_CALLBACK (hildon_window_vbox_expose_event),
331 /* Used to keep track of fullscreen / unfullscreen */
332 g_signal_connect (G_OBJECT (self), "window_state_event",
333 G_CALLBACK (hildon_window_window_state_event), self);
335 g_signal_connect (G_OBJECT (self), "notify::title",
336 G_CALLBACK (hildon_window_title_notify), self);
338 g_signal_connect (G_OBJECT (self), "notify::is-topmost",
339 G_CALLBACK (hildon_window_is_topmost_notify), self);
341 /* We need to track the root window _MB_CURRENT_APP_WINDOW property */
342 gdk_window_set_events (gdk_get_default_root_window (),
343 gdk_window_get_events (gdk_get_default_root_window ()) | GDK_PROPERTY_CHANGE_MASK);
345 gdk_window_add_filter (gdk_get_default_root_window (),
346 hildon_window_root_window_event_filter, self);
350 hildon_window_finalize (GObject * obj_self)
353 g_return_if_fail (HILDON_WINDOW (obj_self));
354 self = HILDON_WINDOW (obj_self);
357 if (G_OBJECT_CLASS (parent_class)->finalize)
358 G_OBJECT_CLASS (parent_class)->finalize (obj_self);
363 hildon_window_realize (GtkWidget *widget)
365 Atom *old_atoms, *new_atoms;
369 Window active_window;
371 GTK_WIDGET_CLASS (parent_class)->realize (widget);
373 gtk_widget_realize (GTK_WIDGET (HILDON_WINDOW (widget)->priv->vbox));
376 /* catch the custom button signal from mb to display the menu */
377 gdk_window_add_filter (widget->window, hildon_window_event_filter, widget );
379 window = GDK_WINDOW_XID ( widget->window );
380 disp = GDK_WINDOW_XDISPLAY ( widget->window );
382 /* Enable custom button that is used for menu */
383 XGetWMProtocols (disp, window, &old_atoms, &atom_count);
384 new_atoms = g_new (Atom, atom_count + 1);
386 memcpy (new_atoms, old_atoms, sizeof(Atom) * atom_count);
388 new_atoms[atom_count++] =
389 XInternAtom (disp, "_NET_WM_CONTEXT_CUSTOM", False);
391 XSetWMProtocols (disp, window, new_atoms, atom_count);
396 /* rely on GDK to set the window group to its default */
397 gdk_window_set_group (widget->window, NULL);
399 if (HILDON_WINDOW (widget)->priv->program)
401 gboolean can_hibernate = hildon_program_get_can_hibernate (
402 HILDON_WINDOW (widget)->priv->program);
404 hildon_window_set_can_hibernate_property (HILDON_WINDOW (widget),
408 /* Update the topmost status */
409 active_window = hildon_window_get_active_window();
410 hildon_window_update_topmost (HILDON_WINDOW (widget), active_window);
412 /* Update the window title */
413 hildon_window_update_title(HILDON_WINDOW (widget));
418 hildon_window_unrealize (GtkWidget *widget)
421 gdk_window_remove_filter (widget->window, hildon_window_event_filter,
424 gtk_widget_unrealize (GTK_WIDGET (HILDON_WINDOW (widget)->priv->vbox));
425 GTK_WIDGET_CLASS(parent_class)->unrealize(widget);
429 hildon_window_set_property (GObject * object, guint property_id,
430 const GValue * value, GParamSpec * pspec)
432 /*HildonWindow *window = HILDON_WINDOW (object);*/
434 switch (property_id) {
437 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
443 hildon_window_get_property (GObject * object, guint property_id,
444 GValue * value, GParamSpec * pspec)
446 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (object);
448 switch (property_id) {
450 case PROP_IS_TOPMOST:
451 g_value_set_boolean (value, priv->is_topmost);
455 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
461 * Retrieve the graphical borders size used by the themes
464 hildon_window_get_borders (HildonWindow *window)
467 gtk_widget_style_get (GTK_WIDGET (window), "borders",&window->priv->borders,
468 "toolbar-borders", &window->priv->toolbar_borders,
471 if (!window->priv->borders)
473 window->priv->borders = (GtkBorder *)g_malloc0 (sizeof (GtkBorder));
476 if (!window->priv->toolbar_borders)
478 window->priv->toolbar_borders =
479 (GtkBorder *)g_malloc0 (sizeof (GtkBorder));
484 visible_toolbars (gpointer data, gpointer user_data)
486 if (GTK_WIDGET_VISIBLE (GTK_WIDGET (((GtkBoxChild *)data)->widget)))
487 (*((gint *)user_data)) ++;
491 hildon_window_expose (GtkWidget * widget, GdkEventExpose * event)
493 HildonWindowPrivate *priv = HILDON_WINDOW (widget)->priv;
494 GtkWidget *bx = HILDON_WINDOW(widget)->priv->vbox;
495 GtkBox *box = GTK_BOX(bx);
496 GtkBorder *b = HILDON_WINDOW(widget)->priv->borders;
497 GtkBorder *tb = HILDON_WINDOW(widget)->priv->toolbar_borders;
499 gint currently_visible_toolbars = 0;
503 hildon_window_get_borders (HILDON_WINDOW (widget));
506 tb_height = bx->allocation.height + tb->top + tb->bottom;
508 g_list_foreach (box->children, visible_toolbars,
509 ¤tly_visible_toolbars);
511 if (priv->previous_vbox_y != priv->vbox->allocation.y)
513 gint draw_from_y = priv->previous_vbox_y < priv->vbox->allocation.y?
514 priv->previous_vbox_y - tb->top:
515 priv->vbox->allocation.y - tb->top;
517 gtk_widget_queue_draw_area (widget, 0, draw_from_y,
518 widget->allocation.width,
519 widget->allocation.height - draw_from_y);
521 priv->previous_vbox_y = priv->vbox->allocation.y;
524 if (!HILDON_WINDOW (widget)->priv->fullscreen)
527 /* Draw the left and right window border */
528 gint side_borders_height = widget->allocation.height - b->top;
530 if (currently_visible_toolbars)
531 side_borders_height -= tb_height;
533 side_borders_height -= b->bottom;
537 gtk_paint_box (widget->style, widget->window,
538 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
539 &event->area, widget, "left-border",
540 widget->allocation.x, widget->allocation.y +
541 b->top, b->left, side_borders_height);
546 gtk_paint_box (widget->style, widget->window,
547 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
548 &event->area, widget, "right-border",
549 widget->allocation.x + widget->allocation.width -
550 b->right, widget->allocation.y + b->top,
551 b->right, side_borders_height);
554 /* If no toolbar, draw the bottom window border */
555 if (!currently_visible_toolbars &&b->bottom > 0)
557 gtk_paint_box (widget->style, widget->window,
558 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
559 &event->area, widget, "bottom-border",
560 widget->allocation.x, widget->allocation.y +
561 (widget->allocation.height - b->bottom),
562 widget->allocation.width, b->bottom);
565 /* Draw the top border */
568 gtk_paint_box (widget->style, widget->window,
569 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
570 &event->area, widget, "top-border",
571 widget->allocation.x, widget->allocation.y,
572 widget->allocation.width, b->top);
578 /* don't draw the window stuff as it overwrites our borders with a blank
579 * rectangle. Instead start with the drawing of the GtkBin */
580 GTK_WIDGET_CLASS (g_type_class_peek_parent (parent_class))->
581 expose_event (widget, event);
588 hildon_window_size_request (GtkWidget * widget, GtkRequisition * requisition)
590 HildonWindowPrivate *priv = HILDON_WINDOW (widget)->priv;
591 GtkWidget *child = GTK_BIN (widget)->child;
593 gint border_width = GTK_CONTAINER(widget)->border_width;
597 hildon_window_get_borders (HILDON_WINDOW (widget));
601 gtk_widget_size_request (child, requisition);
603 if (HILDON_WINDOW (widget)->priv->vbox != NULL)
604 gtk_widget_size_request (HILDON_WINDOW (widget)->priv->vbox,
607 requisition->height += req2.height;
608 requisition->width = (requisition->width < req2.width) ?
609 req2.width : requisition->width;
612 requisition->width += 2 * border_width;
613 requisition->height += 2 * border_width;
615 if (!priv->fullscreen)
617 requisition->height += priv->borders->top;
618 if (req2.height == 0)
619 requisition->height += priv->borders->bottom;
620 requisition->width += priv->borders->left + priv->borders->right;
625 hildon_window_size_allocate (GtkWidget * widget, GtkAllocation * allocation)
627 HildonWindowPrivate *priv = HILDON_WINDOW (widget)->priv;
628 GtkAllocation box_alloc;
629 GtkAllocation alloc = *allocation;
631 gint border_width = GTK_CONTAINER(widget)->border_width;
633 GtkWidget *box = HILDON_WINDOW(widget)->priv->vbox;
634 GtkBin *bin = GTK_BIN(widget);
635 GtkBorder *b = HILDON_WINDOW (widget)->priv->borders;
636 GtkBorder *tb = HILDON_WINDOW (widget)->priv->toolbar_borders;
640 hildon_window_get_borders (HILDON_WINDOW (widget));
643 widget->allocation = *allocation;
645 gtk_widget_size_request (box, &req);
647 box_alloc.width = allocation->width - tb->left - tb->right;
648 box_alloc.height = ( (req.height < allocation->height) ?
649 req.height : allocation->height );
650 box_alloc.x = allocation->x + tb->left;
651 box_alloc.y = allocation->y + allocation->height - box_alloc.height - tb->bottom;
654 if (bin->child != NULL && GTK_IS_WIDGET (bin->child))
656 alloc.x += border_width;
657 alloc.y += border_width;
658 alloc.width -= (border_width * 2);
659 alloc.height -= (border_width * 2) + box_alloc.height;
661 if (!(HILDON_WINDOW (widget)->priv->fullscreen))
664 alloc.width -= (b->left + b->right);
667 alloc.height -= b->top;
670 if (box_alloc.height <= 0 &&
671 !(HILDON_WINDOW (widget)->priv->fullscreen))
672 alloc.height -= b->bottom;
674 alloc.height -= (tb->top + tb->bottom);
676 gtk_widget_size_allocate (bin->child, &alloc);
680 gtk_widget_size_allocate (box, &box_alloc);
684 hildon_window_forall (GtkContainer * container, gboolean include_internals,
685 GtkCallback callback, gpointer callback_data)
687 HildonWindow *self = HILDON_WINDOW (container);
689 g_return_if_fail (callback != NULL);
691 GTK_CONTAINER_CLASS (parent_class)->forall (container, include_internals,
692 callback, callback_data);
693 if (include_internals && self->priv->vbox != NULL)
694 (* callback)(GTK_WIDGET (self->priv->vbox), callback_data);
698 hildon_window_show_all (GtkWidget *widget)
700 HildonWindow *self = HILDON_WINDOW (widget);
702 GTK_WIDGET_CLASS (parent_class)->show_all (widget);
703 gtk_widget_show_all (self->priv->vbox);
708 hildon_window_destroy (GtkObject *obj)
710 HildonWindow *self = HILDON_WINDOW (obj);
713 if (self->priv->vbox != NULL)
715 if (self->priv->program)
717 GtkWidget * common_toolbar = GTK_WIDGET (
718 hildon_program_get_common_toolbar (self->priv->program));
719 if (common_toolbar && common_toolbar->parent == self->priv->vbox)
721 gtk_container_remove (GTK_CONTAINER (self->priv->vbox),
726 gtk_widget_unparent (self->priv->vbox);
727 self->priv->vbox = NULL;
731 menu_list = g_list_copy (gtk_menu_get_for_attach_widget (GTK_WIDGET (obj)));
735 if (GTK_IS_MENU(menu_list->data))
737 if (GTK_WIDGET_VISIBLE (GTK_WIDGET (menu_list->data)))
739 gtk_menu_popdown (GTK_MENU (menu_list->data));
740 gtk_menu_shell_deactivate (GTK_MENU_SHELL (menu_list->data));
742 gtk_menu_detach (GTK_MENU (menu_list->data));
744 menu_list = menu_list->next;
747 g_list_free (menu_list);
749 if (self->priv->program)
751 hildon_program_remove_window (self->priv->program, self);
754 gdk_window_remove_filter (gdk_get_default_root_window(),
755 hildon_window_root_window_event_filter,
758 gtk_widget_set_events (GTK_WIDGET(obj), 0);
760 GTK_OBJECT_CLASS (parent_class)->destroy (obj);
764 hildon_window_is_topmost_notify (GObject *self,
765 GParamSpec *property_spec,
768 HildonWindow *window = HILDON_WINDOW (self);
770 if (window->priv->is_topmost)
772 hildon_window_take_common_toolbar (window);
777 /* If the window lost focus while the user started to press
778 * the ESC key, we won't get the release event. We need to
780 if (window->priv->escape_timeout)
782 g_source_remove (window->priv->escape_timeout);
783 window->priv->escape_timeout = 0;
790 hildon_window_vbox_expose_event (GtkWidget *vbox,
791 GdkEventExpose *event,
794 HildonWindowPrivate *priv = HILDON_WINDOW (window)->priv;
796 hildon_window_get_borders (HILDON_WINDOW(window));
798 event->area.x -= priv->toolbar_borders->left;
799 event->area.y -= priv->toolbar_borders->top;
800 event->area.width += (priv->toolbar_borders->left +
801 priv->toolbar_borders->right);
802 event->area.height += (priv->toolbar_borders->top +
803 priv->toolbar_borders->bottom);
805 paint_toolbar (GTK_WIDGET (window), GTK_BOX (vbox), event, priv->fullscreen);
807 GTK_WIDGET_CLASS (G_TYPE_INSTANCE_GET_CLASS (vbox, GTK_TYPE_VBOX, GtkVBox))
808 ->expose_event (vbox, event);
810 event->area = GTK_WIDGET(window)->allocation;
819 visible_toolbar (gpointer data, gpointer user_data)
821 if (GTK_WIDGET_VISIBLE (((GtkBoxChild *)data)->widget))
822 (*((gint *)user_data))++;
826 find_findtoolbar_index (gpointer data, gpointer user_data)
828 gint *pass_bundle = (gint *)user_data;
830 if(((GtkBoxChild *)data)->widget->allocation.y < pass_bundle[0]
831 && GTK_WIDGET_VISIBLE (((GtkBoxChild *)data)->widget))
836 find_findtoolbar (gpointer data, gpointer user_data)
838 if(HILDON_IS_FIND_TOOLBAR (((GtkBoxChild *)data)->widget)
839 && GTK_WIDGET_VISIBLE (((GtkBoxChild *)data)->widget))
840 (*((GtkWidget **)user_data)) = ((GtkBoxChild *)data)->widget;
844 paint_toolbar (GtkWidget *widget, GtkBox *box,
845 GdkEventExpose * event,
848 gint toolbar_num = 0;
851 GtkWidget *findtoolbar = NULL;
852 gchar toolbar_mode[40];
853 GtkBorder *tb = HILDON_WINDOW (widget)->priv->toolbar_borders;
855 /* collect info to help on painting the boxes */
856 g_list_foreach (box->children, visible_toolbar,
857 (gpointer) &toolbar_num);
862 g_list_foreach (box->children, find_findtoolbar, (gpointer) &findtoolbar);
864 if (findtoolbar != NULL)
866 gint pass_bundle[2];/* an array for convient data passing
867 the first member contains the y allocation
868 of the find toolbar, and the second allocation
869 contains the index(how many toolbars are above
871 pass_bundle[0] = findtoolbar->allocation.y;
872 pass_bundle[1] = ftb_index;
873 g_list_foreach(box->children, find_findtoolbar_index,
874 (gpointer) pass_bundle);
875 ftb_index = pass_bundle[1];
879 sprintf (toolbar_mode, "toolbar%sframe-top",
880 fullscreen ? "-fullscreen-" : "-");
881 gtk_paint_box (widget->style, widget->window,
882 GTK_WIDGET_STATE (widget), GTK_SHADOW_OUT,
883 &event->area, widget, toolbar_mode,
884 widget->allocation.x,
885 GTK_WIDGET (box)->allocation.y - tb->top,
886 widget->allocation.width, tb->top);
888 /*top most toolbar painting*/
889 if (findtoolbar != NULL && ftb_index == 0 )
891 sprintf (toolbar_mode, "findtoolbar%s",
892 fullscreen ? "-fullscreen" : "");
894 gtk_paint_box (widget->style, widget->window,
895 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
896 &event->area, widget, toolbar_mode,
897 widget->allocation.x,
898 GTK_WIDGET(box)->allocation.y,
899 widget->allocation.width,
904 sprintf (toolbar_mode, "toolbar%s",
905 fullscreen ? "-fullscreen" : "");
907 gtk_paint_box (widget->style, widget->window,
908 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
909 &event->area, widget, toolbar_mode,
910 widget->allocation.x,
911 GTK_WIDGET(box)->allocation.y,
912 widget->allocation.width,
915 /*multi toolbar painting*/
916 for (count = 0; count < toolbar_num - 1; count++)
918 sprintf (toolbar_mode, "toolbar%sframe-middle",
919 fullscreen ? "-fullscreen-" : "-");
921 gtk_paint_box (widget->style, widget->window,
922 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
923 &event->area, widget, toolbar_mode,
924 widget->allocation.x,
925 GTK_WIDGET(box)->allocation.y +
926 (1 + count) * TOOLBAR_HEIGHT +
927 count * TOOLBAR_MIDDLE,
928 widget->allocation.width,
931 if (findtoolbar != NULL && count + 1 == ftb_index)
934 sprintf (toolbar_mode, "findtoolbar%s",
935 fullscreen ? "-fullscreen" : "");
937 gtk_paint_box (widget->style, widget->window,
938 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
939 &event->area, widget, toolbar_mode,
940 widget->allocation.x,
941 GTK_WIDGET(box)->allocation.y +
942 (1 + count) * (TOOLBAR_HEIGHT + TOOLBAR_MIDDLE),
943 widget->allocation.width,
948 sprintf (toolbar_mode, "toolbar%s",
949 fullscreen ? "-fullscreen" : "");
951 gtk_paint_box (widget->style, widget->window,
952 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
953 &event->area, widget, toolbar_mode,
954 widget->allocation.x,
955 GTK_WIDGET(box)->allocation.y +
956 (1 + count) * (TOOLBAR_HEIGHT + TOOLBAR_MIDDLE),
957 widget->allocation.width,
961 sprintf (toolbar_mode, "toolbar%sframe-bottom",
962 fullscreen ? "-fullscreen-" : "-");
964 gtk_paint_box (widget->style, widget->window,
965 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
966 &event->area, widget, toolbar_mode,
967 widget->allocation.x,
968 GTK_WIDGET(box)->allocation.y +
969 GTK_WIDGET(box)->allocation.height,
970 widget->allocation.width, tb->bottom);
975 * Checks the root window to know which is the topped window
978 hildon_window_get_active_window (void)
989 unsigned char *char_pointer;
991 Atom active_app_atom =
992 XInternAtom (GDK_DISPLAY (), "_MB_CURRENT_APP_WINDOW", False);
996 status = XGetWindowProperty (GDK_DISPLAY(), GDK_ROOT_WINDOW(),
997 active_app_atom, 0L, 16L,
998 0, XA_WINDOW, &realtype, &format,
999 &n, &extra, &win.char_pointer);
1000 if (!(status == Success && realtype == XA_WINDOW && format == 32
1001 && n == 1 && win.win != NULL))
1003 if (win.win != NULL)
1004 XFree (win.char_pointer);
1005 return GDK_FILTER_CONTINUE;
1010 if (win.win != NULL)
1011 XFree(win.char_pointer);
1022 * Handle the window border custom button, which toggles the menu
1024 static GdkFilterReturn
1025 hildon_window_event_filter (GdkXEvent *xevent, GdkEvent *event, gpointer data)
1027 XAnyEvent *eventti = xevent;
1029 if (eventti->type == ClientMessage)
1031 XClientMessageEvent *cm = xevent;
1032 Atom mb_grab_transfer_atom =
1033 XInternAtom (GDK_DISPLAY(), "_MB_GRAB_TRANSFER", FALSE);
1035 if (cm->message_type == mb_grab_transfer_atom)
1037 hildon_window_toggle_menu (HILDON_WINDOW ( data ));
1038 return GDK_FILTER_REMOVE;
1042 return GDK_FILTER_CONTINUE;
1046 * Here we keep track of changes in the _MB_CURRENT_APP_WINDOW,
1047 * to know when we acquire/lose topmost status
1049 static GdkFilterReturn
1050 hildon_window_root_window_event_filter (GdkXEvent *xevent,
1054 XAnyEvent *eventti = xevent;
1055 HildonWindow *hwindow = HILDON_WINDOW (data);
1058 if (eventti->type == PropertyNotify)
1060 XPropertyEvent *pevent = xevent;
1061 Atom active_app_atom =
1062 XInternAtom (GDK_DISPLAY (), "_MB_CURRENT_APP_WINDOW", False);
1064 if (pevent->atom == active_app_atom)
1066 Window active_window = hildon_window_get_active_window();
1068 hildon_window_update_topmost (hwindow, active_window);
1072 return GDK_FILTER_CONTINUE;
1075 /***************************/
1076 /* Signal handlers */
1077 /***************************/
1080 * Handle the menu hardware key here
1083 hildon_window_key_press_event (GtkWidget *widget, GdkEventKey *event)
1085 HildonWindowPrivate *priv;
1087 g_return_val_if_fail (HILDON_IS_WINDOW (widget),FALSE);
1089 priv = HILDON_WINDOW (widget)->priv;
1091 switch (event->keyval)
1093 case HILDON_HARDKEY_MENU:
1094 if (hildon_window_toggle_menu (HILDON_WINDOW (widget)))
1097 case HILDON_HARDKEY_ESC:
1098 if (!priv->escape_timeout)
1100 priv->escape_timeout = g_timeout_add
1101 (HILDON_WINDOW_LONG_PRESS_TIME,
1102 hildon_window_escape_timeout, widget);
1107 return GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event);
1112 hildon_window_key_release_event (GtkWidget *widget, GdkEventKey *event)
1114 HildonWindowPrivate *priv;
1116 g_return_val_if_fail (HILDON_IS_WINDOW (widget),FALSE);
1118 priv = HILDON_WINDOW (widget)->priv;
1120 switch (event->keyval)
1122 case HILDON_HARDKEY_ESC:
1123 if (priv->escape_timeout)
1125 g_source_remove (priv->escape_timeout);
1126 priv->escape_timeout = 0;
1131 return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
1136 * We keep track of the window state changes, because the drawing
1137 * (borders) differs whether we are in fullscreen mode or not
1140 hildon_window_window_state_event (GtkWidget *widget,
1141 GdkEventWindowState *event,
1144 if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
1146 HILDON_WINDOW (widget)->priv->fullscreen =
1147 event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN;
1154 hildon_window_title_notify (GObject *gobject,
1158 HildonWindow *window = HILDON_WINDOW (gobject);
1160 hildon_window_update_title (window);
1164 /*******************/
1166 /*******************/
1168 /*The menu popuping needs a menu popup-function*/
1170 hildon_window_menupopupfunc (GtkMenu *menu, gint *x, gint *y,
1171 gboolean *push_in, GtkWidget *widget)
1175 GdkWindow *window = GTK_WIDGET(widget)->window;
1179 gdk_window_get_origin (window, &window_x, &window_y);
1182 gtk_widget_style_get (GTK_WIDGET (menu), "horizontal-offset", x,
1183 "vertical-offset", y, NULL);
1191 hildon_window_menupopupfuncfull ( GtkMenu *menu, gint *x, gint *y,
1195 gtk_widget_style_get (GTK_WIDGET (menu), "horizontal-offset", x,
1196 "vertical-offset", y, NULL);
1203 /********************/
1204 /* Private methods */
1205 /********************/
1208 * Sets the program to which the window belongs. This should only be called
1209 * by hildon_program_add_window
1212 hildon_window_set_program (HildonWindow *self, GObject *program)
1214 if (self->priv->program)
1216 g_object_unref (self->priv->program);
1219 /* Now that we are bound to a program, we can rely on it to track the
1221 gdk_window_remove_filter (gdk_get_default_root_window(),
1222 hildon_window_root_window_event_filter,
1225 self->priv->program = HILDON_PROGRAM (program);
1226 g_object_ref (program);
1230 * Unsets the program to which the window belongs. This should only be called
1231 * by hildon_program_add_window
1234 hildon_window_unset_program (HildonWindow *self)
1236 g_return_if_fail(self && HILDON_IS_WINDOW (self));
1238 if (self->priv->program)
1240 g_object_unref (self->priv->program);
1241 self->priv->program = NULL;
1243 /* We need to start tacking the root window again */
1244 gdk_window_set_events (gdk_get_default_root_window (),
1245 gdk_window_get_events (gdk_get_default_root_window ())
1246 | GDK_PROPERTY_CHANGE_MASK);
1248 gdk_window_add_filter (gdk_get_default_root_window (),
1249 hildon_window_root_window_event_filter, self );
1252 self->priv->program = NULL;
1256 * Sets whether or not the program to which this window belongs is
1257 * killable. This is used by the HildonProgram to signify to the
1258 * Task Navigator whether or not it can hibernate in memory-low situations
1261 hildon_window_set_can_hibernate_property (HildonWindow *self,
1262 gpointer _can_hibernate)
1264 GdkAtom killable_atom;
1265 gboolean can_hibernate;
1267 g_return_if_fail(self && HILDON_IS_WINDOW (self));
1269 if (!GTK_WIDGET_REALIZED ((GTK_WIDGET (self))))
1274 can_hibernate = * ((gboolean *)_can_hibernate);
1276 killable_atom = gdk_atom_intern (CAN_HIBERNATE_PROPERTY, FALSE);
1280 gdk_property_change (GTK_WIDGET (self)->window, killable_atom,
1281 (GdkAtom)31/* XA_STRING */, 8,
1282 GDK_PROP_MODE_REPLACE, (const guchar *)CAN_HIBERNATE,
1283 CAN_HIBERNATE_LENGTH);
1287 gdk_property_delete (GTK_WIDGET (self)->window, killable_atom);
1293 * If a common toolbar was set to the program, reparent it to
1297 hildon_window_take_common_toolbar (HildonWindow *self)
1299 g_return_if_fail(self && HILDON_IS_WINDOW (self));
1301 if (self->priv->program)
1303 GtkWidget *common_toolbar =
1304 GTK_WIDGET (hildon_program_get_common_toolbar (self->priv->program));
1306 if (common_toolbar && common_toolbar->parent != self->priv->vbox)
1308 g_object_ref (common_toolbar);
1309 if (common_toolbar->parent)
1311 gtk_container_remove (GTK_CONTAINER (common_toolbar->parent),
1315 gtk_box_pack_end (GTK_BOX(self->priv->vbox), common_toolbar,
1317 g_object_unref (common_toolbar);
1319 gtk_widget_set_size_request (common_toolbar, -1, TOOLBAR_HEIGHT);
1321 gtk_widget_show (self->priv->vbox);
1328 * Compare the window that was last topped, and act consequently
1331 hildon_window_update_topmost (HildonWindow *self, Window window_id)
1335 my_window = GDK_WINDOW_XID (GTK_WIDGET (self)->window);
1337 if (window_id == my_window)
1339 if (!self->priv->is_topmost)
1341 self->priv->is_topmost = TRUE;
1342 g_object_notify (G_OBJECT (self), "is-topmost");
1345 else if (self->priv->is_topmost)
1347 /* Should this go in the signal handler? */
1348 GtkWidget *focus = gtk_window_get_focus (GTK_WINDOW (self));
1350 if (GTK_IS_ENTRY (focus))
1351 gtk_im_context_focus_out (GTK_ENTRY (focus)->im_context);
1352 if (GTK_IS_TEXT_VIEW (focus))
1353 gtk_im_context_focus_out (GTK_TEXT_VIEW (focus)->im_context);
1355 self->priv->is_topmost = FALSE;
1356 g_object_notify (G_OBJECT (self), "is-topmost");
1362 * If the application
1363 * was given a name (with g_set_application_name(), set
1364 * "ProgramName - WindowTitle" as the displayed
1368 hildon_window_update_title (HildonWindow *window)
1370 const gchar * application_name;
1371 g_return_if_fail (window && HILDON_IS_WINDOW (window));
1373 if (!GTK_WIDGET_REALIZED (window))
1378 application_name = g_get_application_name ();
1380 if (application_name && application_name[0])
1382 gchar *title = NULL;
1383 const gchar *old_title = gtk_window_get_title (GTK_WINDOW (window));
1385 title = g_strjoin (TITLE_SEPARATOR, application_name, old_title, NULL);
1387 gdk_window_set_title (GTK_WIDGET (window)->window, title);
1394 detach_menu_func (GtkWidget *attach_widget, GtkMenu *menu)
1398 * Toggles the display of the HildonWindow menu.
1399 * Returns whether or not something was done (whether or not we had a menu
1403 hildon_window_toggle_menu (HildonWindow * self)
1405 GtkMenu *menu_to_use = NULL;
1407 g_return_if_fail (self && HILDON_IS_WINDOW (self));
1409 /* Select which menu to use, Window specific has highest priority,
1410 * then program specific */
1411 if (self->priv->menu)
1413 menu_to_use = GTK_MENU (self->priv->menu);
1415 else if (self->priv->program)
1417 menu_to_use = hildon_program_get_common_menu (self->priv->program);
1418 if (menu_to_use && gtk_menu_get_attach_widget (menu_to_use) !=
1421 g_object_ref (menu_to_use);
1422 if (gtk_menu_get_attach_widget (menu_to_use))
1424 gtk_menu_detach (menu_to_use);
1427 gtk_menu_attach_to_widget (menu_to_use, GTK_WIDGET (self),
1429 g_object_unref (menu_to_use);
1439 if (GTK_WIDGET_VISIBLE (GTK_WIDGET (menu_to_use)))
1441 gtk_menu_popdown (menu_to_use);
1442 gtk_menu_shell_deactivate (GTK_MENU_SHELL (menu_to_use));
1446 if (gtk_container_get_children (GTK_CONTAINER (menu_to_use)) != NULL)
1448 /* Apply right theming */
1449 gtk_widget_set_name (GTK_WIDGET (menu_to_use),
1450 "menu_force_with_corners");
1452 if (self->priv->fullscreen)
1454 gtk_menu_popup (menu_to_use, NULL, NULL,
1455 (GtkMenuPositionFunc)
1456 hildon_window_menupopupfuncfull,
1458 gtk_get_current_event_time ());
1462 gtk_menu_popup (menu_to_use, NULL, NULL,
1463 (GtkMenuPositionFunc)
1464 hildon_window_menupopupfunc,
1466 gtk_get_current_event_time ());
1468 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu_to_use), TRUE);
1475 * If the ESC key was not released when the timeout expires,
1479 hildon_window_escape_timeout (gpointer data)
1481 HildonWindowPrivate *priv;
1484 GDK_THREADS_ENTER ();
1486 priv = HILDON_WINDOW(data)->priv;
1488 /* Send fake event, simulation a situation that user
1489 pressed 'x' from the corner */
1490 event = gdk_event_new(GDK_DELETE);
1491 ((GdkEventAny *)event)->window = GTK_WIDGET(data)->window;
1492 gtk_main_do_event(event);
1493 gdk_event_free(event);
1495 priv->escape_timeout = 0;
1497 GDK_THREADS_LEAVE ();
1503 /******************/
1504 /* public methods */
1505 /******************/
1509 * hildon_window_new:
1511 * Use this function to create a new HildonWindow.
1513 * Return value: A @HildonWindow.
1516 hildon_window_new (void)
1518 HildonWindow *newwindow = g_object_new (HILDON_TYPE_WINDOW, NULL);
1520 return GTK_WIDGET (newwindow);
1524 * hildon_window_add_with_scrollbar
1525 * @self : A @HildonWindow
1526 * @child : A @GtkWidget
1528 * Adds the @child to the HildonWindow and creates a scrollbar
1529 * to it. Similar as adding first a @GtkScrolledWindow and then the
1533 hildon_window_add_with_scrollbar (HildonWindow * self,
1536 GtkScrolledWindow *scrolledw;
1538 g_return_if_fail (HILDON_IS_WINDOW (self));
1539 g_return_if_fail (GTK_IS_WIDGET (child));
1540 g_return_if_fail (child->parent == NULL);
1542 scrolledw = GTK_SCROLLED_WINDOW (gtk_scrolled_window_new (NULL, NULL));
1543 gtk_scrolled_window_set_policy (scrolledw, GTK_POLICY_NEVER,
1544 GTK_POLICY_AUTOMATIC);
1545 gtk_scrolled_window_set_shadow_type (scrolledw, GTK_SHADOW_NONE);
1547 if (GTK_IS_VIEWPORT (child))
1548 gtk_container_add (GTK_CONTAINER (scrolledw), child);
1551 if (GTK_IS_CONTAINER (child) )
1552 gtk_container_set_focus_vadjustment (GTK_CONTAINER(child),
1553 gtk_scrolled_window_get_vadjustment (scrolledw) );
1554 gtk_scrolled_window_add_with_viewport (scrolledw, child);
1557 gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (scrolledw));
1561 * hildon_window_add_toolbar:
1562 * @self: A @HildonWindow
1563 * @toolbar: A #GtkToolbar to add to the HildonWindow
1565 * Adds a toolbar to the window.
1568 hildon_window_add_toolbar (HildonWindow *self, GtkToolbar *toolbar)
1572 g_return_if_fail (self && HILDON_IS_WINDOW (self));
1573 g_return_if_fail (toolbar && GTK_IS_TOOLBAR (toolbar));
1575 vbox = GTK_BOX (self->priv->vbox);
1577 gtk_box_pack_start (vbox, GTK_WIDGET(toolbar), TRUE, TRUE, 0);
1578 gtk_box_reorder_child (vbox, GTK_WIDGET(toolbar), 0);
1579 gtk_widget_set_size_request (GTK_WIDGET (toolbar), -1, TOOLBAR_HEIGHT);
1581 gtk_widget_queue_resize (GTK_WIDGET(self));
1585 * hildon_window_remove_toolbar:
1586 * @self: A @HildonWindow
1587 * @toolbar: A #GtkToolbar to remove from the HildonWindow
1589 * Removes a toolbar from the window.
1592 hildon_window_remove_toolbar (HildonWindow *self, GtkToolbar *toolbar)
1594 GtkContainer *vbox = GTK_CONTAINER (self->priv->vbox);
1596 g_return_if_fail (self && HILDON_IS_WINDOW (self));
1598 gtk_container_remove (vbox, GTK_WIDGET(toolbar));
1599 /* FIXME: As the toolbar border graphics go beyond the VBox, we
1600 * need to trigger a manual redraw */
1601 gtk_widget_queue_draw_area (GTK_WIDGET (self) , 0, 0,
1602 GTK_WIDGET(self)->allocation.width,
1603 GTK_WIDGET(self)->allocation.height);
1607 * hildon_window_get_menu:
1608 * @self : #HildonWindow
1610 * Gets the #GtMenu assigned to the #HildonAppview.
1612 * Return value: The #GtkMenu assigned to this application view.
1615 hildon_window_get_menu (HildonWindow * self)
1617 g_return_val_if_fail (self && HILDON_IS_WINDOW (self), NULL);
1619 return GTK_MENU (self->priv->menu);
1624 * hildon_window_set_menu:
1625 * @self: A #HildonWindow
1626 * @menu: The #GtkMenu to be used for this #HildonWindow
1628 * Sets the menu to be used for this window. This menu overrides
1629 * a program-wide menu that may have been set with
1630 * hildon_program_set_common_menu.
1633 hildon_window_set_menu (HildonWindow *self, GtkMenu *menu)
1635 g_return_if_fail (self && HILDON_IS_WINDOW (self) &&
1636 menu && GTK_IS_MENU (menu));
1638 if (self->priv->menu)
1640 g_object_unref (self->priv->menu);
1643 self->priv->menu = GTK_WIDGET (menu);
1644 gtk_widget_set_name (GTK_WIDGET (self->priv->menu),
1645 "menu_force_with_corners");
1646 gtk_widget_show_all (self->priv->menu);
1648 gtk_menu_attach_to_widget (menu, GTK_WIDGET (self), &detach_menu_func);
1652 * hildon_window_get_is_topmost:
1653 * @self: A #HildonWindow
1655 * Return value: Whether or not the #HildonWindow is currenltly activated
1656 * by the window manager.
1659 hildon_window_get_is_topmost(HildonWindow *self){
1660 g_return_val_if_fail (HILDON_IS_WINDOW (self), FALSE);
1662 return self->priv->is_topmost;