]> git.lyx.org Git - lyx.git/blob - src/insets/insetcollapsable.C
The speed patch: redraw only rows that have changed
[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 "FuncStatus.h"
23 #include "LColor.h"
24 #include "lyxlex.h"
25 #include "funcrequest.h"
26 #include "metricsinfo.h"
27 #include "paragraph.h"
28
29 #include "frontends/font_metrics.h"
30 #include "frontends/Painter.h"
31 #include "frontends/LyXView.h"
32
33
34 using lyx::graphics::PreviewLoader;
35
36 using std::endl;
37 using std::string;
38 using std::max;
39 using std::min;
40 using std::ostream;
41
42
43 InsetCollapsable::CollapseStatus InsetCollapsable::status() const
44 {
45         return (autoOpen_ && status_ != Inlined) ? Open : status_;
46 }
47
48
49 InsetCollapsable::InsetCollapsable
50                 (BufferParams const & bp, CollapseStatus status)
51         : InsetText(bp), label("Label"), status_(status),
52           openinlined_(false), autoOpen_(false)
53 {
54         setAutoBreakRows(true);
55         setDrawFrame(true);
56         setFrameColor(LColor::collapsableframe);
57         setInsetName("Collapsable");
58         setButtonLabel();
59 }
60
61
62 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
63 {
64         os << "status ";
65         switch (status_) {
66         case Open:
67                 os << "open";
68                 break;
69         case Collapsed:
70                 os << "collapsed";
71                 break;
72         case Inlined:
73                 os << "inlined";
74                 break;
75         }
76         os << "\n";
77         text_.write(buf, os);
78 }
79
80
81 void InsetCollapsable::read(Buffer const & buf, LyXLex & lex)
82 {
83         bool token_found = false;
84         if (lex.isOK()) {
85                 lex.next();
86                 string const token = lex.getString();
87                 if (token == "status") {
88                         lex.next();
89                         string const tmp_token = lex.getString();
90
91                         if (tmp_token == "inlined") {
92                                 status_ = Inlined;
93                                 token_found = true;
94                         } else if (tmp_token == "collapsed") {
95                                 status_ = Collapsed;
96                                 token_found = true;
97                         } else if (tmp_token == "open") {
98                                 status_ = Open;
99                                 token_found = true;
100                         } else {
101                                 lyxerr << "InsetCollapsable::read: Missing status!"
102                                        << endl;
103                                 // Take countermeasures
104                                 lex.pushToken(token);
105                         }
106                 } else {
107                         lyxerr << "InsetCollapsable::read: Missing 'status'-tag!"
108                                    << endl;
109                         // take countermeasures
110                         lex.pushToken(token);
111                 }
112         }
113         InsetText::read(buf, lex);
114
115         if (!token_found)
116                 status_ = isOpen() ? Open : Collapsed;
117
118         setButtonLabel();
119 }
120
121
122 Dimension InsetCollapsable::dimensionCollapsed() const
123 {
124         Dimension dim;
125         font_metrics::buttonText(label, labelfont_, dim.wid, dim.asc, dim.des);
126         return dim;
127 }
128
129
130 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
131 {
132         autoOpen_ = mi.base.bv->cursor().isInside(this);
133         mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
134
135         if (status() == Inlined) {
136                 InsetText::metrics(mi, dim);
137         } else {
138                 dim = dimensionCollapsed();
139                 if (status() == Open) {
140                         InsetText::metrics(mi, textdim_);
141                         bool oldopeninlined = openinlined_;
142                         openinlined_ = (textdim_.wid + dim.wid <= mi.base.textwidth);
143                         if (openinlined_ != oldopeninlined)
144                                 InsetText::metrics(mi, textdim_);
145                         if (openinlined_) {
146                                 dim.wid += textdim_.wid;
147                                 dim.des = max(dim.des - textdim_.asc + dim.asc, textdim_.des);
148                                 dim.asc = textdim_.asc;
149                         } else {
150                                 dim.des += textdim_.height() + TEXT_TO_BOTTOM_OFFSET;
151                                 dim.wid = max(dim.wid, textdim_.wid);
152                         }
153                 }
154         }
155         dim.asc += TEXT_TO_INSET_OFFSET;
156         dim.des += TEXT_TO_INSET_OFFSET;
157         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
158         mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
159         dim_ = dim;
160 }
161
162
163 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
164 {
165         const int xx = x + TEXT_TO_INSET_OFFSET;
166         if (status() == Inlined) {
167                 InsetText::draw(pi, xx, y);
168         } else {
169                 Dimension dimc = dimensionCollapsed();
170                 int const top  = y - ascent() + TEXT_TO_INSET_OFFSET;
171                 button_dim.x1 = xx + 0;
172                 button_dim.x2 = xx + dimc.width();
173                 button_dim.y1 = top;
174                 button_dim.y2 = top + dimc.height();
175
176                 pi.pain.buttonText(xx, top + dimc.asc, label, labelfont_);
177                 if (status() == Open) {
178                         int textx, texty;
179                         if (openinlined_) {
180                                 textx = xx + dimc.width();
181                                 texty = top + textdim_.asc;
182                         } else {
183                                 textx = xx;
184                                 texty = top + dimc.height() + textdim_.asc;
185                         }
186                         InsetText::draw(pi, textx, texty);
187                 }
188         }
189         setPosCache(pi, x, y);
190 }
191
192
193 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
194 {
195         x += TEXT_TO_INSET_OFFSET;
196         if (status() == Open) {
197                 if (openinlined_)
198                         x += dimensionCollapsed().wid;
199                 else
200                         y += dimensionCollapsed().des + textdim_.asc;
201         }
202         if (status() != Collapsed)
203                 InsetText::drawSelection(pi, x, y);
204 }
205
206
207 void InsetCollapsable::cursorPos
208         (CursorSlice const & sl, bool boundary, int & x, int & y) const
209 {
210         BOOST_ASSERT(status() != Collapsed);
211         
212         InsetText::cursorPos(sl, boundary, x, y);
213         
214         if (status() == Open) {
215                 if (openinlined_)
216                         x += dimensionCollapsed().wid;
217                 else
218                         y += dimensionCollapsed().height() - ascent()
219                                 + TEXT_TO_INSET_OFFSET + textdim_.asc;
220         }
221         x += TEXT_TO_INSET_OFFSET;
222 }
223
224
225 InsetBase::EDITABLE InsetCollapsable::editable() const
226 {
227         return status() != Collapsed ? HIGHLY_EDITABLE : IS_EDITABLE;
228 }
229
230
231 bool InsetCollapsable::descendable() const
232 {
233         return status() != Collapsed;
234 }
235
236
237 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
238 {
239         return button_dim.contains(cmd.x, cmd.y);
240 }
241
242
243 string const InsetCollapsable::getNewLabel(string const & l) const
244 {
245         string label;
246         pos_type const max_length = 15;
247         pos_type const p_siz = paragraphs().begin()->size();
248         pos_type const n = min(max_length, p_siz);
249         pos_type i = 0;
250         pos_type j = 0;
251         for (; i < n && j < p_siz; ++j) {
252                 if (paragraphs().begin()->isInset(j))
253                         continue;
254                 label += paragraphs().begin()->getChar(j);
255                 ++i;
256         }
257         if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
258                 label += "...";
259         }
260         return label.empty() ? l : label;
261 }
262
263
264 void InsetCollapsable::edit(LCursor & cur, bool left)
265 {
266         //lyxerr << "InsetCollapsable: edit left/right" << endl;
267         cur.push(*this);
268         InsetText::edit(cur, left);
269 }
270
271
272 InsetBase * InsetCollapsable::editXY(LCursor & cur, int x, int y)
273 {
274         //lyxerr << "InsetCollapsable: edit xy" << endl;
275         if (status() == Collapsed)
276                 return this;
277         cur.push(*this);
278         return InsetText::editXY(cur, x, y);
279 }
280
281
282 void InsetCollapsable::doDispatch(LCursor & cur, FuncRequest & cmd)
283 {
284         //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
285         //      << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
286
287         switch (cmd.action) {
288         case LFUN_MOUSE_PRESS:
289                 if (status() == Inlined)
290                         InsetText::doDispatch(cur, cmd);
291                 else if (status() == Open && !hitButton(cmd))
292                         InsetText::doDispatch(cur, cmd);
293                 else
294                         cur.noUpdate();
295                 break;
296
297         case LFUN_MOUSE_MOTION:
298         case LFUN_MOUSE_DOUBLE:
299         case LFUN_MOUSE_TRIPLE:
300                 if (status_ == Inlined)
301                         InsetText::doDispatch(cur, cmd);
302                 else if (status() && !hitButton(cmd))
303                         InsetText::doDispatch(cur, cmd);
304                 else
305                         cur.undispatched();
306                 break;
307
308         case LFUN_MOUSE_RELEASE:
309                 if (cmd.button() == mouse_button::button3) {
310                         showInsetDialog(&cur.bv());
311                         break;
312                 }
313
314                 switch (status()) {
315
316                 case Collapsed:
317                         //lyxerr << "InsetCollapsable::lfunMouseRelease 1" << endl;
318                         setStatus(cur, Open);
319                         edit(cur, true);
320                         cur.bv().cursor() = cur;
321                         break;
322
323                 case Open: {
324                         if (hitButton(cmd)) {
325                                 //lyxerr << "InsetCollapsable::lfunMouseRelease 2" << endl;
326                                 setStatus(cur, Collapsed);
327                                 cur.bv().cursor() = cur;
328                         } else {
329                                 //lyxerr << "InsetCollapsable::lfunMouseRelease 3" << endl;
330                                 InsetText::doDispatch(cur, cmd);
331                         }
332                         break;
333                 }
334
335                 case Inlined:
336                         //lyxerr << "InsetCollapsable::lfunMouseRelease 4" << endl;
337                         InsetText::doDispatch(cur, cmd);
338                         break;
339                 }
340                 break;
341
342         case LFUN_INSET_TOGGLE:
343                 if (cmd.argument == "open")
344                         setStatus(cur, Open);
345                 else if (cmd.argument == "close")
346                         setStatus(cur, Collapsed);
347                 else if (cmd.argument == "toggle" || cmd.argument.empty()) 
348                         if (isOpen()) {
349                                 setStatus(cur, Collapsed);
350                                 cur.forwardPosNoDescend();
351                         }
352                         else
353                                 setStatus(cur, Open);
354                 else // if assign or anything else
355                         cur.undispatched();
356                 cur.dispatched();
357                 break;
358
359         default:
360                 InsetText::doDispatch(cur, cmd);
361                 break;
362         }
363 }
364
365
366 bool InsetCollapsable::getStatus(LCursor & cur, FuncRequest const & cmd,
367                 FuncStatus & flag) const
368 {
369         switch (cmd.action) {
370
371         case LFUN_INSET_TOGGLE:
372                 if (cmd.argument == "open" || cmd.argument == "close" ||
373                     cmd.argument == "toggle")
374                         flag.enabled(true);
375                 else
376                         flag.enabled(false);
377                 return true;
378
379         default:
380                 return InsetText::getStatus(cur, cmd, flag);
381         }
382 }
383
384
385 void InsetCollapsable::setLabel(string const & l)
386 {
387         label = l;
388 }
389
390
391 void InsetCollapsable::setStatus(LCursor & cur, CollapseStatus status)
392 {
393         status_ = status;
394         setButtonLabel();
395         if (status_ == Collapsed)
396                 cur.leaveInset(*this);
397 }
398
399
400 void InsetCollapsable::setLabelFont(LyXFont & font)
401 {
402         labelfont_ = font;
403 }
404