1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Author:
4  *   Bryce Harrington <bryce@bryceharrington.org>
5  *
6  * Copyright (C) 2004 Bryce Harrington
7  *
8  * Released under GNU GPL v2+, read the file 'COPYING' for more information.
9  */
10 
11 #ifndef INKSCAPE_UI_WIDGET_UNIT_H
12 #define INKSCAPE_UI_WIDGET_UNIT_H
13 
14 #include <gtkmm/comboboxtext.h>
15 #include "util/units.h"
16 
17 using namespace Inkscape::Util;
18 
19 namespace Inkscape {
20 namespace UI {
21 namespace Widget {
22 
23 /**
24  * A drop down menu for choosing unit types.
25  */
26 class UnitMenu : public Gtk::ComboBoxText
27 {
28 public:
29 
30     /**
31      *    Construct a UnitMenu
32      */
33     UnitMenu();
34 
35     ~UnitMenu() override;
36 
37     /**
38      * Adds the unit type to the widget.  This extracts the corresponding
39      * units from the unit map matching the given type, and appends them
40      * to the dropdown widget.  It causes the primary unit for the given
41      * unit_type to be selected.
42      */
43     bool          setUnitType(UnitType unit_type);
44 
45     /**
46      * Removes all unit entries, then adds the unit type to the widget.
47      * This extracts the corresponding
48      * units from the unit map matching the given type, and appends them
49      * to the dropdown widget.  It causes the primary unit for the given
50      * unit_type to be selected.
51      */
52     bool          resetUnitType(UnitType unit_type);
53 
54     /**
55      * Adds a unit, possibly user-defined, to the menu.
56      */
57     void          addUnit(Unit const& u);
58 
59     /**
60      * Sets the dropdown widget to the given unit abbreviation.
61      * Returns true if the unit was selectable, false if not
62      * (i.e., if the unit was not present in the widget).
63      */
64     bool          setUnit(Glib::ustring const &unit);
65 
66     /**
67      * Returns the Unit object corresponding to the current selection
68      * in the dropdown widget.
69      */
70     Unit const *  getUnit() const;
71 
72     /**
73      * Returns the abbreviated unit name of the selected unit.
74      */
75     Glib::ustring getUnitAbbr() const;
76 
77     /**
78      * Returns the UnitType of the selected unit.
79      */
80     UnitType      getUnitType() const;
81 
82     /**
83      * Returns the unit factor for the selected unit.
84      */
85     double        getUnitFactor() const;
86 
87     /**
88      * Returns the recommended number of digits for displaying
89      *  numbers of this unit type.
90      */
91     int           getDefaultDigits() const;
92 
93     /**
94      * Returns the recommended step size in spin buttons
95      *  displaying units of this type.
96      */
97     double        getDefaultStep() const;
98 
99     /**
100      * Returns the recommended page size (when hitting pgup/pgdn)
101      *  in spin buttons displaying units of this type.
102      */
103     double        getDefaultPage() const;
104 
105     /**
106      *  Returns the conversion factor required to convert values
107      *  of the currently selected unit into units of type
108      *  new_unit_abbr.
109      */
110     double        getConversion(Glib::ustring const &new_unit_abbr, Glib::ustring const &old_unit_abbr = "no_unit") const;
111 
112     /**
113      * Returns true if the selected unit is not dimensionless
114      *  (false for %, true for px, pt, cm, etc).
115      */
116     bool          isAbsolute() const;
117 
118     /**
119      * Returns true if the selected unit is radial (deg or rad).
120      */
121     bool          isRadial() const;
122 
123 protected:
124     UnitType          _type;
125     /**
126      * block scroll from widget if is inside a scrolled window.
127      */
128     bool on_scroll_event(GdkEventScroll *event) override;
129 };
130 
131 } // namespace Widget
132 } // namespace UI
133 } // namespace Inkscape
134 
135 #endif // INKSCAPE_UI_WIDGET_UNIT_H
136 
137 /*
138   Local Variables:
139   mode:c++
140   c-file-style:"stroustrup"
141   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
142   indent-tabs-mode:nil
143   fill-column:99
144   End:
145 */
146 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
147