Add missing file...
[lxde/gpicview.git] / src / main-win.c
CommitLineData
1d48a247 1/***************************************************************************
d9f34bf9 2 * Copyright (C) 2007, 2008 by PCMan (Hong Jen Yee) *
1d48a247
HJYP
3 * pcman.tw@gmail.com *
4 * *
cf15ca10 5 * mw 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 * *
cf15ca10 10 * mw 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 *
cf15ca10 16 * along with mw 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>
da8bd946 34#include <math.h>
d2d3c393 35#include <stdlib.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
HJYP
44
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 );
58static void rotate_image( MainWin* mw, GdkPixbufRotation angle );
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 );
64static void on_zoom_fit( GtkToggleButton* btn, MainWin* mw );
65static void on_zoom_fit_menu( GtkMenuItem* item, MainWin* mw );
66static void on_full_screen( GtkWidget* btn, MainWin* mw );
67static void on_next( GtkWidget* btn, MainWin* mw );
68static void on_orig_size( GtkToggleButton* btn, MainWin* mw );
69static void on_orig_size_menu( GtkToggleButton* btn, MainWin* mw );
70static void on_prev( GtkWidget* btn, MainWin* mw );
71static void on_rotate_clockwise( GtkWidget* btn, MainWin* mw );
72static void on_rotate_counterclockwise( GtkWidget* btn, MainWin* mw );
73static void on_save_as( GtkWidget* btn, MainWin* mw );
74static void on_save( GtkWidget* btn, MainWin* mw );
75static void on_open( GtkWidget* btn, MainWin* mw );
76static void on_zoom_in( GtkWidget* btn, MainWin* mw );
77static void on_zoom_out( GtkWidget* btn, MainWin* mw );
78static void on_preference( GtkWidget* btn, MainWin* mw );
79static void on_quit( GtkWidget* btn, MainWin* mw );
80static gboolean on_button_press( GtkWidget* widget, GdkEventButton* evt, MainWin* mw );
81static gboolean on_button_release( GtkWidget* widget, GdkEventButton* evt, MainWin* mw );
82static gboolean on_mouse_move( GtkWidget* widget, GdkEventMotion* evt, MainWin* mw );
83static gboolean on_scroll_event( GtkWidget* widget, GdkEventScroll* evt, MainWin* mw );
84static gboolean on_key_press_event(GtkWidget* widget, GdkEventKey * key);
85static void on_drag_data_received( GtkWidget* widget, GdkDragContext *drag_context,
86 int x, int y, GtkSelectionData* data, guint info,
87 guint time, MainWin* mw );
88static void on_delete( GtkWidget* btn, MainWin* mw );
89static void on_about( GtkWidget* menu, MainWin* mw );
1d48a247 90
1d48a247 91
cf15ca10 92// Begin of GObject-related stuff
1d48a247 93
cf15ca10 94G_DEFINE_TYPE( MainWin, main_win, GTK_TYPE_WINDOW )
1d48a247 95
cf15ca10
HJYP
96void main_win_class_init( MainWinClass* klass )
97{
1d48a247
HJYP
98 GObjectClass * obj_class;
99 GtkWidgetClass *widget_class;
100
101 obj_class = ( GObjectClass * ) klass;
102// obj_class->set_property = _set_property;
103// obj_class->get_property = _get_property;
cf15ca10 104 obj_class->finalize = main_win_finalize;
1d48a247
HJYP
105
106 widget_class = GTK_WIDGET_CLASS ( klass );
107 widget_class->delete_event = on_delete_event;
108 widget_class->size_allocate = on_size_allocate;
109 widget_class->key_press_event = on_key_press_event;
110}
111
cf15ca10
HJYP
112void main_win_finalize( GObject* obj )
113{
114 MainWin *mw = (MainWin*)obj;
115
116 main_win_close(mw);
117
118 if( G_LIKELY(mw->img_list) )
119 image_list_free( mw->img_list );
120 gdk_cursor_unref( mw->hand_cursor );
121
122 g_object_unref( mw->tooltips );
123
124 // FIXME: Put this here is weird
125 gtk_main_quit();
126}
127
128GtkWidget* main_win_new()
1d48a247 129{
cf15ca10 130 return (GtkWidget*)g_object_new ( MAIN_WIN_TYPE, NULL );
1d48a247
HJYP
131}
132
133// End of GObject-related stuff
134
cf15ca10 135void main_win_init( MainWin*mw )
1d48a247 136{
cf15ca10
HJYP
137 gtk_window_set_title( (GtkWindow*)mw, _("Image Viewer"));
138 gtk_window_set_icon_from_file( (GtkWindow*)mw, PACKAGE_DATA_DIR"/pixmaps/gpicview.png", NULL );
139 gtk_window_set_default_size( (GtkWindow*)mw, 640, 480 );
1d48a247
HJYP
140
141 GtkWidget* box = gtk_vbox_new( FALSE, 0 );
cf15ca10 142 gtk_container_add( (GtkContainer*)mw, box);
1d48a247
HJYP
143
144 // image area
cf15ca10
HJYP
145 mw->evt_box = gtk_event_box_new();
146 GTK_WIDGET_SET_FLAGS( mw->evt_box, GTK_CAN_FOCUS );
147 gtk_widget_add_events( mw->evt_box,
1d48a247
HJYP
148 GDK_POINTER_MOTION_MASK|GDK_BUTTON_PRESS_MASK|
149 GDK_BUTTON_RELEASE_MASK|GDK_SCROLL_MASK );
cf15ca10
HJYP
150 g_signal_connect( mw->evt_box, "button-press-event", G_CALLBACK(on_button_press), mw );
151 g_signal_connect( mw->evt_box, "button-release-event", G_CALLBACK(on_button_release), mw );
152 g_signal_connect( mw->evt_box, "motion-notify-event", G_CALLBACK(on_mouse_move), mw );
153 g_signal_connect( mw->evt_box, "scroll-event", G_CALLBACK(on_scroll_event), mw );
5bfec971
HJYP
154 // Set bg color to white
155 GdkColor white = {0, 65535, 65535, 65535};
cf15ca10 156 gtk_widget_modify_bg( mw->evt_box, GTK_STATE_NORMAL, &white );
1d48a247 157
cf15ca10
HJYP
158 mw->img_view = image_view_new();
159 gtk_container_add( (GtkContainer*)mw->evt_box, (GtkWidget*)mw->img_view);
1d48a247 160
2c34ffe0
HJYP
161 const char scroll_style[]=
162 "style \"gpicview-scroll\" {"
163 "GtkScrolledWindow::scrollbar-spacing=0"
164 "}"
165 "class \"GtkScrolledWindow\" style \"gpicview-scroll\"";
166 gtk_rc_parse_string( scroll_style );
cf15ca10
HJYP
167 mw->scroll = gtk_scrolled_window_new( NULL, NULL );
168 gtk_scrolled_window_set_shadow_type( (GtkScrolledWindow*)mw->scroll, GTK_SHADOW_NONE );
169 gtk_scrolled_window_set_policy((GtkScrolledWindow*)mw->scroll,
1d48a247
HJYP
170 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
171 GtkAdjustment *hadj, *vadj;
cf15ca10 172 hadj = gtk_scrolled_window_get_hadjustment((GtkScrolledWindow*)mw->scroll);
1d48a247
HJYP
173 hadj->page_increment = 10;
174 gtk_adjustment_changed(hadj);
cf15ca10 175 vadj = gtk_scrolled_window_get_vadjustment((GtkScrolledWindow*)mw->scroll);
1d48a247
HJYP
176 vadj->page_increment = 10;
177 gtk_adjustment_changed(vadj);
9c52ae58 178
d2d3c393 179 image_view_set_adjustments( IMAGE_VIEW(mw->img_view), hadj, vadj ); // dirty hack :-(
cf15ca10
HJYP
180 gtk_scrolled_window_add_with_viewport( (GtkScrolledWindow*)mw->scroll, mw->evt_box );
181 GtkWidget* viewport = gtk_bin_get_child( (GtkBin*)mw->scroll );
2c34ffe0
HJYP
182 gtk_viewport_set_shadow_type( (GtkViewport*)viewport, GTK_SHADOW_NONE );
183 gtk_container_set_border_width( (GtkContainer*)viewport, 0 );
1d48a247 184
cf15ca10 185 gtk_box_pack_start( (GtkBox*)box, mw->scroll, TRUE, TRUE, 0 );
1d48a247
HJYP
186
187 // build toolbar
cf15ca10 188 mw->tooltips = gtk_tooltips_new();
d9f34bf9 189#if GTK_CHECK_VERSION(2, 8, 0)
cf15ca10 190 g_object_ref_sink(mw->tooltips);
1d48a247 191#else
cf15ca10 192 gtk_object_sink((GtkObject*)mw->tooltips);
1d48a247 193#endif
1d48a247 194
cf15ca10 195 create_nav_bar( mw, box );
1d48a247
HJYP
196 gtk_widget_show_all( box );
197
cf15ca10 198 mw->hand_cursor = gdk_cursor_new_for_display( gtk_widget_get_display((GtkWidget*)mw), GDK_FLEUR );
1d48a247 199
5bfec971 200// zoom_mode = ZOOM_NONE;
cf15ca10 201 mw->zoom_mode = ZOOM_FIT;
1d48a247
HJYP
202
203 // Set up drag & drop
cf15ca10
HJYP
204 gtk_drag_dest_set( (GtkWidget*)mw, GTK_DEST_DEFAULT_ALL,
205 drop_targets,
206 G_N_ELEMENTS(drop_targets),
207 GDK_ACTION_COPY | GDK_ACTION_ASK );
208 g_signal_connect( mw, "drag-data-received", G_CALLBACK(on_drag_data_received), mw );
9c52ae58 209
cf15ca10 210 mw->img_list = image_list_new();
7c3b160c 211
7c3b160c
HJYP
212 // rotation angle is zero on startup
213 mw->rotation_angle = 0;
1d48a247
HJYP
214}
215
cf15ca10 216void create_nav_bar( MainWin* mw, GtkWidget* box )
1d48a247 217{
cf15ca10 218 mw->nav_bar = gtk_hbox_new( FALSE, 0 );
1d48a247 219
cf15ca10
HJYP
220 add_nav_btn( mw, GTK_STOCK_GO_BACK, _("Previous"), G_CALLBACK(on_prev), FALSE );
221 add_nav_btn( mw, GTK_STOCK_GO_FORWARD, _("Next"), G_CALLBACK(on_next), FALSE );
1d48a247 222
cf15ca10 223 gtk_box_pack_start( (GtkBox*)mw->nav_bar, gtk_vseparator_new(), FALSE, FALSE, 0 );
1d48a247 224
cf15ca10
HJYP
225 add_nav_btn( mw, GTK_STOCK_ZOOM_OUT, _("Zoom Out"), G_CALLBACK(on_zoom_out), FALSE );
226 add_nav_btn( mw, GTK_STOCK_ZOOM_IN, _("Zoom In"), G_CALLBACK(on_zoom_in), FALSE );
5bfec971
HJYP
227
228// percent = gtk_entry_new(); // show scale (in percentage)
cf15ca10 229// g_signal_connect( percent, "activate", G_CALLBACK(on_percentage), mw );
5bfec971
HJYP
230// gtk_widget_set_size_request( percent, 45, -1 );
231// gtk_box_pack_start( (GtkBox*)nav_bar, percent, FALSE, FALSE, 2 );
232
cf15ca10
HJYP
233 mw->btn_fit = add_nav_btn( mw, GTK_STOCK_ZOOM_FIT, _("Fit Image To Window Size"),
234 G_CALLBACK(on_zoom_fit), TRUE );
235 mw->btn_orig = add_nav_btn( mw, GTK_STOCK_ZOOM_100, _("Original Size"),
236 G_CALLBACK(on_orig_size), TRUE );
237 gtk_toggle_button_set_active( (GtkToggleButton*)mw->btn_fit, TRUE );
5bfec971 238
1d48a247
HJYP
239#ifndef GTK_STOCK_FULLSCREEN
240#define GTK_STOCK_FULLSCREEN "gtk-fullscreen"
241#endif
cf15ca10 242 add_nav_btn( mw, GTK_STOCK_FULLSCREEN, _(" Full Screen"), G_CALLBACK(on_full_screen), FALSE ); // gtk+ 2.8+
1d48a247 243
cf15ca10 244 gtk_box_pack_start( (GtkBox*)mw->nav_bar, gtk_vseparator_new(), FALSE, FALSE, 0 );
1d48a247 245
cf15ca10
HJYP
246 add_nav_btn( mw, "gtk-counterclockwise", _("Rotate Counterclockwise"),
247 G_CALLBACK(on_rotate_counterclockwise), FALSE );
248 add_nav_btn( mw, "gtk-clockwise", _("Rotate Clockwise"), G_CALLBACK(on_rotate_clockwise), FALSE );
1d48a247 249
cf15ca10 250 gtk_box_pack_start( (GtkBox*)mw->nav_bar, gtk_vseparator_new(), FALSE, FALSE, 0 );
1d48a247 251
cf15ca10
HJYP
252 add_nav_btn( mw, GTK_STOCK_OPEN, _("Open File"), G_CALLBACK(on_open), FALSE );
253 add_nav_btn( mw, GTK_STOCK_SAVE, _("Save File"), G_CALLBACK(on_save), FALSE );
254 add_nav_btn( mw, GTK_STOCK_SAVE_AS, _("Save File As"), G_CALLBACK(on_save_as), FALSE );
255 add_nav_btn( mw, GTK_STOCK_DELETE, _("Delete File"), G_CALLBACK(on_delete), FALSE );
1d48a247 256
cf15ca10 257 gtk_box_pack_start( (GtkBox*)mw->nav_bar, gtk_vseparator_new(), FALSE, FALSE, 0 );
bb0f721e 258 add_nav_btn( mw, GTK_STOCK_PREFERENCES, _("Preferences"), G_CALLBACK(on_preference), FALSE );
1d48a247
HJYP
259
260 GtkWidget* align = gtk_alignment_new( 0.5, 0, 0, 0 );
cf15ca10 261 gtk_container_add( (GtkContainer*)align, mw->nav_bar );
1d48a247
HJYP
262 gtk_box_pack_start( (GtkBox*)box, align, FALSE, TRUE, 2 );
263}
264
cf15ca10 265gboolean on_delete_event( GtkWidget* widget, GdkEventAny* evt )
1d48a247
HJYP
266{
267 gtk_widget_destroy( widget );
268 return TRUE;
269}
270
cf15ca10 271gboolean main_win_open( MainWin* mw, const char* file_path, ZoomMode zoom )
1d48a247 272{
1d48a247 273 GError* err = NULL;
cf15ca10
HJYP
274 main_win_close( mw );
275 mw->pix = gdk_pixbuf_new_from_file( file_path, &err );
c215db47 276
cf15ca10 277 if( ! mw->pix )
1d48a247 278 {
cf15ca10
HJYP
279 main_win_show_error( mw, err->message );
280 return FALSE;
1d48a247 281 }
c215db47
HJYP
282#if GTK_CHECK_VERSION( 2, 12, 0 )
283 else
284 {
285 /* apply orientation provided by EXIF (Use gtk+ 2.12 specific API) */
286 GdkPixbuf* tmp = gdk_pixbuf_apply_embedded_orientation(mw->pix);
287 g_object_unref( mw->pix );
288 mw->pix = tmp;
289 }
290#endif
d8ce3af1 291
cf15ca10 292 mw->zoom_mode = zoom;
da8bd946 293
5bfec971 294 // select most suitable viewing mode
d8ce3af1 295 if( zoom == ZOOM_NONE )
1d48a247 296 {
cf15ca10
HJYP
297 int w = gdk_pixbuf_get_width( mw->pix );
298 int h = gdk_pixbuf_get_height( mw->pix );
1d48a247 299
1d48a247 300 GdkRectangle area;
cf15ca10 301 get_working_area( gtk_widget_get_screen((GtkWidget*)mw), &area );
c325c989
HJYP
302 // g_debug("determine best zoom mode: orig size: w=%d, h=%d", w, h);
303 // FIXME: actually this is a little buggy :-(
da8bd946 304 if( w < area.width && h < area.height && (w >= 640 || h >= 480) )
1d48a247 305 {
cf15ca10
HJYP
306 gtk_scrolled_window_set_policy( (GtkScrolledWindow*)mw->scroll, GTK_POLICY_NEVER, GTK_POLICY_NEVER );
307 gtk_widget_set_size_request( (GtkWidget*)mw->img_view, w, h );
5bfec971 308 GtkRequisition req;
cf15ca10 309 gtk_widget_size_request ( (GtkWidget*)mw, &req );
9c52ae58
HJYP
310 if( req.width < 640 ) req.width = 640;
311 if( req.height < 480 ) req.height = 480;
cf15ca10
HJYP
312 gtk_window_resize( (GtkWindow*)mw, req.width, req.height );
313 gtk_widget_set_size_request( (GtkWidget*)mw->img_view, -1, -1 );
314 gtk_scrolled_window_set_policy( (GtkScrolledWindow*)mw->scroll, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
315 mw->zoom_mode = ZOOM_ORIG;
1d48a247
HJYP
316 }
317 else
cf15ca10 318 mw->zoom_mode = ZOOM_FIT;
5bfec971 319 }
5bfec971 320
cf15ca10 321 if( mw->zoom_mode == ZOOM_FIT )
d8ce3af1 322 {
cf15ca10 323 gtk_toggle_button_set_active( (GtkToggleButton*)mw->btn_fit, TRUE );
c325c989 324 main_win_fit_window_size( mw, FALSE, GDK_INTERP_BILINEAR );
d8ce3af1 325 }
cf15ca10 326 else if( mw->zoom_mode == ZOOM_SCALE ) // scale
d8ce3af1 327 {
cf15ca10
HJYP
328 gtk_toggle_button_set_active( (GtkToggleButton*)mw->btn_orig, FALSE );
329 gtk_toggle_button_set_active( (GtkToggleButton*)mw->btn_fit, FALSE );
330 main_win_scale_image( mw, mw->scale, GDK_INTERP_BILINEAR );
d8ce3af1 331 }
cf15ca10 332 else if( mw->zoom_mode == ZOOM_ORIG ) // original size
5bfec971 333 {
cf15ca10
HJYP
334 gtk_toggle_button_set_active( (GtkToggleButton*)mw->btn_orig, TRUE );
335 image_view_set_scale( (ImageView*)mw->img_view, 1.0, GDK_INTERP_BILINEAR );
336 main_win_center_image( mw );
1d48a247
HJYP
337 }
338
cf15ca10 339 image_view_set_pixbuf( (ImageView*)mw->img_view, mw->pix );
9c52ae58 340
1d48a247
HJYP
341// while (gtk_events_pending ())
342// gtk_main_iteration ();
343
344 // build file list
345 char* dir_path = g_path_get_dirname( file_path );
cf15ca10
HJYP
346 image_list_open_dir( mw->img_list, dir_path, NULL );
347 image_list_sort_by_name( mw->img_list, GTK_SORT_ASCENDING );
1d48a247 348 g_free( dir_path );
76e88fcf 349
1d48a247 350 char* base_name = g_path_get_basename( file_path );
cf15ca10 351 image_list_set_current( mw->img_list, base_name );
76e88fcf
HJYP
352
353 char* disp_name = g_filename_display_name( base_name );
1d48a247
HJYP
354 g_free( base_name );
355
cf15ca10 356 gtk_window_set_title( (GtkWindow*)mw, disp_name );
76e88fcf 357 g_free( disp_name );
1d48a247 358
cf15ca10 359 return TRUE;
1d48a247
HJYP
360}
361
cf15ca10 362void main_win_close( MainWin* mw )
1d48a247 363{
cf15ca10 364 if( mw->pix )
9c52ae58 365 {
cf15ca10
HJYP
366 g_object_unref( mw->pix );
367 mw->pix = NULL;
1d48a247
HJYP
368 }
369}
370
cf15ca10 371void main_win_show_error( MainWin* mw, const char* message )
1d48a247 372{
cf15ca10 373 GtkWidget* dlg = gtk_message_dialog_new( (GtkWindow*)mw,
1d48a247
HJYP
374 GTK_DIALOG_MODAL,
375 GTK_MESSAGE_ERROR,
376 GTK_BUTTONS_OK,
377 message );
378 gtk_dialog_run( (GtkDialog*)dlg );
379 gtk_widget_destroy( dlg );
380}
381
cf15ca10 382void on_size_allocate( GtkWidget* widget, GtkAllocation *allocation )
1d48a247 383{
cf15ca10 384 GTK_WIDGET_CLASS(main_win_parent_class)->size_allocate( widget, allocation );
52c867e9 385 if( GTK_WIDGET_REALIZED (widget) )
1d48a247 386 {
cf15ca10 387 MainWin* mw = (MainWin*)widget;
9c52ae58 388
cf15ca10 389 if( mw->zoom_mode == ZOOM_FIT )
52c867e9
HJYP
390 {
391 while(gtk_events_pending ())
392 gtk_main_iteration(); // makes it more fluid
1d48a247 393
c325c989 394 main_win_fit_window_size( mw, FALSE, GDK_INTERP_BILINEAR );
52c867e9 395 }
1d48a247
HJYP
396 }
397}
398
cf15ca10 399void main_win_fit_size( MainWin* mw, int width, int height, gboolean can_strech, GdkInterpType type )
1d48a247 400{
cf15ca10 401 if( ! mw->pix )
1d48a247
HJYP
402 return;
403
cf15ca10
HJYP
404 int orig_w = gdk_pixbuf_get_width( mw->pix );
405 int orig_h = gdk_pixbuf_get_height( mw->pix );
1d48a247 406
da8bd946
HJYP
407 if( can_strech || (orig_w > width || orig_h > height) )
408 {
cf15ca10
HJYP
409 gdouble xscale = ((gdouble)width) / orig_w;
410 gdouble yscale = ((gdouble)height)/ orig_h;
411 gdouble final_scale = xscale < yscale ? xscale : yscale;
1d48a247 412
cf15ca10 413 main_win_scale_image( mw, final_scale, type );
da8bd946
HJYP
414 }
415 else // use original size if the image is smaller than the window
416 {
cf15ca10
HJYP
417 mw->scale = 1.0;
418 image_view_set_scale( (ImageView*)mw->img_view, 1.0, type );
da8bd946 419 }
1d48a247
HJYP
420}
421
cf15ca10 422void main_win_fit_window_size( MainWin* mw, gboolean can_strech, GdkInterpType type )
1d48a247 423{
cf15ca10 424 mw->zoom_mode = ZOOM_FIT;
94fcf707 425
cf15ca10 426 if( mw->pix == NULL )
da8bd946 427 return;
94fcf707 428
cf15ca10 429 main_win_fit_size( mw, mw->scroll->allocation.width, mw->scroll->allocation.height, can_strech, type );
1d48a247
HJYP
430}
431
cf15ca10 432GtkWidget* add_nav_btn( MainWin* mw, const char* icon, const char* tip, GCallback cb, gboolean toggle )
1d48a247
HJYP
433{
434 GtkWidget* img = gtk_image_new_from_stock(icon, GTK_ICON_SIZE_SMALL_TOOLBAR);
435 GtkWidget* btn;
436 if( G_UNLIKELY(toggle) )
437 {
438 btn = gtk_toggle_button_new();
cf15ca10 439 g_signal_connect( btn, "toggled", cb, mw );
1d48a247
HJYP
440 }
441 else
442 {
443 btn = gtk_button_new();
cf15ca10 444 g_signal_connect( btn, "clicked", cb, mw );
1d48a247
HJYP
445 }
446 gtk_button_set_relief( (GtkButton*)btn, GTK_RELIEF_NONE );
447 gtk_button_set_focus_on_click( (GtkButton*)btn, FALSE );
448 gtk_container_add( (GtkContainer*)btn, img );
cf15ca10
HJYP
449 gtk_tooltips_set_tip( mw->tooltips, btn, tip, NULL );
450 gtk_box_pack_start( (GtkBox*)mw->nav_bar, btn, FALSE, FALSE, 0 );
1d48a247
HJYP
451 return btn;
452}
453
cf15ca10 454void on_zoom_fit_menu( GtkMenuItem* item, MainWin* mw )
1d48a247 455{
cf15ca10 456 gtk_button_clicked( (GtkButton*)mw->btn_fit );
1d48a247
HJYP
457}
458
cf15ca10 459void on_zoom_fit( GtkToggleButton* btn, MainWin* mw )
1d48a247
HJYP
460{
461 if( ! btn->active )
462 {
cf15ca10 463 if( mw->zoom_mode == ZOOM_FIT )
1d48a247
HJYP
464 gtk_toggle_button_set_active( btn, TRUE );
465 return;
466 }
cf15ca10 467 gtk_toggle_button_set_active( (GtkToggleButton*)mw->btn_orig, FALSE );
1d48a247 468
c325c989 469 main_win_fit_window_size( mw, FALSE, GDK_INTERP_BILINEAR );
1d48a247
HJYP
470}
471
cf15ca10 472void on_full_screen( GtkWidget* btn, MainWin* mw )
1d48a247 473{
cf15ca10 474 if( ! mw->full_screen )
1d48a247 475 {
5bfec971 476 static GdkColor black = {0};
cf15ca10
HJYP
477 gtk_widget_modify_bg( mw->evt_box, GTK_STATE_NORMAL, &black );
478 gtk_widget_hide( gtk_widget_get_parent(mw->nav_bar) );
479 gtk_window_fullscreen( (GtkWindow*)mw );
1d48a247
HJYP
480 }
481 else
482 {
cf15ca10 483// gtk_widget_reset_rc_styles( mw->evt_box );
5bfec971 484 static GdkColor white = {0, 65535, 65535, 65535};
cf15ca10
HJYP
485 gtk_widget_modify_bg( mw->evt_box, GTK_STATE_NORMAL, &white );
486 gtk_widget_show( gtk_widget_get_parent(mw->nav_bar) );
487 gtk_window_unfullscreen( (GtkWindow*)mw );
1d48a247 488 }
cf15ca10 489 mw->full_screen = ! mw->full_screen;
1d48a247
HJYP
490}
491
cf15ca10 492void on_orig_size_menu( GtkToggleButton* btn, MainWin* mw )
1d48a247 493{
cf15ca10 494 gtk_button_clicked( (GtkButton*)mw->btn_orig );
1d48a247
HJYP
495}
496
cf15ca10 497void on_orig_size( GtkToggleButton* btn, MainWin* mw )
1d48a247 498{
cf15ca10 499 // this callback could be called from activate signal of menu item.
1d48a247
HJYP
500 if( GTK_IS_MENU_ITEM(btn) )
501 {
cf15ca10 502 gtk_button_clicked( (GtkButton*)mw->btn_orig );
1d48a247
HJYP
503 return;
504 }
505
506 if( ! btn->active )
507 {
cf15ca10 508 if( mw->zoom_mode == ZOOM_ORIG )
1d48a247
HJYP
509 gtk_toggle_button_set_active( btn, TRUE );
510 return;
511 }
cf15ca10
HJYP
512 mw->zoom_mode = ZOOM_ORIG;
513 mw->scale = 1.0;
514// gtk_scrolled_window_set_policy( (GtkScrolledWindow*)mw->scroll,
94fcf707 515// GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
1d48a247 516
cf15ca10 517 gtk_toggle_button_set_active( (GtkToggleButton*)mw->btn_fit, FALSE );
1d48a247 518
cf15ca10 519 if( ! mw->pix )
1d48a247
HJYP
520 return;
521
cf15ca10 522 image_view_set_scale( (ImageView*)mw->img_view, 1.0, GDK_INTERP_BILINEAR );
1d48a247 523
9c52ae58
HJYP
524 while (gtk_events_pending ())
525 gtk_main_iteration ();
526
cf15ca10 527 main_win_center_image( mw ); // FIXME: mw doesn't work well. Why?
1d48a247
HJYP
528}
529
f34b3b31 530void on_prev( GtkWidget* btn, MainWin* mw )
1d48a247 531{
cf15ca10
HJYP
532 const char* name;
533 if( image_list_is_empty( mw->img_list ) )
1d48a247
HJYP
534 return;
535
cf15ca10 536 name = image_list_get_prev( mw->img_list );
1d48a247 537
cf15ca10 538 if( ! name && image_list_has_multiple_files( mw->img_list ) )
1d48a247
HJYP
539 {
540 // FIXME: need to ask user first?
cf15ca10 541 name = image_list_get_last( mw->img_list );
1d48a247
HJYP
542 }
543
544 if( name )
545 {
cf15ca10
HJYP
546 char* file_path = image_list_get_current_file_path( mw->img_list );
547 main_win_open( mw, file_path, ZOOM_FIT );
1d48a247
HJYP
548 g_free( file_path );
549 }
550}
551
f34b3b31 552void on_next( GtkWidget* btn, MainWin* mw )
1d48a247 553{
cf15ca10 554 if( image_list_is_empty( mw->img_list ) )
1d48a247
HJYP
555 return;
556
cf15ca10 557 const char* name = image_list_get_next( mw->img_list );
1d48a247 558
cf15ca10 559 if( ! name && image_list_has_multiple_files( mw->img_list ) )
1d48a247
HJYP
560 {
561 // FIXME: need to ask user first?
cf15ca10 562 name = image_list_get_first( mw->img_list );
1d48a247
HJYP
563 }
564
565 if( name )
566 {
cf15ca10
HJYP
567 char* file_path = image_list_get_current_file_path( mw->img_list );
568 main_win_open( mw, file_path, ZOOM_FIT );
1d48a247
HJYP
569 g_free( file_path );
570 }
571}
572
cf15ca10 573void on_rotate_clockwise( GtkWidget* btn, MainWin* mw )
1d48a247 574{
cf15ca10 575 rotate_image( mw, GDK_PIXBUF_ROTATE_CLOCKWISE );
7c3b160c 576 mw->rotation_angle += 90;
bb0f721e
HJYP
577 if(pref.auto_save_rotated){
578 pref.ask_before_save = FALSE;
7c3b160c 579 on_save(btn,mw);
bb0f721e 580 pref.ask_before_save = TRUE;
7c3b160c 581 }
1d48a247
HJYP
582}
583
cf15ca10 584void on_rotate_counterclockwise( GtkWidget* btn, MainWin* mw )
1d48a247 585{
cf15ca10 586 rotate_image( mw, GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE );
7c3b160c 587 mw->rotation_angle += 270;
bb0f721e
HJYP
588 if(pref.auto_save_rotated){
589 pref.ask_before_save = FALSE;
7c3b160c 590 on_save(btn,mw);
bb0f721e 591 pref.ask_before_save = TRUE;
7c3b160c 592 }
1d48a247
HJYP
593}
594
595static void on_update_preview( GtkFileChooser *chooser, GtkImage* img )
596{
597 char* file = gtk_file_chooser_get_preview_filename( chooser );
598 GdkPixbuf* pix = NULL;
599 if( file )
600 {
601 pix = gdk_pixbuf_new_from_file_at_scale( file, 128, 128, TRUE, NULL );
602 g_free( file );
603 }
1d48a247 604 if( pix )
9c52ae58
HJYP
605 {
606 gtk_image_set_from_pixbuf( img, pix );
607 g_object_unref( pix );
608 }
1d48a247
HJYP
609}
610
cf15ca10 611void on_save_as( GtkWidget* btn, MainWin* mw )
1d48a247 612{
cf15ca10 613 if( ! mw->pix )
1d48a247
HJYP
614 return;
615
cf15ca10 616 GtkFileChooser* dlg = (GtkFileChooser*)gtk_file_chooser_dialog_new( NULL, (GtkWindow*)mw,
1d48a247
HJYP
617 GTK_FILE_CHOOSER_ACTION_SAVE, GTK_STOCK_CANCEL,
618 GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_OK, NULL );
619
cf15ca10 620 gtk_file_chooser_set_current_folder( dlg, image_list_get_dir( mw->img_list ) );
1d48a247
HJYP
621
622 GtkWidget* img = gtk_image_new();
623 gtk_widget_set_size_request( img, 128, 128 );
624 gtk_file_chooser_set_preview_widget( dlg, img );
625 g_signal_connect( dlg, "update-preview", G_CALLBACK(on_update_preview), img );
626
627 GtkFileFilter *filter;
628
629 /*
630 /// TODO: determine file type from file name
631 filter = gtk_file_filter_new();
632 gtk_file_filter_set_name( filter, _("Determined by File Name") );
633 gtk_file_filter_add_pixbuf_formats( filter );
634 gtk_file_chooser_add_filter( dlg, filter );
635 */
636
637 GSList* modules = gdk_pixbuf_get_formats();
638 GSList* module;
639 for( module = modules; module; module = module->next )
640 {
641 GdkPixbufFormat* format = (GdkPixbufFormat*)module->data;
642 if( ! gdk_pixbuf_format_is_writable( format ) )
643 continue;
644
645 filter = gtk_file_filter_new();
646
647 char* desc = gdk_pixbuf_format_get_description( format );
648 char* name = gdk_pixbuf_format_get_name( format );
649 char* tmp = g_strjoin( ": ", name, desc, NULL );
650 g_free( desc );
651 g_free( name );
652 gtk_file_filter_set_name( filter, tmp );
653 g_free( tmp );
654
655 char** mimes = gdk_pixbuf_format_get_mime_types( format ), **mime;
656 for( mime = mimes; *mime ; ++mime )
657 gtk_file_filter_add_mime_type( filter, *mime );
658 g_strfreev( mimes );
659 gtk_file_chooser_add_filter( dlg, filter );
660 }
661
662 if( gtk_dialog_run( (GtkDialog*)dlg ) == GTK_RESPONSE_OK )
663 {
664 filter = gtk_file_chooser_get_filter( dlg );
665 const char* filter_name = gtk_file_filter_get_name( filter );
666 char* p = strstr( filter_name, ": " );
667 char* type = NULL;
668 if( ! p ) // auto detection
669 {
670 /// TODO: auto file type
671 }
672 else
673 {
674 type = g_strndup( filter_name, (p - filter_name) );
675 }
676 char* file = gtk_file_chooser_get_filename( dlg );
677 // g_debug("type = %s", type);
cf15ca10 678 main_win_save( mw, file, type, TRUE );
1d48a247
HJYP
679 g_free( file );
680 g_free( type );
681 }
682 gtk_widget_destroy( (GtkWidget*)dlg );
683}
684
7c3b160c
HJYP
685#ifdef HAVE_LIBJPEG
686int rotate_and_save_jpeg_lossless(char * filename,int angle){
687 JXFORM_CODE code = JXFORM_NONE;
688
689 angle = angle % 360;
690
691 if(angle == 90)
692 code = JXFORM_ROT_90;
693 else if(angle == 180)
694 code = JXFORM_ROT_180;
695 else if(angle == 270)
696 code = JXFORM_ROT_270;
697
698 //rotate the image and save it to /tmp/rot.jpg
699 int error = jpegtran (filename, "/tmp/rot.jpg" , code);
700 if(error)
701 return error;
702
703 //now copy /tmp/rot.jpg back to the original file
704 char command[strlen(filename)+50]; //this should not generate buffer owerflow
705 // MS: didn't know, how to make it better, maybe an own copy routine
706 sprintf(command,"cp /tmp/rot.jpg \"%s\"",filename);
707 system(command);
708
709 return 0;
710}
711#endif
712
cf15ca10 713void on_save( GtkWidget* btn, MainWin* mw )
1d48a247 714{
cf15ca10 715 if( ! mw->pix )
1d48a247
HJYP
716 return;
717
cf15ca10
HJYP
718 char* file_name = g_build_filename( image_list_get_dir( mw->img_list ),
719 image_list_get_current( mw->img_list ), NULL );
1d48a247
HJYP
720 GdkPixbufFormat* info;
721 info = gdk_pixbuf_get_file_info( file_name, NULL, NULL );
722 char* type = gdk_pixbuf_format_get_name( info );
7c3b160c
HJYP
723#ifdef HAVE_LIBJPEG
724 if(strcmp(type,"jpeg")==0){
725 if(rotate_and_save_jpeg_lossless(file_name,mw->rotation_angle)!=0)
726 main_win_show_error(mw, "Save failed! Check permissions.");
727 } else
728#endif
bb0f721e 729 main_win_save( mw, file_name, type, pref.ask_before_save );
7c3b160c 730 mw->rotation_angle = 0;
1d48a247
HJYP
731 g_free( file_name );
732 g_free( type );
733}
734
cf15ca10 735void on_open( GtkWidget* btn, MainWin* mw )
1d48a247 736{
cf15ca10 737 GtkFileChooser* dlg = (GtkFileChooser*)gtk_file_chooser_dialog_new( NULL, (GtkWindow*)mw,
1d48a247
HJYP
738 GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL,
739 GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL );
740
cf15ca10
HJYP
741 if( image_list_get_dir( mw->img_list ) )
742 gtk_file_chooser_set_current_folder( dlg, image_list_get_dir( mw->img_list ) );
1d48a247
HJYP
743
744 GtkWidget* img = gtk_image_new();
745 gtk_widget_set_size_request( img, 128, 128 );
746 gtk_file_chooser_set_preview_widget( dlg, img );
747 g_signal_connect( dlg, "update-preview", G_CALLBACK(on_update_preview), img );
748
749 GtkFileFilter *filter = gtk_file_filter_new();
750 gtk_file_filter_set_name( filter, _("All Supported Images") );
751 gtk_file_filter_add_pixbuf_formats( filter );
752 gtk_file_chooser_add_filter( dlg, filter );
753
754 filter = gtk_file_filter_new();
755 gtk_file_filter_set_name( filter, _("All Files") );
756 gtk_file_filter_add_pattern( filter, "*" );
757 gtk_file_chooser_add_filter( dlg, filter );
758
759 char* file = NULL;
760 if( gtk_dialog_run( (GtkDialog*)dlg ) == GTK_RESPONSE_OK )
761 file = gtk_file_chooser_get_filename( dlg );
762 gtk_widget_destroy( (GtkWidget*)dlg );
763
764 if( file )
765 {
cf15ca10 766 main_win_open( mw, file, ZOOM_NONE );
1d48a247
HJYP
767 g_free( file );
768 }
769}
770
cf15ca10 771void on_zoom_in( GtkWidget* btn, MainWin* mw )
1d48a247 772{
cf15ca10
HJYP
773 mw->zoom_mode = ZOOM_SCALE;
774 gtk_toggle_button_set_active( (GtkToggleButton*)mw->btn_fit, FALSE );
775 gtk_toggle_button_set_active( (GtkToggleButton*)mw->btn_orig, FALSE );
776// gtk_scrolled_window_set_policy( (GtkScrolledWindow*)mw->scroll,
94fcf707 777// GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
1d48a247 778
cf15ca10
HJYP
779 double scale = mw->scale;
780 if( mw->pix && scale < 20.0 )
1d48a247 781 {
cf15ca10 782// busy(TRUE);
9c52ae58
HJYP
783 scale *= 1.05;
784 if( scale > 20.0 )
785 scale = 20.0;
cf15ca10
HJYP
786 if( mw->scale != scale )
787 main_win_scale_image( mw, scale, GDK_INTERP_BILINEAR );
1d48a247 788// adjust_adjustment_on_zoom(oldscale);
cf15ca10 789// busy(FALSE);
1d48a247
HJYP
790 }
791}
792
cf15ca10 793void on_zoom_out( GtkWidget* btn, MainWin* mw )
1d48a247 794{
cf15ca10
HJYP
795 mw->zoom_mode = ZOOM_SCALE;
796 gtk_toggle_button_set_active( (GtkToggleButton*)mw->btn_fit, FALSE );
797 gtk_toggle_button_set_active( (GtkToggleButton*)mw->btn_orig, FALSE );
798// gtk_scrolled_window_set_policy( (GtkScrolledWindow*)mw->scroll,
94fcf707 799// GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
1d48a247 800
cf15ca10
HJYP
801 double scale = mw->scale;
802 if( mw->pix && scale > 0.02 )
1d48a247 803 {
cf15ca10 804// busy(TRUE);
da8bd946 805
9c52ae58
HJYP
806 scale /= 1.05;
807 if( scale < 0.02 )
808 scale = 0.02;
cf15ca10
HJYP
809 if( mw->scale != scale )
810 main_win_scale_image( mw, scale, GDK_INTERP_BILINEAR );
1d48a247 811// adjust_adjustment_on_zoom(oldscale);
cf15ca10 812// busy(FALSE);
1d48a247
HJYP
813 }
814}
815
cf15ca10 816void on_preference( GtkWidget* btn, MainWin* mw )
1d48a247 817{
bb0f721e 818 edit_preferences( (GtkWindow*)mw );
1d48a247
HJYP
819}
820
cf15ca10 821void on_quit( GtkWidget* btn, MainWin* mw )
1d48a247 822{
cf15ca10 823 gtk_widget_destroy( (GtkWidget*)mw );
1d48a247
HJYP
824}
825
cf15ca10 826gboolean on_button_press( GtkWidget* widget, GdkEventButton* evt, MainWin* mw )
1d48a247 827{
9c52ae58
HJYP
828 if( ! GTK_WIDGET_HAS_FOCUS( widget ) )
829 gtk_widget_grab_focus( widget );
5bfec971 830
1d48a247
HJYP
831 if( evt->type == GDK_BUTTON_PRESS)
832 {
833 if( evt->button == 1 ) // left button
834 {
cf15ca10 835 if( ! mw->pix )
1d48a247 836 return FALSE;
cf15ca10
HJYP
837 mw->dragging = TRUE;
838 gtk_widget_get_pointer( (GtkWidget*)mw, &mw->drag_old_x ,&mw->drag_old_y );
839 gdk_window_set_cursor( widget->window, mw->hand_cursor );
1d48a247
HJYP
840 }
841 else if( evt->button == 3 ) // right button
842 {
cf15ca10 843 show_popup_menu( mw, evt );
1d48a247
HJYP
844 }
845 }
846 else if( evt->type == GDK_2BUTTON_PRESS && evt->button == 1 ) // double clicked
847 {
cf15ca10 848 on_full_screen( NULL, mw );
1d48a247 849 }
1d48a247
HJYP
850 return FALSE;
851}
852
cf15ca10 853gboolean on_mouse_move( GtkWidget* widget, GdkEventMotion* evt, MainWin* mw )
1d48a247 854{
cf15ca10 855 if( ! mw->dragging )
1d48a247 856 return FALSE;
9c52ae58 857
1d48a247 858 int cur_x, cur_y;
cf15ca10 859 gtk_widget_get_pointer( (GtkWidget*)mw, &cur_x ,&cur_y );
1d48a247 860
cf15ca10
HJYP
861 int dx = (mw->drag_old_x - cur_x);
862 int dy = (mw->drag_old_y - cur_y);
1d48a247
HJYP
863
864 GtkAdjustment *hadj, *vadj;
cf15ca10
HJYP
865 hadj = gtk_scrolled_window_get_hadjustment((GtkScrolledWindow*)mw->scroll);
866 vadj = gtk_scrolled_window_get_vadjustment((GtkScrolledWindow*)mw->scroll);
1d48a247 867
9c52ae58 868 GtkRequisition req;
cf15ca10 869 gtk_widget_size_request( (GtkWidget*)mw->img_view, &req );
1d48a247
HJYP
870
871 if( ABS(dx) > 4 )
872 {
cf15ca10 873 mw->drag_old_x = cur_x;
9c52ae58 874 if( req.width > hadj->page_size )
1d48a247
HJYP
875 {
876 gdouble x = gtk_adjustment_get_value (hadj) + dx;
877 if( x < hadj->lower )
878 x = hadj->lower;
879 else if( (x + hadj->page_size) > hadj->upper )
880 x = hadj->upper - hadj->page_size;
881
882 if( x != hadj->value )
883 gtk_adjustment_set_value (hadj, x );
884 }
885 }
886
887 if( ABS(dy) > 4 )
888 {
9c52ae58 889 if( req.height > vadj->page_size )
1d48a247 890 {
cf15ca10 891 mw->drag_old_y = cur_y;
1d48a247
HJYP
892 gdouble y = gtk_adjustment_get_value (vadj) + dy;
893 if( y < vadj->lower )
894 y = vadj->lower;
895 else if( (y + vadj->page_size) > vadj->upper )
896 y = vadj->upper - vadj->page_size;
897
898 if( y != vadj->value )
899 gtk_adjustment_set_value (vadj, y );
900 }
901 }
902 return FALSE;
903}
904
cf15ca10 905gboolean on_button_release( GtkWidget* widget, GdkEventButton* evt, MainWin* mw )
1d48a247 906{
cf15ca10 907 mw->dragging = FALSE;
1d48a247
HJYP
908 gdk_window_set_cursor( widget->window, NULL );
909 return FALSE;
910}
911
cf15ca10 912gboolean on_scroll_event( GtkWidget* widget, GdkEventScroll* evt, MainWin* mw )
5bfec971 913{
58e1b106 914 guint modifiers = gtk_accelerator_get_default_mod_mask();
5bfec971
HJYP
915 switch( evt->direction )
916 {
917 case GDK_SCROLL_UP:
58e1b106
JH
918 if ((evt->state & modifiers) == GDK_CONTROL_MASK)
919 on_zoom_in( NULL, mw );
920 else
921 on_prev( NULL, mw );
5bfec971
HJYP
922 break;
923 case GDK_SCROLL_DOWN:
58e1b106
JH
924 if ((evt->state & modifiers) == GDK_CONTROL_MASK)
925 on_zoom_out( NULL, mw );
926 else
927 on_next( NULL, mw );
5bfec971
HJYP
928 break;
929 case GDK_SCROLL_LEFT:
cf15ca10 930 on_prev( NULL, mw );
5bfec971
HJYP
931 break;
932 case GDK_SCROLL_RIGHT:
cf15ca10 933 on_next( NULL, mw );
5bfec971
HJYP
934 break;
935 }
da8bd946 936 return TRUE;
5bfec971
HJYP
937}
938
cf15ca10 939gboolean on_key_press_event(GtkWidget* widget, GdkEventKey * key)
1d48a247 940{
cf15ca10 941 MainWin* mw = (MainWin*)widget;
1d48a247
HJYP
942 switch( key->keyval )
943 {
7c3b160c
HJYP
944 case GDK_Right:
945 case GDK_KP_Right:
946 case GDK_rightarrow:
1d48a247
HJYP
947 case GDK_Return:
948 case GDK_space:
949 case GDK_Next:
5bfec971
HJYP
950 case GDK_KP_Down:
951 case GDK_Down:
952 case GDK_downarrow:
cf15ca10 953 on_next( NULL, mw );
1d48a247 954 break;
7c3b160c
HJYP
955 case GDK_Left:
956 case GDK_KP_Left:
957 case GDK_leftarrow:
1d48a247
HJYP
958 case GDK_Prior:
959 case GDK_BackSpace:
5bfec971
HJYP
960 case GDK_KP_Up:
961 case GDK_Up:
962 case GDK_uparrow:
cf15ca10 963 on_prev( NULL, mw );
1d48a247
HJYP
964 break;
965 case GDK_KP_Add:
966 case GDK_plus:
641adad3 967 case GDK_equal:
cf15ca10 968 on_zoom_in( NULL, mw );
1d48a247
HJYP
969 break;
970 case GDK_KP_Subtract:
971 case GDK_minus:
cf15ca10 972 on_zoom_out( NULL, mw );
1d48a247 973 break;
1d48a247 974 case GDK_s:
641adad3 975// case GDK_S:
cf15ca10 976 on_save( NULL, mw );
1d48a247
HJYP
977 break;
978 case GDK_l:
5bfec971 979// case GDK_L:
cf15ca10 980 on_rotate_counterclockwise( NULL, mw );
1d48a247
HJYP
981 break;
982 case GDK_r:
5bfec971 983// case GDK_R:
cf15ca10 984 on_rotate_clockwise( NULL, mw );
1d48a247
HJYP
985 break;
986 case GDK_f:
5bfec971 987// case GDK_F:
cf15ca10
HJYP
988 if( mw->zoom_mode != ZOOM_FIT )
989 gtk_button_clicked((GtkButton*)mw->btn_fit );
1d48a247
HJYP
990 break;
991 case GDK_g:
5bfec971 992// case GDK_G:
cf15ca10
HJYP
993 if( mw->zoom_mode != ZOOM_ORIG )
994 gtk_button_clicked((GtkButton*)mw->btn_orig );
1d48a247
HJYP
995 break;
996 case GDK_o:
5bfec971 997// case GDK_O:
cf15ca10 998 on_open( NULL, mw );
5bfec971
HJYP
999 break;
1000 case GDK_Delete:
1001 case GDK_d:
1002// case GDK_D:
cf15ca10 1003 on_delete( NULL, mw );
1d48a247
HJYP
1004 break;
1005 case GDK_Escape:
cf15ca10
HJYP
1006 if( mw->full_screen )
1007 on_full_screen( NULL, mw );
1d48a247 1008 else
cf15ca10 1009 on_quit( NULL, mw );
1d48a247
HJYP
1010 break;
1011 case GDK_F11:
cf15ca10 1012 on_full_screen( NULL, mw );
1d48a247
HJYP
1013 break;
1014
1015 default:
cf15ca10 1016 GTK_WIDGET_CLASS(main_win_parent_class)->key_press_event( widget, key );
1d48a247
HJYP
1017 }
1018 return FALSE;
1019}
1020
cf15ca10 1021void main_win_center_image( MainWin* mw )
1d48a247
HJYP
1022{
1023 GtkAdjustment *hadj, *vadj;
cf15ca10
HJYP
1024 hadj = gtk_scrolled_window_get_hadjustment((GtkScrolledWindow*)mw->scroll);
1025 vadj = gtk_scrolled_window_get_vadjustment((GtkScrolledWindow*)mw->scroll);
1d48a247 1026
9c52ae58 1027 GtkRequisition req;
cf15ca10 1028 gtk_widget_size_request( (GtkWidget*)mw->img_view, &req );
1d48a247 1029
9c52ae58
HJYP
1030 if( req.width > hadj->page_size )
1031 gtk_adjustment_set_value(hadj, ( hadj->upper - hadj->page_size ) / 2 );
1d48a247 1032
9c52ae58
HJYP
1033 if( req.height > vadj->page_size )
1034 gtk_adjustment_set_value(vadj, ( vadj->upper - vadj->page_size ) / 2 );
1d48a247
HJYP
1035}
1036
cf15ca10 1037void rotate_image( MainWin* mw, GdkPixbufRotation angle )
1d48a247 1038{
cf15ca10 1039 if( ! mw->pix )
1d48a247
HJYP
1040 return;
1041
9c52ae58 1042 GdkPixbuf* rpix = NULL;
cf15ca10
HJYP
1043 rpix = gdk_pixbuf_rotate_simple( mw->pix, angle );
1044 g_object_unref( mw->pix );
1045 mw->pix = rpix;
0c0c8179 1046 image_view_set_pixbuf( (ImageView*)mw->img_view, mw->pix );
1d48a247 1047
cf15ca10 1048 if( mw->zoom_mode == ZOOM_FIT )
c325c989 1049 main_win_fit_window_size( mw, FALSE, GDK_INTERP_BILINEAR );
1d48a247
HJYP
1050}
1051
cf15ca10 1052gboolean main_win_scale_image( MainWin* mw, double new_scale, GdkInterpType type )
1d48a247
HJYP
1053{
1054 if( G_UNLIKELY( new_scale == 1.0 ) )
1055 {
cf15ca10
HJYP
1056 gtk_toggle_button_set_active( (GtkToggleButton*)mw->btn_orig, TRUE );
1057 return TRUE;
1d48a247 1058 }
cf15ca10 1059 mw->scale = new_scale;
0c0c8179 1060 image_view_set_scale( (ImageView*)mw->img_view, new_scale, type );
2c34ffe0 1061
cf15ca10 1062 return TRUE;
1d48a247
HJYP
1063}
1064
cf15ca10 1065gboolean main_win_save( MainWin* mw, const char* file_path, const char* type, gboolean confirm )
1d48a247 1066{
cf15ca10
HJYP
1067 if( ! mw->pix )
1068 return FALSE;
1d48a247
HJYP
1069
1070 if( confirm ) // check existing file
1071 {
1072 if( g_file_test( file_path, G_FILE_TEST_EXISTS ) )
1073 {
cf15ca10 1074 GtkWidget* dlg = gtk_message_dialog_new( (GtkWindow*)mw,
1d48a247
HJYP
1075 GTK_DIALOG_MODAL,
1076 GTK_MESSAGE_QUESTION,
1077 GTK_BUTTONS_YES_NO,
1078 _("The file name you selected already exist.\nDo you want to overwrite existing file?\n(Warning: The quality of original image might be lost)") );
1079 if( gtk_dialog_run( (GtkDialog*)dlg ) != GTK_RESPONSE_YES )
1080 {
1081 gtk_widget_destroy( dlg );
cf15ca10 1082 return FALSE;
1d48a247
HJYP
1083 }
1084 gtk_widget_destroy( dlg );
1085 }
1086 }
1087
1088 GError* err = NULL;
cf15ca10 1089 if( ! gdk_pixbuf_save( mw->pix, file_path, type, &err, NULL ) )
1d48a247 1090 {
cf15ca10
HJYP
1091 main_win_show_error( mw, err->message );
1092 return FALSE;
1d48a247
HJYP
1093 }
1094
cf15ca10 1095 return TRUE;
1d48a247
HJYP
1096}
1097
cf15ca10 1098void on_delete( GtkWidget* btn, MainWin* mw )
1d48a247 1099{
cf15ca10 1100 char* file_path = image_list_get_current_file_path( mw->img_list );
1d48a247
HJYP
1101 if( file_path )
1102 {
cf15ca10 1103 GtkWidget* dlg = gtk_message_dialog_new( (GtkWindow*)mw,
1d48a247
HJYP
1104 GTK_DIALOG_MODAL,
1105 GTK_MESSAGE_QUESTION,
1106 GTK_BUTTONS_YES_NO,
1107 _("Are you sure you want to delete current file?\n\nWarning: Once deleted, the file cannot be recovered.") );
1108 int resp = gtk_dialog_run( (GtkDialog*)dlg );
1109 gtk_widget_destroy( dlg );
1110
1111 if( resp == GTK_RESPONSE_YES )
1112 {
1113 g_unlink( file_path );
1114 if( errno )
cf15ca10 1115 main_win_show_error( mw, g_strerror(errno) );
1d48a247
HJYP
1116 g_free( file_path );
1117 }
1118 }
1119}
1120
cf15ca10 1121void show_popup_menu( MainWin* mw, GdkEventButton* evt )
1d48a247 1122{
5bfec971
HJYP
1123 static PtkMenuItemEntry menu_def[] =
1124 {
1125 PTK_IMG_MENU_ITEM( N_( "Previous" ), GTK_STOCK_GO_BACK, on_prev, GDK_leftarrow, 0 ),
1126 PTK_IMG_MENU_ITEM( N_( "Next" ), GTK_STOCK_GO_FORWARD, on_next, GDK_rightarrow, 0 ),
1127 PTK_SEPARATOR_MENU_ITEM,
1128 PTK_IMG_MENU_ITEM( N_( "Zoom Out" ), GTK_STOCK_ZOOM_OUT, on_zoom_out, GDK_minus, 0 ),
1129 PTK_IMG_MENU_ITEM( N_( "Zoom In" ), GTK_STOCK_ZOOM_IN, on_zoom_in, GDK_plus, 0 ),
1130 PTK_IMG_MENU_ITEM( N_( "Fit Image To Window Size" ), GTK_STOCK_ZOOM_FIT, on_zoom_fit_menu, GDK_F, 0 ),
1131 PTK_IMG_MENU_ITEM( N_( "Original Size" ), GTK_STOCK_ZOOM_100, on_orig_size_menu, GDK_G, 0 ),
1132 PTK_SEPARATOR_MENU_ITEM,
0c8ac162 1133 PTK_IMG_MENU_ITEM( N_( "Full Screen" ), GTK_STOCK_FULLSCREEN, on_full_screen, GDK_F11, 0 ),
5bfec971
HJYP
1134 PTK_SEPARATOR_MENU_ITEM,
1135 PTK_IMG_MENU_ITEM( N_( "Rotate Counterclockwise" ), "gtk-counterclockwise", on_rotate_counterclockwise, GDK_L, 0 ),
1136 PTK_IMG_MENU_ITEM( N_( "Rotate Clockwise" ), "gtk-clockwise", on_rotate_clockwise, GDK_R, 0 ),
1137 PTK_SEPARATOR_MENU_ITEM,
1138 PTK_IMG_MENU_ITEM( N_("Open File"), GTK_STOCK_OPEN, G_CALLBACK(on_open), GDK_O, 0 ),
1139 PTK_IMG_MENU_ITEM( N_("Save File"), GTK_STOCK_SAVE, G_CALLBACK(on_save), GDK_S, 0 ),
1140 PTK_IMG_MENU_ITEM( N_("Save As"), GTK_STOCK_SAVE_AS, G_CALLBACK(on_save_as), GDK_A, 0 ),
1141 PTK_IMG_MENU_ITEM( N_("Delete File"), GTK_STOCK_DELETE, G_CALLBACK(on_delete), GDK_Delete, 0 ),
1142 PTK_SEPARATOR_MENU_ITEM,
1143 PTK_STOCK_MENU_ITEM( GTK_STOCK_ABOUT, on_about ),
1144 PTK_MENU_END
1145 };
1146
cf15ca10 1147 // mw accel group is useless. It's only used to display accels in popup menu
5bfec971 1148 GtkAccelGroup* accel_group = gtk_accel_group_new();
cf15ca10 1149 GtkMenuShell* popup = (GtkMenuShell*)ptk_menu_new_from_data( menu_def, mw, accel_group );
1d48a247
HJYP
1150
1151 gtk_widget_show_all( (GtkWidget*)popup );
1152 g_signal_connect( popup, "selection-done", G_CALLBACK(gtk_widget_destroy), NULL );
1153 gtk_menu_popup( (GtkMenu*)popup, NULL, NULL, NULL, NULL, evt->button, evt->time );
1154}
1155
7c3b160c
HJYP
1156/* callback used to open default browser when URLs got clicked */
1157static void open_url( GtkAboutDialog *dlg, const gchar *url, gpointer data)
1d48a247 1158{
7c3b160c
HJYP
1159 /* FIXME: is there any better way to do this? */
1160 char* programs[] = { "xdg-open", "gnome-open" /* Sorry, KDE users. :-P */, "exo-open" };
1161 int i;
1162 for( i = 0; i < G_N_ELEMENTS(programs); ++i)
1d48a247 1163 {
7c3b160c
HJYP
1164 char* open_cmd = NULL;
1165 if( (open_cmd = g_find_program_in_path( programs[i] )) )
1d48a247 1166 {
7c3b160c
HJYP
1167 char* cmd = g_strdup_printf( "%s \'%s\'", open_cmd, url );
1168 g_spawn_command_line_async( cmd, NULL );
1169 g_free( cmd );
1170 g_free( open_cmd );
1171 break;
1d48a247 1172 }
1d48a247 1173 }
1d48a247
HJYP
1174}
1175
cf15ca10 1176void on_about( GtkWidget* menu, MainWin* mw )
1d48a247
HJYP
1177{
1178 GtkWidget * about_dlg;
1179 const gchar *authors[] =
1180 {
1181 "洪任諭 Hong Jen Yee <pcman.tw@gmail.com>",
7c3b160c
HJYP
1182 "Martin Siggel <martinsiggel@googlemail.com>",
1183 _(" * Refer to source code of EOG image viewer and GThumb"),
1d48a247
HJYP
1184 NULL
1185 };
cf15ca10 1186 /* TRANSLATORS: Replace mw string with your names, one name per line. */
1d48a247
HJYP
1187 gchar *translators = _( "translator-credits" );
1188
7c3b160c
HJYP
1189 gtk_about_dialog_set_url_hook( open_url, mw, NULL);
1190
1d48a247 1191 about_dlg = gtk_about_dialog_new ();
7c3b160c
HJYP
1192
1193 gtk_container_set_border_width ( ( GtkContainer*)about_dlg , 2 );
1194 gtk_about_dialog_set_version ( (GtkAboutDialog*)about_dlg, VERSION );
1195 gtk_about_dialog_set_name ( (GtkAboutDialog*)about_dlg, _( "GPicView" ) );
1196 gtk_about_dialog_set_logo( (GtkAboutDialog*)about_dlg, gdk_pixbuf_new_from_file( PACKAGE_DATA_DIR"/pixmaps/gpicview.png", NULL ) );
1197 gtk_about_dialog_set_copyright ( (GtkAboutDialog*)about_dlg, _( "Copyright (C) 2007" ) );
1198 gtk_about_dialog_set_comments ( (GtkAboutDialog*)about_dlg, _( "Lightweight image viewer from LXDE project" ) );
1199 gtk_about_dialog_set_license ( (GtkAboutDialog*)about_dlg, "GPicView\n\nCopyright (C) 2007 Hong Jen Yee (PCMan)\n\nmw 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\nmw 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 mw program; if not, write to the Free Software\nFoundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA." );
c5a539c7 1200 gtk_about_dialog_set_website ( (GtkAboutDialog*)about_dlg, "http://lxde.org/gpicview/" );
7c3b160c
HJYP
1201 gtk_about_dialog_set_authors ( (GtkAboutDialog*)about_dlg, authors );
1202 gtk_about_dialog_set_translator_credits ( (GtkAboutDialog*)about_dlg, translators );
1203 gtk_window_set_transient_for( (GtkWindow*) about_dlg, GTK_WINDOW( mw ) );
1204
1205 gtk_dialog_run( ( GtkDialog*)about_dlg );
1d48a247
HJYP
1206 gtk_widget_destroy( about_dlg );
1207}
1208
cf15ca10
HJYP
1209void on_drag_data_received( GtkWidget* widget, GdkDragContext *drag_context,
1210 int x, int y, GtkSelectionData* data, guint info, guint time, MainWin* mw )
1d48a247
HJYP
1211{
1212 if( ! data || data->length <= 0)
1213 return;
1214
1d48a247
HJYP
1215 char* file = NULL;
1216 if( info == 0 ) // text/uri-list
1217 {
1218 char** uris = gtk_selection_data_get_uris( data );
1219 if( uris )
1220 {
1221 file = g_filename_from_uri(*uris, NULL, NULL);
1222 g_strfreev( uris );
1223 }
1224 }
1225 else if( info == 1 ) // text/plain
1226 {
1227 file = (char*)gtk_selection_data_get_text( data );
1228 }
1229 if( file )
1230 {
cf15ca10 1231 main_win_open( mw, file, ZOOM_FIT );
1d48a247
HJYP
1232 g_free( file );
1233 }
1234}