~centralelyon2010/inkscape/imagelinks2

« back to all changes in this revision

Viewing changes to src/ui/widget/spinbutton.cpp

  • Committer: JazzyNico
  • Date: 2011-08-29 20:25:30 UTC
  • Revision ID: nicoduf@yahoo.fr-20110829202530-6deuoz11q90usldv
Code refactoring and merging with trunk (revision 10599).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * \brief SpinButton widget, that allows entry of both '.' and ',' for the decimal, even when in numeric mode.
 
3
 */
 
4
/*
 
5
 * Author:
 
6
 *   Johan B. C. Engelen
 
7
 *
 
8
 * Copyright (C) 2011 Author
 
9
 *
 
10
 * Released under GNU GPL.  Read the file 'COPYING' for more information.
 
11
 */
 
12
 
 
13
#ifdef HAVE_CONFIG_H
 
14
# include <config.h>
 
15
#endif
 
16
 
 
17
#include "spinbutton.h"
 
18
 
 
19
#include "unit-menu.h"
 
20
#include "util/expression-evaluator.h"
 
21
#include "event-context.h"
 
22
 
 
23
namespace Inkscape {
 
24
namespace UI {
 
25
namespace Widget {
 
26
 
 
27
 
 
28
void
 
29
SpinButton::connect_signals() {
 
30
    signal_input().connect(sigc::mem_fun(*this, &SpinButton::on_input));
 
31
    signal_focus_in_event().connect(sigc::mem_fun(*this, &SpinButton::on_my_focus_in_event));
 
32
    signal_key_press_event().connect(sigc::mem_fun(*this, &SpinButton::on_my_key_press_event));
 
33
};
 
34
 
 
35
/**
 
36
 * This callback function should try to convert the entered text to a number and write it to newvalue.
 
37
 * It calls a method to evaluate the (potential) mathematical expression.
 
38
 *
 
39
 * @retval false No conversion done, continue with default handler.
 
40
 * @retval true  Conversion successful, don't call default handler. 
 
41
 */
 
42
int
 
43
SpinButton::on_input(double* newvalue)
 
44
{
 
45
    try {
 
46
        Inkscape::Util::GimpEevlQuantity result;
 
47
        if (_unit_menu) {
 
48
            Unit unit = _unit_menu->getUnit();
 
49
            result = Inkscape::Util::gimp_eevl_evaluate (get_text().c_str(), &unit);
 
50
            // check if output dimension corresponds to input unit
 
51
            if (result.dimension != (unit.isAbsolute() ? 1 : 0) ) {
 
52
                throw Inkscape::Util::EvaluatorException("Input dimensions do not match with parameter dimensions.","");
 
53
            }
 
54
        } else {
 
55
            result = Inkscape::Util::gimp_eevl_evaluate (get_text().c_str(), NULL);
 
56
        }
 
57
 
 
58
        *newvalue = result.value;
 
59
    }
 
60
    catch(Inkscape::Util::EvaluatorException &e) {
 
61
        g_message ("%s", e.what());
 
62
 
 
63
        return false;
 
64
    }
 
65
 
 
66
    return true;
 
67
}
 
68
 
 
69
/** When focus is obtained, save the value to enable undo later.
 
70
 * @retval false continue with default handler.
 
71
 * @retval true  don't call default handler. 
 
72
*/
 
73
bool
 
74
SpinButton::on_my_focus_in_event(GdkEventFocus* /*event*/)
 
75
{
 
76
    on_focus_in_value = get_value();
 
77
    return false; // do not consume the event
 
78
}
 
79
 
 
80
/** Handle specific keypress events, like Ctrl+Z
 
81
 * @retval false continue with default handler.
 
82
 * @retval true  don't call default handler. 
 
83
*/
 
84
bool
 
85
SpinButton::on_my_key_press_event(GdkEventKey* event)
 
86
{
 
87
    switch (get_group0_keyval (event)) {
 
88
    case GDK_Escape:
 
89
        undo();
 
90
        return true; // I consumed the event
 
91
        break;
 
92
    case GDK_z:
 
93
    case GDK_Z:
 
94
        if (event->state & GDK_CONTROL_MASK) {
 
95
            undo();
 
96
            return true; // I consumed the event
 
97
        }
 
98
        break;
 
99
    default:
 
100
        break;
 
101
    }
 
102
 
 
103
    return false; // do not consume the event
 
104
}
 
105
 
 
106
/**
 
107
 * Undo the editing, by resetting the value upon when the spinbutton got focus.
 
108
 */
 
109
void
 
110
SpinButton::undo()
 
111
{
 
112
    set_value(on_focus_in_value);
 
113
}
 
114
 
 
115
 
 
116
} // namespace Widget
 
117
} // namespace UI
 
118
} // namespace Inkscape
 
119
 
 
120
/* 
 
121
  Local Variables:
 
122
  mode:c++
 
123
  c-file-style:"stroustrup"
 
124
  c-file-offsets:((innamespace . 0)(inline-open . 0))
 
125
  indent-tabs-mode:nil
 
126
  fill-column:99
 
127
  End:
 
128
*/
 
129
// vim: filetype=c++:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :