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