2008-11-25 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  * <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
86 static void
87 hildon_edit_toolbar_class_init                  (HildonEditToolbarClass *klass)
88 {
89     GObjectClass *gobject_class = (GObjectClass *) klass;
90
91     g_type_class_add_private (klass, sizeof (HildonEditToolbarPrivate));
92
93     /**
94      * HildonEditToolbar::button-clicked:
95      * @widget: the object which received the signal.
96      *
97      * Emitted when the toolbar button has been activated (pressed and released).
98      *
99      */
100     toolbar_signals[BUTTON_CLICKED] =
101         g_signal_new ("button_clicked",
102                       G_OBJECT_CLASS_TYPE (gobject_class),
103                       G_SIGNAL_RUN_FIRST,
104                       0, NULL, NULL,
105                       g_cclosure_marshal_VOID__VOID,
106                       G_TYPE_NONE, 0);
107
108     /**
109      * HildonEditToolbar::arrow-clicked:
110      * @widget: the object which received the signal.
111      *
112      * Emitted when the toolbar back button (arrow) has been activated
113      * (pressed and released).
114      *
115      */
116     toolbar_signals[ARROW_CLICKED] =
117         g_signal_new ("arrow_clicked",
118                       G_OBJECT_CLASS_TYPE (gobject_class),
119                       G_SIGNAL_RUN_FIRST,
120                       0, NULL, NULL,
121                       g_cclosure_marshal_VOID__VOID,
122                       G_TYPE_NONE, 0);
123 }
124
125 static void
126 button_clicked_cb                               (GtkButton *self,
127                                                  HildonEditToolbar *toolbar)
128 {
129     g_signal_emit (toolbar, toolbar_signals[BUTTON_CLICKED], 0);
130 }
131
132 static void
133 arrow_clicked_cb                                (GtkButton *self,
134                                                  HildonEditToolbar *toolbar)
135 {
136     g_signal_emit (toolbar, toolbar_signals[ARROW_CLICKED], 0);
137 }
138
139 static void
140 hildon_edit_toolbar_init                        (HildonEditToolbar *self)
141 {
142     HildonEditToolbarPrivate *priv = HILDON_EDIT_TOOLBAR_GET_PRIVATE (self);
143     GtkBox *hbox = GTK_BOX (self);
144
145     priv->label = GTK_LABEL (gtk_label_new (NULL));
146     priv->button = GTK_BUTTON (hildon_gtk_button_new (HILDON_SIZE_AUTO_WIDTH | HILDON_SIZE_FINGER_HEIGHT));
147     priv->arrow = GTK_BUTTON (gtk_button_new ());
148
149     gtk_button_set_image (priv->arrow, gtk_image_new_from_stock (GTK_STOCK_GO_BACK, GTK_ICON_SIZE_LARGE_TOOLBAR));
150     gtk_button_set_relief (priv->arrow, GTK_RELIEF_NONE);
151     gtk_button_set_focus_on_click (priv->arrow, FALSE);
152
153     g_signal_connect (priv->button, "clicked", G_CALLBACK (button_clicked_cb), self);
154     g_signal_connect (priv->arrow, "clicked", G_CALLBACK (arrow_clicked_cb), self);
155
156     /* Temporary values, should be replaced by properties or fixed values from the specs */
157     gtk_box_set_spacing (hbox, 10);
158     gtk_widget_set_size_request (GTK_WIDGET (priv->arrow), 50, -1);
159
160     gtk_box_pack_start (hbox, GTK_WIDGET (priv->label), TRUE, TRUE, 0);
161     gtk_box_pack_start (hbox, GTK_WIDGET (priv->button), FALSE, FALSE, 0);
162     gtk_box_pack_start (hbox, GTK_WIDGET (priv->arrow), FALSE, FALSE, 0);
163
164     gtk_misc_set_alignment (GTK_MISC (priv->label), 0, 0.5);
165
166     gtk_widget_show (GTK_WIDGET (priv->label));
167     gtk_widget_show (GTK_WIDGET (priv->button));
168     gtk_widget_show (GTK_WIDGET (priv->arrow));
169 }
170
171 /**
172  * hildon_edit_toolbar_set_label:
173  * @toolbar: a #HildonEditToolbar
174  * @label: a new text for the toolbar label
175  *
176  * Sets the label of @toolbar to @label. This will clear any
177  * previously set value.
178  */
179 void
180 hildon_edit_toolbar_set_label                   (HildonEditToolbar *toolbar,
181                                                  const gchar       *label)
182 {
183     HildonEditToolbarPrivate *priv;
184     g_return_if_fail (HILDON_IS_EDIT_TOOLBAR (toolbar));
185     priv = HILDON_EDIT_TOOLBAR_GET_PRIVATE (toolbar);
186     gtk_label_set_text (priv->label, label);
187 }
188
189 /**
190  * hildon_edit_toolbar_set_button_label:
191  * @toolbar: a #HildonEditToolbar
192  * @label: a new text for the label of the toolbar button
193  *
194  * Sets the label of the toolbar button to @label. This will clear any
195  * previously set value.
196  */
197 void
198 hildon_edit_toolbar_set_button_label            (HildonEditToolbar *toolbar,
199                                                  const gchar       *label)
200 {
201     HildonEditToolbarPrivate *priv;
202     g_return_if_fail (HILDON_IS_EDIT_TOOLBAR (toolbar));
203     priv = HILDON_EDIT_TOOLBAR_GET_PRIVATE (toolbar);
204     gtk_button_set_label (priv->button, label);
205 }
206
207 /**
208  * hildon_edit_toolbar_new:
209  *
210  * Creates a new #HildonEditToolbar.
211  *
212  * Returns: a new #HildonEditToolbar
213  */
214 GtkWidget *
215 hildon_edit_toolbar_new                         (void)
216 {
217     return g_object_new (HILDON_TYPE_EDIT_TOOLBAR, NULL);
218 }
219
220 /**
221  * hildon_edit_toolbar_new_with_text:
222  *
223  * Creates a new #HildonEditToolbar, with the toolbar label set to
224  * @label and the button label set to @button.
225  *
226  * Returns: a new #HildonEditToolbar
227  */
228 GtkWidget *
229 hildon_edit_toolbar_new_with_text               (const gchar *label,
230                                                  const gchar *button)
231 {
232     GtkWidget *toolbar = g_object_new (HILDON_TYPE_EDIT_TOOLBAR, NULL);
233
234     hildon_edit_toolbar_set_label (HILDON_EDIT_TOOLBAR (toolbar), label);
235     hildon_edit_toolbar_set_button_label (HILDON_EDIT_TOOLBAR (toolbar), button);
236
237     return toolbar;
238 }