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"
20 #include "dispatchresult.h"
23 #include "funcrequest.h"
24 #include "metricsinfo.h"
25 #include "paragraph.h"
27 #include "frontends/font_metrics.h"
28 #include "frontends/Painter.h"
29 #include "frontends/LyXView.h"
32 using lyx::graphics::PreviewLoader;
40 InsetCollapsable::InsetCollapsable(BufferParams const & bp, bool collapsed)
41 : UpdatableInset(), inset(bp), collapsed_(collapsed),
42 button_dim(0, 0, 0, 0), label("Label"),
46 in_update(false), first_after_edit(false)
49 inset.setAutoBreakRows(true);
50 inset.setDrawFrame(InsetText::ALWAYS);
51 inset.setFrameColor(LColor::collapsableframe);
52 setInsetName("Collapsable");
56 InsetCollapsable::InsetCollapsable(InsetCollapsable const & in)
57 : UpdatableInset(in), inset(in.inset), collapsed_(in.collapsed_),
58 labelfont_(in.labelfont_), button_dim(0, 0, 0, 0), label(in.label),
60 autocollapse(in.autocollapse),
62 in_update(false), first_after_edit(false)
68 bool InsetCollapsable::insertInset(BufferView * bv, InsetOld * in)
70 if (!insetAllowed(in->lyxCode())) {
71 lyxerr << "InsetCollapsable::InsertInset: "
72 "Unable to insert inset." << endl;
75 return inset.insertInset(bv, in);
79 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
81 os << "collapsed " << (collapsed_ ? "true" : "false") << "\n";
82 inset.writeParagraphData(buf, os);
86 void InsetCollapsable::read(Buffer const & buf, LyXLex & lex)
90 string const token = lex.getString();
91 if (token == "collapsed") {
93 collapsed_ = lex.getBool();
95 lyxerr << "InsetCollapsable::Read: Missing collapsed!"
97 // Take countermeasures
101 inset.read(buf, lex);
105 void InsetCollapsable::dimension_collapsed(Dimension & dim) const
107 font_metrics::buttonText(label, labelfont_, dim.wid, dim.asc, dim.des);
111 int InsetCollapsable::height_collapsed() const
114 font_metrics::buttonText(label, labelfont_, dim.wid, dim.asc, dim.des);
115 return dim.asc + dim.des;
119 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
121 //lyxerr << "InsetCollapsable::metrics: width: " << mi.base.textwidth << endl;
122 dimension_collapsed(dim);
125 inset.metrics(mi, insetdim);
126 dim.des += insetdim.height() + TEXT_TO_BOTTOM_OFFSET;
127 dim.wid = max(dim.wid, insetdim.wid);
133 void InsetCollapsable::draw_collapsed(PainterInfo & pi, int x, int y) const
135 pi.pain.buttonText(x, y, label, labelfont_);
139 void InsetCollapsable::draw(PainterInfo & pi, int x, int y, bool inlined) const
141 Dimension dim_collapsed;
142 dimension_collapsed(dim_collapsed);
144 int const aa = ascent();
146 button_dim.x2 = dim_collapsed.width();
148 button_dim.y2 = -aa + dim_collapsed.height();
151 draw_collapsed(pi, x, y);
163 int const bl = y - aa + dim_collapsed.ascent();
166 inset.draw(pi, x, y);
168 draw_collapsed(pi, old_x, bl);
169 inset.draw(pi, x, bl + dim_collapsed.descent() + inset.ascent());
174 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
176 // by default, we are not inlined-drawing
177 draw(pi, x, y, false);
181 InsetOld::EDITABLE InsetCollapsable::editable() const
183 return collapsed_ ? IS_EDITABLE : HIGHLY_EDITABLE;
187 void InsetCollapsable::insetUnlock(BufferView * bv)
191 if (change_label_with_text) {
192 draw_label = get_new_label();
199 inset.insetUnlock(bv);
202 bv->updateInset(this);
206 FuncRequest InsetCollapsable::adjustCommand(FuncRequest const & cmd)
208 FuncRequest cmd1 = cmd;
209 cmd1.y = ascent() + cmd.y - (height_collapsed() + inset.ascent());
214 void InsetCollapsable::lfunMouseRelease(FuncRequest const & cmd)
217 BufferView * bv = cmd.view();
219 if (collapsed_ && cmd.button() != mouse_button::button3) {
221 bv->updateInset(this);
222 bv->buffer()->markDirty();
226 if (cmd.button() != mouse_button::button3 && hitButton(cmd)) {
231 bv->unlockInset(this);
233 bv->updateInset(this);
234 bv->buffer()->markDirty();
235 } else if (!collapsed_ && cmd.y > button_dim.y2) {
236 ret = inset.dispatch(adjustCommand(cmd)) == DispatchResult(true, true);
238 if (cmd.button() == mouse_button::button3 && !ret)
243 int InsetCollapsable::latex(Buffer const & buf, ostream & os,
244 LatexRunParams const & runparams) const
246 return inset.latex(buf, os, runparams);
250 int InsetCollapsable::ascii(Buffer const & buf, ostream & os,
251 LatexRunParams const & runparams) const
253 return inset.ascii(buf, os, runparams);
257 int InsetCollapsable::linuxdoc(Buffer const & buf, ostream & os,
258 LatexRunParams const & runparams) const
260 return inset.linuxdoc(buf, os, runparams);
264 int InsetCollapsable::docbook(Buffer const & buf, ostream & os,
265 LatexRunParams const & runparams) const
267 return inset.docbook(buf, os, runparams);
271 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
273 return button_dim.contains(cmd.x, cmd.y);
277 void InsetCollapsable::edit(BufferView * bv, int index)
279 lyxerr << "InsetCollapsable: edit" << endl;
280 if (!bv->lockInset(this))
281 lyxerr << "InsetCollapsable: can't lock index " << index << endl;
282 inset.dispatch(FuncRequest(bv, LFUN_INSET_EDIT, "left"));
283 first_after_edit = true;
288 InsetCollapsable::priv_dispatch(FuncRequest const & cmd,
289 idx_type & idx, pos_type & pos)
291 //lyxerr << "InsetCollapsable::localDispatch: "
292 // << cmd.action << " '" << cmd.argument << "'\n";
293 BufferView * bv = cmd.view();
294 switch (cmd.action) {
295 case LFUN_INSET_EDIT: {
296 if (!cmd.argument.empty()) {
297 UpdatableInset::priv_dispatch(cmd, idx, pos);
299 lyxerr << "branch collapsed_" << endl;
301 if (bv->lockInset(this)) {
302 bv->updateInset(this);
303 bv->buffer()->markDirty();
305 first_after_edit = true;
308 lyxerr << "branch not collapsed_" << endl;
309 if (bv->lockInset(this))
312 return DispatchResult(true, true);
316 #warning Fix this properly in BufferView_pimpl::workAreaButtonRelease
318 if (cmd.button() == mouse_button::button3)
319 return DispatchResult(true, true);
321 UpdatableInset::priv_dispatch(cmd, idx, pos);
325 // set this only here as it should be recollapsed only if
326 // it was already collapsed!
327 first_after_edit = true;
328 if (!bv->lockInset(this))
329 return DispatchResult(true, true);
330 bv->updateInset(this);
331 bv->buffer()->markDirty();
334 if (!bv->lockInset(this))
335 return DispatchResult(true, true);
336 if (cmd.y <= button_dim.y2) {
337 FuncRequest cmd1 = cmd;
339 inset.dispatch(cmd1);
341 inset.dispatch(adjustCommand(cmd));
343 return DispatchResult(true, true);
346 case LFUN_MOUSE_PRESS:
347 if (!collapsed_ && cmd.y > button_dim.y2)
348 inset.dispatch(adjustCommand(cmd));
349 return DispatchResult(true, true);
351 case LFUN_MOUSE_MOTION:
352 if (!collapsed_ && cmd.y > button_dim.y2)
353 inset.dispatch(adjustCommand(cmd));
354 return DispatchResult(true, true);
356 case LFUN_MOUSE_RELEASE:
357 lfunMouseRelease(cmd);
358 return DispatchResult(true, true);
361 DispatchResult const result = inset.dispatch(cmd);
362 if (result.val() >= FINISHED)
363 bv->unlockInset(this);
364 first_after_edit = false;
370 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
374 return inset.lockInsetInInset(bv, in);
378 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
382 bv->unlockInset(this);
385 return inset.unlockInsetInInset(bv, in, lr);
389 int InsetCollapsable::insetInInsetY() const
391 return inset.insetInInsetY() - (top_baseline - inset.y());
395 void InsetCollapsable::validate(LaTeXFeatures & features) const
397 inset.validate(features);
401 void InsetCollapsable::getCursor(BufferView & bv, int & x, int & y) const
403 inset.getCursor(bv, x, y);
407 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
409 inset.getCursorPos(bv, x , y);
413 UpdatableInset * InsetCollapsable::getLockingInset() const
415 UpdatableInset * in = inset.getLockingInset();
417 return const_cast<InsetCollapsable *>(this);
422 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(InsetOld::Code c)
426 return inset.getFirstLockingInsetOfType(c);
430 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
431 bool toggleall, bool selectall)
433 inset.setFont(bv, font, toggleall, selectall);
437 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
438 bool const recursive) const
440 return inset.getLyXText(bv, recursive);
444 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
446 inset.deleteLyXText(bv, recursive);
450 void InsetCollapsable::getLabelList(Buffer const & buffer,
451 std::vector<string> & list) const
453 inset.getLabelList(buffer, list);
457 int InsetCollapsable::scroll(bool recursive) const
459 int sx = UpdatableInset::scroll(false);
462 sx += inset.scroll(recursive);
468 ParagraphList * InsetCollapsable::getParagraphs(int i) const
470 return inset.getParagraphs(i);
474 int InsetCollapsable::numParagraphs() const
476 return inset.numParagraphs();
480 LyXText * InsetCollapsable::getText(int i) const
482 return inset.getText(i);
486 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
488 return inset.cursor(bv);
492 void InsetCollapsable::open(BufferView * bv)
498 bv->updateInset(this);
502 void InsetCollapsable::close(BufferView * bv) const
508 bv->updateInset(this);
512 void InsetCollapsable::setLabel(string const & l) const
518 void InsetCollapsable::setCollapsed(bool c) const
524 void InsetCollapsable::markErased()
530 void InsetCollapsable::addPreview(PreviewLoader & loader) const
532 inset.addPreview(loader);