]> icculus.org git repositories - dana/obconf.git/blob - src/main.c
add theme previews! yay syscrash!
[dana/obconf.git] / src / main.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    main.c for ObConf, the configuration tool for Openbox
4    Copyright (c) 2003-2007   Dana Jansens
5    Copyright (c) 2003        Tim Riley
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "main.h"
21 #include "handlers.h"
22 #include "theme.h"
23 #include "gettext.h"
24
25 #include <gdk/gdkx.h>
26 #define SN_API_NOT_YET_FROZEN
27 #include <libsn/sn.h>
28 #undef SN_API_NOT_YET_FROZEN
29 #include <stdlib.h>
30
31 GtkWidget *mainwin = NULL;
32
33 GladeXML *glade;
34 xmlDocPtr doc;
35 xmlNodePtr root;
36 RrInstance *rrinst;
37
38 static gchar *obc_theme_install = NULL;
39 static gchar *obc_theme_archive = NULL;
40
41 void obconf_error(gchar *msg)
42 {
43     GtkWidget *d;
44
45     d = gtk_message_dialog_new(mainwin ? GTK_WINDOW(mainwin) : NULL,
46                                GTK_DIALOG_DESTROY_WITH_PARENT,
47                                GTK_MESSAGE_ERROR,
48                                GTK_BUTTONS_CLOSE,
49                                "%s", msg);
50     g_signal_connect_swapped(GTK_OBJECT(d), "response",
51                              G_CALLBACK(gtk_widget_destroy),
52                              GTK_OBJECT(d));
53     gtk_widget_show(d);
54 }
55
56 static void print_version()
57 {
58     g_print("ObConf %s\n", PACKAGE_VERSION);
59     g_print(_("Copyright (c)"));
60     g_print(" 2003-2007   Dana Jansens\n");
61     g_print(_("Copyright (c)"));
62     g_print(" 2003        Tim Riley\n\n");
63     g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
64     g_print("This is free software, and you are welcome to redistribute it\n");
65     g_print("under certain conditions. See the file COPYING for details.\n\n");
66
67     exit(EXIT_SUCCESS);
68 }
69
70 static void print_help()
71 {
72     g_print(_("Syntax: obconf [options] [ARCHIVE.obt]\n"));
73     g_print(_("\nOptions:\n"));
74     g_print(_("  --help                Display this help and exit\n"));
75     g_print(_("  --version             Display the version and exit\n"));
76     g_print(_("  --install ARCHIVE.obt Install the given theme archive and select it\n"));
77     g_print(_("  --archive THEME       Create a theme archive from the given theme directory\n"));
78     g_print(_("\nPlease report bugs at %s\n\n"), PACKAGE_BUGREPORT);
79     
80     exit(EXIT_SUCCESS);
81 }
82
83 static void parse_args(int argc, char **argv)
84 {
85     int i;
86
87     for (i = 1; i < argc; ++i) {
88         if (!strcmp(argv[i], "--help"))
89             print_help();
90         if (!strcmp(argv[i], "--version"))
91             print_version();
92         else if (!strcmp(argv[i], "--install")) {
93             if (i == argc - 1) /* no args left */
94                 g_printerr(_("--install requires an argument\n"));
95             else
96                 obc_theme_install = argv[++i];
97         }
98         else if (!strcmp(argv[i], "--archive")) {
99             if (i == argc - 1) /* no args left */
100                 g_printerr(_("--archive requires an argument\n"));
101             else
102                 obc_theme_archive = argv[++i];
103         } else
104             obc_theme_install = argv[i];
105     }
106 }
107
108 int main(int argc, char **argv)
109 {
110     SnDisplay *sn_d;
111     SnLauncheeContext *sn_cx;
112     gchar *p;
113
114     gtk_init(&argc, &argv);
115     parse_args(argc, argv);
116
117     if (obc_theme_archive) {
118         theme_archive(obc_theme_archive);
119         return;
120     }
121
122     p = g_build_filename(GLADEDIR, "obconf.glade", NULL);
123     glade = glade_xml_new(p, NULL, NULL);
124     g_free(p);
125
126     if (!glade) {
127         obconf_error("Failed to load the obconf.glade interface file. You "
128                      "have probably failed to install ObConf properly.");
129         return 1;
130     }
131
132     parse_paths_startup();
133     rrinst = RrInstanceNew(GDK_DISPLAY(), gdk_x11_get_default_screen());
134
135     xmlIndentTreeOutput = 1;
136     if (!parse_load_rc(NULL, &doc, &root)) {
137         obconf_error("Failed to load an rc.xml. You have probably failed to "
138                      "install Openbox properly.");
139         return 1;
140     }
141
142     glade_xml_signal_autoconnect(glade);
143
144     {
145         gchar *s = g_strdup_printf
146             ("<span weight=\"bold\" size=\"xx-large\">ObConf %s</span>",
147              PACKAGE_VERSION);
148         gtk_label_set_markup(GTK_LABEL
149                              (glade_xml_get_widget(glade, "title_label")), s);
150         g_free(s);
151     }
152
153     setup_behavior_tab();
154     setup_dock_tab();
155     setup_focus_mouse(glade_xml_get_widget(glade, "focus_mouse"));
156     setup_focus_raise(glade_xml_get_widget(glade, "focus_raise"));
157     setup_focus_last(glade_xml_get_widget(glade, "focus_raise"));
158     setup_focus_delay(glade_xml_get_widget(glade, "focus_delay"));
159     setup_focus_new(glade_xml_get_widget(glade, "focus_new"));
160     setup_place_mouse(glade_xml_get_widget(glade, "place_mouse"));
161     setup_resist_window(glade_xml_get_widget(glade, "resist_window"));
162     setup_resist_edge(glade_xml_get_widget(glade, "resist_edge"));
163     setup_resize_contents(glade_xml_get_widget(glade, "resize_contents"));
164     setup_dock_position(glade_xml_get_widget(glade, "dock_position"));
165     setup_dock_float_x(glade_xml_get_widget(glade, "dock_float_x"));
166     setup_dock_float_y(glade_xml_get_widget(glade, "dock_float_y"));
167     setup_dock_stacking(glade_xml_get_widget(glade, "dock_stack_top"),
168                         glade_xml_get_widget(glade, "dock_stack_normal"),
169                         glade_xml_get_widget(glade, "dock_stack_bottom"));
170     setup_dock_direction(glade_xml_get_widget(glade, "dock_direction"));
171     setup_dock_hide(glade_xml_get_widget(glade, "dock_hide"));
172     setup_dock_hide_delay(glade_xml_get_widget(glade, "dock_hide_delay"));
173     setup_theme_names(glade_xml_get_widget(glade, "theme_names"));
174     setup_title_layout(glade_xml_get_widget(glade, "title_layout"));
175     setup_desktop_num(glade_xml_get_widget(glade, "desktop_num"));
176     setup_desktop_names(glade_xml_get_widget(glade, "desktop_names"));
177     setup_window_border(glade_xml_get_widget(glade, "window_border"));
178     setup_font_active(glade_xml_get_widget(glade, "font_active"));
179     setup_font_inactive(glade_xml_get_widget(glade, "font_inactive"));
180     setup_font_menu_header(glade_xml_get_widget(glade, "font_menu_header"));
181     setup_font_menu_item(glade_xml_get_widget(glade, "font_menu_item"));
182     setup_font_display(glade_xml_get_widget(glade, "font_display"));
183
184     mainwin = glade_xml_get_widget(glade, "main_window");
185
186     sn_d = sn_display_new(GDK_DISPLAY_XDISPLAY(gdk_display_get_default()),
187                           NULL, NULL);
188
189     sn_cx = sn_launchee_context_new_from_environment
190         (sn_d, gdk_screen_get_number(gdk_display_get_default_screen
191                                      (gdk_display_get_default())));
192
193     if (sn_cx)
194         sn_launchee_context_setup_window
195             (sn_cx, GDK_WINDOW_XWINDOW(GDK_WINDOW(mainwin->window)));
196
197     gtk_widget_show_all(mainwin);
198
199     if (sn_cx)
200         sn_launchee_context_complete(sn_cx);
201
202     if (sn_cx)
203         sn_launchee_context_unref(sn_cx);
204     sn_display_unref(sn_d);
205
206     if (obc_theme_install)
207         handlers_install_theme(obc_theme_install);
208
209     gtk_main();
210
211     RrInstanceFree(rrinst);
212     parse_paths_shutdown();
213
214     xmlFreeDoc(doc);
215     return 0;
216 }