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