]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCollapsable.cpp
ccb26a5dff8c25f4913f8866ac335c03a75b9e22
[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 {
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 = getLayout().font();
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 = getLayout().labelfont();
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(getLayout().labelfont());
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 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
414 {
415         docstring label;
416         pos_type const max_length = 15;
417         pos_type const p_siz = paragraphs().begin()->size();
418         pos_type const n = min(max_length, p_siz);
419         pos_type i = 0;
420         pos_type j = 0;
421         for (; i < n && j < p_siz; ++j) {
422                 if (paragraphs().begin()->isInset(j))
423                         continue;
424                 label += paragraphs().begin()->getChar(j);
425                 ++i;
426         }
427         if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
428                 label += "...";
429         }
430         return label.empty() ? l : label;
431 }
432
433
434 void InsetCollapsable::edit(Cursor & cur, bool front, EntryDirection entry_from)
435 {
436         //lyxerr << "InsetCollapsable: edit left/right" << endl;
437         cur.push(*this);
438         InsetText::edit(cur, front, entry_from);
439 }
440
441
442 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
443 {
444         //lyxerr << "InsetCollapsable: edit xy" << endl;
445         if (geometry(cur.bv()) == ButtonOnly
446          || (button_dim.contains(x, y) 
447           && geometry(cur.bv()) != NoButton))
448                 return this;
449         cur.push(*this);
450         return InsetText::editXY(cur, x, y);
451 }
452
453
454 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
455 {
456         //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
457         //      << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
458
459         switch (cmd.action()) {
460         case LFUN_MOUSE_PRESS:
461                 if (hitButton(cmd)) {
462                         switch (cmd.button()) {
463                         case mouse_button::button1:
464                         case mouse_button::button3:
465                                 // Pass the command to the enclosing InsetText,
466                                 // so that the cursor gets set.
467                                 cur.undispatched();
468                                 break;
469                         case mouse_button::none:
470                         case mouse_button::button2:
471                         case mouse_button::button4:
472                         case mouse_button::button5:
473                                 // Nothing to do.
474                                 cur.noScreenUpdate();
475                                 break;
476                         }
477                 } else if (geometry(cur.bv()) != ButtonOnly)
478                         InsetText::doDispatch(cur, cmd);
479                 else
480                         cur.undispatched();
481                 break;
482
483         case LFUN_MOUSE_MOTION:
484         case LFUN_MOUSE_DOUBLE:
485         case LFUN_MOUSE_TRIPLE:
486                 if (hitButton(cmd)) 
487                         cur.noScreenUpdate();
488                 else if (geometry(cur.bv()) != ButtonOnly)
489                         InsetText::doDispatch(cur, cmd);
490                 else
491                         cur.undispatched();
492                 break;
493
494         case LFUN_MOUSE_RELEASE:
495                 if (!hitButton(cmd)) {
496                         // The mouse click has to be within the inset!
497                         if (geometry(cur.bv()) != ButtonOnly)
498                                 InsetText::doDispatch(cur, cmd);
499                         else
500                                 cur.undispatched();                     
501                         break;
502                 }
503                 if (cmd.button() != mouse_button::button1) {
504                         // Nothing to do.
505                         cur.noScreenUpdate();
506                         break;
507                 }
508                 // if we are selecting, we do not want to
509                 // toggle the inset.
510                 if (cur.selection())
511                         break;
512                 // Left button is clicked, the user asks to
513                 // toggle the inset visual state.
514                 cur.dispatched();
515                 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
516                 if (geometry(cur.bv()) == ButtonOnly) {
517                         setStatus(cur, Open);
518                         edit(cur, true);
519                 }
520                 else
521                         setStatus(cur, Collapsed);
522                 cur.bv().cursor() = cur;
523                 break;
524
525         case LFUN_INSET_TOGGLE:
526                 if (cmd.argument() == "open")
527                         setStatus(cur, Open);
528                 else if (cmd.argument() == "close")
529                         setStatus(cur, Collapsed);
530                 else if (cmd.argument() == "toggle" || cmd.argument().empty())
531                         if (status_ == Open) {
532                                 setStatus(cur, Collapsed);
533                                 if (geometry(cur.bv()) == ButtonOnly)
534                                         cur.top().forwardPos();
535                         } else
536                                 setStatus(cur, Open);
537                 else // if assign or anything else
538                         cur.undispatched();
539                 cur.dispatched();
540                 break;
541
542         default:
543                 InsetText::doDispatch(cur, cmd);
544                 break;
545         }
546 }
547
548
549 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
550                 FuncStatus & flag) const
551 {
552         switch (cmd.action()) {
553         case LFUN_INSET_TOGGLE:
554                 if (cmd.argument() == "open")
555                         flag.setEnabled(status_ != Open);
556                 else if (cmd.argument() == "close")
557                         flag.setEnabled(status_ == Open);
558                 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
559                         flag.setEnabled(true);
560                         flag.setOnOff(status_ == Open);
561                 } else
562                         flag.setEnabled(false);
563                 return true;
564
565         default:
566                 return InsetText::getStatus(cur, cmd, flag);
567         }
568 }
569
570
571 void InsetCollapsable::setLabel(docstring const & l)
572 {
573         labelstring_ = l;
574 }
575
576
577 docstring const InsetCollapsable::buttonLabel(BufferView const & bv) const
578 {
579         docstring const label = labelstring_.empty() ? 
580                 translateIfPossible(getLayout().labelstring()) : labelstring_;
581         InsetLayout const & il = getLayout();
582         if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
583                 return label;
584         return getNewLabel(label);
585 }
586
587
588 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
589 {
590         status_ = status;
591         setButtonLabel();
592         if (status_ == Collapsed)
593                 cur.leaveInset(*this);
594 }
595
596
597 docstring InsetCollapsable::floatName(string const & type) const
598 {
599         BufferParams const & bp = buffer().params();
600         FloatList const & floats = bp.documentClass().floats();
601         FloatList::const_iterator it = floats[type];
602         // FIXME UNICODE
603         return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
604 }
605
606
607 InsetLayout::InsetDecoration InsetCollapsable::decoration() const
608 {
609         InsetLayout::InsetDecoration const dec = getLayout().decoration();
610         return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
611 }
612
613
614 docstring InsetCollapsable::contextMenu(BufferView const & bv, int x,
615         int y) const
616 {
617         if (decoration() == InsetLayout::CONGLOMERATE)
618                 return from_ascii("context-conglomerate");
619
620         if (geometry(bv) == NoButton)
621                 return from_ascii("context-collapsable");
622
623         Dimension dim = dimensionCollapsed(bv);
624         if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
625                 return from_ascii("context-collapsable");
626
627         return InsetText::contextMenu(bv, x, y);
628 }
629
630 } // namespace lyx