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 );
312 g_free( dir_path );
313 char* base_name = g_path_get_basename( file_path );
314 img_list.set_current( base_name );
315 g_free( base_name );
316
317 char* disp_path = g_filename_display_name( file_path );
318 gtk_window_set_title( (GtkWindow*)this, disp_path );
319 g_free( disp_path );
320
321 return true;
322}
323
324void MainWin::close()
325{
9c52ae58
HJYP
326 if( pix )
327 {
328 g_object_unref( pix );
329 pix = NULL;
1d48a247
HJYP
330 }
331}
332
333void MainWin::show_error( const char* message )
334{
335 GtkWidget* dlg = gtk_message_dialog_new( (GtkWindow*)this,
336 GTK_DIALOG_MODAL,
337 GTK_MESSAGE_ERROR,
338 GTK_BUTTONS_OK,
339 message );
340 gtk_dialog_run( (GtkDialog*)dlg );
341 gtk_widget_destroy( dlg );
342}
343
344void MainWin::on_size_allocate( GtkWidget* widget, GtkAllocation *allocation )
345{
346 GTK_WIDGET_CLASS(_parent_class)->size_allocate( widget, allocation );
52c867e9 347 if( GTK_WIDGET_REALIZED (widget) )
1d48a247 348 {
52c867e9 349 MainWin* self = (MainWin*)widget;
9c52ae58 350
52c867e9
HJYP
351 if( self->zoom_mode == ZOOM_FIT )
352 {
353 while(gtk_events_pending ())
354 gtk_main_iteration(); // makes it more fluid
1d48a247 355
52c867e9
HJYP
356 allocation = &self->scroll->allocation;
357 self->fit_window_size();
358 }
1d48a247
HJYP
359 }
360}
361
da8bd946 362void MainWin::fit_size( int width, int height, bool can_strech, GdkInterpType type )
1d48a247 363{
9c52ae58 364 if( ! pix )
1d48a247
HJYP
365 return;
366
9c52ae58
HJYP
367 int orig_w = gdk_pixbuf_get_width(pix);
368 int orig_h = gdk_pixbuf_get_height(pix);
1d48a247 369
da8bd946
HJYP
370 if( can_strech || (orig_w > width || orig_h > height) )
371 {
372 double xscale = double(width) / orig_w;
373 double yscale = double(height) / orig_h;
374 double final_scale = xscale < yscale ? xscale : yscale;
1d48a247 375
da8bd946
HJYP
376 scale_image( final_scale, type );
377 }
378 else // use original size if the image is smaller than the window
379 {
380 scale = 1.0;
9c52ae58 381 img_view->set_scale( 1.0, type );
da8bd946 382 }
1d48a247
HJYP
383}
384
da8bd946 385void MainWin::fit_window_size( bool can_strech, GdkInterpType type )
1d48a247 386{
9c52ae58 387 if( pix == NULL )
da8bd946 388 return;
1d48a247
HJYP
389 int space = 0;
390 gtk_widget_style_get( scroll, "scrollbar-spacing", &space, NULL );
391 space *= 2;
da8bd946 392/*
9c52ae58
HJYP
393 int w = gdk_pixbuf_get_width( pix );
394 int h = gdk_pixbuf_get_height( pix );
da8bd946
HJYP
395
396 if( w > scroll->allocation.width - space )
397 w = scroll->allocation.width - space;
398 if( h > scroll->allocation.height - space )
399 h = scroll->allocation.height - space;
400 fit_size( w, h, type );
401*/
402 fit_size( scroll->allocation.width - space, scroll->allocation.height - space, can_strech, type );
1d48a247
HJYP
403}
404
405GtkWidget* MainWin::add_nav_btn( const char* icon, const char* tip, GCallback cb, bool toggle )
406{
407 GtkWidget* img = gtk_image_new_from_stock(icon, GTK_ICON_SIZE_SMALL_TOOLBAR);
408 GtkWidget* btn;
409 if( G_UNLIKELY(toggle) )
410 {
411 btn = gtk_toggle_button_new();
412 g_signal_connect( btn, "toggled", cb, this );
413 }
414 else
415 {
416 btn = gtk_button_new();
417 g_signal_connect( btn, "clicked", cb, this );
418 }
419 gtk_button_set_relief( (GtkButton*)btn, GTK_RELIEF_NONE );
420 gtk_button_set_focus_on_click( (GtkButton*)btn, FALSE );
421 gtk_container_add( (GtkContainer*)btn, img );
422 gtk_tooltips_set_tip( tooltips, btn, tip, NULL );
423 gtk_box_pack_start( (GtkBox*)nav_bar, btn, FALSE, FALSE, 0 );
424 return btn;
425}
426
427void MainWin::on_zoom_fit_menu( GtkMenuItem* item, MainWin* self )
428{
429 gtk_button_clicked( (GtkButton*)self->btn_fit );
430}
431
432void MainWin::on_zoom_fit( GtkToggleButton* btn, MainWin* self )
433{
434 if( ! btn->active )
435 {
436 if( self->zoom_mode == ZOOM_FIT )
437 gtk_toggle_button_set_active( btn, TRUE );
438 return;
439 }
440 self->zoom_mode = ZOOM_FIT;
441
442 gtk_toggle_button_set_active( (GtkToggleButton*)self->btn_orig, FALSE );
443
444 self->fit_window_size();
445}
446
447void MainWin::on_full_screen( GtkWidget* btn, MainWin* self )
448{
449 if( ! self->full_screen )
450 {
5bfec971
HJYP
451 static GdkColor black = {0};
452 gtk_widget_modify_bg( self->evt_box, GTK_STATE_NORMAL, &black );
1d48a247
HJYP
453 gtk_widget_hide( gtk_widget_get_parent(self->nav_bar) );
454 gtk_window_fullscreen( (GtkWindow*)self );
455 }
456 else
457 {
5bfec971
HJYP
458// gtk_widget_reset_rc_styles( self->evt_box );
459 static GdkColor white = {0, 65535, 65535, 65535};
460 gtk_widget_modify_bg( self->evt_box, GTK_STATE_NORMAL, &white );
1d48a247
HJYP
461 gtk_widget_show( gtk_widget_get_parent(self->nav_bar) );
462 gtk_window_unfullscreen( (GtkWindow*)self );
463 }
464 self->full_screen = ! self->full_screen;
465}
466
467void MainWin::on_orig_size_menu( GtkToggleButton* btn, MainWin* self )
468{
469 gtk_button_clicked( (GtkButton*)self->btn_orig );
470}
471
472void MainWin::on_orig_size( GtkToggleButton* btn, MainWin* self )
473{
474 // This callback could be called from activate signal of menu item.
475 if( GTK_IS_MENU_ITEM(btn) )
476 {
477 gtk_button_clicked( (GtkButton*)self->btn_orig );
478 return;
479 }
480
481 if( ! btn->active )
482 {
483 if( self->zoom_mode == ZOOM_ORIG )
484 gtk_toggle_button_set_active( btn, TRUE );
485 return;
486 }
487 self->zoom_mode = ZOOM_ORIG;
488 self->scale = 1.0;
489
490 gtk_toggle_button_set_active( (GtkToggleButton*)self->btn_fit, FALSE );
491
9c52ae58 492 if( ! self->pix )
1d48a247
HJYP
493 return;
494
9c52ae58 495 self->img_view->set_scale( 1.0 );
1d48a247 496
9c52ae58
HJYP
497 while (gtk_events_pending ())
498 gtk_main_iteration ();
499
500 self->center_image(); // FIXME: This doesn't work well. Why?
1d48a247
HJYP
501}
502
503void MainWin::on_prev( GtkWidget* btn, MainWin* self )
504{
505 if( self->img_list.is_empty() )
506 return;
507
508 const char* name = self->img_list.get_prev();
509
510 if( ! name && self->img_list.has_multiple_files() )
511 {
512 // FIXME: need to ask user first?
513 name = self->img_list.get_last();
514 }
515
516 if( name )
517 {
518 char* file_path = self->img_list.get_current_file_path();
519 self->open( file_path );
520 g_free( file_path );
521 }
522}
523
524void MainWin::on_next( GtkWidget* btn, MainWin* self )
525{
526 if( self->img_list.is_empty() )
527 return;
528
529 const char* name = self->img_list.get_next();
530
531 if( ! name && self->img_list.has_multiple_files() )
532 {
533 // FIXME: need to ask user first?
534 name = self->img_list.get_first();
535 }
536
537 if( name )
538 {
539 char* file_path = self->img_list.get_current_file_path();
540 self->open( file_path );
541 g_free( file_path );
542 }
543}
544
545void MainWin::on_rotate_clockwise( GtkWidget* btn, MainWin* self )
546{
547 self->rotate_image( GDK_PIXBUF_ROTATE_CLOCKWISE );
548}
549
550void MainWin::on_rotate_counterclockwise( GtkWidget* btn, MainWin* self )
551{
552 self->rotate_image( GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE );
553}
554
555static void on_update_preview( GtkFileChooser *chooser, GtkImage* img )
556{
557 char* file = gtk_file_chooser_get_preview_filename( chooser );
558 GdkPixbuf* pix = NULL;
559 if( file )
560 {
561 pix = gdk_pixbuf_new_from_file_at_scale( file, 128, 128, TRUE, NULL );
562 g_free( file );
563 }
1d48a247 564 if( pix )
9c52ae58
HJYP
565 {
566 gtk_image_set_from_pixbuf( img, pix );
567 g_object_unref( pix );
568 }
1d48a247
HJYP
569}
570
571void MainWin::on_save_as( GtkWidget* btn, MainWin* self )
572{
9c52ae58 573 if( ! self->pix )
1d48a247
HJYP
574 return;
575
576 GtkFileChooser* dlg = (GtkFileChooser*)gtk_file_chooser_dialog_new( NULL, (GtkWindow*)self,
577 GTK_FILE_CHOOSER_ACTION_SAVE, GTK_STOCK_CANCEL,
578 GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_OK, NULL );
579
580 gtk_file_chooser_set_current_folder( dlg, self->img_list.get_dir() );
581
582 GtkWidget* img = gtk_image_new();
583 gtk_widget_set_size_request( img, 128, 128 );
584 gtk_file_chooser_set_preview_widget( dlg, img );
585 g_signal_connect( dlg, "update-preview", G_CALLBACK(on_update_preview), img );
586
587 GtkFileFilter *filter;
588
589 /*
590 /// TODO: determine file type from file name
591 filter = gtk_file_filter_new();
592 gtk_file_filter_set_name( filter, _("Determined by File Name") );
593 gtk_file_filter_add_pixbuf_formats( filter );
594 gtk_file_chooser_add_filter( dlg, filter );
595 */
596
597 GSList* modules = gdk_pixbuf_get_formats();
598 GSList* module;
599 for( module = modules; module; module = module->next )
600 {
601 GdkPixbufFormat* format = (GdkPixbufFormat*)module->data;
602 if( ! gdk_pixbuf_format_is_writable( format ) )
603 continue;
604
605 filter = gtk_file_filter_new();
606
607 char* desc = gdk_pixbuf_format_get_description( format );
608 char* name = gdk_pixbuf_format_get_name( format );
609 char* tmp = g_strjoin( ": ", name, desc, NULL );
610 g_free( desc );
611 g_free( name );
612 gtk_file_filter_set_name( filter, tmp );
613 g_free( tmp );
614
615 char** mimes = gdk_pixbuf_format_get_mime_types( format ), **mime;
616 for( mime = mimes; *mime ; ++mime )
617 gtk_file_filter_add_mime_type( filter, *mime );
618 g_strfreev( mimes );
619 gtk_file_chooser_add_filter( dlg, filter );
620 }
621
622 if( gtk_dialog_run( (GtkDialog*)dlg ) == GTK_RESPONSE_OK )
623 {
624 filter = gtk_file_chooser_get_filter( dlg );
625 const char* filter_name = gtk_file_filter_get_name( filter );
626 char* p = strstr( filter_name, ": " );
627 char* type = NULL;
628 if( ! p ) // auto detection
629 {
630 /// TODO: auto file type
631 }
632 else
633 {
634 type = g_strndup( filter_name, (p - filter_name) );
635 }
636 char* file = gtk_file_chooser_get_filename( dlg );
637 // g_debug("type = %s", type);
638 self->save( file, type );
639 g_free( file );
640 g_free( type );
641 }
642 gtk_widget_destroy( (GtkWidget*)dlg );
643}
644
645void MainWin::on_save( GtkWidget* btn, MainWin* self )
646{
9c52ae58 647 if( ! self->pix )
1d48a247
HJYP
648 return;
649
650 char* file_name = g_build_filename( self->img_list.get_dir(),
651 self->img_list.get_current(), NULL );
652 GdkPixbufFormat* info;
653 info = gdk_pixbuf_get_file_info( file_name, NULL, NULL );
654 char* type = gdk_pixbuf_format_get_name( info );
655 self->save( file_name, type, true );
656 g_free( file_name );
657 g_free( type );
658}
659
660void MainWin::on_open( GtkWidget* btn, MainWin* self )
661{
662 GtkFileChooser* dlg = (GtkFileChooser*)gtk_file_chooser_dialog_new( NULL, (GtkWindow*)self,
663 GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL,
664 GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL );
665
666 if( self->img_list.get_dir() )
667 gtk_file_chooser_set_current_folder( dlg, self->img_list.get_dir() );
668
669 GtkWidget* img = gtk_image_new();
670 gtk_widget_set_size_request( img, 128, 128 );
671 gtk_file_chooser_set_preview_widget( dlg, img );
672 g_signal_connect( dlg, "update-preview", G_CALLBACK(on_update_preview), img );
673
674 GtkFileFilter *filter = gtk_file_filter_new();
675 gtk_file_filter_set_name( filter, _("All Supported Images") );
676 gtk_file_filter_add_pixbuf_formats( filter );
677 gtk_file_chooser_add_filter( dlg, filter );
678
679 filter = gtk_file_filter_new();
680 gtk_file_filter_set_name( filter, _("All Files") );
681 gtk_file_filter_add_pattern( filter, "*" );
682 gtk_file_chooser_add_filter( dlg, filter );
683
684 char* file = NULL;
685 if( gtk_dialog_run( (GtkDialog*)dlg ) == GTK_RESPONSE_OK )
686 file = gtk_file_chooser_get_filename( dlg );
687 gtk_widget_destroy( (GtkWidget*)dlg );
688
689 if( file )
690 {
d8ce3af1 691 self->open( file, ZOOM_NONE );
1d48a247
HJYP
692 g_free( file );
693 }
694}
695
696void MainWin::on_zoom_in( GtkWidget* btn, MainWin* self )
697{
5bfec971 698 self->zoom_mode = ZOOM_SCALE;
1d48a247
HJYP
699 gtk_toggle_button_set_active( (GtkToggleButton*)self->btn_fit, FALSE );
700 gtk_toggle_button_set_active( (GtkToggleButton*)self->btn_orig, FALSE );
701
702 double scale = self->scale;
9c52ae58 703 if( self->pix && scale < 20.0 )
1d48a247
HJYP
704 {
705// busy(true);
9c52ae58
HJYP
706 scale *= 1.05;
707 if( scale > 20.0 )
708 scale = 20.0;
709 if( self->scale != scale )
710 self->scale_image( scale );
1d48a247
HJYP
711// adjust_adjustment_on_zoom(oldscale);
712// busy(false);
713 }
714}
715
716void MainWin::on_zoom_out( GtkWidget* btn, MainWin* self )
717{
5bfec971 718 self->zoom_mode = ZOOM_SCALE;
1d48a247
HJYP
719 gtk_toggle_button_set_active( (GtkToggleButton*)self->btn_fit, FALSE );
720 gtk_toggle_button_set_active( (GtkToggleButton*)self->btn_orig, FALSE );
721
722 double scale = self->scale;
9c52ae58 723 if( self->pix && scale > 0.02 )
1d48a247
HJYP
724 {
725// busy(true);
da8bd946 726
9c52ae58
HJYP
727 scale /= 1.05;
728 if( scale < 0.02 )
729 scale = 0.02;
730 if( self->scale != scale )
da8bd946 731 self->scale_image( scale );
1d48a247
HJYP
732// adjust_adjustment_on_zoom(oldscale);
733// busy(false);
734 }
735}
736
737void MainWin::on_preference( GtkWidget* btn, MainWin* self )
738{
739 self->show_error( "Not implemented yet!" );
740}
741
742void MainWin::on_quit( GtkWidget* btn, MainWin* self )
743{
744 gtk_widget_destroy( (GtkWidget*)self );
745}
746
747gboolean MainWin::on_button_press( GtkWidget* widget, GdkEventButton* evt, MainWin* self )
748{
9c52ae58
HJYP
749 if( ! GTK_WIDGET_HAS_FOCUS( widget ) )
750 gtk_widget_grab_focus( widget );
5bfec971 751
1d48a247
HJYP
752 if( evt->type == GDK_BUTTON_PRESS)
753 {
754 if( evt->button == 1 ) // left button
755 {
9c52ae58 756 if( ! self->pix )
1d48a247
HJYP
757 return FALSE;
758 self->dragging = true;
759 gtk_widget_get_pointer( (GtkWidget*)self, &self->drag_old_x ,&self->drag_old_y );
760 gdk_window_set_cursor( widget->window, self->hand_cursor );
761 }
762 else if( evt->button == 3 ) // right button
763 {
764 self->show_popup_menu( evt );
765 }
766 }
767 else if( evt->type == GDK_2BUTTON_PRESS && evt->button == 1 ) // double clicked
768 {
769 on_full_screen( NULL, self );
770 }
1d48a247
HJYP
771 return FALSE;
772}
773
774gboolean MainWin::on_mouse_move( GtkWidget* widget, GdkEventMotion* evt, MainWin* self )
775{
776 if( ! self->dragging )
1d48a247 777 return FALSE;
9c52ae58 778
1d48a247
HJYP
779 int cur_x, cur_y;
780 gtk_widget_get_pointer( (GtkWidget*)self, &cur_x ,&cur_y );
781
782 int dx = (self->drag_old_x - cur_x);
783 int dy = (self->drag_old_y - cur_y);
784
785 GtkAdjustment *hadj, *vadj;
786 hadj = gtk_scrolled_window_get_hadjustment((GtkScrolledWindow*)self->scroll);
787 vadj = gtk_scrolled_window_get_vadjustment((GtkScrolledWindow*)self->scroll);
788
9c52ae58
HJYP
789 GtkRequisition req;
790 gtk_widget_size_request( (GtkWidget*)self->img_view, &req );
1d48a247
HJYP
791
792 if( ABS(dx) > 4 )
793 {
794 self->drag_old_x = cur_x;
9c52ae58 795 if( req.width > hadj->page_size )
1d48a247
HJYP
796 {
797 gdouble x = gtk_adjustment_get_value (hadj) + dx;
798 if( x < hadj->lower )
799 x = hadj->lower;
800 else if( (x + hadj->page_size) > hadj->upper )
801 x = hadj->upper - hadj->page_size;
802
803 if( x != hadj->value )
804 gtk_adjustment_set_value (hadj, x );
805 }
806 }
807
808 if( ABS(dy) > 4 )
809 {
9c52ae58 810 if( req.height > vadj->page_size )
1d48a247
HJYP
811 {
812 self->drag_old_y = cur_y;
813 gdouble y = gtk_adjustment_get_value (vadj) + dy;
814 if( y < vadj->lower )
815 y = vadj->lower;
816 else if( (y + vadj->page_size) > vadj->upper )
817 y = vadj->upper - vadj->page_size;
818
819 if( y != vadj->value )
820 gtk_adjustment_set_value (vadj, y );
821 }
822 }
823 return FALSE;
824}
825
826gboolean MainWin::on_button_release( GtkWidget* widget, GdkEventButton* evt, MainWin* self )
827{
828 self->dragging = false;
829 gdk_window_set_cursor( widget->window, NULL );
830 return FALSE;
831}
832
5bfec971
HJYP
833gboolean MainWin::on_scroll_event( GtkWidget* widget, GdkEventScroll* evt, MainWin* self )
834{
835 switch( evt->direction )
836 {
837 case GDK_SCROLL_UP:
838 on_zoom_out( NULL, self );
839 break;
840 case GDK_SCROLL_DOWN:
841 on_zoom_in( NULL, self );
842 break;
843 case GDK_SCROLL_LEFT:
844 on_prev( NULL, self );
845 break;
846 case GDK_SCROLL_RIGHT:
847 on_next( NULL, self );
848 break;
849 }
da8bd946 850 return TRUE;
5bfec971
HJYP
851}
852
1d48a247
HJYP
853gboolean MainWin::on_key_press_event(GtkWidget* widget, GdkEventKey * key)
854{
855 MainWin* self = (MainWin*)widget;
856 switch( key->keyval )
857 {
5bfec971
HJYP
858 case GDK_Left:
859 case GDK_KP_Left:
860 case GDK_leftarrow:
1d48a247
HJYP
861 case GDK_Return:
862 case GDK_space:
863 case GDK_Next:
5bfec971
HJYP
864 case GDK_KP_Down:
865 case GDK_Down:
866 case GDK_downarrow:
1d48a247
HJYP
867 on_next( NULL, self );
868 break;
5bfec971
HJYP
869 case GDK_Right:
870 case GDK_KP_Right:
871 case GDK_rightarrow:
1d48a247
HJYP
872 case GDK_Prior:
873 case GDK_BackSpace:
5bfec971
HJYP
874 case GDK_KP_Up:
875 case GDK_Up:
876 case GDK_uparrow:
1d48a247
HJYP
877 on_prev( NULL, self );
878 break;
879 case GDK_KP_Add:
880 case GDK_plus:
641adad3 881 case GDK_equal:
1d48a247
HJYP
882 on_zoom_in( NULL, self );
883 break;
884 case GDK_KP_Subtract:
885 case GDK_minus:
886 on_zoom_out( NULL, self );
887 break;
1d48a247 888 case GDK_s:
641adad3 889// case GDK_S:
1d48a247
HJYP
890 on_save( NULL, self );
891 break;
892 case GDK_l:
5bfec971 893// case GDK_L:
1d48a247
HJYP
894 on_rotate_counterclockwise( NULL, self );
895 break;
896 case GDK_r:
5bfec971 897// case GDK_R:
1d48a247
HJYP
898 on_rotate_clockwise( NULL, self );
899 break;
900 case GDK_f:
5bfec971 901// case GDK_F:
1d48a247
HJYP
902 if( self->zoom_mode != ZOOM_FIT )
903 gtk_button_clicked((GtkButton*)self->btn_fit );
904 break;
905 case GDK_g:
5bfec971 906// case GDK_G:
1d48a247
HJYP
907 if( self->zoom_mode != ZOOM_ORIG )
908 gtk_button_clicked((GtkButton*)self->btn_orig );
909 break;
910 case GDK_o:
5bfec971
HJYP
911// case GDK_O:
912 on_open( NULL, self );
913 break;
914 case GDK_Delete:
915 case GDK_d:
916// case GDK_D:
917 on_delete( NULL, self );
1d48a247
HJYP
918 break;
919 case GDK_Escape:
920 if( self->full_screen )
921 on_full_screen( NULL, self );
922 else
923 on_quit( NULL, self );
924 break;
925 case GDK_F11:
926 on_full_screen( NULL, self );
927 break;
928
929 default:
930 GTK_WIDGET_CLASS(_parent_class)->key_press_event( widget, key );
931 }
932 return FALSE;
933}
934
935void MainWin::center_image()
936{
937 GtkAdjustment *hadj, *vadj;
938 hadj = gtk_scrolled_window_get_hadjustment((GtkScrolledWindow*)scroll);
939 vadj = gtk_scrolled_window_get_vadjustment((GtkScrolledWindow*)scroll);
940
9c52ae58
HJYP
941 GtkRequisition req;
942 gtk_widget_size_request( (GtkWidget*)img_view, &req );
1d48a247 943
9c52ae58
HJYP
944 if( req.width > hadj->page_size )
945 gtk_adjustment_set_value(hadj, ( hadj->upper - hadj->page_size ) / 2 );
1d48a247 946
9c52ae58
HJYP
947 if( req.height > vadj->page_size )
948 gtk_adjustment_set_value(vadj, ( vadj->upper - vadj->page_size ) / 2 );
1d48a247
HJYP
949}
950
951void MainWin::rotate_image( GdkPixbufRotation angle )
952{
9c52ae58 953 if( ! pix )
1d48a247
HJYP
954 return;
955
9c52ae58
HJYP
956 GdkPixbuf* rpix = NULL;
957 rpix = gdk_pixbuf_rotate_simple( pix, angle );
958 g_object_unref( pix );
959 pix = rpix;
960 img_view->set_pixbuf( pix );
1d48a247 961
1d48a247
HJYP
962 if( zoom_mode == ZOOM_FIT )
963 fit_window_size();
1d48a247
HJYP
964}
965
966bool MainWin::scale_image( double new_scale, GdkInterpType type )
967{
968 if( G_UNLIKELY( new_scale == 1.0 ) )
969 {
970 gtk_toggle_button_set_active( (GtkToggleButton*)btn_orig, TRUE );
971 return true;
972 }
9c52ae58
HJYP
973 scale = new_scale;
974 img_view->set_scale( new_scale, type );
2c34ffe0 975
9c52ae58 976 return true;
1d48a247
HJYP
977}
978
979bool MainWin::save( const char* file_path, const char* type, bool confirm )
980{
9c52ae58 981 if( ! pix )
1d48a247
HJYP
982 return false;
983
984 if( confirm ) // check existing file
985 {
986 if( g_file_test( file_path, G_FILE_TEST_EXISTS ) )
987 {
988 GtkWidget* dlg = gtk_message_dialog_new( (GtkWindow*)this,
989 GTK_DIALOG_MODAL,
990 GTK_MESSAGE_QUESTION,
991 GTK_BUTTONS_YES_NO,
992 _("The file name you selected already exist.\nDo you want to overwrite existing file?\n(Warning: The quality of original image might be lost)") );
993 if( gtk_dialog_run( (GtkDialog*)dlg ) != GTK_RESPONSE_YES )
994 {
995 gtk_widget_destroy( dlg );
996 return false;
997 }
998 gtk_widget_destroy( dlg );
999 }
1000 }
1001
1002 GError* err = NULL;
9c52ae58 1003 if( ! gdk_pixbuf_save( pix, file_path, type, &err, NULL ) )
1d48a247
HJYP
1004 {
1005 show_error( err->message );
1006 return false;
1007 }
1008
1009 return true;
1010}
1011
1012void MainWin::on_delete( GtkWidget* btn, MainWin* self )
1013{
1014 char* file_path = self->img_list.get_current_file_path();
1015 if( file_path )
1016 {
1017 GtkWidget* dlg = gtk_message_dialog_new( (GtkWindow*)self,
1018 GTK_DIALOG_MODAL,
1019 GTK_MESSAGE_QUESTION,
1020 GTK_BUTTONS_YES_NO,
1021 _("Are you sure you want to delete current file?\n\nWarning: Once deleted, the file cannot be recovered.") );
1022 int resp = gtk_dialog_run( (GtkDialog*)dlg );
1023 gtk_widget_destroy( dlg );
1024
1025 if( resp == GTK_RESPONSE_YES )
1026 {
1027 g_unlink( file_path );
1028 if( errno )
1029 self->show_error( g_strerror(errno) );
1030 g_free( file_path );
1031 }
1032 }
1033}
1034
5bfec971 1035#include "ptk-menu.h"
1d48a247
HJYP
1036void MainWin::show_popup_menu( GdkEventButton* evt )
1037{
5bfec971 1038/*
1d48a247
HJYP
1039 GtkMenuShell* popup = (GtkMenuShell*)gtk_menu_new();
1040
1041 GtkWidget *item;
1042 add_menu_item( popup, _("Previous"), GTK_STOCK_GO_BACK, G_CALLBACK(on_prev) );
1043 add_menu_item( popup, _("Next"), GTK_STOCK_GO_FORWARD, G_CALLBACK(on_next) );
1044
1045 gtk_menu_shell_append( popup, gtk_separator_menu_item_new() );
1046
1047 add_menu_item( popup, _("Zoom Out"), GTK_STOCK_ZOOM_OUT, G_CALLBACK(on_zoom_out) );
1048 add_menu_item( popup, _("Zoom In"), GTK_STOCK_ZOOM_IN, G_CALLBACK(on_zoom_in) );
1049 add_menu_item( popup, _("Fit Image To Window Size"), GTK_STOCK_ZOOM_OUT,
1050 G_CALLBACK(on_zoom_fit_menu) );
1051 add_menu_item( popup, _("Original Size"), GTK_STOCK_ZOOM_100,
1052 G_CALLBACK(on_orig_size_menu) );
1053
1054#ifndef GTK_STOCK_FULLSCREEN
1055// This stock item is only available after gtk+ 2.8
1056#define GTK_STOCK_FULLSCREEN "gtk-fullscreen"
1057#endif
1058 add_menu_item( popup, _("Full Screen"), GTK_STOCK_FULLSCREEN, G_CALLBACK(on_full_screen) );
1059
1060 gtk_menu_shell_append( popup, gtk_separator_menu_item_new() );
1061
1062 add_menu_item( popup, _("Rotate Counterclockwise"), "gtk-counterclockwise",
1063 G_CALLBACK(on_rotate_counterclockwise) );
1064 add_menu_item( popup, _("Rotate Clockwise"), "gtk-clockwise",
1065 G_CALLBACK(on_rotate_clockwise) );
1066
1067 gtk_menu_shell_append( popup, gtk_separator_menu_item_new() );
1068
1069 add_menu_item( popup, _("Open File"), GTK_STOCK_OPEN, G_CALLBACK(on_open) );
1070 add_menu_item( popup, _("Save File"), GTK_STOCK_SAVE, G_CALLBACK(on_save) );
1071 add_menu_item( popup, _("Save As"), GTK_STOCK_SAVE_AS, G_CALLBACK(on_save_as) );
1072 add_menu_item( popup, _("Delete File"), GTK_STOCK_DELETE, G_CALLBACK(on_delete) );
1073
1074 gtk_menu_shell_append( popup, gtk_separator_menu_item_new() );
1075
1076 item = gtk_image_menu_item_new_from_stock( GTK_STOCK_ABOUT, NULL );
1077 g_signal_connect(item, "activate", G_CALLBACK(on_about), this);
1078 gtk_menu_shell_append( popup, item );
5bfec971 1079*/
1d48a247 1080
5bfec971
HJYP
1081 static PtkMenuItemEntry menu_def[] =
1082 {
1083 PTK_IMG_MENU_ITEM( N_( "Previous" ), GTK_STOCK_GO_BACK, on_prev, GDK_leftarrow, 0 ),
1084 PTK_IMG_MENU_ITEM( N_( "Next" ), GTK_STOCK_GO_FORWARD, on_next, GDK_rightarrow, 0 ),
1085 PTK_SEPARATOR_MENU_ITEM,
1086 PTK_IMG_MENU_ITEM( N_( "Zoom Out" ), GTK_STOCK_ZOOM_OUT, on_zoom_out, GDK_minus, 0 ),
1087 PTK_IMG_MENU_ITEM( N_( "Zoom In" ), GTK_STOCK_ZOOM_IN, on_zoom_in, GDK_plus, 0 ),
1088 PTK_IMG_MENU_ITEM( N_( "Fit Image To Window Size" ), GTK_STOCK_ZOOM_FIT, on_zoom_fit_menu, GDK_F, 0 ),
1089 PTK_IMG_MENU_ITEM( N_( "Original Size" ), GTK_STOCK_ZOOM_100, on_orig_size_menu, GDK_G, 0 ),
1090 PTK_SEPARATOR_MENU_ITEM,
0c8ac162 1091 PTK_IMG_MENU_ITEM( N_( "Full Screen" ), GTK_STOCK_FULLSCREEN, on_full_screen, GDK_F11, 0 ),
5bfec971
HJYP
1092 PTK_SEPARATOR_MENU_ITEM,
1093 PTK_IMG_MENU_ITEM( N_( "Rotate Counterclockwise" ), "gtk-counterclockwise", on_rotate_counterclockwise, GDK_L, 0 ),
1094 PTK_IMG_MENU_ITEM( N_( "Rotate Clockwise" ), "gtk-clockwise", on_rotate_clockwise, GDK_R, 0 ),
1095 PTK_SEPARATOR_MENU_ITEM,
1096 PTK_IMG_MENU_ITEM( N_("Open File"), GTK_STOCK_OPEN, G_CALLBACK(on_open), GDK_O, 0 ),
1097 PTK_IMG_MENU_ITEM( N_("Save File"), GTK_STOCK_SAVE, G_CALLBACK(on_save), GDK_S, 0 ),
1098 PTK_IMG_MENU_ITEM( N_("Save As"), GTK_STOCK_SAVE_AS, G_CALLBACK(on_save_as), GDK_A, 0 ),
1099 PTK_IMG_MENU_ITEM( N_("Delete File"), GTK_STOCK_DELETE, G_CALLBACK(on_delete), GDK_Delete, 0 ),
1100 PTK_SEPARATOR_MENU_ITEM,
1101 PTK_STOCK_MENU_ITEM( GTK_STOCK_ABOUT, on_about ),
1102 PTK_MENU_END
1103 };
1104
1105 // This accel group is useless. It's only used to display accels in popup menu
1106 GtkAccelGroup* accel_group = gtk_accel_group_new();
1107 GtkMenuShell* popup = (GtkMenuShell*)ptk_menu_new_from_data( menu_def, this, accel_group );
1d48a247
HJYP
1108
1109 gtk_widget_show_all( (GtkWidget*)popup );
1110 g_signal_connect( popup, "selection-done", G_CALLBACK(gtk_widget_destroy), NULL );
1111 gtk_menu_popup( (GtkMenu*)popup, NULL, NULL, NULL, NULL, evt->button, evt->time );
1112}
1113
5bfec971 1114/*
1d48a247
HJYP
1115GtkWidget* MainWin::add_menu_item( GtkMenuShell* menu, const char* label,
1116 const char* icon, GCallback cb, bool toggle )
1117{
1118 GtkWidget* item;
1119 if( G_UNLIKELY(toggle) )
1120 {
1121 item = gtk_check_menu_item_new_with_mnemonic( label );
1122 g_signal_connect( item, "toggled", cb, this );
1123 }
1124 else
1125 {
1126 if( icon )
1127 {
1128 item = gtk_image_menu_item_new_with_mnemonic( label);
1129 GtkWidget* img = gtk_image_new_from_stock( icon, GTK_ICON_SIZE_MENU );
1130 gtk_image_menu_item_set_image( (GtkImageMenuItem*)item, img );
1131 }
1132 else {
1133 item = gtk_menu_item_new_with_mnemonic( label );
1134 }
1135 g_signal_connect( item, "activate", cb, this );
1136 }
1137 gtk_menu_shell_append( (GtkMenuShell*)menu, item );
1138}
5bfec971 1139*/
1d48a247
HJYP
1140
1141void MainWin::on_about( GtkWidget* menu, MainWin* self )
1142{
1143 GtkWidget * about_dlg;
1144 const gchar *authors[] =
1145 {
1146 "洪任諭 Hong Jen Yee <pcman.tw@gmail.com>",
9c52ae58 1147 _(" * Refer to source code of EOG image viewer"),
1d48a247
HJYP
1148 _(" * Some icons are taken from gimmage"),
1149 NULL
1150 };
1151 /* TRANSLATORS: Replace this string with your names, one name per line. */
1152 gchar *translators = _( "translator-credits" );
1153
1154 about_dlg = gtk_about_dialog_new ();
1155 gtk_container_set_border_width ( GTK_CONTAINER ( about_dlg ), 2 );
1156 gtk_about_dialog_set_version ( GTK_ABOUT_DIALOG ( about_dlg ), VERSION );
1157 gtk_about_dialog_set_name ( GTK_ABOUT_DIALOG ( about_dlg ), _( "GPicView" ) );
1158 gtk_about_dialog_set_copyright ( GTK_ABOUT_DIALOG ( about_dlg ), _( "Copyright (C) 2007" ) );
1159 gtk_about_dialog_set_comments ( GTK_ABOUT_DIALOG ( about_dlg ), _( "Lightweight image viewer\n\nDeveloped by Hon Jen Yee (PCMan)" ) );
1160 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 1161 gtk_about_dialog_set_website ( GTK_ABOUT_DIALOG ( about_dlg ), "http://lxde.sourceforge.net/gpicview/" );
1d48a247
HJYP
1162 gtk_about_dialog_set_authors ( GTK_ABOUT_DIALOG ( about_dlg ), authors );
1163 gtk_about_dialog_set_translator_credits ( GTK_ABOUT_DIALOG ( about_dlg ), translators );
1164 gtk_window_set_transient_for( GTK_WINDOW( about_dlg ), GTK_WINDOW( self ) );
1165
1166 gtk_dialog_run( GTK_DIALOG( about_dlg ) );
1167 gtk_widget_destroy( about_dlg );
1168}
1169
1170void MainWin::on_drag_data_received( GtkWidget* widget, GdkDragContext *drag_context,
1171 int x, int y, GtkSelectionData* data, guint info, guint time, MainWin* self )
1172{
1173 if( ! data || data->length <= 0)
1174 return;
1175
1d48a247
HJYP
1176 char* file = NULL;
1177 if( info == 0 ) // text/uri-list
1178 {
1179 char** uris = gtk_selection_data_get_uris( data );
1180 if( uris )
1181 {
1182 file = g_filename_from_uri(*uris, NULL, NULL);
1183 g_strfreev( uris );
1184 }
1185 }
1186 else if( info == 1 ) // text/plain
1187 {
1188 file = (char*)gtk_selection_data_get_text( data );
1189 }
1190 if( file )
1191 {
1192 self->open( file );
1193 g_free( file );
1194 }
1195}