~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/poppler/glib/demo/outline.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2007 Carlos Garcia Campos  <carlosgc@gnome.org>
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2, or (at your option)
 
7
 * any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
 
17
 */
 
18
 
 
19
#include <gtk/gtk.h>
 
20
 
 
21
#include "outline.h"
 
22
#include "utils.h"
 
23
 
 
24
enum {
 
25
        OUTLINE_TITLE_COLUMN,
 
26
        OUTLINE_ACTION_TYPE_COLUMN,
 
27
        OUTLINE_EXPAND_COLUMN,
 
28
        OUTLINE_ACTION_COLUMN,
 
29
        N_COLUMNS
 
30
};
 
31
 
 
32
static void
 
33
build_tree (PopplerDocument  *document,
 
34
            GtkTreeModel     *model,
 
35
            GtkTreeIter      *parent,
 
36
            PopplerIndexIter *iter)
 
37
{
 
38
 
 
39
        do {
 
40
                GtkTreeIter       tree_iter;
 
41
                PopplerIndexIter *child;
 
42
                PopplerAction    *action;
 
43
                gboolean          expand;
 
44
                gchar            *markup;
 
45
                GEnumValue       *enum_value;
 
46
 
 
47
                action = poppler_index_iter_get_action (iter);
 
48
                expand = poppler_index_iter_is_open (iter);
 
49
 
 
50
                if (!action)
 
51
                        continue;
 
52
 
 
53
                markup = g_markup_escape_text (action->any.title, -1);
 
54
                enum_value = g_enum_get_value ((GEnumClass *) g_type_class_ref (POPPLER_TYPE_ACTION_TYPE), action->type);
 
55
                
 
56
                if (action->type == POPPLER_ACTION_GOTO_DEST &&
 
57
                    action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
 
58
                        /* TODO */
 
59
                }                       
 
60
 
 
61
                gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
 
62
                gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
 
63
                                    OUTLINE_TITLE_COLUMN, markup,
 
64
                                    OUTLINE_ACTION_TYPE_COLUMN, enum_value->value_name,
 
65
                                    OUTLINE_EXPAND_COLUMN, expand,
 
66
                                    OUTLINE_ACTION_COLUMN, action,
 
67
                                    -1);
 
68
                g_object_weak_ref (G_OBJECT (model),
 
69
                                   (GWeakNotify)poppler_action_free,
 
70
                                   action);
 
71
 
 
72
                g_free (markup);
 
73
 
 
74
                child = poppler_index_iter_get_child (iter);
 
75
                if (child)
 
76
                        build_tree (document, model, &tree_iter, child);
 
77
                poppler_index_iter_free (child);
 
78
        } while (poppler_index_iter_next (iter));
 
79
}
 
80
 
 
81
GtkTreeModel *
 
82
pgd_outline_create_model (PopplerDocument *document)
 
83
{
 
84
        GtkTreeModel     *model;
 
85
        PopplerIndexIter *iter;
 
86
 
 
87
        iter = poppler_index_iter_new (document);
 
88
        if (iter) {
 
89
                model = GTK_TREE_MODEL (
 
90
                        gtk_tree_store_new (N_COLUMNS,
 
91
                                            G_TYPE_STRING, G_TYPE_STRING, 
 
92
                                            G_TYPE_BOOLEAN, G_TYPE_POINTER));
 
93
                build_tree (document, model, NULL, iter);
 
94
                poppler_index_iter_free (iter);
 
95
        } else {
 
96
                GtkTreeIter tree_iter;
 
97
                gchar      *markup;
 
98
                
 
99
                model = GTK_TREE_MODEL (gtk_list_store_new (1, G_TYPE_STRING));
 
100
                gtk_list_store_append (GTK_LIST_STORE (model), &tree_iter);
 
101
                markup = g_strdup_printf ("<span size=\"larger\" style=\"italic\">%s</span>",
 
102
                                          "The document doesn't contain outline");
 
103
                gtk_list_store_set (GTK_LIST_STORE (model), &tree_iter,
 
104
                                    0, markup, -1);
 
105
                g_free (markup);
 
106
        }
 
107
 
 
108
        return model;
 
109
}
 
110
 
 
111
static void
 
112
expand_open_links (GtkTreeView  *tree_view,
 
113
                   GtkTreeModel *model,
 
114
                   GtkTreeIter  *parent)
 
115
{
 
116
        GtkTreeIter iter;
 
117
        gboolean    expand;
 
118
 
 
119
        if (gtk_tree_model_iter_children (model, &iter, parent)) {
 
120
                do {
 
121
                        gtk_tree_model_get (model, &iter,
 
122
                                            OUTLINE_EXPAND_COLUMN, &expand,
 
123
                                            -1);
 
124
                        if (expand) {
 
125
                                GtkTreePath *path;
 
126
 
 
127
                                path = gtk_tree_model_get_path (model, &iter);
 
128
                                gtk_tree_view_expand_row (tree_view, path, FALSE);
 
129
                                gtk_tree_path_free (path);
 
130
                        }
 
131
 
 
132
                        expand_open_links (tree_view, model, &iter);
 
133
                } while (gtk_tree_model_iter_next (model, &iter));
 
134
        }
 
135
}
 
136
 
 
137
static void
 
138
pgd_outline_selection_changed (GtkTreeSelection *treeselection,
 
139
                               GtkWidget        *action_view)
 
140
{
 
141
        GtkTreeModel *model;
 
142
        GtkTreeIter   iter;
 
143
        
 
144
        if (gtk_tree_selection_get_selected (treeselection, &model, &iter)) {
 
145
                PopplerAction *action;
 
146
 
 
147
                gtk_tree_model_get (model, &iter,
 
148
                                    OUTLINE_ACTION_COLUMN, &action,
 
149
                                    -1);
 
150
                pgd_action_view_set_action (action_view, action);
 
151
        }
 
152
}
 
153
 
 
154
GtkWidget *
 
155
pgd_outline_create_widget (PopplerDocument *document)
 
156
{
 
157
        GtkWidget        *swindow;
 
158
        GtkWidget        *treeview;
 
159
        GtkTreeModel     *model;
 
160
        GtkCellRenderer  *renderer;
 
161
        GtkTreeSelection *selection;
 
162
        GtkWidget        *hpaned, *action;
 
163
 
 
164
        hpaned = gtk_hpaned_new ();
 
165
 
 
166
        action = pgd_action_view_new (document);
 
167
        
 
168
        swindow = gtk_scrolled_window_new (NULL, NULL);
 
169
        gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
 
170
                                        GTK_POLICY_AUTOMATIC,
 
171
                                        GTK_POLICY_AUTOMATIC);
 
172
 
 
173
        model = pgd_outline_create_model (document);
 
174
        treeview = gtk_tree_view_new_with_model (model);
 
175
        g_object_unref (model);
 
176
 
 
177
        renderer = gtk_cell_renderer_text_new ();
 
178
        gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
 
179
                                                     0, "Title",
 
180
                                                     renderer,
 
181
                                                     "markup", OUTLINE_TITLE_COLUMN,
 
182
                                                     NULL);
 
183
        g_object_set (G_OBJECT (renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
 
184
        g_object_set (G_OBJECT (gtk_tree_view_get_column (GTK_TREE_VIEW (treeview), 0)),
 
185
                      "expand", TRUE, NULL);
 
186
 
 
187
        if (GTK_IS_TREE_STORE (model)) {
 
188
                renderer = gtk_cell_renderer_text_new ();
 
189
                gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
 
190
                                                             1, "Action Type",
 
191
                                                             renderer,
 
192
                                                             "text", OUTLINE_ACTION_TYPE_COLUMN,
 
193
                                                             NULL);
 
194
 
 
195
                expand_open_links (GTK_TREE_VIEW (treeview), model, NULL);
 
196
 
 
197
                selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview));
 
198
                g_signal_connect (G_OBJECT (selection), "changed",
 
199
                                  G_CALLBACK (pgd_outline_selection_changed),
 
200
                                  (gpointer)action);
 
201
        } else {
 
202
                gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview)),
 
203
                                             GTK_SELECTION_NONE);
 
204
        }
 
205
 
 
206
        gtk_container_add (GTK_CONTAINER (swindow), treeview);
 
207
        gtk_widget_show (treeview);
 
208
 
 
209
        gtk_paned_add1 (GTK_PANED (hpaned), swindow);
 
210
        gtk_widget_show (swindow);
 
211
 
 
212
        gtk_paned_add2 (GTK_PANED (hpaned), action);
 
213
        gtk_widget_show (action);
 
214
 
 
215
        gtk_paned_set_position (GTK_PANED (hpaned), 300);
 
216
 
 
217
        return hpaned;
 
218
}