]> git.lyx.org Git - lyx.git/blob - src/insets/insetcollapsable.C
fa2e8585258ea99cfbe8b4028633488edfe501c9
[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         : inset(bp), label("Label"), status_(status)
45 {
46         inset.setOwner(this);
47         inset.setAutoBreakRows(true);
48         inset.setDrawFrame(InsetText::ALWAYS);
49         inset.setFrameColor(LColor::collapsableframe);
50         setInsetName("Collapsable");
51         setButtonLabel();
52 }
53
54
55 InsetCollapsable::InsetCollapsable(InsetCollapsable const & in)
56         : UpdatableInset(in), inset(in.inset),
57           labelfont_(in.labelfont_), label(in.label), status_(in.status_)
58 {
59         inset.setOwner(this);
60         setButtonLabel();
61 }
62
63
64 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
65 {
66         os << "status ";
67         switch (status_) {
68         case Open:
69                 os << "open";
70                 break;
71         case Collapsed:
72                 os << "collapsed";
73                 break;
74         case Inlined:
75                 os << "inlined";
76                 break;
77         }
78         os << "\n";
79         inset.text_.write(buf, os);
80 }
81
82
83 void InsetCollapsable::read(Buffer const & buf, LyXLex & lex)
84 {
85         bool token_found = false;
86         if (lex.isOK()) {
87                 lex.next();
88                 string const token = lex.getString();
89                 if (token == "status") {
90                         lex.next();
91                         string const tmp_token = lex.getString();
92
93                         if (tmp_token == "inlined") {
94                                 status_ = Inlined;
95                                 token_found = true;
96                         } else if (tmp_token == "collapsed") {
97                                 status_ = Collapsed;
98                                 token_found = true;
99                         } else if (tmp_token == "open") {
100                                 status_ = Open;
101                                 token_found = true;
102                         } else {
103                                 lyxerr << "InsetCollapsable::read: Missing status!"
104                                        << endl;
105                                 // Take countermeasures
106                                 lex.pushToken(token);
107                         }
108                 } else {
109                         lyxerr << "InsetCollapsable::Read: Missing 'status'-tag!"
110                                    << endl;
111                         // take countermeasures
112                         lex.pushToken(token);
113                 }
114         }
115         inset.read(buf, lex);
116
117         if (!token_found)
118                 status_ = isOpen() ? Open : Collapsed;
119
120         setButtonLabel();
121 }
122
123
124 void InsetCollapsable::dimension_collapsed(Dimension & dim) const
125 {
126         font_metrics::buttonText(label, labelfont_, dim.wid, dim.asc, dim.des);
127 }
128
129
130 int InsetCollapsable::height_collapsed() const
131 {
132         Dimension dim;
133         font_metrics::buttonText(label, labelfont_, dim.wid, dim.asc, dim.des);
134         return dim.asc + dim.des;
135 }
136
137
138 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
139 {
140         //lyxerr << "InsetCollapsable::metrics:  width: " << mi.base.textwidth << endl;
141         if (status_ == Inlined) {
142                 inset.metrics(mi, dim);
143         } else {
144                 dimension_collapsed(dim);
145                 if (status_ == Open) {
146                         Dimension insetdim;
147                         inset.metrics(mi, insetdim);
148                         dim.des += insetdim.height() + TEXT_TO_BOTTOM_OFFSET;
149                         dim.wid = max(dim.wid, insetdim.wid);
150                 }
151         }
152         dim_ = dim;
153         //lyxerr << "InsetCollapsable::metrics:  dim.wid: " << dim.wid << endl;
154 }
155
156
157 void InsetCollapsable::draw_collapsed(PainterInfo & pi, int x, int y) const
158 {
159         pi.pain.buttonText(x, y, label, labelfont_);
160 }
161
162
163 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
164 {
165         setPosCache(pi, x, y);
166
167         if (status_ == Inlined) {
168                 inset.draw(pi, x, y);
169         } else {
170                 Dimension dimc;
171                 dimension_collapsed(dimc);
172                 int const aa  = ascent();
173                 button_dim.x1 = x + 0;
174                 button_dim.x2 = x + dimc.width();
175                 button_dim.y1 = y - aa;
176                 button_dim.y2 = y - aa + dimc.height();
177
178                 draw_collapsed(pi, x, y);
179                 if (status_ == Open) {
180                         if (!owner())
181                                 x += scroll();
182                         inset.draw(pi, x, y - aa + dimc.height() + inset.ascent());
183                 }
184         }
185 }
186
187
188 InsetOld::EDITABLE InsetCollapsable::editable() const
189 {
190         return status_ != Collapsed ? HIGHLY_EDITABLE : IS_EDITABLE;
191 }
192
193
194 bool InsetCollapsable::descendable() const
195 {
196         return status_ != Collapsed;
197 }
198
199
200 DispatchResult
201 InsetCollapsable::lfunMouseRelease(LCursor & cur, FuncRequest const & cmd)
202 {
203         if (cmd.button() == mouse_button::button3) {
204                 lyxerr << "InsetCollapsable::lfunMouseRelease 0" << endl;
205                 showInsetDialog(&cur.bv());
206                 return DispatchResult(true, true);
207         }
208
209         switch (status_) {
210         case Collapsed:
211                 lyxerr << "InsetCollapsable::lfunMouseRelease 1" << endl;
212                 setStatus(Open);
213                 edit(cur, true);
214                 return DispatchResult(true, true);
215
216         case Open:
217                 if (hitButton(cmd)) {
218                         lyxerr << "InsetCollapsable::lfunMouseRelease 2" << endl;
219                         setStatus(Collapsed);
220                         // drop one level
221                         cur.bv().cursor() = cur;
222                         return DispatchResult(false, FINISHED_RIGHT);
223                 }
224                 lyxerr << "InsetCollapsable::lfunMouseRelease 3" << endl;
225                 return inset.dispatch(cur, cmd);
226
227         case Inlined:
228                 return inset.dispatch(cur, cmd);
229         }
230         BOOST_ASSERT(false);
231         // shut up compiler
232         return DispatchResult(true, true);
233 }
234
235
236 int InsetCollapsable::latex(Buffer const & buf, ostream & os,
237                             OutputParams const & runparams) const
238 {
239         return inset.latex(buf, os, runparams);
240 }
241
242
243 int InsetCollapsable::plaintext(Buffer const & buf, ostream & os,
244                             OutputParams const & runparams) const
245 {
246         return inset.plaintext(buf, os, runparams);
247 }
248
249
250 int InsetCollapsable::linuxdoc(Buffer const & buf, ostream & os,
251                                OutputParams const & runparams) const
252 {
253         return inset.linuxdoc(buf, os, runparams);
254 }
255
256
257 int InsetCollapsable::docbook(Buffer const & buf, ostream & os,
258                               OutputParams const & runparams) const
259 {
260         return inset.docbook(buf, os, runparams);
261 }
262
263
264 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
265 {
266         return button_dim.contains(cmd.x, cmd.y);
267 }
268
269
270 string const InsetCollapsable::getNewLabel(string const & l) const
271 {
272         string la;
273         pos_type const max_length = 15;
274         pos_type const p_siz = inset.paragraphs().begin()->size();
275         pos_type const n = min(max_length, p_siz);
276         pos_type i = 0;
277         pos_type j = 0;
278         for( ; i < n && j < p_siz; ++j) {
279                 if (inset.paragraphs().begin()->isInset(j))
280                         continue;
281                 la += inset.paragraphs().begin()->getChar(j);
282                 ++i;
283         }
284         if (inset.paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
285                 la += "...";
286         }
287         return la.empty() ? l : la;
288 }
289
290
291 void InsetCollapsable::edit(LCursor & cur, bool left)
292 {
293         //lyxerr << "InsetCollapsable: edit left/right" << endl;
294         cur.push(this);
295         inset.edit(cur, left);
296         open();
297 }
298
299
300 void InsetCollapsable::edit(LCursor & cur, int x, int y)
301 {
302         cur.push(this);
303         //lyxerr << "InsetCollapsable: edit xy" << endl;
304         if (status_ == Collapsed) {
305                 setStatus(Open);
306         } else {
307                 if (y <= button_dim.y2)
308                         y = 0;
309                 else
310                         y += inset.ascent() - height_collapsed();
311         }
312         inset.edit(cur, x, y);
313 }
314
315
316 DispatchResult
317 InsetCollapsable::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
318 {
319         lyxerr << "\nInsetCollapsable::priv_dispatch (begin): cmd: " << cmd
320                 << "  button y: " << button_dim.y2 << endl;
321         switch (cmd.action) {
322                 case LFUN_MOUSE_PRESS:
323                         if (status_ == Inlined)
324                                 inset.dispatch(cur, cmd);
325                         else if (status_ == Open && cmd.y > button_dim.y2)
326                                 inset.dispatch(cur, cmd);
327                         return DispatchResult(true, true);
328
329                 case LFUN_MOUSE_MOTION:
330                         if (status_ == Inlined)
331                                 inset.dispatch(cur, cmd);
332                         else if (status_ == Open && cmd.y > button_dim.y2)
333                                 inset.dispatch(cur, cmd);
334                         return DispatchResult(true, true);
335
336                 case LFUN_MOUSE_RELEASE:
337                         return lfunMouseRelease(cur, cmd);
338
339                 case LFUN_INSET_TOGGLE:
340                         if (inset.text_.toggleInset())
341                                 return DispatchResult(true, true);
342                         if (status_ == Open) {
343                                 setStatus(Inlined);
344                                 return DispatchResult(true, true);
345                         } else {
346                                 setStatus(Collapsed);
347                                 return DispatchResult(false, FINISHED_RIGHT);
348                         }
349
350                 default:
351                         return inset.dispatch(cur, cmd);
352         }
353 }
354
355
356 void InsetCollapsable::validate(LaTeXFeatures & features) const
357 {
358         inset.validate(features);
359 }
360
361
362 void InsetCollapsable::getCursorPos(CursorSlice const & cur,
363         int & x, int & y) const
364 {
365         inset.getCursorPos(cur, x, y);
366 }
367
368
369 void InsetCollapsable::getLabelList(Buffer const & buffer,
370                                     std::vector<string> & list) const
371 {
372         inset.getLabelList(buffer, list);
373 }
374
375
376 int InsetCollapsable::scroll(bool recursive) const
377 {
378         int sx = UpdatableInset::scroll(false);
379
380         if (recursive)
381                 sx += inset.scroll(false);
382
383         return sx;
384 }
385
386
387 int InsetCollapsable::numParagraphs() const
388 {
389         return inset.numParagraphs();
390 }
391
392
393 LyXText * InsetCollapsable::getText(int i) const
394 {
395         return inset.getText(i);
396 }
397
398
399 void InsetCollapsable::open()
400 {
401         if (status_ == Collapsed)   // ...but not inlined
402                 setStatus(Open);
403 }
404
405
406 void InsetCollapsable::close()
407 {
408         setStatus(Collapsed);
409 }
410
411
412 void InsetCollapsable::setLabel(string const & l)
413 {
414         label = l;
415 }
416
417
418 void InsetCollapsable::setStatus(CollapseStatus st)
419 {
420         status_ = st;
421         setButtonLabel();
422 }
423
424
425 void InsetCollapsable::markErased()
426 {
427         inset.markErased();
428 }
429
430
431 void InsetCollapsable::addPreview(PreviewLoader & loader) const
432 {
433         inset.addPreview(loader);
434 }
435
436
437 bool InsetCollapsable::insetAllowed(InsetOld::Code code) const
438 {
439         return inset.insetAllowed(code);
440 }
441
442
443 void InsetCollapsable::setLabelFont(LyXFont & font)
444 {
445         labelfont_ = font;
446 }
447
448
449 void InsetCollapsable::scroll(BufferView & bv, float sx) const
450 {
451         UpdatableInset::scroll(bv, sx);
452 }
453
454
455 void InsetCollapsable::scroll(BufferView & bv, int offset) const
456 {
457         UpdatableInset::scroll(bv, offset);
458 }
459
460
461 Box const & InsetCollapsable::buttonDim() const
462 {
463         return button_dim;
464 }
465
466
467 void InsetCollapsable::setBackgroundColor(LColor_color color)
468 {
469         InsetOld::setBackgroundColor(color);
470         inset.setBackgroundColor(color);
471 }