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 : inset(bp), label("Label"), status_(status)
47 inset.setAutoBreakRows(true);
48 inset.setDrawFrame(InsetText::ALWAYS);
49 inset.setFrameColor(LColor::collapsableframe);
50 setInsetName("Collapsable");
55 InsetCollapsable::InsetCollapsable(InsetCollapsable const & in)
56 : UpdatableInset(in), inset(in.inset),
57 labelfont_(in.labelfont_), label(in.label), status_(in.status_)
64 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
79 inset.text_.write(buf, os);
83 void InsetCollapsable::read(Buffer const & buf, LyXLex & lex)
85 bool token_found = false;
88 string const token = lex.getString();
89 if (token == "status") {
91 string const tmp_token = lex.getString();
93 if (tmp_token == "inlined") {
96 } else if (tmp_token == "collapsed") {
99 } else if (tmp_token == "open") {
103 lyxerr << "InsetCollapsable::read: Missing status!"
105 // Take countermeasures
106 lex.pushToken(token);
109 lyxerr << "InsetCollapsable::Read: Missing 'status'-tag!"
111 // take countermeasures
112 lex.pushToken(token);
115 inset.read(buf, lex);
118 status_ = isOpen() ? Open : Collapsed;
124 void InsetCollapsable::dimension_collapsed(Dimension & dim) const
126 font_metrics::buttonText(label, labelfont_, dim.wid, dim.asc, dim.des);
130 int InsetCollapsable::height_collapsed() const
133 font_metrics::buttonText(label, labelfont_, dim.wid, dim.asc, dim.des);
134 return dim.asc + dim.des;
138 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
140 if (status_ == Inlined) {
141 inset.metrics(mi, dim);
143 dimension_collapsed(dim);
144 if (status_ == Open) {
146 inset.metrics(mi, insetdim);
147 dim.des += insetdim.height() + TEXT_TO_BOTTOM_OFFSET;
148 dim.wid = max(dim.wid, insetdim.wid);
155 void InsetCollapsable::draw_collapsed(PainterInfo & pi, int x, int y) const
157 pi.pain.buttonText(x, y, label, labelfont_);
161 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
163 setPosCache(pi, x, y);
165 if (status_ == Inlined) {
166 inset.draw(pi, x, y);
169 dimension_collapsed(dimc);
170 int const aa = ascent();
171 button_dim.x1 = x + 0;
172 button_dim.x2 = x + dimc.width();
173 button_dim.y1 = y - aa;
174 button_dim.y2 = y - aa + dimc.height();
176 draw_collapsed(pi, x, y);
177 if (status_ == Open) {
180 inset.draw(pi, x, y - aa + dimc.height() + inset.ascent());
186 InsetOld::EDITABLE InsetCollapsable::editable() const
188 return status_ != Collapsed ? HIGHLY_EDITABLE : IS_EDITABLE;
192 bool InsetCollapsable::descendable() const
194 return status_ != Collapsed;
199 InsetCollapsable::lfunMouseRelease(LCursor & cur, FuncRequest const & cmd)
201 if (cmd.button() == mouse_button::button3) {
202 lyxerr << "InsetCollapsable::lfunMouseRelease 0" << endl;
203 showInsetDialog(&cur.bv());
204 return DispatchResult(true, true);
209 lyxerr << "InsetCollapsable::lfunMouseRelease 1" << endl;
212 return DispatchResult(true, true);
215 if (hitButton(cmd)) {
216 lyxerr << "InsetCollapsable::lfunMouseRelease 2" << endl;
217 setStatus(Collapsed);
218 return DispatchResult(false, FINISHED_RIGHT);
220 lyxerr << "InsetCollapsable::lfunMouseRelease 3" << endl;
221 return inset.dispatch(cur, cmd);
224 return inset.dispatch(cur, cmd);
228 return DispatchResult(true, true);
232 int InsetCollapsable::latex(Buffer const & buf, ostream & os,
233 OutputParams const & runparams) const
235 return inset.latex(buf, os, runparams);
239 int InsetCollapsable::plaintext(Buffer const & buf, ostream & os,
240 OutputParams const & runparams) const
242 return inset.plaintext(buf, os, runparams);
246 int InsetCollapsable::linuxdoc(Buffer const & buf, ostream & os,
247 OutputParams const & runparams) const
249 return inset.linuxdoc(buf, os, runparams);
253 int InsetCollapsable::docbook(Buffer const & buf, ostream & os,
254 OutputParams const & runparams) const
256 return inset.docbook(buf, os, runparams);
260 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
262 return button_dim.contains(cmd.x, cmd.y);
266 string const InsetCollapsable::getNewLabel(string const & l) const
269 pos_type const max_length = 15;
270 pos_type const p_siz = inset.paragraphs().begin()->size();
271 pos_type const n = min(max_length, p_siz);
274 for( ; i < n && j < p_siz; ++j) {
275 if (inset.paragraphs().begin()->isInset(j))
277 la += inset.paragraphs().begin()->getChar(j);
280 if (inset.paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
283 return la.empty() ? l : la;
287 void InsetCollapsable::edit(LCursor & cur, bool left)
289 //lyxerr << "InsetCollapsable: edit left/right" << endl;
291 inset.edit(cur, left);
296 void InsetCollapsable::edit(LCursor & cur, int x, int y)
299 //lyxerr << "InsetCollapsable: edit xy" << endl;
300 if (status_ == Collapsed) {
302 inset.edit(cur, true);
304 //we are not calling edit(x,y) because there are no coordinates in the
305 //inset yet. I personally think it's ok. (ab)
307 if (y <= button_dim.y2)
310 y += inset.ascent() - height_collapsed();
312 inset.edit(cur, x, y);
318 InsetCollapsable::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
320 lyxerr << "\nInsetCollapsable::priv_dispatch (begin): cmd: " << cmd
321 << " button y: " << button_dim.y2 << endl;
322 switch (cmd.action) {
323 case LFUN_MOUSE_PRESS:
324 if (status_ == Inlined)
325 inset.dispatch(cur, cmd);
326 else if (status_ == Open && cmd.y > button_dim.y2)
327 inset.dispatch(cur, cmd);
328 return DispatchResult(true, true);
330 case LFUN_MOUSE_MOTION:
331 if (status_ == Inlined)
332 inset.dispatch(cur, cmd);
333 else if (status_ == Open && cmd.y > button_dim.y2)
334 inset.dispatch(cur, cmd);
335 return DispatchResult(true, true);
337 case LFUN_MOUSE_RELEASE:
338 return lfunMouseRelease(cur, cmd);
340 case LFUN_INSET_TOGGLE:
341 if (inset.text_.toggleInset())
342 return DispatchResult(true, true);
343 if (status_ == Open) {
345 return DispatchResult(true, true);
347 setStatus(Collapsed);
348 return DispatchResult(false, FINISHED_RIGHT);
352 return inset.dispatch(cur, cmd);
357 void InsetCollapsable::validate(LaTeXFeatures & features) const
359 inset.validate(features);
363 void InsetCollapsable::getCursorPos(CursorSlice const & cur,
364 int & x, int & y) const
366 inset.getCursorPos(cur, x, y);
370 void InsetCollapsable::getLabelList(Buffer const & buffer,
371 std::vector<string> & list) const
373 inset.getLabelList(buffer, list);
377 int InsetCollapsable::scroll(bool recursive) const
379 int sx = UpdatableInset::scroll(false);
382 sx += inset.scroll(false);
388 int InsetCollapsable::numParagraphs() const
390 return inset.numParagraphs();
394 LyXText * InsetCollapsable::getText(int i) const
396 return inset.getText(i);
400 void InsetCollapsable::open()
402 if (status_ == Collapsed) // ...but not inlined
407 void InsetCollapsable::close()
409 setStatus(Collapsed);
413 void InsetCollapsable::setLabel(string const & l)
419 void InsetCollapsable::setStatus(CollapseStatus st)
426 void InsetCollapsable::markErased()
432 void InsetCollapsable::addPreview(PreviewLoader & loader) const
434 inset.addPreview(loader);
438 bool InsetCollapsable::insetAllowed(InsetOld::Code code) const
440 return inset.insetAllowed(code);
444 void InsetCollapsable::setLabelFont(LyXFont & font)
450 void InsetCollapsable::scroll(BufferView & bv, float sx) const
452 UpdatableInset::scroll(bv, sx);
456 void InsetCollapsable::scroll(BufferView & bv, int offset) const
458 UpdatableInset::scroll(bv, offset);
462 Box const & InsetCollapsable::buttonDim() const
468 void InsetCollapsable::setBackgroundColor(LColor_color color)
470 InsetOld::setBackgroundColor(color);
471 inset.setBackgroundColor(color);