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"
34 labelfont = LyXFont(LyXFont::ALL_SANE);
37 labelfont.setColor(LColor::latex);
43 InsetERT::InsetERT(bool collapsed)
44 : InsetCollapsable(collapsed)
54 InsetERT::InsetERT(InsetERT const & in, bool same_id)
55 : InsetCollapsable(in, same_id), status_(in.status_)
61 Inset * InsetERT::clone(Buffer const &, bool same_id) const
63 return new InsetERT(*const_cast<InsetERT *>(this), same_id);
67 InsetERT::InsetERT(string const & contents, bool collapsed)
68 : InsetCollapsable(collapsed)
75 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
77 LyXFont font(LyXFont::ALL_INHERIT);
79 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
80 font.setColor(LColor::latex);
81 string::const_iterator cit = contents.begin();
82 string::const_iterator end = contents.end();
83 Paragraph::size_type pos = 0;
84 for (; cit != end; ++cit) {
85 inset.paragraph()->insertChar(pos++, *cit, font);
87 // the init has to be after the initialization of the paragraph
88 // because of the label settings (draw_label for ert insets).
99 void InsetERT::read(Buffer const * buf, LyXLex & lex)
101 bool token_found = false;
104 string const token = lex.getString();
105 if (token == "status") {
107 string const tmp_token = lex.getString();
109 if (tmp_token == "Inlined") {
111 } else if (tmp_token == "Collapsed") {
112 status(0, Collapsed);
114 // leave this as default!
120 lyxerr << "InsetERT::Read: Missing 'status'-tag!"
122 // take countermeasures
123 lex.pushToken(token);
126 #warning this should be really short lived only for compatibility to
127 #warning files written 07/08/2001 so this has to go before 1.2.0! (Jug)
130 string const token = lex.getString();
131 if (token == "collapsed") {
133 collapsed_ = lex.getBool();
135 // Take countermeasures
136 lex.pushToken(token);
139 inset.read(buf, lex);
142 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
144 LyXFont font(LyXFont::ALL_INHERIT);
147 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
148 font.setColor(LColor::latex);
149 Paragraph * par = inset.paragraph();
151 Paragraph::size_type siz = par->size();
152 for (Paragraph::size_type i = 0; i < siz; ++i) {
153 par->setFont(i, font);
160 status(0, Collapsed);
169 void InsetERT::write(Buffer const * buf, ostream & os) const
185 os << getInsetName() << "\n"
186 << "status "<< st << "\n";
188 //inset.writeParagraphData(buf, os);
189 string const layout =
190 textclasslist.NameOfLayout(buf->params.textclass, 0);
191 Paragraph * par = inset.paragraph();
193 os << "\n\\layout " << layout << "\n";
194 Paragraph::size_type siz = par->size();
195 for (Paragraph::size_type i = 0; i < siz; ++i) {
196 Paragraph::value_type c = par->getChar(i);
198 case Paragraph::META_INSET:
199 case Paragraph::META_HFILL:
200 lyxerr << "Element is not allowed in insertERT"
202 case Paragraph::META_NEWLINE:
203 os << "\n\\newline \n";
206 os << "\n\\backslash \n";
218 string const InsetERT::editMessage() const
220 return _("Opened ERT Inset");
224 bool InsetERT::insertInset(BufferView *, Inset *)
230 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
232 // if selectall is activated then the fontchange was an outside general
233 // fontchange and this messages is not needed
235 WriteAlert(_("Impossible Operation!"),
236 _("Not permitted to change font-types inside ERT-insets!"),
241 void InsetERT::edit(BufferView * bv, int x, int y, unsigned int button)
243 InsetCollapsable::edit(bv, x, y, button);
244 if (status_ != Inlined) {
246 status(0, Collapsed);
255 Inset::EDITABLE InsetERT::editable() const
257 if (status_ == Collapsed)
259 return HIGHLY_EDITABLE;
263 void InsetERT::edit(BufferView * bv, bool front)
265 InsetCollapsable::edit(bv, front);
266 if (status_ != Inlined) {
268 status(0, Collapsed);
277 void InsetERT::insetButtonRelease(BufferView * bv, int x, int y, int button)
283 if ((x >= 0) && (x < button_length) &&
284 (y >= button_top_y) && (y <= button_bottom_y))
287 // setLabel(_("ERT"));
289 // setLabel(get_new_label());
293 // collapsed_ = false;
294 // inset.insetButtonRelease(bv, 0, 0, button);
295 // inset.setUpdateStatus(bv, InsetText::FULL);
296 // bv->updateInset(this, true);
298 status(bv, Collapsed);
299 // collapsed_ = true;
300 // bv->unlockInset(this);
301 // bv->updateInset(this, true);
303 } else if (!collapsed_ && (y > button_bottom_y)) {
304 LyXFont font(LyXFont::ALL_SANE);
305 int yy = ascent(bv, font) + y -
306 (ascent_collapsed() +
307 descent_collapsed() +
308 inset.ascent(bv, font));
309 inset.insetButtonRelease(bv, x, yy, button);
314 int InsetERT::latex(Buffer const *, std::ostream & os, bool /*fragile*/,
315 bool /*free_spc*/) const
317 Paragraph * par = inset.paragraph();
320 Paragraph::size_type siz = par->size();
321 for (Paragraph::size_type i = 0; i < siz; ++i) {
322 Paragraph::value_type c = par->getChar(i);
324 case Paragraph::META_NEWLINE:
344 int InsetERT::ascii(Buffer const *,
345 std::ostream &, int /*linelen*/) const
351 int InsetERT::linuxdoc(Buffer const *, std::ostream &) const
357 int InsetERT::docbook(Buffer const *, std::ostream &) const
363 UpdatableInset::RESULT
364 InsetERT::localDispatch(BufferView * bv, kb_action action, string const & arg)
366 UpdatableInset::RESULT result = DISPATCHED_NOUPDATE;
368 if (!inset.paragraph()->size()) {
374 bv->owner()->setLayout(inset.paragraph()->getLayout());
377 result = InsetCollapsable::localDispatch(bv, action, arg);
380 case LFUN_BREAKPARAGRAPH:
381 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
383 case LFUN_BACKSPACE_SKIP:
385 case LFUN_DELETE_SKIP:
386 case LFUN_DELETE_LINE_FORWARD:
398 string const InsetERT::get_new_label() const
401 Paragraph::size_type const max_length = 15;
403 Paragraph::size_type const p_siz = inset.paragraph()->size();
404 Paragraph::size_type const n = std::min(max_length, p_siz);
407 for(; i < n && j < p_siz; ++j) {
408 if (inset.paragraph()->isInset(j))
410 la += inset.paragraph()->getChar(j);
413 if (i > 0 && j < p_siz) {
423 void InsetERT::setButtonLabel() const
425 if (status_ == Collapsed) {
426 setLabel(get_new_label());
433 bool InsetERT::checkInsertChar(LyXFont & font)
436 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
438 LyXFont f(LyXFont::ALL_INHERIT);
441 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
442 font.setColor(LColor::latex);
447 int InsetERT::ascent(BufferView * bv, LyXFont const & font) const
450 return InsetCollapsable::ascent(bv, font);
452 return inset.ascent(bv, font);
456 int InsetERT::descent(BufferView * bv, LyXFont const & font) const
459 return InsetCollapsable::descent(bv, font);
461 return inset.descent(bv, font);
465 int InsetERT::width(BufferView * bv, LyXFont const & font) const
468 return InsetCollapsable::width(bv, font);
470 return inset.width(bv, font);
474 void InsetERT::draw(BufferView * bv, LyXFont const & f,
475 int baseline, float & x, bool cleared) const
477 Painter & pain = bv->painter();
479 button_length = width_collapsed();
480 button_top_y = -ascent(bv, f);
481 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
485 draw_collapsed(pain, baseline, x);
486 x += TEXT_TO_INSET_OFFSET;
493 x += static_cast<float>(scroll());
495 if (!cleared && (inset.need_update == InsetText::FULL ||
496 inset.need_update == InsetText::INIT ||
498 top_baseline != baseline))
500 // we don't need anymore to clear here we just have to tell
501 // the underlying LyXText that it should do the RowClear!
502 inset.setUpdateStatus(bv, InsetText::FULL);
503 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
509 top_baseline = baseline;
511 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
514 inset.draw(bv, f, baseline, x, cleared);
516 draw_collapsed(pain, bl, old_x);
518 bl + descent_collapsed() + inset.ascent(bv, f),
525 void InsetERT::set_latex_font(BufferView * bv)
528 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
530 LyXFont font(LyXFont::ALL_INHERIT);
533 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
534 font.setColor(LColor::latex);
535 inset.getLyXText(bv)->setFont(bv, font, false);
539 void InsetERT::status(BufferView * bv, ERTStatus const st) const
545 inset.setAutoBreakRows(false);
548 inset.setAutoBreakRows(true);
554 inset.setAutoBreakRows(true);
559 bv->unlockInset(const_cast<InsetERT *>(this));
563 bv->updateInset(const_cast<InsetERT *>(this), false);
568 bool InsetERT::showInsetDialog(BufferView * bv) const
570 bv->owner()->getDialogs()->showERT(const_cast<InsetERT *>(this));
575 void InsetERT::open(BufferView * bv)
583 void InsetERT::close(BufferView * bv) const
587 status(bv, Collapsed);
591 string const InsetERT::selectNextWordToSpellcheck(BufferView * bv,float &) const
593 bv->unlockInset(const_cast<InsetERT *>(this));