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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #ifdef HAVE__NL_MEASUREMENT_MEASUREMENT
31 #include <glib/gi18n-lib.h>
34 #include "ev-properties-view.h"
44 CREATION_DATE_PROPERTY,
59 static const PropertyInfo properties_info[] = {
60 { TITLE_PROPERTY, N_("Title:") },
61 { URI_PROPERTY, N_("Location:") },
62 { SUBJECT_PROPERTY, N_("Subject:") },
63 { AUTHOR_PROPERTY, N_("Author:") },
64 { KEYWORDS_PROPERTY, N_("Keywords:") },
65 { PRODUCER_PROPERTY, N_("Producer:") },
66 { CREATOR_PROPERTY, N_("Creator:") },
67 { CREATION_DATE_PROPERTY, N_("Created:") },
68 { MOD_DATE_PROPERTY, N_("Modified:") },
69 { N_PAGES_PROPERTY, N_("Number of Pages:") },
70 { LINEARIZED_PROPERTY, N_("Optimized:") },
71 { FORMAT_PROPERTY, N_("Format:") },
72 { SECURITY_PROPERTY, N_("Security:") },
73 { PAPER_SIZE_PROPERTY, N_("Paper Size:") }
76 struct _EvPropertiesView {
77 GtkVBox base_instance;
80 GtkWidget *labels[N_PROPERTIES];
84 struct _EvPropertiesViewClass {
85 GtkVBoxClass base_class;
88 G_DEFINE_TYPE (EvPropertiesView, ev_properties_view, GTK_TYPE_VBOX)
91 ev_properties_view_dispose (GObject *object)
93 EvPropertiesView *properties = EV_PROPERTIES_VIEW (object);
95 if (properties->uri) {
96 g_free (properties->uri);
97 properties->uri = NULL;
100 G_OBJECT_CLASS (ev_properties_view_parent_class)->dispose (object);
104 ev_properties_view_class_init (EvPropertiesViewClass *properties_class)
106 GObjectClass *g_object_class = G_OBJECT_CLASS (properties_class);
108 g_object_class->dispose = ev_properties_view_dispose;
111 /* Returns a locale specific date and time representation */
113 ev_properties_view_format_date (GTime utime)
115 time_t time = (time_t) utime;
117 const char fmt_hack[] = "%c";
119 #ifdef HAVE_LOCALTIME_R
121 if (time == 0 || !localtime_r (&time, &t)) return NULL;
122 len = strftime (s, sizeof (s), fmt_hack, &t);
125 if (time == 0 || !(t = localtime (&time)) ) return NULL;
126 len = strftime (s, sizeof (s), fmt_hack, t);
129 if (len == 0 || s[0] == '\0') return NULL;
131 return g_locale_to_utf8 (s, -1, NULL, NULL, NULL);
134 /* This is cut out of gconvert.c from glib (and mildly modified). Not all
135 backends give valid UTF-8 for properties, so we make sure that is.
138 make_valid_utf8 (const gchar *name)
141 const gchar *remainder, *invalid;
142 gint remaining_bytes, valid_bytes;
146 remaining_bytes = strlen (name);
148 while (remaining_bytes != 0)
150 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
152 valid_bytes = invalid - remainder;
155 string = g_string_sized_new (remaining_bytes);
157 g_string_append_len (string, remainder, valid_bytes);
158 g_string_append_c (string, '?');
160 remaining_bytes -= valid_bytes + 1;
161 remainder = invalid + 1;
165 return g_strdup (name);
167 g_string_append (string, remainder);
169 g_assert (g_utf8_validate (string->str, -1, NULL));
171 return g_string_free (string, FALSE);
175 set_property (EvPropertiesView *properties,
185 if (!properties->labels[property]) {
186 label = gtk_label_new (NULL);
187 g_object_set (G_OBJECT (label), "xalign", 0.0, NULL);
188 markup = g_strdup_printf ("<b>%s</b>", _(properties_info[property].label));
189 gtk_label_set_markup (GTK_LABEL (label), markup);
192 gtk_table_attach (table, label, 0, 1, *row, *row + 1,
193 GTK_FILL, GTK_FILL, 0, 0);
194 gtk_widget_show (label);
197 if (!properties->labels[property]) {
198 label = gtk_label_new (NULL);
200 g_object_set (G_OBJECT (label),
204 "ellipsize", PANGO_ELLIPSIZE_END,
207 label = properties->labels[property];
210 if (text == NULL || text[0] == '\000') {
211 markup = g_markup_printf_escaped ("<i>%s</i>", _("None"));
212 gtk_label_set_markup (GTK_LABEL (label), markup);
215 valid_text = make_valid_utf8 (text ? text : "");
216 gtk_label_set_text (GTK_LABEL (label), valid_text);
220 if (!properties->labels[property]) {
221 gtk_table_attach (table, label, 1, 2, *row, *row + 1,
222 GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
223 properties->labels[property] = label;
226 gtk_widget_show (label);
232 get_default_user_units (void)
234 /* Translate to the default units to use for presenting
235 * lengths to the user. Translate to default:inch if you
236 * want inches, otherwise translate to default:mm.
237 * Do *not* translate it to "predefinito:mm", if it
238 * it isn't default:mm or default:inch it will not work
240 gchar *e = _("default:mm");
242 #ifdef HAVE__NL_MEASUREMENT_MEASUREMENT
243 gchar *imperial = NULL;
245 imperial = nl_langinfo (_NL_MEASUREMENT_MEASUREMENT);
246 if (imperial && imperial[0] == 2)
247 return GTK_UNIT_INCH; /* imperial */
248 if (imperial && imperial[0] == 1)
249 return GTK_UNIT_MM; /* metric */
252 if (strcmp (e, "default:mm") == 0)
254 if (strcmp (e, "default:inch") == 0)
255 return GTK_UNIT_INCH;
257 g_warning ("Whoever translated default:mm did so wrongly.\n");
263 get_tolerance (gdouble size)
267 else if (size >= 150.0f && size <= 600.0f)
274 ev_regular_paper_size (const EvDocumentInfo *info)
276 GList *paper_sizes, *l;
281 units = get_default_user_units ();
283 if (units == GTK_UNIT_MM) {
284 exact_size = g_strdup_printf(_("%.0f × %.0f mm"),
288 exact_size = g_strdup_printf (_("%.2f × %.2f inch"),
289 info->paper_width / 25.4f,
290 info->paper_height / 25.4f);
293 paper_sizes = gtk_paper_size_get_paper_sizes (FALSE);
295 for (l = paper_sizes; l && l->data; l = g_list_next (l)) {
296 GtkPaperSize *size = (GtkPaperSize *) l->data;
298 gdouble paper_height;
299 gdouble width_tolerance;
300 gdouble height_tolerance;
302 paper_width = gtk_paper_size_get_width (size, GTK_UNIT_MM);
303 paper_height = gtk_paper_size_get_height (size, GTK_UNIT_MM);
305 width_tolerance = get_tolerance (paper_width);
306 height_tolerance = get_tolerance (paper_height);
308 if (ABS (info->paper_height - paper_height) <= height_tolerance &&
309 ABS (info->paper_width - paper_width) <= width_tolerance) {
310 /* Note to translators: first placeholder is the paper name (eg.
311 * A4), second placeholder is the paper size (eg. 297x210 mm) */
312 str = g_strdup_printf (_("%s, Portrait (%s)"),
313 gtk_paper_size_get_display_name (size),
315 } else if (ABS (info->paper_width - paper_height) <= height_tolerance &&
316 ABS (info->paper_height - paper_width) <= width_tolerance) {
317 /* Note to translators: first placeholder is the paper name (eg.
318 * A4), second placeholder is the paper size (eg. 297x210 mm) */
319 str = g_strdup_printf ( _("%s, Landscape (%s)"),
320 gtk_paper_size_get_display_name (size),
325 g_list_foreach (paper_sizes, (GFunc) gtk_paper_size_free, NULL);
326 g_list_free (paper_sizes);
337 ev_properties_view_set_info (EvPropertiesView *properties, const EvDocumentInfo *info)
343 table = properties->table;
345 if (info->fields_mask & EV_DOCUMENT_INFO_TITLE) {
346 set_property (properties, GTK_TABLE (table), TITLE_PROPERTY, info->title, &row);
348 set_property (properties, GTK_TABLE (table), URI_PROPERTY, properties->uri, &row);
349 if (info->fields_mask & EV_DOCUMENT_INFO_SUBJECT) {
350 set_property (properties, GTK_TABLE (table), SUBJECT_PROPERTY, info->subject, &row);
352 if (info->fields_mask & EV_DOCUMENT_INFO_AUTHOR) {
353 set_property (properties, GTK_TABLE (table), AUTHOR_PROPERTY, info->author, &row);
355 if (info->fields_mask & EV_DOCUMENT_INFO_KEYWORDS) {
356 set_property (properties, GTK_TABLE (table), KEYWORDS_PROPERTY, info->keywords, &row);
358 if (info->fields_mask & EV_DOCUMENT_INFO_PRODUCER) {
359 set_property (properties, GTK_TABLE (table), PRODUCER_PROPERTY, info->producer, &row);
361 if (info->fields_mask & EV_DOCUMENT_INFO_CREATOR) {
362 set_property (properties, GTK_TABLE (table), CREATOR_PROPERTY, info->creator, &row);
364 if (info->fields_mask & EV_DOCUMENT_INFO_CREATION_DATE) {
365 text = ev_properties_view_format_date (info->creation_date);
366 set_property (properties, GTK_TABLE (table), CREATION_DATE_PROPERTY, text, &row);
369 if (info->fields_mask & EV_DOCUMENT_INFO_MOD_DATE) {
370 text = ev_properties_view_format_date (info->modified_date);
371 set_property (properties, GTK_TABLE (table), MOD_DATE_PROPERTY, text, &row);
374 if (info->fields_mask & EV_DOCUMENT_INFO_FORMAT) {
375 set_property (properties, GTK_TABLE (table), FORMAT_PROPERTY, info->format, &row);
377 if (info->fields_mask & EV_DOCUMENT_INFO_N_PAGES) {
378 text = g_strdup_printf ("%d", info->n_pages);
379 set_property (properties, GTK_TABLE (table), N_PAGES_PROPERTY, text, &row);
382 if (info->fields_mask & EV_DOCUMENT_INFO_LINEARIZED) {
383 set_property (properties, GTK_TABLE (table), LINEARIZED_PROPERTY, info->linearized, &row);
385 if (info->fields_mask & EV_DOCUMENT_INFO_SECURITY) {
386 set_property (properties, GTK_TABLE (table), SECURITY_PROPERTY, info->security, &row);
388 if (info->fields_mask & EV_DOCUMENT_INFO_PAPER_SIZE) {
389 text = ev_regular_paper_size (info);
390 set_property (properties, GTK_TABLE (table), PAPER_SIZE_PROPERTY, text, &row);
396 ev_properties_view_init (EvPropertiesView *properties)
398 properties->table = gtk_table_new (13, 2, FALSE);
399 gtk_table_set_col_spacings (GTK_TABLE (properties->table), 12);
400 gtk_table_set_row_spacings (GTK_TABLE (properties->table), 6);
401 gtk_container_set_border_width (GTK_CONTAINER (properties->table), 12);
402 gtk_box_pack_start (GTK_BOX (properties), properties->table,
404 gtk_widget_show (properties->table);
408 ev_properties_view_register_type (GTypeModule *module)
410 ev_properties_view_get_type ();
414 ev_properties_view_new (const gchar *uri)
416 EvPropertiesView *properties;
418 properties = g_object_new (EV_TYPE_PROPERTIES, NULL);
419 properties->uri = g_strdup (uri);
421 return GTK_WIDGET (properties);