]> git.lyx.org Git - lyx.git/blob - src/insets/insetcollapsable.C
c9714b04a59de776a3597134853567e909e3e173
[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         : InsetText(bp), label("Label"), status_(status), openinlined_(false)
45 {
46         setAutoBreakRows(true);
47         setDrawFrame(InsetText::ALWAYS);
48         setFrameColor(LColor::collapsableframe);
49         setInsetName("Collapsable");
50         setButtonLabel();
51 }
52
53
54 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
55 {
56         os << "status ";
57         switch (status_) {
58         case Open:
59                 os << "open";
60                 break;
61         case Collapsed:
62                 os << "collapsed";
63                 break;
64         case Inlined:
65                 os << "inlined";
66                 break;
67         }
68         os << "\n";
69         text_.write(buf, os);
70 }
71
72
73 void InsetCollapsable::read(Buffer const & buf, LyXLex & lex)
74 {
75         bool token_found = false;
76         if (lex.isOK()) {
77                 lex.next();
78                 string const token = lex.getString();
79                 if (token == "status") {
80                         lex.next();
81                         string const tmp_token = lex.getString();
82
83                         if (tmp_token == "inlined") {
84                                 status_ = Inlined;
85                                 token_found = true;
86                         } else if (tmp_token == "collapsed") {
87                                 status_ = Collapsed;
88                                 token_found = true;
89                         } else if (tmp_token == "open") {
90                                 status_ = Open;
91                                 token_found = true;
92                         } else {
93                                 lyxerr << "InsetCollapsable::read: Missing status!"
94                                        << endl;
95                                 // Take countermeasures
96                                 lex.pushToken(token);
97                         }
98                 } else {
99                         lyxerr << "InsetCollapsable::read: Missing 'status'-tag!"
100                                    << endl;
101                         // take countermeasures
102                         lex.pushToken(token);
103                 }
104         }
105         InsetText::read(buf, lex);
106
107         if (!token_found)
108                 status_ = isOpen() ? Open : Collapsed;
109
110         setButtonLabel();
111 }
112
113
114 void InsetCollapsable::dimension_collapsed(Dimension & dim) const
115 {
116         font_metrics::buttonText(label, labelfont_, dim.wid, dim.asc, dim.des);
117 }
118
119
120 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
121 {
122         if (status_ == Inlined) {
123                 InsetText::metrics(mi, dim);
124         } else {
125                 dimension_collapsed(dim);
126                 if (status_ == Open) {
127                         Dimension insetdim;
128                         InsetText::metrics(mi, insetdim);
129                         openinlined_ = (insetdim.wid + dim.wid <= mi.base.textwidth);
130                         if (openinlined_) {
131                                 dim.wid += insetdim.wid;
132                                 dim.des = max(dim.des, insetdim.des);
133                                 dim.asc = max(dim.asc, insetdim.asc);
134                         } else {
135                                 dim.des += insetdim.height() + TEXT_TO_BOTTOM_OFFSET;
136                                 dim.wid = max(dim.wid, insetdim.wid);
137                         }
138                 }
139         }
140         dim_ = dim;
141 }
142
143
144 void InsetCollapsable::draw_collapsed(PainterInfo & pi, int x, int y) const
145 {
146         pi.pain.buttonText(x, y, label, labelfont_);
147 }
148
149
150 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
151 {
152         setPosCache(pi, x, y);
153
154         if (status_ == Inlined) {
155                 InsetText::draw(pi, x, y);
156         } else {
157                 Dimension dimc;
158                 dimension_collapsed(dimc);
159                 int const aa  = ascent();
160                 button_dim.x1 = x + 0;
161                 button_dim.x2 = x + dimc.width();
162                 button_dim.y1 = y - aa + pi.base.bv->top_y();
163                 button_dim.y2 = y - aa + pi.base.bv->top_y() + dimc.height();
164
165                 draw_collapsed(pi, x, y);
166                 if (status_ == Open) {
167                         x += scroll();
168                         if (openinlined_)
169                                 InsetText::draw(pi, x + dimc.width(), y - aa + InsetText::ascent());
170                         else 
171                                 InsetText::draw(pi, x, y - aa + dimc.height() + InsetText::ascent());
172                 }
173         }
174 }
175
176
177 InsetOld::EDITABLE InsetCollapsable::editable() const
178 {
179         return status_ != Collapsed ? HIGHLY_EDITABLE : IS_EDITABLE;
180 }
181
182
183 bool InsetCollapsable::descendable() const
184 {
185         return status_ != Collapsed;
186 }
187
188
189 bool InsetCollapsable::hitButton(FuncRequest & cmd) const
190 {
191         return button_dim.contains(cmd.x, cmd.y);
192 }
193
194
195 string const InsetCollapsable::getNewLabel(string const & l) const
196 {
197         string label;
198         pos_type const max_length = 15;
199         pos_type const p_siz = paragraphs().begin()->size();
200         pos_type const n = min(max_length, p_siz);
201         pos_type i = 0;
202         pos_type j = 0;
203         for( ; i < n && j < p_siz; ++j) {
204                 if (paragraphs().begin()->isInset(j))
205                         continue;
206                 label += paragraphs().begin()->getChar(j);
207                 ++i;
208         }
209         if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
210                 label += "...";
211         }
212         return label.empty() ? l : label;
213 }
214
215
216 void InsetCollapsable::edit(LCursor & cur, bool left)
217 {
218         //lyxerr << "InsetCollapsable: edit left/right" << endl;
219         cur.push(*this);
220         InsetText::edit(cur, left);
221         open();
222 }
223
224
225 InsetBase * InsetCollapsable::editXY(LCursor & cur, int x, int y)
226 {
227         cur.push(*this);
228         //lyxerr << "InsetCollapsable: edit xy" << endl;
229         if (status_ == Collapsed) {
230                 setStatus(Open);
231                 // We are not calling editXY() because the row cache of the
232                 // inset might be invalid. 'Entering from the left' should be
233                 // ok, though.
234                 InsetText::edit(cur, true);
235                 return this;
236         }
237         return InsetText::editXY(cur, x, y);
238 }
239
240
241 void InsetCollapsable::priv_dispatch(LCursor & cur, FuncRequest & cmd)
242 {
243         //lyxerr << "\nInsetCollapsable::priv_dispatch (begin): cmd: " << cmd
244         //      << "  button y: " << button_dim.y2 << endl;
245         switch (cmd.action) {
246                 case LFUN_MOUSE_PRESS:
247                         if (status_ == Inlined)
248                                 InsetText::priv_dispatch(cur, cmd);
249                         else if (status_ == Open && !hitButton(cmd))
250                                 InsetText::priv_dispatch(cur, cmd);
251                         break;
252
253                 case LFUN_MOUSE_MOTION:
254                         if (status_ == Inlined)
255                                 InsetText::priv_dispatch(cur, cmd);
256                         else if (status_ == Open && !hitButton(cmd))
257                                 InsetText::priv_dispatch(cur, cmd);
258                         break;
259
260                 case LFUN_MOUSE_RELEASE:
261                         if (cmd.button() == mouse_button::button3) {
262                                 showInsetDialog(&cur.bv());
263                                 break;
264                         }
265
266                         switch (status_) {
267
268                         case Collapsed:
269                                 lyxerr << "InsetCollapsable::lfunMouseRelease 1" << endl;
270                                 setStatus(Open);
271                                 edit(cur, true);
272                                 break;
273
274                         case Open: {
275                                 FuncRequest cmd1 = cmd;
276                                 if (hitButton(cmd1)) {
277                                         lyxerr << "InsetCollapsable::lfunMouseRelease 2" << endl;
278                                         setStatus(Collapsed);
279                                         cur.undispatched();
280                                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
281                                 } else {
282                                         lyxerr << "InsetCollapsable::lfunMouseRelease 3" << endl;
283                                         InsetText::priv_dispatch(cur, cmd);
284                                 }
285                                 break;
286                         }
287
288                         case Inlined:
289                                 lyxerr << "InsetCollapsable::lfunMouseRelease 4" << endl;
290                                 InsetText::priv_dispatch(cur, cmd);
291                                 break;
292                         }
293                         break;
294
295                 case LFUN_INSET_TOGGLE:
296                         if (InsetText::text_.toggleInset(cur))
297                                 break;
298                         if (status_ == Open) {
299                                 setStatus(Inlined);
300                                 break;
301                         }
302                         setStatus(Collapsed);
303                         cur.undispatched();
304                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
305                         break;
306
307                 default:
308                         InsetText::priv_dispatch(cur, cmd);
309                         break;
310         }
311 }
312
313
314 int InsetCollapsable::scroll(bool recursive) const
315 {
316         int sx = UpdatableInset::scroll(false);
317
318         if (recursive)
319                 sx += InsetText::scroll(false);
320
321         return sx;
322 }
323
324
325 void InsetCollapsable::open()
326 {
327         if (status_ == Collapsed)   // ...but not inlined
328                 setStatus(Open);
329 }
330
331
332 void InsetCollapsable::close()
333 {
334         setStatus(Collapsed);
335 }
336
337
338 void InsetCollapsable::setLabel(string const & l)
339 {
340         label = l;
341 }
342
343
344 void InsetCollapsable::setStatus(CollapseStatus st)
345 {
346         status_ = st;
347         setButtonLabel();
348 }
349
350
351 void InsetCollapsable::setLabelFont(LyXFont & font)
352 {
353         labelfont_ = font;
354 }
355
356
357 void InsetCollapsable::scroll(BufferView & bv, float sx) const
358 {
359         UpdatableInset::scroll(bv, sx);
360 }
361
362
363 void InsetCollapsable::scroll(BufferView & bv, int offset) const
364 {
365         UpdatableInset::scroll(bv, offset);
366 }
367
368
369 Box const & InsetCollapsable::buttonDim() const
370 {
371         return button_dim;
372 }