2 * \file insetcollapsable.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
8 * \author Lars Gullik Bjønnes
10 * Full author contact details are available in file CREDITS.
15 #include "insetcollapsable.h"
18 #include "BufferView.h"
21 #include "dispatchresult.h"
24 #include "funcrequest.h"
25 #include "metricsinfo.h"
26 #include "paragraph.h"
28 #include "frontends/font_metrics.h"
29 #include "frontends/Painter.h"
30 #include "frontends/LyXView.h"
33 using lyx::graphics::PreviewLoader;
42 InsetCollapsable::InsetCollapsable(BufferParams const & bp,
43 CollapseStatus status)
44 : InsetText(bp), label("Label"), status_(status), openinlined_(false)
46 setAutoBreakRows(true);
47 setDrawFrame(InsetText::ALWAYS);
48 setFrameColor(LColor::collapsableframe);
49 setInsetName("Collapsable");
54 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
73 void InsetCollapsable::read(Buffer const & buf, LyXLex & lex)
75 bool token_found = false;
78 string const token = lex.getString();
79 if (token == "status") {
81 string const tmp_token = lex.getString();
83 if (tmp_token == "inlined") {
86 } else if (tmp_token == "collapsed") {
89 } else if (tmp_token == "open") {
93 lyxerr << "InsetCollapsable::read: Missing status!"
95 // Take countermeasures
99 lyxerr << "InsetCollapsable::read: Missing 'status'-tag!"
101 // take countermeasures
102 lex.pushToken(token);
105 InsetText::read(buf, lex);
108 status_ = isOpen() ? Open : Collapsed;
114 void InsetCollapsable::dimension_collapsed(Dimension & dim) const
116 font_metrics::buttonText(label, labelfont_, dim.wid, dim.asc, dim.des);
120 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
122 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
123 if (status_ == Inlined) {
124 InsetText::metrics(mi, dim);
126 dimension_collapsed(dim);
127 if (status_ == Open) {
128 InsetText::metrics(mi, textdim_);
129 openinlined_ = (textdim_.wid + dim.wid <= mi.base.textwidth);
131 dim.wid += textdim_.wid;
132 dim.des = max(dim.des, textdim_.des);
133 dim.asc = max(dim.asc, textdim_.asc);
135 dim.des += textdim_.height() + TEXT_TO_BOTTOM_OFFSET;
136 dim.wid = max(dim.wid, textdim_.wid);
140 dim.asc += TEXT_TO_INSET_OFFSET;
141 dim.des += TEXT_TO_INSET_OFFSET;
142 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
143 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
148 void InsetCollapsable::draw_collapsed(PainterInfo & pi, int x, int y) const
150 pi.pain.buttonText(x, y, label, labelfont_);
154 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
156 x += TEXT_TO_INSET_OFFSET;
157 y += TEXT_TO_INSET_OFFSET;
159 if (status_ == Inlined) {
160 InsetText::draw(pi, x, y);
163 dimension_collapsed(dimc);
164 int const aa = ascent();
165 button_dim.x1 = x + 0;
166 button_dim.x2 = x + dimc.width();
167 button_dim.y1 = y - aa + pi.base.bv->top_y();
168 button_dim.y2 = y - aa + pi.base.bv->top_y() + dimc.height();
170 draw_collapsed(pi, x, y - aa + dimc.asc);
171 if (status_ == Open) {
173 InsetText::draw(pi, x + dimc.width(),
174 y - aa + textdim_.asc);
176 InsetText::draw(pi, x, dimc.height()
177 + y - aa + textdim_.asc);
180 setPosCache(pi, x, y);
184 InsetOld::EDITABLE InsetCollapsable::editable() const
186 return status_ != Collapsed ? HIGHLY_EDITABLE : IS_EDITABLE;
190 bool InsetCollapsable::descendable() const
192 return status_ != Collapsed;
196 bool InsetCollapsable::hitButton(FuncRequest & cmd) const
198 return button_dim.contains(cmd.x, cmd.y);
202 string const InsetCollapsable::getNewLabel(string const & l) const
205 pos_type const max_length = 15;
206 pos_type const p_siz = paragraphs().begin()->size();
207 pos_type const n = min(max_length, p_siz);
210 for( ; i < n && j < p_siz; ++j) {
211 if (paragraphs().begin()->isInset(j))
213 label += paragraphs().begin()->getChar(j);
216 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
219 return label.empty() ? l : label;
223 void InsetCollapsable::edit(LCursor & cur, bool left)
225 //lyxerr << "InsetCollapsable: edit left/right" << endl;
227 InsetText::edit(cur, left);
232 InsetBase * InsetCollapsable::editXY(LCursor & cur, int x, int y)
235 //lyxerr << "InsetCollapsable: edit xy" << endl;
236 if (status_ == Collapsed) {
238 // We are not calling editXY() because the row cache of the
239 // inset might be invalid. 'Entering from the left' should be
241 InsetText::edit(cur, true);
244 return InsetText::editXY(cur, x, y);
248 void InsetCollapsable::priv_dispatch(LCursor & cur, FuncRequest & cmd)
250 //lyxerr << "\nInsetCollapsable::priv_dispatch (begin): cmd: " << cmd
251 // << " button y: " << button_dim.y2 << endl;
252 switch (cmd.action) {
253 case LFUN_MOUSE_PRESS:
254 if (status_ == Inlined)
255 InsetText::priv_dispatch(cur, cmd);
256 else if (status_ == Open && !hitButton(cmd))
257 InsetText::priv_dispatch(cur, cmd);
260 case LFUN_MOUSE_MOTION:
261 if (status_ == Inlined)
262 InsetText::priv_dispatch(cur, cmd);
263 else if (status_ == Open && !hitButton(cmd))
264 InsetText::priv_dispatch(cur, cmd);
267 case LFUN_MOUSE_RELEASE:
268 if (cmd.button() == mouse_button::button3) {
269 showInsetDialog(&cur.bv());
276 lyxerr << "InsetCollapsable::lfunMouseRelease 1" << endl;
282 FuncRequest cmd1 = cmd;
283 if (hitButton(cmd1)) {
284 lyxerr << "InsetCollapsable::lfunMouseRelease 2" << endl;
285 setStatus(Collapsed);
287 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
289 lyxerr << "InsetCollapsable::lfunMouseRelease 3" << endl;
290 InsetText::priv_dispatch(cur, cmd);
296 lyxerr << "InsetCollapsable::lfunMouseRelease 4" << endl;
297 InsetText::priv_dispatch(cur, cmd);
302 case LFUN_INSET_TOGGLE:
303 if (InsetText::text_.toggleInset(cur))
305 if (status_ == Open) {
309 setStatus(Collapsed);
311 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
315 InsetText::priv_dispatch(cur, cmd);
321 int InsetCollapsable::scroll(bool recursive) const
323 int sx = UpdatableInset::scroll(false);
326 sx += InsetText::scroll(false);
332 void InsetCollapsable::open()
334 if (status_ == Collapsed) // ...but not inlined
339 void InsetCollapsable::close()
341 setStatus(Collapsed);
345 void InsetCollapsable::setLabel(string const & l)
351 void InsetCollapsable::setStatus(CollapseStatus st)
358 void InsetCollapsable::setLabelFont(LyXFont & font)
364 void InsetCollapsable::scroll(BufferView & bv, float sx) const
366 UpdatableInset::scroll(bv, sx);
370 void InsetCollapsable::scroll(BufferView & bv, int offset) const
372 UpdatableInset::scroll(bv, offset);
376 Box const & InsetCollapsable::buttonDim() const