1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998 The LyX Team.
8 *======================================================*/
13 #pragma implementation
21 #include "insets/insettext.h"
22 #include "support/LOstream.h"
23 #include "lyx_gui_misc.h"
24 #include "BufferView.h"
27 #include "frontends/Dialogs.h"
28 #include "frontends/Alert.h"
38 labelfont = LyXFont(LyXFont::ALL_SANE);
41 labelfont.setColor(LColor::latex);
46 InsetERT::InsetERT(bool collapsed)
47 : InsetCollapsable(collapsed)
57 InsetERT::InsetERT(InsetERT const & in, bool same_id)
58 : InsetCollapsable(in, same_id), status_(in.status_)
64 Inset * InsetERT::clone(Buffer const &, bool same_id) const
66 return new InsetERT(*const_cast<InsetERT *>(this), same_id);
70 InsetERT::InsetERT(string const & contents, bool collapsed)
71 : InsetCollapsable(collapsed)
78 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
80 LyXFont font(LyXFont::ALL_INHERIT);
82 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
83 font.setColor(LColor::latex);
84 string::const_iterator cit = contents.begin();
85 string::const_iterator end = contents.end();
87 for (; cit != end; ++cit) {
88 inset.paragraph()->insertChar(pos++, *cit, font);
90 // the init has to be after the initialization of the paragraph
91 // because of the label settings (draw_label for ert insets).
102 void InsetERT::read(Buffer const * buf, LyXLex & lex)
104 bool token_found = false;
107 string const token = lex.getString();
108 if (token == "status") {
110 string const tmp_token = lex.getString();
112 if (tmp_token == "Inlined") {
114 } else if (tmp_token == "Collapsed") {
115 status(0, Collapsed);
117 // leave this as default!
123 lyxerr << "InsetERT::Read: Missing 'status'-tag!"
125 // take countermeasures
126 lex.pushToken(token);
130 #warning this should be really short lived only for compatibility to
131 #warning files written 07/08/2001 so this has to go before 1.2.0! (Jug)
134 string const token = lex.getString();
135 if (token == "collapsed") {
137 collapsed_ = lex.getBool();
139 // Take countermeasures
140 lex.pushToken(token);
144 inset.read(buf, lex);
147 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
149 LyXFont font(LyXFont::ALL_INHERIT);
152 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
153 font.setColor(LColor::latex);
154 Paragraph * par = inset.paragraph();
156 pos_type siz = par->size();
157 for (pos_type i = 0; i < siz; ++i) {
158 par->setFont(i, font);
165 status(0, Collapsed);
174 void InsetERT::write(Buffer const * buf, ostream & os) const
190 os << getInsetName() << "\n"
191 << "status "<< st << "\n";
193 //inset.writeParagraphData(buf, os);
194 string const layout =
195 textclasslist.NameOfLayout(buf->params.textclass, 0);
196 Paragraph * par = inset.paragraph();
198 os << "\n\\layout " << layout << "\n";
199 pos_type siz = par->size();
200 for (pos_type i = 0; i < siz; ++i) {
201 Paragraph::value_type c = par->getChar(i);
203 case Paragraph::META_INSET:
204 case Paragraph::META_HFILL:
205 lyxerr << "Element is not allowed in insertERT"
207 case Paragraph::META_NEWLINE:
208 os << "\n\\newline \n";
211 os << "\n\\backslash \n";
223 string const InsetERT::editMessage() const
225 return _("Opened ERT Inset");
229 bool InsetERT::insertInset(BufferView *, Inset *)
235 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
237 // if selectall is activated then the fontchange was an outside general
238 // fontchange and this messages is not needed
240 Alert::alert(_("Impossible Operation!"),
241 _("Not permitted to change font-types inside ERT-insets!"),
246 void InsetERT::updateStatus(BufferView * bv, bool swap) const
248 if (status_ != Inlined) {
250 status(bv, swap ? Open : Collapsed);
252 status(bv, swap ? Collapsed : Open);
258 void InsetERT::edit(BufferView * bv, int x, int y, unsigned int button)
263 if (status_ == Inlined) {
264 if (!bv->lockInset(this))
266 inset.edit(bv, x, y, button);
268 InsetCollapsable::edit(bv, x, y, button);
275 Inset::EDITABLE InsetERT::editable() const
277 if (status_ == Collapsed)
279 return HIGHLY_EDITABLE;
283 void InsetERT::edit(BufferView * bv, bool front)
285 InsetCollapsable::edit(bv, front);
293 void InsetERT::insetButtonPress(BufferView * bv,
294 int x, int y, int button)
296 if (status_ == Inlined) {
297 inset.insetButtonPress(bv, x, y, button);
299 InsetCollapsable::insetButtonPress(bv, x, y, button);
304 bool InsetERT::insetButtonRelease(BufferView * bv, int x, int y, int button)
311 if (status_ != Inlined && (x >= 0) && (x < button_length) &&
312 (y >= button_top_y) && (y <= button_bottom_y)) {
313 updateStatus(bv, true);
315 LyXFont font(LyXFont::ALL_SANE);
316 int yy = ascent(bv, font) + y - inset.ascent(bv, font);
318 // inlined is special - the text appears above
320 if (status_ == Inlined) {
321 inset.insetButtonRelease(bv, x, yy, button);
322 } else if (!collapsed_ && (y > button_bottom_y)) {
323 yy -= (ascent_collapsed() + descent_collapsed());
324 inset.insetButtonRelease(bv, x, yy, button);
331 void InsetERT::insetMotionNotify(BufferView * bv,
332 int x, int y, int state)
334 if (status_ == Inlined) {
335 inset.insetMotionNotify(bv, x, y, state);
337 InsetCollapsable::insetMotionNotify(bv, x, y, state);
342 int InsetERT::latex(Buffer const *, std::ostream & os, bool /*fragile*/,
343 bool /*free_spc*/) const
345 Paragraph * par = inset.paragraph();
348 pos_type siz = par->size();
349 for (pos_type i = 0; i < siz; ++i) {
350 Paragraph::value_type c = par->getChar(i);
352 case Paragraph::META_NEWLINE:
372 int InsetERT::ascii(Buffer const *,
373 std::ostream &, int /*linelen*/) const
379 int InsetERT::linuxdoc(Buffer const *, std::ostream & os) const
381 Paragraph * par = inset.paragraph();
384 pos_type siz = par->size();
385 for (pos_type i = 0; i < siz; ++i) {
386 Paragraph::value_type c = par->getChar(i);
388 case Paragraph::META_NEWLINE:
408 int InsetERT::docbook(Buffer const *, std::ostream & os) const
410 Paragraph * par = inset.paragraph();
413 pos_type siz = par->size();
414 for (pos_type i = 0; i < siz; ++i) {
415 Paragraph::value_type c = par->getChar(i);
417 case Paragraph::META_NEWLINE:
437 UpdatableInset::RESULT
438 InsetERT::localDispatch(BufferView * bv, kb_action action, string const & arg)
440 UpdatableInset::RESULT result = DISPATCHED_NOUPDATE;
442 if (!inset.paragraph()->size()) {
448 bv->owner()->setLayout(inset.paragraph()->getLayout());
451 result = InsetCollapsable::localDispatch(bv, action, arg);
454 case LFUN_BREAKPARAGRAPH:
455 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
457 case LFUN_BACKSPACE_SKIP:
459 case LFUN_DELETE_SKIP:
460 case LFUN_DELETE_LINE_FORWARD:
472 string const InsetERT::get_new_label() const
475 pos_type const max_length = 15;
476 pos_type const p_siz = inset.paragraph()->size();
477 pos_type const n = std::min(max_length, p_siz);
480 for(; i < n && j < p_siz; ++j) {
481 if (inset.paragraph()->isInset(j))
483 la += inset.paragraph()->getChar(j);
486 if (i > 0 && j < p_siz) {
496 void InsetERT::setButtonLabel() const
498 if (status_ == Collapsed) {
499 setLabel(get_new_label());
506 bool InsetERT::checkInsertChar(LyXFont & font)
509 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
511 LyXFont f(LyXFont::ALL_INHERIT);
514 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
515 font.setColor(LColor::latex);
520 int InsetERT::ascent(BufferView * bv, LyXFont const & font) const
523 return InsetCollapsable::ascent(bv, font);
525 return inset.ascent(bv, font);
529 int InsetERT::descent(BufferView * bv, LyXFont const & font) const
532 return InsetCollapsable::descent(bv, font);
534 return inset.descent(bv, font);
538 int InsetERT::width(BufferView * bv, LyXFont const & font) const
541 return InsetCollapsable::width(bv, font);
543 return inset.width(bv, font);
547 void InsetERT::draw(BufferView * bv, LyXFont const & f,
548 int baseline, float & x, bool cleared) const
550 Painter & pain = bv->painter();
552 button_length = width_collapsed();
553 button_top_y = -ascent(bv, f);
554 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
558 draw_collapsed(pain, baseline, x);
559 x += TEXT_TO_INSET_OFFSET;
566 x += static_cast<float>(scroll());
568 if (!cleared && (inset.need_update == InsetText::FULL ||
569 inset.need_update == InsetText::INIT ||
571 top_baseline != baseline))
573 // we don't need anymore to clear here we just have to tell
574 // the underlying LyXText that it should do the RowClear!
575 inset.setUpdateStatus(bv, InsetText::FULL);
576 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
582 top_baseline = baseline;
584 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
587 inset.draw(bv, f, baseline, x, cleared);
589 draw_collapsed(pain, bl, old_x);
591 bl + descent_collapsed() + inset.ascent(bv, f),
598 void InsetERT::set_latex_font(BufferView * bv)
601 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
603 LyXFont font(LyXFont::ALL_INHERIT);
606 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
607 font.setColor(LColor::latex);
608 inset.getLyXText(bv)->setFont(bv, font, false);
612 void InsetERT::status(BufferView * bv, ERTStatus const st) const
618 inset.setAutoBreakRows(false);
621 inset.setAutoBreakRows(true);
627 inset.setAutoBreakRows(true);
632 bv->unlockInset(const_cast<InsetERT *>(this));
636 bv->updateInset(const_cast<InsetERT *>(this), false);
641 bool InsetERT::showInsetDialog(BufferView * bv) const
643 bv->owner()->getDialogs()->showERT(const_cast<InsetERT *>(this));
648 void InsetERT::open(BufferView * bv)
656 void InsetERT::close(BufferView * bv) const
660 status(bv, Collapsed);
664 string const InsetERT::selectNextWordToSpellcheck(BufferView * bv,float &) const
666 bv->unlockInset(const_cast<InsetERT *>(this));