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