1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998 The LyX Team.
8 *======================================================*/
13 #pragma implementation
21 #include "lyx_gui_misc.h"
22 #include "BufferView.h"
26 #include "lyxtextclasslist.h"
28 #include "insets/insettext.h"
30 #include "frontends/Dialogs.h"
31 #include "frontends/Alert.h"
33 #include "support/LOstream.h"
42 labelfont = LyXFont(LyXFont::ALL_SANE);
45 labelfont.setColor(LColor::latex);
50 InsetERT::InsetERT(bool collapsed)
51 : InsetCollapsable(collapsed)
61 InsetERT::InsetERT(InsetERT const & in, bool same_id)
62 : InsetCollapsable(in, same_id), status_(in.status_)
68 Inset * InsetERT::clone(Buffer const &, bool same_id) const
70 return new InsetERT(*const_cast<InsetERT *>(this), same_id);
74 InsetERT::InsetERT(string const & contents, bool collapsed)
75 : InsetCollapsable(collapsed)
82 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
84 LyXFont font(LyXFont::ALL_INHERIT);
86 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
87 font.setColor(LColor::latex);
88 string::const_iterator cit = contents.begin();
89 string::const_iterator end = contents.end();
91 for (; cit != end; ++cit) {
92 inset.paragraph()->insertChar(pos++, *cit, font);
94 // the init has to be after the initialization of the paragraph
95 // because of the label settings (draw_label for ert insets).
100 InsetERT::~InsetERT()
106 void InsetERT::read(Buffer const * buf, LyXLex & lex)
108 bool token_found = false;
111 string const token = lex.getString();
112 if (token == "status") {
114 string const tmp_token = lex.getString();
116 if (tmp_token == "Inlined") {
118 } else if (tmp_token == "Collapsed") {
119 status(0, Collapsed);
121 // leave this as default!
127 lyxerr << "InsetERT::Read: Missing 'status'-tag!"
129 // take countermeasures
130 lex.pushToken(token);
134 #warning this should be really short lived only for compatibility to
135 #warning files written 07/08/2001 so this has to go before 1.2.0! (Jug)
138 string const token = lex.getString();
139 if (token == "collapsed") {
141 collapsed_ = lex.getBool();
143 // Take countermeasures
144 lex.pushToken(token);
148 inset.read(buf, lex);
151 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
153 LyXFont font(LyXFont::ALL_INHERIT);
156 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
157 font.setColor(LColor::latex);
158 Paragraph * par = inset.paragraph();
160 pos_type siz = par->size();
161 for (pos_type i = 0; i < siz; ++i) {
162 par->setFont(i, font);
169 status(0, Collapsed);
178 void InsetERT::write(Buffer const * buf, ostream & os) const
194 os << getInsetName() << "\n"
195 << "status "<< st << "\n";
197 //inset.writeParagraphData(buf, os);
198 string const layout =
199 textclasslist.NameOfLayout(buf->params.textclass, 0);
200 Paragraph * par = inset.paragraph();
202 os << "\n\\layout " << layout << "\n";
203 pos_type siz = par->size();
204 for (pos_type i = 0; i < siz; ++i) {
205 Paragraph::value_type c = par->getChar(i);
207 case Paragraph::META_INSET:
208 case Paragraph::META_HFILL:
209 lyxerr << "Element is not allowed in insertERT"
211 case Paragraph::META_NEWLINE:
212 os << "\n\\newline \n";
215 os << "\n\\backslash \n";
227 string const InsetERT::editMessage() const
229 return _("Opened ERT Inset");
233 bool InsetERT::insertInset(BufferView *, Inset *)
239 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
241 // if selectall is activated then the fontchange was an outside general
242 // fontchange and this messages is not needed
244 Alert::alert(_("Impossible Operation!"),
245 _("Not permitted to change font-types inside ERT-insets!"),
250 void InsetERT::updateStatus(BufferView * bv, bool swap) const
252 if (status_ != Inlined) {
254 status(bv, swap ? Open : Collapsed);
256 status(bv, swap ? Collapsed : Open);
262 void InsetERT::edit(BufferView * bv, int x, int y, unsigned int button)
267 if (status_ == Inlined) {
268 if (!bv->lockInset(this))
270 inset.edit(bv, x, y, button);
272 InsetCollapsable::edit(bv, x, y, button);
279 Inset::EDITABLE InsetERT::editable() const
281 if (status_ == Collapsed)
283 return HIGHLY_EDITABLE;
287 void InsetERT::edit(BufferView * bv, bool front)
289 InsetCollapsable::edit(bv, front);
297 void InsetERT::insetButtonPress(BufferView * bv,
298 int x, int y, int button)
300 if (status_ == Inlined) {
301 inset.insetButtonPress(bv, x, y, button);
303 InsetCollapsable::insetButtonPress(bv, x, y, button);
308 bool InsetERT::insetButtonRelease(BufferView * bv, int x, int y, int button)
315 if (status_ != Inlined && (x >= 0) && (x < button_length) &&
316 (y >= button_top_y) && (y <= button_bottom_y)) {
317 updateStatus(bv, true);
319 LyXFont font(LyXFont::ALL_SANE);
320 int yy = ascent(bv, font) + y - inset.ascent(bv, font);
322 // inlined is special - the text appears above
324 if (status_ == Inlined) {
325 inset.insetButtonRelease(bv, x, yy, button);
326 } else if (!collapsed_ && (y > button_bottom_y)) {
327 yy -= (ascent_collapsed() + descent_collapsed());
328 inset.insetButtonRelease(bv, x, yy, button);
335 void InsetERT::insetMotionNotify(BufferView * bv,
336 int x, int y, int state)
338 if (status_ == Inlined) {
339 inset.insetMotionNotify(bv, x, y, state);
341 InsetCollapsable::insetMotionNotify(bv, x, y, state);
346 int InsetERT::latex(Buffer const *, std::ostream & os, bool /*fragile*/,
347 bool /*free_spc*/) const
349 Paragraph * par = inset.paragraph();
352 pos_type siz = par->size();
353 for (pos_type i = 0; i < siz; ++i) {
354 Paragraph::value_type c = par->getChar(i);
356 case Paragraph::META_NEWLINE:
376 int InsetERT::ascii(Buffer const *,
377 std::ostream &, int /*linelen*/) const
383 int InsetERT::linuxdoc(Buffer const *, std::ostream & os) const
385 Paragraph * par = inset.paragraph();
388 pos_type siz = par->size();
389 for (pos_type i = 0; i < siz; ++i) {
390 Paragraph::value_type c = par->getChar(i);
392 case Paragraph::META_NEWLINE:
412 int InsetERT::docbook(Buffer const *, std::ostream & os) const
414 Paragraph * par = inset.paragraph();
417 pos_type siz = par->size();
418 for (pos_type i = 0; i < siz; ++i) {
419 Paragraph::value_type c = par->getChar(i);
421 case Paragraph::META_NEWLINE:
441 UpdatableInset::RESULT
442 InsetERT::localDispatch(BufferView * bv, kb_action action, string const & arg)
444 UpdatableInset::RESULT result = DISPATCHED_NOUPDATE;
446 if (!inset.paragraph()->size()) {
452 bv->owner()->setLayout(inset.paragraph()->getLayout());
455 result = InsetCollapsable::localDispatch(bv, action, arg);
458 case LFUN_BREAKPARAGRAPH:
459 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
461 case LFUN_BACKSPACE_SKIP:
463 case LFUN_DELETE_SKIP:
464 case LFUN_DELETE_LINE_FORWARD:
476 string const InsetERT::get_new_label() const
479 pos_type const max_length = 15;
480 pos_type const p_siz = inset.paragraph()->size();
481 pos_type const n = std::min(max_length, p_siz);
484 for(; i < n && j < p_siz; ++j) {
485 if (inset.paragraph()->isInset(j))
487 la += inset.paragraph()->getChar(j);
490 if (i > 0 && j < p_siz) {
500 void InsetERT::setButtonLabel() const
502 if (status_ == Collapsed) {
503 setLabel(get_new_label());
510 bool InsetERT::checkInsertChar(LyXFont & font)
513 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
515 LyXFont f(LyXFont::ALL_INHERIT);
518 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
519 font.setColor(LColor::latex);
524 int InsetERT::ascent(BufferView * bv, LyXFont const & font) const
527 return InsetCollapsable::ascent(bv, font);
529 return inset.ascent(bv, font);
533 int InsetERT::descent(BufferView * bv, LyXFont const & font) const
536 return InsetCollapsable::descent(bv, font);
538 return inset.descent(bv, font);
542 int InsetERT::width(BufferView * bv, LyXFont const & font) const
545 return InsetCollapsable::width(bv, font);
547 return inset.width(bv, font);
551 void InsetERT::draw(BufferView * bv, LyXFont const & f,
552 int baseline, float & x, bool cleared) const
554 Painter & pain = bv->painter();
556 button_length = width_collapsed();
557 button_top_y = -ascent(bv, f);
558 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
562 draw_collapsed(pain, baseline, x);
563 x += TEXT_TO_INSET_OFFSET;
570 x += static_cast<float>(scroll());
572 if (!cleared && (inset.need_update == InsetText::FULL ||
573 inset.need_update == InsetText::INIT ||
575 top_baseline != baseline))
577 // we don't need anymore to clear here we just have to tell
578 // the underlying LyXText that it should do the RowClear!
579 inset.setUpdateStatus(bv, InsetText::FULL);
580 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
586 top_baseline = baseline;
588 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
591 inset.draw(bv, f, baseline, x, cleared);
593 draw_collapsed(pain, bl, old_x);
595 bl + descent_collapsed() + inset.ascent(bv, f),
602 void InsetERT::set_latex_font(BufferView * bv)
605 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
607 LyXFont font(LyXFont::ALL_INHERIT);
610 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
611 font.setColor(LColor::latex);
612 inset.getLyXText(bv)->setFont(bv, font, false);
616 void InsetERT::status(BufferView * bv, ERTStatus const st) const
622 inset.setAutoBreakRows(false);
625 inset.setAutoBreakRows(true);
631 inset.setAutoBreakRows(true);
636 bv->unlockInset(const_cast<InsetERT *>(this));
640 bv->updateInset(const_cast<InsetText *>(&inset), false);
645 bool InsetERT::showInsetDialog(BufferView * bv) const
647 bv->owner()->getDialogs()->showERT(const_cast<InsetERT *>(this));
652 void InsetERT::open(BufferView * bv)
660 void InsetERT::close(BufferView * bv) const
664 status(bv, Collapsed);
668 string const InsetERT::selectNextWordToSpellcheck(BufferView * bv,float &) const
670 bv->unlockInset(const_cast<InsetERT *>(this));