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);
129 #warning this should be really short lived only for compatibility to
130 #warning files written 07/08/2001 so this has to go before 1.2.0! (Jug)
133 string const token = lex.getString();
134 if (token == "collapsed") {
136 collapsed_ = lex.getBool();
138 // Take countermeasures
139 lex.pushToken(token);
142 inset.read(buf, lex);
145 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
147 LyXFont font(LyXFont::ALL_INHERIT);
150 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
151 font.setColor(LColor::latex);
152 Paragraph * par = inset.paragraph();
154 pos_type siz = par->size();
155 for (pos_type i = 0; i < siz; ++i) {
156 par->setFont(i, font);
163 status(0, Collapsed);
172 void InsetERT::write(Buffer const * buf, ostream & os) const
188 os << getInsetName() << "\n"
189 << "status "<< st << "\n";
191 //inset.writeParagraphData(buf, os);
192 string const layout =
193 textclasslist.NameOfLayout(buf->params.textclass, 0);
194 Paragraph * par = inset.paragraph();
196 os << "\n\\layout " << layout << "\n";
197 pos_type siz = par->size();
198 for (pos_type i = 0; i < siz; ++i) {
199 Paragraph::value_type c = par->getChar(i);
201 case Paragraph::META_INSET:
202 case Paragraph::META_HFILL:
203 lyxerr << "Element is not allowed in insertERT"
205 case Paragraph::META_NEWLINE:
206 os << "\n\\newline \n";
209 os << "\n\\backslash \n";
221 string const InsetERT::editMessage() const
223 return _("Opened ERT Inset");
227 bool InsetERT::insertInset(BufferView *, Inset *)
233 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
235 // if selectall is activated then the fontchange was an outside general
236 // fontchange and this messages is not needed
238 Alert::alert(_("Impossible Operation!"),
239 _("Not permitted to change font-types inside ERT-insets!"),
244 void InsetERT::updateStatus(BufferView * bv, bool swap) const
246 if (status_ != Inlined) {
248 status(bv, swap ? Open : Collapsed);
250 status(bv, swap ? Collapsed : Open);
256 void InsetERT::edit(BufferView * bv, int x, int y, unsigned int button)
261 InsetCollapsable::edit(bv, x, y, button);
267 Inset::EDITABLE InsetERT::editable() const
269 if (status_ == Collapsed)
271 return HIGHLY_EDITABLE;
275 void InsetERT::edit(BufferView * bv, bool front)
277 InsetCollapsable::edit(bv, front);
283 void InsetERT::insetButtonRelease(BufferView * bv, int x, int y, int button)
290 if (status_ != Inlined && (x >= 0) && (x < button_length) &&
291 (y >= button_top_y) && (y <= button_bottom_y)) {
292 updateStatus(bv, true);
294 LyXFont font(LyXFont::ALL_SANE);
295 int yy = ascent(bv, font) + y - inset.ascent(bv, font);
297 // inlined is special - the text appears above
299 if (status_ == Inlined) {
300 inset.insetButtonRelease(bv, x, yy, button);
301 } else if (!collapsed_ && (y > button_bottom_y)) {
302 yy -= (ascent_collapsed() + descent_collapsed());
303 inset.insetButtonRelease(bv, x, yy, button);
309 int InsetERT::latex(Buffer const *, std::ostream & os, bool /*fragile*/,
310 bool /*free_spc*/) const
312 Paragraph * par = inset.paragraph();
315 pos_type siz = par->size();
316 for (pos_type i = 0; i < siz; ++i) {
317 Paragraph::value_type c = par->getChar(i);
319 case Paragraph::META_NEWLINE:
339 int InsetERT::ascii(Buffer const *,
340 std::ostream &, int /*linelen*/) const
346 int InsetERT::linuxdoc(Buffer const *, std::ostream & os) const
348 Paragraph * par = inset.paragraph();
351 pos_type siz = par->size();
352 for (pos_type i = 0; i < siz; ++i) {
353 Paragraph::value_type c = par->getChar(i);
355 case Paragraph::META_NEWLINE:
375 int InsetERT::docbook(Buffer const *, std::ostream & os) const
377 Paragraph * par = inset.paragraph();
380 pos_type siz = par->size();
381 for (pos_type i = 0; i < siz; ++i) {
382 Paragraph::value_type c = par->getChar(i);
384 case Paragraph::META_NEWLINE:
404 UpdatableInset::RESULT
405 InsetERT::localDispatch(BufferView * bv, kb_action action, string const & arg)
407 UpdatableInset::RESULT result = DISPATCHED_NOUPDATE;
409 if (!inset.paragraph()->size()) {
415 bv->owner()->setLayout(inset.paragraph()->getLayout());
418 result = InsetCollapsable::localDispatch(bv, action, arg);
421 case LFUN_BREAKPARAGRAPH:
422 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
424 case LFUN_BACKSPACE_SKIP:
426 case LFUN_DELETE_SKIP:
427 case LFUN_DELETE_LINE_FORWARD:
439 string const InsetERT::get_new_label() const
442 pos_type const max_length = 15;
443 pos_type const p_siz = inset.paragraph()->size();
444 pos_type const n = std::min(max_length, p_siz);
447 for(; i < n && j < p_siz; ++j) {
448 if (inset.paragraph()->isInset(j))
450 la += inset.paragraph()->getChar(j);
453 if (i > 0 && j < p_siz) {
463 void InsetERT::setButtonLabel() const
465 if (status_ == Collapsed) {
466 setLabel(get_new_label());
473 bool InsetERT::checkInsertChar(LyXFont & font)
476 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
478 LyXFont f(LyXFont::ALL_INHERIT);
481 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
482 font.setColor(LColor::latex);
487 int InsetERT::ascent(BufferView * bv, LyXFont const & font) const
490 return InsetCollapsable::ascent(bv, font);
492 return inset.ascent(bv, font);
496 int InsetERT::descent(BufferView * bv, LyXFont const & font) const
499 return InsetCollapsable::descent(bv, font);
501 return inset.descent(bv, font);
505 int InsetERT::width(BufferView * bv, LyXFont const & font) const
508 return InsetCollapsable::width(bv, font);
510 return inset.width(bv, font);
514 void InsetERT::draw(BufferView * bv, LyXFont const & f,
515 int baseline, float & x, bool cleared) const
517 Painter & pain = bv->painter();
519 button_length = width_collapsed();
520 button_top_y = -ascent(bv, f);
521 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
525 draw_collapsed(pain, baseline, x);
526 x += TEXT_TO_INSET_OFFSET;
533 x += static_cast<float>(scroll());
535 if (!cleared && (inset.need_update == InsetText::FULL ||
536 inset.need_update == InsetText::INIT ||
538 top_baseline != baseline))
540 // we don't need anymore to clear here we just have to tell
541 // the underlying LyXText that it should do the RowClear!
542 inset.setUpdateStatus(bv, InsetText::FULL);
543 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
549 top_baseline = baseline;
551 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
554 inset.draw(bv, f, baseline, x, cleared);
556 draw_collapsed(pain, bl, old_x);
558 bl + descent_collapsed() + inset.ascent(bv, f),
565 void InsetERT::set_latex_font(BufferView * bv)
568 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
570 LyXFont font(LyXFont::ALL_INHERIT);
573 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
574 font.setColor(LColor::latex);
575 inset.getLyXText(bv)->setFont(bv, font, false);
579 void InsetERT::status(BufferView * bv, ERTStatus const st) const
585 inset.setAutoBreakRows(false);
588 inset.setAutoBreakRows(true);
594 inset.setAutoBreakRows(true);
599 bv->unlockInset(const_cast<InsetERT *>(this));
603 bv->updateInset(const_cast<InsetERT *>(this), false);
608 bool InsetERT::showInsetDialog(BufferView * bv) const
610 bv->owner()->getDialogs()->showERT(const_cast<InsetERT *>(this));
615 void InsetERT::open(BufferView * bv)
623 void InsetERT::close(BufferView * bv) const
627 status(bv, Collapsed);
631 string const InsetERT::selectNextWordToSpellcheck(BufferView * bv,float &) const
633 bv->unlockInset(const_cast<InsetERT *>(this));