]> git.lyx.org Git - features.git/blob - src/insets/insetcollapsable.C
split LyXFunc::getStatus() into inset specific chunks
[features.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 InsetCollapsable::InsetCollapsable(BufferParams const & bp,
43         CollapseStatus status)
44         : inset(bp), label("Label"), status_(status), openinlined_(false)
45 {
46         inset.setOwner(this);
47         inset.setAutoBreakRows(true);
48         inset.setDrawFrame(InsetText::ALWAYS);
49         inset.setFrameColor(LColor::collapsableframe);
50         setInsetName("Collapsable");
51         setButtonLabel();
52 }
53
54
55 InsetCollapsable::InsetCollapsable(InsetCollapsable const & in)
56         : UpdatableInset(in), inset(in.inset),
57           labelfont_(in.labelfont_), label(in.label), status_(in.status_)
58 {
59         inset.setOwner(this);
60         setButtonLabel();
61 }
62
63
64 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
65 {
66         os << "status ";
67         switch (status_) {
68         case Open:
69                 os << "open";
70                 break;
71         case Collapsed:
72                 os << "collapsed";
73                 break;
74         case Inlined:
75                 os << "inlined";
76                 break;
77         }
78         os << "\n";
79         inset.text_.write(buf, os);
80 }
81
82
83 void InsetCollapsable::read(Buffer const & buf, LyXLex & lex)
84 {
85         bool token_found = false;
86         if (lex.isOK()) {
87                 lex.next();
88                 string const token = lex.getString();
89                 if (token == "status") {
90                         lex.next();
91                         string const tmp_token = lex.getString();
92
93                         if (tmp_token == "inlined") {
94                                 status_ = Inlined;
95                                 token_found = true;
96                         } else if (tmp_token == "collapsed") {
97                                 status_ = Collapsed;
98                                 token_found = true;
99                         } else if (tmp_token == "open") {
100                                 status_ = Open;
101                                 token_found = true;
102                         } else {
103                                 lyxerr << "InsetCollapsable::read: Missing status!"
104                                        << endl;
105                                 // Take countermeasures
106                                 lex.pushToken(token);
107                         }
108                 } else {
109                         lyxerr << "InsetCollapsable::Read: Missing 'status'-tag!"
110                                    << endl;
111                         // take countermeasures
112                         lex.pushToken(token);
113                 }
114         }
115         inset.read(buf, lex);
116
117         if (!token_found)
118                 status_ = isOpen() ? Open : Collapsed;
119
120         setButtonLabel();
121 }
122
123
124 void InsetCollapsable::dimension_collapsed(Dimension & dim) const
125 {
126         font_metrics::buttonText(label, labelfont_, dim.wid, dim.asc, dim.des);
127 }
128
129
130 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
131 {
132         if (status_ == Inlined) {
133                 inset.metrics(mi, dim);
134         } else {
135                 dimension_collapsed(dim);
136                 if (status_ == Open) {
137                         Dimension insetdim;
138                         inset.metrics(mi, insetdim);
139                         openinlined_ = (insetdim.wid + dim.wid <= mi.base.textwidth);
140                         if (openinlined_) {
141                                 dim.wid += insetdim.wid;
142                                 dim.des = max(dim.des, insetdim.des);
143                                 dim.asc = max(dim.asc, insetdim.asc);
144                         } else {
145                                 dim.des += insetdim.height()
146                                         + TEXT_TO_BOTTOM_OFFSET;
147                                 dim.wid = max(dim.wid, insetdim.wid);
148                         }
149                 }
150         }
151         dim_ = dim;
152 }
153
154
155 void InsetCollapsable::draw_collapsed(PainterInfo & pi, int x, int y) const
156 {
157         pi.pain.buttonText(x, y, label, labelfont_);
158 }
159
160
161 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
162 {
163         setPosCache(pi, x, y);
164
165         if (status_ == Inlined) {
166                 inset.draw(pi, x, y);
167         } else {
168                 Dimension dimc;
169                 dimension_collapsed(dimc);
170                 int const aa  = ascent();
171                 button_dim.x1 = x + 0;
172                 button_dim.x2 = x + dimc.width();
173                 button_dim.y1 = y - aa + pi.base.bv->top_y();
174                 button_dim.y2 = y - aa + pi.base.bv->top_y() + dimc.height();
175
176                 draw_collapsed(pi, x, y);
177                 if (status_ == Open) {
178                         if (!owner())
179                                 x += scroll();
180                         
181                         if (openinlined_)
182                                 inset.draw(pi, x + dimc.width(), y - aa + inset.ascent());
183                         else 
184                                 inset.draw(pi, x, y - aa + dimc.height() + inset.ascent());
185                 }
186         }
187 }
188
189
190 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
191 {
192         inset.drawSelection(pi, x, y);
193 }
194
195
196 InsetOld::EDITABLE InsetCollapsable::editable() const
197 {
198         return status_ != Collapsed ? HIGHLY_EDITABLE : IS_EDITABLE;
199 }
200
201
202 bool InsetCollapsable::descendable() const
203 {
204         return status_ != Collapsed;
205 }
206
207
208 void InsetCollapsable::lfunMouseRelease(LCursor & cur, FuncRequest & cmd)
209 {
210         if (cmd.button() == mouse_button::button3) {
211                 showInsetDialog(&cur.bv());
212                 return;
213         }
214
215         switch (status_) {
216
217         case Collapsed:
218                 lyxerr << "InsetCollapsable::lfunMouseRelease 1" << endl;
219                 setStatus(Open);
220                 edit(cur, true);
221                 break;
222
223         case Open: {
224                 FuncRequest cmd1 = cmd;
225                 if (hitButton(cmd1)) {
226                         lyxerr << "InsetCollapsable::lfunMouseRelease 2" << endl;
227                         setStatus(Collapsed);
228                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
229                         break;
230                 }
231                 lyxerr << "InsetCollapsable::lfunMouseRelease 3" << endl;
232                 inset.dispatch(cur, cmd);
233                 break;
234         }
235
236         case Inlined:
237                 inset.dispatch(cur, cmd);
238                 break;
239         }
240 }
241
242
243 int InsetCollapsable::latex(Buffer const & buf, ostream & os,
244                             OutputParams const & runparams) const
245 {
246         return inset.latex(buf, os, runparams);
247 }
248
249
250 int InsetCollapsable::plaintext(Buffer const & buf, ostream & os,
251                             OutputParams const & runparams) const
252 {
253         return inset.plaintext(buf, os, runparams);
254 }
255
256
257 int InsetCollapsable::linuxdoc(Buffer const & buf, ostream & os,
258                                OutputParams const & runparams) const
259 {
260         return inset.linuxdoc(buf, os, runparams);
261 }
262
263
264 int InsetCollapsable::docbook(Buffer const & buf, ostream & os,
265                               OutputParams const & runparams) const
266 {
267         return inset.docbook(buf, os, runparams);
268 }
269
270
271 bool InsetCollapsable::hitButton(FuncRequest & cmd) const
272 {
273         return button_dim.contains(cmd.x, cmd.y);
274 }
275
276
277 string const InsetCollapsable::getNewLabel(string const & l) const
278 {
279         string label;
280         pos_type const max_length = 15;
281         pos_type const p_siz = inset.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 (inset.paragraphs().begin()->isInset(j))
287                         continue;
288                 label += inset.paragraphs().begin()->getChar(j);
289                 ++i;
290         }
291         if (inset.paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
292                 label += "...";
293         }
294         return label.empty() ? l : label;
295 }
296
297
298 void InsetCollapsable::edit(LCursor & cur, bool left)
299 {
300         //lyxerr << "InsetCollapsable: edit left/right" << endl;
301         cur.push(*this);
302         inset.edit(cur, left);
303         open();
304 }
305
306
307 InsetBase * InsetCollapsable::editXY(LCursor & cur, int x, int y)
308 {
309         cur.push(*this);
310         //lyxerr << "InsetCollapsable: edit xy" << endl;
311         if (status_ == Collapsed) {
312                 setStatus(Open);
313                 inset.edit(cur, true);
314 #warning look here
315 //we are not calling edit(x,y) because there are no coordinates in the
316 //inset yet. I personally think it's ok. (ab)
317                 return this;
318         }
319         return inset.editXY(cur, x, y);
320 }
321
322
323 void InsetCollapsable::priv_dispatch(LCursor & cur, FuncRequest & cmd)
324 {
325         //lyxerr << "\nInsetCollapsable::priv_dispatch (begin): cmd: " << cmd
326         //      << "  button y: " << button_dim.y2 << endl;
327         switch (cmd.action) {
328                 case LFUN_MOUSE_PRESS:
329                         if (status_ == Inlined)
330                                 inset.dispatch(cur, cmd);
331                         else if (status_ == Open && !hitButton(cmd))
332                                 inset.dispatch(cur, cmd);
333                         break;
334
335                 case LFUN_MOUSE_MOTION:
336                         if (status_ == Inlined)
337                                 inset.dispatch(cur, cmd);
338                         else if (status_ == Open && !hitButton(cmd))
339                                 inset.dispatch(cur, cmd);
340                         break;
341
342                 case LFUN_MOUSE_RELEASE:
343                         lfunMouseRelease(cur, cmd);
344                         break;
345
346                 case LFUN_INSET_TOGGLE:
347                         if (inset.text_.toggleInset(cur))
348                                 break;
349                         if (status_ == Open) {
350                                 setStatus(Inlined);
351                                 break;
352                         }
353                         setStatus(Collapsed);
354                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
355                         break;
356
357                 default:
358                         inset.dispatch(cur, cmd);
359                         break;
360         }
361 }
362
363
364 bool InsetCollapsable::getStatus(LCursor & cur, FuncRequest const & cmd,
365         FuncStatus & flag) const
366 {
367         return inset.getStatus(cur, cmd, flag);
368 }
369
370
371 void InsetCollapsable::validate(LaTeXFeatures & features) const
372 {
373         inset.validate(features);
374 }
375
376
377 void InsetCollapsable::getCursorPos(CursorSlice const & cur,
378         int & x, int & y) const
379 {
380         inset.getCursorPos(cur, x, y);
381 }
382
383
384 void InsetCollapsable::getLabelList(Buffer const & buffer,
385                                     std::vector<string> & list) const
386 {
387         inset.getLabelList(buffer, list);
388 }
389
390
391 int InsetCollapsable::scroll(bool recursive) const
392 {
393         int sx = UpdatableInset::scroll(false);
394
395         if (recursive)
396                 sx += inset.scroll(false);
397
398         return sx;
399 }
400
401
402 size_t InsetCollapsable::nargs() const
403 {
404         return inset.nargs();
405 }
406
407
408 LyXText * InsetCollapsable::getText(int i) const
409 {
410         return inset.getText(i);
411 }
412
413
414 void InsetCollapsable::open()
415 {
416         if (status_ == Collapsed)   // ...but not inlined
417                 setStatus(Open);
418 }
419
420
421 void InsetCollapsable::close()
422 {
423         setStatus(Collapsed);
424 }
425
426
427 void InsetCollapsable::setLabel(string const & l)
428 {
429         label = l;
430 }
431
432
433 void InsetCollapsable::setStatus(CollapseStatus st)
434 {
435         status_ = st;
436         setButtonLabel();
437 }
438
439
440 void InsetCollapsable::markErased()
441 {
442         inset.markErased();
443 }
444
445
446 void InsetCollapsable::addPreview(PreviewLoader & loader) const
447 {
448         inset.addPreview(loader);
449 }
450
451
452 bool InsetCollapsable::insetAllowed(InsetOld::Code code) const
453 {
454         return inset.insetAllowed(code);
455 }
456
457
458 void InsetCollapsable::setLabelFont(LyXFont & font)
459 {
460         labelfont_ = font;
461 }
462
463
464 void InsetCollapsable::scroll(BufferView & bv, float sx) const
465 {
466         UpdatableInset::scroll(bv, sx);
467 }
468
469
470 void InsetCollapsable::scroll(BufferView & bv, int offset) const
471 {
472         UpdatableInset::scroll(bv, offset);
473 }
474
475
476 Box const & InsetCollapsable::buttonDim() const
477 {
478         return button_dim;
479 }
480
481
482 void InsetCollapsable::setBackgroundColor(LColor_color color)
483 {
484         InsetOld::setBackgroundColor(color);
485         inset.setBackgroundColor(color);
486 }