2006-09-12 Michael Dominic Kostrzewa <michael.kostrzewa@nokia.com>
[hildon] / hildon-widgets / hildon-window.c
1 /*
2  * This file is part of hildon-libs
3  *
4  * Copyright (C) 2006 Nokia Corporation, all rights reserved.
5  *
6  * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@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 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 #include <memory.h>
27 #include <string.h>
28 #include <strings.h>
29 #include <stdio.h>
30 #include "hildon-app.h"
31 #include <hildon-window.h>
32 #include "hildon-program.h"
33 #include "hildon-window-private.h"
34 #include <hildon-find-toolbar.h>
35
36 #include <gtk/gtkmenu.h>
37 #include <gtk/gtkimcontext.h>
38 #include <gtk/gtkmenuitem.h>
39 #include <gtk/gtkcheckmenuitem.h>
40 #include <gtk/gtkmenushell.h>
41 #include <gtk/gtkwindow.h>
42 #include <gtk/gtkwidget.h>
43 #include <gtk/gtkvbox.h>
44 #include <gtk/gtklabel.h>
45 #include <gtk/gtkentry.h>
46 #include <gtk/gtktextview.h>
47 #include <gtk/gtkscrolledwindow.h>
48 #include <gtk/gtkmain.h>
49 #include <gdk/gdkkeysyms.h>
50 #include <gdk/gdk.h>
51
52 #include<gtk/gtkprivate.h>
53
54 #include <X11/X.h>
55 #include <X11/Xlib.h>
56 #include <X11/Xatom.h>
57
58
59 #include <libintl.h>
60 #define _(String) gettext(String)
61
62 /*The size of screen*/
63 #define WINDOW_HEIGHT           480
64 #define WINDOW_WIDTH            800
65
66 #define NAVIGATOR_HEIGHT        WINDOW_HEIGHT
67
68 #define APPVIEW_HEIGHT          396
69 #define APPVIEW_WIDTH           672
70
71 #define TOOLBAR_HEIGHT          40
72 #define TOOLBAR_MIDDLE          10
73 #define TOOLBAR_WIDTH           APPVIEW_WIDTH
74
75 /*FIXME*/
76 #define CAN_HIBERNATE "CANKILL"
77 #define CAN_HIBERNATE_LENGTH 7
78
79 #define CAN_HIBERNATE_PROPERTY "_HILDON_ABLE_TO_HIBERNATE"
80
81 #define TITLE_SEPARATOR " - "
82
83
84 #define HILDON_WINDOW_GET_PRIVATE(obj) \
85     (G_TYPE_INSTANCE_GET_PRIVATE ((obj),\
86      HILDON_TYPE_WINDOW, HildonWindowPrivate))
87
88 static GtkWindowClass *parent_class;
89
90 static void
91 hildon_window_init (HildonWindow * self);
92
93 static void
94 hildon_window_class_init (HildonWindowClass * window_class);
95
96 static void
97 hildon_window_menupopupfunc (GtkMenu *menu, gint *x, gint *y,
98                                          gboolean *push_in,
99                                          GtkWidget *widget);
100 static void
101 hildon_window_menupopupfuncfull (GtkMenu *menu, gint *x, gint *y,
102                                              gboolean *push_in,
103                                              GtkWidget *widget);
104 static gboolean
105 hildon_window_expose (GtkWidget * widget, GdkEventExpose * event);
106 static void 
107 hildon_window_forall (GtkContainer * container,
108                       gboolean include_internals,
109                       GtkCallback callback,
110                       gpointer callback_data);
111 static void
112 hildon_window_show_all (GtkWidget *widget);
113
114 static void
115 hildon_window_size_allocate (GtkWidget * widget,
116                              GtkAllocation * allocation);
117 static void
118 hildon_window_size_request (GtkWidget * widget,
119                             GtkRequisition * requisition);
120 static void
121 hildon_window_finalize (GObject * obj_self);
122 static void
123 hildon_window_set_property (GObject * object, guint property_id,
124                             const GValue * value, GParamSpec * pspec);
125 static void
126 hildon_window_get_property (GObject * object, guint property_id,
127                             GValue * value, GParamSpec * pspec);
128 static void
129 hildon_window_destroy (GtkObject *obj);
130 static void
131 hildon_window_realize (GtkWidget *widget);
132 static void
133 hildon_window_unrealize (GtkWidget *widget);
134 static gboolean
135 hildon_window_key_press_event (GtkWidget         *widget,
136                                GdkEventKey       *event);
137 static gboolean
138 hildon_window_key_release_event (GtkWidget       *widget, 
139                                  GdkEventKey     *event);
140 static gboolean
141 hildon_window_window_state_event (GtkWidget *widget, 
142                                   GdkEventWindowState *event);
143
144
145 static void
146 hildon_window_notify (GObject *gobject, GParamSpec *param);
147
148 static void
149 hildon_window_is_topmost_notify (HildonWindow *window);
150
151 static gboolean
152 hildon_window_toggle_menu (HildonWindow * self);
153
154 static gboolean
155 hildon_window_escape_timeout (gpointer data);
156
157 static GdkFilterReturn
158 hildon_window_event_filter (GdkXEvent *xevent, GdkEvent *event, gpointer data );
159
160 static GdkFilterReturn
161 hildon_window_root_window_event_filter (GdkXEvent *xevent, 
162                                         GdkEvent *event, 
163                                         gpointer data );
164
165 static void
166 hildon_window_get_borders (HildonWindow *window);
167
168 static void
169 visible_toolbar (gpointer data, gpointer user_data);
170 static void
171 paint_toolbar (GtkWidget *widget, GtkBox *box, 
172                        GdkEventExpose * event, 
173                            gboolean fullscreen);
174
175 typedef void (*HildonWindowSignal) (HildonWindow *, gint, gpointer);
176
177
178
179 enum
180 {
181     PROP_0,
182     PROP_IS_TOPMOST
183 };
184
185 enum
186 {
187     WIN_TYPE = 0,
188     WIN_TYPE_MESSAGE,
189     MAX_WIN_MESSAGES
190 };
191
192 struct _HildonWindowPrivate
193 {
194     GtkWidget *menu;
195     GtkWidget *vbox;
196
197     GtkBorder *borders;
198     GtkBorder *toolbar_borders;
199
200     GtkAllocation allocation;
201
202     guint fullscreen;
203     guint is_topmost;
204     guint escape_timeout;
205     gint visible_toolbars;
206     gint previous_vbox_y;
207
208     HildonProgram *program;
209 };
210
211 GType 
212 hildon_window_get_type (void)
213 {
214     static GType window_type = 0;
215
216     if (!window_type) {
217         static const GTypeInfo window_info = {
218             sizeof(HildonWindowClass),
219             NULL,       /* base_init */
220             NULL,       /* base_finalize */
221             (GClassInitFunc) hildon_window_class_init,
222             NULL,       /* class_finalize */
223             NULL,       /* class_data */
224             sizeof(HildonWindow),
225             0,  /* n_preallocs */
226             (GInstanceInitFunc) hildon_window_init,
227         };
228         window_type = g_type_register_static(GTK_TYPE_WINDOW,
229                                               "HildonWindow",
230                                               &window_info, 0);
231     }
232     return window_type;
233 }
234
235 /* Virtual methods */
236
237 static void 
238 hildon_window_class_init (HildonWindowClass * window_class)
239 {
240     /* Get convenience variables */
241     GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (window_class);
242     GObjectClass *object_class = G_OBJECT_CLASS (window_class);
243     GtkContainerClass *container_class = GTK_CONTAINER_CLASS (window_class);
244
245     /* Set the global parent_class here */
246     parent_class = g_type_class_peek_parent (window_class);
247
248     object_class->set_property = hildon_window_set_property;
249     object_class->get_property = hildon_window_get_property;
250     object_class->notify = hildon_window_notify;
251
252     /* Set the widgets virtual functions */
253     widget_class->size_allocate = hildon_window_size_allocate;
254     widget_class->size_request = hildon_window_size_request;
255     widget_class->expose_event = hildon_window_expose;
256     widget_class->show_all = hildon_window_show_all;
257     widget_class->realize = hildon_window_realize;
258     widget_class->unrealize = hildon_window_unrealize;
259     widget_class->key_press_event = hildon_window_key_press_event;
260     widget_class->key_release_event = hildon_window_key_release_event;
261     widget_class->window_state_event = hildon_window_window_state_event;
262     
263     /* now the object stuff */
264     object_class->finalize = hildon_window_finalize;
265
266     /* To the container */
267     container_class->forall = hildon_window_forall;
268     
269     /* gtkobject stuff*/
270     GTK_OBJECT_CLASS (window_class)->destroy = hildon_window_destroy; 
271
272     g_type_class_add_private (window_class,
273                               sizeof (struct _HildonWindowPrivate));
274                      
275     /* Install properties */
276     g_object_class_install_property (object_class, PROP_IS_TOPMOST,
277                 g_param_spec_boolean ("is-topmost",
278                 "Is top-most",
279                 "Whether the window is currently activated by the window "
280                 "manager",
281                 FALSE,
282                 G_PARAM_READABLE));
283
284     gtk_widget_class_install_style_property (widget_class,
285                      g_param_spec_boxed ("borders",
286                                          "Graphical borders",
287                                          "Size of graphical window borders",
288                                           GTK_TYPE_BORDER,
289                                           G_PARAM_READABLE));
290     
291     gtk_widget_class_install_style_property (widget_class,
292                      g_param_spec_boxed ("toolbar-borders",
293                                          "Graphical toolbar borders",
294                                          "Size of graphical toolbar borders",
295                                           GTK_TYPE_BORDER,
296                                           G_PARAM_READABLE));
297     
298     /* opera hack, install clip operation signal */
299     g_signal_new ("clipboard_operation",
300                   G_OBJECT_CLASS_TYPE (object_class),
301                   G_SIGNAL_RUN_FIRST,
302                   G_STRUCT_OFFSET (HildonWindowClass, clipboard_operation),
303                   NULL, NULL,
304                   g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1,
305                   GTK_TYPE_INT);
306 }
307
308 static void
309 hildon_window_init (HildonWindow * self)
310 {
311     HildonWindowPrivate *priv = self->priv = HILDON_WINDOW_GET_PRIVATE(self);
312
313     self->priv->vbox = gtk_vbox_new (TRUE, TOOLBAR_MIDDLE);
314     gtk_widget_set_parent (self->priv->vbox, GTK_WIDGET(self));
315     priv->menu = NULL;
316     priv->visible_toolbars = 0;
317     priv->is_topmost = FALSE;
318     priv->borders = NULL;
319     priv->toolbar_borders = NULL;
320     priv->escape_timeout = 0;
321
322     priv->fullscreen = FALSE;
323    
324     priv->program = NULL;
325     
326     /* We need to track the root window _MB_CURRENT_APP_WINDOW property */
327     gdk_window_set_events (gdk_get_default_root_window (),
328                           gdk_window_get_events (gdk_get_default_root_window ())                          | GDK_PROPERTY_CHANGE_MASK);
329
330     gdk_window_add_filter (gdk_get_default_root_window (), 
331             hildon_window_root_window_event_filter, self);
332 }
333
334 static void
335 hildon_window_finalize (GObject * obj_self)
336 {
337     HildonWindow *self;
338     g_return_if_fail (HILDON_WINDOW (obj_self));
339     self = HILDON_WINDOW (obj_self);
340
341     g_free (self->priv->borders);
342     g_free (self->priv->toolbar_borders);
343
344     if (G_OBJECT_CLASS (parent_class)->finalize)
345         G_OBJECT_CLASS (parent_class)->finalize (obj_self);
346
347 }
348
349 static void
350 hildon_window_realize (GtkWidget *widget)
351 {
352     Atom *old_atoms, *new_atoms;
353     Display *disp;
354     Window window;
355     gint atom_count;
356     Window active_window;
357
358     GTK_WIDGET_CLASS (parent_class)->realize (widget);
359     
360     gtk_widget_realize (GTK_WIDGET (HILDON_WINDOW (widget)->priv->vbox));
361
362     
363     /* catch the custom button signal from mb to display the menu */
364     gdk_window_add_filter (widget->window, hildon_window_event_filter, widget );
365     
366     window = GDK_WINDOW_XID ( widget->window );
367     disp = GDK_WINDOW_XDISPLAY ( widget->window );
368     
369     /* Enable custom button that is used for menu */
370     XGetWMProtocols (disp, window, &old_atoms, &atom_count);
371     new_atoms = g_new (Atom, atom_count + 1);
372
373     memcpy (new_atoms, old_atoms, sizeof(Atom) * atom_count);
374
375     new_atoms[atom_count++] =
376         XInternAtom (disp, "_NET_WM_CONTEXT_CUSTOM", False);
377
378     XSetWMProtocols (disp, window, new_atoms, atom_count);
379
380     XFree(old_atoms);
381     g_free(new_atoms);
382        
383    /* rely on GDK to set the window group to its default */
384    gdk_window_set_group (widget->window, NULL);
385
386    if (HILDON_WINDOW (widget)->priv->program)
387    {
388        gboolean can_hibernate = hildon_program_get_can_hibernate (
389              HILDON_WINDOW (widget)->priv->program);
390
391        hildon_window_set_can_hibernate_property (HILDON_WINDOW (widget),
392                  &can_hibernate);
393    }
394
395    /* Update the topmost status */
396    active_window = hildon_window_get_active_window();
397    hildon_window_update_topmost (HILDON_WINDOW (widget), active_window);
398
399    /* Update the window title */
400    hildon_window_update_title(HILDON_WINDOW (widget));
401
402 }
403
404 static void
405 hildon_window_unrealize (GtkWidget *widget)
406 {
407
408     gdk_window_remove_filter (widget->window, hildon_window_event_filter,
409                               widget);
410     
411     gtk_widget_unrealize (GTK_WIDGET (HILDON_WINDOW (widget)->priv->vbox));
412     GTK_WIDGET_CLASS(parent_class)->unrealize(widget);
413 }
414
415 static void
416 hildon_window_set_property (GObject * object, guint property_id,
417                             const GValue * value, GParamSpec * pspec)
418 {
419     /*HildonWindow *window = HILDON_WINDOW (object);*/
420
421     switch (property_id) {
422
423     default:
424         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
425         break;
426     }
427 }
428
429 static void
430 hildon_window_get_property (GObject * object, guint property_id,
431                             GValue * value, GParamSpec * pspec)
432 {
433     HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (object);
434
435     switch (property_id) {
436
437     case PROP_IS_TOPMOST:
438             g_value_set_boolean (value, priv->is_topmost);
439         break;
440
441     default:
442         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
443         break;
444     }
445 }
446
447 /*
448  * Retrieve the graphical borders size used by the themes
449  */
450 static void
451 hildon_window_get_borders (HildonWindow *window)
452 {
453
454     g_free (window->priv->borders);
455     g_free (window->priv->toolbar_borders);
456
457     gtk_widget_style_get (GTK_WIDGET (window), "borders",&window->priv->borders,
458                           "toolbar-borders", &window->priv->toolbar_borders,
459                           NULL);
460     
461     if (!window->priv->borders)
462     {
463         window->priv->borders = (GtkBorder *)g_malloc0 (sizeof (GtkBorder));
464     }
465     
466     if (!window->priv->toolbar_borders)
467     {
468         window->priv->toolbar_borders = 
469             (GtkBorder *)g_malloc0 (sizeof (GtkBorder));
470     }
471 }
472
473 static void
474 visible_toolbars (gpointer data, gpointer user_data)
475 {
476     if (GTK_WIDGET_VISIBLE (GTK_WIDGET (((GtkBoxChild *)data)->widget)))
477         (*((gint *)user_data)) ++;
478 }
479
480 static gboolean
481 hildon_window_expose (GtkWidget * widget, GdkEventExpose * event)
482 {
483     HildonWindowPrivate *priv = HILDON_WINDOW (widget)->priv;
484     GtkWidget *bx = HILDON_WINDOW(widget)->priv->vbox;
485     GtkBox *box = GTK_BOX(bx);
486     GtkBorder *b = HILDON_WINDOW(widget)->priv->borders;
487     GtkBorder *tb = HILDON_WINDOW(widget)->priv->toolbar_borders;
488     gint tb_height = 0;
489     gint currently_visible_toolbars = 0;
490
491     if (!priv->borders)
492     {
493         hildon_window_get_borders (HILDON_WINDOW (widget));
494         b = HILDON_WINDOW(widget)->priv->borders;
495         tb = HILDON_WINDOW(widget)->priv->toolbar_borders;
496     }
497
498     tb_height = bx->allocation.height + tb->top + tb->bottom;
499
500     g_list_foreach (box->children, visible_toolbars, 
501             &currently_visible_toolbars);
502
503     paint_toolbar (widget, box,
504                    event, priv->fullscreen);
505
506     if (!HILDON_WINDOW (widget)->priv->fullscreen)
507     {
508
509         /* Draw the left and right window border */
510         gint side_borders_height = widget->allocation.height - b->top;
511
512         if (currently_visible_toolbars)
513             side_borders_height -= tb_height;
514         else
515             side_borders_height -= b->bottom;
516         
517         if (b->left > 0)
518         {
519             gtk_paint_box (widget->style, widget->window,
520                     GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
521                     &event->area, widget, "left-border",
522                     widget->allocation.x, widget->allocation.y +
523                     b->top, b->left, side_borders_height);
524         } 
525
526         if (b->right > 0)
527         {
528             gtk_paint_box (widget->style, widget->window,
529                     GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
530                     &event->area, widget, "right-border",
531                     widget->allocation.x + widget->allocation.width -
532                     b->right, widget->allocation.y + b->top,
533                     b->right, side_borders_height);
534         }
535
536         /* If no toolbar, draw the bottom window border */
537         if (!currently_visible_toolbars && b->bottom > 0)
538         {
539             gtk_paint_box (widget->style, widget->window,
540                     GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
541                     &event->area, widget, "bottom-border",
542                     widget->allocation.x, widget->allocation.y +
543                     (widget->allocation.height - b->bottom),
544                     widget->allocation.width, b->bottom);
545         }
546
547         /* Draw the top border */
548         if (b->top > 0)
549         {
550             gtk_paint_box (widget->style, widget->window,
551                     GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
552                     &event->area, widget, "top-border",
553                     widget->allocation.x, widget->allocation.y,
554                     widget->allocation.width, b->top);
555         } 
556
557
558     }
559
560     /* don't draw the window stuff as it overwrites our borders with a blank
561      * rectangle. Instead start with the drawing of the GtkBin */
562     GTK_WIDGET_CLASS (g_type_class_peek_parent (parent_class))->
563         expose_event (widget, event);
564     /*GTK_WIDGET_CLASS (parent_class))->
565         expose_event (widget, event);*/
566
567     return FALSE;
568
569 }
570
571 static void
572 hildon_window_size_request (GtkWidget * widget, GtkRequisition * requisition)
573 {
574     HildonWindowPrivate *priv = HILDON_WINDOW (widget)->priv;
575     GtkWidget *child = GTK_BIN (widget)->child;
576     GtkRequisition req2;
577     gint border_width = GTK_CONTAINER(widget)->border_width;
578    
579     if (!priv->borders)
580     {
581         hildon_window_get_borders (HILDON_WINDOW (widget));
582     }
583     
584     if (child)
585         gtk_widget_size_request (child, requisition);
586
587     if (HILDON_WINDOW (widget)->priv->vbox != NULL)
588         gtk_widget_size_request (HILDON_WINDOW (widget)->priv->vbox,
589                 &req2);
590
591     requisition->height += req2.height;
592     requisition->width = (requisition->width < req2.width) ? 
593         req2.width : requisition->width;
594
595
596     requisition->width  += 2 * border_width;
597     requisition->height += 2 * border_width;
598     
599     if (!priv->fullscreen)
600     {
601         requisition->height += priv->borders->top;
602         if (req2.height == 0)
603             requisition->height += priv->borders->bottom;
604         requisition->width += priv->borders->left + priv->borders->right;
605     }
606 }
607
608 static void
609 hildon_window_size_allocate (GtkWidget * widget, GtkAllocation * allocation)
610 {
611     HildonWindowPrivate *priv = HILDON_WINDOW (widget)->priv;
612     GtkAllocation box_alloc;
613     GtkAllocation alloc = *allocation;
614     GtkRequisition req;
615     gint border_width = GTK_CONTAINER(widget)->border_width;
616
617     GtkWidget *box = HILDON_WINDOW(widget)->priv->vbox;
618     GtkBin *bin = GTK_BIN(widget);
619     GtkBorder *b = HILDON_WINDOW (widget)->priv->borders;
620     GtkBorder *tb = HILDON_WINDOW (widget)->priv->toolbar_borders;
621     
622     if (!priv->borders)
623     {
624         hildon_window_get_borders (HILDON_WINDOW (widget));
625         b = HILDON_WINDOW (widget)->priv->borders;
626         tb = HILDON_WINDOW (widget)->priv->toolbar_borders;
627     }
628     
629     widget->allocation = *allocation;
630
631     gtk_widget_get_child_requisition (box, &req);
632
633     box_alloc.width = allocation->width - tb->left - tb->right;
634     box_alloc.height = ( (req.height < allocation->height) ?
635         req.height : allocation->height );
636     box_alloc.x = allocation->x + tb->left;
637     box_alloc.y = allocation->y + allocation->height - box_alloc.height - tb->bottom;
638
639     if (bin->child != NULL && GTK_IS_WIDGET (bin->child)
640                            && GTK_WIDGET_VISIBLE (bin->child))
641     {
642         alloc.x += border_width;
643         alloc.y += border_width;
644         alloc.width -= (border_width * 2);
645         alloc.height -= (border_width * 2) + box_alloc.height;
646
647         if (!(HILDON_WINDOW (widget)->priv->fullscreen))
648         {
649             alloc.x += b->left;
650             alloc.width -= (b->left + b->right);
651             alloc.y += b->top;
652
653             alloc.height -= b->top;
654
655             if (box_alloc.height <= 0)
656                 alloc.height -= b->bottom;
657             else
658                 alloc.height -= (tb->top + tb->bottom);            
659         }
660         else
661         {
662             if (!(box_alloc.height <= 0))
663                 alloc.height -= (tb->top + tb->bottom);              
664         }
665
666         gtk_widget_size_allocate (bin->child, &alloc);
667     }
668
669
670     gtk_widget_size_allocate (box, &box_alloc);
671
672     if (priv->previous_vbox_y != box_alloc.y)
673     {
674         /* The size of the VBox has changed, we need to redraw part
675          * of the window borders */
676         gint draw_from_y = priv->previous_vbox_y < box_alloc.y?
677             priv->previous_vbox_y - tb->top:
678             box_alloc.y - tb->top;
679         
680         gtk_widget_queue_draw_area (widget, 0, draw_from_y, 
681                 widget->allocation.width,
682                 widget->allocation.height - draw_from_y);
683         
684         priv->previous_vbox_y = box_alloc.y;
685     }
686
687 }
688
689 static void
690 hildon_window_forall (GtkContainer * container, gboolean include_internals,
691                       GtkCallback callback,     gpointer callback_data)
692 {
693     HildonWindow *self = HILDON_WINDOW (container);
694
695     g_return_if_fail (callback != NULL);
696
697     GTK_CONTAINER_CLASS (parent_class)->forall (container, include_internals,
698                                                 callback, callback_data);
699     if (include_internals && self->priv->vbox != NULL)
700             (* callback)(GTK_WIDGET (self->priv->vbox), callback_data);
701 }
702
703 static void
704 hildon_window_show_all (GtkWidget *widget)
705 {
706     HildonWindow *self = HILDON_WINDOW (widget);
707     
708     GTK_WIDGET_CLASS (parent_class)->show_all (widget);
709     gtk_widget_show_all (self->priv->vbox);
710
711 }
712
713 static void
714 hildon_window_destroy (GtkObject *obj)
715 {
716     HildonWindow *self = HILDON_WINDOW (obj);
717     GList *menu_list;
718
719     if (self->priv->vbox != NULL)
720     {
721         if (self->priv->program)
722         {
723             GtkWidget * common_toolbar = GTK_WIDGET (
724                     hildon_program_get_common_toolbar (self->priv->program));
725             if (common_toolbar && common_toolbar->parent == self->priv->vbox)
726             {
727                 gtk_container_remove (GTK_CONTAINER (self->priv->vbox),
728                         common_toolbar);
729             }
730         }
731         
732         gtk_widget_unparent (self->priv->vbox);
733         self->priv->vbox = NULL;        
734         
735     }
736
737     menu_list = g_list_copy (gtk_menu_get_for_attach_widget (GTK_WIDGET (obj)));
738
739     while (menu_list)
740     {
741         if (GTK_IS_MENU(menu_list->data))
742         {
743             if (GTK_WIDGET_VISIBLE (GTK_WIDGET (menu_list->data)))
744             {
745                 gtk_menu_popdown (GTK_MENU (menu_list->data));
746                 gtk_menu_shell_deactivate (GTK_MENU_SHELL (menu_list->data));
747             }
748             gtk_menu_detach (GTK_MENU (menu_list->data));
749         }
750         menu_list = menu_list->next;
751     }
752
753     g_list_free (menu_list);
754     
755     if (self->priv->program)
756     {
757         hildon_program_remove_window (self->priv->program, self);
758     }
759     
760     gdk_window_remove_filter (gdk_get_default_root_window(), 
761                               hildon_window_root_window_event_filter,
762                               obj);
763     
764     gtk_widget_set_events (GTK_WIDGET(obj), 0);
765
766     GTK_OBJECT_CLASS (parent_class)->destroy (obj);
767 }
768
769
770 static void
771 hildon_window_notify (GObject *gobject, GParamSpec *param)
772 {
773     HildonWindow *window = HILDON_WINDOW (gobject);
774     
775     if (strcmp (param->name, "title") == 0)
776     {
777
778         hildon_window_update_title (window);
779     }
780     else if (strcmp (param->name, "is-topmost"))
781     {
782         hildon_window_is_topmost_notify (window);
783     }
784
785     if (G_OBJECT_CLASS(parent_class)->notify)
786         G_OBJECT_CLASS(parent_class)->notify (gobject, param);
787 }
788
789 /* Utilities */
790
791 static void
792 visible_toolbar (gpointer data, gpointer user_data)
793 {
794     if (GTK_WIDGET_VISIBLE (((GtkBoxChild *)data)->widget))
795         (*((gint *)user_data))++;
796 }
797
798 static void 
799 find_findtoolbar_index (gpointer data, gpointer user_data)
800 {
801     gint *pass_bundle = (gint *)user_data;
802
803     if(((GtkBoxChild *)data)->widget->allocation.y < pass_bundle[0]
804             && GTK_WIDGET_VISIBLE (((GtkBoxChild *)data)->widget))
805         pass_bundle[1]++;
806 }
807
808 static void
809 find_findtoolbar (gpointer data, gpointer user_data)
810 {
811     if(HILDON_IS_FIND_TOOLBAR (((GtkBoxChild *)data)->widget)
812             && GTK_WIDGET_VISIBLE (((GtkBoxChild *)data)->widget))
813         (*((GtkWidget **)user_data)) = ((GtkBoxChild *)data)->widget;
814 }
815
816 static void
817 paint_toolbar (GtkWidget *widget, GtkBox *box, 
818                        GdkEventExpose * event, 
819                            gboolean fullscreen)
820 {
821     gint toolbar_num = 0; 
822     gint ftb_index = 0;
823     gint count;
824     GtkWidget *findtoolbar = NULL;
825     gchar toolbar_mode[40];
826     GtkBorder *tb = HILDON_WINDOW (widget)->priv->toolbar_borders;
827
828     /* collect info to help on painting the boxes */
829     g_list_foreach (box->children, visible_toolbar, 
830                             (gpointer) &toolbar_num);
831     
832     if(toolbar_num <= 0)
833         return;
834     
835     g_list_foreach (box->children, find_findtoolbar, (gpointer) &findtoolbar);
836     
837     if (findtoolbar != NULL)
838     {
839         gint pass_bundle[2];/* an array for convient data passing
840                                the first member contains the y allocation
841                                of the find toolbar, and the second allocation
842                                contains the index(how many toolbars are above
843                                find toolbar) */
844         pass_bundle[0] = findtoolbar->allocation.y;
845         pass_bundle[1] = ftb_index;
846         g_list_foreach(box->children, find_findtoolbar_index,
847                 (gpointer) pass_bundle);
848         ftb_index = pass_bundle[1];
849     }
850     
851     /*upper border*/
852     sprintf (toolbar_mode, "toolbar%sframe-top", 
853              fullscreen ? "-fullscreen-" : "-");
854     gtk_paint_box (widget->style, widget->window,
855                     GTK_WIDGET_STATE (widget), GTK_SHADOW_OUT,
856                     &event->area, widget, toolbar_mode,
857                     widget->allocation.x,
858                     GTK_WIDGET (box)->allocation.y - tb->top,
859                     widget->allocation.width, tb->top);
860     
861     /*top most toolbar painting*/
862     if (findtoolbar != NULL && ftb_index == 0 )
863     {
864         sprintf (toolbar_mode, "findtoolbar%s", 
865                  fullscreen ? "-fullscreen" : "");
866
867         gtk_paint_box (widget->style, widget->window,
868                 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
869                 &event->area, widget, toolbar_mode,
870                 widget->allocation.x,
871                 GTK_WIDGET(box)->allocation.y,
872                 widget->allocation.width,
873                 TOOLBAR_HEIGHT);
874     }
875     else
876     {
877         sprintf (toolbar_mode, "toolbar%s", 
878                 fullscreen ? "-fullscreen" : "");
879             
880         gtk_paint_box (widget->style, widget->window,
881                 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
882                 &event->area, widget, toolbar_mode,
883                 widget->allocation.x,
884                 GTK_WIDGET(box)->allocation.y,
885                 widget->allocation.width,
886                 TOOLBAR_HEIGHT);
887     }
888     /*multi toolbar painting*/
889     for (count = 0; count < toolbar_num - 1; count++)
890     {
891         sprintf (toolbar_mode, "toolbar%sframe-middle", 
892                  fullscreen ? "-fullscreen-" : "-");
893
894         gtk_paint_box (widget->style, widget->window,
895                 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
896                 &event->area, widget, toolbar_mode,
897                 widget->allocation.x,
898                 GTK_WIDGET(box)->allocation.y + 
899                 (1 + count) * TOOLBAR_HEIGHT + 
900                 count * TOOLBAR_MIDDLE,
901                 widget->allocation.width,
902                 TOOLBAR_MIDDLE);
903
904         if (findtoolbar != NULL && count + 1 == ftb_index)
905         {
906             
907             sprintf (toolbar_mode, "findtoolbar%s", 
908                      fullscreen ? "-fullscreen" : "");
909
910             gtk_paint_box (widget->style, widget->window,
911                     GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
912                     &event->area, widget, toolbar_mode,
913                     widget->allocation.x,
914                     GTK_WIDGET(box)->allocation.y + 
915                     (1 + count) * (TOOLBAR_HEIGHT + TOOLBAR_MIDDLE),
916                     widget->allocation.width,
917                     TOOLBAR_HEIGHT);
918         }
919         else
920         {
921             sprintf (toolbar_mode, "toolbar%s", 
922                      fullscreen ? "-fullscreen" : "");
923
924             gtk_paint_box (widget->style, widget->window,
925                     GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
926                     &event->area, widget, toolbar_mode,
927                     widget->allocation.x,
928                     GTK_WIDGET(box)->allocation.y + 
929                     (1 + count) * (TOOLBAR_HEIGHT + TOOLBAR_MIDDLE),
930                     widget->allocation.width,
931                     TOOLBAR_HEIGHT);
932         }
933     }
934     sprintf (toolbar_mode, "toolbar%sframe-bottom", 
935             fullscreen ? "-fullscreen-" : "-");
936
937     gtk_paint_box (widget->style, widget->window,
938             GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
939             &event->area, widget, toolbar_mode,
940             widget->allocation.x,
941             GTK_WIDGET(box)->allocation.y + 
942             GTK_WIDGET(box)->allocation.height,
943             widget->allocation.width, tb->bottom);
944
945 }
946
947 /*
948  * Checks the root window to know which is the topped window
949  */
950 Window
951 hildon_window_get_active_window (void)
952 {
953     Atom realtype;
954     int format;
955     int status;
956     Window ret;
957     unsigned long n;
958     unsigned long extra;
959     union
960     {
961         Window *win;
962         unsigned char *char_pointer;
963     } win;
964     Atom active_app_atom = 
965             XInternAtom (GDK_DISPLAY (), "_MB_CURRENT_APP_WINDOW", False);
966
967     win.win = NULL;
968
969     status = XGetWindowProperty (GDK_DISPLAY(), GDK_ROOT_WINDOW(),
970             active_app_atom, 0L, 16L,
971             0, XA_WINDOW, &realtype, &format,
972             &n, &extra, &win.char_pointer);
973     if (!(status == Success && realtype == XA_WINDOW && format == 32
974                 && n == 1 && win.win != NULL))
975     {
976         if (win.win != NULL)
977             XFree (win.char_pointer);
978         return None;
979     }
980
981     ret = win.win[0];
982
983     if (win.win != NULL)
984         XFree(win.char_pointer);
985
986     return ret;
987 }
988
989 static int
990 xclient_message_type_check (XClientMessageEvent *cm, const gchar *name)
991 {
992     return cm->message_type == XInternAtom(GDK_DISPLAY(), name, FALSE);
993 }
994
995 /*****************/
996 /* Event filters */
997 /*****************/
998
999 /*
1000  * Handle the window border custom button, which toggles the menu,
1001  * and the Hildon input method copy paste messages
1002  */
1003 static GdkFilterReturn
1004 hildon_window_event_filter (GdkXEvent *xevent, GdkEvent *event, gpointer data)
1005 {
1006     XAnyEvent *eventti = xevent;
1007
1008     if (eventti->type == ClientMessage)
1009     {
1010         XClientMessageEvent *cm = xevent;
1011
1012         if (xclient_message_type_check (cm, "_MB_GRAB_TRANSFER"))
1013         {
1014             hildon_window_toggle_menu (HILDON_WINDOW ( data ));
1015             return GDK_FILTER_REMOVE;
1016         }
1017         /* opera hack clipboard client message */
1018         else if (xclient_message_type_check (cm, "_HILDON_IM_CLIPBOARD_COPY"))
1019         {
1020             g_signal_emit_by_name(G_OBJECT(data), "clipboard_operation",
1021                                   HILDON_WINDOW_CO_COPY);
1022             return GDK_FILTER_REMOVE;
1023         }
1024         else if (xclient_message_type_check(cm, "_HILDON_IM_CLIPBOARD_CUT"))
1025         {
1026             g_signal_emit_by_name(G_OBJECT(data), "clipboard_operation",
1027                                   HILDON_WINDOW_CO_CUT);
1028             return GDK_FILTER_REMOVE;
1029         }
1030         else if (xclient_message_type_check(cm, "_HILDON_IM_CLIPBOARD_PASTE"))
1031         {
1032             g_signal_emit_by_name(G_OBJECT(data), "clipboard_operation",
1033                                   HILDON_WINDOW_CO_PASTE);
1034             return GDK_FILTER_REMOVE;
1035         }
1036     }
1037
1038     return GDK_FILTER_CONTINUE;
1039 }
1040
1041 /*
1042  * Here we keep track of changes in the _MB_CURRENT_APP_WINDOW,
1043  * to know when we acquire/lose topmost status
1044  */
1045 static GdkFilterReturn
1046 hildon_window_root_window_event_filter (GdkXEvent *xevent, 
1047                                         GdkEvent *event, 
1048                                         gpointer data)
1049 {
1050     XAnyEvent *eventti = xevent;
1051     HildonWindow *hwindow = HILDON_WINDOW (data);
1052
1053
1054     if (eventti->type == PropertyNotify)
1055     {
1056         XPropertyEvent *pevent = xevent;
1057         Atom active_app_atom = 
1058             XInternAtom (GDK_DISPLAY (), "_MB_CURRENT_APP_WINDOW", False);
1059
1060         if (pevent->atom == active_app_atom)
1061         {
1062             Window active_window = hildon_window_get_active_window();
1063
1064             hildon_window_update_topmost (hwindow, active_window);
1065         }
1066     }
1067
1068     return GDK_FILTER_CONTINUE;
1069 }
1070
1071 /***************************/
1072 /*   Signal handlers       */
1073 /***************************/
1074
1075 /*
1076  * Handle the menu hardware key here
1077  */
1078 static gboolean
1079 hildon_window_key_press_event (GtkWidget *widget, GdkEventKey *event)
1080 {
1081     HildonWindowPrivate *priv;
1082
1083     g_return_val_if_fail (HILDON_IS_WINDOW (widget),FALSE);
1084
1085     priv = HILDON_WINDOW (widget)->priv;
1086
1087     switch (event->keyval)
1088     {
1089         case HILDON_HARDKEY_MENU:
1090             if (hildon_window_toggle_menu (HILDON_WINDOW (widget)))
1091                 return TRUE;
1092             break;
1093         case HILDON_HARDKEY_ESC:
1094             if (!priv->escape_timeout)
1095             {
1096                 priv->escape_timeout = g_timeout_add 
1097                     (HILDON_WINDOW_LONG_PRESS_TIME,
1098                      hildon_window_escape_timeout, widget);
1099             }
1100             break;
1101     }
1102
1103     return GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event);
1104
1105 }
1106
1107 static gboolean
1108 hildon_window_key_release_event (GtkWidget *widget, GdkEventKey *event)
1109 {
1110     HildonWindowPrivate *priv;
1111
1112     g_return_val_if_fail (HILDON_IS_WINDOW (widget),FALSE);
1113
1114     priv = HILDON_WINDOW (widget)->priv;
1115
1116     switch (event->keyval)
1117     {
1118         case HILDON_HARDKEY_ESC:
1119             if (priv->escape_timeout)
1120             {
1121                 g_source_remove (priv->escape_timeout);
1122                 priv->escape_timeout = 0;
1123             }
1124             break;
1125     }
1126
1127     return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
1128
1129 }
1130
1131 /*
1132  * We keep track of the window state changes, because the drawing
1133  * (borders) differs whether we are in fullscreen mode or not
1134  */
1135 static gboolean
1136 hildon_window_window_state_event (GtkWidget *widget, 
1137                                   GdkEventWindowState *event)
1138 {
1139     if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
1140     {
1141         HILDON_WINDOW (widget)->priv->fullscreen = 
1142             event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN;
1143     }
1144
1145     if (GTK_WIDGET_CLASS (parent_class)->window_state_event)
1146     {
1147         return GTK_WIDGET_CLASS (parent_class)->window_state_event (
1148                 widget,
1149                 event);
1150     }
1151     else
1152     {
1153         return FALSE;
1154     }
1155 }
1156
1157 static void 
1158 hildon_window_title_notify (GObject *gobject,
1159                             GParamSpec *arg1,
1160                             gpointer user_data)
1161 {
1162     HildonWindow *window = HILDON_WINDOW (gobject);
1163
1164     hildon_window_update_title (window);
1165
1166 }
1167
1168 /*******************/
1169 /*     General     */
1170 /*******************/
1171
1172 /*The menu popuping needs a menu popup-function*/
1173 static void
1174 hildon_window_menupopupfunc (GtkMenu *menu, gint *x, gint *y,
1175                              gboolean *push_in, GtkWidget *widget)
1176 {
1177     gint window_x = 0;
1178     gint window_y = 0;
1179     GdkWindow *window = GTK_WIDGET(widget)->window;
1180     
1181     if (window)
1182     {
1183         gdk_window_get_origin (window, &window_x, &window_y);
1184     }
1185
1186     gtk_widget_style_get (GTK_WIDGET (menu), "horizontal-offset", x,
1187             "vertical-offset", y, NULL);
1188
1189     *x += window_x;
1190     *y += window_y;
1191   
1192 }
1193
1194 static void
1195 hildon_window_menupopupfuncfull ( GtkMenu *menu, gint *x, gint *y,
1196                                               gboolean *push_in, 
1197                                               GtkWidget *widget )
1198 {
1199     gtk_widget_style_get (GTK_WIDGET (menu), "horizontal-offset", x,
1200             "vertical-offset", y, NULL);
1201
1202     *x = MAX (0, *x);
1203     *y = MAX (0, *y);
1204 }
1205
1206
1207 /********************/
1208 /* Private methods  */
1209 /********************/
1210
1211
1212 /*
1213  * Takes the common toolbar when we acquire the top-most status
1214  */
1215 static void
1216 hildon_window_is_topmost_notify (HildonWindow *window)
1217 {
1218     if (window->priv->is_topmost)
1219     {
1220         hildon_window_take_common_toolbar (window);
1221     }
1222
1223     else
1224     {
1225         /* If the window lost focus while the user started to press
1226          * the ESC key, we won't get the release event. We need to
1227          * stop the timeout*/
1228         if (window->priv->escape_timeout)
1229         {
1230             g_source_remove (window->priv->escape_timeout);
1231             window->priv->escape_timeout = 0;
1232         }
1233     }
1234 }
1235
1236 /*
1237  * Sets the program to which the window belongs. This should only be called
1238  * by hildon_program_add_window
1239  */
1240 void
1241 hildon_window_set_program (HildonWindow *self, GObject *program)
1242 {
1243     if (self->priv->program)
1244     {
1245         g_object_unref (self->priv->program);
1246     }
1247
1248     /* Now that we are bound to a program, we can rely on it to track the
1249      * root window */
1250     gdk_window_remove_filter (gdk_get_default_root_window(), 
1251                               hildon_window_root_window_event_filter,
1252                               self);
1253
1254     self->priv->program = HILDON_PROGRAM (program);
1255     g_object_ref (program);
1256 }
1257
1258 /*
1259  * Unsets the program to which the window belongs. This should only be called
1260  * by hildon_program_add_window
1261  */
1262 void
1263 hildon_window_unset_program (HildonWindow *self)
1264 {
1265     g_return_if_fail(self && HILDON_IS_WINDOW (self));
1266
1267     if (self->priv->program)
1268     {
1269         g_object_unref (self->priv->program);
1270         self->priv->program = NULL;
1271
1272         /* We need to start tacking the root window again */
1273         gdk_window_set_events (gdk_get_default_root_window (),
1274                 gdk_window_get_events (gdk_get_default_root_window ())
1275                 | GDK_PROPERTY_CHANGE_MASK);
1276
1277         gdk_window_add_filter (gdk_get_default_root_window (),
1278                 hildon_window_root_window_event_filter, self );
1279     }
1280
1281     self->priv->program = NULL;
1282 }
1283
1284 /*
1285  * Sets whether or not the program to which this window belongs is
1286  * killable. This is used by the HildonProgram to signify to the
1287  * Task Navigator whether or not it can hibernate in memory-low situations
1288  **/    
1289 void
1290 hildon_window_set_can_hibernate_property (HildonWindow *self, 
1291                                           gpointer _can_hibernate)
1292 {
1293     GdkAtom killable_atom;
1294     gboolean can_hibernate;
1295
1296     g_return_if_fail(self && HILDON_IS_WINDOW (self));
1297         
1298     if (!GTK_WIDGET_REALIZED ((GTK_WIDGET (self))))
1299     {
1300         return;
1301     }
1302
1303     can_hibernate = * ((gboolean *)_can_hibernate);
1304
1305     killable_atom = gdk_atom_intern (CAN_HIBERNATE_PROPERTY, FALSE);
1306
1307     if (can_hibernate)
1308     {
1309         gdk_property_change (GTK_WIDGET (self)->window, killable_atom,
1310                 (GdkAtom)31/* XA_STRING */, 8,
1311                 GDK_PROP_MODE_REPLACE, (const guchar *)CAN_HIBERNATE,
1312                 CAN_HIBERNATE_LENGTH);
1313     }
1314     else
1315     {
1316         gdk_property_delete (GTK_WIDGET (self)->window, killable_atom);
1317     }
1318         
1319 }
1320
1321 /*
1322  * If a common toolbar was set to the program, reparent it to
1323  * us
1324  */
1325 void
1326 hildon_window_take_common_toolbar (HildonWindow *self)
1327 {
1328     g_return_if_fail(self && HILDON_IS_WINDOW (self));
1329
1330     if (self->priv->program)
1331     {
1332         GtkWidget *common_toolbar =  
1333            GTK_WIDGET (hildon_program_get_common_toolbar (self->priv->program));
1334
1335         if (common_toolbar && common_toolbar->parent != self->priv->vbox)
1336         {
1337             g_object_ref (common_toolbar);
1338             if (common_toolbar->parent)
1339             {
1340                 gtk_container_remove (GTK_CONTAINER (common_toolbar->parent),
1341                         common_toolbar);
1342             }
1343
1344             gtk_box_pack_end (GTK_BOX(self->priv->vbox), common_toolbar,
1345                     TRUE, TRUE, 0);
1346             g_object_unref (common_toolbar);
1347
1348             gtk_widget_set_size_request (common_toolbar, -1, TOOLBAR_HEIGHT);
1349
1350             gtk_widget_show  (self->priv->vbox);
1351
1352         }
1353     }
1354 }
1355
1356 /*
1357  * Compare the window that was last topped, and act consequently
1358  */
1359 void
1360 hildon_window_update_topmost (HildonWindow *self, Window window_id)
1361 {
1362     Window my_window;
1363     
1364     my_window = GDK_WINDOW_XID (GTK_WIDGET (self)->window);
1365
1366     if (window_id == my_window)
1367     {
1368         if (!self->priv->is_topmost)
1369         {
1370             self->priv->is_topmost = TRUE;
1371             hildon_window_is_topmost_notify (self);
1372             g_object_notify (G_OBJECT (self), "is-topmost");
1373         }
1374     }
1375     else if (self->priv->is_topmost)
1376     {
1377         /* Should this go in the signal handler? */
1378         GtkWidget *focus = gtk_window_get_focus (GTK_WINDOW (self));
1379
1380         if (GTK_IS_ENTRY (focus))
1381             gtk_im_context_focus_out (GTK_ENTRY (focus)->im_context);
1382         if (GTK_IS_TEXT_VIEW (focus))
1383             gtk_im_context_focus_out (GTK_TEXT_VIEW (focus)->im_context);
1384             
1385         self->priv->is_topmost = FALSE;
1386         hildon_window_is_topmost_notify (self);
1387         g_object_notify (G_OBJECT (self), "is-topmost");
1388
1389     }
1390 }
1391     
1392 /*
1393  * If the application
1394  * was given a name (with g_set_application_name(), set 
1395  * "ProgramName - WindowTitle" as the displayed
1396  * title
1397  */
1398 void
1399 hildon_window_update_title (HildonWindow *window)
1400 {
1401     const gchar * application_name;
1402     g_return_if_fail (window && HILDON_IS_WINDOW (window));
1403
1404     if (!GTK_WIDGET_REALIZED (window))
1405     {
1406         return;
1407     }
1408
1409     application_name = g_get_application_name ();
1410
1411     if (application_name && application_name[0])
1412     {
1413         const gchar *old_title = gtk_window_get_title (GTK_WINDOW (window));
1414
1415         if (old_title && old_title[0])
1416         {
1417             gchar *title = NULL;
1418             
1419             title = g_strjoin (TITLE_SEPARATOR, application_name,
1420                     old_title, NULL);
1421            
1422             gdk_window_set_title (GTK_WIDGET (window)->window, title);
1423             
1424             g_free (title);
1425         }
1426
1427     }
1428 }
1429
1430 static void
1431 detach_menu_func (GtkWidget *attach_widget, GtkMenu *menu)
1432 {
1433 }
1434 /*
1435  * Toggles the display of the HildonWindow menu.
1436  * Returns whether or not something was done (whether or not we had a menu
1437  * to toggle)
1438  */
1439 static gboolean
1440 hildon_window_toggle_menu (HildonWindow * self)
1441 {
1442     GtkMenu *menu_to_use = NULL;
1443     GList *menu_children = NULL;
1444     
1445     g_return_val_if_fail (self && HILDON_IS_WINDOW (self), FALSE);
1446
1447     /* Select which menu to use, Window specific has highest priority,
1448      * then program specific */
1449     if (self->priv->menu)
1450     {
1451         menu_to_use = GTK_MENU (self->priv->menu);
1452     }
1453     else if (self->priv->program)
1454     {
1455         menu_to_use = hildon_program_get_common_menu (self->priv->program);
1456         if (menu_to_use && gtk_menu_get_attach_widget (menu_to_use) != 
1457                 GTK_WIDGET (self))
1458         {
1459             g_object_ref (menu_to_use);
1460             if (gtk_menu_get_attach_widget (menu_to_use))
1461             {
1462                 gtk_menu_detach (menu_to_use);
1463             }
1464
1465             gtk_menu_attach_to_widget (menu_to_use, GTK_WIDGET (self), 
1466                     &detach_menu_func);
1467             g_object_unref (menu_to_use);
1468         }
1469     }
1470
1471     if (!menu_to_use)
1472     {
1473         return FALSE;
1474     }
1475     
1476
1477     if (GTK_WIDGET_MAPPED (GTK_WIDGET (menu_to_use)))
1478     {
1479         gtk_menu_popdown (menu_to_use);
1480         gtk_menu_shell_deactivate (GTK_MENU_SHELL (menu_to_use));
1481         return TRUE;
1482     }
1483
1484     /* Check if the menu has items */
1485     menu_children = gtk_container_get_children (GTK_CONTAINER (menu_to_use));
1486
1487     if (menu_children)
1488     {
1489         g_list_free (menu_children);
1490
1491         /* Apply right theming */
1492         gtk_widget_set_name (GTK_WIDGET (menu_to_use),
1493                 "menu_force_with_corners");
1494         
1495         if (self->priv->fullscreen) 
1496         {
1497             gtk_menu_popup (menu_to_use, NULL, NULL,
1498                            (GtkMenuPositionFunc)
1499                            hildon_window_menupopupfuncfull,
1500                            self, 0, 
1501                            gtk_get_current_event_time ());
1502         }
1503         else
1504         {
1505             gtk_menu_popup (menu_to_use, NULL, NULL,
1506                            (GtkMenuPositionFunc)
1507                            hildon_window_menupopupfunc,
1508                            self, 0, 
1509                            gtk_get_current_event_time ());
1510         }
1511         gtk_menu_shell_select_first (GTK_MENU_SHELL (menu_to_use), TRUE);
1512         return TRUE;
1513     }
1514
1515     return FALSE;
1516
1517 }
1518
1519 /*
1520  * If the ESC key was not released when the timeout expires,
1521  * close the window
1522  */
1523 static gboolean
1524 hildon_window_escape_timeout (gpointer data)
1525 {
1526     HildonWindowPrivate *priv;
1527     GdkEvent *event;
1528
1529     GDK_THREADS_ENTER ();
1530     
1531     priv = HILDON_WINDOW(data)->priv;
1532
1533     /* Send fake event, simulation a situation that user
1534        pressed 'x' from the corner */
1535     event = gdk_event_new(GDK_DELETE);
1536     ((GdkEventAny *)event)->window = GDK_WINDOW (g_object_ref (GTK_WIDGET(data)->window));
1537     gtk_main_do_event(event);
1538
1539     /* That unrefs the window, so we're reffing it above */
1540     gdk_event_free(event);
1541
1542     priv->escape_timeout = 0;
1543
1544     GDK_THREADS_LEAVE ();
1545     
1546     return FALSE;
1547 }
1548
1549
1550 /******************/
1551 /* public methods */
1552 /******************/
1553
1554
1555 /**
1556  * hildon_window_new: 
1557  * 
1558  * Use this function to create a new HildonWindow.
1559  * 
1560  * Return value: A @HildonWindow.
1561  **/
1562 GtkWidget *
1563 hildon_window_new (void)
1564 {
1565     HildonWindow *newwindow = g_object_new (HILDON_TYPE_WINDOW, NULL);
1566
1567     return GTK_WIDGET (newwindow);
1568 }
1569
1570 /**
1571  * hildon_window_add_with_scrollbar
1572  * @self : A @HildonWindow
1573  * @child : A @GtkWidget
1574  *
1575  * Adds the @child to the HildonWindow and creates a scrollbar
1576  * to it. Similar as adding first a @GtkScrolledWindow and then the
1577  * @child to it.
1578  */
1579 void
1580 hildon_window_add_with_scrollbar (HildonWindow * self,
1581                                   GtkWidget * child)
1582 {
1583     GtkScrolledWindow *scrolledw;
1584
1585     g_return_if_fail (HILDON_IS_WINDOW (self));
1586     g_return_if_fail (GTK_IS_WIDGET (child));
1587     g_return_if_fail (child->parent == NULL);
1588
1589     scrolledw = GTK_SCROLLED_WINDOW (gtk_scrolled_window_new (NULL, NULL));
1590     gtk_scrolled_window_set_policy (scrolledw, GTK_POLICY_NEVER,
1591                                     GTK_POLICY_AUTOMATIC);
1592     gtk_scrolled_window_set_shadow_type (scrolledw, GTK_SHADOW_NONE);
1593
1594     if (GTK_IS_VIEWPORT (child))
1595         gtk_container_add (GTK_CONTAINER (scrolledw), child);
1596     else
1597     {
1598         if (GTK_IS_CONTAINER (child) )
1599             gtk_container_set_focus_vadjustment (GTK_CONTAINER(child),
1600                     gtk_scrolled_window_get_vadjustment (scrolledw) );
1601         gtk_scrolled_window_add_with_viewport (scrolledw, child);
1602     }
1603
1604     gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (scrolledw));
1605 }
1606
1607 /**
1608  * hildon_window_add_toolbar:
1609  * @self: A @HildonWindow
1610  * @toolbar: A #GtkToolbar to add to the HildonWindow
1611  *
1612  * Adds a toolbar to the window.
1613  **/
1614 void 
1615 hildon_window_add_toolbar (HildonWindow *self, GtkToolbar *toolbar)
1616 {
1617     GtkBox *vbox;
1618     
1619     g_return_if_fail (self && HILDON_IS_WINDOW (self));
1620     g_return_if_fail (toolbar && GTK_IS_TOOLBAR (toolbar));
1621
1622     vbox = GTK_BOX (self->priv->vbox);
1623
1624     gtk_box_pack_start (vbox, GTK_WIDGET(toolbar), TRUE, TRUE, 0);
1625     gtk_box_reorder_child (vbox, GTK_WIDGET(toolbar), 0);
1626     gtk_widget_set_size_request (GTK_WIDGET (toolbar), -1, TOOLBAR_HEIGHT);
1627
1628     gtk_widget_queue_resize (GTK_WIDGET(self));
1629 }
1630
1631 /**
1632  * hildon_window_remove_toolbar:
1633  * @self: A @HildonWindow
1634  * @toolbar: A #GtkToolbar to remove from the HildonWindow
1635  *
1636  * Removes a toolbar from the window.
1637  **/
1638 void
1639 hildon_window_remove_toolbar (HildonWindow *self, GtkToolbar *toolbar)
1640 {
1641     GtkContainer *vbox = GTK_CONTAINER (self->priv->vbox);
1642     
1643     g_return_if_fail (self && HILDON_IS_WINDOW (self));
1644
1645     gtk_container_remove (vbox, GTK_WIDGET(toolbar));
1646 }
1647
1648 /**
1649  * hildon_window_get_menu:
1650  * @self : #HildonWindow
1651  * 
1652  * Gets the #GtMenu assigned to the #HildonAppview.
1653  * 
1654  * Return value: The #GtkMenu assigned to this application view.
1655  **/
1656 GtkMenu *
1657 hildon_window_get_menu (HildonWindow * self)
1658 {
1659     g_return_val_if_fail (self && HILDON_IS_WINDOW (self), NULL);
1660
1661     return GTK_MENU (self->priv->menu);
1662 }
1663
1664
1665 /**
1666  * hildon_window_set_menu:
1667  * @self: A #HildonWindow
1668  * @menu: The #GtkMenu to be used for this #HildonWindow
1669  * 
1670  * Sets the menu to be used for this window. This menu overrides
1671  * a program-wide menu that may have been set with
1672  * hildon_program_set_common_menu. Pass NULL to remove the current
1673  * menu.
1674  **/ 
1675 void
1676 hildon_window_set_menu (HildonWindow *self, GtkMenu *menu)
1677 {
1678     g_return_if_fail (HILDON_IS_WINDOW (self));
1679
1680     if (self->priv->menu != NULL) {
1681         gtk_menu_detach (GTK_MENU (self->priv->menu));
1682         g_object_unref (self->priv->menu);
1683     }
1684
1685     self->priv->menu = (menu != NULL) ? GTK_WIDGET (menu) : NULL;
1686     if (self->priv->menu != NULL) {
1687         gtk_widget_set_name (self->priv->menu, "menu_force_with_corners");
1688         gtk_menu_attach_to_widget (GTK_MENU (self->priv->menu), GTK_WIDGET (self), &detach_menu_func);
1689         g_object_ref (GTK_MENU (self->priv->menu));
1690     }
1691
1692     gtk_widget_show_all (GTK_WIDGET (self));
1693 }
1694
1695 /**
1696  * hildon_window_get_is_topmost:
1697  * @self: A #HildonWindow
1698  * 
1699  * Return value: Whether or not the #HildonWindow is currenltly activated
1700  * by the window manager.
1701  **/
1702 gboolean
1703 hildon_window_get_is_topmost(HildonWindow *self){
1704     g_return_val_if_fail (HILDON_IS_WINDOW (self), FALSE);
1705     
1706     return self->priv->is_topmost;
1707 }
1708