2 * This file is a part of hildon
4 * Copyright (C) 2008 Nokia Corporation, all rights reserved.
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.
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.
19 * @short_description: Additional functions for Gtk widgets
20 * @see_also: #HildonButton
22 * Hildon provides some functions to extend the functionality of
23 * existing Gtk widgets. This also includes convenience functions to
24 * easily perform frequent tasks.
27 #include <X11/Xatom.h>
30 #include "hildon-gtk.h"
32 #define HILDON_HEIGHT_FINGER 70
34 #define HILDON_HEIGHT_THUMB 105
36 #define HILDON_WIDTH_FULLSCREEN \
37 (gdk_screen_get_width (gdk_screen_get_default ()))
39 #define HILDON_WIDTH_HALFSCREEN \
40 (HILDON_WIDTH_FULLSCREEN / 2)
43 * hildon_gtk_widget_set_theme_size:
44 * @widget: A #GtkWidget
45 * @size: Flags indicating the size of the widget
47 * This function sets the requested size of a widget.
50 hildon_gtk_widget_set_theme_size (GtkWidget *widget,
55 gchar *widget_name = NULL;
57 g_return_if_fail (GTK_IS_WIDGET (widget));
59 /* Requested height */
60 if (size & HILDON_SIZE_FINGER_HEIGHT) {
61 height = HILDON_HEIGHT_FINGER;
62 widget_name = "-finger";
63 } else if (size & HILDON_SIZE_THUMB_HEIGHT) {
64 height = HILDON_HEIGHT_THUMB;
65 widget_name = "-thumb";
69 widget_name = g_strconcat (g_type_name (GTK_WIDGET_TYPE (widget)), widget_name, NULL);
72 if (size & HILDON_SIZE_HALFSCREEN_WIDTH) {
73 width = HILDON_WIDTH_HALFSCREEN;
74 } else if (size & HILDON_SIZE_FULLSCREEN_WIDTH) {
75 width = HILDON_WIDTH_FULLSCREEN;
78 gtk_widget_set_size_request (widget, width, height);
81 gtk_widget_set_name (widget, widget_name);
87 image_visible_changed_cb (GtkWidget *image,
91 if (!GTK_WIDGET_VISIBLE (image))
92 gtk_widget_show (image);
96 parent_changed_cb (GtkWidget *image,
100 /* If the parent has really changed, remove the old signal handlers */
101 if (image->parent != oldparent) {
102 g_signal_handlers_disconnect_by_func (image, parent_changed_cb, oldparent);
103 g_signal_handlers_disconnect_by_func (image, image_visible_changed_cb, NULL);
108 image_changed_cb (GtkButton *button,
112 GtkWidget *image = gtk_button_get_image (button);
114 g_return_if_fail (image == NULL || GTK_IS_WIDGET (image));
117 /* If the button has a new image, show it */
118 gtk_widget_show (image);
119 /* Show the image no matter the value of gtk-button-images */
120 g_signal_connect (image, "notify::visible", G_CALLBACK (image_visible_changed_cb), NULL);
121 /* If the image is removed from the button, disconnect these handlers */
122 g_signal_connect (image, "notify::parent", G_CALLBACK (parent_changed_cb), image->parent);
127 button_common_init (GtkWidget *button,
130 /* Set requested size */
131 hildon_gtk_widget_set_theme_size (button, size);
133 /* Make sure that all images in this button are always shown */
134 g_signal_connect (button, "notify::image", G_CALLBACK (image_changed_cb), NULL);
138 * hildon_gtk_menu_new:
140 * This is a convenience function to create a #GtkMenu setting its
141 * widget name to allow Hildon specific styling.
143 * Return value: A newly created #GtkMenu widget.
146 hildon_gtk_menu_new (void)
148 GtkWidget *menu = gtk_menu_new ();
149 gtk_widget_set_name (menu, "hildon-context-sensitive-menu");
154 * hildon_gtk_button_new:
155 * @size: Flags indicating the size of the new button
157 * This is a convenience function to create a #GtkButton setting its
158 * size to one of the pre-defined Hildon sizes.
160 * Buttons created with this function also override the
161 * "gtk-button-images" setting. Images set using
162 * gtk_button_set_image() are always shown.
164 * Return value: A newly created #GtkButton widget.
167 hildon_gtk_button_new (HildonSizeType size)
169 GtkWidget *button = gtk_button_new ();
170 button_common_init (button, size);
175 * hildon_gtk_toggle_button_new:
176 * @size: Flags indicating the size of the new button
178 * This is a convenience function to create a #GtkToggleButton setting
179 * its size to one of the pre-defined Hildon sizes.
181 * Buttons created with this function also override the
182 * "gtk-button-images" setting. Images set using
183 * gtk_button_set_image() are always shown.
185 * Return value: A newly created #GtkToggleButton widget.
188 hildon_gtk_toggle_button_new (HildonSizeType size)
190 GtkWidget *button = gtk_toggle_button_new ();
191 button_common_init (button, size);
196 * hildon_gtk_radio_button_new:
197 * @size: Flags indicating the size of the new button
198 * @group: An existing radio button group, or %NULL if you are
199 * creating a new group
201 * This is a convenience function to create a #GtkRadioButton setting
202 * its size to one of the pre-defined Hildon sizes.
204 * Buttons created with this function also override the
205 * "gtk-button-images" setting. Images set using
206 * gtk_button_set_image() are always shown.
208 * Return value: A newly created #GtkRadioButton widget.
211 hildon_gtk_radio_button_new (HildonSizeType size,
214 GtkWidget *button = gtk_radio_button_new (group);
215 button_common_init (button, size);
220 * hildon_gtk_radio_button_new_from_widget:
221 * @size: Flags indicating the size of the new button
222 * @radio_group_member: widget to get radio group from or %NULL
224 * This is a convenience function to create a #GtkRadioButton setting
225 * its size to one of the pre-defined Hildon sizes.
227 * Buttons created with this function also override the
228 * "gtk-button-images" setting. Images set using
229 * gtk_button_set_image() are always shown.
231 * Return value: A newly created #GtkRadioButton widget.
234 hildon_gtk_radio_button_new_from_widget (HildonSizeType size,
235 GtkRadioButton *radio_group_member)
237 GtkWidget *button = gtk_radio_button_new_from_widget (radio_group_member);
238 button_common_init (button, size);
244 * hildon_gtk_tree_view_new:
245 * @mode: the Hildon UI mode
247 * Creates a new #GtkTreeView widget with the Hildon UI mode set to
250 * Return value: A newly created #GtkTreeView widget.
253 hildon_gtk_tree_view_new (HildonUIMode mode)
255 return g_object_new (GTK_TYPE_TREE_VIEW, "hildon-ui-mode", mode, NULL);
259 * hildon_gtk_tree_view_new_with_model:
260 * @mode: the Hildon UI mode
263 * Creates a new #GtkTreeView widget with the Hildon UI mode set to
264 * @mode and the model initialized to @model.
266 * Return value: A newly created #GtkTreeView widget.
269 hildon_gtk_tree_view_new_with_model (HildonUIMode mode,
272 return g_object_new (GTK_TYPE_TREE_VIEW, "hildon-ui-mode", mode, "model", model, NULL);
276 * hildon_gtk_icon_view_new:
277 * @mode: the Hildon UI mode
279 * Creates a new #GtkIconView widget with the Hildon UI mode set to
282 * Return value: A newly created #GtkIconView widget
285 hildon_gtk_icon_view_new (HildonUIMode mode)
287 return g_object_new (GTK_TYPE_ICON_VIEW, "hildon-ui-mode", mode, NULL);
291 * hildon_gtk_icon_view_new_with_model:
292 * @mode: the Hildon UI mode
295 * Creates a new #GtkIconView widget with the Hildon UI mode set to
296 * @mode and the model intitialized to @model.
298 * Return value: A newly created #GtkIconView widget.
301 hildon_gtk_icon_view_new_with_model (HildonUIMode mode,
304 return g_object_new (GTK_TYPE_ICON_VIEW, "hildon-ui-mode", mode, "model", model, NULL);
306 #endif /* MAEMO_GTK */
309 * hildon_gtk_window_set_progress_indicator:
310 * @window: The window, we want to define its state
311 * @state: The state we want to set: 1 -> show progress indicator, 0
312 * -> hide progress indicator.
314 * This functions notifies the window manager that it should add a
315 * progress indicator in the title of the window. It applies to
316 * #HildonDialog and #HildonWindow.
320 hildon_gtk_window_set_progress_indicator (GtkWindow *window,
323 GtkWidget *widget = GTK_WIDGET (window);
327 display = gdk_drawable_get_display (widget->window);
328 atom = gdk_x11_get_xatom_by_name_for_display (display, "_HILDON_WM_WINDOW_PROGRESS_INDICATOR");
330 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XID (widget->window),
331 atom, XA_INTEGER, 32, PropModeReplace,
332 (guchar *)&state, 1);