]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCollapsable.cpp
Cleanup painting of corners
[lyx.git] / src / insets / InsetCollapsable.cpp
1 /**
2  * \file InsetCollapsable.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author Jürgen Vigna
8  * \author Lars Gullik Bjønnes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "InsetCollapsable.h"
16
17 #include "Buffer.h"
18 #include "BufferParams.h"
19 #include "BufferView.h"
20 #include "Cursor.h"
21 #include "debug.h"
22 #include "DispatchResult.h"
23 #include "FloatList.h"
24 #include "FuncStatus.h"
25 #include "gettext.h"
26 #include "Color.h"
27 #include "LaTeXFeatures.h"
28 #include "Lexer.h"
29 #include "FuncRequest.h"
30 #include "MetricsInfo.h"
31
32 #include "frontends/FontMetrics.h"
33 #include "frontends/Painter.h"
34
35
36 namespace lyx {
37
38 using std::endl;
39 using std::string;
40 using std::ostream;
41
42
43 InsetCollapsable::CollapseStatus InsetCollapsable::status() const
44 {
45         return autoOpen_ ? Open : status_;
46 }
47
48
49 InsetCollapsable::Geometry InsetCollapsable::geometry() const
50 {
51         switch (decoration()) {
52         case Classic:
53                 if (status() == Open) {
54                         if (openinlined_)
55                                 return LeftButton;
56                         else
57                                 return TopButton;
58                 } else
59                         return ButtonOnly;
60
61         case Minimalistic:
62                 return status() == Open ? NoButton : ButtonOnly ;
63
64         case Conglomerate:
65                 return status() == Open ? SubLabel : Corners ;
66         }
67
68         // dummy return value to shut down a warning,
69         // this is dead code.
70         return NoButton;
71 }
72
73
74 InsetCollapsable::InsetCollapsable
75                 (BufferParams const & bp, CollapseStatus status)
76         : InsetText(bp), status_(status),
77           openinlined_(false), autoOpen_(false), mouse_hover_(false)
78 {
79         setAutoBreakRows(true);
80         setDrawFrame(true);
81         setFrameColor(Color::collapsableframe);
82         setButtonLabel();
83         // Fallback for lacking inset layout item
84         layout_.bgcolor = Color::background;
85 }
86
87
88 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
89         : InsetText(rhs),
90                 button_dim(rhs.button_dim),
91                 topx(rhs.topx),
92                 topbaseline(rhs.topbaseline),
93                 layout_(rhs.layout_),
94                 status_(rhs.status_),
95                 openinlined_(rhs.openinlined_),
96                 autoOpen_(rhs.autoOpen_),
97                 textdim_(rhs.textdim_),
98                 // the sole purpose of this copy constructor
99                 mouse_hover_(false)
100 {
101 }
102
103
104 void  InsetCollapsable::setLayout(BufferParams const & bp)
105 {
106         layout_ = getLayout(bp);
107 }
108
109
110 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
111 {
112         os << "status ";
113         switch (status_) {
114         case Open:
115                 os << "open";
116                 break;
117         case Collapsed:
118                 os << "collapsed";
119                 break;
120         }
121         os << "\n";
122         text_.write(buf, os);
123 }
124
125
126 void InsetCollapsable::read(Buffer const & buf, Lexer & lex)
127 {
128         bool token_found = false;
129         if (lex.isOK()) {
130                 lex.next();
131                 string const token = lex.getString();
132                 if (token == "status") {
133                         lex.next();
134                         string const tmp_token = lex.getString();
135
136                         if (tmp_token == "collapsed") {
137                                 status_ = Collapsed;
138                                 token_found = true;
139                         } else if (tmp_token == "open") {
140                                 status_ = Open;
141                                 token_found = true;
142                         } else {
143                                 lyxerr << "InsetCollapsable::read: Missing status!"
144                                        << endl;
145                                 // Take countermeasures
146                                 lex.pushToken(token);
147                         }
148                 } else {
149                         lyxerr << "InsetCollapsable::read: Missing 'status'-tag!"
150                                    << endl;
151                         // take countermeasures
152                         lex.pushToken(token);
153                 }
154         }
155         InsetText::read(buf, lex);
156
157         if (!token_found)
158                 status_ = isOpen() ? Open : Collapsed;
159
160         setButtonLabel();
161 }
162
163
164 Dimension InsetCollapsable::dimensionCollapsed() const
165 {
166         Dimension dim;
167         theFontMetrics(layout_.labelfont).buttonText(
168                 layout_.labelstring, dim.wid, dim.asc, dim.des);
169         return dim;
170 }
171
172
173 bool InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
174 {
175         using std::max;
176
177         autoOpen_ = mi.base.bv->cursor().isInside(this);
178         mi.base.textwidth -= int(1.5 * TEXT_TO_INSET_OFFSET);
179
180         switch (geometry()) {
181         case NoButton:
182                 InsetText::metrics(mi, dim);
183                 break;
184         case Corners:
185                 InsetText::metrics(mi, dim);
186                 dim.des -= 3;
187                 dim.asc -= 3;
188                 break;
189         case SubLabel: {
190                 InsetText::metrics(mi, dim);
191                 // consider width of the inset label
192                 Font font(layout_.labelfont);
193                 font.realize(Font(Font::ALL_SANE));
194                 font.decSize();
195                 font.decSize();
196                 int w = 0;
197                 int a = 0;
198                 int d = 0;
199                 docstring s = layout_.labelstring;
200                 theFontMetrics(font).rectText(s, w, a, d);
201                 dim.wid = max(dim.wid, w);
202                 dim.des += ascent();
203                 break;
204                 }
205         case TopButton:
206         case LeftButton:
207         case ButtonOnly:
208                 dim = dimensionCollapsed();
209                 if (geometry() == TopButton
210                  || geometry() == LeftButton) {
211                         InsetText::metrics(mi, textdim_);
212                         // This expression should not contain mi.base.texwidth
213                         openinlined_ = !hasFixedWidth()
214                                 && textdim_.wid < 0.5 * mi.base.bv->workWidth();
215                         if (openinlined_) {
216                                 // Correct for button width.
217                                 dim.wid += textdim_.wid;
218                                 dim.des = max(dim.des - textdim_.asc + dim.asc, textdim_.des);
219                                 dim.asc = textdim_.asc;
220                         } else {
221                                 dim.des += textdim_.height() + TEXT_TO_BOTTOM_OFFSET;
222                                 dim.wid = max(dim.wid, textdim_.wid);
223                                 if (hasFixedWidth())
224                                         dim.wid = max(dim.wid, mi.base.textwidth);
225                         }
226                 }
227                 break;
228         }
229         dim.asc += TEXT_TO_INSET_OFFSET;
230         dim.des += TEXT_TO_INSET_OFFSET;
231         dim.wid += int(1.5 * TEXT_TO_INSET_OFFSET);
232         mi.base.textwidth += int(1.5 * TEXT_TO_INSET_OFFSET);
233         bool const changed = dim_ != dim;
234         dim_ = dim;
235         return changed;
236 }
237
238
239 bool InsetCollapsable::setMouseHover(bool mouse_hover)
240 {
241         mouse_hover_ = mouse_hover;
242         return true;
243 }
244
245
246 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
247 {
248         autoOpen_ = pi.base.bv->cursor().isInside(this);
249         text_.background_color_ = backgroundColor();
250         const int xx = x + TEXT_TO_INSET_OFFSET;
251
252         // Draw button first -- top, left or only
253         Dimension dimc = dimensionCollapsed();
254         int const top  = y - ascent() + TEXT_TO_INSET_OFFSET;
255         if (geometry() == TopButton ||
256             geometry() == LeftButton ||
257             geometry() == ButtonOnly) {
258                 button_dim.x1 = xx + 0;
259                 button_dim.x2 = xx + dimc.width();
260                 button_dim.y1 = top;
261                 button_dim.y2 = top + dimc.height();
262
263                 pi.pain.buttonText(xx, top + dimc.asc, layout_.labelstring, layout_.labelfont, mouse_hover_);
264         } else {
265                 button_dim.x1 = 0;
266                 button_dim.y1 = 0;
267                 button_dim.x2 = 0;
268                 button_dim.y2 = 0;
269         }
270
271         int textx, texty;
272         switch (geometry()) {
273         case LeftButton:
274                 textx = xx + dimc.width();
275                 texty = top + textdim_.asc;
276                 InsetText::draw(pi, textx, texty);
277                 break;
278         case TopButton:
279                 textx = xx;
280                 texty = top + dimc.height() + textdim_.asc;
281                 InsetText::draw(pi, textx, texty);
282                 break;
283         case ButtonOnly:
284                 break;
285         case NoButton:
286                 textx = xx;
287                 texty = y + textdim_.asc;
288                 InsetText::draw(pi, textx, texty);
289                 break;
290         case SubLabel:
291         case Corners:
292                 textx = xx;
293                 texty = y + textdim_.asc;
294                 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
295                 InsetText::draw(pi, textx, texty);
296                 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
297
298                 int desc = InsetText::descent();
299                 if (geometry() == SubLabel)
300                         desc -= ascent();
301                 else
302                         desc -= 3;
303
304                 const int xx1 = xx + border_ - 1;
305                 const int xx2 = x + dim_.wid - border_ 
306                         - TEXT_TO_INSET_OFFSET + 1;
307                 pi.pain.line(xx1, y + desc - 4, 
308                              xx1, y + desc, 
309                         layout_.labelfont.color());
310                 if (internalStatus() == Open)
311                         pi.pain.line(xx1, y + desc, 
312                                 xx2, y + desc,
313                                 layout_.labelfont.color());
314                 else {
315                         // Make status_ value visible:
316                         pi.pain.line(xx1, y + desc,
317                                 xx1 + 4, y + desc,
318                                 layout_.labelfont.color());
319                         pi.pain.line(xx2 - 4, y + desc,
320                                 xx2, y + desc,
321                                 layout_.labelfont.color());
322                 }
323                 pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
324                         layout_.labelfont.color());
325
326                 // the label below the text. Can be toggled.
327                 if (geometry() == SubLabel) {
328                         Font font(layout_.labelfont);
329                         font.realize(Font(Font::ALL_SANE));
330                         font.decSize();
331                         font.decSize();
332                         int w = 0;
333                         int a = 0;
334                         int d = 0;
335                         // FIXME UNICODE
336                         docstring s = layout_.labelstring;
337                         theFontMetrics(font).rectText(s, w, a, d);
338                         pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
339                                 s, font, Color::none, Color::none);
340                 }
341
342                 // a visual clue when the cursor is inside the inset
343                 Cursor & cur = pi.base.bv->cursor();
344                 if (cur.isInside(this)) {
345                         y -= ascent();
346                         y += 3;
347                         pi.pain.line(xx1, y + 4, xx1, y, layout_.labelfont.color());
348                         pi.pain.line(xx1 + 4, y, xx1, y, layout_.labelfont.color());
349                         pi.pain.line(xx2, y + 4, x + dim_.wid - 3, y,
350                                 layout_.labelfont.color());
351                         pi.pain.line(xx2 - 4, y, xx2, y,
352                                 layout_.labelfont.color());
353                 }
354                 break;
355         }
356         setPosCache(pi, x, y);
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 + textdim_.asc;
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                 y += dimensionCollapsed().height() - ascent()
396                         + TEXT_TO_INSET_OFFSET + textdim_.asc;
397                 break;
398         case NoButton:
399         case SubLabel:
400         case Corners:
401                 // Do nothing
402                 break;
403         case ButtonOnly:
404                 // Cannot get here
405                 break;
406         }
407         x += TEXT_TO_INSET_OFFSET;
408 }
409
410
411 Inset::EDITABLE InsetCollapsable::editable() const
412 {
413         return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
414 }
415
416
417 bool InsetCollapsable::descendable() const
418 {
419         return geometry() != ButtonOnly;
420 }
421
422
423 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
424 {
425         return button_dim.contains(cmd.x, cmd.y);
426 }
427
428
429 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
430 {
431         docstring label;
432         pos_type const max_length = 15;
433         pos_type const p_siz = paragraphs().begin()->size();
434         pos_type const n = std::min(max_length, p_siz);
435         pos_type i = 0;
436         pos_type j = 0;
437         for (; i < n && j < p_siz; ++j) {
438                 if (paragraphs().begin()->isInset(j))
439                         continue;
440                 label += paragraphs().begin()->getChar(j);
441                 ++i;
442         }
443         if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
444                 label += "...";
445         }
446         return label.empty() ? l : label;
447 }
448
449
450 void InsetCollapsable::edit(Cursor & cur, bool left)
451 {
452         //lyxerr << "InsetCollapsable: edit left/right" << endl;
453         cur.push(*this);
454         InsetText::edit(cur, left);
455 }
456
457
458 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
459 {
460         //lyxerr << "InsetCollapsable: edit xy" << endl;
461         if (geometry() == ButtonOnly
462          || (button_dim.contains(x, y) 
463           && geometry() != NoButton))
464                 return this;
465         cur.push(*this);
466         return InsetText::editXY(cur, x, y);
467 }
468
469
470 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
471 {
472         //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
473         //      << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
474
475         switch (cmd.action) {
476         case LFUN_MOUSE_PRESS:
477                 if (cmd.button() == mouse_button::button1 
478                  && hitButton(cmd) 
479                  && geometry() != NoButton) {
480                         // reset selection if necessary (see bug 3060)
481                         if (cur.selection())
482                                 cur.bv().cursor().clearSelection();
483                         else
484                                 cur.noUpdate();
485                         cur.dispatched();
486                         break;
487                 }
488                 if (geometry() == NoButton)
489                         InsetText::doDispatch(cur, cmd);
490                 else if (geometry() != ButtonOnly 
491                      && !hitButton(cmd))
492                         InsetText::doDispatch(cur, cmd);
493                 else
494                         cur.undispatched();
495                 break;
496
497         case LFUN_MOUSE_MOTION:
498         case LFUN_MOUSE_DOUBLE:
499         case LFUN_MOUSE_TRIPLE:
500                 if (geometry() == NoButton)
501                         InsetText::doDispatch(cur, cmd);
502                 else if (geometry() != ButtonOnly
503                      && !hitButton(cmd))
504                         InsetText::doDispatch(cur, cmd);
505                 else
506                         cur.undispatched();
507                 break;
508
509         case LFUN_MOUSE_RELEASE:
510                 if (cmd.button() == mouse_button::button3) {
511                         // There is no button to right click:
512                         if (geometry() == Corners ||
513                             geometry() == SubLabel ||
514                             geometry() == NoButton)  {
515                                 if (internalStatus() == Open)
516                                         setStatus(cur, Collapsed);
517                                 else
518                                         setStatus(cur, Open);
519                                 break;
520                         } else {
521                                 // Open the Inset 
522                                 // configuration dialog
523                                 showInsetDialog(&cur.bv());
524                                 break;
525                         }
526                 }
527
528                 if (geometry() == NoButton) {
529                         // The mouse click has to be within the inset!
530                         InsetText::doDispatch(cur, cmd);
531                         break;
532                 }
533
534                 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
535                         // if we are selecting, we do not want to
536                         // toggle the inset.
537                         if (cur.selection())
538                                 break;
539                         // Left button is clicked, the user asks to
540                         // toggle the inset visual state.
541                         cur.dispatched();
542                         cur.updateFlags(Update::Force | Update::FitCursor);
543                         if (geometry() == ButtonOnly) {
544                                 setStatus(cur, Open);
545                                 edit(cur, true);
546                         }
547                         else {
548                                 setStatus(cur, Collapsed);
549                         }
550                         cur.bv().cursor() = cur;
551                         break;
552                 }
553
554                 // The mouse click is within the opened inset.
555                 if (geometry() == TopButton
556                  || geometry() == LeftButton)
557                         InsetText::doDispatch(cur, cmd);
558                 break;
559
560         case LFUN_INSET_TOGGLE:
561                 if (cmd.argument() == "open")
562                         setStatus(cur, Open);
563                 else if (cmd.argument() == "close")
564                         setStatus(cur, Collapsed);
565                 else if (cmd.argument() == "toggle" || cmd.argument().empty())
566                         if (internalStatus() == Open) {
567                                 setStatus(cur, Collapsed);
568                                 if (geometry() == ButtonOnly)
569                                         cur.top().forwardPos();
570                         } else
571                                 setStatus(cur, Open);
572                 else // if assign or anything else
573                         cur.undispatched();
574                 cur.dispatched();
575                 break;
576
577         default:
578                 InsetText::doDispatch(cur, cmd);
579                 break;
580         }
581 }
582
583
584 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
585                 FuncStatus & flag) const
586 {
587         switch (cmd.action) {
588
589         case LFUN_INSET_TOGGLE:
590                 if (cmd.argument() == "open" || cmd.argument() == "close" ||
591                     cmd.argument() == "toggle")
592                         flag.enabled(true);
593                 else
594                         flag.enabled(false);
595                 return true;
596
597         default:
598                 return InsetText::getStatus(cur, cmd, flag);
599         }
600 }
601
602
603 void InsetCollapsable::setLabel(docstring const & l)
604 {
605         layout_.labelstring = l;
606 }
607
608
609 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
610 {
611         status_ = status;
612         setButtonLabel();
613         if (status_ == Collapsed)
614                 cur.leaveInset(*this);
615 }
616
617
618 void InsetCollapsable::setLabelFont(Font const & font)
619 {
620         layout_.labelfont = font;
621 }
622
623 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
624 {
625         FloatList const & floats = bp.getTextClass().floats();
626         FloatList::const_iterator it = floats[type];
627         // FIXME UNICODE
628         return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
629 }
630
631
632 InsetCollapsable::Decoration InsetCollapsable::decoration() const
633 {
634         if (layout_.decoration == "classic")
635                 return Classic;
636         if (layout_.decoration == "minimalistic")
637                 return Minimalistic;
638         if (layout_.decoration == "conglomerate")
639                 return Conglomerate;
640         if (name() == from_ascii("CharStyle"))
641                 return Conglomerate;
642         return Classic;
643 }
644
645
646 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
647                           OutputParams const & runparams) const
648 {
649         // This implements the standard way of handling the LaTeX output of
650         // a collapsable inset, either a command or an environment. Standard 
651         // collapsable insets should not redefine this, non-standard ones may
652         // call this.
653         if (!layout_.latexname.empty()) {
654                 if (layout_.latextype == "command") {
655                         // FIXME UNICODE
656                         os << '\\' << from_utf8(layout_.latexname);
657                         if (!layout_.latexparam.empty())
658                                 os << from_utf8(layout_.latexparam);
659                         os << '{';
660                 } else if (layout_.latextype == "environment") {
661                         os << "%\n\\begin{" << from_utf8(layout_.latexname) << "}\n";
662                         if (!layout_.latexparam.empty())
663                                 os << from_utf8(layout_.latexparam);
664                 }
665         }
666         int i = InsetText::latex(buf, os, runparams);
667         if (!layout_.latexname.empty())
668                 if (layout_.latextype == "command") {
669                         os << "}";
670                 } else if (layout_.latextype == "environment") {
671                         os << "\n\\end{" << from_utf8(layout_.latexname) << "}\n";
672                         i += 4;
673                 }
674         return i;
675 }
676
677
678 void InsetCollapsable::validate(LaTeXFeatures & features) const
679 {
680         // Force inclusion of preamble snippet in layout file
681         features.addPreambleSnippet(layout_.preamble);
682         InsetText::validate(features);
683 }
684
685
686 } // namespace lyx