]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCollapsible.cpp
Do not check again and again for non existing files
[lyx.git] / src / insets / InsetCollapsible.cpp
1 /**
2  * \file InsetCollapsible.cpp
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 "InsetCollapsible.h"
16
17 #include "Buffer.h"
18 #include "BufferView.h"
19 #include "Cursor.h"
20 #include "Dimension.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "InsetLayout.h"
24 #include "Lexer.h"
25 #include "MetricsInfo.h"
26 #include "OutputParams.h"
27 #include "TocBackend.h"
28
29 #include "frontends/FontMetrics.h"
30 #include "frontends/Painter.h"
31
32 #include "support/debug.h"
33 #include "support/docstream.h"
34 #include "support/gettext.h"
35 #include "support/lassert.h"
36 #include "support/lstrings.h"
37 #include "support/RefChanger.h"
38
39 using namespace std;
40
41
42 namespace lyx {
43
44 InsetCollapsible::InsetCollapsible(Buffer * buf, InsetText::UsePlain ltype)
45         : InsetText(buf, ltype), status_(Open)
46 {
47         setDrawFrame(true);
48         setFrameColor(Color_collapsibleframe);
49 }
50
51
52 // The sole purpose of this copy constructor is to make sure
53 // that the view_ map is not copied and remains empty.
54 InsetCollapsible::InsetCollapsible(InsetCollapsible const & rhs)
55         : InsetText(rhs),
56           status_(rhs.status_),
57           labelstring_(rhs.labelstring_)
58 {}
59
60
61 InsetCollapsible::~InsetCollapsible()
62 {
63         map<BufferView const *, View>::iterator it = view_.begin();
64         map<BufferView const *, View>::iterator end = view_.end();
65         for (; it != end; ++it)
66                 if (it->second.mouse_hover_)
67                         it->first->clearLastInset(this);
68 }
69
70
71 InsetCollapsible::CollapseStatus InsetCollapsible::status(BufferView const & bv) const
72 {
73         if (decoration() == InsetLayout::CONGLOMERATE)
74                 return status_;
75         return view_[&bv].auto_open_ ? Open : status_;
76 }
77
78
79 InsetCollapsible::Geometry InsetCollapsible::geometry(BufferView const & bv) const
80 {
81         switch (decoration()) {
82         case InsetLayout::CLASSIC:
83                 if (status(bv) == Open)
84                         return view_[&bv].openinlined_ ? LeftButton : TopButton;
85                 return ButtonOnly;
86
87         case InsetLayout::MINIMALISTIC:
88                 return status(bv) == Open ? NoButton : ButtonOnly ;
89
90         case InsetLayout::CONGLOMERATE:
91                 return status(bv) == Open ? SubLabel : Corners ;
92
93         case InsetLayout::DEFAULT:
94                 break; // this shouldn't happen
95         }
96
97         // dummy return value to shut down a warning,
98         // this is dead code.
99         return NoButton;
100 }
101
102
103 docstring InsetCollapsible::toolTip(BufferView const & bv, int x, int y) const
104 {
105         Dimension const dim = dimensionCollapsed(bv);
106         if (geometry(bv) == NoButton)
107                 return translateIfPossible(getLayout().labelstring());
108         if (x > xo(bv) + dim.wid || y > yo(bv) + dim.des || isOpen(bv))
109                 return docstring();
110
111         return toolTipText();
112 }
113
114
115 void InsetCollapsible::write(ostream & os) const
116 {
117         os << "status ";
118         switch (status_) {
119         case Open:
120                 os << "open";
121                 break;
122         case Collapsed:
123                 os << "collapsed";
124                 break;
125         }
126         os << "\n";
127         text().write(os);
128 }
129
130
131 void InsetCollapsible::read(Lexer & lex)
132 {
133         lex.setContext("InsetCollapsible::read");
134         string tmp_token;
135         status_ = Collapsed;
136         lex >> "status" >> tmp_token;
137         if (tmp_token == "open")
138                 status_ = Open;
139
140         InsetText::read(lex);
141         setButtonLabel();
142 }
143
144
145 Dimension InsetCollapsible::dimensionCollapsed(BufferView const & bv) const
146 {
147         Dimension dim;
148         FontInfo labelfont(getLabelfont());
149         labelfont.realize(sane_font);
150         theFontMetrics(labelfont).buttonText(
151                 buttonLabel(bv), TEXT_TO_INSET_OFFSET, dim.wid, dim.asc, dim.des);
152         return dim;
153 }
154
155
156 void InsetCollapsible::metrics(MetricsInfo & mi, Dimension & dim) const
157 {
158         view_[mi.base.bv].auto_open_ = mi.base.bv->cursor().isInside(this);
159
160         FontInfo tmpfont = mi.base.font;
161         mi.base.font = getFont();
162         mi.base.font.realize(tmpfont);
163
164         BufferView const & bv = *mi.base.bv;
165
166         switch (geometry(bv)) {
167         case NoButton:
168                 InsetText::metrics(mi, dim);
169                 break;
170         case Corners:
171                 InsetText::metrics(mi, dim);
172                 dim.des -= 3;
173                 dim.asc -= 1;
174                 break;
175         case SubLabel: {
176                 InsetText::metrics(mi, dim);
177                 // consider width of the inset label
178                 FontInfo font(getLabelfont());
179                 font.realize(sane_font);
180                 font.decSize();
181                 font.decSize();
182                 int w = 0;
183                 int a = 0;
184                 int d = 0;
185                 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
186                 dim.des += a + d;
187                 break;
188                 }
189         case TopButton:
190         case LeftButton:
191         case ButtonOnly:
192                 if (hasFixedWidth()){
193                         int const mindim = view_[&bv].button_dim_.x2 - view_[&bv].button_dim_.x1;
194                         if (mi.base.textwidth < mindim)
195                                 mi.base.textwidth = mindim;
196                 }
197                 dim = dimensionCollapsed(bv);
198                 if (geometry(bv) == TopButton || geometry(bv) == LeftButton) {
199                         Dimension textdim;
200                         InsetText::metrics(mi, textdim);
201                         view_[&bv].openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
202                         if (view_[&bv].openinlined_) {
203                                 // Correct for button width.
204                                 dim.wid += textdim.wid;
205                                 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
206                                 dim.asc = textdim.asc;
207                         } else {
208                                 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
209                                 dim.wid = max(dim.wid, textdim.wid);
210                         }
211                 }
212                 break;
213         }
214
215         mi.base.font = tmpfont;
216 }
217
218
219 bool InsetCollapsible::setMouseHover(BufferView const * bv, bool mouse_hover)
220         const
221 {
222         view_[bv].mouse_hover_ = mouse_hover;
223         return true;
224 }
225
226
227 void InsetCollapsible::draw(PainterInfo & pi, int x, int y) const
228 {
229         BufferView const & bv = *pi.base.bv;
230
231         view_[&bv].auto_open_ = bv.cursor().isInside(this);
232
233         Changer dummy = pi.base.font.change(getFont(), true);
234
235         // Draw button first -- top, left or only
236         Dimension dimc = dimensionCollapsed(bv);
237
238         if (geometry(bv) == TopButton ||
239             geometry(bv) == LeftButton ||
240             geometry(bv) == ButtonOnly) {
241                 view_[&bv].button_dim_.x1 = x + 0;
242                 view_[&bv].button_dim_.x2 = x + dimc.width();
243                 view_[&bv].button_dim_.y1 = y - dimc.asc;
244                 view_[&bv].button_dim_.y2 = y + dimc.des;
245
246                 FontInfo labelfont = getLabelfont();
247                 labelfont.setColor(labelColor());
248                 labelfont.realize(pi.base.font);
249                 pi.pain.buttonText(x, y, buttonLabel(bv), labelfont,
250                                    view_[&bv].mouse_hover_ ? Color_buttonhoverbg : Color_buttonbg,
251                                    Color_buttonframe, TEXT_TO_INSET_OFFSET);
252                 // Draw the change tracking cue on the label, unless RowPainter already
253                 // takes care of it.
254                 if (canPaintChange(bv))
255                         pi.change_.paintCue(pi, x, y, x + dimc.width(), labelfont);
256         } else {
257                 view_[&bv].button_dim_.x1 = 0;
258                 view_[&bv].button_dim_.y1 = 0;
259                 view_[&bv].button_dim_.x2 = 0;
260                 view_[&bv].button_dim_.y2 = 0;
261         }
262
263         Dimension const textdim = dimensionHelper(bv);
264         int const baseline = y;
265         int textx, texty;
266         Geometry g = geometry(bv);
267         switch (g) {
268         case LeftButton:
269         case TopButton: {
270                 if (g == LeftButton) {
271                         textx = x + dimc.width();
272                         texty = baseline;
273                 } else {
274                         textx = x;
275                         texty = baseline + dimc.des + textdim.asc;
276                 }
277                 // Do not draw the cue for INSERTED -- it is already in the button and
278                 // that's enough.
279                 Changer dummy = (pi.change_.type == Change::INSERTED)
280                         ? make_change(pi.change_, Change())
281                         : Changer();
282                 InsetText::draw(pi, textx, texty);
283                 break;
284         }
285         case ButtonOnly:
286                 break;
287         case NoButton:
288                 textx = x;
289                 texty = baseline;
290                 InsetText::draw(pi, textx, texty);
291                 break;
292         case SubLabel:
293         case Corners:
294                 textx = x;
295                 texty = baseline;
296                 {       // We will take care of the frame and the change tracking cue
297                         // ourselves, below.
298                         Changer dummy = make_change(pi.change_, Change());
299                         const_cast<InsetCollapsible *>(this)->setDrawFrame(false);
300                         InsetText::draw(pi, textx, texty);
301                         const_cast<InsetCollapsible *>(this)->setDrawFrame(true);
302                 }
303
304                 int desc = textdim.descent();
305                 if (g == Corners)
306                         desc -= 3;
307
308                 // Colour the frame according to the change type. (Like for tables.)
309                 Color colour = pi.change_.changed() ? pi.change_.color()
310                                                     : Color_foreground;
311                 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
312                 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
313                 pi.pain.line(xx1, y + desc - 4,
314                              xx1, y + desc, colour);
315                 if (status_ == Open)
316                         pi.pain.line(xx1, y + desc,
317                                      xx2, y + desc, colour);
318                 else {
319                         // Make status_ value visible:
320                         pi.pain.line(xx1, y + desc,
321                                      xx1 + 4, y + desc, colour);
322                         pi.pain.line(xx2 - 4, y + desc,
323                                      xx2, y + desc, colour);
324                 }
325                 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
326                              y + desc - 4, colour);
327
328                 // the label below the text. Can be toggled.
329                 if (g == SubLabel) {
330                         FontInfo font(getLabelfont());
331                         if (pi.change_.changed())
332                                 font.setPaintColor(colour);
333                         font.realize(sane_font);
334                         font.decSize();
335                         font.decSize();
336                         int w = 0;
337                         int a = 0;
338                         int d = 0;
339                         Color const col = pi.full_repaint ? Color_none : pi.backgroundColor(this);
340                         theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
341                         int const ww = max(textdim.wid, w);
342                         pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
343                                          buttonLabel(bv), font, col, Color_none);
344                 }
345
346                 int const y1 = y - textdim.asc + 3;
347                 // a visual cue when the cursor is inside the inset
348                 Cursor const & cur = bv.cursor();
349                 if (cur.isInside(this)) {
350                         pi.pain.line(xx1, y1 + 4, xx1, y1, colour);
351                         pi.pain.line(xx1 + 4, y1, xx1, y1, colour);
352                         pi.pain.line(xx2, y1 + 4, xx2, y1, colour);
353                         pi.pain.line(xx2 - 4, y1, xx2, y1, colour);
354                 }
355                 // Strike through the inset if deleted and not already handled by
356                 // RowPainter.
357                 if (pi.change_.deleted() && canPaintChange(bv))
358                         pi.change_.paintCue(pi, xx1, y1, xx2, y + desc);
359                 break;
360         }
361 }
362
363
364 void InsetCollapsible::cursorPos(BufferView const & bv,
365                 CursorSlice const & sl, bool boundary, int & x, int & y) const
366 {
367         if (geometry(bv) == ButtonOnly)
368                 status_ = Open;
369
370         InsetText::cursorPos(bv, sl, boundary, x, y);
371         Dimension const textdim = dimensionHelper(bv);
372
373         switch (geometry(bv)) {
374         case LeftButton:
375                 x += dimensionCollapsed(bv).wid;
376                 break;
377         case TopButton: {
378                 y += dimensionCollapsed(bv).des + textdim.asc;
379                 break;
380         }
381         case NoButton:
382         case SubLabel:
383         case Corners:
384                 // Do nothing
385                 break;
386         case ButtonOnly:
387                 // Cannot get here
388                 break;
389         }
390 }
391
392
393 bool InsetCollapsible::editable() const
394 {
395         switch (decoration()) {
396         case InsetLayout::CLASSIC:
397         case InsetLayout::MINIMALISTIC:
398                 return status_ == Open;
399         default:
400                 return true;
401         }
402 }
403
404
405 bool InsetCollapsible::descendable(BufferView const & bv) const
406 {
407         return geometry(bv) != ButtonOnly;
408 }
409
410
411 bool InsetCollapsible::clickable(BufferView const & bv, int x, int y) const
412 {
413         return view_[&bv].button_dim_.contains(x, y);
414 }
415
416
417 docstring const InsetCollapsible::getNewLabel(docstring const & l) const
418 {
419         odocstringstream label;
420         pos_type const max_length = 15;
421         pos_type const p_siz = paragraphs().begin()->size();
422         pos_type const n = min(max_length, p_siz);
423         pos_type i = 0;
424         pos_type j = 0;
425         for (; i < n && j < p_siz; ++j) {
426                 if (paragraphs().begin()->isDeleted(j))
427                         continue;
428                 if (paragraphs().begin()->isInset(j)) {
429                         if (!paragraphs().begin()->getInset(j)->isChar())
430                                 continue;
431                         paragraphs().begin()->getInset(j)->toString(label);
432                 } else
433                         label.put(paragraphs().begin()->getChar(j));
434                 ++i;
435         }
436         if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
437                 label << "...";
438         }
439         docstring const lbl = label.str();
440         return lbl.empty() ? l : lbl;
441 }
442
443
444 void InsetCollapsible::edit(Cursor & cur, bool front, EntryDirection entry_from)
445 {
446         //lyxerr << "InsetCollapsible: edit left/right" << endl;
447         cur.push(*this);
448         InsetText::edit(cur, front, entry_from);
449 }
450
451
452 Inset * InsetCollapsible::editXY(Cursor & cur, int x, int y)
453 {
454         //lyxerr << "InsetCollapsible: edit xy" << endl;
455         if (geometry(cur.bv()) == ButtonOnly
456                 || !descendable(cur.bv())
457             || (view_[&cur.bv()].button_dim_.contains(x, y)
458                 && geometry(cur.bv()) != NoButton))
459                 return this;
460         cur.push(*this);
461         return InsetText::editXY(cur, x, y);
462 }
463
464
465 void InsetCollapsible::doDispatch(Cursor & cur, FuncRequest & cmd)
466 {
467         //lyxerr << "InsetCollapsible::doDispatch (begin): cmd: " << cmd
468         //      << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
469
470         bool const hitButton = clickable(cur.bv(), cmd.x(), cmd.y());
471
472         switch (cmd.action()) {
473         case LFUN_MOUSE_PRESS:
474                 if (hitButton) {
475                         switch (cmd.button()) {
476                         case mouse_button::button1:
477                         case mouse_button::button3:
478                                 // Pass the command to the enclosing InsetText,
479                                 // so that the cursor gets set.
480                                 cur.undispatched();
481                                 break;
482                         case mouse_button::none:
483                         case mouse_button::button2:
484                         case mouse_button::button4:
485                         case mouse_button::button5:
486                                 // Nothing to do.
487                                 cur.noScreenUpdate();
488                                 break;
489                         }
490                 } else if (geometry(cur.bv()) != ButtonOnly)
491                         InsetText::doDispatch(cur, cmd);
492                 else
493                         cur.undispatched();
494                 break;
495
496         case LFUN_MOUSE_DOUBLE:
497         case LFUN_MOUSE_TRIPLE:
498                 if (hitButton)
499                         cur.noScreenUpdate();
500                 else if (geometry(cur.bv()) != ButtonOnly)
501                         InsetText::doDispatch(cur, cmd);
502                 else
503                         cur.undispatched();
504                 break;
505
506         case LFUN_MOUSE_RELEASE:
507                 if (!hitButton) {
508                         // The mouse click has to be within the inset!
509                         if (geometry(cur.bv()) != ButtonOnly)
510                                 InsetText::doDispatch(cur, cmd);
511                         else
512                                 cur.undispatched();
513                         break;
514                 }
515                 if (cmd.button() != mouse_button::button1) {
516                         // Nothing to do.
517                         cur.noScreenUpdate();
518                         break;
519                 }
520                 // if we are selecting, we do not want to
521                 // toggle the inset.
522                 if (cur.selection())
523                         break;
524                 // Left button is clicked, the user asks to
525                 // toggle the inset visual state.
526                 cur.dispatched();
527                 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
528                 if (geometry(cur.bv()) == ButtonOnly) {
529                         setStatus(cur, Open);
530                         edit(cur, true);
531                 }
532                 else
533                         setStatus(cur, Collapsed);
534                 cur.bv().cursor() = cur;
535                 break;
536
537         case LFUN_INSET_TOGGLE:
538                 if (cmd.argument() == "open")
539                         setStatus(cur, Open);
540                 else if (cmd.argument() == "close")
541                         setStatus(cur, Collapsed);
542                 else if (cmd.argument() == "toggle" || cmd.argument().empty())
543                         if (status_ == Open)
544                                 setStatus(cur, Collapsed);
545                         else
546                                 setStatus(cur, Open);
547                 else // if assign or anything else
548                         cur.undispatched();
549                 cur.dispatched();
550                 break;
551
552         default:
553                 InsetText::doDispatch(cur, cmd);
554                 break;
555         }
556 }
557
558
559 bool InsetCollapsible::getStatus(Cursor & cur, FuncRequest const & cmd,
560                 FuncStatus & flag) const
561 {
562         switch (cmd.action()) {
563         case LFUN_INSET_TOGGLE:
564                 if (cmd.argument() == "open")
565                         flag.setEnabled(status_ != Open);
566                 else if (cmd.argument() == "close")
567                         flag.setEnabled(status_ == Open);
568                 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
569                         flag.setEnabled(true);
570                         flag.setOnOff(status_ == Open);
571                 } else
572                         flag.setEnabled(false);
573                 return true;
574
575         default:
576                 return InsetText::getStatus(cur, cmd, flag);
577         }
578 }
579
580
581 void InsetCollapsible::setLabel(docstring const & l)
582 {
583         labelstring_ = l;
584 }
585
586
587 docstring InsetCollapsible::getLabel() const
588 {
589         InsetLayout const & il = getLayout();
590         return labelstring_.empty() ?
591                 translateIfPossible(il.labelstring()) : labelstring_;
592 }
593
594
595 docstring const InsetCollapsible::buttonLabel(BufferView const & bv) const
596 {
597         InsetLayout const & il = getLayout();
598         docstring const label = getLabel();
599         if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
600                 return label;
601         return getNewLabel(label);
602 }
603
604
605 void InsetCollapsible::setStatus(Cursor & cur, CollapseStatus status)
606 {
607         status_ = status;
608         setButtonLabel();
609         if (status_ == Collapsed)
610                 cur.leaveInset(*this);
611 }
612
613
614 InsetLayout::InsetDecoration InsetCollapsible::decoration() const
615 {
616         InsetLayout::InsetDecoration const dec = getLayout().decoration();
617         return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
618 }
619
620
621 string InsetCollapsible::contextMenu(BufferView const & bv, int x,
622         int y) const
623 {
624         string context_menu = contextMenuName();
625         string const it_context_menu = InsetText::contextMenuName();
626         if (decoration() == InsetLayout::CONGLOMERATE)
627                 return context_menu + ";" + it_context_menu;
628
629         string const ic_context_menu = InsetCollapsible::contextMenuName();
630         if (ic_context_menu != context_menu)
631                 context_menu += ";" + ic_context_menu;
632
633         if (geometry(bv) == NoButton)
634                 return context_menu + ";" + it_context_menu;
635
636         Dimension dim = dimensionCollapsed(bv);
637         if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
638                 return context_menu;
639
640         return it_context_menu;
641 }
642
643
644 string InsetCollapsible::contextMenuName() const
645 {
646         if (decoration() == InsetLayout::CONGLOMERATE)
647                 return "context-conglomerate";
648         else
649                 return "context-collapsible";
650 }
651
652
653 bool InsetCollapsible::canPaintChange(BufferView const & bv) const
654 {
655         // return false to let RowPainter draw the change tracking cue consistently
656         // with the surrounding text, when the inset is inline: for buttons, for
657         // non-allowMultiPar insets.
658         switch (geometry(bv)) {
659         case Corners:
660         case SubLabel:
661                 return allowMultiPar();
662         case ButtonOnly:
663                 return false;
664         default:
665                 break;
666         }
667         return true;
668 }
669
670
671 void InsetCollapsible::addToToc(DocIterator const & cpit, bool output_active,
672                                 UpdateType utype, TocBackend & backend) const
673 {
674         bool doing_output = output_active && producesOutput();
675         InsetLayout const & layout = getLayout();
676         if (layout.addToToc()) {
677                 TocBuilder & b = backend.builder(layout.tocType());
678                 // Cursor inside the inset
679                 DocIterator pit = cpit;
680                 pit.push_back(CursorSlice(const_cast<InsetCollapsible &>(*this)));
681                 docstring const label = getLabel();
682                 b.pushItem(pit, label + (label.empty() ? "" : ": "), output_active);
683                 // Proceed with the rest of the inset.
684                 InsetText::addToToc(cpit, doing_output, utype, backend);
685                 if (layout.isTocCaption()) {
686                         docstring str;
687                         text().forOutliner(str, TOC_ENTRY_LENGTH);
688                         b.argumentItem(str);
689                 }
690                 b.pop();
691         } else
692                 InsetText::addToToc(cpit, doing_output, utype, backend);
693 }
694
695
696
697 } // namespace lyx