1 #define __SPW_UTILITIES_C__
3 /*
4 * Inkscape Widget Utilities
5 *
6 * Authors:
7 * Bryce W. Harrington <brycehar@bryceharrington.com>
8 * bulia byak <buliabyak@users.sf.net>
9 *
10 * Copyright (C) 2003 Bryce W. Harrington
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
15 #ifdef HAVE_CONFIG_H
16 # include "config.h"
17 #endif
19 #include <cstring>
20 #include <string>
21 #include <gtk/gtk.h>
23 #include "selection.h"
25 #include "helper/unit-menu.h"
28 #include <gtkmm/label.h>
29 #include <gtkmm/box.h>
30 #include <gtkmm/table.h>
32 /**
33 * Creates a label widget with the given text, at the given col, row
34 * position in the table.
35 */
36 Gtk::Label *
37 spw_label(Gtk::Table *table, const gchar *label_text, int col, int row, Gtk::Widget* target)
38 {
39 Gtk::Label *label_widget = new Gtk::Label();
40 g_assert(label_widget != NULL);
41 if (target != NULL)
42 {
43 label_widget->set_text_with_mnemonic(label_text);
44 label_widget->set_mnemonic_widget(*target);
45 }
46 else
47 {
48 label_widget->set_text(label_text);
49 }
50 label_widget->set_alignment(1.0, 0.5);
51 label_widget->show();
52 table->attach(*label_widget, col, col+1, row, row+1, (Gtk::EXPAND | Gtk::FILL), static_cast<Gtk::AttachOptions>(0), 4, 0);
53 return label_widget;
54 }
56 GtkWidget *
57 spw_label_old(GtkWidget *table, const gchar *label_text, int col, int row)
58 {
59 GtkWidget *label_widget;
61 label_widget = gtk_label_new (label_text);
62 g_assert(label_widget != NULL);
63 gtk_misc_set_alignment (GTK_MISC (label_widget), 1.0, 0.5);
64 gtk_widget_show (label_widget);
65 gtk_table_attach (GTK_TABLE (table), label_widget, col, col+1, row, row+1,
66 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), (GtkAttachOptions)0, 4, 0);
67 return label_widget;
68 }
70 /**
71 * Creates a horizontal layout manager with 4-pixel spacing between children
72 * and space for 'width' columns.
73 */
74 Gtk::HBox *
75 spw_hbox(Gtk::Table * table, int width, int col, int row)
76 {
77 /* Create a new hbox with a 4-pixel spacing between children */
78 Gtk::HBox *hb = new Gtk::HBox(false, 4);
79 g_assert(hb != NULL);
80 hb->show();
81 table->attach(*hb, col, col+width, row, row+1, (Gtk::EXPAND | Gtk::FILL), static_cast<Gtk::AttachOptions>(0), 0, 0);
82 return hb;
83 }
85 /**
86 * Creates a checkbutton widget and adds it to a vbox.
87 * This is a compound widget that includes a label.
88 */
89 GtkWidget *spw_vbox_checkbutton(GtkWidget *dialog, GtkWidget *vbox,
90 const gchar *label, const gchar *tip, gchar *key, GCallback cb)
91 {
92 g_assert (dialog != NULL);
93 g_assert (vbox != NULL);
95 GtkTooltips *tt = gtk_tooltips_new ();
97 GtkWidget *b = gtk_check_button_new_with_label (label);
98 gtk_tooltips_set_tip(tt, b, tip, NULL);
99 g_assert (b != NULL);
100 gtk_widget_show (b);
101 gtk_box_pack_start (GTK_BOX (vbox), b, FALSE, FALSE, 0);
102 gtk_object_set_data (GTK_OBJECT (b), "key", key);
103 gtk_object_set_data (GTK_OBJECT (dialog), key, b);
104 g_signal_connect (G_OBJECT (b), "toggled", cb, dialog);
105 return b;
106 }
109 /**
110 * Creates a checkbutton widget and adds it to a table.
111 * This is a compound widget that includes a label.
112 */
113 GtkWidget *
114 spw_checkbutton(GtkWidget * dialog, GtkWidget * table,
115 const gchar * label, gchar * key, int /*col*/, int row,
116 int insensitive, GCallback cb)
117 {
118 GtkWidget *b;
120 g_assert(dialog != NULL);
121 g_assert(table != NULL);
123 GtkWidget *l = gtk_label_new (label);
124 gtk_misc_set_alignment (GTK_MISC (l), 1.0, 0.5);
125 gtk_widget_show (l);
126 gtk_table_attach (GTK_TABLE (table), l, 0, 1, row, row+1,
127 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), (GtkAttachOptions)0, 0, 0);
129 b = gtk_check_button_new ();
130 gtk_widget_show (b);
131 gtk_table_attach (GTK_TABLE (table), b, 1, 2, row, row+1,
132 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), (GtkAttachOptions)0, 0, 0);
133 gtk_object_set_data (GTK_OBJECT (b), "key", key);
134 gtk_object_set_data (GTK_OBJECT (dialog), key, b);
135 g_signal_connect (G_OBJECT (b), "toggled", cb, dialog);
136 if (insensitive == 1) {
137 gtk_widget_set_sensitive (b, FALSE);
138 }
139 return b;
140 }
142 /**
143 * Creates a dropdown widget. This is a compound widget that includes
144 * a label as well as the dropdown.
145 */
146 GtkWidget *
147 spw_dropdown(GtkWidget * dialog, GtkWidget * table,
148 const gchar * label_text, gchar * key, int row,
149 GtkWidget * selector
150 )
151 {
152 g_assert(dialog != NULL);
153 g_assert(table != NULL);
154 g_assert(selector != NULL);
156 spw_label_old(table, label_text, 0, row);
158 gtk_widget_show (selector);
159 gtk_table_attach (GTK_TABLE (table), selector, 1, 2, row, row+1,
160 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), (GtkAttachOptions)0, 0, 0);
161 gtk_object_set_data (GTK_OBJECT (dialog), key, selector);
162 return selector;
163 }
165 /**
166 * Creates a unit selector widget, used for selecting whether one wishes
167 * to measure screen elements in millimeters, points, etc. This is a
168 * compound unit that includes a label as well as the dropdown selector.
169 */
170 GtkWidget *
171 spw_unit_selector(GtkWidget * dialog, GtkWidget * table,
172 const gchar * label_text, gchar * key, int row,
173 GtkWidget * us, GCallback cb, bool can_be_negative)
174 {
175 GtkWidget * sb;
176 GtkObject * a;
178 g_assert(dialog != NULL);
179 g_assert(table != NULL);
180 g_assert(us != NULL);
182 spw_label_old(table, label_text, 0, row);
184 a = gtk_adjustment_new (0.0, can_be_negative?-1e6:0, 1e6, 1.0, 10.0, 10.0);
185 g_assert(a != NULL);
186 gtk_object_set_data (GTK_OBJECT (a), "key", key);
187 gtk_object_set_data (GTK_OBJECT (a), "unit_selector", us);
188 gtk_object_set_data (GTK_OBJECT (dialog), key, a);
189 sp_unit_selector_add_adjustment (SP_UNIT_SELECTOR (us), GTK_ADJUSTMENT (a));
190 sb = gtk_spin_button_new (GTK_ADJUSTMENT (a), 1.0, 4);
191 g_assert(sb != NULL);
192 gtk_widget_show (sb);
193 gtk_table_attach (GTK_TABLE (table), sb, 1, 2, row, row+1,
194 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), (GtkAttachOptions)0, 0, 0);
195 g_signal_connect (G_OBJECT (a), "value_changed", cb, dialog);
196 return sb;
197 }
199 void
200 sp_set_font_size_recursive (GtkWidget *w, gpointer font)
201 {
202 guint size = GPOINTER_TO_UINT (font);
204 PangoFontDescription* pan = pango_font_description_new ();
205 pango_font_description_set_size (pan, size);
207 gtk_widget_modify_font (w, pan);
209 if (GTK_IS_CONTAINER(w)) {
210 gtk_container_foreach (GTK_CONTAINER(w), (GtkCallback) sp_set_font_size_recursive, font);
211 }
213 pango_font_description_free (pan);
214 }
216 void
217 sp_set_font_size (GtkWidget *w, guint font)
218 {
219 sp_set_font_size_recursive (w, GUINT_TO_POINTER(font));
220 }
222 void
223 sp_set_font_size_smaller (GtkWidget *w)
224 {
225 PangoContext *pc = gtk_widget_get_pango_context (w);
226 PangoFontDescription* pfd = pango_context_get_font_description (pc);
227 guint size = pango_font_description_get_size (pfd);
228 sp_set_font_size_recursive (w, GUINT_TO_POINTER((int) (0.8*size)));
229 }
231 /**
232 \brief Finds the descendant of w which has the data with the given key and returns the data, or NULL if there's none
233 */
234 gpointer
235 sp_search_by_data_recursive (GtkWidget *w, gpointer key)
236 {
237 gpointer r = NULL;
239 if (w && GTK_IS_OBJECT(w)) {
240 r = gtk_object_get_data (GTK_OBJECT(w), (gchar *) key);
241 }
242 if (r) return r;
244 if (GTK_IS_CONTAINER(w)) {
245 GList *ch = gtk_container_get_children (GTK_CONTAINER(w));
246 for (GList *i = ch; i != NULL; i = i->next) {
247 r = sp_search_by_data_recursive(GTK_WIDGET(i->data), key);
248 if (r) return r;
249 }
250 }
252 return NULL;
253 }
255 /**
256 \brief Returns the descendant of w which has the given key and value pair, or NULL if there's none
257 */
258 GtkWidget *
259 sp_search_by_value_recursive (GtkWidget *w, gchar *key, gchar *value)
260 {
261 gchar *r = NULL;
262 GtkWidget *child;
264 if (w && GTK_IS_OBJECT(w)) {
265 r = (gchar *) gtk_object_get_data (GTK_OBJECT(w), key);
266 }
267 if (r && !strcmp (r, value)) return w;
269 if (GTK_IS_CONTAINER(w)) {
270 GList *ch = gtk_container_get_children (GTK_CONTAINER(w));
271 for (GList *i = ch; i != NULL; i = i->next) {
272 child = sp_search_by_value_recursive(GTK_WIDGET(i->data), key, value);
273 if (child) return child;
274 }
275 }
277 return NULL;
278 }
280 /*
281 Local Variables:
282 mode:c++
283 c-file-style:"stroustrup"
284 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
285 indent-tabs-mode:nil
286 fill-column:99
287 End:
288 */
289 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :