aboutsummaryrefslogtreecommitdiff
path: root/hildon/hildon-check-button.c
blob: ad2da9f78bf1dd04df252e9f0110a7a552dbacbd (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
/*
 * This file is a part of hildon
 *
 * Copyright (C) 2008 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-check-button
 * @short_description: Button with a check box inside
 *
 * #HildonCheckButton is a button containing a label and a check box
 * which will remain 'pressed-in' when clicked. Clicking again will
 * make the check box toggle its state.
 *
 * #HildonCheckButton is similar to the #GtkCheckButton widget, but
 * with a different appearance that combines a standard button and a
 * check box.
 *
 * The state of a #HildonCheckButton can be set using
 * hildon_check_button_set_active(), and retrieved using
 * hildon_check_button_get_active(). The label can be set using
 * gtk_button_set_label() and retrieved using gtk_button_get_label().
 *
 * <note>
 *   <para>
 * #HildonCheckButton does NOT support an image, so don't use
 * gtk_button_set_image().
 *   </para>
 * </note>
 *
 * <example>
 * <title>Using a Hildon check button</title>
 * <programlisting>
 * void
 * button_toggled (HildonCheckButton *button, gpointer user_data)
 * {
 *     gboolean active;
 * <!-- -->
 *     active = hildon_check_button_get_active (button);
 *     if (active)
 *        g_debug ("Button is active");
 *     else
 *        g_debug ("Button is not active");
 * }
 * <!-- -->
 * GtkWidget *
 * create_button (void)
 * {
 *     GtkWidget *button;
 * <!-- -->
 *     button = hildon_check_button_new (HILDON_SIZE_AUTO);
 *     gtk_button_set_label (GTK_BUTTON (button), "Click me");
 * <!-- -->
 *     g_signal_connect (button, "toggled", G_CALLBACK (button_toggled), NULL);
 * <!-- -->
 *     return button;
 * }
 * </programlisting>
 * </example>
 */

#include                                        "hildon-check-button.h"
#include					"hildon-enum-types.h"

enum {
  TOGGLED,
  LAST_SIGNAL
};

enum {
    PROP_SIZE = 1,
    PROP_ACTIVE
};

static guint                                    signals[LAST_SIGNAL] = { 0 };

G_DEFINE_TYPE                                   (HildonCheckButton, hildon_check_button, GTK_TYPE_BUTTON);

#define                                         HILDON_CHECK_BUTTON_GET_PRIVATE(obj) \
                                                (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
                                                HILDON_TYPE_CHECK_BUTTON, HildonCheckButtonPrivate));

struct                                          _HildonCheckButtonPrivate
{
    GtkCellRendererToggle *toggle_renderer;
};

/**
 * hildon_check_button_toggled:
 * @button: A #HildonCheckButton
 *
 * Emits the #HildonCheckButton::toggled signal on the #HildonCheckButton.
 * There is no good reason for an application ever to call this function.
 *
 * Since: 2.2
 */
void
hildon_check_button_toggled                     (HildonCheckButton *button)
{
    g_return_if_fail (HILDON_IS_CHECK_BUTTON (button));

    g_signal_emit (button, signals[TOGGLED], 0);
}

/**
 * hildon_check_button_set_active:
 * @button: A #HildonCheckButton
 * @is_active: new state for the button
 *
 * Sets the status of a #HildonCheckButton. Set to %TRUE if you want
 * @button to be 'pressed-in', and %FALSE to raise it. This action
 * causes the #HildonCheckButton::toggled signal to be emitted.
 *
 * Since: 2.2
 **/
void
hildon_check_button_set_active                  (HildonCheckButton *button,
                                                 gboolean           is_active)
{
    gboolean prev_is_active;

    g_return_if_fail (HILDON_IS_CHECK_BUTTON (button));

    prev_is_active = hildon_check_button_get_active (button);

    if (prev_is_active != is_active) {
        gtk_button_clicked (GTK_BUTTON (button));
        gtk_widget_queue_draw (GTK_WIDGET (button));
    }
}

/**
 * hildon_check_button_get_active:
 * @button: A #HildonCheckButton
 *
 * Gets the current state of @button.
 *
 * Return value: %TRUE if @button is active, %FALSE otherwise.
 *
 * Since: 2.2
 **/
gboolean
hildon_check_button_get_active                  (HildonCheckButton *button)
{
    g_return_val_if_fail (HILDON_IS_CHECK_BUTTON (button), FALSE);

    return gtk_cell_renderer_toggle_get_active (button->priv->toggle_renderer);
}

/**
 * hildon_check_button_new:
 * @size: Flags indicating the size of the new button
 *
 * Creates a new #HildonCheckButton.
 *
 * Return value: A newly created #HildonCheckButton
 *
 * Since: 2.2
 **/
GtkWidget *
hildon_check_button_new                         (HildonSizeType size)
{
    return g_object_new (HILDON_TYPE_CHECK_BUTTON, "size", size, NULL);
}

static void
hildon_check_button_clicked                     (GtkButton *button)
{
    HildonCheckButton *checkbutton = HILDON_CHECK_BUTTON (button);
    gboolean current = hildon_check_button_get_active (checkbutton);

    gtk_cell_renderer_toggle_set_active (checkbutton->priv->toggle_renderer, !current);

    hildon_check_button_toggled (checkbutton);
}

static void
hildon_check_button_apply_style                 (GtkWidget *widget)
{
    guint checkbox_size;
    HildonCheckButtonPrivate *priv = HILDON_CHECK_BUTTON (widget)->priv;

    gtk_widget_style_get (widget, "checkbox-size", &checkbox_size, NULL);

    g_object_set (priv->toggle_renderer, "indicator-size", checkbox_size, NULL);
}

static void
hildon_check_button_style_set                   (GtkWidget *widget,
                                                 GtkStyle  *previous_style)
{
    if (GTK_WIDGET_CLASS (hildon_check_button_parent_class)->style_set)
        GTK_WIDGET_CLASS (hildon_check_button_parent_class)->style_set (widget, previous_style);

    hildon_check_button_apply_style (widget);
}

static void
set_property                                    (GObject      *object,
                                                 guint         prop_id,
                                                 const GValue *value,
                                                 GParamSpec   *pspec)
{
    switch (prop_id)
    {
    case PROP_SIZE:
        hildon_gtk_widget_set_theme_size (GTK_WIDGET (object), g_value_get_flags (value));
        break;
    case PROP_ACTIVE:
        hildon_check_button_set_active (HILDON_CHECK_BUTTON (object), g_value_get_boolean (value));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
get_property                                    (GObject      *object,
                                                 guint         prop_id,
                                                 GValue       *value,
                                                 GParamSpec   *pspec)
{
    switch (prop_id)
    {
    case PROP_ACTIVE:
        g_value_set_boolean (value, hildon_check_button_get_active (HILDON_CHECK_BUTTON (object)));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
hildon_check_button_class_init                  (HildonCheckButtonClass *klass)
{
    GObjectClass *gobject_class = (GObjectClass*) klass;
    GtkWidgetClass *widget_class = (GtkWidgetClass*) klass;
    GtkButtonClass *button_class = (GtkButtonClass*) klass;

    gobject_class->set_property = set_property;
    gobject_class->get_property = get_property;
    widget_class->style_set = hildon_check_button_style_set;
    button_class->clicked = hildon_check_button_clicked;

    klass->toggled = NULL;

    /**
     * HildonCheckButton::toggled
     *
     * Emitted when the #HildonCheckButton's state is changed.
     *
     * Since: 2.2
     */
    signals[TOGGLED] =
        g_signal_new ("toggled",
                      G_OBJECT_CLASS_TYPE (gobject_class),
                      G_SIGNAL_RUN_FIRST,
                      G_STRUCT_OFFSET (HildonCheckButtonClass, toggled),
                      NULL, NULL,
                      g_cclosure_marshal_VOID__VOID,
                      G_TYPE_NONE, 0);

    gtk_widget_class_install_style_property (
        widget_class,
        g_param_spec_uint (
            "checkbox-size",
            "Size of the check box",
            "Size of the check box",
            0, G_MAXUINT, 26,
            G_PARAM_READABLE));

    g_object_class_install_property (
        gobject_class,
        PROP_SIZE,
        g_param_spec_flags (
            "size",
            "Size",
            "Size request for the button",
            HILDON_TYPE_SIZE_TYPE,
            HILDON_SIZE_AUTO,
            G_PARAM_WRITABLE));

    g_object_class_install_property (
            gobject_class,
            PROP_ACTIVE,
            g_param_spec_boolean (
                    "active",
                    "Active",
                    "Whether the check button is active or not",
                    FALSE,
                    G_PARAM_READWRITE));

    g_type_class_add_private (klass, sizeof (HildonCheckButtonPrivate));
}

static void
hildon_check_button_init                        (HildonCheckButton *button)
{
    HildonCheckButtonPrivate *priv = HILDON_CHECK_BUTTON_GET_PRIVATE (button);
    GtkWidget *cell_view = gtk_cell_view_new ();

    /* Store private part */
    button->priv = priv;

    /* Make sure that the check box is always shown, no matter the value of gtk-button-images */
    g_signal_connect (cell_view, "notify::visible", G_CALLBACK (gtk_widget_show), NULL);

    /* Create toggle renderer and pack it into the cell view */
    priv->toggle_renderer = GTK_CELL_RENDERER_TOGGLE (gtk_cell_renderer_toggle_new ());
    gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cell_view),
                                GTK_CELL_RENDERER (priv->toggle_renderer), FALSE);

    /* Add cell view to the image */
    gtk_button_set_image (GTK_BUTTON (button), cell_view);

    gtk_button_set_focus_on_click (GTK_BUTTON (button), FALSE);
    g_object_set (G_OBJECT (button), "xalign", 0.0, NULL);

    hildon_check_button_apply_style (GTK_WIDGET (button));
}