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