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