2008-08-28 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
34 #include                                        "hildon-edit-toolbar.h"
35 #include                                        "hildon-gtk.h"
36
37 G_DEFINE_TYPE                                   (HildonEditToolbar, hildon_edit_toolbar, GTK_TYPE_HBOX);
38
39 #define                                         HILDON_EDIT_TOOLBAR_GET_PRIVATE(obj) \
40                                                 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
41                                                 HILDON_TYPE_EDIT_TOOLBAR, HildonEditToolbarPrivate));
42
43 typedef struct                                  _HildonEditToolbarPrivate HildonEditToolbarPrivate;
44
45 struct                                          _HildonEditToolbarPrivate
46 {
47     GtkLabel *label;
48     GtkButton *button;
49     GtkButton *arrow;
50 };
51
52 enum {
53     BUTTON_CLICKED,
54     ARROW_CLICKED,
55     N_SIGNALS
56 };
57
58 static guint                                    toolbar_signals [N_SIGNALS] = { 0 };
59
60
61 static void
62 hildon_edit_toolbar_class_init                  (HildonEditToolbarClass *klass)
63 {
64     GObjectClass *gobject_class = (GObjectClass *) klass;
65
66     g_type_class_add_private (klass, sizeof (HildonEditToolbarPrivate));
67
68     /**
69      * HildonEditToolbar::button-clicked:
70      * @widget: the object which received the signal.
71      *
72      * Emitted when the toolbar button has been activated (pressed and released).
73      *
74      */
75     toolbar_signals[BUTTON_CLICKED] =
76         g_signal_new ("button_clicked",
77                       G_OBJECT_CLASS_TYPE (gobject_class),
78                       G_SIGNAL_RUN_FIRST,
79                       0, NULL, NULL,
80                       g_cclosure_marshal_VOID__VOID,
81                       G_TYPE_NONE, 0);
82
83     /**
84      * HildonEditToolbar::arrow-clicked:
85      * @widget: the object which received the signal.
86      *
87      * Emitted when the toolbar back button (arrow) has been activated
88      * (pressed and released).
89      *
90      */
91     toolbar_signals[ARROW_CLICKED] =
92         g_signal_new ("arrow_clicked",
93                       G_OBJECT_CLASS_TYPE (gobject_class),
94                       G_SIGNAL_RUN_FIRST,
95                       0, NULL, NULL,
96                       g_cclosure_marshal_VOID__VOID,
97                       G_TYPE_NONE, 0);
98 }
99
100 static void
101 button_clicked_cb                               (GtkButton *self,
102                                                  HildonEditToolbar *toolbar)
103 {
104     g_signal_emit (toolbar, toolbar_signals[BUTTON_CLICKED], 0);
105 }
106
107 static void
108 arrow_clicked_cb                                (GtkButton *self,
109                                                  HildonEditToolbar *toolbar)
110 {
111     g_signal_emit (toolbar, toolbar_signals[ARROW_CLICKED], 0);
112 }
113
114 static void
115 hildon_edit_toolbar_init                        (HildonEditToolbar *self)
116 {
117     HildonEditToolbarPrivate *priv = HILDON_EDIT_TOOLBAR_GET_PRIVATE (self);
118     GtkBox *hbox = GTK_BOX (self);
119
120     priv->label = GTK_LABEL (gtk_label_new (NULL));
121     priv->button = GTK_BUTTON (hildon_gtk_button_new (HILDON_SIZE_AUTO_WIDTH | HILDON_SIZE_FINGER_HEIGHT));
122     priv->arrow = GTK_BUTTON (gtk_button_new ());
123
124     gtk_button_set_image (priv->arrow, gtk_image_new_from_stock (GTK_STOCK_GO_BACK, GTK_ICON_SIZE_LARGE_TOOLBAR));
125     gtk_button_set_relief (priv->arrow, GTK_RELIEF_NONE);
126     gtk_button_set_focus_on_click (priv->arrow, FALSE);
127
128     g_signal_connect (priv->button, "clicked", G_CALLBACK (button_clicked_cb), self);
129     g_signal_connect (priv->arrow, "clicked", G_CALLBACK (arrow_clicked_cb), self);
130
131     /* Temporary values, should be replaced by properties or fixed values from the specs */
132     gtk_box_set_spacing (hbox, 10);
133     gtk_widget_set_size_request (GTK_WIDGET (priv->arrow), 50, -1);
134
135     gtk_box_pack_start (hbox, GTK_WIDGET (priv->label), TRUE, TRUE, 0);
136     gtk_box_pack_start (hbox, GTK_WIDGET (priv->button), FALSE, FALSE, 0);
137     gtk_box_pack_start (hbox, GTK_WIDGET (priv->arrow), FALSE, FALSE, 0);
138
139     gtk_misc_set_alignment (GTK_MISC (priv->label), 0, 0.5);
140
141     gtk_widget_show (GTK_WIDGET (priv->label));
142     gtk_widget_show (GTK_WIDGET (priv->button));
143     gtk_widget_show (GTK_WIDGET (priv->arrow));
144 }
145
146 /**
147  * hildon_edit_toolbar_set_label:
148  * @toolbar: a #HildonEditToolbar
149  * @label: a new text for the toolbar label
150  *
151  * Sets the label of @toolbar to @label. This will clear any
152  * previously set value.
153  */
154 void
155 hildon_edit_toolbar_set_label                   (HildonEditToolbar *toolbar,
156                                                  const gchar       *label)
157 {
158     HildonEditToolbarPrivate *priv;
159     g_return_if_fail (HILDON_IS_EDIT_TOOLBAR (toolbar));
160     priv = HILDON_EDIT_TOOLBAR_GET_PRIVATE (toolbar);
161     gtk_label_set_text (priv->label, label);
162 }
163
164 /**
165  * hildon_edit_toolbar_set_button_label:
166  * @toolbar: a #HildonEditToolbar
167  * @label: a new text for the label of the toolbar button
168  *
169  * Sets the label of the toolbar button to @label. This will clear any
170  * previously set value.
171  */
172 void
173 hildon_edit_toolbar_set_button_label            (HildonEditToolbar *toolbar,
174                                                  const gchar       *label)
175 {
176     HildonEditToolbarPrivate *priv;
177     g_return_if_fail (HILDON_IS_EDIT_TOOLBAR (toolbar));
178     priv = HILDON_EDIT_TOOLBAR_GET_PRIVATE (toolbar);
179     gtk_button_set_label (priv->button, label);
180 }
181
182 /**
183  * hildon_edit_toolbar_new:
184  *
185  * Creates a new #HildonEditToolbar.
186  *
187  * Returns: a new #HildonEditToolbar
188  */
189 GtkWidget *
190 hildon_edit_toolbar_new                         (void)
191 {
192     return g_object_new (HILDON_TYPE_EDIT_TOOLBAR, NULL);
193 }
194
195 /**
196  * hildon_edit_toolbar_new_with_text:
197  *
198  * Creates a new #HildonEditToolbar, with the toolbar label set to
199  * @label and the button label set to @button.
200  *
201  * Returns: a new #HildonEditToolbar
202  */
203 GtkWidget *
204 hildon_edit_toolbar_new_with_text               (const gchar *label,
205                                                  const gchar *button)
206 {
207     GtkWidget *toolbar = g_object_new (HILDON_TYPE_EDIT_TOOLBAR, NULL);
208
209     hildon_edit_toolbar_set_label (HILDON_EDIT_TOOLBAR (toolbar), label);
210     hildon_edit_toolbar_set_button_label (HILDON_EDIT_TOOLBAR (toolbar), button);
211
212     return toolbar;
213 }