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);
158 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
159 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
160 font.setColor(LColor::latex);
161 Paragraph * par = inset.paragraph();
163 pos_type siz = par->size();
164 for (pos_type i = 0; i < siz; ++i) {
165 par->setFont(i, font);
173 status(0, Collapsed);
182 void InsetERT::write(Buffer const * buf, ostream & os) const
198 os << getInsetName() << "\n"
199 << "status "<< st << "\n";
201 //inset.writeParagraphData(buf, os);
202 string const layout(buf->params.getLyXTextClass().defaultLayoutName());
203 Paragraph * par = inset.paragraph();
205 os << "\n\\layout " << layout << "\n";
206 pos_type siz = par->size();
207 for (pos_type i = 0; i < siz; ++i) {
208 Paragraph::value_type c = par->getChar(i);
210 case Paragraph::META_INSET:
211 case Paragraph::META_HFILL:
212 lyxerr << "Element is not allowed in insertERT"
214 case Paragraph::META_NEWLINE:
215 os << "\n\\newline \n";
218 os << "\n\\backslash \n";
230 string const InsetERT::editMessage() const
232 return _("Opened ERT Inset");
236 bool InsetERT::insertInset(BufferView *, Inset *)
242 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
244 // if selectall is activated then the fontchange was an outside general
245 // fontchange and this messages is not needed
247 Alert::alert(_("Impossible Operation!"),
248 _("Not permitted to change font-types inside ERT-insets!"),
253 void InsetERT::updateStatus(BufferView * bv, bool swap) const
255 if (status_ != Inlined) {
257 status(bv, swap ? Open : Collapsed);
259 status(bv, swap ? Collapsed : Open);
264 void InsetERT::edit(BufferView * bv, int x, int y, mouse_button::state button)
266 if (button == mouse_button::button3)
269 if (status_ == Inlined) {
270 if (!bv->lockInset(this))
272 inset.edit(bv, x, y, button);
274 InsetCollapsable::edit(bv, x, y, button);
281 Inset::EDITABLE InsetERT::editable() const
283 if (status_ == Collapsed)
285 return HIGHLY_EDITABLE;
289 void InsetERT::edit(BufferView * bv, bool front)
291 InsetCollapsable::edit(bv, front);
297 void InsetERT::lfunMousePress(FuncRequest const & cmd)
299 if (status_ == Inlined)
300 inset.localDispatch(cmd);
302 InsetCollapsable::localDispatch(cmd);
306 bool InsetERT::lfunMouseRelease(FuncRequest const & cmd)
308 BufferView * bv = cmd.view();
310 if (cmd.button() == mouse_button::button3) {
315 if (status_ != Inlined && (cmd.x >= 0) && (cmd.x < button_length) &&
316 (cmd.y >= button_top_y) && (cmd.y <= button_bottom_y)) {
317 updateStatus(bv, true);
319 LyXFont font(LyXFont::ALL_SANE);
320 FuncRequest cmd1 = cmd;
321 cmd1.y = ascent(bv, font) + cmd.y - inset.ascent(bv, font);
323 // inlined is special - the text appears above
325 if (status_ == Inlined)
326 inset.localDispatch(cmd1);
327 else if (!collapsed_ && (cmd.y > button_bottom_y)) {
328 cmd1.y -= ascent_collapsed() + descent_collapsed();
329 inset.localDispatch(cmd1);
336 void InsetERT::lfunMouseMotion(FuncRequest const & cmd)
338 if (status_ == Inlined)
339 inset.localDispatch(cmd);
341 InsetCollapsable::localDispatch(cmd);
345 int InsetERT::latex(Buffer const *, ostream & os, bool /*fragile*/,
346 bool /*free_spc*/) const
348 Paragraph * par = inset.paragraph();
351 pos_type siz = par->size();
352 for (pos_type i = 0; i < siz; ++i) {
353 Paragraph::value_type c = par->getChar(i);
355 case Paragraph::META_NEWLINE:
375 int InsetERT::ascii(Buffer const *, ostream &, int /*linelen*/) const
381 int InsetERT::linuxdoc(Buffer const *, ostream & os) const
383 Paragraph * par = inset.paragraph();
386 pos_type siz = par->size();
387 for (pos_type i = 0; i < siz; ++i) {
388 Paragraph::value_type c = par->getChar(i);
390 case Paragraph::META_NEWLINE:
410 int InsetERT::docbook(Buffer const *, ostream & os, bool) const
412 Paragraph * par = inset.paragraph();
415 pos_type siz = par->size();
416 for (pos_type i = 0; i < siz; ++i) {
417 Paragraph::value_type c = par->getChar(i);
419 case Paragraph::META_NEWLINE:
439 Inset::RESULT InsetERT::localDispatch(FuncRequest const & cmd)
441 Inset::RESULT result = DISPATCHED_NOUPDATE;
442 BufferView * bv = cmd.view();
444 if (inset.paragraph()->empty()) {
448 switch (cmd.action) {
449 case LFUN_MOUSE_PRESS:
453 case LFUN_MOUSE_MOTION:
454 lfunMouseMotion(cmd);
457 case LFUN_MOUSE_RELEASE:
458 lfunMouseRelease(cmd);
462 bv->owner()->setLayout(inset.paragraph()->layout()->name());
466 result = InsetCollapsable::localDispatch(cmd);
469 switch (cmd.action) {
470 case LFUN_BREAKPARAGRAPH:
471 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
473 case LFUN_BACKSPACE_SKIP:
475 case LFUN_DELETE_SKIP:
476 case LFUN_DELETE_LINE_FORWARD:
488 string const InsetERT::get_new_label() const
491 pos_type const max_length = 15;
492 pos_type const p_siz = inset.paragraph()->size();
493 pos_type const n = min(max_length, p_siz);
496 for(; i < n && j < p_siz; ++j) {
497 if (inset.paragraph()->isInset(j))
499 la += inset.paragraph()->getChar(j);
502 if (inset.paragraph()->next() || (i > 0 && j < p_siz)) {
512 void InsetERT::setButtonLabel() const
514 if (status_ == Collapsed) {
515 setLabel(get_new_label());
522 bool InsetERT::checkInsertChar(LyXFont & /* font */)
525 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
527 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
528 font.setColor(LColor::latex);
534 int InsetERT::ascent(BufferView * bv, LyXFont const & font) const
537 return InsetCollapsable::ascent(bv, font);
539 return inset.ascent(bv, font);
543 int InsetERT::descent(BufferView * bv, LyXFont const & font) const
546 return InsetCollapsable::descent(bv, font);
548 return inset.descent(bv, font);
552 int InsetERT::width(BufferView * bv, LyXFont const & font) const
555 return InsetCollapsable::width(bv, font);
557 return inset.width(bv, font);
561 void InsetERT::draw(BufferView * bv, LyXFont const & f,
562 int baseline, float & x, bool cleared) const
564 Painter & pain = bv->painter();
566 button_length = width_collapsed();
567 button_top_y = -ascent(bv, f);
568 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
572 draw_collapsed(pain, baseline, x);
579 x += static_cast<float>(scroll());
581 if (!cleared && (inset.need_update == InsetText::FULL ||
582 inset.need_update == InsetText::INIT ||
584 top_baseline != baseline))
586 // we don't need anymore to clear here we just have to tell
587 // the underlying LyXText that it should do the RowClear!
588 inset.setUpdateStatus(bv, InsetText::FULL);
589 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
595 top_baseline = baseline;
597 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
600 inset.draw(bv, f, baseline, x, cleared);
602 draw_collapsed(pain, bl, old_x);
604 bl + descent_collapsed() + inset.ascent(bv, f),
611 void InsetERT::set_latex_font(BufferView * /* bv */)
614 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
616 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
617 font.setColor(LColor::latex);
619 inset.getLyXText(bv)->setFont(bv, font, false);
624 // attention this function can be called with bv == 0
625 void InsetERT::status(BufferView * bv, ERTStatus const st) const
633 inset.setUpdateStatus(bv, InsetText::INIT);
643 bv->unlockInset(const_cast<InsetERT *>(this));
647 bv->updateInset(const_cast<InsetERT *>(this), false);
652 bool InsetERT::showInsetDialog(BufferView * bv) const
654 bv->owner()->getDialogs().showERT(const_cast<InsetERT *>(this));
659 void InsetERT::open(BufferView * bv)
667 void InsetERT::close(BufferView * bv) const
669 if (status_ == Collapsed || status_ == Inlined)
672 status(bv, Collapsed);
677 InsetERT::selectNextWordToSpellcheck(BufferView * bv, float &) const
679 bv->unlockInset(const_cast<InsetERT *>(this));
680 return WordLangTuple();
684 void InsetERT::getDrawFont(LyXFont & font) const
686 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
688 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
689 font.setColor(LColor::latex);
693 int InsetERT::getMaxWidth(BufferView * bv, UpdatableInset const * in) const
695 int w = InsetCollapsable::getMaxWidth(bv, in);
696 if (status_ != Inlined || w < 0)
698 LyXText * text = inset.getLyXText(bv);
699 int rw = text->firstRow()->width();
703 if (!text->firstRow()->next() && rw < w)
709 void InsetERT::update(BufferView * bv, LyXFont const & font,
712 if (inset.need_update & InsetText::INIT ||
713 inset.need_update & InsetText::FULL)
717 InsetCollapsable::update(bv, font, reinit);