]> git.lyx.org Git - lyx.git/blob - src/insets/insetcollapsable.C
Make all of InsetCollapsable's member variables (save 'inset') private.
[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 "LColor.h"
21 #include "lyxlex.h"
22 #include "funcrequest.h"
23 #include "metricsinfo.h"
24 #include "paragraph.h"
25 #include "WordLangTuple.h"
26
27 #include "frontends/font_metrics.h"
28 #include "frontends/Painter.h"
29 #include "frontends/LyXView.h"
30
31 using lyx::graphics::PreviewLoader;
32
33 using std::endl;
34
35 using std::max;
36 using std::ostream;
37
38
39 InsetCollapsable::InsetCollapsable(BufferParams const & bp, bool collapsed)
40         : UpdatableInset(), inset(bp), collapsed_(collapsed),
41           button_dim(0, 0, 0, 0), label("Label"),
42 #if 0
43         autocollapse(false),
44 #endif
45           in_update(false), first_after_edit(false)
46 {
47         inset.setOwner(this);
48         inset.setAutoBreakRows(true);
49         inset.setDrawFrame(InsetText::ALWAYS);
50         inset.setFrameColor(LColor::collapsableframe);
51         setInsetName("Collapsable");
52 }
53
54
55 InsetCollapsable::InsetCollapsable(InsetCollapsable const & in)
56         : UpdatableInset(in), inset(in.inset), collapsed_(in.collapsed_),
57           labelfont_(in.labelfont_), button_dim(0, 0, 0, 0), label(in.label),
58 #if 0
59           autocollapse(in.autocollapse),
60 #endif
61           in_update(false), first_after_edit(false)
62 {
63         inset.init(&(in.inset));
64         inset.setOwner(this);
65 }
66
67
68 bool InsetCollapsable::insertInset(BufferView * bv, InsetOld * in)
69 {
70         if (!insetAllowed(in->lyxCode())) {
71                 lyxerr << "InsetCollapsable::InsertInset: "
72                         "Unable to insert inset." << endl;
73                 return false;
74         }
75         return inset.insertInset(bv, in);
76 }
77
78
79 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
80 {
81         os << "collapsed " << (collapsed_ ? "true" : "false") << "\n";
82         inset.writeParagraphData(buf, os);
83 }
84
85
86 void InsetCollapsable::read(Buffer const & buf, LyXLex & lex)
87 {
88         if (lex.isOK()) {
89                 lex.next();
90                 string const token = lex.getString();
91                 if (token == "collapsed") {
92                         lex.next();
93                         collapsed_ = lex.getBool();
94                 } else {
95                         lyxerr << "InsetCollapsable::Read: Missing collapsed!"
96                                << endl;
97                         // Take countermeasures
98                         lex.pushToken(token);
99                 }
100         }
101         inset.read(buf, lex);
102 }
103
104
105 void InsetCollapsable::dimension_collapsed(Dimension & dim) const
106 {
107         font_metrics::buttonText(label, labelfont_, dim.wid, dim.asc, dim.des);
108 }
109
110
111 int InsetCollapsable::height_collapsed() const
112 {
113         Dimension dim;
114         font_metrics::buttonText(label, labelfont_, dim.wid, dim.asc, dim.des);
115         return dim.asc + dim.des;
116 }
117
118
119 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
120 {
121         //lyxerr << "InsetCollapsable::metrics:  width: " << mi.base.textwidth << endl;
122         dimension_collapsed(dim);
123         if (!collapsed_) {
124                 Dimension insetdim;
125                 inset.metrics(mi, insetdim);
126                 dim.des += insetdim.height() + TEXT_TO_BOTTOM_OFFSET;
127                 dim.wid = max(dim.wid, insetdim.wid);
128         }
129         dim_ = dim;
130 }
131
132
133 void InsetCollapsable::draw_collapsed(PainterInfo & pi, int x, int y) const
134 {
135         pi.pain.buttonText(x, y, label, labelfont_);
136 }
137
138
139 void InsetCollapsable::draw(PainterInfo & pi, int x, int y, bool inlined) const
140 {
141         BOOST_ASSERT(pi.base.bv);
142         cache(pi.base.bv);
143
144         Dimension dim_collapsed;
145         dimension_collapsed(dim_collapsed);
146
147         int const aa  = ascent();
148         button_dim.x1 = 0;
149         button_dim.x2 = dim_collapsed.width();
150         button_dim.y1 = -aa;
151         button_dim.y2 = -aa + dim_collapsed.height();
152
153         if (!isOpen()) {
154                 draw_collapsed(pi, x, y);
155                 return;
156         }
157
158         int old_x = x;
159
160         if (!owner())
161                 x += scroll();
162
163         top_x = x;
164         top_baseline = y;
165
166         int const bl = y - aa + dim_collapsed.ascent();
167
168         if (inlined) {
169                 inset.draw(pi, x, y);
170         } else {
171                 draw_collapsed(pi, old_x, bl);
172                 inset.draw(pi, x, bl + dim_collapsed.descent() + inset.ascent());
173         }
174 }
175
176
177 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
178 {
179         // by default, we are not inlined-drawing
180         draw(pi, x, y, false);
181 }
182
183
184 InsetOld::EDITABLE InsetCollapsable::editable() const
185 {
186         return collapsed_ ? IS_EDITABLE : HIGHLY_EDITABLE;
187 }
188
189
190 void InsetCollapsable::insetUnlock(BufferView * bv)
191 {
192 #if 0
193         if (autocollapse) {
194                 if (change_label_with_text) {
195                         draw_label = get_new_label();
196                 } else {
197                         draw_label = label;
198                 }
199                 collapsed_ = true;
200         }
201 #endif
202         inset.insetUnlock(bv);
203         if (scroll())
204                 scroll(bv, 0.0F);
205         bv->updateInset(this);
206 }
207
208
209 FuncRequest InsetCollapsable::adjustCommand(FuncRequest const & cmd)
210 {
211         FuncRequest cmd1 = cmd;
212         cmd1.y = ascent() + cmd.y - (height_collapsed() + inset.ascent());
213         return cmd1;
214 }
215
216
217 void InsetCollapsable::lfunMouseRelease(FuncRequest const & cmd)
218 {
219         bool ret = false;
220         BufferView * bv = cmd.view();
221
222         if (collapsed_ && cmd.button() != mouse_button::button3) {
223                 collapsed_ = false;
224                 bv->updateInset(this);
225                 bv->buffer()->markDirty();
226                 return;
227         }
228
229         if (cmd.button() != mouse_button::button3 && hitButton(cmd)) {
230                 if (collapsed_) {
231                         collapsed_ = false;
232                 } else {
233                         collapsed_ = true;
234                         bv->unlockInset(this);
235                 }
236                 bv->updateInset(this);
237                 bv->buffer()->markDirty();
238         } else if (!collapsed_ && cmd.y > button_dim.y2) {
239                 ret = inset.localDispatch(adjustCommand(cmd)) == DISPATCHED;
240         }
241         if (cmd.button() == mouse_button::button3 && !ret)
242                 showInsetDialog(bv);
243 }
244
245
246 int InsetCollapsable::latex(Buffer const & buf, ostream & os,
247                             LatexRunParams const & runparams) const
248 {
249         return inset.latex(buf, os, runparams);
250 }
251
252
253 int InsetCollapsable::ascii(Buffer const & buf, ostream & os, int ll) const
254 {
255         return inset.ascii(buf, os, ll);
256 }
257
258
259 int InsetCollapsable::linuxdoc(Buffer const & buf, ostream & os) const
260 {
261         return inset.linuxdoc(buf, os);
262 }
263
264
265 int InsetCollapsable::docbook(Buffer const & buf, ostream & os, bool mixcont) const
266 {
267         return inset.docbook(buf, os, mixcont);
268 }
269
270
271 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
272 {
273         return button_dim.contains(cmd.x, cmd.y);
274 }
275
276
277 InsetOld::RESULT InsetCollapsable::localDispatch(FuncRequest const & cmd)
278 {
279         //lyxerr << "InsetCollapsable::localDispatch: "
280         //      << cmd.action << " '" << cmd.argument << "'\n";
281         BufferView * bv = cmd.view();
282         switch (cmd.action) {
283                 case LFUN_INSET_EDIT: {
284                         if (!cmd.argument.empty()) {
285                                 UpdatableInset::localDispatch(cmd);
286                                 if (collapsed_) {
287                                         lyxerr << "branch collapsed_" << endl;
288                                         collapsed_ = false;
289                                         if (bv->lockInset(this)) {
290                                                 bv->updateInset(this);
291                                                 bv->buffer()->markDirty();
292                                                 inset.localDispatch(cmd);
293                                                 first_after_edit = true;
294                                         }
295                                 } else {
296                                         lyxerr << "branch not collapsed_" << endl;
297                                         if (bv->lockInset(this))
298                                                 inset.localDispatch(cmd);
299                                 }
300                                 return DISPATCHED;
301                         }
302
303 #ifdef WITH_WARNINGS
304 #warning Fix this properly in BufferView_pimpl::workAreaButtonRelease
305 #endif
306                         if (cmd.button() == mouse_button::button3)
307                                 return DISPATCHED;
308
309                         UpdatableInset::localDispatch(cmd);
310
311                         if (collapsed_) {
312                                 collapsed_ = false;
313                                 // set this only here as it should be recollapsed only if
314                                 // it was already collapsed!
315                                 first_after_edit = true;
316                                 if (!bv->lockInset(this))
317                                         return DISPATCHED;
318                                 bv->updateInset(this);
319                                 bv->buffer()->markDirty();
320                                 inset.localDispatch(cmd);
321                         } else {
322                                 if (!bv->lockInset(this))
323                                         return DISPATCHED;
324                                 if (cmd.y <= button_dim.y2) {
325                                         FuncRequest cmd1 = cmd;
326                                         cmd1.y = 0;
327                                         inset.localDispatch(cmd1);
328                                 } else
329                                         inset.localDispatch(adjustCommand(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::setCollapsed(bool c) const
502 {
503         collapsed_ = c;
504 }
505
506
507 void InsetCollapsable::markErased()
508 {
509         inset.markErased();
510 }
511
512
513 bool InsetCollapsable::nextChange(BufferView * bv, lyx::pos_type & length)
514 {
515         bool found = inset.nextChange(bv, length);
516
517         if (first_after_edit && !found)
518                 close(bv);
519         else if (!found)
520                 first_after_edit = false;
521         return found;
522 }
523
524
525 bool InsetCollapsable::searchForward(BufferView * bv, string const & str,
526                                      bool cs, bool mw)
527 {
528         bool found = inset.searchForward(bv, str, cs, mw);
529         if (first_after_edit && !found)
530                 close(bv);
531         else if (!found)
532                 first_after_edit = false;
533         return found;
534 }
535
536
537 bool InsetCollapsable::searchBackward(BufferView * bv, string const & str,
538                                       bool cs, bool mw)
539 {
540         bool found = inset.searchBackward(bv, str, cs, mw);
541         if (first_after_edit && !found)
542                 close(bv);
543         else if (!found)
544                 first_after_edit = false;
545         return found;
546 }
547
548
549 WordLangTuple const
550 InsetCollapsable::selectNextWordToSpellcheck(BufferView * bv, float & value) const
551 {
552         WordLangTuple word = inset.selectNextWordToSpellcheck(bv, value);
553         if (first_after_edit && word.word().empty())
554                 close(bv);
555         first_after_edit = false;
556         return word;
557 }
558
559
560 void InsetCollapsable::addPreview(PreviewLoader & loader) const
561 {
562         inset.addPreview(loader);
563 }
564
565
566 void InsetCollapsable::cache(BufferView * bv) const
567 {
568         view_ = bv->owner()->view();
569 }
570
571
572 BufferView * InsetCollapsable::view() const
573 {
574         return view_.lock().get();
575 }