]> git.lyx.org Git - lyx.git/blob - src/insets/insetcollapsable.C
Output docbook as utf8. Probably quite a bit more work needed, but then help form...
[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 using lyx::docstring;
36 using lyx::graphics::PreviewLoader;
37
38 using std::endl;
39 using std::string;
40 using std::max;
41 using std::min;
42 using std::ostream;
43
44
45 InsetCollapsable::CollapseStatus InsetCollapsable::status() const
46 {
47         return (autoOpen_ && status_ != Inlined) ? Open : status_;
48 }
49
50
51 InsetCollapsable::InsetCollapsable
52                 (BufferParams const & bp, CollapseStatus status)
53         : InsetText(bp), label(lyx::from_ascii("Label")), status_(status),
54           openinlined_(false), autoOpen_(false)
55 {
56         setAutoBreakRows(true);
57         setDrawFrame(true);
58         setFrameColor(LColor::collapsableframe);
59         setInsetName("Collapsable");
60         setButtonLabel();
61 }
62
63
64 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
65 {
66         os << "status ";
67         switch (status_) {
68         case Open:
69                 os << "open";
70                 break;
71         case Collapsed:
72                 os << "collapsed";
73                 break;
74         case Inlined:
75                 os << "inlined";
76                 break;
77         }
78         os << "\n";
79         text_.write(buf, os);
80 }
81
82
83 void InsetCollapsable::read(Buffer const & buf, LyXLex & lex)
84 {
85         bool token_found = false;
86         if (lex.isOK()) {
87                 lex.next();
88                 string const token = lex.getString();
89                 if (token == "status") {
90                         lex.next();
91                         string const tmp_token = lex.getString();
92
93                         if (tmp_token == "inlined") {
94                                 status_ = Inlined;
95                                 token_found = true;
96                         } else if (tmp_token == "collapsed") {
97                                 status_ = Collapsed;
98                                 token_found = true;
99                         } else if (tmp_token == "open") {
100                                 status_ = Open;
101                                 token_found = true;
102                         } else {
103                                 lyxerr << "InsetCollapsable::read: Missing status!"
104                                        << endl;
105                                 // Take countermeasures
106                                 lex.pushToken(token);
107                         }
108                 } else {
109                         lyxerr << "InsetCollapsable::read: Missing 'status'-tag!"
110                                    << endl;
111                         // take countermeasures
112                         lex.pushToken(token);
113                 }
114         }
115         InsetText::read(buf, lex);
116
117         if (!token_found)
118                 status_ = isOpen() ? Open : Collapsed;
119
120         setButtonLabel();
121 }
122
123
124 Dimension InsetCollapsable::dimensionCollapsed() const
125 {
126         Dimension dim;
127         docstring dlab(label.begin(), label.end());
128         theFontMetrics(labelfont_).buttonText(
129                 dlab, dim.wid, dim.asc, dim.des);
130         return dim;
131 }
132
133
134 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
135 {
136         autoOpen_ = mi.base.bv->cursor().isInside(this);
137         mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
138
139         if (status() == Inlined) {
140                 InsetText::metrics(mi, dim);
141         } else {
142                 dim = dimensionCollapsed();
143                 if (status() == Open) {
144                         InsetText::metrics(mi, textdim_);
145                         // This expression should not contain mi.base.texwidth
146                         openinlined_ = textdim_.wid < 0.5 * mi.base.bv->workWidth();
147                         if (openinlined_) {
148                                 // Correct for button width, and re-fit
149                                 mi.base.textwidth -= dim.wid;
150                                 InsetText::metrics(mi, textdim_);
151                                 dim.wid += textdim_.wid;
152                                 dim.des = max(dim.des - textdim_.asc + dim.asc, textdim_.des);
153                                 dim.asc = textdim_.asc;
154                         } else {
155                                 dim.des += textdim_.height() + TEXT_TO_BOTTOM_OFFSET;
156                                 dim.wid = max(dim.wid, textdim_.wid);
157                         }
158                 }
159         }
160         dim.asc += TEXT_TO_INSET_OFFSET;
161         dim.des += TEXT_TO_INSET_OFFSET;
162         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
163         mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
164         dim_ = dim;
165 }
166
167
168 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
169 {
170         const int xx = x + TEXT_TO_INSET_OFFSET;
171         if (status() == Inlined) {
172                 InsetText::draw(pi, xx, y);
173         } else {
174                 Dimension dimc = dimensionCollapsed();
175                 int const top  = y - ascent() + TEXT_TO_INSET_OFFSET;
176                 button_dim.x1 = xx + 0;
177                 button_dim.x2 = xx + dimc.width();
178                 button_dim.y1 = top;
179                 button_dim.y2 = top + dimc.height();
180
181                 docstring dlab(label.begin(), label.end());
182                 pi.pain.buttonText(xx, top + dimc.asc, dlab, labelfont_);
183
184                 if (status() == Open) {
185                         int textx, texty;
186                         if (openinlined_) {
187                                 textx = xx + dimc.width();
188                                 texty = top + textdim_.asc;
189                         } else {
190                                 textx = xx;
191                                 texty = top + dimc.height() + textdim_.asc;
192                         }
193                         InsetText::draw(pi, textx, texty);
194                 }
195         }
196         setPosCache(pi, x, y);
197 }
198
199
200 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
201 {
202         x += TEXT_TO_INSET_OFFSET;
203         if (status() == Open) {
204                 if (openinlined_)
205                         x += dimensionCollapsed().wid;
206                 else
207                         y += dimensionCollapsed().des + textdim_.asc;
208         }
209         if (status() != Collapsed)
210                 InsetText::drawSelection(pi, x, y);
211 }
212
213
214 void InsetCollapsable::cursorPos(BufferView const & bv, 
215                 CursorSlice const & sl, bool boundary, int & x, int & y) const
216 {
217         BOOST_ASSERT(status() != Collapsed);
218
219         InsetText::cursorPos(bv, sl, boundary, x, y);
220
221         if (status() == Open) {
222                 if (openinlined_)
223                         x += dimensionCollapsed().wid;
224                 else
225                         y += dimensionCollapsed().height() - ascent()
226                                 + TEXT_TO_INSET_OFFSET + textdim_.asc;
227         }
228         x += TEXT_TO_INSET_OFFSET;
229 }
230
231
232 InsetBase::EDITABLE InsetCollapsable::editable() const
233 {
234         return status() != Collapsed ? HIGHLY_EDITABLE : IS_EDITABLE;
235 }
236
237
238 bool InsetCollapsable::descendable() const
239 {
240         return status() != Collapsed;
241 }
242
243
244 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
245 {
246         return button_dim.contains(cmd.x, cmd.y);
247 }
248
249
250 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
251 {
252         docstring label;
253         pos_type const max_length = 15;
254         pos_type const p_siz = paragraphs().begin()->size();
255         pos_type const n = min(max_length, p_siz);
256         pos_type i = 0;
257         pos_type j = 0;
258         for (; i < n && j < p_siz; ++j) {
259                 if (paragraphs().begin()->isInset(j))
260                         continue;
261                 label += paragraphs().begin()->getChar(j);
262                 ++i;
263         }
264         if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
265                 label += "...";
266         }
267         return label.empty() ? l : label;
268 }
269
270
271 void InsetCollapsable::edit(LCursor & cur, bool left)
272 {
273         //lyxerr << "InsetCollapsable: edit left/right" << endl;
274         cur.push(*this);
275         InsetText::edit(cur, left);
276 }
277
278
279 InsetBase * InsetCollapsable::editXY(LCursor & cur, int x, int y)
280 {
281         //lyxerr << "InsetCollapsable: edit xy" << endl;
282         if (status() == Collapsed)
283                 return this;
284         cur.push(*this);
285         return InsetText::editXY(cur, x, y);
286 }
287
288
289 void InsetCollapsable::doDispatch(LCursor & cur, FuncRequest & cmd)
290 {
291         //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
292         //      << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
293
294         switch (cmd.action) {
295         case LFUN_MOUSE_PRESS:
296                 if (status() == Inlined)
297                         InsetText::doDispatch(cur, cmd);
298                 else if (status() == Open && !hitButton(cmd))
299                         InsetText::doDispatch(cur, cmd);
300                 else
301                         cur.undispatched();
302                 break;
303
304         case LFUN_MOUSE_MOTION:
305         case LFUN_MOUSE_DOUBLE:
306         case LFUN_MOUSE_TRIPLE:
307                 if (status_ == Inlined)
308                         InsetText::doDispatch(cur, cmd);
309                 else if (status() && !hitButton(cmd))
310                         InsetText::doDispatch(cur, cmd);
311                 else
312                         cur.undispatched();
313                 break;
314
315         case LFUN_MOUSE_RELEASE:
316                 if (cmd.button() == mouse_button::button3) {
317                         showInsetDialog(&cur.bv());
318                         break;
319                 }
320
321                 switch (status()) {
322
323                 case Collapsed:
324                         //lyxerr << "InsetCollapsable::lfunMouseRelease 1" << endl;
325                         setStatus(cur, Open);
326                         edit(cur, true);
327                         cur.bv().cursor() = cur;
328                         break;
329
330                 case Open: {
331                         if (hitButton(cmd)) {
332                                 //lyxerr << "InsetCollapsable::lfunMouseRelease 2" << endl;
333                                 setStatus(cur, Collapsed);
334                                 cur.bv().cursor() = cur;
335                         } else {
336                                 //lyxerr << "InsetCollapsable::lfunMouseRelease 3" << endl;
337                                 InsetText::doDispatch(cur, cmd);
338                         }
339                         break;
340                 }
341
342                 case Inlined:
343                         //lyxerr << "InsetCollapsable::lfunMouseRelease 4" << endl;
344                         InsetText::doDispatch(cur, cmd);
345                         break;
346                 }
347                 break;
348
349         case LFUN_INSET_TOGGLE:
350                 if (cmd.argument() == "open")
351                         setStatus(cur, Open);
352                 else if (cmd.argument() == "close")
353                         setStatus(cur, Collapsed);
354                 else if (cmd.argument() == "toggle" || cmd.argument().empty())
355                         if (isOpen()) {
356                                 setStatus(cur, Collapsed);
357                                 cur.forwardPosNoDescend();
358                         }
359                         else
360                                 setStatus(cur, Open);
361                 else // if assign or anything else
362                         cur.undispatched();
363                 cur.dispatched();
364                 break;
365
366         default:
367                 InsetText::doDispatch(cur, cmd);
368                 break;
369         }
370 }
371
372
373 bool InsetCollapsable::getStatus(LCursor & cur, FuncRequest const & cmd,
374                 FuncStatus & flag) const
375 {
376         switch (cmd.action) {
377
378         case LFUN_INSET_TOGGLE:
379                 if (cmd.argument() == "open" || cmd.argument() == "close" ||
380                     cmd.argument() == "toggle")
381                         flag.enabled(true);
382                 else
383                         flag.enabled(false);
384                 return true;
385
386         default:
387                 return InsetText::getStatus(cur, cmd, flag);
388         }
389 }
390
391
392 void InsetCollapsable::setLabel(docstring const & l)
393 {
394         label = l;
395 }
396
397
398 void InsetCollapsable::setStatus(LCursor & cur, CollapseStatus status)
399 {
400         status_ = status;
401         setButtonLabel();
402         if (status_ == Collapsed)
403                 cur.leaveInset(*this);
404 }
405
406
407 void InsetCollapsable::setLabelFont(LyXFont & font)
408 {
409         labelfont_ = font;
410 }
411
412 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp)
413 {
414         FloatList const & floats = bp.getLyXTextClass().floats();
415         FloatList::const_iterator it = floats[type];
416         // FIXME UNICODE
417         return (it == floats.end()) ? lyx::from_ascii(type) : _(it->second.name());
418 }