]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlTabular.cpp
make lyx compile
[lyx.git] / src / frontends / controllers / ControlTabular.cpp
1 /**
2  * \file ControlTabular.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "BufferView.h"
14 #include "ControlTabular.h"
15 #include "Cursor.h"
16 #include "FuncRequest.h"
17 #include "LyXRC.h"
18 #include "insets/InsetTabular.h"
19
20
21 using std::string;
22
23 namespace lyx {
24 namespace frontend {
25
26 ControlTabular::ControlTabular(Dialog & parent)
27         : Dialog::Controller(parent), active_cell_(Tabular::npos)
28 {}
29
30
31 bool ControlTabular::initialiseParams(string const & data)
32 {
33         // try to get the current cell
34         BufferView const * const bv = kernel().bufferview();
35         InsetTabular const * current_inset = 0;
36         if (bv) {
37                 Cursor const & cur = bv->cursor();
38                 // get the innermost tabular inset;
39                 // assume that it is "ours"
40                 for (int i = cur.depth() - 1; i >= 0; --i)
41                         if (cur[i].inset().lyxCode() == Inset::TABULAR_CODE) {
42                                 current_inset = static_cast<InsetTabular const *>(&cur[i].inset());
43                                 active_cell_ = cur[i].idx();
44                                 break;
45                         }
46         }
47
48         if (current_inset && data.empty()) {
49                 params_.reset(new Tabular(current_inset->tabular));
50                 return true;
51         }
52
53         InsetTabular tmp(kernel().buffer());
54         InsetTabularMailer::string2params(data, tmp);
55         params_.reset(new Tabular(tmp.tabular));
56         return true;
57 }
58
59
60 void ControlTabular::clearParams()
61 {
62         params_.reset();
63         active_cell_ = Tabular::npos;
64 }
65
66
67 Tabular::idx_type ControlTabular::getActiveCell() const
68 {
69         return active_cell_;
70 }
71
72
73 Tabular const & ControlTabular::tabular() const
74 {
75         BOOST_ASSERT(params_.get());
76         return *params_.get();
77 }
78
79
80 void ControlTabular::set(Tabular::Feature f, string const & arg)
81 {
82         string const data = featureAsString(f) + ' ' + arg;
83         kernel().dispatch(FuncRequest(getLfun(), data));
84 }
85
86
87 bool ControlTabular::useMetricUnits() const
88 {
89         return lyxrc.default_papersize > PAPER_USEXECUTIVE;
90 }
91
92
93 void ControlTabular::toggleTopLine()
94 {
95         if (tabular().isMultiColumn(getActiveCell()))
96                 set(Tabular::M_TOGGLE_LINE_TOP);
97         else
98                 set(Tabular::TOGGLE_LINE_TOP);
99 }
100
101
102 void ControlTabular::toggleBottomLine()
103 {
104         if (tabular().isMultiColumn(getActiveCell()))
105                 set(Tabular::M_TOGGLE_LINE_BOTTOM);
106         else
107                 set(Tabular::TOGGLE_LINE_BOTTOM);
108 }
109
110
111 void ControlTabular::toggleLeftLine()
112 {
113         if (tabular().isMultiColumn(getActiveCell()))
114                 set(Tabular::M_TOGGLE_LINE_LEFT);
115         else
116                 set(Tabular::TOGGLE_LINE_LEFT);
117 }
118
119
120 void ControlTabular::toggleRightLine()
121 {
122         if (tabular().isMultiColumn(getActiveCell()))
123                 set(Tabular::M_TOGGLE_LINE_RIGHT);
124         else
125                 set(Tabular::TOGGLE_LINE_RIGHT);
126 }
127
128
129 void ControlTabular::setSpecial(string const & special)
130 {
131         if (tabular().isMultiColumn(getActiveCell()))
132                 set(Tabular::SET_SPECIAL_MULTI, special);
133         else
134                 set(Tabular::SET_SPECIAL_COLUMN, special);
135 }
136
137
138 void ControlTabular::setWidth(string const & width)
139 {
140         if (tabular().isMultiColumn(getActiveCell()))
141                 set(Tabular::SET_MPWIDTH, width);
142         else
143                 set(Tabular::SET_PWIDTH, width);
144
145         dialog().view().update();
146 }
147
148
149 void ControlTabular::toggleMultiColumn()
150 {
151         set(Tabular::MULTICOLUMN);
152         dialog().view().update();
153 }
154
155
156 void ControlTabular::rotateTabular(bool yes)
157 {
158         if (yes)
159                 set(Tabular::SET_ROTATE_TABULAR);
160         else
161                 set(Tabular::UNSET_ROTATE_TABULAR);
162 }
163
164
165 void ControlTabular::rotateCell(bool yes)
166 {
167         if (yes)
168                 set(Tabular::SET_ROTATE_CELL);
169         else
170                 set(Tabular::UNSET_ROTATE_CELL);
171 }
172
173
174 void ControlTabular::halign(ControlTabular::HALIGN h)
175 {
176         Tabular::Feature num = Tabular::ALIGN_LEFT;
177         Tabular::Feature multi_num = Tabular::M_ALIGN_LEFT;
178
179         switch (h) {
180                 case LEFT:
181                         num = Tabular::ALIGN_LEFT;
182                         multi_num = Tabular::M_ALIGN_LEFT;
183                         break;
184                 case CENTER:
185                         num = Tabular::ALIGN_CENTER;
186                         multi_num = Tabular::M_ALIGN_CENTER;
187                         break;
188                 case RIGHT:
189                         num = Tabular::ALIGN_RIGHT;
190                         multi_num = Tabular::M_ALIGN_RIGHT;
191                         break;
192                 case BLOCK:
193                         num = Tabular::ALIGN_BLOCK;
194                         //multi_num: no equivalent
195                         break;
196         }
197
198         if (tabular().isMultiColumn(getActiveCell()))
199                 set(multi_num);
200         else
201                 set(num);
202 }
203
204
205 void ControlTabular::valign(ControlTabular::VALIGN v)
206 {
207         Tabular::Feature num = Tabular::VALIGN_MIDDLE;
208         Tabular::Feature multi_num = Tabular::M_VALIGN_MIDDLE;
209
210         switch (v) {
211                 case TOP:
212                         num = Tabular::VALIGN_TOP;
213                         multi_num = Tabular::M_VALIGN_TOP;
214                         break;
215                 case MIDDLE:
216                         num = Tabular::VALIGN_MIDDLE;
217                         multi_num = Tabular::M_VALIGN_MIDDLE;
218                         break;
219                 case BOTTOM:
220                         num = Tabular::VALIGN_BOTTOM;
221                         multi_num = Tabular::M_VALIGN_BOTTOM;
222                         break;
223         }
224
225         if (tabular().isMultiColumn(getActiveCell()))
226                 set(multi_num);
227         else
228                 set(num);
229 }
230
231
232 void ControlTabular::booktabs(bool yes)
233 {
234         if (yes)
235                 set(Tabular::SET_BOOKTABS);
236         else
237                 set(Tabular::UNSET_BOOKTABS);
238 }
239
240
241 void ControlTabular::longTabular(bool yes)
242 {
243         if (yes)
244                 set(Tabular::SET_LONGTABULAR);
245         else
246                 set(Tabular::UNSET_LONGTABULAR);
247 }
248
249 } // namespace frontend
250 } // namespace lyx