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