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