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