]> git.lyx.org Git - features.git/blob - src/insets/InsetCollapsable.cpp
Remove warnings reported with gcc 4.3:
[features.git] / src / insets / InsetCollapsable.cpp
1 /**
2  * \file InsetCollapsable.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author Jürgen Vigna
8  * \author Lars Gullik Bjønnes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "InsetCollapsable.h"
16
17 #include "Buffer.h"
18 #include "BufferParams.h"
19 #include "BufferView.h"
20 #include "Cursor.h"
21 #include "debug.h"
22 #include "DispatchResult.h"
23 #include "FloatList.h"
24 #include "FuncStatus.h"
25 #include "gettext.h"
26 #include "Color.h"
27 #include "LaTeXFeatures.h"
28 #include "Lexer.h"
29 #include "FuncRequest.h"
30 #include "MetricsInfo.h"
31
32 #include "frontends/FontMetrics.h"
33 #include "frontends/Painter.h"
34
35
36 namespace lyx {
37
38 using std::endl;
39 using std::string;
40 using std::ostream;
41
42
43 InsetCollapsable::CollapseStatus InsetCollapsable::status() const
44 {
45         return autoOpen_ ? Open : status_;
46 }
47
48
49 InsetCollapsable::Geometry InsetCollapsable::geometry() const
50 {
51         switch (decoration()) {
52         case Classic:
53                 if (status() == Open) {
54                         if (openinlined_)
55                                 return LeftButton;
56                         else
57                                 return TopButton;
58                 } else
59                         return ButtonOnly;
60
61         case Minimalistic:
62                 return 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         int const baseline = y;
271         int textx, texty;
272         switch (geometry()) {
273         case LeftButton:
274                 textx = xx + dimc.width();
275                 texty = baseline;
276                 InsetText::draw(pi, textx, texty);
277                 break;
278         case TopButton:
279                 textx = xx;
280                 texty = baseline + dimc.height();
281                 InsetText::draw(pi, textx, texty);
282                 break;
283         case ButtonOnly:
284                 break;
285         case NoButton:
286                 textx = xx;
287                 texty = baseline;
288                 InsetText::draw(pi, textx, texty);
289                 break;
290         case SubLabel:
291         case Corners:
292                 textx = xx;
293                 texty = baseline;
294                 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
295                 InsetText::draw(pi, textx, texty);
296                 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
297
298                 int desc = InsetText::descent();
299                 if (geometry() == SubLabel)
300                         desc -= ascent();
301                 else
302                         desc -= 3;
303
304                 const int xx1 = xx + border_ - 1;
305                 const int xx2 = x + dim_.wid - border_ 
306                         - TEXT_TO_INSET_OFFSET + 1;
307                 pi.pain.line(xx1, y + desc - 4, 
308                              xx1, y + desc, 
309                         layout_.labelfont.color());
310                 if (internalStatus() == Open)
311                         pi.pain.line(xx1, y + desc, 
312                                 xx2, y + desc,
313                                 layout_.labelfont.color());
314                 else {
315                         // Make status_ value visible:
316                         pi.pain.line(xx1, y + desc,
317                                 xx1 + 4, y + desc,
318                                 layout_.labelfont.color());
319                         pi.pain.line(xx2 - 4, y + desc,
320                                 xx2, y + desc,
321                                 layout_.labelfont.color());
322                 }
323                 pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
324                         layout_.labelfont.color());
325
326                 // the label below the text. Can be toggled.
327                 if (geometry() == SubLabel) {
328                         Font font(layout_.labelfont);
329                         font.realize(Font(Font::ALL_SANE));
330                         font.decSize();
331                         font.decSize();
332                         int w = 0;
333                         int a = 0;
334                         int d = 0;
335                         // FIXME UNICODE
336                         docstring s = layout_.labelstring;
337                         theFontMetrics(font).rectText(s, w, a, d);
338                         pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
339                                 s, font, Color::none, Color::none);
340                 }
341
342                 // a visual clue when the cursor is inside the inset
343                 Cursor & cur = pi.base.bv->cursor();
344                 if (cur.isInside(this)) {
345                         y -= ascent();
346                         y += 3;
347                         pi.pain.line(xx1, y + 4, xx1, y, layout_.labelfont.color());
348                         pi.pain.line(xx1 + 4, y, xx1, y, layout_.labelfont.color());
349                         pi.pain.line(xx2, y + 4, x + dim_.wid - 3, y,
350                                 layout_.labelfont.color());
351                         pi.pain.line(xx2 - 4, y, xx2, y,
352                                 layout_.labelfont.color());
353                 }
354                 break;
355         }
356         setPosCache(pi, x, y);
357         pi.background_color = old_color;
358 }
359
360
361 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
362 {
363         x += TEXT_TO_INSET_OFFSET;
364         switch (geometry()) {
365         case LeftButton:
366                 x += dimensionCollapsed().wid;
367                 InsetText::drawSelection(pi, x, y);
368                 break;
369         case TopButton:
370                 y += dimensionCollapsed().des;
371                 InsetText::drawSelection(pi, x, y);
372                 break;
373         case ButtonOnly:
374                 break;
375         case NoButton:
376         case SubLabel:
377         case Corners:
378                 InsetText::drawSelection(pi, x, y);
379                 break;
380         }
381 }
382
383
384 void InsetCollapsable::cursorPos(BufferView const & bv,
385                 CursorSlice const & sl, bool boundary, int & x, int & y) const
386 {
387         BOOST_ASSERT(geometry() != ButtonOnly);
388
389         InsetText::cursorPos(bv, sl, boundary, x, y);
390
391         switch (geometry()) {
392         case LeftButton:
393                 x += dimensionCollapsed().wid;
394                 break;
395         case TopButton: {
396                 TextMetrics const & tm = bv.textMetrics(&text_);
397                 y += dimensionCollapsed().height() - ascent()
398                         + TEXT_TO_INSET_OFFSET + tm.ascent();
399                 break;
400         }
401         case NoButton:
402         case SubLabel:
403         case Corners:
404                 // Do nothing
405                 break;
406         case ButtonOnly:
407                 // Cannot get here
408                 break;
409         }
410         x += TEXT_TO_INSET_OFFSET;
411 }
412
413
414 Inset::EDITABLE InsetCollapsable::editable() const
415 {
416         return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
417 }
418
419
420 bool InsetCollapsable::descendable() const
421 {
422         return geometry() != ButtonOnly;
423 }
424
425
426 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
427 {
428         return button_dim.contains(cmd.x, cmd.y);
429 }
430
431
432 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
433 {
434         docstring label;
435         pos_type const max_length = 15;
436         pos_type const p_siz = paragraphs().begin()->size();
437         pos_type const n = std::min(max_length, p_siz);
438         pos_type i = 0;
439         pos_type j = 0;
440         for (; i < n && j < p_siz; ++j) {
441                 if (paragraphs().begin()->isInset(j))
442                         continue;
443                 label += paragraphs().begin()->getChar(j);
444                 ++i;
445         }
446         if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
447                 label += "...";
448         }
449         return label.empty() ? l : label;
450 }
451
452
453 void InsetCollapsable::edit(Cursor & cur, bool left)
454 {
455         //lyxerr << "InsetCollapsable: edit left/right" << endl;
456         cur.push(*this);
457         InsetText::edit(cur, left);
458 }
459
460
461 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
462 {
463         //lyxerr << "InsetCollapsable: edit xy" << endl;
464         if (geometry() == ButtonOnly
465          || (button_dim.contains(x, y) 
466           && geometry() != NoButton))
467                 return this;
468         cur.push(*this);
469         return InsetText::editXY(cur, x, y);
470 }
471
472
473 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
474 {
475         //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
476         //      << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
477
478         switch (cmd.action) {
479         case LFUN_MOUSE_PRESS:
480                 if (cmd.button() == mouse_button::button1 
481                  && hitButton(cmd) 
482                  && geometry() != NoButton) {
483                         // reset selection if necessary (see bug 3060)
484                         if (cur.selection())
485                                 cur.bv().cursor().clearSelection();
486                         else
487                                 cur.noUpdate();
488                         cur.dispatched();
489                         break;
490                 }
491                 if (geometry() == NoButton)
492                         InsetText::doDispatch(cur, cmd);
493                 else if (geometry() != ButtonOnly 
494                      && !hitButton(cmd))
495                         InsetText::doDispatch(cur, cmd);
496                 else
497                         cur.undispatched();
498                 break;
499
500         case LFUN_MOUSE_MOTION:
501         case LFUN_MOUSE_DOUBLE:
502         case LFUN_MOUSE_TRIPLE:
503                 if (geometry() == NoButton)
504                         InsetText::doDispatch(cur, cmd);
505                 else if (geometry() != ButtonOnly
506                      && !hitButton(cmd))
507                         InsetText::doDispatch(cur, cmd);
508                 else
509                         cur.undispatched();
510                 break;
511
512         case LFUN_MOUSE_RELEASE:
513                 if (cmd.button() == mouse_button::button3) {
514                         // There is no button to right click:
515                         if (geometry() == Corners ||
516                             geometry() == SubLabel ||
517                             geometry() == NoButton)  {
518                                 if (internalStatus() == Open)
519                                         setStatus(cur, Collapsed);
520                                 else
521                                         setStatus(cur, Open);
522                                 break;
523                         } else {
524                                 // Open the Inset 
525                                 // configuration dialog
526                                 showInsetDialog(&cur.bv());
527                                 break;
528                         }
529                 }
530
531                 if (geometry() == NoButton) {
532                         // The mouse click has to be within the inset!
533                         InsetText::doDispatch(cur, cmd);
534                         break;
535                 }
536
537                 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
538                         // if we are selecting, we do not want to
539                         // toggle the inset.
540                         if (cur.selection())
541                                 break;
542                         // Left button is clicked, the user asks to
543                         // toggle the inset visual state.
544                         cur.dispatched();
545                         cur.updateFlags(Update::Force | Update::FitCursor);
546                         if (geometry() == ButtonOnly) {
547                                 setStatus(cur, Open);
548                                 edit(cur, true);
549                         }
550                         else {
551                                 setStatus(cur, Collapsed);
552                         }
553                         cur.bv().cursor() = cur;
554                         break;
555                 }
556
557                 // The mouse click is within the opened inset.
558                 if (geometry() == TopButton
559                  || geometry() == LeftButton)
560                         InsetText::doDispatch(cur, cmd);
561                 break;
562
563         case LFUN_INSET_TOGGLE:
564                 if (cmd.argument() == "open")
565                         setStatus(cur, Open);
566                 else if (cmd.argument() == "close")
567                         setStatus(cur, Collapsed);
568                 else if (cmd.argument() == "toggle" || cmd.argument().empty())
569                         if (internalStatus() == Open) {
570                                 setStatus(cur, Collapsed);
571                                 if (geometry() == ButtonOnly)
572                                         cur.top().forwardPos();
573                         } else
574                                 setStatus(cur, Open);
575                 else // if assign or anything else
576                         cur.undispatched();
577                 cur.dispatched();
578                 break;
579
580         default:
581                 InsetText::doDispatch(cur, cmd);
582                 break;
583         }
584 }
585
586
587 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
588                 FuncStatus & flag) const
589 {
590         switch (cmd.action) {
591
592         case LFUN_INSET_TOGGLE:
593                 if (cmd.argument() == "open" || cmd.argument() == "close" ||
594                     cmd.argument() == "toggle")
595                         flag.enabled(true);
596                 else
597                         flag.enabled(false);
598                 return true;
599
600         default:
601                 return InsetText::getStatus(cur, cmd, flag);
602         }
603 }
604
605
606 void InsetCollapsable::setLabel(docstring const & l)
607 {
608         layout_.labelstring = l;
609 }
610
611
612 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
613 {
614         status_ = status;
615         setButtonLabel();
616         if (status_ == Collapsed)
617                 cur.leaveInset(*this);
618 }
619
620
621 void InsetCollapsable::setLabelFont(Font const & font)
622 {
623         layout_.labelfont = font;
624 }
625
626 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
627 {
628         FloatList const & floats = bp.getTextClass().floats();
629         FloatList::const_iterator it = floats[type];
630         // FIXME UNICODE
631         return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
632 }
633
634
635 InsetCollapsable::Decoration InsetCollapsable::decoration() const
636 {
637         if (layout_.decoration == "classic")
638                 return Classic;
639         if (layout_.decoration == "minimalistic")
640                 return Minimalistic;
641         if (layout_.decoration == "conglomerate")
642                 return Conglomerate;
643         if (name() == from_ascii("Flex"))
644                 return Conglomerate;
645         return Classic;
646 }
647
648
649 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
650                           OutputParams const & runparams) const
651 {
652         // This implements the standard way of handling the LaTeX output of
653         // a collapsable inset, either a command or an environment. Standard 
654         // collapsable insets should not redefine this, non-standard ones may
655         // call this.
656         if (!layout_.latexname.empty()) {
657                 if (layout_.latextype == "command") {
658                         // FIXME UNICODE
659                         os << '\\' << from_utf8(layout_.latexname);
660                         if (!layout_.latexparam.empty())
661                                 os << from_utf8(layout_.latexparam);
662                         os << '{';
663                 } else if (layout_.latextype == "environment") {
664                         os << "%\n\\begin{" << from_utf8(layout_.latexname) << "}\n";
665                         if (!layout_.latexparam.empty())
666                                 os << from_utf8(layout_.latexparam);
667                 }
668         }
669         int i = InsetText::latex(buf, os, runparams);
670         if (!layout_.latexname.empty()) {
671                 if (layout_.latextype == "command") {
672                         os << "}";
673                 } else if (layout_.latextype == "environment") {
674                         os << "\n\\end{" << from_utf8(layout_.latexname) << "}\n";
675                         i += 4;
676                 }
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