aboutsummaryrefslogtreecommitdiff
path: root/hildon/hildon-edit-toolbar.c
blob: 7db0ecc501a0c8317d615272b37b0c15d926292e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
 * This file is a part of hildon
 *
 * Copyright (C) 2008, 2009 Nokia Corporation, all rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Free Software Foundation; version 2 of the license.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 *
 */

/**
 * SECTION:hildon-edit-toolbar
 * @short_description: Toolbar for edition of user data.
 *
 * The #HildonEditToolbar is a toolbar which contains a label and two
 * buttons, one of them being an arrow pointing backwards.
 *
 * The label is a description of the action that the user is supposed
 * to do. The button is to be pressed when the user completes the
 * action. The arrow is used to go back to the previous view
 * discarding any changes.
 *
 * Note that those widgets don't do anything themselves by default. To
 * actually peform actions the developer must provide callbacks for
 * them.
 *
 * To add a #HildonEditToolbar to a window use
 * hildon_window_set_edit_toolbar().
 *
 * This toolbar is meant to replace the window title bar by emulating
 * its appearance, so you should put the window in full screen mode
 * with gtk_window_fullscreen().
 *
 * <example>
 * <title>HildonEditToolbar example</title>
 * <programlisting>
 * GtkWidget *window;
 * GtkWidget *toolbar;
 * // Declare more widgets here ...
 * <!-- -->
 * window = hildon_stackable_window_new ();
 * toolbar = hildon_edit_toolbar_new_with_text ("Choose items to delete", "Delete");
 * // Create more widgets here ...
 * <!-- -->
 * // Add toolbar to window
 * hildon_window_set_edit_toolbar (HILDON_WINDOW (window), HILDON_EDIT_TOOLBAR (toolbar));
 * <!-- -->
 * // Add other widgets ...
 * <!-- -->
 * g_signal_connect (toolbar, "button-clicked", G_CALLBACK (delete_button_clicked), someparameter);
 * g_signal_connect_swapped (toolbar, "arrow-clicked", G_CALLBACK (gtk_widget_destroy), window);
 * <!-- -->
 * gtk_widget_show_all (window);
 * gtk_window_fullscreen (GTK_WINDOW (window));
 * </programlisting>
 * </example>
 */

#include                                        "hildon-edit-toolbar.h"
#include                                        "hildon-edit-toolbar-private.h"
#include                                        "hildon-private.h"
#include                                        "hildon-gtk.h"

G_DEFINE_TYPE                                   (HildonEditToolbar, hildon_edit_toolbar, GTK_TYPE_HBOX);

#define                                         TOOLBAR_LEFT_PADDING 24
#define                                         TOOLBAR_RIGHT_PADDING 8

#define                                         HILDON_EDIT_TOOLBAR_ANIMATION_FRAMERATE ((float)1000/150)
#define                                         HILDON_EDIT_TOOLBAR_ANIMATION_TMPL "indicator_update%d"
#define                                         HILDON_EDIT_TOOLBAR_ANIMATION_NFRAMES 8

#define                                         HILDON_EDIT_TOOLBAR_GET_PRIVATE(obj) \
                                                (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
                                                HILDON_TYPE_EDIT_TOOLBAR, HildonEditToolbarPrivate));

typedef struct                                  _HildonEditToolbarPrivate HildonEditToolbarPrivate;

struct                                          _HildonEditToolbarPrivate
{
    GtkBox *hbox;
    GtkLabel *label;
    GtkWidget *animation;
    GtkButton *button;
    GtkButton *arrow;
};

enum {
    BUTTON_CLICKED,
    ARROW_CLICKED,
    N_SIGNALS
};

static guint                                    toolbar_signals [N_SIGNALS] = { 0 };

static void
hildon_edit_toolbar_style_set                   (GtkWidget *widget,
                                                 GtkStyle  *previous_style)
{
    guint width, height;
    HildonEditToolbarPrivate *priv = HILDON_EDIT_TOOLBAR_GET_PRIVATE (widget);

    if (GTK_WIDGET_CLASS (hildon_edit_toolbar_parent_class)->style_set)
        GTK_WIDGET_CLASS (hildon_edit_toolbar_parent_class)->style_set (widget, previous_style);

    gtk_widget_style_get (widget,
                          "arrow-width", &width,
                          "arrow-height", &height,
                          NULL);

    gtk_widget_set_size_request (GTK_WIDGET (priv->arrow), width, height);
}

static gboolean
hildon_edit_toolbar_expose                      (GtkWidget      *widget,
                                                 GdkEventExpose *event)
{
    if (GTK_WIDGET_DRAWABLE (widget)) {
        gtk_paint_flat_box (widget->style,
                            widget->window,
                            GTK_STATE_NORMAL,
                            GTK_SHADOW_NONE,
                            &event->area, widget, "edit-toolbar",
                            widget->allocation.x, widget->allocation.y,
                            widget->allocation.width, widget->allocation.height);
    }

    if (GTK_WIDGET_CLASS (hildon_edit_toolbar_parent_class)->expose_event)
        return GTK_WIDGET_CLASS (hildon_edit_toolbar_parent_class)->expose_event (widget, event);

    return FALSE;
}

static void
hildon_edit_toolbar_class_init                  (HildonEditToolbarClass *klass)
{
    GObjectClass *gobject_class = (GObjectClass *) klass;
    GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;

    widget_class->style_set = hildon_edit_toolbar_style_set;
    widget_class->expose_event = hildon_edit_toolbar_expose;

    g_type_class_add_private (klass, sizeof (HildonEditToolbarPrivate));

    gtk_widget_class_install_style_property (
        widget_class,
        g_param_spec_uint (
            "arrow-width",
            "Width of the arrow button",
            "Width of the arrow button",
            0, G_MAXUINT, 112,
            G_PARAM_READABLE));

    gtk_widget_class_install_style_property (
        widget_class,
        g_param_spec_uint (
            "arrow-height",
            "Height of the arrow button",
            "Height of the arrow button",
            0, G_MAXUINT, 56,
            G_PARAM_READABLE));

    /**
     * HildonEditToolbar::button-clicked:
     * @widget: the object which received the signal.
     *
     * Emitted when the toolbar button has been activated (pressed and released).
     *
     * Since: 2.2
     */
    toolbar_signals[BUTTON_CLICKED] =
        g_signal_new ("button_clicked",
                      G_OBJECT_CLASS_TYPE (gobject_class),
                      G_SIGNAL_RUN_FIRST,
                      0, NULL, NULL,
                      g_cclosure_marshal_VOID__VOID,
                      G_TYPE_NONE, 0);

    /**
     * HildonEditToolbar::arrow-clicked:
     * @widget: the object which received the signal.
     *
     * Emitted when the toolbar back button (arrow) has been activated
     * (pressed and released).
     *
     * Since: 2.2
     */
    toolbar_signals[ARROW_CLICKED] =
        g_signal_new ("arrow_clicked",
                      G_OBJECT_CLASS_TYPE (gobject_class),
                      G_SIGNAL_RUN_FIRST,
                      0, NULL, NULL,
                      g_cclosure_marshal_VOID__VOID,
                      G_TYPE_NONE, 0);
}

static void
button_clicked_cb                               (GtkButton *self,
                                                 HildonEditToolbar *toolbar)
{
    g_signal_emit (toolbar, toolbar_signals[BUTTON_CLICKED], 0);
}

static void
arrow_clicked_cb                                (GtkButton *self,
                                                 HildonEditToolbar *toolbar)
{
    g_signal_emit (toolbar, toolbar_signals[ARROW_CLICKED], 0);
}

static void
hildon_edit_toolbar_init                        (HildonEditToolbar *self)
{
    HildonEditToolbarPrivate *priv = HILDON_EDIT_TOOLBAR_GET_PRIVATE (self);
    GtkWidget *separator;
    GtkAlignment *align;
    GtkBox *hbox = GTK_BOX (self);

    priv->hbox = GTK_BOX (gtk_hbox_new (FALSE, 0));
    align = GTK_ALIGNMENT (gtk_alignment_new (0, 0.5, 1, 1));
    priv->label = GTK_LABEL (gtk_label_new (NULL));
    priv->animation = hildon_private_create_animation (
        HILDON_EDIT_TOOLBAR_ANIMATION_FRAMERATE,
        HILDON_EDIT_TOOLBAR_ANIMATION_TMPL,
        HILDON_EDIT_TOOLBAR_ANIMATION_NFRAMES);
    priv->button = GTK_BUTTON (hildon_gtk_button_new (HILDON_SIZE_AUTO));
    separator = gtk_vseparator_new ();
    priv->arrow = GTK_BUTTON (gtk_button_new ());

    g_object_set (priv->button, "can-focus", FALSE, NULL);
    g_object_set (priv->arrow, "can-focus", FALSE, NULL);

    g_signal_connect (priv->button, "clicked", G_CALLBACK (button_clicked_cb), self);
    g_signal_connect (priv->arrow, "clicked", G_CALLBACK (arrow_clicked_cb), self);

    gtk_box_set_spacing (hbox, 0);
    gtk_alignment_set_padding (align, 0, 0, TOOLBAR_LEFT_PADDING, TOOLBAR_RIGHT_PADDING);

    gtk_widget_set_name (GTK_WIDGET (self), "toolbar-edit-mode");
    gtk_widget_set_name (GTK_WIDGET (priv->arrow), "hildon-edit-toolbar-arrow");

    gtk_container_add (GTK_CONTAINER (align), GTK_WIDGET (priv->hbox));
    gtk_box_pack_start (priv->hbox, GTK_WIDGET (priv->label), TRUE, TRUE, 0);
    gtk_box_pack_start (priv->hbox, priv->animation, TRUE, TRUE, 10);
    gtk_box_pack_start (priv->hbox, GTK_WIDGET (priv->button), FALSE, FALSE, 0);

    gtk_widget_set_no_show_all (priv->animation, TRUE);

    gtk_box_pack_start (hbox, GTK_WIDGET (align), TRUE, TRUE, 0);
    gtk_box_pack_start (hbox, separator, FALSE, FALSE, 0);
    gtk_box_pack_start (hbox, GTK_WIDGET (priv->arrow), FALSE, FALSE, 0);

    gtk_misc_set_alignment (GTK_MISC (priv->label), 0, 0.5);
    gtk_misc_set_alignment (GTK_MISC (priv->animation), 0, 0.5);

    gtk_widget_show_all (GTK_WIDGET (align));
    gtk_widget_show_all (separator);
    gtk_widget_show_all (GTK_WIDGET (priv->arrow));
}

/**
 * hildon_edit_toolbar_set_label:
 * @toolbar: a #HildonEditToolbar
 * @label: a new text for the toolbar label
 *
 * Sets the label of @toolbar to @label. This will clear any
 * previously set value.
 *
 * Since: 2.2
 */
void
hildon_edit_toolbar_set_label                   (HildonEditToolbar *toolbar,
                                                 const gchar       *label)
{
    HildonEditToolbarPrivate *priv;
    g_return_if_fail (HILDON_IS_EDIT_TOOLBAR (toolbar));
    priv = HILDON_EDIT_TOOLBAR_GET_PRIVATE (toolbar);
    gtk_label_set_text (priv->label, label);
}

/**
 * hildon_edit_toolbar_set_button_label:
 * @toolbar: a #HildonEditToolbar
 * @label: a new text for the label of the toolbar button
 *
 * Sets the label of the toolbar button to @label. This will clear any
 * previously set value.
 *
 * Since: 2.2
 */
void
hildon_edit_toolbar_set_button_label            (HildonEditToolbar *toolbar,
                                                 const gchar       *label)
{
    HildonEditToolbarPrivate *priv;
    g_return_if_fail (HILDON_IS_EDIT_TOOLBAR (toolbar));
    priv = HILDON_EDIT_TOOLBAR_GET_PRIVATE (toolbar);
    gtk_button_set_label (priv->button, label);
}

/**
 * hildon_edit_toolbar_new:
 *
 * Creates a new #HildonEditToolbar.
 *
 * Returns: a new #HildonEditToolbar
 *
 * Since: 2.2
 */
GtkWidget *
hildon_edit_toolbar_new                         (void)
{
    return g_object_new (HILDON_TYPE_EDIT_TOOLBAR, NULL);
}

/**
 * hildon_edit_toolbar_new_with_text:
 * @label: Text for the toolbar label.
 * @button: Text for the toolbar button.
 *
 * Creates a new #HildonEditToolbar, with the toolbar label set to
 * @label and the button label set to @button.
 *
 * Returns: a new #HildonEditToolbar
 *
 * Since: 2.2
 */
GtkWidget *
hildon_edit_toolbar_new_with_text               (const gchar *label,
                                                 const gchar *button)
{
    GtkWidget *toolbar = g_object_new (HILDON_TYPE_EDIT_TOOLBAR, NULL);

    hildon_edit_toolbar_set_label (HILDON_EDIT_TOOLBAR (toolbar), label);
    hildon_edit_toolbar_set_button_label (HILDON_EDIT_TOOLBAR (toolbar), button);

    return toolbar;
}

void G_GNUC_INTERNAL
hildon_edit_toolbar_set_progress_indicator      (HildonEditToolbar *toolbar,
                                                 gboolean           show)
{
    HildonEditToolbarPrivate *priv;
    g_return_if_fail (HILDON_IS_EDIT_TOOLBAR (toolbar));
    priv = HILDON_EDIT_TOOLBAR_GET_PRIVATE (toolbar);
    if (show) {
        gtk_widget_show (priv->animation);
        gtk_box_set_child_packing (priv->hbox, GTK_WIDGET (priv->label),
                                   FALSE, FALSE, 0, GTK_PACK_START);
    } else {
        gtk_box_set_child_packing (priv->hbox, GTK_WIDGET (priv->label),
                                   TRUE, TRUE, 0, GTK_PACK_START);
        gtk_widget_hide (priv->animation);
    }
}