]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCollapsable.cpp
Cleanup: Replace decoration() calls by geometry(). (And get rid of unused call)
[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 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 (geometry()) {
181         case NoButton:
182                 InsetText::metrics(mi, dim);
183                 break;
184         case SubLabel:
185         case Corners:
186                 InsetText::metrics(mi, dim);
187                 if (status() == Open) {
188                         // consider width of the inset label
189                         Font font(layout_.labelfont);
190                         font.realize(Font(Font::ALL_SANE));
191                         font.decSize();
192                         font.decSize();
193                         int w = 0;
194                         int a = 0;
195                         int d = 0;
196                         docstring s = layout_.labelstring;
197                         theFontMetrics(font).rectText(s, w, a, d);
198                         dim.wid = max(dim.wid, w);
199                 }
200                 if (status() == Open)
201                         dim.des += ascent();
202                 else {
203                         dim.des -= 3;
204                         dim.asc -= 3;
205                 }
206                 break;
207         case TopButton:
208         case LeftButton:
209         case ButtonOnly:
210                 dim = dimensionCollapsed();
211                 if (geometry() == TopButton
212                  || geometry() == LeftButton) {
213                         InsetText::metrics(mi, textdim_);
214                         // This expression should not contain mi.base.texwidth
215                         openinlined_ = !hasFixedWidth()
216                                 && textdim_.wid < 0.5 * mi.base.bv->workWidth();
217                         if (openinlined_) {
218                                 // FIXME: this is not ideal but we need to clear it
219                                 // out because the Row::changed() status is reset.
220                                 mi.base.bv->textMetrics(&text_).clear();
221                                 // Correct for button width, and re-fit
222                                 mi.base.textwidth -= dim.wid;
223                                 InsetText::metrics(mi, textdim_);
224                                 dim.wid += textdim_.wid;
225                                 dim.des = max(dim.des - textdim_.asc + dim.asc, textdim_.des);
226                                 dim.asc = textdim_.asc;
227                         } else {
228                                 dim.des += textdim_.height() + TEXT_TO_BOTTOM_OFFSET;
229                                 dim.wid = max(dim.wid, textdim_.wid);
230                                 if (hasFixedWidth())
231                                         dim.wid = max(dim.wid, mi.base.textwidth);
232                         }
233                 }
234                 break;
235         }
236         dim.asc += TEXT_TO_INSET_OFFSET;
237         dim.des += TEXT_TO_INSET_OFFSET;
238         dim.wid += (int) (1.5 * TEXT_TO_INSET_OFFSET);
239         mi.base.textwidth += (int) (1.5 * TEXT_TO_INSET_OFFSET);
240         bool const changed = dim_ != dim;
241         dim_ = dim;
242         return changed;
243 }
244
245
246 bool InsetCollapsable::setMouseHover(bool mouse_hover)
247 {
248         mouse_hover_ = mouse_hover;
249         return true;
250 }
251
252
253 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
254 {
255         autoOpen_ = pi.base.bv->cursor().isInside(this);
256         text_.background_color_ = backgroundColor();
257         const int xx = x + TEXT_TO_INSET_OFFSET;
258
259         // Draw button first -- top, left or only
260         Dimension dimc = dimensionCollapsed();
261         int const top  = y - ascent() + TEXT_TO_INSET_OFFSET;
262         if (decoration() == Classic) {
263                 button_dim.x1 = xx + 0;
264                 button_dim.x2 = xx + dimc.width();
265                 button_dim.y1 = top;
266                 button_dim.y2 = top + dimc.height();
267
268                 pi.pain.buttonText(xx, top + dimc.asc, layout_.labelstring, layout_.labelfont, mouse_hover_);
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 cue 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           && decoration() != Minimalistic))
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 (decoration() == Minimalistic)
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                         if (decoration() == Conglomerate) {
508                                 if (internalStatus() == Open)
509                                         setStatus(cur, Collapsed);
510                                 else
511                                         setStatus(cur, Open);
512                                 break;
513                         } else {
514                                 // Open the Inset 
515                                 // configuration dialog
516                                 showInsetDialog(&cur.bv());
517                                 break;
518                         }
519                 }
520
521                 if (geometry() == NoButton) {
522                         // The mouse click has to be within the inset!
523                         InsetText::doDispatch(cur, cmd);
524                         break;
525                 }
526
527                 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
528                         // if we are selecting, we do not want to
529                         // toggle the inset.
530                         if (cur.selection())
531                                 break;
532                         // Left button is clicked, the user asks to
533                         // toggle the inset visual state.
534                         cur.dispatched();
535                         cur.updateFlags(Update::Force | Update::FitCursor);
536                         if (geometry() == ButtonOnly) {
537                                 setStatus(cur, Open);
538                                 edit(cur, true);
539                         }
540                         else {
541                                 setStatus(cur, Collapsed);
542                         }
543                         cur.bv().cursor() = cur;
544                         break;
545                 }
546
547                 // The mouse click is within the opened inset.
548                 if (geometry() == TopButton
549                  || geometry() == LeftButton)
550                         InsetText::doDispatch(cur, cmd);
551                 break;
552
553         case LFUN_INSET_TOGGLE:
554                 if (cmd.argument() == "open")
555                         setStatus(cur, Open);
556                 else if (cmd.argument() == "close")
557                         setStatus(cur, Collapsed);
558                 else if (cmd.argument() == "toggle" || cmd.argument().empty())
559                         if (internalStatus() == Open) {
560                                 setStatus(cur, Collapsed);
561                                 if (geometry() == ButtonOnly)
562                                         cur.top().forwardPos();
563                         } else
564                                 setStatus(cur, Open);
565                 else // if assign or anything else
566                         cur.undispatched();
567                 cur.dispatched();
568                 break;
569
570         default:
571                 InsetText::doDispatch(cur, cmd);
572                 break;
573         }
574 }
575
576
577 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
578                 FuncStatus & flag) const
579 {
580         switch (cmd.action) {
581
582         case LFUN_INSET_TOGGLE:
583                 if (cmd.argument() == "open" || cmd.argument() == "close" ||
584                     cmd.argument() == "toggle")
585                         flag.enabled(true);
586                 else
587                         flag.enabled(false);
588                 return true;
589
590         default:
591                 return InsetText::getStatus(cur, cmd, flag);
592         }
593 }
594
595
596 void InsetCollapsable::setLabel(docstring const & l)
597 {
598         layout_.labelstring = l;
599 }
600
601
602 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
603 {
604         status_ = status;
605         setButtonLabel();
606         if (status_ == Collapsed)
607                 cur.leaveInset(*this);
608 }
609
610
611 void InsetCollapsable::setLabelFont(Font const & font)
612 {
613         layout_.labelfont = font;
614 }
615
616 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
617 {
618         FloatList const & floats = bp.getTextClass().floats();
619         FloatList::const_iterator it = floats[type];
620         // FIXME UNICODE
621         return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
622 }
623
624
625 InsetCollapsable::Decoration InsetCollapsable::decoration() const
626 {
627         if (layout_.decoration == "classic")
628                 return Classic;
629         if (layout_.decoration == "minimalistic")
630                 return Minimalistic;
631         if (layout_.decoration == "conglomerate")
632                 return Conglomerate;
633         if (name() == from_ascii("CharStyle"))
634                 return Conglomerate;
635         return Classic;
636 }
637
638
639 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
640                           OutputParams const & runparams) const
641 {
642         // This implements the standard way of handling the LaTeX output of
643         // a collapsable inset, either a command or an environment. Standard 
644         // collapsable insets should not redefine this, non-standard ones may
645         // call this.
646         if (!layout_.latexname.empty()) {
647                 if (layout_.latextype == "command") {
648                         // FIXME UNICODE
649                         os << '\\' << from_utf8(layout_.latexname);
650                         if (!layout_.latexparam.empty())
651                                 os << from_utf8(layout_.latexparam);
652                         os << '{';
653                 } else if (layout_.latextype == "environment") {
654                         os << "%\n\\begin{" << from_utf8(layout_.latexname) << "}\n";
655                         if (!layout_.latexparam.empty())
656                                 os << from_utf8(layout_.latexparam);
657                 }
658         }
659         int i = InsetText::latex(buf, os, runparams);
660         if (!layout_.latexname.empty())
661                 if (layout_.latextype == "command") {
662                         os << "}";
663                 } else if (layout_.latextype == "environment") {
664                         os << "\n\\end{" << from_utf8(layout_.latexname) << "}\n";
665                         i += 4;
666                 }
667         return i;
668 }
669
670
671 void InsetCollapsable::validate(LaTeXFeatures & features) const
672 {
673         // Force inclusion of preamble snippet in layout file
674         features.addPreambleSnippet(layout_.preamble);
675         InsetText::validate(features);
676 }
677
678
679 } // namespace lyx