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