gliv-1.7
[gliv.git] / src / gliv.c
blobb30241f7e9e5686a5c5b2817cceb381e555e59a4
1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 * See the COPYING file for license information.
18 * Guillaume Chazarain <gfc@altern.org>
21 /*********************
22 * Main source file. *
23 *********************/
25 #include "gliv.h"
26 #include "cmdline.h"
28 #include <stdlib.h> /* exit() */
29 #include <unistd.h> /* close(), dup(), dup2(), STDERR_FILENO */
30 #include <gtk/gtkgl.h>
32 rt_struct *rt;
33 options_struct *options;
34 gliv_image *current_image = NULL;
35 GtkWidget *gl_widget;
36 GtkMenuBar *menu_bar;
39 * This is borrowed from gtk+-2, without the gtk_grab_notify() calls.
40 * They slow down things a lot when the images menus are built with many files.
42 GtkWindowGroup *_gtk_window_get_group(GtkWindow * window)
44 if (window && window->group)
45 return window->group;
46 else {
47 static GtkWindowGroup *default_group = NULL;
49 if (!default_group)
50 default_group = gtk_window_group_new();
52 return default_group;
56 static GtkWindowGroup *gtk_main_get_window_group(GtkWidget * widget)
58 GtkWidget *toplevel = NULL;
60 if (widget)
61 toplevel = gtk_widget_get_toplevel(widget);
63 if (toplevel && GTK_IS_WINDOW(toplevel))
64 return _gtk_window_get_group(GTK_WINDOW(toplevel));
65 else
66 return _gtk_window_get_group(NULL);
69 void gtk_grab_add(GtkWidget * widget)
71 GtkWindowGroup *group;
73 g_return_if_fail(widget != NULL);
75 if (!GTK_WIDGET_HAS_GRAB(widget) && GTK_WIDGET_IS_SENSITIVE(widget)) {
76 GTK_WIDGET_SET_FLAGS(widget, GTK_HAS_GRAB);
78 group = gtk_main_get_window_group(widget);
80 g_object_ref(widget);
81 group->grabs = g_slist_prepend(group->grabs, widget);
83 /* gtk_grab_notify (group, widget, FALSE); */
87 void gtk_grab_remove(GtkWidget * widget)
89 GtkWindowGroup *group;
91 g_return_if_fail(widget != NULL);
93 if (GTK_WIDGET_HAS_GRAB(widget)) {
94 GTK_WIDGET_UNSET_FLAGS(widget, GTK_HAS_GRAB);
96 group = gtk_main_get_window_group(widget);
97 group->grabs = g_slist_remove(group->grabs, widget);
99 g_object_unref(widget);
101 /* gtk_grab_notify (group, widget, TRUE); */
105 #define check_flag(given, flag, value) if (given) flag = value;
107 typedef struct {
108 gboolean given;
109 gboolean *flag;
110 gboolean value;
111 } ggo_arg;
113 static void fill_flags(struct gengetopt_args_info *ggo)
115 /* *INDENT-OFF* */
116 ggo_arg args[] = {
117 { ggo->zoom_pointer_given, &options->zoom_pointer, ggo->zoom_pointer_flag },
118 { ggo->dither_given, &options->dither, ggo->dither_flag },
119 { ggo->force_load_given, &options->force, ggo->force_load_flag },
120 { ggo->mipmap_given, &options->mipmap, ggo->mipmap_flag },
121 { ggo->build_menus_given, &options->build_menus, ggo->build_menus_flag },
122 { ggo->mnemonics_given, &options->mnemonics, ggo->mnemonics_flag },
123 { ggo->menu_given, &options->menu_bar, ggo->menu_flag },
124 { ggo->info_given, &options->status_bar, ggo->info_flag },
125 { ggo->scrollbars_given, &options->scrollbars, ggo->scrollbars_flag },
126 { ggo->alpha_checks_given, &options->alpha_checks, ggo->alpha_checks_flag },
127 { ggo->loop_given, &options->loop, ggo->loop_flag },
128 { FALSE, NULL, FALSE }
130 /* *INDENT-ON* */
132 ggo_arg *arg;
134 for (arg = args; arg->flag != NULL; arg++)
135 check_flag(arg->given, *(arg->flag), arg->value);
138 static void init_args(gint argc, gchar ** argv)
140 struct gengetopt_args_info ggo;
141 options_struct *rc_file;
143 /* Command line (only the glivrc flag). */
144 if (cmdline_parser(argc, argv, &ggo) != 0)
145 quit(1);
147 /* Configuration file. */
148 rc_file = load_rc(ggo.glivrc_flag);
149 options = g_memdup(rc_file, sizeof(options_struct));
151 /* Command line (remaining options). */
153 if (ggo.full_screen_given || ggo.make_fit_given ||
154 ggo.maximize_given || ggo.scale_down_given) {
156 options->fullscreen = ggo.full_screen_flag || ggo.make_fit_flag ||
157 ggo.maximize_flag || ggo.scale_down_flag;
159 if (ggo.make_fit_given)
160 options->maximize = options->scaledown = ggo.make_fit_flag;
161 else {
163 check_flag(ggo.maximize_given, options->maximize,
164 ggo.maximize_flag);
166 check_flag(ggo.scale_down_given, options->scaledown,
167 ggo.scale_down_flag);
170 } else if (options->maximize || options->scaledown)
171 options->fullscreen = TRUE;
173 if (ggo.delay_given && ggo.delay_arg >= 0)
174 options->delay = ggo.delay_arg;
176 if (ggo.history_given && ggo.history_arg >= -1)
177 options->history_size = ggo.history_arg;
179 if (ggo.duration_given && ggo.duration_arg >= 0)
180 options->duration = ggo.duration_arg;
182 fill_flags(&ggo);
184 rt = g_new(rt_struct, 1);
186 rt->cursor_hidden = FALSE;
187 rt->help = FALSE;
188 rt->alpha_checks_changed = TRUE;
189 rt->scr_width = gdk_screen_width();
190 rt->scr_height = gdk_screen_height();
192 if (ggo.inputs_num > 0) {
193 init_list(ggo.inputs, ggo.inputs_num,
194 ggo.recursive_flag, ggo.shuffle_given, ggo.sort_given);
196 if (ggo.slide_show_flag)
197 begin_with_slide_show();
200 g_free(ggo.inputs);
204 * We call cmdline_parser() a first time to quickly exit if --help or --version
205 * was given. We do the real work after the next call in init_args(), after
206 * gtk_init() removed the gtk arguments.
208 * We temporarily close stderr because unknown and gtk arguments are handled
209 * afterwards.
211 static void check_quick_exit(gint argc, gchar ** argv)
213 struct gengetopt_args_info ggo;
214 gint fd;
216 fd = dup(STDERR_FILENO);
217 close(STDERR_FILENO);
219 cmdline_parser(argc, argv, &ggo);
221 dup2(fd, STDERR_FILENO);
222 close(fd);
225 G_GNUC_NORETURN void quit(gint code)
227 imagemagick_finish();
228 exit(code);
231 gint main(gint argc, gchar ** argv)
233 /* i18n */
234 #ifdef ENABLE_NLS
235 gtk_set_locale();
236 # ifdef LOCALEDIR
237 bindtextdomain(PACKAGE, LOCALEDIR);
238 # endif
239 textdomain(PACKAGE);
240 bind_textdomain_codeset(PACKAGE, "UTF-8");
241 #endif
243 check_quick_exit(argc, argv);
245 gtk_init(&argc, &argv);
246 gtk_gl_init(&argc, &argv);
248 init_args(argc, argv);
250 create_windows();
252 gtk_main();
254 return 0;