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