gth-viewer.c 58.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/*
 *  GThumb
 *
 *  Copyright (C) 2005 Free Software Foundation, Inc.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
 */

#include <config.h>

25 26
#include <string.h>

27
#include <gdk/gdkkeysyms.h>
28 29 30
#include <gtk/gtk.h>
#include <glade/glade.h>
#include <libgnomevfs/gnome-vfs-utils.h>
31
#include <libgnomevfs/gnome-vfs-mime.h>
32 33 34 35

#include "jpegutils/jpeg-data.h"
#include "comments.h"
#include "dlg-save-image.h"
36 37 38 39 40 41 42
#include "dlg-categories.h"
#include "dlg-comment.h"
#include "file-data.h"
#include "file-utils.h"
#include "gconf-utils.h"
#include "glib-utils.h"
#include "gth-viewer.h"
43 44 45
#include "gth-viewer-ui.h"
#include "gth-exif-data-viewer.h"
#include "gth-exif-utils.h"
Paolo Bacchilega's avatar
Paolo Bacchilega committed
46
#include "gth-fullscreen.h"
47
#include "gtk-utils.h"
48
#include "main.h"
49
#include "gth-nav-window.h"
50 51
#include "image-viewer.h"
#include "preferences.h"
52
#include "rotation-utils.h"
53
#include "typedefs.h"
54
#include "dlg-scripts.h"
55 56 57 58 59
#include "icons/pixbufs.h"

#define GLADE_EXPORTER_FILE "gthumb_png_exporter.glade"
#define DEFAULT_WIN_WIDTH 200
#define DEFAULT_WIN_HEIGHT 400
Paolo Bacchilega's avatar
Paolo Bacchilega committed
60
#define DEFAULT_COMMENT_PANE_SIZE 100
61 62
#define DISPLAY_PROGRESS_DELAY 750
#define PANE_MIN_SIZE 60
63
#define GCONF_NOTIFICATIONS 5
64
#define OPEN_TOOLITEM_POS 0
65 66


67 68 69 70
GthViewer *SingleViewer = NULL;
GthViewer *LastFocusedViewer = NULL;


71 72 73 74 75 76 77 78 79 80 81
enum {
	TARGET_PLAIN,
	TARGET_PLAIN_UTF8,
	TARGET_URILIST,
};

static GtkTargetEntry target_table[] = {
	{ "text/uri-list", 0, TARGET_URILIST },
};


82 83 84 85 86 87 88 89 90 91 92 93 94 95
struct _GthViewerPrivateData {
	GtkUIManager    *ui;
	GtkWidget       *toolbar;
	GtkWidget       *statusbar;
	GtkWidget       *image_popup_menu;
	GtkWidget       *viewer;
	GtkWidget       *image_data_hpaned;
	GtkWidget       *image_comment;
	GtkWidget       *exif_data_viewer;
	GtkWidget       *image_info;            /* statusbar widgets. */
	GtkWidget       *image_info_frame;
	GtkWidget       *zoom_info;
	GtkWidget       *zoom_info_frame;

96 97
	GtkWidget       *image_main_pane;

98
	GtkWidget       *comment_button;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
99
	GtkWidget       *fullscreen;
100 101 102

	GtkActionGroup  *actions;

103 104
	GtkToolItem     *open_with_tool_item;
	GtkWidget       *open_with_popup_menu;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
105 106
	GtkToolItem     *rotate_tool_item;

107 108 109 110 111 112 113 114 115
	GtkTooltips     *tooltips;
        guint            help_message_cid;
        guint            image_info_cid;
        guint            progress_cid;

	gboolean         first_time_show;
	guint            first_timeout_handle;

	gboolean         image_data_visible;
116 117
	FileData        *image;
	gboolean         image_error;
118

119 120 121 122
	/* misc */

	guint            cnxn_id[GCONF_NOTIFICATIONS];

123 124 125 126 127
	/* save_pixbuf data */

	gboolean         image_modified;
	gboolean         saving_modified_image;
	ImageSavedFunc   image_saved_func;
128
	FileData        *new_image;
129

130 131 132
	GthPixbufOp     *pixop;
	gboolean         pixop_preview;
	gboolean         closing;
133

134 135
	/* progress dialog */

136 137 138 139 140
	GladeXML        *progress_gui;
	GtkWidget       *progress_dialog;
	GtkWidget       *progress_progressbar;
	GtkWidget       *progress_info;
	guint            progress_timeout;
141 142

	GdkPixbuf       *folder;
143 144 145 146 147 148 149
};

static GthWindowClass *parent_class = NULL;


static void
set_action_active (GthViewer   *viewer,
150
		   const char  *action_name,
151 152 153 154 155 156 157 158 159 160
		   gboolean     is_active)
{
	GtkAction *action;
	action = gtk_action_group_get_action (viewer->priv->actions, action_name);
	gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), is_active);
}


static void
set_action_sensitive (GthViewer  *viewer,
161
	       const char *action_name,
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
	       gboolean    sensitive)
{
	GtkAction *action;
	action = gtk_action_group_get_action (viewer->priv->actions, action_name);
	g_object_set (action, "sensitive", sensitive, NULL);
}


static void
viewer_update_zoom_sensitivity (GthViewer *viewer)
{
	GthViewerPrivateData  *priv = viewer->priv;
	ImageViewer           *image_viewer = (ImageViewer*) priv->viewer;
	gboolean               image_is_visible;
	gboolean               image_is_void;
	int                    zoom;

179
	image_is_visible = (priv->image != NULL) && ! priv->image_error;
180 181 182
	image_is_void = image_viewer_is_void (image_viewer);
	zoom = (int) (image_viewer->zoom_level * 100.0);

183
	set_action_sensitive (viewer,
184 185
		       "View_Zoom100",
		       image_is_visible && !image_is_void && (zoom != 100));
186
	set_action_sensitive (viewer,
187 188
		       "View_ZoomIn",
		       image_is_visible && !image_is_void && (zoom != 10000));
189
	set_action_sensitive (viewer,
190 191
		       "View_ZoomOut",
		       image_is_visible && !image_is_void && (zoom != 5));
192
	set_action_sensitive (viewer,
193 194
		       "View_ZoomFit",
		       image_is_visible && !image_is_void);
195 196 197
        set_action_sensitive (viewer,
                       "View_ZoomWidth",
                       image_is_visible && !image_is_void);
198 199 200 201 202 203
}


static void
viewer_update_sensitivity (GthViewer *viewer)
{
204
	GthViewerPrivateData *priv = viewer->priv;
205 206 207
	gboolean image_is_void;
	gboolean image_is_ani;
	gboolean playing;
208 209
	int      i, number_of_scripts;
        char    *script_name;
210 211 212 213 214

	image_is_void = image_viewer_is_void (IMAGE_VIEWER (priv->viewer));
	image_is_ani = image_viewer_is_animation (IMAGE_VIEWER (priv->viewer));
	playing = image_viewer_is_playing_animation (IMAGE_VIEWER (priv->viewer));

215 216 217 218
	set_action_sensitive (viewer, "File_Save", ! image_is_void && priv->image_modified);
	set_action_sensitive (viewer, "File_SaveAs", ! image_is_void);
	set_action_sensitive (viewer, "File_Revert", ! image_is_void && priv->image_modified);
	set_action_sensitive (viewer, "File_Print", ! image_is_void);
219
	set_action_sensitive (viewer, "ToolBar_Print", ! image_is_void);
220

221 222 223
	set_action_sensitive (viewer, "Edit_Undo", gth_window_get_can_undo (GTH_WINDOW (viewer)));
	set_action_sensitive (viewer, "Edit_Redo", gth_window_get_can_redo (GTH_WINDOW (viewer)));

224 225 226 227 228 229 230 231
	set_action_sensitive (viewer, "AlterImage_Rotate90", ! image_is_void && ! image_is_ani);
	set_action_sensitive (viewer, "AlterImage_Rotate90CC", ! image_is_void && ! image_is_ani);
	set_action_sensitive (viewer, "AlterImage_Flip", ! image_is_void && ! image_is_ani);
	set_action_sensitive (viewer, "AlterImage_Mirror", ! image_is_void && ! image_is_ani);
	set_action_sensitive (viewer, "AlterImage_Desaturate", ! image_is_void && ! image_is_ani);
	set_action_sensitive (viewer, "AlterImage_Resize", ! image_is_void && ! image_is_ani);
	set_action_sensitive (viewer, "AlterImage_ColorBalance", ! image_is_void && ! image_is_ani);
	set_action_sensitive (viewer, "AlterImage_HueSaturation", ! image_is_void && ! image_is_ani);
232
	set_action_sensitive (viewer, "AlterImage_RedeyeRemoval", ! image_is_void && ! image_is_ani);
233 234 235 236 237 238
	set_action_sensitive (viewer, "AlterImage_BrightnessContrast", ! image_is_void && ! image_is_ani);
	set_action_sensitive (viewer, "AlterImage_Invert", ! image_is_void && ! image_is_ani);
	set_action_sensitive (viewer, "AlterImage_Posterize", ! image_is_void && ! image_is_ani);
	set_action_sensitive (viewer, "AlterImage_Equalize", ! image_is_void && ! image_is_ani);
	set_action_sensitive (viewer, "AlterImage_AdjustLevels", ! image_is_void && ! image_is_ani);
	set_action_sensitive (viewer, "AlterImage_Crop", ! image_is_void && ! image_is_ani);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
239 240
	set_action_sensitive (viewer, "AlterImage_Dither_BW", ! image_is_void && ! image_is_ani);
	set_action_sensitive (viewer, "AlterImage_Dither_Web", ! image_is_void && ! image_is_ani);
241 242 243

	set_action_sensitive (viewer, "View_PlayAnimation", image_is_ani);
	set_action_sensitive (viewer, "View_StepAnimation", image_is_ani && ! playing);
244

245 246 247 248 249 250 251
        /* Scripts menu */
        number_of_scripts = eel_gconf_get_integer (PREF_HOTKEY_PREFIX "number_of_scripts", 10);
        for (i = 0; i < number_of_scripts; i++) {
                script_name = g_strdup_printf ("Script_%d", i);
                set_action_sensitive (viewer, script_name, !image_is_void);
                g_free (script_name);
        }
252

253 254
	set_action_sensitive (viewer, "Upload_Flickr", !image_is_void);
	
255 256

	viewer_update_zoom_sensitivity (viewer);
257 258 259
}


260
static void
261 262 263
gth_viewer_finalize (GObject *object)
{
	GthViewer *viewer = GTH_VIEWER (object);
264
	int        i;
265 266 267 268 269 270

	debug (DEBUG_INFO, "Gth::Viewer::Finalize");

	if (viewer->priv != NULL) {
		GthViewerPrivateData *priv = viewer->priv;

271 272 273 274 275 276
		for (i = 0; i < GCONF_NOTIFICATIONS; i++)
			if (viewer->priv->cnxn_id[i] != -1)
				eel_gconf_notification_remove (viewer->priv->cnxn_id[i]);

		file_data_unref (priv->image);
		file_data_unref (priv->new_image);
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344

		g_free (viewer->priv);
		viewer->priv = NULL;
	}

	G_OBJECT_CLASS (parent_class)->finalize (object);
}


static void
menu_item_select_cb (GtkMenuItem *proxy,
		     GthViewer   *viewer)
{
        GtkAction *action;
        char      *message;

        action = g_object_get_data (G_OBJECT (proxy),  "gtk-action");
        g_return_if_fail (action != NULL);

        g_object_get (G_OBJECT (action), "tooltip", &message, NULL);
        if (message) {
		gtk_statusbar_push (GTK_STATUSBAR (viewer->priv->statusbar),
				    viewer->priv->help_message_cid, message);
		g_free (message);
        }
}


static void
menu_item_deselect_cb (GtkMenuItem *proxy,
		       GthViewer   *viewer)
{
        gtk_statusbar_pop (GTK_STATUSBAR (viewer->priv->statusbar),
                           viewer->priv->help_message_cid);
}


static void
disconnect_proxy_cb (GtkUIManager *manager,
                     GtkAction    *action,
                     GtkWidget    *proxy,
                     GthViewer    *viewer)
{
        if (GTK_IS_MENU_ITEM (proxy)) {
                g_signal_handlers_disconnect_by_func
                        (proxy, G_CALLBACK (menu_item_select_cb), viewer);
                g_signal_handlers_disconnect_by_func
                        (proxy, G_CALLBACK (menu_item_deselect_cb), viewer);
        }
}


static void
connect_proxy_cb (GtkUIManager *manager,
                  GtkAction    *action,
                  GtkWidget    *proxy,
		  GthViewer    *viewer)
{
        if (GTK_IS_MENU_ITEM (proxy)) {
		g_signal_connect (proxy, "select",
				  G_CALLBACK (menu_item_select_cb), viewer);
		g_signal_connect (proxy, "deselect",
				  G_CALLBACK (menu_item_deselect_cb), viewer);
	}
}


static void
345 346
gth_viewer_set_toolbar_visibility (GthViewer *viewer,
				   gboolean   visible)
347 348
{
	g_return_if_fail (viewer != NULL);
349 350 351

	set_action_active (viewer, "View_Toolbar", visible);
	if (visible)
352
		gtk_widget_show (viewer->priv->toolbar);
353
	else
354
		gtk_widget_hide (viewer->priv->toolbar);
355 356 357
}


358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
static void
gth_viewer_notify_update_toolbar_style (GthViewer *viewer)
{
	GthToolbarStyle toolbar_style;
	GtkToolbarStyle prop = GTK_TOOLBAR_BOTH;

	toolbar_style = pref_get_real_toolbar_style ();

	switch (toolbar_style) {
	case GTH_TOOLBAR_STYLE_TEXT_BELOW:
		prop = GTK_TOOLBAR_BOTH; break;
	case GTH_TOOLBAR_STYLE_TEXT_BESIDE:
		prop = GTK_TOOLBAR_BOTH_HORIZ; break;
	case GTH_TOOLBAR_STYLE_ICONS:
		prop = GTK_TOOLBAR_ICONS; break;
	case GTH_TOOLBAR_STYLE_TEXT:
		prop = GTK_TOOLBAR_TEXT; break;
	default:
		break;
	}

	gtk_toolbar_set_style (GTK_TOOLBAR (viewer->priv->toolbar), prop);
}


static void
pref_ui_toolbar_style_changed (GConfClient *client,
			       guint        cnxn_id,
			       GConfEntry  *entry,
			       gpointer     user_data)
{
	GthViewer *viewer = user_data;
	gth_viewer_notify_update_toolbar_style (viewer);
}


394 395 396 397 398 399 400
static void
pref_ui_toolbar_visible_changed (GConfClient *client,
				 guint        cnxn_id,
				 GConfEntry  *entry,
				 gpointer     user_data)
{
	GthViewer *viewer = user_data;
401 402 403 404 405
	gth_viewer_set_toolbar_visibility (viewer, gconf_value_get_bool (gconf_entry_get_value (entry)));
}


static void
406 407 408 409 410 411
gth_viewer_set_statusbar_visibility  (GthViewer *viewer,
				      gboolean   visible)
{
	g_return_if_fail (viewer != NULL);

	set_action_active (viewer, "View_Statusbar", visible);
412
	if (visible)
413 414 415 416 417 418 419 420 421 422 423
		gtk_widget_show (viewer->priv->statusbar);
	else
		gtk_widget_hide (viewer->priv->statusbar);
}


static void
pref_ui_statusbar_visible_changed (GConfClient *client,
				   guint        cnxn_id,
				   GConfEntry  *entry,
				   gpointer     user_data)
424
{
425 426
	GthViewer *viewer = user_data;
	gth_viewer_set_statusbar_visibility (viewer, gconf_value_get_bool (gconf_entry_get_value (entry)));
427 428 429
}


430 431 432 433 434 435 436 437 438 439 440 441 442
static void
pref_ui_single_window_changed (GConfClient *client,
			       guint        cnxn_id,
			       GConfEntry  *entry,
			       gpointer     user_data)
{
	GthViewer *viewer = user_data;
	gboolean   value = gconf_value_get_bool (gconf_entry_get_value (entry));

	set_action_active (viewer, "View_SingleWindow", value);
}


443
static void
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
gth_viewer_realize (GtkWidget *widget)
{
	GthViewer *viewer;
	GthViewerPrivateData *priv;

	viewer = GTH_VIEWER (widget);
	priv = viewer->priv;

	GTK_WIDGET_CLASS (parent_class)->realize (widget);
}


static void
save_window_size (GthViewer *viewer)
{
	int w, h;

	gdk_drawable_get_size (GTK_WIDGET (viewer)->window, &w, &h);
	eel_gconf_set_integer (PREF_UI_VIEWER_WIDTH, w);
	eel_gconf_set_integer (PREF_UI_VIEWER_HEIGHT, h);
}


467
static void
468 469 470 471 472 473 474 475 476 477 478
gth_viewer_unrealize (GtkWidget *widget)
{
	GthViewer            *viewer;
	GthViewerPrivateData *priv;

	viewer = GTH_VIEWER (widget);
	priv = viewer->priv;

	/* save ui preferences. */

	save_window_size (viewer);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
479
	eel_gconf_set_integer (PREF_UI_COMMENT_PANE_SIZE, _gtk_widget_get_height (widget) - gtk_paned_get_position (GTK_PANED (viewer->priv->image_main_pane)));
480 481 482 483 484 485 486 487 488 489 490 491

	GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
}


static gboolean
first_time_idle (gpointer callback_data)
{
	GthViewer            *viewer = callback_data;
	GthViewerPrivateData *priv = viewer->priv;

	g_source_remove (priv->first_timeout_handle);
492 493

	gtk_widget_grab_focus (priv->viewer);
494 495
	if (priv->image != NULL)
		image_viewer_load_image (IMAGE_VIEWER (priv->viewer), priv->image);
496 497 498 499 500

	return FALSE;
}


501
static void
502 503 504 505 506
gth_viewer_show (GtkWidget *widget)
{
	GthViewer *viewer = GTH_VIEWER (widget);
	gboolean   view_foobar;

Paolo Bacchilega's avatar
Paolo Bacchilega committed
507 508
	GTK_WIDGET_CLASS (parent_class)->show (widget);

509
	if (!viewer->priv->first_time_show)
Paolo Bacchilega's avatar
Paolo Bacchilega committed
510 511
		return;
	viewer->priv->first_time_show = FALSE;
512 513 514 515 516 517 518 519

	view_foobar = eel_gconf_get_boolean (PREF_UI_TOOLBAR_VISIBLE, TRUE);
	gth_viewer_set_toolbar_visibility (viewer, view_foobar);

	view_foobar = eel_gconf_get_boolean (PREF_UI_STATUSBAR_VISIBLE, TRUE);
	gth_viewer_set_statusbar_visibility (viewer, view_foobar);

	if (viewer->priv->first_timeout_handle == 0)
520
		viewer->priv->first_timeout_handle = g_idle_add (first_time_idle, viewer);
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
}


static void
viewer_update_statusbar_zoom_info (GthViewer *viewer)
{
	GthViewerPrivateData *priv = viewer->priv;
	gboolean              image_is_visible;
	int                   zoom;
	char                 *text;

	viewer_update_zoom_sensitivity (viewer);

	/**/

536
	image_is_visible = (priv->image != NULL) && ! priv->image_error;
537 538 539 540 541
	if (! image_is_visible) {
		if (! GTK_WIDGET_VISIBLE (priv->zoom_info_frame))
			return;
		gtk_widget_hide (priv->zoom_info_frame);
		return;
542
	}
543

544
	if (! GTK_WIDGET_VISIBLE (priv->zoom_info_frame))
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
		gtk_widget_show (priv->zoom_info_frame);

	zoom = (int) (IMAGE_VIEWER (priv->viewer)->zoom_level * 100.0);
	text = g_strdup_printf (" %d%% ", zoom);
	gtk_label_set_text (GTK_LABEL (priv->zoom_info), text);
	g_free (text);
}


static void
viewer_update_statusbar_image_info (GthViewer *viewer)
{
	GthViewerPrivateData *priv = viewer->priv;
	char                 *text;
	char                  time_txt[50], *utf8_time_txt;
	char                 *size_txt;
	char                 *file_size_txt;
	int                   width, height;
	time_t                timer = 0;
	struct tm            *tm;

566
	if ((priv->image == NULL) || priv->image_error) {
567 568 569 570
		if (! GTK_WIDGET_VISIBLE (priv->image_info_frame))
			return;
		gtk_widget_hide (priv->image_info_frame);
		return;
571 572
	} 
	else if (! GTK_WIDGET_VISIBLE (priv->image_info_frame))
573 574 575 576
		gtk_widget_show (priv->image_info_frame);

	if (!image_viewer_is_void (IMAGE_VIEWER (priv->viewer))) {
		width = image_viewer_get_image_width (IMAGE_VIEWER (priv->viewer));
577
		height = image_viewer_get_image_height (IMAGE_VIEWER (priv->viewer));
578 579
	} 
	else {
580 581 582 583
		width = 0;
		height = 0;
	}

584
	get_exif_time_or_mtime (priv->image);
585 586 587 588 589
	tm = localtime (&timer);
	strftime (time_txt, 50, _("%d %B %Y, %H:%M"), tm);
	utf8_time_txt = g_locale_to_utf8 (time_txt, -1, 0, 0, 0);

	size_txt = g_strdup_printf (_("%d x %d pixels"), width, height);
590
	file_size_txt = gnome_vfs_format_file_size_for_display (priv->image->size);
591 592 593 594 595 596 597 598 599

	/**/

	if (! priv->image_modified)
		text = g_strdup_printf (" %s - %s - %s     ",
					size_txt,
					file_size_txt,
					utf8_time_txt);
	else
600
		text = g_strdup_printf (" %s - %s     ",
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
					_("Modified"),
					size_txt);

	gtk_label_set_text (GTK_LABEL (priv->image_info), text);

	/**/

	g_free (size_txt);
	g_free (file_size_txt);
	g_free (text);
	g_free (utf8_time_txt);
}


static void
update_image_comment (GthViewer *viewer)
{
	GthViewerPrivateData *priv = viewer->priv;
	CommentData          *cdata;
	char                 *comment;
	GtkTextBuffer        *text_buffer;

	text_buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->image_comment));

625
	if (priv->image == NULL) {
626 627 628 629 630 631
		GtkTextIter start, end;
		gtk_text_buffer_get_bounds (text_buffer, &start, &end);
		gtk_text_buffer_delete (text_buffer, &start, &end);
                return;
	}

632
	cdata = comments_load_comment (priv->image->path, TRUE);
633 634 635 636 637 638 639 640 641 642 643 644

	if (cdata == NULL) {
		GtkTextIter  iter;
		const char  *click_here = _("[Press 'c' to add a comment]");
		GtkTextIter  start, end;
		GtkTextTag  *tag;

		gtk_text_buffer_set_text (text_buffer, click_here, strlen (click_here));
		gtk_text_buffer_get_iter_at_line (text_buffer, &iter, 0);
		gtk_text_buffer_place_cursor (text_buffer, &iter);

		gtk_text_buffer_get_bounds (text_buffer, &start, &end);
645
		tag = gtk_text_buffer_create_tag (text_buffer, NULL,
646 647 648 649 650 651 652 653 654 655 656 657 658 659
						  "style", PANGO_STYLE_ITALIC,
						  NULL);
		gtk_text_buffer_apply_tag (text_buffer, tag, &start, &end);

		return;
	}

	comment = comments_get_comment_as_string (cdata, "\n\n", " - ");

	if (comment != NULL) {
		GtkTextIter iter;
		gtk_text_buffer_set_text (text_buffer, comment, strlen (comment));
		gtk_text_buffer_get_iter_at_line (text_buffer, &iter, 0);
		gtk_text_buffer_place_cursor (text_buffer, &iter);
660 661
	} 
	else {
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
		GtkTextIter start, end;
		gtk_text_buffer_get_bounds (text_buffer, &start, &end);
		gtk_text_buffer_delete (text_buffer, &start, &end);
	}

	g_free (comment);
	comment_data_free (cdata);
}


static void
viewer_update_image_info (GthViewer *viewer)
{
	viewer_update_statusbar_image_info (viewer);
	viewer_update_statusbar_zoom_info (viewer);
677

678 679
	gth_exif_data_viewer_update (GTH_EXIF_DATA_VIEWER (viewer->priv->exif_data_viewer),
				     IMAGE_VIEWER (viewer->priv->viewer),
680
				     viewer->priv->image);
681

682 683 684 685 686 687 688 689 690 691 692 693 694
	update_image_comment (viewer);
}


static void
viewer_update_title (GthViewer *viewer)
{
	GthViewerPrivateData *priv = viewer->priv;
	char                 *title = NULL;
	char                 *modified;

	modified = priv->image_modified ? _("[modified]") : "";

695
	if (viewer->priv->image == NULL)
696 697
		title = g_strdup (_("No image"));
	else {
698
		char *image_name = basename_for_display (viewer->priv->image->path);
699 700 701 702 703 704 705 706 707
		title = g_strdup_printf ("%s %s", image_name, modified);
		g_free (image_name);
	}

	gtk_window_set_title (GTK_WINDOW (viewer), title);
	g_free (title);
}


708 709 710 711
static void
open_with_menu_item_activate_cb (GtkMenuItem *menuitem,
				 gpointer     user_data)
{
712
	GthViewer               *viewer = user_data;
713
	GnomeVFSMimeApplication *app;
714 715 716 717
	GList                   *uris;

	if (viewer->priv->image == NULL)
		return;
718 719

	app = g_object_get_data (G_OBJECT (menuitem), "app");
720
	uris = g_list_prepend (NULL, viewer->priv->image->path);
721 722 723 724 725 726 727 728 729
	gnome_vfs_mime_application_launch (app, uris);
	g_list_free (uris);
}


static void
viewer_update_open_with_menu (GthViewer *viewer)
{
	GthViewerPrivateData *priv = viewer->priv;
730
	GList                *menu_items, *scan;
731
	const char           *mime_type = NULL;
732 733
	GtkWidget            *mitem;
	int                   pos = 0, i;
734

735
	menu_items = gtk_container_get_children (GTK_CONTAINER (priv->open_with_popup_menu));
736
	for (i = 0, scan = menu_items; i < g_list_length (menu_items) - 1; i++, scan = scan->next)
737 738
		gtk_widget_destroy ((GtkWidget*) scan->data);
	g_list_free (menu_items);
739

740 741
	if (priv->image != NULL)
		mime_type = priv->image->mime_type;
742 743 744 745 746 747 748 749 750 751

	if (mime_type != NULL) {
		GList        *apps = gnome_vfs_mime_get_all_applications (mime_type);
		GtkIconTheme *theme = gtk_icon_theme_get_default ();
		int           icon_size = get_folder_pixbuf_size_for_list (GTK_WIDGET (viewer));

		for (scan = apps; scan; scan = scan->next) {
			GnomeVFSMimeApplication *app = scan->data;
			GtkWidget               *mitem;

752 753 754 755
			/* do not include gthumb itself */
			if (strncmp (gnome_vfs_mime_application_get_exec (app), "gthumb", 6) == 0)
				continue;

756 757 758 759 760 761 762
			mitem = gtk_image_menu_item_new_with_label (gnome_vfs_mime_application_get_name (app));
			gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (mitem), create_image (theme, gnome_vfs_mime_application_get_icon (app), icon_size));
			g_object_set_data_full (G_OBJECT (mitem), "app", app, (GDestroyNotify)gnome_vfs_mime_application_free);
			g_signal_connect (mitem, "activate",
					  G_CALLBACK (open_with_menu_item_activate_cb),
					  viewer);
			gtk_widget_show_all (mitem);
763
			gtk_menu_insert (priv->open_with_popup_menu, mitem, pos++);
764 765
		}
		g_list_free (apps);
766 767
	} 
	else {
768 769 770
		mitem = gtk_menu_item_new_with_label (_("_None"));
		gtk_widget_set_sensitive (mitem, FALSE);
		gtk_widget_show (mitem);
771
		gtk_menu_insert (priv->open_with_popup_menu, mitem, pos++);
772
	}
773 774 775 776

	mitem = gtk_separator_menu_item_new ();
	gtk_widget_show (mitem);
	gtk_menu_insert (priv->open_with_popup_menu, mitem, pos++);
777 778 779
}


780 781 782 783
/* ask_whether_to_save */


static void
784 785
save_pixbuf__image_saved_cb (FileData *file,
			     gpointer  data)
786
{
787 788 789
	GthViewer *viewer = data;
	
	if (file != NULL)
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
		{
		gboolean closing = viewer->priv->closing;

		viewer->priv->image_modified = FALSE;
		viewer->priv->saving_modified_image = FALSE;
		if (viewer->priv->image_saved_func != NULL)
			(*viewer->priv->image_saved_func) (NULL, viewer);

		if (closing)
			return;
	
		if ((viewer->priv->image != NULL) && ! same_uri (viewer->priv->image->path, file->path)) {
			/*FIXME: gtk_widget_show (gth_viewer_new (uri));*/
			file_data_set_path (viewer->priv->image, file->path);
			gth_viewer_load (viewer, viewer->priv->image);
		}
		else {
			viewer_update_statusbar_image_info (viewer);
			viewer_update_image_info (viewer);
			viewer_update_title (viewer);
			viewer_update_sensitivity (viewer);
		}	
	}
813 814
	else
		viewer->priv->saving_modified_image = FALSE;
815 816 817 818
}


static void
819 820
ask_whether_to_save__image_saved_cb (FileData *file,
				     gpointer  data)
821
{
822
	save_pixbuf__image_saved_cb (file, data);
823 824 825 826 827 828 829 830 831 832 833
}


static void
ask_whether_to_save__response_cb (GtkWidget *dialog,
				  int        response_id,
				  GthViewer *viewer)
{
	GthViewerPrivateData *priv = viewer->priv;

        gtk_widget_destroy (dialog);
834

835 836
        if (response_id == GTK_RESPONSE_YES) {
		dlg_save_image_as (GTK_WINDOW (viewer),
837
				   priv->image->path,
838
				   priv->image->metadata,
839 840 841 842
				   image_viewer_get_current_pixbuf (IMAGE_VIEWER (priv->viewer)),
				   ask_whether_to_save__image_saved_cb,
				   viewer);
		priv->saving_modified_image = TRUE;
843 844
	} 
	else {
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
		priv->saving_modified_image = FALSE;
		priv->image_modified = FALSE;
		if (priv->image_saved_func != NULL)
			(*priv->image_saved_func) (NULL, viewer);
	}
}


static gboolean
ask_whether_to_save (GthViewer      *viewer,
		     ImageSavedFunc  image_saved_func)
{
	GthViewerPrivateData *priv = viewer->priv;
	GtkWidget            *d;

860
	if (! eel_gconf_get_boolean (PREF_MSG_SAVE_MODIFIED_IMAGE, TRUE))
861
		return FALSE;
862

863 864 865 866 867 868 869 870 871 872 873
	d = _gtk_yesno_dialog_with_checkbutton_new (
			    GTK_WINDOW (viewer),
			    GTK_DIALOG_MODAL,
			    _("The current image has been modified, do you want to save it?"),
			    _("Do Not Save"),
			    GTK_STOCK_SAVE_AS,
			    _("_Do not display this message again"),
			    PREF_MSG_SAVE_MODIFIED_IMAGE);

	priv->saving_modified_image = TRUE;
	priv->image_saved_func = image_saved_func;
874
	g_signal_connect (G_OBJECT (d),
875 876 877 878 879 880 881 882 883 884
			  "response",
			  G_CALLBACK (ask_whether_to_save__response_cb),
			  viewer);

	gtk_widget_show (d);

	return TRUE;
}


885
static void
886 887
real_set_void (FileData *file,
	       gpointer  data)
888
{
889
	GthViewer *viewer = data;
890

891 892 893 894
	if (! viewer->priv->image_error) {
		file_data_unref (viewer->priv->image);
		viewer->priv->image = NULL;
		viewer->priv->image_modified = FALSE;
895 896
	}

897
	image_viewer_set_void (IMAGE_VIEWER (viewer->priv->viewer));
898 899 900 901

	viewer_update_statusbar_image_info (viewer);
 	viewer_update_image_info (viewer);
	viewer_update_title (viewer);
902
	viewer_update_open_with_menu (viewer);
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
	viewer_update_sensitivity (viewer);
}


static void
viewer_set_void (GthViewer *viewer,
		 gboolean   error)
{
	GthViewerPrivateData *priv = viewer->priv;

	priv->image_error = error;
	if (priv->image_modified)
		if (ask_whether_to_save (viewer, real_set_void))
			return;
	real_set_void (NULL, viewer);
}


static void
922
image_loaded_cb (GtkWidget  *widget,
923 924 925 926 927 928 929 930 931
		 GthViewer  *viewer)
{
	GthViewerPrivateData *priv = viewer->priv;

	if (image_viewer_is_void (IMAGE_VIEWER (priv->viewer))) {
		viewer_set_void (viewer, TRUE);
		return;
	}

932
	file_data_update_info (priv->image); /* FIXME: check if this is necessary */
933 934 935 936
	priv->image_modified = FALSE;

	viewer_update_image_info (viewer);
	viewer_update_title (viewer);
937
	viewer_update_open_with_menu (viewer);
938 939
	viewer_update_sensitivity (viewer);

940 941 942 943
	if (StartInFullscreen) {
		StartInFullscreen = FALSE;
		gth_window_set_fullscreen (GTH_WINDOW (viewer), TRUE);
	}
944 945 946 947
}


static gboolean
948
zoom_changed_cb (GtkWidget  *widget,
949 950 951
		 GthViewer  *viewer)
{
	viewer_update_statusbar_zoom_info (viewer);
952
	return TRUE;
953 954 955
}


956
static void
957 958 959 960 961 962 963 964 965 966 967
viewer_drag_data_get  (GtkWidget        *widget,
		       GdkDragContext   *context,
		       GtkSelectionData *selection_data,
		       guint             info,
		       guint             time,
		       gpointer          data)
{
	GthViewer            *viewer = data;
	GthViewerPrivateData *priv = viewer->priv;
	char                 *path;

968
	if (IMAGE_VIEWER (priv->viewer)->is_void)
969 970 971 972 973
		return;

	path = image_viewer_get_image_filename (IMAGE_VIEWER (priv->viewer));
	gtk_selection_data_set (selection_data,
				selection_data->target,
974
				8,
Paolo Bacchilega's avatar
Paolo Bacchilega committed
975
				(guchar*)path, strlen (path));
976 977 978 979
	g_free (path);
}


980
static void
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
viewer_drag_data_received  (GtkWidget          *widget,
			    GdkDragContext     *context,
			    int                 x,
			    int                 y,
			    GtkSelectionData   *data,
			    guint               info,
			    guint               time,
			    gpointer            extra_data)
{
	GthViewer *viewer = extra_data;
	GList     *list;
	GList     *scan;

	if (! ((data->length >= 0) && (data->format == 8))) {
		gtk_drag_finish (context, FALSE, FALSE, time);
		return;
	}

	gtk_drag_finish (context, TRUE, FALSE, time);

	list = get_file_list_from_url_list ((char *) data->data);
	for (scan = list; scan; scan = scan->next) {
		char *filename = scan->data;
		if (scan == list)
1005
			gth_viewer_load_from_uri (viewer, filename);
1006
		else
1007 1008 1009 1010 1011 1012 1013
			gtk_widget_show (gth_viewer_new (filename));
	}

	path_list_free (list);
}


1014
static gboolean
1015
viewer_key_press_cb (GtkWidget   *widget,
1016 1017 1018
		     GdkEventKey *event,
		     gpointer     data)
{
1019
	GthViewer   *viewer = data;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1020
	GthWindow   *window = (GthWindow*) viewer;
1021
	gboolean     retval = FALSE;
1022

1023
	switch (gdk_keyval_to_lower (event->keyval)) {
1024 1025 1026 1027 1028 1029
		/* Toggle comment visibility. */
	case GDK_i:
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (viewer->priv->comment_button),
					      !gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (viewer->priv->comment_button)));
		retval = TRUE;
		break;
1030

Paolo Bacchilega's avatar
Paolo Bacchilega committed
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
		/* Toggle animation. */
	case GDK_a:
		gth_window_set_animation (window, ! gth_window_get_animation (window));
		break;

		/* Step animation. */
	case GDK_j:
        	gth_window_step_animation (window);
		break;

Paolo Bacchilega's avatar
Paolo Bacchilega committed
1041
		/* Rotate clockwise without saving */
1042
	case GDK_r:
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
		gth_window_activate_action_alter_image_rotate90 (NULL, window);
		return TRUE;

		/* Rotate counter-clockwise without saving */
	case GDK_e:
		gth_window_activate_action_alter_image_rotate90cc (NULL, window);
		return TRUE;

		/* Lossless clockwise rotation. */
	case GDK_bracketright:
		gth_window_activate_action_tools_jpeg_rotate_right (NULL, window);
		return TRUE;

		/* Lossless counter-clockwise rotation */
	case GDK_bracketleft:
		gth_window_activate_action_tools_jpeg_rotate_left (NULL, window);
		return TRUE;

1061 1062
		/* Flip image */
	case GDK_l:
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1063 1064
		gth_window_activate_action_alter_image_flip (NULL, window);
		return TRUE;
1065 1066 1067

		/* Mirror image */
	case GDK_m:
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1068 1069
		gth_window_activate_action_alter_image_mirror (NULL, window);
		return TRUE;
1070

1071 1072 1073 1074 1075 1076 1077
		/* Full screen view. */
	case GDK_v:
	case GDK_f:
		gth_window_set_fullscreen (GTH_WINDOW (viewer), TRUE);
		retval = TRUE;
		break;

Paolo Bacchilega's avatar
Paolo Bacchilega committed
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
		/* Open images. */
	case GDK_o:
		gth_window_activate_action_file_open_with (NULL, viewer);
		return TRUE;

		/* Edit comment */
	case GDK_c:
		gth_window_edit_comment (GTH_WINDOW (viewer));
		return TRUE;

		/* Edit keywords */
	case GDK_k:
		gth_window_edit_categories (GTH_WINDOW (viewer));
		return TRUE;

1093 1094
	default:
		break;
1095 1096 1097 1098 1099 1100 1101
	}

	return retval;
}


static int
1102
image_comment_button_press_cb (GtkWidget      *widget,
1103 1104 1105 1106 1107
			       GdkEventButton *event,
			       gpointer        data)
{
	GthViewer *viewer = data;

1108
	if ((event->button == 1) && (event->type == GDK_2BUTTON_PRESS)) {
1109
		gth_window_edit_comment (GTH_WINDOW (viewer));
1110 1111 1112
		return TRUE;
	}

1113 1114 1115 1116
	return FALSE;
}


1117 1118 1119 1120 1121 1122 1123 1124
void
gth_viewer_set_metadata_visible (GthViewer *viewer,
				 gboolean   visible)
{
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (viewer->priv->comment_button), visible);
}


1125 1126 1127 1128 1129
void
gth_viewer_set_single_window (GthViewer *viewer,
			      gboolean   value)
{
	SingleViewer = NULL;
1130
	if (value && (LastFocusedViewer != NULL))
1131 1132 1133 1134 1135
		SingleViewer = LastFocusedViewer;
	eel_gconf_set_boolean (PREF_SINGLE_WINDOW, value);
}


1136 1137
static gboolean
comment_button_toggled_cb (GtkToggleButton *button,
1138
			   GthViewer       *viewer)
1139
{
1140
	gboolean visible = gtk_toggle_button_get_active (button);
1141

1142
	set_action_active (viewer, "View_ShowMetadata", visible);
1143 1144 1145
	viewer->priv->image_data_visible = visible;
	if (visible)
		gtk_widget_show (viewer->priv->image_data_hpaned);
1146
	else
1147
		gtk_widget_hide (viewer->priv->image_data_hpaned);
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163

	return TRUE;
}


static gboolean
progress_cancel_cb (GtkButton *button,
		    GthViewer *viewer)
{
	if (viewer->priv->pixop != NULL)
		gth_pixbuf_op_stop (viewer->priv->pixop);
	return TRUE;
}


static gboolean
1164
progress_delete_cb (GtkWidget    *caller,
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
		    GdkEvent     *event,
		    GthViewer    *viewer)
{
	if (viewer->priv->pixop != NULL)
		gth_pixbuf_op_stop (viewer->priv->pixop);
	return TRUE;
}


static void
gth_viewer_init (GthViewer *viewer)
{
	GthViewerPrivateData *priv;

	priv = viewer->priv = g_new0 (GthViewerPrivateData, 1);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1180
	priv->first_time_show = TRUE;
1181
	priv->image = NULL;
1182
	priv->image_error = FALSE;
1183

1184
	if (SingleViewer == NULL)
1185
		SingleViewer = viewer;
1186 1187 1188
}


1189 1190 1191