doc: update copyright line for 2019
[adg.git] / src / adg / adg-gtk-utils.c
blob418c917fecbf0148240fa297ab48c38479d4580e
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007-2019 Nicola Fontana <ntd at entidi.it>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 /**
22 * SECTION:adg-gtk-utils
23 * @Section_Id:gtk-utilities
24 * @title: GTK+ utilities
25 * @short_description: Assorted macros and backward compatible fallbacks
27 * Collection of macros and functions that do not fit inside any other topic.
29 * Since: 1.0
30 **/
33 /**
34 * ADG_GTK_MODIFIERS:
36 * A GDK mask of the key/mouse modifiers accepted by the GTK+ widgets
37 * of the ADG library. This means the state of the specified modifiers
38 * is always checked: for example %GDK_CONTROL_MASK and %GDK_SHIFT_MASK
39 * are included, hence keeping <keycap>CTRL</keycap>
40 * and <keycap>SHIFT</keycap> pressed is different from keeping
41 * only <keycap>SHIFT</keycap> pressed. %GDK_LOCK_MASK instead is not
42 * considered, so having it enabled or disabled does not make any
43 * difference while monitoring the status <keycap>SHIFT</keycap>
44 * or <keycap>CTRL</keycap>.
46 * Since: 1.0
47 **/
50 #include "adg-internal.h"
51 #include <gtk/gtk.h>
53 #include "adg-gtk-utils.h"
56 #if GTK_CHECK_VERSION(2, 14, 0)
57 #else
59 /**
60 * gtk_widget_get_window:
61 * @widget: a #GtkWidget
63 * Returns the widget's window if it is realized
64 * or <constant>NULL</constant> otherwise.
66 * This is a fallback API for GTK+ prior to 2.14.
68 * Return value: @widget's window.
70 * Since: 1.0
71 **/
72 GdkWindow *
73 gtk_widget_get_window(GtkWidget *widget)
75 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
77 return widget->window;
80 #endif
82 #if GTK_CHECK_VERSION(2, 20, 0)
83 #else
85 /**
86 * gtk_widget_get_realized:
87 * @widget: a #GtkWidget
89 * Determines whether @widget is realized.
91 * Return value: %TRUE if @widget is realized, %FALSE otherwise
93 * Since: 1.0
94 **/
95 gboolean
96 gtk_widget_get_realized(GtkWidget *widget)
98 g_return_val_if_fail(GTK_IS_WIDGET(widget), FALSE);
99 return GTK_WIDGET_REALIZED(widget);
102 #endif
105 * adg_gtk_window_hide_here:
106 * @window: a #GtkWindow
108 * A convenient function that hides @window and tries to store the
109 * current position. Any subsequent call to gtk_widget_show() will
110 * hopefully reopen the window at the same position.
112 * It can be used instead of gtk_widget_hide() or by connecting it
113 * to a #GtkDialog::response signal, for instance:
115 * <informalexample><programlisting language="C">
116 * g_signal_connect(dialog, "response",
117 * G_CALLBACK(adg_gtk_window_hide_here), NULL);
118 * </programlisting></informalexample>
120 * Since: 1.0
122 void
123 adg_gtk_window_hide_here(GtkWindow *window)
125 gint x, y;
127 g_return_if_fail(GTK_IS_WINDOW(window));
129 gtk_window_get_position(window, &x, &y);
130 gtk_widget_hide((GtkWidget *) window);
131 gtk_window_set_position(window, GTK_WIN_POS_NONE);
132 gtk_window_move(window, x, y);
136 * adg_gtk_toggle_button_sensitivize:
137 * @toggle_button: a #GtkToggleButton
138 * @widget: the #GtkWidget
140 * Assigns the value of the #GtkToggleButton:active property of
141 * @toggle_button to the #GtkWidget:sensitive property of @widget.
142 * Useful to set or reset the sensitiveness of @widget depending
143 * of the state of a check button, for example:
145 * <informalexample><programlisting language="C">
146 * g_signal_connect(toggle_button, "toggled",
147 * G_CALLBACK(adg_gtk_toggle_button_sensitivize), widget1);
148 * g_signal_connect(toggle_button, "toggled",
149 * G_CALLBACK(adg_gtk_toggle_button_sensitivize), widget2);
150 * g_signal_connect(toggle_button, "toggled",
151 * G_CALLBACK(adg_gtk_toggle_button_sensitivize), widget3);
152 * </programlisting></informalexample>
154 * Since: 1.0
156 void
157 adg_gtk_toggle_button_sensitivize(GtkToggleButton *toggle_button,
158 GtkWidget *widget)
160 gboolean is_active;
162 g_return_if_fail(GTK_IS_TOGGLE_BUTTON(toggle_button));
163 g_return_if_fail(GTK_IS_WIDGET(widget));
165 is_active = gtk_toggle_button_get_active(toggle_button);
166 gtk_widget_set_sensitive(widget, is_active);
170 * adg_gtk_use_default_icons:
171 * @dir: the directory where the icons should be installed
173 * Sets the default icon list of every #GtkWindow to a hand-coded
174 * list of ADG icons. Check gtk gtk_window_set_default_icon_list()
175 * for further details.
177 void
178 adg_gtk_use_default_icons(const gchar *dir)
180 const gchar **p_file;
181 const gchar *files[] = {
182 "adg-16.png",
183 "adg-32.png",
184 "adg-48.png",
185 "adg-64.png",
186 "adg-128.png",
187 NULL
189 GList *list;
190 gchar *path;
191 GdkPixbuf *icon;
193 list = NULL;
194 for (p_file = files; *p_file != NULL; ++p_file) {
195 path = adg_find_file(*p_file, dir, NULL);
196 if (path == NULL)
197 continue;
198 icon = gdk_pixbuf_new_from_file(path, NULL);
199 g_free(path);
200 if (icon != NULL)
201 list = g_list_append(list, icon);
204 if (list != NULL) {
205 gtk_window_set_default_icon_list(list);
206 g_list_free(list);