1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /* this file is part of evince, a gnome document viewer
4 * Copyright (C) 2005 Red Hat, Inc
6 * Evince is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Evince is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
25 #include "ev-properties-view.h"
26 #include "ev-document-fonts.h"
28 #include <glib/gi18n.h>
30 #include <glade/glade.h>
44 CREATION_DATE_PROPERTY,
59 static const PropertyInfo properties_info[] = {
60 { TITLE_PROPERTY, "title" },
61 { SUBJECT_PROPERTY, "subject" },
62 { AUTHOR_PROPERTY, "author" },
63 { KEYWORDS_PROPERTY, "keywords" },
64 { PRODUCER_PROPERTY, "producer" },
65 { CREATOR_PROPERTY, "creator" },
66 { CREATION_DATE_PROPERTY, "created" },
67 { MOD_DATE_PROPERTY, "modified" },
68 { N_PAGES_PROPERTY, "pages" },
69 { LINEARIZED_PROPERTY, "optimized" },
70 { FORMAT_PROPERTY, "version" },
71 { SECURITY_PROPERTY, "security" },
72 { PAPER_SIZE_PROPERTY, "papersize" }
75 struct _EvPropertiesView {
76 GtkVBox base_instance;
81 struct _EvPropertiesViewClass {
82 GtkVBoxClass base_class;
85 G_DEFINE_TYPE (EvPropertiesView, ev_properties_view, GTK_TYPE_VBOX)
88 ev_properties_view_dispose (GObject *object)
90 EvPropertiesView *properties = EV_PROPERTIES_VIEW (object);
92 if (properties->xml) {
93 g_object_unref (properties->xml);
94 properties->xml = NULL;
99 ev_properties_view_class_init (EvPropertiesViewClass *properties_class)
101 GObjectClass *g_object_class = G_OBJECT_CLASS (properties_class);
103 g_object_class->dispose = ev_properties_view_dispose;
106 /* Returns a locale specific date and time representation */
108 ev_properties_view_format_date (GTime utime)
110 time_t time = (time_t) utime;
113 const char *fmt_hack = "%c";
116 if (time == 0 || !localtime_r (&time, &t)) return NULL;
118 len = strftime (s, sizeof (s), fmt_hack, &t);
119 if (len == 0 || s[0] == '\0') return NULL;
121 return g_locale_to_utf8 (s, -1, NULL, NULL, NULL);
124 /* This is cut out of gconvert.c from glib (and mildly modified). Not all
125 backends give valid UTF-8 for properties, so we make sure that is.
128 make_valid_utf8 (const gchar *name)
131 const gchar *remainder, *invalid;
132 gint remaining_bytes, valid_bytes;
136 remaining_bytes = strlen (name);
138 while (remaining_bytes != 0)
140 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
142 valid_bytes = invalid - remainder;
145 string = g_string_sized_new (remaining_bytes);
147 g_string_append_len (string, remainder, valid_bytes);
148 g_string_append_c (string, '?');
150 remaining_bytes -= valid_bytes + 1;
151 remainder = invalid + 1;
155 return g_strdup (name);
157 g_string_append (string, remainder);
159 g_assert (g_utf8_validate (string->str, -1, NULL));
161 return g_string_free (string, FALSE);
165 set_property (GladeXML *xml, Property property, const char *text)
170 widget = glade_xml_get_widget (xml, properties_info[property].label_id);
171 g_return_if_fail (GTK_IS_LABEL (widget));
173 if (text == NULL || text[0] == '\000') {
176 markup = g_markup_printf_escaped ("<i>%s</i>", _("None"));
177 gtk_label_set_markup (GTK_LABEL (widget), markup);
182 text = text ? text : "";
184 valid_text = make_valid_utf8 (text);
186 gtk_label_set_text (GTK_LABEL (widget), valid_text);
192 * All values are in mm.
193 * Source: http://en.wikipedia.org/wiki/Paper_size
195 struct regular_paper_size {
198 double width_tolerance;
199 double height_tolerance;
200 const char *description;
201 } const regular_paper_sizes[] = {
202 // ISO 216 paper sizes
203 { 841.0f, 1189.0f, 3.0f, 3.0f, "A0" },
204 { 594.0f, 841.0f, 2.0f, 3.0f, "A1" },
205 { 420.0f, 594.0f, 2.0f, 2.0f, "A2" },
206 { 297.0f, 420.0f, 2.0f, 2.0f, "A3" },
207 { 210.0f, 297.0f, 2.0f, 2.0f, "A4" },
208 { 148.0f, 210.0f, 1.5f, 2.0f, "A5" },
209 { 105.0f, 148.0f, 1.5f, 1.5f, "A6" },
210 { 74.0f, 105.0f, 1.5f, 1.5f, "A7" },
211 { 52.0f, 74.0f, 1.5f, 1.5f, "A8" },
212 { 37.0f, 52.0f, 1.5f, 1.5f, "A9" },
213 { 26.0f, 37.0f, 1.5f, 1.5f, "A10" },
214 { 1000.0f, 1414.0f, 3.0f, 3.0f, "B0" },
215 { 707.0f, 1000.0f, 3.0f, 3.0f, "B1" },
216 { 500.0f, 707.0f, 2.0f, 3.0f, "B2" },
217 { 353.0f, 500.0f, 2.0f, 2.0f, "B3" },
218 { 250.0f, 353.0f, 2.0f, 2.0f, "B4" },
219 { 176.0f, 250.0f, 2.0f, 2.0f, "B5" },
220 { 125.0f, 176.0f, 1.5f, 2.0f, "B6" },
221 { 88.0f, 125.0f, 1.5f, 1.5f, "B7" },
222 { 62.0f, 88.0f, 1.5f, 1.5f, "B8" },
223 { 44.0f, 62.0f, 1.5f, 1.5f, "B9" },
224 { 31.0f, 44.0f, 1.5f, 1.5f, "B10" },
225 { 917.0f, 1297.0f, 3.0f, 3.0f, "C0" },
226 { 648.0f, 917.0f, 3.0f, 3.0f, "C1" },
227 { 458.0f, 648.0f, 2.0f, 3.0f, "C2" },
228 { 324.0f, 458.0f, 2.0f, 2.0f, "C3" },
229 { 229.0f, 324.0f, 2.0f, 2.0f, "C4" },
230 { 162.0f, 229.0f, 2.0f, 2.0f, "C5" },
231 { 114.0f, 162.0f, 1.5f, 2.0f, "C6" },
232 { 81.0f, 114.0f, 1.5f, 1.5f, "C7" },
233 { 57.0f, 81.0f, 1.5f, 1.5f, "C8" },
234 { 40.0f, 57.0f, 1.5f, 1.5f, "C9" },
235 { 28.0f, 40.0f, 1.5f, 1.5f, "C10" },
238 { 279.0f, 216.0f, 3.0f, 3.0f, "Letter" },
239 { 356.0f, 216.0f, 3.0f, 3.0f, "Legal" },
240 { 432.0f, 279.0f, 3.0f, 3.0f, "Ledger" }
244 ev_regular_paper_size (const EvDocumentInfo *info)
246 const struct regular_paper_size *size;
247 char *exact_size = NULL;
248 char *imperial = NULL;
252 imperial = nl_langinfo(_NL_MEASUREMENT_MEASUREMENT);
253 if ( imperial && imperial[0] == 2 )
254 /* Imperial measurement (inches) */
255 exact_size = g_strdup_printf( _("%.2f x %.2f in"),
256 info->paper_width / 25.4f,
257 info->paper_height / 25.4f );
259 /* Metric measurement (millimeters) */
260 exact_size = g_strdup_printf( _("%.0f x %.0f mm"),
262 info->paper_height );
264 for (i = G_N_ELEMENTS ( regular_paper_sizes ) - 1; i >= 0; i--) {
265 size = ®ular_paper_sizes[i];
267 if ( ABS( info->paper_height - size->height ) <= size->height_tolerance &&
268 ABS( info->paper_width - size->width ) <= size->width_tolerance ) {
269 /* Note to translators: first placeholder is the paper name (eg.
270 * A4), second placeholder is the paper size (eg. 297x210 mm) */
271 str = g_strdup_printf ( _("%s, Portrait (%s)"),
274 } else if ( ABS( info->paper_width - size->height ) <= size->height_tolerance &&
275 ABS( info->paper_height - size->width ) <= size->width_tolerance ) {
276 /* Note to translators: first placeholder is the paper name (eg.
277 * A4), second placeholder is the paper size (eg. 297x210 mm) */
278 str = g_strdup_printf ( _("%s, Landscape (%s)"),
292 ev_properties_view_set_info (EvPropertiesView *properties, const EvDocumentInfo *info)
294 GladeXML *xml = properties->xml;
297 if (info->fields_mask & EV_DOCUMENT_INFO_TITLE) {
298 set_property (xml, TITLE_PROPERTY, info->title);
300 if (info->fields_mask & EV_DOCUMENT_INFO_SUBJECT) {
301 set_property (xml, SUBJECT_PROPERTY, info->subject);
303 if (info->fields_mask & EV_DOCUMENT_INFO_AUTHOR) {
304 set_property (xml, AUTHOR_PROPERTY, info->author);
306 if (info->fields_mask & EV_DOCUMENT_INFO_KEYWORDS) {
307 set_property (xml, KEYWORDS_PROPERTY, info->keywords);
309 if (info->fields_mask & EV_DOCUMENT_INFO_PRODUCER) {
310 set_property (xml, PRODUCER_PROPERTY, info->producer);
312 if (info->fields_mask & EV_DOCUMENT_INFO_CREATOR) {
313 set_property (xml, CREATOR_PROPERTY, info->creator);
315 if (info->fields_mask & EV_DOCUMENT_INFO_CREATION_DATE) {
316 text = ev_properties_view_format_date (info->creation_date);
317 set_property (xml, CREATION_DATE_PROPERTY, text);
320 if (info->fields_mask & EV_DOCUMENT_INFO_MOD_DATE) {
321 text = ev_properties_view_format_date (info->modified_date);
322 set_property (xml, MOD_DATE_PROPERTY, text);
325 if (info->fields_mask & EV_DOCUMENT_INFO_FORMAT) {
326 text = g_strdup_printf ("%s", info->format);
327 set_property (xml, FORMAT_PROPERTY, text);
330 if (info->fields_mask & EV_DOCUMENT_INFO_N_PAGES) {
331 text = g_strdup_printf ("%d", info->n_pages);
332 set_property (xml, N_PAGES_PROPERTY, text);
335 if (info->fields_mask & EV_DOCUMENT_INFO_LINEARIZED) {
336 set_property (xml, LINEARIZED_PROPERTY, info->linearized);
338 if (info->fields_mask & EV_DOCUMENT_INFO_SECURITY) {
339 set_property (xml, SECURITY_PROPERTY, info->security);
341 if (info->fields_mask & EV_DOCUMENT_INFO_PAPER_SIZE) {
342 text = ev_regular_paper_size (info);
343 set_property (xml, PAPER_SIZE_PROPERTY, text);
349 ev_properties_view_init (EvPropertiesView *properties)
353 /* Create a new GladeXML object from XML file glade_file */
354 xml = glade_xml_new (DATADIR "/evince-properties.glade", "general_page_root", GETTEXT_PACKAGE);
355 properties->xml = xml;
356 g_assert (xml != NULL);
358 gtk_box_pack_start (GTK_BOX (properties),
359 glade_xml_get_widget (xml, "general_page_root"),
364 ev_properties_view_register_type (GTypeModule *module)
366 ev_properties_view_get_type ();
370 ev_properties_view_new (void)
372 EvPropertiesView *properties;
374 properties = g_object_new (EV_TYPE_PROPERTIES, NULL);
376 return GTK_WIDGET (properties);