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
|
#include <gtk/gtk.h>
#include <glade/glade.h>
#include <glib/gprintf.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "siv.h"
#define _(a) a
struct App
{
int n_windows;
GHashTable *meta_data;
};
void
app_show_warning (GtkWidget *parent_window,
const gchar *format,
...)
{
va_list args;
char *message;
GtkWidget *dialog;
va_start (args, format);
g_vasprintf (&message, format, args);
va_end (args);
dialog = gtk_message_dialog_new (parent_window ? GTK_WINDOW (parent_window) : NULL,
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_WARNING,
GTK_BUTTONS_OK, message);
g_free (message);
gtk_window_set_title (GTK_WINDOW (dialog), APPLICATION_NAME " Warning");
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
}
void
app_register_window (App *app, Window *window)
{
++app->n_windows;
}
void
app_unregister_window (App *app, Window *window)
{
if (--app->n_windows == 0)
gtk_main_quit ();
}
MetaData *
app_get_meta_data (App *app,
const char *file)
{
MetaData *data = g_hash_table_lookup (app->meta_data, file);
return data;
}
static gchar *
make_filename (void)
{
return g_build_filename (
g_get_home_dir(), ".gnome2", "siv.metadata", NULL);
}
static void
foreach (gpointer key, gpointer value, gpointer user_data)
{
gchar *filename = key;
MetaData *meta = value;
GKeyFile *key_file = user_data;
g_key_file_set_integer (key_file, filename, "window_x", meta->window_x);
g_key_file_set_integer (key_file, filename, "window_y", meta->window_y);
g_key_file_set_integer (key_file, filename, "window_width", meta->window_width);
g_key_file_set_integer (key_file, filename, "window_height", meta->window_height);
g_key_file_set_integer (key_file, filename, "background", meta->background);
g_key_file_set_integer (key_file, filename, "smooth_image", meta->smooth_image);
g_key_file_set_integer (key_file, filename, "zoom_level", meta->zoom_level);
g_key_file_set_integer (key_file, filename, "hadj", meta->hadj);
g_key_file_set_integer (key_file, filename, "vadj", meta->vadj);
}
void
app_set_meta_data (App *app,
const char *filename,
int window_x,
int window_y,
int window_width,
int window_height,
gboolean smooth_image,
BackgroundType background,
int zoom_level,
int vadj,
int hadj)
{
GKeyFile *keyfile = g_key_file_new ();
MetaData *data = g_hash_table_lookup (app->meta_data, filename);
char *key_filename = make_filename();
char *output;
if (!data)
{
data = g_new0 (MetaData, 1);
g_hash_table_insert (app->meta_data, g_strdup (filename), data);
}
data->window_x = window_x;
data->window_y = window_y;
data->window_width = window_width;
data->window_height = window_height;
data->background = background;
data->smooth_image = smooth_image;
data->zoom_level = zoom_level;
data->hadj = hadj;
data->vadj = vadj;
g_hash_table_foreach (app->meta_data, foreach, keyfile);
output = g_key_file_to_data (keyfile, NULL, NULL);
if (output)
{
g_file_set_contents (key_filename, output, -1, NULL);
g_free (output);
}
g_key_file_free (keyfile);
g_free (key_filename);
}
static gboolean
get_int (GKeyFile *keyfile, const char *group, const char *key, int *result)
{
GError *err = NULL;
int d;
d = g_key_file_get_integer (keyfile, group, key, &err);
if (err)
{
g_error_free (err);
return FALSE;
}
if (result)
*result = d;
return TRUE;
}
static GHashTable *
load_meta_data (void)
{
GKeyFile *keyfile;
char *filename;
GHashTable *result;
keyfile = g_key_file_new ();
filename = make_filename();
result = g_hash_table_new (g_str_hash, g_str_equal);
if (g_key_file_load_from_file (keyfile, filename, G_KEY_FILE_NONE, NULL))
{
gsize n_groups;
char **groups = g_key_file_get_groups (keyfile, &n_groups);
int i;
for (i = 0; i < n_groups; ++i)
{
MetaData *data = g_new0 (MetaData, 1);
char *group = groups[i];
int b;
if (!get_int (keyfile, group, "window_x", &(data->window_x)))
data->window_x = 0;
if (!get_int (keyfile, group, "window_y", &(data->window_y)))
data->window_y = 0;
if (!get_int (keyfile, group, "window_width", &(data->window_width)))
data->window_width = 0;
if (!get_int (keyfile, group, "window_height", &(data->window_height)))
data->window_height = 0;
if (!get_int (keyfile, group, "background", &b))
data->background = BG_NONE;
else
data->background = b;
if (data->background >= BG_LAST || data->background < BG_FIRST)
data->background = BG_NONE;
if (!get_int (keyfile, group, "smooth_image", &(data->smooth_image)))
data->smooth_image = TRUE;
if (!get_int (keyfile, group, "zoom_level", &(data->zoom_level)))
data->zoom_level = 0;
if (!get_int (keyfile, group, "vadj", &(data->vadj)))
data->vadj = 0;
if (!get_int (keyfile, group, "hadj", &(data->hadj)))
data->hadj = 0;
g_hash_table_insert (result, g_strdup (group), data);
}
g_strfreev (groups);
}
g_free (filename);
g_key_file_free (keyfile);
return result;
}
static void
app_new (int argc, char **argv)
{
GList *filenames = NULL;
GList *list;
App *app;
int i;
for (i = 1; i < argc; ++i)
{
char *option = argv[i];
char *name;
if (strcmp (option, "--version") == 0 ||
strcmp (option, "-v") == 0)
{
g_print ("%s %s\n", APPLICATION_NAME, PACKAGE_VERSION);
exit (1);
return;
}
if (g_path_is_absolute (option))
{
name = g_strdup (option);
}
else
{
name = g_build_filename (g_get_current_dir(), option, NULL);
}
filenames = g_list_prepend (filenames, name);
}
app = g_new0 (App, 1);
filenames = g_list_reverse (filenames);
app->meta_data = load_meta_data ();
g_hash_table_new (g_str_hash, g_str_equal);
/* To make sure we don't exit if the window count reaches zero */
app->n_windows = 1;
if (filenames)
{
int i;
i = 0;
for (list = filenames; list != NULL; list = list->next)
{
Window *window;
GError *err = NULL;
char *filename = list->data;
/* Don't open more than 32 windows */
if (++i > 32)
break;
window = window_new (app);
if (!window_load_file (window, list->data, &err))
{
g_print ("Could not open %s: %s\n", filename, err->message);
window_free (window);
}
else
{
window_show (window, GDK_CURRENT_TIME);
}
}
for (list = filenames; list != NULL; list = list->next)
g_free (list->data);
g_list_free (filenames);
}
else
{
window_show (window_new (app), GDK_CURRENT_TIME);
}
if (app->n_windows == 1)
exit (1);
else
--app->n_windows;
}
int
main (int argc,
char **argv)
{
/* Disable gslice, since it
*
* - confuses valgrind
* - caches too much memory
* - hides memory access bugs
* - is not faster than malloc
*
* Note that g_slice_set_config() is broken in some versions of
* GLib (and 'declared internal' according to Tim), so we use the
* environment variable instead.
*/
if (!getenv ("G_SLICE"))
putenv ("G_SLICE=always_malloc");
gtk_init (&argc, &argv);
if (!g_file_test (GLADE_FILE, G_FILE_TEST_EXISTS))
{
app_show_warning (NULL,
APPLICATION_NAME " was not compiled or installed correctly.\n"
"\n"
"Running \"make install\" may solve this problem.\n");
return FALSE;
}
app_new (argc, argv);
gtk_main ();
return 0;
}
|