]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCollapsable.cpp
6e5051ef5fd90b141e9d618258fa46fd34f79dad
[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 #include "TextClass.h"
30
31 #include "frontends/FontMetrics.h"
32 #include "frontends/Painter.h"
33
34 #include "support/debug.h"
35 #include "support/docstream.h"
36 #include "support/gettext.h"
37 #include "support/lassert.h"
38 #include "support/lstrings.h"
39
40 using namespace std;
41
42
43 namespace lyx {
44
45 InsetCollapsable::InsetCollapsable(Buffer * buf, InsetText::UsePlain ltype)
46         : InsetText(buf, ltype), status_(Open), openinlined_(false)
47 {
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 const 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         FontInfo labelfont(getLabelfont());
176         labelfont.realize(sane_font);
177         theFontMetrics(labelfont).buttonText(
178                 buttonLabel(bv), dim.wid, dim.asc, dim.des);
179         return dim;
180 }
181
182
183 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
184 {
185         auto_open_[mi.base.bv] = mi.base.bv->cursor().isInside(this);
186
187         FontInfo tmpfont = mi.base.font;
188         mi.base.font = getFont();
189         mi.base.font.realize(tmpfont);
190
191         BufferView const & bv = *mi.base.bv;
192
193         switch (geometry(bv)) {
194         case NoButton:
195                 InsetText::metrics(mi, dim);
196                 break;
197         case Corners:
198                 InsetText::metrics(mi, dim);
199                 dim.des -= 3;
200                 dim.asc -= 1;
201                 break;
202         case SubLabel: {
203                 InsetText::metrics(mi, dim);
204                 // consider width of the inset label
205                 FontInfo font(getLabelfont());
206                 font.realize(sane_font);
207                 font.decSize();
208                 font.decSize();
209                 int w = 0;
210                 int a = 0;
211                 int d = 0;
212                 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
213                 dim.des += a + d;
214                 break;
215                 }
216         case TopButton:
217         case LeftButton:
218         case ButtonOnly:
219                 dim = dimensionCollapsed(bv);
220                 if (geometry(bv) == TopButton 
221                           || geometry(bv) == LeftButton) {
222                         Dimension textdim;
223                         InsetText::metrics(mi, textdim);
224                         openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
225                         if (openinlined_) {
226                                 // Correct for button width.
227                                 dim.wid += textdim.wid;
228                                 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
229                                 dim.asc = textdim.asc;
230                         } else {
231                                 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
232                                 dim.wid = max(dim.wid, textdim.wid);
233                         }
234                 }
235                 break;
236         }
237
238         mi.base.font = tmpfont;
239 }
240
241
242 bool InsetCollapsable::setMouseHover(BufferView const * bv, bool mouse_hover)
243         const
244 {
245         mouse_hover_[bv] = mouse_hover;
246         return true;
247 }
248
249
250 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
251 {
252         BufferView const & bv = *pi.base.bv;
253
254         auto_open_[&bv] = bv.cursor().isInside(this);
255
256         FontInfo tmpfont = pi.base.font;
257         pi.base.font = getFont();
258         pi.base.font.realize(tmpfont);
259
260         // Draw button first -- top, left or only
261         Dimension dimc = dimensionCollapsed(bv);
262
263         if (geometry(bv) == TopButton ||
264             geometry(bv) == LeftButton ||
265             geometry(bv) == ButtonOnly) {
266                 button_dim.x1 = x + 0;
267                 button_dim.x2 = x + dimc.width();
268                 button_dim.y1 = y - dimc.asc;
269                 button_dim.y2 = y + dimc.des;
270
271                 FontInfo labelfont = getLabelfont();
272                 labelfont.setColor(labelColor());
273                 pi.pain.buttonText(x, y, buttonLabel(bv), labelfont,
274                         mouse_hover_[&bv]);
275         } else {
276                 button_dim.x1 = 0;
277                 button_dim.y1 = 0;
278                 button_dim.x2 = 0;
279                 button_dim.y2 = 0;
280         }
281
282         Dimension const textdim = InsetText::dimension(bv);
283         int const baseline = y;
284         int textx, texty;
285         switch (geometry(bv)) {
286         case LeftButton:
287                 textx = x + dimc.width();
288                 texty = baseline;
289                 InsetText::draw(pi, textx, texty);
290                 break;
291         case TopButton:
292                 textx = x;
293                 texty = baseline + dimc.des + textdim.asc;
294                 InsetText::draw(pi, textx, texty);
295                 break;
296         case ButtonOnly:
297                 break;
298         case NoButton:
299                 textx = x;
300                 texty = baseline;
301                 InsetText::draw(pi, textx, texty);
302                 break;
303         case SubLabel:
304         case Corners:
305                 textx = x;
306                 texty = baseline;
307                 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
308                 InsetText::draw(pi, textx, texty);
309                 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
310
311                 int desc = textdim.descent();
312                 if (geometry(bv) == Corners)
313                         desc -= 3;
314
315                 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
316                 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
317                 pi.pain.line(xx1, y + desc - 4, 
318                              xx1, y + desc, 
319                         Color_foreground);
320                 if (status_ == Open)
321                         pi.pain.line(xx1, y + desc, 
322                                 xx2, y + desc,
323                                 Color_foreground);
324                 else {
325                         // Make status_ value visible:
326                         pi.pain.line(xx1, y + desc,
327                                 xx1 + 4, y + desc,
328                                 Color_foreground);
329                         pi.pain.line(xx2 - 4, y + desc,
330                                 xx2, y + desc,
331                                 Color_foreground);
332                 }
333                 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3, 
334                         y + desc - 4, Color_foreground);
335
336                 // the label below the text. Can be toggled.
337                 if (geometry(bv) == SubLabel) {
338                         FontInfo font(getLabelfont());
339                         font.realize(sane_font);
340                         font.decSize();
341                         font.decSize();
342                         int w = 0;
343                         int a = 0;
344                         int d = 0;
345                         theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
346                         int const ww = max(textdim.wid, w);
347                         pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
348                                 buttonLabel(bv), font, Color_none, Color_none);
349                         desc += d;
350                 }
351
352                 // a visual cue when the cursor is inside the inset
353                 Cursor const & cur = bv.cursor();
354                 if (cur.isInside(this)) {
355                         y -= textdim.asc;
356                         y += 3;
357                         pi.pain.line(xx1, y + 4, xx1, y, Color_foreground);
358                         pi.pain.line(xx1 + 4, y, xx1, y, Color_foreground);
359                         pi.pain.line(xx2, y + 4, xx2, y, Color_foreground);
360                         pi.pain.line(xx2 - 4, y, xx2, y, Color_foreground);
361                 }
362                 break;
363         }
364
365         pi.base.font = tmpfont;
366 }
367
368
369 void InsetCollapsable::cursorPos(BufferView const & bv,
370                 CursorSlice const & sl, bool boundary, int & x, int & y) const
371 {
372         if (geometry(bv) == ButtonOnly)
373                 status_ = Open;
374
375         InsetText::cursorPos(bv, sl, boundary, x, y);
376         Dimension const textdim = InsetText::dimension(bv);
377
378         switch (geometry(bv)) {
379         case LeftButton:
380                 x += dimensionCollapsed(bv).wid;
381                 break;
382         case TopButton: {
383                 y += dimensionCollapsed(bv).des + textdim.asc;
384                 break;
385         }
386         case NoButton:
387         case SubLabel:
388         case Corners:
389                 // Do nothing
390                 break;
391         case ButtonOnly:
392                 // Cannot get here
393                 break;
394         }
395 }
396
397
398 bool InsetCollapsable::editable() const
399 {
400         return geometry() != ButtonOnly;
401 }
402
403
404 bool InsetCollapsable::descendable(BufferView const & bv) const
405 {
406         return geometry(bv) != ButtonOnly;
407 }
408
409
410 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
411 {
412         return button_dim.contains(cmd.x(), cmd.y());
413 }
414
415
416 bool InsetCollapsable::clickable(int x, int y) const
417 {
418         FuncRequest cmd(LFUN_NOACTION, x, y, mouse_button::none);
419         return hitButton(cmd);
420 }
421
422
423 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
424 {
425         docstring label;
426         pos_type const max_length = 15;
427         pos_type const p_siz = paragraphs().begin()->size();
428         pos_type const n = min(max_length, p_siz);
429         pos_type i = 0;
430         pos_type j = 0;
431         for (; i < n && j < p_siz; ++j) {
432                 if (paragraphs().begin()->isInset(j))
433                         continue;
434                 label += paragraphs().begin()->getChar(j);
435                 ++i;
436         }
437         if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
438                 label += "...";
439         }
440         return label.empty() ? l : label;
441 }
442
443
444 void InsetCollapsable::edit(Cursor & cur, bool front, EntryDirection entry_from)
445 {
446         //lyxerr << "InsetCollapsable: edit left/right" << endl;
447         cur.push(*this);
448         InsetText::edit(cur, front, entry_from);
449 }
450
451
452 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
453 {
454         //lyxerr << "InsetCollapsable: edit xy" << endl;
455         if (geometry(cur.bv()) == ButtonOnly
456          || (button_dim.contains(x, y) 
457           && geometry(cur.bv()) != NoButton))
458                 return this;
459         cur.push(*this);
460         return InsetText::editXY(cur, x, y);
461 }
462
463
464 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
465 {
466         //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
467         //      << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
468
469         switch (cmd.action()) {
470         case LFUN_MOUSE_PRESS:
471                 if (hitButton(cmd)) {
472                         switch (cmd.button()) {
473                         case mouse_button::button1:
474                         case mouse_button::button3:
475                                 // Pass the command to the enclosing InsetText,
476                                 // so that the cursor gets set.
477                                 cur.undispatched();
478                                 break;
479                         case mouse_button::none:
480                         case mouse_button::button2:
481                         case mouse_button::button4:
482                         case mouse_button::button5:
483                                 // Nothing to do.
484                                 cur.noScreenUpdate();
485                                 break;
486                         }
487                 } else if (geometry(cur.bv()) != ButtonOnly)
488                         InsetText::doDispatch(cur, cmd);
489                 else
490                         cur.undispatched();
491                 break;
492
493         case LFUN_MOUSE_MOTION:
494         case LFUN_MOUSE_DOUBLE:
495         case LFUN_MOUSE_TRIPLE:
496                 if (hitButton(cmd)) 
497                         cur.noScreenUpdate();
498                 else if (geometry(cur.bv()) != ButtonOnly)
499                         InsetText::doDispatch(cur, cmd);
500                 else
501                         cur.undispatched();
502                 break;
503
504         case LFUN_MOUSE_RELEASE:
505                 if (!hitButton(cmd)) {
506                         // The mouse click has to be within the inset!
507                         if (geometry(cur.bv()) != ButtonOnly)
508                                 InsetText::doDispatch(cur, cmd);
509                         else
510                                 cur.undispatched();                     
511                         break;
512                 }
513                 if (cmd.button() != mouse_button::button1) {
514                         // Nothing to do.
515                         cur.noScreenUpdate();
516                         break;
517                 }
518                 // if we are selecting, we do not want to
519                 // toggle the inset.
520                 if (cur.selection())
521                         break;
522                 // Left button is clicked, the user asks to
523                 // toggle the inset visual state.
524                 cur.dispatched();
525                 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
526                 if (geometry(cur.bv()) == ButtonOnly) {
527                         setStatus(cur, Open);
528                         edit(cur, true);
529                 }
530                 else
531                         setStatus(cur, Collapsed);
532                 cur.bv().cursor() = cur;
533                 break;
534
535         case LFUN_INSET_TOGGLE:
536                 if (cmd.argument() == "open")
537                         setStatus(cur, Open);
538                 else if (cmd.argument() == "close")
539                         setStatus(cur, Collapsed);
540                 else if (cmd.argument() == "toggle" || cmd.argument().empty())
541                         if (status_ == Open)
542                                 setStatus(cur, Collapsed);
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         InsetLayout const & il = getLayout();
588         docstring const label = labelstring_.empty() ? 
589                 translateIfPossible(il.labelstring()) : labelstring_;
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 string InsetCollapsable::contextMenu(BufferView const & bv, int x,
623         int y) const
624 {
625         string context_menu = contextMenuName();
626         string const it_context_menu = InsetText::contextMenuName();
627         if (decoration() == InsetLayout::CONGLOMERATE)
628                 return context_menu + ";" + it_context_menu;
629
630         string const ic_context_menu = InsetCollapsable::contextMenuName();
631         if (ic_context_menu != context_menu)
632                 context_menu += ";" + ic_context_menu;
633
634         if (geometry(bv) == NoButton)
635                 return context_menu + ";" + it_context_menu;
636
637         Dimension dim = dimensionCollapsed(bv);
638         if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
639                 return context_menu;
640
641         return it_context_menu;
642 }
643
644
645 string InsetCollapsable::contextMenuName() const
646 {
647         if (decoration() == InsetLayout::CONGLOMERATE)
648                 return "context-conglomerate";
649         else
650                 return "context-collapsable";
651 }
652
653 } // namespace lyx