summaryrefslogtreecommitdiffstats
path: root/src/ui/widget/zoom-status.cpp
blob: aea1beb17bd74ad842fef9f5263326a568e849cb (plain)
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
/*
 * Authors:
 *   Ralf Stephan <ralf@ark.in-berlin.de>
 *   Lauris Kaplinski <lauris@kaplinski.com>
 *   MenTaLguY <mental@rydia.net>
 *   bulia byak <buliabyak@users.sf.net>
 *
 * Copyright (C) 2005 Ralf Stephan
 * Copyright (C) 2004 MenTaLguY
 * Copyright (C) 1999-2002 Lauris Kaplinski
 * Copyright (C) 2000-2001 Ximian, Inc.
 *
 * Released under GNU GPL, read the file 'COPYING' for more information
 */

#include "ui/widget/zoom-status.h"
#include "desktop.h"
#include "desktop-handles.h"
#include "widgets/spw-utilities.h"

namespace Inkscape {
namespace UI {
namespace Widget {

ZoomStatus::ZoomStatus()
#if WITH_GTKMM_3_0
    : _adj(Gtk::Adjustment::create(0.0, -1.0, 1.0, 0.1, 0.1))
#else
    : _adj(0.0, -1.0, 1.0, 0.1, 0.1)
#endif
{
    _dt = 0;
    _upd_f = false;

    property_numeric() = false;
    property_update_policy() = Gtk::UPDATE_ALWAYS;
    sp_set_font_size_smaller(static_cast<GtkWidget*>((void*)gobj()));
}

ZoomStatus::~ZoomStatus()
{
    _dt = 0;
}

void
ZoomStatus::init(SPDesktop *dt)
{
    _dt = dt;
    property_digits() = 4;
#if WITH_GTKMM_3_0
    _adj->set_value(0.0);
    _adj->set_lower(log(SP_DESKTOP_ZOOM_MIN)/log(2.0));
    _adj->set_upper(log(SP_DESKTOP_ZOOM_MAX)/log(2.0));
    _adj->set_step_increment(0.1);
    _adj->set_page_increment(0.1);
#else
    _adj.set_value(0.0);
    _adj.set_lower(log(SP_DESKTOP_ZOOM_MIN)/log(2.0));
    _adj.set_upper(log(SP_DESKTOP_ZOOM_MAX)/log(2.0));
    _adj.set_step_increment(0.1);
    _adj.set_page_increment(0.1);
#endif
    set_adjustment(_adj);
}

void
ZoomStatus::update()
{
    if (!_dt) return;
    _upd_f = true;
    set_value(log(_dt->current_zoom())/log(2.0));
    _upd_f = false;
}

inline double
value_to_display(double value)
{
    return floor(pow(2, value) * 100.0 + 0.5);
}

inline double
display_to_value(double value)
{
    return  log(value / 100.0) / log(2.0);
}

int
ZoomStatus::on_input(double *new_val)
{
    double new_scrolled = get_value();
    double new_typed = atof(get_text().c_str());

    if (value_to_display(new_scrolled) == new_typed)
    { // the new value is set by scrolling
        *new_val = new_scrolled;
    } else { // the new value is typed in
        *new_val = display_to_value(new_typed);
    }

    return true;
}

bool
ZoomStatus::on_output()
{
    gchar b[64];
    g_snprintf(b, 64, "%4.0f%%", value_to_display(get_value()));
    set_text(b);
    return true;
}

void
ZoomStatus::on_value_changed()
{
    if (_upd_f) return;
    _upd_f = true;
    g_assert(_dt);
    double zoom_factor = pow(2, get_value());
    Geom::Rect const d = _dt->get_display_area();
    _dt->zoom_absolute(d.midpoint()[Geom::X], d.midpoint()[Geom::Y], zoom_factor);
    gtk_widget_grab_focus(static_cast<GtkWidget*>((void*)_dt->canvas));
    _upd_f = false;
}

}}}

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :