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