gliv-1.4.1
[gliv.git] / src / windows.c
blob8b85d0f8bcd945f5e78884672b2deba4baf940d7
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 <booh@altern.org>
21 /*************************
22 * The toplevel widgets. *
23 *************************/
25 #include <unistd.h> /* alarm() */
26 #include "gliv.h"
28 extern rt_struct *rt;
29 extern options_struct *options;
30 extern gliv_image *current_image;
31 extern GtkWidget *gl_widget;
32 extern GtkMenuBar *menu_bar;
34 static GtkVBox *widgets;
36 static GtkWindow *main_window;
37 static GtkWindow *fs_window;
39 /* The main status bar and its subdivisions. */
40 static GtkHBox *status_bar;
41 static GtkEntry *entry_ident;
42 static GtkEntry *entry_size;
43 static GtkEntry *entry_zoom;
45 #ifdef GTK2
46 void show_dialog(GtkWidget * widget, gboolean unused)
48 if (options->fullscreen == TRUE)
49 gtk_window_set_position(GTK_WINDOW(widget), GTK_WIN_POS_MOUSE);
51 gtk_widget_show_all(widget);
53 #else
54 /* Give back the grab to the fullscreen window when a dialog is closed. */
55 static void restore_grab(void)
57 if (options->fullscreen == TRUE)
58 gdk_keyboard_grab(GTK_WIDGET(fs_window)->window, TRUE,
59 GDK_CURRENT_TIME);
60 else
61 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
64 void show_dialog(GtkWidget * widget, gboolean grab)
66 if (options->fullscreen == TRUE) {
67 gtk_widget_realize(widget);
68 gdk_window_set_override_redirect(widget->window, TRUE);
69 gtk_window_set_position(GTK_WINDOW(widget), GTK_WIN_POS_MOUSE);
72 gtk_widget_show_all(widget);
74 if (options->fullscreen == TRUE && grab == TRUE) {
75 gtk_signal_connect(GTK_OBJECT(widget), "destroy",
76 GTK_SIGNAL_FUNC(restore_grab), NULL);
78 gdk_keyboard_grab(widget->window, FALSE, GDK_CURRENT_TIME);
82 #endif
84 static GtkEntry *add_entry(gboolean grow)
86 GtkEntry *entry;
88 entry = GTK_ENTRY(gtk_entry_new());
89 gtk_editable_set_editable(GTK_EDITABLE(entry), FALSE);
90 gtk_box_pack_start(GTK_BOX(status_bar), GTK_WIDGET(entry), grow, TRUE, 0);
92 return entry;
95 static void create_status_bar(void)
97 status_bar = GTK_HBOX(gtk_hbox_new(FALSE, 0));
99 entry_ident = add_entry(TRUE);
100 entry_size = add_entry(FALSE);
101 entry_zoom = add_entry(FALSE);
104 static void toggle_widget(GtkWidget * widget, gboolean * flag)
106 gint new_width, new_height;
108 new_width = GTK_WIDGET(main_window)->allocation.width;
109 new_height = GTK_WIDGET(main_window)->allocation.height;
111 if (*flag == FALSE) {
112 gtk_widget_show_all(widget);
113 new_height += widget->requisition.height;
114 } else {
115 new_height -= widget->requisition.height;
116 gtk_widget_hide(widget);
119 *flag ^= TRUE;
120 if (options->fullscreen == FALSE)
121 gtk_window_resize(main_window, new_width, new_height);
124 void toggle_status_bar(void)
126 toggle_widget(GTK_WIDGET(status_bar), &(options->status_bar));
129 void toggle_menu_bar(void)
131 toggle_widget(GTK_WIDGET(menu_bar), &(options->menu_bar));
134 void update_status_bar(void)
136 gchar *size_str = NULL;
137 gchar *percent_str;
139 if (current_image->ident != NULL) {
140 /* Filename status, only the first time. */
141 gtk_entry_set_text(entry_ident, current_image->ident);
143 /* Dimensions status, only the first time. */
144 size_str = g_strdup_printf("%dx%d",
145 current_image->width, current_image->height);
146 gtk_entry_set_text(entry_size, size_str);
149 /* Zoom status. */
150 percent_str = g_strdup_printf("%f%%", rt->image_zoom * 100.0);
151 gtk_entry_set_text(entry_zoom, percent_str);
153 g_free(size_str);
154 g_free(percent_str);
157 #ifdef GTK2
158 static gboolean first_map(void)
159 #else
160 static void main_realize(void)
161 #endif
163 if (options->menu_bar == FALSE)
164 gtk_widget_hide(GTK_WIDGET(menu_bar));
166 if (options->status_bar == FALSE)
167 gtk_widget_hide(GTK_WIDGET(status_bar));
169 #ifdef GTK2
170 gtk_window_move(fs_window, 0, 0);
172 return TRUE;
173 #endif
176 void create_windows(void)
178 GtkWindow *first_window;
180 /* Creation. */
181 create_gl_widget();
182 create_status_bar();
183 create_menus();
185 /* Collection. */
186 widgets = GTK_VBOX(gtk_vbox_new(FALSE, 0));
187 gtk_box_pack_start(GTK_BOX(widgets), GTK_WIDGET(menu_bar), FALSE, FALSE, 0);
188 gtk_box_pack_start_defaults(GTK_BOX(widgets), GTK_WIDGET(gl_widget));
189 gtk_box_pack_end(GTK_BOX(widgets), GTK_WIDGET(status_bar), FALSE, 0, 0);
191 /* The main window : seen when not in fullscreen. */
192 main_window = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL));
193 gtk_window_set_title(main_window, "GLiv");
194 #ifndef GTK2
195 gtk_signal_connect(GTK_OBJECT(main_window), "realize",
196 GTK_SIGNAL_FUNC(main_realize), NULL);
197 #endif
198 install_callbacks(GTK_OBJECT(main_window));
200 /* The window seen in fullscreen mode. */
201 #ifdef GTK2
202 fs_window = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL));
203 #else
204 fs_window = GTK_WINDOW(gtk_window_new(GTK_WINDOW_POPUP));
205 gtk_window_set_position(fs_window, GTK_WIN_POS_CENTER);
206 #endif
207 gtk_window_set_title(fs_window, "GLiv in fullscreen");
208 gtk_window_set_default_size(fs_window, rt->scr_width, rt->scr_height);
209 gtk_window_set_decorated(fs_window, FALSE);
210 install_callbacks(GTK_OBJECT(fs_window));
212 if (options->fullscreen == FALSE)
213 first_window = main_window;
214 else {
215 first_window = fs_window;
216 #ifndef GTK2
217 gtk_widget_realize(GTK_WIDGET(main_window));
218 gtk_widget_hide(GTK_WIDGET(main_window));
219 #endif
222 gtk_container_add(GTK_CONTAINER(first_window), GTK_WIDGET(widgets));
223 #ifdef GTK2
224 gtk_signal_connect(GTK_OBJECT(first_window), "map",
225 GTK_SIGNAL_FUNC(first_map), NULL);
226 #endif
227 gtk_widget_show_all(GTK_WIDGET(first_window));
230 void goto_window(void)
232 gint new_width, new_height;
234 new_width = current_image->width;
235 new_height = current_image->height;
237 if (options->menu_bar == TRUE) {
238 new_height += GTK_WIDGET(menu_bar)->requisition.height;
239 if (new_width < GTK_WIDGET(menu_bar)->requisition.width)
240 new_width = GTK_WIDGET(menu_bar)->requisition.width;
243 if (options->status_bar == TRUE) {
244 new_height += GTK_WIDGET(status_bar)->requisition.height;
245 if (new_width < GTK_WIDGET(status_bar)->requisition.width)
246 new_width = GTK_WIDGET(status_bar)->requisition.width;
249 if (new_width >= rt->scr_width || new_height >= rt->scr_height) {
250 new_width = 3 * rt->scr_width / 4;
251 new_height = 3 * rt->scr_height / 4;
253 #ifndef GTK2
254 gtk_widget_set(gl_widget, "GtkWidget::width", current_image->width, NULL);
255 gtk_widget_set(gl_widget, "GtkWidget::height", current_image->height, NULL);
256 gtk_widget_set_usize(gl_widget, 0, 0);
257 #endif
259 gtk_window_resize(main_window, new_width, new_height);
262 #ifndef GTK2
263 static void reparent(GtkWidget * new_parent)
265 /* Be sure all widgets are displayed. */
267 if (options->status_bar == TRUE && GTK_WIDGET_REALIZED(status_bar) == FALSE)
268 gtk_container_foreach(GTK_CONTAINER(status_bar),
269 GTK_SIGNAL_FUNC(gtk_widget_realize), NULL);
271 gtk_widget_show_all(GTK_WIDGET(status_bar));
273 gtk_widget_show_all(GTK_WIDGET(menu_bar));
275 /* Then reparent them. */
276 gtk_widget_reparent(GTK_WIDGET(widgets), new_parent);
278 /* Finally, hide widget previously hidden. */
280 if (options->status_bar == FALSE)
281 gtk_widget_hide(GTK_WIDGET(status_bar));
283 if (options->menu_bar == FALSE)
284 gtk_widget_hide(GTK_WIDGET(menu_bar));
286 #endif
287 void toggle_fullscreen(gboolean enable)
289 GtkWindow *new;
290 static gint previous_width, previous_height;
291 static gliv_image *previous_image = NULL;
293 if (enable == TRUE) {
294 /* Go to fullscreen mode. */
295 new = fs_window;
296 previous_width = GTK_WIDGET(main_window)->allocation.width;
297 previous_height = GTK_WIDGET(main_window)->allocation.height;
298 } else
299 /* Go to window mode. */
300 new = main_window;
302 gtk_widget_show_all(GTK_WIDGET(new));
303 #ifdef GTK2
304 gtk_widget_reparent(GTK_WIDGET(widgets), GTK_WIDGET(new));
305 #else
306 reparent(GTK_WIDGET(new));
307 intercept_events(enable);
308 #endif
310 if (enable == FALSE) {
311 gtk_widget_hide(GTK_WIDGET(fs_window));
312 if (previous_image == current_image)
313 gtk_window_resize(main_window, previous_width, previous_height);
314 else {
315 goto_window();
316 previous_image = current_image;
321 * This is done at the initialization but for some unknown reason
322 * it has to be done here too.
324 gdk_gc_set_foreground(gl_widget->style->fg_gc[GTK_STATE_SELECTED],
325 &(gl_widget->style->white));
327 options->fullscreen = enable;
328 alarm(options->delay);