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);
671 updateLocal(bv, CURSOR, false);
675 void InsetText::edit(BufferView * bv, bool front)
677 UpdatableInset::edit(bv, front);
679 if (!bv->lockInset(this)) {
680 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
684 the_locking_inset = 0;
685 inset_pos = inset_x = inset_y = 0;
686 inset_boundary = false;
695 lt->setCursor(bv, par, 0);
700 // int const pos = (p->size() ? p->size()-1 : p->size());
701 lt->setCursor(bv, p, p->size());
703 lt->clearSelection();
705 // If the inset is empty set the language of the current font to the
706 // language to the surronding text (if different).
707 if (par->size() == 0 && !par->next() &&
708 bv->getParentLanguage(this) != lt->current_font.language()) {
709 LyXFont font(LyXFont::ALL_IGNORE);
710 font.setLanguage(bv->getParentLanguage(this));
711 setFont(bv, font, false);
716 updateLocal(bv, CURSOR, false);
720 void InsetText::insetUnlock(BufferView * bv)
722 if (the_locking_inset) {
723 the_locking_inset->insetUnlock(bv);
724 the_locking_inset = 0;
727 no_selection = false;
730 if (drawFrame_ == LOCKED)
731 code = CURSOR|CLEAR_FRAME;
739 if (lt->selection.set()) {
740 lt->clearSelection();
742 } else if (owner()) {
743 bv->owner()->setLayout(owner()->getLyXText(bv)
744 ->cursor.par()->getLayout());
746 bv->owner()->setLayout(bv->text->cursor.par()->getLayout());
747 // hack for deleteEmptyParMech
748 lt->setCursor(bv, par, 0);
751 updateLocal(bv, code, false);
754 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
756 the_locking_inset = inset;
757 inset_x = cx(bv) - top_x + drawTextXOffset;
758 inset_y = cy(bv) + drawTextYOffset;
759 inset_pos = cpos(bv);
760 inset_par = cpar(bv);
761 inset_boundary = cboundary(bv);
762 updateLocal(bv, CURSOR, false);
766 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
768 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
772 if (!the_locking_inset) {
774 int const id = inset->id();
776 Paragraph::inset_iterator it =
777 p->inset_iterator_begin();
778 Paragraph::inset_iterator const end =
779 p->inset_iterator_end();
780 for (; it != end; ++it) {
781 if ((*it) == inset) {
782 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
783 lockInset(bv, inset);
786 if ((*it)->getInsetFromID(id)) {
787 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
788 lockInset(bv, static_cast<UpdatableInset *>(*it));
789 return the_locking_inset->lockInsetInInset(bv, inset);
796 if (inset == cpar(bv)->getInset(cpos(bv))) {
797 lyxerr[Debug::INSETS] << "OK" << endl;
798 lockInset(bv, inset);
800 } else if (the_locking_inset && (the_locking_inset == inset)) {
801 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
802 lyxerr[Debug::INSETS] << "OK" << endl;
803 inset_x = cx(bv) - top_x + drawTextXOffset;
804 inset_y = cy(bv) + drawTextYOffset;
806 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
808 } else if (the_locking_inset) {
809 lyxerr[Debug::INSETS] << "MAYBE" << endl;
810 return the_locking_inset->lockInsetInInset(bv, inset);
812 lyxerr[Debug::INSETS] << "NOT OK" << endl;
817 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
820 if (!the_locking_inset)
822 if (the_locking_inset == inset) {
823 the_locking_inset->insetUnlock(bv);
824 getLyXText(bv)->updateInset(bv, inset);
825 the_locking_inset = 0;
827 moveRight(bv, false);
828 old_par = 0; // force layout setting
832 updateLocal(bv, CURSOR, false);
835 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
839 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
841 if (!autoBreakRows && par->next())
842 collapseParagraphs(bv->buffer()->params);
850 if (!the_locking_inset) {
851 bool found = lt->updateInset(bv, inset);
855 setUpdateStatus(bv, NONE);
858 if (the_locking_inset != inset) {
859 bool found = the_locking_inset->updateInsetInInset(bv, inset);
863 setUpdateStatus(bv, CURSOR_PAR);
866 bool found = lt->updateInset(bv, inset);
870 setUpdateStatus(bv, CURSOR_PAR);
871 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
872 inset_x = cx(bv) - top_x + drawTextXOffset;
873 inset_y = cy(bv) + drawTextYOffset;
880 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
884 // use this to check mouse motion for selection!
888 int tmp_x = x - drawTextXOffset;
889 int tmp_y = y + insetAscent - getLyXText(bv)->first;
890 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
893 if (the_locking_inset) {
894 if (the_locking_inset == inset) {
895 the_locking_inset->insetButtonPress(bv,
899 no_selection = false;
902 // otherwise unlock the_locking_inset and lock the new inset
903 the_locking_inset->insetUnlock(bv);
904 inset_x = cx(bv) - top_x + drawTextXOffset;
905 inset_y = cy(bv) + drawTextYOffset;
906 the_locking_inset = 0;
907 inset->insetButtonPress(bv, x - inset_x,
908 y - inset_y, button);
909 inset->edit(bv, x - inset_x, y - inset_y, button);
910 if (the_locking_inset)
911 updateLocal(bv, CURSOR, false);
912 no_selection = false;
915 // otherwise only unlock the_locking_inset
916 the_locking_inset->insetUnlock(bv);
917 the_locking_inset = 0;
919 if (bv->theLockingInset()) {
920 if (isHighlyEditableInset(inset)) {
921 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
922 inset_x = cx(bv) - top_x + drawTextXOffset;
923 inset_y = cy(bv) + drawTextYOffset;
924 inset_pos = cpos(bv);
925 inset_par = cpar(bv);
926 inset_boundary = cboundary(bv);
927 the_locking_inset = uinset;
928 uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
930 uinset->edit(bv, x - inset_x, y - inset_y, 0);
931 if (the_locking_inset)
932 updateLocal(bv, CURSOR, false);
933 no_selection = false;
937 if (!inset) { // && (button == 2)) {
938 bool paste_internally = false;
939 if ((button == 2) && getLyXText(bv)->selection.set()) {
940 localDispatch(bv, LFUN_COPY, "");
941 paste_internally = true;
949 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
951 // set the selection cursor!
952 lt->selection.cursor = lt->cursor;
953 lt->cursor.x_fix(lt->cursor.x());
955 if (lt->selection.set()) {
956 lt->clearSelection();
959 updateLocal(bv, FULL, false);
961 lt->clearSelection();
964 updateLocal(bv, CURSOR, false);
966 bv->owner()->setLayout(cpar(bv)->getLayout());
968 // Insert primary selection with middle mouse
969 // if there is a local selection in the current buffer,
972 if (paste_internally)
973 localDispatch(bv, LFUN_PASTE, "");
975 localDispatch(bv, LFUN_PASTESELECTION,
979 getLyXText(bv)->clearSelection();
982 no_selection = false;
986 bool InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
988 if (the_locking_inset) {
989 return the_locking_inset->insetButtonRelease(bv,
990 x - inset_x, y - inset_y,
993 int tmp_x = x - drawTextXOffset;
994 int tmp_y = y + insetAscent - getLyXText(bv)->first;
995 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
998 if (isHighlyEditableInset(inset)) {
999 ret = inset->insetButtonRelease(bv, x - inset_x,
1000 y - inset_y, button);
1002 inset_x = cx(bv) - top_x + drawTextXOffset;
1003 inset_y = cy(bv) + drawTextYOffset;
1004 ret = inset->insetButtonRelease(bv, x - inset_x,
1005 y - inset_y, button);
1006 inset->edit(bv, x - inset_x,
1007 y - inset_y, button);
1009 updateLocal(bv, CURSOR_PAR, false);
1015 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
1017 if (no_selection || ((mouse_x == x) && (mouse_y == y)))
1019 if (the_locking_inset) {
1020 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1026 lt = getLyXText(bv);
1029 hideInsetCursor(bv);
1030 LyXCursor cur = lt->cursor;
1031 lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1032 if (cur == lt->cursor) {
1037 lt->setSelection(bv);
1038 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1039 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1043 updateLocal(bv, SELECTION, false);
1045 showInsetCursor(bv);
1049 void InsetText::insetKeyPress(XKeyEvent * xke)
1051 if (the_locking_inset) {
1052 the_locking_inset->insetKeyPress(xke);
1058 UpdatableInset::RESULT
1059 InsetText::localDispatch(BufferView * bv,
1060 kb_action action, string const & arg)
1062 bool was_empty = par->size() == 0 && !par->next();
1063 no_selection = false;
1064 UpdatableInset::RESULT
1065 result= UpdatableInset::localDispatch(bv, action, arg);
1066 if (result != UNDISPATCHED) {
1070 result = DISPATCHED;
1071 if ((action < 0) && arg.empty())
1074 if (the_locking_inset) {
1075 result = the_locking_inset->localDispatch(bv, action, arg);
1076 if (result == DISPATCHED_NOUPDATE)
1078 else if (result == DISPATCHED) {
1079 updateLocal(bv, CURSOR_PAR, false);
1081 } else if (result >= FINISHED) {
1083 case FINISHED_RIGHT:
1084 moveRightIntern(bv, false, false);
1085 result = DISPATCHED;
1088 if ((result = moveUp(bv)) >= FINISHED) {
1089 updateLocal(bv, CURSOR, false);
1090 bv->unlockInset(this);
1094 if ((result = moveDown(bv)) >= FINISHED) {
1095 updateLocal(bv, CURSOR, false);
1096 bv->unlockInset(this);
1100 result = DISPATCHED;
1103 the_locking_inset = 0;
1104 #ifdef WITH_WARNINGS
1105 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
1110 hideInsetCursor(bv);
1113 lt = getLyXText(bv);
1117 int updflag = false;
1120 case LFUN_SELFINSERT:
1121 if (bv->buffer()->isReadonly()) {
1122 // setErrorMessage(N_("Document is read only"));
1126 /* Automatically delete the currently selected
1127 * text and replace it with what is being
1128 * typed in now. Depends on lyxrc settings
1129 * "auto_region_delete", which defaults to
1132 setUndo(bv, Undo::INSERT,
1133 lt->cursor.par(), lt->cursor.par()->next());
1135 if (lyxrc.auto_region_delete) {
1136 if (lt->selection.set()) {
1137 lt->cutSelection(bv, false);
1140 lt->clearSelection();
1141 for (string::size_type i = 0; i < arg.length(); ++i) {
1142 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1145 lt->selection.cursor = lt->cursor;
1146 updwhat = CURSOR_PAR;
1148 result = DISPATCHED_NOUPDATE;
1150 // --- Cursor Movements -----------------------------------
1153 moveRight(bv, false, true);
1154 lt->setSelection(bv);
1155 updwhat = SELECTION;
1158 result = moveRight(bv);
1164 moveLeft(bv, false, true);
1165 lt->setSelection(bv);
1166 updwhat = SELECTION;
1170 result = moveLeft(bv);
1176 lt->setSelection(bv);
1177 updwhat = SELECTION;
1181 result = moveDown(bv);
1187 lt->setSelection(bv);
1188 updwhat = SELECTION;
1192 result = moveUp(bv);
1204 case LFUN_BACKSPACE: {
1205 setUndo(bv, Undo::DELETE,
1206 lt->cursor.par(), lt->cursor.par()->next());
1207 if (lt->selection.set())
1208 lt->cutSelection(bv);
1211 updwhat = CURSOR_PAR;
1217 setUndo(bv, Undo::DELETE,
1218 lt->cursor.par(), lt->cursor.par()->next());
1219 if (lt->selection.set()) {
1220 lt->cutSelection(bv);
1224 updwhat = CURSOR_PAR;
1230 setUndo(bv, Undo::DELETE,
1231 lt->cursor.par(), lt->cursor.par()->next());
1232 lt->cutSelection(bv);
1233 updwhat = CURSOR_PAR;
1240 lt->copySelection(bv);
1241 updwhat = CURSOR_PAR;
1243 case LFUN_PASTESELECTION:
1245 string const clip(bv->getClipboard());
1249 if (arg == "paragraph") {
1250 lt->insertStringAsParagraphs(bv, clip);
1252 lt->insertStringAsLines(bv, clip);
1254 updwhat = CURSOR_PAR;
1259 if (!autoBreakRows) {
1261 if (CutAndPaste::nrOfParagraphs() > 1) {
1262 Alert::alert(_("Impossible operation"),
1263 _("Cannot include more than one paragraph!"),
1268 setUndo(bv, Undo::INSERT,
1269 lt->cursor.par(), lt->cursor.par()->next());
1270 lt->pasteSelection(bv);
1271 updwhat = CURSOR_PAR;
1276 case LFUN_BREAKPARAGRAPH:
1277 if (!autoBreakRows) {
1278 result = DISPATCHED;
1281 lt->breakParagraph(bv, 0);
1285 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1286 if (!autoBreakRows) {
1287 result = DISPATCHED;
1290 lt->breakParagraph(bv, 1);
1295 case LFUN_BREAKLINE: {
1296 if (!autoBreakRows) {
1297 result = DISPATCHED;
1300 setUndo(bv, Undo::INSERT,
1301 lt->cursor.par(), lt->cursor.par()->next());
1302 lt->insertChar(bv, Paragraph::META_NEWLINE);
1303 updwhat = CURSOR_PAR;
1309 // do not set layouts on non breakable textinsets
1310 if (autoBreakRows) {
1311 layout_type cur_layout = cpar(bv)->layout;
1313 // Derive layout number from given argument (string)
1314 // and current buffer's textclass (number). */
1315 textclass_type tclass = bv->buffer()->params.textclass;
1316 std::pair <bool, layout_type> layout =
1317 textclasslist.NumberOfLayout(tclass, arg);
1319 // If the entry is obsolete, use the new one instead.
1321 string obs = textclasslist.Style(tclass,layout.second).
1324 layout = textclasslist.NumberOfLayout(tclass, obs);
1327 // see if we found the layout number:
1328 if (!layout.first) {
1329 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1330 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1334 if (cur_layout != layout.second) {
1335 cur_layout = layout.second;
1336 lt->setLayout(bv, layout.second);
1337 bv->owner()->setLayout(cpar(bv)->getLayout());
1338 updwhat = CURSOR_PAR;
1342 // reset the layout box
1343 bv->owner()->setLayout(cpar(bv)->getLayout());
1346 case LFUN_PARAGRAPH_SPACING:
1347 // This one is absolutely not working. When fiddling with this
1348 // it also seems to me that the paragraphs inside the insettext
1349 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1351 Paragraph * par = lt->cursor.par();
1352 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1353 float cur_value = 1.0;
1354 if (cur_spacing == Spacing::Other) {
1355 cur_value = par->params().spacing().getValue();
1358 istringstream istr(arg.c_str());
1361 Spacing::Space new_spacing = cur_spacing;
1362 float new_value = cur_value;
1364 lyxerr << "Missing argument to `paragraph-spacing'"
1366 } else if (tmp == "single") {
1367 new_spacing = Spacing::Single;
1368 } else if (tmp == "onehalf") {
1369 new_spacing = Spacing::Onehalf;
1370 } else if (tmp == "double") {
1371 new_spacing = Spacing::Double;
1372 } else if (tmp == "other") {
1373 new_spacing = Spacing::Other;
1376 lyxerr << "new_value = " << tmpval << endl;
1379 } else if (tmp == "default") {
1380 new_spacing = Spacing::Default;
1382 lyxerr << _("Unknown spacing argument: ")
1385 if (cur_spacing != new_spacing || cur_value != new_value) {
1386 par->params().spacing(Spacing(new_spacing, new_value));
1387 updwhat = CURSOR_PAR;
1394 if (!bv->Dispatch(action, arg))
1395 result = UNDISPATCHED;
1402 updateLocal(bv, updwhat, updflag);
1403 /// If the action has deleted all text in the inset, we need to change the
1404 // language to the language of the surronding text.
1405 if (!was_empty && par->size() == 0 && !par->next()) {
1406 LyXFont font(LyXFont::ALL_IGNORE);
1407 font.setLanguage(bv->getParentLanguage(this));
1408 setFont(bv, font, false);
1411 if (result < FINISHED) {
1412 showInsetCursor(bv);
1414 bv->unlockInset(this);
1419 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1422 buf->latexParagraphs(os, par, 0, texrow);
1423 return texrow.rows();
1427 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1429 Paragraph * p = par;
1430 unsigned int lines = 0;
1433 string const tmp = buf->asciiParagraph(p, linelen, p->previous()==0);
1434 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1442 int InsetText::docbook(Buffer const * buf, ostream & os) const
1444 Paragraph * p = par;
1445 unsigned int lines = 0;
1447 vector<string> environment_stack(10);
1448 vector<string> environment_inner(10);
1450 int const command_depth = 0;
1453 Paragraph::depth_type depth = 0; // paragraph depth
1457 int desc_on = 0; // description mode
1459 LyXLayout const & style =
1460 textclasslist.Style(buf->params.textclass,
1463 // environment tag closing
1464 for (; depth > p->params().depth(); --depth) {
1465 if (environment_inner[depth] != "!-- --") {
1466 item_name = "listitem";
1467 buf->sgmlCloseTag(os, command_depth + depth,
1469 if (environment_inner[depth] == "varlistentry")
1470 buf->sgmlCloseTag(os, depth+command_depth,
1471 environment_inner[depth]);
1473 buf->sgmlCloseTag(os, depth + command_depth,
1474 environment_stack[depth]);
1475 environment_stack[depth].erase();
1476 environment_inner[depth].erase();
1479 if (depth == p->params().depth()
1480 && environment_stack[depth] != style.latexname()
1481 && !environment_stack[depth].empty()) {
1482 if (environment_inner[depth] != "!-- --") {
1483 item_name= "listitem";
1484 buf->sgmlCloseTag(os, command_depth+depth,
1486 if (environment_inner[depth] == "varlistentry")
1487 buf->sgmlCloseTag(os,
1488 depth + command_depth,
1489 environment_inner[depth]);
1492 buf->sgmlCloseTag(os, depth + command_depth,
1493 environment_stack[depth]);
1495 environment_stack[depth].erase();
1496 environment_inner[depth].erase();
1499 // Write opening SGML tags.
1500 switch (style.latextype) {
1501 case LATEX_PARAGRAPH:
1502 buf->sgmlOpenTag(os, depth + command_depth,
1507 buf->sgmlError(p, 0,
1508 _("Error : LatexType Command not allowed here.\n"));
1512 case LATEX_ENVIRONMENT:
1513 case LATEX_ITEM_ENVIRONMENT:
1514 if (depth < p->params().depth()) {
1515 depth = p->params().depth();
1516 environment_stack[depth].erase();
1519 if (environment_stack[depth] != style.latexname()) {
1520 if(environment_stack.size() == depth + 1) {
1521 environment_stack.push_back("!-- --");
1522 environment_inner.push_back("!-- --");
1524 environment_stack[depth] = style.latexname();
1525 environment_inner[depth] = "!-- --";
1526 buf->sgmlOpenTag(os, depth + command_depth,
1527 environment_stack[depth]);
1529 if (environment_inner[depth] != "!-- --") {
1530 item_name= "listitem";
1531 buf->sgmlCloseTag(os,
1532 command_depth + depth,
1534 if (environment_inner[depth] == "varlistentry")
1535 buf->sgmlCloseTag(os,
1536 depth + command_depth,
1537 environment_inner[depth]);
1541 if (style.latextype == LATEX_ENVIRONMENT) {
1542 if (!style.latexparam().empty()) {
1543 if(style.latexparam() == "CDATA")
1546 buf->sgmlOpenTag(os, depth + command_depth,
1547 style.latexparam());
1552 desc_on = (style.labeltype == LABEL_MANUAL);
1555 environment_inner[depth]= "varlistentry";
1557 environment_inner[depth]= "listitem";
1559 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1560 environment_inner[depth]);
1564 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1568 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1573 buf->sgmlOpenTag(os, depth + command_depth,
1578 buf->simpleDocBookOnePar(os, p, desc_on,
1579 depth + 1 + command_depth);
1583 // write closing SGML tags
1584 switch (style.latextype) {
1585 case LATEX_ENVIRONMENT:
1586 if (!style.latexparam().empty()) {
1587 if(style.latexparam() == "CDATA")
1590 buf->sgmlCloseTag(os, depth + command_depth,
1591 style.latexparam());
1594 case LATEX_ITEM_ENVIRONMENT:
1595 if (desc_on == 1) break;
1597 buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1599 case LATEX_PARAGRAPH:
1600 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1603 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1609 for (int d = depth; d >= 0; --d) {
1610 if (!environment_stack[depth].empty()) {
1611 if (environment_inner[depth] != "!-- --") {
1612 item_name = "listitem";
1613 buf->sgmlCloseTag(os, command_depth + depth,
1615 if (environment_inner[depth] == "varlistentry")
1616 buf->sgmlCloseTag(os, depth + command_depth,
1617 environment_inner[depth]);
1620 buf->sgmlCloseTag(os, depth + command_depth,
1621 environment_stack[depth]);
1629 void InsetText::validate(LaTeXFeatures & features) const
1631 Paragraph * p = par;
1633 p->validate(features);
1639 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1641 if (textclasslist.Style(buf->params.textclass,
1642 p->getLayout()).labeltype != LABEL_MANUAL)
1645 return p->beginningOfMainBody();
1649 void InsetText::getCursorPos(BufferView * bv,
1650 int & x, int & y) const
1652 if (the_locking_inset) {
1653 the_locking_inset->getCursorPos(bv, x, y);
1661 unsigned int InsetText::insetInInsetY()
1663 if (!the_locking_inset)
1666 return (inset_y + the_locking_inset->insetInInsetY());
1670 void InsetText::toggleInsetCursor(BufferView * bv)
1672 if (the_locking_inset) {
1673 the_locking_inset->toggleInsetCursor(bv);
1677 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1679 int const asc = lyxfont::maxAscent(font);
1680 int const desc = lyxfont::maxDescent(font);
1682 if (isCursorVisible())
1683 bv->hideLockedInsetCursor();
1685 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1686 toggleCursorVisible();
1690 void InsetText::showInsetCursor(BufferView * bv, bool show)
1692 if (the_locking_inset) {
1693 the_locking_inset->showInsetCursor(bv, show);
1696 if (!isCursorVisible()) {
1697 LyXFont const font =
1698 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1700 int const asc = lyxfont::maxAscent(font);
1701 int const desc = lyxfont::maxDescent(font);
1703 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1705 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1706 setCursorVisible(true);
1711 void InsetText::hideInsetCursor(BufferView * bv)
1713 if (isCursorVisible()) {
1714 bv->hideLockedInsetCursor();
1715 setCursorVisible(false);
1717 if (the_locking_inset)
1718 the_locking_inset->hideInsetCursor(bv);
1722 void InsetText::fitInsetCursor(BufferView * bv) const
1724 if (the_locking_inset) {
1725 the_locking_inset->fitInsetCursor(bv);
1728 LyXFont const font =
1729 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1731 int const asc = lyxfont::maxAscent(font);
1732 int const desc = lyxfont::maxDescent(font);
1734 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1738 UpdatableInset::RESULT
1739 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1741 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1742 return moveLeftIntern(bv, false, activate_inset, selecting);
1744 return moveRightIntern(bv, false, activate_inset, selecting);
1748 UpdatableInset::RESULT
1749 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1751 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1752 return moveRightIntern(bv, true, activate_inset, selecting);
1754 return moveLeftIntern(bv, true, activate_inset, selecting);
1758 UpdatableInset::RESULT
1759 InsetText::moveRightIntern(BufferView * bv, bool behind,
1760 bool activate_inset, bool selecting)
1762 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1763 return FINISHED_RIGHT;
1764 if (activate_inset && checkAndActivateInset(bv, behind))
1766 getLyXText(bv)->cursorRight(bv);
1768 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1769 return DISPATCHED_NOUPDATE;
1773 UpdatableInset::RESULT
1774 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1775 bool activate_inset, bool selecting)
1777 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1779 getLyXText(bv)->cursorLeft(bv);
1781 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1782 if (activate_inset && checkAndActivateInset(bv, behind))
1784 return DISPATCHED_NOUPDATE;
1788 UpdatableInset::RESULT
1789 InsetText::moveUp(BufferView * bv)
1791 if (!crow(bv)->previous())
1793 getLyXText(bv)->cursorUp(bv);
1794 return DISPATCHED_NOUPDATE;
1798 UpdatableInset::RESULT
1799 InsetText::moveDown(BufferView * bv)
1801 if (!crow(bv)->next())
1802 return FINISHED_DOWN;
1803 getLyXText(bv)->cursorDown(bv);
1804 return DISPATCHED_NOUPDATE;
1808 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1810 if (the_locking_inset) {
1811 if (the_locking_inset->insetAllowed(inset))
1812 return the_locking_inset->insertInset(bv, inset);
1817 lt = getLyXText(bv);
1820 setUndo(bv, Undo::FINISH, lt->cursor.par(), lt->cursor.par()->next());
1822 inset->setOwner(this);
1823 hideInsetCursor(bv);
1824 lt->insertInset(bv, inset);
1828 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1834 bool InsetText::insetAllowed(Inset::Code code) const
1836 // in_insetAllowed is a really gross hack,
1837 // to allow us to call the owner's insetAllowed
1838 // without stack overflow, which can happen
1839 // when the owner uses InsetCollapsable::insetAllowed()
1841 if (in_insetAllowed)
1843 in_insetAllowed = true;
1844 if (the_locking_inset)
1845 ret = the_locking_inset->insetAllowed(code);
1847 ret = owner()->insetAllowed(code);
1848 in_insetAllowed = false;
1853 UpdatableInset * InsetText::getLockingInset() const
1855 return the_locking_inset ? the_locking_inset->getLockingInset() :
1856 const_cast<InsetText *>(this);
1860 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1864 if (the_locking_inset)
1865 return the_locking_inset->getFirstLockingInsetOfType(c);
1870 bool InsetText::showInsetDialog(BufferView * bv) const
1872 if (the_locking_inset)
1873 return the_locking_inset->showInsetDialog(bv);
1878 std::vector<string> const InsetText::getLabelList() const
1880 std::vector<string> label_list;
1882 Paragraph * tpar = par;
1884 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1885 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1886 for (; beg != end; ++beg) {
1887 std::vector<string> const l = (*beg)->getLabelList();
1888 label_list.insert(label_list.end(), l.begin(), l.end());
1890 tpar = tpar->next();
1896 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1899 if (the_locking_inset) {
1900 the_locking_inset->setFont(bv, font, toggleall, selectall);
1903 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
1904 getLyXText(bv)->setFont(bv, font, toggleall);
1909 lt = getLyXText(bv);
1912 if (lt->selection.set()) {
1913 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1917 lt->toggleFree(bv, font, toggleall);
1919 lt->clearSelection();
1921 bool flag = (selectall || lt->selection.set());
1925 updateLocal(bv, FULL, true);
1927 updateLocal(bv, CURSOR_PAR, true);
1931 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1933 if (cpar(bv)->isInset(cpos(bv))) {
1937 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1938 if (!isHighlyEditableInset(inset))
1940 LyXFont const font =
1941 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1943 x = inset->width(bv, font);
1944 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1947 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1949 //inset_x = cx(bv) - top_x + drawTextXOffset;
1950 //inset_y = cy(bv) + drawTextYOffset;
1951 inset->edit(bv, x, y, 0);
1952 if (!the_locking_inset)
1954 updateLocal(bv, CURSOR, false);
1961 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1964 x -= drawTextXOffset;
1966 int dummyy = y + insetAscent;
1967 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
1974 inset_x = cx(bv) - top_x + drawTextXOffset;
1975 inset_y = cy(bv) + drawTextYOffset;
1976 inset->edit(bv, x - inset_x, y - inset_y, button);
1977 if (!the_locking_inset)
1979 updateLocal(bv, CURSOR, false);
1986 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1989 int w = UpdatableInset::getMaxWidth(bv, inset);
1994 w = w - top_x + owner()->x();
1997 w -= (2 * TEXT_TO_INSET_OFFSET);
2000 return UpdatableInset::getMaxWidth(bv, inset);
2005 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2007 // we have to unlock any locked inset otherwise we're in troubles
2008 the_locking_inset = 0;
2010 Paragraph * tmp = par->next();
2015 par = new Paragraph(*p, same_id);
2016 par->setInsetOwner(this);
2017 Paragraph * np = par;
2020 np->next(new Paragraph(*p, same_id));
2021 np->next()->previous(np);
2023 np->setInsetOwner(this);
2030 void InsetText::setText(string const & data)
2033 LyXFont font(LyXFont::ALL_SANE);
2034 for (unsigned int i=0; i < data.length(); ++i)
2035 par->insertChar(i, data[i], font);
2039 void InsetText::setAutoBreakRows(bool flag)
2041 if (flag != autoBreakRows) {
2042 autoBreakRows = flag;
2050 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2052 if (how != drawFrame_) {
2055 updateLocal(bv, DRAW_FRAME, false);
2060 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2062 if (frame_color != col) {
2065 updateLocal(bv, DRAW_FRAME, false);
2070 int InsetText::cx(BufferView * bv) const
2072 // we do nothing dangerous so we use a local cache
2073 LyXText * llt = getLyXText(bv);
2074 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2075 if (the_locking_inset) {
2076 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2078 if (font.isVisibleRightToLeft())
2079 x -= the_locking_inset->width(bv, font);
2085 int InsetText::cy(BufferView * bv) const
2088 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2092 pos_type InsetText::cpos(BufferView * bv) const
2094 return getLyXText(bv)->cursor.pos();
2098 Paragraph * InsetText::cpar(BufferView * bv) const
2100 return getLyXText(bv)->cursor.par();
2104 bool InsetText::cboundary(BufferView * bv) const
2106 return getLyXText(bv)->cursor.boundary();
2110 Row * InsetText::crow(BufferView * bv) const
2112 return getLyXText(bv)->cursor.row();
2116 LyXText * InsetText::getLyXText(BufferView const * lbv,
2117 bool const recursive) const
2119 if (!recursive && (cached_bview == lbv)) {
2120 LyXText * lt = cached_text.get();
2121 lyx::Assert(lt && lt->firstRow()->par() == par);
2125 // Super UGLY! (Lgb)
2126 BufferView * bv = const_cast<BufferView *>(lbv);
2129 Cache::iterator it = cache.find(bv);
2131 if (it != cache.end()) {
2134 } else if (do_resize) {
2135 resizeLyXText(do_resize);
2137 if (lt || !it->second.remove) {
2138 lyx::Assert(it->second.text.get());
2139 cached_text = it->second.text;
2140 if (recursive && the_locking_inset) {
2141 return the_locking_inset->getLyXText(bv, true);
2143 return cached_text.get();
2144 } else if (it->second.remove) {
2146 saveLyXTextState(it->second.text.get());
2152 // when we have to reinit the existing LyXText!
2154 it->second.text->init(bv);
2155 restoreLyXTextState(bv, it->second.text.get());
2156 it->second.remove = false;
2158 cached_text = it->second.text;
2159 if (the_locking_inset && recursive) {
2160 return the_locking_inset->getLyXText(bv);
2162 return cached_text.get();
2165 // we are here only if we don't have a BufferView * in the cache!!!
2167 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2168 cached_text->init(bv);
2169 restoreLyXTextState(bv, cached_text.get());
2171 cache.insert(make_pair(bv, cached_text));
2173 if (the_locking_inset && recursive) {
2174 return the_locking_inset->getLyXText(bv);
2176 return cached_text.get();
2180 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2184 Cache::iterator it = cache.find(bv);
2186 if (it == cache.end()) {
2190 lyx::Assert(it->second.text.get());
2192 it->second.remove = true;
2194 /// then remove all LyXText in text-insets
2195 Paragraph * p = par;
2196 for (; p; p = p->next()) {
2197 p->deleteInsetsLyXText(bv);
2203 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2206 // we cannot resize this because we are in use!
2207 // so do this on the next possible getLyXText()
2212 // lyxerr << "InsetText::resizeLyXText\n";
2213 if (!par->next() && !par->size()) { // no data, resize not neccessary!
2214 // we have to do this as a fixed width may have changed!
2215 LyXText * t = getLyXText(bv);
2216 saveLyXTextState(t);
2218 restoreLyXTextState(bv, t);
2221 // one endless line, resize normally not necessary
2222 if (!force && getMaxWidth(bv, this) < 0)
2225 Cache::iterator it = cache.find(bv);
2226 if (it == cache.end()) {
2229 lyx::Assert(it->second.text.get());
2231 LyXText * t = it->second.text.get();
2232 saveLyXTextState(t);
2233 for (Paragraph * p = par; p; p = p->next()) {
2234 p->resizeInsetsLyXText(bv);
2237 restoreLyXTextState(bv, t);
2238 if (the_locking_inset) {
2239 inset_x = cx(bv) - top_x + drawTextXOffset;
2240 inset_y = cy(bv) + drawTextYOffset;
2244 t->first = bv->screen()->topCursorVisible(t);
2247 updateLocal(bv, FULL, false);
2248 // this will scroll the screen such that the cursor becomes visible
2249 bv->updateScrollbar();
2251 need_update |= FULL;
2256 void InsetText::reinitLyXText() const
2259 // we cannot resize this because we are in use!
2260 // so do this on the next possible getLyXText()
2266 // lyxerr << "InsetText::reinitLyXText\n";
2267 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2268 lyx::Assert(it->second.text.get());
2270 LyXText * t = it->second.text.get();
2271 BufferView * bv = it->first;
2273 saveLyXTextState(t);
2274 for (Paragraph * p = par; p; p = p->next()) {
2275 p->resizeInsetsLyXText(bv);
2278 restoreLyXTextState(bv, t);
2279 if (the_locking_inset) {
2280 inset_x = cx(bv) - top_x + drawTextXOffset;
2281 inset_y = cy(bv) + drawTextYOffset;
2284 t->first = bv->screen()->topCursorVisible(t);
2287 updateLocal(bv, FULL, false);
2288 // this will scroll the screen such that the cursor becomes visible
2289 bv->updateScrollbar();
2297 void InsetText::removeNewlines()
2299 bool changed = false;
2301 for (Paragraph * p = par; p; p = p->next()) {
2302 for (int i = 0; i < p->size(); ++i) {
2303 if (p->getChar(i) == Paragraph::META_NEWLINE) {
2314 bool InsetText::nodraw() const
2316 if (the_locking_inset)
2317 return the_locking_inset->nodraw();
2318 return UpdatableInset::nodraw();
2322 int InsetText::scroll(bool recursive) const
2324 int sx = UpdatableInset::scroll(false);
2326 if (recursive && the_locking_inset)
2327 sx += the_locking_inset->scroll(recursive);
2333 bool InsetText::doClearArea() const
2335 return !locked || (need_update & (FULL|INIT));
2339 void InsetText::selectAll(BufferView * bv)
2341 getLyXText(bv)->cursorTop(bv);
2342 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2343 getLyXText(bv)->cursorBottom(bv);
2344 getLyXText(bv)->setSelection(bv);
2348 void InsetText::clearSelection(BufferView * bv)
2350 getLyXText(bv)->clearSelection();
2354 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2356 Painter & pain = bv->painter();
2358 int h = insetAscent + insetDescent;
2359 int ty = baseline - insetAscent;
2365 if ((ty + h) > pain.paperHeight())
2366 h = pain.paperHeight();
2367 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2368 w = pain.paperWidth();
2369 // w -= TEXT_TO_INSET_OFFSET;
2370 pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2373 frame_is_visible = false;
2377 Paragraph * InsetText::getParFromID(int id) const
2380 Paragraph * result = par;
2381 Paragraph * ires = 0;
2382 while (result && result->id() != id) {
2383 if ((ires = result->getParFromID(id)))
2385 result = result->next();
2389 Paragraph * tmp = par;
2391 if (tmp->id() == id) {
2394 Paragraph * tmp2 = tmp->getParFromID(id);
2405 Paragraph * InsetText::firstParagraph() const
2408 if (the_locking_inset)
2409 if ((result = the_locking_inset->firstParagraph()))
2415 Paragraph * InsetText::getFirstParagraph(int i) const
2417 return (i == 0) ? par : 0;
2421 LyXCursor const & InsetText::cursor(BufferView * bv) const
2423 if (the_locking_inset)
2424 return the_locking_inset->cursor(bv);
2425 return getLyXText(bv)->cursor;
2429 Paragraph * InsetText::paragraph() const
2435 void InsetText::paragraph(Paragraph * p)
2437 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2438 // caller of this function has to take care of it. This IS important
2439 // as we could have to insert a paragraph before this one and just
2440 // link the actual to a new ones next and set it with this function
2443 // set ourself as owner for all the paragraphs inserted!
2444 Paragraph * np = par;
2446 np->setInsetOwner(this);
2450 // redraw myself when asked for
2455 Inset * InsetText::getInsetFromID(int id_arg) const
2458 return const_cast<InsetText *>(this);
2460 Paragraph * lp = par;
2463 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2464 en = lp->inset_iterator_end();
2467 if ((*it)->id() == id_arg)
2469 Inset * in = (*it)->getInsetFromID(id_arg);
2479 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2485 lt = getLyXText(bv);
2488 if (the_locking_inset) {
2489 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2496 // we have to go on checking so move cusor to the next char
2497 lt->cursor.pos(lt->cursor.pos() + 1);
2499 str = lt->selectNextWordToSpellcheck(bv, value);
2501 bv->unlockInset(const_cast<InsetText *>(this));
2510 void InsetText::selectSelectedWord(BufferView * bv)
2512 if (the_locking_inset) {
2513 the_locking_inset->selectSelectedWord(bv);
2516 getLyXText(bv)->selectSelectedWord(bv);
2517 updateLocal(bv, SELECTION, false);
2521 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2523 if (the_locking_inset) {
2524 the_locking_inset->toggleSelection(bv, kill_selection);
2528 lt = getLyXText(bv);
2532 int x = top_x + TEXT_TO_INSET_OFFSET;
2534 Row * row = lt->firstRow();
2535 int y_offset = top_baseline - row->ascent_of_text();
2537 while ((row != 0) && ((y+row->height()) <= 0)) {
2544 if (need_update & SELECTION)
2546 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2552 bool InsetText::searchForward(BufferView * bv, string const & str,
2555 if (the_locking_inset) {
2556 if (the_locking_inset->searchForward(bv, str, cs, mw))
2560 lt = getLyXText(bv);
2563 Paragraph * lpar = lt->cursor.par();
2564 pos_type pos = lt->cursor.pos();
2565 if (pos < lpar->size() - 1)
2569 lpar = lpar->next();
2574 // we have to unlock ourself in this function by default!
2575 bv->unlockInset(const_cast<InsetText *>(this));
2578 lt->setCursor(bv, lpar, pos);
2582 if (LyXFind(bv, str, true, true, cs , mw)) {
2585 // we have to unlock ourself in this function by default!
2586 bv->unlockInset(const_cast<InsetText *>(this));
2590 bool InsetText::searchBackward(BufferView * bv, string const & str,
2593 if (the_locking_inset)
2594 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2596 if (LyXFind(bv, str, false, true, cs, mw)) {
2599 // we have to unlock ourself in this function by default!
2600 bv->unlockInset(const_cast<InsetText *>(this));
2605 bool InsetText::checkInsertChar(LyXFont & font)
2608 return owner()->checkInsertChar(font);
2613 void InsetText::collapseParagraphs(BufferParams const & bparams) const
2615 while(par->next()) {
2616 if (!par->isSeparator(par->size()-1))
2617 par->insertChar(par->size()-1, ' ');
2618 par->pasteParagraph(bparams);