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