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