]> git.lyx.org Git - features.git/blob - src/insets/InsetCollapsable.cpp
* DocIterator.h (forwardPosNoDescend): remove method
[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 "debug.h"
22 #include "DispatchResult.h"
23 #include "FloatList.h"
24 #include "FuncStatus.h"
25 #include "gettext.h"
26 #include "Color.h"
27 #include "Lexer.h"
28 #include "FuncRequest.h"
29 #include "MetricsInfo.h"
30
31 #include "frontends/FontMetrics.h"
32 #include "frontends/Painter.h"
33
34
35 namespace lyx {
36
37 using graphics::PreviewLoader;
38
39 using std::endl;
40 using std::string;
41 using std::max;
42 using std::min;
43 using std::ostream;
44
45
46 InsetCollapsable::CollapseStatus InsetCollapsable::status() const
47 {
48         return (autoOpen_ && status_ != Inlined) ? Open : status_;
49 }
50
51
52 InsetCollapsable::InsetCollapsable
53                 (BufferParams const & bp, CollapseStatus status)
54         : InsetText(bp), label(from_ascii("Label")), status_(status),
55           openinlined_(false), autoOpen_(false), mouse_hover_(false)
56 {
57         setAutoBreakRows(true);
58         setDrawFrame(true);
59         setFrameColor(Color::collapsableframe);
60         setButtonLabel();
61 }
62
63
64 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
65         : InsetText(rhs),
66                 layout_(rhs.layout_),
67                 button_dim(rhs.button_dim),
68                 topx(rhs.topx),
69                 topbaseline(rhs.topbaseline),
70                 label(rhs.label),
71                 status_(rhs.status_),
72                 openinlined_(rhs.openinlined_),
73                 autoOpen_(rhs.autoOpen_),
74                 textdim_(rhs.textdim_),
75                 // the sole purpose of this copy constructor
76                 mouse_hover_(false)
77 {
78 }
79
80
81 void  InsetCollapsable::setLayout(BufferParams const & bp)
82 {
83         setLabelFont(getLayout(bp).labelfont);
84         setLabel(getLayout(bp).labelstring);
85 }
86
87
88 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
89 {
90         os << "status ";
91         switch (status_) {
92         case Open:
93                 os << "open";
94                 break;
95         case Collapsed:
96                 os << "collapsed";
97                 break;
98         case Inlined:
99                 os << "inlined";
100                 break;
101         }
102         os << "\n";
103         text_.write(buf, os);
104 }
105
106
107 void InsetCollapsable::read(Buffer const & buf, Lexer & lex)
108 {
109         bool token_found = false;
110         if (lex.isOK()) {
111                 lex.next();
112                 string const token = lex.getString();
113                 if (token == "status") {
114                         lex.next();
115                         string const tmp_token = lex.getString();
116
117                         if (tmp_token == "inlined") {
118                                 status_ = Inlined;
119                                 token_found = true;
120                         } else if (tmp_token == "collapsed") {
121                                 status_ = Collapsed;
122                                 token_found = true;
123                         } else if (tmp_token == "open") {
124                                 status_ = Open;
125                                 token_found = true;
126                         } else {
127                                 lyxerr << "InsetCollapsable::read: Missing status!"
128                                        << endl;
129                                 // Take countermeasures
130                                 lex.pushToken(token);
131                         }
132                 } else {
133                         lyxerr << "InsetCollapsable::read: Missing 'status'-tag!"
134                                    << endl;
135                         // take countermeasures
136                         lex.pushToken(token);
137                 }
138         }
139         InsetText::read(buf, lex);
140
141         if (!token_found)
142                 status_ = isOpen() ? Open : Collapsed;
143
144         setButtonLabel();
145 }
146
147
148 Dimension InsetCollapsable::dimensionCollapsed() const
149 {
150         Dimension dim;
151         theFontMetrics(layout_.labelfont).buttonText(
152                 label, dim.wid, dim.asc, dim.des);
153         return dim;
154 }
155
156
157 bool InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
158 {
159         autoOpen_ = mi.base.bv->cursor().isInside(this);
160         mi.base.textwidth -= (int) (1.5 * TEXT_TO_INSET_OFFSET);
161
162         if (status() == Inlined) {
163                 InsetText::metrics(mi, dim);
164         } else {
165                 dim = dimensionCollapsed();
166                 if (status() == Open) {
167                         InsetText::metrics(mi, textdim_);
168                         // This expression should not contain mi.base.texwidth
169                         openinlined_ = !hasFixedWidth()
170                                 && textdim_.wid < 0.5 * mi.base.bv->workWidth();
171                         if (openinlined_) {
172                                 // Correct for button width, and re-fit
173                                 mi.base.textwidth -= dim.wid;
174                                 InsetText::metrics(mi, textdim_);
175                                 dim.wid += textdim_.wid;
176                                 dim.des = max(dim.des - textdim_.asc + dim.asc, textdim_.des);
177                                 dim.asc = textdim_.asc;
178                         } else {
179                                 dim.des += textdim_.height() + TEXT_TO_BOTTOM_OFFSET;
180                                 dim.wid = max(dim.wid, textdim_.wid);
181                                 if (hasFixedWidth())
182                                         dim.wid = max(dim.wid, mi.base.textwidth);
183                         }
184                 }
185         }
186         dim.asc += TEXT_TO_INSET_OFFSET;
187         dim.des += TEXT_TO_INSET_OFFSET;
188         dim.wid += (int) (1.5 * TEXT_TO_INSET_OFFSET);
189         mi.base.textwidth += (int) (1.5 * TEXT_TO_INSET_OFFSET);
190         bool const changed = dim_ != dim;
191         dim_ = dim;
192         return changed;
193 }
194
195
196 bool InsetCollapsable::setMouseHover(bool mouse_hover)
197 {
198         mouse_hover_ = mouse_hover;
199         return true;
200 }
201
202
203 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
204 {
205         const int xx = x + TEXT_TO_INSET_OFFSET;
206         if (status() == Inlined) {
207                 InsetText::draw(pi, xx, y);
208         } else {
209                 Dimension dimc = dimensionCollapsed();
210                 int const top  = y - ascent() + TEXT_TO_INSET_OFFSET;
211                 button_dim.x1 = xx + 0;
212                 button_dim.x2 = xx + dimc.width();
213                 button_dim.y1 = top;
214                 button_dim.y2 = top + dimc.height();
215
216                 pi.pain.buttonText(xx, top + dimc.asc, label, layout_.labelfont, mouse_hover_);
217
218                 if (status() == Open) {
219                         int textx, texty;
220                         if (openinlined_) {
221                                 textx = xx + dimc.width();
222                                 texty = top + textdim_.asc;
223                         } else {
224                                 textx = xx;
225                                 texty = top + dimc.height() + textdim_.asc;
226                         }
227                         InsetText::draw(pi, textx, texty);
228                 }
229         }
230         setPosCache(pi, x, y);
231 }
232
233
234 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
235 {
236         x += TEXT_TO_INSET_OFFSET;
237         if (status() == Open) {
238                 if (openinlined_)
239                         x += dimensionCollapsed().wid;
240                 else
241                         y += dimensionCollapsed().des + textdim_.asc;
242         }
243         if (status() != Collapsed)
244                 InsetText::drawSelection(pi, x, y);
245 }
246
247
248 void InsetCollapsable::cursorPos(BufferView const & bv,
249                 CursorSlice const & sl, bool boundary, int & x, int & y) const
250 {
251         BOOST_ASSERT(status() != Collapsed);
252
253         InsetText::cursorPos(bv, sl, boundary, x, y);
254
255         if (status() == Open) {
256                 if (openinlined_)
257                         x += dimensionCollapsed().wid;
258                 else
259                         y += dimensionCollapsed().height() - ascent()
260                                 + TEXT_TO_INSET_OFFSET + textdim_.asc;
261         }
262         x += TEXT_TO_INSET_OFFSET;
263 }
264
265
266 Inset::EDITABLE InsetCollapsable::editable() const
267 {
268         return status() != Collapsed ? HIGHLY_EDITABLE : IS_EDITABLE;
269 }
270
271
272 bool InsetCollapsable::descendable() const
273 {
274         return status() != Collapsed;
275 }
276
277
278 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
279 {
280         return button_dim.contains(cmd.x, cmd.y);
281 }
282
283
284 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
285 {
286         docstring label;
287         pos_type const max_length = 15;
288         pos_type const p_siz = paragraphs().begin()->size();
289         pos_type const n = min(max_length, p_siz);
290         pos_type i = 0;
291         pos_type j = 0;
292         for (; i < n && j < p_siz; ++j) {
293                 if (paragraphs().begin()->isInset(j))
294                         continue;
295                 label += paragraphs().begin()->getChar(j);
296                 ++i;
297         }
298         if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
299                 label += "...";
300         }
301         return label.empty() ? l : label;
302 }
303
304
305 void InsetCollapsable::edit(Cursor & cur, bool left)
306 {
307         //lyxerr << "InsetCollapsable: edit left/right" << endl;
308         cur.push(*this);
309         InsetText::edit(cur, left);
310 }
311
312
313 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
314 {
315         //lyxerr << "InsetCollapsable: edit xy" << endl;
316         if (status() == Collapsed || (button_dim.contains(x, y) && status() != Inlined))
317                 return this;
318         cur.push(*this);
319         return InsetText::editXY(cur, x, y);
320 }
321
322
323 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
324 {
325         //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
326         //      << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
327
328         switch (cmd.action) {
329         case LFUN_MOUSE_PRESS:
330                 if (cmd.button() == mouse_button::button1 && hitButton(cmd) && status() != Inlined) {
331                         // reset selection if necessary (see bug 3060)
332                         if (cur.selection())
333                                 cur.bv().cursor().clearSelection();
334                         else
335                                 cur.noUpdate();
336                         cur.dispatched();
337                         break;
338                 }
339                 if (status() == Inlined)
340                         InsetText::doDispatch(cur, cmd);
341                 else if (status() == Open && !hitButton(cmd))
342                         InsetText::doDispatch(cur, cmd);
343                 else
344                         cur.undispatched();
345                 break;
346
347         case LFUN_MOUSE_MOTION:
348         case LFUN_MOUSE_DOUBLE:
349         case LFUN_MOUSE_TRIPLE:
350                 if (status_ == Inlined)
351                         InsetText::doDispatch(cur, cmd);
352                 else if (status() && !hitButton(cmd))
353                         InsetText::doDispatch(cur, cmd);
354                 else
355                         cur.undispatched();
356                 break;
357
358         case LFUN_MOUSE_RELEASE:
359                 if (cmd.button() == mouse_button::button3) {
360                         // Open the Inset configuration dialog
361                         showInsetDialog(&cur.bv());
362                         break;
363                 }
364
365                 if (status() == Inlined) {
366                         // The mouse click has to be within the inset!
367                         InsetText::doDispatch(cur, cmd);
368                         break;
369                 }
370
371                 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
372                         // if we are selecting, we do not want to
373                         // toggle the inset.
374                         if (cur.selection())
375                                 break;
376                         // Left button is clicked, the user asks to
377                         // toggle the inset visual state.
378                         cur.dispatched();
379                         cur.updateFlags(Update::Force | Update::FitCursor);
380                         if (status() == Collapsed) {
381                                 setStatus(cur, Open);
382                                 edit(cur, true);
383                         }
384                         else {
385                                 setStatus(cur, Collapsed);
386                         }
387                         cur.bv().cursor() = cur;
388                         break;
389                 }
390
391                 // The mouse click is within the opened inset.
392                 if (status() == Open)
393                         InsetText::doDispatch(cur, cmd);
394                 break;
395
396         case LFUN_INSET_TOGGLE:
397                 if (cmd.argument() == "open")
398                         setStatus(cur, Open);
399                 else if (cmd.argument() == "close")
400                         setStatus(cur, Collapsed);
401                 else if (cmd.argument() == "toggle" || cmd.argument().empty())
402                         if (isOpen()) {
403                                 setStatus(cur, Collapsed);
404                                 cur.top().forwardPos();
405                         }
406                         else
407                                 setStatus(cur, Open);
408                 else // if assign or anything else
409                         cur.undispatched();
410                 cur.dispatched();
411                 break;
412
413         default:
414                 InsetText::doDispatch(cur, cmd);
415                 break;
416         }
417 }
418
419
420 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
421                 FuncStatus & flag) const
422 {
423         switch (cmd.action) {
424
425         case LFUN_INSET_TOGGLE:
426                 if (cmd.argument() == "open" || cmd.argument() == "close" ||
427                     cmd.argument() == "toggle")
428                         flag.enabled(true);
429                 else
430                         flag.enabled(false);
431                 return true;
432
433         default:
434                 return InsetText::getStatus(cur, cmd, flag);
435         }
436 }
437
438
439 void InsetCollapsable::setLabel(docstring const & l)
440 {
441         label = l;
442 }
443
444
445 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
446 {
447         status_ = status;
448         setButtonLabel();
449         if (status_ == Collapsed)
450                 cur.leaveInset(*this);
451         // Because the collapse status is part of the inset and thus an
452         // integral part of the Buffer contents a changed status must be
453         // signaled to all views of current buffer.
454         cur.bv().buffer()->changed();
455 }
456
457
458 void InsetCollapsable::setLabelFont(Font const & font)
459 {
460         layout_.labelfont = font;
461 }
462
463 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
464 {
465         FloatList const & floats = bp.getTextClass().floats();
466         FloatList::const_iterator it = floats[type];
467         // FIXME UNICODE
468         return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
469 }
470
471
472 } // namespace lyx