comparison src/main.c @ 648:e34c1002e553

Move some functions from main.[ch] to new window.[ch].
author zas_
date Tue, 13 May 2008 08:02:46 +0000
parents b50deb0f9968
children 3097880d7d95
comparison
equal deleted inserted replaced
647:42d36db921bc 648:e34c1002e553
37 #include "ui_bookmark.h" 37 #include "ui_bookmark.h"
38 #include "ui_help.h" 38 #include "ui_help.h"
39 #include "ui_fileops.h" 39 #include "ui_fileops.h"
40 #include "ui_tabcomp.h" 40 #include "ui_tabcomp.h"
41 #include "ui_utildlg.h" 41 #include "ui_utildlg.h"
42 #include "window.h"
42 43
43 #include <gdk/gdkkeysyms.h> /* for keyboard values */ 44 #include <gdk/gdkkeysyms.h> /* for keyboard values */
44 45
45 46
46 #include <math.h> 47 #include <math.h>
54 *----------------------------------------------------------------------------- 55 *-----------------------------------------------------------------------------
55 * misc (public) 56 * misc (public)
56 *----------------------------------------------------------------------------- 57 *-----------------------------------------------------------------------------
57 */ 58 */
58 59
59 GtkWidget *window_new(GtkWindowType type, const gchar *name, const gchar *icon,
60 const gchar *icon_file, const gchar *subtitle)
61 {
62 gchar *title;
63 GtkWidget *window;
64
65 window = gtk_window_new(type);
66 if (!window) return NULL;
67
68 if (subtitle)
69 {
70 title = g_strdup_printf("%s - %s", subtitle, GQ_APPNAME);
71 }
72 else
73 {
74 title = g_strdup_printf("%s", GQ_APPNAME);
75 }
76
77 gtk_window_set_title(GTK_WINDOW(window), title);
78 g_free(title);
79
80 window_set_icon(window, icon, icon_file);
81 gtk_window_set_role(GTK_WINDOW(window), name);
82 gtk_window_set_wmclass(GTK_WINDOW(window), name, GQ_WMCLASS);
83
84 return window;
85 }
86
87 void window_set_icon(GtkWidget *window, const gchar *icon, const gchar *file)
88 {
89 if (!icon && !file) icon = PIXBUF_INLINE_ICON;
90
91 if (icon)
92 {
93 GdkPixbuf *pixbuf;
94
95 pixbuf = pixbuf_inline(icon);
96 if (pixbuf)
97 {
98 gtk_window_set_icon(GTK_WINDOW(window), pixbuf);
99 g_object_unref(pixbuf);
100 }
101 }
102 else
103 {
104 gtk_window_set_icon_from_file(GTK_WINDOW(window), file, NULL);
105 }
106 }
107
108 gint window_maximized(GtkWidget *window)
109 {
110 GdkWindowState state;
111
112 if (!window || !window->window) return FALSE;
113
114 state = gdk_window_get_state(window->window);
115 return (state & GDK_WINDOW_STATE_MAXIMIZED);
116 }
117 60
118 gdouble get_zoom_increment(void) 61 gdouble get_zoom_increment(void)
119 { 62 {
120 return ((options->image.zoom_increment != 0) ? (gdouble)options->image.zoom_increment / 10.0 : 1.0); 63 return ((options->image.zoom_increment != 0) ? (gdouble)options->image.zoom_increment / 10.0 : 1.0);
121 } 64 }
136 text = conv_text; 79 text = conv_text;
137 } 80 }
138 81
139 return text; 82 return text;
140 } 83 }
141
142
143 /*
144 *-----------------------------------------------------------------------------
145 * Open browser with the help Documentation
146 *-----------------------------------------------------------------------------
147 */
148
149 static gchar *command_result(const gchar *binary, const gchar *command)
150 {
151 gchar *result = NULL;
152 FILE *f;
153 char buf[2048];
154 int l;
155
156 if (!binary) return NULL;
157 if (!file_in_path(binary)) return NULL;
158
159 if (!command) return g_strdup(binary);
160 if (command[0] == '!') return g_strdup(command + 1);
161
162 f = popen(command, "r");
163 if (!f) return NULL;
164
165 while ((l = fread(buf, sizeof(char), sizeof(buf), f)) > 0)
166 {
167 if (!result)
168 {
169 int n = 0;
170
171 while (n < l && buf[n] != '\n' && buf[n] != '\r') n++;
172 if (n > 0) result = g_strndup(buf, n);
173 }
174 }
175
176 pclose(f);
177
178 return result;
179 }
180
181 static void help_browser_command(const gchar *command, const gchar *path)
182 {
183 gchar *result;
184 gchar *buf;
185 gchar *begin;
186 gchar *end;
187
188 if (!command || !path) return;
189
190 DEBUG_1("Help command pre \"%s\", \"%s\"", command, path);
191
192 buf = g_strdup(command);
193 begin = strstr(buf, "%s");
194 if (begin)
195 {
196 *begin = '\0';
197 end = begin + 2;
198 begin = buf;
199
200 result = g_strdup_printf("%s%s%s &", begin, path, end);
201 }
202 else
203 {
204 result = g_strdup_printf("%s \"%s\" &", command, path);
205 }
206 g_free(buf);
207
208 DEBUG_1("Help command post [%s]", result);
209
210 system(result);
211
212 g_free(result);
213 }
214
215 /*
216 * each set of 2 strings is one browser:
217 * the 1st is the binary to look for in the path
218 * the 2nd has 3 capabilities:
219 * NULL exec binary with html file path as command line
220 * string exec string and use results for command line
221 * !string use text following ! as command line, replacing optional %s with html file path
222 */
223 static gchar *html_browsers[] =
224 {
225 /* Redhat has a nifty htmlview script to start the user's preferred browser */
226 "htmlview", NULL,
227 /* GNOME 2 */
228 "gconftool-2", "gconftool-2 -g /desktop/gnome/url-handlers/http/command",
229 /* KDE */
230 "kfmclient", "!kfmclient exec \"%s\"",
231 /* use fallbacks */
232 "firefox", NULL,
233 "mozilla", NULL,
234 "konqueror", NULL,
235 "netscape", NULL,
236 NULL, NULL
237 };
238
239 static void help_browser_run(void)
240 {
241 gchar *result = NULL;
242 gint i;
243
244 i = 0;
245 while (!result && html_browsers[i])
246 {
247 result = command_result(html_browsers[i], html_browsers[i+1]);
248 i += 2;
249 }
250
251 if (!result)
252 {
253 printf("Unable to detect an installed browser.\n");
254 return;
255 }
256
257 help_browser_command(result, GQ_HTMLDIR "/index.html");
258
259 g_free(result);
260 }
261
262 /*
263 *-----------------------------------------------------------------------------
264 * help window
265 *-----------------------------------------------------------------------------
266 */
267
268 static GtkWidget *help_window = NULL;
269
270 static void help_window_destroy_cb(GtkWidget *window, gpointer data)
271 {
272 help_window = NULL;
273 }
274
275 void help_window_show(const gchar *key)
276 {
277 if (key && strcmp(key, "html_contents") == 0)
278 {
279 help_browser_run();
280 return;
281 }
282
283 if (help_window)
284 {
285 gtk_window_present(GTK_WINDOW(help_window));
286 if (key) help_window_set_key(help_window, key);
287 return;
288 }
289
290 help_window = help_window_new(_("Help"), GQ_WMCLASS, "help",
291 GQ_HELPDIR "/README", key);
292
293 g_signal_connect(G_OBJECT(help_window), "destroy",
294 G_CALLBACK(help_window_destroy_cb), NULL);
295 }
296
297 84
298 /* 85 /*
299 *----------------------------------------------------------------------------- 86 *-----------------------------------------------------------------------------
300 * keyboard functions 87 * keyboard functions
301 *----------------------------------------------------------------------------- 88 *-----------------------------------------------------------------------------