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