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