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