1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
|
/***************************************************************************
main.cpp - description
-------------------
begin : Wed Jan 1 19:06:46 GMT+4 2003
copyright : (C) 2003 - 2005 by Alex Shaduri
email : ashaduri '@' gmail.com
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <sstream>
#include <gtk/gtk.h>
#ifdef _WIN32
#include "win32util.h"
#include <io.h>
#else
#include <sys/stat.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "interface.h"
#include "callbacks.h"
#include "support.h"
#include "main.h"
// ------------------------------------------------------
GtkWidget* g_main_window = 0;
// GtkWidget* g_fontsel_dialog = 0;
static std::string s_tmp_file;
// ------------------------------------------------------
static std::string gchar_to_string(gchar* gstr)
{
std::string str = (gstr ? gstr : "");
g_free(gstr);
return str;
}
std::string get_home_dir()
{
std::string dir;
if (g_get_home_dir())
dir = g_get_home_dir();
#ifdef _WIN32
if (dir == "") {
dir = win32_get_registry_value_string(HKEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "Personal");
}
if (dir == "") {
dir = win32_get_registry_value_string(HKEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "User");
}
#endif
return dir;
}
// ------------------------------------------------------
static std::string s_orig_theme;
static std::string s_orig_font;
std::string& get_orig_theme()
{
return s_orig_theme;
}
std::string& get_orig_font()
{
return s_orig_font;
}
// ------------------------------------------------------
std::string get_current_theme()
{
GtkSettings* settings = gtk_settings_get_default();
gchar* theme;
g_object_get(settings, "gtk-theme-name", &theme, NULL);
return gchar_to_string(theme);
}
std::string get_current_font()
{
return gchar_to_string(pango_font_description_to_string(gtk_rc_get_style(g_main_window)->font_desc));
}
// ------------------------------------------------------
std::string get_selected_theme()
{
GtkTreeView* treeview = GTK_TREE_VIEW(lookup_widget(g_main_window, "main_themelist"));
GtkTreeModel* model = gtk_tree_view_get_model(treeview);
GtkTreeSelection* selection = gtk_tree_view_get_selection(treeview);
GtkTreeIter iter;
gtk_tree_selection_get_selected(selection, 0, &iter);
gchar* theme_name;
gtk_tree_model_get(model, &iter, 0, &theme_name, -1);
// std::cout << theme_name << "\n";
return gchar_to_string(theme_name);
}
std::string get_selected_font()
{
// GtkWidget* fontentry = lookup_widget(g_main_window, "main_fontentry");
// return gtk_entry_get_text(GTK_ENTRY(fontentry));
bool default_font = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(lookup_widget(g_main_window, "main_use_default_font_radio")));
if (default_font)
return "";
GtkWidget* fontbutton = lookup_widget(g_main_window, "main_font_selector_button");
return gtk_font_button_get_font_name(GTK_FONT_BUTTON(fontbutton));
}
// ------------------------------------------------------
static void themelist_selection_changed_cb(GtkTreeSelection* selection, gpointer data)
{
if (gtk_tree_selection_get_selected (selection, 0, 0))
apply_theme(get_selected_theme(), get_current_font());
}
// ------------------------------------------------------
static void populate_with_themes(GtkWidget* w)
{
std::string search_path = gchar_to_string(gtk_rc_get_theme_dir());
//std::string search_path = gchar_to_string(g_build_filename("lib", "gtk-2.0", "2.10.0", "engines", NULL));
if (search_path.size() && search_path[search_path.size() -1] != G_DIR_SEPARATOR)
search_path += G_DIR_SEPARATOR_S;
GDir* gdir = g_dir_open(search_path.c_str(), 0, NULL);
if (gdir == NULL)
return;
char* name;
GList* glist = 0;
while ( (name = const_cast<char*>(g_dir_read_name(gdir))) != NULL ) {
std::string filename = name;
// if (g_ascii_strup(fname.c_str(), -1) == "Default")
// continue;
std::string fullname = search_path + filename;
std::string rc = fullname; rc += G_DIR_SEPARATOR_S; rc += "gtk-2.0"; rc += G_DIR_SEPARATOR_S; rc += "gtkrc";
bool is_dir = 0;
if (g_file_test(fullname.c_str(), G_FILE_TEST_IS_DIR))
is_dir = 1;
if (is_dir && g_file_test(rc.c_str(), G_FILE_TEST_IS_REGULAR)) {
glist = g_list_insert_sorted(glist, g_strdup(filename.c_str()), (GCompareFunc)strcmp);
}
}
g_dir_close(gdir);
// ---------------- tree
GtkTreeView* treeview = GTK_TREE_VIEW(w);
GtkListStore *store = gtk_list_store_new (1, G_TYPE_STRING);
gtk_tree_view_set_model(treeview, GTK_TREE_MODEL(store));
GtkTreeViewColumn* column = gtk_tree_view_column_new_with_attributes (
"Theme", gtk_cell_renderer_text_new(),
"text", 0,
NULL);
gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
GtkTreeIter iter;
int i =0, curr=0;
while (char* theme = (char*)g_list_nth_data(glist, i)) {
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, 0, theme, -1);
if (strcmp(theme, get_current_theme().c_str()) == 0) {
curr = i;
}
++i;
}
GtkTreeSelection* selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview));
// set the default theme
// THIS IS IMPORTANT!!!
gtk_widget_grab_focus(w);
std::stringstream str;
str << curr;
GtkTreePath* selpath = gtk_tree_path_new_from_string (str.str().c_str());
if (selpath) {
gtk_tree_selection_select_path(selection, selpath);
gtk_tree_view_scroll_to_cell(treeview, selpath, NULL, true, 0.5, 0.0);
gtk_tree_path_free(selpath);
}
g_signal_connect (G_OBJECT (selection), "changed",
G_CALLBACK (themelist_selection_changed_cb), NULL);
g_object_unref (G_OBJECT (store));
// ---------------- font
// GtkWidget* fontentry = lookup_widget(g_main_window, "main_fontentry");
// gtk_entry_set_text(GTK_ENTRY(fontentry), get_current_font().c_str());
GtkWidget* fontbutton = lookup_widget(g_main_window, "main_font_selector_button");
gtk_font_button_set_font_name(GTK_FONT_BUTTON(fontbutton), get_current_font().c_str());
}
// ------------------------------------------------------
#ifdef _WIN32
static void redirect_to_file (const gchar* log_domain, GLogLevelFlags log_level,
const gchar* message, gpointer user_data)
{
std::fstream f;
f.open("g_stdout.txt", std::ios::app);
f << message << "\n";
f.close();
}
#endif
// ------------------------------------------------------
int main(int argc, char *argv[])
{
// work around pango weirdness
#ifdef _WIN32
// no longer needed as of pango 1.2.5, but won't do any harm
// putenv("PANGO_WIN32_NO_UNISCRIBE=1");
#endif
std::string user = g_get_user_name();
std::string tmp = g_get_tmp_dir();
std::string tmp_file = tmp + G_DIR_SEPARATOR_S + "gtk_prefs_tmprc_" + user;
s_tmp_file = tmp_file;
gtk_rc_add_default_file(tmp_file.c_str());
gtk_init (&argc, &argv);
// redirect gtk warnings to file when in win32
#if defined _WIN32 && !defined DEBUG
g_log_set_handler ("GLib", GLogLevelFlags(G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
| G_LOG_FLAG_RECURSION), redirect_to_file, NULL);
g_log_set_handler ("GModule", GLogLevelFlags(G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
| G_LOG_FLAG_RECURSION), redirect_to_file, NULL);
g_log_set_handler ("GLib-GObject", GLogLevelFlags(G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
| G_LOG_FLAG_RECURSION), redirect_to_file, NULL);
g_log_set_handler ("GThread", GLogLevelFlags(G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
| G_LOG_FLAG_RECURSION), redirect_to_file, NULL);
g_log_set_handler ("Gtk", GLogLevelFlags(G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
| G_LOG_FLAG_RECURSION), redirect_to_file, NULL);
g_log_set_handler ("Gdk", GLogLevelFlags(G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
| G_LOG_FLAG_RECURSION), redirect_to_file, NULL);
g_log_set_handler ("GdkPixbuf", GLogLevelFlags(G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
| G_LOG_FLAG_RECURSION), redirect_to_file, NULL);
#endif
add_pixmap_directory(".");
g_main_window = create_main_window();
populate_with_themes(lookup_widget(g_main_window, "main_themelist"));
std::string about_text = std::string("GTK+ Preference Tool") + "\n\
\n\
by Alex Shaduri <ashaduri@gmail.com>\n\
\n\
The authors make NO WARRANTY or representation, either express or implied, with respect to this software, its quality, accuracy, merchantability, or fitness for a particular purpose. This software is provided \"AS IS\", and you, its user, assume the entire risk as to its quality and accuracy.\n\
\n\
This is free software, and you are welcome to redistribute it under terms of GNU General Public License v2.";
gtk_label_set_text(GTK_LABEL(lookup_widget(g_main_window, "about_label")), about_text.c_str());
GtkTreeView* treeview = GTK_TREE_VIEW(lookup_widget(g_main_window, "preview_treeview"));
GtkTreeStore *store = gtk_tree_store_new (2, G_TYPE_STRING, G_TYPE_INT);
gtk_tree_view_set_model(treeview, GTK_TREE_MODEL(store));
GtkTreeViewColumn* column1 = gtk_tree_view_column_new_with_attributes ("Text", gtk_cell_renderer_text_new(), "text", 0, NULL);
gtk_tree_view_column_set_sizing(column1, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
gtk_tree_view_column_set_resizable(column1, true);
gtk_tree_view_column_set_reorderable (column1, true);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column1);
GtkTreeViewColumn* column2 = gtk_tree_view_column_new_with_attributes ("Number", gtk_cell_renderer_text_new(), "text", 1, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column2);
GtkTreeIter iter1, iter2;
gtk_tree_store_append (store, &iter1, NULL);
gtk_tree_store_set (store, &iter1, 0, "Parent 2", 1, 66, -1);
gtk_tree_store_append (store, &iter1, NULL);
gtk_tree_store_set (store, &iter1, 0, "Parent 1", 1, 65, -1);
gtk_tree_store_append (store, &iter2, &iter1);
gtk_tree_store_set (store, &iter2, 0, "Child 1", 1, 67, -1);
gtk_tree_view_column_set_sort_column_id(column1, 0);
gtk_tree_view_column_set_sort_order (column1, GTK_SORT_ASCENDING);
gtk_tree_view_column_set_sort_indicator(column1, true);
gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), 0, GTK_SORT_ASCENDING);
g_object_unref (G_OBJECT (store));
get_orig_theme() = get_current_theme();
get_orig_font() = get_current_font();
gtk_widget_show (g_main_window);
gtk_main();
return EXIT_SUCCESS;
}
// -------------------------------
void set_theme(const std::string& theme_name, const std::string& font)
{
// set widgets accordingly
// tree
GtkTreeView* treeview = GTK_TREE_VIEW(lookup_widget(g_main_window, "main_themelist"));
GtkTreeModel* model = gtk_tree_view_get_model(treeview);
GtkTreeSelection* selection = gtk_tree_view_get_selection(treeview);
GtkTreeIter iter;
gtk_tree_model_get_iter_first(model, &iter);
while(gtk_tree_model_iter_next(model, &iter)) {
gchar* text;
gtk_tree_model_get (model, &iter, 0, &text, -1);
std::string theme = gchar_to_string(text);
if (theme_name == theme) {
gtk_tree_selection_select_iter(selection, &iter);
break;
}
}
// font
// GtkWidget* fontentry = lookup_widget(g_main_window, "main_fontentry");
// gtk_entry_set_text(GTK_ENTRY(fontentry), font.c_str());
if (font != "") {
GtkWidget* fontbutton = lookup_widget(g_main_window, "main_font_selector_button");
gtk_font_button_set_font_name(GTK_FONT_BUTTON(fontbutton), get_current_font().c_str());
}
apply_theme(get_selected_theme(), get_selected_font());
}
void apply_theme(const std::string& theme_name, const std::string& font)
{
std::stringstream strstr;
strstr << "gtk-theme-name = \"" << theme_name << "\"\n";
if (font != "")
strstr << "style \"user-font\"\n{\nfont_name=\"" << font << "\"\n}\nwidget_class \"*\" style \"user-font\"";
// std::cout << strstr.str() << "\n\n\n";
std::fstream f;
f.open(s_tmp_file.c_str(), std::ios::out);
f << strstr.str();
f.close();
GtkSettings* settings = gtk_settings_get_default();
gtk_rc_reparse_all_for_settings (settings, true);
// gtk_rc_parse_string(strstr.str().c_str());
// gtk_rc_parse("/root/.gtk-tmp");
// gtk_rc_reset_styles(settings);
unlink(s_tmp_file.c_str());
while (gtk_events_pending())
gtk_main_iteration();
}
#ifdef _WIN32
#ifdef HAVE_DIRECT_H
#include <direct.h>
#define mkdir(a) _mkdir(a)
#else
#define mkdir(a, b) mkdir(a)
#endif
#endif
bool save_current_theme()
{
std::string conf_file = "";
if ((_access( "portable-mode", 0 )) != -1) {
char* themes_dir_c = gtk_rc_get_theme_dir();
char* conf_file_c = g_build_filename("etc", "gtk-2.0", "gtkrc", NULL);
conf_file = (conf_file_c ? conf_file_c : "");
// file doesn't exist, try to get it from gtk.
if (!g_file_test(conf_file.c_str(), G_FILE_TEST_EXISTS)) {
gchar** rc_files = gtk_rc_get_default_files();
if (rc_files[0] != 0) {
conf_file = rc_files[0];
}
}
g_free(themes_dir_c);
g_free(conf_file_c);
// mkdir a directory, only one level deep
char* etc = g_path_get_dirname(conf_file.c_str());
if (!g_file_test(etc, G_FILE_TEST_IS_DIR)) {
mkdir(etc, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
}
g_free(etc);
} else {
conf_file = get_home_dir();
if (conf_file[conf_file.length()-1] != G_DIR_SEPARATOR)
conf_file += G_DIR_SEPARATOR_S;
conf_file += ".gtkrc-2.0";
}
// ask
GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
GtkWidget* dialog = gtk_dialog_new_with_buttons ("Query", GTK_WINDOW(window),
GtkDialogFlags(GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL),
GTK_STOCK_NO, GTK_RESPONSE_REJECT,
GTK_STOCK_YES, GTK_RESPONSE_ACCEPT,
NULL);
gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
GtkWidget* hbox = gtk_hbox_new(1, 1);
gtk_container_set_border_width(GTK_CONTAINER(hbox), 10);
gtk_container_add (GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), hbox);
gtk_container_add (GTK_CONTAINER(hbox),
gtk_label_new((std::string("\n\nThe file \"") + conf_file + "\" will be overwritten.\nAre you sure?\n\n").c_str()));
gtk_widget_show_all (dialog);
bool ret = 0;
gint result = gtk_dialog_run (GTK_DIALOG (dialog));
switch (result) {
case GTK_RESPONSE_ACCEPT:
ret = 1;
break;
}
gtk_widget_destroy(dialog);
gtk_widget_destroy(window);
if (!ret) // the user pressed "No".
return false;
std::string font = get_selected_font();
std::fstream f;
f.open(conf_file.c_str(), std::ios::out);
f << "# Auto-written by gtk2_prefs. Do not edit.\n\n";
f << "gtk-theme-name = \"" << get_selected_theme() << "\"\n";
if (font != "")
f << "style \"user-font\"\n{\n\tfont_name=\"" << font << "\"\n}\nwidget_class \"*\" style \"user-font\"";
f.close();
return true;
}
void program_shutdown()
{
gtk_main_quit();
}
|