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