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