2008-08-08 Alberto Garcia <agarcia@igalia.com>
[hildon] / src / hildon-gtk.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-gtk
19  * @short_description: Additional functions for Gtk widgets
20  *
21  * Hildon provides some functions to extend the functionality of
22  * existing Gtk widgets. This also includes convenience functions to
23  * easily perform frequent tasks.
24  */
25
26 #include "hildon-gtk.h"
27
28 #define                                         HILDON_HEIGHT_FINGER 70
29
30 #define                                         HILDON_HEIGHT_THUMB 105
31
32 #define                                         HILDON_WIDTH_FULLSCREEN \
33                                                 (gdk_screen_get_width (gdk_screen_get_default ()))
34
35 #define                                         HILDON_WIDTH_HALFSCREEN \
36                                                 (HILDON_WIDTH_FULLSCREEN / 2)
37
38 /**
39  * hildon_gtk_widget_set_theme_size
40  * @widget: A #GtkWidget
41  * @size: Flags indicating the size of the widget
42  *
43  * This function sets the requested size of a widget.
44  **/
45 void
46 hildon_gtk_widget_set_theme_size                (GtkWidget      *widget,
47                                                  HildonSizeType  size)
48 {
49     gint width = -1;
50     gint height = -1;
51     const gchar *widget_name = NULL;
52
53     g_return_if_fail (GTK_IS_WIDGET (widget));
54
55     /* Requested height */
56     if (size & HILDON_SIZE_FINGER_HEIGHT) {
57         height = HILDON_HEIGHT_FINGER;
58         widget_name = "hildon-finger-widget";
59     } else if (size & HILDON_SIZE_THUMB_HEIGHT) {
60         height = HILDON_HEIGHT_THUMB;
61         widget_name = "hildon-thumb-widget";
62     }
63
64     /* Requested width */
65     if (size & HILDON_SIZE_HALFSCREEN_WIDTH) {
66         width = HILDON_WIDTH_HALFSCREEN;
67     } else if (size & HILDON_SIZE_FULLSCREEN_WIDTH) {
68         width = HILDON_WIDTH_FULLSCREEN;
69     }
70
71     gtk_widget_set_size_request (widget, width, height);
72
73     if (widget_name)
74         gtk_widget_set_name (widget, widget_name);
75 }
76
77 /**
78  * hildon_gtk_button_new:
79  * @size: Flags indicating the size of the new button
80  *
81  * This is a convenience function to create a #GtkButton setting its
82  * size to one of the pre-defined Hildon sizes.
83  *
84  * Return value: A newly created #GtkButton widget.
85  **/
86 GtkWidget *
87 hildon_gtk_button_new                           (HildonSizeType size)
88 {
89     GtkWidget *button = gtk_button_new ();
90     hildon_gtk_widget_set_theme_size (button, size);
91     return button;
92 }
93
94 /**
95  * hildon_gtk_toggle_button_new:
96  * @size: Flags indicating the size of the new button
97  *
98  * This is a convenience function to create a #GtkToggleButton setting
99  * its size to one of the pre-defined Hildon sizes.
100  *
101  * Return value: A newly created #GtkToggleButton widget.
102  **/
103 GtkWidget *
104 hildon_gtk_toggle_button_new                    (HildonSizeType size)
105 {
106     GtkWidget *button = gtk_toggle_button_new ();
107     hildon_gtk_widget_set_theme_size (button, size);
108     return button;
109 }
110
111 /**
112  * hildon_gtk_check_button_new:
113  * @size: Flags indicating the size of the new button
114  *
115  * This is a convenience function to create a #GtkCheckButton setting
116  * its size to one of the pre-defined Hildon sizes.
117  *
118  * Return value: A newly created #GtkCheckButton widget.
119  **/
120 GtkWidget *
121 hildon_gtk_check_button_new                     (HildonSizeType size)
122 {
123     GtkWidget *button = gtk_check_button_new ();
124     hildon_gtk_widget_set_theme_size (button, size);
125     return button;
126 }
127
128 /**
129  * hildon_gtk_radio_button_new:
130  * @size: Flags indicating the size of the new button
131  * @group: An existing radio button group, or %NULL if you are
132  * creating a new group
133  *
134  * This is a convenience function to create a #GtkRadioButton setting
135  * its size to one of the pre-defined Hildon sizes.
136  *
137  * Return value: A newly created #GtkRadioButton widget.
138  **/
139 GtkWidget *
140 hildon_gtk_radio_button_new                     (HildonSizeType  size,
141                                                  GSList         *group)
142 {
143     GtkWidget *button = gtk_radio_button_new (group);
144     hildon_gtk_widget_set_theme_size (button, size);
145     return button;
146 }
147
148 /**
149  * hildon_gtk_radio_button_new_from_widget:
150  * @size: Flags indicating the size of the new button
151  * @radio_group_member: widget to get radio group from or %NULL
152  *
153  * This is a convenience function to create a #GtkRadioButton setting
154  * its size to one of the pre-defined Hildon sizes.
155  *
156  * Return value: A newly created #GtkRadioButton widget.
157  **/
158 GtkWidget *
159 hildon_gtk_radio_button_new_from_widget         (HildonSizeType  size,
160                                                  GtkRadioButton *radio_group_member)
161 {
162     GtkWidget *button = gtk_radio_button_new_from_widget (radio_group_member);
163     hildon_gtk_widget_set_theme_size (button, size);
164     return button;
165 }
166
167 /**
168  * hildon_gtk_tree_view_new:
169  * @mode: the Hildon UI mode
170  *
171  * Creates a new #GtkTreeView widget with the Hildon UI mode set to
172  * @mode
173  *
174  * Return value: A newly created #GtkTreeView widget.
175  **/
176 GtkWidget *
177 hildon_gtk_tree_view_new                        (HildonUIMode mode)
178 {
179     return g_object_new (GTK_TYPE_TREE_VIEW, "hildon-ui-mode", mode, NULL);
180 }
181
182 /**
183  * hildon_gtk_tree_view_new_with_model:
184  * @mode: the Hildon UI mode
185  * @model: the model.
186  *
187  * Creates a new #GtkTreeView widget with the Hildon UI mode set to
188  * @mode and the model initialized to @model.
189  *
190  * Return value: A newly created #GtkTreeView widget.
191  **/
192 GtkWidget *
193 hildon_gtk_tree_view_new_with_model             (HildonUIMode  mode,
194                                                  GtkTreeModel *model)
195 {
196     return g_object_new (GTK_TYPE_TREE_VIEW, "hildon-ui-mode", mode, "model", model, NULL);
197 }
198
199 /**
200  * hildon_gtk_icon_view_new:
201  * @mode: the Hildon UI mode
202  *
203  * Creates a new #GtkIconView widget with the Hildon UI mode set to
204  * @mode
205  *
206  * Return value: A newly created #GtkIconView widget
207  **/
208 GtkWidget *
209 hildon_gtk_icon_view_new                        (HildonUIMode mode)
210 {
211     return g_object_new (GTK_TYPE_ICON_VIEW, "hildon-ui-mode", mode, NULL);
212 }
213
214 /**
215  * hildon_gtk_icon_view_new_with_model:
216  * @mode: the Hildon UI mode
217  * @model: The model.
218  *
219  * Creates a new #GtkIconView widget with the Hildon UI mode set to
220  * @mode and the model intitialized to @model.
221  *
222  * Return value: A newly created #GtkIconView widget.
223  **/
224 GtkWidget *
225 hildon_gtk_icon_view_new_with_model             (HildonUIMode  mode,
226                                                  GtkTreeModel *model)
227 {
228     return g_object_new (GTK_TYPE_ICON_VIEW, "hildon-ui-mode", mode, "model", model, NULL);
229 }