1
/* GIMP - The GNU Image Manipulation Program
3
* poppler.c - PDF file loader
5
* Copyright (C) 2005 Nathan Summers
7
* Some code in render_page_to_surface() borrowed from
8
* poppler.git/glib/poppler-page.cc.
10
* This program is free software; you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation; either version 2 of the License, or
13
* (at your option) any later version.
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
20
* You should have received a copy of the GNU General Public License
21
* along with this program; if not, write to the Free Software
22
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29
#include <libgimp/gimp.h>
30
#include <libgimp/gimpui.h>
32
#undef GTK_DISABLE_SINGLE_INCLUDES
34
#define GTK_DISABLE_SINGLE_INCLUDES
36
#include "libgimp/stdplugins-intl.h"
39
#define LOAD_PROC "file-pdf-load"
40
#define LOAD_THUMB_PROC "file-pdf-load-thumb"
41
#define PLUG_IN_BINARY "file-pdf"
43
#define THUMBNAIL_SIZE 128
46
/* Structs for the load dialog */
49
GimpPageSelectorTarget target;
53
static PdfLoadVals loadvals =
55
GIMP_PAGE_SELECTOR_TARGET_LAYERS,
65
/* Declare local functions */
66
static void query (void);
67
static void run (const gchar *name,
69
const GimpParam *param,
71
GimpParam **return_vals);
73
static gint32 load_image (PopplerDocument *doc,
74
const gchar *filename,
76
GimpPageSelectorTarget target,
78
PdfSelectedPages *pages);
80
static gboolean load_dialog (PopplerDocument *doc,
81
PdfSelectedPages *pages);
83
static PopplerDocument * open_document (const gchar *filename,
86
static cairo_surface_t * get_thumb_surface (PopplerDocument *doc,
90
static GdkPixbuf * get_thumb_pixbuf (PopplerDocument *doc,
94
static gint32 layer_from_pixbuf (gint32 image,
95
const gchar *layer_name,
98
gdouble progress_start,
99
gdouble progress_scale);
102
** the following was formerly part of
103
** gimpresolutionentry.h and gimpresolutionentry.c,
104
** moved here because this is the only thing that uses
105
** it, and it is undesirable to maintain all that api.
106
** Most unused functions have been removed.
108
#define GIMP_TYPE_RESOLUTION_ENTRY (gimp_resolution_entry_get_type ())
109
#define GIMP_RESOLUTION_ENTRY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIMP_TYPE_RESOLUTION_ENTRY, GimpResolutionEntry))
110
#define GIMP_RESOLUTION_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GIMP_TYPE_RESOLUTION_ENTRY, GimpResolutionEntryClass))
111
#define GIMP_IS_RESOLUTION_ENTRY(obj) (G_TYPE_CHECK_INSTANCE_TYPE (obj, GIMP_TYPE_RESOLUTION_ENTRY))
112
#define GIMP_IS_RESOLUTION_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GIMP_TYPE_RESOLUTION_ENTRY))
113
#define GIMP_RESOLUTION_ENTRY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GIMP_TYPE_RESOLUTION_ENTRY, GimpResolutionEntryClass))
116
typedef struct _GimpResolutionEntry GimpResolutionEntry;
117
typedef struct _GimpResolutionEntryClass GimpResolutionEntryClass;
119
typedef struct _GimpResolutionEntryField GimpResolutionEntryField;
121
struct _GimpResolutionEntryField
123
GimpResolutionEntry *gre;
124
GimpResolutionEntryField *corresponding;
130
guint changed_signal;
132
GtkObject *adjustment;
133
GtkWidget *spinbutton;
145
struct _GimpResolutionEntry
147
GtkTable parent_instance;
153
GtkWidget *chainbutton;
155
GimpResolutionEntryField width;
156
GimpResolutionEntryField height;
157
GimpResolutionEntryField x;
158
GimpResolutionEntryField y;
162
struct _GimpResolutionEntryClass
164
GtkTableClass parent_class;
166
void (* value_changed) (GimpResolutionEntry *gse);
167
void (* refval_changed) (GimpResolutionEntry *gse);
168
void (* unit_changed) (GimpResolutionEntry *gse);
172
GType gimp_resolution_entry_get_type (void) G_GNUC_CONST;
174
GtkWidget * gimp_resolution_entry_new (const gchar *width_label,
176
const gchar *height_label,
179
const gchar *res_label,
181
GimpUnit initial_unit);
183
GtkWidget * gimp_resolution_entry_attach_label (GimpResolutionEntry *gre,
189
gdouble gimp_resolution_entry_get_x_in_dpi (GimpResolutionEntry *gre);
191
gdouble gimp_resolution_entry_get_y_in_dpi (GimpResolutionEntry *gre);
194
/* signal callback convenience functions */
195
void gimp_resolution_entry_update_x_in_dpi (GimpResolutionEntry *gre,
198
void gimp_resolution_entry_update_y_in_dpi (GimpResolutionEntry *gre,
212
static void gimp_resolution_entry_class_init (GimpResolutionEntryClass *class);
213
static void gimp_resolution_entry_init (GimpResolutionEntry *gre);
215
static void gimp_resolution_entry_update_value (GimpResolutionEntryField *gref,
217
static void gimp_resolution_entry_value_callback (GtkWidget *widget,
219
static void gimp_resolution_entry_update_unit (GimpResolutionEntry *gre,
221
static void gimp_resolution_entry_unit_callback (GtkWidget *widget,
222
GimpResolutionEntry *gre);
224
static void gimp_resolution_entry_field_init (GimpResolutionEntry *gre,
225
GimpResolutionEntryField *gref,
226
GimpResolutionEntryField *corresponding,
227
guint changed_signal,
229
GimpUnit initial_unit,
231
gint spinbutton_width);
233
static void gimp_resolution_entry_format_label (GimpResolutionEntry *gre,
238
** end of gimpresolutionentry stuff
239
** the actual code can be found at the end of this file
241
const GimpPlugInInfo PLUG_IN_INFO =
243
NULL, /* init_proc */
244
NULL, /* quit_proc */
245
query, /* query_proc */
255
static const GimpParamDef load_args[] =
257
{ GIMP_PDB_INT32, "run-mode", "Interactive, non-interactive" },
258
{ GIMP_PDB_STRING, "filename", "The name of the file to load" },
259
{ GIMP_PDB_STRING, "raw-filename", "The name entered" }
260
/* XXX: Nice to have API at some point, but needs work
261
{ GIMP_PDB_INT32, "resolution", "Resolution to rasterize to (dpi)" },
262
{ GIMP_PDB_INT32, "n-pages", "Number of pages to load (0 for all)" },
263
{ GIMP_PDB_INT32ARRAY,"pages", "The pages to load" }
267
static const GimpParamDef load_return_vals[] =
269
{ GIMP_PDB_IMAGE, "image", "Output image" }
272
static const GimpParamDef thumb_args[] =
274
{ GIMP_PDB_STRING, "filename", "The name of the file to load" },
275
{ GIMP_PDB_INT32, "thumb-size", "Preferred thumbnail size" }
278
static const GimpParamDef thumb_return_vals[] =
280
{ GIMP_PDB_IMAGE, "image", "Thumbnail image" },
281
{ GIMP_PDB_INT32, "image-width", "Width of full-sized image" },
282
{ GIMP_PDB_INT32, "image-height", "Height of full-sized image" }
285
gimp_install_procedure (LOAD_PROC,
286
"Load file in PDF format.",
287
"Load file in PDF format. "
288
"PDF is a portable document format created by Adobe. "
289
"It is designed to be easily processed by a variety "
290
"of different platforms, and is a distant cousin of "
295
N_("Portable Document Format"),
298
G_N_ELEMENTS (load_args),
299
G_N_ELEMENTS (load_return_vals),
300
load_args, load_return_vals);
302
gimp_register_file_handler_mime (LOAD_PROC, "application/pdf");
303
gimp_register_magic_load_handler (LOAD_PROC,
308
gimp_install_procedure (LOAD_THUMB_PROC,
309
"Loads a preview from a PDF file.",
310
"Loads a small preview of the first page of the PDF "
311
"format file. Uses the embedded thumbnail if "
319
G_N_ELEMENTS (thumb_args),
320
G_N_ELEMENTS (thumb_return_vals),
321
thumb_args, thumb_return_vals);
323
gimp_register_thumbnail_loader (LOAD_PROC, LOAD_THUMB_PROC);
327
run (const gchar *name,
329
const GimpParam *param,
331
GimpParam **return_vals)
333
static GimpParam values[4];
334
GimpRunMode run_mode;
335
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
336
gint32 image_ID = -1;
337
PopplerDocument *doc = NULL;
338
GError *error = NULL;
340
run_mode = param[0].data.d_int32;
345
*return_vals = values;
347
values[0].type = GIMP_PDB_STATUS;
348
values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
350
if (! g_thread_supported ())
351
g_thread_init (NULL);
353
if (strcmp (name, LOAD_PROC) == 0)
355
PdfSelectedPages pages = { 0, NULL };
359
case GIMP_RUN_INTERACTIVE:
360
/* Possibly retrieve last settings */
361
gimp_get_data (LOAD_PROC, &loadvals);
363
doc = open_document (param[1].data.d_string, &error);
367
status = GIMP_PDB_EXECUTION_ERROR;
371
if (load_dialog (doc, &pages))
372
gimp_set_data (LOAD_PROC, &loadvals, sizeof(loadvals));
374
status = GIMP_PDB_CANCEL;
377
case GIMP_RUN_WITH_LAST_VALS:
378
/* FIXME: implement last vals mode */
379
status = GIMP_PDB_EXECUTION_ERROR;
382
case GIMP_RUN_NONINTERACTIVE:
383
doc = open_document (param[1].data.d_string, &error);
387
PopplerPage *test_page = poppler_document_get_page (doc, 0);
392
pages.pages = g_new (gint, 1);
395
g_object_unref (test_page);
399
status = GIMP_PDB_EXECUTION_ERROR;
400
g_object_unref (doc);
405
status = GIMP_PDB_EXECUTION_ERROR;
410
if (status == GIMP_PDB_SUCCESS)
412
image_ID = load_image (doc, param[1].data.d_string,
421
values[1].type = GIMP_PDB_IMAGE;
422
values[1].data.d_image = image_ID;
426
status = GIMP_PDB_EXECUTION_ERROR;
431
g_object_unref (doc);
433
g_free (pages.pages);
435
else if (strcmp (name, LOAD_THUMB_PROC) == 0)
439
status = GIMP_PDB_CALLING_ERROR;
448
GdkPixbuf *pixbuf = NULL;
450
/* Possibly retrieve last settings */
451
gimp_get_data (LOAD_PROC, &loadvals);
453
doc = open_document (param[0].data.d_string, &error);
457
PopplerPage *page = poppler_document_get_page (doc, 0);
461
poppler_page_get_size (page, &width, &height);
463
g_object_unref (page);
466
num_pages = poppler_document_get_n_pages (doc);
468
pixbuf = get_thumb_pixbuf (doc, 0, param[1].data.d_int32);
470
g_object_unref (doc);
475
image = gimp_image_new (gdk_pixbuf_get_width (pixbuf),
476
gdk_pixbuf_get_height (pixbuf),
479
gimp_image_undo_disable (image);
481
layer_from_pixbuf (image, "thumbnail", 0, pixbuf, 0.0, 1.0);
482
g_object_unref (pixbuf);
484
gimp_image_undo_enable (image);
485
gimp_image_clean_all (image);
488
scale = loadvals.resolution / gimp_unit_get_factor (GIMP_UNIT_POINT);
497
values[1].type = GIMP_PDB_IMAGE;
498
values[1].data.d_image = image;
499
values[2].type = GIMP_PDB_INT32;
500
values[2].data.d_int32 = width;
501
values[3].type = GIMP_PDB_INT32;
502
values[3].data.d_int32 = height;
506
status = GIMP_PDB_EXECUTION_ERROR;
513
status = GIMP_PDB_CALLING_ERROR;
516
if (status != GIMP_PDB_SUCCESS && error)
519
values[1].type = GIMP_PDB_STRING;
520
values[1].data.d_string = error->message;
523
values[0].data.d_status = status;
526
static PopplerDocument*
527
open_document (const gchar *filename,
530
PopplerDocument *doc;
532
GError *error = NULL;
534
uri = g_filename_to_uri (filename, NULL, &error);
538
g_set_error (load_error, 0, 0,
539
"Could not convert '%s' to an URI: %s",
540
gimp_filename_to_utf8 (filename), error->message);
541
g_error_free (error);
545
doc = poppler_document_new_from_file (uri, NULL, &error);
551
g_set_error (load_error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
552
_("Could not open '%s' for reading: %s"),
553
gimp_filename_to_utf8 (filename),
555
g_error_free (error);
562
/* FIXME: Remove this someday when we depend fully on GTK+ >= 3 */
564
#if (!GTK_CHECK_VERSION (3, 0, 0))
566
static cairo_format_t
567
gdk_cairo_format_for_content (cairo_content_t content)
571
case CAIRO_CONTENT_COLOR:
572
return CAIRO_FORMAT_RGB24;
573
case CAIRO_CONTENT_ALPHA:
574
return CAIRO_FORMAT_A8;
575
case CAIRO_CONTENT_COLOR_ALPHA:
577
return CAIRO_FORMAT_ARGB32;
581
static cairo_surface_t *
582
gdk_cairo_surface_coerce_to_image (cairo_surface_t *surface,
583
cairo_content_t content,
589
cairo_surface_t *copy;
592
copy = cairo_image_surface_create (gdk_cairo_format_for_content (content),
596
cr = cairo_create (copy);
597
cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
598
cairo_set_source_surface (cr, surface, -src_x, -src_y);
606
convert_alpha (guchar *dest_data,
617
src_data += src_stride * src_y + src_x * 4;
619
for (y = 0; y < height; y++) {
620
guint32 *src = (guint32 *) src_data;
622
for (x = 0; x < width; x++) {
623
guint alpha = src[x] >> 24;
627
dest_data[x * 4 + 0] = 0;
628
dest_data[x * 4 + 1] = 0;
629
dest_data[x * 4 + 2] = 0;
633
dest_data[x * 4 + 0] = (((src[x] & 0xff0000) >> 16) * 255 + alpha / 2) / alpha;
634
dest_data[x * 4 + 1] = (((src[x] & 0x00ff00) >> 8) * 255 + alpha / 2) / alpha;
635
dest_data[x * 4 + 2] = (((src[x] & 0x0000ff) >> 0) * 255 + alpha / 2) / alpha;
637
dest_data[x * 4 + 3] = alpha;
640
src_data += src_stride;
641
dest_data += dest_stride;
646
convert_no_alpha (guchar *dest_data,
657
src_data += src_stride * src_y + src_x * 4;
659
for (y = 0; y < height; y++) {
660
guint32 *src = (guint32 *) src_data;
662
for (x = 0; x < width; x++) {
663
dest_data[x * 3 + 0] = src[x] >> 16;
664
dest_data[x * 3 + 1] = src[x] >> 8;
665
dest_data[x * 3 + 2] = src[x];
668
src_data += src_stride;
669
dest_data += dest_stride;
674
* gdk_pixbuf_get_from_surface:
675
* @surface: surface to copy from
676
* @src_x: Source X coordinate within @surface
677
* @src_y: Source Y coordinate within @surface
678
* @width: Width in pixels of region to get
679
* @height: Height in pixels of region to get
681
* Transfers image data from a #cairo_surface_t and converts it to an RGB(A)
682
* representation inside a #GdkPixbuf. This allows you to efficiently read
683
* individual pixels from cairo surfaces. For #GdkWindows, use
684
* gdk_pixbuf_get_from_window() instead.
686
* This function will create an RGB pixbuf with 8 bits per channel.
687
* The pixbuf will contain an alpha channel if the @surface contains one.
689
* Return value: (transfer full): A newly-created pixbuf with a reference
690
* count of 1, or %NULL on error
693
gdk_pixbuf_get_from_surface (cairo_surface_t *surface,
699
cairo_content_t content;
702
/* General sanity checks */
703
g_return_val_if_fail (surface != NULL, NULL);
704
g_return_val_if_fail (width > 0 && height > 0, NULL);
706
content = cairo_surface_get_content (surface) | CAIRO_CONTENT_COLOR;
707
dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
708
!!(content & CAIRO_CONTENT_ALPHA),
712
surface = gdk_cairo_surface_coerce_to_image (surface, content,
715
cairo_surface_flush (surface);
716
if (cairo_surface_status (surface) || dest == NULL)
718
cairo_surface_destroy (surface);
722
if (gdk_pixbuf_get_has_alpha (dest))
723
convert_alpha (gdk_pixbuf_get_pixels (dest),
724
gdk_pixbuf_get_rowstride (dest),
725
cairo_image_surface_get_data (surface),
726
cairo_image_surface_get_stride (surface),
730
convert_no_alpha (gdk_pixbuf_get_pixels (dest),
731
gdk_pixbuf_get_rowstride (dest),
732
cairo_image_surface_get_data (surface),
733
cairo_image_surface_get_stride (surface),
737
cairo_surface_destroy (surface);
744
layer_from_pixbuf (gint32 image,
745
const gchar *layer_name,
748
gdouble progress_start,
749
gdouble progress_scale)
751
gint32 layer = gimp_layer_new_from_pixbuf (image, layer_name, pixbuf,
752
100.0, GIMP_NORMAL_MODE,
754
progress_start + progress_scale);
756
gimp_image_add_layer (image, layer, position);
761
static cairo_surface_t *
762
render_page_to_surface (PopplerPage *page,
767
cairo_surface_t *surface;
770
surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
771
cr = cairo_create (surface);
774
cairo_translate (cr, 0.0, 0.0);
777
cairo_scale (cr, scale, scale);
779
poppler_page_render (page, cr);
782
cairo_set_operator (cr, CAIRO_OPERATOR_DEST_OVER);
783
cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
792
render_page_to_pixbuf (PopplerPage *page,
798
cairo_surface_t *surface;
800
surface = render_page_to_surface (page, width, height, scale);
801
pixbuf = gdk_pixbuf_get_from_surface (surface, 0, 0,
802
cairo_image_surface_get_width (surface),
803
cairo_image_surface_get_height (surface));
804
cairo_surface_destroy (surface);
810
load_image (PopplerDocument *doc,
811
const gchar *filename,
812
GimpRunMode run_mode,
813
GimpPageSelectorTarget target,
815
PdfSelectedPages *pages)
818
gint32 *images = NULL;
821
gdouble doc_progress = 0;
823
if (target == GIMP_PAGE_SELECTOR_TARGET_IMAGES)
824
images = g_new0 (gint32, pages->n_pages);
826
gimp_progress_init_printf (_("Opening '%s'"),
827
gimp_filename_to_utf8 (filename));
829
scale = resolution / gimp_unit_get_factor (GIMP_UNIT_POINT);
833
for (i = 0; i < pages->n_pages; i++)
844
page = poppler_document_get_page (doc, pages->pages[i]);
846
poppler_page_get_size (page, &page_width, &page_height);
847
width = page_width * scale;
848
height = page_height * scale;
850
g_object_get (G_OBJECT (page), "label", &page_label, NULL);
856
image_ID = gimp_image_new (width, height, GIMP_RGB);
857
gimp_image_undo_disable (image_ID);
859
if (target == GIMP_PAGE_SELECTOR_TARGET_IMAGES)
860
name = g_strdup_printf (_("%s-%s"), filename, page_label);
862
name = g_strdup_printf (_("%s-pages"), filename);
864
gimp_image_set_filename (image_ID, name);
867
gimp_image_set_resolution (image_ID, resolution, resolution);
870
pixbuf = render_page_to_pixbuf (page, width, height, scale);
872
layer_from_pixbuf (image_ID, page_label, i, pixbuf,
873
doc_progress, 1.0 / pages->n_pages);
876
g_object_unref(pixbuf);
878
doc_progress = (double) (i + 1) / pages->n_pages;
879
gimp_progress_update (doc_progress);
881
if (target == GIMP_PAGE_SELECTOR_TARGET_IMAGES)
883
images[i] = image_ID;
885
gimp_image_undo_enable (image_ID);
886
gimp_image_clean_all (image_ID);
894
gimp_image_undo_enable (image_ID);
895
gimp_image_clean_all (image_ID);
898
if (target == GIMP_PAGE_SELECTOR_TARGET_IMAGES)
900
if (run_mode != GIMP_RUN_NONINTERACTIVE)
902
/* Display images in reverse order. The last will be
903
* displayed by GIMP itself
905
for (i = pages->n_pages - 1; i > 0; i--)
906
gimp_display_new (images[i]);
909
image_ID = images[0];
917
static cairo_surface_t *
918
get_thumb_surface (PopplerDocument *doc,
923
cairo_surface_t *surface;
925
page = poppler_document_get_page (doc, page_num);
930
surface = poppler_page_get_thumbnail (page);
938
poppler_page_get_size (page, &width, &height);
940
scale = (gdouble) preferred_size / MAX (width, height);
945
surface = render_page_to_surface (page, width, height, scale);
948
g_object_unref (page);
954
get_thumb_pixbuf (PopplerDocument *doc,
958
cairo_surface_t *surface;
961
surface = get_thumb_surface (doc, page_num, preferred_size);
962
pixbuf = gdk_pixbuf_get_from_surface (surface, 0, 0,
963
cairo_image_surface_get_width (surface),
964
cairo_image_surface_get_height (surface));
965
cairo_surface_destroy (surface);
972
PopplerDocument *document;
973
GimpPageSelector *selector;
974
gboolean stop_thumbnailing;
979
GimpPageSelector *selector;
985
idle_set_thumbnail (gpointer data)
987
IdleData *idle_data = data;
989
gimp_page_selector_set_page_thumbnail (idle_data->selector,
992
g_object_unref (idle_data->pixbuf);
999
thumbnail_thread (gpointer data)
1001
ThreadData *thread_data = data;
1005
n_pages = poppler_document_get_n_pages (thread_data->document);
1007
for (i = 0; i < n_pages; i++)
1009
IdleData *idle_data = g_new0 (IdleData, 1);
1011
idle_data->selector = thread_data->selector;
1012
idle_data->page_no = i;
1014
/* FIXME get preferred size from somewhere? */
1015
idle_data->pixbuf = get_thumb_pixbuf (thread_data->document, i,
1018
g_idle_add (idle_set_thumbnail, idle_data);
1020
if (thread_data->stop_thumbnailing)
1028
load_dialog (PopplerDocument *doc,
1029
PdfSelectedPages *pages)
1034
GtkWidget *selector;
1035
GtkWidget *resolution;
1038
ThreadData thread_data;
1049
gimp_ui_init (PLUG_IN_BINARY, FALSE);
1051
dialog = gimp_dialog_new (_("Import from PDF"), PLUG_IN_BINARY,
1053
gimp_standard_help_func, LOAD_PROC,
1055
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1056
_("_Import"), GTK_RESPONSE_OK,
1060
gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
1062
GTK_RESPONSE_CANCEL,
1065
gimp_window_set_transient (GTK_WINDOW (dialog));
1067
vbox = gtk_vbox_new (FALSE, 12);
1068
gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
1069
gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), vbox);
1070
gtk_widget_show (vbox);
1073
title = gimp_prop_label_new (G_OBJECT (doc), "title");
1074
gtk_label_set_ellipsize (GTK_LABEL (title), PANGO_ELLIPSIZE_END);
1075
gtk_box_pack_start (GTK_BOX (vbox), title, FALSE, FALSE, 0);
1076
gtk_widget_show (title);
1079
selector = gimp_page_selector_new ();
1080
gtk_widget_set_size_request (selector, 380, 360);
1081
gtk_box_pack_start (GTK_BOX (vbox), selector, TRUE, TRUE, 0);
1082
gtk_widget_show (selector);
1084
n_pages = poppler_document_get_n_pages (doc);
1085
gimp_page_selector_set_n_pages (GIMP_PAGE_SELECTOR (selector), n_pages);
1086
gimp_page_selector_set_target (GIMP_PAGE_SELECTOR (selector),
1089
for (i = 0; i < n_pages; i++)
1094
page = poppler_document_get_page (doc, i);
1095
g_object_get (G_OBJECT (page), "label", &label, NULL);
1097
gimp_page_selector_set_page_label (GIMP_PAGE_SELECTOR (selector), i,
1101
poppler_page_get_size (page, &width, &height);
1103
g_object_unref (page);
1107
g_signal_connect_swapped (selector, "activate",
1108
G_CALLBACK (gtk_window_activate_default),
1111
thread_data.document = doc;
1112
thread_data.selector = GIMP_PAGE_SELECTOR (selector);
1113
thread_data.stop_thumbnailing = FALSE;
1115
thread = g_thread_create (thumbnail_thread, &thread_data, TRUE, NULL);
1119
hbox = gtk_hbox_new (FALSE, 0);
1120
gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
1121
gtk_widget_show (hbox);
1123
resolution = gimp_resolution_entry_new (_("_Width (pixels):"), width,
1124
_("_Height (pixels):"), height,
1127
loadvals.resolution, GIMP_UNIT_INCH);
1129
gtk_box_pack_start (GTK_BOX (hbox), resolution, FALSE, FALSE, 0);
1130
gtk_widget_show (resolution);
1132
g_signal_connect (resolution, "x-changed",
1133
G_CALLBACK (gimp_resolution_entry_update_x_in_dpi),
1134
&loadvals.resolution);
1136
/* Setup done; display the dialog */
1137
gtk_widget_show (dialog);
1139
/* run the dialog */
1140
run = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);
1143
gimp_page_selector_get_target (GIMP_PAGE_SELECTOR (selector));
1146
gimp_page_selector_get_selected_pages (GIMP_PAGE_SELECTOR (selector),
1149
/* select all if none selected */
1150
if (pages->n_pages == 0)
1152
gimp_page_selector_select_all (GIMP_PAGE_SELECTOR (selector));
1155
gimp_page_selector_get_selected_pages (GIMP_PAGE_SELECTOR (selector),
1160
thread_data.stop_thumbnailing = TRUE;
1161
g_thread_join (thread);
1168
** code for GimpResolutionEntry widget, formerly in libgimpwidgets
1171
static guint gimp_resolution_entry_signals[LAST_SIGNAL] = { 0 };
1173
static GtkTableClass *parent_class = NULL;
1177
gimp_resolution_entry_get_type (void)
1179
static GType gre_type = 0;
1183
const GTypeInfo gre_info =
1185
sizeof (GimpResolutionEntryClass),
1186
(GBaseInitFunc) NULL,
1187
(GBaseFinalizeFunc) NULL,
1188
(GClassInitFunc) gimp_resolution_entry_class_init,
1189
NULL, /* class_finalize */
1190
NULL, /* class_data */
1191
sizeof (GimpResolutionEntry),
1192
0, /* n_preallocs */
1193
(GInstanceInitFunc) gimp_resolution_entry_init,
1196
gre_type = g_type_register_static (GTK_TYPE_TABLE,
1197
"GimpResolutionEntry",
1205
gimp_resolution_entry_class_init (GimpResolutionEntryClass *klass)
1207
parent_class = g_type_class_peek_parent (klass);
1209
gimp_resolution_entry_signals[HEIGHT_CHANGED] =
1210
g_signal_new ("height-changed",
1211
G_TYPE_FROM_CLASS (klass),
1213
G_STRUCT_OFFSET (GimpResolutionEntryClass, value_changed),
1215
g_cclosure_marshal_VOID__VOID,
1218
gimp_resolution_entry_signals[WIDTH_CHANGED] =
1219
g_signal_new ("width-changed",
1220
G_TYPE_FROM_CLASS (klass),
1222
G_STRUCT_OFFSET (GimpResolutionEntryClass, value_changed),
1224
g_cclosure_marshal_VOID__VOID,
1227
gimp_resolution_entry_signals[X_CHANGED] =
1228
g_signal_new ("x-changed",
1229
G_TYPE_FROM_CLASS (klass),
1231
G_STRUCT_OFFSET (GimpResolutionEntryClass, value_changed),
1233
g_cclosure_marshal_VOID__VOID,
1236
gimp_resolution_entry_signals[Y_CHANGED] =
1237
g_signal_new ("y-changed",
1238
G_TYPE_FROM_CLASS (klass),
1240
G_STRUCT_OFFSET (GimpResolutionEntryClass, refval_changed),
1242
g_cclosure_marshal_VOID__VOID,
1245
gimp_resolution_entry_signals[UNIT_CHANGED] =
1246
g_signal_new ("unit-changed",
1247
G_TYPE_FROM_CLASS (klass),
1249
G_STRUCT_OFFSET (GimpResolutionEntryClass, unit_changed),
1251
g_cclosure_marshal_VOID__VOID,
1254
klass->value_changed = NULL;
1255
klass->refval_changed = NULL;
1256
klass->unit_changed = NULL;
1260
gimp_resolution_entry_init (GimpResolutionEntry *gre)
1262
gre->unitmenu = NULL;
1263
gre->unit = GIMP_UNIT_INCH;
1265
gtk_table_set_col_spacings (GTK_TABLE (gre), 4);
1266
gtk_table_set_row_spacings (GTK_TABLE (gre), 2);
1270
gimp_resolution_entry_field_init (GimpResolutionEntry *gre,
1271
GimpResolutionEntryField *gref,
1272
GimpResolutionEntryField *corresponding,
1273
guint changed_signal,
1274
gdouble initial_val,
1275
GimpUnit initial_unit,
1277
gint spinbutton_width)
1281
g_return_if_fail (GIMP_IS_RESOLUTION_ENTRY (gre));
1284
gref->corresponding = corresponding;
1285
gref->changed_signal = gimp_resolution_entry_signals[changed_signal];
1289
gref->value = initial_val /
1290
gimp_unit_get_factor (initial_unit) *
1291
corresponding->value *
1292
gimp_unit_get_factor (gre->unit);
1294
gref->phy_size = initial_val /
1295
gimp_unit_get_factor (initial_unit);
1299
gref->value = initial_val;
1302
gref->min_value = GIMP_MIN_RESOLUTION;
1303
gref->max_value = GIMP_MAX_RESOLUTION;
1304
gref->adjustment = NULL;
1306
gref->stop_recursion = 0;
1312
gref->label = g_object_new (GTK_TYPE_LABEL,
1316
gimp_label_set_attributes (GTK_LABEL (gref->label),
1317
PANGO_ATTR_STYLE, PANGO_STYLE_ITALIC,
1320
gimp_resolution_entry_format_label (gre, gref->label, gref->phy_size);
1323
digits = size ? 0 : MIN (gimp_unit_get_digits (initial_unit), 5) + 1;
1325
gref->spinbutton = gimp_spin_button_new (&gref->adjustment,
1334
if (spinbutton_width > 0)
1336
if (spinbutton_width < 17)
1337
gtk_entry_set_width_chars (GTK_ENTRY (gref->spinbutton),
1340
gtk_widget_set_size_request (gref->spinbutton,
1341
spinbutton_width, -1);
1346
* gimp_resolution_entry_new:
1347
* @width_label: Optional label for the width control.
1348
* @width: Width of the item, specified in terms of @size_unit.
1349
* @height_label: Optional label for the height control.
1350
* @height: Height of the item, specified in terms of @size_unit.
1351
* @size_unit: Unit used to specify the width and height.
1352
* @res_label: Optional label for the resolution entry.
1353
* @initial_res: The initial resolution.
1354
* @initial_unit: The initial unit.
1356
* Creates a new #GimpResolutionEntry widget.
1358
* The #GimpResolutionEntry is derived from #GtkTable and will have
1359
* an empty border of one cell width on each side plus an empty column left
1360
* of the #GimpUnitMenu to allow the caller to add labels or other widgets.
1362
* A #GimpChainButton is displayed if independent is set to %TRUE.
1364
* Returns: A pointer to the new #GimpResolutionEntry widget.
1367
gimp_resolution_entry_new (const gchar *width_label,
1369
const gchar *height_label,
1372
const gchar *res_label,
1373
gdouble initial_res,
1374
GimpUnit initial_unit)
1376
GimpResolutionEntry *gre;
1378
gre = g_object_new (GIMP_TYPE_RESOLUTION_ENTRY, NULL);
1380
gre->unit = initial_unit;
1382
gtk_table_resize (GTK_TABLE (gre), 4, 4);
1384
gimp_resolution_entry_field_init (gre, &gre->x,
1387
initial_res, initial_unit,
1390
gtk_table_attach_defaults (GTK_TABLE (gre), gre->x.spinbutton,
1394
g_signal_connect (gre->x.adjustment, "value-changed",
1395
G_CALLBACK (gimp_resolution_entry_value_callback),
1398
gtk_widget_show (gre->x.spinbutton);
1400
gre->unitmenu = gimp_unit_menu_new (_("pixels/%s"), initial_unit,
1403
gtk_table_attach (GTK_TABLE (gre), gre->unitmenu,
1405
GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
1406
g_signal_connect (gre->unitmenu, "unit-changed",
1407
G_CALLBACK (gimp_resolution_entry_unit_callback),
1409
gtk_widget_show (gre->unitmenu);
1411
gimp_resolution_entry_field_init (gre, &gre->width,
1417
gtk_table_attach_defaults (GTK_TABLE (gre), gre->width.spinbutton,
1421
gtk_table_attach_defaults (GTK_TABLE (gre), gre->width.label,
1425
g_signal_connect (gre->width.adjustment, "value-changed",
1426
G_CALLBACK (gimp_resolution_entry_value_callback),
1429
gtk_widget_show (gre->width.spinbutton);
1430
gtk_widget_show (gre->width.label);
1432
gimp_resolution_entry_field_init (gre, &gre->height, &gre->x,
1437
gtk_table_attach_defaults (GTK_TABLE (gre), gre->height.spinbutton,
1440
gtk_table_attach_defaults (GTK_TABLE (gre), gre->height.label,
1443
g_signal_connect (gre->height.adjustment, "value-changed",
1444
G_CALLBACK (gimp_resolution_entry_value_callback),
1447
gtk_widget_show (gre->height.spinbutton);
1448
gtk_widget_show (gre->height.label);
1451
gimp_resolution_entry_attach_label (gre, width_label, 1, 0, 0.0);
1454
gimp_resolution_entry_attach_label (gre, height_label, 2, 0, 0.0);
1457
gimp_resolution_entry_attach_label (gre, res_label, 3, 0, 0.0);
1459
return GTK_WIDGET (gre);
1463
* gimp_resolution_entry_attach_label:
1464
* @gre: The #GimpResolutionEntry you want to add a label to.
1465
* @text: The text of the label.
1466
* @row: The row where the label will be attached.
1467
* @column: The column where the label will be attached.
1468
* @alignment: The horizontal alignment of the label.
1470
* Attaches a #GtkLabel to the #GimpResolutionEntry (which is a #GtkTable).
1472
* Returns: A pointer to the new #GtkLabel widget.
1475
gimp_resolution_entry_attach_label (GimpResolutionEntry *gre,
1483
g_return_val_if_fail (GIMP_IS_RESOLUTION_ENTRY (gre), NULL);
1484
g_return_val_if_fail (text != NULL, NULL);
1486
label = gtk_label_new_with_mnemonic (text);
1490
GtkTableChild *child;
1493
for (list = GTK_TABLE (gre)->children; list; list = g_list_next (list))
1497
if (child->left_attach == 1 && child->top_attach == row)
1499
gtk_label_set_mnemonic_widget (GTK_LABEL (label),
1506
gtk_misc_set_alignment (GTK_MISC (label), alignment, 0.5);
1508
gtk_table_attach (GTK_TABLE (gre), label, column, column+1, row, row+1,
1509
GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
1510
gtk_widget_show (label);
1516
* gimp_resolution_entry_get_x_in_dpi;
1517
* @gre: The #GimpResolutionEntry you want to know the resolution of.
1519
* Returns the X resolution of the #GimpResolutionEntry in pixels per inch.
1522
gimp_resolution_entry_get_x_in_dpi (GimpResolutionEntry *gre)
1524
g_return_val_if_fail (GIMP_IS_RESOLUTION_ENTRY (gre), 0);
1526
/* dots_in_one_unit * units_in_one_inch -> dpi */
1527
return gre->x.value * gimp_unit_get_factor (gre->unit);
1531
* gimp_resolution_entry_get_y_in_dpi;
1532
* @gre: The #GimpResolutionEntry you want to know the resolution of.
1534
* Returns the Y resolution of the #GimpResolutionEntry in pixels per inch.
1537
gimp_resolution_entry_get_y_in_dpi (GimpResolutionEntry *gre)
1539
g_return_val_if_fail (GIMP_IS_RESOLUTION_ENTRY (gre), 0);
1541
return gre->y.value * gimp_unit_get_factor (gre->unit);
1546
gimp_resolution_entry_update_value (GimpResolutionEntryField *gref,
1549
if (gref->stop_recursion > 0)
1552
gref->value = value;
1554
gref->stop_recursion++;
1557
gimp_resolution_entry_update_value (gref->corresponding,
1560
gimp_unit_get_factor (gref->gre->unit));
1563
gdouble factor = gimp_unit_get_factor (gref->gre->unit);
1565
gimp_resolution_entry_update_value (&gref->gre->width,
1567
gref->gre->width.phy_size *
1570
gimp_resolution_entry_update_value (&gref->gre->height,
1572
gref->gre->height.phy_size *
1576
gtk_adjustment_set_value (GTK_ADJUSTMENT (gref->adjustment), value);
1578
gref->stop_recursion--;
1580
g_signal_emit (gref->gre, gref->changed_signal, 0);
1584
gimp_resolution_entry_value_callback (GtkWidget *widget,
1587
GimpResolutionEntryField *gref = (GimpResolutionEntryField *) data;
1590
new_value = GTK_ADJUSTMENT (widget)->value;
1592
if (gref->value != new_value)
1593
gimp_resolution_entry_update_value (gref, new_value);
1597
gimp_resolution_entry_update_unit (GimpResolutionEntry *gre,
1604
old_unit = gre->unit;
1607
digits = (gimp_unit_get_digits (GIMP_UNIT_INCH) -
1608
gimp_unit_get_digits (unit));
1610
gtk_spin_button_set_digits (GTK_SPIN_BUTTON (gre->x.spinbutton),
1611
MAX (3 + digits, 3));
1613
factor = gimp_unit_get_factor (old_unit) / gimp_unit_get_factor (unit);
1615
gre->x.min_value *= factor;
1616
gre->x.max_value *= factor;
1617
gre->x.value *= factor;
1619
gtk_adjustment_set_value (GTK_ADJUSTMENT (gre->x.adjustment),
1622
gimp_resolution_entry_format_label (gre,
1623
gre->width.label, gre->width.phy_size);
1624
gimp_resolution_entry_format_label (gre,
1625
gre->height.label, gre->height.phy_size);
1627
g_signal_emit (gre, gimp_resolution_entry_signals[UNIT_CHANGED], 0);
1631
gimp_resolution_entry_unit_callback (GtkWidget *widget,
1632
GimpResolutionEntry *gre)
1636
new_unit = gimp_unit_menu_get_unit (GIMP_UNIT_MENU (widget));
1638
if (gre->unit != new_unit)
1639
gimp_resolution_entry_update_unit (gre, new_unit);
1643
* gimp_resolution_entry_update_x_in_dpi:
1644
* @gre: the #GimpResolutionEntry
1645
* @data: a pointer to a gdouble
1647
* Convenience function to set a double to the X resolution, suitable
1648
* for use as a signal callback.
1651
gimp_resolution_entry_update_x_in_dpi (GimpResolutionEntry *gre,
1656
g_return_if_fail (gre != NULL);
1657
g_return_if_fail (data != NULL);
1658
g_return_if_fail (GIMP_IS_RESOLUTION_ENTRY (gre));
1660
val = (gdouble *) data;
1662
*val = gimp_resolution_entry_get_x_in_dpi (gre);
1666
* gimp_resolution_entry_update_y_in_dpi:
1667
* @gre: the #GimpResolutionEntry
1668
* @data: a pointer to a gdouble
1670
* Convenience function to set a double to the Y resolution, suitable
1671
* for use as a signal callback.
1674
gimp_resolution_entry_update_y_in_dpi (GimpResolutionEntry *gre,
1679
g_return_if_fail (gre != NULL);
1680
g_return_if_fail (data != NULL);
1681
g_return_if_fail (GIMP_IS_RESOLUTION_ENTRY (gre));
1683
val = (gdouble *) data;
1685
*val = gimp_resolution_entry_get_y_in_dpi (gre);
1689
gimp_resolution_entry_format_label (GimpResolutionEntry *gre,
1693
gchar *format = g_strdup_printf ("%%.%df %%s",
1694
gimp_unit_get_digits (gre->unit));
1695
gchar *text = g_strdup_printf (format,
1696
size * gimp_unit_get_factor (gre->unit),
1697
gimp_unit_get_plural (gre->unit));
1700
gtk_label_set_text (GTK_LABEL (label), text);