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