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