2008-09-24 Claudio Saavedra <csaavedra@igalia.com>
[hildon] / src / hildon-dialog.c
1 /*
2  * This file is a part of hildon
3  *
4  * Copyright (C) 2008 Nokia Corporation, all rights reserved.
5  *
6  * Contact: Karl Lattimer <karl.lattimer@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-dialog
27  * @short_description: Widget representing a popup window in the Hildon framework.
28  * @see_also: #HildonCodeDialog, #HildonColorChooserDialog, #HildonFontSelectionDialog, #HildonGetPasswordDialog, #HildonLoginDialog, #HildonSetPasswordDialog, #HildonSortDialog, #HildonWizardDialog
29  *
30  * The HildonDialog is a GTK widget which represent a popup window in the
31  * Hildon framework. It is derived from the GtkDialog and provides additional
32  * commodities specific to the Hildon framework.
33  *
34  * <example>
35  * <title>Simple <structname>HildonDialog</structname> usage</title>
36  * <programlisting>
37  * void quick_message (gchar *message)
38  * {
39  * <!-- -->
40  *     GtkWidget *dialog, *label;
41  * <!-- -->
42  *     dialog = hildon_dialog_new ();
43  *     label = gtk_label_new (message);
44  * <!-- -->
45  *     g_signal_connect_swapped (dialog,
46  *                               "response",
47  *                               G_CALLBACK (gtk_widget_destroy),
48  *                               dialog);
49  * <!-- -->
50  *     gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox),
51  *                        label);
52  *     gtk_widget_show_all (dialog);
53  * <!-- -->
54  * }
55  * </programlisting>
56  * </example>
57  */
58
59 #include                                        "hildon-dialog.h"
60 #include                                        "hildon-gtk.h"
61
62 G_DEFINE_TYPE (HildonDialog, hildon_dialog, GTK_TYPE_DIALOG);
63
64 static void
65 hildon_dialog_class_init                        (HildonDialogClass *dialog_class)
66 {
67 }
68
69 static void
70 hildon_dialog_init                              (HildonDialog *self)
71 {
72 }
73
74 /**
75  * hildon_dialog_new:
76  *
77  * Creates a new #HildonDialog widget
78  *
79  * Returns: the newly created #HildonDialog
80  */
81 GtkWidget*
82 hildon_dialog_new                               (void)
83 {
84     GtkWidget *self = g_object_new (HILDON_TYPE_DIALOG, NULL);
85
86     return self;
87 }
88
89 /**
90  * hildon_dialog_new_with_buttons:
91  * @title: Title of the dialog, or %NULL
92  * @parent: Transient parent of the dialog, or %NULL
93  * @flags: from #GtkDialogFlags
94  * @first_button_text: stock ID or text to go in first button, or %NULL
95  * @Varargs: response ID for first button, then additional buttons, ending with %NULL
96  *
97  * Creates a new #HildonDialog. See gtk_dialog_new_with_buttons() for
98  * more information.
99  *
100  * Return value: a new #HildonDialog
101  */
102 GtkWidget*
103 hildon_dialog_new_with_buttons                  (const gchar *title,
104                                                  GtkWindow *parent,
105                                                  GtkDialogFlags flags,
106                                                  const gchar *first_button_text,
107                                                  ...)
108 {
109     GtkWidget *dialog;
110
111     dialog = g_object_new (HILDON_TYPE_DIALOG, NULL);
112
113     /* This code is copied from gtk_dialog_new_empty(), as it's a
114      * private function that we cannot use here */
115     if (title)
116         gtk_window_set_title (GTK_WINDOW (dialog), title);
117
118     if (parent)
119         gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
120
121     if (flags & GTK_DIALOG_MODAL)
122         gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
123
124     if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
125         gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
126
127     if (flags & GTK_DIALOG_NO_SEPARATOR)
128         gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
129
130     /* This is almost copied from gtk_dialog_add_buttons_valist() */
131     if (first_button_text != NULL) {
132         va_list args;
133         const gchar *text;
134         gint response_id;
135
136         va_start (args, first_button_text);
137         text = first_button_text;
138         response_id = va_arg (args, gint);
139
140         while (text != NULL) {
141             hildon_dialog_add_button (HILDON_DIALOG (dialog), text, response_id);
142
143             text = va_arg (args, gchar*);
144             if (text == NULL)
145                 break;
146             response_id = va_arg (args, int);
147         }
148         va_end (args);
149     }
150
151     return dialog;
152 }
153
154 /**
155  * hildon_dialog_add_button:
156  * @dialog: a #HildonDialog
157  * @button_text: text of the button, or stock ID
158  * @response_id: response ID for the button.
159  *
160  * Adds a button to the dialog. Works exactly like
161  * gtk_dialog_add_button(), the only difference being that the button
162  * has finger size.
163  *
164  * Returns: the button widget that was added
165  */
166 GtkWidget *
167 hildon_dialog_add_button                        (HildonDialog *dialog,
168                                                  const gchar  *button_text,
169                                                  gint          response_id)
170 {
171     GtkWidget *button;
172     button = gtk_dialog_add_button (GTK_DIALOG (dialog), button_text, response_id);
173     hildon_gtk_widget_set_theme_size (button, HILDON_SIZE_AUTO_WIDTH | HILDON_SIZE_FINGER_HEIGHT);
174     return button;
175 }
176
177 /**
178  * hildon_dialog_add_buttons:
179  * @dialog: a #HildonDialog
180  * @first_button_text: text of the button, or stock ID
181  * @Varargs: response ID for first button, then more text-response_id pairs
182  *
183  * Adds several buttons to the dialog. Works exactly like
184  * gtk_dialog_add_buttons(), the only difference being that the
185  * buttons have finger size.
186  */
187 void
188 hildon_dialog_add_buttons                       (HildonDialog *dialog,
189                                                  const gchar  *first_button_text,
190                                                  ...)
191 {
192     va_list args;
193     const gchar *text;
194     gint response_id;
195
196     va_start (args, first_button_text);
197     text = first_button_text;
198     response_id = va_arg (args, gint);
199
200     while (text != NULL) {
201         hildon_dialog_add_button (HILDON_DIALOG (dialog), text, response_id);
202
203         text = va_arg (args, gchar*);
204         if (text == NULL)
205             break;
206         response_id = va_arg (args, int);
207     }
208
209     va_end (args);
210 }
211