]> git.lyx.org Git - lyx.git/blob - src/insets/insetcollapsable.C
6efb25a2b750911e35532f985127eb3a8178a9d8
[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 << endl;
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         Dimension dim_collapsed;
153         dimension_collapsed(dim_collapsed);
154
155         int const aa  = ascent();
156         button_dim.x1 = 0;
157         button_dim.x2 = dim_collapsed.width();
158         button_dim.y1 = -aa;
159         button_dim.y2 = -aa + dim_collapsed.height();
160
161         if (!isOpen()) {
162                 draw_collapsed(pi, x, y);
163                 return;
164         }
165
166         int old_x = x;
167
168         if (!owner())
169                 x += scroll();
170
171         top_x = x;
172         top_baseline = y;
173
174         int const bl = y - aa + dim_collapsed.ascent();
175
176         if (inlined) {
177                 inset.draw(pi, x, y);
178         } else {
179                 draw_collapsed(pi, old_x, bl);
180                 inset.draw(pi, x, bl + dim_collapsed.descent() + inset.ascent());
181         }
182 }
183
184
185 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
186 {
187         // by default, we are not inlined-drawing
188         draw(pi, x, y, false);
189 }
190
191
192 InsetOld::EDITABLE InsetCollapsable::editable() const
193 {
194         return collapsed_ ? IS_EDITABLE : HIGHLY_EDITABLE;
195 }
196
197
198 void InsetCollapsable::insetUnlock(BufferView * bv)
199 {
200 #if 0
201         if (autocollapse) {
202                 if (change_label_with_text) {
203                         draw_label = get_new_label();
204                 } else {
205                         draw_label = label;
206                 }
207                 collapsed_ = true;
208         }
209 #endif
210         inset.insetUnlock(bv);
211         if (scroll())
212                 scroll(bv, 0.0F);
213         bv->updateInset();
214 }
215
216
217 FuncRequest InsetCollapsable::adjustCommand(FuncRequest const & cmd)
218 {
219         FuncRequest cmd1 = cmd;
220         cmd1.y = ascent() + cmd.y - (height_collapsed() + inset.ascent());
221         return cmd1;
222 }
223
224
225 void InsetCollapsable::lfunMouseRelease(FuncRequest const & cmd)
226 {
227         bool ret = false;
228         BufferView * bv = cmd.view();
229
230         if (collapsed_ && cmd.button() != mouse_button::button3) {
231                 collapsed_ = false;
232                 bv->updateInset();
233                 bv->buffer()->markDirty();
234                 return;
235         }
236
237         if (cmd.button() != mouse_button::button3 && hitButton(cmd)) {
238                 if (collapsed_) {
239                         collapsed_ = false;
240                 } else {
241                         collapsed_ = true;
242                         bv->unlockInset(this);
243                 }
244                 bv->updateInset();
245                 bv->buffer()->markDirty();
246         } else if (!collapsed_ && cmd.y > button_dim.y2) {
247                 ret = inset.localDispatch(adjustCommand(cmd)) == DISPATCHED;
248         }
249         if (cmd.button() == mouse_button::button3 && !ret)
250                 showInsetDialog(bv);
251 }
252
253
254 int InsetCollapsable::latex(Buffer const * buf, ostream & os,
255                             LatexRunParams const & runparams) const
256 {
257         return inset.latex(buf, os, runparams);
258 }
259
260
261 int InsetCollapsable::ascii(Buffer const * buf, ostream & os, int ll) const
262 {
263         return inset.ascii(buf, os, ll);
264 }
265
266
267 int InsetCollapsable::linuxdoc(Buffer const * buf, ostream & os) const
268 {
269         return inset.linuxdoc(buf, os);
270 }
271
272
273 int InsetCollapsable::docbook(Buffer const * buf, ostream & os, bool mixcont) const
274 {
275         return inset.docbook(buf, os, mixcont);
276 }
277
278
279 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
280 {
281         return button_dim.contained(cmd.x, cmd.y);
282 }
283
284
285 InsetOld::RESULT InsetCollapsable::localDispatch(FuncRequest const & cmd)
286 {
287         //lyxerr << "InsetCollapsable::localDispatch: "
288         //      << cmd.action << " '" << cmd.argument << "'\n";
289         BufferView * bv = cmd.view();
290         switch (cmd.action) {
291                 case LFUN_INSET_EDIT: {
292                         if (!cmd.argument.empty()) {
293                                 UpdatableInset::localDispatch(cmd);
294                                 if (collapsed_) {
295                                         lyxerr << "branch collapsed_" << endl;
296                                         collapsed_ = false;
297                                         if (bv->lockInset(this)) {
298                                                 bv->updateInset();
299                                                 bv->buffer()->markDirty();
300                                                 inset.localDispatch(cmd);
301                                                 first_after_edit = true;
302                                         }
303                                 } else {
304                                         lyxerr << "branch not collapsed_" << endl;
305                                         if (bv->lockInset(this))
306                                                 inset.localDispatch(cmd);
307                                 }
308                                 return DISPATCHED;
309                         }
310
311 #ifdef WITH_WARNINGS
312 #warning Fix this properly in BufferView_pimpl::workAreaButtonRelease
313 #endif
314                         if (cmd.button() == mouse_button::button3)
315                                 return DISPATCHED;
316
317                         UpdatableInset::localDispatch(cmd);
318
319                         if (collapsed_) {
320                                 collapsed_ = false;
321                                 // set this only here as it should be recollapsed only if
322                                 // it was already collapsed!
323                                 first_after_edit = true;
324                                 if (!bv->lockInset(this))
325                                         return DISPATCHED;
326                                 bv->updateInset();
327                                 bv->buffer()->markDirty();
328                                 inset.localDispatch(cmd);
329                         } else {
330                                 FuncRequest cmd1 = cmd;
331                                 if (!bv->lockInset(this))
332                                         return DISPATCHED;
333                                 if (cmd.y <= button_dim.y2) {
334                                         cmd1.y = 0;
335                                 } else {
336                                         cmd1.y = ascent() + cmd.y - (height_collapsed() + inset.ascent());
337                                 }
338                                 inset.localDispatch(cmd);
339                         }
340                         return DISPATCHED;
341                 }
342
343                 case LFUN_MOUSE_PRESS:
344                         if (!collapsed_ && cmd.y > button_dim.y2)
345                                 inset.localDispatch(adjustCommand(cmd));
346                         return DISPATCHED;
347
348                 case LFUN_MOUSE_MOTION:
349                         if (!collapsed_ && cmd.y > button_dim.y2)
350                                 inset.localDispatch(adjustCommand(cmd));
351                         return DISPATCHED;
352
353                 case LFUN_MOUSE_RELEASE:
354                         lfunMouseRelease(cmd);
355                         return DISPATCHED;
356
357                 default:
358                         UpdatableInset::RESULT result = inset.localDispatch(cmd);
359                         if (result >= FINISHED)
360                                 bv->unlockInset(this);
361                         first_after_edit = false;
362                         return result;
363         }
364 }
365
366
367 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
368 {
369         if (&inset == in)
370                 return true;
371         return inset.lockInsetInInset(bv, in);
372 }
373
374
375 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
376                                           bool lr)
377 {
378         if (&inset == in) {
379                 bv->unlockInset(this);
380                 return true;
381         }
382         return inset.unlockInsetInInset(bv, in, lr);
383 }
384
385
386 int InsetCollapsable::insetInInsetY() const
387 {
388         return inset.insetInInsetY() - (top_baseline - inset.y());
389 }
390
391
392 void InsetCollapsable::validate(LaTeXFeatures & features) const
393 {
394         inset.validate(features);
395 }
396
397
398 void InsetCollapsable::getCursor(BufferView & bv, int & x, int & y) const
399 {
400         inset.getCursor(bv, x, y);
401 }
402
403
404 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
405 {
406         inset.getCursorPos(bv, x , y);
407 }
408
409
410 UpdatableInset * InsetCollapsable::getLockingInset() const
411 {
412         UpdatableInset * in = inset.getLockingInset();
413         if (&inset == in)
414                 return const_cast<InsetCollapsable *>(this);
415         return in;
416 }
417
418
419 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(InsetOld::Code c)
420 {
421         if (c == lyxCode())
422                 return this;
423         return inset.getFirstLockingInsetOfType(c);
424 }
425
426
427 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
428                                bool toggleall, bool selectall)
429 {
430         inset.setFont(bv, font, toggleall, selectall);
431 }
432
433
434 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
435                                        bool const recursive) const
436 {
437         return inset.getLyXText(bv, recursive);
438 }
439
440
441 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
442 {
443         inset.deleteLyXText(bv, recursive);
444 }
445
446
447 void InsetCollapsable::resizeLyXText(BufferView * bv, bool force) const
448 {
449         inset.resizeLyXText(bv, force);
450         oldWidth = width();
451 }
452
453
454 void InsetCollapsable::getLabelList(std::vector<string> & list) const
455 {
456         inset.getLabelList(list);
457 }
458
459
460 int InsetCollapsable::scroll(bool recursive) const
461 {
462         int sx = UpdatableInset::scroll(false);
463
464         if (recursive)
465                 sx += inset.scroll(recursive);
466
467         return sx;
468 }
469
470
471 ParagraphList * InsetCollapsable::getParagraphs(int i) const
472 {
473         return inset.getParagraphs(i);
474 }
475
476
477 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
478 {
479         return inset.cursor(bv);
480 }
481
482
483 InsetOld * InsetCollapsable::getInsetFromID(int id_arg) const
484 {
485         if (id_arg == id())
486                 return const_cast<InsetCollapsable *>(this);
487         return inset.getInsetFromID(id_arg);
488 }
489
490
491 void InsetCollapsable::open(BufferView * bv)
492 {
493         if (!collapsed_)
494                 return;
495
496         collapsed_ = false;
497         bv->updateInset();
498 }
499
500
501 void InsetCollapsable::close(BufferView * bv) const
502 {
503         if (collapsed_)
504                 return;
505
506         collapsed_ = true;
507         bv->updateInset();
508 }
509
510
511 void InsetCollapsable::setLabel(string const & l) const
512 {
513         label = l;
514 }
515
516
517 void InsetCollapsable::markErased()
518 {
519         inset.markErased();
520 }
521
522
523 bool InsetCollapsable::nextChange(BufferView * bv, lyx::pos_type & length)
524 {
525         bool found = inset.nextChange(bv, length);
526
527         if (first_after_edit && !found)
528                 close(bv);
529         else if (!found)
530                 first_after_edit = false;
531         return found;
532 }
533
534
535 bool InsetCollapsable::searchForward(BufferView * bv, string const & str,
536                                      bool cs, bool mw)
537 {
538         bool found = inset.searchForward(bv, str, cs, mw);
539         if (first_after_edit && !found)
540                 close(bv);
541         else if (!found)
542                 first_after_edit = false;
543         return found;
544 }
545
546
547 bool InsetCollapsable::searchBackward(BufferView * bv, string const & str,
548                                       bool cs, bool mw)
549 {
550         bool found = inset.searchBackward(bv, str, cs, mw);
551         if (first_after_edit && !found)
552                 close(bv);
553         else if (!found)
554                 first_after_edit = false;
555         return found;
556 }
557
558
559 WordLangTuple const
560 InsetCollapsable::selectNextWordToSpellcheck(BufferView * bv, float & value) const
561 {
562         WordLangTuple word = inset.selectNextWordToSpellcheck(bv, value);
563         if (first_after_edit && word.word().empty())
564                 close(bv);
565         first_after_edit = false;
566         return word;
567 }
568
569
570 void InsetCollapsable::addPreview(PreviewLoader & loader) const
571 {
572         inset.addPreview(loader);
573 }
574
575
576 void InsetCollapsable::cache(BufferView * bv) const
577 {
578         view_ = bv->owner()->view();
579 }
580
581
582 BufferView * InsetCollapsable::view() const
583 {
584         return view_.lock().get();
585 }