]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCollapsable.cpp
remove InsetCollapsable::drawSelection(): this was not used.
[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::cursorPos(BufferView const & bv,
355                 CursorSlice const & sl, bool boundary, int & x, int & y) const
356 {
357         BOOST_ASSERT(geometry() != ButtonOnly);
358
359         InsetText::cursorPos(bv, sl, boundary, x, y);
360         Dimension const textdim = InsetText::dimension(bv);
361
362         switch (geometry()) {
363         case LeftButton:
364                 x += dimensionCollapsed().wid;
365                 break;
366         case TopButton: {
367                 y += dimensionCollapsed().des + textdim.asc;
368                 break;
369         }
370         case NoButton:
371         case SubLabel:
372         case Corners:
373                 // Do nothing
374                 break;
375         case ButtonOnly:
376                 // Cannot get here
377                 break;
378         }
379 }
380
381
382 Inset::EDITABLE InsetCollapsable::editable() const
383 {
384         return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
385 }
386
387
388 bool InsetCollapsable::descendable() const
389 {
390         return geometry() != ButtonOnly;
391 }
392
393
394 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
395 {
396         return button_dim.contains(cmd.x, cmd.y);
397 }
398
399
400 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
401 {
402         docstring label;
403         pos_type const max_length = 15;
404         pos_type const p_siz = paragraphs().begin()->size();
405         pos_type const n = std::min(max_length, p_siz);
406         pos_type i = 0;
407         pos_type j = 0;
408         for (; i < n && j < p_siz; ++j) {
409                 if (paragraphs().begin()->isInset(j))
410                         continue;
411                 label += paragraphs().begin()->getChar(j);
412                 ++i;
413         }
414         if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
415                 label += "...";
416         }
417         return label.empty() ? l : label;
418 }
419
420
421 void InsetCollapsable::edit(Cursor & cur, bool left)
422 {
423         //lyxerr << "InsetCollapsable: edit left/right" << endl;
424         cur.push(*this);
425         InsetText::edit(cur, left);
426 }
427
428
429 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
430 {
431         //lyxerr << "InsetCollapsable: edit xy" << endl;
432         if (geometry() == ButtonOnly
433          || (button_dim.contains(x, y) 
434           && geometry() != NoButton))
435                 return this;
436         cur.push(*this);
437         return InsetText::editXY(cur, x, y);
438 }
439
440
441 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
442 {
443         //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
444         //      << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
445
446         switch (cmd.action) {
447         case LFUN_MOUSE_PRESS:
448                 if (cmd.button() == mouse_button::button1 
449                  && hitButton(cmd) 
450                  && geometry() != NoButton) {
451                         // reset selection if necessary (see bug 3060)
452                         if (cur.selection())
453                                 cur.bv().cursor().clearSelection();
454                         else
455                                 cur.noUpdate();
456                         cur.dispatched();
457                         break;
458                 }
459                 if (geometry() == NoButton)
460                         InsetText::doDispatch(cur, cmd);
461                 else if (geometry() != ButtonOnly 
462                      && !hitButton(cmd))
463                         InsetText::doDispatch(cur, cmd);
464                 else
465                         cur.undispatched();
466                 break;
467
468         case LFUN_MOUSE_MOTION:
469         case LFUN_MOUSE_DOUBLE:
470         case LFUN_MOUSE_TRIPLE:
471                 if (geometry() == NoButton)
472                         InsetText::doDispatch(cur, cmd);
473                 else if (geometry() != ButtonOnly
474                      && !hitButton(cmd))
475                         InsetText::doDispatch(cur, cmd);
476                 else
477                         cur.undispatched();
478                 break;
479
480         case LFUN_MOUSE_RELEASE:
481                 if (cmd.button() == mouse_button::button3) {
482                         // There is no button to right click:
483                         if (geometry() == Corners ||
484                             geometry() == SubLabel ||
485                             geometry() == NoButton)  {
486                                 if (internalStatus() == Open)
487                                         setStatus(cur, Collapsed);
488                                 else
489                                         setStatus(cur, Open);
490                                 break;
491                         } else {
492                                 // Open the Inset 
493                                 // configuration dialog
494                                 showInsetDialog(&cur.bv());
495                                 break;
496                         }
497                 }
498
499                 if (geometry() == NoButton) {
500                         // The mouse click has to be within the inset!
501                         InsetText::doDispatch(cur, cmd);
502                         break;
503                 }
504
505                 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
506                         // if we are selecting, we do not want to
507                         // toggle the inset.
508                         if (cur.selection())
509                                 break;
510                         // Left button is clicked, the user asks to
511                         // toggle the inset visual state.
512                         cur.dispatched();
513                         cur.updateFlags(Update::Force | Update::FitCursor);
514                         if (geometry() == ButtonOnly) {
515                                 setStatus(cur, Open);
516                                 edit(cur, true);
517                         }
518                         else {
519                                 setStatus(cur, Collapsed);
520                         }
521                         cur.bv().cursor() = cur;
522                         break;
523                 }
524
525                 // The mouse click is within the opened inset.
526                 if (geometry() == TopButton
527                  || geometry() == LeftButton)
528                         InsetText::doDispatch(cur, cmd);
529                 break;
530
531         case LFUN_INSET_TOGGLE:
532                 if (cmd.argument() == "open")
533                         setStatus(cur, Open);
534                 else if (cmd.argument() == "close")
535                         setStatus(cur, Collapsed);
536                 else if (cmd.argument() == "toggle" || cmd.argument().empty())
537                         if (internalStatus() == Open) {
538                                 setStatus(cur, Collapsed);
539                                 if (geometry() == ButtonOnly)
540                                         cur.top().forwardPos();
541                         } else
542                                 setStatus(cur, Open);
543                 else // if assign or anything else
544                         cur.undispatched();
545                 cur.dispatched();
546                 break;
547
548         default:
549                 InsetText::doDispatch(cur, cmd);
550                 break;
551         }
552 }
553
554
555 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
556                 FuncStatus & flag) const
557 {
558         switch (cmd.action) {
559
560         case LFUN_INSET_TOGGLE:
561                 if (cmd.argument() == "open" || cmd.argument() == "close" ||
562                     cmd.argument() == "toggle")
563                         flag.enabled(true);
564                 else
565                         flag.enabled(false);
566                 return true;
567
568         default:
569                 return InsetText::getStatus(cur, cmd, flag);
570         }
571 }
572
573
574 void InsetCollapsable::setLabel(docstring const & l)
575 {
576         layout_.labelstring = l;
577 }
578
579
580 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
581 {
582         status_ = status;
583         setButtonLabel();
584         if (status_ == Collapsed)
585                 cur.leaveInset(*this);
586 }
587
588
589 void InsetCollapsable::setLabelFont(Font const & font)
590 {
591         layout_.labelfont = font;
592 }
593
594 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
595 {
596         FloatList const & floats = bp.getTextClass().floats();
597         FloatList::const_iterator it = floats[type];
598         // FIXME UNICODE
599         return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
600 }
601
602
603 InsetCollapsable::Decoration InsetCollapsable::decoration() const
604 {
605         if (layout_.decoration == "classic")
606                 return Classic;
607         if (layout_.decoration == "minimalistic")
608                 return Minimalistic;
609         if (layout_.decoration == "conglomerate")
610                 return Conglomerate;
611         if (name() == from_ascii("Flex"))
612                 return Conglomerate;
613         return Classic;
614 }
615
616
617 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
618                           OutputParams const & runparams) const
619 {
620         // This implements the standard way of handling the LaTeX output of
621         // a collapsable inset, either a command or an environment. Standard 
622         // collapsable insets should not redefine this, non-standard ones may
623         // call this.
624         if (!layout_.latexname.empty()) {
625                 if (layout_.latextype == "command") {
626                         // FIXME UNICODE
627                         os << '\\' << from_utf8(layout_.latexname);
628                         if (!layout_.latexparam.empty())
629                                 os << from_utf8(layout_.latexparam);
630                         os << '{';
631                 } else if (layout_.latextype == "environment") {
632                         os << "%\n\\begin{" << from_utf8(layout_.latexname) << "}\n";
633                         if (!layout_.latexparam.empty())
634                                 os << from_utf8(layout_.latexparam);
635                 }
636         }
637         int i = InsetText::latex(buf, os, runparams);
638         if (!layout_.latexname.empty()) {
639                 if (layout_.latextype == "command") {
640                         os << "}";
641                 } else if (layout_.latextype == "environment") {
642                         os << "\n\\end{" << from_utf8(layout_.latexname) << "}\n";
643                         i += 4;
644                 }
645         }
646         return i;
647 }
648
649
650 void InsetCollapsable::validate(LaTeXFeatures & features) const
651 {
652         // Force inclusion of preamble snippet in layout file
653         features.require(layout_.name);
654         InsetText::validate(features);
655 }
656
657
658 } // namespace lyx