]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlTabular.cpp
* src/frontend/controllers/frontend_helpers.cpp: safety fix suggested by Andre
[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         if (bv) {
36                 Cursor const & cur = bv->cursor();
37                 // get the innermost tabular inset;
38                 // assume that it is "ours"
39                 for (int i = cur.depth() - 1; i >= 0; --i)
40                         if (cur[i].inset().lyxCode() == Inset::TABULAR_CODE) {
41                                 active_cell_ = cur[i].idx();
42                                 break;
43                         }
44         }
45         InsetTabular tmp(kernel().buffer());
46         InsetTabularMailer::string2params(data, tmp);
47         params_.reset(new Tabular(tmp.tabular));
48         return true;
49 }
50
51
52 void ControlTabular::clearParams()
53 {
54         params_.reset();
55         active_cell_ = Tabular::npos;
56 }
57
58
59 Tabular::idx_type ControlTabular::getActiveCell() const
60 {
61         return active_cell_;
62 }
63
64
65 Tabular const & ControlTabular::tabular() const
66 {
67         BOOST_ASSERT(params_.get());
68         return *params_.get();
69 }
70
71
72 void ControlTabular::set(Tabular::Feature f, string const & arg)
73 {
74         string const data = featureAsString(f) + ' ' + arg;
75         kernel().dispatch(FuncRequest(getLfun(), data));
76 }
77
78
79 bool ControlTabular::useMetricUnits() const
80 {
81         return lyxrc.default_papersize > PAPER_USEXECUTIVE;
82 }
83
84
85 void ControlTabular::toggleTopLine()
86 {
87         if (tabular().isMultiColumn(getActiveCell()))
88                 set(Tabular::M_TOGGLE_LINE_TOP);
89         else
90                 set(Tabular::TOGGLE_LINE_TOP);
91 }
92
93
94 void ControlTabular::toggleBottomLine()
95 {
96         if (tabular().isMultiColumn(getActiveCell()))
97                 set(Tabular::M_TOGGLE_LINE_BOTTOM);
98         else
99                 set(Tabular::TOGGLE_LINE_BOTTOM);
100 }
101
102
103 void ControlTabular::toggleLeftLine()
104 {
105         if (tabular().isMultiColumn(getActiveCell()))
106                 set(Tabular::M_TOGGLE_LINE_LEFT);
107         else
108                 set(Tabular::TOGGLE_LINE_LEFT);
109 }
110
111
112 void ControlTabular::toggleRightLine()
113 {
114         if (tabular().isMultiColumn(getActiveCell()))
115                 set(Tabular::M_TOGGLE_LINE_RIGHT);
116         else
117                 set(Tabular::TOGGLE_LINE_RIGHT);
118 }
119
120
121 void ControlTabular::setSpecial(string const & special)
122 {
123         if (tabular().isMultiColumn(getActiveCell()))
124                 set(Tabular::SET_SPECIAL_MULTI, special);
125         else
126                 set(Tabular::SET_SPECIAL_COLUMN, special);
127 }
128
129
130 void ControlTabular::setWidth(string const & width)
131 {
132         if (tabular().isMultiColumn(getActiveCell()))
133                 set(Tabular::SET_MPWIDTH, width);
134         else
135                 set(Tabular::SET_PWIDTH, width);
136
137         dialog().view().update();
138 }
139
140
141 void ControlTabular::toggleMultiColumn()
142 {
143         set(Tabular::MULTICOLUMN);
144         dialog().view().update();
145 }
146
147
148 void ControlTabular::rotateTabular(bool yes)
149 {
150         if (yes)
151                 set(Tabular::SET_ROTATE_TABULAR);
152         else
153                 set(Tabular::UNSET_ROTATE_TABULAR);
154 }
155
156
157 void ControlTabular::rotateCell(bool yes)
158 {
159         if (yes)
160                 set(Tabular::SET_ROTATE_CELL);
161         else
162                 set(Tabular::UNSET_ROTATE_CELL);
163 }
164
165
166 void ControlTabular::halign(ControlTabular::HALIGN h)
167 {
168         Tabular::Feature num = Tabular::ALIGN_LEFT;
169         Tabular::Feature multi_num = Tabular::M_ALIGN_LEFT;
170
171         switch (h) {
172                 case LEFT:
173                         num = Tabular::ALIGN_LEFT;
174                         multi_num = Tabular::M_ALIGN_LEFT;
175                         break;
176                 case CENTER:
177                         num = Tabular::ALIGN_CENTER;
178                         multi_num = Tabular::M_ALIGN_CENTER;
179                         break;
180                 case RIGHT:
181                         num = Tabular::ALIGN_RIGHT;
182                         multi_num = Tabular::M_ALIGN_RIGHT;
183                         break;
184                 case BLOCK:
185                         num = Tabular::ALIGN_BLOCK;
186                         //multi_num: no equivalent
187                         break;
188         }
189
190         if (tabular().isMultiColumn(getActiveCell()))
191                 set(multi_num);
192         else
193                 set(num);
194 }
195
196
197 void ControlTabular::valign(ControlTabular::VALIGN v)
198 {
199         Tabular::Feature num = Tabular::VALIGN_MIDDLE;
200         Tabular::Feature multi_num = Tabular::M_VALIGN_MIDDLE;
201
202         switch (v) {
203                 case TOP:
204                         num = Tabular::VALIGN_TOP;
205                         multi_num = Tabular::M_VALIGN_TOP;
206                         break;
207                 case MIDDLE:
208                         num = Tabular::VALIGN_MIDDLE;
209                         multi_num = Tabular::M_VALIGN_MIDDLE;
210                         break;
211                 case BOTTOM:
212                         num = Tabular::VALIGN_BOTTOM;
213                         multi_num = Tabular::M_VALIGN_BOTTOM;
214                         break;
215         }
216
217         if (tabular().isMultiColumn(getActiveCell()))
218                 set(multi_num);
219         else
220                 set(num);
221 }
222
223
224 void ControlTabular::booktabs(bool yes)
225 {
226         if (yes)
227                 set(Tabular::SET_BOOKTABS);
228         else
229                 set(Tabular::UNSET_BOOKTABS);
230 }
231
232
233 void ControlTabular::longTabular(bool yes)
234 {
235         if (yes)
236                 set(Tabular::SET_LONGTABULAR);
237         else
238                 set(Tabular::UNSET_LONGTABULAR);
239 }
240
241 } // namespace frontend
242 } // namespace lyx