5080f03f497ea1c50ab6064117e60e76f1b5ca0a
[lxde/gpicview.git] / src / main-win.c
1 /***************************************************************************
2 * Copyright (C) 2007, 2008 by PCMan (Hong Jen Yee) *
3 * pcman.tw@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include "main-win.h"
26
27 #include <glib/gi18n.h>
28 #include <glib/gstdio.h>
29 #include <gdk/gdkkeysyms.h>
30
31 #include <unistd.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36
37 #include "pref.h"
38
39 #include "image-view.h"
40 #include "image-list.h"
41 #include "working-area.h"
42 #include "ptk-menu.h"
43 #include "file-dlgs.h"
44 #include "jpeg-tran.h"
45
46 /* For drag & drop */
47 static GtkTargetEntry drop_targets[] =
48 {
49 {"text/uri-list", 0, 0},
50 {"text/plain", 0, 1}
51 };
52
53 extern int ExifRotate(const char * fname, int new_angle);
54
55 static void main_win_init( MainWin*mw );
56 static void main_win_finalize( GObject* obj );
57
58 static void create_nav_bar( MainWin* mw, GtkWidget* box);
59 GtkWidget* add_nav_btn( MainWin* mw, const char* icon, const char* tip, GCallback cb, gboolean toggle);
60 GtkWidget* add_nav_btn_img( MainWin* mw, const char* icon, const char* tip, GCallback cb, gboolean toggle, GtkWidget** ret_img);
61 // GtkWidget* add_menu_item( GtkMenuShell* menu, const char* label, const char* icon, GCallback cb, gboolean toggle=FALSE );
62 static void rotate_image( MainWin* mw, int angle );
63 static void show_popup_menu( MainWin* mw, GdkEventButton* evt );
64
65 /* signal handlers */
66 static gboolean on_delete_event( GtkWidget* widget, GdkEventAny* evt );
67 static void on_size_allocate( GtkWidget* widget, GtkAllocation *allocation );
68 static gboolean on_win_state_event( GtkWidget* widget, GdkEventWindowState* state );
69 static void on_zoom_fit( GtkToggleButton* btn, MainWin* mw );
70 static void on_zoom_fit_menu( GtkMenuItem* item, MainWin* mw );
71 static void on_full_screen( GtkWidget* btn, MainWin* mw );
72 static void on_next( GtkWidget* btn, MainWin* mw );
73 static void on_orig_size( GtkToggleButton* btn, MainWin* mw );
74 static void on_orig_size_menu( GtkToggleButton* btn, MainWin* mw );
75 static void on_prev( GtkWidget* btn, MainWin* mw );
76 static void on_rotate_auto_save( GtkWidget* btn, MainWin* mw );
77 static void on_rotate_clockwise( GtkWidget* btn, MainWin* mw );
78 static void on_rotate_counterclockwise( GtkWidget* btn, MainWin* mw );
79 static void on_save_as( GtkWidget* btn, MainWin* mw );
80 static void on_save( GtkWidget* btn, MainWin* mw );
81 static void cancel_slideshow(MainWin* mw);
82 static gboolean next_slide(MainWin* mw);
83 static void on_slideshow_menu( GtkMenuItem* item, MainWin* mw );
84 static void on_slideshow( GtkToggleButton* btn, MainWin* mw );
85 static void on_open( GtkWidget* btn, MainWin* mw );
86 static void on_zoom_in( GtkWidget* btn, MainWin* mw );
87 static void on_zoom_out( GtkWidget* btn, MainWin* mw );
88 static void on_preference( GtkWidget* btn, MainWin* mw );
89 static void on_quit( GtkWidget* btn, MainWin* mw );
90 static gboolean on_button_press( GtkWidget* widget, GdkEventButton* evt, MainWin* mw );
91 static gboolean on_button_release( GtkWidget* widget, GdkEventButton* evt, MainWin* mw );
92 static gboolean on_mouse_move( GtkWidget* widget, GdkEventMotion* evt, MainWin* mw );
93 static gboolean on_scroll_event( GtkWidget* widget, GdkEventScroll* evt, MainWin* mw );
94 static gboolean on_key_press_event(GtkWidget* widget, GdkEventKey * key);
95 static gboolean save_confirm( MainWin* mw, const char* file_path );
96 static void on_drag_data_received( GtkWidget* widget, GdkDragContext *drag_context,
97 int x, int y, GtkSelectionData* data, guint info, guint time, MainWin* mw );
98 static void on_delete( GtkWidget* btn, MainWin* mw );
99 static void on_about( GtkWidget* menu, MainWin* mw );
100
101 static void update_title(const char *filename, MainWin *mw );
102
103 void on_flip_vertical( GtkWidget* btn, MainWin* mw );
104 void on_flip_horizontal( GtkWidget* btn, MainWin* mw );
105 static int trans_angle_to_id(int i);
106 static int get_new_angle( int orig_angle, int rotate_angle );
107
108 // Begin of GObject-related stuff
109
110 G_DEFINE_TYPE( MainWin, main_win, GTK_TYPE_WINDOW )
111
112 void main_win_class_init( MainWinClass* klass )
113 {
114 GObjectClass * obj_class;
115 GtkWidgetClass *widget_class;
116
117 obj_class = ( GObjectClass * ) klass;
118 // obj_class->set_property = _set_property;
119 // obj_class->get_property = _get_property;
120 obj_class->finalize = main_win_finalize;
121
122 widget_class = GTK_WIDGET_CLASS ( klass );
123 widget_class->delete_event = on_delete_event;
124 widget_class->size_allocate = on_size_allocate;
125 widget_class->key_press_event = on_key_press_event;
126 widget_class->window_state_event = on_win_state_event;
127 }
128
129 void main_win_finalize( GObject* obj )
130 {
131 MainWin *mw = (MainWin*)obj;
132
133 main_win_close(mw);
134
135 if( G_LIKELY(mw->img_list) )
136 image_list_free( mw->img_list );
137 gdk_cursor_unref( mw->hand_cursor );
138
139 // FIXME: Put this here is weird
140 gtk_main_quit();
141 }
142
143 GtkWidget* main_win_new()
144 {
145 return (GtkWidget*)g_object_new ( MAIN_WIN_TYPE, NULL );
146 }
147
148 // End of GObject-related stuff
149
150 void main_win_init( MainWin*mw )
151 {
152 gtk_window_set_title( (GtkWindow*)mw, _("Image Viewer"));
153 gtk_window_set_icon_from_file( (GtkWindow*)mw, PACKAGE_DATA_DIR"/pixmaps/gpicview.png", NULL );
154 gtk_window_set_default_size( (GtkWindow*)mw, 640, 480 );
155
156 GtkWidget* box = gtk_vbox_new( FALSE, 0 );
157 gtk_container_add( (GtkContainer*)mw, box);
158
159 // image area
160 mw->evt_box = gtk_event_box_new();
161 GTK_WIDGET_SET_FLAGS( mw->evt_box, GTK_CAN_FOCUS );
162 gtk_widget_add_events( mw->evt_box,
163 GDK_POINTER_MOTION_MASK|GDK_BUTTON_PRESS_MASK|
164 GDK_BUTTON_RELEASE_MASK|GDK_SCROLL_MASK );
165 g_signal_connect( mw->evt_box, "button-press-event", G_CALLBACK(on_button_press), mw );
166 g_signal_connect( mw->evt_box, "button-release-event", G_CALLBACK(on_button_release), mw );
167 g_signal_connect( mw->evt_box, "motion-notify-event", G_CALLBACK(on_mouse_move), mw );
168 g_signal_connect( mw->evt_box, "scroll-event", G_CALLBACK(on_scroll_event), mw );
169 // Set bg color to white
170
171 gtk_widget_modify_bg( mw->evt_box, GTK_STATE_NORMAL, &pref.bg );
172
173 mw->img_view = image_view_new();
174 gtk_container_add( (GtkContainer*)mw->evt_box, (GtkWidget*)mw->img_view);
175
176 const char scroll_style[]=
177 "style \"gpicview-scroll\" {"
178 "GtkScrolledWindow::scrollbar-spacing=0"
179 "}"
180 "class \"GtkScrolledWindow\" style \"gpicview-scroll\"";
181 gtk_rc_parse_string( scroll_style );
182 mw->scroll = gtk_scrolled_window_new( NULL, NULL );
183 gtk_scrolled_window_set_shadow_type( (GtkScrolledWindow*)mw->scroll, GTK_SHADOW_NONE );
184 gtk_scrolled_window_set_policy((GtkScrolledWindow*)mw->scroll,
185 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
186 GtkAdjustment *hadj, *vadj;
187 hadj = gtk_scrolled_window_get_hadjustment((GtkScrolledWindow*)mw->scroll);
188 hadj->page_increment = 10;
189 gtk_adjustment_changed(hadj);
190 vadj = gtk_scrolled_window_get_vadjustment((GtkScrolledWindow*)mw->scroll);
191 vadj->page_increment = 10;
192 gtk_adjustment_changed(vadj);
193
194 image_view_set_adjustments( IMAGE_VIEW(mw->img_view), hadj, vadj ); // dirty hack :-(
195 gtk_scrolled_window_add_with_viewport( (GtkScrolledWindow*)mw->scroll, mw->evt_box );
196 GtkWidget* viewport = gtk_bin_get_child( (GtkBin*)mw->scroll );
197 gtk_viewport_set_shadow_type( (GtkViewport*)viewport, GTK_SHADOW_NONE );
198 gtk_container_set_border_width( (GtkContainer*)viewport, 0 );
199
200 gtk_box_pack_start( (GtkBox*)box, mw->scroll, TRUE, TRUE, 0 );
201
202 // build toolbar
203 create_nav_bar( mw, box );
204 gtk_widget_show_all( box );
205
206 mw->hand_cursor = gdk_cursor_new_for_display( gtk_widget_get_display((GtkWidget*)mw), GDK_FLEUR );
207
208 // zoom_mode = ZOOM_NONE;
209 mw->zoom_mode = ZOOM_FIT;
210
211 // Set up drag & drop
212 gtk_drag_dest_set( (GtkWidget*)mw, GTK_DEST_DEFAULT_ALL,
213 drop_targets,
214 G_N_ELEMENTS(drop_targets),
215 GDK_ACTION_COPY | GDK_ACTION_ASK );
216 g_signal_connect( mw, "drag-data-received", G_CALLBACK(on_drag_data_received), mw );
217
218 mw->img_list = image_list_new();
219
220 // rotation angle is zero on startup
221 mw->rotation_angle = 0;
222 }
223
224 void create_nav_bar( MainWin* mw, GtkWidget* box )
225 {
226 mw->nav_bar = gtk_hbox_new( FALSE, 0 );
227
228 add_nav_btn( mw, GTK_STOCK_GO_BACK, _("Previous"), G_CALLBACK(on_prev), FALSE );
229 add_nav_btn( mw, GTK_STOCK_GO_FORWARD, _("Next"), G_CALLBACK(on_next), FALSE );
230 mw->btn_play_stop = add_nav_btn_img( mw, GTK_STOCK_MEDIA_PLAY, _("Start Slideshow"), G_CALLBACK(on_slideshow), TRUE, &mw->img_play_stop );
231
232 gtk_box_pack_start( (GtkBox*)mw->nav_bar, gtk_vseparator_new(), FALSE, FALSE, 0 );
233
234 add_nav_btn( mw, GTK_STOCK_ZOOM_OUT, _("Zoom Out"), G_CALLBACK(on_zoom_out), FALSE );
235 add_nav_btn( mw, GTK_STOCK_ZOOM_IN, _("Zoom In"), G_CALLBACK(on_zoom_in), FALSE );
236
237 // percent = gtk_entry_new(); // show scale (in percentage)
238 // g_signal_connect( percent, "activate", G_CALLBACK(on_percentage), mw );
239 // gtk_widget_set_size_request( percent, 45, -1 );
240 // gtk_box_pack_start( (GtkBox*)nav_bar, percent, FALSE, FALSE, 2 );
241
242 mw->btn_fit = add_nav_btn( mw, GTK_STOCK_ZOOM_FIT, _("Fit Image To Window Size"),
243 G_CALLBACK(on_zoom_fit), TRUE );
244 mw->btn_orig = add_nav_btn( mw, GTK_STOCK_ZOOM_100, _("Original Size"),
245 G_CALLBACK(on_orig_size), TRUE );
246 gtk_toggle_button_set_active( (GtkToggleButton*)mw->btn_fit, TRUE );
247
248 #ifndef GTK_STOCK_FULLSCREEN
249 #define GTK_STOCK_FULLSCREEN "gtk-fullscreen"
250 #endif
251 add_nav_btn( mw, GTK_STOCK_FULLSCREEN, _("Full Screen"), G_CALLBACK(on_full_screen), FALSE ); // gtk+ 2.8+
252
253 gtk_box_pack_start( (GtkBox*)mw->nav_bar, gtk_vseparator_new(), FALSE, FALSE, 0 );
254
255 mw->btn_rotate_ccw = add_nav_btn( mw, "object-rotate-left", _("Rotate Counterclockwise"), G_CALLBACK(on_rotate_counterclockwise), FALSE );
256 mw->btn_rotate_cw = add_nav_btn( mw, "object-rotate-right", _("Rotate Clockwise"), G_CALLBACK(on_rotate_clockwise), FALSE );
257
258 mw->btn_flip_h = add_nav_btn( mw, "object-flip-horizontal", _("Flip Horizontal"), G_CALLBACK(on_flip_horizontal), FALSE );
259 mw->btn_flip_v = add_nav_btn( mw, "object-flip-vertical", _("Flip Vertical"), G_CALLBACK(on_flip_vertical), FALSE );
260
261 gtk_box_pack_start( (GtkBox*)mw->nav_bar, gtk_vseparator_new(), FALSE, FALSE, 0 );
262
263 add_nav_btn( mw, GTK_STOCK_OPEN, _("Open File"), G_CALLBACK(on_open), FALSE );
264 add_nav_btn( mw, GTK_STOCK_SAVE, _("Save File"), G_CALLBACK(on_save), FALSE );
265 add_nav_btn( mw, GTK_STOCK_SAVE_AS, _("Save File As"), G_CALLBACK(on_save_as), FALSE );
266 add_nav_btn( mw, GTK_STOCK_DELETE, _("Delete File"), G_CALLBACK(on_delete), FALSE );
267
268 gtk_box_pack_start( (GtkBox*)mw->nav_bar, gtk_vseparator_new(), FALSE, FALSE, 0 );
269 add_nav_btn( mw, GTK_STOCK_PREFERENCES, _("Preferences"), G_CALLBACK(on_preference), FALSE );
270 add_nav_btn( mw, GTK_STOCK_QUIT, _("Quit"), G_CALLBACK(on_quit), FALSE );
271
272 GtkWidget* align = gtk_alignment_new( 0.5, 0, 0, 0 );
273 gtk_container_add( (GtkContainer*)align, mw->nav_bar );
274 gtk_box_pack_start( (GtkBox*)box, align, FALSE, TRUE, 2 );
275 }
276
277 gboolean on_delete_event( GtkWidget* widget, GdkEventAny* evt )
278 {
279 gtk_widget_destroy( widget );
280 return TRUE;
281 }
282
283 static void update_title(const char *filename, MainWin *mw )
284 {
285 static char fname[50];
286 static int wid, hei;
287
288 char buf[100];
289
290 if(filename != NULL)
291 {
292 strncpy(fname, filename, 49);
293 fname[49] = '\0';
294
295 wid = gdk_pixbuf_get_width( mw->pix );
296 hei = gdk_pixbuf_get_height( mw->pix );
297 }
298
299 snprintf(buf, 100, "%s (%dx%d) %d%%", fname, wid, hei, (int)(mw->scale * 100));
300 gtk_window_set_title( (GtkWindow*)mw, buf );
301
302 return;
303 }
304
305 gboolean on_animation_timeout( MainWin* mw )
306 {
307 int delay;
308 if ( gdk_pixbuf_animation_iter_advance( mw->animation_iter, NULL ) )
309 {
310 mw->pix = gdk_pixbuf_animation_iter_get_pixbuf( mw->animation_iter );
311 image_view_set_pixbuf( (ImageView*)mw->img_view, mw->pix );
312 }
313 delay = gdk_pixbuf_animation_iter_get_delay_time( mw->animation_iter );
314 mw->animation_timeout = g_timeout_add(delay, (GSourceFunc) on_animation_timeout, mw );
315 return FALSE;
316 }
317
318 static void update_btns(MainWin* mw)
319 {
320 gboolean enable = (mw->animation == NULL);
321 gtk_widget_set_sensitive(mw->btn_rotate_cw, enable);
322 gtk_widget_set_sensitive(mw->btn_rotate_ccw, enable);
323 gtk_widget_set_sensitive(mw->btn_flip_v, enable);
324 gtk_widget_set_sensitive(mw->btn_flip_h, enable);
325 }
326
327 gboolean main_win_open( MainWin* mw, const char* file_path, ZoomMode zoom )
328 {
329 GError* err = NULL;
330 GdkPixbufFormat* info;
331 info = gdk_pixbuf_get_file_info( file_path, NULL, NULL );
332 char* type = ((info != NULL) ? gdk_pixbuf_format_get_name(info) : "");
333
334 main_win_close( mw );
335
336 /* grabs a file as if it were an animation */
337 mw->animation = gdk_pixbuf_animation_new_from_file( file_path, &err );
338 if( ! mw->animation )
339 {
340 main_win_show_error( mw, err->message );
341 g_error_free(err);
342 update_btns( mw );
343 return FALSE;
344 }
345
346 /* tests if the file is actually just a normal picture */
347 if ( gdk_pixbuf_animation_is_static_image( mw->animation ) )
348 {
349 mw->pix = gdk_pixbuf_animation_get_static_image( mw->animation );
350 g_object_ref(mw->pix);
351 g_object_unref(mw->animation);
352 mw->animation = NULL;
353 }
354 else
355 {
356 int delay;
357 /* we found an animation */
358 mw->animation_iter = gdk_pixbuf_animation_get_iter( mw->animation, NULL );
359 mw->pix = gdk_pixbuf_animation_iter_get_pixbuf( mw->animation_iter );
360 delay = gdk_pixbuf_animation_iter_get_delay_time( mw->animation_iter );
361 mw->animation_timeout = g_timeout_add( delay, (GSourceFunc) on_animation_timeout, mw );
362 }
363 update_btns( mw );
364
365 if(!strcmp(type,"jpeg"))
366 {
367 GdkPixbuf* tmp;
368 // Only jpeg should rotate by EXIF
369 tmp = gdk_pixbuf_apply_embedded_orientation(mw->pix);
370 g_object_unref(mw->pix);
371 mw->pix = tmp;
372 }
373
374 mw->zoom_mode = zoom;
375
376 // select most suitable viewing mode
377 if( zoom == ZOOM_NONE )
378 {
379 int w = gdk_pixbuf_get_width( mw->pix );
380 int h = gdk_pixbuf_get_height( mw->pix );
381
382 GdkRectangle area;
383 get_working_area( gtk_widget_get_screen((GtkWidget*)mw), &area );
384 // g_debug("determine best zoom mode: orig size: w=%d, h=%d", w, h);
385 // FIXME: actually this is a little buggy :-(
386 if( w < area.width && h < area.height && (w >= 640 || h >= 480) )
387 {
388 gtk_scrolled_window_set_policy( (GtkScrolledWindow*)mw->scroll, GTK_POLICY_NEVER, GTK_POLICY_NEVER );
389 gtk_widget_set_size_request( (GtkWidget*)mw->img_view, w, h );
390 GtkRequisition req;
391 gtk_widget_size_request ( (GtkWidget*)mw, &req );
392 if( req.width < 640 ) req.width = 640;
393 if( req.height < 480 ) req.height = 480;
394 gtk_window_resize( (GtkWindow*)mw, req.width, req.height );
395 gtk_widget_set_size_request( (GtkWidget*)mw->img_view, -1, -1 );
396 gtk_scrolled_window_set_policy( (GtkScrolledWindow*)mw->scroll, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
397 mw->zoom_mode = ZOOM_ORIG;
398 mw->scale = 1.0;
399 }
400 else
401 mw->zoom_mode = ZOOM_FIT;
402 }
403
404 if( mw->zoom_mode == ZOOM_FIT )
405 {
406 gtk_toggle_button_set_active( (GtkToggleButton*)mw->btn_fit, TRUE );
407 main_win_fit_window_size( mw, FALSE, GDK_INTERP_BILINEAR );
408 }
409 else if( mw->zoom_mode == ZOOM_SCALE ) // scale
410 {
411 gtk_toggle_button_set_active( (GtkToggleButton*)mw->btn_orig, FALSE );
412 gtk_toggle_button_set_active( (GtkToggleButton*)mw->btn_fit, FALSE );
413 main_win_scale_image( mw, mw->scale, GDK_INTERP_BILINEAR );
414 }
415 else if( mw->zoom_mode == ZOOM_ORIG ) // original size
416 {
417 gtk_toggle_button_set_active( (GtkToggleButton*)mw->btn_orig, TRUE );
418 image_view_set_scale( (ImageView*)mw->img_view, mw->scale, GDK_INTERP_BILINEAR );
419 main_win_center_image( mw );
420 }
421
422 image_view_set_pixbuf( (ImageView*)mw->img_view, mw->pix );
423
424 // while (gtk_events_pending ())
425 // gtk_main_iteration ();
426
427 // build file list
428 char* dir_path = g_path_get_dirname( file_path );
429 image_list_open_dir( mw->img_list, dir_path, NULL );
430 image_list_sort_by_name( mw->img_list, GTK_SORT_DESCENDING );
431 g_free( dir_path );
432
433 char* base_name = g_path_get_basename( file_path );
434 image_list_set_current( mw->img_list, base_name );
435
436 char* disp_name = g_filename_display_name( base_name );
437 g_free( base_name );
438
439 update_title( disp_name, mw );
440 g_free( disp_name );
441
442 return TRUE;
443 }
444
445 void main_win_start_slideshow( MainWin* mw )
446 {
447 on_slideshow_menu(NULL, mw);
448 }
449
450 void main_win_close( MainWin* mw )
451 {
452 if( mw->animation )
453 {
454 g_object_unref( mw->animation );
455 mw->animation = NULL;
456 if( mw->animation_timeout );
457 {
458 g_source_remove( mw->animation_timeout );
459 mw->animation_timeout = 0;
460 }
461 }
462 else if( mw->pix )
463 {
464 g_object_unref( mw->pix );
465 }
466 mw->pix = NULL;
467 }
468
469 void main_win_show_error( MainWin* mw, const char* message )
470 {
471 GtkWidget* dlg = gtk_message_dialog_new( (GtkWindow*)mw,
472 GTK_DIALOG_MODAL,
473 GTK_MESSAGE_ERROR,
474 GTK_BUTTONS_OK,
475 "%s", message );
476 gtk_dialog_run( (GtkDialog*)dlg );
477 gtk_widget_destroy( dlg );
478 }
479
480 void on_size_allocate( GtkWidget* widget, GtkAllocation *allocation )
481 {
482 GTK_WIDGET_CLASS(main_win_parent_class)->size_allocate( widget, allocation );
483 if( GTK_WIDGET_REALIZED (widget) )
484 {
485 MainWin* mw = (MainWin*)widget;
486
487 if( mw->zoom_mode == ZOOM_FIT )
488 {
489 while(gtk_events_pending ())
490 gtk_main_iteration(); // makes it more fluid
491
492 main_win_fit_window_size( mw, FALSE, GDK_INTERP_BILINEAR );
493 }
494 }
495 }
496
497 gboolean on_win_state_event( GtkWidget* widget, GdkEventWindowState* state )
498 {
499 MainWin* mw = (MainWin*)widget;
500 if( state->new_window_state == GDK_WINDOW_STATE_FULLSCREEN )
501 {
502 gtk_widget_modify_bg( mw->evt_box, GTK_STATE_NORMAL, &pref.bg_full );
503 gtk_widget_hide( gtk_widget_get_parent(mw->nav_bar) );
504 mw->full_screen = TRUE;
505 }
506 else
507 {
508 gtk_widget_modify_bg( mw->evt_box, GTK_STATE_NORMAL, &pref.bg );
509 gtk_widget_show( gtk_widget_get_parent(mw->nav_bar) );
510 mw->full_screen = FALSE;
511 }
512
513 int previous = pref.open_maximized;
514 pref.open_maximized = (state->new_window_state == GDK_WINDOW_STATE_MAXIMIZED);
515 if (previous != pref.open_maximized)
516 save_preferences();
517 return TRUE;
518 }
519
520 void main_win_fit_size( MainWin* mw, int width, int height, gboolean can_strech, GdkInterpType type )
521 {
522 if( ! mw->pix )
523 return;
524
525 int orig_w = gdk_pixbuf_get_width( mw->pix );
526 int orig_h = gdk_pixbuf_get_height( mw->pix );
527
528 if( can_strech || (orig_w > width || orig_h > height) )
529 {
530 gdouble xscale = ((gdouble)width) / orig_w;
531 gdouble yscale = ((gdouble)height)/ orig_h;
532 gdouble final_scale = xscale < yscale ? xscale : yscale;
533
534 main_win_scale_image( mw, final_scale, type );
535 }
536 else // use original size if the image is smaller than the window
537 {
538 mw->scale = 1.0;
539 image_view_set_scale( (ImageView*)mw->img_view, 1.0, type );
540
541 update_title(NULL, mw);
542 }
543 }
544
545 void main_win_fit_window_size( MainWin* mw, gboolean can_strech, GdkInterpType type )
546 {
547 mw->zoom_mode = ZOOM_FIT;
548
549 if( mw->pix == NULL )
550 return;
551
552 main_win_fit_size( mw, mw->scroll->allocation.width, mw->scroll->allocation.height, can_strech, type );
553 }
554
555 GtkWidget* add_nav_btn( MainWin* mw, const char* icon, const char* tip, GCallback cb, gboolean toggle)
556 {
557 GtkWidget* unused;
558 return add_nav_btn_img(mw, icon, tip, cb, toggle, &unused);
559 }
560
561 GtkWidget* add_nav_btn_img( MainWin* mw, const char* icon, const char* tip, GCallback cb, gboolean toggle, GtkWidget** ret_img )
562 {
563 GtkWidget* img;
564 if( g_str_has_prefix(icon, "gtk-") )
565 img = gtk_image_new_from_stock(icon, GTK_ICON_SIZE_SMALL_TOOLBAR);
566 else
567 img = gtk_image_new_from_icon_name(icon, GTK_ICON_SIZE_SMALL_TOOLBAR);
568 GtkWidget* btn;
569 if( G_UNLIKELY(toggle) )
570 {
571 btn = gtk_toggle_button_new();
572 g_signal_connect( btn, "toggled", cb, mw );
573 }
574 else
575 {
576 btn = gtk_button_new();
577 g_signal_connect( btn, "clicked", cb, mw );
578 }
579 gtk_button_set_relief( (GtkButton*)btn, GTK_RELIEF_NONE );
580 gtk_button_set_focus_on_click( (GtkButton*)btn, FALSE );
581 gtk_container_add( (GtkContainer*)btn, img );
582 gtk_widget_set_tooltip_text( btn, tip );
583 gtk_box_pack_start( (GtkBox*)mw->nav_bar, btn, FALSE, FALSE, 0 );
584 *ret_img = img;
585 return btn;
586 }
587
588 void on_zoom_fit_menu( GtkMenuItem* item, MainWin* mw )
589 {
590 gtk_button_clicked( (GtkButton*)mw->btn_fit );
591 }
592
593 void on_zoom_fit( GtkToggleButton* btn, MainWin* mw )
594 {
595 if( ! btn->active )
596 {
597 if( mw->zoom_mode == ZOOM_FIT )
598 gtk_toggle_button_set_active( btn, TRUE );
599 return;
600 }
601 gtk_toggle_button_set_active( (GtkToggleButton*)mw->btn_orig, FALSE );
602
603 main_win_fit_window_size( mw, FALSE, GDK_INTERP_BILINEAR );
604 }
605
606 void on_full_screen( GtkWidget* btn, MainWin* mw )
607 {
608 if( ! mw->full_screen )
609 gtk_window_fullscreen( (GtkWindow*)mw );
610 else
611 gtk_window_unfullscreen( (GtkWindow*)mw );
612 }
613
614 void on_orig_size_menu( GtkToggleButton* btn, MainWin* mw )
615 {
616 gtk_button_clicked( (GtkButton*)mw->btn_orig );
617 }
618
619 void on_orig_size( GtkToggleButton* btn, MainWin* mw )
620 {
621 // this callback could be called from activate signal of menu item.
622 if( GTK_IS_MENU_ITEM(btn) )
623 {
624 gtk_button_clicked( (GtkButton*)mw->btn_orig );
625 return;
626 }
627
628 if( ! btn->active )
629 {
630 if( mw->zoom_mode == ZOOM_ORIG )
631 gtk_toggle_button_set_active( btn, TRUE );
632 return;
633 }
634 mw->zoom_mode = ZOOM_ORIG;
635 mw->scale = 1.0;
636 // gtk_scrolled_window_set_policy( (GtkScrolledWindow*)mw->scroll,
637 // GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
638
639 gtk_toggle_button_set_active( (GtkToggleButton*)mw->btn_fit, FALSE );
640
641 if( ! mw->pix )
642 return;
643
644 // update scale
645 update_title(NULL, mw);
646
647 image_view_set_scale( (ImageView*)mw->img_view, 1.0, GDK_INTERP_BILINEAR );
648
649 while (gtk_events_pending ())
650 gtk_main_iteration ();
651
652 main_win_center_image( mw ); // FIXME: mw doesn't work well. Why?
653 }
654
655 void on_prev( GtkWidget* btn, MainWin* mw )
656 {
657 const char* name;
658 if( image_list_is_empty( mw->img_list ) )
659 return;
660
661 name = image_list_get_prev( mw->img_list );
662
663 if( ! name && image_list_has_multiple_files( mw->img_list ) )
664 {
665 // FIXME: need to ask user first?
666 name = image_list_get_last( mw->img_list );
667 }
668
669 if( name )
670 {
671 char* file_path = image_list_get_current_file_path( mw->img_list );
672 main_win_open( mw, file_path, ZOOM_FIT );
673 g_free( file_path );
674 }
675 }
676
677 void on_next( GtkWidget* btn, MainWin* mw )
678 {
679 if( image_list_is_empty( mw->img_list ) )
680 return;
681
682 const char* name = image_list_get_next( mw->img_list );
683
684 if( ! name && image_list_has_multiple_files( mw->img_list ) )
685 {
686 // FIXME: need to ask user first?
687 name = image_list_get_first( mw->img_list );
688 }
689
690 if( name )
691 {
692 char* file_path = image_list_get_current_file_path( mw->img_list );
693 main_win_open( mw, file_path, ZOOM_FIT );
694 g_free( file_path );
695 }
696 }
697
698 void cancel_slideshow(MainWin* mw)
699 {
700 mw->slideshow_cancelled = TRUE;
701 mw->slideshow_running = FALSE;
702 if (mw->slide_timeout != 0)
703 g_source_remove(mw->slide_timeout);
704 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(mw->btn_play_stop), FALSE );
705 }
706
707 gboolean next_slide(MainWin* mw)
708 {
709 /* Timeout causes an implicit "Next". */
710 if (mw->slideshow_running)
711 on_next( NULL, mw );
712
713 return mw->slideshow_running;
714 }
715
716 void on_slideshow_menu( GtkMenuItem* item, MainWin* mw )
717 {
718 gtk_button_clicked( (GtkButton*)mw->btn_play_stop );
719 }
720
721 void on_slideshow( GtkToggleButton* btn, MainWin* mw )
722 {
723 if ((mw->slideshow_running) || (mw->slideshow_cancelled))
724 {
725 mw->slideshow_running = FALSE;
726 mw->slideshow_cancelled = FALSE;
727 gtk_image_set_from_stock( GTK_IMAGE(mw->img_play_stop), GTK_STOCK_MEDIA_PLAY, GTK_ICON_SIZE_SMALL_TOOLBAR );
728 gtk_widget_set_tooltip_text( GTK_WIDGET(btn), _("Start Slideshow") );
729 gtk_toggle_button_set_active( btn, FALSE );
730 }
731 else
732 {
733 gtk_toggle_button_set_active( btn, TRUE );
734 mw->slideshow_running = TRUE;
735 gtk_image_set_from_stock( GTK_IMAGE(mw->img_play_stop), GTK_STOCK_MEDIA_STOP, GTK_ICON_SIZE_SMALL_TOOLBAR );
736 gtk_widget_set_tooltip_text( GTK_WIDGET(btn), _("Stop Slideshow") );
737 mw->slide_timeout = g_timeout_add(1000 * pref.slide_delay, (GSourceFunc) next_slide, mw);
738 }
739 }
740
741 //////////////////// rotate & flip
742
743 static int trans_angle_to_id(int i)
744 {
745 if(i == 0) return 1;
746 else if(i == 90) return 6;
747 else if(i == 180) return 3;
748 else if(i == 270) return 8;
749 else if(i == -45) return 7;
750 else if(i == -90) return 2;
751 else if(i == -135) return 5;
752 else /* -180 */ return 4;
753 }
754
755 static int get_new_angle( int orig_angle, int rotate_angle )
756 {
757 // defined in exif.c
758 extern int ExifRotateFlipMapping[9][9];
759 static int angle_trans_back[] = {0, 0, -90, 180, -180, -135, 90, -45, 270};
760
761 orig_angle = trans_angle_to_id(orig_angle);
762 rotate_angle = trans_angle_to_id(rotate_angle);
763
764 return angle_trans_back[ ExifRotateFlipMapping[orig_angle][rotate_angle] ];
765 }
766
767 void on_rotate_auto_save( GtkWidget* btn, MainWin* mw )
768 {
769 if(pref.auto_save_rotated){
770 // gboolean ask_before_save = pref.ask_before_save;
771 // pref.ask_before_save = FALSE;
772 on_save(btn,mw);
773 // pref.ask_before_save = ask_before_save;
774 }
775 }
776
777 void on_rotate_clockwise( GtkWidget* btn, MainWin* mw )
778 {
779 cancel_slideshow(mw);
780 rotate_image( mw, GDK_PIXBUF_ROTATE_CLOCKWISE );
781 mw->rotation_angle = get_new_angle(mw->rotation_angle, 90);
782 on_rotate_auto_save(btn, mw);
783 }
784
785 void on_rotate_counterclockwise( GtkWidget* btn, MainWin* mw )
786 {
787 cancel_slideshow(mw);
788 rotate_image( mw, GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE );
789 mw->rotation_angle = get_new_angle(mw->rotation_angle, 270);
790 on_rotate_auto_save(btn, mw);
791 }
792
793 void on_flip_vertical( GtkWidget* btn, MainWin* mw )
794 {
795 cancel_slideshow(mw);
796 rotate_image( mw, -180 );
797 mw->rotation_angle = get_new_angle(mw->rotation_angle, -180);
798 on_rotate_auto_save(btn, mw);
799 }
800
801 void on_flip_horizontal( GtkWidget* btn, MainWin* mw )
802 {
803 cancel_slideshow(mw);
804 rotate_image( mw, -90 );
805 mw->rotation_angle = get_new_angle(mw->rotation_angle, -90);
806 on_rotate_auto_save(btn, mw);
807 }
808
809 /* end of rotate & flip */
810
811 void on_save_as( GtkWidget* btn, MainWin* mw )
812 {
813 char *file, *type;
814
815 cancel_slideshow(mw);
816 if( ! mw->pix )
817 return;
818
819 file = get_save_filename( GTK_WINDOW(mw), image_list_get_dir( mw->img_list ), &type );
820 if( file )
821 {
822 char* dir;
823 main_win_save( mw, file, type, TRUE );
824 dir = g_path_get_dirname(file);
825 const char* name = file + strlen(dir) + 1;
826
827 if( strcmp( image_list_get_dir(mw->img_list), dir ) == 0 )
828 {
829 /* if the saved file is located in the same dir */
830 /* simply add it to image list */
831 image_list_add_sorted( mw->img_list, name, TRUE );
832 }
833 else /* otherwise reload the whole image list. */
834 {
835 /* switch to the dir containing the saved file. */
836 image_list_open_dir( mw->img_list, dir, NULL );
837 }
838 update_title( name, mw );
839 g_free( dir );
840 g_free( file );
841 g_free( type );
842 }
843 }
844
845 void on_save( GtkWidget* btn, MainWin* mw )
846 {
847 cancel_slideshow(mw);
848 if( ! mw->pix )
849 return;
850
851 char* file_name = g_build_filename( image_list_get_dir( mw->img_list ),
852 image_list_get_current( mw->img_list ), NULL );
853 GdkPixbufFormat* info;
854 info = gdk_pixbuf_get_file_info( file_name, NULL, NULL );
855 char* type = gdk_pixbuf_format_get_name( info );
856
857 /* Confirm save if requested. */
858 if ((pref.ask_before_save) && ( ! save_confirm(mw, file_name)))
859 return;
860
861 if(strcmp(type,"jpeg")==0)
862 {
863 if(!pref.rotate_exif_only || ExifRotate(file_name, mw->rotation_angle) == FALSE)
864 {
865 // hialan notes:
866 // ExifRotate retrun FALSE when
867 // 1. Can not read file
868 // 2. Exif do not have TAG_ORIENTATION tag
869 // 3. Format unknown
870 // And then we apply rotate_and_save_jpeg_lossless() ,
871 // the result would not effected by EXIF Orientation...
872 #ifdef HAVE_LIBJPEG
873 int status = rotate_and_save_jpeg_lossless(file_name,mw->rotation_angle);
874 if(status != 0)
875 {
876 main_win_show_error( mw, g_strerror(status) );
877 }
878 #else
879 main_win_save( mw, file_name, type, pref.ask_before_save );
880 #endif
881 }
882 } else
883 main_win_save( mw, file_name, type, pref.ask_before_save );
884 mw->rotation_angle = 0;
885 g_free( file_name );
886 g_free( type );
887 }
888
889 void on_open( GtkWidget* btn, MainWin* mw )
890 {
891 cancel_slideshow(mw);
892 char* file = get_open_filename( (GtkWindow*)mw, image_list_get_dir( mw->img_list ) );
893 if( file )
894 {
895 main_win_open( mw, file, ZOOM_NONE );
896 g_free( file );
897 }
898 }
899
900 void on_zoom_in( GtkWidget* btn, MainWin* mw )
901 {
902 mw->zoom_mode = ZOOM_SCALE;
903 gtk_toggle_button_set_active( (GtkToggleButton*)mw->btn_fit, FALSE );
904 gtk_toggle_button_set_active( (GtkToggleButton*)mw->btn_orig, FALSE );
905 // gtk_scrolled_window_set_policy( (GtkScrolledWindow*)mw->scroll,
906 // GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
907
908 double scale = mw->scale;
909 if( mw->pix && scale < 20.0 )
910 {
911 // busy(TRUE);
912 scale *= 1.05;
913 if( scale > 20.0 )
914 scale = 20.0;
915 if( mw->scale != scale )
916 main_win_scale_image( mw, scale, GDK_INTERP_BILINEAR );
917 // adjust_adjustment_on_zoom(oldscale);
918 // busy(FALSE);
919 }
920 }
921
922 void on_zoom_out( GtkWidget* btn, MainWin* mw )
923 {
924 mw->zoom_mode = ZOOM_SCALE;
925 gtk_toggle_button_set_active( (GtkToggleButton*)mw->btn_fit, FALSE );
926 gtk_toggle_button_set_active( (GtkToggleButton*)mw->btn_orig, FALSE );
927 // gtk_scrolled_window_set_policy( (GtkScrolledWindow*)mw->scroll,
928 // GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
929
930 double scale = mw->scale;
931 if( mw->pix && scale > 0.02 )
932 {
933 // busy(TRUE);
934
935 scale /= 1.05;
936 if( scale < 0.02 )
937 scale = 0.02;
938 if( mw->scale != scale )
939 main_win_scale_image( mw, scale, GDK_INTERP_BILINEAR );
940 // adjust_adjustment_on_zoom(oldscale);
941 // busy(FALSE);
942 }
943 }
944
945 void on_preference( GtkWidget* btn, MainWin* mw )
946 {
947 edit_preferences( (GtkWindow*)mw );
948 }
949
950 void on_quit( GtkWidget* btn, MainWin* mw )
951 {
952 cancel_slideshow(mw);
953 gtk_widget_destroy( (GtkWidget*)mw );
954 }
955
956 gboolean on_button_press( GtkWidget* widget, GdkEventButton* evt, MainWin* mw )
957 {
958 if( ! GTK_WIDGET_HAS_FOCUS( widget ) )
959 gtk_widget_grab_focus( widget );
960
961 if( evt->type == GDK_BUTTON_PRESS)
962 {
963 if( evt->button == 1 ) // left button
964 {
965 if( ! mw->pix )
966 return FALSE;
967 mw->dragging = TRUE;
968 gtk_widget_get_pointer( (GtkWidget*)mw, &mw->drag_old_x ,&mw->drag_old_y );
969 gdk_window_set_cursor( widget->window, mw->hand_cursor );
970 }
971 else if( evt->button == 3 ) // right button
972 {
973 show_popup_menu( mw, evt );
974 }
975 }
976 else if( evt->type == GDK_2BUTTON_PRESS && evt->button == 1 ) // double clicked
977 {
978 on_full_screen( NULL, mw );
979 }
980 return FALSE;
981 }
982
983 gboolean on_mouse_move( GtkWidget* widget, GdkEventMotion* evt, MainWin* mw )
984 {
985 if( ! mw->dragging )
986 return FALSE;
987
988 int cur_x, cur_y;
989 gtk_widget_get_pointer( (GtkWidget*)mw, &cur_x ,&cur_y );
990
991 int dx = (mw->drag_old_x - cur_x);
992 int dy = (mw->drag_old_y - cur_y);
993
994 GtkAdjustment *hadj, *vadj;
995 hadj = gtk_scrolled_window_get_hadjustment((GtkScrolledWindow*)mw->scroll);
996 vadj = gtk_scrolled_window_get_vadjustment((GtkScrolledWindow*)mw->scroll);
997
998 GtkRequisition req;
999 gtk_widget_size_request( (GtkWidget*)mw->img_view, &req );
1000
1001 if( ABS(dx) > 4 )
1002 {
1003 mw->drag_old_x = cur_x;
1004 if( req.width > hadj->page_size )
1005 {
1006 gdouble x = gtk_adjustment_get_value (hadj) + dx;
1007 if( x < hadj->lower )
1008 x = hadj->lower;
1009 else if( (x + hadj->page_size) > hadj->upper )
1010 x = hadj->upper - hadj->page_size;
1011
1012 if( x != hadj->value )
1013 gtk_adjustment_set_value (hadj, x );
1014 }
1015 }
1016
1017 if( ABS(dy) > 4 )
1018 {
1019 if( req.height > vadj->page_size )
1020 {
1021 mw->drag_old_y = cur_y;
1022 gdouble y = gtk_adjustment_get_value (vadj) + dy;
1023 if( y < vadj->lower )
1024 y = vadj->lower;
1025 else if( (y + vadj->page_size) > vadj->upper )
1026 y = vadj->upper - vadj->page_size;
1027
1028 if( y != vadj->value )
1029 gtk_adjustment_set_value (vadj, y );
1030 }
1031 }
1032 return FALSE;
1033 }
1034
1035 gboolean on_button_release( GtkWidget* widget, GdkEventButton* evt, MainWin* mw )
1036 {
1037 mw->dragging = FALSE;
1038 gdk_window_set_cursor( widget->window, NULL );
1039 return FALSE;
1040 }
1041
1042 gboolean on_scroll_event( GtkWidget* widget, GdkEventScroll* evt, MainWin* mw )
1043 {
1044 guint modifiers = gtk_accelerator_get_default_mod_mask();
1045 switch( evt->direction )
1046 {
1047 case GDK_SCROLL_UP:
1048 if ((evt->state & modifiers) == GDK_CONTROL_MASK)
1049 on_zoom_in( NULL, mw );
1050 else
1051 on_prev( NULL, mw );
1052 break;
1053 case GDK_SCROLL_DOWN:
1054 if ((evt->state & modifiers) == GDK_CONTROL_MASK)
1055 on_zoom_out( NULL, mw );
1056 else
1057 on_next( NULL, mw );
1058 break;
1059 case GDK_SCROLL_LEFT:
1060 if( gtk_widget_get_default_direction () == GTK_TEXT_DIR_RTL )
1061 on_next( NULL, mw );
1062 else
1063 on_prev( NULL, mw );
1064 break;
1065 case GDK_SCROLL_RIGHT:
1066 if( gtk_widget_get_default_direction () == GTK_TEXT_DIR_RTL )
1067 on_prev( NULL, mw );
1068 else
1069 on_next( NULL, mw );
1070 break;
1071 }
1072 return TRUE;
1073 }
1074
1075 gboolean on_key_press_event(GtkWidget* widget, GdkEventKey * key)
1076 {
1077 MainWin* mw = (MainWin*)widget;
1078 switch( key->keyval )
1079 {
1080 case GDK_Right:
1081 case GDK_KP_Right:
1082 case GDK_rightarrow:
1083 if( gtk_widget_get_default_direction () == GTK_TEXT_DIR_RTL )
1084 on_prev( NULL, mw );
1085 else
1086 on_next( NULL, mw );
1087 break;
1088 case GDK_Return:
1089 case GDK_space:
1090 case GDK_Next:
1091 case GDK_KP_Down:
1092 case GDK_Down:
1093 case GDK_downarrow:
1094 on_next( NULL, mw );
1095 break;
1096 case GDK_Left:
1097 case GDK_KP_Left:
1098 case GDK_leftarrow:
1099 if( gtk_widget_get_default_direction () == GTK_TEXT_DIR_RTL )
1100 on_next( NULL, mw );
1101 else
1102 on_prev( NULL, mw );
1103 break;
1104 case GDK_Prior:
1105 case GDK_BackSpace:
1106 case GDK_KP_Up:
1107 case GDK_Up:
1108 case GDK_uparrow:
1109 on_prev( NULL, mw );
1110 break;
1111 case GDK_w:
1112 case GDK_W:
1113 on_slideshow_menu( NULL, mw );
1114 break;
1115 case GDK_KP_Add:
1116 case GDK_plus:
1117 case GDK_equal:
1118 on_zoom_in( NULL, mw );
1119 break;
1120 case GDK_KP_Subtract:
1121 case GDK_minus:
1122 on_zoom_out( NULL, mw );
1123 break;
1124 case GDK_s:
1125 case GDK_S:
1126 on_save( NULL, mw );
1127 break;
1128 case GDK_a:
1129 case GDK_A:
1130 on_save_as( NULL, mw );
1131 break;
1132 case GDK_l:
1133 case GDK_L:
1134 on_rotate_counterclockwise( NULL, mw );
1135 break;
1136 case GDK_r:
1137 case GDK_R:
1138 on_rotate_clockwise( NULL, mw );
1139 break;
1140 case GDK_f:
1141 case GDK_F:
1142 if( mw->zoom_mode != ZOOM_FIT )
1143 gtk_button_clicked((GtkButton*)mw->btn_fit );
1144 break;
1145 case GDK_g:
1146 case GDK_G:
1147 if( mw->zoom_mode != ZOOM_ORIG )
1148 gtk_button_clicked((GtkButton*)mw->btn_orig );
1149 break;
1150 case GDK_h:
1151 case GDK_H:
1152 on_flip_horizontal( NULL, mw );
1153 break;
1154 case GDK_v:
1155 case GDK_V:
1156 on_flip_vertical( NULL, mw );
1157 break;
1158 case GDK_o:
1159 case GDK_O:
1160 on_open( NULL, mw );
1161 break;
1162 case GDK_Delete:
1163 case GDK_d:
1164 case GDK_D:
1165 on_delete( NULL, mw );
1166 break;
1167 case GDK_p:
1168 case GDK_P:
1169 on_preference( NULL, mw );
1170 break;
1171 case GDK_Escape:
1172 if( mw->full_screen )
1173 on_full_screen( NULL, mw );
1174 else
1175 on_quit( NULL, mw );
1176 break;
1177 case GDK_q:
1178 case GDK_Q:
1179 on_quit( NULL, mw );
1180 break;
1181 case GDK_F11:
1182 on_full_screen( NULL, mw );
1183 break;
1184
1185 default:
1186 GTK_WIDGET_CLASS(main_win_parent_class)->key_press_event( widget, key );
1187 }
1188 return FALSE;
1189 }
1190
1191 void main_win_center_image( MainWin* mw )
1192 {
1193 GtkAdjustment *hadj, *vadj;
1194 hadj = gtk_scrolled_window_get_hadjustment((GtkScrolledWindow*)mw->scroll);
1195 vadj = gtk_scrolled_window_get_vadjustment((GtkScrolledWindow*)mw->scroll);
1196
1197 GtkRequisition req;
1198 gtk_widget_size_request( (GtkWidget*)mw->img_view, &req );
1199
1200 if( req.width > hadj->page_size )
1201 gtk_adjustment_set_value(hadj, ( hadj->upper - hadj->page_size ) / 2 );
1202
1203 if( req.height > vadj->page_size )
1204 gtk_adjustment_set_value(vadj, ( vadj->upper - vadj->page_size ) / 2 );
1205 }
1206
1207 void rotate_image( MainWin* mw, int angle )
1208 {
1209 GdkPixbuf* rpix = NULL;
1210
1211 if( ! mw->pix )
1212 return;
1213
1214 if(angle > 0)
1215 {
1216 rpix = gdk_pixbuf_rotate_simple( mw->pix, angle );
1217 }
1218 else
1219 {
1220 if(angle == -90)
1221 rpix = gdk_pixbuf_flip( mw->pix, TRUE );
1222 else if(angle == -180)
1223 rpix = gdk_pixbuf_flip( mw->pix, FALSE );
1224 }
1225
1226 if (!rpix) {
1227 return;
1228 }
1229
1230 g_object_unref( mw->pix );
1231
1232 mw->pix = rpix;
1233 image_view_set_pixbuf( (ImageView*)mw->img_view, mw->pix );
1234
1235 if( mw->zoom_mode == ZOOM_FIT )
1236 main_win_fit_window_size( mw, FALSE, GDK_INTERP_BILINEAR );
1237 }
1238
1239 gboolean main_win_scale_image( MainWin* mw, double new_scale, GdkInterpType type )
1240 {
1241 if( G_UNLIKELY( new_scale == 1.0 ) )
1242 {
1243 gtk_toggle_button_set_active( (GtkToggleButton*)mw->btn_orig, TRUE );
1244 mw->scale = 1.0;
1245 return TRUE;
1246 }
1247 mw->scale = new_scale;
1248 image_view_set_scale( (ImageView*)mw->img_view, new_scale, type );
1249
1250 update_title( NULL, mw );
1251
1252 return TRUE;
1253 }
1254
1255 gboolean save_confirm( MainWin* mw, const char* file_path )
1256 {
1257 if( g_file_test( file_path, G_FILE_TEST_EXISTS ) )
1258 {
1259 GtkWidget* dlg = gtk_message_dialog_new( (GtkWindow*)mw,
1260 GTK_DIALOG_MODAL,
1261 GTK_MESSAGE_QUESTION,
1262 GTK_BUTTONS_YES_NO,
1263 _("The file name you selected already exists.\nDo you want to overwrite existing file?\n(Warning: The quality of original image might be lost)") );
1264 if( gtk_dialog_run( (GtkDialog*)dlg ) != GTK_RESPONSE_YES )
1265 {
1266 gtk_widget_destroy( dlg );
1267 return FALSE;
1268 }
1269 gtk_widget_destroy( dlg );
1270 }
1271 return TRUE;
1272 }
1273
1274 gboolean main_win_save( MainWin* mw, const char* file_path, const char* type, gboolean confirm )
1275 {
1276 gboolean result1,gdk_save_supported;
1277 GSList *gdk_formats;
1278 GSList *gdk_formats_i;
1279 if( ! mw->pix )
1280 return FALSE;
1281
1282 /* detect if the current type can be save by gdk_pixbuf_save() */
1283 gdk_save_supported = FALSE;
1284 gdk_formats = gdk_pixbuf_get_formats();
1285 for (gdk_formats_i = gdk_formats; gdk_formats_i;
1286 gdk_formats_i = g_slist_next(gdk_formats_i))
1287 {
1288 GdkPixbufFormat *data;
1289 data = gdk_formats_i->data;
1290 if (gdk_pixbuf_format_is_writable(data))
1291 {
1292 if ( strcmp(type, gdk_pixbuf_format_get_name(data))==0)
1293 {
1294 gdk_save_supported = TRUE;
1295 break;
1296 }
1297 }
1298 }
1299 g_slist_free (gdk_formats);
1300
1301 GError* err = NULL;
1302 if (!gdk_save_supported)
1303 {
1304 main_win_show_error( mw, _("Writing this image format is not supported.") );
1305 return FALSE;
1306 }
1307 if( strcmp( type, "jpeg" ) == 0 )
1308 {
1309 char tmp[32];
1310 g_sprintf(tmp, "%d", pref.jpg_quality);
1311 result1 = gdk_pixbuf_save( mw->pix, file_path, type, &err, "quality", tmp, NULL );
1312 }
1313 else if( strcmp( type, "png" ) == 0 )
1314 {
1315 char tmp[32];
1316 g_sprintf(tmp, "%d", pref.png_compression);
1317 result1 = gdk_pixbuf_save( mw->pix, file_path, type, &err, "compression", tmp, NULL );
1318 }
1319 else
1320 result1 = gdk_pixbuf_save( mw->pix, file_path, type, &err, NULL );
1321 if( ! result1 )
1322 {
1323 main_win_show_error( mw, err->message );
1324 return FALSE;
1325 }
1326 return TRUE;
1327 }
1328
1329 void on_delete( GtkWidget* btn, MainWin* mw )
1330 {
1331 cancel_slideshow(mw);
1332 char* file_path = image_list_get_current_file_path( mw->img_list );
1333 if( file_path )
1334 {
1335 int resp = GTK_RESPONSE_YES;
1336 if ( pref.ask_before_delete )
1337 {
1338 GtkWidget* dlg = gtk_message_dialog_new( (GtkWindow*)mw,
1339 GTK_DIALOG_MODAL,
1340 GTK_MESSAGE_QUESTION,
1341 GTK_BUTTONS_YES_NO,
1342 _("Are you sure you want to delete current file?\n\nWarning: Once deleted, the file cannot be recovered.") );
1343 resp = gtk_dialog_run( (GtkDialog*)dlg );
1344 gtk_widget_destroy( dlg );
1345 }
1346
1347 if( resp == GTK_RESPONSE_YES )
1348 {
1349 const char* name = image_list_get_current( mw->img_list );
1350
1351 if( g_unlink( file_path ) != 0 )
1352 main_win_show_error( mw, g_strerror(errno) );
1353 else
1354 {
1355 const char* next_name = image_list_get_next( mw->img_list );
1356 if( ! next_name )
1357 next_name = image_list_get_prev( mw->img_list );
1358
1359 if( next_name )
1360 {
1361 char* next_file_path = image_list_get_current_file_path( mw->img_list );
1362 main_win_open( mw, next_file_path, ZOOM_FIT );
1363 g_free( next_file_path );
1364 }
1365
1366 image_list_remove ( mw->img_list, name );
1367
1368 if ( ! next_name )
1369 {
1370 main_win_close( mw );
1371 image_list_close( mw->img_list );
1372 image_view_set_pixbuf( (ImageView*)mw->img_view, NULL );
1373 gtk_window_set_title( (GtkWindow*) mw, _("Image Viewer"));
1374 }
1375 }
1376 }
1377 g_free( file_path );
1378 }
1379 }
1380
1381 void show_popup_menu( MainWin* mw, GdkEventButton* evt )
1382 {
1383 static PtkMenuItemEntry menu_def[] =
1384 {
1385 PTK_IMG_MENU_ITEM( N_( "Previous" ), GTK_STOCK_GO_BACK, on_prev, GDK_leftarrow, 0 ),
1386 PTK_IMG_MENU_ITEM( N_( "Next" ), GTK_STOCK_GO_FORWARD, on_next, GDK_rightarrow, 0 ),
1387 PTK_IMG_MENU_ITEM( N_( "Start/Stop Slideshow" ), GTK_STOCK_MEDIA_PLAY, on_slideshow_menu, GDK_W, 0 ),
1388 PTK_SEPARATOR_MENU_ITEM,
1389 PTK_IMG_MENU_ITEM( N_( "Zoom Out" ), GTK_STOCK_ZOOM_OUT, on_zoom_out, GDK_minus, 0 ),
1390 PTK_IMG_MENU_ITEM( N_( "Zoom In" ), GTK_STOCK_ZOOM_IN, on_zoom_in, GDK_plus, 0 ),
1391 PTK_IMG_MENU_ITEM( N_( "Fit Image To Window Size" ), GTK_STOCK_ZOOM_FIT, on_zoom_fit_menu, GDK_F, 0 ),
1392 PTK_IMG_MENU_ITEM( N_( "Original Size" ), GTK_STOCK_ZOOM_100, on_orig_size_menu, GDK_G, 0 ),
1393 PTK_SEPARATOR_MENU_ITEM,
1394 PTK_IMG_MENU_ITEM( N_( "Full Screen" ), GTK_STOCK_FULLSCREEN, on_full_screen, GDK_F11, 0 ),
1395 PTK_SEPARATOR_MENU_ITEM,
1396 PTK_IMG_MENU_ITEM( N_( "Rotate Counterclockwise" ), "object-rotate-left", on_rotate_counterclockwise, GDK_L, 0 ),
1397 PTK_IMG_MENU_ITEM( N_( "Rotate Clockwise" ), "object-rotate-right", on_rotate_clockwise, GDK_R, 0 ),
1398 PTK_IMG_MENU_ITEM( N_( "Flip Horizontal" ), "object-flip-horizontal", on_flip_horizontal, GDK_H, 0 ),
1399 PTK_IMG_MENU_ITEM( N_( "Flip Vertical" ), "object-flip-vertical", on_flip_vertical, GDK_V, 0 ),
1400 PTK_SEPARATOR_MENU_ITEM,
1401 PTK_IMG_MENU_ITEM( N_("Open File"), GTK_STOCK_OPEN, G_CALLBACK(on_open), GDK_O, 0 ),
1402 PTK_IMG_MENU_ITEM( N_("Save File"), GTK_STOCK_SAVE, G_CALLBACK(on_save), GDK_S, 0 ),
1403 PTK_IMG_MENU_ITEM( N_("Save As"), GTK_STOCK_SAVE_AS, G_CALLBACK(on_save_as), GDK_A, 0 ),
1404 // PTK_IMG_MENU_ITEM( N_("Save As Other Size"), GTK_STOCK_SAVE_AS, G_CALLBACK(on_save_as), GDK_A, 0 ),
1405 PTK_IMG_MENU_ITEM( N_("Delete File"), GTK_STOCK_DELETE, G_CALLBACK(on_delete), GDK_Delete, 0 ),
1406 PTK_SEPARATOR_MENU_ITEM,
1407 PTK_IMG_MENU_ITEM( N_("Preferences"), GTK_STOCK_PREFERENCES, G_CALLBACK(on_preference), GDK_P, 0 ),
1408 PTK_STOCK_MENU_ITEM( GTK_STOCK_ABOUT, on_about ),
1409 PTK_SEPARATOR_MENU_ITEM,
1410 PTK_IMG_MENU_ITEM( N_("Quit"), GTK_STOCK_QUIT, G_CALLBACK(on_quit), GDK_Q, 0 ),
1411 PTK_MENU_END
1412 };
1413 GtkWidget* rotate_cw;
1414 GtkWidget* rotate_ccw;
1415 GtkWidget* flip_v;
1416 GtkWidget* flip_h;
1417
1418 menu_def[10].ret = &rotate_ccw;
1419 menu_def[11].ret = &rotate_cw;
1420 menu_def[12].ret = &flip_h;
1421 menu_def[13].ret = &flip_v;
1422
1423 // mw accel group is useless. It's only used to display accels in popup menu
1424 GtkAccelGroup* accel_group = gtk_accel_group_new();
1425 GtkMenuShell* popup = (GtkMenuShell*)ptk_menu_new_from_data( menu_def, mw, accel_group );
1426
1427 if( mw->animation )
1428 {
1429 gtk_widget_set_sensitive( rotate_ccw, FALSE );
1430 gtk_widget_set_sensitive( rotate_cw, FALSE );
1431 gtk_widget_set_sensitive( flip_h, FALSE );
1432 gtk_widget_set_sensitive( flip_v, FALSE );
1433 }
1434
1435 gtk_widget_show_all( (GtkWidget*)popup );
1436 g_signal_connect( popup, "selection-done", G_CALLBACK(gtk_widget_destroy), NULL );
1437 gtk_menu_popup( (GtkMenu*)popup, NULL, NULL, NULL, NULL, evt->button, evt->time );
1438 }
1439
1440 /* callback used to open default browser when URLs got clicked */
1441 static void open_url( GtkAboutDialog *dlg, const gchar *url, gpointer data)
1442 {
1443 /* FIXME: is there any better way to do this? */
1444 char* programs[] = { "xdg-open", "gnome-open", "exo-open" };
1445 int i;
1446 for( i = 0; i < G_N_ELEMENTS(programs); ++i)
1447 {
1448 gchar* open_cmd = NULL;
1449 if( (open_cmd = g_find_program_in_path( programs[i] )) )
1450 {
1451 char* argv [3];
1452 argv [0] = programs[i];
1453 argv [1] = (gchar *) url;
1454 argv [2] = NULL;
1455 g_spawn_async (NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL);
1456 g_free( open_cmd );
1457 break;
1458 }
1459 }
1460 }
1461
1462 void on_about( GtkWidget* menu, MainWin* mw )
1463 {
1464 GtkWidget * about_dlg;
1465 const gchar *authors[] =
1466 {
1467 "洪任諭 Hong Jen Yee <pcman.tw@gmail.com>",
1468 "Martin Siggel <martinsiggel@googlemail.com>",
1469 "Hialan Liu <hialan.liu@gmail.com>",
1470 "Marty Jack <martyj19@comcast.net>",
1471 "Louis Casillas <oxaric@gmail.com>",
1472 "Will Davies",
1473 _(" * Refer to source code of EOG image viewer and GThumb"),
1474 NULL
1475 };
1476 /* TRANSLATORS: Replace this string with your names, one name per line. */
1477 gchar *translators = _( "translator-credits" );
1478
1479 gtk_about_dialog_set_url_hook( open_url, mw, NULL);
1480
1481 about_dlg = gtk_about_dialog_new ();
1482
1483 gtk_container_set_border_width ( ( GtkContainer*)about_dlg , 2 );
1484 gtk_about_dialog_set_version ( (GtkAboutDialog*)about_dlg, VERSION );
1485 gtk_about_dialog_set_name ( (GtkAboutDialog*)about_dlg, _( "GPicView" ) );
1486 gtk_about_dialog_set_logo( (GtkAboutDialog*)about_dlg, gdk_pixbuf_new_from_file( PACKAGE_DATA_DIR"/pixmaps/gpicview.png", NULL ) );
1487 gtk_about_dialog_set_copyright ( (GtkAboutDialog*)about_dlg, _( "Copyright (C) 2007 - 2009" ) );
1488 gtk_about_dialog_set_comments ( (GtkAboutDialog*)about_dlg, _( "Lightweight image viewer from LXDE project" ) );
1489 gtk_about_dialog_set_license ( (GtkAboutDialog*)about_dlg, "GPicView\n\nCopyright (C) 2007 Hong Jen Yee (PCMan)\n\nThis program is free software; you can redistribute it and/or\nmodify it under the terms of the GNU General Public License\nas published by the Free Software Foundation; either version 2\nof the License, or (at your option) any later version.\n\nThis program is distributed in the hope that it will be useful,\nbut WITHOUT ANY WARRANTY; without even the implied warranty of\nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\nGNU General Public License for more details.\n\nYou should have received a copy of the GNU General Public License\nalong with this program; if not, write to the Free Software\nFoundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA." );
1490 gtk_about_dialog_set_website ( (GtkAboutDialog*)about_dlg, "http://wiki.lxde.org/en/GPicView" );
1491 gtk_about_dialog_set_authors ( (GtkAboutDialog*)about_dlg, authors );
1492 gtk_about_dialog_set_translator_credits ( (GtkAboutDialog*)about_dlg, translators );
1493 gtk_window_set_transient_for( (GtkWindow*) about_dlg, GTK_WINDOW( mw ) );
1494
1495 gtk_dialog_run( ( GtkDialog*)about_dlg );
1496 gtk_widget_destroy( about_dlg );
1497 }
1498
1499 void on_drag_data_received( GtkWidget* widget, GdkDragContext *drag_context,
1500 int x, int y, GtkSelectionData* data, guint info, guint time, MainWin* mw )
1501 {
1502 if( ! data || data->length <= 0)
1503 return;
1504
1505 char* file = NULL;
1506 if( info == 0 ) // text/uri-list
1507 {
1508 char** uris = gtk_selection_data_get_uris( data );
1509 if( uris )
1510 {
1511 file = g_filename_from_uri(*uris, NULL, NULL);
1512 g_strfreev( uris );
1513 }
1514 }
1515 else if( info == 1 ) // text/plain
1516 {
1517 file = (char*)gtk_selection_data_get_text( data );
1518 }
1519 if( file )
1520 {
1521 main_win_open( mw, file, ZOOM_FIT );
1522 g_free( file );
1523 }
1524 }