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