]> icculus.org git repositories - dana/obconf.git/blob - src/main.c
more auto scrollbars
[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 "archive.h"
22 #include "theme.h"
23 #include "appearance.h"
24 #include "behavior.h"
25 #include "desktops.h"
26 #include "dock.h"
27 #include "preview_update.h"
28 #include "gettext.h"
29
30 #include <gdk/gdkx.h>
31 #define SN_API_NOT_YET_FROZEN
32 #include <libsn/sn.h>
33 #undef SN_API_NOT_YET_FROZEN
34 #include <stdlib.h>
35
36 GtkWidget *mainwin = NULL;
37
38 GladeXML *glade;
39 xmlDocPtr doc;
40 xmlNodePtr root;
41 RrInstance *rrinst;
42
43 static gchar *obc_theme_install = NULL;
44 static gchar *obc_theme_archive = NULL;
45
46 void obconf_error(gchar *msg)
47 {
48     GtkWidget *d;
49
50     d = gtk_message_dialog_new(mainwin ? GTK_WINDOW(mainwin) : NULL,
51                                GTK_DIALOG_DESTROY_WITH_PARENT,
52                                GTK_MESSAGE_ERROR,
53                                GTK_BUTTONS_CLOSE,
54                                "%s", msg);
55     g_signal_connect_swapped(GTK_OBJECT(d), "response",
56                              G_CALLBACK(gtk_widget_destroy),
57                              GTK_OBJECT(d));
58     gtk_widget_show(d);
59 }
60
61 static void print_version()
62 {
63     g_print("ObConf %s\n", PACKAGE_VERSION);
64     g_print(_("Copyright (c)"));
65     g_print(" 2003-2007   Dana Jansens\n");
66     g_print(_("Copyright (c)"));
67     g_print(" 2003        Tim Riley\n\n");
68     g_print(" 2007        Javeed Shaikh\n\n");
69     g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
70     g_print("This is free software, and you are welcome to redistribute it\n");
71     g_print("under certain conditions. See the file COPYING for details.\n\n");
72
73     exit(EXIT_SUCCESS);
74 }
75
76 static void print_help()
77 {
78     g_print(_("Syntax: obconf [options] [ARCHIVE.obt]\n"));
79     g_print(_("\nOptions:\n"));
80     g_print(_("  --help                Display this help and exit\n"));
81     g_print(_("  --version             Display the version and exit\n"));
82     g_print(_("  --install ARCHIVE.obt Install the given theme archive and select it\n"));
83     g_print(_("  --archive THEME       Create a theme archive from the given theme directory\n"));
84     g_print(_("\nPlease report bugs at %s\n\n"), PACKAGE_BUGREPORT);
85     
86     exit(EXIT_SUCCESS);
87 }
88
89 static void parse_args(int argc, char **argv)
90 {
91     int i;
92
93     for (i = 1; i < argc; ++i) {
94         if (!strcmp(argv[i], "--help"))
95             print_help();
96         if (!strcmp(argv[i], "--version"))
97             print_version();
98         else if (!strcmp(argv[i], "--install")) {
99             if (i == argc - 1) /* no args left */
100                 g_printerr(_("--install requires an argument\n"));
101             else
102                 obc_theme_install = argv[++i];
103         }
104         else if (!strcmp(argv[i], "--archive")) {
105             if (i == argc - 1) /* no args left */
106                 g_printerr(_("--archive requires an argument\n"));
107             else
108                 obc_theme_archive = argv[++i];
109         } else
110             obc_theme_install = argv[i];
111     }
112 }
113
114 int main(int argc, char **argv)
115 {
116     gchar *p;
117
118     gtk_init(&argc, &argv);
119     parse_args(argc, argv);
120
121     if (obc_theme_archive) {
122         archive_create(obc_theme_archive);
123         return;
124     }
125
126     p = g_build_filename(GLADEDIR, "obconf.glade", NULL);
127     glade = glade_xml_new(p, NULL, NULL);
128     g_free(p);
129
130     if (!glade) {
131         obconf_error("Failed to load the obconf.glade interface file. You "
132                      "have probably failed to install ObConf properly.");
133         return 1;
134     }
135
136     parse_paths_startup();
137     rrinst = RrInstanceNew(GDK_DISPLAY(), gdk_x11_get_default_screen());
138
139     xmlIndentTreeOutput = 1;
140     if (!parse_load_rc(NULL, &doc, &root)) {
141         obconf_error("Failed to load an rc.xml. You have probably failed to "
142                      "install Openbox properly.");
143         return 1;
144     }
145
146     glade_xml_signal_autoconnect(glade);
147
148     {
149         gchar *s = g_strdup_printf
150             ("<span weight=\"bold\" size=\"xx-large\">ObConf %s</span>",
151              PACKAGE_VERSION);
152         gtk_label_set_markup(GTK_LABEL
153                              (glade_xml_get_widget(glade, "title_label")), s);
154         g_free(s);
155     }
156
157     theme_setup_tab();
158     appearance_setup_tab();
159     behavior_setup_tab();
160     desktops_setup_tab();
161     dock_setup_tab();
162     keybindings_setup_tab();
163
164     mainwin = get_widget("main_window");
165
166     if (obc_theme_install)
167         theme_install(obc_theme_install);
168     theme_load_all();
169
170     /* the main window is not shown here ! it is shown when the theme previews
171        are completed */
172     gtk_main();
173
174     preview_update_set_active_font(NULL);
175     preview_update_set_inactive_font(NULL);
176     preview_update_set_menu_header_font(NULL);
177     preview_update_set_menu_item_font(NULL);
178     preview_update_set_osd_font(NULL);
179     preview_update_set_title_layout(NULL);
180
181     RrInstanceFree(rrinst);
182     parse_paths_shutdown();
183
184     xmlFreeDoc(doc);
185     return 0;
186 }
187
188 gboolean on_main_window_delete_event(GtkWidget *w, GdkEvent *e, gpointer d)
189 {
190     gtk_main_quit();
191     return FALSE;
192 }
193
194 void on_close_clicked()
195 {
196     gtk_main_quit();
197 }
198
199 void obconf_show_main()
200 {
201     SnDisplay *sn_d;
202     SnLauncheeContext *sn_cx;
203
204     if (GTK_WIDGET_VISIBLE(mainwin)) return;
205
206     gtk_widget_show_all(mainwin);
207
208     sn_d = sn_display_new(GDK_DISPLAY_XDISPLAY(gdk_display_get_default()),
209                           NULL, NULL);
210
211     sn_cx = sn_launchee_context_new_from_environment
212         (sn_d, gdk_screen_get_number(gdk_display_get_default_screen
213                                      (gdk_display_get_default())));
214
215     if (sn_cx)
216         sn_launchee_context_setup_window
217             (sn_cx, GDK_WINDOW_XWINDOW(GDK_WINDOW(mainwin->window)));
218
219     if (sn_cx)
220         sn_launchee_context_complete(sn_cx);
221
222     if (sn_cx)
223         sn_launchee_context_unref(sn_cx);
224     sn_display_unref(sn_d);
225 }