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);
200 if (status_ != Inlined) {
202 status(0, Collapsed);
211 Inset::EDITABLE InsetERT::editable() const
213 if (status_ == Collapsed)
215 return HIGHLY_EDITABLE;
219 void InsetERT::edit(BufferView * bv, bool front)
221 InsetCollapsable::edit(bv, front);
222 if (status_ != Inlined) {
224 status(0, Collapsed);
233 void InsetERT::insetButtonRelease(BufferView * bv, int x, int y, int button)
239 if ((x >= 0) && (x < button_length) &&
240 (y >= button_top_y) && (y <= button_bottom_y))
243 // setLabel(_("ERT"));
245 // setLabel(get_new_label());
249 // collapsed_ = false;
250 // inset.insetButtonRelease(bv, 0, 0, button);
251 // inset.setUpdateStatus(bv, InsetText::FULL);
252 // bv->updateInset(this, true);
254 status(bv, Collapsed);
255 // collapsed_ = true;
256 // bv->unlockInset(this);
257 // bv->updateInset(this, true);
259 } else if (!collapsed_ && (y > button_bottom_y)) {
260 LyXFont font(LyXFont::ALL_SANE);
261 int yy = ascent(bv, font) + y -
262 (ascent_collapsed() +
263 descent_collapsed() +
264 inset.ascent(bv, font));
265 inset.insetButtonRelease(bv, x, yy, button);
270 int InsetERT::latex(Buffer const *, std::ostream & os, bool /*fragile*/,
271 bool /*free_spc*/) const
273 Paragraph * par = inset.paragraph();
275 Paragraph::size_type siz = inset.paragraph()->size();
276 for (Paragraph::size_type i = 0; i != siz; ++i) {
277 char c = inset.paragraph()->getChar(i);
279 case Paragraph::META_NEWLINE:
294 int InsetERT::ascii(Buffer const *,
295 std::ostream &, int /*linelen*/) const
301 int InsetERT::linuxdoc(Buffer const *, std::ostream &) const
307 int InsetERT::docBook(Buffer const *, std::ostream &) const
313 UpdatableInset::RESULT
314 InsetERT::localDispatch(BufferView * bv, kb_action action, string const & arg)
316 UpdatableInset::RESULT result = DISPATCHED_NOUPDATE;
318 if (!inset.paragraph()->size()) {
324 bv->owner()->setLayout(inset.paragraph()->getLayout());
327 result = InsetCollapsable::localDispatch(bv, action, arg);
330 case LFUN_BREAKPARAGRAPH:
331 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
342 string const InsetERT::get_new_label() const
345 Paragraph::size_type const max_length = 15;
347 Paragraph::size_type const p_siz = inset.paragraph()->size();
348 Paragraph::size_type const n = std::min(max_length, p_siz);
351 for(; i < n && j < p_siz; ++j) {
352 if (inset.paragraph()->isInset(j))
354 la += inset.paragraph()->getChar(j);
357 if (i > 0 && j < p_siz) {
367 void InsetERT::setButtonLabel() const
369 if (status_ == Collapsed) {
370 setLabel(get_new_label());
377 bool InsetERT::checkInsertChar(LyXFont & font)
380 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
382 LyXFont f(LyXFont::ALL_INHERIT);
385 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
386 font.setColor(LColor::latex);
391 int InsetERT::ascent(BufferView * bv, LyXFont const & font) const
394 return InsetCollapsable::ascent(bv, font);
396 return inset.ascent(bv, font);
400 int InsetERT::descent(BufferView * bv, LyXFont const & font) const
403 return InsetCollapsable::descent(bv, font);
405 return inset.descent(bv, font);
409 int InsetERT::width(BufferView * bv, LyXFont const & font) const
412 return InsetCollapsable::width(bv, font);
414 return inset.width(bv, font);
418 void InsetERT::draw(BufferView * bv, LyXFont const & f,
419 int baseline, float & x, bool cleared) const
421 Painter & pain = bv->painter();
423 button_length = width_collapsed();
424 button_top_y = -ascent(bv, f);
425 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
429 draw_collapsed(pain, baseline, x);
430 x += TEXT_TO_INSET_OFFSET;
437 x += static_cast<float>(scroll());
439 if (!cleared && (inset.need_update == InsetText::FULL ||
440 inset.need_update == InsetText::INIT ||
442 top_baseline != baseline))
444 // we don't need anymore to clear here we just have to tell
445 // the underlying LyXText that it should do the RowClear!
446 inset.setUpdateStatus(bv, InsetText::FULL);
447 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
453 top_baseline = baseline;
455 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
458 inset.draw(bv, f, baseline, x, cleared);
460 draw_collapsed(pain, bl, old_x);
462 bl + descent_collapsed() + inset.ascent(bv, f),
469 void InsetERT::set_latex_font(BufferView * bv)
472 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
474 LyXFont font(LyXFont::ALL_INHERIT);
477 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
478 font.setColor(LColor::latex);
479 inset.getLyXText(bv)->setFont(bv, font, false);
483 void InsetERT::status(BufferView * bv, ERTStatus const st) const
489 inset.setAutoBreakRows(false);
492 inset.setAutoBreakRows(true);
498 inset.setAutoBreakRows(true);
503 bv->unlockInset(const_cast<InsetERT *>(this));
507 bv->updateInset(const_cast<InsetERT *>(this), true);
512 bool InsetERT::showInsetDialog(BufferView * bv) const
514 bv->owner()->getDialogs()->showERT(const_cast<InsetERT *>(this));
519 void InsetERT::open(BufferView * bv)
527 void InsetERT::close(BufferView * bv) const
531 status(bv, Collapsed);