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"
42 #include "ParagraphParameters.h"
43 #include "undo_funcs.h"
46 #include "frontends/Alert.h"
48 #include "support/textutils.h"
49 #include "support/LAssert.h"
50 #include "support/lstrings.h"
51 #include "support/lyxalgo.h" // lyx::count
67 using lyx::layout_type;
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()
139 : UpdatableInset(), lt(0), in_update(false), do_resize(0),
147 InsetText::InsetText(InsetText const & in, bool same_id)
148 : UpdatableInset(in, same_id), lt(0), in_update(false), do_resize(0),
156 InsetText & InsetText::operator=(InsetText const & it)
163 void InsetText::init(InsetText const * ins, bool same_id)
166 setParagraphData(ins->par, same_id);
167 autoBreakRows = ins->autoBreakRows;
168 drawFrame_ = ins->drawFrame_;
169 frame_color = ins->frame_color;
175 p->setInsetOwner(this);
178 the_locking_inset = 0;
180 frame_color = LColor::insetframe;
181 autoBreakRows = false;
188 no_selection = false;
195 last_drawn_width = -1;
196 frame_is_visible = false;
199 in_insetAllowed = false;
203 InsetText::~InsetText()
210 Paragraph * tmp = par->next();
217 void InsetText::clear()
220 Paragraph * tmp = par->next();
230 Inset * InsetText::clone(Buffer const &, bool same_id) const
232 return new InsetText(*this, same_id);
236 void InsetText::write(Buffer const * buf, ostream & os) const
239 writeParagraphData(buf, os);
243 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
245 par->writeFile(buf, os, buf->params, 0);
249 void InsetText::read(Buffer const * buf, LyXLex & lex)
253 Paragraph * return_par = 0;
254 Paragraph::depth_type depth = 0;
255 LyXFont font(LyXFont::ALL_INHERIT);
261 token = lex.getString();
264 if (token == "\\end_inset") {
265 #ifndef NO_COMPABILITY
266 const_cast<Buffer*>(buf)->insertErtContents(par, pos, false);
271 if (const_cast<Buffer*>(buf)->
272 parseSingleLyXformat2Token(lex, par, return_par,
273 token, pos, depth, font)) {
274 // the_end read this should NEVER happen
275 lex.printError("\\the_end read in inset! Error in document!");
283 return_par->setInsetOwner(this);
284 return_par = return_par->next();
287 if (token != "\\end_inset") {
288 lex.printError("Missing \\end_inset at this point. "
295 int InsetText::ascent(BufferView * bv, LyXFont const &) const
297 insetAscent = getLyXText(bv)->firstRow()->ascent_of_text() +
298 TEXT_TO_INSET_OFFSET;
303 int InsetText::descent(BufferView * bv, LyXFont const &) const
305 LyXText * llt = getLyXText(bv);
306 insetDescent = llt->height - llt->firstRow()->ascent_of_text() +
307 TEXT_TO_INSET_OFFSET;
312 int InsetText::width(BufferView * bv, LyXFont const &) const
314 insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
315 (2 * TEXT_TO_INSET_OFFSET);
316 insetWidth = max(insetWidth, 10);
321 int InsetText::textWidth(BufferView * bv, bool fordraw) const
324 if (!autoBreakRows) {
327 w = getMaxWidth(bv, this);
330 return max(w - (2 * TEXT_TO_INSET_OFFSET),
331 (int)getLyXText(bv)->width);
335 return w - (2 * TEXT_TO_INSET_OFFSET);
339 void InsetText::draw(BufferView * bv, LyXFont const & f,
340 int baseline, float & x, bool cleared) const
345 Painter & pain = bv->painter();
347 // this is the first thing we have to ask because if the x pos
348 // changed we have to do a complete rebreak of the text as we
349 // may have few space to draw in. Well we should check on this too
351 if (top_x != int(x)) {
354 int nw = getMaxWidth(bv, this);
355 if (nw > 0 && old_max_width != nw) {
358 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
363 // call these methods so that insetWidth, insetAscent and
364 // insetDescent have the right values.
369 // repaint the background if needed
370 if (cleared && backgroundColor() != LColor::background) {
371 clearInset(bv, baseline, cleared);
374 // no draw is necessary !!!
375 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
376 top_baseline = baseline;
378 if (need_update & CLEAR_FRAME)
379 clearFrame(pain, cleared);
386 x += static_cast<float>(scroll());
388 // if top_x differs we did it already
389 if (!cleared && (old_x == int(x))
390 && ((need_update&(INIT|FULL)) || (top_baseline != baseline)
391 ||(last_drawn_width != insetWidth)))
393 // Condition necessary to eliminate bug 59 attachment 37
395 clearInset(bv, baseline, cleared);
399 frame_is_visible = false;
401 if (!cleared && (need_update == NONE)) {
403 drawFrame(pain, cleared);
407 top_baseline = baseline;
408 top_y = baseline - insetAscent;
410 if (last_drawn_width != insetWidth) {
412 clearInset(bv, baseline, cleared);
414 last_drawn_width = insetWidth;
417 if (the_locking_inset && (cpar(bv) == inset_par)
418 && (cpos(bv) == inset_pos)) {
419 inset_x = cx(bv) - top_x + drawTextXOffset;
420 inset_y = cy(bv) + drawTextYOffset;
422 if (!cleared && (need_update == CURSOR)
423 && !getLyXText(bv)->selection.set()) {
424 drawFrame(pain, cleared);
434 x += TEXT_TO_INSET_OFFSET;
436 Row * row = lt->firstRow();
437 int y_offset = baseline - row->ascent_of_text();
438 int ph = pain.paperHeight();
441 while ((row != 0) && ((y+row->height()) <= 0)) {
443 first += row->height();
449 if (cleared || (need_update&(INIT|FULL))) {
452 while ((row != 0) && (yf < ph)) {
453 lt->getVisibleRow(bv, y+y_offset, int(x), row,
459 } else if (!locked) {
460 if (need_update & CURSOR) {
461 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
462 lt->clearSelection();
463 lt->selection.cursor = lt->cursor;
465 bv->screen()->update(lt, bv, y_offset, int(x));
468 if (need_update & SELECTION) {
469 bv->screen()->toggleToggle(lt, bv, y_offset, int(x));
470 } else if (need_update & CURSOR) {
471 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
472 lt->clearSelection();
473 lt->selection.cursor = lt->cursor;
475 bv->screen()->update(lt, bv, y_offset, int(x));
480 lt->status(bv, LyXText::UNCHANGED);
481 if ((need_update != CURSOR_PAR) &&
482 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked))) {
483 drawFrame(pain, cleared);
484 } else if (need_update & CLEAR_FRAME) {
485 clearFrame(pain, cleared);
488 x += insetWidth - TEXT_TO_INSET_OFFSET;
490 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
492 } else if (need_update != INIT) {
500 void InsetText::drawFrame(Painter & pain, bool cleared) const
502 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
503 if (!frame_is_visible || cleared) {
504 frame_x = top_x + ttoD2;
505 frame_y = top_baseline - insetAscent + ttoD2;
506 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
507 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
508 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
510 frame_is_visible = true;
515 void InsetText::clearFrame(Painter & pain, bool cleared) const
517 if (frame_is_visible) {
519 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
522 frame_is_visible = false;
527 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
530 if (reinit && owner()) {
532 owner()->update(bv, font, true);
537 if (reinit || need_update == INIT) {
539 // we should put this call where we set need_update to INIT!
542 owner()->update(bv, font, true);
546 if (the_locking_inset) {
547 inset_x = cx(bv) - top_x + drawTextXOffset;
548 inset_y = cy(bv) + drawTextYOffset;
549 the_locking_inset->update(bv, font, reinit);
557 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
560 lt->updateInset(bv, the_locking_inset);
562 if (lt->status() == LyXText::NEED_MORE_REFRESH)
570 void InsetText::setUpdateStatus(BufferView * bv, int what) const
572 // this does nothing dangerous so use only a localized buffer
573 LyXText * llt = getLyXText(bv);
576 // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
577 // if we don't break row so that we only have one row to update!
578 if ((llt->status() == LyXText::NEED_MORE_REFRESH) ||
580 (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
583 } else if (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
584 need_update |= CURSOR_PAR;
587 // this to not draw a selection when we redraw all of it!
588 if (need_update & CURSOR && !(need_update & SELECTION)) {
589 if (llt->selection.set())
591 llt->clearSelection();
596 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
598 if (!autoBreakRows && par->next())
599 collapseParagraphs(bv->buffer()->params);
606 setUpdateStatus(bv, what);
607 bool flag = (((need_update != CURSOR) && (need_update != NONE)) ||
608 (lt->status() != LyXText::UNCHANGED) || lt->selection.set());
612 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
616 if (need_update == CURSOR)
618 bv->owner()->showState();
619 bv->owner()->updateMenubar();
620 bv->owner()->updateToolbar();
621 if (old_par != cpar(bv)) {
622 bv->owner()->setLayout(cpar(bv)->getLayout());
628 string const InsetText::editMessage() const
630 return _("Opened Text Inset");
634 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
636 UpdatableInset::edit(bv, x, y, button);
638 if (!bv->lockInset(this)) {
639 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
643 the_locking_inset = 0;
644 inset_pos = inset_x = inset_y = 0;
645 inset_boundary = false;
648 int tmp_y = (y < 0) ? 0 : y;
654 if (!checkAndActivateInset(bv, x, tmp_y, button))
655 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
657 lt->clearSelection();
659 // If the inset is empty set the language of the current font to the
660 // language to the surronding text (if different).
661 if (par->size() == 0 && !par->next() &&
662 bv->getParentLanguage(this) != lt->current_font.language())
664 LyXFont font(LyXFont::ALL_IGNORE);
665 font.setLanguage(bv->getParentLanguage(this));
666 setFont(bv, font, false);
673 if (drawFrame_ == LOCKED)
674 code = CURSOR|DRAW_FRAME;
675 updateLocal(bv, code, false);
679 void InsetText::edit(BufferView * bv, bool front)
681 UpdatableInset::edit(bv, front);
683 if (!bv->lockInset(this)) {
684 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
688 the_locking_inset = 0;
689 inset_pos = inset_x = inset_y = 0;
690 inset_boundary = false;
699 lt->setCursor(bv, par, 0);
704 // int const pos = (p->size() ? p->size()-1 : p->size());
705 lt->setCursor(bv, p, p->size());
707 lt->clearSelection();
709 // If the inset is empty set the language of the current font to the
710 // language to the surronding text (if different).
711 if (par->size() == 0 && !par->next() &&
712 bv->getParentLanguage(this) != lt->current_font.language()) {
713 LyXFont font(LyXFont::ALL_IGNORE);
714 font.setLanguage(bv->getParentLanguage(this));
715 setFont(bv, font, false);
721 if (drawFrame_ == LOCKED)
722 code = CURSOR|DRAW_FRAME;
723 updateLocal(bv, code, false);
727 void InsetText::insetUnlock(BufferView * bv)
729 if (the_locking_inset) {
730 the_locking_inset->insetUnlock(bv);
731 the_locking_inset = 0;
734 no_selection = false;
737 if (drawFrame_ == LOCKED)
738 code = CURSOR|CLEAR_FRAME;
746 if (lt->selection.set()) {
747 lt->clearSelection();
749 } else if (owner()) {
750 bv->owner()->setLayout(owner()->getLyXText(bv)
751 ->cursor.par()->getLayout());
753 bv->owner()->setLayout(bv->text->cursor.par()->getLayout());
754 // hack for deleteEmptyParMech
755 lt->setCursor(bv, par, 0);
758 updateLocal(bv, code, false);
761 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
763 the_locking_inset = inset;
764 inset_x = cx(bv) - top_x + drawTextXOffset;
765 inset_y = cy(bv) + drawTextYOffset;
766 inset_pos = cpos(bv);
767 inset_par = cpar(bv);
768 inset_boundary = cboundary(bv);
769 updateLocal(bv, CURSOR, false);
773 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
775 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
779 if (!the_locking_inset) {
781 int const id = inset->id();
783 Paragraph::inset_iterator it =
784 p->inset_iterator_begin();
785 Paragraph::inset_iterator const end =
786 p->inset_iterator_end();
787 for (; it != end; ++it) {
788 if ((*it) == inset) {
789 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
790 lockInset(bv, inset);
793 if ((*it)->getInsetFromID(id)) {
794 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
796 return the_locking_inset->lockInsetInInset(bv, inset);
803 if (inset == cpar(bv)->getInset(cpos(bv))) {
804 lyxerr[Debug::INSETS] << "OK" << endl;
805 lockInset(bv, inset);
807 } else if (the_locking_inset && (the_locking_inset == inset)) {
808 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
809 lyxerr[Debug::INSETS] << "OK" << endl;
810 inset_x = cx(bv) - top_x + drawTextXOffset;
811 inset_y = cy(bv) + drawTextYOffset;
813 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
815 } else if (the_locking_inset) {
816 lyxerr[Debug::INSETS] << "MAYBE" << endl;
817 return the_locking_inset->lockInsetInInset(bv, inset);
819 lyxerr[Debug::INSETS] << "NOT OK" << endl;
824 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
827 if (!the_locking_inset)
829 if (the_locking_inset == inset) {
830 the_locking_inset->insetUnlock(bv);
831 getLyXText(bv)->updateInset(bv, inset);
832 the_locking_inset = 0;
834 moveRight(bv, false);
835 old_par = 0; // force layout setting
839 updateLocal(bv, CURSOR, false);
842 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
846 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
848 if (!autoBreakRows && par->next())
849 collapseParagraphs(bv->buffer()->params);
857 if (!the_locking_inset) {
858 bool found = lt->updateInset(bv, inset);
862 setUpdateStatus(bv, NONE);
865 if (the_locking_inset != inset) {
866 bool found = the_locking_inset->updateInsetInInset(bv, inset);
870 setUpdateStatus(bv, CURSOR_PAR);
873 bool found = lt->updateInset(bv, inset);
877 setUpdateStatus(bv, CURSOR_PAR);
878 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
879 inset_x = cx(bv) - top_x + drawTextXOffset;
880 inset_y = cy(bv) + drawTextYOffset;
887 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
891 // use this to check mouse motion for selection!
895 int tmp_x = x - drawTextXOffset;
896 int tmp_y = y + insetAscent - getLyXText(bv)->first;
897 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
900 if (the_locking_inset) {
901 if (the_locking_inset == inset) {
902 the_locking_inset->insetButtonPress(bv,
906 no_selection = false;
909 // otherwise unlock the_locking_inset and lock the new inset
910 the_locking_inset->insetUnlock(bv);
911 inset_x = cx(bv) - top_x + drawTextXOffset;
912 inset_y = cy(bv) + drawTextYOffset;
913 the_locking_inset = 0;
914 inset->insetButtonPress(bv, x - inset_x,
915 y - inset_y, button);
916 inset->edit(bv, x - inset_x, y - inset_y, button);
917 if (the_locking_inset)
918 updateLocal(bv, CURSOR, false);
919 no_selection = false;
922 // otherwise only unlock the_locking_inset
923 the_locking_inset->insetUnlock(bv);
924 the_locking_inset = 0;
926 if (bv->theLockingInset()) {
927 if (isHighlyEditableInset(inset)) {
928 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
929 inset_x = cx(bv) - top_x + drawTextXOffset;
930 inset_y = cy(bv) + drawTextYOffset;
931 inset_pos = cpos(bv);
932 inset_par = cpar(bv);
933 inset_boundary = cboundary(bv);
934 the_locking_inset = uinset;
935 uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
937 uinset->edit(bv, x - inset_x, y - inset_y, 0);
938 if (the_locking_inset)
939 updateLocal(bv, CURSOR, false);
940 no_selection = false;
944 if (!inset) { // && (button == 2)) {
945 bool paste_internally = false;
946 if ((button == 2) && getLyXText(bv)->selection.set()) {
947 localDispatch(bv, LFUN_COPY, "");
948 paste_internally = true;
956 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
958 // set the selection cursor!
959 lt->selection.cursor = lt->cursor;
960 lt->cursor.x_fix(lt->cursor.x());
962 if (lt->selection.set()) {
963 lt->clearSelection();
966 updateLocal(bv, FULL, false);
968 lt->clearSelection();
971 updateLocal(bv, CURSOR, false);
973 bv->owner()->setLayout(cpar(bv)->getLayout());
975 // Insert primary selection with middle mouse
976 // if there is a local selection in the current buffer,
979 if (paste_internally)
980 localDispatch(bv, LFUN_PASTE, "");
982 localDispatch(bv, LFUN_PASTESELECTION,
986 getLyXText(bv)->clearSelection();
989 no_selection = false;
993 bool InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
995 if (the_locking_inset) {
996 return the_locking_inset->insetButtonRelease(bv,
997 x - inset_x, y - inset_y,
1000 int tmp_x = x - drawTextXOffset;
1001 int tmp_y = y + insetAscent - getLyXText(bv)->first;
1002 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1005 if (isHighlyEditableInset(inset)) {
1006 ret = inset->insetButtonRelease(bv, x - inset_x,
1007 y - inset_y, button);
1009 inset_x = cx(bv) - top_x + drawTextXOffset;
1010 inset_y = cy(bv) + drawTextYOffset;
1011 ret = inset->insetButtonRelease(bv, x - inset_x,
1012 y - inset_y, button);
1013 inset->edit(bv, x - inset_x,
1014 y - inset_y, button);
1016 updateLocal(bv, CURSOR_PAR, false);
1022 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
1024 if (no_selection || ((mouse_x == x) && (mouse_y == y)))
1026 if (the_locking_inset) {
1027 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1033 lt = getLyXText(bv);
1036 hideInsetCursor(bv);
1037 LyXCursor cur = lt->cursor;
1038 lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1039 if (cur == lt->cursor) {
1044 lt->setSelection(bv);
1045 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1046 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1050 updateLocal(bv, SELECTION, false);
1052 showInsetCursor(bv);
1056 void InsetText::insetKeyPress(XKeyEvent * xke)
1058 if (the_locking_inset) {
1059 the_locking_inset->insetKeyPress(xke);
1065 UpdatableInset::RESULT
1066 InsetText::localDispatch(BufferView * bv,
1067 kb_action action, string const & arg)
1069 bool was_empty = par->size() == 0 && !par->next();
1070 no_selection = false;
1071 UpdatableInset::RESULT
1072 result= UpdatableInset::localDispatch(bv, action, arg);
1073 if (result != UNDISPATCHED) {
1077 result = DISPATCHED;
1078 if ((action < 0) && arg.empty())
1081 if (the_locking_inset) {
1082 result = the_locking_inset->localDispatch(bv, action, arg);
1083 if (result == DISPATCHED_NOUPDATE)
1085 else if (result == DISPATCHED) {
1086 updateLocal(bv, CURSOR_PAR, false);
1088 } else if (result >= FINISHED) {
1090 case FINISHED_RIGHT:
1091 moveRightIntern(bv, false, false);
1092 result = DISPATCHED;
1095 if ((result = moveUp(bv)) >= FINISHED) {
1096 updateLocal(bv, CURSOR, false);
1097 bv->unlockInset(this);
1101 if ((result = moveDown(bv)) >= FINISHED) {
1102 updateLocal(bv, CURSOR, false);
1103 bv->unlockInset(this);
1107 result = DISPATCHED;
1110 the_locking_inset = 0;
1111 #ifdef WITH_WARNINGS
1112 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
1117 hideInsetCursor(bv);
1120 lt = getLyXText(bv);
1124 int updflag = false;
1127 case LFUN_SELFINSERT:
1128 if (bv->buffer()->isReadonly()) {
1129 // setErrorMessage(N_("Document is read only"));
1133 /* Automatically delete the currently selected
1134 * text and replace it with what is being
1135 * typed in now. Depends on lyxrc settings
1136 * "auto_region_delete", which defaults to
1139 setUndo(bv, Undo::INSERT,
1140 lt->cursor.par(), lt->cursor.par()->next());
1142 if (lyxrc.auto_region_delete) {
1143 if (lt->selection.set()) {
1144 lt->cutSelection(bv, false);
1147 lt->clearSelection();
1148 for (string::size_type i = 0; i < arg.length(); ++i) {
1149 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1152 lt->selection.cursor = lt->cursor;
1153 updwhat = CURSOR_PAR;
1155 result = DISPATCHED_NOUPDATE;
1157 // --- Cursor Movements -----------------------------------
1160 moveRight(bv, false, true);
1161 lt->setSelection(bv);
1162 updwhat = SELECTION;
1165 result = moveRight(bv);
1171 moveLeft(bv, false, true);
1172 lt->setSelection(bv);
1173 updwhat = SELECTION;
1177 result = moveLeft(bv);
1183 lt->setSelection(bv);
1184 updwhat = SELECTION;
1188 result = moveDown(bv);
1194 lt->setSelection(bv);
1195 updwhat = SELECTION;
1199 result = moveUp(bv);
1211 case LFUN_BACKSPACE: {
1212 setUndo(bv, Undo::DELETE,
1213 lt->cursor.par(), lt->cursor.par()->next());
1214 if (lt->selection.set())
1215 lt->cutSelection(bv);
1218 updwhat = CURSOR_PAR;
1224 setUndo(bv, Undo::DELETE,
1225 lt->cursor.par(), lt->cursor.par()->next());
1226 if (lt->selection.set()) {
1227 lt->cutSelection(bv);
1231 updwhat = CURSOR_PAR;
1237 setUndo(bv, Undo::DELETE,
1238 lt->cursor.par(), lt->cursor.par()->next());
1239 lt->cutSelection(bv);
1240 updwhat = CURSOR_PAR;
1247 lt->copySelection(bv);
1248 updwhat = CURSOR_PAR;
1250 case LFUN_PASTESELECTION:
1252 string const clip(bv->getClipboard());
1256 if (arg == "paragraph") {
1257 lt->insertStringAsParagraphs(bv, clip);
1259 lt->insertStringAsLines(bv, clip);
1261 updwhat = CURSOR_PAR;
1266 if (!autoBreakRows) {
1268 if (CutAndPaste::nrOfParagraphs() > 1) {
1269 Alert::alert(_("Impossible operation"),
1270 _("Cannot include more than one paragraph!"),
1275 setUndo(bv, Undo::INSERT,
1276 lt->cursor.par(), lt->cursor.par()->next());
1277 lt->pasteSelection(bv);
1278 updwhat = CURSOR_PAR;
1283 case LFUN_BREAKPARAGRAPH:
1284 if (!autoBreakRows) {
1285 result = DISPATCHED;
1288 lt->breakParagraph(bv, 0);
1292 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1293 if (!autoBreakRows) {
1294 result = DISPATCHED;
1297 lt->breakParagraph(bv, 1);
1302 case LFUN_BREAKLINE: {
1303 if (!autoBreakRows) {
1304 result = DISPATCHED;
1307 setUndo(bv, Undo::INSERT,
1308 lt->cursor.par(), lt->cursor.par()->next());
1309 lt->insertChar(bv, Paragraph::META_NEWLINE);
1310 updwhat = CURSOR_PAR;
1316 // do not set layouts on non breakable textinsets
1317 if (autoBreakRows) {
1318 layout_type cur_layout = cpar(bv)->layout;
1320 // Derive layout number from given argument (string)
1321 // and current buffer's textclass (number). */
1322 textclass_type tclass = bv->buffer()->params.textclass;
1323 std::pair <bool, layout_type> layout =
1324 textclasslist.NumberOfLayout(tclass, arg);
1326 // If the entry is obsolete, use the new one instead.
1328 string obs = textclasslist.Style(tclass,layout.second).
1331 layout = textclasslist.NumberOfLayout(tclass, obs);
1334 // see if we found the layout number:
1335 if (!layout.first) {
1336 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1337 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1341 if (cur_layout != layout.second) {
1342 cur_layout = layout.second;
1343 lt->setLayout(bv, layout.second);
1344 bv->owner()->setLayout(cpar(bv)->getLayout());
1345 updwhat = CURSOR_PAR;
1349 // reset the layout box
1350 bv->owner()->setLayout(cpar(bv)->getLayout());
1353 case LFUN_PARAGRAPH_SPACING:
1354 // This one is absolutely not working. When fiddling with this
1355 // it also seems to me that the paragraphs inside the insettext
1356 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1358 Paragraph * par = lt->cursor.par();
1359 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1360 float cur_value = 1.0;
1361 if (cur_spacing == Spacing::Other) {
1362 cur_value = par->params().spacing().getValue();
1365 istringstream istr(arg.c_str());
1368 Spacing::Space new_spacing = cur_spacing;
1369 float new_value = cur_value;
1371 lyxerr << "Missing argument to `paragraph-spacing'"
1373 } else if (tmp == "single") {
1374 new_spacing = Spacing::Single;
1375 } else if (tmp == "onehalf") {
1376 new_spacing = Spacing::Onehalf;
1377 } else if (tmp == "double") {
1378 new_spacing = Spacing::Double;
1379 } else if (tmp == "other") {
1380 new_spacing = Spacing::Other;
1383 lyxerr << "new_value = " << tmpval << endl;
1386 } else if (tmp == "default") {
1387 new_spacing = Spacing::Default;
1389 lyxerr << _("Unknown spacing argument: ")
1392 if (cur_spacing != new_spacing || cur_value != new_value) {
1393 par->params().spacing(Spacing(new_spacing, new_value));
1394 updwhat = CURSOR_PAR;
1401 if (!bv->Dispatch(action, arg))
1402 result = UNDISPATCHED;
1409 updateLocal(bv, updwhat, updflag);
1410 /// If the action has deleted all text in the inset, we need to change the
1411 // language to the language of the surronding text.
1412 if (!was_empty && par->size() == 0 && !par->next()) {
1413 LyXFont font(LyXFont::ALL_IGNORE);
1414 font.setLanguage(bv->getParentLanguage(this));
1415 setFont(bv, font, false);
1418 if (result < FINISHED) {
1419 showInsetCursor(bv);
1421 bv->unlockInset(this);
1426 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1429 buf->latexParagraphs(os, par, 0, texrow);
1430 return texrow.rows();
1434 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1436 Paragraph * p = par;
1437 unsigned int lines = 0;
1440 string const tmp = buf->asciiParagraph(p, linelen, p->previous()==0);
1441 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1449 int InsetText::docbook(Buffer const * buf, ostream & os) const
1451 Paragraph * p = par;
1452 unsigned int lines = 0;
1454 vector<string> environment_stack(10);
1455 vector<string> environment_inner(10);
1457 int const command_depth = 0;
1460 Paragraph::depth_type depth = 0; // paragraph depth
1464 int desc_on = 0; // description mode
1466 LyXLayout const & style =
1467 textclasslist.Style(buf->params.textclass,
1470 // environment tag closing
1471 for (; depth > p->params().depth(); --depth) {
1472 if (environment_inner[depth] != "!-- --") {
1473 item_name = "listitem";
1474 buf->sgmlCloseTag(os, command_depth + depth,
1476 if (environment_inner[depth] == "varlistentry")
1477 buf->sgmlCloseTag(os, depth+command_depth,
1478 environment_inner[depth]);
1480 buf->sgmlCloseTag(os, depth + command_depth,
1481 environment_stack[depth]);
1482 environment_stack[depth].erase();
1483 environment_inner[depth].erase();
1486 if (depth == p->params().depth()
1487 && environment_stack[depth] != style.latexname()
1488 && !environment_stack[depth].empty()) {
1489 if (environment_inner[depth] != "!-- --") {
1490 item_name= "listitem";
1491 buf->sgmlCloseTag(os, command_depth+depth,
1493 if (environment_inner[depth] == "varlistentry")
1494 buf->sgmlCloseTag(os,
1495 depth + command_depth,
1496 environment_inner[depth]);
1499 buf->sgmlCloseTag(os, depth + command_depth,
1500 environment_stack[depth]);
1502 environment_stack[depth].erase();
1503 environment_inner[depth].erase();
1506 // Write opening SGML tags.
1507 switch (style.latextype) {
1508 case LATEX_PARAGRAPH:
1509 buf->sgmlOpenTag(os, depth + command_depth,
1514 buf->sgmlError(p, 0,
1515 _("Error : LatexType Command not allowed here.\n"));
1519 case LATEX_ENVIRONMENT:
1520 case LATEX_ITEM_ENVIRONMENT:
1521 if (depth < p->params().depth()) {
1522 depth = p->params().depth();
1523 environment_stack[depth].erase();
1526 if (environment_stack[depth] != style.latexname()) {
1527 if(environment_stack.size() == depth + 1) {
1528 environment_stack.push_back("!-- --");
1529 environment_inner.push_back("!-- --");
1531 environment_stack[depth] = style.latexname();
1532 environment_inner[depth] = "!-- --";
1533 buf->sgmlOpenTag(os, depth + command_depth,
1534 environment_stack[depth]);
1536 if (environment_inner[depth] != "!-- --") {
1537 item_name= "listitem";
1538 buf->sgmlCloseTag(os,
1539 command_depth + depth,
1541 if (environment_inner[depth] == "varlistentry")
1542 buf->sgmlCloseTag(os,
1543 depth + command_depth,
1544 environment_inner[depth]);
1548 if (style.latextype == LATEX_ENVIRONMENT) {
1549 if (!style.latexparam().empty()) {
1550 if(style.latexparam() == "CDATA")
1553 buf->sgmlOpenTag(os, depth + command_depth,
1554 style.latexparam());
1559 desc_on = (style.labeltype == LABEL_MANUAL);
1562 environment_inner[depth]= "varlistentry";
1564 environment_inner[depth]= "listitem";
1566 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1567 environment_inner[depth]);
1571 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1575 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1580 buf->sgmlOpenTag(os, depth + command_depth,
1585 buf->simpleDocBookOnePar(os, p, desc_on,
1586 depth + 1 + command_depth);
1590 // write closing SGML tags
1591 switch (style.latextype) {
1592 case LATEX_ENVIRONMENT:
1593 if (!style.latexparam().empty()) {
1594 if(style.latexparam() == "CDATA")
1597 buf->sgmlCloseTag(os, depth + command_depth,
1598 style.latexparam());
1601 case LATEX_ITEM_ENVIRONMENT:
1602 if (desc_on == 1) break;
1604 buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1606 case LATEX_PARAGRAPH:
1607 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1610 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1616 for (int d = depth; d >= 0; --d) {
1617 if (!environment_stack[depth].empty()) {
1618 if (environment_inner[depth] != "!-- --") {
1619 item_name = "listitem";
1620 buf->sgmlCloseTag(os, command_depth + depth,
1622 if (environment_inner[depth] == "varlistentry")
1623 buf->sgmlCloseTag(os, depth + command_depth,
1624 environment_inner[depth]);
1627 buf->sgmlCloseTag(os, depth + command_depth,
1628 environment_stack[depth]);
1636 void InsetText::validate(LaTeXFeatures & features) const
1638 Paragraph * p = par;
1640 p->validate(features);
1646 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1648 if (textclasslist.Style(buf->params.textclass,
1649 p->getLayout()).labeltype != LABEL_MANUAL)
1652 return p->beginningOfMainBody();
1656 void InsetText::getCursorPos(BufferView * bv,
1657 int & x, int & y) const
1659 if (the_locking_inset) {
1660 the_locking_inset->getCursorPos(bv, x, y);
1668 unsigned int InsetText::insetInInsetY()
1670 if (!the_locking_inset)
1673 return (inset_y + the_locking_inset->insetInInsetY());
1677 void InsetText::toggleInsetCursor(BufferView * bv)
1679 if (the_locking_inset) {
1680 the_locking_inset->toggleInsetCursor(bv);
1684 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1686 int const asc = lyxfont::maxAscent(font);
1687 int const desc = lyxfont::maxDescent(font);
1689 if (isCursorVisible())
1690 bv->hideLockedInsetCursor();
1692 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1693 toggleCursorVisible();
1697 void InsetText::showInsetCursor(BufferView * bv, bool show)
1699 if (the_locking_inset) {
1700 the_locking_inset->showInsetCursor(bv, show);
1703 if (!isCursorVisible()) {
1704 LyXFont const font =
1705 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1707 int const asc = lyxfont::maxAscent(font);
1708 int const desc = lyxfont::maxDescent(font);
1710 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1712 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1713 setCursorVisible(true);
1718 void InsetText::hideInsetCursor(BufferView * bv)
1720 if (isCursorVisible()) {
1721 bv->hideLockedInsetCursor();
1722 setCursorVisible(false);
1724 if (the_locking_inset)
1725 the_locking_inset->hideInsetCursor(bv);
1729 void InsetText::fitInsetCursor(BufferView * bv) const
1731 if (the_locking_inset) {
1732 the_locking_inset->fitInsetCursor(bv);
1735 LyXFont const font =
1736 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1738 int const asc = lyxfont::maxAscent(font);
1739 int const desc = lyxfont::maxDescent(font);
1741 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1745 UpdatableInset::RESULT
1746 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1748 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1749 return moveLeftIntern(bv, false, activate_inset, selecting);
1751 return moveRightIntern(bv, false, activate_inset, selecting);
1755 UpdatableInset::RESULT
1756 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1758 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1759 return moveRightIntern(bv, true, activate_inset, selecting);
1761 return moveLeftIntern(bv, true, activate_inset, selecting);
1765 UpdatableInset::RESULT
1766 InsetText::moveRightIntern(BufferView * bv, bool behind,
1767 bool activate_inset, bool selecting)
1769 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1770 return FINISHED_RIGHT;
1771 if (activate_inset && checkAndActivateInset(bv, behind))
1773 getLyXText(bv)->cursorRight(bv);
1775 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1776 return DISPATCHED_NOUPDATE;
1780 UpdatableInset::RESULT
1781 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1782 bool activate_inset, bool selecting)
1784 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1786 getLyXText(bv)->cursorLeft(bv);
1788 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1789 if (activate_inset && checkAndActivateInset(bv, behind))
1791 return DISPATCHED_NOUPDATE;
1795 UpdatableInset::RESULT
1796 InsetText::moveUp(BufferView * bv)
1798 if (!crow(bv)->previous())
1800 getLyXText(bv)->cursorUp(bv);
1801 return DISPATCHED_NOUPDATE;
1805 UpdatableInset::RESULT
1806 InsetText::moveDown(BufferView * bv)
1808 if (!crow(bv)->next())
1809 return FINISHED_DOWN;
1810 getLyXText(bv)->cursorDown(bv);
1811 return DISPATCHED_NOUPDATE;
1815 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1817 if (the_locking_inset) {
1818 if (the_locking_inset->insetAllowed(inset))
1819 return the_locking_inset->insertInset(bv, inset);
1824 lt = getLyXText(bv);
1827 setUndo(bv, Undo::FINISH, lt->cursor.par(), lt->cursor.par()->next());
1829 inset->setOwner(this);
1830 hideInsetCursor(bv);
1831 lt->insertInset(bv, inset);
1835 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1841 bool InsetText::insetAllowed(Inset::Code code) const
1843 // in_insetAllowed is a really gross hack,
1844 // to allow us to call the owner's insetAllowed
1845 // without stack overflow, which can happen
1846 // when the owner uses InsetCollapsable::insetAllowed()
1848 if (in_insetAllowed)
1850 in_insetAllowed = true;
1851 if (the_locking_inset)
1852 ret = the_locking_inset->insetAllowed(code);
1854 ret = owner()->insetAllowed(code);
1855 in_insetAllowed = false;
1860 UpdatableInset * InsetText::getLockingInset() const
1862 return the_locking_inset ? the_locking_inset->getLockingInset() :
1863 const_cast<InsetText *>(this);
1867 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1871 if (the_locking_inset)
1872 return the_locking_inset->getFirstLockingInsetOfType(c);
1877 bool InsetText::showInsetDialog(BufferView * bv) const
1879 if (the_locking_inset)
1880 return the_locking_inset->showInsetDialog(bv);
1885 std::vector<string> const InsetText::getLabelList() const
1887 std::vector<string> label_list;
1889 Paragraph * tpar = par;
1891 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1892 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1893 for (; beg != end; ++beg) {
1894 std::vector<string> const l = (*beg)->getLabelList();
1895 label_list.insert(label_list.end(), l.begin(), l.end());
1897 tpar = tpar->next();
1903 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1906 if (the_locking_inset) {
1907 the_locking_inset->setFont(bv, font, toggleall, selectall);
1910 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
1911 getLyXText(bv)->setFont(bv, font, toggleall);
1916 lt = getLyXText(bv);
1919 if (lt->selection.set()) {
1920 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1924 lt->toggleFree(bv, font, toggleall);
1926 lt->clearSelection();
1928 bool flag = (selectall || lt->selection.set());
1932 updateLocal(bv, FULL, true);
1934 updateLocal(bv, CURSOR_PAR, true);
1938 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1940 if (cpar(bv)->isInset(cpos(bv))) {
1944 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1945 if (!isHighlyEditableInset(inset))
1947 LyXFont const font =
1948 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1950 x = inset->width(bv, font);
1951 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1954 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1956 //inset_x = cx(bv) - top_x + drawTextXOffset;
1957 //inset_y = cy(bv) + drawTextYOffset;
1958 inset->edit(bv, x, y, 0);
1959 if (!the_locking_inset)
1961 updateLocal(bv, CURSOR, false);
1968 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1971 x -= drawTextXOffset;
1973 int dummyy = y + insetAscent;
1974 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
1981 inset_x = cx(bv) - top_x + drawTextXOffset;
1982 inset_y = cy(bv) + drawTextYOffset;
1983 inset->edit(bv, x - inset_x, y - inset_y, button);
1984 if (!the_locking_inset)
1986 updateLocal(bv, CURSOR, false);
1993 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1996 int w = UpdatableInset::getMaxWidth(bv, inset);
2001 w = w - top_x + owner()->x();
2004 w -= (2 * TEXT_TO_INSET_OFFSET);
2007 return UpdatableInset::getMaxWidth(bv, inset);
2012 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2014 // we have to unlock any locked inset otherwise we're in troubles
2015 the_locking_inset = 0;
2017 Paragraph * tmp = par->next();
2022 par = new Paragraph(*p, same_id);
2023 par->setInsetOwner(this);
2024 Paragraph * np = par;
2027 np->next(new Paragraph(*p, same_id));
2028 np->next()->previous(np);
2030 np->setInsetOwner(this);
2037 void InsetText::setText(string const & data)
2040 LyXFont font(LyXFont::ALL_SANE);
2041 for (unsigned int i=0; i < data.length(); ++i)
2042 par->insertChar(i, data[i], font);
2046 void InsetText::setAutoBreakRows(bool flag)
2048 if (flag != autoBreakRows) {
2049 autoBreakRows = flag;
2057 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2059 if (how != drawFrame_) {
2062 updateLocal(bv, DRAW_FRAME, false);
2067 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2069 if (frame_color != col) {
2072 updateLocal(bv, DRAW_FRAME, false);
2077 int InsetText::cx(BufferView * bv) const
2079 // we do nothing dangerous so we use a local cache
2080 LyXText * llt = getLyXText(bv);
2081 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2082 if (the_locking_inset) {
2083 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2085 if (font.isVisibleRightToLeft())
2086 x -= the_locking_inset->width(bv, font);
2092 int InsetText::cy(BufferView * bv) const
2095 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2099 pos_type InsetText::cpos(BufferView * bv) const
2101 return getLyXText(bv)->cursor.pos();
2105 Paragraph * InsetText::cpar(BufferView * bv) const
2107 return getLyXText(bv)->cursor.par();
2111 bool InsetText::cboundary(BufferView * bv) const
2113 return getLyXText(bv)->cursor.boundary();
2117 Row * InsetText::crow(BufferView * bv) const
2119 return getLyXText(bv)->cursor.row();
2123 LyXText * InsetText::getLyXText(BufferView const * lbv,
2124 bool const recursive) const
2126 if (!recursive && (cached_bview == lbv)) {
2127 LyXText * lt = cached_text.get();
2128 lyx::Assert(lt && lt->firstRow()->par() == par);
2132 // Super UGLY! (Lgb)
2133 BufferView * bv = const_cast<BufferView *>(lbv);
2136 Cache::iterator it = cache.find(bv);
2138 if (it != cache.end()) {
2141 } else if (do_resize) {
2142 resizeLyXText(do_resize);
2144 if (lt || !it->second.remove) {
2145 lyx::Assert(it->second.text.get());
2146 cached_text = it->second.text;
2147 if (recursive && the_locking_inset) {
2148 return the_locking_inset->getLyXText(bv, true);
2150 return cached_text.get();
2151 } else if (it->second.remove) {
2153 saveLyXTextState(it->second.text.get());
2159 // when we have to reinit the existing LyXText!
2161 it->second.text->init(bv);
2162 restoreLyXTextState(bv, it->second.text.get());
2163 it->second.remove = false;
2165 cached_text = it->second.text;
2166 if (the_locking_inset && recursive) {
2167 return the_locking_inset->getLyXText(bv);
2169 return cached_text.get();
2172 // we are here only if we don't have a BufferView * in the cache!!!
2174 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2175 cached_text->init(bv);
2176 restoreLyXTextState(bv, cached_text.get());
2178 cache.insert(make_pair(bv, cached_text));
2180 if (the_locking_inset && recursive) {
2181 return the_locking_inset->getLyXText(bv);
2183 return cached_text.get();
2187 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2191 Cache::iterator it = cache.find(bv);
2193 if (it == cache.end()) {
2197 lyx::Assert(it->second.text.get());
2199 it->second.remove = true;
2201 /// then remove all LyXText in text-insets
2202 Paragraph * p = par;
2203 for (; p; p = p->next()) {
2204 p->deleteInsetsLyXText(bv);
2210 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2213 // we cannot resize this because we are in use!
2214 // so do this on the next possible getLyXText()
2219 // lyxerr << "InsetText::resizeLyXText\n";
2220 if (!par->next() && !par->size()) { // no data, resize not neccessary!
2221 // we have to do this as a fixed width may have changed!
2222 LyXText * t = getLyXText(bv);
2223 saveLyXTextState(t);
2225 restoreLyXTextState(bv, t);
2228 // one endless line, resize normally not necessary
2229 if (!force && getMaxWidth(bv, this) < 0)
2232 Cache::iterator it = cache.find(bv);
2233 if (it == cache.end()) {
2236 lyx::Assert(it->second.text.get());
2238 LyXText * t = it->second.text.get();
2239 saveLyXTextState(t);
2240 for (Paragraph * p = par; p; p = p->next()) {
2241 p->resizeInsetsLyXText(bv);
2244 restoreLyXTextState(bv, t);
2245 if (the_locking_inset) {
2246 inset_x = cx(bv) - top_x + drawTextXOffset;
2247 inset_y = cy(bv) + drawTextYOffset;
2251 t->first = bv->screen()->topCursorVisible(t);
2254 updateLocal(bv, FULL, false);
2255 // this will scroll the screen such that the cursor becomes visible
2256 bv->updateScrollbar();
2258 need_update |= FULL;
2263 void InsetText::reinitLyXText() const
2266 // we cannot resize this because we are in use!
2267 // so do this on the next possible getLyXText()
2273 // lyxerr << "InsetText::reinitLyXText\n";
2274 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2275 lyx::Assert(it->second.text.get());
2277 LyXText * t = it->second.text.get();
2278 BufferView * bv = it->first;
2280 saveLyXTextState(t);
2281 for (Paragraph * p = par; p; p = p->next()) {
2282 p->resizeInsetsLyXText(bv);
2285 restoreLyXTextState(bv, t);
2286 if (the_locking_inset) {
2287 inset_x = cx(bv) - top_x + drawTextXOffset;
2288 inset_y = cy(bv) + drawTextYOffset;
2291 t->first = bv->screen()->topCursorVisible(t);
2294 updateLocal(bv, FULL, false);
2295 // this will scroll the screen such that the cursor becomes visible
2296 bv->updateScrollbar();
2304 void InsetText::removeNewlines()
2306 bool changed = false;
2308 for (Paragraph * p = par; p; p = p->next()) {
2309 for (int i = 0; i < p->size(); ++i) {
2310 if (p->getChar(i) == Paragraph::META_NEWLINE) {
2321 bool InsetText::nodraw() const
2323 if (the_locking_inset)
2324 return the_locking_inset->nodraw();
2325 return UpdatableInset::nodraw();
2329 int InsetText::scroll(bool recursive) const
2331 int sx = UpdatableInset::scroll(false);
2333 if (recursive && the_locking_inset)
2334 sx += the_locking_inset->scroll(recursive);
2340 bool InsetText::doClearArea() const
2342 return !locked || (need_update & (FULL|INIT));
2346 void InsetText::selectAll(BufferView * bv)
2348 getLyXText(bv)->cursorTop(bv);
2349 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2350 getLyXText(bv)->cursorBottom(bv);
2351 getLyXText(bv)->setSelection(bv);
2355 void InsetText::clearSelection(BufferView * bv)
2357 getLyXText(bv)->clearSelection();
2361 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2363 Painter & pain = bv->painter();
2365 int h = insetAscent + insetDescent;
2366 int ty = baseline - insetAscent;
2372 if ((ty + h) > pain.paperHeight())
2373 h = pain.paperHeight();
2374 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2375 w = pain.paperWidth();
2376 // w -= TEXT_TO_INSET_OFFSET;
2377 pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2380 frame_is_visible = false;
2384 Paragraph * InsetText::getParFromID(int id) const
2387 Paragraph * result = par;
2388 Paragraph * ires = 0;
2389 while (result && result->id() != id) {
2390 if ((ires = result->getParFromID(id)))
2392 result = result->next();
2396 Paragraph * tmp = par;
2398 if (tmp->id() == id) {
2401 Paragraph * tmp2 = tmp->getParFromID(id);
2412 Paragraph * InsetText::firstParagraph() const
2415 if (the_locking_inset)
2416 if ((result = the_locking_inset->firstParagraph()))
2422 Paragraph * InsetText::getFirstParagraph(int i) const
2424 return (i == 0) ? par : 0;
2428 LyXCursor const & InsetText::cursor(BufferView * bv) const
2430 if (the_locking_inset)
2431 return the_locking_inset->cursor(bv);
2432 return getLyXText(bv)->cursor;
2436 Paragraph * InsetText::paragraph() const
2442 void InsetText::paragraph(Paragraph * p)
2444 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2445 // caller of this function has to take care of it. This IS important
2446 // as we could have to insert a paragraph before this one and just
2447 // link the actual to a new ones next and set it with this function
2450 // set ourself as owner for all the paragraphs inserted!
2451 Paragraph * np = par;
2453 np->setInsetOwner(this);
2457 // redraw myself when asked for
2462 Inset * InsetText::getInsetFromID(int id_arg) const
2465 return const_cast<InsetText *>(this);
2467 Paragraph * lp = par;
2470 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2471 en = lp->inset_iterator_end();
2474 if ((*it)->id() == id_arg)
2476 Inset * in = (*it)->getInsetFromID(id_arg);
2486 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2492 lt = getLyXText(bv);
2495 if (the_locking_inset) {
2496 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2503 // we have to go on checking so move cusor to the next char
2504 lt->cursor.pos(lt->cursor.pos() + 1);
2506 str = lt->selectNextWordToSpellcheck(bv, value);
2508 bv->unlockInset(const_cast<InsetText *>(this));
2517 void InsetText::selectSelectedWord(BufferView * bv)
2519 if (the_locking_inset) {
2520 the_locking_inset->selectSelectedWord(bv);
2523 getLyXText(bv)->selectSelectedWord(bv);
2524 updateLocal(bv, SELECTION, false);
2528 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2530 if (the_locking_inset) {
2531 the_locking_inset->toggleSelection(bv, kill_selection);
2535 lt = getLyXText(bv);
2539 int x = top_x + TEXT_TO_INSET_OFFSET;
2541 Row * row = lt->firstRow();
2542 int y_offset = top_baseline - row->ascent_of_text();
2544 while ((row != 0) && ((y+row->height()) <= 0)) {
2551 if (need_update & SELECTION)
2553 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2559 bool InsetText::searchForward(BufferView * bv, string const & str,
2562 if (the_locking_inset) {
2563 if (the_locking_inset->searchForward(bv, str, cs, mw))
2567 lt = getLyXText(bv);
2570 Paragraph * lpar = lt->cursor.par();
2571 pos_type pos = lt->cursor.pos();
2572 if (pos < lpar->size() - 1)
2576 lpar = lpar->next();
2581 // we have to unlock ourself in this function by default!
2582 bv->unlockInset(const_cast<InsetText *>(this));
2585 lt->setCursor(bv, lpar, pos);
2589 if (LyXFind(bv, str, true, true, cs , mw)) {
2592 // we have to unlock ourself in this function by default!
2593 bv->unlockInset(const_cast<InsetText *>(this));
2597 bool InsetText::searchBackward(BufferView * bv, string const & str,
2600 if (the_locking_inset)
2601 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2603 if (LyXFind(bv, str, false, true, cs, mw)) {
2606 // we have to unlock ourself in this function by default!
2607 bv->unlockInset(const_cast<InsetText *>(this));
2612 bool InsetText::checkInsertChar(LyXFont & font)
2615 return owner()->checkInsertChar(font);
2620 void InsetText::collapseParagraphs(BufferParams const & bparams) const
2622 while(par->next()) {
2623 if (!par->isSeparator(par->size()-1))
2624 par->insertChar(par->size()-1, ' ');
2625 par->pasteParagraph(bparams);