c513426e5e08e03c016f30dafeec26ad01eb6cfd
[hildon] / src / hildon-note.c
1 /*
2  * This file is a part of hildon
3  *
4  * Copyright (C) 2005, 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 (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-note
27  * @short_description: A widget to ask confirmation from the user.
28  *
29  * Notes are used to for confirmation (OK/Cancel/etc.) from the user.
30  * A simple note contains an information text. Additional features
31  * such as progress bars or animation can also be included.
32  * 
33  * <example>
34  * <title>HildonNote example</title>
35  * <programlisting>
36  * <!-- -->
37  * hildon_note_new_confirmation (window, "Confirmation message...");
38  * <!-- -->
39  * i = gtk_dialog_run (GTK_DIALOG (note));
40  * gtk_widget_destroy (GTK_WIDGET (note));
41  * <!-- -->
42  * if (i == GTK_RESPONSE_OK)
43  *      gtk_infoprint (window, "User pressed 'OK' button'");
44  * else
45  *      gtk_infoprint (window, "User pressed 'Cancel' button" );
46  * </programlisting>
47  * </example>
48  */
49
50 #ifdef                                          HAVE_CONFIG_H
51 #include                                        <config.h>
52 #endif
53
54 #include                                        "hildon-note.h"
55 #include                                        <gtk/gtklabel.h>
56 #include                                        <gtk/gtkimage.h>
57 #include                                        <gtk/gtkhbox.h>
58 #include                                        <gtk/gtkalignment.h>
59 #include                                        <gtk/gtkvbox.h>
60 #include                                        <gtk/gtkbutton.h>
61 #include                                        <libintl.h>
62 #include                                        "hildon-defines.h"
63 #include                                        "hildon-sound.h"
64 #include                                        "hildon-banner.h" 
65 #include                                        "hildon-enum-types.h"
66 #include                                        <stdio.h>
67 #include                                        <string.h>
68 #include                                        <X11/X.h>
69 #include                                        <X11/Xatom.h>
70 #include                                        "hildon-note-private.h"
71
72 #define                                         CONFIRMATION_SOUND_PATH \
73                                                 "/usr/share/sounds/ui-confirmation_note.wav"
74
75 #define                                         INFORMATION_SOUND_PATH \
76                                                 "/usr/share/sounds/ui-information_note.wav"
77
78 #define                                         HILDON_NOTE_CONFIRMATION_ICON \
79                                                 "qgn_note_confirm"
80
81 #define                                         HILDON_NOTE_INFORMATION_ICON \
82                                                 "qgn_note_info"
83
84 #define                                         _(String) dgettext("hildon-libs", String)
85
86 static void 
87 hildon_note_class_init                          (HildonNoteClass *class);
88
89 static void
90 hildon_note_init                                (HildonNote *dialog);
91
92 static void 
93 hildon_note_rebuild                             (HildonNote *note);
94
95 static void
96 hildon_note_finalize                            (GObject *obj_self);
97
98 static gboolean
99 hildon_note_button_release                      (GtkWidget *widget,
100                                                  GdkEventButton *event);
101
102 static void
103 hildon_note_map                                 (GtkWidget *widget);
104
105 static void
106 hildon_note_unmap                               (GtkWidget *widget);
107
108 static void
109 hildon_note_realize                             (GtkWidget *widget);
110
111 static void 
112 hildon_note_set_property                        (GObject *object,
113                                                  guint prop_id,
114                                                  const GValue *value,
115                                                  GParamSpec *pspec);
116
117 static void
118 hildon_note_get_property                        (GObject *object,
119                                                  guint prop_id,
120                                                  GValue *value, 
121                                                  GParamSpec *pspec);
122
123 static gboolean
124 sound_handling                                  (GtkWidget *widget, 
125                                                  GdkEventExpose *event, 
126                                                  gpointer data);
127
128 enum 
129 {
130     PROP_0,
131     PROP_HILDON_NOTE_TYPE,
132     PROP_HILDON_NOTE_DESCRIPTION,
133     PROP_HILDON_NOTE_ICON,
134     PROP_HILDON_NOTE_PROGRESSBAR,
135     PROP_HILDON_NOTE_STOCK_ICON
136 };
137
138 static GtkDialogClass*                          parent_class;
139
140 static GdkWindow *
141 grab_transfer_window_get                        (GtkWidget *widget)
142 {
143     GdkWindow *window;
144     GdkWindowAttr attributes;
145     gint attributes_mask;
146
147     attributes.x = 0;
148     attributes.y = 0;
149     attributes.width = 10;
150     attributes.height = 10;
151     attributes.window_type = GDK_WINDOW_TEMP;
152     attributes.wclass = GDK_INPUT_ONLY;
153     attributes.override_redirect = TRUE;
154     attributes.event_mask = 0;
155
156     attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
157
158     window = gdk_window_new (gtk_widget_get_root_window (widget),
159                              &attributes, attributes_mask);
160     gdk_window_set_user_data (window, widget);
161
162     gdk_window_show (window);
163
164     return window;
165 }
166
167 static void
168 hildon_note_set_property                        (GObject *object,
169                                                  guint prop_id,
170                                                  const GValue *value, 
171                                                  GParamSpec * pspec)
172 {
173     HildonNote *note = HILDON_NOTE (object);
174     HildonNotePrivate *priv;
175     GtkWidget *widget;
176
177     priv = HILDON_NOTE_GET_PRIVATE (note);
178     g_assert (priv);
179
180     switch (prop_id) {
181
182         case PROP_HILDON_NOTE_TYPE:
183             priv->note_n = g_value_get_enum (value);
184             hildon_note_rebuild (note);
185             break;
186
187         case PROP_HILDON_NOTE_DESCRIPTION:
188             if (priv->original_description) 
189                     g_free (priv->original_description);
190             priv->original_description = g_value_dup_string (value);
191
192             gtk_label_set_text (GTK_LABEL (priv->label), priv->original_description);
193             /* FIXME Is the "original_description" used anywhere? */
194             
195             break;
196
197         case PROP_HILDON_NOTE_ICON:
198             gtk_image_set_from_icon_name (GTK_IMAGE (priv->icon), 
199                     g_value_get_string(value), HILDON_ICON_SIZE_BIG_NOTE);
200             break;
201
202         case PROP_HILDON_NOTE_STOCK_ICON:
203             gtk_image_set_from_stock (GTK_IMAGE (priv->icon), 
204                     g_value_get_string (value), HILDON_ICON_SIZE_BIG_NOTE);
205             break;
206
207         case PROP_HILDON_NOTE_PROGRESSBAR:
208             widget = g_value_get_object (value);
209             if (widget != priv->progressbar)
210             {
211                 if (priv->progressbar)
212                     g_object_unref (priv->progressbar);
213
214                 priv->progressbar = widget;
215
216                 if (widget)
217                 {
218                     g_object_ref (widget);
219                     gtk_object_sink (GTK_OBJECT (widget));
220                 }
221
222                 hildon_note_rebuild (note);
223             }
224             break;
225
226         default:
227             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
228             break;
229     }
230 }
231
232 static void
233 hildon_note_get_property                        (GObject *object,
234                                                  guint prop_id, 
235                                                  GValue *value, 
236                                                  GParamSpec *pspec)
237 {
238     HildonNote *note = HILDON_NOTE (object);
239     HildonNotePrivate *priv;
240
241     priv = HILDON_NOTE_GET_PRIVATE (note);
242
243     switch (prop_id) {
244
245         case PROP_HILDON_NOTE_TYPE:
246             g_value_set_enum (value, priv->note_n);
247             break;
248
249         case PROP_HILDON_NOTE_DESCRIPTION:
250             g_value_set_string (value, priv->original_description);
251             break;
252
253         case PROP_HILDON_NOTE_ICON:
254             g_object_get_property (G_OBJECT (priv->icon), "icon-name", value);
255             break;
256
257         case PROP_HILDON_NOTE_STOCK_ICON:
258             g_object_get_property (G_OBJECT (priv->icon), "stock", value);
259             break;
260
261         case PROP_HILDON_NOTE_PROGRESSBAR:
262             g_value_set_object (value, priv->progressbar);
263             break;
264
265         default:
266             G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
267             break;
268     }
269 }
270
271 /**
272  * hildon_note_get_type:
273  *
274  * Returns GType for HildonNote.
275  *
276  * Returns: HildonNote type
277  */
278 GType G_GNUC_CONST
279 hildon_note_get_type                            (void)
280 {
281     static GType dialog_type = 0;
282
283     if (! dialog_type) {
284         static const GTypeInfo dialog_info = {
285             sizeof(HildonNoteClass),
286             NULL,       /* base_init */
287             NULL,       /* base_finalize */
288             (GClassInitFunc) hildon_note_class_init,
289             NULL,       /* class_finalize */
290             NULL,       /* class_data */
291             sizeof(HildonNote),
292             0,  /* n_preallocs */
293             (GInstanceInitFunc) hildon_note_init
294         };
295         dialog_type = g_type_register_static (GTK_TYPE_DIALOG,
296                 "HildonNote",
297                 &dialog_info, 0);
298     }
299     return dialog_type;
300 }
301
302 static void 
303 hildon_note_class_init                          (HildonNoteClass *class)
304 {
305     GObjectClass *object_class = G_OBJECT_CLASS (class);
306     GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
307
308     /* set the global parent_class */
309     parent_class = g_type_class_peek_parent (class);
310
311     g_type_class_add_private (class, sizeof (HildonNotePrivate));
312
313     object_class->finalize      = hildon_note_finalize;
314     object_class->set_property  = hildon_note_set_property;
315     object_class->get_property  = hildon_note_get_property;
316     widget_class->button_release_event = hildon_note_button_release;
317     widget_class->map           = hildon_note_map;
318     widget_class->unmap         = hildon_note_unmap;
319     widget_class->realize       = hildon_note_realize;
320
321     g_object_class_install_property (object_class,
322             PROP_HILDON_NOTE_TYPE,
323             g_param_spec_enum ("note-type",
324                 "note type",
325                 "The type of the note dialog",
326                 hildon_note_type_get_type (),
327                 HILDON_NOTE_TYPE_CONFIRMATION,
328                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
329
330     /**
331      * HildonNote:description:
332      *
333      * Description for the note.
334      */
335     g_object_class_install_property (object_class,
336             PROP_HILDON_NOTE_DESCRIPTION,
337             g_param_spec_string ("description",
338                 "note description",
339                 "The text that appears in the note dialog",
340                 "",
341                 G_PARAM_READWRITE));
342
343     /**
344      * HildonNote:icon:
345      *
346      * Icon for the note.
347      */
348     g_object_class_install_property (object_class,
349             PROP_HILDON_NOTE_ICON,
350             g_param_spec_string ("icon",
351                 "note icon",
352                 "The name of the icon that appears in the note dialog",
353                 "",
354                 G_PARAM_READWRITE));
355
356     /**
357      * HildonNote:stock-icon:
358      *
359      * Stock icon name for the note.
360      */
361     g_object_class_install_property (object_class,
362             PROP_HILDON_NOTE_STOCK_ICON,
363             g_param_spec_string ("stock-icon",
364                 "Stock note icon",
365                 "The stock name of the icon that appears in the note dialog",
366                 "",
367                 G_PARAM_READWRITE));
368
369     /**
370      * HildonNote:progressbar:
371      *
372      * Progressbar for the note (if any).
373      */
374     g_object_class_install_property (object_class,
375             PROP_HILDON_NOTE_PROGRESSBAR,
376             g_param_spec_object ("progressbar",
377                 "Progressbar widget",
378                 "The progressbar that appears in the note dialog",
379                 GTK_TYPE_PROGRESS_BAR,
380                 G_PARAM_READWRITE));
381 }
382
383 static void 
384 hildon_note_init                                (HildonNote *dialog)
385 {
386     HildonNotePrivate *priv = HILDON_NOTE_GET_PRIVATE (dialog);
387     g_assert (priv);
388
389     priv->label = gtk_label_new (NULL);
390     gtk_label_set_line_wrap (GTK_LABEL (priv->label), TRUE);
391     
392     priv->icon  = gtk_image_new ();
393     priv->close_if_pressed_outside = FALSE;
394     priv->transfer_window = NULL;
395
396     /* Acquire real references to our internal children, since
397        they are not nessecarily packed into container in each
398        layout */
399     g_object_ref_sink (priv->label);
400     g_object_ref_sink (priv->icon);
401
402     gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
403     gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
404 }
405
406
407 static void 
408 hildon_note_finalize                            (GObject *obj_self)
409 {
410     HildonNotePrivate *priv = HILDON_NOTE_GET_PRIVATE (obj_self);
411     g_assert (priv);
412
413     /* FIXME Some of this stuff should be moved to dispose */
414
415     /* Free internal data */
416     if (priv->label)
417         g_object_unref (priv->label);
418
419     if (priv->icon)
420         g_object_unref (priv->icon);
421
422     if (priv->progressbar)
423         g_object_unref (priv->progressbar);
424
425     if (priv->original_description)
426         g_free (priv->original_description);
427
428     G_OBJECT_CLASS (parent_class)->finalize (obj_self);
429 }
430
431
432 static gboolean
433 hildon_note_button_release                      (GtkWidget *widget,
434                                                  GdkEventButton *event)
435 {
436     int x, y;
437     gboolean released_outside;
438     HildonNotePrivate *priv = HILDON_NOTE_GET_PRIVATE (widget);
439
440     gdk_window_get_position (widget->window, &x, &y);
441
442     /* Whether the button has been released outside the widget */
443     released_outside = (event->window != priv->transfer_window ||
444                         event->x < x || event->x > x + widget->allocation.width ||
445                         event->y < y || event->y > y + widget->allocation.height);
446
447     if (released_outside && priv->close_if_pressed_outside) {
448         gtk_dialog_response (GTK_DIALOG (widget), GTK_RESPONSE_CANCEL);
449     }
450
451     if (GTK_WIDGET_CLASS (parent_class)->button_release_event) {
452         return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
453     } else {
454         return FALSE;
455     }
456 }
457
458 static void
459 hildon_note_map                                 (GtkWidget *widget)
460 {
461     HildonNotePrivate *priv = HILDON_NOTE_GET_PRIVATE (widget);
462     g_assert (priv);
463
464     /* Map the window */
465     GTK_WIDGET_CLASS (parent_class)->map (widget);
466
467     if (priv->transfer_window == NULL && priv->close_if_pressed_outside) {
468         gboolean has_grab = FALSE;
469
470         priv->transfer_window = grab_transfer_window_get (widget);
471
472         if (gdk_pointer_grab (priv->transfer_window, TRUE,
473                               GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
474                               GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
475                               GDK_POINTER_MOTION_MASK, NULL, NULL,
476                               GDK_CURRENT_TIME) == GDK_GRAB_SUCCESS) {
477             if (gdk_keyboard_grab (priv->transfer_window, TRUE,
478                                    GDK_CURRENT_TIME) == GDK_GRAB_SUCCESS) {
479                 has_grab = TRUE;
480             } else {
481                 gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
482                                             GDK_CURRENT_TIME);
483             }
484         }
485
486         if (has_grab) {
487             gtk_grab_add (widget);
488         } else {
489             gdk_window_destroy (priv->transfer_window);
490             priv->transfer_window = NULL;
491         }
492     }
493 }
494
495 static void
496 hildon_note_unmap                               (GtkWidget *widget)
497 {
498     HildonNotePrivate *priv = HILDON_NOTE_GET_PRIVATE (widget);
499     g_assert (priv);
500
501     if (priv->transfer_window != NULL) {
502         /* Remove the grab */
503         gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
504                                     GDK_CURRENT_TIME);
505         gtk_grab_remove (widget);
506
507         /* Destroy the transfer window */
508         gdk_window_destroy (priv->transfer_window);
509         priv->transfer_window = NULL;
510     }
511 }
512
513 static void
514 hildon_note_realize                             (GtkWidget *widget)
515 {
516     GdkDisplay *display;
517     Atom atom;
518     const gchar *notification_type;
519     HildonNotePrivate *priv = HILDON_NOTE_GET_PRIVATE (widget);
520     g_assert (priv);
521
522     /* Make widget->window accessible */
523     GTK_WIDGET_CLASS (parent_class)->realize (widget);
524
525     /* Border only, no titlebar */
526     gdk_window_set_decorations (widget->window, GDK_DECOR_BORDER);
527
528     /* Because ESD is synchronous, we wish to play sound after the
529        note is already on screen to avoid blocking its appearance */
530     if (priv->sound_signal_handler == 0)
531         priv->sound_signal_handler = g_signal_connect_after(widget, 
532                 "expose-event", G_CALLBACK (sound_handling), NULL);
533
534     /* We use special hint to turn the note into information notification. */
535     gdk_window_set_type_hint (widget->window, GDK_WINDOW_TYPE_HINT_NOTIFICATION);
536
537     /* Set the _HILDON_NOTIFICATION_TYPE property so Matchbox places the window correctly */
538     display = gdk_drawable_get_display (widget->window);
539     atom = gdk_x11_get_xatom_by_name_for_display (display, "_HILDON_NOTIFICATION_TYPE");
540
541     if (priv->note_n == HILDON_NOTE_TYPE_INFORMATION ||
542         priv->note_n == HILDON_NOTE_TYPE_INFORMATION_THEME) {
543         notification_type = "_HILDON_NOTIFICATION_TYPE_INFO";
544     } else {
545         notification_type = "_HILDON_NOTIFICATION_TYPE_CONFIRMATION";
546     }
547
548     XChangeProperty (GDK_WINDOW_XDISPLAY (widget->window), GDK_WINDOW_XID (widget->window),
549                      atom, XA_STRING, 8, PropModeReplace, (guchar *) notification_type,
550                      strlen (notification_type));
551 }
552
553 /* Helper function for removing a widget from it's container.
554    we own a separate reference to each object we try to unpack,
555    so extra referencing is not needed. */
556 static void 
557 unpack_widget                                   (GtkWidget *widget)
558 {
559     g_assert (widget == NULL || GTK_IS_WIDGET (widget));
560
561     if (widget && widget->parent)
562         gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
563 }
564
565 static void
566 hildon_note_rebuild                             (HildonNote *note)
567 {
568     GtkDialog *dialog;
569     HildonNotePrivate *priv;
570     gboolean IsHorizontal = TRUE;
571
572     g_assert (HILDON_IS_NOTE (note));
573
574     priv = HILDON_NOTE_GET_PRIVATE (note);
575     g_assert (priv);
576
577     dialog = GTK_DIALOG (note);
578
579     /* Reuse exiting content widgets for new layout */
580     unpack_widget (priv->label);
581     unpack_widget (priv->icon);
582     unpack_widget (priv->progressbar);
583
584     /* Destroy old layout and buttons */
585     if (priv->box) {
586         gtk_widget_destroy (priv->box);
587         priv->box = NULL;
588     }
589     if (priv->okButton) {
590         gtk_widget_destroy (priv->okButton);
591         priv->okButton = NULL;
592     }
593     if (priv->cancelButton) {
594         gtk_widget_destroy (priv->cancelButton);
595         priv->cancelButton = NULL;
596     }
597
598     /* By default the note won't be closed when pressing outside */
599     priv->close_if_pressed_outside = FALSE;
600
601     /* Add needed buttons and images for each note type */
602     switch (priv->note_n)
603     {
604         case HILDON_NOTE_TYPE_CONFIRMATION:
605             priv->okButton = gtk_dialog_add_button (dialog,
606                     _("ecdg_bd_confirmation_note_ok"), GTK_RESPONSE_OK);
607             priv->cancelButton = gtk_dialog_add_button (dialog,
608                     _("ecdg_bd_confirmation_note_cancel"), GTK_RESPONSE_CANCEL);
609
610             /* Fall through */
611         case HILDON_NOTE_TYPE_CONFIRMATION_BUTTON:
612             gtk_image_set_from_icon_name (GTK_IMAGE (priv->icon),
613                     HILDON_NOTE_CONFIRMATION_ICON, 
614                     HILDON_ICON_SIZE_BIG_NOTE);
615             break;
616
617         case HILDON_NOTE_TYPE_INFORMATION_THEME:
618         case HILDON_NOTE_TYPE_INFORMATION:
619             priv->close_if_pressed_outside = TRUE;
620             gtk_image_set_from_icon_name (GTK_IMAGE (priv->icon),
621                     HILDON_NOTE_INFORMATION_ICON,
622                     HILDON_ICON_SIZE_BIG_NOTE);
623             break;
624
625         case HILDON_NOTE_TYPE_PROGRESSBAR:
626             priv->cancelButton = gtk_dialog_add_button (dialog,
627                     _("ecdg_bd_cancel_note_cancel"), GTK_RESPONSE_CANCEL);
628             IsHorizontal = FALSE;
629             break;
630
631         default:
632             break;
633     }
634
635     if (IsHorizontal) {
636         /* Pack item with label horizontally */
637         priv->box = gtk_hbox_new (FALSE, HILDON_MARGIN_DEFAULT);
638         gtk_container_add (GTK_CONTAINER (dialog->vbox), priv->box);
639
640         if (priv->icon) {
641             GtkWidget *alignment = gtk_alignment_new (0, 0, 0, 0);
642
643             gtk_box_pack_start (GTK_BOX (priv->box), alignment, FALSE, FALSE, 0);
644             gtk_container_add (GTK_CONTAINER (alignment), priv->icon);
645         }
646         gtk_box_pack_start (GTK_BOX (priv->box), priv->label, TRUE, TRUE, 0);
647
648     } else {
649         /* Pack item with label vertically */
650         priv->box = gtk_vbox_new (FALSE, HILDON_MARGIN_DOUBLE);
651         gtk_container_add (GTK_CONTAINER (dialog->vbox), priv->box);
652         gtk_box_pack_start (GTK_BOX (priv->box), priv->label, TRUE, TRUE, 0);
653
654         if (priv->progressbar)
655             gtk_box_pack_start (GTK_BOX (priv->box), priv->progressbar, FALSE, FALSE, 0);
656     }
657
658     gtk_widget_show_all (priv->box);
659 }
660
661 /**
662  * hildon_note_new_confirmation_add_buttons:
663  * @parent: the parent window. The X window ID of the parent window
664  *   has to be the same as the X window ID of the application. This is
665  *   important so that the window manager could handle the windows
666  *   correctly.
667  *   In GTK the X window ID can be checked using
668  *   GDK_WINDOW_XID(GTK_WIDGET(parent)->window).
669  * @description: the message to confirm
670  * @Varargs: arguments pairs for new buttons(label and return value). 
671  *   Terminate the list with %NULL value.
672  * 
673  * Create a new confirmation note with custom buttons. Confirmation
674  * note has a text and any number of buttons. It's important to note
675  * that even though the name of the function might suggest, the
676  * default ok/cancel buttons are not appended but you have to provide
677  * all of the buttons.
678  *
679  * FIXME: This doc seems to be wrong, the two buttons aren't added so
680  * it would only contain the "additional" buttons? However, changing
681  * this would break those applications that rely on current behaviour.
682  *
683  * Returns: A #GtkWidget pointer of the note
684  */
685 GtkWidget*
686 hildon_note_new_confirmation_add_buttons        (GtkWindow *parent,
687                                                  const gchar *description,
688                                                  ...)
689 {
690     va_list args;
691     char *message;
692     int value;
693
694     g_return_val_if_fail (description != NULL, NULL);
695
696     GtkWidget *conf_note =
697         g_object_new (HILDON_TYPE_NOTE,
698                 "note-type", HILDON_NOTE_TYPE_CONFIRMATION_BUTTON,
699                 "description", description,
700                 "icon", HILDON_NOTE_CONFIRMATION_ICON, 
701                 NULL);
702
703     if (parent != NULL)
704         gtk_window_set_transient_for (GTK_WINDOW (conf_note), parent);
705
706     /* Add the buttons from varargs */
707     va_start(args, description);
708
709     while (TRUE) {
710         message = va_arg (args, char *);
711
712         if (! message) {
713             break;
714         }
715         value = va_arg (args, int);
716
717         gtk_dialog_add_button (GTK_DIALOG (conf_note), message, value);
718     }
719
720     va_end (args);
721
722     return conf_note;
723 }
724
725
726 /**
727  * hildon_note_new_confirmation:
728  * @parent: the parent window. The X window ID of the parent window
729  *   has to be the same as the X window ID of the application. This is
730  *   important so that the window manager could handle the windows
731  *   correctly. In GTK the X window ID can be checked using
732  *   GDK_WINDOW_XID(GTK_WIDGET(parent)->window).
733  * @description: the message to confirm
734  * 
735  * Create a new confirmation note. Confirmation note has text (description)
736  * that you specify, two buttons and a default confirmation stock icon.
737  *
738  * Returns: a #GtkWidget pointer of the note
739  */
740 GtkWidget*
741 hildon_note_new_confirmation                    (GtkWindow *parent,
742                                                  const gchar *description)
743 {
744     return hildon_note_new_confirmation_with_icon_name
745         (parent, description, HILDON_NOTE_CONFIRMATION_ICON);
746 }
747
748 /**
749  * hildon_note_new_confirmation_with_icon_name:
750  * @parent: the parent window. The X window ID of the parent window
751  *   has to be the same as the X window ID of the application. This is
752  *   important so that the window manager could handle the windows
753  *   correctly. In GTK the X window ID can be checked using
754  *   GDK_WINDOW_XID(GTK_WIDGET(parent)->window).
755  * @description: the message to confirm
756  * @icon_name: icon to be displayed. If NULL, default icon is used.
757  * 
758  * Create a new confirmation note. Confirmation note has text(description) 
759  * that you specify, two buttons and an icon.
760  *
761  * Returns: a #GtkWidget pointer of the note
762  */
763 GtkWidget*
764 hildon_note_new_confirmation_with_icon_name     (GtkWindow *parent,
765                                                  const gchar *description,
766                                                  const gchar *icon_name)
767 {
768     GtkWidget *dialog = NULL;
769
770     g_return_val_if_fail (description != NULL, NULL);
771
772     dialog = g_object_new (HILDON_TYPE_NOTE,
773             "note-type",
774             HILDON_NOTE_TYPE_CONFIRMATION,
775             "description", description, "icon",
776             icon_name, NULL);
777
778     if (parent != NULL)
779         gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
780
781     return dialog;
782 }
783
784 /**
785  * hildon_note_new_information:
786  * @parent: the parent window. The X window ID of the parent window
787  *   has to be the same as the X window ID of the application. This is
788  *   important so that the window manager could handle the windows
789  *   correctly. In GTK the X window ID can be checked using
790  *   GDK_WINDOW_XID(GTK_WIDGET(parent)->window).
791  * @description: the message to confirm
792  * 
793  * Create a new information note. Information note has a text(description) 
794  * that you specify, an OK button and an icon.
795  * 
796  * Returns: a #GtkWidget pointer of the note
797  */
798 GtkWidget*
799 hildon_note_new_information                     (GtkWindow *parent,
800                                                  const gchar *description)
801 {
802     return hildon_note_new_information_with_icon_name
803         (parent, description, HILDON_NOTE_INFORMATION_ICON);
804 }
805
806 /**
807  * hildon_note_new_information_with_icon_name:
808  * @parent: the parent window. The X window ID of the parent window
809  *   has to be the same as the X window ID of the application. This is
810  *   important so that the window manager could handle the windows
811  *   correctly. In GTK the X window ID can be checked using
812  *   GDK_WINDOW_XID(GTK_WIDGET(parent)->window).
813  * @description: the message to confirm
814  * @icon_name: icon to be displayed. If NULL, default icon is used.
815  * 
816  * Create a new information note. Information note has text(description) 
817  * that you specify, an OK button and an icon.
818  * 
819  * Returns: a #GtkWidget pointer of the note
820  */
821 GtkWidget*
822 hildon_note_new_information_with_icon_name      (GtkWindow * parent,
823                                                  const gchar *description,
824                                                  const gchar *icon_name)
825 {
826     GtkWidget *dialog = NULL;
827
828     g_return_val_if_fail (description != NULL, NULL);
829     g_return_val_if_fail (icon_name != NULL, NULL);
830
831     dialog = g_object_new (HILDON_TYPE_NOTE,
832             "note-type",
833             HILDON_NOTE_TYPE_INFORMATION_THEME,
834             "description", description,
835             "icon", icon_name, NULL);
836
837     if (parent != NULL)
838         gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
839
840     return dialog;
841 }
842
843 /* FIXME This documentation string LIES! */
844
845 /**
846  * hildon_note_new_cancel_with_progress_bar:
847  * @parent: the parent window. The X window ID of the parent window
848  *   has to be the same as the X window ID of the application. This is
849  *   important so that the window manager could handle the windows
850  *   correctly. In GTK the X window ID can be checked using
851  *   GDK_WINDOW_XID(GTK_WIDGET(parent)->window).
852  * @description: the action to cancel
853  * @progressbar: a pointer to #GtkProgressBar to be filled with the
854  *   progressbar assigned to this note. Use this to set the fraction of
855  *   progressbar done. This parameter can be %NULL as well, in which
856  *   case plain text cancel note appears.
857  *
858  * Create a new cancel note with a progress bar. Cancel note has 
859  * text(description) that you specify, a Cancel button and a progress bar.
860  *
861  * Returns: a #GtkDialog. Use this to get rid of this note when you
862  *   no longer need it.
863  */
864 GtkWidget*
865 hildon_note_new_cancel_with_progress_bar        (GtkWindow *parent,
866                                                  const gchar *description,
867                                                  GtkProgressBar *progressbar)
868 {
869     GtkWidget *dialog = NULL;
870
871     g_return_val_if_fail (description != NULL, NULL);
872
873     dialog = g_object_new (HILDON_TYPE_NOTE,
874             "note-type",
875             HILDON_NOTE_TYPE_PROGRESSBAR,
876             "description", description,
877             "progressbar",
878             progressbar, NULL);
879
880     if (parent != NULL)
881         gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
882
883     return dialog;
884 }
885
886
887 /**
888  * hildon_note_set_button_text:
889  * @note: a #HildonNote
890  * @text: sets the button text and if there is two buttons in dialog, 
891  *   the button texts will be &lt;text&gt;, "Cancel".  
892  *
893  * Sets the button text to be used by the hildon_note widget.
894  */
895 void 
896 hildon_note_set_button_text                     (HildonNote *note, 
897                                                  const gchar *text)
898 {
899     HildonNotePrivate *priv;
900
901     g_return_if_fail (HILDON_IS_NOTE (note));
902
903     priv = HILDON_NOTE_GET_PRIVATE (HILDON_NOTE (note));
904     g_assert (priv);
905
906     if (priv->okButton) {
907         gtk_button_set_label (GTK_BUTTON (priv->okButton), text);
908         gtk_button_set_label (GTK_BUTTON (priv->cancelButton),
909                 _("ecdg_bd_confirmation_note_cancel"));
910     } else {
911         gtk_button_set_label (GTK_BUTTON (priv->cancelButton), text);
912     }
913 }
914
915 /**
916  * hildon_note_set_button_texts:
917  * @note: a #HildonNote
918  * @text_ok: the new text of the default OK button
919  * @text_cancel: the new text of the default cancel button 
920  *
921  * Sets the button texts to be used by this hildon_note widget.
922  */
923 void 
924 hildon_note_set_button_texts                    (HildonNote *note,
925                                                  const gchar *text_ok,
926                                                  const gchar *text_cancel)
927 {
928     HildonNotePrivate *priv;
929
930     g_return_if_fail (HILDON_IS_NOTE (note));
931
932     priv = HILDON_NOTE_GET_PRIVATE (HILDON_NOTE (note));
933     g_assert (priv);
934
935     if (priv->okButton) {
936         gtk_button_set_label (GTK_BUTTON (priv->okButton), text_ok);
937         gtk_button_set_label (GTK_BUTTON (priv->cancelButton), text_cancel);
938     } else {
939         gtk_button_set_label (GTK_BUTTON (priv->cancelButton), text_cancel);
940     }
941 }
942
943 /* We play a system sound when the note comes visible */
944 static gboolean
945 sound_handling                                  (GtkWidget *widget, 
946                                                  GdkEventExpose *event, 
947                                                  gpointer data)
948 {
949     HildonNotePrivate *priv = HILDON_NOTE_GET_PRIVATE (widget);
950     g_assert (priv);
951
952     g_signal_handler_disconnect (widget, priv->sound_signal_handler);
953
954     priv->sound_signal_handler = 0;
955
956     switch (priv->note_n)
957     {
958         case HILDON_NOTE_TYPE_INFORMATION:
959         case HILDON_NOTE_TYPE_INFORMATION_THEME:
960             hildon_play_system_sound (INFORMATION_SOUND_PATH);
961             break;
962
963         case HILDON_NOTE_TYPE_CONFIRMATION:
964         case HILDON_NOTE_TYPE_CONFIRMATION_BUTTON:
965             hildon_play_system_sound (CONFIRMATION_SOUND_PATH);
966             break;
967
968         default:
969             break;
970     };
971
972     return FALSE;
973 }