Add HildonAppMenu::changed signal
[hildon] / hildon / hildon-note.c
index 2fa15f2..8401c55 100644 (file)
@@ -28,8 +28,8 @@
  *
  * #HildonNote is a convenient way to prompt users for a small amount of
  * input. A simple note contains an information text and, in case of
- * confirmation notes, it shows buttons to confirm or cancel. It also can
- * include a progress bar.
+ * confirmation notes, it shows buttons to confirm or cancel. It can also
+ * include a #GtkProgressBar.
  *
  * This widget provides convenient functions to create either
  * information notes, confirmation notes or cancel notes, which are
@@ -88,7 +88,7 @@
 #include                                        "hildon-note.h"
 #include                                        "hildon-defines.h"
 #include                                        "hildon-sound.h"
-#include                                        "hildon-banner.h" 
+#include                                        "hildon-gtk.h"
 #include                                        "hildon-enum-types.h"
 #include                                        "hildon-note-private.h"
 
@@ -114,6 +114,9 @@ static void
 hildon_note_rebuild                             (HildonNote *note);
 
 static void
+hildon_note_set_padding                         (HildonNote *note);
+
+static void
 hildon_note_rename                              (HildonNote *note);
 
 static void
@@ -126,6 +129,10 @@ static void
 hildon_note_unrealize                           (GtkWidget *widget);
 
 static void
+hildon_note_size_request                        (GtkWidget      *note,
+                                                 GtkRequisition *req);
+
+static void
 label_size_request                              (GtkWidget      *label,
                                                  GtkRequisition *req,
                                                  GtkWidget      *note);
@@ -142,10 +149,12 @@ hildon_note_get_property                        (GObject *object,
                                                  GValue *value, 
                                                  GParamSpec *pspec);
 
-static gboolean
-sound_handling                                  (GtkWidget *widget, 
-                                                 GdkEventExpose *event, 
+static void
+on_show_cb                                      (GtkWidget *widget,
                                                  gpointer data);
+static gboolean
+sound_handling                                  (gpointer data);
+
 static void
 unpack_widget                                   (GtkWidget *widget);
 
@@ -286,6 +295,21 @@ hildon_note_get_property                        (GObject *object,
     }
 }
 
+#ifdef MAEMO_GTK
+static GObject *
+hildon_note_constructor (GType type,
+                         guint n_construct_properties,
+                         GObjectConstructParam *construct_params)
+{
+    GObject *object;
+    object = (* G_OBJECT_CLASS (parent_class)->constructor)
+        (type, n_construct_properties, construct_params);
+    hildon_note_set_padding (HILDON_NOTE (object));
+
+    return object;
+}
+#endif /* MAEMO_GTK */
+
 /**
  * hildon_note_get_type:
  *
@@ -331,9 +355,19 @@ hildon_note_class_init                          (HildonNoteClass *class)
     object_class->finalize      = hildon_note_finalize;
     object_class->set_property  = hildon_note_set_property;
     object_class->get_property  = hildon_note_get_property;
+#ifdef MAEMO_GTK
+    object_class->constructor   = hildon_note_constructor;
+#endif /* MAEMO_GTK */
     widget_class->realize       = hildon_note_realize;
     widget_class->unrealize     = hildon_note_unrealize;
+    widget_class->size_request  = hildon_note_size_request;
 
+    /**
+     * HildonNote:type:
+     *
+     * The type of the #HildonNote, defining its contents, behavior, and
+     * theming.
+     */
     g_object_class_install_property (object_class,
             PROP_HILDON_NOTE_TYPE,
             g_param_spec_enum ("note-type",
@@ -346,7 +380,7 @@ hildon_note_class_init                          (HildonNoteClass *class)
     /**
      * HildonNote:description:
      *
-     * Description for the note.
+     * The text that appears in the #HildonNote.
      */
     g_object_class_install_property (object_class,
             PROP_HILDON_NOTE_DESCRIPTION,
@@ -389,7 +423,7 @@ hildon_note_class_init                          (HildonNoteClass *class)
     /**
      * HildonNote:progressbar:
      *
-     * Progressbar for the note (if any).
+     * If set, a #GtkProgressBar is displayed in the note.
      */
     g_object_class_install_property (object_class,
             PROP_HILDON_NOTE_PROGRESSBAR,
@@ -413,12 +447,18 @@ hildon_note_init                                (HildonNote *dialog)
     priv->event_box = gtk_event_box_new ();
     priv->icon = NULL;
     priv->stock_icon = NULL;
+    priv->idle_handler = 0;
 
     gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->event_box), FALSE);
     gtk_event_box_set_above_child (GTK_EVENT_BOX (priv->event_box), TRUE);
     g_signal_connect (priv->event_box, "button-press-event",
                       G_CALLBACK (event_box_press_event), dialog);
 
+    /* Because ESD is synchronous, we wish to play sound after the
+       note is already on screen to avoid blocking its appearance */
+    g_signal_connect (GTK_WIDGET (dialog), "show",
+                      G_CALLBACK (on_show_cb), NULL);
+
     /* Acquire real references to our internal children, since
        they are not nessecarily packed into container in each
        layout */
@@ -456,6 +496,10 @@ hildon_note_finalize                            (GObject *obj_self)
         g_free (priv->stock_icon);
         priv->stock_icon = NULL;
     }
+    if (priv->idle_handler) {
+        g_source_remove (priv->idle_handler);
+        priv->idle_handler = 0;
+    }
 
     if (priv->progressbar)
         g_object_unref (priv->progressbar);
@@ -471,7 +515,7 @@ label_size_request                              (GtkWidget      *label,
                                                  GtkRequisition *req,
                                                  GtkWidget      *note)
 {
-    gint note_height = MAX (HILDON_INFORMATION_NOTE_MIN_HEIGHT, req->height);
+    gint note_height = MAX (HILDON_INFORMATION_NOTE_MIN_HEIGHT, req->height + 2*HILDON_MARGIN_DOUBLE);
     g_object_set (note, "height-request", note_height, NULL);
 }
 
@@ -516,16 +560,25 @@ hildon_note_orientation_update (HildonNote *note, GdkScreen *screen)
 }
 
 static void
+hildon_note_size_request                        (GtkWidget      *note,
+                                                 GtkRequisition *req)
+{
+    GTK_WIDGET_CLASS (parent_class)->size_request (note, req);
+    req->width = gdk_screen_get_width (gtk_widget_get_screen (note));
+}
+
+static void
 screen_size_changed                            (GdkScreen *screen,
                                                 GtkWidget *note)
 {
     HildonNotePrivate *priv = HILDON_NOTE_GET_PRIVATE (note);
-    gint screen_width = gdk_screen_get_width (screen);
-    gint text_width = screen_width - HILDON_INFORMATION_NOTE_MARGIN * 2;
+
+    hildon_note_rename (HILDON_NOTE (note));
 
     if (priv->note_n == HILDON_NOTE_TYPE_INFORMATION ||
         priv->note_n == HILDON_NOTE_TYPE_INFORMATION_THEME) {
-        g_object_set (note, "width-request", screen_width, NULL);
+        gint screen_width = gdk_screen_get_width (screen);
+        gint text_width = screen_width - HILDON_INFORMATION_NOTE_MARGIN * 2;
         g_object_set (priv->label, "width-request", text_width, NULL);
 
         return;
@@ -550,12 +603,6 @@ hildon_note_realize                             (GtkWidget *widget)
     /* Border only, no titlebar */
     gdk_window_set_decorations (widget->window, GDK_DECOR_BORDER);
 
-    /* Because ESD is synchronous, we wish to play sound after the
-       note is already on screen to avoid blocking its appearance */
-    if (priv->sound_signal_handler == 0)
-        priv->sound_signal_handler = g_signal_connect_after(widget, 
-                "expose-event", G_CALLBACK (sound_handling), NULL);
-
     /* Set the _HILDON_NOTIFICATION_TYPE property so Matchbox places the window correctly */
     display = gdk_drawable_get_display (widget->window);
     atom = gdk_x11_get_xatom_by_name_for_display (display, "_HILDON_NOTIFICATION_TYPE");
@@ -579,6 +626,8 @@ hildon_note_realize                             (GtkWidget *widget)
     GdkScreen *screen = gtk_widget_get_screen (widget);
     g_signal_connect (screen, "size-changed", G_CALLBACK (screen_size_changed), widget);
     screen_size_changed (screen, widget);
+
+    hildon_gtk_window_set_portrait_flags (GTK_WINDOW (widget), HILDON_PORTRAIT_MODE_SUPPORT);
 }
 
 static void
@@ -616,13 +665,16 @@ hildon_note_rename                              (HildonNote *note)
   GEnumValue *value;
   GEnumClass *enum_class;
   gchar *name;
+  GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (note));
+  gboolean portrait = gdk_screen_get_width (screen) < gdk_screen_get_height (screen);
+  const gchar *portrait_suffix = portrait ? "-portrait" : NULL;
 
   HildonNotePrivate *priv = HILDON_NOTE_GET_PRIVATE (note);
 
   enum_class = g_type_class_ref (HILDON_TYPE_NOTE_TYPE);
   value = g_enum_get_value (enum_class, priv->note_n);
 
-  name = g_strconcat ("HildonNote-", value->value_nick, NULL);
+  name = g_strconcat ("HildonNote-", value->value_nick, portrait_suffix, NULL);
   gtk_widget_set_name (GTK_WIDGET (note), name);
   g_free (name);
 
@@ -633,6 +685,39 @@ hildon_note_rename                              (HildonNote *note)
   g_type_class_unref (enum_class);
 }
 
+#ifdef MAEMO_GTK
+static void
+hildon_note_set_padding (HildonNote *note)
+{
+    HildonNotePrivate *priv;
+
+    priv = HILDON_NOTE_GET_PRIVATE (note);
+
+    switch (priv->note_n) {
+    case HILDON_NOTE_TYPE_INFORMATION:
+    case HILDON_NOTE_TYPE_INFORMATION_THEME:
+        gtk_dialog_set_padding (GTK_DIALOG (note),
+                                HILDON_MARGIN_DOUBLE,
+                                HILDON_MARGIN_DOUBLE,
+                                0,
+                                0);
+        break;
+
+    case HILDON_NOTE_TYPE_CONFIRMATION:
+    case HILDON_NOTE_TYPE_CONFIRMATION_BUTTON:
+        gtk_dialog_set_padding (GTK_DIALOG (note),
+                                HILDON_MARGIN_DOUBLE,
+                                HILDON_MARGIN_DEFAULT,
+                                HILDON_MARGIN_DOUBLE,
+                                HILDON_MARGIN_DOUBLE);
+        break;
+
+    default:
+        break;
+    }
+}
+#endif /* MAEMO_GTK */
+
 static void
 hildon_note_rebuild                             (HildonNote *note)
 {
@@ -678,6 +763,13 @@ hildon_note_rebuild                             (HildonNote *note)
             g_object_get (priv->okButton, "width-request",
                           &priv->button_width, NULL);
             gtk_widget_set_no_show_all (priv->cancelButton, FALSE);
+#ifdef MAEMO_GTK
+           gtk_dialog_set_padding (dialog,
+                                   HILDON_MARGIN_DOUBLE,
+                                   HILDON_MARGIN_DEFAULT,
+                                   HILDON_MARGIN_DOUBLE,
+                                   HILDON_MARGIN_DOUBLE);
+#endif /* MAEMO_GTK */
             break;
 
         case HILDON_NOTE_TYPE_PROGRESSBAR:
@@ -689,6 +781,13 @@ hildon_note_rebuild                             (HildonNote *note)
 
         case HILDON_NOTE_TYPE_INFORMATION_THEME:
         case HILDON_NOTE_TYPE_INFORMATION:
+#ifdef MAEMO_GTK
+           gtk_dialog_set_padding (dialog,
+                                   HILDON_MARGIN_DOUBLE,
+                                   HILDON_MARGIN_DOUBLE,
+                                   0,
+                                   0);
+#endif /* MAEMO_GTK */
             is_info_note = TRUE;
             break;
 
@@ -716,6 +815,10 @@ hildon_note_rebuild                             (HildonNote *note)
     if (priv->progressbar)
         gtk_box_pack_start (GTK_BOX (priv->box), priv->progressbar, FALSE, FALSE, 0);
 
+#ifdef MAEMO_GTK
+    hildon_note_set_padding (note);
+#endif /* MAEMO_GTK */
+
     gtk_container_add (GTK_CONTAINER (dialog->vbox), priv->event_box);
 
     gtk_widget_show_all (priv->event_box);
@@ -743,7 +846,7 @@ hildon_note_rebuild                             (HildonNote *note)
  * it would only contain the "additional" buttons? However, changing
  * this would break those applications that rely on current behaviour.
  *
- * Returns: A #GtkWidget pointer of the note
+ * Returns: A new #HildonNote.
  */
 GtkWidget*
 hildon_note_new_confirmation_add_buttons        (GtkWindow *parent,
@@ -799,12 +902,12 @@ hildon_note_new_confirmation_add_buttons        (GtkWindow *parent,
  *   important so that the window manager could handle the windows
  *   correctly. In GTK the X window ID can be checked using
  *   GDK_WINDOW_XID(GTK_WIDGET(parent)->window).
- * @description: the message to confirm
+ * @description: the message to confirm.
  *
  * Create a new confirmation note. Confirmation note has a text (description)
  * that you specify and two buttons.
  *
- * Returns: a #GtkWidget pointer of the note
+ * Returns: a new #HildonNote.
  */
 GtkWidget*
 hildon_note_new_confirmation                    (GtkWindow *parent,
@@ -841,7 +944,7 @@ hildon_note_new_confirmation                    (GtkWindow *parent,
  * Deprecated: Since 2.2, icons are not shown in confirmation notes. Icons set
  * with this function will be ignored. Use hildon_note_new_confirmation() instead.
  *
- * Returns: a #GtkWidget pointer of the note
+ * Returns: a new #HildonNote.
  */
 GtkWidget*
 hildon_note_new_confirmation_with_icon_name     (GtkWindow *parent,
@@ -863,12 +966,12 @@ hildon_note_new_confirmation_with_icon_name     (GtkWindow *parent,
  *   important so that the window manager could handle the windows
  *   correctly. In GTK the X window ID can be checked using
  *   GDK_WINDOW_XID(GTK_WIDGET(parent)->window).
- * @description: the message to confirm
+ * @description: the message to confirm.
  * 
- * Create a new information note. Information note has a text (description)
+ * Create a new information note. Information note has text (a description)
  * that you specify and an OK button.
  * 
- * Returns: a #GtkWidget pointer of the note
+ * Returns: a new #HildonNote.
  */
 GtkWidget*
 hildon_note_new_information                     (GtkWindow *parent,
@@ -896,16 +999,17 @@ hildon_note_new_information                     (GtkWindow *parent,
  *   important so that the window manager could handle the windows
  *   correctly. In GTK the X window ID can be checked using
  *   GDK_WINDOW_XID(GTK_WIDGET(parent)->window).
- * @description: the message to confirm
- * @icon_name: icon to be displayed. If NULL, default icon is used.
+ * @description: the message to confirm.
+ * @icon_name: icon to be displayed. If %NULL, the default icon is used.
  * 
- * Create a new information note. Information note has text(description) 
+ * Create a new information note. An information note has text (a description)
  * that you specify, an OK button and an icon.
  * 
  * Deprecated: Since 2.2, icons are not shown in confirmation notes. Icons set
- * with this function will be ignored. Use hildon_note_new_information() instead.
+ * with this function will be ignored. Use hildon_note_new_information()
+ * instead.
  *
- * Returns: a #GtkWidget pointer of the note
+ * Returns: a new #HildonNote.
  */
 GtkWidget*
 hildon_note_new_information_with_icon_name      (GtkWindow * parent,
@@ -929,7 +1033,7 @@ hildon_note_new_information_with_icon_name      (GtkWindow * parent,
  *   important so that the window manager could handle the windows
  *   correctly. In GTK the X window ID can be checked using
  *   GDK_WINDOW_XID(GTK_WIDGET(parent)->window).
- * @description: the action to cancel
+ * @description: the action to cancel.
  * @progressbar: a pointer to #GtkProgressBar to be filled with the
  *   progressbar assigned to this note. Use this to set the fraction of
  *   progressbar done. This parameter can be %NULL as well, in which
@@ -966,11 +1070,11 @@ hildon_note_new_cancel_with_progress_bar        (GtkWindow *parent,
 
 /**
  * hildon_note_set_button_text:
- * @note: a #HildonNote
- * @text: sets the button text and if there is two buttons in dialog, 
+ * @note: a #HildonNote.
+ * @text: sets the button text. If there are two buttons in dialog,
  *   the button texts will be &lt;text&gt;, "Cancel".  
  *
- * Sets the button text to be used by the hildon_note widget.
+ * Sets the text of the button in @note.
  */
 void 
 hildon_note_set_button_text                     (HildonNote *note, 
@@ -994,11 +1098,11 @@ hildon_note_set_button_text                     (HildonNote *note,
 
 /**
  * hildon_note_set_button_texts:
- * @note: a #HildonNote
- * @text_ok: the new text of the default OK button
- * @text_cancel: the new text of the default cancel button 
+ * @note: a #HildonNote.
+ * @text_ok: the new text of the default OK button.
+ * @text_cancel: the new text of the default cancel button.
  *
- * Sets the button texts to be used by this hildon_note widget.
+ * Sets the text for the buttons in @note.
  */
 void 
 hildon_note_set_button_texts                    (HildonNote *note,
@@ -1020,18 +1124,22 @@ hildon_note_set_button_texts                    (HildonNote *note,
     }
 }
 
-/* We play a system sound when the note comes visible */
-static gboolean
-sound_handling                                  (GtkWidget *widget, 
-                                                 GdkEventExpose *event, 
+static void
+on_show_cb                                      (GtkWidget *widget,
                                                  gpointer data)
 {
-    HildonNotePrivate *priv = HILDON_NOTE_GET_PRIVATE (widget);
-    g_assert (priv);
+    HildonNotePrivate *priv;
 
-    g_signal_handler_disconnect (widget, priv->sound_signal_handler);
+    priv = HILDON_NOTE_GET_PRIVATE (widget);
+    priv->idle_handler = gdk_threads_add_idle (sound_handling, widget);
+}
 
-    priv->sound_signal_handler = 0;
+/* We play a system sound when the note comes visible */
+static gboolean
+sound_handling                                  (gpointer data)
+{
+    HildonNotePrivate *priv = HILDON_NOTE_GET_PRIVATE (data);
+    g_assert (priv);
 
     switch (priv->note_n)
     {
@@ -1049,5 +1157,7 @@ sound_handling                                  (GtkWidget *widget,
             break;
     };
 
+    priv->idle_handler = 0;
+
     return FALSE;
 }