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"
22 #include "frontends/LyXView.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);
266 void InsetERT::edit(BufferView * bv, int x, int y, mouse_button::state button)
268 if (button == mouse_button::button3)
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, mouse_button::state 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,
313 mouse_button::state button)
315 if (button == mouse_button::button3) {
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, mouse_button::state 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 (inset.paragraph()->next() || (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);
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 // 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);
677 string const InsetERT::selectNextWordToSpellcheck(BufferView * bv,
680 bv->unlockInset(const_cast<InsetERT *>(this));
685 void InsetERT::getDrawFont(LyXFont & font) const
688 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
690 LyXFont f(LyXFont::ALL_INHERIT);
693 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
694 font.setColor(LColor::latex);
698 int InsetERT::getMaxWidth(BufferView * bv, UpdatableInset const * in) const
700 int w = InsetCollapsable::getMaxWidth(bv, in);
701 if (status_ != Inlined || w < 0)
703 LyXText * text = inset.getLyXText(bv);
704 int rw = text->firstRow()->width();
708 if (!text->firstRow()->next() && rw < w)
714 void InsetERT::update(BufferView * bv, LyXFont const & font,
717 if (inset.need_update & InsetText::INIT ||
718 inset.need_update & InsetText::FULL)
722 InsetCollapsable::update(bv, font, reinit);