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