* src/hildon-note.c (hildon_note_button_release): Use root coordinates to check if...
[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->x_root < x || event->x_root > x + widget->allocation.width ||
444                         event->y_root < y || event->y_root > y + widget->allocation.height);
445
446     if (released_outside && priv->close_if_pressed_outside) {
447         gtk_dialog_response (GTK_DIALOG (widget), GTK_RESPONSE_CANCEL);
448     }
449
450     if (GTK_WIDGET_CLASS (parent_class)->button_release_event) {
451         return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
452     } else {
453         return FALSE;
454     }
455 }
456
457 static void
458 hildon_note_map                                 (GtkWidget *widget)
459 {
460     HildonNotePrivate *priv = HILDON_NOTE_GET_PRIVATE (widget);
461     g_assert (priv);
462
463     /* Map the window */
464     GTK_WIDGET_CLASS (parent_class)->map (widget);
465
466     if (priv->transfer_window == NULL && priv->close_if_pressed_outside) {
467         gboolean has_grab = FALSE;
468
469         priv->transfer_window = grab_transfer_window_get (widget);
470
471         if (gdk_pointer_grab (priv->transfer_window, TRUE,
472                               GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
473                               GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
474                               GDK_POINTER_MOTION_MASK, NULL, NULL,
475                               GDK_CURRENT_TIME) == GDK_GRAB_SUCCESS) {
476             if (gdk_keyboard_grab (priv->transfer_window, TRUE,
477                                    GDK_CURRENT_TIME) == GDK_GRAB_SUCCESS) {
478                 has_grab = TRUE;
479             } else {
480                 gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
481                                             GDK_CURRENT_TIME);
482             }
483         }
484
485         if (has_grab) {
486             gtk_grab_add (widget);
487         } else {
488             gdk_window_destroy (priv->transfer_window);
489             priv->transfer_window = NULL;
490         }
491     }
492 }
493
494 static void
495 hildon_note_unmap                               (GtkWidget *widget)
496 {
497     HildonNotePrivate *priv = HILDON_NOTE_GET_PRIVATE (widget);
498     g_assert (priv);
499
500     if (priv->transfer_window != NULL) {
501         /* Remove the grab */
502         gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
503                                     GDK_CURRENT_TIME);
504         gtk_grab_remove (widget);
505
506         /* Destroy the transfer window */
507         gdk_window_destroy (priv->transfer_window);
508         priv->transfer_window = NULL;
509     }
510 }
511
512 static void
513 hildon_note_realize                             (GtkWidget *widget)
514 {
515     GdkDisplay *display;
516     Atom atom;
517     const gchar *notification_type;
518     HildonNotePrivate *priv = HILDON_NOTE_GET_PRIVATE (widget);
519     g_assert (priv);
520
521     /* Make widget->window accessible */
522     GTK_WIDGET_CLASS (parent_class)->realize (widget);
523
524     /* Border only, no titlebar */
525     gdk_window_set_decorations (widget->window, GDK_DECOR_BORDER);
526
527     /* Because ESD is synchronous, we wish to play sound after the
528        note is already on screen to avoid blocking its appearance */
529     if (priv->sound_signal_handler == 0)
530         priv->sound_signal_handler = g_signal_connect_after(widget, 
531                 "expose-event", G_CALLBACK (sound_handling), NULL);
532
533     /* We use special hint to turn the note into information notification. */
534     gdk_window_set_type_hint (widget->window, GDK_WINDOW_TYPE_HINT_NOTIFICATION);
535
536     /* Set the _HILDON_NOTIFICATION_TYPE property so Matchbox places the window correctly */
537     display = gdk_drawable_get_display (widget->window);
538     atom = gdk_x11_get_xatom_by_name_for_display (display, "_HILDON_NOTIFICATION_TYPE");
539
540     if (priv->note_n == HILDON_NOTE_TYPE_INFORMATION ||
541         priv->note_n == HILDON_NOTE_TYPE_INFORMATION_THEME) {
542         notification_type = "_HILDON_NOTIFICATION_TYPE_INFO";
543     } else {
544         notification_type = "_HILDON_NOTIFICATION_TYPE_CONFIRMATION";
545     }
546
547     XChangeProperty (GDK_WINDOW_XDISPLAY (widget->window), GDK_WINDOW_XID (widget->window),
548                      atom, XA_STRING, 8, PropModeReplace, (guchar *) notification_type,
549                      strlen (notification_type));
550 }
551
552 /* Helper function for removing a widget from it's container.
553    we own a separate reference to each object we try to unpack,
554    so extra referencing is not needed. */
555 static void 
556 unpack_widget                                   (GtkWidget *widget)
557 {
558     g_assert (widget == NULL || GTK_IS_WIDGET (widget));
559
560     if (widget && widget->parent)
561         gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
562 }
563
564 static void
565 hildon_note_rebuild                             (HildonNote *note)
566 {
567     GtkDialog *dialog;
568     HildonNotePrivate *priv;
569     gboolean IsHorizontal = TRUE;
570
571     g_assert (HILDON_IS_NOTE (note));
572
573     priv = HILDON_NOTE_GET_PRIVATE (note);
574     g_assert (priv);
575
576     dialog = GTK_DIALOG (note);
577
578     /* Reuse exiting content widgets for new layout */
579     unpack_widget (priv->label);
580     unpack_widget (priv->icon);
581     unpack_widget (priv->progressbar);
582
583     /* Destroy old layout and buttons */
584     if (priv->box) {
585         gtk_widget_destroy (priv->box);
586         priv->box = NULL;
587     }
588     if (priv->okButton) {
589         gtk_widget_destroy (priv->okButton);
590         priv->okButton = NULL;
591     }
592     if (priv->cancelButton) {
593         gtk_widget_destroy (priv->cancelButton);
594         priv->cancelButton = NULL;
595     }
596
597     /* By default the note won't be closed when pressing outside */
598     priv->close_if_pressed_outside = FALSE;
599
600     /* Add needed buttons and images for each note type */
601     switch (priv->note_n)
602     {
603         case HILDON_NOTE_TYPE_CONFIRMATION:
604             priv->okButton = gtk_dialog_add_button (dialog,
605                     _("ecdg_bd_confirmation_note_ok"), GTK_RESPONSE_OK);
606             priv->cancelButton = gtk_dialog_add_button (dialog,
607                     _("ecdg_bd_confirmation_note_cancel"), GTK_RESPONSE_CANCEL);
608
609             /* Fall through */
610         case HILDON_NOTE_TYPE_CONFIRMATION_BUTTON:
611             gtk_image_set_from_icon_name (GTK_IMAGE (priv->icon),
612                     HILDON_NOTE_CONFIRMATION_ICON, 
613                     HILDON_ICON_SIZE_BIG_NOTE);
614             break;
615
616         case HILDON_NOTE_TYPE_INFORMATION_THEME:
617         case HILDON_NOTE_TYPE_INFORMATION:
618             priv->close_if_pressed_outside = TRUE;
619             gtk_image_set_from_icon_name (GTK_IMAGE (priv->icon),
620                     HILDON_NOTE_INFORMATION_ICON,
621                     HILDON_ICON_SIZE_BIG_NOTE);
622             break;
623
624         case HILDON_NOTE_TYPE_PROGRESSBAR:
625             priv->cancelButton = gtk_dialog_add_button (dialog,
626                     _("ecdg_bd_cancel_note_cancel"), GTK_RESPONSE_CANCEL);
627             IsHorizontal = FALSE;
628             break;
629
630         default:
631             break;
632     }
633
634     if (IsHorizontal) {
635         /* Pack item with label horizontally */
636         priv->box = gtk_hbox_new (FALSE, HILDON_MARGIN_DEFAULT);
637         gtk_container_add (GTK_CONTAINER (dialog->vbox), priv->box);
638
639         if (priv->icon) {
640             GtkWidget *alignment = gtk_alignment_new (0, 0, 0, 0);
641
642             gtk_box_pack_start (GTK_BOX (priv->box), alignment, FALSE, FALSE, 0);
643             gtk_container_add (GTK_CONTAINER (alignment), priv->icon);
644         }
645         gtk_box_pack_start (GTK_BOX (priv->box), priv->label, TRUE, TRUE, 0);
646
647     } else {
648         /* Pack item with label vertically */
649         priv->box = gtk_vbox_new (FALSE, HILDON_MARGIN_DOUBLE);
650         gtk_container_add (GTK_CONTAINER (dialog->vbox), priv->box);
651         gtk_box_pack_start (GTK_BOX (priv->box), priv->label, TRUE, TRUE, 0);
652
653         if (priv->progressbar)
654             gtk_box_pack_start (GTK_BOX (priv->box), priv->progressbar, FALSE, FALSE, 0);
655     }
656
657     gtk_widget_show_all (priv->box);
658 }
659
660 /**
661  * hildon_note_new_confirmation_add_buttons:
662  * @parent: the parent window. The X window ID of the parent window
663  *   has to be the same as the X window ID of the application. This is
664  *   important so that the window manager could handle the windows
665  *   correctly.
666  *   In GTK the X window ID can be checked using
667  *   GDK_WINDOW_XID(GTK_WIDGET(parent)->window).
668  * @description: the message to confirm
669  * @Varargs: arguments pairs for new buttons(label and return value). 
670  *   Terminate the list with %NULL value.
671  * 
672  * Create a new confirmation note with custom buttons. Confirmation
673  * note has a text and any number of buttons. It's important to note
674  * that even though the name of the function might suggest, the
675  * default ok/cancel buttons are not appended but you have to provide
676  * all of the buttons.
677  *
678  * FIXME: This doc seems to be wrong, the two buttons aren't added so
679  * it would only contain the "additional" buttons? However, changing
680  * this would break those applications that rely on current behaviour.
681  *
682  * Returns: A #GtkWidget pointer of the note
683  */
684 GtkWidget*
685 hildon_note_new_confirmation_add_buttons        (GtkWindow *parent,
686                                                  const gchar *description,
687                                                  ...)
688 {
689     va_list args;
690     char *message;
691     int value;
692
693     g_return_val_if_fail (description != NULL, NULL);
694
695     GtkWidget *conf_note =
696         g_object_new (HILDON_TYPE_NOTE,
697                 "note-type", HILDON_NOTE_TYPE_CONFIRMATION_BUTTON,
698                 "description", description,
699                 "icon", HILDON_NOTE_CONFIRMATION_ICON, 
700                 NULL);
701
702     if (parent != NULL)
703         gtk_window_set_transient_for (GTK_WINDOW (conf_note), parent);
704
705     /* Add the buttons from varargs */
706     va_start(args, description);
707
708     while (TRUE) {
709         message = va_arg (args, char *);
710
711         if (! message) {
712             break;
713         }
714         value = va_arg (args, int);
715
716         gtk_dialog_add_button (GTK_DIALOG (conf_note), message, value);
717     }
718
719     va_end (args);
720
721     return conf_note;
722 }
723
724
725 /**
726  * hildon_note_new_confirmation:
727  * @parent: the parent window. The X window ID of the parent window
728  *   has to be the same as the X window ID of the application. This is
729  *   important so that the window manager could handle the windows
730  *   correctly. In GTK the X window ID can be checked using
731  *   GDK_WINDOW_XID(GTK_WIDGET(parent)->window).
732  * @description: the message to confirm
733  * 
734  * Create a new confirmation note. Confirmation note has text (description)
735  * that you specify, two buttons and a default confirmation stock icon.
736  *
737  * Returns: a #GtkWidget pointer of the note
738  */
739 GtkWidget*
740 hildon_note_new_confirmation                    (GtkWindow *parent,
741                                                  const gchar *description)
742 {
743     return hildon_note_new_confirmation_with_icon_name
744         (parent, description, HILDON_NOTE_CONFIRMATION_ICON);
745 }
746
747 /**
748  * hildon_note_new_confirmation_with_icon_name:
749  * @parent: the parent window. The X window ID of the parent window
750  *   has to be the same as the X window ID of the application. This is
751  *   important so that the window manager could handle the windows
752  *   correctly. In GTK the X window ID can be checked using
753  *   GDK_WINDOW_XID(GTK_WIDGET(parent)->window).
754  * @description: the message to confirm
755  * @icon_name: icon to be displayed. If NULL, default icon is used.
756  * 
757  * Create a new confirmation note. Confirmation note has text(description) 
758  * that you specify, two buttons and an icon.
759  *
760  * Returns: a #GtkWidget pointer of the note
761  */
762 GtkWidget*
763 hildon_note_new_confirmation_with_icon_name     (GtkWindow *parent,
764                                                  const gchar *description,
765                                                  const gchar *icon_name)
766 {
767     GtkWidget *dialog = NULL;
768
769     g_return_val_if_fail (description != NULL, NULL);
770
771     dialog = g_object_new (HILDON_TYPE_NOTE,
772             "note-type",
773             HILDON_NOTE_TYPE_CONFIRMATION,
774             "description", description, "icon",
775             icon_name, NULL);
776
777     if (parent != NULL)
778         gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
779
780     return dialog;
781 }
782
783 /**
784  * hildon_note_new_information:
785  * @parent: the parent window. The X window ID of the parent window
786  *   has to be the same as the X window ID of the application. This is
787  *   important so that the window manager could handle the windows
788  *   correctly. In GTK the X window ID can be checked using
789  *   GDK_WINDOW_XID(GTK_WIDGET(parent)->window).
790  * @description: the message to confirm
791  * 
792  * Create a new information note. Information note has a text(description) 
793  * that you specify, an OK button and an icon.
794  * 
795  * Returns: a #GtkWidget pointer of the note
796  */
797 GtkWidget*
798 hildon_note_new_information                     (GtkWindow *parent,
799                                                  const gchar *description)
800 {
801     return hildon_note_new_information_with_icon_name
802         (parent, description, HILDON_NOTE_INFORMATION_ICON);
803 }
804
805 /**
806  * hildon_note_new_information_with_icon_name:
807  * @parent: the parent window. The X window ID of the parent window
808  *   has to be the same as the X window ID of the application. This is
809  *   important so that the window manager could handle the windows
810  *   correctly. In GTK the X window ID can be checked using
811  *   GDK_WINDOW_XID(GTK_WIDGET(parent)->window).
812  * @description: the message to confirm
813  * @icon_name: icon to be displayed. If NULL, default icon is used.
814  * 
815  * Create a new information note. Information note has text(description) 
816  * that you specify, an OK button and an icon.
817  * 
818  * Returns: a #GtkWidget pointer of the note
819  */
820 GtkWidget*
821 hildon_note_new_information_with_icon_name      (GtkWindow * parent,
822                                                  const gchar *description,
823                                                  const gchar *icon_name)
824 {
825     GtkWidget *dialog = NULL;
826
827     g_return_val_if_fail (description != NULL, NULL);
828     g_return_val_if_fail (icon_name != NULL, NULL);
829
830     dialog = g_object_new (HILDON_TYPE_NOTE,
831             "note-type",
832             HILDON_NOTE_TYPE_INFORMATION_THEME,
833             "description", description,
834             "icon", icon_name, NULL);
835
836     if (parent != NULL)
837         gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
838
839     return dialog;
840 }
841
842 /* FIXME This documentation string LIES! */
843
844 /**
845  * hildon_note_new_cancel_with_progress_bar:
846  * @parent: the parent window. The X window ID of the parent window
847  *   has to be the same as the X window ID of the application. This is
848  *   important so that the window manager could handle the windows
849  *   correctly. In GTK the X window ID can be checked using
850  *   GDK_WINDOW_XID(GTK_WIDGET(parent)->window).
851  * @description: the action to cancel
852  * @progressbar: a pointer to #GtkProgressBar to be filled with the
853  *   progressbar assigned to this note. Use this to set the fraction of
854  *   progressbar done. This parameter can be %NULL as well, in which
855  *   case plain text cancel note appears.
856  *
857  * Create a new cancel note with a progress bar. Cancel note has 
858  * text(description) that you specify, a Cancel button and a progress bar.
859  *
860  * Returns: a #GtkDialog. Use this to get rid of this note when you
861  *   no longer need it.
862  */
863 GtkWidget*
864 hildon_note_new_cancel_with_progress_bar        (GtkWindow *parent,
865                                                  const gchar *description,
866                                                  GtkProgressBar *progressbar)
867 {
868     GtkWidget *dialog = NULL;
869
870     g_return_val_if_fail (description != NULL, NULL);
871
872     dialog = g_object_new (HILDON_TYPE_NOTE,
873             "note-type",
874             HILDON_NOTE_TYPE_PROGRESSBAR,
875             "description", description,
876             "progressbar",
877             progressbar, NULL);
878
879     if (parent != NULL)
880         gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
881
882     return dialog;
883 }
884
885
886 /**
887  * hildon_note_set_button_text:
888  * @note: a #HildonNote
889  * @text: sets the button text and if there is two buttons in dialog, 
890  *   the button texts will be &lt;text&gt;, "Cancel".  
891  *
892  * Sets the button text to be used by the hildon_note widget.
893  */
894 void 
895 hildon_note_set_button_text                     (HildonNote *note, 
896                                                  const gchar *text)
897 {
898     HildonNotePrivate *priv;
899
900     g_return_if_fail (HILDON_IS_NOTE (note));
901
902     priv = HILDON_NOTE_GET_PRIVATE (HILDON_NOTE (note));
903     g_assert (priv);
904
905     if (priv->okButton) {
906         gtk_button_set_label (GTK_BUTTON (priv->okButton), text);
907         gtk_button_set_label (GTK_BUTTON (priv->cancelButton),
908                 _("ecdg_bd_confirmation_note_cancel"));
909     } else {
910         gtk_button_set_label (GTK_BUTTON (priv->cancelButton), text);
911     }
912 }
913
914 /**
915  * hildon_note_set_button_texts:
916  * @note: a #HildonNote
917  * @text_ok: the new text of the default OK button
918  * @text_cancel: the new text of the default cancel button 
919  *
920  * Sets the button texts to be used by this hildon_note widget.
921  */
922 void 
923 hildon_note_set_button_texts                    (HildonNote *note,
924                                                  const gchar *text_ok,
925                                                  const gchar *text_cancel)
926 {
927     HildonNotePrivate *priv;
928
929     g_return_if_fail (HILDON_IS_NOTE (note));
930
931     priv = HILDON_NOTE_GET_PRIVATE (HILDON_NOTE (note));
932     g_assert (priv);
933
934     if (priv->okButton) {
935         gtk_button_set_label (GTK_BUTTON (priv->okButton), text_ok);
936         gtk_button_set_label (GTK_BUTTON (priv->cancelButton), text_cancel);
937     } else {
938         gtk_button_set_label (GTK_BUTTON (priv->cancelButton), text_cancel);
939     }
940 }
941
942 /* We play a system sound when the note comes visible */
943 static gboolean
944 sound_handling                                  (GtkWidget *widget, 
945                                                  GdkEventExpose *event, 
946                                                  gpointer data)
947 {
948     HildonNotePrivate *priv = HILDON_NOTE_GET_PRIVATE (widget);
949     g_assert (priv);
950
951     g_signal_handler_disconnect (widget, priv->sound_signal_handler);
952
953     priv->sound_signal_handler = 0;
954
955     switch (priv->note_n)
956     {
957         case HILDON_NOTE_TYPE_INFORMATION:
958         case HILDON_NOTE_TYPE_INFORMATION_THEME:
959             hildon_play_system_sound (INFORMATION_SOUND_PATH);
960             break;
961
962         case HILDON_NOTE_TYPE_CONFIRMATION:
963         case HILDON_NOTE_TYPE_CONFIRMATION_BUTTON:
964             hildon_play_system_sound (CONFIRMATION_SOUND_PATH);
965             break;
966
967         default:
968             break;
969     };
970
971     return FALSE;
972 }