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