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)
71 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
73 LyXFont font(LyXFont::ALL_INHERIT);
75 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
76 font.setColor(LColor::latex);
77 string::const_iterator cit = contents.begin();
78 string::const_iterator end = contents.end();
79 Paragraph::size_type pos = 0;
80 for (; cit != end; ++cit) {
81 inset.paragraph()->insertChar(pos++, *cit, font);
83 // the init has to be after the initialization of the paragraph
84 // because of the label settings (draw_label for ert insets).
95 void InsetERT::read(Buffer const * buf, LyXLex & lex)
97 bool token_found = false;
100 string const token = lex.getString();
101 if (token == "status") {
103 string const tmp_token = lex.getString();
105 if (tmp_token == "Inlined") {
107 } else if (tmp_token == "Collapsed") {
108 status(0, Collapsed);
110 // leave this as default!
116 lyxerr << "InsetERT::Read: Missing 'status'-tag!"
118 // take countermeasures
119 lex.pushToken(token);
122 #warning this should be really short lived only for compatibility to
123 #warning files written 07/08/2001 so this has to go before 1.2.0! (Jug)
126 string const token = lex.getString();
127 if (token == "collapsed") {
129 collapsed_ = lex.getBool();
131 // Take countermeasures
132 lex.pushToken(token);
135 inset.read(buf, lex);
138 status(0, Collapsed);
147 void InsetERT::write(Buffer const * buf, ostream & os) const
163 os << getInsetName() << "\n"
164 << "status "<< st << "\n";
166 inset.writeParagraphData(buf, os);
170 string const InsetERT::editMessage() const
172 return _("Opened ERT Inset");
176 bool InsetERT::insertInset(BufferView *, Inset *)
182 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
184 // if selectall is activated then the fontchange was an outside general
185 // fontchange and this messages is not needed
187 WriteAlert(_("Impossible Operation!"),
188 _("Not permitted to change font-types inside ERT-insets!"),
193 void InsetERT::edit(BufferView * bv, int x, int y, unsigned int button)
195 InsetCollapsable::edit(bv, x, y, button);
200 Inset::EDITABLE InsetERT::editable() const
202 if (status_ == Collapsed)
204 return HIGHLY_EDITABLE;
208 void InsetERT::edit(BufferView * bv, bool front)
210 InsetCollapsable::edit(bv, front);
215 void InsetERT::insetButtonRelease(BufferView * bv, int x, int y, int button)
221 if ((x >= 0) && (x < button_length) &&
222 (y >= button_top_y) && (y <= button_bottom_y))
225 // setLabel(_("ERT"));
227 // setLabel(get_new_label());
231 // collapsed_ = false;
232 // inset.insetButtonRelease(bv, 0, 0, button);
233 // inset.setUpdateStatus(bv, InsetText::FULL);
234 // bv->updateInset(this, true);
236 status(bv, Collapsed);
237 // collapsed_ = true;
238 // bv->unlockInset(this);
239 // bv->updateInset(this, true);
241 } else if (!collapsed_ && (y > button_bottom_y)) {
242 LyXFont font(LyXFont::ALL_SANE);
243 int yy = ascent(bv, font) + y -
244 (ascent_collapsed() +
245 descent_collapsed() +
246 inset.ascent(bv, font));
247 inset.insetButtonRelease(bv, x, yy, button);
252 int InsetERT::latex(Buffer const *, std::ostream & os, bool /*fragile*/,
253 bool /*free_spc*/) const
255 Paragraph * par = inset.paragraph();
257 Paragraph::size_type siz = inset.paragraph()->size();
258 for (Paragraph::size_type i = 0; i != siz; ++i) {
259 char c = inset.paragraph()->getChar(i);
261 case Paragraph::META_NEWLINE:
276 int InsetERT::ascii(Buffer const *,
277 std::ostream &, int /*linelen*/) const
283 int InsetERT::linuxdoc(Buffer const *, std::ostream &) const
289 int InsetERT::docBook(Buffer const *, std::ostream &) const
295 UpdatableInset::RESULT
296 InsetERT::localDispatch(BufferView * bv, kb_action action, string const & arg)
298 UpdatableInset::RESULT result = DISPATCHED_NOUPDATE;
300 if (!inset.paragraph()->size()) {
306 bv->owner()->setLayout(inset.paragraph()->getLayout());
309 result = InsetCollapsable::localDispatch(bv, action, arg);
312 case LFUN_BREAKPARAGRAPH:
313 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
324 string const InsetERT::get_new_label() const
327 Paragraph::size_type const max_length = 15;
329 Paragraph::size_type const p_siz = inset.paragraph()->size();
330 Paragraph::size_type const n = std::min(max_length, p_siz);
333 for(; i < n && j < p_siz; ++j) {
334 if (inset.paragraph()->isInset(j))
336 la += inset.paragraph()->getChar(j);
339 if (i > 0 && j < p_siz) {
349 void InsetERT::setButtonLabel() const
351 if (status_ == Collapsed) {
352 setLabel(get_new_label());
359 bool InsetERT::checkInsertChar(LyXFont & font)
362 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
364 LyXFont f(LyXFont::ALL_INHERIT);
367 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
368 font.setColor(LColor::latex);
373 int InsetERT::ascent(BufferView * bv, LyXFont const & font) const
376 return InsetCollapsable::ascent(bv, font);
378 return inset.ascent(bv, font);
382 int InsetERT::descent(BufferView * bv, LyXFont const & font) const
385 return InsetCollapsable::descent(bv, font);
387 return inset.descent(bv, font);
391 int InsetERT::width(BufferView * bv, LyXFont const & font) const
394 return InsetCollapsable::width(bv, font);
396 return inset.width(bv, font);
400 void InsetERT::draw(BufferView * bv, LyXFont const & f,
401 int baseline, float & x, bool cleared) const
403 Painter & pain = bv->painter();
405 button_length = width_collapsed();
406 button_top_y = -ascent(bv, f);
407 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
411 draw_collapsed(pain, baseline, x);
412 x += TEXT_TO_INSET_OFFSET;
419 x += static_cast<float>(scroll());
421 if (!cleared && (inset.need_update == InsetText::FULL ||
422 inset.need_update == InsetText::INIT ||
424 top_baseline != baseline))
426 // we don't need anymore to clear here we just have to tell
427 // the underlying LyXText that it should do the RowClear!
428 inset.setUpdateStatus(bv, InsetText::FULL);
429 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
435 top_baseline = baseline;
437 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
440 inset.draw(bv, f, baseline, x, cleared);
442 draw_collapsed(pain, bl, old_x);
444 bl + descent_collapsed() + inset.ascent(bv, f),
451 void InsetERT::set_latex_font(BufferView * bv)
454 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
456 LyXFont font(LyXFont::ALL_INHERIT);
459 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
460 font.setColor(LColor::latex);
461 inset.getLyXText(bv)->setFont(bv, font, false);
465 void InsetERT::status(BufferView * bv, ERTStatus const st) const
471 inset.setAutoBreakRows(false);
474 inset.setAutoBreakRows(true);
480 inset.setAutoBreakRows(true);
485 bv->unlockInset(const_cast<InsetERT *>(this));
489 bv->updateInset(const_cast<InsetERT *>(this), true);
494 bool InsetERT::showInsetDialog(BufferView * bv) const
496 bv->owner()->getDialogs()->showERT(const_cast<InsetERT *>(this));
501 void InsetERT::open(BufferView * bv)
509 void InsetERT::close(BufferView * bv) const
513 status(bv, Collapsed);