]> git.lyx.org Git - lyx.git/blob - src/insets/insetcollapsable.C
ea1e8349031d9e378de2a83095301b7b5fb47745
[lyx.git] / src / insets / insetcollapsable.C
1 /**
2  * \file insetcollapsable.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author Jürgen Vigna
8  * \author Lars Gullik Bjønnes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "insetcollapsable.h"
16
17 #include "buffer.h"
18 #include "BufferView.h"
19 #include "cursor.h"
20 #include "debug.h"
21 #include "dispatchresult.h"
22 #include "LColor.h"
23 #include "lyxlex.h"
24 #include "funcrequest.h"
25 #include "metricsinfo.h"
26 #include "paragraph.h"
27
28 #include "frontends/font_metrics.h"
29 #include "frontends/Painter.h"
30 #include "frontends/LyXView.h"
31
32
33 using lyx::graphics::PreviewLoader;
34
35 using std::endl;
36 using std::string;
37 using std::max;
38 using std::min;
39 using std::ostream;
40
41
42 InsetCollapsable::InsetCollapsable(BufferParams const & bp,
43         CollapseStatus status)
44         : InsetText(bp), label("Label"), status_(status), openinlined_(false)
45 {
46         setAutoBreakRows(true);
47         setDrawFrame(InsetText::ALWAYS);
48         setFrameColor(LColor::collapsableframe);
49         setInsetName("Collapsable");
50         setButtonLabel();
51 }
52
53
54 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
55 {
56         os << "status ";
57         switch (status_) {
58         case Open:
59                 os << "open";
60                 break;
61         case Collapsed:
62                 os << "collapsed";
63                 break;
64         case Inlined:
65                 os << "inlined";
66                 break;
67         }
68         os << "\n";
69         text_.write(buf, os);
70 }
71
72
73 void InsetCollapsable::read(Buffer const & buf, LyXLex & lex)
74 {
75         bool token_found = false;
76         if (lex.isOK()) {
77                 lex.next();
78                 string const token = lex.getString();
79                 if (token == "status") {
80                         lex.next();
81                         string const tmp_token = lex.getString();
82
83                         if (tmp_token == "inlined") {
84                                 status_ = Inlined;
85                                 token_found = true;
86                         } else if (tmp_token == "collapsed") {
87                                 status_ = Collapsed;
88                                 token_found = true;
89                         } else if (tmp_token == "open") {
90                                 status_ = Open;
91                                 token_found = true;
92                         } else {
93                                 lyxerr << "InsetCollapsable::read: Missing status!"
94                                        << endl;
95                                 // Take countermeasures
96                                 lex.pushToken(token);
97                         }
98                 } else {
99                         lyxerr << "InsetCollapsable::read: Missing 'status'-tag!"
100                                    << endl;
101                         // take countermeasures
102                         lex.pushToken(token);
103                 }
104         }
105         InsetText::read(buf, lex);
106
107         if (!token_found)
108                 status_ = isOpen() ? Open : Collapsed;
109
110         setButtonLabel();
111 }
112
113
114 void InsetCollapsable::dimension_collapsed(Dimension & dim) const
115 {
116         font_metrics::buttonText(label, labelfont_, dim.wid, dim.asc, dim.des);
117 }
118
119
120 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
121 {
122         mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
123         if (status_ == Inlined) {
124                 InsetText::metrics(mi, dim);
125         } else {
126                 dimension_collapsed(dim);
127                 if (status_ == Open) {
128                         InsetText::metrics(mi, textdim_);
129                         openinlined_ = (textdim_.wid + dim.wid <= mi.base.textwidth);
130                         if (openinlined_) {
131                                 dim.wid += textdim_.wid;
132                                 dim.des = max(dim.des, textdim_.des);
133                                 dim.asc = max(dim.asc, textdim_.asc);
134                         } else {
135                                 dim.des += textdim_.height() + TEXT_TO_BOTTOM_OFFSET;
136                                 dim.wid = max(dim.wid, textdim_.wid);
137                         }
138                 }
139         }
140         dim.asc += TEXT_TO_INSET_OFFSET;
141         dim.des += TEXT_TO_INSET_OFFSET;
142         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
143         mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
144         dim_ = dim;
145 }
146
147
148 void InsetCollapsable::draw_collapsed(PainterInfo & pi, int x, int y) const
149 {
150         pi.pain.buttonText(x, y, label, labelfont_);
151 }
152
153
154 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
155 {
156         x += TEXT_TO_INSET_OFFSET;
157         y += TEXT_TO_INSET_OFFSET;
158
159         if (status_ == Inlined) {
160                 InsetText::draw(pi, x, y);
161         } else {
162                 Dimension dimc;
163                 dimension_collapsed(dimc);
164                 int const aa  = ascent();
165                 button_dim.x1 = x + 0;
166                 button_dim.x2 = x + dimc.width();
167                 button_dim.y1 = y - aa + pi.base.bv->top_y();
168                 button_dim.y2 = y - aa + pi.base.bv->top_y() + dimc.height();
169
170                 draw_collapsed(pi, x, y - aa + dimc.asc);
171                 if (status_ == Open) {
172                         if (openinlined_)
173                                 InsetText::draw(pi, x + dimc.width(),
174                                                 y - aa + textdim_.asc);
175                         else 
176                                 InsetText::draw(pi, x, dimc.height()
177                                                 + y - aa + textdim_.asc);
178                 }
179         }
180         setPosCache(pi, x, y);
181 }
182
183
184 InsetOld::EDITABLE InsetCollapsable::editable() const
185 {
186         return status_ != Collapsed ? HIGHLY_EDITABLE : IS_EDITABLE;
187 }
188
189
190 bool InsetCollapsable::descendable() const
191 {
192         return status_ != Collapsed;
193 }
194
195
196 bool InsetCollapsable::hitButton(FuncRequest & cmd) const
197 {
198         return button_dim.contains(cmd.x, cmd.y);
199 }
200
201
202 string const InsetCollapsable::getNewLabel(string const & l) const
203 {
204         string label;
205         pos_type const max_length = 15;
206         pos_type const p_siz = paragraphs().begin()->size();
207         pos_type const n = min(max_length, p_siz);
208         pos_type i = 0;
209         pos_type j = 0;
210         for( ; i < n && j < p_siz; ++j) {
211                 if (paragraphs().begin()->isInset(j))
212                         continue;
213                 label += paragraphs().begin()->getChar(j);
214                 ++i;
215         }
216         if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
217                 label += "...";
218         }
219         return label.empty() ? l : label;
220 }
221
222
223 void InsetCollapsable::edit(LCursor & cur, bool left)
224 {
225         //lyxerr << "InsetCollapsable: edit left/right" << endl;
226         cur.push(*this);
227         InsetText::edit(cur, left);
228         open();
229 }
230
231
232 InsetBase * InsetCollapsable::editXY(LCursor & cur, int x, int y)
233 {
234         cur.push(*this);
235         //lyxerr << "InsetCollapsable: edit xy" << endl;
236         if (status_ == Collapsed) {
237                 setStatus(Open);
238                 // We are not calling editXY() because the row cache of the
239                 // inset might be invalid. 'Entering from the left' should be
240                 // ok, though.
241                 InsetText::edit(cur, true);
242                 return this;
243         }
244         return InsetText::editXY(cur, x, y);
245 }
246
247
248 void InsetCollapsable::priv_dispatch(LCursor & cur, FuncRequest & cmd)
249 {
250         //lyxerr << "\nInsetCollapsable::priv_dispatch (begin): cmd: " << cmd
251         //      << "  button y: " << button_dim.y2 << endl;
252         switch (cmd.action) {
253         case LFUN_MOUSE_PRESS:
254                 if (status_ == Inlined)
255                         InsetText::priv_dispatch(cur, cmd);
256                 else if (status_ == Open && !hitButton(cmd))
257                         InsetText::priv_dispatch(cur, cmd);
258                 break;
259
260         case LFUN_MOUSE_MOTION:
261                 if (status_ == Inlined)
262                         InsetText::priv_dispatch(cur, cmd);
263                 else if (status_ == Open && !hitButton(cmd))
264                         InsetText::priv_dispatch(cur, cmd);
265                 break;
266
267         case LFUN_MOUSE_RELEASE:
268                 if (cmd.button() == mouse_button::button3) {
269                         showInsetDialog(&cur.bv());
270                         break;
271                 }
272
273                 switch (status_) {
274
275                 case Collapsed:
276                         lyxerr << "InsetCollapsable::lfunMouseRelease 1" << endl;
277                         setStatus(Open);
278                         edit(cur, true);
279                         break;
280
281                 case Open: {
282                         FuncRequest cmd1 = cmd;
283                         if (hitButton(cmd1)) {
284                                 lyxerr << "InsetCollapsable::lfunMouseRelease 2" << endl;
285                                 setStatus(Collapsed);
286                                 cur.undispatched();
287                                 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
288                         } else {
289                                 lyxerr << "InsetCollapsable::lfunMouseRelease 3" << endl;
290                                 InsetText::priv_dispatch(cur, cmd);
291                         }
292                         break;
293                 }
294
295                 case Inlined:
296                         lyxerr << "InsetCollapsable::lfunMouseRelease 4" << endl;
297                         InsetText::priv_dispatch(cur, cmd);
298                         break;
299                 }
300                 break;
301
302         case LFUN_INSET_TOGGLE:
303                 if (cmd.argument == "open")
304                         setStatus(Open);
305                 else if (cmd.argument == "close")
306                         setStatus(Collapsed);
307                 else if (cmd.argument == "toggle"
308                          || cmd.argument.empty()) {
309                         if (isOpen())
310                                 setStatus(Collapsed);
311                         else
312                                 setStatus(Open);
313                 }
314                 cur.dispatched();
315                 break;
316
317         default:
318                 InsetText::priv_dispatch(cur, cmd);
319                 break;
320         }
321 }
322
323
324 int InsetCollapsable::scroll(bool recursive) const
325 {
326         int sx = UpdatableInset::scroll(false);
327
328         if (recursive)
329                 sx += InsetText::scroll(false);
330
331         return sx;
332 }
333
334
335 void InsetCollapsable::open()
336 {
337         if (status_ == Collapsed)   // ...but not inlined
338                 setStatus(Open);
339 }
340
341
342 void InsetCollapsable::close()
343 {
344         setStatus(Collapsed);
345 }
346
347
348 void InsetCollapsable::setLabel(string const & l)
349 {
350         label = l;
351 }
352
353
354 void InsetCollapsable::setStatus(CollapseStatus st)
355 {
356         status_ = st;
357         setButtonLabel();
358 }
359
360
361 void InsetCollapsable::setLabelFont(LyXFont & font)
362 {
363         labelfont_ = font;
364 }
365
366
367 void InsetCollapsable::scroll(BufferView & bv, float sx) const
368 {
369         UpdatableInset::scroll(bv, sx);
370 }
371
372
373 void InsetCollapsable::scroll(BufferView & bv, int offset) const
374 {
375         UpdatableInset::scroll(bv, offset);
376 }
377
378
379 Box const & InsetCollapsable::buttonDim() const
380 {
381         return button_dim;
382 }