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