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