Mercurial > geeqie.yaz
annotate src/window.c @ 1171:8721675f80ce
Tidy up.
author | zas_ |
---|---|
date | Sat, 22 Nov 2008 22:10:10 +0000 |
parents | 5fe3b8b3a612 |
children | 0bea79d87065 |
rev | line source |
---|---|
648 | 1 /* |
2 * Geeqie | |
3 * Copyright (C) 2008 The Geeqie Team | |
4 * | |
5 * Authors: Vladimir Nadvornik / Laurent Monin | |
995 | 6 * |
648 | 7 * This software is released under the GNU General Public License (GNU GPL). |
8 * Please read the included file COPYING for more information. | |
9 * This software comes with no warranty of any kind, use at your own risk! | |
10 */ | |
11 | |
12 #include "main.h" | |
13 #include "window.h" | |
14 | |
1144
5fe3b8b3a612
Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents:
1055
diff
changeset
|
15 #include "misc.h" |
648 | 16 #include "pixbuf_util.h" |
17 #include "ui_fileops.h" | |
18 #include "ui_help.h" | |
19 | |
20 GtkWidget *window_new(GtkWindowType type, const gchar *name, const gchar *icon, | |
21 const gchar *icon_file, const gchar *subtitle) | |
22 { | |
23 gchar *title; | |
24 GtkWidget *window; | |
25 | |
26 window = gtk_window_new(type); | |
27 if (!window) return NULL; | |
28 | |
29 if (subtitle) | |
30 { | |
31 title = g_strdup_printf("%s - %s", subtitle, GQ_APPNAME); | |
32 } | |
33 else | |
34 { | |
35 title = g_strdup_printf("%s", GQ_APPNAME); | |
36 } | |
37 | |
38 gtk_window_set_title(GTK_WINDOW(window), title); | |
39 g_free(title); | |
40 | |
41 window_set_icon(window, icon, icon_file); | |
42 gtk_window_set_role(GTK_WINDOW(window), name); | |
43 gtk_window_set_wmclass(GTK_WINDOW(window), name, GQ_WMCLASS); | |
44 | |
45 return window; | |
46 } | |
47 | |
48 void window_set_icon(GtkWidget *window, const gchar *icon, const gchar *file) | |
49 { | |
50 if (!icon && !file) icon = PIXBUF_INLINE_ICON; | |
51 | |
52 if (icon) | |
53 { | |
54 GdkPixbuf *pixbuf; | |
55 | |
56 pixbuf = pixbuf_inline(icon); | |
57 if (pixbuf) | |
58 { | |
59 gtk_window_set_icon(GTK_WINDOW(window), pixbuf); | |
60 g_object_unref(pixbuf); | |
61 } | |
62 } | |
63 else | |
64 { | |
65 gtk_window_set_icon_from_file(GTK_WINDOW(window), file, NULL); | |
66 } | |
67 } | |
68 | |
69 gint window_maximized(GtkWidget *window) | |
70 { | |
71 GdkWindowState state; | |
72 | |
73 if (!window || !window->window) return FALSE; | |
74 | |
75 state = gdk_window_get_state(window->window); | |
76 return (state & GDK_WINDOW_STATE_MAXIMIZED); | |
77 } | |
78 | |
79 /* | |
80 *----------------------------------------------------------------------------- | |
884
ff16ed0d2c8a
Improve ways to specify html browser (used for help, see bug 2015099).
zas_
parents:
883
diff
changeset
|
81 * Open browser with the help Documentation |
648 | 82 *----------------------------------------------------------------------------- |
83 */ | |
84 | |
85 static gchar *command_result(const gchar *binary, const gchar *command) | |
86 { | |
87 gchar *result = NULL; | |
88 FILE *f; | |
884
ff16ed0d2c8a
Improve ways to specify html browser (used for help, see bug 2015099).
zas_
parents:
883
diff
changeset
|
89 gchar buf[2048]; |
ff16ed0d2c8a
Improve ways to specify html browser (used for help, see bug 2015099).
zas_
parents:
883
diff
changeset
|
90 gint l; |
648 | 91 |
884
ff16ed0d2c8a
Improve ways to specify html browser (used for help, see bug 2015099).
zas_
parents:
883
diff
changeset
|
92 if (!binary || binary[0] == '\0') return NULL; |
648 | 93 if (!file_in_path(binary)) return NULL; |
94 | |
884
ff16ed0d2c8a
Improve ways to specify html browser (used for help, see bug 2015099).
zas_
parents:
883
diff
changeset
|
95 if (!command || command[0] == '\0') return g_strdup(binary); |
648 | 96 if (command[0] == '!') return g_strdup(command + 1); |
97 | |
98 f = popen(command, "r"); | |
99 if (!f) return NULL; | |
100 | |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
995
diff
changeset
|
101 while ((l = fread(buf, sizeof(gchar), sizeof(buf), f)) > 0) |
648 | 102 { |
103 if (!result) | |
104 { | |
884
ff16ed0d2c8a
Improve ways to specify html browser (used for help, see bug 2015099).
zas_
parents:
883
diff
changeset
|
105 gint n = 0; |
648 | 106 |
107 while (n < l && buf[n] != '\n' && buf[n] != '\r') n++; | |
108 if (n > 0) result = g_strndup(buf, n); | |
109 } | |
110 } | |
111 | |
112 pclose(f); | |
113 | |
114 return result; | |
115 } | |
116 | |
1144
5fe3b8b3a612
Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents:
1055
diff
changeset
|
117 static int help_browser_command(const gchar *command, const gchar *path) |
648 | 118 { |
119 gchar *result; | |
120 gchar *buf; | |
121 gchar *begin; | |
122 gchar *end; | |
1144
5fe3b8b3a612
Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents:
1055
diff
changeset
|
123 int retval = -1; |
648 | 124 |
1144
5fe3b8b3a612
Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents:
1055
diff
changeset
|
125 if (!command || !path) return retval; |
648 | 126 |
127 DEBUG_1("Help command pre \"%s\", \"%s\"", command, path); | |
128 | |
129 buf = g_strdup(command); | |
130 begin = strstr(buf, "%s"); | |
131 if (begin) | |
132 { | |
133 *begin = '\0'; | |
134 end = begin + 2; | |
135 begin = buf; | |
136 | |
137 result = g_strdup_printf("%s%s%s &", begin, path, end); | |
138 } | |
139 else | |
140 { | |
141 result = g_strdup_printf("%s \"%s\" &", command, path); | |
142 } | |
143 g_free(buf); | |
144 | |
145 DEBUG_1("Help command post [%s]", result); | |
146 | |
1144
5fe3b8b3a612
Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents:
1055
diff
changeset
|
147 retval = runcmd(result); |
5fe3b8b3a612
Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents:
1055
diff
changeset
|
148 DEBUG_1("Help command exit code: %d", retval); |
648 | 149 |
150 g_free(result); | |
1144
5fe3b8b3a612
Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents:
1055
diff
changeset
|
151 return retval; |
648 | 152 } |
153 | |
154 /* | |
155 * each set of 2 strings is one browser: | |
156 * the 1st is the binary to look for in the path | |
157 * the 2nd has 3 capabilities: | |
158 * NULL exec binary with html file path as command line | |
159 * string exec string and use results for command line | |
160 * !string use text following ! as command line, replacing optional %s with html file path | |
161 */ | |
162 static gchar *html_browsers[] = | |
163 { | |
884
ff16ed0d2c8a
Improve ways to specify html browser (used for help, see bug 2015099).
zas_
parents:
883
diff
changeset
|
164 /* Our specific script */ |
ff16ed0d2c8a
Improve ways to specify html browser (used for help, see bug 2015099).
zas_
parents:
883
diff
changeset
|
165 GQ_APPNAME_LC "_html_browser", NULL, |
648 | 166 /* Redhat has a nifty htmlview script to start the user's preferred browser */ |
167 "htmlview", NULL, | |
883
391a9e3336db
Apply debian-specific patch to launch help browser (bug 2015099).
zas_
parents:
728
diff
changeset
|
168 /* Debian has even better approach with alternatives */ |
391a9e3336db
Apply debian-specific patch to launch help browser (bug 2015099).
zas_
parents:
728
diff
changeset
|
169 "sensible-browser", NULL, |
648 | 170 /* GNOME 2 */ |
171 "gconftool-2", "gconftool-2 -g /desktop/gnome/url-handlers/http/command", | |
172 /* KDE */ | |
173 "kfmclient", "!kfmclient exec \"%s\"", | |
174 /* use fallbacks */ | |
175 "firefox", NULL, | |
176 "mozilla", NULL, | |
177 "konqueror", NULL, | |
178 "netscape", NULL, | |
179 NULL, NULL | |
180 }; | |
181 | |
182 static void help_browser_run(void) | |
183 { | |
1144
5fe3b8b3a612
Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents:
1055
diff
changeset
|
184 gchar *name = options->helpers.html_browser.command_name; |
5fe3b8b3a612
Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents:
1055
diff
changeset
|
185 gchar *cmd = options->helpers.html_browser.command_line; |
5fe3b8b3a612
Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents:
1055
diff
changeset
|
186 gchar *path = g_build_filename(options->documentation.htmldir, "index.html", NULL); |
5fe3b8b3a612
Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents:
1055
diff
changeset
|
187 gchar *result = NULL; |
648 | 188 gint i; |
189 | |
1144
5fe3b8b3a612
Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents:
1055
diff
changeset
|
190 i = 0; |
5fe3b8b3a612
Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents:
1055
diff
changeset
|
191 while (!result) |
648 | 192 { |
1144
5fe3b8b3a612
Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents:
1055
diff
changeset
|
193 if ((name && *name) || (cmd && *cmd)) { |
5fe3b8b3a612
Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents:
1055
diff
changeset
|
194 DEBUG_1("Trying browser: name=%s command=%s", name, cmd); |
5fe3b8b3a612
Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents:
1055
diff
changeset
|
195 result = command_result(name, cmd); |
5fe3b8b3a612
Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents:
1055
diff
changeset
|
196 DEBUG_1("Result: %s", result); |
5fe3b8b3a612
Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents:
1055
diff
changeset
|
197 if (result) |
5fe3b8b3a612
Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents:
1055
diff
changeset
|
198 { |
5fe3b8b3a612
Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents:
1055
diff
changeset
|
199 int ret = help_browser_command(result, path); |
5fe3b8b3a612
Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents:
1055
diff
changeset
|
200 |
5fe3b8b3a612
Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents:
1055
diff
changeset
|
201 if (ret == 0) break; |
5fe3b8b3a612
Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents:
1055
diff
changeset
|
202 g_free(result); |
5fe3b8b3a612
Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents:
1055
diff
changeset
|
203 result = NULL; |
5fe3b8b3a612
Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents:
1055
diff
changeset
|
204 } |
5fe3b8b3a612
Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents:
1055
diff
changeset
|
205 } |
5fe3b8b3a612
Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents:
1055
diff
changeset
|
206 if (!html_browsers[i]) break; |
5fe3b8b3a612
Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents:
1055
diff
changeset
|
207 name = html_browsers[i++]; |
5fe3b8b3a612
Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents:
1055
diff
changeset
|
208 cmd = html_browsers[i++]; |
648 | 209 } |
210 | |
211 if (!result) | |
212 { | |
673
fbebf5cf4a55
Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents:
671
diff
changeset
|
213 log_printf("Unable to detect an installed browser.\n"); |
648 | 214 return; |
215 } | |
216 | |
1013
88ebc61e33ae
Allow to override documentation paths through options:
zas_
parents:
1000
diff
changeset
|
217 g_free(path); |
648 | 218 g_free(result); |
219 } | |
220 | |
221 /* | |
222 *----------------------------------------------------------------------------- | |
223 * help window | |
224 *----------------------------------------------------------------------------- | |
225 */ | |
226 | |
227 static GtkWidget *help_window = NULL; | |
228 | |
229 static void help_window_destroy_cb(GtkWidget *window, gpointer data) | |
230 { | |
231 help_window = NULL; | |
232 } | |
233 | |
234 void help_window_show(const gchar *key) | |
235 { | |
1013
88ebc61e33ae
Allow to override documentation paths through options:
zas_
parents:
1000
diff
changeset
|
236 gchar *path; |
88ebc61e33ae
Allow to override documentation paths through options:
zas_
parents:
1000
diff
changeset
|
237 |
648 | 238 if (key && strcmp(key, "html_contents") == 0) |
239 { | |
240 help_browser_run(); | |
241 return; | |
242 } | |
243 | |
244 if (help_window) | |
245 { | |
246 gtk_window_present(GTK_WINDOW(help_window)); | |
247 if (key) help_window_set_key(help_window, key); | |
248 return; | |
249 } | |
250 | |
1013
88ebc61e33ae
Allow to override documentation paths through options:
zas_
parents:
1000
diff
changeset
|
251 path = g_build_filename(options->documentation.helpdir, "README", NULL); |
88ebc61e33ae
Allow to override documentation paths through options:
zas_
parents:
1000
diff
changeset
|
252 help_window = help_window_new(_("Help"), GQ_WMCLASS, "help", path, key); |
88ebc61e33ae
Allow to override documentation paths through options:
zas_
parents:
1000
diff
changeset
|
253 g_free(path); |
648 | 254 |
255 g_signal_connect(G_OBJECT(help_window), "destroy", | |
256 G_CALLBACK(help_window_destroy_cb), NULL); | |
257 } | |
258 | |
1055
1646720364cf
Adding a vim modeline to all files - patch by Klaus Ethgen
nadvornik
parents:
1013
diff
changeset
|
259 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */ |