]> git.lyx.org Git - features.git/blob - src/insets/InsetCollapsable.cpp
Fix bug #3900: Mathed corners displayed without mouse hover.
[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 "BufferParams.h"
19 #include "BufferView.h"
20 #include "Cursor.h"
21 #include "Dimension.h"
22 #include "FloatList.h"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
25 #include "InsetLayout.h"
26 #include "Lexer.h"
27 #include "MetricsInfo.h"
28 #include "OutputParams.h"
29
30 #include "frontends/FontMetrics.h"
31 #include "frontends/Painter.h"
32
33 #include "support/debug.h"
34 #include "support/docstream.h"
35 #include "support/gettext.h"
36 #include "support/lassert.h"
37 #include "support/lstrings.h"
38
39 using namespace std;
40
41
42 namespace lyx {
43
44 InsetCollapsable::InsetCollapsable(Buffer * buf, InsetText::UsePlain ltype)
45         : InsetText(buf, ltype), status_(Open), openinlined_(false)
46 {
47         setAutoBreakRows(true);
48         setDrawFrame(true);
49         setFrameColor(Color_collapsableframe);
50 }
51
52
53 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
54         : InsetText(rhs),
55           status_(rhs.status_),
56           labelstring_(rhs.labelstring_),
57           button_dim(rhs.button_dim),
58           openinlined_(rhs.openinlined_),
59           auto_open_(rhs.auto_open_),
60           // the sole purpose of this copy constructor
61           mouse_hover_()
62 {
63 }
64
65
66 InsetCollapsable::~InsetCollapsable()
67 {
68         map<BufferView const *, bool>::iterator it = mouse_hover_.begin();
69         map<BufferView const *, bool>::iterator end = mouse_hover_.end();
70         for (; it != end; ++it)
71                 if (it->second)
72                         it->first->clearLastInset(this);
73 }
74
75
76 InsetCollapsable::CollapseStatus InsetCollapsable::status(BufferView const & bv) const
77 {
78         if (decoration() == InsetLayout::CONGLOMERATE)
79                 return status_;
80         return auto_open_[&bv] ? Open : status_;
81 }
82
83
84 InsetCollapsable::Geometry InsetCollapsable::geometry(BufferView const & bv) const
85 {
86         switch (decoration()) {
87         case InsetLayout::CLASSIC:
88                 if (status(bv) == Open)
89                         return openinlined_ ? LeftButton : TopButton;
90                 return ButtonOnly;
91
92         case InsetLayout::MINIMALISTIC:
93                 return status(bv) == Open ? NoButton : ButtonOnly ;
94
95         case InsetLayout::CONGLOMERATE:
96                 return status(bv) == Open ? SubLabel : Corners ;
97
98         case InsetLayout::DEFAULT:
99                 break; // this shouldn't happen
100         }
101
102         // dummy return value to shut down a warning,
103         // this is dead code.
104         return NoButton;
105 }
106
107
108 InsetCollapsable::Geometry InsetCollapsable::geometry() const
109 {
110         switch (decoration()) {
111         case InsetLayout::CLASSIC:
112                 if (status_ == Open)
113                         return openinlined_ ? LeftButton : TopButton;
114                 return ButtonOnly;
115
116         case InsetLayout::MINIMALISTIC:
117                 return status_ == Open ? NoButton : ButtonOnly ;
118
119         case InsetLayout::CONGLOMERATE:
120                 return status_ == Open ? SubLabel : Corners ;
121
122         case InsetLayout::DEFAULT:
123                 break; // this shouldn't happen
124         }
125
126         // dummy return value to shut down a warning,
127         // this is dead code.
128         return NoButton;
129 }
130
131
132 docstring InsetCollapsable::toolTip(BufferView const & bv, int x, int y) const
133 {
134         Dimension dim = dimensionCollapsed(bv);
135         if (geometry(bv) == NoButton)
136                 return translateIfPossible(getLayout().labelstring());
137         if (x > xo(bv) + dim.wid || y > yo(bv) + dim.des || isOpen(bv))
138                 return docstring();
139
140         return toolTipText();
141 }
142
143
144 void InsetCollapsable::write(ostream & os) const
145 {
146         os << "status ";
147         switch (status_) {
148         case Open:
149                 os << "open";
150                 break;
151         case Collapsed:
152                 os << "collapsed";
153                 break;
154         }
155         os << "\n";
156         text().write(os);
157 }
158
159
160 void InsetCollapsable::read(Lexer & lex)
161 {
162         lex.setContext("InsetCollapsable::read");
163         string tmp_token;
164         status_ = Collapsed;
165         lex >> "status" >> tmp_token;
166         if (tmp_token == "open")
167                 status_ = Open;
168
169         InsetText::read(lex);
170         setButtonLabel();
171 }
172
173
174 Dimension InsetCollapsable::dimensionCollapsed(BufferView const & bv) const
175 {
176         Dimension dim;
177         theFontMetrics(getLayout().labelfont()).buttonText(
178                 buttonLabel(bv), dim.wid, dim.asc, dim.des);
179         return dim;
180 }
181
182
183 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
184 {
185         auto_open_[mi.base.bv] =  mi.base.bv->cursor().isInside(this);
186
187         FontInfo tmpfont = mi.base.font;
188         mi.base.font = getLayout().font();
189         mi.base.font.realize(tmpfont);
190
191         BufferView const & bv = *mi.base.bv;
192
193         switch (geometry(bv)) {
194         case NoButton:
195                 InsetText::metrics(mi, dim);
196                 break;
197         case Corners:
198                 InsetText::metrics(mi, dim);
199                 dim.des -= 3;
200                 dim.asc -= 1;
201                 break;
202         case SubLabel: {
203                 InsetText::metrics(mi, dim);
204                 // consider width of the inset label
205                 FontInfo font(getLayout().labelfont());
206                 font.realize(sane_font);
207                 font.decSize();
208                 font.decSize();
209                 int w = 0;
210                 int a = 0;
211                 int d = 0;
212                 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
213                 dim.des += a + d;
214                 break;
215                 }
216         case TopButton:
217         case LeftButton:
218         case ButtonOnly:
219                 dim = dimensionCollapsed(bv);
220                 if (geometry(bv) == TopButton 
221                           || geometry(bv) == LeftButton) {
222                         Dimension textdim;
223                         InsetText::metrics(mi, textdim);
224                         openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
225                         if (openinlined_) {
226                                 // Correct for button width.
227                                 dim.wid += textdim.wid;
228                                 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
229                                 dim.asc = textdim.asc;
230                         } else {
231                                 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
232                                 dim.wid = max(dim.wid, textdim.wid);
233                         }
234                 }
235                 break;
236         }
237
238         mi.base.font = tmpfont;
239 }
240
241
242 bool InsetCollapsable::setMouseHover(BufferView const * bv, bool mouse_hover)
243 {
244         mouse_hover_[bv] = mouse_hover;
245         return true;
246 }
247
248
249 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
250 {
251         BufferView const & bv = *pi.base.bv;
252
253         auto_open_[&bv] =  bv.cursor().isInside(this);
254
255         FontInfo tmpfont = pi.base.font;
256         pi.base.font = getLayout().font();
257         pi.base.font.realize(tmpfont);
258
259         // Draw button first -- top, left or only
260         Dimension dimc = dimensionCollapsed(bv);
261
262         if (geometry(bv) == TopButton ||
263             geometry(bv) == LeftButton ||
264             geometry(bv) == ButtonOnly) {
265                 button_dim.x1 = x + 0;
266                 button_dim.x2 = x + dimc.width();
267                 button_dim.y1 = y - dimc.asc;
268                 button_dim.y2 = y + dimc.des;
269
270                 FontInfo labelfont = getLayout().labelfont();
271                 labelfont.setColor(labelColor());
272                 pi.pain.buttonText(x, y, buttonLabel(bv), labelfont,
273                         mouse_hover_[&bv]);
274         } else {
275                 button_dim.x1 = 0;
276                 button_dim.y1 = 0;
277                 button_dim.x2 = 0;
278                 button_dim.y2 = 0;
279         }
280
281         Dimension const textdim = InsetText::dimension(bv);
282         int const baseline = y;
283         int textx, texty;
284         switch (geometry(bv)) {
285         case LeftButton:
286                 textx = x + dimc.width();
287                 texty = baseline;
288                 InsetText::draw(pi, textx, texty);
289                 break;
290         case TopButton:
291                 textx = x;
292                 texty = baseline + dimc.des + textdim.asc;
293                 InsetText::draw(pi, textx, texty);
294                 break;
295         case ButtonOnly:
296                 break;
297         case NoButton:
298                 textx = x;
299                 texty = baseline;
300                 InsetText::draw(pi, textx, texty);
301                 break;
302         case SubLabel:
303         case Corners:
304                 textx = x;
305                 texty = baseline;
306                 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
307                 InsetText::draw(pi, textx, texty);
308                 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
309
310                 int desc = textdim.descent();
311                 if (geometry(bv) == Corners)
312                         desc -= 3;
313
314                 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
315                 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
316                 pi.pain.line(xx1, y + desc - 4, 
317                              xx1, y + desc, 
318                         labelColor());
319                 if (status_ == Open)
320                         pi.pain.line(xx1, y + desc, 
321                                 xx2, y + desc,
322                                 labelColor());
323                 else {
324                         // Make status_ value visible:
325                         pi.pain.line(xx1, y + desc,
326                                 xx1 + 4, y + desc,
327                                 labelColor());
328                         pi.pain.line(xx2 - 4, y + desc,
329                                 xx2, y + desc,
330                                 labelColor());
331                 }
332                 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3, 
333                         y + desc - 4, labelColor());
334
335                 // the label below the text. Can be toggled.
336                 if (geometry(bv) == SubLabel) {
337                         FontInfo font(getLayout().labelfont());
338                         font.realize(sane_font);
339                         font.decSize();
340                         font.decSize();
341                         int w = 0;
342                         int a = 0;
343                         int d = 0;
344                         theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
345                         int const ww = max(textdim.wid, w);
346                         pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
347                                 buttonLabel(bv), font, Color_none, Color_none);
348                         desc += d;
349                 }
350
351                 // a visual cue when the cursor is inside the inset
352                 Cursor const & cur = bv.cursor();
353                 if (cur.isInside(this)) {
354                         y -= textdim.asc;
355                         y += 3;
356                         pi.pain.line(xx1, y + 4, xx1, y, labelColor());
357                         pi.pain.line(xx1 + 4, y, xx1, y, labelColor());
358                         pi.pain.line(xx2, y + 4, xx2, y,
359                                 labelColor());
360                         pi.pain.line(xx2 - 4, y, xx2, y,
361                                 labelColor());
362                 }
363                 break;
364         }
365
366         pi.base.font = tmpfont;
367 }
368
369
370 void InsetCollapsable::cursorPos(BufferView const & bv,
371                 CursorSlice const & sl, bool boundary, int & x, int & y) const
372 {
373         if (geometry(bv) == ButtonOnly)
374                 status_ = Open;
375
376         InsetText::cursorPos(bv, sl, boundary, x, y);
377         Dimension const textdim = InsetText::dimension(bv);
378
379         switch (geometry(bv)) {
380         case LeftButton:
381                 x += dimensionCollapsed(bv).wid;
382                 break;
383         case TopButton: {
384                 y += dimensionCollapsed(bv).des + textdim.asc;
385                 break;
386         }
387         case NoButton:
388         case SubLabel:
389         case Corners:
390                 // Do nothing
391                 break;
392         case ButtonOnly:
393                 // Cannot get here
394                 break;
395         }
396 }
397
398
399 bool InsetCollapsable::editable() const
400 {
401         return geometry() != ButtonOnly;
402 }
403
404
405 bool InsetCollapsable::descendable(BufferView const & bv) const
406 {
407         return geometry(bv) != ButtonOnly;
408 }
409
410
411 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
412 {
413         return button_dim.contains(cmd.x(), cmd.y());
414 }
415
416
417 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
418 {
419         docstring label;
420         pos_type const max_length = 15;
421         pos_type const p_siz = paragraphs().begin()->size();
422         pos_type const n = min(max_length, p_siz);
423         pos_type i = 0;
424         pos_type j = 0;
425         for (; i < n && j < p_siz; ++j) {
426                 if (paragraphs().begin()->isInset(j))
427                         continue;
428                 label += paragraphs().begin()->getChar(j);
429                 ++i;
430         }
431         if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
432                 label += "...";
433         }
434         return label.empty() ? l : label;
435 }
436
437
438 void InsetCollapsable::edit(Cursor & cur, bool front, EntryDirection entry_from)
439 {
440         //lyxerr << "InsetCollapsable: edit left/right" << endl;
441         cur.push(*this);
442         InsetText::edit(cur, front, entry_from);
443 }
444
445
446 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
447 {
448         //lyxerr << "InsetCollapsable: edit xy" << endl;
449         if (geometry(cur.bv()) == ButtonOnly
450          || (button_dim.contains(x, y) 
451           && geometry(cur.bv()) != NoButton))
452                 return this;
453         cur.push(*this);
454         return InsetText::editXY(cur, x, y);
455 }
456
457
458 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
459 {
460         //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
461         //      << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
462
463         switch (cmd.action()) {
464         case LFUN_MOUSE_PRESS:
465                 if (hitButton(cmd)) {
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.noUpdate();
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(cmd)) 
491                         cur.noUpdate();
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(cmd)) {
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.noUpdate();
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.updateFlags(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                                 if (geometry(cur.bv()) == ButtonOnly)
538                                         cur.top().forwardPos();
539                         } else
540                                 setStatus(cur, Open);
541                 else // if assign or anything else
542                         cur.undispatched();
543                 cur.dispatched();
544                 break;
545
546         default:
547                 InsetText::doDispatch(cur, cmd);
548                 break;
549         }
550 }
551
552
553 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
554                 FuncStatus & flag) const
555 {
556         switch (cmd.action()) {
557         case LFUN_INSET_TOGGLE:
558                 if (cmd.argument() == "open")
559                         flag.setEnabled(status_ != Open);
560                 else if (cmd.argument() == "close")
561                         flag.setEnabled(status_ == Open);
562                 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
563                         flag.setEnabled(true);
564                         flag.setOnOff(status_ == Open);
565                 } else
566                         flag.setEnabled(false);
567                 return true;
568
569         default:
570                 return InsetText::getStatus(cur, cmd, flag);
571         }
572 }
573
574
575 void InsetCollapsable::setLabel(docstring const & l)
576 {
577         labelstring_ = l;
578 }
579
580
581 docstring const InsetCollapsable::buttonLabel(BufferView const & bv) const
582 {
583         docstring const label = labelstring_.empty() ? 
584                 translateIfPossible(getLayout().labelstring()) : labelstring_;
585         InsetLayout const & il = getLayout();
586         if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
587                 return label;
588         return getNewLabel(label);
589 }
590
591
592 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
593 {
594         status_ = status;
595         setButtonLabel();
596         if (status_ == Collapsed) {
597                 cur.leaveInset(*this);
598                 mouse_hover_.clear();
599         }
600 }
601
602
603 docstring InsetCollapsable::floatName(string const & type) const
604 {
605         BufferParams const & bp = buffer().params();
606         FloatList const & floats = bp.documentClass().floats();
607         FloatList::const_iterator it = floats[type];
608         // FIXME UNICODE
609         return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
610 }
611
612
613 InsetLayout::InsetDecoration InsetCollapsable::decoration() const
614 {
615         InsetLayout::InsetDecoration const dec = getLayout().decoration();
616         return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
617 }
618
619
620 docstring InsetCollapsable::contextMenu(BufferView const & bv, int x,
621         int y) const
622 {
623         if (decoration() == InsetLayout::CONGLOMERATE)
624                 return from_ascii("context-conglomerate");
625
626         if (geometry(bv) == NoButton)
627                 return from_ascii("context-collapsable");
628
629         Dimension dim = dimensionCollapsed(bv);
630         if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
631                 return from_ascii("context-collapsable");
632
633         return InsetText::contextMenu(bv, x, y);
634 }
635
636 } // namespace lyx