3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
9 * Full author contact details are available in file CREDITS
14 #pragma implementation
18 #include "insettext.h"
21 #include "BufferView.h"
23 #include "funcrequest.h"
30 #include "WordLangTuple.h"
32 #include "frontends/Alert.h"
33 #include "frontends/Dialogs.h"
34 #include "frontends/LyXView.h"
36 #include "support/LOstream.h"
49 labelfont = LyXFont(LyXFont::ALL_SANE);
52 labelfont.setColor(LColor::latex);
57 InsetERT::InsetERT(BufferParams const & bp, bool collapsed)
58 : InsetCollapsable(bp, collapsed)
68 InsetERT::InsetERT(InsetERT const & in, bool same_id)
69 : InsetCollapsable(in, same_id), status_(in.status_)
75 Inset * InsetERT::clone(Buffer const &, bool same_id) const
77 return new InsetERT(*const_cast<InsetERT *>(this), same_id);
81 InsetERT::InsetERT(BufferParams const & bp,
82 Language const * l, string const & contents, bool collapsed)
83 : InsetCollapsable(bp, collapsed)
90 LyXFont font(LyXFont::ALL_INHERIT, l);
92 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
93 font.setColor(LColor::latex);
96 string::const_iterator cit = contents.begin();
97 string::const_iterator end = contents.end();
99 for (; cit != end; ++cit) {
100 inset.paragraph()->insertChar(pos++, *cit, font);
102 // the init has to be after the initialization of the paragraph
103 // because of the label settings (draw_label for ert insets).
108 InsetERT::~InsetERT()
114 void InsetERT::read(Buffer const * buf, LyXLex & lex)
116 bool token_found = false;
119 string const token = lex.getString();
120 if (token == "status") {
122 string const tmp_token = lex.getString();
124 if (tmp_token == "Inlined") {
126 } else if (tmp_token == "Collapsed") {
127 status(0, Collapsed);
129 // leave this as default!
135 lyxerr << "InsetERT::Read: Missing 'status'-tag!"
137 // take countermeasures
138 lex.pushToken(token);
142 #warning this should be really short lived only for compatibility to
143 #warning files written 07/08/2001 so this has to go before 1.2.0! (Jug)
146 string const token = lex.getString();
147 if (token == "collapsed") {
149 collapsed_ = lex.getBool();
151 // Take countermeasures
152 lex.pushToken(token);
156 inset.read(buf, lex);
159 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
160 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
161 font.setColor(LColor::latex);
162 Paragraph * par = inset.paragraph();
164 pos_type siz = par->size();
165 for (pos_type i = 0; i < siz; ++i) {
166 par->setFont(i, font);
174 status(0, Collapsed);
183 void InsetERT::write(Buffer const * buf, ostream & os) const
199 os << getInsetName() << "\n"
200 << "status "<< st << "\n";
202 //inset.writeParagraphData(buf, os);
203 string const layout(buf->params.getLyXTextClass().defaultLayoutName());
204 Paragraph * par = inset.paragraph();
206 os << "\n\\layout " << layout << "\n";
207 pos_type siz = par->size();
208 for (pos_type i = 0; i < siz; ++i) {
209 Paragraph::value_type c = par->getChar(i);
211 case Paragraph::META_INSET:
212 case Paragraph::META_HFILL:
213 lyxerr << "Element is not allowed in insertERT"
215 case Paragraph::META_NEWLINE:
216 os << "\n\\newline \n";
219 os << "\n\\backslash \n";
231 string const InsetERT::editMessage() const
233 return _("Opened ERT Inset");
237 bool InsetERT::insertInset(BufferView *, Inset *)
243 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
245 // if selectall is activated then the fontchange was an outside general
246 // fontchange and this messages is not needed
248 Alert::alert(_("Impossible operation!"),
249 _("Not permitted to change font-types inside ERT-insets!"),
254 void InsetERT::updateStatus(BufferView * bv, bool swap) const
256 if (status_ != Inlined) {
258 status(bv, swap ? Open : Collapsed);
260 status(bv, swap ? Collapsed : Open);
265 void InsetERT::edit(BufferView * bv, int x, int y, mouse_button::state button)
267 if (button == mouse_button::button3)
270 if (status_ == Inlined) {
271 if (!bv->lockInset(this))
273 inset.edit(bv, x, y, button);
275 InsetCollapsable::edit(bv, x, y, button);
282 Inset::EDITABLE InsetERT::editable() const
284 if (status_ == Collapsed)
286 return HIGHLY_EDITABLE;
290 void InsetERT::edit(BufferView * bv, bool front)
292 InsetCollapsable::edit(bv, front);
298 void InsetERT::lfunMousePress(FuncRequest const & cmd)
300 if (status_ == Inlined)
301 inset.localDispatch(cmd);
303 InsetCollapsable::localDispatch(cmd);
307 bool InsetERT::lfunMouseRelease(FuncRequest const & cmd)
309 BufferView * bv = cmd.view();
311 if (cmd.button() == mouse_button::button3) {
316 if (status_ != Inlined && (cmd.x >= 0) && (cmd.x < button_length) &&
317 (cmd.y >= button_top_y) && (cmd.y <= button_bottom_y)) {
318 updateStatus(bv, true);
320 LyXFont font(LyXFont::ALL_SANE);
321 FuncRequest cmd1 = cmd;
322 cmd1.y = ascent(bv, font) + cmd.y - inset.ascent(bv, font);
324 // inlined is special - the text appears above
326 if (status_ == Inlined)
327 inset.localDispatch(cmd1);
328 else if (!collapsed_ && (cmd.y > button_bottom_y)) {
329 cmd1.y -= ascent_collapsed() + descent_collapsed();
330 inset.localDispatch(cmd1);
337 void InsetERT::lfunMouseMotion(FuncRequest const & cmd)
339 if (status_ == Inlined)
340 inset.localDispatch(cmd);
342 InsetCollapsable::localDispatch(cmd);
346 int InsetERT::latex(Buffer const *, ostream & os, bool /*fragile*/,
347 bool /*free_spc*/) const
349 Paragraph * par = inset.paragraph();
352 pos_type siz = par->size();
353 for (pos_type i = 0; i < siz; ++i) {
354 Paragraph::value_type c = par->getChar(i);
356 case Paragraph::META_NEWLINE:
376 int InsetERT::ascii(Buffer const *, ostream &, int /*linelen*/) const
382 int InsetERT::linuxdoc(Buffer const *, ostream & os) const
384 Paragraph * par = inset.paragraph();
387 pos_type siz = par->size();
388 for (pos_type i = 0; i < siz; ++i) {
389 Paragraph::value_type c = par->getChar(i);
391 case Paragraph::META_NEWLINE:
411 int InsetERT::docbook(Buffer const *, ostream & os, bool) const
413 Paragraph * par = inset.paragraph();
416 pos_type siz = par->size();
417 for (pos_type i = 0; i < siz; ++i) {
418 Paragraph::value_type c = par->getChar(i);
420 case Paragraph::META_NEWLINE:
440 Inset::RESULT InsetERT::localDispatch(FuncRequest const & cmd)
442 Inset::RESULT result = DISPATCHED_NOUPDATE;
443 BufferView * bv = cmd.view();
445 if (inset.paragraph()->empty()) {
449 switch (cmd.action) {
450 case LFUN_MOUSE_PRESS:
454 case LFUN_MOUSE_MOTION:
455 lfunMouseMotion(cmd);
458 case LFUN_MOUSE_RELEASE:
459 lfunMouseRelease(cmd);
463 bv->owner()->setLayout(inset.paragraph()->layout()->name());
467 result = InsetCollapsable::localDispatch(cmd);
470 switch (cmd.action) {
471 case LFUN_BREAKPARAGRAPH:
472 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
474 case LFUN_BACKSPACE_SKIP:
476 case LFUN_DELETE_SKIP:
477 case LFUN_DELETE_LINE_FORWARD:
489 string const InsetERT::get_new_label() const
492 pos_type const max_length = 15;
493 pos_type const p_siz = inset.paragraph()->size();
494 pos_type const n = min(max_length, p_siz);
497 for(; i < n && j < p_siz; ++j) {
498 if (inset.paragraph()->isInset(j))
500 la += inset.paragraph()->getChar(j);
503 if (inset.paragraph()->next() || (i > 0 && j < p_siz)) {
513 void InsetERT::setButtonLabel() const
515 if (status_ == Collapsed) {
516 setLabel(get_new_label());
523 bool InsetERT::checkInsertChar(LyXFont & /* font */)
526 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
528 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
529 font.setColor(LColor::latex);
535 int InsetERT::ascent(BufferView * bv, LyXFont const & font) const
538 return InsetCollapsable::ascent(bv, font);
540 return inset.ascent(bv, font);
544 int InsetERT::descent(BufferView * bv, LyXFont const & font) const
547 return InsetCollapsable::descent(bv, font);
549 return inset.descent(bv, font);
553 int InsetERT::width(BufferView * bv, LyXFont const & font) const
556 return InsetCollapsable::width(bv, font);
558 return inset.width(bv, font);
562 void InsetERT::draw(BufferView * bv, LyXFont const & f,
563 int baseline, float & x, bool cleared) const
565 Painter & pain = bv->painter();
567 button_length = width_collapsed();
568 button_top_y = -ascent(bv, f);
569 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
573 draw_collapsed(pain, baseline, x);
580 x += static_cast<float>(scroll());
582 if (!cleared && (inset.need_update == InsetText::FULL ||
583 inset.need_update == InsetText::INIT ||
585 top_baseline != baseline))
587 // we don't need anymore to clear here we just have to tell
588 // the underlying LyXText that it should do the RowClear!
589 inset.setUpdateStatus(bv, InsetText::FULL);
590 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
596 top_baseline = baseline;
598 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
601 inset.draw(bv, f, baseline, x, cleared);
603 draw_collapsed(pain, bl, old_x);
605 bl + descent_collapsed() + inset.ascent(bv, f),
612 void InsetERT::set_latex_font(BufferView * /* bv */)
615 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
617 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
618 font.setColor(LColor::latex);
620 inset.getLyXText(bv)->setFont(bv, font, false);
625 // attention this function can be called with bv == 0
626 void InsetERT::status(BufferView * bv, ERTStatus const st) const
634 inset.setUpdateStatus(bv, InsetText::INIT);
644 bv->unlockInset(const_cast<InsetERT *>(this));
648 bv->updateInset(const_cast<InsetERT *>(this), false);
653 bool InsetERT::showInsetDialog(BufferView * bv) const
655 bv->owner()->getDialogs().showERT(const_cast<InsetERT *>(this));
660 void InsetERT::open(BufferView * bv)
668 void InsetERT::close(BufferView * bv) const
670 if (status_ == Collapsed || status_ == Inlined)
673 status(bv, Collapsed);
678 InsetERT::selectNextWordToSpellcheck(BufferView * bv, float &) const
680 bv->unlockInset(const_cast<InsetERT *>(this));
681 return WordLangTuple();
685 void InsetERT::getDrawFont(LyXFont & font) const
687 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
689 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
690 font.setColor(LColor::latex);
694 int InsetERT::getMaxWidth(BufferView * bv, UpdatableInset const * in) const
696 int w = InsetCollapsable::getMaxWidth(bv, in);
697 if (status_ != Inlined || w < 0)
699 LyXText * text = inset.getLyXText(bv);
700 int rw = text->firstRow()->width();
704 if (!text->firstRow()->next() && rw < w)
710 void InsetERT::update(BufferView * bv, LyXFont const & font,
713 if (inset.need_update & InsetText::INIT ||
714 inset.need_update & InsetText::FULL)
718 InsetCollapsable::update(bv, font, reinit);