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") {
100 if (lex.GetString() == "Inlined")
102 else if (lex.GetString() == "Collapsed")
104 else // leave this as default!
108 lyxerr << "InsetERT::Read: Missing 'status'-tag!"
110 // take countermeasures
111 lex.pushToken(token);
114 InsetCollapsable::read(buf, lex);
125 void InsetERT::write(Buffer const * buf, ostream & os) const
141 os << getInsetName() << "\n"
142 << "status "<< st << "\n";
143 InsetCollapsable::write(buf, os);
147 string const InsetERT::editMessage() const
149 return _("Opened ERT Inset");
153 bool InsetERT::insertInset(BufferView *, Inset *)
159 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
161 // if selectall is activated then the fontchange was an outside general
162 // fontchange and this messages is not needed
164 WriteAlert(_("Impossible Operation!"),
165 _("Not permitted to change font-types inside ERT-insets!"),
170 void InsetERT::edit(BufferView * bv, int x, int y, unsigned int button)
172 InsetCollapsable::edit(bv, x, y, button);
177 Inset::EDITABLE InsetERT::editable() const
179 if (status_ == Collapsed)
181 return HIGHLY_EDITABLE;
185 void InsetERT::edit(BufferView * bv, bool front)
187 InsetCollapsable::edit(bv, front);
192 void InsetERT::insetButtonRelease(BufferView * bv, int x, int y, int button)
198 if ((x >= 0) && (x < button_length) &&
199 (y >= button_top_y) && (y <= button_bottom_y))
202 // setLabel(_("ERT"));
204 // setLabel(get_new_label());
208 // collapsed_ = false;
209 // inset.insetButtonRelease(bv, 0, 0, button);
210 // inset.setUpdateStatus(bv, InsetText::FULL);
211 // bv->updateInset(this, true);
213 status(bv, Collapsed);
214 // collapsed_ = true;
215 // bv->unlockInset(this);
216 // bv->updateInset(this, true);
218 } else if (!collapsed_ && (y > button_bottom_y)) {
219 LyXFont font(LyXFont::ALL_SANE);
220 int yy = ascent(bv, font) + y -
221 (ascent_collapsed() +
222 descent_collapsed() +
223 inset.ascent(bv, font));
224 inset.insetButtonRelease(bv, x, yy, button);
229 int InsetERT::latex(Buffer const *, std::ostream & os, bool /*fragile*/,
230 bool /*free_spc*/) const
232 Paragraph * par = inset.paragraph();
234 Paragraph::size_type siz = inset.paragraph()->size();
235 for (Paragraph::size_type i = 0; i != siz; ++i) {
236 char c = inset.paragraph()->getChar(i);
238 case Paragraph::META_NEWLINE:
253 int InsetERT::ascii(Buffer const *,
254 std::ostream &, int /*linelen*/) const
260 int InsetERT::linuxdoc(Buffer const *, std::ostream &) const
266 int InsetERT::docBook(Buffer const *, std::ostream &) const
272 UpdatableInset::RESULT
273 InsetERT::localDispatch(BufferView * bv, kb_action action, string const & arg)
275 UpdatableInset::RESULT result = DISPATCHED_NOUPDATE;
277 if (!inset.paragraph()->size()) {
283 bv->owner()->setLayout(inset.paragraph()->getLayout());
286 result = InsetCollapsable::localDispatch(bv, action, arg);
289 case LFUN_BREAKPARAGRAPH:
290 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
301 string const InsetERT::get_new_label() const
304 Paragraph::size_type const max_length = 15;
306 Paragraph::size_type const p_siz = inset.paragraph()->size();
307 Paragraph::size_type const n = std::min(max_length, p_siz);
310 for(; i < n && j < p_siz; ++j) {
311 if (inset.paragraph()->isInset(j))
313 la += inset.paragraph()->getChar(j);
316 if (i > 0 && j < p_siz) {
326 void InsetERT::setButtonLabel()
328 if (status_ == Collapsed) {
329 setLabel(get_new_label());
336 bool InsetERT::checkInsertChar(LyXFont & font)
338 LyXFont f(LyXFont::ALL_INHERIT);
340 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
341 font.setColor(LColor::latex);
346 int InsetERT::ascent(BufferView * bv, LyXFont const & font) const
349 return InsetCollapsable::ascent(bv, font);
351 return inset.ascent(bv, font);
355 int InsetERT::descent(BufferView * bv, LyXFont const & font) const
358 return InsetCollapsable::descent(bv, font);
360 return inset.descent(bv, font);
364 int InsetERT::width(BufferView * bv, LyXFont const & font) const
367 return InsetCollapsable::width(bv, font);
369 return inset.width(bv, font);
373 void InsetERT::draw(BufferView * bv, LyXFont const & f,
374 int baseline, float & x, bool cleared) const
376 Painter & pain = bv->painter();
378 button_length = width_collapsed();
379 button_top_y = -ascent(bv, f);
380 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
384 draw_collapsed(pain, baseline, x);
385 x += TEXT_TO_INSET_OFFSET;
392 x += static_cast<float>(scroll());
394 if (!cleared && (inset.need_update == InsetText::FULL ||
395 inset.need_update == InsetText::INIT ||
397 top_baseline != baseline))
399 // we don't need anymore to clear here we just have to tell
400 // the underlying LyXText that it should do the RowClear!
401 inset.setUpdateStatus(bv, InsetText::FULL);
402 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
407 top_baseline = baseline;
409 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
412 inset.draw(bv, f, baseline, x, cleared);
414 draw_collapsed(pain, bl, old_x);
416 bl + descent_collapsed() + inset.ascent(bv, f),
423 void InsetERT::set_latex_font(BufferView * bv)
425 LyXFont font(LyXFont::ALL_INHERIT);
427 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
428 font.setColor(LColor::latex);
429 inset.getLyXText(bv)->setFont(bv, font, false);
433 void InsetERT::status(BufferView * bv, ERTStatus const st)
439 inset.setAutoBreakRows(false);
442 inset.setAutoBreakRows(true);
448 inset.setAutoBreakRows(true);
452 bv->unlockInset(this);
455 bv->updateInset(this, true);
460 bool InsetERT::showInsetDialog(BufferView * bv) const
462 bv->owner()->getDialogs()->showERT(const_cast<InsetERT *>(this));
467 void InsetERT::open(BufferView * bv)
475 void InsetERT::close(BufferView * bv)
479 status(bv, Collapsed);