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, #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 <X11/Xatom.h>
30 #include "hildon-gtk.h"
32 typedef void (*HildonFlagFunc) (GtkWindow *window, gpointer userdata);
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 the
114 * "gtk-button-images" setting. 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 the
140 * "gtk-button-images" setting. 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 the
168 * "gtk-button-images" setting. 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 the
196 * "gtk-button-images" setting. 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 set_clear_window_flag (GtkWindow *window,
326 const gchar *atomname,
330 GdkWindow *gdkwin = GTK_WIDGET (window)->window;
331 GdkAtom atom = gdk_atom_intern (atomname, FALSE);
335 gdk_property_change (gdkwin, atom, gdk_x11_xatom_to_atom (xatom),
336 32, GDK_PROP_MODE_REPLACE, (const guchar *) &set, 1);
338 gdk_property_delete (gdkwin, atom);
343 do_set_progress_indicator (GtkWindow *window,
346 guint state = GPOINTER_TO_UINT (stateptr);
347 set_clear_window_flag (window, "_HILDON_WM_WINDOW_PROGRESS_INDICATOR", XA_INTEGER, state);
348 g_signal_handlers_disconnect_matched (window, G_SIGNAL_MATCH_FUNC,
349 0, 0, NULL, do_set_progress_indicator, NULL);
353 do_set_do_not_disturb (GtkWindow *window,
356 gboolean dndflag = GPOINTER_TO_INT (dndptr);
357 set_clear_window_flag (window, "_HILDON_DO_NOT_DISTURB", XA_INTEGER, dndflag);
358 g_signal_handlers_disconnect_matched (window, G_SIGNAL_MATCH_FUNC,
359 0, 0, NULL, do_set_do_not_disturb, NULL);
363 do_set_portrait_flags (GtkWindow *window,
366 HildonPortraitFlags flags = GPOINTER_TO_INT (flagsptr);
368 set_clear_window_flag (window, "_HILDON_PORTRAIT_MODE_REQUEST", XA_CARDINAL,
369 flags & HILDON_PORTRAIT_MODE_REQUEST);
370 set_clear_window_flag (window, "_HILDON_PORTRAIT_MODE_SUPPORT", XA_CARDINAL,
371 flags & HILDON_PORTRAIT_MODE_SUPPORT);
373 g_signal_handlers_disconnect_matched (window, G_SIGNAL_MATCH_FUNC,
374 0, 0, NULL, do_set_portrait_flags, NULL);
378 set_flag (GtkWindow *window,
382 g_return_if_fail (GTK_IS_WINDOW (window));
383 if (GTK_WIDGET_REALIZED (window)) {
384 (*func) (window, userdata);
386 g_signal_handlers_disconnect_matched (window, G_SIGNAL_MATCH_FUNC,
387 0, 0, NULL, func, NULL);
388 g_signal_connect (window, "realize", G_CALLBACK (func), userdata);
393 * hildon_gtk_window_set_progress_indicator:
394 * @window: a #GtkWindow.
395 * @state: The state we want to set: 1 -> show progress indicator, 0
396 * -> hide progress indicator.
398 * This functions tells the window manager to show/hide a progress
399 * indicator in the window title. It applies to #HildonDialog and
400 * #HildonWindow (including subclasses).
405 hildon_gtk_window_set_progress_indicator (GtkWindow *window,
408 set_flag (window, (HildonFlagFunc) do_set_progress_indicator, GUINT_TO_POINTER (state));
412 * hildon_gtk_window_set_do_not_disturb:
413 * @window: a #GtkWindow
414 * @dndflag: %TRUE to set the "do-not-disturb" flag, %FALSE to clear it
416 * This function tells the window manager to set (or clear) the
417 * "do-not-disturb" flag on @window.
422 hildon_gtk_window_set_do_not_disturb (GtkWindow *window,
425 set_flag (window, (HildonFlagFunc) do_set_do_not_disturb, GUINT_TO_POINTER (dndflag));
429 * hildon_gtk_window_set_portrait_flags:
430 * @window: a #GtkWindow
431 * @portrait_flags: a combination of #HildonPortraitFlags
433 * Sets the portrait flags for @window.
438 hildon_gtk_window_set_portrait_flags (GtkWindow *window,
439 HildonPortraitFlags portrait_flags)
441 set_flag (window, (HildonFlagFunc) do_set_portrait_flags, GUINT_TO_POINTER (portrait_flags));
445 * hildon_gtk_window_take_screenshot:
446 * @window: a #GtkWindow
447 * @take: %TRUE to take a screenshot, %FALSE to destroy the existing one.
449 * Tells the window manager to take a screenshot of @window, or to
450 * destroy the existing one. @window must be mapped.
453 hildon_gtk_window_take_screenshot (GtkWindow *window,
459 g_return_if_fail (GTK_IS_WINDOW (window));
460 g_return_if_fail (GTK_WIDGET_MAPPED (window));
462 rootwin = gdk_screen_get_root_window (gtk_window_get_screen (window));
464 ev = (GdkEventClient *) gdk_event_new (GDK_CLIENT_EVENT);
465 ev->window = g_object_ref (rootwin);
466 ev->send_event = TRUE;
467 ev->message_type = gdk_atom_intern ("_HILDON_LOADING_SCREENSHOT", FALSE);
468 ev->data_format = 32;
469 ev->data.l[0] = take ? 1 : 0;
470 ev->data.l[1] = GDK_WINDOW_XID (GTK_WIDGET (window)->window);
472 gdk_event_send_client_message ((GdkEvent *) ev, GDK_WINDOW_XWINDOW (rootwin));
474 gdk_event_free ((GdkEvent *) ev);
479 * hildon_gtk_hscale_new:
481 * Creates a new horizontal scale widget that lets the user select
482 * a value. The value is technically a double between 0.0 and 1.0.
483 * See gtk_adjustment_configure() for reconfiguring the adjustment.
485 * The scale is hildonized, which means that a click or tap immediately
486 * jumps to the desired position, see gtk_range_set_jump_to_position().
487 * Further more the value is not displayed, see gtk_scale_set_draw_value().
489 * Returns: a new hildonized #GtkHScale
494 hildon_gtk_hscale_new (void)
496 GtkWidget *scale = gtk_hscale_new_with_range (0.0, 1.0, 0.1);
500 "jump-to-position", TRUE,
508 * hildon_gtk_vscale_new:
510 * Creates a new vertical scale widget that lets the user select
511 * a value. The value is technically a double between 0.0 and 1.0.
512 * See gtk_adjustment_configure() for reconfiguring the adjustment.
514 * The scale is hildonized, which means that a click or tap immediately
515 * jumps to the desired position, see gtk_range_set_jump_to_position().
516 * Further more the value is not displayed, see gtk_scale_set_draw_value().
518 * Returns: a new hildonized #GtkVScale
523 hildon_gtk_vscale_new (void)
525 GtkWidget *scale = gtk_vscale_new_with_range (0.0, 1.0, 0.1);
529 "jump-to-position", TRUE,