]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCollapsable.cpp
Fix text frame drawing.
[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                 // the sole purpose of this copy constructor
98                 mouse_hover_(false)
99 {
100 }
101
102
103 void  InsetCollapsable::setLayout(BufferParams const & bp)
104 {
105         layout_ = getLayout(bp);
106 }
107
108
109 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
110 {
111         os << "status ";
112         switch (status_) {
113         case Open:
114                 os << "open";
115                 break;
116         case Collapsed:
117                 os << "collapsed";
118                 break;
119         }
120         os << "\n";
121         text_.write(buf, os);
122 }
123
124
125 void InsetCollapsable::read(Buffer const & buf, Lexer & lex)
126 {
127         bool token_found = false;
128         if (lex.isOK()) {
129                 lex.next();
130                 string const token = lex.getString();
131                 if (token == "status") {
132                         lex.next();
133                         string const tmp_token = lex.getString();
134
135                         if (tmp_token == "collapsed") {
136                                 status_ = Collapsed;
137                                 token_found = true;
138                         } else if (tmp_token == "open") {
139                                 status_ = Open;
140                                 token_found = true;
141                         } else {
142                                 lyxerr << "InsetCollapsable::read: Missing status!"
143                                        << endl;
144                                 // Take countermeasures
145                                 lex.pushToken(token);
146                         }
147                 } else {
148                         lyxerr << "InsetCollapsable::read: Missing 'status'-tag!"
149                                    << endl;
150                         // take countermeasures
151                         lex.pushToken(token);
152                 }
153         }
154         InsetText::read(buf, lex);
155
156         if (!token_found)
157                 status_ = isOpen() ? Open : Collapsed;
158
159         setButtonLabel();
160 }
161
162
163 Dimension InsetCollapsable::dimensionCollapsed() const
164 {
165         Dimension dim;
166         theFontMetrics(layout_.labelfont).buttonText(
167                 layout_.labelstring, dim.wid, dim.asc, dim.des);
168         return dim;
169 }
170
171
172 bool InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
173 {
174         using std::max;
175
176         autoOpen_ = mi.base.bv->cursor().isInside(this);
177         mi.base.textwidth -= int(1.5 * TEXT_TO_INSET_OFFSET);
178
179         switch (geometry()) {
180         case NoButton:
181                 InsetText::metrics(mi, dim);
182                 break;
183         case Corners:
184                 InsetText::metrics(mi, dim);
185                 dim.des -= 3;
186                 dim.asc -= 3;
187                 break;
188         case SubLabel: {
189                 InsetText::metrics(mi, dim);
190                 // consider width of the inset label
191                 Font font(layout_.labelfont);
192                 font.realize(Font(Font::ALL_SANE));
193                 font.decSize();
194                 font.decSize();
195                 int w = 0;
196                 int a = 0;
197                 int d = 0;
198                 docstring s = layout_.labelstring;
199                 theFontMetrics(font).rectText(s, w, a, d);
200                 dim.wid = max(dim.wid, w);
201                 dim.des += ascent();
202                 break;
203                 }
204         case TopButton:
205         case LeftButton:
206         case ButtonOnly:
207                 dim = dimensionCollapsed();
208                 if (geometry() == TopButton
209                  || geometry() == LeftButton) {
210                         Dimension textdim;
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         int const old_color = pi.background_color;
248         pi.background_color = backgroundColor();
249         int const xx = x + TEXT_TO_INSET_OFFSET;
250
251         // Draw button first -- top, left or only
252         Dimension dimc = dimensionCollapsed();
253         int const top  = y - ascent() + TEXT_TO_INSET_OFFSET;
254         if (geometry() == TopButton ||
255             geometry() == LeftButton ||
256             geometry() == ButtonOnly) {
257                 button_dim.x1 = xx + 0;
258                 button_dim.x2 = xx + dimc.width();
259                 button_dim.y1 = top;
260                 button_dim.y2 = top + dimc.height();
261
262                 pi.pain.buttonText(xx, top + dimc.asc, layout_.labelstring, layout_.labelfont, mouse_hover_);
263         } else {
264                 button_dim.x1 = 0;
265                 button_dim.y1 = 0;
266                 button_dim.x2 = 0;
267                 button_dim.y2 = 0;
268         }
269
270         int textx, texty;
271         switch (geometry()) {
272         case LeftButton:
273                 textx = xx + dimc.width();
274                 texty = top;
275                 InsetText::draw(pi, textx, texty);
276                 break;
277         case TopButton:
278                 textx = xx;
279                 texty = top + dimc.height();
280                 InsetText::draw(pi, textx, texty);
281                 break;
282         case ButtonOnly:
283                 break;
284         case NoButton:
285                 textx = xx;
286                 texty = y;
287                 InsetText::draw(pi, textx, texty);
288                 break;
289         case SubLabel:
290         case Corners:
291                 textx = xx;
292                 texty = y;
293                 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
294                 InsetText::draw(pi, textx, texty);
295                 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
296
297                 int desc = InsetText::descent();
298                 if (geometry() == SubLabel)
299                         desc -= ascent();
300                 else
301                         desc -= 3;
302
303                 const int xx1 = xx + border_ - 1;
304                 const int xx2 = x + dim_.wid - border_ 
305                         - TEXT_TO_INSET_OFFSET + 1;
306                 pi.pain.line(xx1, y + desc - 4, 
307                              xx1, y + desc, 
308                         layout_.labelfont.color());
309                 if (internalStatus() == Open)
310                         pi.pain.line(xx1, y + desc, 
311                                 xx2, y + desc,
312                                 layout_.labelfont.color());
313                 else {
314                         // Make status_ value visible:
315                         pi.pain.line(xx1, y + desc,
316                                 xx1 + 4, y + desc,
317                                 layout_.labelfont.color());
318                         pi.pain.line(xx2 - 4, y + desc,
319                                 xx2, y + desc,
320                                 layout_.labelfont.color());
321                 }
322                 pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
323                         layout_.labelfont.color());
324
325                 // the label below the text. Can be toggled.
326                 if (geometry() == SubLabel) {
327                         Font font(layout_.labelfont);
328                         font.realize(Font(Font::ALL_SANE));
329                         font.decSize();
330                         font.decSize();
331                         int w = 0;
332                         int a = 0;
333                         int d = 0;
334                         // FIXME UNICODE
335                         docstring s = layout_.labelstring;
336                         theFontMetrics(font).rectText(s, w, a, d);
337                         pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
338                                 s, font, Color::none, Color::none);
339                 }
340
341                 // a visual clue when the cursor is inside the inset
342                 Cursor & cur = pi.base.bv->cursor();
343                 if (cur.isInside(this)) {
344                         y -= ascent();
345                         y += 3;
346                         pi.pain.line(xx1, y + 4, xx1, y, layout_.labelfont.color());
347                         pi.pain.line(xx1 + 4, y, xx1, y, layout_.labelfont.color());
348                         pi.pain.line(xx2, y + 4, x + dim_.wid - 3, y,
349                                 layout_.labelfont.color());
350                         pi.pain.line(xx2 - 4, y, xx2, y,
351                                 layout_.labelfont.color());
352                 }
353                 break;
354         }
355         setPosCache(pi, x, y);
356         pi.background_color = old_color;
357 }
358
359
360 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
361 {
362         x += TEXT_TO_INSET_OFFSET;
363         switch (geometry()) {
364         case LeftButton:
365                 x += dimensionCollapsed().wid;
366                 InsetText::drawSelection(pi, x, y);
367                 break;
368         case TopButton:
369                 y += dimensionCollapsed().des;
370                 InsetText::drawSelection(pi, x, y);
371                 break;
372         case ButtonOnly:
373                 break;
374         case NoButton:
375         case SubLabel:
376         case Corners:
377                 InsetText::drawSelection(pi, x, y);
378                 break;
379         }
380 }
381
382
383 void InsetCollapsable::cursorPos(BufferView const & bv,
384                 CursorSlice const & sl, bool boundary, int & x, int & y) const
385 {
386         BOOST_ASSERT(geometry() != ButtonOnly);
387
388         InsetText::cursorPos(bv, sl, boundary, x, y);
389
390         switch (geometry()) {
391         case LeftButton:
392                 x += dimensionCollapsed().wid;
393                 break;
394         case TopButton: {
395                 TextMetrics const & tm = bv.textMetrics(&text_);
396                 y += dimensionCollapsed().height() - ascent()
397                         + TEXT_TO_INSET_OFFSET + tm.ascent();
398                 break;
399         }
400         case NoButton:
401         case SubLabel:
402         case Corners:
403                 // Do nothing
404                 break;
405         case ButtonOnly:
406                 // Cannot get here
407                 break;
408         }
409         x += TEXT_TO_INSET_OFFSET;
410 }
411
412
413 Inset::EDITABLE InsetCollapsable::editable() const
414 {
415         return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
416 }
417
418
419 bool InsetCollapsable::descendable() const
420 {
421         return geometry() != ButtonOnly;
422 }
423
424
425 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
426 {
427         return button_dim.contains(cmd.x, cmd.y);
428 }
429
430
431 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
432 {
433         docstring label;
434         pos_type const max_length = 15;
435         pos_type const p_siz = paragraphs().begin()->size();
436         pos_type const n = std::min(max_length, p_siz);
437         pos_type i = 0;
438         pos_type j = 0;
439         for (; i < n && j < p_siz; ++j) {
440                 if (paragraphs().begin()->isInset(j))
441                         continue;
442                 label += paragraphs().begin()->getChar(j);
443                 ++i;
444         }
445         if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
446                 label += "...";
447         }
448         return label.empty() ? l : label;
449 }
450
451
452 void InsetCollapsable::edit(Cursor & cur, bool left)
453 {
454         //lyxerr << "InsetCollapsable: edit left/right" << endl;
455         cur.push(*this);
456         InsetText::edit(cur, left);
457 }
458
459
460 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
461 {
462         //lyxerr << "InsetCollapsable: edit xy" << endl;
463         if (geometry() == ButtonOnly
464          || (button_dim.contains(x, y) 
465           && geometry() != NoButton))
466                 return this;
467         cur.push(*this);
468         return InsetText::editXY(cur, x, y);
469 }
470
471
472 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
473 {
474         //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
475         //      << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
476
477         switch (cmd.action) {
478         case LFUN_MOUSE_PRESS:
479                 if (cmd.button() == mouse_button::button1 
480                  && hitButton(cmd) 
481                  && geometry() != NoButton) {
482                         // reset selection if necessary (see bug 3060)
483                         if (cur.selection())
484                                 cur.bv().cursor().clearSelection();
485                         else
486                                 cur.noUpdate();
487                         cur.dispatched();
488                         break;
489                 }
490                 if (geometry() == NoButton)
491                         InsetText::doDispatch(cur, cmd);
492                 else if (geometry() != ButtonOnly 
493                      && !hitButton(cmd))
494                         InsetText::doDispatch(cur, cmd);
495                 else
496                         cur.undispatched();
497                 break;
498
499         case LFUN_MOUSE_MOTION:
500         case LFUN_MOUSE_DOUBLE:
501         case LFUN_MOUSE_TRIPLE:
502                 if (geometry() == NoButton)
503                         InsetText::doDispatch(cur, cmd);
504                 else if (geometry() != ButtonOnly
505                      && !hitButton(cmd))
506                         InsetText::doDispatch(cur, cmd);
507                 else
508                         cur.undispatched();
509                 break;
510
511         case LFUN_MOUSE_RELEASE:
512                 if (cmd.button() == mouse_button::button3) {
513                         // There is no button to right click:
514                         if (geometry() == Corners ||
515                             geometry() == SubLabel ||
516                             geometry() == NoButton)  {
517                                 if (internalStatus() == Open)
518                                         setStatus(cur, Collapsed);
519                                 else
520                                         setStatus(cur, Open);
521                                 break;
522                         } else {
523                                 // Open the Inset 
524                                 // configuration dialog
525                                 showInsetDialog(&cur.bv());
526                                 break;
527                         }
528                 }
529
530                 if (geometry() == NoButton) {
531                         // The mouse click has to be within the inset!
532                         InsetText::doDispatch(cur, cmd);
533                         break;
534                 }
535
536                 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
537                         // if we are selecting, we do not want to
538                         // toggle the inset.
539                         if (cur.selection())
540                                 break;
541                         // Left button is clicked, the user asks to
542                         // toggle the inset visual state.
543                         cur.dispatched();
544                         cur.updateFlags(Update::Force | Update::FitCursor);
545                         if (geometry() == ButtonOnly) {
546                                 setStatus(cur, Open);
547                                 edit(cur, true);
548                         }
549                         else {
550                                 setStatus(cur, Collapsed);
551                         }
552                         cur.bv().cursor() = cur;
553                         break;
554                 }
555
556                 // The mouse click is within the opened inset.
557                 if (geometry() == TopButton
558                  || geometry() == LeftButton)
559                         InsetText::doDispatch(cur, cmd);
560                 break;
561
562         case LFUN_INSET_TOGGLE:
563                 if (cmd.argument() == "open")
564                         setStatus(cur, Open);
565                 else if (cmd.argument() == "close")
566                         setStatus(cur, Collapsed);
567                 else if (cmd.argument() == "toggle" || cmd.argument().empty())
568                         if (internalStatus() == Open) {
569                                 setStatus(cur, Collapsed);
570                                 if (geometry() == ButtonOnly)
571                                         cur.top().forwardPos();
572                         } else
573                                 setStatus(cur, Open);
574                 else // if assign or anything else
575                         cur.undispatched();
576                 cur.dispatched();
577                 break;
578
579         default:
580                 InsetText::doDispatch(cur, cmd);
581                 break;
582         }
583 }
584
585
586 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
587                 FuncStatus & flag) const
588 {
589         switch (cmd.action) {
590
591         case LFUN_INSET_TOGGLE:
592                 if (cmd.argument() == "open" || cmd.argument() == "close" ||
593                     cmd.argument() == "toggle")
594                         flag.enabled(true);
595                 else
596                         flag.enabled(false);
597                 return true;
598
599         default:
600                 return InsetText::getStatus(cur, cmd, flag);
601         }
602 }
603
604
605 void InsetCollapsable::setLabel(docstring const & l)
606 {
607         layout_.labelstring = l;
608 }
609
610
611 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
612 {
613         status_ = status;
614         setButtonLabel();
615         if (status_ == Collapsed)
616                 cur.leaveInset(*this);
617 }
618
619
620 void InsetCollapsable::setLabelFont(Font const & font)
621 {
622         layout_.labelfont = font;
623 }
624
625 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
626 {
627         FloatList const & floats = bp.getTextClass().floats();
628         FloatList::const_iterator it = floats[type];
629         // FIXME UNICODE
630         return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
631 }
632
633
634 InsetCollapsable::Decoration InsetCollapsable::decoration() const
635 {
636         if (layout_.decoration == "classic")
637                 return Classic;
638         if (layout_.decoration == "minimalistic")
639                 return Minimalistic;
640         if (layout_.decoration == "conglomerate")
641                 return Conglomerate;
642         if (name() == from_ascii("CharStyle"))
643                 return Conglomerate;
644         return Classic;
645 }
646
647
648 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
649                           OutputParams const & runparams) const
650 {
651         // This implements the standard way of handling the LaTeX output of
652         // a collapsable inset, either a command or an environment. Standard 
653         // collapsable insets should not redefine this, non-standard ones may
654         // call this.
655         if (!layout_.latexname.empty()) {
656                 if (layout_.latextype == "command") {
657                         // FIXME UNICODE
658                         os << '\\' << from_utf8(layout_.latexname);
659                         if (!layout_.latexparam.empty())
660                                 os << from_utf8(layout_.latexparam);
661                         os << '{';
662                 } else if (layout_.latextype == "environment") {
663                         os << "%\n\\begin{" << from_utf8(layout_.latexname) << "}\n";
664                         if (!layout_.latexparam.empty())
665                                 os << from_utf8(layout_.latexparam);
666                 }
667         }
668         int i = InsetText::latex(buf, os, runparams);
669         if (!layout_.latexname.empty())
670                 if (layout_.latextype == "command") {
671                         os << "}";
672                 } else if (layout_.latextype == "environment") {
673                         os << "\n\\end{" << from_utf8(layout_.latexname) << "}\n";
674                         i += 4;
675                 }
676         return i;
677 }
678
679
680 void InsetCollapsable::validate(LaTeXFeatures & features) const
681 {
682         // Force inclusion of preamble snippet in layout file
683         features.addPreambleSnippet(layout_.preamble);
684         InsetText::validate(features);
685 }
686
687
688 } // namespace lyx