]> git.lyx.org Git - features.git/blob - src/insets/InsetCollapsable.cpp
Fix drawing of collpsable insets
[features.git] / src / insets / InsetCollapsable.cpp
1 /**
2  * \file InsetCollapsable.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 "InsetCollapsable.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 InsetCollapsable::InsetCollapsable(Buffer * buf, InsetText::UsePlain ltype)
45         : InsetText(buf, ltype), status_(Open)
46 {
47         setDrawFrame(true);
48         setFrameColor(Color_collapsableframe);
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 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
55         : InsetText(rhs),
56           status_(rhs.status_),
57           labelstring_(rhs.labelstring_)
58 {}
59
60
61 InsetCollapsable::~InsetCollapsable()
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 InsetCollapsable::CollapseStatus InsetCollapsable::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 InsetCollapsable::Geometry InsetCollapsable::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 InsetCollapsable::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 InsetCollapsable::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 InsetCollapsable::read(Lexer & lex)
132 {
133         lex.setContext("InsetCollapsable::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 InsetCollapsable::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), dim.wid, dim.asc, dim.des);
152         return dim;
153 }
154
155
156 void InsetCollapsable::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 InsetCollapsable::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 InsetCollapsable::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_);
251                 // Draw the change tracking cue on the label, unless RowPainter already
252                 // takes care of it.
253                 if (canPaintChange(bv))
254                         pi.change_.paintCue(pi, x, y, x + dimc.width(), labelfont);
255         } else {
256                 view_[&bv].button_dim_.x1 = 0;
257                 view_[&bv].button_dim_.y1 = 0;
258                 view_[&bv].button_dim_.x2 = 0;
259                 view_[&bv].button_dim_.y2 = 0;
260         }
261
262         Dimension const textdim = dimensionHelper(bv);
263         int const baseline = y;
264         int textx, texty;
265         Geometry g = geometry(bv);
266         switch (g) {
267         case LeftButton:
268         case TopButton: {
269                 if (g == LeftButton) {
270                         textx = x + dimc.width();
271                         texty = baseline;
272                 } else {
273                         textx = x;
274                         texty = baseline + dimc.des + textdim.asc;
275                 }
276                 // Do not draw the cue for INSERTED -- it is already in the button and
277                 // that's enough.
278                 Changer dummy = (pi.change_.type == Change::INSERTED)
279                         ? make_change(pi.change_, Change())
280                         : Changer();
281                 InsetText::draw(pi, textx, texty);
282                 break;
283         }
284         case ButtonOnly:
285                 break;
286         case NoButton:
287                 textx = x;
288                 texty = baseline;
289                 InsetText::draw(pi, textx, texty);
290                 break;
291         case SubLabel:
292         case Corners:
293                 textx = x;
294                 texty = baseline;
295                 {       // We will take care of the frame and the change tracking cue
296                         // ourselves, below.
297                         Changer dummy = make_change(pi.change_, Change());
298                         const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
299                         InsetText::draw(pi, textx, texty);
300                         const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
301                 }
302
303                 int desc = textdim.descent();
304                 if (g == Corners)
305                         desc -= 3;
306
307                 // Colour the frame according to the change type. (Like for tables.)
308                 Color colour = pi.change_.changed() ? pi.change_.color()
309                                                     : Color_foreground;
310                 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
311                 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
312                 pi.pain.line(xx1, y + desc - 4,
313                              xx1, y + desc, colour);
314                 if (status_ == Open)
315                         pi.pain.line(xx1, y + desc,
316                                      xx2, y + desc, colour);
317                 else {
318                         // Make status_ value visible:
319                         pi.pain.line(xx1, y + desc,
320                                      xx1 + 4, y + desc, colour);
321                         pi.pain.line(xx2 - 4, y + desc,
322                                      xx2, y + desc, colour);
323                 }
324                 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
325                              y + desc - 4, colour);
326
327                 // the label below the text. Can be toggled.
328                 if (g == SubLabel) {
329                         FontInfo font(getLabelfont());
330                         if (pi.change_.changed())
331                                 font.setPaintColor(colour);
332                         font.realize(sane_font);
333                         font.decSize();
334                         font.decSize();
335                         int w = 0;
336                         int a = 0;
337                         int d = 0;
338                         theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
339                         int const ww = max(textdim.wid, w);
340                         pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
341                                          buttonLabel(bv), font, Color_none, Color_none);
342                 }
343
344                 int const y1 = y - textdim.asc + 3;
345                 // a visual cue when the cursor is inside the inset
346                 Cursor const & cur = bv.cursor();
347                 if (cur.isInside(this)) {
348                         pi.pain.line(xx1, y1 + 4, xx1, y1, colour);
349                         pi.pain.line(xx1 + 4, y1, xx1, y1, colour);
350                         pi.pain.line(xx2, y1 + 4, xx2, y1, colour);
351                         pi.pain.line(xx2 - 4, y1, xx2, y1, colour);
352                 }
353                 // Strike through the inset if deleted and not already handled by
354                 // RowPainter.
355                 if (pi.change_.deleted() && canPaintChange(bv))
356                         pi.change_.paintCue(pi, xx1, y1, xx2, y + desc);
357                 break;
358         }
359 }
360
361
362 void InsetCollapsable::cursorPos(BufferView const & bv,
363                 CursorSlice const & sl, bool boundary, int & x, int & y) const
364 {
365         if (geometry(bv) == ButtonOnly)
366                 status_ = Open;
367
368         InsetText::cursorPos(bv, sl, boundary, x, y);
369         Dimension const textdim = dimensionHelper(bv);
370
371         switch (geometry(bv)) {
372         case LeftButton:
373                 x += dimensionCollapsed(bv).wid;
374                 break;
375         case TopButton: {
376                 y += dimensionCollapsed(bv).des + textdim.asc;
377                 break;
378         }
379         case NoButton:
380         case SubLabel:
381         case Corners:
382                 // Do nothing
383                 break;
384         case ButtonOnly:
385                 // Cannot get here
386                 break;
387         }
388 }
389
390
391 bool InsetCollapsable::editable() const
392 {
393         switch (decoration()) {
394         case InsetLayout::CLASSIC:
395         case InsetLayout::MINIMALISTIC:
396                 return status_ == Open;
397         default:
398                 return true;
399         }
400 }
401
402
403 bool InsetCollapsable::descendable(BufferView const & bv) const
404 {
405         return geometry(bv) != ButtonOnly;
406 }
407
408
409 bool InsetCollapsable::clickable(BufferView const & bv, int x, int y) const
410 {
411         return view_[&bv].button_dim_.contains(x, y);
412 }
413
414
415 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
416 {
417         docstring label;
418         pos_type const max_length = 15;
419         pos_type const p_siz = paragraphs().begin()->size();
420         pos_type const n = min(max_length, p_siz);
421         pos_type i = 0;
422         pos_type j = 0;
423         for (; i < n && j < p_siz; ++j) {
424                 if (paragraphs().begin()->isInset(j))
425                         continue;
426                 label += paragraphs().begin()->getChar(j);
427                 ++i;
428         }
429         if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
430                 label += "...";
431         }
432         return label.empty() ? l : label;
433 }
434
435
436 void InsetCollapsable::edit(Cursor & cur, bool front, EntryDirection entry_from)
437 {
438         //lyxerr << "InsetCollapsable: edit left/right" << endl;
439         cur.push(*this);
440         InsetText::edit(cur, front, entry_from);
441 }
442
443
444 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
445 {
446         //lyxerr << "InsetCollapsable: edit xy" << endl;
447         if (geometry(cur.bv()) == ButtonOnly
448             || (view_[&cur.bv()].button_dim_.contains(x, y)
449                 && geometry(cur.bv()) != NoButton))
450                 return this;
451         cur.push(*this);
452         return InsetText::editXY(cur, x, y);
453 }
454
455
456 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
457 {
458         //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
459         //      << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
460
461         bool const hitButton = clickable(cur.bv(), cmd.x(), cmd.y());
462
463         switch (cmd.action()) {
464         case LFUN_MOUSE_PRESS:
465                 if (hitButton) {
466                         switch (cmd.button()) {
467                         case mouse_button::button1:
468                         case mouse_button::button3:
469                                 // Pass the command to the enclosing InsetText,
470                                 // so that the cursor gets set.
471                                 cur.undispatched();
472                                 break;
473                         case mouse_button::none:
474                         case mouse_button::button2:
475                         case mouse_button::button4:
476                         case mouse_button::button5:
477                                 // Nothing to do.
478                                 cur.noScreenUpdate();
479                                 break;
480                         }
481                 } else if (geometry(cur.bv()) != ButtonOnly)
482                         InsetText::doDispatch(cur, cmd);
483                 else
484                         cur.undispatched();
485                 break;
486
487         case LFUN_MOUSE_DOUBLE:
488         case LFUN_MOUSE_TRIPLE:
489                 if (hitButton)
490                         cur.noScreenUpdate();
491                 else if (geometry(cur.bv()) != ButtonOnly)
492                         InsetText::doDispatch(cur, cmd);
493                 else
494                         cur.undispatched();
495                 break;
496
497         case LFUN_MOUSE_RELEASE:
498                 if (!hitButton) {
499                         // The mouse click has to be within the inset!
500                         if (geometry(cur.bv()) != ButtonOnly)
501                                 InsetText::doDispatch(cur, cmd);
502                         else
503                                 cur.undispatched();
504                         break;
505                 }
506                 if (cmd.button() != mouse_button::button1) {
507                         // Nothing to do.
508                         cur.noScreenUpdate();
509                         break;
510                 }
511                 // if we are selecting, we do not want to
512                 // toggle the inset.
513                 if (cur.selection())
514                         break;
515                 // Left button is clicked, the user asks to
516                 // toggle the inset visual state.
517                 cur.dispatched();
518                 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
519                 if (geometry(cur.bv()) == ButtonOnly) {
520                         setStatus(cur, Open);
521                         edit(cur, true);
522                 }
523                 else
524                         setStatus(cur, Collapsed);
525                 cur.bv().cursor() = cur;
526                 break;
527
528         case LFUN_INSET_TOGGLE:
529                 if (cmd.argument() == "open")
530                         setStatus(cur, Open);
531                 else if (cmd.argument() == "close")
532                         setStatus(cur, Collapsed);
533                 else if (cmd.argument() == "toggle" || cmd.argument().empty())
534                         if (status_ == Open)
535                                 setStatus(cur, Collapsed);
536                         else
537                                 setStatus(cur, Open);
538                 else // if assign or anything else
539                         cur.undispatched();
540                 cur.dispatched();
541                 break;
542
543         default:
544                 InsetText::doDispatch(cur, cmd);
545                 break;
546         }
547 }
548
549
550 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
551                 FuncStatus & flag) const
552 {
553         switch (cmd.action()) {
554         case LFUN_INSET_TOGGLE:
555                 if (cmd.argument() == "open")
556                         flag.setEnabled(status_ != Open);
557                 else if (cmd.argument() == "close")
558                         flag.setEnabled(status_ == Open);
559                 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
560                         flag.setEnabled(true);
561                         flag.setOnOff(status_ == Open);
562                 } else
563                         flag.setEnabled(false);
564                 return true;
565
566         default:
567                 return InsetText::getStatus(cur, cmd, flag);
568         }
569 }
570
571
572 void InsetCollapsable::setLabel(docstring const & l)
573 {
574         labelstring_ = l;
575 }
576
577
578 docstring InsetCollapsable::getLabel() const
579 {
580         InsetLayout const & il = getLayout();
581         return labelstring_.empty() ?
582                 translateIfPossible(il.labelstring()) : labelstring_;
583 }
584
585
586 docstring const InsetCollapsable::buttonLabel(BufferView const & bv) const
587 {
588         InsetLayout const & il = getLayout();
589         docstring const label = getLabel();
590         if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
591                 return label;
592         return getNewLabel(label);
593 }
594
595
596 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
597 {
598         status_ = status;
599         setButtonLabel();
600         if (status_ == Collapsed)
601                 cur.leaveInset(*this);
602 }
603
604
605 InsetLayout::InsetDecoration InsetCollapsable::decoration() const
606 {
607         InsetLayout::InsetDecoration const dec = getLayout().decoration();
608         return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
609 }
610
611
612 string InsetCollapsable::contextMenu(BufferView const & bv, int x,
613         int y) const
614 {
615         string context_menu = contextMenuName();
616         string const it_context_menu = InsetText::contextMenuName();
617         if (decoration() == InsetLayout::CONGLOMERATE)
618                 return context_menu + ";" + it_context_menu;
619
620         string const ic_context_menu = InsetCollapsable::contextMenuName();
621         if (ic_context_menu != context_menu)
622                 context_menu += ";" + ic_context_menu;
623
624         if (geometry(bv) == NoButton)
625                 return context_menu + ";" + it_context_menu;
626
627         Dimension dim = dimensionCollapsed(bv);
628         if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
629                 return context_menu;
630
631         return it_context_menu;
632 }
633
634
635 string InsetCollapsable::contextMenuName() const
636 {
637         if (decoration() == InsetLayout::CONGLOMERATE)
638                 return "context-conglomerate";
639         else
640                 return "context-collapsable";
641 }
642
643
644 bool InsetCollapsable::canPaintChange(BufferView const & bv) const
645 {
646         // return false to let RowPainter draw the change tracking cue consistently
647         // with the surrounding text, when the inset is inline: for buttons, for
648         // non-allowMultiPar insets.
649         switch (geometry(bv)) {
650         case Corners:
651         case SubLabel:
652                 return allowMultiPar();
653         case ButtonOnly:
654                 return false;
655         default:
656                 break;
657         }
658         return true;
659 }
660
661
662 void InsetCollapsable::addToToc(DocIterator const & cpit, bool output_active,
663                                 UpdateType utype, TocBackend & backend) const
664 {
665         bool doing_output = output_active && producesOutput();
666         InsetLayout const & layout = getLayout();
667         if (layout.addToToc()) {
668                 TocBuilder & b = backend.builder(layout.tocType());
669                 // Cursor inside the inset
670                 DocIterator pit = cpit;
671                 pit.push_back(CursorSlice(const_cast<InsetCollapsable &>(*this)));
672                 docstring const label = getLabel();
673                 b.pushItem(pit, label + (label.empty() ? "" : ": "), output_active);
674                 // Proceed with the rest of the inset.
675                 InsetText::addToToc(cpit, doing_output, utype, backend);
676                 if (layout.isTocCaption()) {
677                         docstring str;
678                         text().forOutliner(str, TOC_ENTRY_LENGTH);
679                         b.argumentItem(str);
680                 }
681                 b.pop();
682         } else
683                 InsetText::addToToc(cpit, doing_output, utype, backend);
684 }
685
686
687
688 } // namespace lyx