]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCollapsable.cpp
Fix redrawing bug for multirow inlined collapsable inset.
[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 "debug.h"
22 #include "DispatchResult.h"
23 #include "FloatList.h"
24 #include "FuncStatus.h"
25 #include "gettext.h"
26 #include "Color.h"
27 #include "LaTeXFeatures.h"
28 #include "Lexer.h"
29 #include "FuncRequest.h"
30 #include "MetricsInfo.h"
31
32 #include "frontends/FontMetrics.h"
33 #include "frontends/Painter.h"
34
35
36 namespace lyx {
37
38 using std::endl;
39 using std::string;
40 using std::ostream;
41
42
43 InsetCollapsable::CollapseStatus InsetCollapsable::status() const
44 {
45         return autoOpen_ ? Open : status_;
46 }
47
48
49 InsetCollapsable::Geometry InsetCollapsable::geometry() const
50 {
51         switch (decoration()) {
52         case Classic:
53                 if (status_ == Open || autoOpen_) {
54                         if (openinlined_)
55                                 return LeftButton;
56                         else
57                                 return TopButton;
58                 } else
59                         return ButtonOnly;
60
61         case Minimalistic:
62                 return NoButton;
63
64         case Conglomerate:
65                 return status_ == Open ? SubLabel : Corners;
66         }
67
68         // dummy return value to shut down a warning,
69         // this is dead code.
70         return NoButton;
71 }
72
73
74 InsetCollapsable::InsetCollapsable
75                 (BufferParams const & bp, CollapseStatus status)
76         : InsetText(bp), status_(status),
77           openinlined_(false), autoOpen_(false), mouse_hover_(false)
78 {
79         setAutoBreakRows(true);
80         setDrawFrame(true);
81         setFrameColor(Color::collapsableframe);
82         setButtonLabel();
83         // Fallback for lacking inset layout item
84         layout_.bgcolor = Color::background;
85 }
86
87
88 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
89         : InsetText(rhs),
90                 button_dim(rhs.button_dim),
91                 topx(rhs.topx),
92                 topbaseline(rhs.topbaseline),
93                 layout_(rhs.layout_),
94                 status_(rhs.status_),
95                 openinlined_(rhs.openinlined_),
96                 autoOpen_(rhs.autoOpen_),
97                 textdim_(rhs.textdim_),
98                 // the sole purpose of this copy constructor
99                 mouse_hover_(false)
100 {
101 }
102
103
104 void  InsetCollapsable::setLayout(BufferParams const & bp)
105 {
106         layout_ = getLayout(bp);
107 }
108
109
110 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
111 {
112         os << "status ";
113         switch (status_) {
114         case Open:
115                 os << "open";
116                 break;
117         case Collapsed:
118                 os << "collapsed";
119                 break;
120         }
121         os << "\n";
122         text_.write(buf, os);
123 }
124
125
126 void InsetCollapsable::read(Buffer const & buf, Lexer & lex)
127 {
128         bool token_found = false;
129         if (lex.isOK()) {
130                 lex.next();
131                 string const token = lex.getString();
132                 if (token == "status") {
133                         lex.next();
134                         string const tmp_token = lex.getString();
135
136                         if (tmp_token == "collapsed") {
137                                 status_ = Collapsed;
138                                 token_found = true;
139                         } else if (tmp_token == "open") {
140                                 status_ = Open;
141                                 token_found = true;
142                         } else {
143                                 lyxerr << "InsetCollapsable::read: Missing status!"
144                                        << endl;
145                                 // Take countermeasures
146                                 lex.pushToken(token);
147                         }
148                 } else {
149                         lyxerr << "InsetCollapsable::read: Missing 'status'-tag!"
150                                    << endl;
151                         // take countermeasures
152                         lex.pushToken(token);
153                 }
154         }
155         InsetText::read(buf, lex);
156
157         if (!token_found)
158                 status_ = isOpen() ? Open : Collapsed;
159
160         setButtonLabel();
161 }
162
163
164 Dimension InsetCollapsable::dimensionCollapsed() const
165 {
166         Dimension dim;
167         theFontMetrics(layout_.labelfont).buttonText(
168                 layout_.labelstring, dim.wid, dim.asc, dim.des);
169         return dim;
170 }
171
172
173 bool InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
174 {
175         using std::max;
176
177         autoOpen_ = mi.base.bv->cursor().isInside(this);
178         mi.base.textwidth -= (int) (1.5 * TEXT_TO_INSET_OFFSET);
179
180         switch (decoration()) {
181         case Minimalistic:
182                 InsetText::metrics(mi, dim);
183                 break;
184         case Conglomerate:
185                 InsetText::metrics(mi, dim);
186                 if (status() == Open) {
187                         // consider width of the inset label
188                         Font font(layout_.labelfont);
189                         font.realize(Font(Font::ALL_SANE));
190                         font.decSize();
191                         font.decSize();
192                         int w = 0;
193                         int a = 0;
194                         int d = 0;
195                         docstring s = layout_.labelstring;
196                         theFontMetrics(font).rectText(s, w, a, d);
197                         dim.wid = max(dim.wid, w);
198                 }
199                 if (status() == Open)
200                         dim.des += ascent();
201                 else {
202                         dim.des -= 3;
203                         dim.asc -= 3;
204                 }
205                 break;
206         case Classic:
207                 dim = dimensionCollapsed();
208                 if (geometry() == TopButton
209                  || geometry() == LeftButton) {
210                         InsetText::metrics(mi, textdim_);
211                         // This expression should not contain mi.base.texwidth
212                         openinlined_ = !hasFixedWidth()
213                                 && textdim_.wid < 0.5 * mi.base.bv->workWidth();
214                         if (openinlined_) {
215                                 // FIXME: this is not ideal but we need to clear it
216                                 // out because the Row::changed() status is reset.
217                                 mi.base.bv->textMetrics(&text_).clear();
218                                 // Correct for button width, and re-fit
219                                 mi.base.textwidth -= dim.wid;
220                                 InsetText::metrics(mi, textdim_);
221                                 dim.wid += textdim_.wid;
222                                 dim.des = max(dim.des - textdim_.asc + dim.asc, textdim_.des);
223                                 dim.asc = textdim_.asc;
224                         } else {
225                                 dim.des += textdim_.height() + TEXT_TO_BOTTOM_OFFSET;
226                                 dim.wid = max(dim.wid, textdim_.wid);
227                                 if (hasFixedWidth())
228                                         dim.wid = max(dim.wid, mi.base.textwidth);
229                         }
230                 }
231                 break;
232         }
233         dim.asc += TEXT_TO_INSET_OFFSET;
234         dim.des += TEXT_TO_INSET_OFFSET;
235         dim.wid += (int) (1.5 * TEXT_TO_INSET_OFFSET);
236         mi.base.textwidth += (int) (1.5 * TEXT_TO_INSET_OFFSET);
237         bool const changed = dim_ != dim;
238         dim_ = dim;
239         return changed;
240 }
241
242
243 bool InsetCollapsable::setMouseHover(bool mouse_hover)
244 {
245         mouse_hover_ = mouse_hover;
246         return true;
247 }
248
249
250 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
251 {
252         autoOpen_ = pi.base.bv->cursor().isInside(this);
253         text_.background_color_ = backgroundColor();
254         const int xx = x + TEXT_TO_INSET_OFFSET;
255
256         // Draw button first -- top, left or only
257         Dimension dimc = dimensionCollapsed();
258         int const top  = y - ascent() + TEXT_TO_INSET_OFFSET;
259         if (decoration() == Classic) {
260                 button_dim.x1 = xx + 0;
261                 button_dim.x2 = xx + dimc.width();
262                 button_dim.y1 = top;
263                 button_dim.y2 = top + dimc.height();
264
265                 pi.pain.buttonText(xx, top + dimc.asc, layout_.labelstring, layout_.labelfont, mouse_hover_);
266         }
267
268         int textx, texty;
269         switch (geometry()) {
270         case LeftButton:
271                 textx = xx + dimc.width();
272                 texty = top + textdim_.asc;
273                 InsetText::draw(pi, textx, texty);
274                 break;
275         case TopButton:
276                 textx = xx;
277                 texty = top + dimc.height() + textdim_.asc;
278                 InsetText::draw(pi, textx, texty);
279                 break;
280         case ButtonOnly:
281                 break;
282         case NoButton:
283                 textx = xx;
284                 texty = y + textdim_.asc;
285                 InsetText::draw(pi, textx, texty);
286                 break;
287         case SubLabel:
288         case Corners:
289                 textx = xx;
290                 texty = y + textdim_.asc;
291                 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
292                 InsetText::draw(pi, textx, texty);
293                 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
294
295                 int desc = InsetText::descent();
296                 if (status() == Open)
297                         desc -= ascent();
298                 else
299                         desc -= 3;
300
301                 pi.pain.line(x, y + desc - 4, x, y + desc, 
302                         layout_.labelfont.color());
303                 if (internalStatus() == Open)
304                         pi.pain.line(x, y + desc, 
305                                 x + dim_.wid - 3, y + desc,
306                                 layout_.labelfont.color());
307                 else {
308                         // Make status_ value visible:
309                         pi.pain.line(x, y + desc,
310                                 x + 4, y + desc,
311                                 layout_.labelfont.color());
312                         pi.pain.line(x + dim_.wid - 7, y + desc,
313                                 x + dim_.wid -3, y + desc,
314                                 layout_.labelfont.color());
315                 }
316                 pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
317                         layout_.labelfont.color());
318
319                 // the label of the charstyle. Can be toggled.
320                 if (status() == Open) {
321                         Font font(layout_.labelfont);
322                         font.realize(Font(Font::ALL_SANE));
323                         font.decSize();
324                         font.decSize();
325                         int w = 0;
326                         int a = 0;
327                         int d = 0;
328                         // FIXME UNICODE
329                         docstring s = layout_.labelstring;
330                         theFontMetrics(font).rectText(s, w, a, d);
331                         pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
332                                 s, font, Color::none, Color::none);
333                 }
334
335                 // a visual cue when the cursor is inside the inset
336                 Cursor & cur = pi.base.bv->cursor();
337                 if (cur.isInside(this)) {
338                         y -= ascent();
339                         y += 3;
340                         pi.pain.line(x, y + 4, x, y, layout_.labelfont.color());
341                         pi.pain.line(x + 4, y, x, y, layout_.labelfont.color());
342                         pi.pain.line(x + dim_.wid - 3, y + 4, x + dim_.wid - 3, y,
343                                 layout_.labelfont.color());
344                         pi.pain.line(x + dim_.wid - 7, y, x + dim_.wid - 3, y,
345                                 layout_.labelfont.color());
346                 }
347                 break;
348         }
349         setPosCache(pi, x, y);
350 }
351
352
353 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
354 {
355         x += TEXT_TO_INSET_OFFSET;
356         switch (geometry()) {
357         case LeftButton:
358                 x += dimensionCollapsed().wid;
359                 InsetText::drawSelection(pi, x, y);
360                 break;
361         case TopButton:
362                 y += dimensionCollapsed().des + textdim_.asc;
363                 InsetText::drawSelection(pi, x, y);
364                 break;
365         case ButtonOnly:
366                 break;
367         case NoButton:
368         case SubLabel:
369         case Corners:
370                 InsetText::drawSelection(pi, x, y);
371                 break;
372         }
373 }
374
375
376 void InsetCollapsable::cursorPos(BufferView const & bv,
377                 CursorSlice const & sl, bool boundary, int & x, int & y) const
378 {
379         BOOST_ASSERT(geometry() != ButtonOnly);
380
381         InsetText::cursorPos(bv, sl, boundary, x, y);
382
383         switch (geometry()) {
384         case LeftButton:
385                 x += dimensionCollapsed().wid;
386                 break;
387         case TopButton:
388                 y += dimensionCollapsed().height() - ascent()
389                         + TEXT_TO_INSET_OFFSET + textdim_.asc;
390                 break;
391         case NoButton:
392         case SubLabel:
393         case Corners:
394                 // Do nothing
395                 break;
396         case ButtonOnly:
397                 // Cannot get here
398                 break;
399         }
400         x += TEXT_TO_INSET_OFFSET;
401 }
402
403
404 Inset::EDITABLE InsetCollapsable::editable() const
405 {
406         return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
407 }
408
409
410 bool InsetCollapsable::descendable() const
411 {
412         return geometry() != ButtonOnly;
413 }
414
415
416 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
417 {
418         return button_dim.contains(cmd.x, cmd.y);
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 = std::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 left)
444 {
445         //lyxerr << "InsetCollapsable: edit left/right" << endl;
446         cur.push(*this);
447         InsetText::edit(cur, left);
448 }
449
450
451 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
452 {
453         //lyxerr << "InsetCollapsable: edit xy" << endl;
454         if (geometry() == ButtonOnly
455          || (button_dim.contains(x, y) 
456           && decoration() != Minimalistic))
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 (cmd.button() == mouse_button::button1 
471                  && hitButton(cmd) 
472                  && decoration() != Minimalistic) {
473                         // reset selection if necessary (see bug 3060)
474                         if (cur.selection())
475                                 cur.bv().cursor().clearSelection();
476                         else
477                                 cur.noUpdate();
478                         cur.dispatched();
479                         break;
480                 }
481                 if (decoration() == Minimalistic)
482                         InsetText::doDispatch(cur, cmd);
483                 else if (geometry() != ButtonOnly 
484                      && !hitButton(cmd))
485                         InsetText::doDispatch(cur, cmd);
486                 else
487                         cur.undispatched();
488                 break;
489
490         case LFUN_MOUSE_MOTION:
491         case LFUN_MOUSE_DOUBLE:
492         case LFUN_MOUSE_TRIPLE:
493                 if (decoration() == Minimalistic)
494                         InsetText::doDispatch(cur, cmd);
495                 else if (geometry() != ButtonOnly
496                      && !hitButton(cmd))
497                         InsetText::doDispatch(cur, cmd);
498                 else
499                         cur.undispatched();
500                 break;
501
502         case LFUN_MOUSE_RELEASE:
503                 if (cmd.button() == mouse_button::button3) {
504                         if (decoration() == Conglomerate) {
505                                 if (internalStatus() == Open)
506                                         setStatus(cur, Collapsed);
507                                 else
508                                         setStatus(cur, Open);
509                                 break;
510                         } else {
511                                 // Open the Inset 
512                                 // configuration dialog
513                                 showInsetDialog(&cur.bv());
514                                 break;
515                         }
516                 }
517
518                 if (decoration() == Minimalistic) {
519                         // The mouse click has to be within the inset!
520                         InsetText::doDispatch(cur, cmd);
521                         break;
522                 }
523
524                 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
525                         // if we are selecting, we do not want to
526                         // toggle the inset.
527                         if (cur.selection())
528                                 break;
529                         // Left button is clicked, the user asks to
530                         // toggle the inset visual state.
531                         cur.dispatched();
532                         cur.updateFlags(Update::Force | Update::FitCursor);
533                         if (geometry() == ButtonOnly) {
534                                 setStatus(cur, Open);
535                                 edit(cur, true);
536                         }
537                         else {
538                                 setStatus(cur, Collapsed);
539                         }
540                         cur.bv().cursor() = cur;
541                         break;
542                 }
543
544                 // The mouse click is within the opened inset.
545                 if (geometry() == TopButton
546                  || geometry() == LeftButton)
547                         InsetText::doDispatch(cur, cmd);
548                 break;
549
550         case LFUN_INSET_TOGGLE:
551                 if (cmd.argument() == "open")
552                         setStatus(cur, Open);
553                 else if (cmd.argument() == "close")
554                         setStatus(cur, Collapsed);
555                 else if (cmd.argument() == "toggle" || cmd.argument().empty())
556                         if (internalStatus() == Open) {
557                                 setStatus(cur, Collapsed);
558                                 if (geometry() == ButtonOnly)
559                                         cur.top().forwardPos();
560                         } else
561                                 setStatus(cur, Open);
562                 else // if assign or anything else
563                         cur.undispatched();
564                 cur.dispatched();
565                 break;
566
567         default:
568                 InsetText::doDispatch(cur, cmd);
569                 break;
570         }
571 }
572
573
574 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
575                 FuncStatus & flag) const
576 {
577         switch (cmd.action) {
578
579         case LFUN_INSET_TOGGLE:
580                 if (cmd.argument() == "open" || cmd.argument() == "close" ||
581                     cmd.argument() == "toggle")
582                         flag.enabled(true);
583                 else
584                         flag.enabled(false);
585                 return true;
586
587         default:
588                 return InsetText::getStatus(cur, cmd, flag);
589         }
590 }
591
592
593 void InsetCollapsable::setLabel(docstring const & l)
594 {
595         layout_.labelstring = l;
596 }
597
598
599 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
600 {
601         status_ = status;
602         setButtonLabel();
603         if (status_ == Collapsed)
604                 cur.leaveInset(*this);
605 }
606
607
608 void InsetCollapsable::setLabelFont(Font const & font)
609 {
610         layout_.labelfont = font;
611 }
612
613 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
614 {
615         FloatList const & floats = bp.getTextClass().floats();
616         FloatList::const_iterator it = floats[type];
617         // FIXME UNICODE
618         return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
619 }
620
621
622 InsetCollapsable::Decoration InsetCollapsable::decoration() const
623 {
624         if (layout_.decoration == "classic")
625                 return Classic;
626         if (layout_.decoration == "minimalistic")
627                 return Minimalistic;
628         if (layout_.decoration == "conglomerate")
629                 return Conglomerate;
630         if (name() == from_ascii("CharStyle"))
631                 return Conglomerate;
632         return Classic;
633 }
634
635
636 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
637                           OutputParams const & runparams) const
638 {
639         // This implements the standard way of handling the LaTeX output of
640         // a collapsable inset, either a command or an environment. Standard 
641         // collapsable insets should not redefine this, non-standard ones may
642         // call this.
643         if (!layout_.latexname.empty()) {
644                 if (layout_.latextype == "command") {
645                         // FIXME UNICODE
646                         os << '\\' << from_utf8(layout_.latexname);
647                         if (!layout_.latexparam.empty())
648                                 os << from_utf8(layout_.latexparam);
649                         os << '{';
650                 } else if (layout_.latextype == "environment") {
651                         os << "%\n\\begin{" << from_utf8(layout_.latexname) << "}\n";
652                         if (!layout_.latexparam.empty())
653                                 os << from_utf8(layout_.latexparam);
654                 }
655         }
656         int i = InsetText::latex(buf, os, runparams);
657         if (!layout_.latexname.empty())
658                 if (layout_.latextype == "command") {
659                         os << "}";
660                 } else if (layout_.latextype == "environment") {
661                         os << "\n\\end{" << from_utf8(layout_.latexname) << "}\n";
662                         i += 4;
663                 }
664         return i;
665 }
666
667
668 void InsetCollapsable::validate(LaTeXFeatures & features) const
669 {
670         // Force inclusion of preamble snippet in layout file
671         features.addPreambleSnippet(layout_.preamble);
672         InsetText::validate(features);
673 }
674
675
676 } // namespace lyx