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(bool collapsed)
54 : InsetCollapsable(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(Language const * l, string const & contents, bool collapsed)
78 : InsetCollapsable(collapsed)
85 LyXFont font(LyXFont::ALL_INHERIT, l);
87 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
88 font.setColor(LColor::latex);
91 string::const_iterator cit = contents.begin();
92 string::const_iterator end = contents.end();
94 for (; cit != end; ++cit) {
95 inset.paragraph()->insertChar(pos++, *cit, font);
97 // the init has to be after the initialization of the paragraph
98 // because of the label settings (draw_label for ert insets).
103 InsetERT::~InsetERT()
109 void InsetERT::read(Buffer const * buf, LyXLex & lex)
111 bool token_found = false;
114 string const token = lex.getString();
115 if (token == "status") {
117 string const tmp_token = lex.getString();
119 if (tmp_token == "Inlined") {
121 } else if (tmp_token == "Collapsed") {
122 status(0, Collapsed);
124 // leave this as default!
130 lyxerr << "InsetERT::Read: Missing 'status'-tag!"
132 // take countermeasures
133 lex.pushToken(token);
137 #warning this should be really short lived only for compatibility to
138 #warning files written 07/08/2001 so this has to go before 1.2.0! (Jug)
141 string const token = lex.getString();
142 if (token == "collapsed") {
144 collapsed_ = lex.getBool();
146 // Take countermeasures
147 lex.pushToken(token);
151 inset.read(buf, lex);
155 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
157 LyXFont font(LyXFont::ALL_INHERIT);
159 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
160 font.setColor(LColor::latex);
161 Paragraph * par = inset.paragraph();
163 pos_type siz = par->size();
164 for (pos_type i = 0; i < siz; ++i) {
165 par->setFont(i, font);
173 status(0, Collapsed);
182 void InsetERT::write(Buffer const * buf, ostream & os) const
198 os << getInsetName() << "\n"
199 << "status "<< st << "\n";
201 //inset.writeParagraphData(buf, os);
202 string const layout(textclasslist[buf->params.textclass].defaultLayoutName());
203 Paragraph * par = inset.paragraph();
205 os << "\n\\layout " << layout << "\n";
206 pos_type siz = par->size();
207 for (pos_type i = 0; i < siz; ++i) {
208 Paragraph::value_type c = par->getChar(i);
210 case Paragraph::META_INSET:
211 case Paragraph::META_HFILL:
212 lyxerr << "Element is not allowed in insertERT"
214 case Paragraph::META_NEWLINE:
215 os << "\n\\newline \n";
218 os << "\n\\backslash \n";
230 string const InsetERT::editMessage() const
232 return _("Opened ERT Inset");
236 bool InsetERT::insertInset(BufferView *, Inset *)
242 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
244 // if selectall is activated then the fontchange was an outside general
245 // fontchange and this messages is not needed
247 Alert::alert(_("Impossible Operation!"),
248 _("Not permitted to change font-types inside ERT-insets!"),
253 void InsetERT::updateStatus(BufferView * bv, bool swap) const
255 if (status_ != Inlined) {
257 status(bv, swap ? Open : Collapsed);
259 status(bv, swap ? Collapsed : Open);
265 void InsetERT::edit(BufferView * bv, int x, int y, unsigned int button)
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);
300 void InsetERT::insetButtonPress(BufferView * bv,
301 int x, int y, int button)
303 if (status_ == Inlined) {
304 inset.insetButtonPress(bv, x, y, button);
306 InsetCollapsable::insetButtonPress(bv, x, y, button);
311 bool InsetERT::insetButtonRelease(BufferView * bv, int x, int y, int button)
318 if (status_ != Inlined && (x >= 0) && (x < button_length) &&
319 (y >= button_top_y) && (y <= button_bottom_y)) {
320 updateStatus(bv, true);
322 LyXFont font(LyXFont::ALL_SANE);
323 int yy = ascent(bv, font) + y - inset.ascent(bv, font);
325 // inlined is special - the text appears above
327 if (status_ == Inlined) {
328 inset.insetButtonRelease(bv, x, yy, button);
329 } else if (!collapsed_ && (y > button_bottom_y)) {
330 yy -= (ascent_collapsed() + descent_collapsed());
331 inset.insetButtonRelease(bv, x, yy, button);
338 void InsetERT::insetMotionNotify(BufferView * bv,
339 int x, int y, int state)
341 if (status_ == Inlined) {
342 inset.insetMotionNotify(bv, x, y, state);
344 InsetCollapsable::insetMotionNotify(bv, x, y, state);
349 int InsetERT::latex(Buffer const *, ostream & os, bool /*fragile*/,
350 bool /*free_spc*/) const
352 Paragraph * par = inset.paragraph();
355 pos_type siz = par->size();
356 for (pos_type i = 0; i < siz; ++i) {
357 Paragraph::value_type c = par->getChar(i);
359 case Paragraph::META_NEWLINE:
379 int InsetERT::ascii(Buffer const *,
380 ostream &, int /*linelen*/) const
386 int InsetERT::linuxdoc(Buffer const *, ostream & os) const
388 Paragraph * par = inset.paragraph();
391 pos_type siz = par->size();
392 for (pos_type i = 0; i < siz; ++i) {
393 Paragraph::value_type c = par->getChar(i);
395 case Paragraph::META_NEWLINE:
415 int InsetERT::docbook(Buffer const *, ostream & os) const
417 Paragraph * par = inset.paragraph();
420 pos_type siz = par->size();
421 for (pos_type i = 0; i < siz; ++i) {
422 Paragraph::value_type c = par->getChar(i);
424 case Paragraph::META_NEWLINE:
444 UpdatableInset::RESULT
445 InsetERT::localDispatch(BufferView * bv, kb_action action, string const & arg)
447 UpdatableInset::RESULT result = DISPATCHED_NOUPDATE;
449 if (!inset.paragraph()->size()) {
455 bv->owner()->setLayout(inset.paragraph()->layout());
458 result = InsetCollapsable::localDispatch(bv, action, arg);
461 case LFUN_BREAKPARAGRAPH:
462 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
464 case LFUN_BACKSPACE_SKIP:
466 case LFUN_DELETE_SKIP:
467 case LFUN_DELETE_LINE_FORWARD:
479 string const InsetERT::get_new_label() const
482 pos_type const max_length = 15;
483 pos_type const p_siz = inset.paragraph()->size();
484 pos_type const n = min(max_length, p_siz);
487 for(; i < n && j < p_siz; ++j) {
488 if (inset.paragraph()->isInset(j))
490 la += inset.paragraph()->getChar(j);
493 if (i > 0 && j < p_siz) {
503 void InsetERT::setButtonLabel() const
505 if (status_ == Collapsed) {
506 setLabel(get_new_label());
513 bool InsetERT::checkInsertChar(LyXFont & /* font */)
517 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
519 LyXFont f(LyXFont::ALL_INHERIT);
522 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
523 font.setColor(LColor::latex);
529 int InsetERT::ascent(BufferView * bv, LyXFont const & font) const
532 return InsetCollapsable::ascent(bv, font);
534 return inset.ascent(bv, font);
538 int InsetERT::descent(BufferView * bv, LyXFont const & font) const
541 return InsetCollapsable::descent(bv, font);
543 return inset.descent(bv, font);
547 int InsetERT::width(BufferView * bv, LyXFont const & font) const
550 return InsetCollapsable::width(bv, font);
552 return inset.width(bv, font);
556 void InsetERT::draw(BufferView * bv, LyXFont const & f,
557 int baseline, float & x, bool cleared) const
559 Painter & pain = bv->painter();
561 button_length = width_collapsed();
562 button_top_y = -ascent(bv, f);
563 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
567 draw_collapsed(pain, baseline, x);
568 x += TEXT_TO_INSET_OFFSET;
575 x += static_cast<float>(scroll());
577 if (!cleared && (inset.need_update == InsetText::FULL ||
578 inset.need_update == InsetText::INIT ||
580 top_baseline != baseline))
582 // we don't need anymore to clear here we just have to tell
583 // the underlying LyXText that it should do the RowClear!
584 inset.setUpdateStatus(bv, InsetText::FULL);
585 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
591 top_baseline = baseline;
593 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
596 inset.draw(bv, f, baseline, x, cleared);
598 draw_collapsed(pain, bl, old_x);
600 bl + descent_collapsed() + inset.ascent(bv, f),
607 void InsetERT::set_latex_font(BufferView * /* bv */)
611 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
613 LyXFont font(LyXFont::ALL_INHERIT);
616 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
617 font.setColor(LColor::latex);
619 inset.getLyXText(bv)->setFont(bv, font, false);
624 void InsetERT::status(BufferView * bv, ERTStatus const st) const
630 inset.setAutoBreakRows(false);
633 inset.setAutoBreakRows(true);
639 inset.setAutoBreakRows(true);
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
672 status(bv, Collapsed);
676 string const InsetERT::selectNextWordToSpellcheck(BufferView * bv,float &) const
678 bv->unlockInset(const_cast<InsetERT *>(this));
682 void InsetERT::getDrawFont(LyXFont & font) const
685 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
687 LyXFont f(LyXFont::ALL_INHERIT);
690 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
691 font.setColor(LColor::latex);