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