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