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