1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998 The LyX Team.
8 *======================================================*/
13 #pragma implementation
21 #include "BufferView.h"
25 #include "lyxtextclasslist.h"
28 #include "insets/insettext.h"
30 #include "frontends/Dialogs.h"
31 #include "frontends/Alert.h"
33 #include "support/LOstream.h"
46 labelfont = LyXFont(LyXFont::ALL_SANE);
49 labelfont.setColor(LColor::latex);
54 InsetERT::InsetERT(BufferParams const & bp, bool collapsed)
55 : InsetCollapsable(bp, collapsed)
65 InsetERT::InsetERT(InsetERT const & in, bool same_id)
66 : InsetCollapsable(in, same_id), status_(in.status_)
72 Inset * InsetERT::clone(Buffer const &, bool same_id) const
74 return new InsetERT(*const_cast<InsetERT *>(this), same_id);
78 InsetERT::InsetERT(BufferParams const & bp,
79 Language const * l, string const & contents, bool collapsed)
80 : InsetCollapsable(bp, collapsed)
87 LyXFont font(LyXFont::ALL_INHERIT, l);
89 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
90 font.setColor(LColor::latex);
93 string::const_iterator cit = contents.begin();
94 string::const_iterator end = contents.end();
96 for (; cit != end; ++cit) {
97 inset.paragraph()->insertChar(pos++, *cit, font);
99 // the init has to be after the initialization of the paragraph
100 // because of the label settings (draw_label for ert insets).
105 InsetERT::~InsetERT()
111 void InsetERT::read(Buffer const * buf, LyXLex & lex)
113 bool token_found = false;
116 string const token = lex.getString();
117 if (token == "status") {
119 string const tmp_token = lex.getString();
121 if (tmp_token == "Inlined") {
123 } else if (tmp_token == "Collapsed") {
124 status(0, Collapsed);
126 // leave this as default!
132 lyxerr << "InsetERT::Read: Missing 'status'-tag!"
134 // take countermeasures
135 lex.pushToken(token);
139 #warning this should be really short lived only for compatibility to
140 #warning files written 07/08/2001 so this has to go before 1.2.0! (Jug)
143 string const token = lex.getString();
144 if (token == "collapsed") {
146 collapsed_ = lex.getBool();
148 // Take countermeasures
149 lex.pushToken(token);
153 inset.read(buf, lex);
157 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
159 LyXFont font(LyXFont::ALL_INHERIT);
161 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
162 font.setColor(LColor::latex);
163 Paragraph * par = inset.paragraph();
165 pos_type siz = par->size();
166 for (pos_type i = 0; i < siz; ++i) {
167 par->setFont(i, font);
175 status(0, Collapsed);
184 void InsetERT::write(Buffer const * buf, ostream & os) const
200 os << getInsetName() << "\n"
201 << "status "<< st << "\n";
203 //inset.writeParagraphData(buf, os);
204 string const layout(textclasslist[buf->params.textclass].defaultLayoutName());
205 Paragraph * par = inset.paragraph();
207 os << "\n\\layout " << layout << "\n";
208 pos_type siz = par->size();
209 for (pos_type i = 0; i < siz; ++i) {
210 Paragraph::value_type c = par->getChar(i);
212 case Paragraph::META_INSET:
213 case Paragraph::META_HFILL:
214 lyxerr << "Element is not allowed in insertERT"
216 case Paragraph::META_NEWLINE:
217 os << "\n\\newline \n";
220 os << "\n\\backslash \n";
232 string const InsetERT::editMessage() const
234 return _("Opened ERT Inset");
238 bool InsetERT::insertInset(BufferView *, Inset *)
244 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
246 // if selectall is activated then the fontchange was an outside general
247 // fontchange and this messages is not needed
249 Alert::alert(_("Impossible Operation!"),
250 _("Not permitted to change font-types inside ERT-insets!"),
255 void InsetERT::updateStatus(BufferView * bv, bool swap) const
257 if (status_ != Inlined) {
259 status(bv, swap ? Open : Collapsed);
261 status(bv, swap ? Collapsed : Open);
267 void InsetERT::edit(BufferView * bv, int x, int y, unsigned int button)
272 if (status_ == Inlined) {
273 if (!bv->lockInset(this))
275 inset.edit(bv, x, y, button);
277 InsetCollapsable::edit(bv, x, y, button);
284 Inset::EDITABLE InsetERT::editable() const
286 if (status_ == Collapsed)
288 return HIGHLY_EDITABLE;
292 void InsetERT::edit(BufferView * bv, bool front)
294 InsetCollapsable::edit(bv, front);
302 void InsetERT::insetButtonPress(BufferView * bv,
303 int x, int y, int button)
305 if (status_ == Inlined) {
306 inset.insetButtonPress(bv, x, y, button);
308 InsetCollapsable::insetButtonPress(bv, x, y, button);
313 bool InsetERT::insetButtonRelease(BufferView * bv, int x, int y, int button)
320 if (status_ != Inlined && (x >= 0) && (x < button_length) &&
321 (y >= button_top_y) && (y <= button_bottom_y)) {
322 updateStatus(bv, true);
324 LyXFont font(LyXFont::ALL_SANE);
325 int yy = ascent(bv, font) + y - inset.ascent(bv, font);
327 // inlined is special - the text appears above
329 if (status_ == Inlined) {
330 inset.insetButtonRelease(bv, x, yy, button);
331 } else if (!collapsed_ && (y > button_bottom_y)) {
332 yy -= (ascent_collapsed() + descent_collapsed());
333 inset.insetButtonRelease(bv, x, yy, button);
340 void InsetERT::insetMotionNotify(BufferView * bv,
341 int x, int y, int state)
343 if (status_ == Inlined) {
344 inset.insetMotionNotify(bv, x, y, state);
346 InsetCollapsable::insetMotionNotify(bv, x, y, state);
351 int InsetERT::latex(Buffer const *, ostream & os, bool /*fragile*/,
352 bool /*free_spc*/) const
354 Paragraph * par = inset.paragraph();
357 pos_type siz = par->size();
358 for (pos_type i = 0; i < siz; ++i) {
359 Paragraph::value_type c = par->getChar(i);
361 case Paragraph::META_NEWLINE:
381 int InsetERT::ascii(Buffer const *,
382 ostream &, int /*linelen*/) const
388 int InsetERT::linuxdoc(Buffer const *, ostream & os) const
390 Paragraph * par = inset.paragraph();
393 pos_type siz = par->size();
394 for (pos_type i = 0; i < siz; ++i) {
395 Paragraph::value_type c = par->getChar(i);
397 case Paragraph::META_NEWLINE:
417 int InsetERT::docbook(Buffer const *, ostream & os) const
419 Paragraph * par = inset.paragraph();
422 pos_type siz = par->size();
423 for (pos_type i = 0; i < siz; ++i) {
424 Paragraph::value_type c = par->getChar(i);
426 case Paragraph::META_NEWLINE:
446 UpdatableInset::RESULT
447 InsetERT::localDispatch(BufferView * bv, kb_action action, string const & arg)
449 UpdatableInset::RESULT result = DISPATCHED_NOUPDATE;
451 if (!inset.paragraph()->size()) {
457 bv->owner()->setLayout(inset.paragraph()->layout());
460 result = InsetCollapsable::localDispatch(bv, action, arg);
463 case LFUN_BREAKPARAGRAPH:
464 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
466 case LFUN_BACKSPACE_SKIP:
468 case LFUN_DELETE_SKIP:
469 case LFUN_DELETE_LINE_FORWARD:
481 string const InsetERT::get_new_label() const
484 pos_type const max_length = 15;
485 pos_type const p_siz = inset.paragraph()->size();
486 pos_type const n = min(max_length, p_siz);
489 for(; i < n && j < p_siz; ++j) {
490 if (inset.paragraph()->isInset(j))
492 la += inset.paragraph()->getChar(j);
495 if (i > 0 && j < p_siz) {
505 void InsetERT::setButtonLabel() const
507 if (status_ == Collapsed) {
508 setLabel(get_new_label());
515 bool InsetERT::checkInsertChar(LyXFont & /* font */)
519 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
521 LyXFont f(LyXFont::ALL_INHERIT);
524 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
525 font.setColor(LColor::latex);
531 int InsetERT::ascent(BufferView * bv, LyXFont const & font) const
534 return InsetCollapsable::ascent(bv, font);
536 return inset.ascent(bv, font);
540 int InsetERT::descent(BufferView * bv, LyXFont const & font) const
543 return InsetCollapsable::descent(bv, font);
545 return inset.descent(bv, font);
549 int InsetERT::width(BufferView * bv, LyXFont const & font) const
552 return InsetCollapsable::width(bv, font);
554 return inset.width(bv, font);
558 void InsetERT::draw(BufferView * bv, LyXFont const & f,
559 int baseline, float & x, bool cleared) const
561 Painter & pain = bv->painter();
563 button_length = width_collapsed();
564 button_top_y = -ascent(bv, f);
565 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
569 draw_collapsed(pain, baseline, x);
570 x += TEXT_TO_INSET_OFFSET;
577 x += static_cast<float>(scroll());
579 if (!cleared && (inset.need_update == InsetText::FULL ||
580 inset.need_update == InsetText::INIT ||
582 top_baseline != baseline))
584 // we don't need anymore to clear here we just have to tell
585 // the underlying LyXText that it should do the RowClear!
586 inset.setUpdateStatus(bv, InsetText::FULL);
587 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
593 top_baseline = baseline;
595 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
598 inset.draw(bv, f, baseline, x, cleared);
600 draw_collapsed(pain, bl, old_x);
602 bl + descent_collapsed() + inset.ascent(bv, f),
609 void InsetERT::set_latex_font(BufferView * /* bv */)
613 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
615 LyXFont font(LyXFont::ALL_INHERIT);
618 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
619 font.setColor(LColor::latex);
621 inset.getLyXText(bv)->setFont(bv, font, false);
626 void InsetERT::status(BufferView * bv, ERTStatus const st) const
633 inset.setUpdateStatus(bv, InsetText::INIT);
643 bv->unlockInset(const_cast<InsetERT *>(this));
647 bv->updateInset(const_cast<InsetERT *>(this), false);
652 bool InsetERT::showInsetDialog(BufferView * bv) const
654 bv->owner()->getDialogs()->showERT(const_cast<InsetERT *>(this));
659 void InsetERT::open(BufferView * bv)
667 void InsetERT::close(BufferView * bv) const
671 status(bv, Collapsed);
675 string const InsetERT::selectNextWordToSpellcheck(BufferView * bv,float &) const
677 bv->unlockInset(const_cast<InsetERT *>(this));
681 void InsetERT::getDrawFont(LyXFont & font) const
684 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
686 LyXFont f(LyXFont::ALL_INHERIT);
689 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
690 font.setColor(LColor::latex);
694 int InsetERT::getMaxWidth(BufferView * bv, UpdatableInset const * in) const
696 unsigned int w = InsetCollapsable::getMaxWidth(bv, in);
697 if (status_ != Inlined || w < 0)
699 LyXText * text = inset.getLyXText(bv);
700 if (text->width < w && !text->firstRow()->next())