]> www.fi.muni.cz Git - evince.git/blob - shell/ev-properties-fonts.c
bookmarks: Add a popup menu to open, rename and remove bookmarks
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <glib/gi18n.h>
26 #include <gtk/gtk.h>
27
28 #include "ev-document-fonts.h"
29 #include "ev-job-scheduler.h"
30 #include "ev-jobs.h"
31 #include "ev-properties-fonts.h"
32
33 struct _EvPropertiesFonts {
34         GtkVBox base_instance;
35
36         GtkWidget *fonts_treeview;
37         GtkWidget *fonts_progress_label;
38         EvJob     *fonts_job;
39
40         EvDocument *document;
41 };
42
43 struct _EvPropertiesFontsClass {
44         GtkVBoxClass base_class;
45 };
46
47 static void
48 job_fonts_finished_cb (EvJob *job, EvPropertiesFonts *properties);
49
50 G_DEFINE_TYPE (EvPropertiesFonts, ev_properties_fonts, GTK_TYPE_VBOX)
51
52 static void
53 ev_properties_fonts_dispose (GObject *object)
54 {
55         EvPropertiesFonts *properties = EV_PROPERTIES_FONTS (object);
56
57         if (properties->fonts_job) {
58                 g_signal_handlers_disconnect_by_func (properties->fonts_job, 
59                                                       job_fonts_finished_cb, 
60                                                       properties);
61                 ev_job_cancel (properties->fonts_job);
62
63                 g_object_unref (properties->fonts_job);         
64                 properties->fonts_job = NULL;
65         }
66
67         G_OBJECT_CLASS (ev_properties_fonts_parent_class)->dispose (object);
68 }
69
70 static void
71 ev_properties_fonts_class_init (EvPropertiesFontsClass *properties_class)
72 {
73         GObjectClass *g_object_class = G_OBJECT_CLASS (properties_class);
74
75         g_object_class->dispose = ev_properties_fonts_dispose;
76 }
77
78 static void
79 font_cell_data_func (GtkTreeViewColumn *col, GtkCellRenderer *renderer,
80                      GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data)
81 {
82         char *name;
83         char *details;
84         char *markup;
85
86         gtk_tree_model_get (model, iter,
87                             EV_DOCUMENT_FONTS_COLUMN_NAME, &name,
88                             EV_DOCUMENT_FONTS_COLUMN_DETAILS, &details,
89                             -1);        
90
91         if (details) {
92                 markup = g_strdup_printf ("<b><big>%s</big></b>\n<small>%s</small>",
93                                           name, details);
94         } else {
95                 markup = g_strdup_printf ("<b><big>%s</big></b>", name);
96         }
97
98         g_object_set (renderer, "markup", markup, NULL);
99         
100         g_free (markup);
101         g_free (details);
102         g_free (name);
103 }
104
105 static void
106 ev_properties_fonts_init (EvPropertiesFonts *properties)
107 {
108         GtkWidget         *swindow;
109         GtkCellRenderer   *renderer;
110         GtkTreeViewColumn *column;
111
112         gtk_container_set_border_width (GTK_CONTAINER (properties), 12);
113         gtk_box_set_spacing (GTK_BOX (properties), 6);
114         
115         swindow = gtk_scrolled_window_new (NULL, NULL);
116         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
117                                         GTK_POLICY_AUTOMATIC,
118                                         GTK_POLICY_AUTOMATIC);
119         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swindow),
120                                              GTK_SHADOW_IN);
121         
122         properties->fonts_treeview = gtk_tree_view_new ();
123         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (properties->fonts_treeview),
124                                            FALSE);
125         column = gtk_tree_view_column_new ();
126         gtk_tree_view_column_set_expand (GTK_TREE_VIEW_COLUMN (column), TRUE);
127         gtk_tree_view_append_column (GTK_TREE_VIEW (properties->fonts_treeview),
128                                      column);
129
130         renderer = GTK_CELL_RENDERER (g_object_new (GTK_TYPE_CELL_RENDERER_TEXT,
131                                                     "ypad", 6, NULL));
132         gtk_tree_view_column_pack_start (GTK_TREE_VIEW_COLUMN (column),
133                                          renderer, FALSE);
134         gtk_tree_view_column_set_title (GTK_TREE_VIEW_COLUMN (column),
135                                         _("Font"));
136         gtk_tree_view_column_set_cell_data_func (column, renderer,
137                                                  font_cell_data_func,
138                                                  NULL, NULL);
139
140         gtk_container_add (GTK_CONTAINER (swindow), properties->fonts_treeview);
141         gtk_widget_show (properties->fonts_treeview);
142
143         gtk_box_pack_start (GTK_BOX (properties), swindow, 
144                             TRUE, TRUE, 0);
145         gtk_widget_show (swindow);
146
147         properties->fonts_progress_label = gtk_label_new (NULL);
148         g_object_set (G_OBJECT (properties->fonts_progress_label),
149                       "xalign", 0.0,
150                       NULL);
151         gtk_box_pack_start (GTK_BOX (properties),
152                             properties->fonts_progress_label,
153                             FALSE, FALSE, 0);
154         gtk_widget_show (properties->fonts_progress_label);
155 }
156
157 static void
158 update_progress_label (GtkWidget *label, double progress)
159 {
160         if (progress > 0) {
161                 char *progress_text;
162                 progress_text = g_strdup_printf (_("Gathering font information… %3d%%"),
163                                                  (int) (progress * 100));
164                 gtk_label_set_text (GTK_LABEL (label), progress_text);
165                 g_free (progress_text);
166                 gtk_widget_show (label);
167         } else {
168                 gtk_widget_hide (label);
169         }
170 }
171
172 static void
173 job_fonts_finished_cb (EvJob *job, EvPropertiesFonts *properties)
174 {
175         g_signal_handlers_disconnect_by_func (job, job_fonts_finished_cb, properties);
176         g_object_unref (properties->fonts_job);
177         properties->fonts_job = NULL;
178 }
179
180 static void
181 job_fonts_updated_cb (EvJobFonts *job, gdouble progress, EvPropertiesFonts *properties)
182 {
183         GtkTreeModel *model;
184         EvDocumentFonts *document_fonts = EV_DOCUMENT_FONTS (properties->document);
185
186         update_progress_label (properties->fonts_progress_label, progress);
187
188         model = gtk_tree_view_get_model (GTK_TREE_VIEW (properties->fonts_treeview));
189         /* Documen lock is already held by the jop */
190         ev_document_fonts_fill_model (document_fonts, model);
191 }
192
193 void
194 ev_properties_fonts_set_document (EvPropertiesFonts *properties,
195                                   EvDocument        *document)
196 {
197         GtkTreeView *tree_view = GTK_TREE_VIEW (properties->fonts_treeview);
198         GtkListStore *list_store;
199
200         properties->document = document;
201
202         list_store = gtk_list_store_new (EV_DOCUMENT_FONTS_COLUMN_NUM_COLUMNS,
203                                          G_TYPE_STRING, G_TYPE_STRING);
204         gtk_tree_view_set_model (tree_view, GTK_TREE_MODEL (list_store));
205
206         properties->fonts_job = ev_job_fonts_new (properties->document);
207         g_signal_connect (properties->fonts_job, "updated",
208                           G_CALLBACK (job_fonts_updated_cb),
209                           properties);
210         g_signal_connect (properties->fonts_job, "finished",
211                           G_CALLBACK (job_fonts_finished_cb),
212                           properties);
213         ev_job_scheduler_push_job (properties->fonts_job, EV_JOB_PRIORITY_NONE);
214 }
215
216 GtkWidget *
217 ev_properties_fonts_new ()
218 {
219         EvPropertiesFonts *properties;
220
221         properties = g_object_new (EV_TYPE_PROPERTIES_FONTS, NULL);
222
223         return GTK_WIDGET (properties);
224 }