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