1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998-2001 The LyX Team.
8 * ======================================================
14 #pragma implementation
17 #include "insettext.h"
18 #include "paragraph.h"
22 #include "commandtags.h"
25 #include "BufferView.h"
26 #include "lyxtextclasslist.h"
27 #include "LaTeXFeatures.h"
30 #include "lyxcursor.h"
31 #include "CutAndPaste.h"
37 #include "trans_mgr.h"
38 #include "lyxscreen.h"
41 #include "ParagraphParameters.h"
42 #include "undo_funcs.h"
45 #include "frontends/Alert.h"
46 #include "frontends/Dialogs.h"
48 #include "support/textutils.h"
49 #include "support/LAssert.h"
50 #include "support/lstrings.h"
51 #include "support/lyxalgo.h" // lyx::count
68 using lyx::textclass_type;
70 extern unsigned char getCurrentTextClass(Buffer *);
71 extern bool math_insert_greek(BufferView *, char);
72 extern int greek_kb_flag;
75 // These functions should probably go into bufferview_funcs somehow (Jug)
77 void InsetText::saveLyXTextState(LyXText * t) const
79 // check if my paragraphs are still valid
82 if (p == t->cursor.par())
87 if (p && t->cursor.pos() <= p->size()) {
88 sstate.lpar = t->cursor.par();
89 sstate.pos = t->cursor.pos();
90 sstate.boundary = t->cursor.boundary();
91 sstate.selstartpar = t->selection.start.par();
92 sstate.selstartpos = t->selection.start.pos();
93 sstate.selstartboundary = t->selection.start.boundary();
94 sstate.selendpar = t->selection.end.par();
95 sstate.selendpos = t->selection.end.pos();
96 sstate.selendboundary = t->selection.end.boundary();
97 sstate.selection = t->selection.set();
98 sstate.mark_set = t->selection.mark();
99 sstate.refresh = t->refresh_row != 0;
105 void InsetText::restoreLyXTextState(BufferView * bv, LyXText * t) const
108 t->selection.set(true);
109 /* at this point just to avoid the Delete-Empty-Paragraph
110 * Mechanism when setting the cursor */
111 t->selection.mark(sstate.mark_set);
112 if (sstate.selection) {
113 t->setCursor(bv, sstate.selstartpar, sstate.selstartpos,
114 true, sstate.selstartboundary);
115 t->selection.cursor = t->cursor;
116 t->setCursor(bv, sstate.selendpar, sstate.selendpos,
117 true, sstate.selendboundary);
119 t->setCursor(bv, sstate.lpar, sstate.pos);
121 t->setCursor(bv, sstate.lpar, sstate.pos, true, sstate.boundary);
122 t->selection.cursor = t->cursor;
123 t->selection.set(false);
125 if (sstate.refresh) {
131 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
138 InsetText::InsetText(BufferParams const & bp)
139 : UpdatableInset(), lt(0), in_update(false), do_resize(0),
143 par->layout(textclasslist[bp.textclass].defaultLayoutName());
149 InsetText::InsetText(InsetText const & in, bool same_id)
150 : UpdatableInset(in, same_id), lt(0), in_update(false), do_resize(0),
158 InsetText & InsetText::operator=(InsetText const & it)
165 void InsetText::init(InsetText const * ins, bool same_id)
168 setParagraphData(ins->par, same_id);
169 autoBreakRows = ins->autoBreakRows;
170 drawFrame_ = ins->drawFrame_;
171 frame_color = ins->frame_color;
177 p->setInsetOwner(this);
180 the_locking_inset = 0;
182 frame_color = LColor::insetframe;
183 autoBreakRows = false;
197 last_drawn_width = -1;
198 frame_is_visible = false;
201 in_insetAllowed = false;
205 InsetText::~InsetText()
212 Paragraph * tmp = par->next();
219 void InsetText::clear()
221 // This is a gross hack...
222 string old_layout = par->layout();
225 Paragraph * tmp = par->next();
230 par->setInsetOwner(this);
231 par->layout(old_layout);
238 Inset * InsetText::clone(Buffer const &, bool same_id) const
240 return new InsetText(*this, same_id);
244 void InsetText::write(Buffer const * buf, ostream & os) const
247 writeParagraphData(buf, os);
251 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
253 par->writeFile(buf, os, buf->params, 0);
257 void InsetText::read(Buffer const * buf, LyXLex & lex)
261 Paragraph * return_par = 0;
262 Paragraph::depth_type depth = 0;
263 LyXFont font(LyXFont::ALL_INHERIT);
269 token = lex.getString();
272 if (token == "\\end_inset") {
273 #ifndef NO_COMPABILITY
274 const_cast<Buffer*>(buf)->insertErtContents(par, pos, false);
279 if (const_cast<Buffer*>(buf)->
280 parseSingleLyXformat2Token(lex, par, return_par,
281 token, pos, depth, font)) {
282 // the_end read this should NEVER happen
283 lex.printError("\\the_end read in inset! Error in document!");
291 return_par->setInsetOwner(this);
292 return_par = return_par->next();
295 if (token != "\\end_inset") {
296 lex.printError("Missing \\end_inset at this point. "
303 int InsetText::ascent(BufferView * bv, LyXFont const &) const
305 insetAscent = getLyXText(bv)->firstRow()->ascent_of_text() +
306 TEXT_TO_INSET_OFFSET;
311 int InsetText::descent(BufferView * bv, LyXFont const &) const
313 LyXText * llt = getLyXText(bv);
314 insetDescent = llt->height - llt->firstRow()->ascent_of_text() +
315 TEXT_TO_INSET_OFFSET;
320 int InsetText::width(BufferView * bv, LyXFont const &) const
322 insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
323 (2 * TEXT_TO_INSET_OFFSET);
324 insetWidth = max(insetWidth, 10);
329 int InsetText::textWidth(BufferView * bv, bool fordraw) const
332 if (!autoBreakRows) {
335 w = getMaxWidth(bv, this);
338 return max(w - (2 * TEXT_TO_INSET_OFFSET),
339 (int)getLyXText(bv)->width);
343 return w - (2 * TEXT_TO_INSET_OFFSET);
347 void InsetText::draw(BufferView * bv, LyXFont const & f,
348 int baseline, float & x, bool cleared) const
353 Painter & pain = bv->painter();
355 // this is the first thing we have to ask because if the x pos
356 // changed we have to do a complete rebreak of the text as we
357 // may have few space to draw in. Well we should check on this too
359 if (top_x != int(x)) {
362 int nw = getMaxWidth(bv, this);
363 if (nw > 0 && old_max_width != nw) {
366 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
371 // call these methods so that insetWidth, insetAscent and
372 // insetDescent have the right values.
377 // repaint the background if needed
378 if (cleared && backgroundColor() != LColor::background) {
379 clearInset(bv, baseline, cleared);
382 // no draw is necessary !!!
383 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
384 top_baseline = baseline;
386 if (need_update & CLEAR_FRAME)
387 clearFrame(pain, cleared);
394 x += static_cast<float>(scroll());
396 // if top_x differs we did it already
397 if (!cleared && (old_x == int(x))
398 && ((need_update&(INIT|FULL)) || (top_baseline != baseline)
399 ||(last_drawn_width != insetWidth)))
401 // Condition necessary to eliminate bug 59 attachment 37
403 clearInset(bv, baseline, cleared);
407 frame_is_visible = false;
409 if (!cleared && (need_update == NONE)) {
411 drawFrame(pain, cleared);
415 top_baseline = baseline;
416 top_y = baseline - insetAscent;
418 if (last_drawn_width != insetWidth) {
420 clearInset(bv, baseline, cleared);
422 last_drawn_width = insetWidth;
425 if (the_locking_inset && (cpar(bv) == inset_par)
426 && (cpos(bv) == inset_pos)) {
427 inset_x = cix(bv) - top_x + drawTextXOffset;
428 inset_y = ciy(bv) + drawTextYOffset;
430 if (!cleared && (need_update == CURSOR)
431 && !getLyXText(bv)->selection.set()) {
432 drawFrame(pain, cleared);
442 x += TEXT_TO_INSET_OFFSET;
444 Row * row = lt->firstRow();
445 int y_offset = baseline - row->ascent_of_text();
446 int ph = pain.paperHeight();
449 while ((row != 0) && ((y+row->height()) <= 0)) {
451 first += row->height();
455 lt->first_y = -y_offset;
462 if (cleared || (need_update&(INIT|FULL))) {
463 int yf = y_offset + first;
465 while ((row != 0) && (yf < ph)) {
466 lt->getVisibleRow(bv, y+y_offset+first, int(x), row,
467 y+lt->first_y, cleared);
472 } else if (!locked) {
473 if (need_update & CURSOR) {
474 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
475 lt->clearSelection();
476 lt->selection.cursor = lt->cursor;
478 bv->screen()->update(lt, bv, y_offset, int(x));
481 if (need_update & SELECTION) {
482 bv->screen()->toggleToggle(lt, bv, y_offset, int(x));
483 } else if (need_update & CURSOR) {
484 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
485 lt->clearSelection();
486 lt->selection.cursor = lt->cursor;
488 bv->screen()->update(lt, bv, y_offset, int(x));
493 lt->status(bv, LyXText::UNCHANGED);
494 if ((drawFrame_ == ALWAYS) ||
495 ((cleared || (need_update != CURSOR_PAR)) &&
496 (drawFrame_ == LOCKED) && locked))
498 drawFrame(pain, cleared);
499 } else if (need_update & CLEAR_FRAME) {
500 clearFrame(pain, cleared);
503 x += insetWidth - TEXT_TO_INSET_OFFSET;
505 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
507 } else if (need_update != INIT) {
515 void InsetText::drawFrame(Painter & pain, bool cleared) const
517 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
518 if (!frame_is_visible || cleared) {
519 frame_x = top_x + ttoD2;
520 frame_y = top_baseline - insetAscent + ttoD2;
521 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
522 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
523 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
525 frame_is_visible = true;
530 void InsetText::clearFrame(Painter & pain, bool cleared) const
532 if (frame_is_visible) {
534 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
537 frame_is_visible = false;
542 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
545 if (reinit && owner()) {
547 owner()->update(bv, font, true);
552 if (reinit || need_update == INIT) {
554 // we should put this call where we set need_update to INIT!
557 owner()->update(bv, font, true);
562 if (!autoBreakRows && par->next())
563 collapseParagraphs(bv);
565 if (the_locking_inset) {
566 inset_x = cix(bv) - top_x + drawTextXOffset;
567 inset_y = ciy(bv) + drawTextYOffset;
568 the_locking_inset->update(bv, font, reinit);
576 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
579 lt->updateInset(bv, the_locking_inset);
581 if (lt->status() == LyXText::NEED_MORE_REFRESH)
589 void InsetText::setUpdateStatus(BufferView * bv, int what) const
591 // this does nothing dangerous so use only a localized buffer
592 LyXText * llt = getLyXText(bv);
595 // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
596 // if we don't break row so that we only have one row to update!
597 if ((llt->status() == LyXText::NEED_MORE_REFRESH) ||
599 (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
602 } else if (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
603 need_update |= CURSOR_PAR;
606 // this to not draw a selection when we redraw all of it!
607 if (need_update & CURSOR && !(need_update & SELECTION)) {
608 if (llt->selection.set())
610 llt->clearSelection();
615 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
617 if (!autoBreakRows && par->next())
618 collapseParagraphs(bv);
625 setUpdateStatus(bv, what);
626 bool flag = mark_dirty ||
627 (((need_update != CURSOR) && (need_update != NONE)) ||
628 (lt->status() != LyXText::UNCHANGED) || lt->selection.set());
629 if (!lt->selection.set())
630 lt->selection.cursor = lt->cursor;
634 // IMO this is not anymore needed as we do this in fitInsetCursor!
635 // and we always get "true" as returnvalue of this function in the
636 // case of a locking inset (Jug 20020412)
637 if (locked && (need_update & CURSOR) && bv->fitCursor())
643 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
645 if (need_update == CURSOR)
647 bv->owner()->showState();
648 bv->owner()->updateMenubar();
649 bv->owner()->updateToolbar();
650 if (old_par != cpar(bv)) {
651 bv->owner()->setLayout(cpar(bv)->layout());
657 string const InsetText::editMessage() const
659 return _("Opened Text Inset");
663 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
665 UpdatableInset::edit(bv, x, y, button);
667 if (!bv->lockInset(this)) {
668 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
672 the_locking_inset = 0;
673 inset_pos = inset_x = inset_y = 0;
674 inset_boundary = false;
677 int tmp_y = (y < 0) ? 0 : y;
683 if (!checkAndActivateInset(bv, x, tmp_y, button)) {
684 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
686 lt->cursor.x_fix(lt->cursor.x());
688 lt->clearSelection();
690 // If the inset is empty set the language of the current font to the
691 // language to the surronding text (if different).
692 if (par->size() == 0 && !par->next() &&
693 bv->getParentLanguage(this) != lt->current_font.language())
695 LyXFont font(LyXFont::ALL_IGNORE);
696 font.setLanguage(bv->getParentLanguage(this));
697 setFont(bv, font, false);
703 if (drawFrame_ == LOCKED)
704 code = CURSOR|DRAW_FRAME;
705 updateLocal(bv, code, false);
708 // Tell the paragraph dialog that we've entered an insettext.
709 bv->owner()->getDialogs()->updateParagraph();
713 void InsetText::edit(BufferView * bv, bool front)
715 UpdatableInset::edit(bv, front);
717 if (!bv->lockInset(this)) {
718 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
722 the_locking_inset = 0;
723 inset_pos = inset_x = inset_y = 0;
724 inset_boundary = false;
733 lt->setCursor(bv, par, 0);
738 // int const pos = (p->size() ? p->size()-1 : p->size());
739 lt->setCursor(bv, p, p->size());
741 lt->clearSelection();
743 // If the inset is empty set the language of the current font to the
744 // language to the surronding text (if different).
745 if (par->size() == 0 && !par->next() &&
746 bv->getParentLanguage(this) != lt->current_font.language()) {
747 LyXFont font(LyXFont::ALL_IGNORE);
748 font.setLanguage(bv->getParentLanguage(this));
749 setFont(bv, font, false);
754 if (drawFrame_ == LOCKED)
755 code = CURSOR|DRAW_FRAME;
756 updateLocal(bv, code, false);
761 void InsetText::insetUnlock(BufferView * bv)
763 if (the_locking_inset) {
764 the_locking_inset->insetUnlock(bv);
765 the_locking_inset = 0;
766 updateLocal(bv, CURSOR_PAR, false);
773 if (drawFrame_ == LOCKED)
774 code = CURSOR|CLEAR_FRAME;
778 if (drawFrame_ == LOCKED)
786 if (lt->selection.set()) {
787 lt->clearSelection();
789 } else if (owner()) {
790 bv->owner()->setLayout(owner()->getLyXText(bv)
791 ->cursor.par()->layout());
793 bv->owner()->setLayout(bv->text->cursor.par()->layout());
794 // hack for deleteEmptyParMech
796 lt->setCursor(bv, par, 0);
797 } else if (par->next()) {
798 lt->setCursor(bv, par->next(), 0);
803 updateLocal(bv, code, false);
805 setUpdateStatus(bv, code);
810 void InsetText::lockInset(BufferView * bv)
813 the_locking_inset = 0;
814 inset_pos = inset_x = inset_y = 0;
815 inset_boundary = false;
823 lt->setCursor(bv, par, 0);
824 lt->clearSelection();
826 // If the inset is empty set the language of the current font to the
827 // language to the surronding text (if different).
828 if (par->size() == 0 && !par->next() &&
829 bv->getParentLanguage(this) != lt->current_font.language()) {
830 LyXFont font(LyXFont::ALL_IGNORE);
831 font.setLanguage(bv->getParentLanguage(this));
832 setFont(bv, font, false);
837 if (drawFrame_ == LOCKED)
838 code = CURSOR|DRAW_FRAME;
839 setUpdateStatus(bv, code);
843 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
845 the_locking_inset = inset;
846 inset_x = cix(bv) - top_x + drawTextXOffset;
847 inset_y = ciy(bv) + drawTextYOffset;
848 inset_pos = cpos(bv);
849 inset_par = cpar(bv);
850 inset_boundary = cboundary(bv);
851 updateLocal(bv, CURSOR, false);
855 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
857 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
861 if (!the_locking_inset) {
863 int const id = inset->id();
865 Paragraph::inset_iterator it =
866 p->inset_iterator_begin();
867 Paragraph::inset_iterator const end =
868 p->inset_iterator_end();
869 for (; it != end; ++it) {
870 if ((*it) == inset) {
871 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
872 lockInset(bv, inset);
875 if ((*it)->getInsetFromID(id)) {
876 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
878 return the_locking_inset->lockInsetInInset(bv, inset);
885 if (inset == cpar(bv)->getInset(cpos(bv))) {
886 lyxerr[Debug::INSETS] << "OK" << endl;
887 lockInset(bv, inset);
889 } else if (the_locking_inset && (the_locking_inset == inset)) {
890 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
891 lyxerr[Debug::INSETS] << "OK" << endl;
892 inset_x = cix(bv) - top_x + drawTextXOffset;
893 inset_y = ciy(bv) + drawTextYOffset;
895 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
897 } else if (the_locking_inset) {
898 lyxerr[Debug::INSETS] << "MAYBE" << endl;
899 return the_locking_inset->lockInsetInInset(bv, inset);
901 lyxerr[Debug::INSETS] << "NOT OK" << endl;
906 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
909 if (!the_locking_inset)
911 if (the_locking_inset == inset) {
912 the_locking_inset->insetUnlock(bv);
913 getLyXText(bv)->updateInset(bv, inset);
914 the_locking_inset = 0;
916 moveRight(bv, false);
917 old_par = 0; // force layout setting
921 updateLocal(bv, CURSOR, false);
924 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
928 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
930 if (!autoBreakRows && par->next())
931 collapseParagraphs(bv);
939 if (inset->owner() != this) {
940 int ustat = CURSOR_PAR;
942 UpdatableInset * tl_inset = the_locking_inset;
944 found = tl_inset->updateInsetInInset(bv, inset);
946 tl_inset = static_cast<UpdatableInset *>(inset);
947 while(tl_inset->owner() && tl_inset->owner() != this)
948 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
949 if (!tl_inset->owner())
951 found = tl_inset->updateInsetInInset(bv, inset);
955 lt->updateInset(bv, tl_inset);
959 setUpdateStatus(bv, ustat);
962 bool found = lt->updateInset(bv, inset);
966 setUpdateStatus(bv, CURSOR_PAR);
967 if (the_locking_inset &&
968 cpar(bv) == inset_par && cpos(bv) == inset_pos)
970 inset_x = cix(bv) - top_x + drawTextXOffset;
971 inset_y = ciy(bv) + drawTextYOffset;
978 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
982 // use this to check mouse motion for selection!
989 int tmp_x = x - drawTextXOffset;
990 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
991 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
994 if (the_locking_inset) {
995 if (the_locking_inset == inset) {
996 the_locking_inset->insetButtonPress(bv,
1004 // otherwise unlock the_locking_inset and lock the new inset
1005 the_locking_inset->insetUnlock(bv);
1006 inset_x = cix(bv) - top_x + drawTextXOffset;
1007 inset_y = ciy(bv) + drawTextYOffset;
1008 the_locking_inset = 0;
1009 inset->insetButtonPress(bv, x - inset_x,
1010 y - inset_y, button);
1011 // inset->edit(bv, x - inset_x, y - inset_y, button);
1012 if (the_locking_inset)
1013 updateLocal(bv, CURSOR, false);
1017 // otherwise only unlock the_locking_inset
1018 the_locking_inset->insetUnlock(bv);
1019 the_locking_inset = 0;
1022 no_selection = false;
1024 if (bv->theLockingInset()) {
1025 if (isHighlyEditableInset(inset)) {
1026 // We just have to lock the inset before calling a
1027 // PressEvent on it!
1028 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
1029 if (!bv->lockInset(uinset)) {
1030 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1032 inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
1033 if (the_locking_inset)
1034 updateLocal(bv, CURSOR, false);
1038 if (!inset) { // && (button == 2)) {
1039 bool paste_internally = false;
1040 if ((button == 2) && getLyXText(bv)->selection.set()) {
1041 localDispatch(bv, LFUN_COPY, "");
1042 paste_internally = true;
1046 lt = getLyXText(bv);
1049 int old_first_y = lt->first_y;
1051 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
1053 // set the selection cursor!
1054 lt->selection.cursor = lt->cursor;
1055 lt->cursor.x_fix(lt->cursor.x());
1057 if (lt->selection.set()) {
1058 lt->clearSelection();
1061 updateLocal(bv, FULL, false);
1063 lt->clearSelection();
1066 updateLocal(bv, CURSOR, false);
1068 bv->owner()->setLayout(cpar(bv)->layout());
1069 // we moved the view we cannot do mouse selection in this case!
1070 if (getLyXText(bv)->first_y != old_first_y)
1071 no_selection = true;
1073 // Insert primary selection with middle mouse
1074 // if there is a local selection in the current buffer,
1077 if (paste_internally)
1078 localDispatch(bv, LFUN_PASTE, "");
1080 localDispatch(bv, LFUN_PASTESELECTION,
1084 getLyXText(bv)->clearSelection();
1086 showInsetCursor(bv);
1090 bool InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
1092 no_selection = true;
1093 if (the_locking_inset) {
1094 return the_locking_inset->insetButtonRelease(bv,
1095 x - inset_x, y - inset_y,
1098 int tmp_x = x - drawTextXOffset;
1099 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
1100 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1103 if (isHighlyEditableInset(inset)) {
1104 ret = inset->insetButtonRelease(bv, x - inset_x,
1105 y - inset_y, button);
1107 inset_x = cix(bv) - top_x + drawTextXOffset;
1108 inset_y = ciy(bv) + drawTextYOffset;
1109 ret = inset->insetButtonRelease(bv, x - inset_x,
1110 y - inset_y, button);
1111 inset->edit(bv, x - inset_x,
1112 y - inset_y, button);
1114 updateLocal(bv, CURSOR_PAR, false);
1120 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
1122 if (the_locking_inset) {
1123 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1128 if (no_selection || ((mouse_x == x) && (mouse_y == y)))
1133 lt = getLyXText(bv);
1136 hideInsetCursor(bv);
1137 LyXCursor cur = lt->cursor;
1138 lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1139 lt->cursor.x_fix(lt->cursor.x());
1140 if (cur == lt->cursor) {
1145 lt->setSelection(bv);
1146 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1147 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1151 updateLocal(bv, SELECTION, false);
1153 showInsetCursor(bv);
1157 void InsetText::insetKeyPress(XKeyEvent * xke)
1159 if (the_locking_inset) {
1160 the_locking_inset->insetKeyPress(xke);
1166 UpdatableInset::RESULT
1167 InsetText::localDispatch(BufferView * bv,
1168 kb_action action, string const & arg)
1170 bool was_empty = par->size() == 0 && !par->next();
1171 no_selection = false;
1172 UpdatableInset::RESULT
1173 result= UpdatableInset::localDispatch(bv, action, arg);
1174 if (result != UNDISPATCHED) {
1178 result = DISPATCHED;
1179 if ((action < 0) && arg.empty())
1182 if (the_locking_inset) {
1183 result = the_locking_inset->localDispatch(bv, action, arg);
1184 if (result == DISPATCHED_NOUPDATE)
1186 else if (result == DISPATCHED) {
1187 updateLocal(bv, CURSOR_PAR, false);
1189 } else if (result >= FINISHED) {
1191 case FINISHED_RIGHT:
1192 moveRightIntern(bv, false, false);
1193 result = DISPATCHED;
1196 if ((result = moveUp(bv)) >= FINISHED) {
1197 updateLocal(bv, CURSOR, false);
1198 bv->unlockInset(this);
1202 if ((result = moveDown(bv)) >= FINISHED) {
1203 updateLocal(bv, CURSOR, false);
1204 bv->unlockInset(this);
1208 result = DISPATCHED;
1211 the_locking_inset = 0;
1212 #ifdef WITH_WARNINGS
1213 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
1215 updateLocal(bv, CURSOR, false);
1219 hideInsetCursor(bv);
1222 lt = getLyXText(bv);
1226 int updflag = false;
1229 case LFUN_SELFINSERT:
1230 if (bv->buffer()->isReadonly()) {
1231 // setErrorMessage(N_("Document is read only"));
1235 /* Automatically delete the currently selected
1236 * text and replace it with what is being
1237 * typed in now. Depends on lyxrc settings
1238 * "auto_region_delete", which defaults to
1241 setUndo(bv, Undo::INSERT,
1242 lt->cursor.par(), lt->cursor.par()->next());
1244 if (lyxrc.auto_region_delete) {
1245 if (lt->selection.set()) {
1246 lt->cutSelection(bv, false);
1249 lt->clearSelection();
1250 for (string::size_type i = 0; i < arg.length(); ++i) {
1251 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1254 lt->selection.cursor = lt->cursor;
1255 updwhat = CURSOR | CURSOR_PAR;
1257 result = DISPATCHED_NOUPDATE;
1259 // --- Cursor Movements -----------------------------------
1262 moveRight(bv, false, true);
1263 lt->setSelection(bv);
1264 updwhat = SELECTION;
1267 result = moveRight(bv);
1273 moveLeft(bv, false, true);
1274 lt->setSelection(bv);
1275 updwhat = SELECTION;
1279 result = moveLeft(bv);
1285 lt->setSelection(bv);
1286 updwhat = SELECTION;
1290 result = moveDown(bv);
1296 lt->setSelection(bv);
1297 updwhat = SELECTION;
1301 result = moveUp(bv);
1313 case LFUN_BACKSPACE: {
1314 setUndo(bv, Undo::DELETE,
1315 lt->cursor.par(), lt->cursor.par()->next());
1316 if (lt->selection.set())
1317 lt->cutSelection(bv);
1320 updwhat = CURSOR_PAR;
1326 setUndo(bv, Undo::DELETE,
1327 lt->cursor.par(), lt->cursor.par()->next());
1328 if (lt->selection.set()) {
1329 lt->cutSelection(bv);
1333 updwhat = CURSOR_PAR;
1339 setUndo(bv, Undo::DELETE,
1340 lt->cursor.par(), lt->cursor.par()->next());
1341 lt->cutSelection(bv);
1342 updwhat = CURSOR_PAR;
1349 lt->copySelection(bv);
1350 updwhat = CURSOR_PAR;
1352 case LFUN_PASTESELECTION:
1354 string const clip(bv->getClipboard());
1358 if (arg == "paragraph") {
1359 lt->insertStringAsParagraphs(bv, clip);
1361 lt->insertStringAsLines(bv, clip);
1363 updwhat = CURSOR_PAR;
1368 if (!autoBreakRows) {
1370 if (CutAndPaste::nrOfParagraphs() > 1) {
1371 Alert::alert(_("Impossible operation"),
1372 _("Cannot include more than one paragraph!"),
1377 setUndo(bv, Undo::INSERT,
1378 lt->cursor.par(), lt->cursor.par()->next());
1379 lt->pasteSelection(bv);
1380 updwhat = CURSOR_PAR;
1385 case LFUN_BREAKPARAGRAPH:
1386 if (!autoBreakRows) {
1387 result = DISPATCHED;
1390 lt->breakParagraph(bv, 0);
1391 updwhat = CURSOR | FULL;
1394 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1395 if (!autoBreakRows) {
1396 result = DISPATCHED;
1399 lt->breakParagraph(bv, 1);
1400 updwhat = CURSOR | FULL;
1404 case LFUN_BREAKLINE: {
1405 if (!autoBreakRows) {
1406 result = DISPATCHED;
1409 setUndo(bv, Undo::INSERT,
1410 lt->cursor.par(), lt->cursor.par()->next());
1411 lt->insertChar(bv, Paragraph::META_NEWLINE);
1412 updwhat = CURSOR | CURSOR_PAR;
1418 // do not set layouts on non breakable textinsets
1419 if (autoBreakRows) {
1420 string cur_layout = cpar(bv)->layout();
1422 // Derive layout number from given argument (string)
1423 // and current buffer's textclass (number). */
1424 textclass_type tclass = bv->buffer()->params.textclass;
1425 string layout = arg;
1426 bool hasLayout = textclasslist[tclass].hasLayout(layout);
1428 // If the entry is obsolete, use the new one instead.
1430 string const & obs =
1431 textclasslist[tclass][layout].
1437 // see if we found the layout number:
1439 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1440 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1444 if (cur_layout != layout) {
1445 cur_layout = layout;
1446 lt->setLayout(bv, layout);
1447 bv->owner()->setLayout(cpar(bv)->layout());
1448 updwhat = CURSOR_PAR;
1452 // reset the layout box
1453 bv->owner()->setLayout(cpar(bv)->layout());
1456 case LFUN_PARAGRAPH_SPACING:
1457 // This one is absolutely not working. When fiddling with this
1458 // it also seems to me that the paragraphs inside the insettext
1459 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1461 Paragraph * par = lt->cursor.par();
1462 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1463 float cur_value = 1.0;
1464 if (cur_spacing == Spacing::Other) {
1465 cur_value = par->params().spacing().getValue();
1468 istringstream istr(arg.c_str());
1471 Spacing::Space new_spacing = cur_spacing;
1472 float new_value = cur_value;
1474 lyxerr << "Missing argument to `paragraph-spacing'"
1476 } else if (tmp == "single") {
1477 new_spacing = Spacing::Single;
1478 } else if (tmp == "onehalf") {
1479 new_spacing = Spacing::Onehalf;
1480 } else if (tmp == "double") {
1481 new_spacing = Spacing::Double;
1482 } else if (tmp == "other") {
1483 new_spacing = Spacing::Other;
1486 lyxerr << "new_value = " << tmpval << endl;
1489 } else if (tmp == "default") {
1490 new_spacing = Spacing::Default;
1492 lyxerr << _("Unknown spacing argument: ")
1495 if (cur_spacing != new_spacing || cur_value != new_value) {
1496 par->params().spacing(Spacing(new_spacing, new_value));
1497 updwhat = CURSOR_PAR;
1504 if (!bv->Dispatch(action, arg))
1505 result = UNDISPATCHED;
1512 updateLocal(bv, updwhat, updflag);
1513 /// If the action has deleted all text in the inset, we need to change the
1514 // language to the language of the surronding text.
1515 if (!was_empty && par->size() == 0 && !par->next()) {
1516 LyXFont font(LyXFont::ALL_IGNORE);
1517 font.setLanguage(bv->getParentLanguage(this));
1518 setFont(bv, font, false);
1521 if (result >= FINISHED)
1522 bv->unlockInset(this);
1524 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1525 result = DISPATCHED;
1530 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1533 buf->latexParagraphs(os, par, 0, texrow);
1534 return texrow.rows();
1538 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1540 Paragraph * p = par;
1541 unsigned int lines = 0;
1544 string const tmp = buf->asciiParagraph(p, linelen, p->previous()==0);
1545 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1553 int InsetText::docbook(Buffer const * buf, ostream & os) const
1555 Paragraph * p = par;
1556 unsigned int lines = 0;
1558 vector<string> environment_stack(10);
1559 vector<string> environment_inner(10);
1561 int const command_depth = 0;
1564 Paragraph::depth_type depth = 0; // paragraph depth
1568 int desc_on = 0; // description mode
1570 LyXLayout const & style =
1571 textclasslist[buf->params.textclass][p->layout()];
1573 // environment tag closing
1574 for (; depth > p->params().depth(); --depth) {
1575 if (environment_inner[depth] != "!-- --") {
1576 item_name = "listitem";
1577 buf->sgmlCloseTag(os, command_depth + depth,
1579 if (environment_inner[depth] == "varlistentry")
1580 buf->sgmlCloseTag(os, depth+command_depth,
1581 environment_inner[depth]);
1583 buf->sgmlCloseTag(os, depth + command_depth,
1584 environment_stack[depth]);
1585 environment_stack[depth].erase();
1586 environment_inner[depth].erase();
1589 if (depth == p->params().depth()
1590 && environment_stack[depth] != style.latexname()
1591 && !environment_stack[depth].empty()) {
1592 if (environment_inner[depth] != "!-- --") {
1593 item_name= "listitem";
1594 buf->sgmlCloseTag(os, command_depth+depth,
1596 if (environment_inner[depth] == "varlistentry")
1597 buf->sgmlCloseTag(os,
1598 depth + command_depth,
1599 environment_inner[depth]);
1602 buf->sgmlCloseTag(os, depth + command_depth,
1603 environment_stack[depth]);
1605 environment_stack[depth].erase();
1606 environment_inner[depth].erase();
1609 // Write opening SGML tags.
1610 switch (style.latextype) {
1611 case LATEX_PARAGRAPH:
1612 buf->sgmlOpenTag(os, depth + command_depth,
1617 buf->sgmlError(p, 0,
1618 _("Error : LatexType Command not allowed here.\n"));
1622 case LATEX_ENVIRONMENT:
1623 case LATEX_ITEM_ENVIRONMENT:
1624 if (depth < p->params().depth()) {
1625 depth = p->params().depth();
1626 environment_stack[depth].erase();
1629 if (environment_stack[depth] != style.latexname()) {
1630 if (environment_stack.size() == depth + 1) {
1631 environment_stack.push_back("!-- --");
1632 environment_inner.push_back("!-- --");
1634 environment_stack[depth] = style.latexname();
1635 environment_inner[depth] = "!-- --";
1636 buf->sgmlOpenTag(os, depth + command_depth,
1637 environment_stack[depth]);
1639 if (environment_inner[depth] != "!-- --") {
1640 item_name= "listitem";
1641 buf->sgmlCloseTag(os,
1642 command_depth + depth,
1644 if (environment_inner[depth] == "varlistentry")
1645 buf->sgmlCloseTag(os,
1646 depth + command_depth,
1647 environment_inner[depth]);
1651 if (style.latextype == LATEX_ENVIRONMENT) {
1652 if (!style.latexparam().empty()) {
1653 if (style.latexparam() == "CDATA")
1656 buf->sgmlOpenTag(os, depth + command_depth,
1657 style.latexparam());
1662 desc_on = (style.labeltype == LABEL_MANUAL);
1665 environment_inner[depth]= "varlistentry";
1667 environment_inner[depth]= "listitem";
1669 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1670 environment_inner[depth]);
1674 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1678 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1683 buf->sgmlOpenTag(os, depth + command_depth,
1688 buf->simpleDocBookOnePar(os, p, desc_on,
1689 depth + 1 + command_depth);
1693 // write closing SGML tags
1694 switch (style.latextype) {
1695 case LATEX_ENVIRONMENT:
1696 if (!style.latexparam().empty()) {
1697 if (style.latexparam() == "CDATA")
1700 buf->sgmlCloseTag(os, depth + command_depth,
1701 style.latexparam());
1704 case LATEX_ITEM_ENVIRONMENT:
1705 if (desc_on == 1) break;
1707 buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1709 case LATEX_PARAGRAPH:
1710 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1713 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1719 for (int d = depth; d >= 0; --d) {
1720 if (!environment_stack[depth].empty()) {
1721 if (environment_inner[depth] != "!-- --") {
1722 item_name = "listitem";
1723 buf->sgmlCloseTag(os, command_depth + depth,
1725 if (environment_inner[depth] == "varlistentry")
1726 buf->sgmlCloseTag(os, depth + command_depth,
1727 environment_inner[depth]);
1730 buf->sgmlCloseTag(os, depth + command_depth,
1731 environment_stack[depth]);
1739 void InsetText::validate(LaTeXFeatures & features) const
1741 Paragraph * p = par;
1743 p->validate(features);
1749 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1751 if (textclasslist[buf->params.textclass][p->layout()].labeltype != LABEL_MANUAL)
1754 return p->beginningOfMainBody();
1758 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1760 if (the_locking_inset) {
1761 the_locking_inset->getCursorPos(bv, x, y);
1764 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1765 y = cy(bv) - TEXT_TO_INSET_OFFSET;
1769 int InsetText::insetInInsetY() const
1771 if (!the_locking_inset)
1774 return (inset_y + the_locking_inset->insetInInsetY());
1778 void InsetText::toggleInsetCursor(BufferView * bv)
1780 if (the_locking_inset) {
1781 the_locking_inset->toggleInsetCursor(bv);
1785 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1787 int const asc = lyxfont::maxAscent(font);
1788 int const desc = lyxfont::maxDescent(font);
1790 if (isCursorVisible())
1791 bv->hideLockedInsetCursor();
1793 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1794 toggleCursorVisible();
1798 void InsetText::showInsetCursor(BufferView * bv, bool show)
1800 if (the_locking_inset) {
1801 the_locking_inset->showInsetCursor(bv, show);
1804 if (!isCursorVisible()) {
1805 LyXFont const font =
1806 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1808 int const asc = lyxfont::maxAscent(font);
1809 int const desc = lyxfont::maxDescent(font);
1811 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1813 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1814 setCursorVisible(true);
1819 void InsetText::hideInsetCursor(BufferView * bv)
1821 if (isCursorVisible()) {
1822 bv->hideLockedInsetCursor();
1823 setCursorVisible(false);
1825 if (the_locking_inset)
1826 the_locking_inset->hideInsetCursor(bv);
1830 void InsetText::fitInsetCursor(BufferView * bv) const
1832 if (the_locking_inset) {
1833 the_locking_inset->fitInsetCursor(bv);
1836 LyXFont const font =
1837 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1839 int const asc = lyxfont::maxAscent(font);
1840 int const desc = lyxfont::maxDescent(font);
1842 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1843 need_update |= FULL;
1847 UpdatableInset::RESULT
1848 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1850 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1851 return moveLeftIntern(bv, true, activate_inset, selecting);
1853 return moveRightIntern(bv, true, activate_inset, selecting);
1857 UpdatableInset::RESULT
1858 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1860 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1861 return moveRightIntern(bv, false, activate_inset, selecting);
1863 return moveLeftIntern(bv, false, activate_inset, selecting);
1867 UpdatableInset::RESULT
1868 InsetText::moveRightIntern(BufferView * bv, bool front,
1869 bool activate_inset, bool selecting)
1871 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1872 return FINISHED_RIGHT;
1873 if (activate_inset && checkAndActivateInset(bv, front))
1875 getLyXText(bv)->cursorRight(bv);
1877 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1878 return DISPATCHED_NOUPDATE;
1882 UpdatableInset::RESULT
1883 InsetText::moveLeftIntern(BufferView * bv, bool front,
1884 bool activate_inset, bool selecting)
1886 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1888 getLyXText(bv)->cursorLeft(bv);
1890 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1891 if (activate_inset && checkAndActivateInset(bv, front))
1893 return DISPATCHED_NOUPDATE;
1897 UpdatableInset::RESULT
1898 InsetText::moveUp(BufferView * bv)
1900 if (!crow(bv)->previous())
1902 getLyXText(bv)->cursorUp(bv);
1903 return DISPATCHED_NOUPDATE;
1907 UpdatableInset::RESULT
1908 InsetText::moveDown(BufferView * bv)
1910 if (!crow(bv)->next())
1911 return FINISHED_DOWN;
1912 getLyXText(bv)->cursorDown(bv);
1913 return DISPATCHED_NOUPDATE;
1917 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1919 if (the_locking_inset) {
1920 if (the_locking_inset->insetAllowed(inset))
1921 return the_locking_inset->insertInset(bv, inset);
1924 inset->setOwner(this);
1925 hideInsetCursor(bv);
1926 getLyXText(bv)->insertInset(bv, inset);
1928 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1933 bool InsetText::insetAllowed(Inset::Code code) const
1935 // in_insetAllowed is a really gross hack,
1936 // to allow us to call the owner's insetAllowed
1937 // without stack overflow, which can happen
1938 // when the owner uses InsetCollapsable::insetAllowed()
1940 if (in_insetAllowed)
1942 in_insetAllowed = true;
1943 if (the_locking_inset)
1944 ret = the_locking_inset->insetAllowed(code);
1946 ret = owner()->insetAllowed(code);
1947 in_insetAllowed = false;
1952 UpdatableInset * InsetText::getLockingInset() const
1954 return the_locking_inset ? the_locking_inset->getLockingInset() :
1955 const_cast<InsetText *>(this);
1959 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1963 if (the_locking_inset)
1964 return the_locking_inset->getFirstLockingInsetOfType(c);
1969 bool InsetText::showInsetDialog(BufferView * bv) const
1971 if (the_locking_inset)
1972 return the_locking_inset->showInsetDialog(bv);
1977 vector<string> const InsetText::getLabelList() const
1979 vector<string> label_list;
1981 Paragraph * tpar = par;
1983 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1984 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1985 for (; beg != end; ++beg) {
1986 vector<string> const l = (*beg)->getLabelList();
1987 label_list.insert(label_list.end(), l.begin(), l.end());
1989 tpar = tpar->next();
1995 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1998 if (the_locking_inset) {
1999 the_locking_inset->setFont(bv, font, toggleall, selectall);
2002 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
2003 getLyXText(bv)->setFont(bv, font, toggleall);
2008 lt = getLyXText(bv);
2011 if (lt->selection.set()) {
2012 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
2016 lt->toggleFree(bv, font, toggleall);
2018 lt->clearSelection();
2020 bool flag = (selectall || lt->selection.set());
2024 updateLocal(bv, FULL, true);
2026 updateLocal(bv, CURSOR_PAR, true);
2030 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
2032 if (cpar(bv)->isInset(cpos(bv))) {
2034 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
2035 if (!isHighlyEditableInset(inset))
2037 inset->edit(bv, front);
2038 if (!the_locking_inset)
2040 updateLocal(bv, CURSOR, false);
2047 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
2050 x -= drawTextXOffset;
2052 int dummyy = y + insetAscent;
2053 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
2060 inset_x = cix(bv) - top_x + drawTextXOffset;
2061 inset_y = ciy(bv) + drawTextYOffset;
2062 inset->edit(bv, x - inset_x, y - inset_y, button);
2063 if (!the_locking_inset)
2065 updateLocal(bv, CURSOR, false);
2072 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
2075 int w = UpdatableInset::getMaxWidth(bv, inset);
2080 w = w - top_x + owner()->x();
2083 w -= (2 * TEXT_TO_INSET_OFFSET);
2086 return UpdatableInset::getMaxWidth(bv, inset);
2091 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2093 // we have to unlock any locked inset otherwise we're in troubles
2094 the_locking_inset = 0;
2096 Paragraph * tmp = par->next();
2101 par = new Paragraph(*p, same_id);
2102 par->setInsetOwner(this);
2103 Paragraph * np = par;
2106 np->next(new Paragraph(*p, same_id));
2107 np->next()->previous(np);
2109 np->setInsetOwner(this);
2116 void InsetText::setText(string const & data, LyXFont const & font)
2119 for (unsigned int i=0; i < data.length(); ++i)
2120 par->insertChar(i, data[i], font);
2125 void InsetText::setAutoBreakRows(bool flag)
2127 if (flag != autoBreakRows) {
2128 autoBreakRows = flag;
2136 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2138 if (how != drawFrame_) {
2141 updateLocal(bv, DRAW_FRAME, false);
2146 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2148 if (frame_color != col) {
2151 updateLocal(bv, DRAW_FRAME, false);
2156 int InsetText::cx(BufferView * bv) const
2158 // we do nothing dangerous so we use a local cache
2159 LyXText * llt = getLyXText(bv);
2160 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2161 if (the_locking_inset) {
2162 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2164 if (font.isVisibleRightToLeft())
2165 x -= the_locking_inset->width(bv, font);
2171 int InsetText::cix(BufferView * bv) const
2173 // we do nothing dangerous so we use a local cache
2174 LyXText * llt = getLyXText(bv);
2175 int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2176 if (the_locking_inset) {
2177 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2179 if (font.isVisibleRightToLeft())
2180 x -= the_locking_inset->width(bv, font);
2186 int InsetText::cy(BufferView * bv) const
2189 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2193 int InsetText::ciy(BufferView * bv) const
2196 return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2200 pos_type InsetText::cpos(BufferView * bv) const
2202 return getLyXText(bv)->cursor.pos();
2206 Paragraph * InsetText::cpar(BufferView * bv) const
2208 return getLyXText(bv)->cursor.par();
2212 bool InsetText::cboundary(BufferView * bv) const
2214 return getLyXText(bv)->cursor.boundary();
2218 Row * InsetText::crow(BufferView * bv) const
2220 return getLyXText(bv)->cursor.row();
2224 LyXText * InsetText::getLyXText(BufferView const * lbv,
2225 bool const recursive) const
2227 if (cached_bview == lbv) {
2228 if (recursive && the_locking_inset)
2229 return the_locking_inset->getLyXText(lbv, true);
2230 LyXText * lt = cached_text.get();
2231 lyx::Assert(lt && lt->firstRow()->par() == par);
2234 // Super UGLY! (Lgb)
2235 BufferView * bv = const_cast<BufferView *>(lbv);
2238 Cache::iterator it = cache.find(bv);
2240 if (it != cache.end()) {
2243 } else if (do_resize) {
2244 resizeLyXText(do_resize);
2246 if (lt || !it->second.remove) {
2247 lyx::Assert(it->second.text.get());
2248 cached_text = it->second.text;
2249 if (recursive && the_locking_inset) {
2250 return the_locking_inset->getLyXText(bv, true);
2252 return cached_text.get();
2253 } else if (it->second.remove) {
2255 saveLyXTextState(it->second.text.get());
2261 // when we have to reinit the existing LyXText!
2263 it->second.text->init(bv);
2264 restoreLyXTextState(bv, it->second.text.get());
2265 it->second.remove = false;
2267 cached_text = it->second.text;
2268 if (the_locking_inset && recursive) {
2269 return the_locking_inset->getLyXText(bv);
2271 return cached_text.get();
2274 // we are here only if we don't have a BufferView * in the cache!!!
2276 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2277 cached_text->init(bv);
2278 restoreLyXTextState(bv, cached_text.get());
2280 cache.insert(make_pair(bv, cached_text));
2282 if (the_locking_inset && recursive) {
2283 return the_locking_inset->getLyXText(bv);
2285 return cached_text.get();
2289 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2293 Cache::iterator it = cache.find(bv);
2295 if (it == cache.end()) {
2299 lyx::Assert(it->second.text.get());
2301 it->second.remove = true;
2303 /// then remove all LyXText in text-insets
2304 Paragraph * p = par;
2305 for (; p; p = p->next()) {
2306 p->deleteInsetsLyXText(bv);
2312 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2315 // we cannot resize this because we are in use!
2316 // so do this on the next possible getLyXText()
2321 // lyxerr << "InsetText::resizeLyXText\n";
2322 if (!par->next() && !par->size()) { // no data, resize not neccessary!
2323 // we have to do this as a fixed width may have changed!
2324 LyXText * t = getLyXText(bv);
2325 saveLyXTextState(t);
2327 restoreLyXTextState(bv, t);
2330 // one endless line, resize normally not necessary
2331 if (!force && getMaxWidth(bv, this) < 0)
2334 Cache::iterator it = cache.find(bv);
2335 if (it == cache.end()) {
2338 lyx::Assert(it->second.text.get());
2340 LyXText * t = it->second.text.get();
2341 saveLyXTextState(t);
2342 for (Paragraph * p = par; p; p = p->next()) {
2343 p->resizeInsetsLyXText(bv);
2346 restoreLyXTextState(bv, t);
2347 if (the_locking_inset) {
2348 inset_x = cix(bv) - top_x + drawTextXOffset;
2349 inset_y = ciy(bv) + drawTextYOffset;
2353 t->first_y = bv->screen()->topCursorVisible(t);
2356 updateLocal(bv, FULL, false);
2357 // this will scroll the screen such that the cursor becomes visible
2358 bv->updateScrollbar();
2360 need_update |= FULL;
2365 void InsetText::reinitLyXText() const
2368 // we cannot resize this because we are in use!
2369 // so do this on the next possible getLyXText()
2375 // lyxerr << "InsetText::reinitLyXText\n";
2376 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2377 lyx::Assert(it->second.text.get());
2379 LyXText * t = it->second.text.get();
2380 BufferView * bv = it->first;
2382 saveLyXTextState(t);
2383 for (Paragraph * p = par; p; p = p->next()) {
2384 p->resizeInsetsLyXText(bv);
2387 restoreLyXTextState(bv, t);
2388 if (the_locking_inset) {
2389 inset_x = cix(bv) - top_x + drawTextXOffset;
2390 inset_y = ciy(bv) + drawTextYOffset;
2393 t->first_y = bv->screen()->topCursorVisible(t);
2396 updateLocal(bv, FULL, false);
2397 // this will scroll the screen such that the cursor becomes visible
2398 bv->updateScrollbar();
2406 void InsetText::removeNewlines()
2408 bool changed = false;
2410 for (Paragraph * p = par; p; p = p->next()) {
2411 for (int i = 0; i < p->size(); ++i) {
2412 if (p->getChar(i) == Paragraph::META_NEWLINE) {
2423 bool InsetText::nodraw() const
2425 if (the_locking_inset)
2426 return the_locking_inset->nodraw();
2427 return UpdatableInset::nodraw();
2431 int InsetText::scroll(bool recursive) const
2433 int sx = UpdatableInset::scroll(false);
2435 if (recursive && the_locking_inset)
2436 sx += the_locking_inset->scroll(recursive);
2442 bool InsetText::doClearArea() const
2444 return !locked || (need_update & (FULL|INIT));
2448 void InsetText::selectAll(BufferView * bv)
2450 getLyXText(bv)->cursorTop(bv);
2451 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2452 getLyXText(bv)->cursorBottom(bv);
2453 getLyXText(bv)->setSelection(bv);
2457 void InsetText::clearSelection(BufferView * bv)
2459 getLyXText(bv)->clearSelection();
2463 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2465 Painter & pain = bv->painter();
2467 int h = insetAscent + insetDescent;
2468 int ty = baseline - insetAscent;
2474 if ((ty + h) > pain.paperHeight())
2475 h = pain.paperHeight();
2476 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2477 w = pain.paperWidth();
2478 // w -= TEXT_TO_INSET_OFFSET;
2479 pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2482 frame_is_visible = false;
2486 Paragraph * InsetText::getParFromID(int id) const
2489 Paragraph * result = par;
2490 Paragraph * ires = 0;
2491 while (result && result->id() != id) {
2492 if ((ires = result->getParFromID(id)))
2494 result = result->next();
2498 Paragraph * tmp = par;
2500 if (tmp->id() == id) {
2503 Paragraph * tmp2 = tmp->getParFromID(id);
2514 Paragraph * InsetText::firstParagraph() const
2517 if (the_locking_inset)
2518 if ((result = the_locking_inset->firstParagraph()))
2524 Paragraph * InsetText::getFirstParagraph(int i) const
2526 return (i == 0) ? par : 0;
2530 LyXCursor const & InsetText::cursor(BufferView * bv) const
2532 if (the_locking_inset)
2533 return the_locking_inset->cursor(bv);
2534 return getLyXText(bv)->cursor;
2538 Paragraph * InsetText::paragraph() const
2544 void InsetText::paragraph(Paragraph * p)
2546 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2547 // caller of this function has to take care of it. This IS important
2548 // as we could have to insert a paragraph before this one and just
2549 // link the actual to a new ones next and set it with this function
2552 // set ourself as owner for all the paragraphs inserted!
2553 Paragraph * np = par;
2555 np->setInsetOwner(this);
2559 // redraw myself when asked for
2564 Inset * InsetText::getInsetFromID(int id_arg) const
2567 return const_cast<InsetText *>(this);
2569 Paragraph * lp = par;
2572 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2573 en = lp->inset_iterator_end();
2576 if ((*it)->id() == id_arg)
2578 Inset * in = (*it)->getInsetFromID(id_arg);
2588 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2594 lt = getLyXText(bv);
2597 if (the_locking_inset) {
2598 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2605 // we have to go on checking so move cusor to the next char
2606 lt->cursor.pos(lt->cursor.pos() + 1);
2608 str = lt->selectNextWordToSpellcheck(bv, value);
2610 bv->unlockInset(const_cast<InsetText *>(this));
2619 void InsetText::selectSelectedWord(BufferView * bv)
2621 if (the_locking_inset) {
2622 the_locking_inset->selectSelectedWord(bv);
2625 getLyXText(bv)->selectSelectedWord(bv);
2626 updateLocal(bv, SELECTION, false);
2630 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2632 if (the_locking_inset) {
2633 the_locking_inset->toggleSelection(bv, kill_selection);
2637 lt = getLyXText(bv);
2641 int x = top_x + TEXT_TO_INSET_OFFSET;
2643 Row * row = lt->firstRow();
2644 int y_offset = top_baseline - row->ascent_of_text();
2646 while ((row != 0) && ((y+row->height()) <= 0)) {
2653 if (need_update & SELECTION)
2655 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2661 bool InsetText::searchForward(BufferView * bv, string const & str,
2664 if (the_locking_inset) {
2665 if (the_locking_inset->searchForward(bv, str, cs, mw))
2669 lt = getLyXText(bv);
2672 Paragraph * lpar = lt->cursor.par();
2673 pos_type pos = lt->cursor.pos();
2674 if (pos < lpar->size() - 1)
2678 lpar = lpar->next();
2683 // we have to unlock ourself in this function by default!
2684 bv->unlockInset(const_cast<InsetText *>(this));
2687 lt->setCursor(bv, lpar, pos);
2691 if (LyXFind(bv, str, true, true, cs , mw)) {
2694 // we have to unlock ourself in this function by default!
2695 bv->unlockInset(const_cast<InsetText *>(this));
2699 bool InsetText::searchBackward(BufferView * bv, string const & str,
2702 if (the_locking_inset)
2703 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2705 if (LyXFind(bv, str, false, true, cs, mw)) {
2708 // we have to unlock ourself in this function by default!
2709 bv->unlockInset(const_cast<InsetText *>(this));
2714 bool InsetText::checkInsertChar(LyXFont & font)
2717 return owner()->checkInsertChar(font);
2722 void InsetText::collapseParagraphs(BufferView * bv) const
2724 BufferParams const & bparams = bv->buffer()->params;
2725 LyXText * llt = getLyXText(bv);
2727 while(par->next()) {
2728 if (par->size() && par->next()->size() &&
2729 !par->isSeparator(par->size()-1))
2731 par->insertChar(par->size(), ' ');
2733 if (llt->selection.set()) {
2734 if (llt->selection.start.par() == par->next()) {
2735 llt->selection.start.par(par);
2736 llt->selection.start.pos(
2737 llt->selection.start.pos() + par->size());
2739 if (llt->selection.end.par() == par->next()) {
2740 llt->selection.end.par(par);
2741 llt->selection.end.pos(
2742 llt->selection.end.pos() + par->size());
2745 par->pasteParagraph(bparams);
2751 void InsetText::getDrawFont(LyXFont & font) const
2755 owner()->getDrawFont(font);
2759 void InsetText::appendParagraphs(BufferParams const & bparams,
2763 Paragraph * tmpbuf = newpar;
2764 Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2766 while (tmpbuf->next()) {
2767 tmpbuf = tmpbuf->next();
2768 lastbuffer->next(new Paragraph(*tmpbuf, false));
2769 lastbuffer->next()->previous(lastbuffer);
2770 lastbuffer = lastbuffer->next();
2773 while (lastbuffer->next())
2774 lastbuffer = lastbuffer->next();
2775 if (newpar->size() && lastbuffer->size() &&
2776 !lastbuffer->isSeparator(lastbuffer->size()-1))
2778 lastbuffer->insertChar(lastbuffer->size(), ' ');
2781 // make the buf exactly the same layout than our last paragraph
2782 buf->makeSameLayout(lastbuffer);
2785 lastbuffer->next(buf);
2786 buf->previous(lastbuffer);
2787 lastbuffer->pasteParagraph(bparams);