1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998 The LyX Team.
8 *======================================================*/
13 #pragma implementation
21 #include "lyx_gui_misc.h"
22 #include "BufferView.h"
26 #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(bool collapsed)
55 : InsetCollapsable(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(Language const * l, string const & contents, bool collapsed)
79 : InsetCollapsable(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 =
204 textclasslist.NameOfLayout(buf->params.textclass, 0);
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);
267 void InsetERT::edit(BufferView * bv, int x, int y, unsigned int button)
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, int 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, int button)
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, int 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()->getLayout());
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 (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);
570 x += TEXT_TO_INSET_OFFSET;
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 void InsetERT::status(BufferView * bv, ERTStatus const st) const
632 inset.setAutoBreakRows(false);
635 inset.setAutoBreakRows(true);
641 inset.setAutoBreakRows(true);
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
674 status(bv, Collapsed);
678 string const InsetERT::selectNextWordToSpellcheck(BufferView * bv,float &) const
680 bv->unlockInset(const_cast<InsetERT *>(this));
684 void InsetERT::getDrawFont(LyXFont & font) const
687 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
689 LyXFont f(LyXFont::ALL_INHERIT);
692 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
693 font.setColor(LColor::latex);