]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCollapsable.cpp
Polish
[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 -= 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                 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                         }
220                 }
221                 break;
222         }
223
224         bool const changed = dim_ != dim;
225         dim_ = dim;
226         return changed;
227 }
228
229
230 bool InsetCollapsable::setMouseHover(bool mouse_hover)
231 {
232         mouse_hover_ = mouse_hover;
233         return true;
234 }
235
236
237 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
238 {
239         autoOpen_ = pi.base.bv->cursor().isInside(this);
240         int const old_color = pi.background_color;
241         pi.background_color = backgroundColor();
242
243         // Draw button first -- top, left or only
244         Dimension dimc = dimensionCollapsed();
245
246         if (geometry() == TopButton ||
247             geometry() == LeftButton ||
248             geometry() == ButtonOnly) {
249                 button_dim.x1 = x + 0;
250                 button_dim.x2 = x + dimc.width();
251                 button_dim.y1 = y - dimc.asc;
252                 button_dim.y2 = y + dimc.des;
253
254                 pi.pain.buttonText(x, y, layout_.labelstring, layout_.labelfont, mouse_hover_);
255         } else {
256                 button_dim.x1 = 0;
257                 button_dim.y1 = 0;
258                 button_dim.x2 = 0;
259                 button_dim.y2 = 0;
260         }
261
262         Dimension const textdim = InsetText::dimension(*pi.base.bv);
263         int const baseline = y;
264         int textx, texty;
265         switch (geometry()) {
266         case LeftButton:
267                 textx = x + dimc.width();
268                 texty = baseline;
269                 InsetText::draw(pi, textx, texty);
270                 break;
271         case TopButton:
272                 textx = x;
273                 texty = baseline + dimc.des + textdim.asc;
274                 InsetText::draw(pi, textx, texty);
275                 break;
276         case ButtonOnly:
277                 break;
278         case NoButton:
279                 textx = x;
280                 texty = baseline;
281                 InsetText::draw(pi, textx, texty);
282                 break;
283         case SubLabel:
284         case Corners:
285                 textx = x;
286                 texty = baseline;
287                 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
288                 InsetText::draw(pi, textx, texty);
289                 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
290
291                 int desc = InsetText::descent();
292                 if (geometry() == SubLabel)
293                         desc -= ascent();
294                 else
295                         desc -= 3;
296
297                 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
298                 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
299                 pi.pain.line(xx1, y + desc - 4, 
300                              xx1, y + desc, 
301                         layout_.labelfont.color());
302                 if (internalStatus() == Open)
303                         pi.pain.line(xx1, y + desc, 
304                                 xx2, y + desc,
305                                 layout_.labelfont.color());
306                 else {
307                         // Make status_ value visible:
308                         pi.pain.line(xx1, y + desc,
309                                 xx1 + 4, y + desc,
310                                 layout_.labelfont.color());
311                         pi.pain.line(xx2 - 4, y + desc,
312                                 xx2, y + desc,
313                                 layout_.labelfont.color());
314                 }
315                 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3, y + desc - 4,
316                         layout_.labelfont.color());
317
318                 // the label below the text. Can be toggled.
319                 if (geometry() == SubLabel) {
320                         Font font(layout_.labelfont);
321                         font.realize(Font(Font::ALL_SANE));
322                         font.decSize();
323                         font.decSize();
324                         int w = 0;
325                         int a = 0;
326                         int d = 0;
327                         docstring s = layout_.labelstring;
328                         theFontMetrics(font).rectText(s, w, a, d);
329                         pi.pain.rectText(x + (textdim.wid - w) / 2, y + desc + a,
330                                 s, font, Color::none, Color::none);
331                 }
332
333                 // a visual cue when the cursor is inside the inset
334                 Cursor & cur = pi.base.bv->cursor();
335                 if (cur.isInside(this)) {
336                         y -= ascent();
337                         y += 3;
338                         pi.pain.line(xx1, y + 4, xx1, y, layout_.labelfont.color());
339                         pi.pain.line(xx1 + 4, y, xx1, y, layout_.labelfont.color());
340                         pi.pain.line(xx2, y + 4, xx2, y,
341                                 layout_.labelfont.color());
342                         pi.pain.line(xx2 - 4, y, xx2, y,
343                                 layout_.labelfont.color());
344                 }
345                 break;
346         }
347         pi.background_color = old_color;
348 }
349
350
351 void InsetCollapsable::cursorPos(BufferView const & bv,
352                 CursorSlice const & sl, bool boundary, int & x, int & y) const
353 {
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