2009-02-04 Alberto Garcia <agarcia@igalia.com>
[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  * 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                                         HILDON_EDIT_TOOLBAR_GET_PRIVATE(obj) \
67                                                 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
68                                                 HILDON_TYPE_EDIT_TOOLBAR, HildonEditToolbarPrivate));
69
70 typedef struct                                  _HildonEditToolbarPrivate HildonEditToolbarPrivate;
71
72 struct                                          _HildonEditToolbarPrivate
73 {
74     GtkLabel *label;
75     GtkButton *button;
76     GtkButton *arrow;
77 };
78
79 enum {
80     BUTTON_CLICKED,
81     ARROW_CLICKED,
82     N_SIGNALS
83 };
84
85 static guint                                    toolbar_signals [N_SIGNALS] = { 0 };
86
87 static void
88 hildon_edit_toolbar_style_set                   (GtkWidget *widget,
89                                                  GtkStyle  *previous_style)
90 {
91     guint width, height;
92     HildonEditToolbarPrivate *priv = HILDON_EDIT_TOOLBAR_GET_PRIVATE (widget);
93
94     if (GTK_WIDGET_CLASS (hildon_edit_toolbar_parent_class)->style_set)
95         GTK_WIDGET_CLASS (hildon_edit_toolbar_parent_class)->style_set (widget, previous_style);
96
97     gtk_widget_style_get (widget,
98                           "arrow-width", &width,
99                           "arrow-height", &height,
100                           NULL);
101
102     gtk_widget_set_size_request (GTK_WIDGET (priv->arrow), width, height);
103 }
104
105 static void
106 hildon_edit_toolbar_class_init                  (HildonEditToolbarClass *klass)
107 {
108     GObjectClass *gobject_class = (GObjectClass *) klass;
109     GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
110
111     widget_class->style_set = hildon_edit_toolbar_style_set;
112
113     g_type_class_add_private (klass, sizeof (HildonEditToolbarPrivate));
114
115     gtk_widget_class_install_style_property (
116         widget_class,
117         g_param_spec_uint (
118             "arrow-width",
119             "Width of the arrow button",
120             "Width of the arrow button",
121             0, G_MAXUINT, 112,
122             G_PARAM_READABLE));
123
124     gtk_widget_class_install_style_property (
125         widget_class,
126         g_param_spec_uint (
127             "arrow-height",
128             "Height of the arrow button",
129             "Height of the arrow button",
130             0, G_MAXUINT, 56,
131             G_PARAM_READABLE));
132
133     /**
134      * HildonEditToolbar::button-clicked:
135      * @widget: the object which received the signal.
136      *
137      * Emitted when the toolbar button has been activated (pressed and released).
138      *
139      * Since: 2.2
140      */
141     toolbar_signals[BUTTON_CLICKED] =
142         g_signal_new ("button_clicked",
143                       G_OBJECT_CLASS_TYPE (gobject_class),
144                       G_SIGNAL_RUN_FIRST,
145                       0, NULL, NULL,
146                       g_cclosure_marshal_VOID__VOID,
147                       G_TYPE_NONE, 0);
148
149     /**
150      * HildonEditToolbar::arrow-clicked:
151      * @widget: the object which received the signal.
152      *
153      * Emitted when the toolbar back button (arrow) has been activated
154      * (pressed and released).
155      *
156      * Since: 2.2
157      */
158     toolbar_signals[ARROW_CLICKED] =
159         g_signal_new ("arrow_clicked",
160                       G_OBJECT_CLASS_TYPE (gobject_class),
161                       G_SIGNAL_RUN_FIRST,
162                       0, NULL, NULL,
163                       g_cclosure_marshal_VOID__VOID,
164                       G_TYPE_NONE, 0);
165 }
166
167 static void
168 button_clicked_cb                               (GtkButton *self,
169                                                  HildonEditToolbar *toolbar)
170 {
171     g_signal_emit (toolbar, toolbar_signals[BUTTON_CLICKED], 0);
172 }
173
174 static void
175 arrow_clicked_cb                                (GtkButton *self,
176                                                  HildonEditToolbar *toolbar)
177 {
178     g_signal_emit (toolbar, toolbar_signals[ARROW_CLICKED], 0);
179 }
180
181 static void
182 hildon_edit_toolbar_init                        (HildonEditToolbar *self)
183 {
184     HildonEditToolbarPrivate *priv = HILDON_EDIT_TOOLBAR_GET_PRIVATE (self);
185     GtkBox *hbox = GTK_BOX (self);
186
187     priv->label = GTK_LABEL (gtk_label_new (NULL));
188     priv->button = GTK_BUTTON (hildon_gtk_button_new (HILDON_SIZE_AUTO_WIDTH | HILDON_SIZE_FINGER_HEIGHT));
189     priv->arrow = GTK_BUTTON (gtk_button_new ());
190
191     gtk_button_set_focus_on_click (priv->arrow, FALSE);
192
193     g_signal_connect (priv->button, "clicked", G_CALLBACK (button_clicked_cb), self);
194     g_signal_connect (priv->arrow, "clicked", G_CALLBACK (arrow_clicked_cb), self);
195
196     gtk_box_set_spacing (hbox, 10);
197
198     gtk_widget_set_name (GTK_WIDGET (priv->arrow), "hildon-edit-toolbar-arrow");
199
200     gtk_box_pack_start (hbox, GTK_WIDGET (priv->label), TRUE, TRUE, 0);
201     gtk_box_pack_start (hbox, GTK_WIDGET (priv->button), FALSE, FALSE, 0);
202     gtk_box_pack_start (hbox, GTK_WIDGET (priv->arrow), FALSE, FALSE, 0);
203
204     gtk_misc_set_alignment (GTK_MISC (priv->label), 0, 0.5);
205
206     gtk_widget_show (GTK_WIDGET (priv->label));
207     gtk_widget_show (GTK_WIDGET (priv->button));
208     gtk_widget_show (GTK_WIDGET (priv->arrow));
209 }
210
211 /**
212  * hildon_edit_toolbar_set_label:
213  * @toolbar: a #HildonEditToolbar
214  * @label: a new text for the toolbar label
215  *
216  * Sets the label of @toolbar to @label. This will clear any
217  * previously set value.
218  *
219  * Since: 2.2
220  */
221 void
222 hildon_edit_toolbar_set_label                   (HildonEditToolbar *toolbar,
223                                                  const gchar       *label)
224 {
225     HildonEditToolbarPrivate *priv;
226     g_return_if_fail (HILDON_IS_EDIT_TOOLBAR (toolbar));
227     priv = HILDON_EDIT_TOOLBAR_GET_PRIVATE (toolbar);
228     gtk_label_set_text (priv->label, label);
229 }
230
231 /**
232  * hildon_edit_toolbar_set_button_label:
233  * @toolbar: a #HildonEditToolbar
234  * @label: a new text for the label of the toolbar button
235  *
236  * Sets the label of the toolbar button to @label. This will clear any
237  * previously set value.
238  *
239  * Since: 2.2
240  */
241 void
242 hildon_edit_toolbar_set_button_label            (HildonEditToolbar *toolbar,
243                                                  const gchar       *label)
244 {
245     HildonEditToolbarPrivate *priv;
246     g_return_if_fail (HILDON_IS_EDIT_TOOLBAR (toolbar));
247     priv = HILDON_EDIT_TOOLBAR_GET_PRIVATE (toolbar);
248     gtk_button_set_label (priv->button, label);
249 }
250
251 /**
252  * hildon_edit_toolbar_new:
253  *
254  * Creates a new #HildonEditToolbar.
255  *
256  * Returns: a new #HildonEditToolbar
257  *
258  * Since: 2.2
259  */
260 GtkWidget *
261 hildon_edit_toolbar_new                         (void)
262 {
263     return g_object_new (HILDON_TYPE_EDIT_TOOLBAR, NULL);
264 }
265
266 /**
267  * hildon_edit_toolbar_new_with_text:
268  * @label: Text for the toolbar label.
269  * @button: Text for the toolbar button.
270  *
271  * Creates a new #HildonEditToolbar, with the toolbar label set to
272  * @label and the button label set to @button.
273  *
274  * Returns: a new #HildonEditToolbar
275  *
276  * Since: 2.2
277  */
278 GtkWidget *
279 hildon_edit_toolbar_new_with_text               (const gchar *label,
280                                                  const gchar *button)
281 {
282     GtkWidget *toolbar = g_object_new (HILDON_TYPE_EDIT_TOOLBAR, NULL);
283
284     hildon_edit_toolbar_set_label (HILDON_EDIT_TOOLBAR (toolbar), label);
285     hildon_edit_toolbar_set_button_label (HILDON_EDIT_TOOLBAR (toolbar), button);
286
287     return toolbar;
288 }