]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCollapsable.cpp
Enable InsetQuote in verbatim and Hebrew
[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 = dimensionHelper(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 = (pi.change_.type == Change::INSERTED)
278                         ? make_change(pi.change_, Change())
279                         : Changer();
280                 InsetText::draw(pi, textx, texty);
281                 break;
282         }
283         case ButtonOnly:
284                 break;
285         case NoButton:
286                 textx = x;
287                 texty = baseline;
288                 InsetText::draw(pi, textx, texty);
289                 break;
290         case SubLabel:
291         case Corners:
292                 textx = x;
293                 texty = baseline;
294                 {       // We will take care of the frame and the change tracking cue
295                         // ourselves, below.
296                         Changer dummy = make_change(pi.change_, Change());
297                         const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
298                         InsetText::draw(pi, textx, texty);
299                         const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
300                 }
301
302                 int desc = textdim.descent();
303                 if (g == Corners)
304                         desc -= 3;
305
306                 // Colour the frame according to the change type. (Like for tables.)
307                 Color colour = pi.change_.changed() ? pi.change_.color()
308                                                     : Color_foreground;
309                 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
310                 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
311                 pi.pain.line(xx1, y + desc - 4,
312                              xx1, y + desc, colour);
313                 if (status_ == Open)
314                         pi.pain.line(xx1, y + desc,
315                                      xx2, y + desc, colour);
316                 else {
317                         // Make status_ value visible:
318                         pi.pain.line(xx1, y + desc,
319                                      xx1 + 4, y + desc, colour);
320                         pi.pain.line(xx2 - 4, y + desc,
321                                      xx2, y + desc, colour);
322                 }
323                 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
324                              y + desc - 4, colour);
325
326                 // the label below the text. Can be toggled.
327                 if (g == SubLabel) {
328                         FontInfo font(getLabelfont());
329                         if (pi.change_.changed())
330                                 font.setPaintColor(colour);
331                         font.realize(sane_font);
332                         font.decSize();
333                         font.decSize();
334                         int w = 0;
335                         int a = 0;
336                         int d = 0;
337                         Color const col = pi.full_repaint ? Color_none : pi.backgroundColor(this);
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, col, 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_MOTION:
488         case LFUN_MOUSE_DOUBLE:
489         case LFUN_MOUSE_TRIPLE:
490                 if (hitButton)
491                         cur.noScreenUpdate();
492                 else if (geometry(cur.bv()) != ButtonOnly)
493                         InsetText::doDispatch(cur, cmd);
494                 else
495                         cur.undispatched();
496                 break;
497
498         case LFUN_MOUSE_RELEASE:
499                 if (!hitButton) {
500                         // The mouse click has to be within the inset!
501                         if (geometry(cur.bv()) != ButtonOnly)
502                                 InsetText::doDispatch(cur, cmd);
503                         else
504                                 cur.undispatched();
505                         break;
506                 }
507                 if (cmd.button() != mouse_button::button1) {
508                         // Nothing to do.
509                         cur.noScreenUpdate();
510                         break;
511                 }
512                 // if we are selecting, we do not want to
513                 // toggle the inset.
514                 if (cur.selection())
515                         break;
516                 // Left button is clicked, the user asks to
517                 // toggle the inset visual state.
518                 cur.dispatched();
519                 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
520                 if (geometry(cur.bv()) == ButtonOnly) {
521                         setStatus(cur, Open);
522                         edit(cur, true);
523                 }
524                 else
525                         setStatus(cur, Collapsed);
526                 cur.bv().cursor() = cur;
527                 break;
528
529         case LFUN_INSET_TOGGLE:
530                 if (cmd.argument() == "open")
531                         setStatus(cur, Open);
532                 else if (cmd.argument() == "close")
533                         setStatus(cur, Collapsed);
534                 else if (cmd.argument() == "toggle" || cmd.argument().empty())
535                         if (status_ == Open)
536                                 setStatus(cur, Collapsed);
537                         else
538                                 setStatus(cur, Open);
539                 else // if assign or anything else
540                         cur.undispatched();
541                 cur.dispatched();
542                 break;
543
544         default:
545                 InsetText::doDispatch(cur, cmd);
546                 break;
547         }
548 }
549
550
551 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
552                 FuncStatus & flag) const
553 {
554         switch (cmd.action()) {
555         case LFUN_INSET_TOGGLE:
556                 if (cmd.argument() == "open")
557                         flag.setEnabled(status_ != Open);
558                 else if (cmd.argument() == "close")
559                         flag.setEnabled(status_ == Open);
560                 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
561                         flag.setEnabled(true);
562                         flag.setOnOff(status_ == Open);
563                 } else
564                         flag.setEnabled(false);
565                 return true;
566
567         default:
568                 return InsetText::getStatus(cur, cmd, flag);
569         }
570 }
571
572
573 void InsetCollapsable::setLabel(docstring const & l)
574 {
575         labelstring_ = l;
576 }
577
578
579 docstring const InsetCollapsable::buttonLabel(BufferView const & bv) const
580 {
581         InsetLayout const & il = getLayout();
582         docstring const label = labelstring_.empty() ?
583                 translateIfPossible(il.labelstring()) : labelstring_;
584         if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
585                 return label;
586         return getNewLabel(label);
587 }
588
589
590 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
591 {
592         status_ = status;
593         setButtonLabel();
594         if (status_ == Collapsed)
595                 cur.leaveInset(*this);
596 }
597
598
599 InsetLayout::InsetDecoration InsetCollapsable::decoration() const
600 {
601         InsetLayout::InsetDecoration const dec = getLayout().decoration();
602         return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
603 }
604
605
606 string InsetCollapsable::contextMenu(BufferView const & bv, int x,
607         int y) const
608 {
609         string context_menu = contextMenuName();
610         string const it_context_menu = InsetText::contextMenuName();
611         if (decoration() == InsetLayout::CONGLOMERATE)
612                 return context_menu + ";" + it_context_menu;
613
614         string const ic_context_menu = InsetCollapsable::contextMenuName();
615         if (ic_context_menu != context_menu)
616                 context_menu += ";" + ic_context_menu;
617
618         if (geometry(bv) == NoButton)
619                 return context_menu + ";" + it_context_menu;
620
621         Dimension dim = dimensionCollapsed(bv);
622         if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
623                 return context_menu;
624
625         return it_context_menu;
626 }
627
628
629 string InsetCollapsable::contextMenuName() const
630 {
631         if (decoration() == InsetLayout::CONGLOMERATE)
632                 return "context-conglomerate";
633         else
634                 return "context-collapsable";
635 }
636
637
638 bool InsetCollapsable::canPaintChange(BufferView const & bv) const
639 {
640         // return false to let RowPainter draw the change tracking cue consistently
641         // with the surrounding text, when the inset is inline: for buttons, for
642         // non-allowMultiPar insets.
643         switch (geometry(bv)) {
644         case Corners:
645         case SubLabel:
646                 return allowMultiPar();
647         case ButtonOnly:
648                 return false;
649         default:
650                 break;
651         }
652         return true;
653 }
654
655
656 } // namespace lyx