1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998 The LyX Team.
8 *======================================================*/
13 #pragma implementation
17 #include "insettext.h"
20 #include "BufferView.h"
22 #include "funcrequest.h"
29 #include "WordLangTuple.h"
31 #include "frontends/Alert.h"
32 #include "frontends/Dialogs.h"
33 #include "frontends/LyXView.h"
35 #include "support/LOstream.h"
48 labelfont = LyXFont(LyXFont::ALL_SANE);
51 labelfont.setColor(LColor::latex);
56 InsetERT::InsetERT(BufferParams const & bp, bool collapsed)
57 : InsetCollapsable(bp, collapsed)
67 InsetERT::InsetERT(InsetERT const & in, bool same_id)
68 : InsetCollapsable(in, same_id), status_(in.status_)
74 Inset * InsetERT::clone(Buffer const &, bool same_id) const
76 return new InsetERT(*const_cast<InsetERT *>(this), same_id);
80 InsetERT::InsetERT(BufferParams const & bp,
81 Language const * l, string const & contents, bool collapsed)
82 : InsetCollapsable(bp, collapsed)
89 LyXFont font(LyXFont::ALL_INHERIT, l);
91 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
92 font.setColor(LColor::latex);
95 string::const_iterator cit = contents.begin();
96 string::const_iterator end = contents.end();
98 for (; cit != end; ++cit) {
99 inset.paragraph()->insertChar(pos++, *cit, font);
101 // the init has to be after the initialization of the paragraph
102 // because of the label settings (draw_label for ert insets).
107 InsetERT::~InsetERT()
113 void InsetERT::read(Buffer const * buf, LyXLex & lex)
115 bool token_found = false;
118 string const token = lex.getString();
119 if (token == "status") {
121 string const tmp_token = lex.getString();
123 if (tmp_token == "Inlined") {
125 } else if (tmp_token == "Collapsed") {
126 status(0, Collapsed);
128 // leave this as default!
134 lyxerr << "InsetERT::Read: Missing 'status'-tag!"
136 // take countermeasures
137 lex.pushToken(token);
141 #warning this should be really short lived only for compatibility to
142 #warning files written 07/08/2001 so this has to go before 1.2.0! (Jug)
145 string const token = lex.getString();
146 if (token == "collapsed") {
148 collapsed_ = lex.getBool();
150 // Take countermeasures
151 lex.pushToken(token);
155 inset.read(buf, lex);
159 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
161 LyXFont font(LyXFont::ALL_INHERIT);
163 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
164 font.setColor(LColor::latex);
165 Paragraph * par = inset.paragraph();
167 pos_type siz = par->size();
168 for (pos_type i = 0; i < siz; ++i) {
169 par->setFont(i, font);
177 status(0, Collapsed);
186 void InsetERT::write(Buffer const * buf, ostream & os) const
202 os << getInsetName() << "\n"
203 << "status "<< st << "\n";
205 //inset.writeParagraphData(buf, os);
206 string const layout(buf->params.getLyXTextClass().defaultLayoutName());
207 Paragraph * par = inset.paragraph();
209 os << "\n\\layout " << layout << "\n";
210 pos_type siz = par->size();
211 for (pos_type i = 0; i < siz; ++i) {
212 Paragraph::value_type c = par->getChar(i);
214 case Paragraph::META_INSET:
215 case Paragraph::META_HFILL:
216 lyxerr << "Element is not allowed in insertERT"
218 case Paragraph::META_NEWLINE:
219 os << "\n\\newline \n";
222 os << "\n\\backslash \n";
234 string const InsetERT::editMessage() const
236 return _("Opened ERT Inset");
240 bool InsetERT::insertInset(BufferView *, Inset *)
246 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
248 // if selectall is activated then the fontchange was an outside general
249 // fontchange and this messages is not needed
251 Alert::alert(_("Impossible Operation!"),
252 _("Not permitted to change font-types inside ERT-insets!"),
257 void InsetERT::updateStatus(BufferView * bv, bool swap) const
259 if (status_ != Inlined) {
261 status(bv, swap ? Open : Collapsed);
263 status(bv, swap ? Collapsed : Open);
268 void InsetERT::edit(BufferView * bv, int x, int y, mouse_button::state button)
270 if (button == mouse_button::button3)
273 if (status_ == Inlined) {
274 if (!bv->lockInset(this))
276 inset.edit(bv, x, y, button);
278 InsetCollapsable::edit(bv, x, y, button);
285 Inset::EDITABLE InsetERT::editable() const
287 if (status_ == Collapsed)
289 return HIGHLY_EDITABLE;
293 void InsetERT::edit(BufferView * bv, bool front)
295 InsetCollapsable::edit(bv, front);
301 void InsetERT::lfunMousePress(FuncRequest const & cmd)
303 if (status_ == Inlined)
304 inset.localDispatch(cmd);
306 InsetCollapsable::localDispatch(cmd);
310 bool InsetERT::lfunMouseRelease(FuncRequest const & cmd)
312 BufferView * bv = cmd.view();
314 if (cmd.button() == mouse_button::button3) {
319 if (status_ != Inlined && (cmd.x >= 0) && (cmd.x < button_length) &&
320 (cmd.y >= button_top_y) && (cmd.y <= button_bottom_y)) {
321 updateStatus(bv, true);
323 LyXFont font(LyXFont::ALL_SANE);
324 FuncRequest cmd1 = cmd;
325 cmd1.y = ascent(bv, font) + cmd.y - inset.ascent(bv, font);
327 // inlined is special - the text appears above
329 if (status_ == Inlined)
330 inset.localDispatch(cmd1);
331 else if (!collapsed_ && (cmd.y > button_bottom_y)) {
332 cmd1.y -= ascent_collapsed() + descent_collapsed();
333 inset.localDispatch(cmd1);
340 void InsetERT::lfunMouseMotion(FuncRequest const & cmd)
342 if (status_ == Inlined)
343 inset.localDispatch(cmd);
345 InsetCollapsable::localDispatch(cmd);
349 int InsetERT::latex(Buffer const *, ostream & os, bool /*fragile*/,
350 bool /*free_spc*/) const
352 Paragraph * par = inset.paragraph();
355 pos_type siz = par->size();
356 for (pos_type i = 0; i < siz; ++i) {
357 Paragraph::value_type c = par->getChar(i);
359 case Paragraph::META_NEWLINE:
379 int InsetERT::ascii(Buffer const *, ostream &, int /*linelen*/) const
385 int InsetERT::linuxdoc(Buffer const *, ostream & os) const
387 Paragraph * par = inset.paragraph();
390 pos_type siz = par->size();
391 for (pos_type i = 0; i < siz; ++i) {
392 Paragraph::value_type c = par->getChar(i);
394 case Paragraph::META_NEWLINE:
414 int InsetERT::docbook(Buffer const *, ostream & os, bool) const
416 Paragraph * par = inset.paragraph();
419 pos_type siz = par->size();
420 for (pos_type i = 0; i < siz; ++i) {
421 Paragraph::value_type c = par->getChar(i);
423 case Paragraph::META_NEWLINE:
443 Inset::RESULT InsetERT::localDispatch(FuncRequest const & cmd)
445 Inset::RESULT result = DISPATCHED_NOUPDATE;
446 BufferView * bv = cmd.view();
448 if (inset.paragraph()->empty()) {
452 switch (cmd.action) {
453 case LFUN_MOUSE_PRESS:
457 case LFUN_MOUSE_MOTION:
458 lfunMouseMotion(cmd);
461 case LFUN_MOUSE_RELEASE:
462 lfunMouseRelease(cmd);
466 bv->owner()->setLayout(inset.paragraph()->layout()->name());
470 result = InsetCollapsable::localDispatch(cmd);
473 switch (cmd.action) {
474 case LFUN_BREAKPARAGRAPH:
475 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
477 case LFUN_BACKSPACE_SKIP:
479 case LFUN_DELETE_SKIP:
480 case LFUN_DELETE_LINE_FORWARD:
492 string const InsetERT::get_new_label() const
495 pos_type const max_length = 15;
496 pos_type const p_siz = inset.paragraph()->size();
497 pos_type const n = min(max_length, p_siz);
500 for(; i < n && j < p_siz; ++j) {
501 if (inset.paragraph()->isInset(j))
503 la += inset.paragraph()->getChar(j);
506 if (inset.paragraph()->next() || (i > 0 && j < p_siz)) {
516 void InsetERT::setButtonLabel() const
518 if (status_ == Collapsed) {
519 setLabel(get_new_label());
526 bool InsetERT::checkInsertChar(LyXFont & /* font */)
530 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
532 LyXFont f(LyXFont::ALL_INHERIT);
535 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
536 font.setColor(LColor::latex);
542 int InsetERT::ascent(BufferView * bv, LyXFont const & font) const
545 return InsetCollapsable::ascent(bv, font);
547 return inset.ascent(bv, font);
551 int InsetERT::descent(BufferView * bv, LyXFont const & font) const
554 return InsetCollapsable::descent(bv, font);
556 return inset.descent(bv, font);
560 int InsetERT::width(BufferView * bv, LyXFont const & font) const
563 return InsetCollapsable::width(bv, font);
565 return inset.width(bv, font);
569 void InsetERT::draw(BufferView * bv, LyXFont const & f,
570 int baseline, float & x, bool cleared) const
572 Painter & pain = bv->painter();
574 button_length = width_collapsed();
575 button_top_y = -ascent(bv, f);
576 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
580 draw_collapsed(pain, baseline, x);
587 x += static_cast<float>(scroll());
589 if (!cleared && (inset.need_update == InsetText::FULL ||
590 inset.need_update == InsetText::INIT ||
592 top_baseline != baseline))
594 // we don't need anymore to clear here we just have to tell
595 // the underlying LyXText that it should do the RowClear!
596 inset.setUpdateStatus(bv, InsetText::FULL);
597 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
603 top_baseline = baseline;
605 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
608 inset.draw(bv, f, baseline, x, cleared);
610 draw_collapsed(pain, bl, old_x);
612 bl + descent_collapsed() + inset.ascent(bv, f),
619 void InsetERT::set_latex_font(BufferView * /* bv */)
623 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
625 LyXFont font(LyXFont::ALL_INHERIT);
628 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
629 font.setColor(LColor::latex);
631 inset.getLyXText(bv)->setFont(bv, font, false);
636 // attention this function can be called with bv == 0
637 void InsetERT::status(BufferView * bv, ERTStatus const st) const
645 inset.setUpdateStatus(bv, InsetText::INIT);
655 bv->unlockInset(const_cast<InsetERT *>(this));
659 bv->updateInset(const_cast<InsetERT *>(this), false);
664 bool InsetERT::showInsetDialog(BufferView * bv) const
666 bv->owner()->getDialogs().showERT(const_cast<InsetERT *>(this));
671 void InsetERT::open(BufferView * bv)
679 void InsetERT::close(BufferView * bv) const
681 if (status_ == Collapsed || status_ == Inlined)
684 status(bv, Collapsed);
689 InsetERT::selectNextWordToSpellcheck(BufferView * bv, float &) const
691 bv->unlockInset(const_cast<InsetERT *>(this));
692 return WordLangTuple();
696 void InsetERT::getDrawFont(LyXFont & font) const
699 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
701 LyXFont f(LyXFont::ALL_INHERIT);
704 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
705 font.setColor(LColor::latex);
709 int InsetERT::getMaxWidth(BufferView * bv, UpdatableInset const * in) const
711 int w = InsetCollapsable::getMaxWidth(bv, in);
712 if (status_ != Inlined || w < 0)
714 LyXText * text = inset.getLyXText(bv);
715 int rw = text->firstRow()->width();
719 if (!text->firstRow()->next() && rw < w)
725 void InsetERT::update(BufferView * bv, LyXFont const & font,
728 if (inset.need_update & InsetText::INIT ||
729 inset.need_update & InsetText::FULL)
733 InsetCollapsable::update(bv, font, reinit);