61cc3670b3221150bff2c91cf33641c45bc0f6c9
[hildon] / src / hildon-toggle-button.c
1 /*
2  * This file is a part of hildon
3  *
4  * Copyright (C) 2008 Nokia Corporation, all rights reserved.
5  *
6  * Contact: Karl Lattimer <karl.lattimer@nokia.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser Public License as published by
10  * the Free Software Foundation; version 2 of the license.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Lesser Public License for more details.
16  *
17  */
18
19 /**
20  * SECTION:hildon-toggle-button
21  * @short_description: Widget representing a toggle button in the Hildon framework.
22  *
23  * The #HildonToggleButton is a GTK widget which represents a toggle
24  * button. It is derived from the GtkToggleButton widget and provides
25  * additional commodities specific to the Hildon framework.
26  *
27  * The height of a #HildonToggleButton can be set to either "finger" height
28  * or "thumb" height. It can also be configured to use halfscreen or
29  * fullscreen width. Alternatively, either dimension can be set to
30  * "auto" so it behaves like a standard GtkToggleButton.
31  */
32
33 #include                                        "hildon-toggle-button.h"
34 #include                                        "hildon-enum-types.h"
35
36 #define FINGER_BUTTON_HEIGHT                    70
37 #define THUMB_BUTTON_HEIGHT                     105
38 #define HALFSCREEN_BUTTON_WIDTH                 400
39 #define FULLSCREEN_BUTTON_WIDTH                 800
40
41 G_DEFINE_TYPE                                   (HildonToggleButton, hildon_toggle_button, GTK_TYPE_TOGGLE_BUTTON);
42
43 enum {
44   PROP_ARRANGEMENT_FLAGS
45 };
46
47 static void
48 hildon_toggle_button_set_arrangement            (HildonToggleButton      *button,
49                                                  HildonToggleButtonFlags  flags);
50
51 static void
52 hildon_toggle_button_set_property               (GObject      *object,
53                                                  guint         prop_id,
54                                                  const GValue *value,
55                                                  GParamSpec   *pspec)
56 {
57     HildonToggleButton *button = HILDON_TOGGLE_BUTTON (object);
58
59     switch (prop_id)
60     {
61     case PROP_ARRANGEMENT_FLAGS:
62         hildon_toggle_button_set_arrangement (button, g_value_get_flags (value));
63         break;
64     default:
65         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
66         break;
67     }
68 }
69
70 static void
71 hildon_toggle_button_class_init                 (HildonToggleButtonClass *klass)
72 {
73     GObjectClass *gobject_class = (GObjectClass *)klass;
74
75     gobject_class->set_property = hildon_toggle_button_set_property;
76
77     g_object_class_install_property (
78         gobject_class,
79         PROP_ARRANGEMENT_FLAGS,
80         g_param_spec_flags (
81             "arrangement-flags",
82             "Arrangement flags",
83             "How the button contents must be arranged",
84             HILDON_TYPE_TOGGLE_BUTTON_FLAGS,
85             HILDON_TOGGLE_BUTTON_AUTO_WIDTH | HILDON_TOGGLE_BUTTON_AUTO_HEIGHT,
86             G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
87 }
88
89 static void
90 hildon_toggle_button_init                       (HildonToggleButton *self)
91 {
92 }
93
94 /**
95  * hildon_toggle_button_new:
96  * @flags: flags to set the size of the button
97  *
98  * Creates a new #HildonToggleButton. To add a child widget use gtk_container_add().
99  *
100  * Returns: a new #HildonToggleButton
101  **/
102 GtkWidget *
103 hildon_toggle_button_new                        (HildonToggleButtonFlags flags)
104 {
105     GtkWidget *button;
106     /* Create widget */
107     button = g_object_new (HILDON_TYPE_TOGGLE_BUTTON,
108                            "arrangement-flags", flags,
109                            NULL);
110     return button;
111 }
112
113 /**
114  * hildon_toggle_button_new_with_label:
115  * @flags: flags to set the size of the button
116  * @label: a text to set in the button label
117  *
118  * Creates a new #HildonToggleButton with a text label.
119  *
120  * Returns: a new #HildonToggleButton
121  **/
122 GtkWidget *
123 hildon_toggle_button_new_with_label             (HildonToggleButtonFlags  flags,
124                                                  const char              *label)
125 {
126     GtkWidget *button;
127     /* Create widget */
128     button = g_object_new (HILDON_TYPE_TOGGLE_BUTTON,
129                            "arrangement-flags", flags,
130                            "label", label,
131                            NULL);
132     return button;
133 }
134
135 static void
136 hildon_toggle_button_set_arrangement            (HildonToggleButton      *button,
137                                                  HildonToggleButtonFlags  flags)
138 {
139     gint width = -1;
140     gint height = -1;
141     const char *widget_name = NULL;
142
143     /* Requested height */
144     if (flags & HILDON_TOGGLE_BUTTON_FINGER_HEIGHT) {
145         height = FINGER_BUTTON_HEIGHT;
146         widget_name = "hildon-finger-button";
147     } else if (flags & HILDON_TOGGLE_BUTTON_THUMB_HEIGHT) {
148         height = THUMB_BUTTON_HEIGHT;
149         widget_name = "hildon-thumb-button";
150     }
151
152     if (widget_name) {
153         gtk_widget_set_name (GTK_WIDGET (button), widget_name);
154     }
155
156     /* Requested width */
157     if (flags & HILDON_TOGGLE_BUTTON_HALFSCREEN_WIDTH) {
158         width = HALFSCREEN_BUTTON_WIDTH;
159     } else if (flags & HILDON_TOGGLE_BUTTON_FULLSCREEN_WIDTH) {
160         width = FULLSCREEN_BUTTON_WIDTH;
161     }
162
163     g_object_set (button,
164                   "width-request", width,
165                   "height-request", height,
166                   NULL);
167
168 }