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::updateStatus(BufferView * bv, bool swap) const
243 if (status_ != Inlined) {
245 status(bv, swap ? Open : Collapsed);
247 status(bv, swap ? Collapsed : Open);
253 void InsetERT::edit(BufferView * bv, int x, int y, unsigned int button)
258 InsetCollapsable::edit(bv, x, y, button);
264 Inset::EDITABLE InsetERT::editable() const
266 if (status_ == Collapsed)
268 return HIGHLY_EDITABLE;
272 void InsetERT::edit(BufferView * bv, bool front)
274 InsetCollapsable::edit(bv, front);
280 void InsetERT::insetButtonRelease(BufferView * bv, int x, int y, int button)
287 if (status_ != Inlined && (x >= 0) && (x < button_length) &&
288 (y >= button_top_y) && (y <= button_bottom_y)) {
289 updateStatus(bv, true);
291 LyXFont font(LyXFont::ALL_SANE);
292 int yy = ascent(bv, font) + y - inset.ascent(bv, font);
294 // inlined is special - the text appears above
296 if (status_ == Inlined) {
297 inset.insetButtonRelease(bv, x, yy, button);
298 } else if (!collapsed_ && (y > button_bottom_y)) {
299 yy -= (ascent_collapsed() + descent_collapsed());
300 inset.insetButtonRelease(bv, x, yy, button);
306 int InsetERT::latex(Buffer const *, std::ostream & os, bool /*fragile*/,
307 bool /*free_spc*/) const
309 Paragraph * par = inset.paragraph();
312 Paragraph::size_type siz = par->size();
313 for (Paragraph::size_type i = 0; i < siz; ++i) {
314 Paragraph::value_type c = par->getChar(i);
316 case Paragraph::META_NEWLINE:
336 int InsetERT::ascii(Buffer const *,
337 std::ostream &, int /*linelen*/) const
343 int InsetERT::linuxdoc(Buffer const *, std::ostream & os) const
345 Paragraph * par = inset.paragraph();
348 Paragraph::size_type siz = par->size();
349 for (Paragraph::size_type i = 0; i < siz; ++i) {
350 Paragraph::value_type c = par->getChar(i);
352 case Paragraph::META_NEWLINE:
372 int InsetERT::docbook(Buffer const *, std::ostream & os) const
374 Paragraph * par = inset.paragraph();
377 Paragraph::size_type siz = par->size();
378 for (Paragraph::size_type i = 0; i < siz; ++i) {
379 Paragraph::value_type c = par->getChar(i);
381 case Paragraph::META_NEWLINE:
401 UpdatableInset::RESULT
402 InsetERT::localDispatch(BufferView * bv, kb_action action, string const & arg)
404 UpdatableInset::RESULT result = DISPATCHED_NOUPDATE;
406 if (!inset.paragraph()->size()) {
412 bv->owner()->setLayout(inset.paragraph()->getLayout());
415 result = InsetCollapsable::localDispatch(bv, action, arg);
418 case LFUN_BREAKPARAGRAPH:
419 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
421 case LFUN_BACKSPACE_SKIP:
423 case LFUN_DELETE_SKIP:
424 case LFUN_DELETE_LINE_FORWARD:
436 string const InsetERT::get_new_label() const
439 Paragraph::size_type const max_length = 15;
441 Paragraph::size_type const p_siz = inset.paragraph()->size();
442 Paragraph::size_type const n = std::min(max_length, p_siz);
445 for(; i < n && j < p_siz; ++j) {
446 if (inset.paragraph()->isInset(j))
448 la += inset.paragraph()->getChar(j);
451 if (i > 0 && j < p_siz) {
461 void InsetERT::setButtonLabel() const
463 if (status_ == Collapsed) {
464 setLabel(get_new_label());
471 bool InsetERT::checkInsertChar(LyXFont & font)
474 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
476 LyXFont f(LyXFont::ALL_INHERIT);
479 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
480 font.setColor(LColor::latex);
485 int InsetERT::ascent(BufferView * bv, LyXFont const & font) const
488 return InsetCollapsable::ascent(bv, font);
490 return inset.ascent(bv, font);
494 int InsetERT::descent(BufferView * bv, LyXFont const & font) const
497 return InsetCollapsable::descent(bv, font);
499 return inset.descent(bv, font);
503 int InsetERT::width(BufferView * bv, LyXFont const & font) const
506 return InsetCollapsable::width(bv, font);
508 return inset.width(bv, font);
512 void InsetERT::draw(BufferView * bv, LyXFont const & f,
513 int baseline, float & x, bool cleared) const
515 Painter & pain = bv->painter();
517 button_length = width_collapsed();
518 button_top_y = -ascent(bv, f);
519 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
523 draw_collapsed(pain, baseline, x);
524 x += TEXT_TO_INSET_OFFSET;
531 x += static_cast<float>(scroll());
533 if (!cleared && (inset.need_update == InsetText::FULL ||
534 inset.need_update == InsetText::INIT ||
536 top_baseline != baseline))
538 // we don't need anymore to clear here we just have to tell
539 // the underlying LyXText that it should do the RowClear!
540 inset.setUpdateStatus(bv, InsetText::FULL);
541 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
547 top_baseline = baseline;
549 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
552 inset.draw(bv, f, baseline, x, cleared);
554 draw_collapsed(pain, bl, old_x);
556 bl + descent_collapsed() + inset.ascent(bv, f),
563 void InsetERT::set_latex_font(BufferView * bv)
566 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
568 LyXFont font(LyXFont::ALL_INHERIT);
571 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
572 font.setColor(LColor::latex);
573 inset.getLyXText(bv)->setFont(bv, font, false);
577 void InsetERT::status(BufferView * bv, ERTStatus const st) const
583 inset.setAutoBreakRows(false);
586 inset.setAutoBreakRows(true);
592 inset.setAutoBreakRows(true);
597 bv->unlockInset(const_cast<InsetERT *>(this));
601 bv->updateInset(const_cast<InsetERT *>(this), false);
606 bool InsetERT::showInsetDialog(BufferView * bv) const
608 bv->owner()->getDialogs()->showERT(const_cast<InsetERT *>(this));
613 void InsetERT::open(BufferView * bv)
621 void InsetERT::close(BufferView * bv) const
625 status(bv, Collapsed);
629 string const InsetERT::selectNextWordToSpellcheck(BufferView * bv,float &) const
631 bv->unlockInset(const_cast<InsetERT *>(this));