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