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