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 status(0, Collapsed);
151 void InsetERT::write(Buffer const * buf, ostream & os) const
167 os << getInsetName() << "\n"
168 << "status "<< st << "\n";
170 inset.writeParagraphData(buf, os);
174 string const InsetERT::editMessage() const
176 return _("Opened ERT Inset");
180 bool InsetERT::insertInset(BufferView *, Inset *)
186 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
188 // if selectall is activated then the fontchange was an outside general
189 // fontchange and this messages is not needed
191 WriteAlert(_("Impossible Operation!"),
192 _("Not permitted to change font-types inside ERT-insets!"),
197 void InsetERT::edit(BufferView * bv, int x, int y, unsigned int button)
199 InsetCollapsable::edit(bv, x, y, button);
204 Inset::EDITABLE InsetERT::editable() const
206 if (status_ == Collapsed)
208 return HIGHLY_EDITABLE;
212 void InsetERT::edit(BufferView * bv, bool front)
214 InsetCollapsable::edit(bv, front);
219 void InsetERT::insetButtonRelease(BufferView * bv, int x, int y, int button)
225 if ((x >= 0) && (x < button_length) &&
226 (y >= button_top_y) && (y <= button_bottom_y))
229 // setLabel(_("ERT"));
231 // setLabel(get_new_label());
235 // collapsed_ = false;
236 // inset.insetButtonRelease(bv, 0, 0, button);
237 // inset.setUpdateStatus(bv, InsetText::FULL);
238 // bv->updateInset(this, true);
240 status(bv, Collapsed);
241 // collapsed_ = true;
242 // bv->unlockInset(this);
243 // bv->updateInset(this, true);
245 } else if (!collapsed_ && (y > button_bottom_y)) {
246 LyXFont font(LyXFont::ALL_SANE);
247 int yy = ascent(bv, font) + y -
248 (ascent_collapsed() +
249 descent_collapsed() +
250 inset.ascent(bv, font));
251 inset.insetButtonRelease(bv, x, yy, button);
256 int InsetERT::latex(Buffer const *, std::ostream & os, bool /*fragile*/,
257 bool /*free_spc*/) const
259 Paragraph * par = inset.paragraph();
261 Paragraph::size_type siz = inset.paragraph()->size();
262 for (Paragraph::size_type i = 0; i != siz; ++i) {
263 char c = inset.paragraph()->getChar(i);
265 case Paragraph::META_NEWLINE:
280 int InsetERT::ascii(Buffer const *,
281 std::ostream &, int /*linelen*/) const
287 int InsetERT::linuxdoc(Buffer const *, std::ostream &) const
293 int InsetERT::docBook(Buffer const *, std::ostream &) const
299 UpdatableInset::RESULT
300 InsetERT::localDispatch(BufferView * bv, kb_action action, string const & arg)
302 UpdatableInset::RESULT result = DISPATCHED_NOUPDATE;
304 if (!inset.paragraph()->size()) {
310 bv->owner()->setLayout(inset.paragraph()->getLayout());
313 result = InsetCollapsable::localDispatch(bv, action, arg);
316 case LFUN_BREAKPARAGRAPH:
317 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
328 string const InsetERT::get_new_label() const
331 Paragraph::size_type const max_length = 15;
333 Paragraph::size_type const p_siz = inset.paragraph()->size();
334 Paragraph::size_type const n = std::min(max_length, p_siz);
337 for(; i < n && j < p_siz; ++j) {
338 if (inset.paragraph()->isInset(j))
340 la += inset.paragraph()->getChar(j);
343 if (i > 0 && j < p_siz) {
353 void InsetERT::setButtonLabel() const
355 if (status_ == Collapsed) {
356 setLabel(get_new_label());
363 bool InsetERT::checkInsertChar(LyXFont & font)
366 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
368 LyXFont f(LyXFont::ALL_INHERIT);
371 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
372 font.setColor(LColor::latex);
377 int InsetERT::ascent(BufferView * bv, LyXFont const & font) const
380 return InsetCollapsable::ascent(bv, font);
382 return inset.ascent(bv, font);
386 int InsetERT::descent(BufferView * bv, LyXFont const & font) const
389 return InsetCollapsable::descent(bv, font);
391 return inset.descent(bv, font);
395 int InsetERT::width(BufferView * bv, LyXFont const & font) const
398 return InsetCollapsable::width(bv, font);
400 return inset.width(bv, font);
404 void InsetERT::draw(BufferView * bv, LyXFont const & f,
405 int baseline, float & x, bool cleared) const
407 Painter & pain = bv->painter();
409 button_length = width_collapsed();
410 button_top_y = -ascent(bv, f);
411 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
415 draw_collapsed(pain, baseline, x);
416 x += TEXT_TO_INSET_OFFSET;
423 x += static_cast<float>(scroll());
425 if (!cleared && (inset.need_update == InsetText::FULL ||
426 inset.need_update == InsetText::INIT ||
428 top_baseline != baseline))
430 // we don't need anymore to clear here we just have to tell
431 // the underlying LyXText that it should do the RowClear!
432 inset.setUpdateStatus(bv, InsetText::FULL);
433 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
439 top_baseline = baseline;
441 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
444 inset.draw(bv, f, baseline, x, cleared);
446 draw_collapsed(pain, bl, old_x);
448 bl + descent_collapsed() + inset.ascent(bv, f),
455 void InsetERT::set_latex_font(BufferView * bv)
458 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
460 LyXFont font(LyXFont::ALL_INHERIT);
463 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
464 font.setColor(LColor::latex);
465 inset.getLyXText(bv)->setFont(bv, font, false);
469 void InsetERT::status(BufferView * bv, ERTStatus const st) const
475 inset.setAutoBreakRows(false);
478 inset.setAutoBreakRows(true);
484 inset.setAutoBreakRows(true);
489 bv->unlockInset(const_cast<InsetERT *>(this));
493 bv->updateInset(const_cast<InsetERT *>(this), true);
498 bool InsetERT::showInsetDialog(BufferView * bv) const
500 bv->owner()->getDialogs()->showERT(const_cast<InsetERT *>(this));
505 void InsetERT::open(BufferView * bv)
513 void InsetERT::close(BufferView * bv) const
517 status(bv, Collapsed);