2dd12fbaf6622b07a27bc90d06f7fad7310dd240
[hildon] / hildon / 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  * To add a #HildonEditToolbar to a window use
34  * hildon_window_set_edit_toolbar().
35  *
36  * <example>
37  * <title>HildonEditToolbar example</title>
38  * <programlisting>
39  * GtkWidget *window;
40  * GtkWidget *toolbar;
41  * // Declare more widgets here ...
42  * <!-- -->
43  * window = hildon_stackable_window_new ();
44  * toolbar = hildon_edit_toolbar_new_with_text ("Choose items to delete", "Delete");
45  * // Create more widgets here ...
46  * <!-- -->
47  * // Add toolbar to window
48  * hildon_window_set_edit_toolbar (HILDON_WINDOW (window), HILDON_EDIT_TOOLBAR (toolbar));
49  * <!-- -->
50  * // Add other widgets ...
51  * <!-- -->
52  * g_signal_connect (toolbar, "button-clicked", G_CALLBACK (delete_button_clicked), someparameter);
53  * g_signal_connect_swapped (toolbar, "arrow-clicked", G_CALLBACK (gtk_widget_destroy), window);
54  * <!-- -->
55  * gtk_widget_show_all (window);
56  * gtk_window_fullscreen (GTK_WINDOW (window));
57  * </programlisting>
58  * </example>
59  */
60
61 #include                                        "hildon-edit-toolbar.h"
62 #include                                        "hildon-gtk.h"
63
64 G_DEFINE_TYPE                                   (HildonEditToolbar, hildon_edit_toolbar, GTK_TYPE_HBOX);
65
66 #define                                         TOOLBAR_LEFT_PADDING 24
67 #define                                         TOOLBAR_RIGHT_PADDING 8
68
69 #define                                         HILDON_EDIT_TOOLBAR_GET_PRIVATE(obj) \
70                                                 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
71                                                 HILDON_TYPE_EDIT_TOOLBAR, HildonEditToolbarPrivate));
72
73 typedef struct                                  _HildonEditToolbarPrivate HildonEditToolbarPrivate;
74
75 struct                                          _HildonEditToolbarPrivate
76 {
77     GtkLabel *label;
78     GtkButton *button;
79     GtkButton *arrow;
80 };
81
82 enum {
83     BUTTON_CLICKED,
84     ARROW_CLICKED,
85     N_SIGNALS
86 };
87
88 static guint                                    toolbar_signals [N_SIGNALS] = { 0 };
89
90 static void
91 hildon_edit_toolbar_style_set                   (GtkWidget *widget,
92                                                  GtkStyle  *previous_style)
93 {
94     guint width, height;
95     HildonEditToolbarPrivate *priv = HILDON_EDIT_TOOLBAR_GET_PRIVATE (widget);
96
97     if (GTK_WIDGET_CLASS (hildon_edit_toolbar_parent_class)->style_set)
98         GTK_WIDGET_CLASS (hildon_edit_toolbar_parent_class)->style_set (widget, previous_style);
99
100     gtk_widget_style_get (widget,
101                           "arrow-width", &width,
102                           "arrow-height", &height,
103                           NULL);
104
105     gtk_widget_set_size_request (GTK_WIDGET (priv->arrow), width, height);
106 }
107
108 static gboolean
109 hildon_edit_toolbar_expose                      (GtkWidget      *widget,
110                                                  GdkEventExpose *event)
111 {
112     if (GTK_WIDGET_DRAWABLE (widget)) {
113         gtk_paint_flat_box (widget->style,
114                             widget->window,
115                             GTK_STATE_NORMAL,
116                             GTK_SHADOW_NONE,
117                             &event->area, widget, "edit-toolbar",
118                             widget->allocation.x, widget->allocation.y,
119                             widget->allocation.width, widget->allocation.height);
120     }
121
122     if (GTK_WIDGET_CLASS (hildon_edit_toolbar_parent_class)->expose_event)
123         return GTK_WIDGET_CLASS (hildon_edit_toolbar_parent_class)->expose_event (widget, event);
124
125     return FALSE;
126 }
127
128 static void
129 hildon_edit_toolbar_class_init                  (HildonEditToolbarClass *klass)
130 {
131     GObjectClass *gobject_class = (GObjectClass *) klass;
132     GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
133
134     widget_class->style_set = hildon_edit_toolbar_style_set;
135     widget_class->expose_event = hildon_edit_toolbar_expose;
136
137     g_type_class_add_private (klass, sizeof (HildonEditToolbarPrivate));
138
139     gtk_widget_class_install_style_property (
140         widget_class,
141         g_param_spec_uint (
142             "arrow-width",
143             "Width of the arrow button",
144             "Width of the arrow button",
145             0, G_MAXUINT, 112,
146             G_PARAM_READABLE));
147
148     gtk_widget_class_install_style_property (
149         widget_class,
150         g_param_spec_uint (
151             "arrow-height",
152             "Height of the arrow button",
153             "Height of the arrow button",
154             0, G_MAXUINT, 56,
155             G_PARAM_READABLE));
156
157     /**
158      * HildonEditToolbar::button-clicked:
159      * @widget: the object which received the signal.
160      *
161      * Emitted when the toolbar button has been activated (pressed and released).
162      *
163      * Since: 2.2
164      */
165     toolbar_signals[BUTTON_CLICKED] =
166         g_signal_new ("button_clicked",
167                       G_OBJECT_CLASS_TYPE (gobject_class),
168                       G_SIGNAL_RUN_FIRST,
169                       0, NULL, NULL,
170                       g_cclosure_marshal_VOID__VOID,
171                       G_TYPE_NONE, 0);
172
173     /**
174      * HildonEditToolbar::arrow-clicked:
175      * @widget: the object which received the signal.
176      *
177      * Emitted when the toolbar back button (arrow) has been activated
178      * (pressed and released).
179      *
180      * Since: 2.2
181      */
182     toolbar_signals[ARROW_CLICKED] =
183         g_signal_new ("arrow_clicked",
184                       G_OBJECT_CLASS_TYPE (gobject_class),
185                       G_SIGNAL_RUN_FIRST,
186                       0, NULL, NULL,
187                       g_cclosure_marshal_VOID__VOID,
188                       G_TYPE_NONE, 0);
189 }
190
191 static void
192 button_clicked_cb                               (GtkButton *self,
193                                                  HildonEditToolbar *toolbar)
194 {
195     g_signal_emit (toolbar, toolbar_signals[BUTTON_CLICKED], 0);
196 }
197
198 static void
199 arrow_clicked_cb                                (GtkButton *self,
200                                                  HildonEditToolbar *toolbar)
201 {
202     g_signal_emit (toolbar, toolbar_signals[ARROW_CLICKED], 0);
203 }
204
205 static void
206 hildon_edit_toolbar_init                        (HildonEditToolbar *self)
207 {
208     HildonEditToolbarPrivate *priv = HILDON_EDIT_TOOLBAR_GET_PRIVATE (self);
209     GtkWidget *separator;
210     GtkAlignment *align;
211     GtkBox *hbox = GTK_BOX (self);
212     GtkBox *hbox2;
213
214     hbox2 = GTK_BOX (gtk_hbox_new (FALSE, 0));
215     align = GTK_ALIGNMENT (gtk_alignment_new (0, 0.5, 1, 1));
216     priv->label = GTK_LABEL (gtk_label_new (NULL));
217     priv->button = GTK_BUTTON (hildon_gtk_button_new (HILDON_SIZE_AUTO));
218     separator = gtk_vseparator_new ();
219     priv->arrow = GTK_BUTTON (gtk_button_new ());
220
221     g_object_set (priv->button, "can-focus", FALSE, NULL);
222     g_object_set (priv->arrow, "can-focus", FALSE, NULL);
223
224     g_signal_connect (priv->button, "clicked", G_CALLBACK (button_clicked_cb), self);
225     g_signal_connect (priv->arrow, "clicked", G_CALLBACK (arrow_clicked_cb), self);
226
227     gtk_box_set_spacing (hbox, 0);
228     gtk_alignment_set_padding (align, 0, 0, TOOLBAR_LEFT_PADDING, TOOLBAR_RIGHT_PADDING);
229
230     gtk_widget_set_name (GTK_WIDGET (self), "toolbar-edit-mode");
231     gtk_widget_set_name (GTK_WIDGET (priv->arrow), "hildon-edit-toolbar-arrow");
232
233     gtk_container_add (GTK_CONTAINER (align), GTK_WIDGET (hbox2));
234     gtk_box_pack_start (hbox2, GTK_WIDGET (priv->label), TRUE, TRUE, 0);
235     gtk_box_pack_start (hbox2, GTK_WIDGET (priv->button), FALSE, FALSE, 0);
236
237     gtk_box_pack_start (hbox, GTK_WIDGET (align), TRUE, TRUE, 0);
238     gtk_box_pack_start (hbox, separator, FALSE, FALSE, 0);
239     gtk_box_pack_start (hbox, GTK_WIDGET (priv->arrow), FALSE, FALSE, 0);
240
241     gtk_misc_set_alignment (GTK_MISC (priv->label), 0, 0.5);
242
243     gtk_widget_show_all (GTK_WIDGET (align));
244     gtk_widget_show_all (separator);
245     gtk_widget_show_all (GTK_WIDGET (priv->arrow));
246 }
247
248 /**
249  * hildon_edit_toolbar_set_label:
250  * @toolbar: a #HildonEditToolbar
251  * @label: a new text for the toolbar label
252  *
253  * Sets the label of @toolbar to @label. This will clear any
254  * previously set value.
255  *
256  * Since: 2.2
257  */
258 void
259 hildon_edit_toolbar_set_label                   (HildonEditToolbar *toolbar,
260                                                  const gchar       *label)
261 {
262     HildonEditToolbarPrivate *priv;
263     g_return_if_fail (HILDON_IS_EDIT_TOOLBAR (toolbar));
264     priv = HILDON_EDIT_TOOLBAR_GET_PRIVATE (toolbar);
265     gtk_label_set_text (priv->label, label);
266 }
267
268 /**
269  * hildon_edit_toolbar_set_button_label:
270  * @toolbar: a #HildonEditToolbar
271  * @label: a new text for the label of the toolbar button
272  *
273  * Sets the label of the toolbar button to @label. This will clear any
274  * previously set value.
275  *
276  * Since: 2.2
277  */
278 void
279 hildon_edit_toolbar_set_button_label            (HildonEditToolbar *toolbar,
280                                                  const gchar       *label)
281 {
282     HildonEditToolbarPrivate *priv;
283     g_return_if_fail (HILDON_IS_EDIT_TOOLBAR (toolbar));
284     priv = HILDON_EDIT_TOOLBAR_GET_PRIVATE (toolbar);
285     gtk_button_set_label (priv->button, label);
286 }
287
288 /**
289  * hildon_edit_toolbar_new:
290  *
291  * Creates a new #HildonEditToolbar.
292  *
293  * Returns: a new #HildonEditToolbar
294  *
295  * Since: 2.2
296  */
297 GtkWidget *
298 hildon_edit_toolbar_new                         (void)
299 {
300     return g_object_new (HILDON_TYPE_EDIT_TOOLBAR, NULL);
301 }
302
303 /**
304  * hildon_edit_toolbar_new_with_text:
305  * @label: Text for the toolbar label.
306  * @button: Text for the toolbar button.
307  *
308  * Creates a new #HildonEditToolbar, with the toolbar label set to
309  * @label and the button label set to @button.
310  *
311  * Returns: a new #HildonEditToolbar
312  *
313  * Since: 2.2
314  */
315 GtkWidget *
316 hildon_edit_toolbar_new_with_text               (const gchar *label,
317                                                  const gchar *button)
318 {
319     GtkWidget *toolbar = g_object_new (HILDON_TYPE_EDIT_TOOLBAR, NULL);
320
321     hildon_edit_toolbar_set_label (HILDON_EDIT_TOOLBAR (toolbar), label);
322     hildon_edit_toolbar_set_button_label (HILDON_EDIT_TOOLBAR (toolbar), button);
323
324     return toolbar;
325 }