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