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