]> git.lyx.org Git - lyx.git/blob - src/insets/insetcollapsable.C
Get rid of trailing whitespace 'noise' in future patches for the
[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 "debug.h"
20 #include "lyxlex.h"
21 #include "funcrequest.h"
22 #include "metricsinfo.h"
23 #include "paragraph.h"
24 #include "WordLangTuple.h"
25
26 #include "frontends/font_metrics.h"
27 #include "frontends/Painter.h"
28 #include "frontends/LyXView.h"
29
30 using namespace lyx::graphics;
31
32 using std::endl;
33 using std::max;
34 using std::ostream;
35
36
37 InsetCollapsable::InsetCollapsable(BufferParams const & bp, bool collapsed)
38         : UpdatableInset(), collapsed_(collapsed), inset(bp),
39           button_dim(0, 0, 0, 0), label("Label"),
40 #if 0
41         autocollapse(false),
42 #endif
43           in_update(false), first_after_edit(false)
44 {
45         inset.setOwner(this);
46         inset.setAutoBreakRows(true);
47         inset.setDrawFrame(InsetText::ALWAYS);
48         inset.setFrameColor(LColor::collapsableframe);
49         setInsetName("Collapsable");
50 }
51
52
53 InsetCollapsable::InsetCollapsable(InsetCollapsable const & in)
54         : UpdatableInset(in), collapsed_(in.collapsed_),
55           framecolor(in.framecolor), labelfont(in.labelfont), inset(in.inset),
56           button_dim(0, 0, 0, 0), label(in.label),
57 #if 0
58           autocollapse(in.autocollapse),
59 #endif
60           in_update(false), first_after_edit(false)
61 {
62         inset.init(&(in.inset));
63         inset.setOwner(this);
64 }
65
66
67 bool InsetCollapsable::insertInset(BufferView * bv, InsetOld * in)
68 {
69         if (!insetAllowed(in->lyxCode())) {
70                 lyxerr << "InsetCollapsable::InsertInset: "
71                         "Unable to insert inset." << endl;
72                 return false;
73         }
74         return inset.insertInset(bv, in);
75 }
76
77
78 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
79 {
80         os << "collapsed " << (collapsed_ ? "true" : "false") << "\n";
81         inset.writeParagraphData(buf, os);
82 }
83
84
85 void InsetCollapsable::read(Buffer const & buf, LyXLex & lex)
86 {
87         if (lex.isOK()) {
88                 lex.next();
89                 string const token = lex.getString();
90                 if (token == "collapsed") {
91                         lex.next();
92                         collapsed_ = lex.getBool();
93                 } else {
94                         lyxerr << "InsetCollapsable::Read: Missing collapsed!"
95                                << endl;
96                         // Take countermeasures
97                         lex.pushToken(token);
98                 }
99         }
100         inset.read(buf, lex);
101 }
102
103
104 void InsetCollapsable::dimension_collapsed(Dimension & dim) const
105 {
106         font_metrics::buttonText(label, labelfont, dim.wid, dim.asc, dim.des);
107 }
108
109
110 int InsetCollapsable::height_collapsed() const
111 {
112         Dimension dim;
113         font_metrics::buttonText(label, labelfont, dim.wid, dim.asc, dim.des);
114         return dim.asc + dim.des;
115 }
116
117
118 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
119 {
120         //lyxerr << "InsetCollapsable::metrics:  width: " << mi.base.textwidth << endl;
121         dimension_collapsed(dim);
122         if (!collapsed_) {
123                 Dimension insetdim;
124                 inset.metrics(mi, insetdim);
125                 dim.des += insetdim.height() + TEXT_TO_BOTTOM_OFFSET;
126                 dim.wid = max(dim.wid, insetdim.wid);
127         }
128         dim_ = dim;
129 }
130
131
132 void InsetCollapsable::draw_collapsed(PainterInfo & pi, int x, int y) const
133 {
134         pi.pain.buttonText(x, y, label, labelfont);
135 }
136
137
138 void InsetCollapsable::draw(PainterInfo & pi, int x, int y, bool inlined) const
139 {
140         BOOST_ASSERT(pi.base.bv);
141         cache(pi.base.bv);
142
143         Dimension dim_collapsed;
144         dimension_collapsed(dim_collapsed);
145
146         int const aa  = ascent();
147         button_dim.x1 = 0;
148         button_dim.x2 = dim_collapsed.width();
149         button_dim.y1 = -aa;
150         button_dim.y2 = -aa + dim_collapsed.height();
151
152         if (!isOpen()) {
153                 draw_collapsed(pi, x, y);
154                 return;
155         }
156
157         int old_x = x;
158
159         if (!owner())
160                 x += scroll();
161
162         top_x = x;
163         top_baseline = y;
164
165         int const bl = y - aa + dim_collapsed.ascent();
166
167         if (inlined) {
168                 inset.draw(pi, x, y);
169         } else {
170                 draw_collapsed(pi, old_x, bl);
171                 inset.draw(pi, x, bl + dim_collapsed.descent() + inset.ascent());
172         }
173 }
174
175
176 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
177 {
178         // by default, we are not inlined-drawing
179         draw(pi, x, y, false);
180 }
181
182
183 InsetOld::EDITABLE InsetCollapsable::editable() const
184 {
185         return collapsed_ ? IS_EDITABLE : HIGHLY_EDITABLE;
186 }
187
188
189 void InsetCollapsable::insetUnlock(BufferView * bv)
190 {
191 #if 0
192         if (autocollapse) {
193                 if (change_label_with_text) {
194                         draw_label = get_new_label();
195                 } else {
196                         draw_label = label;
197                 }
198                 collapsed_ = true;
199         }
200 #endif
201         inset.insetUnlock(bv);
202         if (scroll())
203                 scroll(bv, 0.0F);
204         bv->updateInset(this);
205 }
206
207
208 FuncRequest InsetCollapsable::adjustCommand(FuncRequest const & cmd)
209 {
210         FuncRequest cmd1 = cmd;
211         cmd1.y = ascent() + cmd.y - (height_collapsed() + inset.ascent());
212         return cmd1;
213 }
214
215
216 void InsetCollapsable::lfunMouseRelease(FuncRequest const & cmd)
217 {
218         bool ret = false;
219         BufferView * bv = cmd.view();
220
221         if (collapsed_ && cmd.button() != mouse_button::button3) {
222                 collapsed_ = false;
223                 bv->updateInset(this);
224                 bv->buffer()->markDirty();
225                 return;
226         }
227
228         if (cmd.button() != mouse_button::button3 && hitButton(cmd)) {
229                 if (collapsed_) {
230                         collapsed_ = false;
231                 } else {
232                         collapsed_ = true;
233                         bv->unlockInset(this);
234                 }
235                 bv->updateInset(this);
236                 bv->buffer()->markDirty();
237         } else if (!collapsed_ && cmd.y > button_dim.y2) {
238                 ret = inset.localDispatch(adjustCommand(cmd)) == DISPATCHED;
239         }
240         if (cmd.button() == mouse_button::button3 && !ret)
241                 showInsetDialog(bv);
242 }
243
244
245 int InsetCollapsable::latex(Buffer const & buf, ostream & os,
246                             LatexRunParams const & runparams) const
247 {
248         return inset.latex(buf, os, runparams);
249 }
250
251
252 int InsetCollapsable::ascii(Buffer const & buf, ostream & os, int ll) const
253 {
254         return inset.ascii(buf, os, ll);
255 }
256
257
258 int InsetCollapsable::linuxdoc(Buffer const & buf, ostream & os) const
259 {
260         return inset.linuxdoc(buf, os);
261 }
262
263
264 int InsetCollapsable::docbook(Buffer const & buf, ostream & os, bool mixcont) const
265 {
266         return inset.docbook(buf, os, mixcont);
267 }
268
269
270 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
271 {
272         return button_dim.contains(cmd.x, cmd.y);
273 }
274
275
276 InsetOld::RESULT InsetCollapsable::localDispatch(FuncRequest const & cmd)
277 {
278         //lyxerr << "InsetCollapsable::localDispatch: "
279         //      << cmd.action << " '" << cmd.argument << "'\n";
280         BufferView * bv = cmd.view();
281         switch (cmd.action) {
282                 case LFUN_INSET_EDIT: {
283                         if (!cmd.argument.empty()) {
284                                 UpdatableInset::localDispatch(cmd);
285                                 if (collapsed_) {
286                                         lyxerr << "branch collapsed_" << endl;
287                                         collapsed_ = false;
288                                         if (bv->lockInset(this)) {
289                                                 bv->updateInset(this);
290                                                 bv->buffer()->markDirty();
291                                                 inset.localDispatch(cmd);
292                                                 first_after_edit = true;
293                                         }
294                                 } else {
295                                         lyxerr << "branch not collapsed_" << endl;
296                                         if (bv->lockInset(this))
297                                                 inset.localDispatch(cmd);
298                                 }
299                                 return DISPATCHED;
300                         }
301
302 #ifdef WITH_WARNINGS
303 #warning Fix this properly in BufferView_pimpl::workAreaButtonRelease
304 #endif
305                         if (cmd.button() == mouse_button::button3)
306                                 return DISPATCHED;
307
308                         UpdatableInset::localDispatch(cmd);
309
310                         if (collapsed_) {
311                                 collapsed_ = false;
312                                 // set this only here as it should be recollapsed only if
313                                 // it was already collapsed!
314                                 first_after_edit = true;
315                                 if (!bv->lockInset(this))
316                                         return DISPATCHED;
317                                 bv->updateInset(this);
318                                 bv->buffer()->markDirty();
319                                 inset.localDispatch(cmd);
320                         } else {
321                                 FuncRequest cmd1 = cmd;
322                                 if (!bv->lockInset(this))
323                                         return DISPATCHED;
324                                 if (cmd.y <= button_dim.y2) {
325                                         cmd1.y = 0;
326                                 } else {
327                                         cmd1.y = ascent() + cmd.y - (height_collapsed() + inset.ascent());
328                                 }
329                                 inset.localDispatch(cmd);
330                         }
331                         return DISPATCHED;
332                 }
333
334                 case LFUN_MOUSE_PRESS:
335                         if (!collapsed_ && cmd.y > button_dim.y2)
336                                 inset.localDispatch(adjustCommand(cmd));
337                         return DISPATCHED;
338
339                 case LFUN_MOUSE_MOTION:
340                         if (!collapsed_ && cmd.y > button_dim.y2)
341                                 inset.localDispatch(adjustCommand(cmd));
342                         return DISPATCHED;
343
344                 case LFUN_MOUSE_RELEASE:
345                         lfunMouseRelease(cmd);
346                         return DISPATCHED;
347
348                 default:
349                         UpdatableInset::RESULT result = inset.localDispatch(cmd);
350                         if (result >= FINISHED)
351                                 bv->unlockInset(this);
352                         first_after_edit = false;
353                         return result;
354         }
355 }
356
357
358 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
359 {
360         if (&inset == in)
361                 return true;
362         return inset.lockInsetInInset(bv, in);
363 }
364
365
366 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
367                                           bool lr)
368 {
369         if (&inset == in) {
370                 bv->unlockInset(this);
371                 return true;
372         }
373         return inset.unlockInsetInInset(bv, in, lr);
374 }
375
376
377 int InsetCollapsable::insetInInsetY() const
378 {
379         return inset.insetInInsetY() - (top_baseline - inset.y());
380 }
381
382
383 void InsetCollapsable::validate(LaTeXFeatures & features) const
384 {
385         inset.validate(features);
386 }
387
388
389 void InsetCollapsable::getCursor(BufferView & bv, int & x, int & y) const
390 {
391         inset.getCursor(bv, x, y);
392 }
393
394
395 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
396 {
397         inset.getCursorPos(bv, x , y);
398 }
399
400
401 UpdatableInset * InsetCollapsable::getLockingInset() const
402 {
403         UpdatableInset * in = inset.getLockingInset();
404         if (&inset == in)
405                 return const_cast<InsetCollapsable *>(this);
406         return in;
407 }
408
409
410 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(InsetOld::Code c)
411 {
412         if (c == lyxCode())
413                 return this;
414         return inset.getFirstLockingInsetOfType(c);
415 }
416
417
418 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
419                                bool toggleall, bool selectall)
420 {
421         inset.setFont(bv, font, toggleall, selectall);
422 }
423
424
425 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
426                                        bool const recursive) const
427 {
428         return inset.getLyXText(bv, recursive);
429 }
430
431
432 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
433 {
434         inset.deleteLyXText(bv, recursive);
435 }
436
437
438 void InsetCollapsable::getLabelList(std::vector<string> & list) const
439 {
440         inset.getLabelList(list);
441 }
442
443
444 int InsetCollapsable::scroll(bool recursive) const
445 {
446         int sx = UpdatableInset::scroll(false);
447
448         if (recursive)
449                 sx += inset.scroll(recursive);
450
451         return sx;
452 }
453
454
455 ParagraphList * InsetCollapsable::getParagraphs(int i) const
456 {
457         return inset.getParagraphs(i);
458 }
459
460
461 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
462 {
463         return inset.cursor(bv);
464 }
465
466
467 InsetOld * InsetCollapsable::getInsetFromID(int id_arg) const
468 {
469         if (id_arg == id())
470                 return const_cast<InsetCollapsable *>(this);
471         return inset.getInsetFromID(id_arg);
472 }
473
474
475 void InsetCollapsable::open(BufferView * bv)
476 {
477         if (!collapsed_)
478                 return;
479
480         collapsed_ = false;
481         bv->updateInset(this);
482 }
483
484
485 void InsetCollapsable::close(BufferView * bv) const
486 {
487         if (collapsed_)
488                 return;
489
490         collapsed_ = true;
491         bv->updateInset(this);
492 }
493
494
495 void InsetCollapsable::setLabel(string const & l) const
496 {
497         label = l;
498 }
499
500
501 void InsetCollapsable::markErased()
502 {
503         inset.markErased();
504 }
505
506
507 bool InsetCollapsable::nextChange(BufferView * bv, lyx::pos_type & length)
508 {
509         bool found = inset.nextChange(bv, length);
510
511         if (first_after_edit && !found)
512                 close(bv);
513         else if (!found)
514                 first_after_edit = false;
515         return found;
516 }
517
518
519 bool InsetCollapsable::searchForward(BufferView * bv, string const & str,
520                                      bool cs, bool mw)
521 {
522         bool found = inset.searchForward(bv, str, cs, mw);
523         if (first_after_edit && !found)
524                 close(bv);
525         else if (!found)
526                 first_after_edit = false;
527         return found;
528 }
529
530
531 bool InsetCollapsable::searchBackward(BufferView * bv, string const & str,
532                                       bool cs, bool mw)
533 {
534         bool found = inset.searchBackward(bv, str, cs, mw);
535         if (first_after_edit && !found)
536                 close(bv);
537         else if (!found)
538                 first_after_edit = false;
539         return found;
540 }
541
542
543 WordLangTuple const
544 InsetCollapsable::selectNextWordToSpellcheck(BufferView * bv, float & value) const
545 {
546         WordLangTuple word = inset.selectNextWordToSpellcheck(bv, value);
547         if (first_after_edit && word.word().empty())
548                 close(bv);
549         first_after_edit = false;
550         return word;
551 }
552
553
554 void InsetCollapsable::addPreview(PreviewLoader & loader) const
555 {
556         inset.addPreview(loader);
557 }
558
559
560 void InsetCollapsable::cache(BufferView * bv) const
561 {
562         view_ = bv->owner()->view();
563 }
564
565
566 BufferView * InsetCollapsable::view() const
567 {
568         return view_.lock().get();
569 }