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