]> git.lyx.org Git - lyx.git/blob - src/insets/insetcollapsable.C
0adaaf6a77d05fbe1db5b6b472508f3ee310a78c
[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), mouse_hover_(false)
57 {
58         setAutoBreakRows(true);
59         setDrawFrame(true);
60         setFrameColor(LColor::collapsableframe);
61         setInsetName(from_ascii("Collapsable"));
62         setButtonLabel();
63 }
64
65
66 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs):
67         InsetText(rhs),         
68         labelfont_(rhs.labelfont_),
69         button_dim(rhs.button_dim),
70         topx(rhs.topx),
71         topbaseline(rhs.topbaseline),
72         label(rhs.label),
73         status_(rhs.status_),
74         openinlined_(rhs.openinlined_),
75         autoOpen_(rhs.autoOpen_),
76         textdim_(rhs.textdim_),
77         // the sole purpose of this copy constructor
78         mouse_hover_(false)
79 {
80 }
81
82
83 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
84 {
85         os << "status ";
86         switch (status_) {
87         case Open:
88                 os << "open";
89                 break;
90         case Collapsed:
91                 os << "collapsed";
92                 break;
93         case Inlined:
94                 os << "inlined";
95                 break;
96         }
97         os << "\n";
98         text_.write(buf, os);
99 }
100
101
102 void InsetCollapsable::read(Buffer const & buf, LyXLex & lex)
103 {
104         bool token_found = false;
105         if (lex.isOK()) {
106                 lex.next();
107                 string const token = lex.getString();
108                 if (token == "status") {
109                         lex.next();
110                         string const tmp_token = lex.getString();
111
112                         if (tmp_token == "inlined") {
113                                 status_ = Inlined;
114                                 token_found = true;
115                         } else if (tmp_token == "collapsed") {
116                                 status_ = Collapsed;
117                                 token_found = true;
118                         } else if (tmp_token == "open") {
119                                 status_ = Open;
120                                 token_found = true;
121                         } else {
122                                 lyxerr << "InsetCollapsable::read: Missing status!"
123                                        << endl;
124                                 // Take countermeasures
125                                 lex.pushToken(token);
126                         }
127                 } else {
128                         lyxerr << "InsetCollapsable::read: Missing 'status'-tag!"
129                                    << endl;
130                         // take countermeasures
131                         lex.pushToken(token);
132                 }
133         }
134         InsetText::read(buf, lex);
135
136         if (!token_found)
137                 status_ = isOpen() ? Open : Collapsed;
138
139         setButtonLabel();
140 }
141
142
143 Dimension InsetCollapsable::dimensionCollapsed() const
144 {
145         Dimension dim;
146         theFontMetrics(labelfont_).buttonText(
147                 label, dim.wid, dim.asc, dim.des);
148         return dim;
149 }
150
151
152 bool InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
153 {
154         autoOpen_ = mi.base.bv->cursor().isInside(this);
155         mi.base.textwidth -= (int) (1.5 * TEXT_TO_INSET_OFFSET);
156
157         if (status() == Inlined) {
158                 InsetText::metrics(mi, dim);
159         } else {
160                 dim = dimensionCollapsed();
161                 if (status() == Open) {
162                         InsetText::metrics(mi, textdim_);
163                         // This expression should not contain mi.base.texwidth
164                         openinlined_ = !hasFixedWidth() 
165                                 && textdim_.wid < 0.5 * mi.base.bv->workWidth();
166                         if (openinlined_) {
167                                 // Correct for button width, and re-fit
168                                 mi.base.textwidth -= dim.wid;
169                                 InsetText::metrics(mi, textdim_);
170                                 dim.wid += textdim_.wid;
171                                 dim.des = max(dim.des - textdim_.asc + dim.asc, textdim_.des);
172                                 dim.asc = textdim_.asc;
173                         } else {
174                                 dim.des += textdim_.height() + TEXT_TO_BOTTOM_OFFSET;
175                                 dim.wid = max(dim.wid, textdim_.wid);
176                         }
177                 }
178         }
179         dim.asc += TEXT_TO_INSET_OFFSET;
180         dim.des += TEXT_TO_INSET_OFFSET;
181         dim.wid += (int) (1.5 * TEXT_TO_INSET_OFFSET);
182         mi.base.textwidth += (int) (1.5 * TEXT_TO_INSET_OFFSET);
183         bool const changed = dim_ != dim;
184         dim_ = dim;
185         return changed;
186 }
187
188
189 bool InsetCollapsable::setMouseHover(bool mouse_hover)
190 {
191         mouse_hover_ = mouse_hover;
192         return true;
193 }
194
195
196 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
197 {
198         const int xx = x + TEXT_TO_INSET_OFFSET;
199         if (status() == Inlined) {
200                 InsetText::draw(pi, xx, y);
201         } else {
202                 Dimension dimc = dimensionCollapsed();
203                 int const top  = y - ascent() + TEXT_TO_INSET_OFFSET;
204                 button_dim.x1 = xx + 0;
205                 button_dim.x2 = xx + dimc.width();
206                 button_dim.y1 = top;
207                 button_dim.y2 = top + dimc.height();
208
209                 pi.pain.buttonText(xx, top + dimc.asc, label, labelfont_, mouse_hover_);
210
211                 if (status() == Open) {
212                         int textx, texty;
213                         if (openinlined_) {
214                                 textx = xx + dimc.width();
215                                 texty = top + textdim_.asc;
216                         } else {
217                                 textx = xx;
218                                 texty = top + dimc.height() + textdim_.asc;
219                         }
220                         InsetText::draw(pi, textx, texty);
221                 }
222         }
223         setPosCache(pi, x, y);
224 }
225
226
227 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
228 {
229         x += TEXT_TO_INSET_OFFSET;
230         if (status() == Open) {
231                 if (openinlined_)
232                         x += dimensionCollapsed().wid;
233                 else
234                         y += dimensionCollapsed().des + textdim_.asc;
235         }
236         if (status() != Collapsed)
237                 InsetText::drawSelection(pi, x, y);
238 }
239
240
241 void InsetCollapsable::cursorPos(BufferView const & bv, 
242                 CursorSlice const & sl, bool boundary, int & x, int & y) const
243 {
244         BOOST_ASSERT(status() != Collapsed);
245
246         InsetText::cursorPos(bv, sl, boundary, x, y);
247
248         if (status() == Open) {
249                 if (openinlined_)
250                         x += dimensionCollapsed().wid;
251                 else
252                         y += dimensionCollapsed().height() - ascent()
253                                 + TEXT_TO_INSET_OFFSET + textdim_.asc;
254         }
255         x += TEXT_TO_INSET_OFFSET;
256 }
257
258
259 InsetBase::EDITABLE InsetCollapsable::editable() const
260 {
261         return status() != Collapsed ? HIGHLY_EDITABLE : IS_EDITABLE;
262 }
263
264
265 bool InsetCollapsable::descendable() const
266 {
267         return status() != Collapsed;
268 }
269
270
271 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
272 {
273         return button_dim.contains(cmd.x, cmd.y);
274 }
275
276
277 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
278 {
279         docstring label;
280         pos_type const max_length = 15;
281         pos_type const p_siz = paragraphs().begin()->size();
282         pos_type const n = min(max_length, p_siz);
283         pos_type i = 0;
284         pos_type j = 0;
285         for (; i < n && j < p_siz; ++j) {
286                 if (paragraphs().begin()->isInset(j))
287                         continue;
288                 label += paragraphs().begin()->getChar(j);
289                 ++i;
290         }
291         if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
292                 label += "...";
293         }
294         return label.empty() ? l : label;
295 }
296
297
298 void InsetCollapsable::edit(LCursor & cur, bool left)
299 {
300         //lyxerr << "InsetCollapsable: edit left/right" << endl;
301         cur.push(*this);
302         InsetText::edit(cur, left);
303 }
304
305
306 InsetBase * InsetCollapsable::editXY(LCursor & cur, int x, int y)
307 {
308         //lyxerr << "InsetCollapsable: edit xy" << endl;
309         if (status() == Collapsed || (button_dim.contains(x, y) && status() != Inlined))
310                 return this;
311         cur.push(*this);
312         return InsetText::editXY(cur, x, y);
313 }
314
315
316 void InsetCollapsable::doDispatch(LCursor & cur, FuncRequest & cmd)
317 {
318         //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
319         //      << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
320
321         switch (cmd.action) {
322         case LFUN_MOUSE_PRESS:
323                 if (cmd.button() == mouse_button::button1 && hitButton(cmd) && status() != Inlined) {
324                         // reset selection if necessary (see bug 3060)
325                         if (cur.selection())
326                                 cur.bv().cursor().clearSelection();
327                         else
328                                 cur.noUpdate();
329                         cur.dispatched();
330                         break;
331                 }
332                 if (status() == Inlined)
333                         InsetText::doDispatch(cur, cmd);
334                 else if (status() == Open && !hitButton(cmd))
335                         InsetText::doDispatch(cur, cmd);
336                 else
337                         cur.undispatched();
338                 break;
339
340         case LFUN_MOUSE_MOTION:
341         case LFUN_MOUSE_DOUBLE:
342         case LFUN_MOUSE_TRIPLE:
343                 if (status_ == Inlined)
344                         InsetText::doDispatch(cur, cmd);
345                 else if (status() && !hitButton(cmd))
346                         InsetText::doDispatch(cur, cmd);
347                 else
348                         cur.undispatched();
349                 break;
350
351         case LFUN_MOUSE_RELEASE:
352                 if (cmd.button() == mouse_button::button3) {
353                         // Open the Inset configuration dialog
354                         showInsetDialog(&cur.bv());
355                         break;
356                 }
357
358                 if (status() == Inlined) {
359                         // The mouse click has to be within the inset!
360                         InsetText::doDispatch(cur, cmd);
361                         break;
362                 }
363
364                 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
365                         // if we are selecting, we do not want to
366                         // toggle the inset.
367                         if (cur.selection())
368                                 break;
369                         // Left button is clicked, the user asks to
370                         // toggle the inset visual state.
371                         cur.dispatched();
372                         cur.updateFlags(Update::Force | Update::FitCursor);
373                         if (status() == Collapsed) {
374                                 setStatus(cur, Open);
375                                 edit(cur, true);
376                         }
377                         else {
378                                 setStatus(cur, Collapsed);
379                         }
380                         cur.bv().cursor() = cur;
381                         break;
382                 }
383
384                 // The mouse click is within the opened inset.
385                 if (status() == Open)
386                         InsetText::doDispatch(cur, cmd);
387                 break;
388
389         case LFUN_INSET_TOGGLE:
390                 if (cmd.argument() == "open")
391                         setStatus(cur, Open);
392                 else if (cmd.argument() == "close")
393                         setStatus(cur, Collapsed);
394                 else if (cmd.argument() == "toggle" || cmd.argument().empty())
395                         if (isOpen()) {
396                                 setStatus(cur, Collapsed);
397                                 cur.forwardPosNoDescend();
398                         }
399                         else
400                                 setStatus(cur, Open);
401                 else // if assign or anything else
402                         cur.undispatched();
403                 cur.dispatched();
404                 break;
405
406         default:
407                 InsetText::doDispatch(cur, cmd);
408                 break;
409         }
410 }
411
412
413 bool InsetCollapsable::getStatus(LCursor & cur, FuncRequest const & cmd,
414                 FuncStatus & flag) const
415 {
416         switch (cmd.action) {
417
418         case LFUN_INSET_TOGGLE:
419                 if (cmd.argument() == "open" || cmd.argument() == "close" ||
420                     cmd.argument() == "toggle")
421                         flag.enabled(true);
422                 else
423                         flag.enabled(false);
424                 return true;
425
426         default:
427                 return InsetText::getStatus(cur, cmd, flag);
428         }
429 }
430
431
432 void InsetCollapsable::setLabel(docstring const & l)
433 {
434         label = l;
435 }
436
437
438 void InsetCollapsable::setStatus(LCursor & cur, CollapseStatus status)
439 {
440         status_ = status;
441         setButtonLabel();
442         if (status_ == Collapsed)
443                 cur.leaveInset(*this);
444         // Because we save CollapseStatus in lyx file, change of status
445         // should lead to a dirty buffer. (This fixes bug 2993).
446         cur.bv().buffer()->markDirty();
447 }
448
449
450 void InsetCollapsable::setLabelFont(LyXFont & font)
451 {
452         labelfont_ = font;
453 }
454
455 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
456 {
457         FloatList const & floats = bp.getLyXTextClass().floats();
458         FloatList::const_iterator it = floats[type];
459         // FIXME UNICODE
460         return (it == floats.end()) ? from_ascii(type) : _(it->second.name());
461 }
462
463
464 } // namespace lyx