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