aboutsummaryrefslogtreecommitdiff
path: root/hildon/hildon-program.c
blob: e7d332fd8665c11ae5ef22de9938df90c6560257 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
/*
 * This file is a part of hildon
 *
 * Copyright (C) 2006 Nokia Corporation, all rights reserved.
 *
 * Contact: Rodrigo Novo <rodrigo.novo@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**
 * SECTION:hildon-program
 * @short_description: Application abstraction in the Hildon framework.
 * @see_also: #HildonWindow, #HildonStackableWindow
 *
 * #HildonProgram is an object used to represent an application running
 * in the Hildon framework.
 *
 * Applications can have one or more #HildonWindow<!-- -->s. These
 * can be registered in the #HildonProgram with hildon_program_add_window(),
 * and can be unregistered similarly with hildon_program_remove_window().
 *
 * #HildonProgram provides the programmer with commodities such
 * as applying a common toolbar and menu to all registered
 * #HildonWindow<!-- -->s. This is done with hildon_program_set_common_menu(),
 * hildon_program_set_common_app_menu() and hildon_program_set_common_toolbar().
 *
 * #HildonProgram is also used to apply program-wide properties that
 * are specific to the Hildon framework. For instance
 * hildon_program_set_can_hibernate() sets whether or not an application
 * can be set to hibernate by the Hildon task navigator, in situations of
 * low memory.
 *
 * <example>
 * <programlisting>
 * HildonProgram *program;
 * HildonWindow *window1;
 * HildonWindow *window2;
 * GtkToolbar *common_toolbar, *window_specific_toolbar;
 * HildonAppMenu *menu;
 * <!-- -->
 * program = HILDON_PROGRAM (hildon_program_get_instance ());
 * <!-- -->
 * window1 = HILDON_WINDOW (hildon_window_new ());
 * window2 = HILDON_WINDOW (hildon_window_new ());
 * <!-- -->
 * common_toolbar = create_common_toolbar ();
 * window_specific_toolbar = create_window_specific_toolbar ();
 * <!-- -->
 * menu = create_menu ();
 * <!-- -->
 * hildon_program_add_window (program, window1);
 * hildon_program_add_window (program, window2);
 * <!-- -->
 * hildon_program_set_common_app_menu (program, menu);
 * <!-- -->
 * hildon_program_set_common_toolbar (program, common_toolbar);
 * hildon_window_add_toolbar (window1, window_specific_toolbar);
 * <!-- -->
 * hildon_program_set_can_hibernate (program, TRUE);
 * </programlisting>
 * </example>
 */

#ifdef                                          HAVE_CONFIG_H
#include                                        <config.h>
#endif

#include                                        <gdk/gdkx.h>
#include                                        <X11/Xatom.h>

#include                                        "hildon-program.h"
#include                                        "hildon-program-private.h"
#include                                        "hildon-window-private.h"
#include                                        "hildon-window-stack.h"
#include                                        "hildon-app-menu-private.h"

static void
hildon_program_init                             (HildonProgram *self);

static void
hildon_program_finalize                         (GObject *self);

static void
hildon_program_class_init                       (HildonProgramClass *self);

static void
hildon_program_get_property                     (GObject *object, 
                                                 guint property_id,
                                                 GValue *value, 
                                                 GParamSpec *pspec);
static void
hildon_program_set_property                     (GObject *object, 
                                                 guint property_id,
                                                 const GValue *value, 
                                                 GParamSpec *pspec);

enum
{
    PROP_0,
    PROP_IS_TOPMOST,
    PROP_KILLABLE
};

GType G_GNUC_CONST
hildon_program_get_type                         (void)
{
    static GType program_type = 0;

    if (! program_type)
    {
        static const GTypeInfo program_info =
        {
            sizeof (HildonProgramClass),
            NULL,       /* base_init */
            NULL,       /* base_finalize */
            (GClassInitFunc) hildon_program_class_init,
            NULL,       /* class_finalize */
            NULL,       /* class_data */
            sizeof (HildonProgram),
            0,  /* n_preallocs */
            (GInstanceInitFunc) hildon_program_init,
        };
        program_type = g_type_register_static(G_TYPE_OBJECT,
                "HildonProgram", &program_info, 0);
    }
    return program_type;
}

static void
hildon_program_init                             (HildonProgram *self)
{
    HildonProgramPrivate *priv = HILDON_PROGRAM_GET_PRIVATE (self);
    g_assert (priv);
    
    priv->killable = FALSE;
    priv->window_count = 0;
    priv->is_topmost = FALSE;
    priv->common_menu = NULL;
    priv->common_app_menu = NULL;
    priv->common_toolbar = NULL;
    priv->windows = NULL;
}

static void
hildon_program_finalize                         (GObject *self)
{
    HildonProgramPrivate *priv = HILDON_PROGRAM_GET_PRIVATE (HILDON_PROGRAM (self));
    g_assert (priv);
    
    if (priv->common_toolbar)
    {
        g_object_unref (priv->common_toolbar);
        priv->common_toolbar = NULL;
    }

    if (priv->common_menu)
    {
        g_object_unref (priv->common_menu);
        priv->common_menu = NULL;
    }
}

static void
hildon_program_class_init                       (HildonProgramClass *self)
{
    GObjectClass *object_class = G_OBJECT_CLASS (self);

    g_type_class_add_private (self, sizeof (HildonProgramPrivate));

    /* Set up object virtual functions */
    object_class->finalize      = hildon_program_finalize;
    object_class->set_property  = hildon_program_set_property;
    object_class->get_property  = hildon_program_get_property;

    /* Install properties */

    /**
     * HildonProgram:is-topmost:
     *
     * Whether one of the program's window or dialog currently
     * is activated by window manager. 
     */
    g_object_class_install_property (object_class, PROP_IS_TOPMOST,
                g_param_spec_boolean ("is-topmost",
                "Is top-most",
                "Whether one of the program's window or dialog currently "
                "is activated by window manager",
                FALSE,
                G_PARAM_READABLE)); 

    /**
     * HildonProgram:can-hibernate:
     *
     * Whether the program should be set to hibernate by the Task
     * Navigator in low memory situation.
     */
    g_object_class_install_property (object_class, PROP_KILLABLE,
                g_param_spec_boolean ("can-hibernate",
                "Can hibernate",
                "Whether the program should be set to hibernate by the Task "
                "Navigator in low memory situation",
                FALSE,
                G_PARAM_READWRITE)); 
    return;
}

static void
hildon_program_set_property                     (GObject *object, 
                                                 guint property_id,
                                                 const GValue *value, 
                                                 GParamSpec *pspec)
{
    switch (property_id) {

        case PROP_KILLABLE:
            hildon_program_set_can_hibernate (HILDON_PROGRAM (object), g_value_get_boolean (value));
            break;
            
        default:
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
            break;
    }

}

static void
hildon_program_get_property                     (GObject *object, 
                                                 guint property_id,
                                                 GValue *value, 
                                                 GParamSpec *pspec)
{
    HildonProgramPrivate *priv = HILDON_PROGRAM_GET_PRIVATE (object);
    g_assert (priv);

    switch (property_id)
    {
        case PROP_KILLABLE:
            g_value_set_boolean (value, priv->killable);
            break;

        case PROP_IS_TOPMOST:
            g_value_set_boolean (value, priv->is_topmost);
            break;

        default:
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
            break;
    }
}

/* Utilities */
static gint 
hildon_program_window_list_compare              (gconstpointer window_a, 
                                                 gconstpointer window_b)
{
    g_return_val_if_fail (HILDON_IS_WINDOW(window_a) && 
                          HILDON_IS_WINDOW(window_b), 1);

    return window_a != window_b;
}

/*
 * foreach function, checks if a window is topmost and acts consequently
 */
static void
hildon_program_window_list_is_is_topmost        (gpointer data, 
                                                 gpointer window_id_)
{
    if (data && HILDON_IS_WINDOW (data))
    {
        HildonWindow *window = HILDON_WINDOW (data);
        Window window_id = * (Window*)window_id_;

        hildon_window_update_topmost (window, window_id);
    }
}

/*
 * Check the _MB_CURRENT_APP_WINDOW on the root window, and update
 * the top_most status accordingly
 */
static void
hildon_program_update_top_most                  (HildonProgram *program)
{
    gboolean is_topmost;
    Window active_window;
    HildonProgramPrivate *priv;

    priv = HILDON_PROGRAM_GET_PRIVATE (program);
    g_assert (priv);
    
    active_window = hildon_window_get_active_window();
    is_topmost = FALSE;

    if (active_window)
    {
      gint xerror;
      XWMHints *wm_hints;
      
      gdk_error_trap_push ();
      wm_hints = XGetWMHints (GDK_DISPLAY (), active_window);
      xerror = gdk_error_trap_pop ();
      if (xerror && xerror != BadWindow)
      {
        if (wm_hints)
          XFree (wm_hints);
        return;
      }

      if (wm_hints)
      {
        GSList *iter;
        for (iter = priv->windows ; iter && !is_topmost; iter = iter->next)
          {
            GdkWindow *gdkwin = GTK_WIDGET (iter->data)->window;
            GdkWindow *group = gdkwin ? gdk_window_get_group (gdkwin) : NULL;
            if (group)
              is_topmost = wm_hints->window_group == GDK_WINDOW_XID (group);
          }
        XFree (wm_hints);
      }
    }

    /* Send notification if is_topmost has changed */
    if (!priv->is_topmost != !is_topmost)
    {
      priv->is_topmost = is_topmost;
      g_object_notify (G_OBJECT (program), "is-topmost");
    }

    /* Check each window if it was is_topmost */
    g_slist_foreach (priv->windows, 
            (GFunc)hildon_program_window_list_is_is_topmost, &active_window);
}

/*
 * We keep track of the _MB_CURRENT_APP_WINDOW property on the root window,
 * to detect when a window belonging to this program was is_topmost. This
 * is based on the window group WM hint.
 */
static GdkFilterReturn
hildon_program_root_window_event_filter         (GdkXEvent *xevent,
                                                 GdkEvent *event,
                                                 gpointer data)
{
    XAnyEvent *eventti = xevent;
    HildonProgram *program = HILDON_PROGRAM (data);
    Atom active_app_atom =
            XInternAtom (GDK_DISPLAY (), "_MB_CURRENT_APP_WINDOW", False);

    if (eventti->type == PropertyNotify)
    {
        XPropertyEvent *pevent = xevent;

        if (pevent->atom == active_app_atom)
        {
            hildon_program_update_top_most (program);
        }
    }

    return GDK_FILTER_CONTINUE;
}

static void
hildon_program_window_set_common_menu_flag (HildonWindow *window,
                                            gboolean common_menu)
{
    if (HILDON_IS_WINDOW (window))
    {
        gboolean has_menu = hildon_window_get_app_menu (window) ||
            hildon_window_get_main_menu (window);

        if (!has_menu) {
            hildon_window_set_menu_flag (window, common_menu);
        }
    }
}

static void
hildon_program_set_common_menu_flag (HildonProgram *self,
                                     gboolean common_menu)
{
    HildonProgramPrivate *priv = HILDON_PROGRAM_GET_PRIVATE (self);

    g_slist_foreach (priv->windows,
                     (GFunc) hildon_program_window_set_common_menu_flag,
                     GINT_TO_POINTER (common_menu));
}

/* 
 * Checks if the window is the topmost window of the program and in
 * that case forces the window to take the common toolbar.
 */
static void
hildon_program_common_toolbar_topmost_window    (gpointer window, 
                                                 gpointer data)
{
    if (HILDON_IS_WINDOW (window) && hildon_window_get_is_topmost (HILDON_WINDOW (window)))
        hildon_window_take_common_toolbar (HILDON_WINDOW (window));
}

/**
 * hildon_program_get_instance:
 *
 * Returns the #HildonProgram for the current process. The object is
 * created on the first call. Note that you're not supposed to unref
 * the returned object since it's not reffed in the first place.
 *
 * Return value: the #HildonProgram.
 **/
HildonProgram*
hildon_program_get_instance                     (void)
{
    static HildonProgram *program = NULL;

    if (! program)
    {
        program = g_object_new (HILDON_TYPE_PROGRAM, NULL);
    }

    return program;
}

static void
window_add_event_filter                         (GtkWidget     *widget,
                                                 HildonProgram *program)
{
    GdkWindow *gdk_window = gtk_widget_get_window (widget);
    g_assert (gdk_window);

    gdk_window_set_events (gdk_window,
                           gdk_window_get_events (gdk_window) | GDK_PROPERTY_CHANGE_MASK);

    gdk_window_add_filter (gdk_window,
                           hildon_program_root_window_event_filter, program);

    g_signal_handlers_disconnect_by_func (widget, G_CALLBACK (window_add_event_filter),
                                          program);
}

/**
 * hildon_program_add_window:
 * @self: The #HildonProgram to which the window should be registered
 * @window: A #HildonWindow to be added
 *
 * Registers a #HildonWindow as belonging to a given #HildonProgram. This
 * allows to apply program-wide settings as all the registered windows,
 * such as hildon_program_set_common_menu(), hildon_program_set_common_app_menu()
 * and hildon_program_set_common_toolbar().
 **/
void
hildon_program_add_window                       (HildonProgram *self, 
                                                 HildonWindow *window)
{
    HildonProgramPrivate *priv;
    
    g_return_if_fail (HILDON_IS_PROGRAM (self));
    g_return_if_fail (HILDON_IS_WINDOW (window));
    
    priv = HILDON_PROGRAM_GET_PRIVATE (self);
    g_assert (priv);

    if (g_slist_find_custom (priv->windows, window,
           hildon_program_window_list_compare) )
    {
        /* We already have that window */
        return;
    }

    hildon_program_update_top_most (self);

    /* Now that we have a window we should start keeping track of
     * the root window */
    if (GTK_WIDGET_REALIZED (window))
    {
        window_add_event_filter (GTK_WIDGET (window), self);
    }
    else
    {
        g_signal_connect_after (window, "realize",
                                G_CALLBACK (window_add_event_filter), self);
    }

    hildon_window_set_can_hibernate_property (window, &priv->killable);

    hildon_window_set_program (window, G_OBJECT (self));

    if (priv->common_menu || priv->common_app_menu)
        hildon_program_window_set_common_menu_flag (window, TRUE);

    priv->windows = g_slist_append (priv->windows, window);
    priv->window_count ++;
}

/**
 * hildon_program_remove_window:
 * @self: The #HildonProgram to which the window should be unregistered
 * @window: The #HildonWindow to unregister
 *
 * Used to unregister a window from the program. Subsequent calls to
 * hildon_program_set_common_menu(), hildon_program_set_common_app_menu()
 * and hildon_program_set_common_toolbar() will not affect the window.
 **/
void
hildon_program_remove_window                    (HildonProgram *self, 
                                                 HildonWindow *window)
{
    HildonProgramPrivate *priv;
    
    g_return_if_fail (HILDON_IS_PROGRAM (self));
    g_return_if_fail (HILDON_IS_WINDOW (window));
    
    priv = HILDON_PROGRAM_GET_PRIVATE (self);
    g_assert (priv);
    
    g_return_if_fail (g_slist_find (priv->windows, window));

    hildon_window_unset_program (window);

    priv->windows = g_slist_remove (priv->windows, window);

    priv->window_count --;

    if (GTK_WIDGET_REALIZED (window))
    {
        gdk_window_remove_filter (gtk_widget_get_window (GTK_WIDGET (window)),
                                  hildon_program_root_window_event_filter,
                                  self);
    }

    if (priv->common_menu || priv->common_app_menu)
        hildon_program_window_set_common_menu_flag (window, FALSE);
}

/**
 * hildon_program_set_can_hibernate:
 * @self: The #HildonProgram which can hibernate or not
 * @can_hibernate: whether or not the #HildonProgram can hibernate
 *
 * Used to set whether or not the Hildon task navigator should
 * be able to set the program to hibernation in case of low memory
 **/
void
hildon_program_set_can_hibernate                (HildonProgram *self, 
                                                 gboolean can_hibernate)
{
    HildonProgramPrivate *priv;
    
    g_return_if_fail (HILDON_IS_PROGRAM (self));
    
    priv = HILDON_PROGRAM_GET_PRIVATE (self);
    g_assert (priv);

    if (priv->killable != can_hibernate)
    {
        g_slist_foreach (priv->windows, 
                (GFunc) hildon_window_set_can_hibernate_property, &can_hibernate);
    }

    priv->killable = can_hibernate;
}

/**
 * hildon_program_get_can_hibernate:
 * @self: The #HildonProgram which can hibernate or not
 *
 * Returns whether the #HildonProgram is set to be support hibernation
 * from the Hildon task navigator
 *
 * Return value: %TRUE if the program can hibernate, %FALSE otherwise.
 **/
gboolean
hildon_program_get_can_hibernate                (HildonProgram *self)
{
    HildonProgramPrivate *priv;
    
    g_return_val_if_fail (HILDON_IS_PROGRAM (self), FALSE);
   
    priv = HILDON_PROGRAM_GET_PRIVATE (self);
    g_assert (priv);

    return priv->killable;
}

/**
 * hildon_program_set_common_menu:
 * @self: The #HildonProgram in which the common menu should be used
 * @menu: A #GtkMenu to use as common menu for the program
 *
 * Sets a #GtkMenu that will appear in all #HildonWindow<!-- -->s
 * registered with the #HildonProgram. Only one common #GtkMenu can be
 * set, further calls will detach the previous common #GtkMenu. A
 * #HildonWindow can use its own #GtkMenu with
 * hildon_window_set_menu()
 *
 * This method does not support #HildonAppMenu<!-- -->s. See
 * hildon_program_set_common_app_menu() for that.
 **/
void
hildon_program_set_common_menu                  (HildonProgram *self, 
                                                 GtkMenu *menu)
{
    HildonProgramPrivate *priv;

    g_return_if_fail (HILDON_IS_PROGRAM (self));

    priv = HILDON_PROGRAM_GET_PRIVATE (self);
    g_assert (priv);

    if (priv->common_menu)
    {
        if (GTK_WIDGET_VISIBLE (priv->common_menu))
        {
            gtk_menu_popdown (priv->common_menu);
            gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->common_menu));
        }

        if (gtk_menu_get_attach_widget (priv->common_menu))
        {
            gtk_menu_detach (priv->common_menu);
        }
        else
        {
            g_object_unref (priv->common_menu);
        }
    }

    /* Only set the menu flag if there was no common menu and
       we are setting one. If we are unsetting the current common menu,
       remove the commmon menu flag. Otherwise, nothing to do. */

    GList *menu_children = gtk_container_get_children (GTK_CONTAINER (menu));
    if (!priv->common_menu
        && menu && menu_children != NULL) {
        hildon_program_set_common_menu_flag (self, TRUE);
    } else if (priv->common_menu &&
               (!menu || menu_children == NULL))
    {
        hildon_program_set_common_menu_flag (self, FALSE);
    }
    g_list_free (menu_children);

    priv->common_menu = menu;

    if (priv->common_menu)
    {
        g_object_ref (menu);
        gtk_object_sink (GTK_OBJECT (menu));
        gtk_widget_show_all (GTK_WIDGET (menu));
    }
}

/**
 * hildon_program_get_common_menu:
 * @self: The #HildonProgram from which to retrieve the common menu
 *
 * Returns the #GtkMenu that was set as common menu for this
 * #HildonProgram.
 *
 * Return value: the #GtkMenu or %NULL of no common menu was set.
 **/
GtkMenu*
hildon_program_get_common_menu                  (HildonProgram *self)
{
    HildonProgramPrivate *priv;

    g_return_val_if_fail (HILDON_IS_PROGRAM (self), NULL);

    priv = HILDON_PROGRAM_GET_PRIVATE (self);
    g_assert (priv);

    return priv->common_menu;
}

static void
hildon_program_on_common_app_menu_changed       (HildonAppMenu *menu,
                                                 HildonProgram *program)
{
    hildon_program_set_common_menu_flag (program,
                                         hildon_app_menu_has_visible_children (menu));
}

/**
 * hildon_program_set_common_app_menu:
 * @self: The #HildonProgram in which the common menu should be used
 * @menu: A #HildonAppMenu to use as common menu for the program
 *
 * Sets a #HildonAppMenu that will appear in all
 * #HildonWindow<!-- -->s registered with the #HildonProgram. Only
 * one common #HildonAppMenu can be set, further calls will detach the
 * previous common #HildonAppMenu. A #HildonWindow can use its own
 * #HildonAppMenu with hildon_window_set_app_menu()
 *
 * This method does not support #GtkMenu<!-- -->s. See
 * hildon_program_set_common_menu() for that.
 *
 * Since: 2.2
 **/
void
hildon_program_set_common_app_menu              (HildonProgram *self,
                                                 HildonAppMenu *menu)
{
    HildonProgramPrivate *priv;
    HildonAppMenu *old_menu;

    g_return_if_fail (HILDON_IS_PROGRAM (self));
    g_return_if_fail (menu == NULL || HILDON_IS_APP_MENU (menu));

    priv = HILDON_PROGRAM_GET_PRIVATE (self);
    g_assert (priv);

    old_menu = priv->common_app_menu;

    /* Only set the menu flag if there was no common menu and
       we are setting one. If we are unsetting the current common menu,
       remove the commmon menu flag. Otherwise, nothing to do. */
    if (!priv->common_app_menu
        && menu && hildon_app_menu_has_visible_children (menu)) {
        hildon_program_set_common_menu_flag (self, TRUE);
    } else if (priv->common_app_menu &&
               (!menu || !hildon_app_menu_has_visible_children (menu))) {
        hildon_program_set_common_menu_flag (self, FALSE);
    }

    /* Set new menu */
    priv->common_app_menu = menu;
    if (menu) {
        g_signal_connect (menu, "changed",
                          G_CALLBACK (hildon_program_on_common_app_menu_changed), self);
        g_object_ref_sink (menu);
    }

    /* Hide and unref old menu */
    if (old_menu) {
        hildon_app_menu_set_parent_window (old_menu, NULL);
        g_signal_handlers_disconnect_by_func (old_menu,
                                              hildon_program_on_common_app_menu_changed,
                                              self);
        g_object_unref (old_menu);
    }
}

/**
 * hildon_program_get_common_app_menu:
 * @self: The #HildonProgram from which to retrieve the common app menu
 *
 * Returns the #HildonAppMenu that was set as common menu for this
 * #HildonProgram.
 *
 * Return value: the #HildonAppMenu or %NULL of no common app menu was
 * set.
 *
 * Since: 2.2
 **/
HildonAppMenu*
hildon_program_get_common_app_menu              (HildonProgram *self)
{
    HildonProgramPrivate *priv;

    g_return_val_if_fail (HILDON_IS_PROGRAM (self), NULL);

    priv = HILDON_PROGRAM_GET_PRIVATE (self);
    g_assert (priv);

    return priv->common_app_menu;
}

/**
 * hildon_program_set_common_toolbar:
 * @self: The #HildonProgram in which the common toolbar should be used
 * @toolbar: A #GtkToolbar to use as common toolbar for the program
 *
 * Sets a #GtkToolbar that will appear in all the #HildonWindow registered
 * to the #HildonProgram. Only one common #GtkToolbar can be set, further
 * call will detach the previous common #GtkToolbar. A #HildonWindow
 * can use its own #GtkToolbar with hildon_window_add_toolbar(). Both
 * #HildonProgram and #HildonWindow specific toolbars will be shown
 **/
void
hildon_program_set_common_toolbar               (HildonProgram *self, 
                                                 GtkToolbar *toolbar)
{
    HildonProgramPrivate *priv;

    g_return_if_fail (HILDON_IS_PROGRAM (self));

    priv = HILDON_PROGRAM_GET_PRIVATE (self);
    g_assert (priv);

    if (priv->common_toolbar)
    {
        if (priv->common_toolbar->parent)
        {
            gtk_container_remove (GTK_CONTAINER (priv->common_toolbar->parent), 
                                  priv->common_toolbar);
        }
        
        g_object_unref (priv->common_toolbar);
    }

    priv->common_toolbar = GTK_WIDGET (toolbar);

    if (priv->common_toolbar)
    {
        g_object_ref (priv->common_toolbar);
        gtk_object_sink (GTK_OBJECT (priv->common_toolbar) );
    }

    /* if the program is the topmost we have to update the common
       toolbar right now for the topmost window */
    if (priv->is_topmost)
      {
        g_slist_foreach (priv->windows, 
                         (GFunc) hildon_program_common_toolbar_topmost_window, NULL);
      }
}

/**
 * hildon_program_get_common_toolbar:
 * @self: The #HildonProgram from which to retrieve the common toolbar
 *
 * Returns the #GtkToolbar that was set as common toolbar for this
 * #HildonProgram.
 *
 * Return value: the #GtkToolbar or %NULL of no common toolbar was
 * set.
 **/
GtkToolbar*
hildon_program_get_common_toolbar               (HildonProgram *self)
{
    HildonProgramPrivate *priv;

    g_return_val_if_fail (HILDON_IS_PROGRAM (self), NULL);

    priv = HILDON_PROGRAM_GET_PRIVATE (self);
    g_assert (priv);

    return priv->common_toolbar ? GTK_TOOLBAR (priv->common_toolbar) : NULL;
}

/**
 * hildon_program_get_is_topmost:
 * @self: A #HildonWindow
 *
 * Returns whether one of the program's windows or dialogs is
 * currently activated by the window manager.
 *
 * Return value: %TRUE if a window or dialog is topmost, %FALSE
 * otherwise.
 **/
gboolean
hildon_program_get_is_topmost                   (HildonProgram *self)
{
    HildonProgramPrivate *priv;

    g_return_val_if_fail (HILDON_IS_PROGRAM (self), FALSE);
    
    priv = HILDON_PROGRAM_GET_PRIVATE (self);
    g_assert (priv);

    return priv->is_topmost;
}