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)
70 LyXFont font(LyXFont::ALL_INHERIT);
71 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
72 font.setColor(LColor::latex);
73 string::const_iterator cit = contents.begin();
74 string::const_iterator end = contents.end();
75 Paragraph::size_type pos = 0;
76 for (; cit != end; ++cit) {
77 inset.paragraph()->insertChar(pos++, *cit, font);
79 // the init has to be after the initialization of the paragraph
80 // because of the label settings (draw_label for ert insets).
91 void InsetERT::read(Buffer const * buf, LyXLex & lex)
93 bool token_found = false;
96 string const token = lex.getString();
97 if (token == "status") {
99 string const tmp_token = lex.getString();
101 if (tmp_token == "Inlined") {
103 } else if (tmp_token == "Collapsed") {
106 // leave this as default!
112 lyxerr << "InsetERT::Read: Missing 'status'-tag!"
114 // take countermeasures
115 lex.pushToken(token);
118 InsetCollapsable::read(buf, lex);
130 void InsetERT::write(Buffer const * buf, ostream & os) const
146 os << getInsetName() << "\n"
147 << "status "<< st << "\n";
148 InsetCollapsable::write(buf, os);
152 string const InsetERT::editMessage() const
154 return _("Opened ERT Inset");
158 bool InsetERT::insertInset(BufferView *, Inset *)
164 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
166 // if selectall is activated then the fontchange was an outside general
167 // fontchange and this messages is not needed
169 WriteAlert(_("Impossible Operation!"),
170 _("Not permitted to change font-types inside ERT-insets!"),
175 void InsetERT::edit(BufferView * bv, int x, int y, unsigned int button)
177 InsetCollapsable::edit(bv, x, y, button);
182 Inset::EDITABLE InsetERT::editable() const
184 if (status_ == Collapsed)
186 return HIGHLY_EDITABLE;
190 void InsetERT::edit(BufferView * bv, bool front)
192 InsetCollapsable::edit(bv, front);
197 void InsetERT::insetButtonRelease(BufferView * bv, int x, int y, int button)
203 if ((x >= 0) && (x < button_length) &&
204 (y >= button_top_y) && (y <= button_bottom_y))
207 // setLabel(_("ERT"));
209 // setLabel(get_new_label());
213 // collapsed_ = false;
214 // inset.insetButtonRelease(bv, 0, 0, button);
215 // inset.setUpdateStatus(bv, InsetText::FULL);
216 // bv->updateInset(this, true);
218 status(bv, Collapsed);
219 // collapsed_ = true;
220 // bv->unlockInset(this);
221 // bv->updateInset(this, true);
223 } else if (!collapsed_ && (y > button_bottom_y)) {
224 LyXFont font(LyXFont::ALL_SANE);
225 int yy = ascent(bv, font) + y -
226 (ascent_collapsed() +
227 descent_collapsed() +
228 inset.ascent(bv, font));
229 inset.insetButtonRelease(bv, x, yy, button);
234 int InsetERT::latex(Buffer const *, std::ostream & os, bool /*fragile*/,
235 bool /*free_spc*/) const
237 Paragraph * par = inset.paragraph();
239 Paragraph::size_type siz = inset.paragraph()->size();
240 for (Paragraph::size_type i = 0; i != siz; ++i) {
241 char c = inset.paragraph()->getChar(i);
243 case Paragraph::META_NEWLINE:
258 int InsetERT::ascii(Buffer const *,
259 std::ostream &, int /*linelen*/) const
265 int InsetERT::linuxdoc(Buffer const *, std::ostream &) const
271 int InsetERT::docBook(Buffer const *, std::ostream &) const
277 UpdatableInset::RESULT
278 InsetERT::localDispatch(BufferView * bv, kb_action action, string const & arg)
280 UpdatableInset::RESULT result = DISPATCHED_NOUPDATE;
282 if (!inset.paragraph()->size()) {
288 bv->owner()->setLayout(inset.paragraph()->getLayout());
291 result = InsetCollapsable::localDispatch(bv, action, arg);
294 case LFUN_BREAKPARAGRAPH:
295 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
306 string const InsetERT::get_new_label() const
309 Paragraph::size_type const max_length = 15;
311 Paragraph::size_type const p_siz = inset.paragraph()->size();
312 Paragraph::size_type const n = std::min(max_length, p_siz);
315 for(; i < n && j < p_siz; ++j) {
316 if (inset.paragraph()->isInset(j))
318 la += inset.paragraph()->getChar(j);
321 if (i > 0 && j < p_siz) {
331 void InsetERT::setButtonLabel()
333 if (status_ == Collapsed) {
334 setLabel(get_new_label());
341 bool InsetERT::checkInsertChar(LyXFont & font)
343 LyXFont f(LyXFont::ALL_INHERIT);
345 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
346 font.setColor(LColor::latex);
351 int InsetERT::ascent(BufferView * bv, LyXFont const & font) const
354 return InsetCollapsable::ascent(bv, font);
356 return inset.ascent(bv, font);
360 int InsetERT::descent(BufferView * bv, LyXFont const & font) const
363 return InsetCollapsable::descent(bv, font);
365 return inset.descent(bv, font);
369 int InsetERT::width(BufferView * bv, LyXFont const & font) const
372 return InsetCollapsable::width(bv, font);
374 return inset.width(bv, font);
378 void InsetERT::draw(BufferView * bv, LyXFont const & f,
379 int baseline, float & x, bool cleared) const
381 Painter & pain = bv->painter();
383 button_length = width_collapsed();
384 button_top_y = -ascent(bv, f);
385 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
389 draw_collapsed(pain, baseline, x);
390 x += TEXT_TO_INSET_OFFSET;
397 x += static_cast<float>(scroll());
399 if (!cleared && (inset.need_update == InsetText::FULL ||
400 inset.need_update == InsetText::INIT ||
402 top_baseline != baseline))
404 // we don't need anymore to clear here we just have to tell
405 // the underlying LyXText that it should do the RowClear!
406 inset.setUpdateStatus(bv, InsetText::FULL);
407 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
412 top_baseline = baseline;
414 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
417 inset.draw(bv, f, baseline, x, cleared);
419 draw_collapsed(pain, bl, old_x);
421 bl + descent_collapsed() + inset.ascent(bv, f),
428 void InsetERT::set_latex_font(BufferView * bv)
430 LyXFont font(LyXFont::ALL_INHERIT);
432 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
433 font.setColor(LColor::latex);
434 inset.getLyXText(bv)->setFont(bv, font, false);
438 void InsetERT::status(BufferView * bv, ERTStatus const st)
444 inset.setAutoBreakRows(false);
447 inset.setAutoBreakRows(true);
453 inset.setAutoBreakRows(true);
457 bv->unlockInset(this);
460 bv->updateInset(this, true);
465 bool InsetERT::showInsetDialog(BufferView * bv) const
467 bv->owner()->getDialogs()->showERT(const_cast<InsetERT *>(this));
472 void InsetERT::open(BufferView * bv)
480 void InsetERT::close(BufferView * bv)
484 status(bv, Collapsed);