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