2009-02-23 Alejandro G. Castro <alex@igalia.com>
[hildon] / src / hildon-window.c
1 /*
2  * This file is a part of hildon
3  *
4  * Copyright (C) 2006 Nokia Corporation, all rights reserved.
5  *
6  * Contact: Rodrigo Novo <rodrigo.novo@nokia.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; version 2.1 of
11  * the License, or (at your option) any later version.
12  *
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.
17  *
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
21  * 02110-1301 USA
22  *
23  */
24
25 /**
26  * SECTION:hildon-window
27  * @short_description: Widget representing a top-level window in the Hildon framework.
28  *
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
32  * framework.
33  *
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_main_menu() method.
38  *
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().
42  *
43  * A #HildonWindow can also have a #HildonEditToolbar. To add it to
44  * the window use hildon_window_set_edit_toolbar().
45  *
46  * <example>
47  * <title>Creating a HildonWindow</title>
48  * <programlisting>
49  * HildonWindow *window;
50  * GtkToolbar *toolbar;
51  * GtkMenu *menu;
52  * GdkPixbuf *icon_pixbuf;
53  * <!-- -->
54  * window = HILDON_WINDOW (hildon_window_new());
55  * <!-- -->
56  * toolbar = create_toolbar();
57  * <!-- -->
58  * menu = create_menu();
59  * <!-- -->
60  * icon_pixbuf = create_icon();
61  * <!-- -->
62  * hildon_window_set_main_menu (window, menu);
63  * <!-- -->
64  * hildon_window_add_toolbar (window, toolbar);
65  * <!-- -->
66  * // Can be used to set the window fullscreen
67  * gtk_window_fullscreen (GTK_WINDOW (window));
68  * <!-- -->
69  * // Used to trigger the blinking of the window's icon in the task navigator
70  * gtk_window_set_urgency_hint (GTK_WINDOW (window), TRUE);
71  * <!-- -->
72  * // Change the window's icon in the task navigator
73  * gtk_window_set_icon (GTK_WINDOW (window), icon_pixbuf);
74  * </programlisting>
75  * </example>
76  */
77
78 #undef                                          HILDON_DISABLE_DEPRECATED
79
80 #include                                        <memory.h>
81 #include                                        <string.h>
82 #include                                        <strings.h>
83 #include                                        <stdio.h>
84 #include                                        <libintl.h>
85 #include                                        <X11/X.h>
86 #include                                        <X11/Xatom.h>
87 #include                                        <gdk/gdkkeysyms.h>
88 #include                                        <gdk/gdkx.h>
89 #include                                        <gtk/gtkprivate.h>
90
91 #include                                        "hildon-window.h"
92 #include                                        "hildon-window-private.h"
93 #include                                        "hildon-find-toolbar.h"
94 #include                                        "hildon-defines.h"
95
96 #define                                         _(String) gettext(String)
97
98 #define                                         TOOLBAR_HEIGHT 70
99
100 #define                                         TOOLBAR_MIDDLE 0
101
102 /*FIXME*/
103 #define                                         CAN_HIBERNATE "CANKILL"
104
105 #define                                         CAN_HIBERNATE_LENGTH 7
106
107 #define                                         CAN_HIBERNATE_PROPERTY "_HILDON_ABLE_TO_HIBERNATE"
108
109 #define TITLE_SEPARATOR                         " - "
110
111 typedef void                                    (*HildonWindowSignal) (HildonWindow *, gint, gpointer);
112
113 static void
114 hildon_window_init                              (HildonWindow * self);
115
116 static void
117 hildon_window_class_init                        (HildonWindowClass * window_class);
118
119 static void
120 hildon_window_menu_popup_func                   (GtkMenu *menu, 
121                                                  gint *x, 
122                                                  gint *y,
123                                                  gboolean *push_in,
124                                                  GtkWidget *widget);
125 static void
126 hildon_window_menu_popup_func_full              (GtkMenu *menu, 
127                                                  gint *x, 
128                                                  gint *y,
129                                                  gboolean *push_in,
130                                                  GtkWidget *widget);
131 static gboolean
132 hildon_window_expose                            (GtkWidget *widget, 
133                                                  GdkEventExpose *event);
134 static void 
135 hildon_window_forall                            (GtkContainer *container,
136                                                  gboolean include_internals,
137                                                  GtkCallback callback,
138                                                  gpointer callback_data);
139 static void
140 hildon_window_show_all                          (GtkWidget *widget);
141
142 static void
143 hildon_window_size_allocate                     (GtkWidget * widget,
144                                                  GtkAllocation *allocation);
145 static void
146 hildon_window_size_request                      (GtkWidget * widget,
147                                                  GtkRequisition *requisition);
148 static void
149 hildon_window_finalize                          (GObject *obj_self);
150
151 static void
152 hildon_window_get_property                      (GObject *object,
153                                                  guint property_id,
154                                                  GValue *value, 
155                                                  GParamSpec *pspec);
156
157 static void
158 hildon_window_destroy                           (GtkObject *obj);
159
160 static void
161 hildon_window_realize                           (GtkWidget *widget);
162
163 static void
164 hildon_window_unrealize                         (GtkWidget *widget);
165
166 static void
167 hildon_window_map                               (GtkWidget *widget);
168
169 static void
170 hildon_window_unmap                             (GtkWidget *widget);
171
172 static gboolean
173 hildon_window_key_press_event                   (GtkWidget *widget,
174                                                  GdkEventKey *event);
175
176 static gboolean
177 hildon_window_key_release_event                 (GtkWidget *widget, 
178                                                  GdkEventKey *event);
179 static gboolean
180 hildon_window_window_state_event                (GtkWidget *widget, 
181                                                  GdkEventWindowState *event);
182 static gboolean
183 hildon_window_focus_out_event                   (GtkWidget *widget, 
184                                                  GdkEventFocus *event);
185
186 static void
187 hildon_window_notify                            (GObject *gobject, 
188                                                  GParamSpec *param);
189
190 static void
191 hildon_window_is_topmost_notify                 (HildonWindow *window);
192
193 static gboolean
194 hildon_window_toggle_menu                       (HildonWindow * self,
195                                                  guint button,
196                                                  guint32 time);
197
198 static gboolean
199 hildon_window_toggle_menu_real                  (HildonWindow * self,
200                                                  guint button,
201                                                  guint32 time);
202
203 static gboolean
204 hildon_window_escape_timeout                    (gpointer data);
205
206 static GdkFilterReturn
207 hildon_window_event_filter                      (GdkXEvent *xevent, 
208                                                  GdkEvent *event, 
209                                                  gpointer data);
210
211 static GdkFilterReturn
212 hildon_window_root_window_event_filter          (GdkXEvent *xevent, 
213                                                  GdkEvent *event, 
214                                                  gpointer data );
215
216 static void
217 hildon_window_get_borders                       (HildonWindow *window);
218
219 static void
220 visible_toolbar                                 (gpointer data, 
221                                                  gpointer user_data);
222
223 static void
224 paint_toolbar                                   (GtkWidget *widget, 
225                                                  GtkBox *box, 
226                                                  GdkEventExpose * event, 
227                                                  gboolean fullscreen);
228
229 static void
230 paint_edit_toolbar                              (GtkWidget *widget,
231                                                  GtkWidget *toolbar,
232                                                  GdkEventExpose *event,
233                                                  gboolean fullscreen);
234
235 enum
236 {
237     PROP_0,
238     PROP_IS_TOPMOST
239 };
240
241 enum
242 {
243     WIN_TYPE = 0,
244     WIN_TYPE_MESSAGE,
245     MAX_WIN_MESSAGES
246 };
247
248 G_DEFINE_TYPE (HildonWindow, hildon_window, GTK_TYPE_WINDOW);
249
250 static void 
251 hildon_window_class_init                        (HildonWindowClass * window_class)
252 {
253     /* Get convenience variables */
254     GtkWidgetClass *widget_class        = GTK_WIDGET_CLASS (window_class);
255     GObjectClass *object_class          = G_OBJECT_CLASS (window_class);
256     GtkContainerClass *container_class  = GTK_CONTAINER_CLASS (window_class);
257
258     object_class->get_property          = hildon_window_get_property;
259     object_class->notify                = hildon_window_notify;
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;
268     widget_class->window_state_event    = hildon_window_window_state_event;
269     widget_class->focus_out_event       = hildon_window_focus_out_event;
270     widget_class->map                   = hildon_window_map;
271     widget_class->unmap                 = hildon_window_unmap;
272
273     /* now the object stuff */
274     object_class->finalize              = hildon_window_finalize;
275
276     /* To the container */
277     container_class->forall             = hildon_window_forall;
278
279     /* To this class */
280     window_class->toggle_menu           = hildon_window_toggle_menu_real;
281
282     /* gtkobject stuff*/
283     GTK_OBJECT_CLASS (window_class)->destroy = hildon_window_destroy; 
284
285     g_type_class_add_private (window_class,
286             sizeof (struct _HildonWindowPrivate));
287
288     /* Install properties */
289
290     g_object_class_install_property (object_class, PROP_IS_TOPMOST,
291             g_param_spec_boolean ("is-topmost",
292                 "Is top-most",
293                 "Whether the window is currently activated by the window "
294                 "manager",
295                 FALSE,
296                 G_PARAM_READABLE));
297
298     gtk_widget_class_install_style_property (widget_class,
299             g_param_spec_boxed ("borders",
300                 "Graphical borders",
301                 "Size of graphical window borders",
302                 GTK_TYPE_BORDER,
303                 G_PARAM_READABLE));
304
305     gtk_widget_class_install_style_property (widget_class,
306             g_param_spec_boxed ("toolbar-borders",
307                 "Graphical toolbar borders",
308                 "Size of graphical toolbar borders",
309                 GTK_TYPE_BORDER,
310                 G_PARAM_READABLE));
311
312     /* opera hack, install clip operation signal */
313     g_signal_new ("clipboard_operation",
314             G_OBJECT_CLASS_TYPE (object_class),
315             G_SIGNAL_RUN_FIRST,
316             G_STRUCT_OFFSET (HildonWindowClass, clipboard_operation),
317             NULL, NULL,
318             g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1,
319             G_TYPE_INT);
320 }
321
322 static void
323 hildon_window_init                              (HildonWindow *self)
324 {
325     HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
326     g_assert (priv != NULL);
327
328     priv->vbox = gtk_vbox_new (TRUE, TOOLBAR_MIDDLE);
329     gtk_widget_set_parent (priv->vbox, GTK_WIDGET(self));
330     priv->menu = NULL;
331     priv->edit_toolbar = NULL;
332     priv->visible_toolbars = 0;
333     priv->is_topmost = FALSE;
334     priv->borders = NULL;
335     priv->toolbar_borders = NULL;
336     priv->escape_timeout = 0;
337
338     priv->fullscreen = FALSE;
339
340     priv->program = NULL;
341
342     /* We need to track the root window _MB_CURRENT_APP_WINDOW property */
343     gdk_window_set_events (gdk_get_default_root_window (),
344             gdk_window_get_events (gdk_get_default_root_window ()) | GDK_PROPERTY_CHANGE_MASK);
345
346     gdk_window_add_filter (gdk_get_default_root_window (), 
347             hildon_window_root_window_event_filter, self);
348 }
349
350 static void
351 hildon_window_finalize                          (GObject * obj_self)
352 {
353     HildonWindowPrivate *priv; 
354       
355     g_return_if_fail (HILDON_WINDOW (obj_self));
356
357     priv = HILDON_WINDOW_GET_PRIVATE (obj_self);
358     g_assert (priv != NULL);
359     
360     if (priv->escape_timeout) {
361       g_source_remove (priv->escape_timeout);
362       priv->escape_timeout = 0;
363     }
364
365     if (priv->borders)
366         gtk_border_free (priv->borders);
367
368     if (priv->toolbar_borders)
369         gtk_border_free (priv->toolbar_borders);
370
371     if (G_OBJECT_CLASS (hildon_window_parent_class)->finalize)
372         G_OBJECT_CLASS (hildon_window_parent_class)->finalize (obj_self);
373
374 }
375
376 static void
377 hildon_window_realize                           (GtkWidget *widget)
378 {
379     Atom *old_atoms, *new_atoms;
380     Display *disp;
381     Window window;
382     gint atom_count;
383     Window active_window;
384     HildonWindowPrivate *priv;
385
386     GTK_WIDGET_CLASS (hildon_window_parent_class)->realize (widget);
387
388     priv = HILDON_WINDOW_GET_PRIVATE (widget);
389     g_assert (priv != NULL);
390
391     gtk_widget_realize (GTK_WIDGET (priv->vbox));
392
393     if (priv->edit_toolbar != NULL)
394         gtk_widget_realize (priv->edit_toolbar);
395
396     /* catch the custom button signal from mb to display the menu */
397     gdk_window_add_filter (widget->window, hildon_window_event_filter, widget);
398
399     window = GDK_WINDOW_XID (widget->window);
400     disp = GDK_WINDOW_XDISPLAY (widget->window);
401
402     /* Enable custom button that is used for menu */
403     XGetWMProtocols (disp, window, &old_atoms, &atom_count);
404     new_atoms = g_new (Atom, atom_count + 1);
405
406     memcpy (new_atoms, old_atoms, sizeof(Atom) * atom_count);
407
408     new_atoms[atom_count++] =
409         XInternAtom (disp, "_NET_WM_CONTEXT_CUSTOM", False);
410
411     XSetWMProtocols (disp, window, new_atoms, atom_count);
412
413     XFree(old_atoms);
414     g_free(new_atoms);
415
416     /* rely on GDK to set the window group to its default */
417     gdk_window_set_group (widget->window, NULL);
418
419     if (priv->program) {
420         gboolean can_hibernate = hildon_program_get_can_hibernate (priv->program);
421
422         hildon_window_set_can_hibernate_property (HILDON_WINDOW (widget),
423                 &can_hibernate);
424     }
425
426     /* Update the topmost status */
427     active_window = hildon_window_get_active_window();
428     hildon_window_update_topmost (HILDON_WINDOW (widget), active_window);
429 }
430
431 static void
432 hildon_window_unrealize                         (GtkWidget *widget)
433 {
434     HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (widget);
435     g_assert (priv != NULL);
436
437     gdk_window_remove_filter (widget->window, hildon_window_event_filter,
438             widget);
439
440     gtk_widget_unrealize (GTK_WIDGET (priv->vbox));
441
442     if (priv->edit_toolbar != NULL)
443         gtk_widget_unrealize (priv->edit_toolbar);
444
445     GTK_WIDGET_CLASS(hildon_window_parent_class)->unrealize(widget);
446 }
447
448 static void
449 hildon_window_map                             (GtkWidget *widget)
450 {
451   HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (widget);
452   g_assert (priv != NULL);
453
454   if (GTK_WIDGET_CLASS (hildon_window_parent_class)->map)
455     GTK_WIDGET_CLASS (hildon_window_parent_class)->map (widget);
456
457   if (GTK_WIDGET_VISIBLE (priv->vbox))
458     gtk_widget_map (priv->vbox);
459
460   if (priv->edit_toolbar != NULL && GTK_WIDGET_VISIBLE (priv->edit_toolbar))
461     gtk_widget_map (priv->edit_toolbar);
462 }
463
464 static void
465 hildon_window_unmap                             (GtkWidget *widget)
466 {
467   HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (widget);
468   g_assert (priv != NULL);
469
470   gtk_widget_unmap (priv->vbox);
471
472   if (priv->edit_toolbar != NULL)
473     gtk_widget_unmap (priv->edit_toolbar);
474
475   if (GTK_WIDGET_CLASS (hildon_window_parent_class)->unmap)
476     GTK_WIDGET_CLASS (hildon_window_parent_class)->unmap (widget);
477 }
478
479 static void
480 hildon_window_get_property                      (GObject *object, 
481                                                  guint property_id,
482                                                  GValue *value, 
483                                                  GParamSpec * pspec)
484 {
485     HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (object);
486     g_assert (priv != NULL);
487
488     switch (property_id) {
489
490         case PROP_IS_TOPMOST:
491             g_value_set_boolean (value, priv->is_topmost);
492             break;
493
494         default:
495             G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
496             break;
497     }
498 }
499
500 /*
501  * Retrieve the graphical borders size used by the themes
502  */
503 static void
504 hildon_window_get_borders                       (HildonWindow *window)
505 {
506     GtkBorder zero = {0, 0, 0, 0};
507     HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (window);
508     g_assert (priv);
509
510     GtkBorder *borders = NULL;
511     GtkBorder *toolbar_borders = NULL;
512
513     if (priv->borders)
514         gtk_border_free (priv->borders);
515     if (priv->toolbar_borders)
516         gtk_border_free (priv->toolbar_borders);
517
518     priv->borders = NULL;
519     priv->toolbar_borders = NULL;
520
521     gtk_widget_style_get (GTK_WIDGET (window), "borders",&borders,
522             "toolbar-borders", &toolbar_borders,
523             NULL);
524
525     // We're doing a copy here instead of reusing the pointer, 
526     // as we don't know where it comes from (has it been allocated using 
527     // malloc or slices... and we want to free it sanely. Blowing on 
528     // cold probbably.
529
530     if (borders) {
531         priv->borders = gtk_border_copy (borders);
532         gtk_border_free (borders);
533     } else
534         priv->borders = g_boxed_copy (GTK_TYPE_BORDER, &zero);
535
536     if (toolbar_borders) {
537         priv->toolbar_borders = gtk_border_copy (toolbar_borders);
538         gtk_border_free (toolbar_borders);
539     } else
540         priv->toolbar_borders = g_boxed_copy (GTK_TYPE_BORDER, &zero);
541 }
542
543 static gboolean
544 hildon_window_expose                            (GtkWidget *widget, 
545                                                  GdkEventExpose * event)
546 {
547     HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (widget);
548     g_assert (priv);
549
550     GtkWidget *bx = priv->vbox;
551     GtkBox *box = GTK_BOX(bx);
552     GtkBorder *b = priv->borders;
553     GtkBorder *tb = priv->toolbar_borders;
554     gint tb_height = 0;
555
556     if (! priv->borders) {
557         hildon_window_get_borders (HILDON_WINDOW (widget));
558         b = priv->borders;
559         tb = priv->toolbar_borders;
560     }
561
562     tb_height = bx->allocation.height + tb->top + tb->bottom;
563
564     paint_toolbar (widget, box,
565             event, priv->fullscreen);
566
567     if (priv->edit_toolbar != NULL)
568     {
569         paint_edit_toolbar (widget, priv->edit_toolbar,
570                             event, priv->fullscreen);
571     }
572
573     if (! priv->fullscreen) {
574
575         /* Draw the left and right window border */
576         gint side_borders_height = widget->allocation.height - b->top;
577
578         if (priv->visible_toolbars)
579             side_borders_height -= tb_height;
580         else
581             side_borders_height -= b->bottom;
582
583         if (b->left > 0) 
584         {
585             gtk_paint_box (widget->style, widget->window,
586                     GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
587                     &event->area, widget, "left-border",
588                     widget->allocation.x, widget->allocation.y +
589                     b->top, b->left, side_borders_height);
590         } 
591
592         if (b->right > 0)
593         {
594             gtk_paint_box (widget->style, widget->window,
595                     GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
596                     &event->area, widget, "right-border",
597                     widget->allocation.x + widget->allocation.width -
598                     b->right, widget->allocation.y + b->top,
599                     b->right, side_borders_height);
600         }
601
602         /* If no toolbar, draw the bottom window border */
603         if (! priv->visible_toolbars && b->bottom > 0)
604         {
605             gtk_paint_box (widget->style, widget->window,
606                     GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
607                     &event->area, widget, "bottom-border",
608                     widget->allocation.x, widget->allocation.y +
609                     (widget->allocation.height - b->bottom),
610                     widget->allocation.width, b->bottom);
611         }
612
613         /* Draw the top border */
614         if (b->top > 0)
615         {
616             gtk_paint_box (widget->style, widget->window,
617                     GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
618                     &event->area, widget, "top-border",
619                     widget->allocation.x, widget->allocation.y,
620                     widget->allocation.width, b->top);
621         } 
622
623
624     }
625
626     /* don't draw the window stuff as it overwrites our borders with a blank
627      * rectangle. Instead start with the drawing of the GtkBin */
628     GTK_WIDGET_CLASS (g_type_class_peek_parent (hildon_window_parent_class))->expose_event (widget, event);
629
630     /* FIXME Not sure why this is commented out 
631      * GTK_WIDGET_CLASS (hildon_window_parent_class))->
632      *  expose_event (widget, event); 
633      */
634
635     return FALSE;
636 }
637
638 static void
639 hildon_window_size_request                      (GtkWidget *widget, 
640                                                  GtkRequisition *requisition)
641 {
642     HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (widget);
643     g_assert (priv);
644
645     GtkWidget *child = GTK_BIN (widget)->child;
646     GtkRequisition req2 = { 0 };
647     gint border_width = GTK_CONTAINER(widget)->border_width;
648
649     if (! priv->borders)
650     {
651         hildon_window_get_borders (HILDON_WINDOW (widget));
652     }
653
654     if (child)
655         gtk_widget_size_request (child, requisition);
656
657     if (priv->vbox != NULL)
658         gtk_widget_size_request (priv->vbox, &req2);
659
660     requisition->height += req2.height;
661     requisition->width = MAX (requisition->width, req2.width);
662
663     if (priv->edit_toolbar != NULL && GTK_WIDGET_VISIBLE (priv->edit_toolbar))
664     {
665         GtkRequisition req;
666         gtk_widget_size_request (priv->edit_toolbar, &req);
667         requisition->height += req.height;
668         requisition->width = MAX (requisition->width, req.width);
669     }
670
671     requisition->width  += 2 * border_width;
672     requisition->height += 2 * border_width;
673
674     if (! priv->fullscreen)
675     {
676         requisition->height += priv->borders->top;
677         if (req2.height == 0)
678             requisition->height += priv->borders->bottom;
679         requisition->width += priv->borders->left + priv->borders->right;
680     }
681 }
682
683 static void
684 hildon_window_size_allocate                     (GtkWidget *widget, 
685                                                  GtkAllocation *allocation)
686 {
687     HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (widget);
688     g_assert (priv);
689
690     GtkAllocation box_alloc = { 0 };
691     GtkAllocation edittb_alloc = { 0 };
692     GtkAllocation alloc = *allocation;
693
694     GtkWidget *child = gtk_bin_get_child (GTK_BIN (widget));
695     GtkBorder *tb;
696
697     if (!priv->borders)
698         hildon_window_get_borders (HILDON_WINDOW (widget));
699
700     tb = priv->toolbar_borders;
701
702     widget->allocation = *allocation;
703
704     /* Calculate allocation of edit toolbar */
705     if (priv->edit_toolbar != NULL && GTK_WIDGET_VISIBLE (priv->edit_toolbar))
706     {
707         GtkRequisition req;
708         gtk_widget_get_child_requisition (priv->edit_toolbar, &req);
709         edittb_alloc.width = alloc.width - tb->left - tb->right;
710         edittb_alloc.height = MIN (req.height, alloc.height);
711         edittb_alloc.x = alloc.x + tb->left;
712         edittb_alloc.y = alloc.y + tb->top;
713
714         if (edittb_alloc.height > 0)
715         {
716             alloc.y += tb->top + tb->bottom + edittb_alloc.height;
717             alloc.height -= tb->top + tb->bottom + edittb_alloc.height;
718             gtk_widget_size_allocate (priv->edit_toolbar, &edittb_alloc);
719         }
720     }
721
722     /* Calculate allocation of normal toolbars */
723     if (priv->vbox != NULL && GTK_WIDGET_VISIBLE (priv->vbox))
724     {
725         GtkRequisition req;
726         gtk_widget_get_child_requisition (priv->vbox, &req);
727         box_alloc.width = alloc.width - tb->left - tb->right;
728         box_alloc.height = MIN (req.height, alloc.height);
729         box_alloc.x = alloc.x + tb->left;
730         box_alloc.y = alloc.y + alloc.height - box_alloc.height - tb->bottom;
731
732         if (box_alloc.height > 0)
733         {
734             alloc.height -= tb->top + tb->bottom + box_alloc.height;
735             gtk_widget_size_allocate (priv->vbox, &box_alloc);
736         }
737     }
738
739     /* Calculate allocation of the child widget */
740     if (child != NULL && GTK_WIDGET_VISIBLE (child))
741     {
742         guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
743         alloc.x += border_width;
744         alloc.y += border_width;
745         alloc.width -= (border_width * 2);
746         alloc.height -= (border_width * 2);
747
748         if (! priv->fullscreen)
749         {
750             GtkBorder *b = priv->borders;
751             alloc.x += b->left;
752             alloc.width -= (b->left + b->right);
753
754             /* Use the top border if there's no edit toolbar */
755             if (edittb_alloc.height <= 0)
756             {
757                 alloc.y += b->top;
758                 alloc.height -= b->top;
759             }
760
761             /* Use the top border if there are no standard toolbars */
762             if (box_alloc.height <= 0)
763                 alloc.height -= b->bottom;
764         }
765
766         gtk_widget_size_allocate (child, &alloc);
767     }
768
769     if (priv->previous_vbox_y != box_alloc.y)
770     {
771         /* The size of the VBox has changed, we need to redraw part
772          * of the window borders */
773         gint draw_from_y = MIN (priv->previous_vbox_y, box_alloc.y) - tb->top;
774
775         gtk_widget_queue_draw_area (widget, 0, draw_from_y, 
776                 widget->allocation.width,
777                 widget->allocation.height - draw_from_y);
778
779         priv->previous_vbox_y = box_alloc.y;
780     }
781
782 }
783
784 static void
785 hildon_window_forall                            (GtkContainer *container, 
786                                                  gboolean include_internals,
787                                                  GtkCallback callback, 
788                                                  gpointer callback_data)
789 {
790     HildonWindow *self = HILDON_WINDOW (container);
791     HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
792
793     g_return_if_fail (callback != NULL);
794     g_assert (priv);
795
796     GTK_CONTAINER_CLASS (hildon_window_parent_class)->forall (container, include_internals,
797             callback, callback_data);
798
799     if (include_internals && priv->vbox != NULL)
800         (* callback)(GTK_WIDGET (priv->vbox), callback_data);
801
802     if (include_internals && priv->edit_toolbar != NULL)
803         (* callback)(GTK_WIDGET (priv->edit_toolbar), callback_data);
804 }
805
806 static void
807 hildon_window_show_all                          (GtkWidget *widget)
808 {
809     HildonWindow *self = HILDON_WINDOW (widget);
810     HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
811
812     g_assert (priv != NULL);
813
814     GTK_WIDGET_CLASS (hildon_window_parent_class)->show_all (widget);
815
816     gtk_widget_show_all (priv->vbox);
817
818     if (priv->edit_toolbar)
819         gtk_widget_show_all (priv->edit_toolbar);
820 }
821
822 static void
823 hildon_window_destroy                           (GtkObject *obj)
824 {
825     HildonWindow *self = HILDON_WINDOW (obj);
826     HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (obj);
827     GList *menu_list = NULL;
828     GList *menu_node = NULL;
829
830     g_assert (priv != NULL);
831
832     if (priv->vbox != NULL)
833     {
834         if (priv->program)
835         {
836             GtkWidget * common_toolbar = 
837                 GTK_WIDGET (hildon_program_get_common_toolbar (priv->program));
838             if (common_toolbar && common_toolbar->parent == priv->vbox)
839             {
840                 gtk_container_remove (GTK_CONTAINER (priv->vbox),
841                         common_toolbar);
842             }
843         }
844
845         gtk_widget_unparent (priv->vbox);
846         priv->vbox = NULL;    
847
848     }
849
850     if (priv->edit_toolbar != NULL)
851     {
852         gtk_widget_unparent (priv->edit_toolbar);
853         priv->edit_toolbar = NULL;
854     }
855
856     menu_list = g_list_copy (gtk_menu_get_for_attach_widget (GTK_WIDGET (obj)));
857     menu_node = menu_list;
858
859     while (menu_node)
860     {
861         if (GTK_IS_MENU (menu_node->data))
862         {
863             if (GTK_WIDGET_VISIBLE (GTK_WIDGET (menu_node->data)))
864             {
865                 gtk_menu_popdown (GTK_MENU (menu_node->data));
866                 gtk_menu_shell_deactivate (GTK_MENU_SHELL (menu_node->data));
867             }
868             gtk_menu_detach (GTK_MENU (menu_node->data));
869
870             /* Destroy it, but only if it's not a common menu */
871             if (priv->program && 
872                 hildon_program_get_common_menu (priv->program) != menu_node->data) {
873                     gtk_object_destroy (GTK_OBJECT (menu_node->data));
874                     g_object_unref (menu_node->data);
875             }
876         }
877         menu_node = menu_node->next;
878     }
879
880     g_list_free (menu_list);
881     menu_list = NULL;
882
883     if (priv->program)
884     {
885         hildon_program_remove_window (priv->program, self);
886     }
887
888     gdk_window_remove_filter (gdk_get_default_root_window(), 
889             hildon_window_root_window_event_filter,
890             obj);
891
892     gtk_widget_set_events (GTK_WIDGET(obj), 0);
893
894     GTK_OBJECT_CLASS (hildon_window_parent_class)->destroy (obj);
895 }
896
897 static void
898 hildon_window_notify                            (GObject *gobject, 
899                                                  GParamSpec *param)
900 {
901     HildonWindow *window = HILDON_WINDOW (gobject);
902
903     if (g_str_equal (param->name, "is-topmost"))
904     {
905         hildon_window_is_topmost_notify (window);
906     }
907
908     if (G_OBJECT_CLASS(hildon_window_parent_class)->notify)
909         G_OBJECT_CLASS(hildon_window_parent_class)->notify (gobject, param);
910 }
911
912
913 static void
914 visible_toolbar                                 (gpointer data, 
915                                                  gpointer user_data)
916 {
917     if (GTK_WIDGET_VISIBLE (((GtkBoxChild *)data)->widget))
918         (*((gint *)user_data))++;
919 }
920
921 static void
922 paint_toolbar                                   (GtkWidget *widget, 
923                                                  GtkBox *box, 
924                                                  GdkEventExpose * event, 
925                                                  gboolean fullscreen)
926 {
927     gint toolbar_num = 0; 
928     gint count;
929
930     /* collect info to help on painting the boxes */
931     g_list_foreach (box->children, visible_toolbar, 
932             (gpointer) &toolbar_num);
933
934     if(toolbar_num <= 0)
935         return;
936
937     /*top most toolbar painting*/
938     gtk_paint_box (widget->style, widget->window,
939             GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
940             &event->area, widget, "toolbar-primary",
941             widget->allocation.x,
942             GTK_WIDGET(box)->allocation.y,
943             widget->allocation.width,
944             TOOLBAR_HEIGHT);
945
946     /*multi toolbar painting*/
947     for (count = 0; count < toolbar_num - 1; count++)
948     {
949             gtk_paint_box (widget->style, widget->window,
950                            GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
951                            &event->area, widget, "toolbar-secondary",
952                            widget->allocation.x,
953                            GTK_WIDGET(box)->allocation.y +
954                            (1 + count) * (TOOLBAR_HEIGHT),
955                            widget->allocation.width,
956                            TOOLBAR_HEIGHT);
957     }
958 }
959
960 static void
961 paint_edit_toolbar                              (GtkWidget *widget,
962                                                  GtkWidget *toolbar,
963                                                  GdkEventExpose *event,
964                                                  gboolean fullscreen)
965 {
966     if (!GTK_WIDGET_VISIBLE (toolbar))
967         return;
968
969     gtk_paint_box (widget->style, widget->window,
970                    GTK_WIDGET_STATE (widget), GTK_SHADOW_OUT,
971                    &event->area, widget, "toolbar-edit-mode",
972                    toolbar->allocation.x,
973                    toolbar->allocation.y,
974                    toolbar->allocation.width,
975                    toolbar->allocation.height);
976 }
977
978 /*
979  * Checks the root window to know which is the topped window
980  */
981 Window
982 hildon_window_get_active_window                 (void)
983 {
984     Atom realtype;
985     gint xerror;
986     int format;
987     int status;
988     Window ret;
989     unsigned long n;
990     unsigned long extra;
991     union
992     {
993         Window *win;
994         unsigned char *char_pointer;
995     } win;
996     Atom active_app_atom = 
997         XInternAtom (GDK_DISPLAY (), "_MB_CURRENT_APP_WINDOW", False);
998
999     win.win = NULL;
1000
1001     gdk_error_trap_push ();
1002     status = XGetWindowProperty (GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1003             active_app_atom, 0L, 16L,
1004             0, XA_WINDOW, &realtype, &format,
1005             &n, &extra, &win.char_pointer);
1006     xerror = gdk_error_trap_pop ();
1007     if (xerror || !(status == Success && realtype == XA_WINDOW && format == 32
1008                 && n == 1 && win.win != NULL))
1009     {
1010         if (win.win != NULL)
1011             XFree (win.char_pointer);
1012         return None;
1013     }
1014
1015     ret = win.win[0];
1016
1017     if (win.win != NULL)
1018         XFree(win.char_pointer);
1019
1020     return ret;
1021 }
1022
1023 static int
1024 xclient_message_type_check                      (XClientMessageEvent *cm, 
1025                                                  const gchar *name)
1026 {
1027     return cm->message_type == XInternAtom(GDK_DISPLAY(), name, FALSE);
1028 }
1029
1030 /*
1031  * Handle the window border custom button, which toggles the menu,
1032  * and the Hildon input method copy paste messages
1033  */
1034 static GdkFilterReturn
1035 hildon_window_event_filter                      (GdkXEvent *xevent, 
1036                                                  GdkEvent *event, 
1037                                                  gpointer data)
1038 {
1039     XAnyEvent *eventti = xevent;
1040
1041     if (eventti->type == ClientMessage)
1042     {
1043         XClientMessageEvent *cm = xevent;
1044
1045         if (xclient_message_type_check (cm, "_MB_GRAB_TRANSFER"))
1046         {
1047             hildon_window_toggle_menu (HILDON_WINDOW ( data ), cm->data.l[2], cm->data.l[0]);
1048             return GDK_FILTER_REMOVE;
1049         }
1050         /* opera hack clipboard client message */
1051         else if (xclient_message_type_check (cm, "_HILDON_IM_CLIPBOARD_COPY"))
1052         {
1053             g_signal_emit_by_name(G_OBJECT(data), "clipboard_operation",
1054                     HILDON_WINDOW_CO_COPY);
1055             return GDK_FILTER_REMOVE;
1056         }
1057         else if (xclient_message_type_check(cm, "_HILDON_IM_CLIPBOARD_CUT"))
1058         {
1059             g_signal_emit_by_name(G_OBJECT(data), "clipboard_operation",
1060                     HILDON_WINDOW_CO_CUT);
1061             return GDK_FILTER_REMOVE;
1062         }
1063         else if (xclient_message_type_check(cm, "_HILDON_IM_CLIPBOARD_PASTE"))
1064         {
1065             g_signal_emit_by_name(G_OBJECT(data), "clipboard_operation",
1066                     HILDON_WINDOW_CO_PASTE);
1067             return GDK_FILTER_REMOVE;
1068         }
1069     }
1070
1071     return GDK_FILTER_CONTINUE;
1072 }
1073
1074 /*
1075  * Here we keep track of changes in the _MB_CURRENT_APP_WINDOW,
1076  * to know when we acquire/lose topmost status
1077  */
1078 static GdkFilterReturn
1079 hildon_window_root_window_event_filter          (GdkXEvent *xevent, 
1080                                                  GdkEvent *event, 
1081                                                  gpointer data)
1082 {
1083     XAnyEvent *eventti = xevent;
1084     HildonWindow *hwindow = HILDON_WINDOW (data);
1085
1086     if (eventti->type == PropertyNotify)
1087     {
1088         XPropertyEvent *pevent = xevent;
1089         Atom active_app_atom = 
1090             XInternAtom (GDK_DISPLAY (), "_MB_CURRENT_APP_WINDOW", False);
1091
1092         if (pevent->atom == active_app_atom)
1093         {
1094             Window active_window = hildon_window_get_active_window();
1095
1096             hildon_window_update_topmost (hwindow, active_window);
1097         }
1098     }
1099
1100     return GDK_FILTER_CONTINUE;
1101 }
1102
1103 /*
1104  * Handle the menu hardware key here
1105  */
1106 static gboolean
1107 hildon_window_key_press_event                   (GtkWidget *widget, 
1108                                                  GdkEventKey *event)
1109 {
1110     HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (widget);
1111
1112     g_return_val_if_fail (HILDON_IS_WINDOW (widget),FALSE);
1113     g_assert (priv);
1114
1115     switch (event->keyval)
1116     {
1117         case HILDON_HARDKEY_MENU:
1118             if (hildon_window_toggle_menu (HILDON_WINDOW (widget), 0, GDK_CURRENT_TIME))
1119                 return TRUE;
1120             break;
1121         case HILDON_HARDKEY_ESC:
1122             if (!priv->escape_timeout)
1123             {
1124                 priv->escape_timeout = g_timeout_add 
1125                     (HILDON_WINDOW_LONG_PRESS_TIME,
1126                      hildon_window_escape_timeout, widget);
1127             }
1128             break;
1129     }
1130
1131     return GTK_WIDGET_CLASS (hildon_window_parent_class)->key_press_event (widget, event);
1132 }
1133
1134 static gboolean
1135 hildon_window_key_release_event                 (GtkWidget *widget, 
1136                                                  GdkEventKey *event)
1137 {
1138     HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (widget);
1139
1140     g_return_val_if_fail (HILDON_IS_WINDOW (widget), FALSE);
1141     g_assert (priv);
1142
1143     switch (event->keyval)
1144     {
1145         case HILDON_HARDKEY_ESC:
1146             if (priv->escape_timeout)
1147             {
1148                 g_source_remove (priv->escape_timeout);
1149                 priv->escape_timeout = 0;
1150             }
1151             break;
1152     }
1153
1154     return GTK_WIDGET_CLASS (hildon_window_parent_class)->key_release_event (widget, event);
1155
1156 }
1157
1158 /*
1159  * We keep track of the window state changes, because the drawing
1160  * (borders) differs whether we are in fullscreen mode or not
1161  */
1162 static gboolean
1163 hildon_window_window_state_event                (GtkWidget *widget, 
1164                                                  GdkEventWindowState *event)
1165 {
1166     HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (widget);
1167     g_assert (priv != NULL);
1168
1169     if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
1170         priv->fullscreen = event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN;
1171
1172     if (GTK_WIDGET_CLASS (hildon_window_parent_class)->window_state_event)
1173     {
1174         return GTK_WIDGET_CLASS (hildon_window_parent_class)->window_state_event (
1175                 widget,
1176                 event);
1177     }
1178     else
1179     {
1180         return FALSE;
1181     }
1182 }
1183
1184 /*
1185  * If the window lost focus while the user started to press the ESC key, we
1186  * won't get the release event. We need to stop the timeout.
1187  */
1188 static gboolean
1189 hildon_window_focus_out_event                   (GtkWidget *widget, 
1190                                                  GdkEventFocus *event)
1191 {
1192   HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (widget);
1193
1194   if (priv->escape_timeout)
1195   {
1196       g_source_remove (priv->escape_timeout);
1197       priv->escape_timeout = 0;
1198   }
1199
1200   return GTK_WIDGET_CLASS (hildon_window_parent_class)->focus_out_event (widget, event);
1201 }
1202
1203 /*
1204  * The menu popuping needs a menu popup-function
1205  */
1206 static void
1207 hildon_window_menu_popup_func                   (GtkMenu *menu, 
1208                                                  gint *x, 
1209                                                  gint *y,
1210                                                  gboolean *push_in, 
1211                                                  GtkWidget *widget)
1212 {
1213     gint window_x = 0;
1214     gint window_y = 0;
1215     GdkWindow *window = GTK_WIDGET(widget)->window;
1216
1217     if (window)
1218     {
1219         gdk_window_get_origin (window, &window_x, &window_y);
1220     }
1221
1222     gtk_widget_style_get (GTK_WIDGET (menu), "horizontal-offset", x,
1223             "vertical-offset", y, NULL);
1224
1225     if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1226     {
1227         *x = GTK_WIDGET (widget)->allocation.width + window_x - GTK_WIDGET (menu)->allocation.width - *x;
1228     }
1229     else
1230         *x += window_x;
1231
1232     *y += window_y;
1233
1234 }
1235
1236 static void
1237 hildon_window_menu_popup_func_full              (GtkMenu *menu, 
1238                                                  gint *x, 
1239                                                  gint *y,
1240                                                  gboolean *push_in, 
1241                                                  GtkWidget *widget)
1242 {
1243     gtk_widget_style_get (GTK_WIDGET (menu), "horizontal-offset", x,
1244             "vertical-offset", y, NULL);
1245
1246     if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1247         *x = GTK_WIDGET (widget)->allocation.width - GTK_WIDGET (menu)->allocation.width - *x;
1248     else
1249         *x = MAX (0, *x);
1250
1251     *y = MAX (0, *y);
1252 }
1253
1254
1255 /*
1256  * Takes the common toolbar when we acquire the top-most status
1257  */
1258 static void
1259 hildon_window_is_topmost_notify                 (HildonWindow *window)
1260 {
1261     HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (window);
1262
1263     g_assert (priv);
1264
1265     if (priv->is_topmost)
1266     {
1267         hildon_window_take_common_toolbar (window);
1268     }
1269 }
1270
1271 /*
1272  * Sets the program to which the window belongs. This should only be called
1273  * by hildon_program_add_window
1274  */
1275 void G_GNUC_INTERNAL
1276 hildon_window_set_program                       (HildonWindow *self, 
1277                                                  GObject *program)
1278 {
1279     HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
1280
1281     g_return_if_fail (HILDON_IS_WINDOW (self));
1282     g_assert (priv != NULL);
1283
1284     if (priv->program)
1285     {
1286         g_object_unref (priv->program);
1287     }
1288
1289     /* Now that we are bound to a program, we can rely on it to track the
1290      * root window */
1291     gdk_window_remove_filter (gdk_get_default_root_window(), 
1292             hildon_window_root_window_event_filter,
1293             self);
1294
1295     priv->program = HILDON_PROGRAM (program);
1296     g_object_ref (program);
1297 }
1298
1299 /*
1300  * Unsets the program to which the window belongs. This should only be called
1301  * by hildon_program_remove_window
1302  */
1303 void G_GNUC_INTERNAL
1304 hildon_window_unset_program                     (HildonWindow *self)
1305 {
1306     HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
1307
1308     g_return_if_fail(HILDON_IS_WINDOW (self));
1309     g_assert (priv != NULL);
1310
1311     if (priv->program)
1312     {
1313         g_object_unref (priv->program);
1314         priv->program = NULL;
1315
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);
1320
1321         gdk_window_add_filter (gdk_get_default_root_window (),
1322                 hildon_window_root_window_event_filter, self );
1323     }
1324
1325     priv->program = NULL;
1326 }
1327
1328 /*
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
1332  **/    
1333 void G_GNUC_INTERNAL
1334 hildon_window_set_can_hibernate_property        (HildonWindow *self, 
1335                                                  gpointer _can_hibernate)
1336 {
1337     GdkAtom killable_atom;
1338     gboolean can_hibernate;
1339
1340     g_return_if_fail(self && HILDON_IS_WINDOW (self));
1341
1342     if (!GTK_WIDGET_REALIZED ((GTK_WIDGET (self))))
1343     {
1344         return;
1345     }
1346
1347     can_hibernate = * ((gboolean *)_can_hibernate);
1348
1349     killable_atom = gdk_atom_intern (CAN_HIBERNATE_PROPERTY, FALSE);
1350
1351     if (can_hibernate)
1352     {
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);
1357     }
1358     else
1359     {
1360         gdk_property_delete (GTK_WIDGET (self)->window, killable_atom);
1361     }
1362
1363 }
1364
1365 /*
1366  * If a common toolbar was set to the program, reparent it to
1367  * us
1368  */
1369 void G_GNUC_INTERNAL
1370 hildon_window_take_common_toolbar               (HildonWindow *self)
1371 {
1372     HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
1373
1374     g_return_if_fail(HILDON_IS_WINDOW (self));
1375     g_assert (priv);
1376
1377     if (priv->program)
1378     {
1379         GtkWidget *common_toolbar =  
1380             GTK_WIDGET (hildon_program_get_common_toolbar (priv->program));
1381
1382         if (common_toolbar && common_toolbar->parent != priv->vbox)
1383         {
1384             g_object_ref (common_toolbar);
1385             if (common_toolbar->parent)
1386             {
1387                 gtk_container_remove (GTK_CONTAINER (common_toolbar->parent),
1388                         common_toolbar);
1389             }
1390
1391             gtk_box_pack_end (GTK_BOX(priv->vbox), common_toolbar,
1392                     TRUE, TRUE, 0);
1393             g_object_unref (common_toolbar);
1394
1395             gtk_widget_set_size_request (common_toolbar, -1, TOOLBAR_HEIGHT);
1396
1397             gtk_widget_show  (priv->vbox);
1398
1399         }
1400     }
1401 }
1402
1403 /*
1404  * Compare the window that was last topped, and act consequently
1405  */
1406 void
1407 hildon_window_update_topmost                    (HildonWindow *self, 
1408                                                  Window window_id)
1409 {
1410     HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
1411
1412     GdkWindow *my_window;
1413
1414     g_return_if_fail (HILDON_IS_WINDOW (self));
1415     g_assert (priv);
1416
1417     my_window = GTK_WIDGET (self)->window;
1418
1419     if (my_window && window_id == GDK_WINDOW_XID (my_window))
1420     {
1421         if (! priv->is_topmost)
1422         {
1423             priv->is_topmost = TRUE;
1424             hildon_window_is_topmost_notify (self);
1425             g_object_notify (G_OBJECT (self), "is-topmost");
1426         }
1427     }
1428     else if (priv->is_topmost)
1429     {
1430         /* Should this go in the signal handler? */
1431         GtkWidget *focus = gtk_window_get_focus (GTK_WINDOW (self));
1432
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);
1437
1438         priv->is_topmost = FALSE;
1439         hildon_window_is_topmost_notify (self);
1440         g_object_notify (G_OBJECT (self), "is-topmost");
1441     }
1442 }
1443
1444 static void
1445 detach_menu_func                                (GtkWidget *attach_widget, 
1446                                                  GtkMenu *menu)
1447 {
1448     /* FIXME Why is this even needed here? */
1449 }
1450
1451 static gboolean
1452 hildon_window_toggle_menu                       (HildonWindow *self,
1453                                                  guint button,
1454                                                  guint32 time)
1455 {
1456     g_return_val_if_fail (HILDON_IS_WINDOW (self), FALSE);
1457
1458     if (HILDON_WINDOW_GET_CLASS (self)->toggle_menu != NULL)
1459     {
1460         return HILDON_WINDOW_GET_CLASS (self)->toggle_menu (self, button, time);
1461     }
1462     else
1463     {
1464         return FALSE;
1465     }
1466 }
1467
1468
1469 /*
1470  * Toggles the display of the HildonWindow menu.
1471  * Returns whether or not something was done (whether or not we had a menu
1472  * to toggle)
1473  */
1474 static gboolean
1475 hildon_window_toggle_menu_real                  (HildonWindow * self,
1476                                                  guint button,
1477                                                  guint32 time)
1478 {
1479     GtkMenu *menu_to_use = NULL;
1480     GList *menu_children = NULL;
1481     HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
1482
1483     g_return_val_if_fail (HILDON_IS_WINDOW (self), FALSE);
1484     g_assert (priv != NULL);
1485
1486     /* Select which menu to use, Window specific has highest priority,
1487      * then program specific */
1488     if (priv->menu)
1489     {
1490         menu_to_use = GTK_MENU (priv->menu);
1491     }
1492     else if (priv->program)
1493     {
1494         menu_to_use = hildon_program_get_common_menu (priv->program);
1495         if (menu_to_use && gtk_menu_get_attach_widget (menu_to_use) != 
1496                 GTK_WIDGET (self))
1497         {
1498             g_object_ref (menu_to_use);
1499             if (gtk_menu_get_attach_widget (menu_to_use))
1500             {
1501                 gtk_menu_detach (menu_to_use);
1502             }
1503
1504             gtk_menu_attach_to_widget (menu_to_use, GTK_WIDGET (self), 
1505                     &detach_menu_func);
1506             g_object_unref (menu_to_use);
1507         }
1508     }
1509
1510     if (! menu_to_use)
1511     {
1512         return FALSE;
1513     }
1514
1515
1516     if (GTK_WIDGET_MAPPED (GTK_WIDGET (menu_to_use)))
1517     {
1518         gtk_menu_popdown (menu_to_use);
1519         gtk_menu_shell_deactivate (GTK_MENU_SHELL (menu_to_use));
1520         return TRUE;
1521     }
1522
1523     /* Check if the menu has items */
1524     menu_children = gtk_container_get_children (GTK_CONTAINER (menu_to_use));
1525
1526     if (menu_children)
1527     {
1528         g_list_free (menu_children);
1529
1530         /* Apply right theming */
1531         gtk_widget_set_name (GTK_WIDGET (menu_to_use),
1532                 "menu_force_with_corners");
1533
1534         if (priv->fullscreen) 
1535         {
1536             gtk_menu_popup (menu_to_use, NULL, NULL,
1537                     (GtkMenuPositionFunc)
1538                     hildon_window_menu_popup_func_full,
1539                     self, button, time);
1540         }
1541         else
1542         {
1543             gtk_menu_popup (menu_to_use, NULL, NULL,
1544                     (GtkMenuPositionFunc)
1545                     hildon_window_menu_popup_func,
1546                     self, button, time);
1547         }
1548         gtk_menu_shell_select_first (GTK_MENU_SHELL (menu_to_use), TRUE);
1549         return TRUE;
1550     }
1551
1552     return FALSE;
1553 }
1554
1555 /*
1556  * If the ESC key was not released when the timeout expires,
1557  * close the window
1558  */
1559 static gboolean
1560 hildon_window_escape_timeout                    (gpointer data)
1561 {
1562     HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (data);
1563     GdkEvent *event;
1564
1565     g_assert (priv);
1566
1567     GDK_THREADS_ENTER ();
1568
1569     /* Send fake event, simulation a situation that user
1570        pressed 'x' from the corner */
1571     event = gdk_event_new(GDK_DELETE);
1572     ((GdkEventAny *)event)->window = GDK_WINDOW (g_object_ref (GTK_WIDGET(data)->window));
1573     gtk_main_do_event(event);
1574
1575     /* That unrefs the window, so we're reffing it above */
1576     gdk_event_free(event);
1577
1578     priv->escape_timeout = 0;
1579
1580     GDK_THREADS_LEAVE ();
1581
1582     return FALSE;
1583 }
1584
1585 /**
1586  * hildon_window_new: 
1587  * 
1588  * Creates a new HildonWindow.
1589  * 
1590  * Return value: A @HildonWindow.
1591  **/
1592 GtkWidget*
1593 hildon_window_new                               (void)
1594 {
1595     HildonWindow *newwindow = g_object_new (HILDON_TYPE_WINDOW, NULL);
1596
1597     return GTK_WIDGET (newwindow);
1598 }
1599
1600 /**
1601  * hildon_window_add_with_scrollbar
1602  * @self : A @HildonWindow
1603  * @child : A @GtkWidget
1604  *
1605  * Adds the @child to the HildonWindow and creates a scrollbar
1606  * for it. Similar as adding first a @GtkScrolledWindow and then the
1607  * @child to it.
1608  */
1609 void
1610 hildon_window_add_with_scrollbar                (HildonWindow *self,
1611                                                  GtkWidget *child)
1612 {
1613     GtkScrolledWindow *scrolledw;
1614
1615     g_return_if_fail (HILDON_IS_WINDOW (self));
1616     g_return_if_fail (GTK_IS_WIDGET (child));
1617     g_return_if_fail (child->parent == NULL);
1618
1619     scrolledw = GTK_SCROLLED_WINDOW (gtk_scrolled_window_new (NULL, NULL));
1620     gtk_scrolled_window_set_policy (scrolledw, GTK_POLICY_NEVER,
1621             GTK_POLICY_AUTOMATIC);
1622     gtk_scrolled_window_set_shadow_type (scrolledw, GTK_SHADOW_NONE);
1623
1624     if (GTK_IS_VIEWPORT (child))
1625         gtk_container_add (GTK_CONTAINER (scrolledw), child);
1626     else
1627     {
1628         if (GTK_IS_CONTAINER (child) )
1629             gtk_container_set_focus_vadjustment (GTK_CONTAINER(child),
1630                     gtk_scrolled_window_get_vadjustment (scrolledw) );
1631         gtk_scrolled_window_add_with_viewport (scrolledw, child);
1632     }
1633
1634     gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (scrolledw));
1635 }
1636
1637 static void
1638 calculate_visible_toolbars                      (gpointer data,
1639                                                  gpointer user_data)
1640 {
1641   if (GTK_WIDGET_VISIBLE (GTK_WIDGET (((GtkBoxChild *)data)->widget)))
1642     (*((gint *)user_data)) ++;
1643 }
1644
1645 static void
1646 toolbar_visible_notify                          (GtkWidget *toolbar, GParamSpec *pspec,
1647                                                  HildonWindow *window)
1648 {
1649   HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (window);
1650
1651   g_assert (priv);
1652
1653   /* Recalculate from scratch the value just in case */
1654   priv->visible_toolbars = 0;
1655
1656   g_list_foreach (GTK_BOX (priv->vbox)->children, calculate_visible_toolbars, 
1657                   &priv->visible_toolbars);
1658
1659   if (priv->visible_toolbars == 0)
1660     gtk_widget_hide (priv->vbox);
1661   else
1662     gtk_widget_show (priv->vbox);
1663 }
1664
1665 /**
1666  * hildon_window_add_toolbar:
1667  * @self: A @HildonWindow
1668  * @toolbar: A #GtkToolbar to add to the HildonWindow
1669  *
1670  * Adds a toolbar to the window. Note that the toolbar is not automatically
1671  * shown. You need to call #gtk_widget_show_all on it to make it visible. 
1672  * It's also possible to hide the toolbar (without removing it) by calling
1673  * #gtk_widget_hide.
1674  **/
1675 void 
1676 hildon_window_add_toolbar                       (HildonWindow *self, 
1677                                                  GtkToolbar *toolbar)
1678 {
1679     GtkBox *vbox;
1680     HildonWindowPrivate *priv;
1681
1682     g_return_if_fail (HILDON_IS_WINDOW (self));
1683     g_return_if_fail (toolbar && GTK_IS_TOOLBAR (toolbar));
1684
1685     priv = HILDON_WINDOW_GET_PRIVATE (self);
1686
1687     vbox = GTK_BOX (priv->vbox);
1688
1689     gtk_box_pack_start (vbox, GTK_WIDGET (toolbar), TRUE, TRUE, 0);
1690     gtk_box_reorder_child (vbox, GTK_WIDGET (toolbar), 0);
1691     gtk_widget_set_size_request (GTK_WIDGET (toolbar), -1, TOOLBAR_HEIGHT);
1692
1693     g_signal_connect (G_OBJECT (toolbar), "notify::visible",
1694                       G_CALLBACK (toolbar_visible_notify), self);
1695
1696     if (GTK_WIDGET_VISIBLE (toolbar))
1697       {
1698         priv->visible_toolbars++;
1699         gtk_widget_show (priv->vbox);
1700       }
1701
1702     gtk_widget_queue_resize (GTK_WIDGET (self));
1703 }
1704
1705 /**
1706  * hildon_window_remove_toolbar:
1707  * @self: A @HildonWindow
1708  * @toolbar: A #GtkToolbar to remove from the HildonWindow
1709  *
1710  * Removes a toolbar from the window. Note that this decreases the refference
1711  * count on the widget. If you want to keep the toolbar alive call #g_object_ref 
1712  * before calling this function.
1713  **/
1714 void
1715 hildon_window_remove_toolbar                    (HildonWindow *self, 
1716                                                  GtkToolbar *toolbar)
1717 {
1718     HildonWindowPrivate *priv;
1719
1720     g_return_if_fail (HILDON_IS_WINDOW (self));
1721     
1722     priv = HILDON_WINDOW_GET_PRIVATE (self);
1723
1724     if (GTK_WIDGET_VISIBLE (toolbar))
1725       {
1726         if (--(priv->visible_toolbars) == 0)
1727           gtk_widget_hide (priv->vbox);
1728       }
1729
1730     g_signal_handlers_disconnect_by_func (toolbar, toolbar_visible_notify, self);
1731
1732     gtk_container_remove (GTK_CONTAINER (priv->vbox), GTK_WIDGET (toolbar));
1733 }
1734
1735 /**
1736  * hildon_window_set_edit_toolbar:
1737  * @self: A #HildonWindow
1738  * @toolbar: A #HildonEditToolbar, or %NULL to remove the current one.
1739  *
1740  * Adds a #HildonEditToolbar to the window. Note that the toolbar is
1741  * not automatically shown. You need to call gtk_widget_show() on it
1742  * to make it visible. It's also possible to hide the toolbar (without
1743  * removing it) by calling gtk_widget_hide().
1744  *
1745  * A window can only have at most one edit toolbar at a time, so the
1746  * previous toolbar (if any) is replaced after calling this function.
1747  **/
1748 void
1749 hildon_window_set_edit_toolbar                  (HildonWindow      *self,
1750                                                  HildonEditToolbar *toolbar)
1751 {
1752     HildonWindowPrivate *priv;
1753
1754     g_return_if_fail (HILDON_IS_WINDOW (self));
1755     g_return_if_fail (toolbar == NULL || HILDON_IS_EDIT_TOOLBAR (toolbar));
1756
1757     priv = HILDON_WINDOW_GET_PRIVATE (self);
1758
1759     if (priv->edit_toolbar != GTK_WIDGET (toolbar))
1760     {
1761         GtkWidget *old_toolbar = priv->edit_toolbar;
1762         priv->edit_toolbar = GTK_WIDGET (toolbar);
1763
1764         if (priv->edit_toolbar)
1765             gtk_widget_set_parent (priv->edit_toolbar, GTK_WIDGET (self));
1766
1767         if (old_toolbar)
1768             gtk_widget_unparent (old_toolbar);
1769     }
1770 }
1771
1772 /**
1773  * hildon_window_get_menu:
1774  * @self : #HildonWindow
1775  * 
1776  * Gets the #GtMenu assigned to the #HildonAppview. Note that the 
1777  * window is still the owner of the menu.
1778  * 
1779  * Return value: The #GtkMenu assigned to this application view. 
1780  **/
1781 GtkMenu*
1782 hildon_window_get_menu                          (HildonWindow * self)
1783 {
1784     HildonWindowPrivate *priv;
1785
1786     g_return_val_if_fail (HILDON_IS_WINDOW (self), NULL);
1787
1788     priv = HILDON_WINDOW_GET_PRIVATE (self);
1789
1790     return GTK_MENU (priv->menu);
1791 }
1792
1793 /* Since we've been asking developers to call gtk_window_add_accel_group()
1794  * themselves, do not trigger criticals by trying it again.
1795  */
1796 static void
1797 hildon_window_add_accel_group (HildonWindow *self,
1798                                GtkAccelGroup *accel_group)
1799 {
1800     GSList *groups, *l;
1801
1802     groups = gtk_accel_groups_from_object (G_OBJECT (self));
1803     for (l = groups; l != NULL; l = l->next)
1804       if (l->data == (gpointer)accel_group)
1805         /* Maybe print a warning here? */
1806         return;
1807
1808     gtk_window_add_accel_group (GTK_WINDOW (self), accel_group);
1809 }
1810
1811 /**
1812  * hildon_window_set_main_menu:
1813  * @self: A #HildonWindow
1814  * @menu: The #GtkMenu to be used for this #HildonWindow
1815  * 
1816  * Sets the menu to be used for this window. This menu overrides
1817  * a program-wide menu that may have been set with
1818  * hildon_program_set_common_menu(). Pass %NULL to remove the current
1819  * menu. #HildonWindow takes ownership of the passed menu and you're
1820  * not supposed to free it yourself anymore.
1821  *
1822  * Note that if you're using a #HildonStackableWindow (and not just a
1823  * standard #HildonWindow) you should use
1824  * hildon_stackable_window_set_main_menu()
1825  * instead. #HildonStackableWindow uses #HildonAppMenu rather than
1826  * #GtkMenu.
1827  *
1828  * Since: 2.2
1829  **/ 
1830 void
1831 hildon_window_set_main_menu (HildonWindow* self,
1832                              GtkMenu     * menu)
1833 {
1834     HildonWindowPrivate *priv;
1835     GtkAccelGroup *accel_group;
1836
1837     g_return_if_fail (HILDON_IS_WINDOW (self));
1838
1839     priv = HILDON_WINDOW_GET_PRIVATE (self);
1840
1841     if (priv->menu != NULL)
1842     {
1843         accel_group = gtk_menu_get_accel_group (GTK_MENU (priv->menu));
1844         if (accel_group != NULL)
1845             gtk_window_remove_accel_group (GTK_WINDOW (self), accel_group);
1846
1847         gtk_menu_detach (GTK_MENU (priv->menu));
1848         g_object_unref (priv->menu);
1849     }
1850
1851     priv->menu = (menu != NULL) ? GTK_WIDGET (menu) : NULL;
1852     if (priv->menu != NULL)
1853     {
1854         gtk_widget_set_name (priv->menu, "menu_force_with_corners");
1855         gtk_menu_attach_to_widget (GTK_MENU (priv->menu), GTK_WIDGET (self), &detach_menu_func);
1856         g_object_ref (GTK_MENU (priv->menu));
1857
1858         accel_group = gtk_menu_get_accel_group (GTK_MENU (priv->menu));
1859         if (accel_group != NULL)
1860             hildon_window_add_accel_group (self, accel_group);
1861     }
1862 }
1863
1864 /**
1865  * hildon_window_set_menu:
1866  * @self: A #HildonWindow
1867  * @menu: The #GtkMenu to be used for this #HildonWindow
1868  * 
1869  * Sets the menu to be used for this window. This menu overrides
1870  * a program-wide menu that may have been set with
1871  * hildon_program_set_common_menu. Pass NULL to remove the current
1872  * menu. HildonWindow takes ownership of the passed menu and you're
1873  * not supposed to free it yourself anymore.
1874  *
1875  * Note: hildon_window_set_menu() calls gtk_widget_show_all() for the
1876  * #GtkMenu. To pass control about visibility to the application
1877  * developer, hildon_window_set_main_menu() was introduced, which
1878  * doesn't do this.
1879  *
1880  * Deprecated: Hildon 2.2: use hildon_window_set_main_menu()
1881  **/ 
1882 void
1883 hildon_window_set_menu                          (HildonWindow *self, 
1884                                                  GtkMenu *menu)
1885 {
1886     HildonWindowPrivate *priv;
1887
1888     g_return_if_fail (HILDON_IS_WINDOW (self));
1889
1890     hildon_window_set_main_menu (self, menu);
1891
1892     priv = HILDON_WINDOW_GET_PRIVATE (self);
1893
1894     if (priv->menu != NULL)
1895         gtk_widget_show_all (GTK_WIDGET (priv->menu));
1896 }
1897
1898 /**
1899  * hildon_window_get_is_topmost:
1900  * @self: A #HildonWindow
1901  *
1902  * Returns whether the #HildonWindow is currenty activated by the
1903  * window manager.
1904  *
1905  * Return value: %TRUE if @self is currently activated, %FALSE otherwise.
1906  **/
1907 gboolean
1908 hildon_window_get_is_topmost                    (HildonWindow *self)
1909 {
1910     HildonWindowPrivate *priv;
1911
1912     g_return_val_if_fail (HILDON_IS_WINDOW (self), FALSE);
1913
1914     priv = HILDON_WINDOW_GET_PRIVATE (self);
1915     return priv->is_topmost;
1916 }
1917