]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCollapsable.cpp
Fix bug #6672 (part 2): Add tooltips to the outliner.
[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         return toolTipText();
132 }
133
134
135 void InsetCollapsable::write(ostream & os) const
136 {
137         os << "status ";
138         switch (status_) {
139         case Open:
140                 os << "open";
141                 break;
142         case Collapsed:
143                 os << "collapsed";
144                 break;
145         }
146         os << "\n";
147         text().write(os);
148 }
149
150
151 void InsetCollapsable::read(Lexer & lex)
152 {
153         lex.setContext("InsetCollapsable::read");
154         string tmp_token;
155         status_ = Collapsed;
156         lex >> "status" >> tmp_token;
157         if (tmp_token == "open")
158                 status_ = Open;
159
160         InsetText::read(lex);
161         setButtonLabel();
162 }
163
164
165 Dimension InsetCollapsable::dimensionCollapsed(BufferView const & bv) const
166 {
167         Dimension dim;
168         theFontMetrics(getLayout().labelfont()).buttonText(
169                 buttonLabel(bv), dim.wid, dim.asc, dim.des);
170         return dim;
171 }
172
173
174 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
175 {
176         auto_open_[mi.base.bv] =  mi.base.bv->cursor().isInside(this);
177
178         FontInfo tmpfont = mi.base.font;
179         mi.base.font = getLayout().font();
180         mi.base.font.realize(tmpfont);
181
182         BufferView const & bv = *mi.base.bv;
183
184         switch (geometry(bv)) {
185         case NoButton:
186                 InsetText::metrics(mi, dim);
187                 break;
188         case Corners:
189                 InsetText::metrics(mi, dim);
190                 dim.des -= 3;
191                 dim.asc -= 1;
192                 break;
193         case SubLabel: {
194                 InsetText::metrics(mi, dim);
195                 // consider width of the inset label
196                 FontInfo font(getLayout().labelfont());
197                 font.realize(sane_font);
198                 font.decSize();
199                 font.decSize();
200                 int w = 0;
201                 int a = 0;
202                 int d = 0;
203                 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
204                 dim.des += a + d;
205                 break;
206                 }
207         case TopButton:
208         case LeftButton:
209         case ButtonOnly:
210                 dim = dimensionCollapsed(bv);
211                 if (geometry(bv) == TopButton 
212                           || geometry(bv) == LeftButton) {
213                         Dimension textdim;
214                         InsetText::metrics(mi, textdim);
215                         openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
216                         if (openinlined_) {
217                                 // Correct for button width.
218                                 dim.wid += textdim.wid;
219                                 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
220                                 dim.asc = textdim.asc;
221                         } else {
222                                 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
223                                 dim.wid = max(dim.wid, textdim.wid);
224                         }
225                 }
226                 break;
227         }
228
229         mi.base.font = tmpfont;
230 }
231
232
233 bool InsetCollapsable::setMouseHover(bool mouse_hover)
234 {
235         mouse_hover_ = mouse_hover;
236         return true;
237 }
238
239
240 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
241 {
242         BufferView const & bv = *pi.base.bv;
243
244         auto_open_[&bv] =  bv.cursor().isInside(this);
245
246         FontInfo tmpfont = pi.base.font;
247         pi.base.font = getLayout().font();
248         pi.base.font.realize(tmpfont);
249
250         // Draw button first -- top, left or only
251         Dimension dimc = dimensionCollapsed(bv);
252
253         if (geometry(bv) == TopButton ||
254             geometry(bv) == LeftButton ||
255             geometry(bv) == ButtonOnly) {
256                 button_dim.x1 = x + 0;
257                 button_dim.x2 = x + dimc.width();
258                 button_dim.y1 = y - dimc.asc;
259                 button_dim.y2 = y + dimc.des;
260
261                 FontInfo labelfont = getLayout().labelfont();
262                 labelfont.setColor(labelColor());
263                 pi.pain.buttonText(x, y, buttonLabel(bv), labelfont,
264                         mouse_hover_);
265         } else {
266                 button_dim.x1 = 0;
267                 button_dim.y1 = 0;
268                 button_dim.x2 = 0;
269                 button_dim.y2 = 0;
270         }
271
272         Dimension const textdim = InsetText::dimension(bv);
273         int const baseline = y;
274         int textx, texty;
275         switch (geometry(bv)) {
276         case LeftButton:
277                 textx = x + dimc.width();
278                 texty = baseline;
279                 InsetText::draw(pi, textx, texty);
280                 break;
281         case TopButton:
282                 textx = x;
283                 texty = baseline + dimc.des + textdim.asc;
284                 InsetText::draw(pi, textx, texty);
285                 break;
286         case ButtonOnly:
287                 break;
288         case NoButton:
289                 textx = x;
290                 texty = baseline;
291                 InsetText::draw(pi, textx, texty);
292                 break;
293         case SubLabel:
294         case Corners:
295                 textx = x;
296                 texty = baseline;
297                 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
298                 InsetText::draw(pi, textx, texty);
299                 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
300
301                 int desc = textdim.descent();
302                 if (geometry(bv) == Corners)
303                         desc -= 3;
304
305                 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
306                 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
307                 pi.pain.line(xx1, y + desc - 4, 
308                              xx1, y + desc, 
309                         labelColor());
310                 if (status_ == Open)
311                         pi.pain.line(xx1, y + desc, 
312                                 xx2, y + desc,
313                                 labelColor());
314                 else {
315                         // Make status_ value visible:
316                         pi.pain.line(xx1, y + desc,
317                                 xx1 + 4, y + desc,
318                                 labelColor());
319                         pi.pain.line(xx2 - 4, y + desc,
320                                 xx2, y + desc,
321                                 labelColor());
322                 }
323                 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3, 
324                         y + desc - 4, labelColor());
325
326                 // the label below the text. Can be toggled.
327                 if (geometry(bv) == SubLabel) {
328                         FontInfo font(getLayout().labelfont());
329                         font.realize(sane_font);
330                         font.decSize();
331                         font.decSize();
332                         int w = 0;
333                         int a = 0;
334                         int d = 0;
335                         theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
336                         int const ww = max(textdim.wid, w);
337                         pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
338                                 buttonLabel(bv), font, Color_none, Color_none);
339                         desc += d;
340                 }
341
342                 // a visual cue when the cursor is inside the inset
343                 Cursor const & cur = bv.cursor();
344                 if (cur.isInside(this)) {
345                         y -= textdim.asc;
346                         y += 3;
347                         pi.pain.line(xx1, y + 4, xx1, y, labelColor());
348                         pi.pain.line(xx1 + 4, y, xx1, y, labelColor());
349                         pi.pain.line(xx2, y + 4, xx2, y,
350                                 labelColor());
351                         pi.pain.line(xx2 - 4, y, xx2, y,
352                                 labelColor());
353                 }
354                 break;
355         }
356
357         pi.base.font = tmpfont;
358 }
359
360
361 void InsetCollapsable::cursorPos(BufferView const & bv,
362                 CursorSlice const & sl, bool boundary, int & x, int & y) const
363 {
364         if (geometry(bv) == ButtonOnly)
365                 status_ = Open;
366
367         InsetText::cursorPos(bv, sl, boundary, x, y);
368         Dimension const textdim = InsetText::dimension(bv);
369
370         switch (geometry(bv)) {
371         case LeftButton:
372                 x += dimensionCollapsed(bv).wid;
373                 break;
374         case TopButton: {
375                 y += dimensionCollapsed(bv).des + textdim.asc;
376                 break;
377         }
378         case NoButton:
379         case SubLabel:
380         case Corners:
381                 // Do nothing
382                 break;
383         case ButtonOnly:
384                 // Cannot get here
385                 break;
386         }
387 }
388
389
390 bool InsetCollapsable::editable() const
391 {
392         return geometry() != ButtonOnly;
393 }
394
395
396 bool InsetCollapsable::descendable(BufferView const & bv) const
397 {
398         return geometry(bv) != ButtonOnly;
399 }
400
401
402 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
403 {
404         return button_dim.contains(cmd.x(), cmd.y());
405 }
406
407
408 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
409 {
410         docstring label;
411         pos_type const max_length = 15;
412         pos_type const p_siz = paragraphs().begin()->size();
413         pos_type const n = min(max_length, p_siz);
414         pos_type i = 0;
415         pos_type j = 0;
416         for (; i < n && j < p_siz; ++j) {
417                 if (paragraphs().begin()->isInset(j))
418                         continue;
419                 label += paragraphs().begin()->getChar(j);
420                 ++i;
421         }
422         if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
423                 label += "...";
424         }
425         return label.empty() ? l : label;
426 }
427
428
429 void InsetCollapsable::edit(Cursor & cur, bool front, EntryDirection entry_from)
430 {
431         //lyxerr << "InsetCollapsable: edit left/right" << endl;
432         cur.push(*this);
433         InsetText::edit(cur, front, entry_from);
434 }
435
436
437 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
438 {
439         //lyxerr << "InsetCollapsable: edit xy" << endl;
440         if (geometry(cur.bv()) == ButtonOnly
441          || (button_dim.contains(x, y) 
442           && geometry(cur.bv()) != NoButton))
443                 return this;
444         cur.push(*this);
445         return InsetText::editXY(cur, x, y);
446 }
447
448
449 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
450 {
451         //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
452         //      << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
453
454         switch (cmd.action()) {
455         case LFUN_MOUSE_PRESS:
456                 if (hitButton(cmd)) {
457                         switch (cmd.button()) {
458                         case mouse_button::button1:
459                         case mouse_button::button3:
460                                 // Pass the command to the enclosing InsetText,
461                                 // so that the cursor gets set.
462                                 cur.undispatched();
463                                 break;
464                         case mouse_button::none:
465                         case mouse_button::button2:
466                         case mouse_button::button4:
467                         case mouse_button::button5:
468                                 // Nothing to do.
469                                 cur.noUpdate();
470                                 break;
471                         }
472                 } else if (geometry(cur.bv()) != ButtonOnly)
473                         InsetText::doDispatch(cur, cmd);
474                 else
475                         cur.undispatched();
476                 break;
477
478         case LFUN_MOUSE_MOTION:
479         case LFUN_MOUSE_DOUBLE:
480         case LFUN_MOUSE_TRIPLE:
481                 if (hitButton(cmd)) 
482                         cur.noUpdate();
483                 else if (geometry(cur.bv()) != ButtonOnly)
484                         InsetText::doDispatch(cur, cmd);
485                 else
486                         cur.undispatched();
487                 break;
488
489         case LFUN_MOUSE_RELEASE:
490                 if (!hitButton(cmd)) {
491                         // The mouse click has to be within the inset!
492                         if (geometry(cur.bv()) != ButtonOnly)
493                                 InsetText::doDispatch(cur, cmd);
494                         else
495                                 cur.undispatched();                     
496                         break;
497                 }
498                 if (cmd.button() != mouse_button::button1) {
499                         // Nothing to do.
500                         cur.noUpdate();
501                         break;
502                 }
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(cur.bv()) == ButtonOnly) {
512                         setStatus(cur, Open);
513                         edit(cur, true);
514                 }
515                 else
516                         setStatus(cur, Collapsed);
517                 cur.bv().cursor() = cur;
518                 break;
519
520         case LFUN_INSET_TOGGLE:
521                 if (cmd.argument() == "open")
522                         setStatus(cur, Open);
523                 else if (cmd.argument() == "close")
524                         setStatus(cur, Collapsed);
525                 else if (cmd.argument() == "toggle" || cmd.argument().empty())
526                         if (status_ == Open) {
527                                 setStatus(cur, Collapsed);
528                                 if (geometry(cur.bv()) == ButtonOnly)
529                                         cur.top().forwardPos();
530                         } else
531                                 setStatus(cur, Open);
532                 else // if assign or anything else
533                         cur.undispatched();
534                 cur.dispatched();
535                 break;
536
537         default:
538                 InsetText::doDispatch(cur, cmd);
539                 break;
540         }
541 }
542
543
544 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
545                 FuncStatus & flag) const
546 {
547         switch (cmd.action()) {
548         case LFUN_INSET_TOGGLE:
549                 if (cmd.argument() == "open")
550                         flag.setEnabled(status_ != Open);
551                 else if (cmd.argument() == "close")
552                         flag.setEnabled(status_ == Open);
553                 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
554                         flag.setEnabled(true);
555                         flag.setOnOff(status_ == Open);
556                 } else
557                         flag.setEnabled(false);
558                 return true;
559
560         default:
561                 return InsetText::getStatus(cur, cmd, flag);
562         }
563 }
564
565
566 void InsetCollapsable::setLabel(docstring const & l)
567 {
568         labelstring_ = l;
569 }
570
571
572 docstring const InsetCollapsable::buttonLabel(BufferView const & bv) const
573 {
574         docstring const label = labelstring_.empty() ? 
575                 translateIfPossible(getLayout().labelstring()) : labelstring_;
576         InsetLayout const & il = getLayout();
577         if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
578                 return label;
579         return getNewLabel(label);
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