Code

UI generalisation
[inkscape.git] / src / ui / widget / zoom-status.cpp
1 /** \file
2  * Gtkmm facade/wrapper around zoom_status code that formerly lived
3  * in desktop-widget.cpp
4  *
5  * Authors:
6  *   Ralf Stephan <ralf@ark.in-berlin.de>
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   MenTaLguY <mental@rydia.net>
9  *   bulia byak <buliabyak@users.sf.net>
10  *
11  * Copyright (C) 2005 Ralf Stephan
12  * Copyright (C) 2004 MenTaLguY
13  * Copyright (C) 1999-2002 Lauris Kaplinski
14  * Copyright (C) 2000-2001 Ximian, Inc.
15  *
16  * Released under GNU GPL, read the file 'COPYING' for more information
17  */
19 #include "ui/widget/zoom-status.h"
20 #include "desktop.h"
21 #include "desktop-handles.h"
22 #include "widgets/spw-utilities.h"
23 #include "libnr/nr-convert2geom.h"
25 namespace Inkscape {
26 namespace UI {
27 namespace Widget {
29 ZoomStatus::ZoomStatus()
30     : _adj(0.0, -1.0, 1.0, 0.1, 0.1)
31 {
32     _dt = 0;
33     _upd_f = false;
35     property_numeric() = false;
36     property_update_policy() = Gtk::UPDATE_ALWAYS;
37     sp_set_font_size_smaller(static_cast<GtkWidget*>((void*)gobj()));
38 }
40 ZoomStatus::~ZoomStatus()
41 {
42     _dt = 0;
43 }
45 void
46 ZoomStatus::init(SPDesktop *dt)
47 {
48     _dt = dt;
49     property_digits() = 4;
50     _adj.set_value(0.0);
51     _adj.set_lower(log(SP_DESKTOP_ZOOM_MIN)/log(2.0));
52     _adj.set_upper(log(SP_DESKTOP_ZOOM_MAX)/log(2.0));
53     _adj.set_step_increment(0.1);
54     _adj.set_page_increment(0.1);
55     set_adjustment(_adj);
56 }
58 void
59 ZoomStatus::update()
60 {
61     if (!_dt) return;
62     _upd_f = true;
63     set_value(log(_dt->current_zoom())/log(2.0));
64     _upd_f = false;
65 }
67 inline double
68 value_to_display(double value)
69 {
70     return floor(pow(2, value) * 100.0 + 0.5);
71 }
73 inline double
74 display_to_value(double value)
75 {
76     return  log(value / 100.0) / log(2.0);
77 }
79 int
80 ZoomStatus::on_input(double *new_val)
81 {
82     double new_scrolled = get_value();
83     double new_typed = atof(get_text().c_str());
85     if (value_to_display(new_scrolled) == new_typed)
86     { // the new value is set by scrolling
87         *new_val = new_scrolled;
88     } else { // the new value is typed in
89         *new_val = display_to_value(new_typed);
90     }
92     return true;
93 }
95 bool
96 ZoomStatus::on_output()
97 {
98     gchar b[64];
99     g_snprintf(b, 64, "%4.0f%%", value_to_display(get_value()));
100     set_text(b);
101     return true;
104 void
105 ZoomStatus::on_value_changed()
107     if (_upd_f) return;
108     _upd_f = true;
109     g_assert(_dt);
110     double zoom_factor = pow(2, get_value());
111     Geom::Rect const d = _dt->get_display_area();
112     _dt->zoom_absolute(d.midpoint()[Geom::X], d.midpoint()[Geom::Y], zoom_factor);
113     gtk_widget_grab_focus(static_cast<GtkWidget*>((void*)_dt->canvas));   /// \todo this no love song
114     _upd_f = false;
117 }}}
119 /*
120   Local Variables:
121   mode:c++
122   c-file-style:"stroustrup"
123   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
124   indent-tabs-mode:nil
125   fill-column:99
126   End:
127 */
128 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :