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