aboutsummaryrefslogtreecommitdiffstats
path: root/src/ppview-toolbar.c
blob: f501946c3d79635a98ef5c24eacfe3c334469223 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Copyright © 2002 Marco Pesenti Gritti
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *  $Id$
 */

#include "config.h"

#include "ppview-toolbar.h"
#include "ephy-embed-container.h"
#include "ephy-window.h"

#include <string.h>
#include <glib/gi18n.h>
#include <gtk/gtkentry.h>
#include <gtk/gtkmenu.h>
#include <gtk/gtkstock.h>
#include <gtk/gtktoolbar.h>
#include <gtk/gtkuimanager.h>

static void ppview_toolbar_class_init (PPViewToolbarClass *klass);
static void ppview_toolbar_init (PPViewToolbar *t);
static void ppview_toolbar_finalize (GObject *object);
static void ppview_toolbar_set_window (PPViewToolbar *t, EphyWindow *window);
static void
ppview_toolbar_set_property (GObject *object,
                             guint prop_id,
                             const GValue *value,
                             GParamSpec *pspec);
static void
ppview_toolbar_get_property (GObject *object,
                             guint prop_id,
                             GValue *value,
                             GParamSpec *pspec);

enum
{
    PROP_0,
    PROP_WINDOW
};

#define EPHY_PPVIEW_TOOLBAR_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_PPVIEW_TOOLBAR, PPViewToolbarPrivate))

struct PPViewToolbarPrivate
{
    EphyWindow *window;
    GtkUIManager *manager;
    GtkActionGroup *action_group;
    guint ui_id;
    int current_page;
};

static void
toolbar_cmd_ppv_goto_first (GtkUIManager *merge,
                PPViewToolbar *t);

static void
toolbar_cmd_ppv_goto_last (GtkUIManager *merge,
               PPViewToolbar *t);

static void
toolbar_cmd_ppv_go_back (GtkUIManager *merge,
             PPViewToolbar *t);

static void
toolbar_cmd_ppv_go_forward (GtkUIManager *merge,
                PPViewToolbar *t);

static void
toolbar_cmd_ppv_close (GtkUIManager *merge,
               PPViewToolbar *t);

static const GtkActionEntry entries [] = {
    { "PPVGotoFirst", GTK_STOCK_GOTO_FIRST,
      N_("First"), NULL,
      N_("Go to the first page"),
      (GCallback)toolbar_cmd_ppv_goto_first },
    { "PPVGotoLast", GTK_STOCK_GOTO_LAST,
      N_("Last"), NULL,
      N_("Go to the last page"),
      (GCallback)toolbar_cmd_ppv_goto_last },
    { "PPVGoBack", GTK_STOCK_GO_BACK,
      N_("Previous"), NULL,
      N_("Go to the previous page"),
      (GCallback)toolbar_cmd_ppv_go_back },
    { "PPVGoForward", GTK_STOCK_GO_FORWARD,
      N_("Next"), NULL,
      N_("Go to next page"),
      (GCallback)toolbar_cmd_ppv_go_forward },
    { "PPVClose", GTK_STOCK_CLOSE,
      N_("Close"), NULL,
      N_("Close print preview"),
      (GCallback)toolbar_cmd_ppv_close },
};

static const char ui_info[] =
"<ui>"
"<toolbar name=\"PPViewToolbar\">"
"<toolitem name=\"PPVGotoFirstItem\" action=\"PPVGotoFirst\" />"
"<toolitem name=\"PPVGotoLastItem\" action=\"PPVGotoLast\" />"
"<toolitem name=\"PPVGoBackItem\" action=\"PPVGoBack\" />"
"<toolitem name=\"PPVGoForwardItem\" action=\"PPVGoForward\" />"
"<toolitem name=\"PPVCloseItem\" action=\"PPVClose\" />"
"</toolbar>"
"</ui>\n";

G_DEFINE_TYPE (PPViewToolbar, ppview_toolbar, G_TYPE_OBJECT)

static void
ppview_toolbar_class_init (PPViewToolbarClass *klass)
{
        GObjectClass *object_class = G_OBJECT_CLASS (klass);

        object_class->finalize = ppview_toolbar_finalize;

    object_class->set_property = ppview_toolbar_set_property;
    object_class->get_property = ppview_toolbar_get_property;

    g_object_class_install_property (object_class,
                                         PROP_WINDOW,
                                         g_param_spec_object ("window",
                                                              "Window",
                                                              "Window",
                                                              EPHY_TYPE_WINDOW,
                                  G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB |
                                  G_PARAM_CONSTRUCT_ONLY));

    g_type_class_add_private (object_class, sizeof (PPViewToolbarPrivate));
}

static void
ppview_toolbar_set_property (GObject *object,
                      guint prop_id,
                      const GValue *value,
                      GParamSpec *pspec)
{
        PPViewToolbar *t = EPHY_PPVIEW_TOOLBAR (object);

        switch (prop_id)
        {
                case PROP_WINDOW:
                        ppview_toolbar_set_window (t, g_value_get_object (value));
                        break;
        }
}

static void
ppview_toolbar_get_property (GObject *object,
                      guint prop_id,
                      GValue *value,
                      GParamSpec *pspec)
{
    /* no readable properties */
    g_return_if_reached ();
}

static void
toolbar_update_sensitivity (PPViewToolbar *t)
{
    int pages, c_page;
    EphyWindow *window = t->priv->window;
    EphyEmbed *embed;
    GtkAction *action;
    GtkActionGroup *action_group = t->priv->action_group;

    embed = ephy_embed_container_get_active_child 
          (EPHY_EMBED_CONTAINER (window));
    g_return_if_fail (embed != NULL);

    pages = ephy_embed_print_preview_n_pages (embed);
    c_page = t->priv->current_page;

    action = gtk_action_group_get_action (action_group, "PPVGoBack");
    gtk_action_set_sensitive (action, c_page > 1);
    action = gtk_action_group_get_action (action_group, "PPVGotoFirst");
    gtk_action_set_sensitive (action, c_page > 1);
    action = gtk_action_group_get_action (action_group, "PPVGoForward");
    gtk_action_set_sensitive (action, c_page < pages);
    action = gtk_action_group_get_action (action_group, "PPVGotoLast");
    gtk_action_set_sensitive (action, c_page < pages);
}

static void
ppview_toolbar_set_window (PPViewToolbar *t, EphyWindow *window)
{
    GtkAction *action;
    GtkWidget *widget;
    GtkToolbarStyle style;

    g_return_if_fail (t->priv->window == NULL);

    t->priv->window = window;
    t->priv->manager = GTK_UI_MANAGER (ephy_window_get_ui_manager (t->priv->window));

    t->priv->action_group = gtk_action_group_new ("PPViewActions");
    gtk_action_group_set_translation_domain (t->priv->action_group, NULL);
    gtk_action_group_add_actions (t->priv->action_group, entries,   
                      G_N_ELEMENTS (entries), t);
    gtk_ui_manager_insert_action_group (t->priv->manager,
                        t->priv->action_group, 0);
    t->priv->ui_id = gtk_ui_manager_add_ui_from_string
        (t->priv->manager, ui_info, -1, NULL);

    action = gtk_action_group_get_action (t->priv->action_group,
                          "PPVClose");
    g_object_set (action,
              "short-label",
              _("Close print preview"),
              "is-important",
              TRUE,
              NULL);

    /* Force the display of the text only when needed */
    widget = gtk_ui_manager_get_widget (t->priv->manager,
                        "/PPViewToolbar");
    style = gtk_toolbar_get_style (GTK_TOOLBAR (widget));
    if (style == GTK_TOOLBAR_ICONS)
    {
        gtk_toolbar_set_style (GTK_TOOLBAR (widget),
                       GTK_TOOLBAR_BOTH_HORIZ);
    }

    toolbar_update_sensitivity (t);
}

static void
ppview_toolbar_init (PPViewToolbar *t)
{
    t->priv = EPHY_PPVIEW_TOOLBAR_GET_PRIVATE (t);

    t->priv->current_page = 1;
}

static void
ppview_toolbar_finalize (GObject *object)
{
    PPViewToolbar *t = EPHY_PPVIEW_TOOLBAR (object);

    gtk_ui_manager_remove_ui (t->priv->manager, t->priv->ui_id);
    gtk_ui_manager_remove_action_group (t->priv->manager,
                        t->priv->action_group);
    g_object_unref (t->priv->action_group);

        G_OBJECT_CLASS (ppview_toolbar_parent_class)->finalize (object);
}

PPViewToolbar *
ppview_toolbar_new (EphyWindow *window)
{
    return g_object_new (EPHY_TYPE_PPVIEW_TOOLBAR,
                 "window", window,
                 NULL);
}

static void
toolbar_cmd_ppv_goto_first (GtkUIManager *merge,
                PPViewToolbar *t)
{
    EphyWindow *window = t->priv->window;
    EphyEmbed *embed;

    embed = ephy_embed_container_get_active_child (EPHY_EMBED_CONTAINER (window));
    g_return_if_fail (EPHY_IS_EMBED (embed));

    ephy_embed_print_preview_navigate (embed, EPHY_EMBED_PRINTPREVIEW_HOME, 0);

    t->priv->current_page = 1;

    toolbar_update_sensitivity (t);
}

static void
toolbar_cmd_ppv_goto_last  (GtkUIManager *merge,
                PPViewToolbar *t)
{
    EphyWindow *window = t->priv->window;
    EphyEmbed *embed;

    embed = ephy_embed_container_get_active_child (EPHY_EMBED_CONTAINER (window));
    g_return_if_fail (embed != NULL);

    ephy_embed_print_preview_navigate (embed,
                       EPHY_EMBED_PRINTPREVIEW_END,
                       0);

    t->priv->current_page = ephy_embed_print_preview_n_pages (embed);

    toolbar_update_sensitivity (t);
}

static int
clamp_page_limits (PPViewToolbar *t, int page)
{
    EphyWindow *window = t->priv->window;
    EphyEmbed *embed;
    int pages;

    embed = ephy_embed_container_get_active_child 
          (EPHY_EMBED_CONTAINER (window));
    g_return_val_if_fail (embed != NULL, -1);

    pages = ephy_embed_print_preview_n_pages (embed);

    return CLAMP (page, 1, pages);
}

static void
toolbar_cmd_ppv_go_back  (GtkUIManager *merge,
              PPViewToolbar *t)
{
        EphyEmbedContainer *window = EPHY_EMBED_CONTAINER (t->priv->window);
    EphyEmbed *embed;

    embed = ephy_embed_container_get_active_child (window);
    g_return_if_fail (EPHY_IS_EMBED (embed));

    t->priv->current_page = clamp_page_limits (t, t->priv->current_page - 1);

    ephy_embed_print_preview_navigate (embed,
                       EPHY_EMBED_PRINTPREVIEW_GOTO_PAGENUM,
                       t->priv->current_page);

    toolbar_update_sensitivity (t);
}

static void
toolbar_cmd_ppv_go_forward (GtkUIManager *merge,
                PPViewToolbar *t)
{
    EphyWindow *window = t->priv->window;
    EphyEmbed *embed;

    embed = ephy_embed_container_get_active_child (EPHY_EMBED_CONTAINER (window));
    g_return_if_fail (EPHY_IS_EMBED (embed));

    t->priv->current_page = clamp_page_limits (t, t->priv->current_page + 1);

    ephy_embed_print_preview_navigate (embed,
                       EPHY_EMBED_PRINTPREVIEW_GOTO_PAGENUM,
                       t->priv->current_page);

    toolbar_update_sensitivity (t);
}

static void
toolbar_cmd_ppv_close (GtkUIManager *merge,
               PPViewToolbar *t)
{
    EphyWindow *window;
    EphyEmbed *embed;

    g_return_if_fail (EPHY_IS_PPVIEW_TOOLBAR (t));

    window = t->priv->window;
    g_return_if_fail (EPHY_IS_WINDOW (window));

    embed = ephy_embed_container_get_active_child 
          (EPHY_EMBED_CONTAINER (window));
    g_return_if_fail (EPHY_IS_EMBED (embed));

    _ephy_window_set_print_preview (window, FALSE);

    ephy_embed_set_print_preview_mode (embed, FALSE);
}