1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998 The LyX Team.
8 *======================================================*/
13 #pragma implementation
21 #include "insets/insettext.h"
22 #include "support/LOstream.h"
23 #include "lyx_gui_misc.h"
24 #include "BufferView.h"
27 #include "frontends/Dialogs.h"
28 #include "frontends/Alert.h"
38 labelfont = LyXFont(LyXFont::ALL_SANE);
41 labelfont.setColor(LColor::latex);
46 InsetERT::InsetERT(bool collapsed)
47 : InsetCollapsable(collapsed)
57 InsetERT::InsetERT(InsetERT const & in, bool same_id)
58 : InsetCollapsable(in, same_id), status_(in.status_)
64 Inset * InsetERT::clone(Buffer const &, bool same_id) const
66 return new InsetERT(*const_cast<InsetERT *>(this), same_id);
70 InsetERT::InsetERT(string const & contents, bool collapsed)
71 : InsetCollapsable(collapsed)
78 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
80 LyXFont font(LyXFont::ALL_INHERIT);
82 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
83 font.setColor(LColor::latex);
84 string::const_iterator cit = contents.begin();
85 string::const_iterator end = contents.end();
87 for (; cit != end; ++cit) {
88 inset.paragraph()->insertChar(pos++, *cit, font);
90 // the init has to be after the initialization of the paragraph
91 // because of the label settings (draw_label for ert insets).
102 void InsetERT::read(Buffer const * buf, LyXLex & lex)
104 bool token_found = false;
107 string const token = lex.getString();
108 if (token == "status") {
110 string const tmp_token = lex.getString();
112 if (tmp_token == "Inlined") {
114 } else if (tmp_token == "Collapsed") {
115 status(0, Collapsed);
117 // leave this as default!
123 lyxerr << "InsetERT::Read: Missing 'status'-tag!"
125 // take countermeasures
126 lex.pushToken(token);
129 #warning this should be really short lived only for compatibility to
130 #warning files written 07/08/2001 so this has to go before 1.2.0! (Jug)
133 string const token = lex.getString();
134 if (token == "collapsed") {
136 collapsed_ = lex.getBool();
138 // Take countermeasures
139 lex.pushToken(token);
142 inset.read(buf, lex);
145 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
147 LyXFont font(LyXFont::ALL_INHERIT);
150 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
151 font.setColor(LColor::latex);
152 Paragraph * par = inset.paragraph();
154 pos_type siz = par->size();
155 for (pos_type i = 0; i < siz; ++i) {
156 par->setFont(i, font);
163 status(0, Collapsed);
172 void InsetERT::write(Buffer const * buf, ostream & os) const
188 os << getInsetName() << "\n"
189 << "status "<< st << "\n";
191 //inset.writeParagraphData(buf, os);
192 string const layout =
193 textclasslist.NameOfLayout(buf->params.textclass, 0);
194 Paragraph * par = inset.paragraph();
196 os << "\n\\layout " << layout << "\n";
197 pos_type siz = par->size();
198 for (pos_type i = 0; i < siz; ++i) {
199 Paragraph::value_type c = par->getChar(i);
201 case Paragraph::META_INSET:
202 case Paragraph::META_HFILL:
203 lyxerr << "Element is not allowed in insertERT"
205 case Paragraph::META_NEWLINE:
206 os << "\n\\newline \n";
209 os << "\n\\backslash \n";
221 string const InsetERT::editMessage() const
223 return _("Opened ERT Inset");
227 bool InsetERT::insertInset(BufferView *, Inset *)
233 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
235 // if selectall is activated then the fontchange was an outside general
236 // fontchange and this messages is not needed
238 Alert::alert(_("Impossible Operation!"),
239 _("Not permitted to change font-types inside ERT-insets!"),
244 void InsetERT::updateStatus(BufferView * bv, bool swap) const
246 if (status_ != Inlined) {
248 status(bv, swap ? Open : Collapsed);
250 status(bv, swap ? Collapsed : Open);
256 void InsetERT::edit(BufferView * bv, int x, int y, unsigned int button)
261 if (status_ == Inlined) {
262 if (!bv->lockInset(this))
264 inset.edit(bv, x, y, button);
266 InsetCollapsable::edit(bv, x, y, button);
273 Inset::EDITABLE InsetERT::editable() const
275 if (status_ == Collapsed)
277 return HIGHLY_EDITABLE;
281 void InsetERT::edit(BufferView * bv, bool front)
283 InsetCollapsable::edit(bv, front);
291 void InsetERT::insetButtonPress(BufferView * bv,
292 int x, int y, int button)
294 if (status_ == Inlined) {
295 inset.insetButtonPress(bv, x, y, button);
297 InsetCollapsable::insetButtonPress(bv, x, y, button);
302 void InsetERT::insetButtonRelease(BufferView * bv, int x, int y, int button)
309 if (status_ != Inlined && (x >= 0) && (x < button_length) &&
310 (y >= button_top_y) && (y <= button_bottom_y)) {
311 updateStatus(bv, true);
313 LyXFont font(LyXFont::ALL_SANE);
314 int yy = ascent(bv, font) + y - inset.ascent(bv, font);
316 // inlined is special - the text appears above
318 if (status_ == Inlined) {
319 inset.insetButtonRelease(bv, x, yy, button);
320 } else if (!collapsed_ && (y > button_bottom_y)) {
321 yy -= (ascent_collapsed() + descent_collapsed());
322 inset.insetButtonRelease(bv, x, yy, button);
328 void InsetERT::insetMotionNotify(BufferView * bv,
329 int x, int y, int state)
331 if (status_ == Inlined) {
332 inset.insetMotionNotify(bv, x, y, state);
334 InsetCollapsable::insetMotionNotify(bv, x, y, state);
339 int InsetERT::latex(Buffer const *, std::ostream & os, bool /*fragile*/,
340 bool /*free_spc*/) const
342 Paragraph * par = inset.paragraph();
345 pos_type siz = par->size();
346 for (pos_type i = 0; i < siz; ++i) {
347 Paragraph::value_type c = par->getChar(i);
349 case Paragraph::META_NEWLINE:
369 int InsetERT::ascii(Buffer const *,
370 std::ostream &, int /*linelen*/) const
376 int InsetERT::linuxdoc(Buffer const *, std::ostream & os) const
378 Paragraph * par = inset.paragraph();
381 pos_type siz = par->size();
382 for (pos_type i = 0; i < siz; ++i) {
383 Paragraph::value_type c = par->getChar(i);
385 case Paragraph::META_NEWLINE:
405 int InsetERT::docbook(Buffer const *, std::ostream & os) const
407 Paragraph * par = inset.paragraph();
410 pos_type siz = par->size();
411 for (pos_type i = 0; i < siz; ++i) {
412 Paragraph::value_type c = par->getChar(i);
414 case Paragraph::META_NEWLINE:
434 UpdatableInset::RESULT
435 InsetERT::localDispatch(BufferView * bv, kb_action action, string const & arg)
437 UpdatableInset::RESULT result = DISPATCHED_NOUPDATE;
439 if (!inset.paragraph()->size()) {
445 bv->owner()->setLayout(inset.paragraph()->getLayout());
448 result = InsetCollapsable::localDispatch(bv, action, arg);
451 case LFUN_BREAKPARAGRAPH:
452 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
454 case LFUN_BACKSPACE_SKIP:
456 case LFUN_DELETE_SKIP:
457 case LFUN_DELETE_LINE_FORWARD:
469 string const InsetERT::get_new_label() const
472 pos_type const max_length = 15;
473 pos_type const p_siz = inset.paragraph()->size();
474 pos_type const n = std::min(max_length, p_siz);
477 for(; i < n && j < p_siz; ++j) {
478 if (inset.paragraph()->isInset(j))
480 la += inset.paragraph()->getChar(j);
483 if (i > 0 && j < p_siz) {
493 void InsetERT::setButtonLabel() const
495 if (status_ == Collapsed) {
496 setLabel(get_new_label());
503 bool InsetERT::checkInsertChar(LyXFont & font)
506 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
508 LyXFont f(LyXFont::ALL_INHERIT);
511 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
512 font.setColor(LColor::latex);
517 int InsetERT::ascent(BufferView * bv, LyXFont const & font) const
520 return InsetCollapsable::ascent(bv, font);
522 return inset.ascent(bv, font);
526 int InsetERT::descent(BufferView * bv, LyXFont const & font) const
529 return InsetCollapsable::descent(bv, font);
531 return inset.descent(bv, font);
535 int InsetERT::width(BufferView * bv, LyXFont const & font) const
538 return InsetCollapsable::width(bv, font);
540 return inset.width(bv, font);
544 void InsetERT::draw(BufferView * bv, LyXFont const & f,
545 int baseline, float & x, bool cleared) const
547 Painter & pain = bv->painter();
549 button_length = width_collapsed();
550 button_top_y = -ascent(bv, f);
551 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
555 draw_collapsed(pain, baseline, x);
556 x += TEXT_TO_INSET_OFFSET;
563 x += static_cast<float>(scroll());
565 if (!cleared && (inset.need_update == InsetText::FULL ||
566 inset.need_update == InsetText::INIT ||
568 top_baseline != baseline))
570 // we don't need anymore to clear here we just have to tell
571 // the underlying LyXText that it should do the RowClear!
572 inset.setUpdateStatus(bv, InsetText::FULL);
573 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
579 top_baseline = baseline;
581 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
584 inset.draw(bv, f, baseline, x, cleared);
586 draw_collapsed(pain, bl, old_x);
588 bl + descent_collapsed() + inset.ascent(bv, f),
595 void InsetERT::set_latex_font(BufferView * bv)
598 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
600 LyXFont font(LyXFont::ALL_INHERIT);
603 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
604 font.setColor(LColor::latex);
605 inset.getLyXText(bv)->setFont(bv, font, false);
609 void InsetERT::status(BufferView * bv, ERTStatus const st) const
615 inset.setAutoBreakRows(false);
618 inset.setAutoBreakRows(true);
624 inset.setAutoBreakRows(true);
629 bv->unlockInset(const_cast<InsetERT *>(this));
633 bv->updateInset(const_cast<InsetERT *>(this), false);
638 bool InsetERT::showInsetDialog(BufferView * bv) const
640 bv->owner()->getDialogs()->showERT(const_cast<InsetERT *>(this));
645 void InsetERT::open(BufferView * bv)
653 void InsetERT::close(BufferView * bv) const
657 status(bv, Collapsed);
661 string const InsetERT::selectNextWordToSpellcheck(BufferView * bv,float &) const
663 bv->unlockInset(const_cast<InsetERT *>(this));