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"
32 labelfont = LyXFont(LyXFont::ALL_SANE);
35 labelfont.setColor(LColor::latex);
40 InsetERT::InsetERT() : InsetCollapsable()
46 InsetERT::InsetERT(InsetERT const & in, bool same_id)
47 : InsetCollapsable(in, same_id)
53 Inset * InsetERT::clone(Buffer const &, bool same_id) const
55 return new InsetERT(*const_cast<InsetERT *>(this), same_id);
59 InsetERT::InsetERT(string const & contents, bool collapsed)
60 : InsetCollapsable(collapsed)
62 LyXFont font(LyXFont::ALL_INHERIT);
63 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
64 font.setColor(LColor::latex);
65 string::const_iterator cit = contents.begin();
66 string::const_iterator end = contents.end();
67 Paragraph::size_type pos = 0;
68 for (; cit != end; ++cit) {
69 inset.paragraph()->insertChar(pos++, *cit, font);
71 // the init has to be after the initialization of the paragraph
72 // because of the label settings (draw_label for ert insets).
77 void InsetERT::read(Buffer const * buf, LyXLex & lex)
79 InsetCollapsable::read(buf, lex);
85 void InsetERT::write(Buffer const * buf, ostream & os) const
87 os << getInsetName() << "\n";
88 InsetCollapsable::write(buf, os);
92 string const InsetERT::editMessage() const
94 return _("Opened ERT Inset");
98 bool InsetERT::insertInset(BufferView *, Inset *)
104 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
106 // if selectall is activated then the fontchange was an outside general
107 // fontchange and this messages is not needed
109 WriteAlert(_("Impossible Operation!"),
110 _("Not permitted to change font-types inside ERT-insets!"),
115 void InsetERT::edit(BufferView * bv, int x, int y, unsigned int button)
117 InsetCollapsable::edit(bv, x, y, button);
122 void InsetERT::edit(BufferView * bv, bool front)
124 InsetCollapsable::edit(bv, front);
129 void InsetERT::insetButtonRelease(BufferView * bv,
130 int x, int y, int button)
132 if ((x >= 0) && (x < button_length) &&
133 (y >= button_top_y) && (y <= button_bottom_y))
136 inlined(bv, !inlined());
142 setLabel(get_new_label());
146 inset.insetButtonRelease(bv, 0, 0, button);
147 inset.setUpdateStatus(bv, InsetText::FULL);
148 bv->updateInset(this, true);
151 bv->unlockInset(this);
152 bv->updateInset(this, true);
154 } else if (!collapsed_ && (y > button_bottom_y)) {
155 LyXFont font(LyXFont::ALL_SANE);
156 int yy = ascent(bv, font) + y -
157 (ascent_collapsed() +
158 descent_collapsed() +
159 inset.ascent(bv, font));
160 inset.insetButtonRelease(bv, x, yy, button);
165 int InsetERT::latex(Buffer const *, std::ostream & os, bool /*fragile*/,
166 bool /*free_spc*/) const
168 Paragraph * par = inset.paragraph();
170 Paragraph::size_type siz = inset.paragraph()->size();
171 for (Paragraph::size_type i = 0; i != siz; ++i) {
172 char c = inset.paragraph()->getChar(i);
174 case Paragraph::META_NEWLINE:
189 int InsetERT::ascii(Buffer const *,
190 std::ostream &, int /*linelen*/) const
196 int InsetERT::linuxdoc(Buffer const *, std::ostream &) const
202 int InsetERT::docBook(Buffer const *, std::ostream &) const
208 UpdatableInset::RESULT
209 InsetERT::localDispatch(BufferView * bv, kb_action action, string const & arg)
211 UpdatableInset::RESULT result = DISPATCHED_NOUPDATE;
213 if (!inset.paragraph()->size()) {
219 bv->owner()->setLayout(inset.paragraph()->getLayout());
222 result = InsetCollapsable::localDispatch(bv, action, arg);
225 case LFUN_BREAKPARAGRAPH:
226 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
237 string const InsetERT::get_new_label() const
240 Paragraph::size_type const max_length = 15;
242 Paragraph::size_type const p_siz = inset.paragraph()->size();
243 Paragraph::size_type const n = std::min(max_length, p_siz);
246 for(; i < n && j < p_siz; ++j) {
247 if (inset.paragraph()->isInset(j))
249 la += inset.paragraph()->getChar(j);
252 if (i > 0 && j < p_siz) {
262 void InsetERT::setButtonLabel()
265 setLabel(get_new_label());
272 bool InsetERT::checkInsertChar(LyXFont & font)
274 LyXFont f(LyXFont::ALL_INHERIT);
276 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
277 font.setColor(LColor::latex);
282 void InsetERT::inlined(BufferView * bv, bool flag)
284 if (flag != inset.getAutoBreakRows())
287 inset.setAutoBreakRows(!flag);
288 bv->updateInset(this, true);
291 void InsetERT::draw(BufferView * bv, LyXFont const & f,
292 int baseline, float & x, bool cleared) const
294 Painter & pain = bv->painter();
296 button_length = width_collapsed();
297 button_top_y = -ascent(bv, f);
298 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
302 draw_collapsed(pain, baseline, x);
303 x += TEXT_TO_INSET_OFFSET;
310 x += static_cast<float>(scroll());
312 if (!cleared && (inset.need_update == InsetText::FULL ||
313 inset.need_update == InsetText::INIT ||
315 top_baseline != baseline))
317 // we don't need anymore to clear here we just have to tell
318 // the underlying LyXText that it should do the RowClear!
319 inset.setUpdateStatus(bv, InsetText::FULL);
320 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
325 top_baseline = baseline;
327 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
330 inset.draw(bv, f, baseline, x, cleared);
332 draw_collapsed(pain, bl, old_x);
334 bl + descent_collapsed() + inset.ascent(bv, f),
341 void InsetERT::set_latex_font(BufferView * bv)
343 LyXFont font(LyXFont::ALL_INHERIT);
345 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
346 font.setColor(LColor::latex);
347 inset.setFont(bv, font);