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(string const & contents, bool collapsed)
79 : InsetCollapsable(collapsed)
86 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
88 LyXFont font(LyXFont::ALL_INHERIT);
90 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
91 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);
155 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
157 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);
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 =
203 textclasslist.NameOfLayout(buf->params.textclass, 0);
204 Paragraph * par = inset.paragraph();
206 os << "\n\\layout " << layout << "\n";
207 pos_type siz = par->size();
208 for (pos_type i = 0; i < siz; ++i) {
209 Paragraph::value_type c = par->getChar(i);
211 case Paragraph::META_INSET:
212 case Paragraph::META_HFILL:
213 lyxerr << "Element is not allowed in insertERT"
215 case Paragraph::META_NEWLINE:
216 os << "\n\\newline \n";
219 os << "\n\\backslash \n";
231 string const InsetERT::editMessage() const
233 return _("Opened ERT Inset");
237 bool InsetERT::insertInset(BufferView *, Inset *)
243 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
245 // if selectall is activated then the fontchange was an outside general
246 // fontchange and this messages is not needed
248 Alert::alert(_("Impossible Operation!"),
249 _("Not permitted to change font-types inside ERT-insets!"),
254 void InsetERT::updateStatus(BufferView * bv, bool swap) const
256 if (status_ != Inlined) {
258 status(bv, swap ? Open : Collapsed);
260 status(bv, swap ? Collapsed : Open);
266 void InsetERT::edit(BufferView * bv, int x, int y, unsigned int button)
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, int 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, int button)
319 if (status_ != Inlined && (x >= 0) && (x < button_length) &&
320 (y >= button_top_y) && (y <= button_bottom_y)) {
321 updateStatus(bv, true);
323 LyXFont font(LyXFont::ALL_SANE);
324 int yy = ascent(bv, font) + y - inset.ascent(bv, font);
326 // inlined is special - the text appears above
328 if (status_ == Inlined) {
329 inset.insetButtonRelease(bv, x, yy, button);
330 } else if (!collapsed_ && (y > button_bottom_y)) {
331 yy -= (ascent_collapsed() + descent_collapsed());
332 inset.insetButtonRelease(bv, x, yy, button);
339 void InsetERT::insetMotionNotify(BufferView * bv,
340 int x, int y, int state)
342 if (status_ == Inlined) {
343 inset.insetMotionNotify(bv, x, y, state);
345 InsetCollapsable::insetMotionNotify(bv, x, y, state);
350 int InsetERT::latex(Buffer const *, ostream & os, bool /*fragile*/,
351 bool /*free_spc*/) const
353 Paragraph * par = inset.paragraph();
356 pos_type siz = par->size();
357 for (pos_type i = 0; i < siz; ++i) {
358 Paragraph::value_type c = par->getChar(i);
360 case Paragraph::META_NEWLINE:
380 int InsetERT::ascii(Buffer const *,
381 ostream &, int /*linelen*/) const
387 int InsetERT::linuxdoc(Buffer const *, ostream & os) const
389 Paragraph * par = inset.paragraph();
392 pos_type siz = par->size();
393 for (pos_type i = 0; i < siz; ++i) {
394 Paragraph::value_type c = par->getChar(i);
396 case Paragraph::META_NEWLINE:
416 int InsetERT::docbook(Buffer const *, ostream & os) const
418 Paragraph * par = inset.paragraph();
421 pos_type siz = par->size();
422 for (pos_type i = 0; i < siz; ++i) {
423 Paragraph::value_type c = par->getChar(i);
425 case Paragraph::META_NEWLINE:
445 UpdatableInset::RESULT
446 InsetERT::localDispatch(BufferView * bv, kb_action action, string const & arg)
448 UpdatableInset::RESULT result = DISPATCHED_NOUPDATE;
450 if (!inset.paragraph()->size()) {
456 bv->owner()->setLayout(inset.paragraph()->getLayout());
459 result = InsetCollapsable::localDispatch(bv, action, arg);
462 case LFUN_BREAKPARAGRAPH:
463 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
465 case LFUN_BACKSPACE_SKIP:
467 case LFUN_DELETE_SKIP:
468 case LFUN_DELETE_LINE_FORWARD:
480 string const InsetERT::get_new_label() const
483 pos_type const max_length = 15;
484 pos_type const p_siz = inset.paragraph()->size();
485 pos_type const n = min(max_length, p_siz);
488 for(; i < n && j < p_siz; ++j) {
489 if (inset.paragraph()->isInset(j))
491 la += inset.paragraph()->getChar(j);
494 if (i > 0 && j < p_siz) {
504 void InsetERT::setButtonLabel() const
506 if (status_ == Collapsed) {
507 setLabel(get_new_label());
514 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);
528 int InsetERT::ascent(BufferView * bv, LyXFont const & font) const
531 return InsetCollapsable::ascent(bv, font);
533 return inset.ascent(bv, font);
537 int InsetERT::descent(BufferView * bv, LyXFont const & font) const
540 return InsetCollapsable::descent(bv, font);
542 return inset.descent(bv, font);
546 int InsetERT::width(BufferView * bv, LyXFont const & font) const
549 return InsetCollapsable::width(bv, font);
551 return inset.width(bv, font);
555 void InsetERT::draw(BufferView * bv, LyXFont const & f,
556 int baseline, float & x, bool cleared) const
558 Painter & pain = bv->painter();
560 button_length = width_collapsed();
561 button_top_y = -ascent(bv, f);
562 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
566 draw_collapsed(pain, baseline, x);
567 x += TEXT_TO_INSET_OFFSET;
574 x += static_cast<float>(scroll());
576 if (!cleared && (inset.need_update == InsetText::FULL ||
577 inset.need_update == InsetText::INIT ||
579 top_baseline != baseline))
581 // we don't need anymore to clear here we just have to tell
582 // the underlying LyXText that it should do the RowClear!
583 inset.setUpdateStatus(bv, InsetText::FULL);
584 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
590 top_baseline = baseline;
592 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
595 inset.draw(bv, f, baseline, x, cleared);
597 draw_collapsed(pain, bl, old_x);
599 bl + descent_collapsed() + inset.ascent(bv, f),
606 void InsetERT::set_latex_font(BufferView * bv)
609 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
611 LyXFont font(LyXFont::ALL_INHERIT);
614 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
615 font.setColor(LColor::latex);
616 inset.getLyXText(bv)->setFont(bv, font, false);
620 void InsetERT::status(BufferView * bv, ERTStatus const st) const
626 inset.setAutoBreakRows(false);
629 inset.setAutoBreakRows(true);
635 inset.setAutoBreakRows(true);
640 bv->unlockInset(const_cast<InsetERT *>(this));
644 bv->updateInset(const_cast<InsetERT *>(this), false);
649 bool InsetERT::showInsetDialog(BufferView * bv) const
651 bv->owner()->getDialogs()->showERT(const_cast<InsetERT *>(this));
656 void InsetERT::open(BufferView * bv)
664 void InsetERT::close(BufferView * bv) const
668 status(bv, Collapsed);
672 string const InsetERT::selectNextWordToSpellcheck(BufferView * bv,float &) const
674 bv->unlockInset(const_cast<InsetERT *>(this));