1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998 The LyX Team.
8 *======================================================*/
13 #pragma implementation
20 #include "insets/insettext.h"
21 #include "support/LOstream.h"
22 #include "lyx_gui_misc.h"
23 #include "BufferView.h"
26 #include "frontends/Dialogs.h"
27 #include "frontends/Alert.h"
35 labelfont = LyXFont(LyXFont::ALL_SANE);
38 labelfont.setColor(LColor::latex);
44 InsetERT::InsetERT(bool collapsed)
45 : InsetCollapsable(collapsed)
55 InsetERT::InsetERT(InsetERT const & in, bool same_id)
56 : InsetCollapsable(in, same_id), status_(in.status_)
62 Inset * InsetERT::clone(Buffer const &, bool same_id) const
64 return new InsetERT(*const_cast<InsetERT *>(this), same_id);
68 InsetERT::InsetERT(string const & contents, bool collapsed)
69 : InsetCollapsable(collapsed)
76 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
78 LyXFont font(LyXFont::ALL_INHERIT);
80 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
81 font.setColor(LColor::latex);
82 string::const_iterator cit = contents.begin();
83 string::const_iterator end = contents.end();
84 Paragraph::size_type pos = 0;
85 for (; cit != end; ++cit) {
86 inset.paragraph()->insertChar(pos++, *cit, font);
88 // the init has to be after the initialization of the paragraph
89 // because of the label settings (draw_label for ert insets).
100 void InsetERT::read(Buffer const * buf, LyXLex & lex)
102 bool token_found = false;
105 string const token = lex.getString();
106 if (token == "status") {
108 string const tmp_token = lex.getString();
110 if (tmp_token == "Inlined") {
112 } else if (tmp_token == "Collapsed") {
113 status(0, Collapsed);
115 // leave this as default!
121 lyxerr << "InsetERT::Read: Missing 'status'-tag!"
123 // take countermeasures
124 lex.pushToken(token);
127 #warning this should be really short lived only for compatibility to
128 #warning files written 07/08/2001 so this has to go before 1.2.0! (Jug)
131 string const token = lex.getString();
132 if (token == "collapsed") {
134 collapsed_ = lex.getBool();
136 // Take countermeasures
137 lex.pushToken(token);
140 inset.read(buf, lex);
143 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
145 LyXFont font(LyXFont::ALL_INHERIT);
148 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
149 font.setColor(LColor::latex);
150 Paragraph * par = inset.paragraph();
152 Paragraph::size_type siz = par->size();
153 for (Paragraph::size_type i = 0; i < siz; ++i) {
154 par->setFont(i, font);
161 status(0, Collapsed);
170 void InsetERT::write(Buffer const * buf, ostream & os) const
186 os << getInsetName() << "\n"
187 << "status "<< st << "\n";
189 //inset.writeParagraphData(buf, os);
190 string const layout =
191 textclasslist.NameOfLayout(buf->params.textclass, 0);
192 Paragraph * par = inset.paragraph();
194 os << "\n\\layout " << layout << "\n";
195 Paragraph::size_type siz = par->size();
196 for (Paragraph::size_type i = 0; i < siz; ++i) {
197 Paragraph::value_type c = par->getChar(i);
199 case Paragraph::META_INSET:
200 case Paragraph::META_HFILL:
201 lyxerr << "Element is not allowed in insertERT"
203 case Paragraph::META_NEWLINE:
204 os << "\n\\newline \n";
207 os << "\n\\backslash \n";
219 string const InsetERT::editMessage() const
221 return _("Opened ERT Inset");
225 bool InsetERT::insertInset(BufferView *, Inset *)
231 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
233 // if selectall is activated then the fontchange was an outside general
234 // fontchange and this messages is not needed
236 Alert::alert(_("Impossible Operation!"),
237 _("Not permitted to change font-types inside ERT-insets!"),
242 void InsetERT::updateStatus(BufferView * bv, bool swap) const
244 if (status_ != Inlined) {
246 status(bv, swap ? Open : Collapsed);
248 status(bv, swap ? Collapsed : Open);
254 void InsetERT::edit(BufferView * bv, int x, int y, unsigned int button)
259 InsetCollapsable::edit(bv, x, y, button);
265 Inset::EDITABLE InsetERT::editable() const
267 if (status_ == Collapsed)
269 return HIGHLY_EDITABLE;
273 void InsetERT::edit(BufferView * bv, bool front)
275 InsetCollapsable::edit(bv, front);
281 void InsetERT::insetButtonRelease(BufferView * bv, int x, int y, int button)
288 if (status_ != Inlined && (x >= 0) && (x < button_length) &&
289 (y >= button_top_y) && (y <= button_bottom_y)) {
290 updateStatus(bv, true);
292 LyXFont font(LyXFont::ALL_SANE);
293 int yy = ascent(bv, font) + y - inset.ascent(bv, font);
295 // inlined is special - the text appears above
297 if (status_ == Inlined) {
298 inset.insetButtonRelease(bv, x, yy, button);
299 } else if (!collapsed_ && (y > button_bottom_y)) {
300 yy -= (ascent_collapsed() + descent_collapsed());
301 inset.insetButtonRelease(bv, x, yy, button);
307 int InsetERT::latex(Buffer const *, std::ostream & os, bool /*fragile*/,
308 bool /*free_spc*/) const
310 Paragraph * par = inset.paragraph();
313 Paragraph::size_type siz = par->size();
314 for (Paragraph::size_type i = 0; i < siz; ++i) {
315 Paragraph::value_type c = par->getChar(i);
317 case Paragraph::META_NEWLINE:
337 int InsetERT::ascii(Buffer const *,
338 std::ostream &, int /*linelen*/) const
344 int InsetERT::linuxdoc(Buffer const *, std::ostream & os) const
346 Paragraph * par = inset.paragraph();
349 Paragraph::size_type siz = par->size();
350 for (Paragraph::size_type i = 0; i < siz; ++i) {
351 Paragraph::value_type c = par->getChar(i);
353 case Paragraph::META_NEWLINE:
373 int InsetERT::docbook(Buffer const *, std::ostream & os) const
375 Paragraph * par = inset.paragraph();
378 Paragraph::size_type siz = par->size();
379 for (Paragraph::size_type i = 0; i < siz; ++i) {
380 Paragraph::value_type c = par->getChar(i);
382 case Paragraph::META_NEWLINE:
402 UpdatableInset::RESULT
403 InsetERT::localDispatch(BufferView * bv, kb_action action, string const & arg)
405 UpdatableInset::RESULT result = DISPATCHED_NOUPDATE;
407 if (!inset.paragraph()->size()) {
413 bv->owner()->setLayout(inset.paragraph()->getLayout());
416 result = InsetCollapsable::localDispatch(bv, action, arg);
419 case LFUN_BREAKPARAGRAPH:
420 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
422 case LFUN_BACKSPACE_SKIP:
424 case LFUN_DELETE_SKIP:
425 case LFUN_DELETE_LINE_FORWARD:
437 string const InsetERT::get_new_label() const
440 Paragraph::size_type const max_length = 15;
442 Paragraph::size_type const p_siz = inset.paragraph()->size();
443 Paragraph::size_type const n = std::min(max_length, p_siz);
446 for(; i < n && j < p_siz; ++j) {
447 if (inset.paragraph()->isInset(j))
449 la += inset.paragraph()->getChar(j);
452 if (i > 0 && j < p_siz) {
462 void InsetERT::setButtonLabel() const
464 if (status_ == Collapsed) {
465 setLabel(get_new_label());
472 bool InsetERT::checkInsertChar(LyXFont & font)
475 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
477 LyXFont f(LyXFont::ALL_INHERIT);
480 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
481 font.setColor(LColor::latex);
486 int InsetERT::ascent(BufferView * bv, LyXFont const & font) const
489 return InsetCollapsable::ascent(bv, font);
491 return inset.ascent(bv, font);
495 int InsetERT::descent(BufferView * bv, LyXFont const & font) const
498 return InsetCollapsable::descent(bv, font);
500 return inset.descent(bv, font);
504 int InsetERT::width(BufferView * bv, LyXFont const & font) const
507 return InsetCollapsable::width(bv, font);
509 return inset.width(bv, font);
513 void InsetERT::draw(BufferView * bv, LyXFont const & f,
514 int baseline, float & x, bool cleared) const
516 Painter & pain = bv->painter();
518 button_length = width_collapsed();
519 button_top_y = -ascent(bv, f);
520 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
524 draw_collapsed(pain, baseline, x);
525 x += TEXT_TO_INSET_OFFSET;
532 x += static_cast<float>(scroll());
534 if (!cleared && (inset.need_update == InsetText::FULL ||
535 inset.need_update == InsetText::INIT ||
537 top_baseline != baseline))
539 // we don't need anymore to clear here we just have to tell
540 // the underlying LyXText that it should do the RowClear!
541 inset.setUpdateStatus(bv, InsetText::FULL);
542 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
548 top_baseline = baseline;
550 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
553 inset.draw(bv, f, baseline, x, cleared);
555 draw_collapsed(pain, bl, old_x);
557 bl + descent_collapsed() + inset.ascent(bv, f),
564 void InsetERT::set_latex_font(BufferView * bv)
567 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
569 LyXFont font(LyXFont::ALL_INHERIT);
572 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
573 font.setColor(LColor::latex);
574 inset.getLyXText(bv)->setFont(bv, font, false);
578 void InsetERT::status(BufferView * bv, ERTStatus const st) const
584 inset.setAutoBreakRows(false);
587 inset.setAutoBreakRows(true);
593 inset.setAutoBreakRows(true);
598 bv->unlockInset(const_cast<InsetERT *>(this));
602 bv->updateInset(const_cast<InsetERT *>(this), false);
607 bool InsetERT::showInsetDialog(BufferView * bv) const
609 bv->owner()->getDialogs()->showERT(const_cast<InsetERT *>(this));
614 void InsetERT::open(BufferView * bv)
622 void InsetERT::close(BufferView * bv) const
626 status(bv, Collapsed);
630 string const InsetERT::selectNextWordToSpellcheck(BufferView * bv,float &) const
632 bv->unlockInset(const_cast<InsetERT *>(this));