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)
75 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
77 LyXFont font(LyXFont::ALL_INHERIT);
79 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
80 font.setColor(LColor::latex);
81 string::const_iterator cit = contents.begin();
82 string::const_iterator end = contents.end();
83 Paragraph::size_type pos = 0;
84 for (; cit != end; ++cit) {
85 inset.paragraph()->insertChar(pos++, *cit, font);
87 // the init has to be after the initialization of the paragraph
88 // because of the label settings (draw_label for ert insets).
99 void InsetERT::read(Buffer const * buf, LyXLex & lex)
101 bool token_found = false;
104 string const token = lex.getString();
105 if (token == "status") {
107 string const tmp_token = lex.getString();
109 if (tmp_token == "Inlined") {
111 } else if (tmp_token == "Collapsed") {
112 status(0, Collapsed);
114 // leave this as default!
120 lyxerr << "InsetERT::Read: Missing 'status'-tag!"
122 // take countermeasures
123 lex.pushToken(token);
126 #warning this should be really short lived only for compatibility to
127 #warning files written 07/08/2001 so this has to go before 1.2.0! (Jug)
130 string const token = lex.getString();
131 if (token == "collapsed") {
133 collapsed_ = lex.getBool();
135 // Take countermeasures
136 lex.pushToken(token);
139 inset.read(buf, lex);
142 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
144 LyXFont font(LyXFont::ALL_INHERIT);
147 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
148 font.setColor(LColor::latex);
149 Paragraph * par = inset.paragraph();
151 Paragraph::size_type siz = par->size();
152 for (Paragraph::size_type i = 0; i < siz; ++i) {
153 par->setFont(i, font);
160 status(0, Collapsed);
169 void InsetERT::write(Buffer const * buf, ostream & os) const
185 os << getInsetName() << "\n"
186 << "status "<< st << "\n";
188 //inset.writeParagraphData(buf, os);
189 string const layout =
190 textclasslist.NameOfLayout(buf->params.textclass, 0);
191 Paragraph * par = inset.paragraph();
193 os << "\n\\layout " << layout << "\n";
194 Paragraph::size_type siz = par->size();
195 for (Paragraph::size_type i = 0; i < siz; ++i) {
196 Paragraph::value_type c = par->getChar(i);
198 case Paragraph::META_INSET:
199 case Paragraph::META_HFILL:
200 lyxerr << "Element is not allowed in insertERT"
202 case Paragraph::META_NEWLINE:
203 os << "\n\\newline \n";
206 os << "\n\\backslash \n";
218 string const InsetERT::editMessage() const
220 return _("Opened ERT Inset");
224 bool InsetERT::insertInset(BufferView *, Inset *)
230 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
232 // if selectall is activated then the fontchange was an outside general
233 // fontchange and this messages is not needed
235 WriteAlert(_("Impossible Operation!"),
236 _("Not permitted to change font-types inside ERT-insets!"),
241 void InsetERT::edit(BufferView * bv, int x, int y, unsigned int button)
243 InsetCollapsable::edit(bv, x, y, button);
244 if (status_ != Inlined) {
246 status(0, Collapsed);
255 Inset::EDITABLE InsetERT::editable() const
257 if (status_ == Collapsed)
259 return HIGHLY_EDITABLE;
263 void InsetERT::edit(BufferView * bv, bool front)
265 InsetCollapsable::edit(bv, front);
266 if (status_ != Inlined) {
268 status(0, Collapsed);
277 void InsetERT::insetButtonRelease(BufferView * bv, int x, int y, int button)
283 if ((x >= 0) && (x < button_length) &&
284 (y >= button_top_y) && (y <= button_bottom_y))
287 // setLabel(_("ERT"));
289 // setLabel(get_new_label());
293 // collapsed_ = false;
294 // inset.insetButtonRelease(bv, 0, 0, button);
295 // inset.setUpdateStatus(bv, InsetText::FULL);
296 // bv->updateInset(this, true);
298 status(bv, Collapsed);
299 // collapsed_ = true;
300 // bv->unlockInset(this);
301 // bv->updateInset(this, true);
303 } else if (!collapsed_ && (y > button_bottom_y)) {
304 LyXFont font(LyXFont::ALL_SANE);
305 int yy = ascent(bv, font) + y -
306 (ascent_collapsed() +
307 descent_collapsed() +
308 inset.ascent(bv, font));
309 inset.insetButtonRelease(bv, x, yy, button);
314 int InsetERT::latex(Buffer const *, std::ostream & os, bool /*fragile*/,
315 bool /*free_spc*/) const
317 Paragraph * par = inset.paragraph();
319 Paragraph::size_type siz = par->size();
320 for (Paragraph::size_type i = 0; i < siz; ++i) {
321 Paragraph::value_type c = par->getChar(i);
323 case Paragraph::META_NEWLINE:
340 int InsetERT::ascii(Buffer const *,
341 std::ostream &, int /*linelen*/) const
347 int InsetERT::linuxdoc(Buffer const *, std::ostream &) const
353 int InsetERT::docbook(Buffer const *, std::ostream &) const
359 UpdatableInset::RESULT
360 InsetERT::localDispatch(BufferView * bv, kb_action action, string const & arg)
362 UpdatableInset::RESULT result = DISPATCHED_NOUPDATE;
364 if (!inset.paragraph()->size()) {
370 bv->owner()->setLayout(inset.paragraph()->getLayout());
373 result = InsetCollapsable::localDispatch(bv, action, arg);
376 case LFUN_BREAKPARAGRAPH:
377 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
379 case LFUN_BACKSPACE_SKIP:
381 case LFUN_DELETE_SKIP:
382 case LFUN_DELETE_LINE_FORWARD:
394 string const InsetERT::get_new_label() const
397 Paragraph::size_type const max_length = 15;
399 Paragraph::size_type const p_siz = inset.paragraph()->size();
400 Paragraph::size_type const n = std::min(max_length, p_siz);
403 for(; i < n && j < p_siz; ++j) {
404 if (inset.paragraph()->isInset(j))
406 la += inset.paragraph()->getChar(j);
409 if (i > 0 && j < p_siz) {
419 void InsetERT::setButtonLabel() const
421 if (status_ == Collapsed) {
422 setLabel(get_new_label());
429 bool InsetERT::checkInsertChar(LyXFont & font)
432 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
434 LyXFont f(LyXFont::ALL_INHERIT);
437 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
438 font.setColor(LColor::latex);
443 int InsetERT::ascent(BufferView * bv, LyXFont const & font) const
446 return InsetCollapsable::ascent(bv, font);
448 return inset.ascent(bv, font);
452 int InsetERT::descent(BufferView * bv, LyXFont const & font) const
455 return InsetCollapsable::descent(bv, font);
457 return inset.descent(bv, font);
461 int InsetERT::width(BufferView * bv, LyXFont const & font) const
464 return InsetCollapsable::width(bv, font);
466 return inset.width(bv, font);
470 void InsetERT::draw(BufferView * bv, LyXFont const & f,
471 int baseline, float & x, bool cleared) const
473 Painter & pain = bv->painter();
475 button_length = width_collapsed();
476 button_top_y = -ascent(bv, f);
477 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
481 draw_collapsed(pain, baseline, x);
482 x += TEXT_TO_INSET_OFFSET;
489 x += static_cast<float>(scroll());
491 if (!cleared && (inset.need_update == InsetText::FULL ||
492 inset.need_update == InsetText::INIT ||
494 top_baseline != baseline))
496 // we don't need anymore to clear here we just have to tell
497 // the underlying LyXText that it should do the RowClear!
498 inset.setUpdateStatus(bv, InsetText::FULL);
499 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
505 top_baseline = baseline;
507 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
510 inset.draw(bv, f, baseline, x, cleared);
512 draw_collapsed(pain, bl, old_x);
514 bl + descent_collapsed() + inset.ascent(bv, f),
521 void InsetERT::set_latex_font(BufferView * bv)
524 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
526 LyXFont font(LyXFont::ALL_INHERIT);
529 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
530 font.setColor(LColor::latex);
531 inset.getLyXText(bv)->setFont(bv, font, false);
535 void InsetERT::status(BufferView * bv, ERTStatus const st) const
541 inset.setAutoBreakRows(false);
544 inset.setAutoBreakRows(true);
550 inset.setAutoBreakRows(true);
555 bv->unlockInset(const_cast<InsetERT *>(this));
559 bv->updateInset(const_cast<InsetERT *>(this), true);
564 bool InsetERT::showInsetDialog(BufferView * bv) const
566 bv->owner()->getDialogs()->showERT(const_cast<InsetERT *>(this));
571 void InsetERT::open(BufferView * bv)
579 void InsetERT::close(BufferView * bv) const
583 status(bv, Collapsed);
587 string const InsetERT::selectNextWordToSpellcheck(BufferView * bv,float &) const
589 bv->unlockInset(const_cast<InsetERT *>(this));