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"
24 #include "funcrequest.h"
29 #include "insets/insettext.h"
31 #include "frontends/Dialogs.h"
32 #include "frontends/Alert.h"
34 #include "support/LOstream.h"
47 labelfont = LyXFont(LyXFont::ALL_SANE);
50 labelfont.setColor(LColor::latex);
55 InsetERT::InsetERT(BufferParams const & bp, bool collapsed)
56 : InsetCollapsable(bp, collapsed)
66 InsetERT::InsetERT(InsetERT const & in, bool same_id)
67 : InsetCollapsable(in, same_id), status_(in.status_)
73 Inset * InsetERT::clone(Buffer const &, bool same_id) const
75 return new InsetERT(*const_cast<InsetERT *>(this), same_id);
79 InsetERT::InsetERT(BufferParams const & bp,
80 Language const * l, string const & contents, bool collapsed)
81 : InsetCollapsable(bp, collapsed)
88 LyXFont font(LyXFont::ALL_INHERIT, l);
90 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
91 font.setColor(LColor::latex);
94 string::const_iterator cit = contents.begin();
95 string::const_iterator end = contents.end();
97 for (; cit != end; ++cit) {
98 inset.paragraph()->insertChar(pos++, *cit, font);
100 // the init has to be after the initialization of the paragraph
101 // because of the label settings (draw_label for ert insets).
106 InsetERT::~InsetERT()
112 void InsetERT::read(Buffer const * buf, LyXLex & lex)
114 bool token_found = false;
117 string const token = lex.getString();
118 if (token == "status") {
120 string const tmp_token = lex.getString();
122 if (tmp_token == "Inlined") {
124 } else if (tmp_token == "Collapsed") {
125 status(0, Collapsed);
127 // leave this as default!
133 lyxerr << "InsetERT::Read: Missing 'status'-tag!"
135 // take countermeasures
136 lex.pushToken(token);
140 #warning this should be really short lived only for compatibility to
141 #warning files written 07/08/2001 so this has to go before 1.2.0! (Jug)
144 string const token = lex.getString();
145 if (token == "collapsed") {
147 collapsed_ = lex.getBool();
149 // Take countermeasures
150 lex.pushToken(token);
154 inset.read(buf, lex);
158 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
160 LyXFont font(LyXFont::ALL_INHERIT);
162 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
163 font.setColor(LColor::latex);
164 Paragraph * par = inset.paragraph();
166 pos_type siz = par->size();
167 for (pos_type i = 0; i < siz; ++i) {
168 par->setFont(i, font);
176 status(0, Collapsed);
185 void InsetERT::write(Buffer const * buf, ostream & os) const
201 os << getInsetName() << "\n"
202 << "status "<< st << "\n";
204 //inset.writeParagraphData(buf, os);
205 string const layout(buf->params.getLyXTextClass().defaultLayoutName());
206 Paragraph * par = inset.paragraph();
208 os << "\n\\layout " << layout << "\n";
209 pos_type siz = par->size();
210 for (pos_type i = 0; i < siz; ++i) {
211 Paragraph::value_type c = par->getChar(i);
213 case Paragraph::META_INSET:
214 case Paragraph::META_HFILL:
215 lyxerr << "Element is not allowed in insertERT"
217 case Paragraph::META_NEWLINE:
218 os << "\n\\newline \n";
221 os << "\n\\backslash \n";
233 string const InsetERT::editMessage() const
235 return _("Opened ERT Inset");
239 bool InsetERT::insertInset(BufferView *, Inset *)
245 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
247 // if selectall is activated then the fontchange was an outside general
248 // fontchange and this messages is not needed
250 Alert::alert(_("Impossible Operation!"),
251 _("Not permitted to change font-types inside ERT-insets!"),
256 void InsetERT::updateStatus(BufferView * bv, bool swap) const
258 if (status_ != Inlined) {
260 status(bv, swap ? Open : Collapsed);
262 status(bv, swap ? Collapsed : Open);
267 void InsetERT::edit(BufferView * bv, int x, int y, mouse_button::state button)
269 if (button == mouse_button::button3)
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, mouse_button::state 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,
314 mouse_button::state button)
316 if (button == mouse_button::button3) {
321 if (status_ != Inlined && (x >= 0) && (x < button_length) &&
322 (y >= button_top_y) && (y <= button_bottom_y)) {
323 updateStatus(bv, true);
325 LyXFont font(LyXFont::ALL_SANE);
326 int yy = ascent(bv, font) + y - inset.ascent(bv, font);
328 // inlined is special - the text appears above
330 if (status_ == Inlined) {
331 inset.insetButtonRelease(bv, x, yy, button);
332 } else if (!collapsed_ && (y > button_bottom_y)) {
333 yy -= (ascent_collapsed() + descent_collapsed());
334 inset.insetButtonRelease(bv, x, yy, button);
341 void InsetERT::insetMotionNotify(BufferView * bv,
342 int x, int y, mouse_button::state state)
344 if (status_ == Inlined) {
345 inset.insetMotionNotify(bv, x, y, state);
347 InsetCollapsable::insetMotionNotify(bv, x, y, state);
352 int InsetERT::latex(Buffer const *, ostream & os, bool /*fragile*/,
353 bool /*free_spc*/) const
355 Paragraph * par = inset.paragraph();
358 pos_type siz = par->size();
359 for (pos_type i = 0; i < siz; ++i) {
360 Paragraph::value_type c = par->getChar(i);
362 case Paragraph::META_NEWLINE:
382 int InsetERT::ascii(Buffer const *,
383 ostream &, int /*linelen*/) const
389 int InsetERT::linuxdoc(Buffer const *, ostream & os) const
391 Paragraph * par = inset.paragraph();
394 pos_type siz = par->size();
395 for (pos_type i = 0; i < siz; ++i) {
396 Paragraph::value_type c = par->getChar(i);
398 case Paragraph::META_NEWLINE:
418 int InsetERT::docbook(Buffer const *, ostream & os, bool) const
420 Paragraph * par = inset.paragraph();
423 pos_type siz = par->size();
424 for (pos_type i = 0; i < siz; ++i) {
425 Paragraph::value_type c = par->getChar(i);
427 case Paragraph::META_NEWLINE:
447 UpdatableInset::RESULT
448 InsetERT::localDispatch(BufferView * bv, FuncRequest const & ev)
450 UpdatableInset::RESULT result = DISPATCHED_NOUPDATE;
452 if (inset.paragraph()->empty()) {
458 bv->owner()->setLayout(inset.paragraph()->layout()->name());
461 result = InsetCollapsable::localDispatch(bv, ev);
464 case LFUN_BREAKPARAGRAPH:
465 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
467 case LFUN_BACKSPACE_SKIP:
469 case LFUN_DELETE_SKIP:
470 case LFUN_DELETE_LINE_FORWARD:
482 string const InsetERT::get_new_label() const
485 pos_type const max_length = 15;
486 pos_type const p_siz = inset.paragraph()->size();
487 pos_type const n = min(max_length, p_siz);
490 for(; i < n && j < p_siz; ++j) {
491 if (inset.paragraph()->isInset(j))
493 la += inset.paragraph()->getChar(j);
496 if (inset.paragraph()->next() || (i > 0 && j < p_siz)) {
506 void InsetERT::setButtonLabel() const
508 if (status_ == Collapsed) {
509 setLabel(get_new_label());
516 bool InsetERT::checkInsertChar(LyXFont & /* font */)
520 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
522 LyXFont f(LyXFont::ALL_INHERIT);
525 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
526 font.setColor(LColor::latex);
532 int InsetERT::ascent(BufferView * bv, LyXFont const & font) const
535 return InsetCollapsable::ascent(bv, font);
537 return inset.ascent(bv, font);
541 int InsetERT::descent(BufferView * bv, LyXFont const & font) const
544 return InsetCollapsable::descent(bv, font);
546 return inset.descent(bv, font);
550 int InsetERT::width(BufferView * bv, LyXFont const & font) const
553 return InsetCollapsable::width(bv, font);
555 return inset.width(bv, font);
559 void InsetERT::draw(BufferView * bv, LyXFont const & f,
560 int baseline, float & x, bool cleared) const
562 Painter & pain = bv->painter();
564 button_length = width_collapsed();
565 button_top_y = -ascent(bv, f);
566 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
570 draw_collapsed(pain, baseline, x);
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 // attention this function can be called with bv == 0
627 void InsetERT::status(BufferView * bv, ERTStatus const st) const
635 inset.setUpdateStatus(bv, InsetText::INIT);
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
671 if (status_ == Collapsed || status_ == Inlined)
674 status(bv, Collapsed);
678 WordLangTuple InsetERT::selectNextWordToSpellcheck(BufferView * bv,
681 bv->unlockInset(const_cast<InsetERT *>(this));
682 return WordLangTuple();
686 void InsetERT::getDrawFont(LyXFont & font) const
689 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
691 LyXFont f(LyXFont::ALL_INHERIT);
694 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
695 font.setColor(LColor::latex);
699 int InsetERT::getMaxWidth(BufferView * bv, UpdatableInset const * in) const
701 int w = InsetCollapsable::getMaxWidth(bv, in);
702 if (status_ != Inlined || w < 0)
704 LyXText * text = inset.getLyXText(bv);
705 int rw = text->firstRow()->width();
709 if (!text->firstRow()->next() && rw < w)
715 void InsetERT::update(BufferView * bv, LyXFont const & font,
718 if (inset.need_update & InsetText::INIT ||
719 inset.need_update & InsetText::FULL)
723 InsetCollapsable::update(bv, font, reinit);