]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCollapsable.cpp
Circumvent odd stmary font metrics (part of #9990).
[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                 }
352
353                 // a visual cue when the cursor is inside the inset
354                 Cursor const & cur = bv.cursor();
355                 if (cur.isInside(this)) {
356                         y -= textdim.asc;
357                         y += 3;
358                         pi.pain.line(xx1, y + 4, xx1, y, Color_foreground);
359                         pi.pain.line(xx1 + 4, y, xx1, y, Color_foreground);
360                         pi.pain.line(xx2, y + 4, xx2, y, Color_foreground);
361                         pi.pain.line(xx2 - 4, y, xx2, y, Color_foreground);
362                 }
363                 break;
364         }
365
366         pi.base.font = tmpfont;
367 }
368
369
370 void InsetCollapsable::cursorPos(BufferView const & bv,
371                 CursorSlice const & sl, bool boundary, int & x, int & y) const
372 {
373         if (geometry(bv) == ButtonOnly)
374                 status_ = Open;
375
376         InsetText::cursorPos(bv, sl, boundary, x, y);
377         Dimension const textdim = InsetText::dimension(bv);
378
379         switch (geometry(bv)) {
380         case LeftButton:
381                 x += dimensionCollapsed(bv).wid;
382                 break;
383         case TopButton: {
384                 y += dimensionCollapsed(bv).des + textdim.asc;
385                 break;
386         }
387         case NoButton:
388         case SubLabel:
389         case Corners:
390                 // Do nothing
391                 break;
392         case ButtonOnly:
393                 // Cannot get here
394                 break;
395         }
396 }
397
398
399 bool InsetCollapsable::editable() const
400 {
401         return geometry() != ButtonOnly;
402 }
403
404
405 bool InsetCollapsable::descendable(BufferView const & bv) const
406 {
407         return geometry(bv) != ButtonOnly;
408 }
409
410
411 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
412 {
413         return button_dim.contains(cmd.x(), cmd.y());
414 }
415
416
417 bool InsetCollapsable::clickable(int x, int y) const
418 {
419         FuncRequest cmd(LFUN_NOACTION, x, y, mouse_button::none);
420         return hitButton(cmd);
421 }
422
423
424 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
425 {
426         docstring label;
427         pos_type const max_length = 15;
428         pos_type const p_siz = paragraphs().begin()->size();
429         pos_type const n = min(max_length, p_siz);
430         pos_type i = 0;
431         pos_type j = 0;
432         for (; i < n && j < p_siz; ++j) {
433                 if (paragraphs().begin()->isInset(j))
434                         continue;
435                 label += paragraphs().begin()->getChar(j);
436                 ++i;
437         }
438         if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
439                 label += "...";
440         }
441         return label.empty() ? l : label;
442 }
443
444
445 void InsetCollapsable::edit(Cursor & cur, bool front, EntryDirection entry_from)
446 {
447         //lyxerr << "InsetCollapsable: edit left/right" << endl;
448         cur.push(*this);
449         InsetText::edit(cur, front, entry_from);
450 }
451
452
453 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
454 {
455         //lyxerr << "InsetCollapsable: edit xy" << endl;
456         if (geometry(cur.bv()) == ButtonOnly
457          || (button_dim.contains(x, y) 
458           && geometry(cur.bv()) != NoButton))
459                 return this;
460         cur.push(*this);
461         return InsetText::editXY(cur, x, y);
462 }
463
464
465 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
466 {
467         //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
468         //      << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
469
470         switch (cmd.action()) {
471         case LFUN_MOUSE_PRESS:
472                 if (hitButton(cmd)) {
473                         switch (cmd.button()) {
474                         case mouse_button::button1:
475                         case mouse_button::button3:
476                                 // Pass the command to the enclosing InsetText,
477                                 // so that the cursor gets set.
478                                 cur.undispatched();
479                                 break;
480                         case mouse_button::none:
481                         case mouse_button::button2:
482                         case mouse_button::button4:
483                         case mouse_button::button5:
484                                 // Nothing to do.
485                                 cur.noScreenUpdate();
486                                 break;
487                         }
488                 } else if (geometry(cur.bv()) != ButtonOnly)
489                         InsetText::doDispatch(cur, cmd);
490                 else
491                         cur.undispatched();
492                 break;
493
494         case LFUN_MOUSE_MOTION:
495         case LFUN_MOUSE_DOUBLE:
496         case LFUN_MOUSE_TRIPLE:
497                 if (hitButton(cmd)) 
498                         cur.noScreenUpdate();
499                 else if (geometry(cur.bv()) != ButtonOnly)
500                         InsetText::doDispatch(cur, cmd);
501                 else
502                         cur.undispatched();
503                 break;
504
505         case LFUN_MOUSE_RELEASE:
506                 if (!hitButton(cmd)) {
507                         // The mouse click has to be within the inset!
508                         if (geometry(cur.bv()) != ButtonOnly)
509                                 InsetText::doDispatch(cur, cmd);
510                         else
511                                 cur.undispatched();                     
512                         break;
513                 }
514                 if (cmd.button() != mouse_button::button1) {
515                         // Nothing to do.
516                         cur.noScreenUpdate();
517                         break;
518                 }
519                 // if we are selecting, we do not want to
520                 // toggle the inset.
521                 if (cur.selection())
522                         break;
523                 // Left button is clicked, the user asks to
524                 // toggle the inset visual state.
525                 cur.dispatched();
526                 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
527                 if (geometry(cur.bv()) == ButtonOnly) {
528                         setStatus(cur, Open);
529                         edit(cur, true);
530                 }
531                 else
532                         setStatus(cur, Collapsed);
533                 cur.bv().cursor() = cur;
534                 break;
535
536         case LFUN_INSET_TOGGLE:
537                 if (cmd.argument() == "open")
538                         setStatus(cur, Open);
539                 else if (cmd.argument() == "close")
540                         setStatus(cur, Collapsed);
541                 else if (cmd.argument() == "toggle" || cmd.argument().empty())
542                         if (status_ == Open)
543                                 setStatus(cur, Collapsed);
544                         else
545                                 setStatus(cur, Open);
546                 else // if assign or anything else
547                         cur.undispatched();
548                 cur.dispatched();
549                 break;
550
551         default:
552                 InsetText::doDispatch(cur, cmd);
553                 break;
554         }
555 }
556
557
558 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
559                 FuncStatus & flag) const
560 {
561         switch (cmd.action()) {
562         case LFUN_INSET_TOGGLE:
563                 if (cmd.argument() == "open")
564                         flag.setEnabled(status_ != Open);
565                 else if (cmd.argument() == "close")
566                         flag.setEnabled(status_ == Open);
567                 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
568                         flag.setEnabled(true);
569                         flag.setOnOff(status_ == Open);
570                 } else
571                         flag.setEnabled(false);
572                 return true;
573
574         default:
575                 return InsetText::getStatus(cur, cmd, flag);
576         }
577 }
578
579
580 void InsetCollapsable::setLabel(docstring const & l)
581 {
582         labelstring_ = l;
583 }
584
585
586 docstring const InsetCollapsable::buttonLabel(BufferView const & bv) const
587 {
588         InsetLayout const & il = getLayout();
589         docstring const label = labelstring_.empty() ? 
590                 translateIfPossible(il.labelstring()) : labelstring_;
591         if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
592                 return label;
593         return getNewLabel(label);
594 }
595
596
597 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
598 {
599         status_ = status;
600         setButtonLabel();
601         if (status_ == Collapsed)
602                 cur.leaveInset(*this);
603 }
604
605
606 InsetLayout::InsetDecoration InsetCollapsable::decoration() const
607 {
608         InsetLayout::InsetDecoration const dec = getLayout().decoration();
609         return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
610 }
611
612
613 string InsetCollapsable::contextMenu(BufferView const & bv, int x,
614         int y) const
615 {
616         string context_menu = contextMenuName();
617         string const it_context_menu = InsetText::contextMenuName();
618         if (decoration() == InsetLayout::CONGLOMERATE)
619                 return context_menu + ";" + it_context_menu;
620
621         string const ic_context_menu = InsetCollapsable::contextMenuName();
622         if (ic_context_menu != context_menu)
623                 context_menu += ";" + ic_context_menu;
624
625         if (geometry(bv) == NoButton)
626                 return context_menu + ";" + it_context_menu;
627
628         Dimension dim = dimensionCollapsed(bv);
629         if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
630                 return context_menu;
631
632         return it_context_menu;
633 }
634
635
636 string InsetCollapsable::contextMenuName() const
637 {
638         if (decoration() == InsetLayout::CONGLOMERATE)
639                 return "context-conglomerate";
640         else
641                 return "context-collapsable";
642 }
643
644 } // namespace lyx