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"
27 #include "insets/insettext.h"
29 #include "frontends/Dialogs.h"
30 #include "frontends/Alert.h"
32 #include "support/LOstream.h"
45 labelfont = LyXFont(LyXFont::ALL_SANE);
48 labelfont.setColor(LColor::latex);
53 InsetERT::InsetERT(BufferParams const & bp, bool collapsed)
54 : InsetCollapsable(bp, collapsed)
64 InsetERT::InsetERT(InsetERT const & in, bool same_id)
65 : InsetCollapsable(in, same_id), status_(in.status_)
71 Inset * InsetERT::clone(Buffer const &, bool same_id) const
73 return new InsetERT(*const_cast<InsetERT *>(this), same_id);
77 InsetERT::InsetERT(BufferParams const & bp,
78 Language const * l, string const & contents, bool collapsed)
79 : InsetCollapsable(bp, collapsed)
86 LyXFont font(LyXFont::ALL_INHERIT, l);
88 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
89 font.setColor(LColor::latex);
92 string::const_iterator cit = contents.begin();
93 string::const_iterator end = contents.end();
95 for (; cit != end; ++cit) {
96 inset.paragraph()->insertChar(pos++, *cit, font);
98 // the init has to be after the initialization of the paragraph
99 // because of the label settings (draw_label for ert insets).
104 InsetERT::~InsetERT()
110 void InsetERT::read(Buffer const * buf, LyXLex & lex)
112 bool token_found = false;
115 string const token = lex.getString();
116 if (token == "status") {
118 string const tmp_token = lex.getString();
120 if (tmp_token == "Inlined") {
122 } else if (tmp_token == "Collapsed") {
123 status(0, Collapsed);
125 // leave this as default!
131 lyxerr << "InsetERT::Read: Missing 'status'-tag!"
133 // take countermeasures
134 lex.pushToken(token);
138 #warning this should be really short lived only for compatibility to
139 #warning files written 07/08/2001 so this has to go before 1.2.0! (Jug)
142 string const token = lex.getString();
143 if (token == "collapsed") {
145 collapsed_ = lex.getBool();
147 // Take countermeasures
148 lex.pushToken(token);
152 inset.read(buf, lex);
156 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
158 LyXFont font(LyXFont::ALL_INHERIT);
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(textclasslist[buf->params.textclass].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);
266 void InsetERT::edit(BufferView * bv, int x, int y, unsigned int button)
271 if (status_ == Inlined) {
272 if (!bv->lockInset(this))
274 inset.edit(bv, x, y, button);
276 InsetCollapsable::edit(bv, x, y, button);
283 Inset::EDITABLE InsetERT::editable() const
285 if (status_ == Collapsed)
287 return HIGHLY_EDITABLE;
291 void InsetERT::edit(BufferView * bv, bool front)
293 InsetCollapsable::edit(bv, front);
301 void InsetERT::insetButtonPress(BufferView * bv,
302 int x, int y, int button)
304 if (status_ == Inlined) {
305 inset.insetButtonPress(bv, x, y, button);
307 InsetCollapsable::insetButtonPress(bv, x, y, button);
312 bool InsetERT::insetButtonRelease(BufferView * bv, int x, int y, int button)
319 if (status_ != Inlined && (x >= 0) && (x < button_length) &&
320 (y >= button_top_y) && (y <= button_bottom_y)) {
321 updateStatus(bv, true);
323 LyXFont font(LyXFont::ALL_SANE);
324 int yy = ascent(bv, font) + y - inset.ascent(bv, font);
326 // inlined is special - the text appears above
328 if (status_ == Inlined) {
329 inset.insetButtonRelease(bv, x, yy, button);
330 } else if (!collapsed_ && (y > button_bottom_y)) {
331 yy -= (ascent_collapsed() + descent_collapsed());
332 inset.insetButtonRelease(bv, x, yy, button);
339 void InsetERT::insetMotionNotify(BufferView * bv,
340 int x, int y, int state)
342 if (status_ == Inlined) {
343 inset.insetMotionNotify(bv, x, y, state);
345 InsetCollapsable::insetMotionNotify(bv, x, y, state);
350 int InsetERT::latex(Buffer const *, ostream & os, bool /*fragile*/,
351 bool /*free_spc*/) const
353 Paragraph * par = inset.paragraph();
356 pos_type siz = par->size();
357 for (pos_type i = 0; i < siz; ++i) {
358 Paragraph::value_type c = par->getChar(i);
360 case Paragraph::META_NEWLINE:
380 int InsetERT::ascii(Buffer const *,
381 ostream &, int /*linelen*/) const
387 int InsetERT::linuxdoc(Buffer const *, ostream & os) const
389 Paragraph * par = inset.paragraph();
392 pos_type siz = par->size();
393 for (pos_type i = 0; i < siz; ++i) {
394 Paragraph::value_type c = par->getChar(i);
396 case Paragraph::META_NEWLINE:
416 int InsetERT::docbook(Buffer const *, ostream & os) const
418 Paragraph * par = inset.paragraph();
421 pos_type siz = par->size();
422 for (pos_type i = 0; i < siz; ++i) {
423 Paragraph::value_type c = par->getChar(i);
425 case Paragraph::META_NEWLINE:
445 UpdatableInset::RESULT
446 InsetERT::localDispatch(BufferView * bv, kb_action action, string const & arg)
448 UpdatableInset::RESULT result = DISPATCHED_NOUPDATE;
450 if (!inset.paragraph()->size()) {
456 bv->owner()->setLayout(inset.paragraph()->layout());
459 result = InsetCollapsable::localDispatch(bv, action, arg);
462 case LFUN_BREAKPARAGRAPH:
463 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
465 case LFUN_BACKSPACE_SKIP:
467 case LFUN_DELETE_SKIP:
468 case LFUN_DELETE_LINE_FORWARD:
480 string const InsetERT::get_new_label() const
483 pos_type const max_length = 15;
484 pos_type const p_siz = inset.paragraph()->size();
485 pos_type const n = min(max_length, p_siz);
488 for(; i < n && j < p_siz; ++j) {
489 if (inset.paragraph()->isInset(j))
491 la += inset.paragraph()->getChar(j);
494 if (i > 0 && j < p_siz) {
504 void InsetERT::setButtonLabel() const
506 if (status_ == Collapsed) {
507 setLabel(get_new_label());
514 bool InsetERT::checkInsertChar(LyXFont & /* font */)
518 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
520 LyXFont f(LyXFont::ALL_INHERIT);
523 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
524 font.setColor(LColor::latex);
530 int InsetERT::ascent(BufferView * bv, LyXFont const & font) const
533 return InsetCollapsable::ascent(bv, font);
535 return inset.ascent(bv, font);
539 int InsetERT::descent(BufferView * bv, LyXFont const & font) const
542 return InsetCollapsable::descent(bv, font);
544 return inset.descent(bv, font);
548 int InsetERT::width(BufferView * bv, LyXFont const & font) const
551 return InsetCollapsable::width(bv, font);
553 return inset.width(bv, font);
557 void InsetERT::draw(BufferView * bv, LyXFont const & f,
558 int baseline, float & x, bool cleared) const
560 Painter & pain = bv->painter();
562 button_length = width_collapsed();
563 button_top_y = -ascent(bv, f);
564 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
568 draw_collapsed(pain, baseline, x);
569 x += TEXT_TO_INSET_OFFSET;
576 x += static_cast<float>(scroll());
578 if (!cleared && (inset.need_update == InsetText::FULL ||
579 inset.need_update == InsetText::INIT ||
581 top_baseline != baseline))
583 // we don't need anymore to clear here we just have to tell
584 // the underlying LyXText that it should do the RowClear!
585 inset.setUpdateStatus(bv, InsetText::FULL);
586 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
592 top_baseline = baseline;
594 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
597 inset.draw(bv, f, baseline, x, cleared);
599 draw_collapsed(pain, bl, old_x);
601 bl + descent_collapsed() + inset.ascent(bv, f),
608 void InsetERT::set_latex_font(BufferView * /* bv */)
612 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
614 LyXFont font(LyXFont::ALL_INHERIT);
617 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
618 font.setColor(LColor::latex);
620 inset.getLyXText(bv)->setFont(bv, font, false);
625 void InsetERT::status(BufferView * bv, ERTStatus const st) const
631 inset.setAutoBreakRows(false);
634 inset.setAutoBreakRows(true);
640 inset.setAutoBreakRows(true);
645 bv->unlockInset(const_cast<InsetERT *>(this));
649 bv->updateInset(const_cast<InsetERT *>(this), false);
654 bool InsetERT::showInsetDialog(BufferView * bv) const
656 bv->owner()->getDialogs()->showERT(const_cast<InsetERT *>(this));
661 void InsetERT::open(BufferView * bv)
669 void InsetERT::close(BufferView * bv) const
673 status(bv, Collapsed);
677 string const InsetERT::selectNextWordToSpellcheck(BufferView * bv,float &) const
679 bv->unlockInset(const_cast<InsetERT *>(this));
683 void InsetERT::getDrawFont(LyXFont & font) const
686 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
688 LyXFont f(LyXFont::ALL_INHERIT);
691 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
692 font.setColor(LColor::latex);