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