1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998 The LyX Team.
8 *======================================================*/
13 #pragma implementation
17 #include "insettext.h"
20 #include "BufferView.h"
22 #include "funcrequest.h"
29 #include "WordLangTuple.h"
31 #include "frontends/Alert.h"
32 #include "frontends/Dialogs.h"
33 #include "frontends/LyXView.h"
35 #include "support/LOstream.h"
48 labelfont = LyXFont(LyXFont::ALL_SANE);
51 labelfont.setColor(LColor::latex);
56 InsetERT::InsetERT(BufferParams const & bp, bool collapsed)
57 : InsetCollapsable(bp, collapsed)
67 InsetERT::InsetERT(InsetERT const & in, bool same_id)
68 : InsetCollapsable(in, same_id), status_(in.status_)
74 Inset * InsetERT::clone(Buffer const &, bool same_id) const
76 return new InsetERT(*const_cast<InsetERT *>(this), same_id);
80 InsetERT::InsetERT(BufferParams const & bp,
81 Language const * l, string const & contents, bool collapsed)
82 : InsetCollapsable(bp, collapsed)
89 LyXFont font(LyXFont::ALL_INHERIT, l);
91 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
92 font.setColor(LColor::latex);
95 string::const_iterator cit = contents.begin();
96 string::const_iterator end = contents.end();
98 for (; cit != end; ++cit) {
99 inset.paragraph()->insertChar(pos++, *cit, font);
101 // the init has to be after the initialization of the paragraph
102 // because of the label settings (draw_label for ert insets).
107 InsetERT::~InsetERT()
113 void InsetERT::read(Buffer const * buf, LyXLex & lex)
115 bool token_found = false;
118 string const token = lex.getString();
119 if (token == "status") {
121 string const tmp_token = lex.getString();
123 if (tmp_token == "Inlined") {
125 } else if (tmp_token == "Collapsed") {
126 status(0, Collapsed);
128 // leave this as default!
134 lyxerr << "InsetERT::Read: Missing 'status'-tag!"
136 // take countermeasures
137 lex.pushToken(token);
141 #warning this should be really short lived only for compatibility to
142 #warning files written 07/08/2001 so this has to go before 1.2.0! (Jug)
145 string const token = lex.getString();
146 if (token == "collapsed") {
148 collapsed_ = lex.getBool();
150 // Take countermeasures
151 lex.pushToken(token);
155 inset.read(buf, lex);
159 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
161 LyXFont font(LyXFont::ALL_INHERIT);
163 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
164 font.setColor(LColor::latex);
165 Paragraph * par = inset.paragraph();
167 pos_type siz = par->size();
168 for (pos_type i = 0; i < siz; ++i) {
169 par->setFont(i, font);
177 status(0, Collapsed);
186 void InsetERT::write(Buffer const * buf, ostream & os) const
202 os << getInsetName() << "\n"
203 << "status "<< st << "\n";
205 //inset.writeParagraphData(buf, os);
206 string const layout(buf->params.getLyXTextClass().defaultLayoutName());
207 Paragraph * par = inset.paragraph();
209 os << "\n\\layout " << layout << "\n";
210 pos_type siz = par->size();
211 for (pos_type i = 0; i < siz; ++i) {
212 Paragraph::value_type c = par->getChar(i);
214 case Paragraph::META_INSET:
215 case Paragraph::META_HFILL:
216 lyxerr << "Element is not allowed in insertERT"
218 case Paragraph::META_NEWLINE:
219 os << "\n\\newline \n";
222 os << "\n\\backslash \n";
234 string const InsetERT::editMessage() const
236 return _("Opened ERT Inset");
240 bool InsetERT::insertInset(BufferView *, Inset *)
246 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
248 // if selectall is activated then the fontchange was an outside general
249 // fontchange and this messages is not needed
251 Alert::alert(_("Impossible Operation!"),
252 _("Not permitted to change font-types inside ERT-insets!"),
257 void InsetERT::updateStatus(BufferView * bv, bool swap) const
259 if (status_ != Inlined) {
261 status(bv, swap ? Open : Collapsed);
263 status(bv, swap ? Collapsed : Open);
268 void InsetERT::edit(BufferView * bv, int x, int y, mouse_button::state button)
270 if (button == mouse_button::button3)
273 if (status_ == Inlined) {
274 if (!bv->lockInset(this))
276 inset.edit(bv, x, y, button);
278 InsetCollapsable::edit(bv, x, y, button);
285 Inset::EDITABLE InsetERT::editable() const
287 if (status_ == Collapsed)
289 return HIGHLY_EDITABLE;
293 void InsetERT::edit(BufferView * bv, bool front)
295 InsetCollapsable::edit(bv, front);
303 void InsetERT::insetButtonPress(BufferView * bv,
304 int x, int y, mouse_button::state button)
306 if (status_ == Inlined) {
307 inset.insetButtonPress(bv, x, y, button);
309 InsetCollapsable::insetButtonPress(bv, x, y, button);
314 bool InsetERT::insetButtonRelease(BufferView * bv, int x, int y,
315 mouse_button::state button)
317 if (button == mouse_button::button3) {
322 if (status_ != Inlined && (x >= 0) && (x < button_length) &&
323 (y >= button_top_y) && (y <= button_bottom_y)) {
324 updateStatus(bv, true);
326 LyXFont font(LyXFont::ALL_SANE);
327 int yy = ascent(bv, font) + y - inset.ascent(bv, font);
329 // inlined is special - the text appears above
331 if (status_ == Inlined) {
332 inset.insetButtonRelease(bv, x, yy, button);
333 } else if (!collapsed_ && (y > button_bottom_y)) {
334 yy -= (ascent_collapsed() + descent_collapsed());
335 inset.insetButtonRelease(bv, x, yy, button);
342 void InsetERT::insetMotionNotify(BufferView * bv,
343 int x, int y, mouse_button::state state)
345 if (status_ == Inlined) {
346 inset.insetMotionNotify(bv, x, y, state);
348 InsetCollapsable::insetMotionNotify(bv, x, y, state);
353 int InsetERT::latex(Buffer const *, ostream & os, bool /*fragile*/,
354 bool /*free_spc*/) const
356 Paragraph * par = inset.paragraph();
359 pos_type siz = par->size();
360 for (pos_type i = 0; i < siz; ++i) {
361 Paragraph::value_type c = par->getChar(i);
363 case Paragraph::META_NEWLINE:
383 int InsetERT::ascii(Buffer const *,
384 ostream &, int /*linelen*/) const
390 int InsetERT::linuxdoc(Buffer const *, ostream & os) const
392 Paragraph * par = inset.paragraph();
395 pos_type siz = par->size();
396 for (pos_type i = 0; i < siz; ++i) {
397 Paragraph::value_type c = par->getChar(i);
399 case Paragraph::META_NEWLINE:
419 int InsetERT::docbook(Buffer const *, ostream & os, bool) const
421 Paragraph * par = inset.paragraph();
424 pos_type siz = par->size();
425 for (pos_type i = 0; i < siz; ++i) {
426 Paragraph::value_type c = par->getChar(i);
428 case Paragraph::META_NEWLINE:
448 UpdatableInset::RESULT
449 InsetERT::localDispatch(BufferView * bv, FuncRequest const & ev)
451 UpdatableInset::RESULT result = DISPATCHED_NOUPDATE;
453 if (inset.paragraph()->empty()) {
459 bv->owner()->setLayout(inset.paragraph()->layout()->name());
462 result = InsetCollapsable::localDispatch(bv, ev);
465 case LFUN_BREAKPARAGRAPH:
466 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
468 case LFUN_BACKSPACE_SKIP:
470 case LFUN_DELETE_SKIP:
471 case LFUN_DELETE_LINE_FORWARD:
483 string const InsetERT::get_new_label() const
486 pos_type const max_length = 15;
487 pos_type const p_siz = inset.paragraph()->size();
488 pos_type const n = min(max_length, p_siz);
491 for(; i < n && j < p_siz; ++j) {
492 if (inset.paragraph()->isInset(j))
494 la += inset.paragraph()->getChar(j);
497 if (inset.paragraph()->next() || (i > 0 && j < p_siz)) {
507 void InsetERT::setButtonLabel() const
509 if (status_ == Collapsed) {
510 setLabel(get_new_label());
517 bool InsetERT::checkInsertChar(LyXFont & /* font */)
521 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
523 LyXFont f(LyXFont::ALL_INHERIT);
526 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
527 font.setColor(LColor::latex);
533 int InsetERT::ascent(BufferView * bv, LyXFont const & font) const
536 return InsetCollapsable::ascent(bv, font);
538 return inset.ascent(bv, font);
542 int InsetERT::descent(BufferView * bv, LyXFont const & font) const
545 return InsetCollapsable::descent(bv, font);
547 return inset.descent(bv, font);
551 int InsetERT::width(BufferView * bv, LyXFont const & font) const
554 return InsetCollapsable::width(bv, font);
556 return inset.width(bv, font);
560 void InsetERT::draw(BufferView * bv, LyXFont const & f,
561 int baseline, float & x, bool cleared) const
563 Painter & pain = bv->painter();
565 button_length = width_collapsed();
566 button_top_y = -ascent(bv, f);
567 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
571 draw_collapsed(pain, baseline, x);
578 x += static_cast<float>(scroll());
580 if (!cleared && (inset.need_update == InsetText::FULL ||
581 inset.need_update == InsetText::INIT ||
583 top_baseline != baseline))
585 // we don't need anymore to clear here we just have to tell
586 // the underlying LyXText that it should do the RowClear!
587 inset.setUpdateStatus(bv, InsetText::FULL);
588 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
594 top_baseline = baseline;
596 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
599 inset.draw(bv, f, baseline, x, cleared);
601 draw_collapsed(pain, bl, old_x);
603 bl + descent_collapsed() + inset.ascent(bv, f),
610 void InsetERT::set_latex_font(BufferView * /* bv */)
614 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
616 LyXFont font(LyXFont::ALL_INHERIT);
619 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
620 font.setColor(LColor::latex);
622 inset.getLyXText(bv)->setFont(bv, font, false);
627 // attention this function can be called with bv == 0
628 void InsetERT::status(BufferView * bv, ERTStatus const st) const
636 inset.setUpdateStatus(bv, InsetText::INIT);
646 bv->unlockInset(const_cast<InsetERT *>(this));
650 bv->updateInset(const_cast<InsetERT *>(this), false);
655 bool InsetERT::showInsetDialog(BufferView * bv) const
657 bv->owner()->getDialogs().showERT(const_cast<InsetERT *>(this));
662 void InsetERT::open(BufferView * bv)
670 void InsetERT::close(BufferView * bv) const
672 if (status_ == Collapsed || status_ == Inlined)
675 status(bv, Collapsed);
680 InsetERT::selectNextWordToSpellcheck(BufferView * bv, float &) const
682 bv->unlockInset(const_cast<InsetERT *>(this));
683 return WordLangTuple();
687 void InsetERT::getDrawFont(LyXFont & font) const
690 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
692 LyXFont f(LyXFont::ALL_INHERIT);
695 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
696 font.setColor(LColor::latex);
700 int InsetERT::getMaxWidth(BufferView * bv, UpdatableInset const * in) const
702 int w = InsetCollapsable::getMaxWidth(bv, in);
703 if (status_ != Inlined || w < 0)
705 LyXText * text = inset.getLyXText(bv);
706 int rw = text->firstRow()->width();
710 if (!text->firstRow()->next() && rw < w)
716 void InsetERT::update(BufferView * bv, LyXFont const & font,
719 if (inset.need_update & InsetText::INIT ||
720 inset.need_update & InsetText::FULL)
724 InsetCollapsable::update(bv, font, reinit);