]> git.lyx.org Git - lyx.git/blob - src/insets/insetcollapsable.C
cleanup after svn hang-up, #undef CursorShape. Should be compilable ganin now.
[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 using lyx::docstring;
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         docstring dlab(label.begin(), label.end());
126         font_metrics::buttonText(dlab, labelfont_, dim.wid, dim.asc, dim.des);
127         return dim;
128 }
129
130
131 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
132 {
133         autoOpen_ = mi.base.bv->cursor().isInside(this);
134         mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
135
136         if (status() == Inlined) {
137                 InsetText::metrics(mi, dim);
138         } else {
139                 dim = dimensionCollapsed();
140                 if (status() == Open) {
141                         InsetText::metrics(mi, textdim_);
142                         // This expression should not contain mi.base.texwidth
143                         openinlined_ = textdim_.wid < 0.5 * mi.base.bv->workWidth();
144                         if (openinlined_) {
145                                 // Correct for button width, and re-fit
146                                 mi.base.textwidth -= dim.wid;
147                                 InsetText::metrics(mi, textdim_);
148                                 dim.wid += textdim_.wid;
149                                 dim.des = max(dim.des - textdim_.asc + dim.asc, textdim_.des);
150                                 dim.asc = textdim_.asc;
151                         } else {
152                                 dim.des += textdim_.height() + TEXT_TO_BOTTOM_OFFSET;
153                                 dim.wid = max(dim.wid, textdim_.wid);
154                         }
155                 }
156         }
157         dim.asc += TEXT_TO_INSET_OFFSET;
158         dim.des += TEXT_TO_INSET_OFFSET;
159         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
160         mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
161         dim_ = dim;
162 }
163
164
165 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
166 {
167         const int xx = x + TEXT_TO_INSET_OFFSET;
168         if (status() == Inlined) {
169                 InsetText::draw(pi, xx, y);
170         } else {
171                 Dimension dimc = dimensionCollapsed();
172                 int const top  = y - ascent() + TEXT_TO_INSET_OFFSET;
173                 button_dim.x1 = xx + 0;
174                 button_dim.x2 = xx + dimc.width();
175                 button_dim.y1 = top;
176                 button_dim.y2 = top + dimc.height();
177
178                 docstring dlab(label.begin(), label.end());
179                 pi.pain.buttonText(xx, top + dimc.asc, dlab, labelfont_);
180
181                 if (status() == Open) {
182                         int textx, texty;
183                         if (openinlined_) {
184                                 textx = xx + dimc.width();
185                                 texty = top + textdim_.asc;
186                         } else {
187                                 textx = xx;
188                                 texty = top + dimc.height() + textdim_.asc;
189                         }
190                         InsetText::draw(pi, textx, texty);
191                 }
192         }
193         setPosCache(pi, x, y);
194 }
195
196
197 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
198 {
199         x += TEXT_TO_INSET_OFFSET;
200         if (status() == Open) {
201                 if (openinlined_)
202                         x += dimensionCollapsed().wid;
203                 else
204                         y += dimensionCollapsed().des + textdim_.asc;
205         }
206         if (status() != Collapsed)
207                 InsetText::drawSelection(pi, x, y);
208 }
209
210
211 void InsetCollapsable::cursorPos
212         (CursorSlice const & sl, bool boundary, int & x, int & y) const
213 {
214         BOOST_ASSERT(status() != Collapsed);
215
216         InsetText::cursorPos(sl, boundary, x, y);
217
218         if (status() == Open) {
219                 if (openinlined_)
220                         x += dimensionCollapsed().wid;
221                 else
222                         y += dimensionCollapsed().height() - ascent()
223                                 + TEXT_TO_INSET_OFFSET + textdim_.asc;
224         }
225         x += TEXT_TO_INSET_OFFSET;
226 }
227
228
229 InsetBase::EDITABLE InsetCollapsable::editable() const
230 {
231         return status() != Collapsed ? HIGHLY_EDITABLE : IS_EDITABLE;
232 }
233
234
235 bool InsetCollapsable::descendable() const
236 {
237         return status() != Collapsed;
238 }
239
240
241 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
242 {
243         return button_dim.contains(cmd.x, cmd.y);
244 }
245
246
247 string const InsetCollapsable::getNewLabel(string const & l) const
248 {
249         string label;
250         pos_type const max_length = 15;
251         pos_type const p_siz = paragraphs().begin()->size();
252         pos_type const n = min(max_length, p_siz);
253         pos_type i = 0;
254         pos_type j = 0;
255         for (; i < n && j < p_siz; ++j) {
256                 if (paragraphs().begin()->isInset(j))
257                         continue;
258                 label += paragraphs().begin()->getChar(j);
259                 ++i;
260         }
261         if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
262                 label += "...";
263         }
264         return label.empty() ? l : label;
265 }
266
267
268 void InsetCollapsable::edit(LCursor & cur, bool left)
269 {
270         //lyxerr << "InsetCollapsable: edit left/right" << endl;
271         cur.push(*this);
272         InsetText::edit(cur, left);
273 }
274
275
276 InsetBase * InsetCollapsable::editXY(LCursor & cur, int x, int y)
277 {
278         //lyxerr << "InsetCollapsable: edit xy" << endl;
279         if (status() == Collapsed)
280                 return this;
281         cur.push(*this);
282         return InsetText::editXY(cur, x, y);
283 }
284
285
286 void InsetCollapsable::doDispatch(LCursor & cur, FuncRequest & cmd)
287 {
288         //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
289         //      << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
290
291         switch (cmd.action) {
292         case LFUN_MOUSE_PRESS:
293                 if (status() == Inlined)
294                         InsetText::doDispatch(cur, cmd);
295                 else if (status() == Open && !hitButton(cmd))
296                         InsetText::doDispatch(cur, cmd);
297                 else
298                         cur.undispatched();
299                 break;
300
301         case LFUN_MOUSE_MOTION:
302         case LFUN_MOUSE_DOUBLE:
303         case LFUN_MOUSE_TRIPLE:
304                 if (status_ == Inlined)
305                         InsetText::doDispatch(cur, cmd);
306                 else if (status() && !hitButton(cmd))
307                         InsetText::doDispatch(cur, cmd);
308                 else
309                         cur.undispatched();
310                 break;
311
312         case LFUN_MOUSE_RELEASE:
313                 if (cmd.button() == mouse_button::button3) {
314                         showInsetDialog(&cur.bv());
315                         break;
316                 }
317
318                 switch (status()) {
319
320                 case Collapsed:
321                         //lyxerr << "InsetCollapsable::lfunMouseRelease 1" << endl;
322                         setStatus(cur, Open);
323                         edit(cur, true);
324                         cur.bv().cursor() = cur;
325                         break;
326
327                 case Open: {
328                         if (hitButton(cmd)) {
329                                 //lyxerr << "InsetCollapsable::lfunMouseRelease 2" << endl;
330                                 setStatus(cur, Collapsed);
331                                 cur.bv().cursor() = cur;
332                         } else {
333                                 //lyxerr << "InsetCollapsable::lfunMouseRelease 3" << endl;
334                                 InsetText::doDispatch(cur, cmd);
335                         }
336                         break;
337                 }
338
339                 case Inlined:
340                         //lyxerr << "InsetCollapsable::lfunMouseRelease 4" << endl;
341                         InsetText::doDispatch(cur, cmd);
342                         break;
343                 }
344                 break;
345
346         case LFUN_INSET_TOGGLE:
347                 if (cmd.argument() == "open")
348                         setStatus(cur, Open);
349                 else if (cmd.argument() == "close")
350                         setStatus(cur, Collapsed);
351                 else if (cmd.argument() == "toggle" || cmd.argument().empty())
352                         if (isOpen()) {
353                                 setStatus(cur, Collapsed);
354                                 cur.forwardPosNoDescend();
355                         }
356                         else
357                                 setStatus(cur, Open);
358                 else // if assign or anything else
359                         cur.undispatched();
360                 cur.dispatched();
361                 break;
362
363         default:
364                 InsetText::doDispatch(cur, cmd);
365                 break;
366         }
367 }
368
369
370 bool InsetCollapsable::getStatus(LCursor & cur, FuncRequest const & cmd,
371                 FuncStatus & flag) const
372 {
373         switch (cmd.action) {
374
375         case LFUN_INSET_TOGGLE:
376                 if (cmd.argument() == "open" || cmd.argument() == "close" ||
377                     cmd.argument() == "toggle")
378                         flag.enabled(true);
379                 else
380                         flag.enabled(false);
381                 return true;
382
383         default:
384                 return InsetText::getStatus(cur, cmd, flag);
385         }
386 }
387
388
389 void InsetCollapsable::setLabel(string const & l)
390 {
391         label = l;
392 }
393
394
395 void InsetCollapsable::setStatus(LCursor & cur, CollapseStatus status)
396 {
397         status_ = status;
398         setButtonLabel();
399         if (status_ == Collapsed)
400                 cur.leaveInset(*this);
401 }
402
403
404 void InsetCollapsable::setLabelFont(LyXFont & font)
405 {
406         labelfont_ = font;
407 }