d1afb15958c64126c5dc814b619cc6042338291f
[hildon] / src / hildon-edit-toolbar.c
1 /*
2  * This file is a part of hildon
3  *
4  * Copyright (C) 2008 Nokia Corporation, all rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser Public License as published by
8  * the Free Software Foundation; version 2 of the license.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser Public License for more details.
14  *
15  */
16
17 /**
18  * SECTION:hildon-edit-toolbar
19  * @short_description: Widget representing a toolbar for editing.
20  *
21  * The #HildonEditToolbar is a toolbar which contains a label and two
22  * buttons, one of them being an arrow pointing backwards.
23  *
24  * The label is a description of the action that the user is supposed
25  * to do. The button is to be pressed when the user completes the
26  * action. The arrow is used to go back to the previous view
27  * discarding any changes.
28  *
29  * Note that those widgets don't do anything themselves by default. To
30  * actually peform actions the developer must provide callbacks for
31  * them.
32  *
33  * <example>
34  * <title>HildonEditToolbar example</title>
35  * <programlisting>
36  * GtkWidget *window;
37  * GtkWidget *vbox;
38  * GtkWidget *toolbar;
39  * // Declare more widgets here ...
40  * <!-- -->
41  * window = hildon_stackable_window_new ();
42  * vbox = gtk_vbox_new (FALSE, 10);
43  * toolbar = hildon_edit_toolbar_new_with_text ("Choose items to delete", "Delete");
44  * // Create more widgets here ...
45  * <!-- -->
46  * gtk_container_add (GTK_CONTAINER (window), vbox);
47  * gtk_box_pack_start (GTK_BOX (vbox), toolbar, FALSE, FALSE, 0);
48  * // Pack more widgets here ...
49  * <!-- -->
50  * g_signal_connect (toolbar, "button-clicked", G_CALLBACK (delete_button_clicked), someparameter);
51  * g_signal_connect_swapped (toolbar, "arrow-clicked", G_CALLBACK (gtk_widget_destroy), window);
52  * <!-- -->
53  * gtk_widget_show_all (window);
54  * gtk_window_fullscreen (GTK_WINDOW (window));
55  * </programlisting>
56  * </example>
57  */
58
59 #include                                        "hildon-edit-toolbar.h"
60 #include                                        "hildon-gtk.h"
61
62 G_DEFINE_TYPE                                   (HildonEditToolbar, hildon_edit_toolbar, GTK_TYPE_HBOX);
63
64 #define                                         HILDON_EDIT_TOOLBAR_GET_PRIVATE(obj) \
65                                                 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
66                                                 HILDON_TYPE_EDIT_TOOLBAR, HildonEditToolbarPrivate));
67
68 typedef struct                                  _HildonEditToolbarPrivate HildonEditToolbarPrivate;
69
70 struct                                          _HildonEditToolbarPrivate
71 {
72     GtkLabel *label;
73     GtkButton *button;
74     GtkButton *arrow;
75 };
76
77 enum {
78     BUTTON_CLICKED,
79     ARROW_CLICKED,
80     N_SIGNALS
81 };
82
83 static guint                                    toolbar_signals [N_SIGNALS] = { 0 };
84
85 static void
86 hildon_edit_toolbar_style_set                   (GtkWidget *widget,
87                                                  GtkStyle  *previous_style)
88 {
89     guint width, height;
90     HildonEditToolbarPrivate *priv = HILDON_EDIT_TOOLBAR_GET_PRIVATE (widget);
91
92     if (GTK_WIDGET_CLASS (hildon_edit_toolbar_parent_class)->style_set)
93         GTK_WIDGET_CLASS (hildon_edit_toolbar_parent_class)->style_set (widget, previous_style);
94
95     gtk_widget_style_get (widget,
96                           "arrow-width", &width,
97                           "arrow-height", &height,
98                           NULL);
99
100     gtk_widget_set_size_request (GTK_WIDGET (priv->arrow), width, height);
101 }
102
103 static void
104 hildon_edit_toolbar_class_init                  (HildonEditToolbarClass *klass)
105 {
106     GObjectClass *gobject_class = (GObjectClass *) klass;
107     GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
108
109     widget_class->style_set = hildon_edit_toolbar_style_set;
110
111     g_type_class_add_private (klass, sizeof (HildonEditToolbarPrivate));
112
113     gtk_widget_class_install_style_property (
114         widget_class,
115         g_param_spec_uint (
116             "arrow-width",
117             "Width of the arrow button",
118             "Width of the arrow button",
119             0, G_MAXUINT, 112,
120             G_PARAM_READABLE));
121
122     gtk_widget_class_install_style_property (
123         widget_class,
124         g_param_spec_uint (
125             "arrow-height",
126             "Height of the arrow button",
127             "Height of the arrow button",
128             0, G_MAXUINT, 56,
129             G_PARAM_READABLE));
130
131     /**
132      * HildonEditToolbar::button-clicked:
133      * @widget: the object which received the signal.
134      *
135      * Emitted when the toolbar button has been activated (pressed and released).
136      *
137      * Since: 2.2
138      */
139     toolbar_signals[BUTTON_CLICKED] =
140         g_signal_new ("button_clicked",
141                       G_OBJECT_CLASS_TYPE (gobject_class),
142                       G_SIGNAL_RUN_FIRST,
143                       0, NULL, NULL,
144                       g_cclosure_marshal_VOID__VOID,
145                       G_TYPE_NONE, 0);
146
147     /**
148      * HildonEditToolbar::arrow-clicked:
149      * @widget: the object which received the signal.
150      *
151      * Emitted when the toolbar back button (arrow) has been activated
152      * (pressed and released).
153      *
154      * Since: 2.2
155      */
156     toolbar_signals[ARROW_CLICKED] =
157         g_signal_new ("arrow_clicked",
158                       G_OBJECT_CLASS_TYPE (gobject_class),
159                       G_SIGNAL_RUN_FIRST,
160                       0, NULL, NULL,
161                       g_cclosure_marshal_VOID__VOID,
162                       G_TYPE_NONE, 0);
163 }
164
165 static void
166 button_clicked_cb                               (GtkButton *self,
167                                                  HildonEditToolbar *toolbar)
168 {
169     g_signal_emit (toolbar, toolbar_signals[BUTTON_CLICKED], 0);
170 }
171
172 static void
173 arrow_clicked_cb                                (GtkButton *self,
174                                                  HildonEditToolbar *toolbar)
175 {
176     g_signal_emit (toolbar, toolbar_signals[ARROW_CLICKED], 0);
177 }
178
179 static void
180 hildon_edit_toolbar_init                        (HildonEditToolbar *self)
181 {
182     HildonEditToolbarPrivate *priv = HILDON_EDIT_TOOLBAR_GET_PRIVATE (self);
183     GtkBox *hbox = GTK_BOX (self);
184
185     priv->label = GTK_LABEL (gtk_label_new (NULL));
186     priv->button = GTK_BUTTON (hildon_gtk_button_new (HILDON_SIZE_AUTO_WIDTH | HILDON_SIZE_FINGER_HEIGHT));
187     priv->arrow = GTK_BUTTON (gtk_button_new ());
188
189     gtk_button_set_focus_on_click (priv->arrow, FALSE);
190
191     g_signal_connect (priv->button, "clicked", G_CALLBACK (button_clicked_cb), self);
192     g_signal_connect (priv->arrow, "clicked", G_CALLBACK (arrow_clicked_cb), self);
193
194     gtk_box_set_spacing (hbox, 10);
195
196     gtk_widget_set_name (GTK_WIDGET (priv->arrow), "hildon-edit-toolbar-arrow");
197
198     gtk_box_pack_start (hbox, GTK_WIDGET (priv->label), TRUE, TRUE, 0);
199     gtk_box_pack_start (hbox, GTK_WIDGET (priv->button), FALSE, FALSE, 0);
200     gtk_box_pack_start (hbox, GTK_WIDGET (priv->arrow), FALSE, FALSE, 0);
201
202     gtk_misc_set_alignment (GTK_MISC (priv->label), 0, 0.5);
203
204     gtk_widget_show (GTK_WIDGET (priv->label));
205     gtk_widget_show (GTK_WIDGET (priv->button));
206     gtk_widget_show (GTK_WIDGET (priv->arrow));
207 }
208
209 /**
210  * hildon_edit_toolbar_set_label:
211  * @toolbar: a #HildonEditToolbar
212  * @label: a new text for the toolbar label
213  *
214  * Sets the label of @toolbar to @label. This will clear any
215  * previously set value.
216  *
217  * Since: 2.2
218  */
219 void
220 hildon_edit_toolbar_set_label                   (HildonEditToolbar *toolbar,
221                                                  const gchar       *label)
222 {
223     HildonEditToolbarPrivate *priv;
224     g_return_if_fail (HILDON_IS_EDIT_TOOLBAR (toolbar));
225     priv = HILDON_EDIT_TOOLBAR_GET_PRIVATE (toolbar);
226     gtk_label_set_text (priv->label, label);
227 }
228
229 /**
230  * hildon_edit_toolbar_set_button_label:
231  * @toolbar: a #HildonEditToolbar
232  * @label: a new text for the label of the toolbar button
233  *
234  * Sets the label of the toolbar button to @label. This will clear any
235  * previously set value.
236  *
237  * Since: 2.2
238  */
239 void
240 hildon_edit_toolbar_set_button_label            (HildonEditToolbar *toolbar,
241                                                  const gchar       *label)
242 {
243     HildonEditToolbarPrivate *priv;
244     g_return_if_fail (HILDON_IS_EDIT_TOOLBAR (toolbar));
245     priv = HILDON_EDIT_TOOLBAR_GET_PRIVATE (toolbar);
246     gtk_button_set_label (priv->button, label);
247 }
248
249 /**
250  * hildon_edit_toolbar_new:
251  *
252  * Creates a new #HildonEditToolbar.
253  *
254  * Returns: a new #HildonEditToolbar
255  *
256  * Since: 2.2
257  */
258 GtkWidget *
259 hildon_edit_toolbar_new                         (void)
260 {
261     return g_object_new (HILDON_TYPE_EDIT_TOOLBAR, NULL);
262 }
263
264 /**
265  * hildon_edit_toolbar_new_with_text:
266  * @label: Text for the toolbar label.
267  * @button: Text for the toolbar button.
268  *
269  * Creates a new #HildonEditToolbar, with the toolbar label set to
270  * @label and the button label set to @button.
271  *
272  * Returns: a new #HildonEditToolbar
273  *
274  * Since: 2.2
275  */
276 GtkWidget *
277 hildon_edit_toolbar_new_with_text               (const gchar *label,
278                                                  const gchar *button)
279 {
280     GtkWidget *toolbar = g_object_new (HILDON_TYPE_EDIT_TOOLBAR, NULL);
281
282     hildon_edit_toolbar_set_label (HILDON_EDIT_TOOLBAR (toolbar), label);
283     hildon_edit_toolbar_set_button_label (HILDON_EDIT_TOOLBAR (toolbar), button);
284
285     return toolbar;
286 }