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