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") {
104 status(0, Collapsed);
106 // leave this as default!
112 lyxerr << "InsetERT::Read: Missing 'status'-tag!"
114 // take countermeasures
115 lex.pushToken(token);
118 #warning this should be really short lived only for compatibility to
119 #warning files written 07/08/2001 so this has to go before 1.2.0! (Jug)
122 string const token = lex.getString();
123 if (token == "collapsed") {
125 collapsed_ = lex.getBool();
127 // Take countermeasures
128 lex.pushToken(token);
131 inset.read(buf, lex);
134 status(0, Collapsed);
143 void InsetERT::write(Buffer const * buf, ostream & os) const
159 os << getInsetName() << "\n"
160 << "status "<< st << "\n";
162 inset.writeParagraphData(buf, os);
166 string const InsetERT::editMessage() const
168 return _("Opened ERT Inset");
172 bool InsetERT::insertInset(BufferView *, Inset *)
178 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
180 // if selectall is activated then the fontchange was an outside general
181 // fontchange and this messages is not needed
183 WriteAlert(_("Impossible Operation!"),
184 _("Not permitted to change font-types inside ERT-insets!"),
189 void InsetERT::edit(BufferView * bv, int x, int y, unsigned int button)
191 InsetCollapsable::edit(bv, x, y, button);
196 Inset::EDITABLE InsetERT::editable() const
198 if (status_ == Collapsed)
200 return HIGHLY_EDITABLE;
204 void InsetERT::edit(BufferView * bv, bool front)
206 InsetCollapsable::edit(bv, front);
211 void InsetERT::insetButtonRelease(BufferView * bv, int x, int y, int button)
217 if ((x >= 0) && (x < button_length) &&
218 (y >= button_top_y) && (y <= button_bottom_y))
221 // setLabel(_("ERT"));
223 // setLabel(get_new_label());
227 // collapsed_ = false;
228 // inset.insetButtonRelease(bv, 0, 0, button);
229 // inset.setUpdateStatus(bv, InsetText::FULL);
230 // bv->updateInset(this, true);
232 status(bv, Collapsed);
233 // collapsed_ = true;
234 // bv->unlockInset(this);
235 // bv->updateInset(this, true);
237 } else if (!collapsed_ && (y > button_bottom_y)) {
238 LyXFont font(LyXFont::ALL_SANE);
239 int yy = ascent(bv, font) + y -
240 (ascent_collapsed() +
241 descent_collapsed() +
242 inset.ascent(bv, font));
243 inset.insetButtonRelease(bv, x, yy, button);
248 int InsetERT::latex(Buffer const *, std::ostream & os, bool /*fragile*/,
249 bool /*free_spc*/) const
251 Paragraph * par = inset.paragraph();
253 Paragraph::size_type siz = inset.paragraph()->size();
254 for (Paragraph::size_type i = 0; i != siz; ++i) {
255 char c = inset.paragraph()->getChar(i);
257 case Paragraph::META_NEWLINE:
272 int InsetERT::ascii(Buffer const *,
273 std::ostream &, int /*linelen*/) const
279 int InsetERT::linuxdoc(Buffer const *, std::ostream &) const
285 int InsetERT::docBook(Buffer const *, std::ostream &) const
291 UpdatableInset::RESULT
292 InsetERT::localDispatch(BufferView * bv, kb_action action, string const & arg)
294 UpdatableInset::RESULT result = DISPATCHED_NOUPDATE;
296 if (!inset.paragraph()->size()) {
302 bv->owner()->setLayout(inset.paragraph()->getLayout());
305 result = InsetCollapsable::localDispatch(bv, action, arg);
308 case LFUN_BREAKPARAGRAPH:
309 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
320 string const InsetERT::get_new_label() const
323 Paragraph::size_type const max_length = 15;
325 Paragraph::size_type const p_siz = inset.paragraph()->size();
326 Paragraph::size_type const n = std::min(max_length, p_siz);
329 for(; i < n && j < p_siz; ++j) {
330 if (inset.paragraph()->isInset(j))
332 la += inset.paragraph()->getChar(j);
335 if (i > 0 && j < p_siz) {
345 void InsetERT::setButtonLabel()
347 if (status_ == Collapsed) {
348 setLabel(get_new_label());
355 bool InsetERT::checkInsertChar(LyXFont & font)
357 LyXFont f(LyXFont::ALL_INHERIT);
359 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
360 font.setColor(LColor::latex);
365 int InsetERT::ascent(BufferView * bv, LyXFont const & font) const
368 return InsetCollapsable::ascent(bv, font);
370 return inset.ascent(bv, font);
374 int InsetERT::descent(BufferView * bv, LyXFont const & font) const
377 return InsetCollapsable::descent(bv, font);
379 return inset.descent(bv, font);
383 int InsetERT::width(BufferView * bv, LyXFont const & font) const
386 return InsetCollapsable::width(bv, font);
388 return inset.width(bv, font);
392 void InsetERT::draw(BufferView * bv, LyXFont const & f,
393 int baseline, float & x, bool cleared) const
395 Painter & pain = bv->painter();
397 button_length = width_collapsed();
398 button_top_y = -ascent(bv, f);
399 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
403 draw_collapsed(pain, baseline, x);
404 x += TEXT_TO_INSET_OFFSET;
411 x += static_cast<float>(scroll());
413 if (!cleared && (inset.need_update == InsetText::FULL ||
414 inset.need_update == InsetText::INIT ||
416 top_baseline != baseline))
418 // we don't need anymore to clear here we just have to tell
419 // the underlying LyXText that it should do the RowClear!
420 inset.setUpdateStatus(bv, InsetText::FULL);
421 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
426 top_baseline = baseline;
428 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
431 inset.draw(bv, f, baseline, x, cleared);
433 draw_collapsed(pain, bl, old_x);
435 bl + descent_collapsed() + inset.ascent(bv, f),
442 void InsetERT::set_latex_font(BufferView * bv)
444 LyXFont font(LyXFont::ALL_INHERIT);
446 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
447 font.setColor(LColor::latex);
448 inset.getLyXText(bv)->setFont(bv, font, false);
452 void InsetERT::status(BufferView * bv, ERTStatus const st)
458 inset.setAutoBreakRows(false);
461 inset.setAutoBreakRows(true);
467 inset.setAutoBreakRows(true);
472 bv->unlockInset(this);
476 bv->updateInset(this, true);
481 bool InsetERT::showInsetDialog(BufferView * bv) const
483 bv->owner()->getDialogs()->showERT(const_cast<InsetERT *>(this));
488 void InsetERT::open(BufferView * bv)
496 void InsetERT::close(BufferView * bv)
500 status(bv, Collapsed);