]> git.lyx.org Git - lyx.git/blob - src/frontends/gtk/GtkLengthEntry.C
Extracted from r14281
[lyx.git] / src / frontends / gtk / GtkLengthEntry.C
1 /**
2  * \file GtkLengthEntry.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Spray
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 // Too hard to make concept checks work with this file
14 #ifdef _GLIBCXX_CONCEPT_CHECKS
15 #undef _GLIBCXX_CONCEPT_CHECKS
16 #endif
17 #ifdef _GLIBCPP_CONCEPT_CHECKS
18 #undef _GLIBCPP_CONCEPT_CHECKS
19 #endif
20
21 #include "GtkLengthEntry.h"
22
23 #include "ghelpers.h"
24
25 #include <vector>
26 #include <sstream>
27
28 using std::string;
29 using std::vector;
30
31 namespace lyx {
32 namespace frontend {
33
34 namespace {
35
36 string const getLengthFromWidgets(Gtk::Adjustment const & adj, Gtk::ComboBoxText const & combo)
37 {
38         std::ostringstream os;
39         os << adj.get_value();
40         os << combo.get_active_text();
41         return os.str();
42 }
43
44
45 void setWidgetsFromLength(Gtk::Adjustment & adj, Gtk::ComboBoxText & combo, LyXLength const & length)
46 {
47         adj.set_value(length.value());
48
49         string unit = stringFromUnit(length.unit());
50         if (unit.empty())
51                 unit = getDefaultUnit();
52
53         combo.set_active_text (unit);
54 }
55
56
57 void populateUnitCombo(Gtk::ComboBoxText & combo, bool const userelative)
58 {
59         vector<string> const units = buildLengthUnitList(userelative);
60
61         vector<string>::const_iterator it = units.begin();
62         vector<string>::const_iterator const end = units.end();
63         for(; it != end; ++it)
64                 combo.append_text(*it);
65 }
66
67
68 }
69
70
71 GtkLengthEntry::GtkLengthEntry(
72         BaseObjectType* cobject,
73         const Glib::RefPtr<Gnome::Glade::Xml>& /*refGlade*/)
74 : Gtk::HBox(cobject), adj_(666.0, 0.0, 99999.0, 0.1, 1, 0.0), spin_(adj_, 0.1, 2)
75 {
76         populateUnitCombo (combo_, true);
77         relative_ = true;
78
79         set_spacing(6);
80         pack_start (spin_, true, true, 0);
81         pack_start (combo_, false, false, 0);
82         show_all();
83         spin_.signal_value_changed().connect(sigc::mem_fun(changedsignal_, &sigc::signal<void>::emit));
84         combo_.signal_changed().connect(sigc::mem_fun(changedsignal_, &sigc::signal<void>::emit));
85 }
86
87
88 sigc::signal< void >& GtkLengthEntry::signal_changed()
89 {
90         return changedsignal_;
91 }
92
93
94 void GtkLengthEntry::set_length(LyXLength const & length)
95 {
96         setWidgetsFromLength (*spin_.get_adjustment(), combo_, length);
97 }
98
99
100 void GtkLengthEntry::set_length(std::string const & length)
101 {
102         setWidgetsFromLength (*spin_.get_adjustment(), combo_, LyXLength(length));
103 }
104
105
106 LyXLength const GtkLengthEntry::get_length()
107 {
108         return LyXLength(getLengthFromWidgets(*spin_.get_adjustment(), combo_));
109 }
110
111
112 std::string const GtkLengthEntry::get_length_string()
113 {
114         return getLengthFromWidgets(*spin_.get_adjustment(), combo_);
115 }
116
117
118 void GtkLengthEntry::set_relative(bool rel)
119 {
120         combo_.clear();
121         if (rel != relative_) {
122                 populateUnitCombo (combo_, rel);
123                 relative_ = rel;
124         }
125 }
126
127
128 Gtk::ComboBoxText *GtkLengthEntry::get_combo()
129 {
130         return &combo_;
131 }
132
133
134 Gtk::SpinButton *GtkLengthEntry::get_spin()
135 {
136         return &spin_;
137 }
138
139 Gtk::ComboBoxText const *const GtkLengthEntry::get_combo() const
140 {
141         return &combo_;
142 }
143
144
145 Gtk::SpinButton const *const GtkLengthEntry::get_spin() const
146 {
147         return &spin_;
148 }
149
150
151 } // namespace frontend
152 } // namespace lyx