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