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
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;
190 no_selection = 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()
222 Paragraph * tmp = par->next();
232 Inset * InsetText::clone(Buffer const &, bool same_id) const
234 return new InsetText(*this, same_id);
238 void InsetText::write(Buffer const * buf, ostream & os) const
241 writeParagraphData(buf, os);
245 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
247 par->writeFile(buf, os, buf->params, 0);
251 void InsetText::read(Buffer const * buf, LyXLex & lex)
255 Paragraph * return_par = 0;
256 Paragraph::depth_type depth = 0;
257 LyXFont font(LyXFont::ALL_INHERIT);
263 token = lex.getString();
266 if (token == "\\end_inset") {
267 #ifndef NO_COMPABILITY
268 const_cast<Buffer*>(buf)->insertErtContents(par, pos, false);
273 if (const_cast<Buffer*>(buf)->
274 parseSingleLyXformat2Token(lex, par, return_par,
275 token, pos, depth, font)) {
276 // the_end read this should NEVER happen
277 lex.printError("\\the_end read in inset! Error in document!");
285 return_par->setInsetOwner(this);
286 return_par = return_par->next();
289 if (token != "\\end_inset") {
290 lex.printError("Missing \\end_inset at this point. "
297 int InsetText::ascent(BufferView * bv, LyXFont const &) const
299 insetAscent = getLyXText(bv)->firstRow()->ascent_of_text() +
300 TEXT_TO_INSET_OFFSET;
305 int InsetText::descent(BufferView * bv, LyXFont const &) const
307 LyXText * llt = getLyXText(bv);
308 insetDescent = llt->height - llt->firstRow()->ascent_of_text() +
309 TEXT_TO_INSET_OFFSET;
314 int InsetText::width(BufferView * bv, LyXFont const &) const
316 insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
317 (2 * TEXT_TO_INSET_OFFSET);
318 insetWidth = max(insetWidth, 10);
323 int InsetText::textWidth(BufferView * bv, bool fordraw) const
326 if (!autoBreakRows) {
329 w = getMaxWidth(bv, this);
332 return max(w - (2 * TEXT_TO_INSET_OFFSET),
333 (int)getLyXText(bv)->width);
337 return w - (2 * TEXT_TO_INSET_OFFSET);
341 void InsetText::draw(BufferView * bv, LyXFont const & f,
342 int baseline, float & x, bool cleared) const
347 Painter & pain = bv->painter();
349 // this is the first thing we have to ask because if the x pos
350 // changed we have to do a complete rebreak of the text as we
351 // may have few space to draw in. Well we should check on this too
353 if (top_x != int(x)) {
356 int nw = getMaxWidth(bv, this);
357 if (nw > 0 && old_max_width != nw) {
360 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
365 // call these methods so that insetWidth, insetAscent and
366 // insetDescent have the right values.
371 // repaint the background if needed
372 if (cleared && backgroundColor() != LColor::background) {
373 clearInset(bv, baseline, cleared);
376 // no draw is necessary !!!
377 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
378 top_baseline = baseline;
380 if (need_update & CLEAR_FRAME)
381 clearFrame(pain, cleared);
388 x += static_cast<float>(scroll());
390 // if top_x differs we did it already
391 if (!cleared && (old_x == int(x))
392 && ((need_update&(INIT|FULL)) || (top_baseline != baseline)
393 ||(last_drawn_width != insetWidth)))
395 // Condition necessary to eliminate bug 59 attachment 37
397 clearInset(bv, baseline, cleared);
401 frame_is_visible = false;
403 if (!cleared && (need_update == NONE)) {
405 drawFrame(pain, cleared);
409 top_baseline = baseline;
410 top_y = baseline - insetAscent;
412 if (last_drawn_width != insetWidth) {
414 clearInset(bv, baseline, cleared);
416 last_drawn_width = insetWidth;
419 if (the_locking_inset && (cpar(bv) == inset_par)
420 && (cpos(bv) == inset_pos)) {
421 inset_x = cx(bv) - top_x + drawTextXOffset;
422 inset_y = cy(bv) + drawTextYOffset;
424 if (!cleared && (need_update == CURSOR)
425 && !getLyXText(bv)->selection.set()) {
426 drawFrame(pain, cleared);
436 x += TEXT_TO_INSET_OFFSET;
438 Row * row = lt->firstRow();
439 int y_offset = baseline - row->ascent_of_text();
440 int ph = pain.paperHeight();
443 while ((row != 0) && ((y+row->height()) <= 0)) {
445 first += row->height();
451 if (cleared || (need_update&(INIT|FULL))) {
454 while ((row != 0) && (yf < ph)) {
455 lt->getVisibleRow(bv, y+y_offset, int(x), row,
461 } else if (!locked) {
462 if (need_update & CURSOR) {
463 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
464 lt->clearSelection();
465 lt->selection.cursor = lt->cursor;
467 bv->screen()->update(lt, bv, y_offset, int(x));
470 if (need_update & SELECTION) {
471 bv->screen()->toggleToggle(lt, bv, y_offset, int(x));
472 } else if (need_update & CURSOR) {
473 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
474 lt->clearSelection();
475 lt->selection.cursor = lt->cursor;
477 bv->screen()->update(lt, bv, y_offset, int(x));
482 lt->status(bv, LyXText::UNCHANGED);
483 if ((need_update != CURSOR_PAR) &&
484 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked))) {
485 drawFrame(pain, cleared);
486 } else if (need_update & CLEAR_FRAME) {
487 clearFrame(pain, cleared);
490 x += insetWidth - TEXT_TO_INSET_OFFSET;
492 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
494 } else if (need_update != INIT) {
502 void InsetText::drawFrame(Painter & pain, bool cleared) const
504 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
505 if (!frame_is_visible || cleared) {
506 frame_x = top_x + ttoD2;
507 frame_y = top_baseline - insetAscent + ttoD2;
508 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
509 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
510 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
512 frame_is_visible = true;
517 void InsetText::clearFrame(Painter & pain, bool cleared) const
519 if (frame_is_visible) {
521 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
524 frame_is_visible = false;
529 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
532 if (reinit && owner()) {
534 owner()->update(bv, font, true);
539 if (reinit || need_update == INIT) {
541 // we should put this call where we set need_update to INIT!
544 owner()->update(bv, font, true);
548 if (the_locking_inset) {
549 inset_x = cx(bv) - top_x + drawTextXOffset;
550 inset_y = cy(bv) + drawTextYOffset;
551 the_locking_inset->update(bv, font, reinit);
559 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
562 lt->updateInset(bv, the_locking_inset);
564 if (lt->status() == LyXText::NEED_MORE_REFRESH)
572 void InsetText::setUpdateStatus(BufferView * bv, int what) const
574 // this does nothing dangerous so use only a localized buffer
575 LyXText * llt = getLyXText(bv);
578 // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
579 // if we don't break row so that we only have one row to update!
580 if ((llt->status() == LyXText::NEED_MORE_REFRESH) ||
582 (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
585 } else if (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
586 need_update |= CURSOR_PAR;
589 // this to not draw a selection when we redraw all of it!
590 if (need_update & CURSOR && !(need_update & SELECTION)) {
591 if (llt->selection.set())
593 llt->clearSelection();
598 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
600 if (!autoBreakRows && par->next())
601 collapseParagraphs(bv->buffer()->params);
608 setUpdateStatus(bv, what);
609 bool flag = (((need_update != CURSOR) && (need_update != NONE)) ||
610 (lt->status() != LyXText::UNCHANGED) || lt->selection.set());
614 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
618 if (need_update == CURSOR)
620 bv->owner()->showState();
621 bv->owner()->updateMenubar();
622 bv->owner()->updateToolbar();
623 if (old_par != cpar(bv)) {
624 bv->owner()->setLayout(cpar(bv)->layout());
630 string const InsetText::editMessage() const
632 return _("Opened Text Inset");
636 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
638 UpdatableInset::edit(bv, x, y, button);
640 if (!bv->lockInset(this)) {
641 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
645 the_locking_inset = 0;
646 inset_pos = inset_x = inset_y = 0;
647 inset_boundary = false;
650 int tmp_y = (y < 0) ? 0 : y;
656 if (!checkAndActivateInset(bv, x, tmp_y, button))
657 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
659 lt->clearSelection();
661 // If the inset is empty set the language of the current font to the
662 // language to the surronding text (if different).
663 if (par->size() == 0 && !par->next() &&
664 bv->getParentLanguage(this) != lt->current_font.language())
666 LyXFont font(LyXFont::ALL_IGNORE);
667 font.setLanguage(bv->getParentLanguage(this));
668 setFont(bv, font, false);
675 if (drawFrame_ == LOCKED)
676 code = CURSOR|DRAW_FRAME;
677 updateLocal(bv, code, false);
681 void InsetText::edit(BufferView * bv, bool front)
683 UpdatableInset::edit(bv, front);
685 if (!bv->lockInset(this)) {
686 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
690 the_locking_inset = 0;
691 inset_pos = inset_x = inset_y = 0;
692 inset_boundary = false;
701 lt->setCursor(bv, par, 0);
706 // int const pos = (p->size() ? p->size()-1 : p->size());
707 lt->setCursor(bv, p, p->size());
709 lt->clearSelection();
711 // If the inset is empty set the language of the current font to the
712 // language to the surronding text (if different).
713 if (par->size() == 0 && !par->next() &&
714 bv->getParentLanguage(this) != lt->current_font.language()) {
715 LyXFont font(LyXFont::ALL_IGNORE);
716 font.setLanguage(bv->getParentLanguage(this));
717 setFont(bv, font, false);
723 if (drawFrame_ == LOCKED)
724 code = CURSOR|DRAW_FRAME;
725 updateLocal(bv, code, false);
729 void InsetText::insetUnlock(BufferView * bv)
731 if (the_locking_inset) {
732 the_locking_inset->insetUnlock(bv);
733 the_locking_inset = 0;
736 no_selection = false;
739 if (drawFrame_ == LOCKED)
740 code = CURSOR|CLEAR_FRAME;
748 if (lt->selection.set()) {
749 lt->clearSelection();
751 } else if (owner()) {
752 bv->owner()->setLayout(owner()->getLyXText(bv)
753 ->cursor.par()->layout());
755 bv->owner()->setLayout(bv->text->cursor.par()->layout());
756 // hack for deleteEmptyParMech
758 lt->setCursor(bv, par, 0);
759 } else if (par->next()) {
760 lt->setCursor(bv, par->next(), 0);
764 updateLocal(bv, code, false);
767 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
769 the_locking_inset = inset;
770 inset_x = cx(bv) - top_x + drawTextXOffset;
771 inset_y = cy(bv) + drawTextYOffset;
772 inset_pos = cpos(bv);
773 inset_par = cpar(bv);
774 inset_boundary = cboundary(bv);
775 updateLocal(bv, CURSOR, false);
779 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
781 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
785 if (!the_locking_inset) {
787 int const id = inset->id();
789 Paragraph::inset_iterator it =
790 p->inset_iterator_begin();
791 Paragraph::inset_iterator const end =
792 p->inset_iterator_end();
793 for (; it != end; ++it) {
794 if ((*it) == inset) {
795 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
796 lockInset(bv, inset);
799 if ((*it)->getInsetFromID(id)) {
800 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
802 return the_locking_inset->lockInsetInInset(bv, inset);
809 if (inset == cpar(bv)->getInset(cpos(bv))) {
810 lyxerr[Debug::INSETS] << "OK" << endl;
811 lockInset(bv, inset);
813 } else if (the_locking_inset && (the_locking_inset == inset)) {
814 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
815 lyxerr[Debug::INSETS] << "OK" << endl;
816 inset_x = cx(bv) - top_x + drawTextXOffset;
817 inset_y = cy(bv) + drawTextYOffset;
819 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
821 } else if (the_locking_inset) {
822 lyxerr[Debug::INSETS] << "MAYBE" << endl;
823 return the_locking_inset->lockInsetInInset(bv, inset);
825 lyxerr[Debug::INSETS] << "NOT OK" << endl;
830 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
833 if (!the_locking_inset)
835 if (the_locking_inset == inset) {
836 the_locking_inset->insetUnlock(bv);
837 getLyXText(bv)->updateInset(bv, inset);
838 the_locking_inset = 0;
840 moveRight(bv, false);
841 old_par = 0; // force layout setting
845 updateLocal(bv, CURSOR, false);
848 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
852 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
854 if (!autoBreakRows && par->next())
855 collapseParagraphs(bv->buffer()->params);
863 if (!the_locking_inset) {
864 bool found = lt->updateInset(bv, inset);
868 setUpdateStatus(bv, NONE);
871 if (the_locking_inset != inset) {
872 bool found = the_locking_inset->updateInsetInInset(bv, inset);
876 setUpdateStatus(bv, CURSOR_PAR);
879 bool found = lt->updateInset(bv, inset);
883 setUpdateStatus(bv, CURSOR_PAR);
884 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
885 inset_x = cx(bv) - top_x + drawTextXOffset;
886 inset_y = cy(bv) + drawTextYOffset;
893 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
897 // use this to check mouse motion for selection!
901 int tmp_x = x - drawTextXOffset;
902 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
903 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
906 if (the_locking_inset) {
907 if (the_locking_inset == inset) {
908 the_locking_inset->insetButtonPress(bv,
912 no_selection = false;
915 // otherwise unlock the_locking_inset and lock the new inset
916 the_locking_inset->insetUnlock(bv);
917 inset_x = cx(bv) - top_x + drawTextXOffset;
918 inset_y = cy(bv) + drawTextYOffset;
919 the_locking_inset = 0;
920 inset->insetButtonPress(bv, x - inset_x,
921 y - inset_y, button);
922 inset->edit(bv, x - inset_x, y - inset_y, button);
923 if (the_locking_inset)
924 updateLocal(bv, CURSOR, false);
925 no_selection = false;
928 // otherwise only unlock the_locking_inset
929 the_locking_inset->insetUnlock(bv);
930 the_locking_inset = 0;
932 if (bv->theLockingInset()) {
933 if (isHighlyEditableInset(inset)) {
934 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
935 inset_x = cx(bv) - top_x + drawTextXOffset;
936 inset_y = cy(bv) + drawTextYOffset;
937 inset_pos = cpos(bv);
938 inset_par = cpar(bv);
939 inset_boundary = cboundary(bv);
940 the_locking_inset = uinset;
941 uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
943 uinset->edit(bv, x - inset_x, y - inset_y, 0);
944 if (the_locking_inset)
945 updateLocal(bv, CURSOR, false);
946 no_selection = false;
950 if (!inset) { // && (button == 2)) {
951 bool paste_internally = false;
952 if ((button == 2) && getLyXText(bv)->selection.set()) {
953 localDispatch(bv, LFUN_COPY, "");
954 paste_internally = true;
962 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
964 // set the selection cursor!
965 lt->selection.cursor = lt->cursor;
966 lt->cursor.x_fix(lt->cursor.x());
968 if (lt->selection.set()) {
969 lt->clearSelection();
972 updateLocal(bv, FULL, false);
974 lt->clearSelection();
977 updateLocal(bv, CURSOR, false);
979 bv->owner()->setLayout(cpar(bv)->layout());
981 // Insert primary selection with middle mouse
982 // if there is a local selection in the current buffer,
985 if (paste_internally)
986 localDispatch(bv, LFUN_PASTE, "");
988 localDispatch(bv, LFUN_PASTESELECTION,
992 getLyXText(bv)->clearSelection();
995 no_selection = false;
999 bool InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
1001 if (the_locking_inset) {
1002 return the_locking_inset->insetButtonRelease(bv,
1003 x - inset_x, y - inset_y,
1006 int tmp_x = x - drawTextXOffset;
1007 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
1008 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1011 if (isHighlyEditableInset(inset)) {
1012 ret = inset->insetButtonRelease(bv, x - inset_x,
1013 y - inset_y, button);
1015 inset_x = cx(bv) - top_x + drawTextXOffset;
1016 inset_y = cy(bv) + drawTextYOffset;
1017 ret = inset->insetButtonRelease(bv, x - inset_x,
1018 y - inset_y, button);
1019 inset->edit(bv, x - inset_x,
1020 y - inset_y, button);
1022 updateLocal(bv, CURSOR_PAR, false);
1028 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
1030 if (no_selection || ((mouse_x == x) && (mouse_y == y)))
1032 if (the_locking_inset) {
1033 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1039 lt = getLyXText(bv);
1042 hideInsetCursor(bv);
1043 LyXCursor cur = lt->cursor;
1044 lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1045 if (cur == lt->cursor) {
1050 lt->setSelection(bv);
1051 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1052 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1056 updateLocal(bv, SELECTION, false);
1058 showInsetCursor(bv);
1062 void InsetText::insetKeyPress(XKeyEvent * xke)
1064 if (the_locking_inset) {
1065 the_locking_inset->insetKeyPress(xke);
1071 UpdatableInset::RESULT
1072 InsetText::localDispatch(BufferView * bv,
1073 kb_action action, string const & arg)
1075 bool was_empty = par->size() == 0 && !par->next();
1076 no_selection = false;
1077 UpdatableInset::RESULT
1078 result= UpdatableInset::localDispatch(bv, action, arg);
1079 if (result != UNDISPATCHED) {
1083 result = DISPATCHED;
1084 if ((action < 0) && arg.empty())
1087 if (the_locking_inset) {
1088 result = the_locking_inset->localDispatch(bv, action, arg);
1089 if (result == DISPATCHED_NOUPDATE)
1091 else if (result == DISPATCHED) {
1092 updateLocal(bv, CURSOR_PAR, false);
1094 } else if (result >= FINISHED) {
1096 case FINISHED_RIGHT:
1097 moveRightIntern(bv, false, false);
1098 result = DISPATCHED;
1101 if ((result = moveUp(bv)) >= FINISHED) {
1102 updateLocal(bv, CURSOR, false);
1103 bv->unlockInset(this);
1107 if ((result = moveDown(bv)) >= FINISHED) {
1108 updateLocal(bv, CURSOR, false);
1109 bv->unlockInset(this);
1113 result = DISPATCHED;
1116 the_locking_inset = 0;
1117 #ifdef WITH_WARNINGS
1118 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
1123 hideInsetCursor(bv);
1126 lt = getLyXText(bv);
1130 int updflag = false;
1133 case LFUN_SELFINSERT:
1134 if (bv->buffer()->isReadonly()) {
1135 // setErrorMessage(N_("Document is read only"));
1139 /* Automatically delete the currently selected
1140 * text and replace it with what is being
1141 * typed in now. Depends on lyxrc settings
1142 * "auto_region_delete", which defaults to
1145 setUndo(bv, Undo::INSERT,
1146 lt->cursor.par(), lt->cursor.par()->next());
1148 if (lyxrc.auto_region_delete) {
1149 if (lt->selection.set()) {
1150 lt->cutSelection(bv, false);
1153 lt->clearSelection();
1154 for (string::size_type i = 0; i < arg.length(); ++i) {
1155 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1158 lt->selection.cursor = lt->cursor;
1159 updwhat = CURSOR_PAR;
1161 result = DISPATCHED_NOUPDATE;
1163 // --- Cursor Movements -----------------------------------
1166 moveRight(bv, false, true);
1167 lt->setSelection(bv);
1168 updwhat = SELECTION;
1171 result = moveRight(bv);
1177 moveLeft(bv, false, true);
1178 lt->setSelection(bv);
1179 updwhat = SELECTION;
1183 result = moveLeft(bv);
1189 lt->setSelection(bv);
1190 updwhat = SELECTION;
1194 result = moveDown(bv);
1200 lt->setSelection(bv);
1201 updwhat = SELECTION;
1205 result = moveUp(bv);
1217 case LFUN_BACKSPACE: {
1218 setUndo(bv, Undo::DELETE,
1219 lt->cursor.par(), lt->cursor.par()->next());
1220 if (lt->selection.set())
1221 lt->cutSelection(bv);
1224 updwhat = CURSOR_PAR;
1230 setUndo(bv, Undo::DELETE,
1231 lt->cursor.par(), lt->cursor.par()->next());
1232 if (lt->selection.set()) {
1233 lt->cutSelection(bv);
1237 updwhat = CURSOR_PAR;
1243 setUndo(bv, Undo::DELETE,
1244 lt->cursor.par(), lt->cursor.par()->next());
1245 lt->cutSelection(bv);
1246 updwhat = CURSOR_PAR;
1253 lt->copySelection(bv);
1254 updwhat = CURSOR_PAR;
1256 case LFUN_PASTESELECTION:
1258 string const clip(bv->getClipboard());
1262 if (arg == "paragraph") {
1263 lt->insertStringAsParagraphs(bv, clip);
1265 lt->insertStringAsLines(bv, clip);
1267 updwhat = CURSOR_PAR;
1272 if (!autoBreakRows) {
1274 if (CutAndPaste::nrOfParagraphs() > 1) {
1275 Alert::alert(_("Impossible operation"),
1276 _("Cannot include more than one paragraph!"),
1281 setUndo(bv, Undo::INSERT,
1282 lt->cursor.par(), lt->cursor.par()->next());
1283 lt->pasteSelection(bv);
1284 updwhat = CURSOR_PAR;
1289 case LFUN_BREAKPARAGRAPH:
1290 if (!autoBreakRows) {
1291 result = DISPATCHED;
1294 lt->breakParagraph(bv, 0);
1298 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1299 if (!autoBreakRows) {
1300 result = DISPATCHED;
1303 lt->breakParagraph(bv, 1);
1308 case LFUN_BREAKLINE: {
1309 if (!autoBreakRows) {
1310 result = DISPATCHED;
1313 setUndo(bv, Undo::INSERT,
1314 lt->cursor.par(), lt->cursor.par()->next());
1315 lt->insertChar(bv, Paragraph::META_NEWLINE);
1316 updwhat = CURSOR_PAR;
1322 // do not set layouts on non breakable textinsets
1323 if (autoBreakRows) {
1324 string cur_layout = cpar(bv)->layout();
1326 // Derive layout number from given argument (string)
1327 // and current buffer's textclass (number). */
1328 textclass_type tclass = bv->buffer()->params.textclass;
1329 string layout = arg;
1330 bool hasLayout = textclasslist[tclass].hasLayout(layout);
1332 // If the entry is obsolete, use the new one instead.
1334 string const & obs =
1335 textclasslist[tclass][layout].
1341 // see if we found the layout number:
1343 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1344 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1348 if (cur_layout != layout) {
1349 cur_layout = layout;
1350 lt->setLayout(bv, layout);
1351 bv->owner()->setLayout(cpar(bv)->layout());
1352 updwhat = CURSOR_PAR;
1356 // reset the layout box
1357 bv->owner()->setLayout(cpar(bv)->layout());
1360 case LFUN_PARAGRAPH_SPACING:
1361 // This one is absolutely not working. When fiddling with this
1362 // it also seems to me that the paragraphs inside the insettext
1363 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1365 Paragraph * par = lt->cursor.par();
1366 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1367 float cur_value = 1.0;
1368 if (cur_spacing == Spacing::Other) {
1369 cur_value = par->params().spacing().getValue();
1372 istringstream istr(arg.c_str());
1375 Spacing::Space new_spacing = cur_spacing;
1376 float new_value = cur_value;
1378 lyxerr << "Missing argument to `paragraph-spacing'"
1380 } else if (tmp == "single") {
1381 new_spacing = Spacing::Single;
1382 } else if (tmp == "onehalf") {
1383 new_spacing = Spacing::Onehalf;
1384 } else if (tmp == "double") {
1385 new_spacing = Spacing::Double;
1386 } else if (tmp == "other") {
1387 new_spacing = Spacing::Other;
1390 lyxerr << "new_value = " << tmpval << endl;
1393 } else if (tmp == "default") {
1394 new_spacing = Spacing::Default;
1396 lyxerr << _("Unknown spacing argument: ")
1399 if (cur_spacing != new_spacing || cur_value != new_value) {
1400 par->params().spacing(Spacing(new_spacing, new_value));
1401 updwhat = CURSOR_PAR;
1408 if (!bv->Dispatch(action, arg))
1409 result = UNDISPATCHED;
1416 updateLocal(bv, updwhat, updflag);
1417 /// If the action has deleted all text in the inset, we need to change the
1418 // language to the language of the surronding text.
1419 if (!was_empty && par->size() == 0 && !par->next()) {
1420 LyXFont font(LyXFont::ALL_IGNORE);
1421 font.setLanguage(bv->getParentLanguage(this));
1422 setFont(bv, font, false);
1425 if (result < FINISHED) {
1426 showInsetCursor(bv);
1428 bv->unlockInset(this);
1433 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1436 buf->latexParagraphs(os, par, 0, texrow);
1437 return texrow.rows();
1441 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1443 Paragraph * p = par;
1444 unsigned int lines = 0;
1447 string const tmp = buf->asciiParagraph(p, linelen, p->previous()==0);
1448 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1456 int InsetText::docbook(Buffer const * buf, ostream & os) const
1458 Paragraph * p = par;
1459 unsigned int lines = 0;
1461 vector<string> environment_stack(10);
1462 vector<string> environment_inner(10);
1464 int const command_depth = 0;
1467 Paragraph::depth_type depth = 0; // paragraph depth
1471 int desc_on = 0; // description mode
1473 LyXLayout const & style =
1474 textclasslist[buf->params.textclass][p->layout()];
1476 // environment tag closing
1477 for (; depth > p->params().depth(); --depth) {
1478 if (environment_inner[depth] != "!-- --") {
1479 item_name = "listitem";
1480 buf->sgmlCloseTag(os, command_depth + depth,
1482 if (environment_inner[depth] == "varlistentry")
1483 buf->sgmlCloseTag(os, depth+command_depth,
1484 environment_inner[depth]);
1486 buf->sgmlCloseTag(os, depth + command_depth,
1487 environment_stack[depth]);
1488 environment_stack[depth].erase();
1489 environment_inner[depth].erase();
1492 if (depth == p->params().depth()
1493 && environment_stack[depth] != style.latexname()
1494 && !environment_stack[depth].empty()) {
1495 if (environment_inner[depth] != "!-- --") {
1496 item_name= "listitem";
1497 buf->sgmlCloseTag(os, command_depth+depth,
1499 if (environment_inner[depth] == "varlistentry")
1500 buf->sgmlCloseTag(os,
1501 depth + command_depth,
1502 environment_inner[depth]);
1505 buf->sgmlCloseTag(os, depth + command_depth,
1506 environment_stack[depth]);
1508 environment_stack[depth].erase();
1509 environment_inner[depth].erase();
1512 // Write opening SGML tags.
1513 switch (style.latextype) {
1514 case LATEX_PARAGRAPH:
1515 buf->sgmlOpenTag(os, depth + command_depth,
1520 buf->sgmlError(p, 0,
1521 _("Error : LatexType Command not allowed here.\n"));
1525 case LATEX_ENVIRONMENT:
1526 case LATEX_ITEM_ENVIRONMENT:
1527 if (depth < p->params().depth()) {
1528 depth = p->params().depth();
1529 environment_stack[depth].erase();
1532 if (environment_stack[depth] != style.latexname()) {
1533 if (environment_stack.size() == depth + 1) {
1534 environment_stack.push_back("!-- --");
1535 environment_inner.push_back("!-- --");
1537 environment_stack[depth] = style.latexname();
1538 environment_inner[depth] = "!-- --";
1539 buf->sgmlOpenTag(os, depth + command_depth,
1540 environment_stack[depth]);
1542 if (environment_inner[depth] != "!-- --") {
1543 item_name= "listitem";
1544 buf->sgmlCloseTag(os,
1545 command_depth + depth,
1547 if (environment_inner[depth] == "varlistentry")
1548 buf->sgmlCloseTag(os,
1549 depth + command_depth,
1550 environment_inner[depth]);
1554 if (style.latextype == LATEX_ENVIRONMENT) {
1555 if (!style.latexparam().empty()) {
1556 if (style.latexparam() == "CDATA")
1559 buf->sgmlOpenTag(os, depth + command_depth,
1560 style.latexparam());
1565 desc_on = (style.labeltype == LABEL_MANUAL);
1568 environment_inner[depth]= "varlistentry";
1570 environment_inner[depth]= "listitem";
1572 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1573 environment_inner[depth]);
1577 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1581 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1586 buf->sgmlOpenTag(os, depth + command_depth,
1591 buf->simpleDocBookOnePar(os, p, desc_on,
1592 depth + 1 + command_depth);
1596 // write closing SGML tags
1597 switch (style.latextype) {
1598 case LATEX_ENVIRONMENT:
1599 if (!style.latexparam().empty()) {
1600 if (style.latexparam() == "CDATA")
1603 buf->sgmlCloseTag(os, depth + command_depth,
1604 style.latexparam());
1607 case LATEX_ITEM_ENVIRONMENT:
1608 if (desc_on == 1) break;
1610 buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1612 case LATEX_PARAGRAPH:
1613 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1616 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1622 for (int d = depth; d >= 0; --d) {
1623 if (!environment_stack[depth].empty()) {
1624 if (environment_inner[depth] != "!-- --") {
1625 item_name = "listitem";
1626 buf->sgmlCloseTag(os, command_depth + depth,
1628 if (environment_inner[depth] == "varlistentry")
1629 buf->sgmlCloseTag(os, depth + command_depth,
1630 environment_inner[depth]);
1633 buf->sgmlCloseTag(os, depth + command_depth,
1634 environment_stack[depth]);
1642 void InsetText::validate(LaTeXFeatures & features) const
1644 Paragraph * p = par;
1646 p->validate(features);
1652 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1654 if (textclasslist[buf->params.textclass][p->layout()].labeltype != LABEL_MANUAL)
1657 return p->beginningOfMainBody();
1661 void InsetText::getCursorPos(BufferView * bv,
1662 int & x, int & y) const
1664 if (the_locking_inset) {
1665 the_locking_inset->getCursorPos(bv, x, y);
1673 unsigned int InsetText::insetInInsetY()
1675 if (!the_locking_inset)
1678 return (inset_y + the_locking_inset->insetInInsetY());
1682 void InsetText::toggleInsetCursor(BufferView * bv)
1684 if (the_locking_inset) {
1685 the_locking_inset->toggleInsetCursor(bv);
1689 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1691 int const asc = lyxfont::maxAscent(font);
1692 int const desc = lyxfont::maxDescent(font);
1694 if (isCursorVisible())
1695 bv->hideLockedInsetCursor();
1697 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1698 toggleCursorVisible();
1702 void InsetText::showInsetCursor(BufferView * bv, bool show)
1704 if (the_locking_inset) {
1705 the_locking_inset->showInsetCursor(bv, show);
1708 if (!isCursorVisible()) {
1709 LyXFont const font =
1710 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1712 int const asc = lyxfont::maxAscent(font);
1713 int const desc = lyxfont::maxDescent(font);
1715 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1717 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1718 setCursorVisible(true);
1723 void InsetText::hideInsetCursor(BufferView * bv)
1725 if (isCursorVisible()) {
1726 bv->hideLockedInsetCursor();
1727 setCursorVisible(false);
1729 if (the_locking_inset)
1730 the_locking_inset->hideInsetCursor(bv);
1734 void InsetText::fitInsetCursor(BufferView * bv) const
1736 if (the_locking_inset) {
1737 the_locking_inset->fitInsetCursor(bv);
1740 LyXFont const font =
1741 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1743 int const asc = lyxfont::maxAscent(font);
1744 int const desc = lyxfont::maxDescent(font);
1746 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1750 UpdatableInset::RESULT
1751 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1753 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1754 return moveLeftIntern(bv, false, activate_inset, selecting);
1756 return moveRightIntern(bv, false, activate_inset, selecting);
1760 UpdatableInset::RESULT
1761 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1763 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1764 return moveRightIntern(bv, true, activate_inset, selecting);
1766 return moveLeftIntern(bv, true, activate_inset, selecting);
1770 UpdatableInset::RESULT
1771 InsetText::moveRightIntern(BufferView * bv, bool behind,
1772 bool activate_inset, bool selecting)
1774 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1775 return FINISHED_RIGHT;
1776 if (activate_inset && checkAndActivateInset(bv, behind))
1778 getLyXText(bv)->cursorRight(bv);
1780 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1781 return DISPATCHED_NOUPDATE;
1785 UpdatableInset::RESULT
1786 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1787 bool activate_inset, bool selecting)
1789 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1791 getLyXText(bv)->cursorLeft(bv);
1793 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1794 if (activate_inset && checkAndActivateInset(bv, behind))
1796 return DISPATCHED_NOUPDATE;
1800 UpdatableInset::RESULT
1801 InsetText::moveUp(BufferView * bv)
1803 if (!crow(bv)->previous())
1805 getLyXText(bv)->cursorUp(bv);
1806 return DISPATCHED_NOUPDATE;
1810 UpdatableInset::RESULT
1811 InsetText::moveDown(BufferView * bv)
1813 if (!crow(bv)->next())
1814 return FINISHED_DOWN;
1815 getLyXText(bv)->cursorDown(bv);
1816 return DISPATCHED_NOUPDATE;
1820 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1822 if (the_locking_inset) {
1823 if (the_locking_inset->insetAllowed(inset))
1824 return the_locking_inset->insertInset(bv, inset);
1829 lt = getLyXText(bv);
1832 setUndo(bv, Undo::FINISH, lt->cursor.par(), lt->cursor.par()->next());
1834 inset->setOwner(this);
1835 hideInsetCursor(bv);
1836 lt->insertInset(bv, inset);
1840 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1846 bool InsetText::insetAllowed(Inset::Code code) const
1848 // in_insetAllowed is a really gross hack,
1849 // to allow us to call the owner's insetAllowed
1850 // without stack overflow, which can happen
1851 // when the owner uses InsetCollapsable::insetAllowed()
1853 if (in_insetAllowed)
1855 in_insetAllowed = true;
1856 if (the_locking_inset)
1857 ret = the_locking_inset->insetAllowed(code);
1859 ret = owner()->insetAllowed(code);
1860 in_insetAllowed = false;
1865 UpdatableInset * InsetText::getLockingInset() const
1867 return the_locking_inset ? the_locking_inset->getLockingInset() :
1868 const_cast<InsetText *>(this);
1872 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1876 if (the_locking_inset)
1877 return the_locking_inset->getFirstLockingInsetOfType(c);
1882 bool InsetText::showInsetDialog(BufferView * bv) const
1884 if (the_locking_inset)
1885 return the_locking_inset->showInsetDialog(bv);
1890 vector<string> const InsetText::getLabelList() const
1892 vector<string> label_list;
1894 Paragraph * tpar = par;
1896 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1897 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1898 for (; beg != end; ++beg) {
1899 vector<string> const l = (*beg)->getLabelList();
1900 label_list.insert(label_list.end(), l.begin(), l.end());
1902 tpar = tpar->next();
1908 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1911 if (the_locking_inset) {
1912 the_locking_inset->setFont(bv, font, toggleall, selectall);
1915 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
1916 getLyXText(bv)->setFont(bv, font, toggleall);
1921 lt = getLyXText(bv);
1924 if (lt->selection.set()) {
1925 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1929 lt->toggleFree(bv, font, toggleall);
1931 lt->clearSelection();
1933 bool flag = (selectall || lt->selection.set());
1937 updateLocal(bv, FULL, true);
1939 updateLocal(bv, CURSOR_PAR, true);
1943 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1945 if (cpar(bv)->isInset(cpos(bv))) {
1949 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1950 if (!isHighlyEditableInset(inset))
1952 LyXFont const font =
1953 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1955 x = inset->width(bv, font);
1956 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1959 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1961 //inset_x = cx(bv) - top_x + drawTextXOffset;
1962 //inset_y = cy(bv) + drawTextYOffset;
1963 inset->edit(bv, x, y, 0);
1964 if (!the_locking_inset)
1966 updateLocal(bv, CURSOR, false);
1973 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1976 x -= drawTextXOffset;
1978 int dummyy = y + insetAscent;
1979 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
1986 inset_x = cx(bv) - top_x + drawTextXOffset;
1987 inset_y = cy(bv) + drawTextYOffset;
1988 inset->edit(bv, x - inset_x, y - inset_y, button);
1989 if (!the_locking_inset)
1991 updateLocal(bv, CURSOR, false);
1998 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
2001 int w = UpdatableInset::getMaxWidth(bv, inset);
2006 w = w - top_x + owner()->x();
2009 w -= (2 * TEXT_TO_INSET_OFFSET);
2012 return UpdatableInset::getMaxWidth(bv, inset);
2017 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2019 // we have to unlock any locked inset otherwise we're in troubles
2020 the_locking_inset = 0;
2022 Paragraph * tmp = par->next();
2027 par = new Paragraph(*p, same_id);
2028 par->setInsetOwner(this);
2029 Paragraph * np = par;
2032 np->next(new Paragraph(*p, same_id));
2033 np->next()->previous(np);
2035 np->setInsetOwner(this);
2042 void InsetText::setText(string const & data)
2045 LyXFont font(LyXFont::ALL_SANE);
2046 for (unsigned int i=0; i < data.length(); ++i)
2047 par->insertChar(i, data[i], font);
2051 void InsetText::setAutoBreakRows(bool flag)
2053 if (flag != autoBreakRows) {
2054 autoBreakRows = flag;
2062 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2064 if (how != drawFrame_) {
2067 updateLocal(bv, DRAW_FRAME, false);
2072 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2074 if (frame_color != col) {
2077 updateLocal(bv, DRAW_FRAME, false);
2082 int InsetText::cx(BufferView * bv) const
2084 // we do nothing dangerous so we use a local cache
2085 LyXText * llt = getLyXText(bv);
2086 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2087 if (the_locking_inset) {
2088 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2090 if (font.isVisibleRightToLeft())
2091 x -= the_locking_inset->width(bv, font);
2097 int InsetText::cy(BufferView * bv) const
2100 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2104 pos_type InsetText::cpos(BufferView * bv) const
2106 return getLyXText(bv)->cursor.pos();
2110 Paragraph * InsetText::cpar(BufferView * bv) const
2112 return getLyXText(bv)->cursor.par();
2116 bool InsetText::cboundary(BufferView * bv) const
2118 return getLyXText(bv)->cursor.boundary();
2122 Row * InsetText::crow(BufferView * bv) const
2124 return getLyXText(bv)->cursor.row();
2128 LyXText * InsetText::getLyXText(BufferView const * lbv,
2129 bool const recursive) const
2131 if (!recursive && (cached_bview == lbv)) {
2132 LyXText * lt = cached_text.get();
2133 lyx::Assert(lt && lt->firstRow()->par() == par);
2137 // Super UGLY! (Lgb)
2138 BufferView * bv = const_cast<BufferView *>(lbv);
2141 Cache::iterator it = cache.find(bv);
2143 if (it != cache.end()) {
2146 } else if (do_resize) {
2147 resizeLyXText(do_resize);
2149 if (lt || !it->second.remove) {
2150 lyx::Assert(it->second.text.get());
2151 cached_text = it->second.text;
2152 if (recursive && the_locking_inset) {
2153 return the_locking_inset->getLyXText(bv, true);
2155 return cached_text.get();
2156 } else if (it->second.remove) {
2158 saveLyXTextState(it->second.text.get());
2164 // when we have to reinit the existing LyXText!
2166 it->second.text->init(bv);
2167 restoreLyXTextState(bv, it->second.text.get());
2168 it->second.remove = false;
2170 cached_text = it->second.text;
2171 if (the_locking_inset && recursive) {
2172 return the_locking_inset->getLyXText(bv);
2174 return cached_text.get();
2177 // we are here only if we don't have a BufferView * in the cache!!!
2179 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2180 cached_text->init(bv);
2181 restoreLyXTextState(bv, cached_text.get());
2183 cache.insert(make_pair(bv, cached_text));
2185 if (the_locking_inset && recursive) {
2186 return the_locking_inset->getLyXText(bv);
2188 return cached_text.get();
2192 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2196 Cache::iterator it = cache.find(bv);
2198 if (it == cache.end()) {
2202 lyx::Assert(it->second.text.get());
2204 it->second.remove = true;
2206 /// then remove all LyXText in text-insets
2207 Paragraph * p = par;
2208 for (; p; p = p->next()) {
2209 p->deleteInsetsLyXText(bv);
2215 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2218 // we cannot resize this because we are in use!
2219 // so do this on the next possible getLyXText()
2224 // lyxerr << "InsetText::resizeLyXText\n";
2225 if (!par->next() && !par->size()) { // no data, resize not neccessary!
2226 // we have to do this as a fixed width may have changed!
2227 LyXText * t = getLyXText(bv);
2228 saveLyXTextState(t);
2230 restoreLyXTextState(bv, t);
2233 // one endless line, resize normally not necessary
2234 if (!force && getMaxWidth(bv, this) < 0)
2237 Cache::iterator it = cache.find(bv);
2238 if (it == cache.end()) {
2241 lyx::Assert(it->second.text.get());
2243 LyXText * t = it->second.text.get();
2244 saveLyXTextState(t);
2245 for (Paragraph * p = par; p; p = p->next()) {
2246 p->resizeInsetsLyXText(bv);
2249 restoreLyXTextState(bv, t);
2250 if (the_locking_inset) {
2251 inset_x = cx(bv) - top_x + drawTextXOffset;
2252 inset_y = cy(bv) + drawTextYOffset;
2256 t->first_y = bv->screen()->topCursorVisible(t);
2259 updateLocal(bv, FULL, false);
2260 // this will scroll the screen such that the cursor becomes visible
2261 bv->updateScrollbar();
2263 need_update |= FULL;
2268 void InsetText::reinitLyXText() const
2271 // we cannot resize this because we are in use!
2272 // so do this on the next possible getLyXText()
2278 // lyxerr << "InsetText::reinitLyXText\n";
2279 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2280 lyx::Assert(it->second.text.get());
2282 LyXText * t = it->second.text.get();
2283 BufferView * bv = it->first;
2285 saveLyXTextState(t);
2286 for (Paragraph * p = par; p; p = p->next()) {
2287 p->resizeInsetsLyXText(bv);
2290 restoreLyXTextState(bv, t);
2291 if (the_locking_inset) {
2292 inset_x = cx(bv) - top_x + drawTextXOffset;
2293 inset_y = cy(bv) + drawTextYOffset;
2296 t->first_y = bv->screen()->topCursorVisible(t);
2299 updateLocal(bv, FULL, false);
2300 // this will scroll the screen such that the cursor becomes visible
2301 bv->updateScrollbar();
2309 void InsetText::removeNewlines()
2311 bool changed = false;
2313 for (Paragraph * p = par; p; p = p->next()) {
2314 for (int i = 0; i < p->size(); ++i) {
2315 if (p->getChar(i) == Paragraph::META_NEWLINE) {
2326 bool InsetText::nodraw() const
2328 if (the_locking_inset)
2329 return the_locking_inset->nodraw();
2330 return UpdatableInset::nodraw();
2334 int InsetText::scroll(bool recursive) const
2336 int sx = UpdatableInset::scroll(false);
2338 if (recursive && the_locking_inset)
2339 sx += the_locking_inset->scroll(recursive);
2345 bool InsetText::doClearArea() const
2347 return !locked || (need_update & (FULL|INIT));
2351 void InsetText::selectAll(BufferView * bv)
2353 getLyXText(bv)->cursorTop(bv);
2354 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2355 getLyXText(bv)->cursorBottom(bv);
2356 getLyXText(bv)->setSelection(bv);
2360 void InsetText::clearSelection(BufferView * bv)
2362 getLyXText(bv)->clearSelection();
2366 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2368 Painter & pain = bv->painter();
2370 int h = insetAscent + insetDescent;
2371 int ty = baseline - insetAscent;
2377 if ((ty + h) > pain.paperHeight())
2378 h = pain.paperHeight();
2379 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2380 w = pain.paperWidth();
2381 // w -= TEXT_TO_INSET_OFFSET;
2382 pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2385 frame_is_visible = false;
2389 Paragraph * InsetText::getParFromID(int id) const
2392 Paragraph * result = par;
2393 Paragraph * ires = 0;
2394 while (result && result->id() != id) {
2395 if ((ires = result->getParFromID(id)))
2397 result = result->next();
2401 Paragraph * tmp = par;
2403 if (tmp->id() == id) {
2406 Paragraph * tmp2 = tmp->getParFromID(id);
2417 Paragraph * InsetText::firstParagraph() const
2420 if (the_locking_inset)
2421 if ((result = the_locking_inset->firstParagraph()))
2427 Paragraph * InsetText::getFirstParagraph(int i) const
2429 return (i == 0) ? par : 0;
2433 LyXCursor const & InsetText::cursor(BufferView * bv) const
2435 if (the_locking_inset)
2436 return the_locking_inset->cursor(bv);
2437 return getLyXText(bv)->cursor;
2441 Paragraph * InsetText::paragraph() const
2447 void InsetText::paragraph(Paragraph * p)
2449 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2450 // caller of this function has to take care of it. This IS important
2451 // as we could have to insert a paragraph before this one and just
2452 // link the actual to a new ones next and set it with this function
2455 // set ourself as owner for all the paragraphs inserted!
2456 Paragraph * np = par;
2458 np->setInsetOwner(this);
2462 // redraw myself when asked for
2467 Inset * InsetText::getInsetFromID(int id_arg) const
2470 return const_cast<InsetText *>(this);
2472 Paragraph * lp = par;
2475 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2476 en = lp->inset_iterator_end();
2479 if ((*it)->id() == id_arg)
2481 Inset * in = (*it)->getInsetFromID(id_arg);
2491 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2497 lt = getLyXText(bv);
2500 if (the_locking_inset) {
2501 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2508 // we have to go on checking so move cusor to the next char
2509 lt->cursor.pos(lt->cursor.pos() + 1);
2511 str = lt->selectNextWordToSpellcheck(bv, value);
2513 bv->unlockInset(const_cast<InsetText *>(this));
2522 void InsetText::selectSelectedWord(BufferView * bv)
2524 if (the_locking_inset) {
2525 the_locking_inset->selectSelectedWord(bv);
2528 getLyXText(bv)->selectSelectedWord(bv);
2529 updateLocal(bv, SELECTION, false);
2533 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2535 if (the_locking_inset) {
2536 the_locking_inset->toggleSelection(bv, kill_selection);
2540 lt = getLyXText(bv);
2544 int x = top_x + TEXT_TO_INSET_OFFSET;
2546 Row * row = lt->firstRow();
2547 int y_offset = top_baseline - row->ascent_of_text();
2549 while ((row != 0) && ((y+row->height()) <= 0)) {
2556 if (need_update & SELECTION)
2558 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2564 bool InsetText::searchForward(BufferView * bv, string const & str,
2567 if (the_locking_inset) {
2568 if (the_locking_inset->searchForward(bv, str, cs, mw))
2572 lt = getLyXText(bv);
2575 Paragraph * lpar = lt->cursor.par();
2576 pos_type pos = lt->cursor.pos();
2577 if (pos < lpar->size() - 1)
2581 lpar = lpar->next();
2586 // we have to unlock ourself in this function by default!
2587 bv->unlockInset(const_cast<InsetText *>(this));
2590 lt->setCursor(bv, lpar, pos);
2594 if (LyXFind(bv, str, true, true, cs , mw)) {
2597 // we have to unlock ourself in this function by default!
2598 bv->unlockInset(const_cast<InsetText *>(this));
2602 bool InsetText::searchBackward(BufferView * bv, string const & str,
2605 if (the_locking_inset)
2606 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2608 if (LyXFind(bv, str, false, true, cs, mw)) {
2611 // we have to unlock ourself in this function by default!
2612 bv->unlockInset(const_cast<InsetText *>(this));
2617 bool InsetText::checkInsertChar(LyXFont & font)
2620 return owner()->checkInsertChar(font);
2625 void InsetText::collapseParagraphs(BufferParams const & bparams) const
2627 while(par->next()) {
2628 if (!par->isSeparator(par->size()-1))
2629 par->insertChar(par->size()-1, ' ');
2630 par->pasteParagraph(bparams);
2636 void InsetText::getDrawFont(LyXFont & font) const
2640 owner()->getDrawFont(font);