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