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