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