]> www.fi.muni.cz Git - evince.git/blob - shell/ev-properties-fonts.c
Bug #311280
[evince.git] / shell / ev-properties-fonts.c
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
3  *
4  *  Copyright (C) 2005 Red Hat, Inc
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "ev-properties-fonts.h"
26 #include "ev-document-fonts.h"
27 #include "ev-jobs.h"
28 #include "ev-job-queue.h"
29
30 #include <glib/gi18n.h>
31 #include <gtk/gtktreeview.h>
32 #include <glade/glade.h>
33
34 struct _EvPropertiesFonts {
35         GtkVBox base_instance;
36
37         GladeXML *xml;
38
39         GtkWidget *fonts_treeview;
40         GtkWidget *fonts_progress_label;
41
42         EvDocument *document;
43 };
44
45 struct _EvPropertiesFontsClass {
46         GtkVBoxClass base_class;
47 };
48
49 G_DEFINE_TYPE (EvPropertiesFonts, ev_properties_fonts, GTK_TYPE_VBOX)
50
51 static void
52 ev_properties_fonts_dispose (GObject *object)
53 {
54         EvPropertiesFonts *properties = EV_PROPERTIES_FONTS (object);
55
56         if (properties->xml) {
57                 g_object_unref (properties->xml);
58                 properties->xml = NULL;
59         }
60 }
61
62 static void
63 ev_properties_fonts_class_init (EvPropertiesFontsClass *properties_class)
64 {
65         GObjectClass *g_object_class = G_OBJECT_CLASS (properties_class);
66
67         g_object_class->dispose = ev_properties_fonts_dispose;
68 }
69
70 static void
71 font_cell_data_func (GtkTreeViewColumn *col, GtkCellRenderer *renderer,
72                      GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data)
73 {
74         char *name;
75         char *details;
76         char *markup;
77
78         gtk_tree_model_get(model, iter,
79                            EV_DOCUMENT_FONTS_COLUMN_NAME, &name,
80                            EV_DOCUMENT_FONTS_COLUMN_DETAILS, &details,
81                            -1); 
82
83         if (details) {
84                 markup = g_strdup_printf ("<b><big>%s</big></b>\n<small>%s</small>",
85                                           name, details);
86         } else {
87                 markup = g_strdup_printf ("<b><big>%s</big></b>", name);
88         }
89
90         g_object_set (renderer, "markup", markup, NULL);
91         
92         g_free (markup);
93         g_free (details);
94         g_free (name);
95 }
96
97 static void
98 ev_properties_fonts_init (EvPropertiesFonts *properties)
99 {
100         GladeXML *xml;
101         GtkCellRenderer *renderer;
102         GtkTreeViewColumn *column;
103
104         /* Create a new GladeXML object from XML file glade_file */
105         xml = glade_xml_new (DATADIR "/evince-properties.glade", "fonts_page_root", NULL);
106         properties->xml = xml;
107         g_assert (xml != NULL);
108
109         gtk_box_pack_start (GTK_BOX (properties),
110                             glade_xml_get_widget (xml, "fonts_page_root"),
111                             TRUE, TRUE, 0);
112
113         properties->fonts_treeview = glade_xml_get_widget (xml, "fonts_treeview");
114         properties->fonts_progress_label = glade_xml_get_widget (xml, "font_progress_label");
115
116         column = gtk_tree_view_column_new ();
117         gtk_tree_view_column_set_expand (GTK_TREE_VIEW_COLUMN (column), TRUE);
118         gtk_tree_view_append_column (GTK_TREE_VIEW (properties->fonts_treeview), column);
119
120         renderer = GTK_CELL_RENDERER (g_object_new (GTK_TYPE_CELL_RENDERER_TEXT,
121                                                     "ypad", 6, NULL));
122         gtk_tree_view_column_pack_start (GTK_TREE_VIEW_COLUMN (column), renderer, FALSE);
123         gtk_tree_view_column_set_title (GTK_TREE_VIEW_COLUMN (column), _("Font"));
124         gtk_tree_view_column_set_cell_data_func (column, renderer,
125                                                  font_cell_data_func,
126                                                  NULL, NULL);
127 }
128
129 static void
130 update_progress_label (GtkWidget *label, double progress)
131 {
132         if (progress > 0) {
133                 char *progress_text;
134                 progress_text = g_strdup_printf (_("Gathering font information... %3d%%"),
135                                                  (int) (progress * 100));
136                 gtk_label_set_text (GTK_LABEL (label), progress_text);
137                 g_free (progress_text);
138                 gtk_widget_show (label);
139         } else {
140                 gtk_widget_hide (label);
141         }
142 }
143
144 static void
145 job_fonts_finished_cb (EvJob *job, EvPropertiesFonts *properties)
146 {       
147         EvDocumentFonts *document_fonts = EV_DOCUMENT_FONTS (job->document);
148         double progress;
149
150         progress = ev_document_fonts_get_progress (document_fonts);
151         update_progress_label (properties->fonts_progress_label, progress);
152
153         if (EV_JOB_FONTS (job)->scan_completed) {
154                 g_signal_handlers_disconnect_by_func
155                                 (job, job_fonts_finished_cb, properties);
156         } else {
157                 GtkTreeModel *model;
158                 EvJob *new_job;
159
160                 model = gtk_tree_view_get_model
161                                 (GTK_TREE_VIEW (properties->fonts_treeview));
162                 ev_document_doc_mutex_lock ();
163                 ev_document_fonts_fill_model (document_fonts, model);
164                 ev_document_doc_mutex_unlock ();
165                 new_job = ev_job_fonts_new (job->document);
166                 ev_job_queue_add_job (job, EV_JOB_PRIORITY_LOW);
167                 g_object_unref (new_job);
168         }
169 }
170
171 void
172 ev_properties_fonts_set_document (EvPropertiesFonts *properties,
173                                   EvDocument        *document)
174 {
175         GtkTreeView *tree_view = GTK_TREE_VIEW (properties->fonts_treeview);
176         GtkListStore *list_store;
177         EvJob *job;
178
179         properties->document = document;
180
181         list_store = gtk_list_store_new (EV_DOCUMENT_FONTS_COLUMN_NUM_COLUMNS,
182                                          G_TYPE_STRING, G_TYPE_STRING);
183         gtk_tree_view_set_model (tree_view, GTK_TREE_MODEL (list_store));
184
185         job = ev_job_fonts_new (properties->document);
186         g_signal_connect_object (job, "finished",
187                                  G_CALLBACK (job_fonts_finished_cb),
188                                  properties, 0);
189         ev_job_queue_add_job (job, EV_JOB_PRIORITY_LOW);
190         g_object_unref (job);
191 }
192
193 GtkWidget *
194 ev_properties_fonts_new ()
195 {
196         EvPropertiesFonts *properties;
197
198         properties = g_object_new (EV_TYPE_PROPERTIES_FONTS, NULL);
199
200         return GTK_WIDGET (properties);
201 }