1 /****************************************************************************
2 *
3 * Copyright (c) 2005 Sasha Vasko <sasha at aftercode.net>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
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 General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 ****************************************************************************/
20
21 #define LOCAL_DEBUG
22 #include "../configure.h"
23
24 #include "../libAfterStep/asapp.h"
25
26
27 #include "asgtk.h"
28
29 #include <gdk/gdkx.h>
30
31 #include "../libAfterStep/module.h"
32 #include "../libAfterConf/afterconf.h"
33
print_default_gtkrc_files()34 static void print_default_gtkrc_files ()
35 {
36 char **rcfiles = gtk_rc_get_default_files ();
37
38 if (rcfiles && rcfiles[0]) {
39 int i = 0;
40
41 while (rcfiles[i]) {
42 show_progress ("GTK rcfile[%d] = \"%s\";", i, rcfiles[i]);
43 ++i;
44 }
45 } else
46 show_progress ("no GTK rcfile defined;");
47 }
48
49
50 void
init_asgtkapp(int argc,char * argv[],const char * module_name,void (* custom_usage_func)(void),ASFlagType opt_mask)51 init_asgtkapp (int argc, char *argv[], const char *module_name,
52 void (*custom_usage_func) (void), ASFlagType opt_mask)
53 {
54 GdkDisplay *gdk_display;
55 int i;
56 static char *deleted_arg = "_deleted_arg_";
57
58 InitMyApp (module_name, argc, argv, NULL, custom_usage_func, opt_mask);
59 for (i = 1; i < argc; ++i)
60 if (argv[i] == NULL)
61 argv[i] = strdup (deleted_arg);
62 LinkAfterStepConfig ();
63 InitSession ();
64
65 #ifdef ENABLE_NLS
66 bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
67 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
68 textdomain (GETTEXT_PACKAGE);
69 #endif
70
71 gtk_set_locale ();
72 gtk_init (&argc, &argv);
73
74 #if defined(LOCAL_DEBUG) && !defined(NO_DEBUG_OUTPUT)
75 print_default_gtkrc_files ();
76 #endif
77
78 gdk_display = gdk_display_get_default ();
79
80 LoadColorScheme ();
81
82 ConnectXDisplay (gdk_x11_display_get_xdisplay (gdk_display), NULL,
83 False);
84 ReloadASEnvironment (NULL, NULL, NULL, False, True);
85 /*MyArgs.display_name */
86 }
87
88
color_name2GdkColor(const char * name,GdkColor * color)89 GdkColor *color_name2GdkColor (const char *name, GdkColor * color)
90 {
91 ARGB32 argb;
92
93 parse_argb_color (name, &argb);
94 color->pixel = argb;
95 color->red = ARGB32_RED16 (argb);
96 color->green = ARGB32_GREEN16 (argb);
97 color->blue = ARGB32_BLUE16 (argb);
98 return color;
99 }
100
GdkColor2string(GdkColor * color,int alpha)101 char *GdkColor2string (GdkColor * color, int alpha)
102 {
103 char buffer[10];
104
105 if (alpha >= 0)
106 sprintf (&buffer[0], "#%2.2X%2.2X%2.2X%2.2X", alpha >> 8,
107 color->red >> 8, color->green >> 8, color->blue >> 8);
108 else
109 sprintf (&buffer[0], "#%2.2X%2.2X%2.2X", color->red >> 8,
110 color->green >> 8, color->blue >> 8);
111 return mystrdup (&buffer[0]);
112 }
113
114 static GtkStyle *ASGtkStyleNormal = NULL;
115 static GtkStyle *ASGtkStyleButton = NULL;
116
get_colorschemed_style_normal()117 GtkStyle *get_colorschemed_style_normal ()
118 {
119 if (ASGtkStyleNormal == NULL) {
120 ASGtkStyleNormal = gtk_style_new (); /* gtk_style_copy(GTK_WIDGET(WallpaperState.main_window)->style); */
121 color_name2GdkColor ("InactiveText1",
122 &(ASGtkStyleNormal->fg[GTK_STATE_NORMAL]));
123 color_name2GdkColor ("InactiveText2",
124 &(ASGtkStyleNormal->fg[GTK_STATE_ACTIVE]));
125 color_name2GdkColor ("HighInactiveText",
126 &(ASGtkStyleNormal->fg[GTK_STATE_PRELIGHT]));
127 color_name2GdkColor ("ActiveText",
128 &(ASGtkStyleNormal->fg[GTK_STATE_SELECTED]));
129 color_name2GdkColor ("DisabledText",
130 &(ASGtkStyleNormal->fg[GTK_STATE_INSENSITIVE]));
131 color_name2GdkColor ("Inactive1",
132 &(ASGtkStyleNormal->bg[GTK_STATE_NORMAL]));
133 color_name2GdkColor ("Inactive2",
134 &(ASGtkStyleNormal->bg[GTK_STATE_ACTIVE]));
135 color_name2GdkColor ("HighInactive",
136 &(ASGtkStyleNormal->bg[GTK_STATE_PRELIGHT]));
137 color_name2GdkColor ("Active",
138 &(ASGtkStyleNormal->bg[GTK_STATE_SELECTED]));
139 color_name2GdkColor ("Inactive1",
140 &(ASGtkStyleNormal->bg[GTK_STATE_INSENSITIVE]));
141 color_name2GdkColor ("Inactive1Light",
142 &(ASGtkStyleNormal->light[GTK_STATE_NORMAL]));
143 color_name2GdkColor ("Inactive2Light",
144 &(ASGtkStyleNormal->light[GTK_STATE_ACTIVE]));
145 color_name2GdkColor ("HighInactiveLight",
146 &(ASGtkStyleNormal->light[GTK_STATE_PRELIGHT]));
147 color_name2GdkColor ("ActiveLight",
148 &(ASGtkStyleNormal->light[GTK_STATE_SELECTED]));
149 color_name2GdkColor ("Inactive1Light",
150 &(ASGtkStyleNormal->
151 light[GTK_STATE_INSENSITIVE]));
152 color_name2GdkColor ("Inactive1Dark",
153 &(ASGtkStyleNormal->dark[GTK_STATE_NORMAL]));
154 color_name2GdkColor ("Inactive2Dark",
155 &(ASGtkStyleNormal->dark[GTK_STATE_ACTIVE]));
156 color_name2GdkColor ("HighInactiveDark",
157 &(ASGtkStyleNormal->dark[GTK_STATE_PRELIGHT]));
158 color_name2GdkColor ("ActiveDark",
159 &(ASGtkStyleNormal->dark[GTK_STATE_SELECTED]));
160 color_name2GdkColor ("Inactive1Dark",
161 &(ASGtkStyleNormal->dark[GTK_STATE_INSENSITIVE]));
162 color_name2GdkColor ("Inactive1",
163 &(ASGtkStyleNormal->mid[GTK_STATE_NORMAL]));
164 color_name2GdkColor ("Inactive2",
165 &(ASGtkStyleNormal->mid[GTK_STATE_ACTIVE]));
166 color_name2GdkColor ("HighInactive",
167 &(ASGtkStyleNormal->mid[GTK_STATE_PRELIGHT]));
168 color_name2GdkColor ("Active",
169 &(ASGtkStyleNormal->mid[GTK_STATE_SELECTED]));
170 color_name2GdkColor ("Inactive1",
171 &(ASGtkStyleNormal->mid[GTK_STATE_INSENSITIVE]));
172 color_name2GdkColor ("InactiveText1",
173 &(ASGtkStyleNormal->text[GTK_STATE_NORMAL]));
174 color_name2GdkColor ("InactiveText2",
175 &(ASGtkStyleNormal->text[GTK_STATE_ACTIVE]));
176 color_name2GdkColor ("HighInactiveText",
177 &(ASGtkStyleNormal->text[GTK_STATE_PRELIGHT]));
178 color_name2GdkColor ("ActiveText",
179 &(ASGtkStyleNormal->text[GTK_STATE_SELECTED]));
180 color_name2GdkColor ("InactiveText1",
181 &(ASGtkStyleNormal->text[GTK_STATE_INSENSITIVE]));
182
183 color_name2GdkColor ("Inactive1",
184 &(ASGtkStyleNormal->base[GTK_STATE_NORMAL]));
185 color_name2GdkColor ("Inactive2",
186 &(ASGtkStyleNormal->base[GTK_STATE_ACTIVE]));
187 color_name2GdkColor ("HighInactive",
188 &(ASGtkStyleNormal->base[GTK_STATE_PRELIGHT]));
189 color_name2GdkColor ("Active",
190 &(ASGtkStyleNormal->base[GTK_STATE_SELECTED]));
191 color_name2GdkColor ("Inactive1",
192 &(ASGtkStyleNormal->base[GTK_STATE_INSENSITIVE]));
193 #if 0
194 color_name2GdkColor ("",
195 &(ASGtkStyleNormal->text_aa[GTK_STATE_NORMAL]));
196 color_name2GdkColor ("",
197 &(ASGtkStyleNormal->text_aa[GTK_STATE_ACTIVE]));
198 color_name2GdkColor ("",
199 &(ASGtkStyleNormal->text_aa[GTK_STATE_PRELIGHT]));
200 color_name2GdkColor ("",
201 &(ASGtkStyleNormal->text_aa[GTK_STATE_SELECTED]));
202 color_name2GdkColor ("",
203 &(ASGtkStyleNormal->
204 text_aa[GTK_STATE_INSENSITIVE]));
205 #endif
206 }
207 return ASGtkStyleNormal;
208 }
209
get_colorschemed_style_button()210 GtkStyle *get_colorschemed_style_button ()
211 {
212 if (ASGtkStyleButton == NULL) {
213 ASGtkStyleButton = gtk_style_copy (get_colorschemed_style_normal ());
214 color_name2GdkColor ("HighInactiveText",
215 &(ASGtkStyleButton->fg[GTK_STATE_NORMAL]));
216 color_name2GdkColor ("HighInactiveText",
217 &(ASGtkStyleButton->fg[GTK_STATE_ACTIVE]));
218 color_name2GdkColor ("HighInactiveText",
219 &(ASGtkStyleButton->fg[GTK_STATE_PRELIGHT]));
220 color_name2GdkColor ("ActiveText",
221 &(ASGtkStyleButton->fg[GTK_STATE_SELECTED]));
222 color_name2GdkColor ("HighInactiveText",
223 &(ASGtkStyleButton->fg[GTK_STATE_INSENSITIVE]));
224 color_name2GdkColor ("HighInactiveBack",
225 &(ASGtkStyleButton->bg[GTK_STATE_NORMAL]));
226 color_name2GdkColor ("HighInactiveLight",
227 &(ASGtkStyleButton->bg[GTK_STATE_ACTIVE]));
228 color_name2GdkColor ("HighInactive",
229 &(ASGtkStyleButton->bg[GTK_STATE_PRELIGHT]));
230 color_name2GdkColor ("Active",
231 &(ASGtkStyleButton->bg[GTK_STATE_SELECTED]));
232 color_name2GdkColor ("HighInactive",
233 &(ASGtkStyleButton->bg[GTK_STATE_INSENSITIVE]));
234 color_name2GdkColor ("Inactive1Light",
235 &(ASGtkStyleButton->light[GTK_STATE_NORMAL]));
236 color_name2GdkColor ("Inactive2Light",
237 &(ASGtkStyleButton->light[GTK_STATE_ACTIVE]));
238 color_name2GdkColor ("HighInactiveLight",
239 &(ASGtkStyleButton->light[GTK_STATE_PRELIGHT]));
240 color_name2GdkColor ("ActiveLight",
241 &(ASGtkStyleButton->light[GTK_STATE_SELECTED]));
242 color_name2GdkColor ("Inactive1Light",
243 &(ASGtkStyleButton->
244 light[GTK_STATE_INSENSITIVE]));
245 color_name2GdkColor ("Inactive1Dark",
246 &(ASGtkStyleButton->dark[GTK_STATE_NORMAL]));
247 color_name2GdkColor ("Inactive2Dark",
248 &(ASGtkStyleButton->dark[GTK_STATE_ACTIVE]));
249 color_name2GdkColor ("HighInactiveDark",
250 &(ASGtkStyleButton->dark[GTK_STATE_PRELIGHT]));
251 color_name2GdkColor ("ActiveDark",
252 &(ASGtkStyleButton->dark[GTK_STATE_SELECTED]));
253 color_name2GdkColor ("Inactive1Dark",
254 &(ASGtkStyleButton->dark[GTK_STATE_INSENSITIVE]));
255 color_name2GdkColor ("Inactive1",
256 &(ASGtkStyleButton->mid[GTK_STATE_NORMAL]));
257 color_name2GdkColor ("Inactive2",
258 &(ASGtkStyleButton->mid[GTK_STATE_ACTIVE]));
259 color_name2GdkColor ("HighInactive",
260 &(ASGtkStyleButton->mid[GTK_STATE_PRELIGHT]));
261 color_name2GdkColor ("Active",
262 &(ASGtkStyleButton->mid[GTK_STATE_SELECTED]));
263 color_name2GdkColor ("Inactive1",
264 &(ASGtkStyleButton->mid[GTK_STATE_INSENSITIVE]));
265 color_name2GdkColor ("HighInactiveText",
266 &(ASGtkStyleButton->text[GTK_STATE_NORMAL]));
267 color_name2GdkColor ("HighInactiveText",
268 &(ASGtkStyleButton->text[GTK_STATE_ACTIVE]));
269 color_name2GdkColor ("HighInactiveText",
270 &(ASGtkStyleButton->text[GTK_STATE_PRELIGHT]));
271 color_name2GdkColor ("ActiveText",
272 &(ASGtkStyleButton->text[GTK_STATE_SELECTED]));
273 color_name2GdkColor ("DisabledText",
274 &(ASGtkStyleButton->text[GTK_STATE_INSENSITIVE]));
275
276 color_name2GdkColor ("HighInactiveDark",
277 &(ASGtkStyleButton->base[GTK_STATE_NORMAL]));
278 color_name2GdkColor ("HighInactive",
279 &(ASGtkStyleButton->base[GTK_STATE_ACTIVE]));
280 color_name2GdkColor ("HighInactiveLight",
281 &(ASGtkStyleButton->base[GTK_STATE_PRELIGHT]));
282 color_name2GdkColor ("Active",
283 &(ASGtkStyleButton->base[GTK_STATE_SELECTED]));
284 color_name2GdkColor ("HighInactive",
285 &(ASGtkStyleButton->base[GTK_STATE_INSENSITIVE]));
286 #if 0
287 color_name2GdkColor ("",
288 &(ASGtkStyleButton->text_aa[GTK_STATE_NORMAL]));
289 color_name2GdkColor ("",
290 &(ASGtkStyleButton->text_aa[GTK_STATE_ACTIVE]));
291 color_name2GdkColor ("",
292 &(ASGtkStyleButton->text_aa[GTK_STATE_PRELIGHT]));
293 color_name2GdkColor ("",
294 &(ASGtkStyleButton->text_aa[GTK_STATE_SELECTED]));
295 color_name2GdkColor ("",
296 &(ASGtkStyleButton->
297 text_aa[GTK_STATE_INSENSITIVE]));
298 #endif
299 }
300 return ASGtkStyleButton;
301 }
302
colorize_gtk_window(GtkWidget * window)303 void colorize_gtk_window (GtkWidget * window)
304 {
305 GdkColor bg;
306 ARGB32 argb;
307 CARD32 val;
308
309 parse_argb_color ("Base", &argb);
310 val =
311 ASCS_BLACK_O_WHITE_CRITERIA16_VAL (ARGB32_RED16 (argb),
312 ARGB32_GREEN16 (argb),
313 ARGB32_BLUE16 (argb));
314 if (val >= 0x09FFF) {
315 LOCAL_DEBUG_OUT ("Base is light, val = 0x%lX", val);
316 color_name2GdkColor ("BaseDark", &bg);
317 } else {
318 LOCAL_DEBUG_OUT ("Base is dark, val = 0x%lX", val);
319 color_name2GdkColor ("BaseLight", &bg);
320 }
321 gtk_widget_modify_bg (window, GTK_STATE_NORMAL, &bg);
322 }
323
324 void
colorize_gtk_tree_view(GtkCellRenderer * cell,GtkTreeViewColumn * column)325 colorize_gtk_tree_view (GtkCellRenderer * cell, GtkTreeViewColumn * column)
326 {
327 #if 0
328 GdkColor bg;
329 GdkColor fg;
330
331 color_name2GdkColor ("Inactive1", &bg);
332 color_name2GdkColor ("InactiveText1", &fg);
333
334 g_object_set (G_OBJECT (cell), "background-gdk", &bg, "foreground-gdk",
335 &fg, NULL);
336 g_object_set (G_OBJECT (column), "background-gdk", &bg, "foreground-gdk",
337 &fg, NULL);
338 #else
339 gtk_widget_set_style (GTK_WIDGET (cell),
340 get_colorschemed_style_normal ());
341 #endif
342
343 }
344
colorize_gtk_widget(GtkWidget * widget,gpointer data)345 void colorize_gtk_widget (GtkWidget * widget, gpointer data)
346 {
347
348 GtkStyle *style = data ? GTK_STYLE (data) : NULL;
349 int i;
350
351 if (style == NULL)
352 style = get_colorschemed_style_normal ();
353
354 gtk_widget_set_style (widget, style);
355 for (i = 0; i < 5; ++i)
356 gtk_widget_modify_fg (widget, i, &(style->fg[i]));
357 LOCAL_DEBUG_OUT ("widget %p", widget);
358 if (GTK_IS_CONTAINER (widget))
359 gtk_container_forall (GTK_CONTAINER (widget), colorize_gtk_widget,
360 data);
361
362 }
363
364
colorize_gtk_tree_view_window(GtkWidget * widget)365 void colorize_gtk_tree_view_window (GtkWidget * widget)
366 {
367 colorize_gtk_widget (widget, get_colorschemed_style_button ());
368 gtk_widget_set_style (widget, get_colorschemed_style_normal ());
369 }
370
asgtk_new_button(const char * stock,const char * label)371 GtkWidget *asgtk_new_button (const char *stock, const char *label)
372 {
373 GtkWidget *btn;
374
375 if (stock != NULL) {
376 btn = gtk_button_new_from_stock (stock);
377 if (label) {
378 gtk_button_set_label (GTK_BUTTON (btn), label);
379 #if (GTK_MAJOR_VERSION>=2) && (GTK_MINOR_VERSION>=6)
380 gtk_button_set_image (GTK_BUTTON (btn),
381 gtk_image_new_from_stock (stock,
382 GTK_ICON_SIZE_BUTTON));
383 #endif
384 }
385 } else if (label)
386 btn = gtk_button_new_with_label (label);
387 else
388 btn = gtk_button_new ();
389 colorize_gtk_widget (GTK_WIDGET (btn), get_colorschemed_style_button ());
390
391 return btn;
392 }
393
asgtk_add_button_to_box(GtkBox * hbox,const char * stock,const char * label,GCallback func,gpointer user_data)394 GtkWidget *asgtk_add_button_to_box (GtkBox * hbox, const char *stock,
395 const char *label, GCallback func,
396 gpointer user_data)
397 {
398 GtkWidget *btn = asgtk_new_button (stock, label);
399
400 gtk_widget_show (btn);
401 if (hbox)
402 gtk_box_pack_start (hbox, btn, FALSE, FALSE, 0);
403 g_signal_connect ((gpointer) btn, "clicked", G_CALLBACK (func),
404 user_data);
405 return btn;
406 }
407
asgtk_combo_box_get_active_text(GtkComboBox * combobox)408 const char *asgtk_combo_box_get_active_text (GtkComboBox * combobox)
409 {
410 GtkTreeIter iter;
411 GtkTreeModel *model = gtk_combo_box_get_model (combobox);
412 char *text;
413
414 if (!gtk_combo_box_get_active_iter (combobox, &iter))
415 return NULL;
416
417 gtk_tree_model_get (model, &iter, 0, &text, -1);
418 return text;
419 }
420
asgtk_combo_box_add_to_history(GtkComboBox * cb,const char * str)421 void asgtk_combo_box_add_to_history (GtkComboBox * cb, const char *str)
422 {
423 GtkTreeModel *model = gtk_combo_box_get_model (cb);
424 GtkTreeIter iter;
425
426 if (gtk_tree_model_get_iter_first (model, &iter)) {
427 do {
428 gchar *val = NULL;
429
430 gtk_tree_model_get (model, &iter, 0, &val, -1);
431 if (val && strcmp (str, val) == 0) {
432 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
433 break;
434 }
435 }
436 while (gtk_tree_model_iter_next (model, &iter));
437 }
438 gtk_combo_box_prepend_text (cb, str);
439 gtk_combo_box_set_active (cb, 0);
440 }
441
442
443
444 Bool
asgtk_yes_no_question1(GtkWidget * main_window,const char * format,const char * detail1)445 asgtk_yes_no_question1 (GtkWidget * main_window, const char *format,
446 const char *detail1)
447 {
448 Bool result = False;
449 GtkWidget *dialog = gtk_message_dialog_new (GTK_WINDOW (main_window),
450 GTK_DIALOG_DESTROY_WITH_PARENT,
451 GTK_MESSAGE_QUESTION,
452 GTK_BUTTONS_YES_NO,
453 format, detail1);
454
455 result = (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_YES);
456 gtk_widget_destroy (dialog);
457 return result;
458 }
459
460 void
asgtk_warning2(GtkWidget * main_window,const char * format,const char * detail1,const char * detail2)461 asgtk_warning2 (GtkWidget * main_window, const char *format,
462 const char *detail1, const char *detail2)
463 {
464 GtkWidget *dialog = gtk_message_dialog_new (GTK_WINDOW (main_window),
465 GTK_DIALOG_DESTROY_WITH_PARENT,
466 GTK_MESSAGE_WARNING,
467 GTK_BUTTONS_OK,
468 format, detail1, detail2);
469
470 gtk_dialog_run (GTK_DIALOG (dialog));
471 gtk_widget_destroy (dialog);
472 }
473
474 void
asgtk_info2(GtkWidget * main_window,const char * format,const char * detail1,const char * detail2)475 asgtk_info2 (GtkWidget * main_window, const char *format,
476 const char *detail1, const char *detail2)
477 {
478 GtkWidget *dialog = gtk_message_dialog_new (GTK_WINDOW (main_window),
479 GTK_DIALOG_DESTROY_WITH_PARENT,
480 GTK_MESSAGE_INFO,
481 GTK_BUTTONS_OK,
482 format, detail1, detail2);
483
484 gtk_dialog_run (GTK_DIALOG (dialog));
485 gtk_widget_destroy (dialog);
486 }
487
488
find_combobox_entry(GtkWidget * widget,gpointer data)489 void find_combobox_entry (GtkWidget * widget, gpointer data)
490 {
491 GtkWidget **pentry = data;
492
493 if (GTK_IS_ENTRY (widget))
494 *pentry = widget;
495 }
496