9780b184be9c9699eadefab5096ae9b92994bbc2
[hildon] / hildon / hildon-banner.c
1 /*
2  * This file is a part of hildon
3  *
4  * Copyright (C) 2005, 2006, 2007 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-banner
27  * @short_description: A widget used to display timed notifications.
28  *
29  * #HildonBanner is a small, pop-up window that can be used to display
30  * a short, timed notification or information to the user. It can
31  * communicate that a task has been finished or that the application
32  * state has changed.
33  *
34  * Hildon provides convenient funtions to create and show banners. To
35  * create and show information banners you can use
36  * hildon_banner_show_information(), hildon_banner_show_informationf()
37  * or hildon_banner_show_information_with_markup().
38  *
39  * If the application window has set the _HILDON_DO_NOT_DISTURB flag (using
40  * hildon_gtk_window_set_do_not_disturb() for example), the banner will not
41  * be shown. If you need to override this flag for important information,
42  * you can use the method hildon_banner_show_information_override_dnd().
43  * Please, take into account that this is only for important information.
44  *
45  *
46  * Two more kinds of banners are maintained for backward compatibility
47  * but are no longer recommended in Hildon 2.2. These are the animated
48  * banner (created with hildon_banner_show_animation()) and the
49  * progress banner (created with hildon_banner_show_progress()). See
50  * hildon_gtk_window_set_progress_indicator() for the preferred way of
51  * showing progress notifications in Hildon 2.2.
52  *
53  * Information banners are automatically destroyed after a certain
54  * period. This is stored in the #HildonBanner:timeout property (in
55  * miliseconds), and can be changed using hildon_banner_set_timeout().
56  *
57  * Note that #HildonBanner<!-- -->s should only be used to display
58  * non-critical pieces of information.
59  *
60  * <example>
61  * <title>Using the HildonBanner widget</title>
62  * <programlisting>
63  * void show_info_banner (GtkWidget *parent)
64  * {
65  *   GtkWidget *banner;
66  * <!-- -->
67  *   banner = hildon_banner_show_information (widget, NULL, "Information banner");
68  *   hildon_banner_set_timeout (HILDON_BANNER (banner), 9000);
69  * <!-- -->
70  *   return;
71  * }
72  * </programlisting>
73  * </example>
74  */
75
76 #ifdef                                          HAVE_CONFIG_H
77 #include                                        <config.h>
78 #endif
79
80 #include                                        <string.h>
81 #include                                        <X11/Xatom.h>
82 #include                                        <gdk/gdkx.h>
83
84 #undef                                          HILDON_DISABLE_DEPRECATED
85
86 #include                                        "hildon-banner.h"
87 #include                                        "hildon-private.h"
88 #include                                        "hildon-defines.h"
89 #include                                        "hildon-gtk.h"
90
91 /* max widths */
92
93 #define                                         HILDON_BANNER_LABEL_MAX_TIMED \
94                                                 (gdk_screen_width() - ((HILDON_MARGIN_TRIPLE) * 2))
95
96 #define                                         HILDON_BANNER_LABEL_MAX_PROGRESS 375 /*265*/
97
98 /* default timeout */
99
100 #define                                         HILDON_BANNER_DEFAULT_TIMEOUT 3000
101
102 /* default icons */
103
104 #define                                         HILDON_BANNER_DEFAULT_PROGRESS_ANIMATION "indicator_update"
105
106 /* animation related stuff */
107
108 #define                                         HILDON_BANNER_ANIMATION_FRAMERATE ((float)1000/150)
109
110 #define                                         HILDON_BANNER_ANIMATION_TMPL "indicator_update%d"
111
112 #define                                         HILDON_BANNER_ANIMATION_NFRAMES 8
113
114 enum 
115 {
116     PROP_0,
117     PROP_PARENT_WINDOW, 
118     PROP_IS_TIMED,
119     PROP_TIMEOUT
120 };
121
122 static GtkWidget*                               global_timed_banner = NULL;
123
124 static GQuark 
125 hildon_banner_timed_quark                       (void);
126
127 static void 
128 hildon_banner_bind_style                        (HildonBanner *self);
129
130 static gboolean 
131 hildon_banner_timeout                           (gpointer data);
132
133 static gboolean 
134 hildon_banner_clear_timeout                     (HildonBanner *self);
135
136 static void 
137 hildon_banner_ensure_timeout                    (HildonBanner *self);
138
139 static void 
140 hildon_banner_set_property                      (GObject *object,
141                                                  guint prop_id,
142                                                  const GValue *value,
143                                                  GParamSpec *pspec);
144     
145 static void 
146 hildon_banner_get_property                      (GObject *object,
147                                                  guint prop_id,
148                                                  GValue *value,
149                                                  GParamSpec *pspec);
150
151 static void
152 hildon_banner_destroy                           (GtkObject *object);
153         
154 static GObject*
155 hildon_banner_real_get_instance                 (GObject *window, 
156                                                  gboolean timed);
157
158 static GObject* 
159 hildon_banner_constructor                       (GType type,
160                                                  guint n_construct_params,
161                                                  GObjectConstructParam *construct_params);
162
163 static void
164 hildon_banner_finalize                          (GObject *object);
165
166 static gboolean
167 hildon_banner_button_press_event                (GtkWidget* widget,
168                                                  GdkEventButton* event);
169
170 static gboolean 
171 hildon_banner_map_event                         (GtkWidget *widget, 
172                                                  GdkEventAny *event);
173
174 static void 
175 force_to_wrap_truncated                         (HildonBanner *banner);
176
177 static void
178 hildon_banner_realize                           (GtkWidget *widget);
179
180 static void 
181 hildon_banner_class_init                        (HildonBannerClass *klass);
182
183 static void 
184 hildon_banner_init                              (HildonBanner *self);
185
186 static void
187 hildon_banner_ensure_child                      (HildonBanner *self, 
188                                                  GtkWidget *user_widget,
189                                                  guint pos,
190                                                  GType type,
191                                                  const gchar *first_property, 
192                                                  ...);
193
194 static HildonBanner*
195 hildon_banner_get_instance_for_widget           (GtkWidget *widget, 
196                                                  gboolean timed);
197
198 static void
199 hildon_banner_set_override_flag                 (HildonBanner *banner);
200
201 static GtkWidget*
202 hildon_banner_real_show_information             (GtkWidget *widget,
203                                                  const gchar *text,
204                                                  gboolean override_dnd);
205
206 G_DEFINE_TYPE (HildonBanner, hildon_banner, GTK_TYPE_WINDOW)
207
208 typedef struct                                  _HildonBannerPrivate HildonBannerPrivate;
209
210 #define                                         HILDON_BANNER_GET_PRIVATE(obj) \
211                                                 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
212                                                 HILDON_TYPE_BANNER, HildonBannerPrivate));
213
214 struct                                          _HildonBannerPrivate
215 {
216     GtkWidget   *main_item;
217     GtkWidget   *alignment;
218     GtkWidget   *label;
219     GtkWidget   *layout;
220     GtkWindow   *parent;
221     const gchar *name_suffix;
222     guint        timeout;
223     guint        timeout_id;
224     guint        is_timed             : 1;
225     guint        require_override_dnd : 1;
226     guint        overrides_dnd        : 1;
227 };
228
229 static GQuark 
230 hildon_banner_timed_quark                       (void)
231 {
232     static GQuark quark = 0;
233
234     if (G_UNLIKELY(quark == 0))
235         quark = g_quark_from_static_string ("hildon-banner-timed");
236
237     return quark;
238 }
239
240 /* Set the widget and label name to make the correct rc-style attached into them */
241 static void 
242 hildon_banner_bind_style                  (HildonBanner *self)
243 {
244     HildonBannerPrivate *priv = HILDON_BANNER_GET_PRIVATE (self);
245     GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (self));
246     gboolean portrait = gdk_screen_get_width (screen) < gdk_screen_get_height (screen);
247     const gchar *portrait_suffix = portrait ? "-portrait" : NULL;
248     gchar *name;
249
250     g_assert (priv);
251
252     name = g_strconcat ("HildonBannerLabel-", priv->name_suffix, NULL);
253     gtk_widget_set_name (priv->label, name);
254     g_free (name);
255
256     name = g_strconcat ("HildonBanner-", priv->name_suffix, portrait_suffix, NULL);
257     gtk_widget_set_name (GTK_WIDGET (self), name);
258     g_free (name);
259 }
260
261 /* In timeout function we automatically destroy timed banners */
262 static gboolean
263 simulate_close (GtkWidget* widget)
264 {
265     gboolean result = FALSE;
266
267     /* If the banner is currently visible (it normally should), 
268        we simulate clicking the close button of the window.
269        This allows applications to reuse the banner by prevent
270        closing it etc */
271     if (GTK_WIDGET_DRAWABLE (widget))
272     {
273         GdkEvent *event = gdk_event_new (GDK_DELETE);
274         event->any.window = g_object_ref (widget->window);
275         event->any.send_event = FALSE;
276         result = gtk_widget_event (widget, event);
277         gdk_event_free (event);
278     }
279
280     return result;
281 }
282
283 static void
284 hildon_banner_size_request                      (GtkWidget      *self,
285                                                  GtkRequisition *req)
286 {
287     GTK_WIDGET_CLASS (hildon_banner_parent_class)->size_request (self, req);
288     req->width = gdk_screen_get_width (gtk_widget_get_screen (self));
289 }
290
291 static gboolean 
292 hildon_banner_timeout                           (gpointer data)
293 {
294     GtkWidget *widget;
295     gboolean continue_timeout = FALSE;
296
297     GDK_THREADS_ENTER ();
298
299     g_assert (HILDON_IS_BANNER (data));
300
301     widget = GTK_WIDGET (data);
302     g_object_ref (widget);
303
304     continue_timeout = simulate_close (widget);
305
306     if (! continue_timeout) {
307         HildonBannerPrivate *priv = HILDON_BANNER_GET_PRIVATE (data);
308         priv->timeout_id = 0;
309         gtk_widget_destroy (widget);
310     }
311
312     g_object_unref (widget);
313
314     GDK_THREADS_LEAVE ();
315
316     return continue_timeout;
317 }
318
319 static gboolean 
320 hildon_banner_clear_timeout                     (HildonBanner *self)
321 {
322     HildonBannerPrivate *priv = HILDON_BANNER_GET_PRIVATE (self);
323     g_assert (priv);
324
325     if (priv->timeout_id != 0) {
326         g_source_remove (priv->timeout_id);
327         priv->timeout_id = 0;
328         return TRUE;
329     }
330
331     return FALSE;
332 }
333
334 static void 
335 hildon_banner_ensure_timeout                    (HildonBanner *self)
336 {
337     HildonBannerPrivate *priv = HILDON_BANNER_GET_PRIVATE (self);
338     g_assert (priv);
339
340     if (priv->timeout_id == 0 && priv->is_timed && priv->timeout > 0)
341         priv->timeout_id = g_timeout_add (priv->timeout, 
342                 hildon_banner_timeout, self);
343 }
344
345 static void 
346 hildon_banner_set_property                      (GObject *object,
347                                                  guint prop_id,
348                                                  const GValue *value,
349                                                  GParamSpec *pspec)
350 {
351     GtkWidget *window;
352     HildonBannerPrivate *priv = HILDON_BANNER_GET_PRIVATE (object);
353     g_assert (priv);
354
355     switch (prop_id) {
356
357         case PROP_TIMEOUT:
358              priv->timeout = g_value_get_uint (value);
359              break;
360  
361         case PROP_IS_TIMED:
362             priv->is_timed = g_value_get_boolean (value);
363             break;
364
365         case PROP_PARENT_WINDOW:
366             window = g_value_get_object (value);         
367             if (priv->parent) {
368                 g_object_remove_weak_pointer(G_OBJECT (priv->parent), (gpointer) &priv->parent);
369             }
370
371             gtk_window_set_transient_for (GTK_WINDOW (object), (GtkWindow *) window);
372             priv->parent = (GtkWindow *) window;
373
374             if (window) {
375                 gtk_window_set_destroy_with_parent (GTK_WINDOW (object), TRUE);
376                 g_object_add_weak_pointer(G_OBJECT (window), (gpointer) &priv->parent);
377             }
378
379             break;
380
381         default:
382             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
383             break;
384     }
385 }
386
387 static void 
388 hildon_banner_get_property                      (GObject *object,
389                                                  guint prop_id,
390                                                  GValue *value,
391                                                  GParamSpec *pspec)
392 {
393     HildonBannerPrivate *priv = HILDON_BANNER_GET_PRIVATE (object);
394     g_assert (priv);
395
396     switch (prop_id)
397     {
398         case PROP_TIMEOUT:
399              g_value_set_uint (value, priv->timeout);
400              break;
401  
402         case PROP_IS_TIMED:
403             g_value_set_boolean (value, priv->is_timed);
404             break;
405
406         case PROP_PARENT_WINDOW:
407             g_value_set_object (value, gtk_window_get_transient_for (GTK_WINDOW (object)));
408             break;
409
410         default:
411             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
412             break;
413     }
414 }
415
416 static void
417 hildon_banner_destroy                           (GtkObject *object)
418 {
419     HildonBannerPrivate *priv = HILDON_BANNER_GET_PRIVATE (object);
420     g_assert (priv);
421
422     HildonBanner *self;
423     GObject *parent_window = (GObject *) priv->parent;
424
425     g_assert (HILDON_IS_BANNER (object));
426     self = HILDON_BANNER (object);
427
428     /* Drop possible global pointer. That can hold reference to us */
429     if ((gpointer) object == (gpointer) global_timed_banner) {
430         global_timed_banner = NULL;
431         g_object_unref (object);
432     }
433
434     /* Remove the data from parent window for timed banners. Those hold reference */
435     if (priv->is_timed && parent_window != NULL) {
436         g_object_set_qdata (parent_window, hildon_banner_timed_quark (), NULL);
437     }
438
439     if (!priv->is_timed && priv->parent) {
440         hildon_gtk_window_set_progress_indicator (priv->parent, 0);
441     }
442
443     (void) hildon_banner_clear_timeout (self);
444
445     if (GTK_OBJECT_CLASS (hildon_banner_parent_class)->destroy)
446         GTK_OBJECT_CLASS (hildon_banner_parent_class)->destroy (object);
447 }
448
449 /* Search a previous banner instance */
450 static GObject*
451 hildon_banner_real_get_instance                 (GObject *window, 
452                                                  gboolean timed)
453 {
454     if (timed) {
455         /* If we have a parent window, the previous instance is stored there */
456         if (window) {
457             return g_object_get_qdata(window, hildon_banner_timed_quark ());
458         }
459
460         /* System notification instance is stored into global pointer */
461         return (GObject *) global_timed_banner;
462     }
463
464     /* Non-timed banners are normal (non-singleton) objects */
465     return NULL;
466 }
467
468 /* By overriding constructor we force timed banners to be
469    singletons for each window */
470 static GObject* 
471 hildon_banner_constructor                       (GType type,
472                                                  guint n_construct_params,
473                                                  GObjectConstructParam *construct_params)
474 {
475     GObject *banner, *window = NULL;
476     gboolean timed = FALSE;
477     guint i;
478
479     /* Search banner type information from parameters in order
480        to locate the possible previous banner instance. */
481     for (i = 0; i < n_construct_params; i++)
482     {
483         if (strcmp(construct_params[i].pspec->name, "parent-window") == 0)
484             window = g_value_get_object (construct_params[i].value);       
485         else if (strcmp(construct_params[i].pspec->name, "is-timed") == 0)
486             timed = g_value_get_boolean (construct_params[i].value);
487     }
488
489     /* Try to get a previous instance if such exists */
490     banner = hildon_banner_real_get_instance (window, timed);
491     if (! banner)
492     {
493         /* We have to create a new banner */
494         banner = G_OBJECT_CLASS (hildon_banner_parent_class)->constructor (type, n_construct_params, construct_params);
495
496         /* Store the newly created singleton instance either into parent 
497            window data or into global variables. */
498         if (timed) {
499             if (window) {
500                 g_object_set_qdata_full (G_OBJECT (window), hildon_banner_timed_quark (), 
501                         g_object_ref (banner), g_object_unref); 
502             } else {
503                 g_assert (global_timed_banner == NULL);
504                 global_timed_banner = g_object_ref (banner);
505             }
506         }
507     }
508     else {
509         /* FIXME: This is a hack! We have to manually freeze
510            notifications. This is normally done by g_object_init, but we
511            are not going to call that. g_object_newv will otherwise give
512            a critical like this:
513
514            GLIB CRITICAL ** GLib-GObject - g_object_notify_queue_thaw: 
515            assertion `nqueue->freeze_count > 0' failed */
516
517         g_object_freeze_notify (banner);
518     }
519
520     /* We restart possible timeouts for each new timed banner request */
521     if (timed && hildon_banner_clear_timeout (HILDON_BANNER (banner)))
522         hildon_banner_ensure_timeout (HILDON_BANNER(banner));
523
524     return banner;
525 }
526
527 static void
528 hildon_banner_finalize                          (GObject *object)
529 {
530     HildonBannerPrivate *priv = HILDON_BANNER_GET_PRIVATE (object);
531
532     if (priv->parent) {
533         g_object_remove_weak_pointer(G_OBJECT (priv->parent), (gpointer) &priv->parent);
534     }
535
536     G_OBJECT_CLASS (hildon_banner_parent_class)->finalize (object);
537 }
538
539 static gboolean
540 hildon_banner_button_press_event                (GtkWidget* widget,
541                                                  GdkEventButton* event)
542 {
543     gboolean result = simulate_close (widget);
544
545     if (!result) {
546         /* signal emission not stopped - basically behave like
547          * gtk_main_do_event() for a delete event, but just hide the
548          * banner instead of destroying it, as it is already meant to
549          * be destroyed by hildon_banner_timeout() (if it's timed) or
550          * the application (if it's not). */
551         gtk_widget_hide (widget);
552     }
553
554     return result;
555 }
556
557 #if defined(MAEMO_GTK)
558 static void
559 hildon_banner_map                               (GtkWidget *widget)
560 {
561     if (GTK_WIDGET_CLASS (hildon_banner_parent_class)->map) {
562         /* Make the banner temporary _before_ mapping it, to avoid closing
563          * other temporary windows */
564         gtk_window_set_is_temporary (GTK_WINDOW (widget), TRUE);
565
566         GTK_WIDGET_CLASS (hildon_banner_parent_class)->map (widget);
567
568         /* Make the banner non-temporary _after_ mapping it, to avoid
569          * being closed by other non-temporary windows */
570         gtk_window_set_is_temporary (GTK_WINDOW (widget), FALSE);
571     }
572 }
573 #endif
574
575 /* We start the timer for timed notifications after the window appears on screen */
576 static gboolean 
577 hildon_banner_map_event                         (GtkWidget *widget, 
578                                                  GdkEventAny *event)
579 {
580     gboolean result = FALSE;
581
582     if (GTK_WIDGET_CLASS (hildon_banner_parent_class)->map_event)
583         result = GTK_WIDGET_CLASS (hildon_banner_parent_class)->map_event (widget, event);
584
585     hildon_banner_ensure_timeout (HILDON_BANNER(widget));
586
587     return result;
588 }  
589
590 static void
591 banner_do_set_text                              (HildonBanner *banner,
592                                                  const gchar  *text,
593                                                  gboolean      is_markup)
594 {
595     HildonBannerPrivate *priv;
596     GtkRequisition req;
597
598     priv = HILDON_BANNER_GET_PRIVATE (banner);
599
600     if (is_markup) {
601         gtk_label_set_markup (GTK_LABEL (priv->label), text);
602     } else {
603         gtk_label_set_text (GTK_LABEL (priv->label), text);
604     }
605     gtk_widget_set_size_request (priv->label, -1, -1);
606     gtk_widget_size_request (priv->label, &req);
607
608     force_to_wrap_truncated (banner);
609 }
610
611 /* force to wrap truncated label by setting explicit size request
612  * see N#27000 and G#329646 */
613 static void 
614 force_to_wrap_truncated                         (HildonBanner *banner)
615 {
616     GtkLabel *label;
617     PangoLayout *layout;
618     int width_max;
619     int width = -1;
620     int height = -1;
621     PangoRectangle logical;
622     GtkRequisition requisition;
623     HildonBannerPrivate *priv = HILDON_BANNER_GET_PRIVATE (banner);
624
625     g_return_if_fail (priv);
626
627     label = GTK_LABEL (priv->label);
628
629     layout = gtk_label_get_layout (label);
630
631     pango_layout_get_extents (layout, NULL, &logical);
632     width = PANGO_PIXELS (logical.width);
633
634     width_max = priv->is_timed ? HILDON_BANNER_LABEL_MAX_TIMED
635         : HILDON_BANNER_LABEL_MAX_PROGRESS;
636
637     /* If the width of the label is going to exceed the maximum allowed
638      * width, enforce the maximum allowed width now.
639      */
640     if (width >= width_max || pango_layout_is_wrapped (layout)) {
641         width = width_max;
642     }
643
644     /* Make the label update its layout; and update our layout pointer
645      * because the layout will be cleared and refreshed.
646      */
647     gtk_widget_set_size_request (GTK_WIDGET (label), width, height);
648     gtk_widget_size_request (GTK_WIDGET (label), &requisition);
649
650     layout = gtk_label_get_layout (label);
651
652     /* If the layout has now been wrapped and exceeds 3 lines, we truncate
653      * the rest of the label according to spec.
654      */
655     if (pango_layout_is_wrapped (layout) && pango_layout_get_line_count (layout) > 3) {
656         int lines;
657
658         pango_layout_get_extents (layout, NULL, &logical);
659         lines = pango_layout_get_line_count (layout);
660
661         /* This calculation assumes that the same font is used
662          * throughout the banner -- this is usually the case on maemo
663          *
664          * FIXME: Pango >= 1.20 has pango_layout_set_height().
665          */
666         height = (PANGO_PIXELS (logical.height) * 3) / lines + 1;
667     }
668
669     /* Set the new width/height if applicable */
670     gtk_widget_set_size_request (GTK_WIDGET (label), width, height);
671 }
672
673 static void
674 screen_size_changed                            (GdkScreen *screen,
675                                                 GtkWindow *banner)
676
677 {
678     hildon_banner_bind_style (HILDON_BANNER (banner));
679     gtk_window_reshow_with_initial_size (banner);
680     force_to_wrap_truncated (HILDON_BANNER (banner));
681 }
682
683 static void
684 hildon_banner_realize                           (GtkWidget *widget)
685 {
686     GdkWindow *gdkwin;
687     GdkScreen *screen;
688     GdkAtom atom;
689     guint32 portrait = 1;
690     const gchar *notification_type = "_HILDON_NOTIFICATION_TYPE_BANNER";
691     HildonBannerPrivate *priv = HILDON_BANNER_GET_PRIVATE (widget);
692     g_assert (priv);
693
694     /* We let the parent to init widget->window before we need it */
695     if (GTK_WIDGET_CLASS (hildon_banner_parent_class)->realize)
696         GTK_WIDGET_CLASS (hildon_banner_parent_class)->realize (widget);
697
698     /* We use special hint to turn the banner into information notification. */
699     gdk_window_set_type_hint (widget->window, GDK_WINDOW_TYPE_HINT_NOTIFICATION);
700     gtk_window_set_transient_for (GTK_WINDOW (widget), (GtkWindow *) priv->parent);
701
702     gdkwin = widget->window;
703
704     /* Set the _HILDON_NOTIFICATION_TYPE property so Matchbox places the window correctly */
705     atom = gdk_atom_intern ("_HILDON_NOTIFICATION_TYPE", FALSE);
706     gdk_property_change (gdkwin, atom, gdk_x11_xatom_to_atom (XA_STRING), 8, GDK_PROP_MODE_REPLACE,
707                          (gpointer) notification_type, strlen (notification_type));
708
709     /* HildonBanner supports portrait mode */
710     atom = gdk_atom_intern ("_HILDON_PORTRAIT_MODE_SUPPORT", FALSE);
711     gdk_property_change (gdkwin, atom, gdk_x11_xatom_to_atom (XA_CARDINAL), 32,
712                          GDK_PROP_MODE_REPLACE, (gpointer) &portrait, 1);
713
714     /* Manage override flag */
715     if ((priv->require_override_dnd)&&(!priv->overrides_dnd)) {
716       hildon_banner_set_override_flag (HILDON_BANNER (widget));
717         priv->overrides_dnd = TRUE;
718     }
719
720     screen = gtk_widget_get_screen (widget);
721     g_signal_connect (screen, "size-changed", G_CALLBACK (screen_size_changed), widget);
722 }
723
724 static void
725 hildon_banner_unrealize                         (GtkWidget *widget)
726 {
727     GdkScreen *screen = gtk_widget_get_screen (widget);
728     g_signal_handlers_disconnect_by_func (screen, G_CALLBACK (screen_size_changed), widget);
729
730     GTK_WIDGET_CLASS (hildon_banner_parent_class)->unrealize (widget);
731 }
732
733 static void 
734 hildon_banner_class_init                        (HildonBannerClass *klass)
735 {
736     GObjectClass *object_class;
737     GtkWidgetClass *widget_class;
738
739     object_class = G_OBJECT_CLASS (klass);
740     widget_class = GTK_WIDGET_CLASS (klass);
741
742     /* Append private structure to class. This is more elegant than
743        on g_new based approach */
744     g_type_class_add_private (klass, sizeof (HildonBannerPrivate));
745
746     /* Override virtual methods */
747     object_class->constructor = hildon_banner_constructor;
748     object_class->finalize = hildon_banner_finalize;
749     object_class->set_property = hildon_banner_set_property;
750     object_class->get_property = hildon_banner_get_property;
751     GTK_OBJECT_CLASS (klass)->destroy = hildon_banner_destroy;
752     widget_class->size_request = hildon_banner_size_request;
753     widget_class->map_event = hildon_banner_map_event;
754     widget_class->realize = hildon_banner_realize;
755     widget_class->unrealize = hildon_banner_unrealize;
756     widget_class->button_press_event = hildon_banner_button_press_event;
757 #if defined(MAEMO_GTK)
758     widget_class->map = hildon_banner_map;
759 #endif
760
761     /* Install properties.
762        We need construct properties for singleton purposes */
763
764     /**
765      * HildonBanner:parent-window:
766      *
767      * The window for which the banner will be singleton. 
768      *                      
769      */
770     g_object_class_install_property (object_class, PROP_PARENT_WINDOW,
771             g_param_spec_object ("parent-window",
772                 "Parent window",
773                 "The window for which the banner will be singleton",
774                 GTK_TYPE_WINDOW, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
775
776     /**
777      * HildonBanner:is-timed:
778      *
779      * Whether the banner is timed and goes away automatically.
780      *                      
781      */
782     g_object_class_install_property (object_class, PROP_IS_TIMED,
783             g_param_spec_boolean ("is-timed",
784                 "Is timed",
785                 "Whether or not the notification goes away automatically "
786                 "after the specified time has passed",
787                 FALSE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
788
789     /**
790      * HildonBanner:timeout:
791      *
792      * The time before destroying the banner. This needs
793      * to be adjusted before the banner is mapped to the screen.
794      *                      
795      */
796     g_object_class_install_property (object_class, PROP_TIMEOUT,
797             g_param_spec_uint ("timeout",
798                 "Timeout",
799                 "The time before making the banner banner go away",
800                 0,
801                 10000,
802                 HILDON_BANNER_DEFAULT_TIMEOUT,
803                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
804 }
805
806 static void 
807 hildon_banner_init                              (HildonBanner *self)
808 {
809     HildonBannerPrivate *priv = HILDON_BANNER_GET_PRIVATE (self);
810     g_assert (priv);
811
812     priv->parent = NULL;
813     priv->overrides_dnd = FALSE;
814     priv->require_override_dnd = FALSE;
815     priv->name_suffix = NULL;
816
817     /* Initialize the common layout inside banner */
818     priv->alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
819     priv->layout = gtk_hbox_new (FALSE, HILDON_MARGIN_DEFAULT);
820
821     priv->label = g_object_new (GTK_TYPE_LABEL, NULL);
822     gtk_label_set_line_wrap (GTK_LABEL (priv->label), TRUE);
823     gtk_label_set_line_wrap_mode (GTK_LABEL (priv->label), PANGO_WRAP_WORD_CHAR);
824     gtk_label_set_justify (GTK_LABEL (priv->label), GTK_JUSTIFY_CENTER);
825
826     gtk_container_set_border_width (GTK_CONTAINER (priv->layout), HILDON_MARGIN_DEFAULT);
827     gtk_container_add (GTK_CONTAINER (self), priv->alignment);
828     gtk_container_add (GTK_CONTAINER (priv->alignment), priv->layout);
829     gtk_box_pack_start (GTK_BOX (priv->layout), priv->label, FALSE, FALSE, 0);
830
831     gtk_window_set_accept_focus (GTK_WINDOW (self), FALSE);
832
833     gtk_widget_add_events (GTK_WIDGET (self), GDK_BUTTON_PRESS_MASK);
834 }
835
836 /* Makes sure that icon/progress item contains the desired type
837    of item. If possible, tries to avoid creating a new widget but
838    reuses the existing one */
839 static void
840 hildon_banner_ensure_child                      (HildonBanner *self, 
841                                                  GtkWidget *user_widget,
842                                                  guint pos,
843                                                  GType type,
844                                                  const gchar *first_property, 
845                                                  ...)
846 {
847     GtkWidget *widget;
848     va_list args;
849     HildonBannerPrivate *priv = HILDON_BANNER_GET_PRIVATE (self);
850
851     g_assert (priv);
852
853     widget = priv->main_item;
854     va_start (args, first_property);
855
856     /* Reuse existing widget if possible */
857     if (! user_widget && G_TYPE_CHECK_INSTANCE_TYPE (widget, type))
858     {
859         g_object_set_valist (G_OBJECT (widget), first_property, args);
860     }
861     else
862     {
863         /* We have to abandon old content widget */
864         if (widget)
865             gtk_container_remove (GTK_CONTAINER (priv->layout), widget);
866         
867         /* Use user provided widget or create a new one */
868         priv->main_item = widget = user_widget ? 
869             user_widget : GTK_WIDGET (g_object_new_valist(type, first_property, args));
870         gtk_box_pack_start (GTK_BOX (priv->layout), widget, FALSE, FALSE, 0);
871     }
872
873     /* We make sure that the widget exists in desired position. Different
874        banners place this child widget to different places */
875     gtk_box_reorder_child (GTK_BOX (priv->layout), widget, pos);
876     va_end (args);
877 }
878
879 /* Creates a new banner instance or uses an existing one */
880 static HildonBanner*
881 hildon_banner_get_instance_for_widget           (GtkWidget *widget, 
882                                                  gboolean timed)
883 {
884     GtkWidget *window;
885
886     window = widget ? gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW) : NULL;
887     return g_object_new (HILDON_TYPE_BANNER, "parent-window", window, "is-timed", timed, NULL);
888 }
889
890 /**
891  * hildon_banner_show_information:
892  * @widget: the #GtkWidget that is the owner of the banner
893  * @icon_name: since Hildon 2.2 this parameter is not used anymore and
894  * any value that you pass will be ignored
895  * @text: Text to display
896  *
897  * This function creates and displays an information banner that is
898  * automatically destroyed after a certain time period (see
899  * hildon_banner_set_timeout()). For each window in your application
900  * there can only be one timed banner, so if you spawn a new banner
901  * before the earlier one has timed out, the previous one will be
902  * replaced.
903  *
904  * Returns: The newly created banner
905  *
906  */
907 GtkWidget*
908 hildon_banner_show_information                  (GtkWidget *widget, 
909                                                  const gchar *icon_name,
910                                                  const gchar *text)
911 {
912     return hildon_banner_real_show_information (widget, text, FALSE);
913 }
914
915 /**
916  * hildon_banner_show_information_override_dnd:
917  * @widget: the #GtkWidget that is the owner of the banner
918  * @text: Text to display
919  *
920  * Equivalent to hildon_banner_show_information(), but overriding the
921  * "do not disturb" flag.
922  *
923  * Returns: The newly created banner
924  *
925  * Since: 2.2
926  *
927  */
928 GtkWidget*
929 hildon_banner_show_information_override_dnd     (GtkWidget *widget,
930                                                  const gchar *text)
931 {
932     return hildon_banner_real_show_information (widget, text, TRUE);
933 }
934
935 static void
936 hildon_banner_set_override_flag                 (HildonBanner *banner)
937 {
938     guint32 state = 1;
939
940     gdk_property_change (GTK_WIDGET (banner)->window,
941                          gdk_atom_intern_static_string ("_HILDON_DO_NOT_DISTURB_OVERRIDE"),
942                          gdk_x11_xatom_to_atom (XA_INTEGER),
943                          32,
944                          GDK_PROP_MODE_REPLACE,
945                          (const guchar*) &state,
946                          1);
947 }
948
949 static void
950 reshow_banner                                   (HildonBanner *banner)
951 {
952     if (GTK_WIDGET_VISIBLE (banner)) {
953         gint width = gdk_screen_get_width (gtk_widget_get_screen (GTK_WIDGET (banner)));
954         gtk_window_resize (GTK_WINDOW (banner), width, 1);
955     }
956     force_to_wrap_truncated (banner);
957     gtk_widget_show_all (GTK_WIDGET (banner));
958 }
959
960 static GtkWidget*
961 hildon_banner_real_show_information             (GtkWidget *widget,
962                                                  const gchar *text,
963                                                  gboolean override_dnd)
964 {
965     HildonBanner *banner;
966     HildonBannerPrivate *priv = NULL;
967
968     g_return_val_if_fail (text != NULL, NULL);
969
970     /* Prepare banner */
971     banner = hildon_banner_get_instance_for_widget (widget, TRUE);
972     priv = HILDON_BANNER_GET_PRIVATE (banner);
973
974     priv->name_suffix = "information";
975     banner_do_set_text (banner, text, FALSE);
976     hildon_banner_bind_style (banner);
977
978     if (override_dnd) {
979       /* so on the realize it will set the property */
980       priv->require_override_dnd = TRUE;
981     }
982
983     /* Show the banner, since caller cannot do that */
984     reshow_banner (banner);
985
986     return GTK_WIDGET (banner);
987 }
988
989 /**
990  * hildon_banner_show_informationf:
991  * @widget: the #GtkWidget that is the owner of the banner
992  * @icon_name: since Hildon 2.2 this parameter is not used anymore and
993  * any value that you pass will be ignored
994  * @format: a printf-like format string
995  * @Varargs: arguments for the format string
996  *
997  * A helper function for hildon_banner_show_information() with
998  * string formatting.
999  *
1000  * Returns: the newly created banner
1001  */
1002 GtkWidget* 
1003 hildon_banner_show_informationf                 (GtkWidget *widget, 
1004                                                  const gchar *icon_name,
1005                                                  const gchar *format, 
1006                                                  ...)
1007 {
1008     g_return_val_if_fail (format != NULL, NULL);
1009
1010     gchar *message;
1011     va_list args;
1012     GtkWidget *banner;
1013
1014     va_start (args, format);
1015     message = g_strdup_vprintf (format, args);
1016     va_end (args);
1017
1018     banner = hildon_banner_show_information (widget, icon_name, message);
1019
1020     g_free (message);
1021
1022     return banner;
1023 }
1024
1025 /**
1026  * hildon_banner_show_information_with_markup:
1027  * @widget: the #GtkWidget that wants to display banner
1028  * @icon_name: since Hildon 2.2 this parameter is not used anymore and
1029  * any value that you pass will be ignored
1030  * @markup: a markup string to display (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
1031  *
1032  * This function creates and displays an information banner that is
1033  * automatically destroyed after certain time period (see
1034  * hildon_banner_set_timeout()). For each window in your application
1035  * there can only be one timed banner, so if you spawn a new banner
1036  * before the earlier one has timed out, the previous one will be
1037  * replaced.
1038  *
1039  * Returns: the newly created banner
1040  *
1041  */
1042 GtkWidget*
1043 hildon_banner_show_information_with_markup      (GtkWidget *widget, 
1044                                                  const gchar *icon_name, 
1045                                                  const gchar *markup)
1046 {
1047     HildonBanner *banner;
1048     HildonBannerPrivate *priv;
1049
1050     g_return_val_if_fail (icon_name == NULL || icon_name[0] != 0, NULL);
1051     g_return_val_if_fail (markup != NULL, NULL);
1052
1053     /* Prepare banner */
1054     banner = hildon_banner_get_instance_for_widget (widget, TRUE);
1055     priv = HILDON_BANNER_GET_PRIVATE (banner);
1056
1057     priv->name_suffix = "information";
1058     banner_do_set_text (banner, markup, TRUE);
1059     hildon_banner_bind_style (banner);
1060
1061     /* Show the banner, since caller cannot do that */
1062     reshow_banner (banner);
1063
1064     return (GtkWidget *) banner;
1065 }
1066
1067 /**
1068  * hildon_banner_show_animation:
1069  * @widget: the #GtkWidget that wants to display banner
1070  * @animation_name: since Hildon 2.2 this parameter is not used
1071  *                  anymore and any value that you pass will be
1072  *                  ignored
1073  * @text: the text to display.
1074  *
1075  * Shows an animated progress notification. It's recommended not to try
1076  * to show more than one progress notification at a time, since
1077  * they will appear on top of each other. You can use progress
1078  * notifications with timed banners. In this case the banners are
1079  * located so that you can somehow see both.
1080  *
1081  * Unlike information banners (created with
1082  * hildon_banner_show_information()), animation banners are not
1083  * destroyed automatically using a timeout. You have to destroy them
1084  * yourself.
1085  *
1086  * Please note also that these banners are destroyed automatically if the
1087  * window they are attached to is closed. The pointer that you receive
1088  * with this function does not contain additional references, so it
1089  * can become invalid without warning (this is true for all toplevel
1090  * windows in gtk). To make sure that the banner does not disappear
1091  * automatically, you can separately ref the return value (this
1092  * doesn't prevent the banner from disappearing, just the object from
1093  * being finalized). In this case you have to call
1094  * gtk_widget_destroy() followed by g_object_unref().
1095  *
1096  * Returns: a #HildonBanner widget. You must call gtk_widget_destroy()
1097  *          once you are done with the banner.
1098  *
1099  * Deprecated: Hildon 2.2: use
1100  * hildon_gtk_window_set_progress_indicator() instead.
1101  */
1102 GtkWidget*
1103 hildon_banner_show_animation                    (GtkWidget *widget, 
1104                                                  const gchar *animation_name, 
1105                                                  const gchar *text)
1106 {
1107     HildonBanner *banner;
1108     GtkWidget *image_widget;
1109     HildonBannerPrivate *priv;
1110
1111     g_return_val_if_fail (text != NULL, NULL);
1112
1113     image_widget = hildon_private_create_animation (
1114         HILDON_BANNER_ANIMATION_FRAMERATE,
1115         HILDON_BANNER_ANIMATION_TMPL,
1116         HILDON_BANNER_ANIMATION_NFRAMES);
1117
1118     /* Prepare banner */
1119     banner = hildon_banner_get_instance_for_widget (widget, FALSE);
1120     hildon_banner_ensure_child (banner, image_widget, 0,
1121             GTK_TYPE_IMAGE, "yalign", 0.0, NULL);
1122
1123     priv = HILDON_BANNER_GET_PRIVATE (banner);
1124     priv->name_suffix = "animation";
1125     banner_do_set_text (banner, text, FALSE);
1126     hildon_banner_bind_style (banner);
1127
1128     /* And show it */
1129     reshow_banner (banner);
1130
1131     return (GtkWidget *) banner;
1132 }
1133
1134 /**
1135  * hildon_banner_show_progress:
1136  * @widget: the #GtkWidget that wants to display banner
1137  * @bar: since Hildon 2.2 this parameter is not used anymore and
1138  * any value that you pass will be ignored
1139  * @text: text to display.
1140  *
1141  * Shows progress notification. See hildon_banner_show_animation()
1142  * for more information.
1143  * 
1144  * Returns: a #HildonBanner widget. You must call #gtk_widget_destroy
1145  *          once you are done with the banner.
1146  *
1147  * Deprecated: Hildon 2.2: use hildon_gtk_window_set_progress_indicator() instead.
1148  */
1149 GtkWidget*
1150 hildon_banner_show_progress                     (GtkWidget *widget, 
1151                                                  GtkProgressBar *bar, 
1152                                                  const gchar *text)
1153 {
1154     HildonBanner *banner;
1155     HildonBannerPrivate *priv;
1156
1157     g_return_val_if_fail (text != NULL, NULL);
1158
1159     /* Prepare banner */
1160     banner = hildon_banner_get_instance_for_widget (widget, FALSE);
1161     priv = HILDON_BANNER_GET_PRIVATE (banner);
1162     g_assert (priv);
1163
1164     priv->name_suffix = "progress";
1165     banner_do_set_text (banner, text, FALSE);
1166     hildon_banner_bind_style (banner);
1167
1168     if (priv->parent)
1169         hildon_gtk_window_set_progress_indicator (priv->parent, 1);
1170
1171     /* Show the banner */
1172     reshow_banner (banner);
1173
1174     return GTK_WIDGET (banner);   
1175 }
1176
1177 /**
1178  * hildon_banner_set_text:
1179  * @self: a #HildonBanner widget
1180  * @text: a new text to display in banner
1181  *
1182  * Sets the text that is displayed in the banner.
1183  *
1184  */
1185 void 
1186 hildon_banner_set_text                          (HildonBanner *self, 
1187                                                  const gchar *text)
1188 {
1189     g_return_if_fail (HILDON_IS_BANNER (self));
1190
1191     banner_do_set_text (self, text, FALSE);
1192
1193     if (GTK_WIDGET_VISIBLE (self))
1194         reshow_banner (self);
1195 }
1196
1197 /**
1198  * hildon_banner_set_markup:
1199  * @self: a #HildonBanner widget
1200  * @markup: a new text with Pango markup to display in the banner
1201  *
1202  * Sets the text with markup that is displayed in the banner.
1203  *
1204  */
1205 void 
1206 hildon_banner_set_markup                        (HildonBanner *self, 
1207                                                  const gchar *markup)
1208 {
1209     g_return_if_fail (HILDON_IS_BANNER (self));
1210
1211     banner_do_set_text (self, markup, TRUE);
1212
1213     if (GTK_WIDGET_VISIBLE (self))
1214         reshow_banner (self);
1215 }
1216
1217 /**
1218  * hildon_banner_set_fraction:
1219  * @self: a #HildonBanner widget
1220  * @fraction: #gdouble
1221  *
1222  * The fraction is the completion of progressbar, 
1223  * the scale is from 0.0 to 1.0.
1224  * Sets the amount of fraction the progressbar has.
1225  *
1226  * Note that this method only has effect if @self was created with
1227  * hildon_banner_show_progress()
1228  *
1229  * Deprecated: This function does nothing. As of Hildon 2.2, hildon
1230  * banners don't have progress bars.
1231  */
1232 void 
1233 hildon_banner_set_fraction                      (HildonBanner *self, 
1234                                                  gdouble fraction)
1235 {
1236 }
1237
1238 /**
1239  * hildon_banner_set_timeout:
1240  * @self: a #HildonBanner widget
1241  * @timeout: timeout to set in miliseconds.
1242  *
1243  * Sets the timeout on the banner. After the given amount of miliseconds
1244  * has elapsed the banner will be destroyed. Setting this only makes
1245  * sense on banners that are timed and that have not been yet displayed
1246  * on the screen.
1247  *
1248  * Note that this method only has effect if @self is an information
1249  * banner (created using hildon_banner_show_information() and
1250  * friends).
1251  */
1252 void
1253 hildon_banner_set_timeout                       (HildonBanner *self,
1254                                                  guint timeout)
1255 {
1256     HildonBannerPrivate *priv;
1257
1258     g_return_if_fail (HILDON_IS_BANNER (self));
1259     priv = HILDON_BANNER_GET_PRIVATE (self);
1260     g_assert (priv);
1261
1262     priv->timeout = timeout;
1263 }
1264
1265 /**
1266  * hildon_banner_set_icon:
1267  * @self: a #HildonBanner widget
1268  * @icon_name: the name of icon to use. Can be %NULL for default icon
1269  *
1270  * Sets the icon to be used in the banner.
1271  *
1272  * Deprecated: This function does nothing. As of hildon 2.2, hildon
1273  * banners don't allow changing their icons.
1274  */
1275 void 
1276 hildon_banner_set_icon                          (HildonBanner *self, 
1277                                                  const gchar  *icon_name)
1278 {
1279 }
1280
1281 /**
1282  * hildon_banner_set_icon_from_file:
1283  * @self: a #HildonBanner widget
1284  * @icon_file: the filename of icon to use. Can be %NULL for default icon
1285  *
1286  * Sets the icon from its filename to be used in the banner.
1287  *
1288  * Deprecated: This function does nothing. As of hildon 2.2, hildon
1289  * banners don't allow changing their icons.
1290  */
1291 void 
1292 hildon_banner_set_icon_from_file                (HildonBanner *self, 
1293                                                  const gchar  *icon_file)
1294 {
1295 }