]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCollapsable.cpp
Use Minimalistic for branches
[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) {
54                         if (openinlined_)
55                                 return LeftButton;
56                         else
57                                 return TopButton;
58                 } else
59                         return ButtonOnly;
60
61         case Minimalistic:
62                 return status() == Open ? NoButton : ButtonOnly ;
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 (geometry()) {
181         case NoButton:
182                 InsetText::metrics(mi, dim);
183                 break;
184         case Corners:
185                 InsetText::metrics(mi, dim);
186                 dim.des -= 3;
187                 dim.asc -= 3;
188                 break;
189         case SubLabel: {
190                 InsetText::metrics(mi, dim);
191                 // consider width of the inset label
192                 Font font(layout_.labelfont);
193                 font.realize(Font(Font::ALL_SANE));
194                 font.decSize();
195                 font.decSize();
196                 int w = 0;
197                 int a = 0;
198                 int d = 0;
199                 docstring s = layout_.labelstring;
200                 theFontMetrics(font).rectText(s, w, a, d);
201                 dim.wid = max(dim.wid, w);
202                 dim.des += ascent();
203                 break;
204                 }
205         case TopButton:
206         case LeftButton:
207         case ButtonOnly:
208                 dim = dimensionCollapsed();
209                 if (geometry() == TopButton
210                  || geometry() == LeftButton) {
211                         InsetText::metrics(mi, textdim_);
212                         // This expression should not contain mi.base.texwidth
213                         openinlined_ = !hasFixedWidth()
214                                 && textdim_.wid < 0.5 * mi.base.bv->workWidth();
215                         if (openinlined_) {
216                                 // Correct for button width.
217                                 dim.wid += textdim_.wid;
218                                 dim.des = max(dim.des - textdim_.asc + dim.asc, textdim_.des);
219                                 dim.asc = textdim_.asc;
220                         } else {
221                                 dim.des += textdim_.height() + TEXT_TO_BOTTOM_OFFSET;
222                                 dim.wid = max(dim.wid, textdim_.wid);
223                                 if (hasFixedWidth())
224                                         dim.wid = max(dim.wid, mi.base.textwidth);
225                         }
226                 }
227                 break;
228         }
229         dim.asc += TEXT_TO_INSET_OFFSET;
230         dim.des += TEXT_TO_INSET_OFFSET;
231         dim.wid += int(1.5 * TEXT_TO_INSET_OFFSET);
232         mi.base.textwidth += int(1.5 * TEXT_TO_INSET_OFFSET);
233         bool const changed = dim_ != dim;
234         dim_ = dim;
235         return changed;
236 }
237
238
239 bool InsetCollapsable::setMouseHover(bool mouse_hover)
240 {
241         mouse_hover_ = mouse_hover;
242         return true;
243 }
244
245
246 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
247 {
248         autoOpen_ = pi.base.bv->cursor().isInside(this);
249         text_.background_color_ = backgroundColor();
250         const int xx = x + TEXT_TO_INSET_OFFSET;
251
252         // Draw button first -- top, left or only
253         Dimension dimc = dimensionCollapsed();
254         int const top  = y - ascent() + TEXT_TO_INSET_OFFSET;
255         if (geometry() == TopButton ||
256             geometry() == LeftButton ||
257             geometry() == ButtonOnly) {
258                 button_dim.x1 = xx + 0;
259                 button_dim.x2 = xx + dimc.width();
260                 button_dim.y1 = top;
261                 button_dim.y2 = top + dimc.height();
262
263                 pi.pain.buttonText(xx, top + dimc.asc, layout_.labelstring, layout_.labelfont, mouse_hover_);
264         } else {
265                 button_dim.x1 = 0;
266                 button_dim.y1 = 0;
267                 button_dim.x2 = 0;
268                 button_dim.y2 = 0;
269         }
270
271         int textx, texty;
272         switch (geometry()) {
273         case LeftButton:
274                 textx = xx + dimc.width();
275                 texty = top + textdim_.asc;
276                 InsetText::draw(pi, textx, texty);
277                 break;
278         case TopButton:
279                 textx = xx;
280                 texty = top + dimc.height() + textdim_.asc;
281                 InsetText::draw(pi, textx, texty);
282                 break;
283         case ButtonOnly:
284                 break;
285         case NoButton:
286                 textx = xx;
287                 texty = y + textdim_.asc;
288                 InsetText::draw(pi, textx, texty);
289                 break;
290         case SubLabel:
291         case Corners:
292                 textx = xx;
293                 texty = y + textdim_.asc;
294                 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
295                 InsetText::draw(pi, textx, texty);
296                 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
297
298                 int desc = InsetText::descent();
299                 if (status() == Open)
300                         desc -= ascent();
301                 else
302                         desc -= 3;
303
304                 pi.pain.line(x, y + desc - 4, x, y + desc, 
305                         layout_.labelfont.color());
306                 if (internalStatus() == Open)
307                         pi.pain.line(x, y + desc, 
308                                 x + dim_.wid - 3, y + desc,
309                                 layout_.labelfont.color());
310                 else {
311                         // Make status_ value visible:
312                         pi.pain.line(x, y + desc,
313                                 x + 4, y + desc,
314                                 layout_.labelfont.color());
315                         pi.pain.line(x + dim_.wid - 7, y + desc,
316                                 x + dim_.wid -3, y + desc,
317                                 layout_.labelfont.color());
318                 }
319                 pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
320                         layout_.labelfont.color());
321
322                 // the label of the charstyle. Can be toggled.
323                 if (status() == Open) {
324                         Font font(layout_.labelfont);
325                         font.realize(Font(Font::ALL_SANE));
326                         font.decSize();
327                         font.decSize();
328                         int w = 0;
329                         int a = 0;
330                         int d = 0;
331                         // FIXME UNICODE
332                         docstring s = layout_.labelstring;
333                         theFontMetrics(font).rectText(s, w, a, d);
334                         pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
335                                 s, font, Color::none, Color::none);
336                 }
337
338                 // a visual clue when the cursor is inside the inset
339                 Cursor & cur = pi.base.bv->cursor();
340                 if (cur.isInside(this)) {
341                         y -= ascent();
342                         y += 3;
343                         pi.pain.line(x, y + 4, x, y, layout_.labelfont.color());
344                         pi.pain.line(x + 4, y, x, y, layout_.labelfont.color());
345                         pi.pain.line(x + dim_.wid - 3, y + 4, x + dim_.wid - 3, y,
346                                 layout_.labelfont.color());
347                         pi.pain.line(x + dim_.wid - 7, y, x + dim_.wid - 3, y,
348                                 layout_.labelfont.color());
349                 }
350                 break;
351         }
352         setPosCache(pi, x, y);
353 }
354
355
356 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
357 {
358         x += TEXT_TO_INSET_OFFSET;
359         switch (geometry()) {
360         case LeftButton:
361                 x += dimensionCollapsed().wid;
362                 InsetText::drawSelection(pi, x, y);
363                 break;
364         case TopButton:
365                 y += dimensionCollapsed().des + textdim_.asc;
366                 InsetText::drawSelection(pi, x, y);
367                 break;
368         case ButtonOnly:
369                 break;
370         case NoButton:
371         case SubLabel:
372         case Corners:
373                 InsetText::drawSelection(pi, x, y);
374                 break;
375         }
376 }
377
378
379 void InsetCollapsable::cursorPos(BufferView const & bv,
380                 CursorSlice const & sl, bool boundary, int & x, int & y) const
381 {
382         BOOST_ASSERT(geometry() != ButtonOnly);
383
384         InsetText::cursorPos(bv, sl, boundary, x, y);
385
386         switch (geometry()) {
387         case LeftButton:
388                 x += dimensionCollapsed().wid;
389                 break;
390         case TopButton:
391                 y += dimensionCollapsed().height() - ascent()
392                         + TEXT_TO_INSET_OFFSET + textdim_.asc;
393                 break;
394         case NoButton:
395         case SubLabel:
396         case Corners:
397                 // Do nothing
398                 break;
399         case ButtonOnly:
400                 // Cannot get here
401                 break;
402         }
403         x += TEXT_TO_INSET_OFFSET;
404 }
405
406
407 Inset::EDITABLE InsetCollapsable::editable() const
408 {
409         return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
410 }
411
412
413 bool InsetCollapsable::descendable() const
414 {
415         return geometry() != ButtonOnly;
416 }
417
418
419 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
420 {
421         return button_dim.contains(cmd.x, cmd.y);
422 }
423
424
425 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
426 {
427         docstring label;
428         pos_type const max_length = 15;
429         pos_type const p_siz = paragraphs().begin()->size();
430         pos_type const n = std::min(max_length, p_siz);
431         pos_type i = 0;
432         pos_type j = 0;
433         for (; i < n && j < p_siz; ++j) {
434                 if (paragraphs().begin()->isInset(j))
435                         continue;
436                 label += paragraphs().begin()->getChar(j);
437                 ++i;
438         }
439         if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
440                 label += "...";
441         }
442         return label.empty() ? l : label;
443 }
444
445
446 void InsetCollapsable::edit(Cursor & cur, bool left)
447 {
448         //lyxerr << "InsetCollapsable: edit left/right" << endl;
449         cur.push(*this);
450         InsetText::edit(cur, left);
451 }
452
453
454 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
455 {
456         //lyxerr << "InsetCollapsable: edit xy" << endl;
457         if (geometry() == ButtonOnly
458          || (button_dim.contains(x, y) 
459           && geometry() != NoButton))
460                 return this;
461         cur.push(*this);
462         return InsetText::editXY(cur, x, y);
463 }
464
465
466 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
467 {
468         //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
469         //      << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
470
471         switch (cmd.action) {
472         case LFUN_MOUSE_PRESS:
473                 if (cmd.button() == mouse_button::button1 
474                  && hitButton(cmd) 
475                  && geometry() != NoButton) {
476                         // reset selection if necessary (see bug 3060)
477                         if (cur.selection())
478                                 cur.bv().cursor().clearSelection();
479                         else
480                                 cur.noUpdate();
481                         cur.dispatched();
482                         break;
483                 }
484                 if (geometry() == NoButton)
485                         InsetText::doDispatch(cur, cmd);
486                 else if (geometry() != ButtonOnly 
487                      && !hitButton(cmd))
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 (geometry() == NoButton)
497                         InsetText::doDispatch(cur, cmd);
498                 else if (geometry() != ButtonOnly
499                      && !hitButton(cmd))
500                         InsetText::doDispatch(cur, cmd);
501                 else
502                         cur.undispatched();
503                 break;
504
505         case LFUN_MOUSE_RELEASE:
506                 if (cmd.button() == mouse_button::button3) {
507                         // There is no button to right click:
508                         if (geometry() == Corners ||
509                             geometry() == SubLabel ||
510                             geometry() == NoButton)  {
511                                 if (internalStatus() == Open)
512                                         setStatus(cur, Collapsed);
513                                 else
514                                         setStatus(cur, Open);
515                                 break;
516                         } else {
517                                 // Open the Inset 
518                                 // configuration dialog
519                                 showInsetDialog(&cur.bv());
520                                 break;
521                         }
522                 }
523
524                 if (geometry() == NoButton) {
525                         // The mouse click has to be within the inset!
526                         InsetText::doDispatch(cur, cmd);
527                         break;
528                 }
529
530                 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
531                         // if we are selecting, we do not want to
532                         // toggle the inset.
533                         if (cur.selection())
534                                 break;
535                         // Left button is clicked, the user asks to
536                         // toggle the inset visual state.
537                         cur.dispatched();
538                         cur.updateFlags(Update::Force | Update::FitCursor);
539                         if (geometry() == ButtonOnly) {
540                                 setStatus(cur, Open);
541                                 edit(cur, true);
542                         }
543                         else {
544                                 setStatus(cur, Collapsed);
545                         }
546                         cur.bv().cursor() = cur;
547                         break;
548                 }
549
550                 // The mouse click is within the opened inset.
551                 if (geometry() == TopButton
552                  || geometry() == LeftButton)
553                         InsetText::doDispatch(cur, cmd);
554                 break;
555
556         case LFUN_INSET_TOGGLE:
557                 if (cmd.argument() == "open")
558                         setStatus(cur, Open);
559                 else if (cmd.argument() == "close")
560                         setStatus(cur, Collapsed);
561                 else if (cmd.argument() == "toggle" || cmd.argument().empty())
562                         if (internalStatus() == Open) {
563                                 setStatus(cur, Collapsed);
564                                 if (geometry() == ButtonOnly)
565                                         cur.top().forwardPos();
566                         } else
567                                 setStatus(cur, Open);
568                 else // if assign or anything else
569                         cur.undispatched();
570                 cur.dispatched();
571                 break;
572
573         default:
574                 InsetText::doDispatch(cur, cmd);
575                 break;
576         }
577 }
578
579
580 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
581                 FuncStatus & flag) const
582 {
583         switch (cmd.action) {
584
585         case LFUN_INSET_TOGGLE:
586                 if (cmd.argument() == "open" || cmd.argument() == "close" ||
587                     cmd.argument() == "toggle")
588                         flag.enabled(true);
589                 else
590                         flag.enabled(false);
591                 return true;
592
593         default:
594                 return InsetText::getStatus(cur, cmd, flag);
595         }
596 }
597
598
599 void InsetCollapsable::setLabel(docstring const & l)
600 {
601         layout_.labelstring = l;
602 }
603
604
605 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
606 {
607         status_ = status;
608         setButtonLabel();
609         if (status_ == Collapsed)
610                 cur.leaveInset(*this);
611 }
612
613
614 void InsetCollapsable::setLabelFont(Font const & font)
615 {
616         layout_.labelfont = font;
617 }
618
619 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
620 {
621         FloatList const & floats = bp.getTextClass().floats();
622         FloatList::const_iterator it = floats[type];
623         // FIXME UNICODE
624         return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
625 }
626
627
628 InsetCollapsable::Decoration InsetCollapsable::decoration() const
629 {
630         if (layout_.decoration == "classic")
631                 return Classic;
632         if (layout_.decoration == "minimalistic")
633                 return Minimalistic;
634         if (layout_.decoration == "conglomerate")
635                 return Conglomerate;
636         if (name() == from_ascii("CharStyle"))
637                 return Conglomerate;
638         return Classic;
639 }
640
641
642 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
643                           OutputParams const & runparams) const
644 {
645         // This implements the standard way of handling the LaTeX output of
646         // a collapsable inset, either a command or an environment. Standard 
647         // collapsable insets should not redefine this, non-standard ones may
648         // call this.
649         if (!layout_.latexname.empty()) {
650                 if (layout_.latextype == "command") {
651                         // FIXME UNICODE
652                         os << '\\' << from_utf8(layout_.latexname);
653                         if (!layout_.latexparam.empty())
654                                 os << from_utf8(layout_.latexparam);
655                         os << '{';
656                 } else if (layout_.latextype == "environment") {
657                         os << "%\n\\begin{" << from_utf8(layout_.latexname) << "}\n";
658                         if (!layout_.latexparam.empty())
659                                 os << from_utf8(layout_.latexparam);
660                 }
661         }
662         int i = InsetText::latex(buf, os, runparams);
663         if (!layout_.latexname.empty())
664                 if (layout_.latextype == "command") {
665                         os << "}";
666                 } else if (layout_.latextype == "environment") {
667                         os << "\n\\end{" << from_utf8(layout_.latexname) << "}\n";
668                         i += 4;
669                 }
670         return i;
671 }
672
673
674 void InsetCollapsable::validate(LaTeXFeatures & features) const
675 {
676         // Force inclusion of preamble snippet in layout file
677         features.addPreambleSnippet(layout_.preamble);
678         InsetText::validate(features);
679 }
680
681
682 } // namespace lyx