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