2 * This file is a part of hildon
4 * Copyright (C) 2008, 2009 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, #HildonCheckButton
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 "hildon-gtk.h"
28 #include "hildon-window.h"
29 #include "hildon-window-private.h"
30 #include "hildon-edit-toolbar.h"
31 #include "hildon-edit-toolbar-private.h"
32 #include "hildon-private.h"
35 image_visible_changed_cb (GtkWidget *image,
39 if (!GTK_WIDGET_VISIBLE (image))
40 gtk_widget_show (image);
44 parent_changed_cb (GtkWidget *image,
48 /* If the parent has really changed, remove the old signal handlers */
49 if (image->parent != oldparent) {
50 g_signal_handlers_disconnect_by_func (image, parent_changed_cb, oldparent);
51 g_signal_handlers_disconnect_by_func (image, image_visible_changed_cb, NULL);
56 image_changed_cb (GtkButton *button,
60 GtkWidget *image = gtk_button_get_image (button);
62 g_return_if_fail (image == NULL || GTK_IS_WIDGET (image));
65 /* If the button has a new image, show it */
66 gtk_widget_show (image);
67 /* Show the image no matter the value of gtk-button-images */
68 g_signal_connect (image, "notify::visible", G_CALLBACK (image_visible_changed_cb), NULL);
69 /* If the image is removed from the button, disconnect these handlers */
70 g_signal_connect (image, "notify::parent", G_CALLBACK (parent_changed_cb), image->parent);
75 button_common_init (GtkWidget *button,
78 /* Set requested size */
79 hildon_gtk_widget_set_theme_size (button, size);
81 /* Set focus-on-click to FALSE by default */
82 gtk_button_set_focus_on_click (GTK_BUTTON (button), FALSE);
84 /* Make sure that all images in this button are always shown */
85 g_signal_connect (button, "notify::image", G_CALLBACK (image_changed_cb), NULL);
89 * hildon_gtk_menu_new:
91 * This is a convenience function to create a #GtkMenu setting its
92 * widget name to allow Hildon specific styling.
94 * Return value: A newly created #GtkMenu widget.
99 hildon_gtk_menu_new (void)
101 GtkWidget *menu = gtk_menu_new ();
102 gtk_widget_set_name (menu, "hildon-context-sensitive-menu");
107 * hildon_gtk_button_new:
108 * @size: Flags indicating the size of the new button
110 * This is a convenience function to create a #GtkButton setting its
111 * size to one of the pre-defined Hildon sizes.
113 * Buttons created with this function also override
114 * #GtkSettings:gtk-button-images. Images set using
115 * gtk_button_set_image() are always shown.
117 * Buttons created using this function have #GtkButton:focus-on-click
118 * set to %FALSE by default.
120 * Return value: A newly created #GtkButton widget.
125 hildon_gtk_button_new (HildonSizeType size)
127 GtkWidget *button = gtk_button_new ();
128 button_common_init (button, size);
133 * hildon_gtk_toggle_button_new:
134 * @size: Flags indicating the size of the new button
136 * This is a convenience function to create a #GtkToggleButton setting
137 * its size to one of the pre-defined Hildon sizes.
139 * Buttons created with this function also override
140 * #GtkSettings:gtk-button-images. Images set using
141 * gtk_button_set_image() are always shown.
143 * Buttons created using this function have #GtkButton:focus-on-click
144 * set to %FALSE by default.
146 * Return value: A newly created #GtkToggleButton widget.
151 hildon_gtk_toggle_button_new (HildonSizeType size)
153 GtkWidget *button = gtk_toggle_button_new ();
154 button_common_init (button, size);
159 * hildon_gtk_radio_button_new:
160 * @size: Flags indicating the size of the new button
161 * @group: An existing radio button group, or %NULL if you are
162 * creating a new group
164 * This is a convenience function to create a #GtkRadioButton setting
165 * its size to one of the pre-defined Hildon sizes.
167 * Buttons created with this function also override
168 * #GtkSettings:gtk-button-images. Images set using
169 * gtk_button_set_image() are always shown.
171 * Buttons created using this function have #GtkButton:focus-on-click
172 * set to %FALSE by default.
174 * Return value: A newly created #GtkRadioButton widget.
179 hildon_gtk_radio_button_new (HildonSizeType size,
182 GtkWidget *button = gtk_radio_button_new (group);
183 button_common_init (button, size);
188 * hildon_gtk_radio_button_new_from_widget:
189 * @size: Flags indicating the size of the new button
190 * @radio_group_member: widget to get radio group from or %NULL
192 * This is a convenience function to create a #GtkRadioButton setting
193 * its size to one of the pre-defined Hildon sizes.
195 * Buttons created with this function also override
196 * #GtkSettings:gtk-button-images. Images set using
197 * gtk_button_set_image() are always shown.
199 * Buttons created using this function have #GtkButton:focus-on-click
200 * set to %FALSE by default.
202 * Return value: A newly created #GtkRadioButton widget.
207 hildon_gtk_radio_button_new_from_widget (HildonSizeType size,
208 GtkRadioButton *radio_group_member)
210 GtkWidget *button = gtk_radio_button_new_from_widget (radio_group_member);
211 button_common_init (button, size);
217 * hildon_gtk_tree_view_new:
218 * @mode: the Hildon UI mode
220 * Creates a new #GtkTreeView widget with the Hildon UI mode set to
223 * Return value: A newly created #GtkTreeView widget.
228 hildon_gtk_tree_view_new (HildonUIMode mode)
230 return g_object_new (GTK_TYPE_TREE_VIEW, "hildon-ui-mode", mode,
231 "enable-search", FALSE, NULL);
235 * hildon_gtk_tree_view_new_with_model:
236 * @mode: the Hildon UI mode
239 * Creates a new #GtkTreeView widget with the Hildon UI mode set to
240 * @mode and the model initialized to @model.
242 * Return value: A newly created #GtkTreeView widget.
247 hildon_gtk_tree_view_new_with_model (HildonUIMode mode,
250 return g_object_new (GTK_TYPE_TREE_VIEW, "hildon-ui-mode", mode, "model", model, NULL);
254 * hildon_gtk_tree_view_set_ui_mode:
255 * @treeview: A #GtkTreeView
256 * @mode: The new #HildonUIMode
258 * Sets the UI mode of @treeview to @mode.
263 hildon_gtk_tree_view_set_ui_mode (GtkTreeView *treeview,
266 g_return_if_fail (GTK_IS_TREE_VIEW (treeview));
267 g_object_set (treeview, "hildon-ui-mode", mode, NULL);
271 * hildon_gtk_icon_view_new:
272 * @mode: the Hildon UI mode
274 * Creates a new #GtkIconView widget with the Hildon UI mode set to
277 * Return value: A newly created #GtkIconView widget
282 hildon_gtk_icon_view_new (HildonUIMode mode)
284 return g_object_new (GTK_TYPE_ICON_VIEW, "hildon-ui-mode", mode, NULL);
288 * hildon_gtk_icon_view_new_with_model:
289 * @mode: the Hildon UI mode
292 * Creates a new #GtkIconView widget with the Hildon UI mode set to
293 * @mode and the model intitialized to @model.
295 * Return value: A newly created #GtkIconView widget.
300 hildon_gtk_icon_view_new_with_model (HildonUIMode mode,
303 return g_object_new (GTK_TYPE_ICON_VIEW, "hildon-ui-mode", mode, "model", model, NULL);
307 * hildon_gtk_icon_view_set_ui_mode:
308 * @iconview: A #GtkIconView
309 * @mode: The new #HildonUIMode
311 * Sets the UI mode of @iconview to @mode.
316 hildon_gtk_icon_view_set_ui_mode (GtkIconView *iconview,
319 g_return_if_fail (GTK_IS_ICON_VIEW (iconview));
320 g_object_set (iconview, "hildon-ui-mode", mode, NULL);
322 #endif /* MAEMO_GTK */
325 do_set_progress_indicator (GtkWindow *window,
328 guint state = GPOINTER_TO_UINT (stateptr);
329 hildon_gtk_window_set_clear_window_flag (window, "_HILDON_WM_WINDOW_PROGRESS_INDICATOR",
331 g_signal_handlers_disconnect_matched (window, G_SIGNAL_MATCH_FUNC,
332 0, 0, NULL, do_set_progress_indicator, NULL);
336 do_set_do_not_disturb (GtkWindow *window,
339 gboolean dndflag = GPOINTER_TO_INT (dndptr);
340 hildon_gtk_window_set_clear_window_flag (window, "_HILDON_DO_NOT_DISTURB",
341 XA_INTEGER, dndflag);
342 g_signal_handlers_disconnect_matched (window, G_SIGNAL_MATCH_FUNC,
343 0, 0, NULL, do_set_do_not_disturb, NULL);
347 do_set_portrait_flags (GtkWindow *window,
350 HildonPortraitFlags flags = GPOINTER_TO_INT (flagsptr);
352 hildon_gtk_window_set_clear_window_flag (window, "_HILDON_PORTRAIT_MODE_REQUEST",
354 flags & HILDON_PORTRAIT_MODE_REQUEST);
355 hildon_gtk_window_set_clear_window_flag (window, "_HILDON_PORTRAIT_MODE_SUPPORT",
357 flags & HILDON_PORTRAIT_MODE_SUPPORT);
359 g_signal_handlers_disconnect_matched (window, G_SIGNAL_MATCH_FUNC,
360 0, 0, NULL, do_set_portrait_flags, NULL);
364 * hildon_gtk_window_set_progress_indicator:
365 * @window: a #GtkWindow.
366 * @state: The state we want to set: 1 -> show progress indicator, 0
367 * -> hide progress indicator.
369 * This functions tells the window manager to show/hide a progress
370 * indicator in the window title. It applies to #HildonDialog and
371 * #HildonWindow (including subclasses).
376 hildon_gtk_window_set_progress_indicator (GtkWindow *window,
379 hildon_gtk_window_set_flag (window, (HildonFlagFunc) do_set_progress_indicator, GUINT_TO_POINTER (state));
380 if (HILDON_IS_WINDOW (window)) {
381 HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (window);
382 if (priv->edit_toolbar) {
383 HildonEditToolbar *tb = HILDON_EDIT_TOOLBAR (priv->edit_toolbar);
384 hildon_edit_toolbar_set_progress_indicator (tb, state);
390 * hildon_gtk_window_set_do_not_disturb:
391 * @window: a #GtkWindow
392 * @dndflag: %TRUE to set the "do-not-disturb" flag, %FALSE to clear it
394 * This function tells the window manager to set (or clear) the
395 * "do-not-disturb" flag on @window.
400 hildon_gtk_window_set_do_not_disturb (GtkWindow *window,
403 hildon_gtk_window_set_flag (window, (HildonFlagFunc) do_set_do_not_disturb, GUINT_TO_POINTER (dndflag));
407 * hildon_gtk_window_set_portrait_flags:
408 * @window: a #GtkWindow
409 * @portrait_flags: a combination of #HildonPortraitFlags
411 * Sets the portrait flags for @window.
416 hildon_gtk_window_set_portrait_flags (GtkWindow *window,
417 HildonPortraitFlags portrait_flags)
419 hildon_gtk_window_set_flag (window, (HildonFlagFunc) do_set_portrait_flags, GUINT_TO_POINTER (portrait_flags));
423 * hildon_gtk_window_take_screenshot:
424 * @window: a #GtkWindow
425 * @take: %TRUE to take a screenshot, %FALSE to destroy the existing one.
427 * Tells the window manager to take a screenshot of @window, or to
428 * destroy the existing one. @window must be mapped.
431 hildon_gtk_window_take_screenshot (GtkWindow *window,
437 g_return_if_fail (GTK_IS_WINDOW (window));
438 g_return_if_fail (GTK_WIDGET_MAPPED (window));
440 rootwin = gdk_screen_get_root_window (gtk_window_get_screen (window));
442 ev = (GdkEventClient *) gdk_event_new (GDK_CLIENT_EVENT);
443 ev->window = g_object_ref (rootwin);
444 ev->send_event = TRUE;
445 ev->message_type = gdk_atom_intern ("_HILDON_LOADING_SCREENSHOT", FALSE);
446 ev->data_format = 32;
447 ev->data.l[0] = take ? 1 : 0;
448 ev->data.l[1] = GDK_WINDOW_XID (GTK_WIDGET (window)->window);
450 gdk_event_send_client_message ((GdkEvent *) ev, GDK_WINDOW_XWINDOW (rootwin));
452 gdk_event_free ((GdkEvent *) ev);
457 * hildon_gtk_hscale_new:
459 * Creates a new horizontal scale widget that lets the user select
460 * a value. The value is technically a double between 0.0 and 1.0.
461 * See gtk_adjustment_configure() for reconfiguring the adjustment.
463 * The scale is hildonized, which means that a click or tap immediately
464 * jumps to the desired position, see gtk_range_set_jump_to_position().
465 * Further more the value is not displayed, see gtk_scale_set_draw_value().
467 * Returns: a new hildonized #GtkHScale
472 hildon_gtk_hscale_new (void)
474 GtkWidget *scale = gtk_hscale_new_with_range (0.0, 1.0, 0.1);
478 "jump-to-position", TRUE,
486 * hildon_gtk_vscale_new:
488 * Creates a new vertical scale widget that lets the user select
489 * a value. The value is technically a double between 0.0 and 1.0.
490 * See gtk_adjustment_configure() for reconfiguring the adjustment.
492 * The scale is hildonized, which means that a click or tap immediately
493 * jumps to the desired position, see gtk_range_set_jump_to_position().
494 * Further more the value is not displayed, see gtk_scale_set_draw_value().
496 * Returns: a new hildonized #GtkVScale
501 hildon_gtk_vscale_new (void)
503 GtkWidget *scale = gtk_vscale_new_with_range (0.0, 1.0, 0.1);
507 "jump-to-position", TRUE,