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"
36 labelfont = LyXFont(LyXFont::ALL_SANE);
39 labelfont.setColor(LColor::latex);
45 InsetERT::InsetERT(bool collapsed)
46 : InsetCollapsable(collapsed)
56 InsetERT::InsetERT(InsetERT const & in, bool same_id)
57 : InsetCollapsable(in, same_id), status_(in.status_)
63 Inset * InsetERT::clone(Buffer const &, bool same_id) const
65 return new InsetERT(*const_cast<InsetERT *>(this), same_id);
69 InsetERT::InsetERT(string const & contents, bool collapsed)
70 : InsetCollapsable(collapsed)
77 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
79 LyXFont font(LyXFont::ALL_INHERIT);
81 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
82 font.setColor(LColor::latex);
83 string::const_iterator cit = contents.begin();
84 string::const_iterator end = contents.end();
85 Paragraph::pos_type pos = 0;
86 for (; cit != end; ++cit) {
87 inset.paragraph()->insertChar(pos++, *cit, font);
89 // the init has to be after the initialization of the paragraph
90 // because of the label settings (draw_label for ert insets).
101 void InsetERT::read(Buffer const * buf, LyXLex & lex)
103 bool token_found = false;
106 string const token = lex.getString();
107 if (token == "status") {
109 string const tmp_token = lex.getString();
111 if (tmp_token == "Inlined") {
113 } else if (tmp_token == "Collapsed") {
114 status(0, Collapsed);
116 // leave this as default!
122 lyxerr << "InsetERT::Read: Missing 'status'-tag!"
124 // take countermeasures
125 lex.pushToken(token);
128 #warning this should be really short lived only for compatibility to
129 #warning files written 07/08/2001 so this has to go before 1.2.0! (Jug)
132 string const token = lex.getString();
133 if (token == "collapsed") {
135 collapsed_ = lex.getBool();
137 // Take countermeasures
138 lex.pushToken(token);
141 inset.read(buf, lex);
144 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
146 LyXFont font(LyXFont::ALL_INHERIT);
149 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
150 font.setColor(LColor::latex);
151 Paragraph * par = inset.paragraph();
153 Paragraph::pos_type siz = par->size();
154 for (Paragraph::pos_type i = 0; i < siz; ++i) {
155 par->setFont(i, font);
162 status(0, Collapsed);
171 void InsetERT::write(Buffer const * buf, ostream & os) const
187 os << getInsetName() << "\n"
188 << "status "<< st << "\n";
190 //inset.writeParagraphData(buf, os);
191 string const layout =
192 textclasslist.NameOfLayout(buf->params.textclass, 0);
193 Paragraph * par = inset.paragraph();
195 os << "\n\\layout " << layout << "\n";
196 Paragraph::pos_type siz = par->size();
197 for (Paragraph::pos_type i = 0; i < siz; ++i) {
198 Paragraph::value_type c = par->getChar(i);
200 case Paragraph::META_INSET:
201 case Paragraph::META_HFILL:
202 lyxerr << "Element is not allowed in insertERT"
204 case Paragraph::META_NEWLINE:
205 os << "\n\\newline \n";
208 os << "\n\\backslash \n";
220 string const InsetERT::editMessage() const
222 return _("Opened ERT Inset");
226 bool InsetERT::insertInset(BufferView *, Inset *)
232 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
234 // if selectall is activated then the fontchange was an outside general
235 // fontchange and this messages is not needed
237 Alert::alert(_("Impossible Operation!"),
238 _("Not permitted to change font-types inside ERT-insets!"),
243 void InsetERT::updateStatus(BufferView * bv, bool swap) const
245 if (status_ != Inlined) {
247 status(bv, swap ? Open : Collapsed);
249 status(bv, swap ? Collapsed : Open);
255 void InsetERT::edit(BufferView * bv, int x, int y, unsigned int button)
260 InsetCollapsable::edit(bv, x, y, button);
266 Inset::EDITABLE InsetERT::editable() const
268 if (status_ == Collapsed)
270 return HIGHLY_EDITABLE;
274 void InsetERT::edit(BufferView * bv, bool front)
276 InsetCollapsable::edit(bv, front);
282 void InsetERT::insetButtonRelease(BufferView * bv, int x, int y, int button)
289 if (status_ != Inlined && (x >= 0) && (x < button_length) &&
290 (y >= button_top_y) && (y <= button_bottom_y)) {
291 updateStatus(bv, true);
293 LyXFont font(LyXFont::ALL_SANE);
294 int yy = ascent(bv, font) + y - inset.ascent(bv, font);
296 // inlined is special - the text appears above
298 if (status_ == Inlined) {
299 inset.insetButtonRelease(bv, x, yy, button);
300 } else if (!collapsed_ && (y > button_bottom_y)) {
301 yy -= (ascent_collapsed() + descent_collapsed());
302 inset.insetButtonRelease(bv, x, yy, button);
308 int InsetERT::latex(Buffer const *, std::ostream & os, bool /*fragile*/,
309 bool /*free_spc*/) const
311 Paragraph * par = inset.paragraph();
314 Paragraph::pos_type siz = par->size();
315 for (Paragraph::pos_type i = 0; i < siz; ++i) {
316 Paragraph::value_type c = par->getChar(i);
318 case Paragraph::META_NEWLINE:
338 int InsetERT::ascii(Buffer const *,
339 std::ostream &, int /*linelen*/) const
345 int InsetERT::linuxdoc(Buffer const *, std::ostream & os) const
347 Paragraph * par = inset.paragraph();
350 Paragraph::pos_type siz = par->size();
351 for (Paragraph::pos_type i = 0; i < siz; ++i) {
352 Paragraph::value_type c = par->getChar(i);
354 case Paragraph::META_NEWLINE:
374 int InsetERT::docbook(Buffer const *, std::ostream & os) const
376 Paragraph * par = inset.paragraph();
379 Paragraph::pos_type siz = par->size();
380 for (Paragraph::pos_type i = 0; i < siz; ++i) {
381 Paragraph::value_type c = par->getChar(i);
383 case Paragraph::META_NEWLINE:
403 UpdatableInset::RESULT
404 InsetERT::localDispatch(BufferView * bv, kb_action action, string const & arg)
406 UpdatableInset::RESULT result = DISPATCHED_NOUPDATE;
408 if (!inset.paragraph()->size()) {
414 bv->owner()->setLayout(inset.paragraph()->getLayout());
417 result = InsetCollapsable::localDispatch(bv, action, arg);
420 case LFUN_BREAKPARAGRAPH:
421 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
423 case LFUN_BACKSPACE_SKIP:
425 case LFUN_DELETE_SKIP:
426 case LFUN_DELETE_LINE_FORWARD:
438 string const InsetERT::get_new_label() const
441 Paragraph::pos_type const max_length = 15;
443 Paragraph::pos_type const p_siz = inset.paragraph()->size();
444 Paragraph::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));