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