]> git.lyx.org Git - lyx.git/blob - src/insets/insetcollapsable.C
5c2b3dee169dceeed6fe8c5e2f2aa1b34eefd23d
[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                         // 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                 pi.pain.buttonText(xx, top + dimc.asc, label, labelfont_);
178
179                 if (status() == Open) {
180                         int textx, texty;
181                         if (openinlined_) {
182                                 textx = xx + dimc.width();
183                                 texty = top + textdim_.asc;
184                         } else {
185                                 textx = xx;
186                                 texty = top + dimc.height() + textdim_.asc;
187                         }
188                         InsetText::draw(pi, textx, texty);
189                 }
190         }
191         setPosCache(pi, x, y);
192 }
193
194
195 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
196 {
197         x += TEXT_TO_INSET_OFFSET;
198         if (status() == Open) {
199                 if (openinlined_)
200                         x += dimensionCollapsed().wid;
201                 else
202                         y += dimensionCollapsed().des + textdim_.asc;
203         }
204         if (status() != Collapsed)
205                 InsetText::drawSelection(pi, x, y);
206 }
207
208
209 void InsetCollapsable::cursorPos
210         (CursorSlice const & sl, bool boundary, int & x, int & y) const
211 {
212         BOOST_ASSERT(status() != Collapsed);
213
214         InsetText::cursorPos(sl, boundary, x, y);
215
216         if (status() == Open) {
217                 if (openinlined_)
218                         x += dimensionCollapsed().wid;
219                 else
220                         y += dimensionCollapsed().height() - ascent()
221                                 + TEXT_TO_INSET_OFFSET + textdim_.asc;
222         }
223         x += TEXT_TO_INSET_OFFSET;
224 }
225
226
227 InsetBase::EDITABLE InsetCollapsable::editable() const
228 {
229         return status() != Collapsed ? HIGHLY_EDITABLE : IS_EDITABLE;
230 }
231
232
233 bool InsetCollapsable::descendable() const
234 {
235         return status() != Collapsed;
236 }
237
238
239 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
240 {
241         return button_dim.contains(cmd.x, cmd.y);
242 }
243
244
245 string const InsetCollapsable::getNewLabel(string const & l) const
246 {
247         string label;
248         pos_type const max_length = 15;
249         pos_type const p_siz = paragraphs().begin()->size();
250         pos_type const n = min(max_length, p_siz);
251         pos_type i = 0;
252         pos_type j = 0;
253         for (; i < n && j < p_siz; ++j) {
254                 if (paragraphs().begin()->isInset(j))
255                         continue;
256                 label += paragraphs().begin()->getChar(j);
257                 ++i;
258         }
259         if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
260                 label += "...";
261         }
262         return label.empty() ? l : label;
263 }
264
265
266 void InsetCollapsable::edit(LCursor & cur, bool left)
267 {
268         //lyxerr << "InsetCollapsable: edit left/right" << endl;
269         cur.push(*this);
270         InsetText::edit(cur, left);
271 }
272
273
274 InsetBase * InsetCollapsable::editXY(LCursor & cur, int x, int y)
275 {
276         //lyxerr << "InsetCollapsable: edit xy" << endl;
277         if (status() == Collapsed)
278                 return this;
279         cur.push(*this);
280         return InsetText::editXY(cur, x, y);
281 }
282
283
284 void InsetCollapsable::doDispatch(LCursor & cur, FuncRequest & cmd)
285 {
286         //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
287         //      << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
288
289         switch (cmd.action) {
290         case LFUN_MOUSE_PRESS:
291                 if (status() == Inlined)
292                         InsetText::doDispatch(cur, cmd);
293                 else if (status() == Open && !hitButton(cmd))
294                         InsetText::doDispatch(cur, cmd);
295                 else
296                         cur.undispatched();
297                 break;
298
299         case LFUN_MOUSE_MOTION:
300         case LFUN_MOUSE_DOUBLE:
301         case LFUN_MOUSE_TRIPLE:
302                 if (status_ == Inlined)
303                         InsetText::doDispatch(cur, cmd);
304                 else if (status() && !hitButton(cmd))
305                         InsetText::doDispatch(cur, cmd);
306                 else
307                         cur.undispatched();
308                 break;
309
310         case LFUN_MOUSE_RELEASE:
311                 if (cmd.button() == mouse_button::button3) {
312                         showInsetDialog(&cur.bv());
313                         break;
314                 }
315
316                 switch (status()) {
317
318                 case Collapsed:
319                         //lyxerr << "InsetCollapsable::lfunMouseRelease 1" << endl;
320                         setStatus(cur, Open);
321                         edit(cur, true);
322                         cur.bv().cursor() = cur;
323                         break;
324
325                 case Open: {
326                         if (hitButton(cmd)) {
327                                 //lyxerr << "InsetCollapsable::lfunMouseRelease 2" << endl;
328                                 setStatus(cur, Collapsed);
329                                 cur.bv().cursor() = cur;
330                         } else {
331                                 //lyxerr << "InsetCollapsable::lfunMouseRelease 3" << endl;
332                                 InsetText::doDispatch(cur, cmd);
333                         }
334                         break;
335                 }
336
337                 case Inlined:
338                         //lyxerr << "InsetCollapsable::lfunMouseRelease 4" << endl;
339                         InsetText::doDispatch(cur, cmd);
340                         break;
341                 }
342                 break;
343
344         case LFUN_INSET_TOGGLE:
345                 if (cmd.argument == "open")
346                         setStatus(cur, Open);
347                 else if (cmd.argument == "close")
348                         setStatus(cur, Collapsed);
349                 else if (cmd.argument == "toggle" || cmd.argument.empty())
350                         if (isOpen()) {
351                                 setStatus(cur, Collapsed);
352                                 cur.forwardPosNoDescend();
353                         }
354                         else
355                                 setStatus(cur, Open);
356                 else // if assign or anything else
357                         cur.undispatched();
358                 cur.dispatched();
359                 break;
360
361         default:
362                 InsetText::doDispatch(cur, cmd);
363                 break;
364         }
365 }
366
367
368 bool InsetCollapsable::getStatus(LCursor & cur, FuncRequest const & cmd,
369                 FuncStatus & flag) const
370 {
371         switch (cmd.action) {
372
373         case LFUN_INSET_TOGGLE:
374                 if (cmd.argument == "open" || cmd.argument == "close" ||
375                     cmd.argument == "toggle")
376                         flag.enabled(true);
377                 else
378                         flag.enabled(false);
379                 return true;
380
381         default:
382                 return InsetText::getStatus(cur, cmd, flag);
383         }
384 }
385
386
387 void InsetCollapsable::setLabel(string const & l)
388 {
389         label = l;
390 }
391
392
393 void InsetCollapsable::setStatus(LCursor & cur, CollapseStatus status)
394 {
395         status_ = status;
396         setButtonLabel();
397         if (status_ == Collapsed)
398                 cur.leaveInset(*this);
399 }
400
401
402 void InsetCollapsable::setLabelFont(LyXFont & font)
403 {
404         labelfont_ = font;
405 }