]> git.lyx.org Git - lyx.git/blob - src/insets/insetcollapsable.C
hopefully fix tex2lyx linking.
[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 (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
297                         cur.dispatched();
298                         cur.noUpdate();
299                         break;
300                 }
301                 if (status() == Inlined)
302                         InsetText::doDispatch(cur, cmd);
303                 else if (status() == Open && !hitButton(cmd))
304                         InsetText::doDispatch(cur, cmd);
305                 else
306                         cur.undispatched();
307                 break;
308
309         case LFUN_MOUSE_MOTION:
310         case LFUN_MOUSE_DOUBLE:
311         case LFUN_MOUSE_TRIPLE:
312                 if (status_ == Inlined)
313                         InsetText::doDispatch(cur, cmd);
314                 else if (status() && !hitButton(cmd))
315                         InsetText::doDispatch(cur, cmd);
316                 else
317                         cur.undispatched();
318                 break;
319
320         case LFUN_MOUSE_RELEASE:
321                 if (cmd.button() == mouse_button::button3) {
322                         // Open the Inset configuration dialog
323                         showInsetDialog(&cur.bv());
324                         break;
325                 }
326
327                 if (status() == Inlined) {
328                         // The mouse click has to be within the inset!
329                         InsetText::doDispatch(cur, cmd);
330                         break;
331                 }
332
333                 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
334                         // Left button is clicked, the user asks to toggle the inset
335                         // visual state.
336                         cur.dispatched();
337                         cur.updateFlags(Update::Force | Update::FitCursor);
338                         if (status() == Collapsed) {
339                                 setStatus(cur, Open);
340                                 edit(cur, true);
341                         }
342                         else {
343                                 setStatus(cur, Collapsed);
344                         }
345                         cur.bv().cursor() = cur;
346                         break;
347                 }
348
349                 // The mouse click is within the opened inset.
350                 InsetText::doDispatch(cur, cmd);
351                 break;
352
353         case LFUN_INSET_TOGGLE:
354                 if (cmd.argument() == "open")
355                         setStatus(cur, Open);
356                 else if (cmd.argument() == "close")
357                         setStatus(cur, Collapsed);
358                 else if (cmd.argument() == "toggle" || cmd.argument().empty())
359                         if (isOpen()) {
360                                 setStatus(cur, Collapsed);
361                                 cur.forwardPosNoDescend();
362                         }
363                         else
364                                 setStatus(cur, Open);
365                 else // if assign or anything else
366                         cur.undispatched();
367                 cur.dispatched();
368                 break;
369
370         default:
371                 InsetText::doDispatch(cur, cmd);
372                 break;
373         }
374 }
375
376
377 bool InsetCollapsable::getStatus(LCursor & cur, FuncRequest const & cmd,
378                 FuncStatus & flag) const
379 {
380         switch (cmd.action) {
381
382         case LFUN_INSET_TOGGLE:
383                 if (cmd.argument() == "open" || cmd.argument() == "close" ||
384                     cmd.argument() == "toggle")
385                         flag.enabled(true);
386                 else
387                         flag.enabled(false);
388                 return true;
389
390         default:
391                 return InsetText::getStatus(cur, cmd, flag);
392         }
393 }
394
395
396 void InsetCollapsable::setLabel(docstring const & l)
397 {
398         label = l;
399 }
400
401
402 void InsetCollapsable::setStatus(LCursor & cur, CollapseStatus status)
403 {
404         status_ = status;
405         setButtonLabel();
406         if (status_ == Collapsed)
407                 cur.leaveInset(*this);
408 }
409
410
411 void InsetCollapsable::setLabelFont(LyXFont & font)
412 {
413         labelfont_ = font;
414 }
415
416 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp)
417 {
418         FloatList const & floats = bp.getLyXTextClass().floats();
419         FloatList::const_iterator it = floats[type];
420         // FIXME UNICODE
421         return (it == floats.end()) ? from_ascii(type) : _(it->second.name());
422 }
423
424
425 } // namespace lyx