]> git.lyx.org Git - features.git/blob - src/insets/InsetCollapsable.cpp
Remove nonsense assert.
[features.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 "Dimension.h"
22 #include "DispatchResult.h"
23 #include "FloatList.h"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
26 #include "InsetLayout.h"
27 #include "InsetList.h"
28 #include "Language.h"
29 #include "Lexer.h"
30 #include "MetricsInfo.h"
31 #include "output_xhtml.h"
32 #include "OutputParams.h"
33 #include "paragraph_funcs.h"
34 #include "ParagraphParameters.h"
35 #include "sgml.h"
36 #include "TextClass.h"
37
38 #include "frontends/FontMetrics.h"
39 #include "frontends/Painter.h"
40
41 #include "support/debug.h"
42 #include "support/docstream.h"
43 #include "support/gettext.h"
44 #include "support/lassert.h"
45 #include "support/lstrings.h"
46
47 using namespace std;
48
49
50 namespace lyx {
51
52 InsetCollapsable::InsetCollapsable(Buffer const & buf, InsetText::UsePlain ltype)
53         : InsetText(buf, ltype), status_(Inset::Open),
54           openinlined_(false), mouse_hover_(false)
55 {
56         setAutoBreakRows(true);
57         setDrawFrame(true);
58         setFrameColor(Color_collapsableframe);
59 }
60
61
62 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
63         : InsetText(rhs),
64           status_(rhs.status_),
65           labelstring_(rhs.labelstring_),
66           button_dim(rhs.button_dim),
67           openinlined_(rhs.openinlined_),
68           auto_open_(rhs.auto_open_),
69           // the sole purpose of this copy constructor
70           mouse_hover_(false)
71 {
72 }
73
74
75 InsetCollapsable::CollapseStatus InsetCollapsable::status(BufferView const & bv) const
76 {
77         if (decoration() == InsetLayout::CONGLOMERATE)
78                 return status_;
79         return auto_open_[&bv] ? Open : status_;
80 }
81
82
83 InsetCollapsable::Geometry InsetCollapsable::geometry(BufferView const & bv) const
84 {
85         switch (decoration()) {
86         case InsetLayout::CLASSIC:
87                 if (status(bv) == Open)
88                         return openinlined_ ? LeftButton : TopButton;
89                 return ButtonOnly;
90
91         case InsetLayout::MINIMALISTIC:
92                 return status(bv) == Open ? NoButton : ButtonOnly ;
93
94         case InsetLayout::CONGLOMERATE:
95                 return status(bv) == Open ? SubLabel : Corners ;
96
97         case InsetLayout::DEFAULT:
98                 break; // this shouldn't happen
99         }
100
101         // dummy return value to shut down a warning,
102         // this is dead code.
103         return NoButton;
104 }
105
106
107 InsetCollapsable::Geometry InsetCollapsable::geometry() const
108 {
109         switch (decoration()) {
110         case InsetLayout::CLASSIC:
111                 if (status_ == Open)
112                         return openinlined_ ? LeftButton : TopButton;
113                 return ButtonOnly;
114
115         case InsetLayout::MINIMALISTIC:
116                 return status_ == Open ? NoButton : ButtonOnly ;
117
118         case InsetLayout::CONGLOMERATE:
119                 return status_ == Open ? SubLabel : Corners ;
120
121         case InsetLayout::DEFAULT:
122                 break; // this shouldn't happen
123         }
124
125         // dummy return value to shut down a warning,
126         // this is dead code.
127         return NoButton;
128 }
129
130
131 docstring InsetCollapsable::toolTip(BufferView const & bv, int x, int y) const
132 {
133         Dimension dim = dimensionCollapsed(bv);
134         if (geometry(bv) == NoButton)
135                 return translateIfPossible(getLayout().labelstring());
136         if (x > xo(bv) + dim.wid || y > yo(bv) + dim.des || isOpen(bv))
137                 return docstring();
138
139         OutputParams rp(&buffer().params().encoding());
140         odocstringstream ods;
141         InsetText::plaintext(ods, rp);
142         docstring const content_tip = ods.str();
143         return support::wrapParas(content_tip, 4);
144 }
145
146
147 void InsetCollapsable::write(ostream & os) const
148 {
149         os << "status ";
150         switch (status_) {
151         case Open:
152                 os << "open";
153                 break;
154         case Collapsed:
155                 os << "collapsed";
156                 break;
157         }
158         os << "\n";
159         text().write(buffer(), os);
160 }
161
162
163 void InsetCollapsable::read(Lexer & lex)
164 {
165         lex.setContext("InsetCollapsable::read");
166         string tmp_token;
167         status_ = Collapsed;
168         lex >> "status" >> tmp_token;
169         if (tmp_token == "open")
170                 status_ = Open;
171
172         InsetText::read(lex);
173         setButtonLabel();
174 }
175
176
177 Dimension InsetCollapsable::dimensionCollapsed(BufferView const & bv) const
178 {
179         Dimension dim;
180         theFontMetrics(getLayout().labelfont()).buttonText(
181                 buttonLabel(bv), dim.wid, dim.asc, dim.des);
182         return dim;
183 }
184
185
186 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
187 {
188         auto_open_[mi.base.bv] =  mi.base.bv->cursor().isInside(this);
189
190         FontInfo tmpfont = mi.base.font;
191         mi.base.font = getLayout().font();
192         mi.base.font.realize(tmpfont);
193
194         BufferView const & bv = *mi.base.bv;
195
196         switch (geometry(bv)) {
197         case NoButton:
198                 InsetText::metrics(mi, dim);
199                 break;
200         case Corners:
201                 InsetText::metrics(mi, dim);
202                 dim.des -= 3;
203                 dim.asc -= 1;
204                 break;
205         case SubLabel: {
206                 InsetText::metrics(mi, dim);
207                 // consider width of the inset label
208                 FontInfo font(getLayout().labelfont());
209                 font.realize(sane_font);
210                 font.decSize();
211                 font.decSize();
212                 int w = 0;
213                 int a = 0;
214                 int d = 0;
215                 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
216                 dim.des += a + d;
217                 break;
218                 }
219         case TopButton:
220         case LeftButton:
221         case ButtonOnly:
222                 dim = dimensionCollapsed(bv);
223                 if (geometry(bv) == TopButton 
224                           || geometry(bv) == LeftButton) {
225                         Dimension textdim;
226                         InsetText::metrics(mi, textdim);
227                         openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
228                         if (openinlined_) {
229                                 // Correct for button width.
230                                 dim.wid += textdim.wid;
231                                 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
232                                 dim.asc = textdim.asc;
233                         } else {
234                                 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
235                                 dim.wid = max(dim.wid, textdim.wid);
236                         }
237                 }
238                 break;
239         }
240
241         mi.base.font = tmpfont;
242 }
243
244
245 bool InsetCollapsable::setMouseHover(bool mouse_hover)
246 {
247         mouse_hover_ = mouse_hover;
248         return true;
249 }
250
251
252 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
253 {
254         BufferView const & bv = *pi.base.bv;
255
256         auto_open_[&bv] =  bv.cursor().isInside(this);
257
258         FontInfo tmpfont = pi.base.font;
259         pi.base.font = getLayout().font();
260         pi.base.font.realize(tmpfont);
261
262         // Draw button first -- top, left or only
263         Dimension dimc = dimensionCollapsed(bv);
264
265         if (geometry(*pi.base.bv) == TopButton ||
266             geometry(*pi.base.bv) == LeftButton ||
267             geometry(*pi.base.bv) == ButtonOnly) {
268                 button_dim.x1 = x + 0;
269                 button_dim.x2 = x + dimc.width();
270                 button_dim.y1 = y - dimc.asc;
271                 button_dim.y2 = y + dimc.des;
272
273                 FontInfo labelfont = getLayout().labelfont();
274                 labelfont.setColor(labelColor());
275                 pi.pain.buttonText(x, y, buttonLabel(bv), labelfont,
276                         mouse_hover_);
277         } else {
278                 button_dim.x1 = 0;
279                 button_dim.y1 = 0;
280                 button_dim.x2 = 0;
281                 button_dim.y2 = 0;
282         }
283
284         Dimension const textdim = InsetText::dimension(bv);
285         int const baseline = y;
286         int textx, texty;
287         switch (geometry(bv)) {
288         case LeftButton:
289                 textx = x + dimc.width();
290                 texty = baseline;
291                 InsetText::draw(pi, textx, texty);
292                 break;
293         case TopButton:
294                 textx = x;
295                 texty = baseline + dimc.des + textdim.asc;
296                 InsetText::draw(pi, textx, texty);
297                 break;
298         case ButtonOnly:
299                 break;
300         case NoButton:
301                 textx = x;
302                 texty = baseline;
303                 InsetText::draw(pi, textx, texty);
304                 break;
305         case SubLabel:
306         case Corners:
307                 textx = x;
308                 texty = baseline;
309                 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
310                 InsetText::draw(pi, textx, texty);
311                 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
312
313                 int desc = textdim.descent();
314                 if (geometry(bv) == Corners)
315                         desc -= 3;
316
317                 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
318                 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
319                 pi.pain.line(xx1, y + desc - 4, 
320                              xx1, y + desc, 
321                         labelColor());
322                 if (status_ == Open)
323                         pi.pain.line(xx1, y + desc, 
324                                 xx2, y + desc,
325                                 labelColor());
326                 else {
327                         // Make status_ value visible:
328                         pi.pain.line(xx1, y + desc,
329                                 xx1 + 4, y + desc,
330                                 labelColor());
331                         pi.pain.line(xx2 - 4, y + desc,
332                                 xx2, y + desc,
333                                 labelColor());
334                 }
335                 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3, 
336                         y + desc - 4, labelColor());
337
338                 // the label below the text. Can be toggled.
339                 if (geometry(bv) == SubLabel) {
340                         FontInfo font(getLayout().labelfont());
341                         font.realize(sane_font);
342                         font.decSize();
343                         font.decSize();
344                         int w = 0;
345                         int a = 0;
346                         int d = 0;
347                         theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
348                         int const ww = max(textdim.wid, w);
349                         pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
350                                 buttonLabel(bv), font, Color_none, Color_none);
351                         desc += d;
352                 }
353
354                 // a visual cue when the cursor is inside the inset
355                 Cursor const & cur = bv.cursor();
356                 if (cur.isInside(this)) {
357                         y -= textdim.asc;
358                         y += 3;
359                         pi.pain.line(xx1, y + 4, xx1, y, labelColor());
360                         pi.pain.line(xx1 + 4, y, xx1, y, labelColor());
361                         pi.pain.line(xx2, y + 4, xx2, y,
362                                 labelColor());
363                         pi.pain.line(xx2 - 4, y, xx2, y,
364                                 labelColor());
365                 }
366                 break;
367         }
368
369         pi.base.font = tmpfont;
370 }
371
372
373 void InsetCollapsable::cursorPos(BufferView const & bv,
374                 CursorSlice const & sl, bool boundary, int & x, int & y) const
375 {
376         if (geometry(bv) == ButtonOnly)
377                 status_ = Open;
378
379         InsetText::cursorPos(bv, sl, boundary, x, y);
380         Dimension const textdim = InsetText::dimension(bv);
381
382         switch (geometry(bv)) {
383         case LeftButton:
384                 x += dimensionCollapsed(bv).wid;
385                 break;
386         case TopButton: {
387                 y += dimensionCollapsed(bv).des + textdim.asc;
388                 break;
389         }
390         case NoButton:
391         case SubLabel:
392         case Corners:
393                 // Do nothing
394                 break;
395         case ButtonOnly:
396                 // Cannot get here
397                 break;
398         }
399 }
400
401
402 bool InsetCollapsable::editable() const
403 {
404         return geometry() != ButtonOnly;
405 }
406
407
408 bool InsetCollapsable::descendable() const
409 {
410         return geometry() != ButtonOnly;
411 }
412
413
414 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
415 {
416         return button_dim.contains(cmd.x, cmd.y);
417 }
418
419
420 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
421 {
422         docstring label;
423         pos_type const max_length = 15;
424         pos_type const p_siz = paragraphs().begin()->size();
425         pos_type const n = min(max_length, p_siz);
426         pos_type i = 0;
427         pos_type j = 0;
428         for (; i < n && j < p_siz; ++j) {
429                 if (paragraphs().begin()->isInset(j))
430                         continue;
431                 label += paragraphs().begin()->getChar(j);
432                 ++i;
433         }
434         if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
435                 label += "...";
436         }
437         return label.empty() ? l : label;
438 }
439
440
441 void InsetCollapsable::edit(Cursor & cur, bool front, EntryDirection entry_from)
442 {
443         //lyxerr << "InsetCollapsable: edit left/right" << endl;
444         cur.push(*this);
445         InsetText::edit(cur, front, entry_from);
446 }
447
448
449 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
450 {
451         //lyxerr << "InsetCollapsable: edit xy" << endl;
452         if (geometry(cur.bv()) == ButtonOnly
453          || (button_dim.contains(x, y) 
454           && geometry(cur.bv()) != NoButton))
455                 return this;
456         cur.push(*this);
457         return InsetText::editXY(cur, x, y);
458 }
459
460
461 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
462 {
463         //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
464         //      << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
465
466         switch (cmd.action) {
467         case LFUN_MOUSE_PRESS:
468                 if (hitButton(cmd)) {
469                         switch (cmd.button()) {
470                         case mouse_button::button1:
471                         case mouse_button::button3:
472                                 // Pass the command to the enclosing InsetText,
473                                 // so that the cursor gets set.
474                                 cur.undispatched();
475                                 break;
476                         case mouse_button::none:
477                         case mouse_button::button2:
478                         case mouse_button::button4:
479                         case mouse_button::button5:
480                                 // Nothing to do.
481                                 cur.noUpdate();
482                                 break;
483                         }
484                 } else if (geometry(cur.bv()) != ButtonOnly)
485                         InsetText::doDispatch(cur, cmd);
486                 else
487                         cur.undispatched();
488                 break;
489
490         case LFUN_MOUSE_MOTION:
491         case LFUN_MOUSE_DOUBLE:
492         case LFUN_MOUSE_TRIPLE:
493                 if (hitButton(cmd)) 
494                         cur.noUpdate();
495                 else if (geometry(cur.bv()) != ButtonOnly)
496                         InsetText::doDispatch(cur, cmd);
497                 else
498                         cur.undispatched();
499                 break;
500
501         case LFUN_MOUSE_RELEASE:
502                 if (!hitButton(cmd)) {
503                         // The mouse click has to be within the inset!
504                         if (geometry(cur.bv()) != ButtonOnly)
505                                 InsetText::doDispatch(cur, cmd);
506                         else
507                                 cur.undispatched();                     
508                         break;
509                 }
510                 if (cmd.button() != mouse_button::button1) {
511                         // Nothing to do.
512                         cur.noUpdate();
513                         break;
514                 }
515                 // if we are selecting, we do not want to
516                 // toggle the inset.
517                 if (cur.selection())
518                         break;
519                 // Left button is clicked, the user asks to
520                 // toggle the inset visual state.
521                 cur.dispatched();
522                 cur.updateFlags(Update::Force | Update::FitCursor);
523                 if (geometry(cur.bv()) == ButtonOnly) {
524                         setStatus(cur, Open);
525                         edit(cur, true);
526                 }
527                 else
528                         setStatus(cur, Collapsed);
529                 cur.bv().cursor() = cur;
530                 break;
531
532         case LFUN_INSET_TOGGLE:
533                 if (cmd.argument() == "open")
534                         setStatus(cur, Open);
535                 else if (cmd.argument() == "close")
536                         setStatus(cur, Collapsed);
537                 else if (cmd.argument() == "toggle" || cmd.argument().empty())
538                         if (status_ == Open) {
539                                 setStatus(cur, Collapsed);
540                                 if (geometry(cur.bv()) == ButtonOnly)
541                                         cur.top().forwardPos();
542                         } else
543                                 setStatus(cur, Open);
544                 else // if assign or anything else
545                         cur.undispatched();
546                 cur.dispatched();
547                 break;
548
549         default:
550                 InsetText::doDispatch(cur, cmd);
551                 break;
552         }
553 }
554
555
556 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
557                 FuncStatus & flag) const
558 {
559         switch (cmd.action) {
560         case LFUN_INSET_TOGGLE:
561                 if (cmd.argument() == "open")
562                         flag.setEnabled(status_ != Open);
563                 else if (cmd.argument() == "close")
564                         flag.setEnabled(status_ == Open);
565                 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
566                         flag.setEnabled(true);
567                         flag.setOnOff(status_ == Open);
568                 } else
569                         flag.setEnabled(false);
570                 return true;
571
572         default:
573                 return InsetText::getStatus(cur, cmd, flag);
574         }
575 }
576
577
578 void InsetCollapsable::setLabel(docstring const & l)
579 {
580         labelstring_ = l;
581 }
582
583
584 docstring const InsetCollapsable::buttonLabel(BufferView const &) const
585 {
586         return labelstring_.empty() ? getLayout().labelstring() : labelstring_;
587 }
588
589
590 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
591 {
592         status_ = status;
593         setButtonLabel();
594         if (status_ == Collapsed) {
595                 cur.leaveInset(*this);
596                 mouse_hover_ = false;
597         }
598 }
599
600
601 docstring InsetCollapsable::floatName(string const & type) const
602 {
603         BufferParams const & bp = buffer().params();
604         FloatList const & floats = bp.documentClass().floats();
605         FloatList::const_iterator it = floats[type];
606         // FIXME UNICODE
607         return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
608 }
609
610
611 InsetLayout::InsetDecoration InsetCollapsable::decoration() const
612 {
613         InsetLayout::InsetDecoration const dec = getLayout().decoration();
614         return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
615 }
616
617
618 docstring InsetCollapsable::contextMenu(BufferView const & bv, int x,
619         int y) const
620 {
621         if (decoration() == InsetLayout::CONGLOMERATE)
622                 return from_ascii("context-conglomerate");
623
624         if (geometry(bv) == NoButton)
625                 return from_ascii("context-collapsable");
626
627         Dimension dim = dimensionCollapsed(bv);
628         if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
629                 return from_ascii("context-collapsable");
630
631         return InsetText::contextMenu(bv, x, y);
632 }
633
634 } // namespace lyx