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"
66 using lyx::layout_type;
67 using lyx::textclass_type;
69 extern unsigned char getCurrentTextClass(Buffer *);
70 extern bool math_insert_greek(BufferView *, char);
71 extern int greek_kb_flag;
74 // These functions should probably go into bufferview_funcs somehow (Jug)
76 void InsetText::saveLyXTextState(LyXText * t) const
78 // check if my paragraphs are still valid
81 if (p == t->cursor.par())
86 if (p && t->cursor.pos() <= p->size()) {
87 sstate.lpar = t->cursor.par();
88 sstate.pos = t->cursor.pos();
89 sstate.boundary = t->cursor.boundary();
90 sstate.selstartpar = t->selection.start.par();
91 sstate.selstartpos = t->selection.start.pos();
92 sstate.selstartboundary = t->selection.start.boundary();
93 sstate.selendpar = t->selection.end.par();
94 sstate.selendpos = t->selection.end.pos();
95 sstate.selendboundary = t->selection.end.boundary();
96 sstate.selection = t->selection.set();
97 sstate.mark_set = t->selection.mark();
98 sstate.refresh = t->refresh_row != 0;
104 void InsetText::restoreLyXTextState(BufferView * bv, LyXText * t) const
107 t->selection.set(true);
108 /* at this point just to avoid the Delete-Empty-Paragraph
109 * Mechanism when setting the cursor */
110 t->selection.mark(sstate.mark_set);
111 if (sstate.selection) {
112 t->setCursor(bv, sstate.selstartpar, sstate.selstartpos,
113 true, sstate.selstartboundary);
114 t->selection.cursor = t->cursor;
115 t->setCursor(bv, sstate.selendpar, sstate.selendpos,
116 true, sstate.selendboundary);
118 t->setCursor(bv, sstate.lpar, sstate.pos);
120 t->setCursor(bv, sstate.lpar, sstate.pos, true, sstate.boundary);
121 t->selection.cursor = t->cursor;
122 t->selection.set(false);
124 if (sstate.refresh) {
130 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
137 InsetText::InsetText()
138 : UpdatableInset(), lt(0), in_update(false), do_resize(0),
146 InsetText::InsetText(InsetText const & in, bool same_id)
147 : UpdatableInset(in, same_id), lt(0), in_update(false), do_resize(0),
155 InsetText & InsetText::operator=(InsetText const & it)
162 void InsetText::init(InsetText const * ins, bool same_id)
165 setParagraphData(ins->par, same_id);
166 autoBreakRows = ins->autoBreakRows;
167 drawFrame_ = ins->drawFrame_;
168 frame_color = ins->frame_color;
174 p->setInsetOwner(this);
177 the_locking_inset = 0;
179 frame_color = LColor::insetframe;
180 autoBreakRows = false;
187 no_selection = false;
194 last_drawn_width = -1;
195 frame_is_visible = false;
198 in_insetAllowed = false;
202 InsetText::~InsetText()
209 Paragraph * tmp = par->next();
216 void InsetText::clear()
219 Paragraph * tmp = par->next();
229 Inset * InsetText::clone(Buffer const &, bool same_id) const
231 return new InsetText(*this, same_id);
235 void InsetText::write(Buffer const * buf, ostream & os) const
238 writeParagraphData(buf, os);
242 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
244 par->writeFile(buf, os, buf->params, 0);
248 void InsetText::read(Buffer const * buf, LyXLex & lex)
252 Paragraph * return_par = 0;
253 Paragraph::depth_type depth = 0;
254 LyXFont font(LyXFont::ALL_INHERIT);
260 token = lex.getString();
263 if (token == "\\end_inset") {
264 #ifndef NO_COMPABILITY
265 const_cast<Buffer*>(buf)->insertErtContents(par, pos, font, false);
270 if (const_cast<Buffer*>(buf)->
271 parseSingleLyXformat2Token(lex, par, return_par,
272 token, pos, depth, font)) {
273 // the_end read this should NEVER happen
274 lex.printError("\\the_end read in inset! Error in document!");
282 return_par->setInsetOwner(this);
283 return_par = return_par->next();
286 if (token != "\\end_inset") {
287 lex.printError("Missing \\end_inset at this point. "
294 int InsetText::ascent(BufferView * bv, LyXFont const &) const
296 insetAscent = getLyXText(bv)->firstRow()->ascent_of_text() +
297 TEXT_TO_INSET_OFFSET;
302 int InsetText::descent(BufferView * bv, LyXFont const &) const
304 LyXText * llt = getLyXText(bv);
305 insetDescent = llt->height - llt->firstRow()->ascent_of_text() +
306 TEXT_TO_INSET_OFFSET;
311 int InsetText::width(BufferView * bv, LyXFont const &) const
313 insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
314 (2 * TEXT_TO_INSET_OFFSET);
315 insetWidth = max(insetWidth, 10);
320 int InsetText::textWidth(BufferView * bv, bool fordraw) const
323 if (!autoBreakRows) {
326 w = getMaxWidth(bv, this);
329 return max(w - (2 * TEXT_TO_INSET_OFFSET),
330 (int)getLyXText(bv)->width);
334 return w - (2 * TEXT_TO_INSET_OFFSET);
338 void InsetText::draw(BufferView * bv, LyXFont const & f,
339 int baseline, float & x, bool cleared) const
344 Painter & pain = bv->painter();
346 // this is the first thing we have to ask because if the x pos
347 // changed we have to do a complete rebreak of the text as we
348 // may have few space to draw in. Well we should check on this too
350 if (top_x != int(x)) {
352 int nw = getMaxWidth(bv, this);
353 if (nw > 0 && old_max_width != nw) {
356 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
364 // repaint the background if needed
365 if (cleared && backgroundColor() != LColor::background) {
367 clearInset(bv, baseline, cleared);
371 // no draw is necessary !!!
372 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
375 top_baseline = baseline;
377 if (need_update & CLEAR_FRAME)
378 clearFrame(pain, cleared);
385 x += static_cast<float>(scroll());
387 // if top_x differs we did it already
388 if (!cleared && (top_x == int(x))
389 && ((need_update&(INIT|FULL)) || (top_baseline != baseline)
390 ||(last_drawn_width != insetWidth))) {
391 clearInset(bv, baseline, cleared);
397 frame_is_visible = false;
399 if (!cleared && (need_update == NONE)) {
401 drawFrame(pain, cleared);
405 // call these methods so that insetWidth, insetAscent and
406 // insetDescent have the right values.
411 top_baseline = baseline;
412 top_y = baseline - insetAscent;
414 if (last_drawn_width != insetWidth) {
416 clearInset(bv, baseline, cleared);
418 last_drawn_width = insetWidth;
421 if (the_locking_inset && (cpar(bv) == inset_par)
422 && (cpos(bv) == inset_pos)) {
423 inset_x = cx(bv) - top_x + drawTextXOffset;
424 inset_y = cy(bv) + drawTextYOffset;
426 if (!cleared && (need_update == CURSOR)
427 && !getLyXText(bv)->selection.set()) {
428 drawFrame(pain, cleared);
438 x += TEXT_TO_INSET_OFFSET;
440 Row * row = lt->firstRow();
441 int y_offset = baseline - row->ascent_of_text();
442 int ph = pain.paperHeight();
445 while ((row != 0) && ((y+row->height()) <= 0)) {
447 first += row->height();
453 if (cleared || (need_update&(INIT|FULL))) {
456 while ((row != 0) && (yf < ph)) {
457 lt->getVisibleRow(bv, y+y_offset, int(x), row,
463 } else if (!locked) {
464 if (need_update & CURSOR) {
465 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
466 lt->clearSelection();
467 lt->selection.cursor = lt->cursor;
469 bv->screen()->update(lt, bv, y_offset, int(x));
472 if (need_update & SELECTION) {
473 bv->screen()->toggleToggle(lt, bv, y_offset, int(x));
474 } else if (need_update & CURSOR) {
475 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
476 lt->clearSelection();
477 lt->selection.cursor = lt->cursor;
479 bv->screen()->update(lt, bv, y_offset, int(x));
484 lt->status(bv, LyXText::UNCHANGED);
485 if ((need_update != CURSOR_PAR) &&
486 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked))) {
487 drawFrame(pain, cleared);
488 } else if (need_update & CLEAR_FRAME) {
489 clearFrame(pain, cleared);
492 x += insetWidth - TEXT_TO_INSET_OFFSET;
494 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
496 } else if (need_update != INIT) {
504 void InsetText::drawFrame(Painter & pain, bool cleared) const
506 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
507 if (!frame_is_visible || cleared) {
508 frame_x = top_x + ttoD2;
509 frame_y = top_baseline - insetAscent + ttoD2;
510 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
511 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
512 pain.rectangle(frame_x, frame_y,
515 frame_is_visible = true;
520 void InsetText::clearFrame(Painter & pain, bool cleared) const
522 if (frame_is_visible) {
524 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
527 frame_is_visible = false;
532 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
535 if (reinit && owner()) {
537 owner()->update(bv, font, true);
542 if (reinit || need_update == INIT) {
544 // we should put this call where we set need_update to INIT!
547 owner()->update(bv, font, true);
551 if (the_locking_inset) {
552 inset_x = cx(bv) - top_x + drawTextXOffset;
553 inset_y = cy(bv) + drawTextYOffset;
554 the_locking_inset->update(bv, font, reinit);
562 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
565 lt->updateInset(bv, the_locking_inset);
567 if (lt->status() == LyXText::NEED_MORE_REFRESH)
575 void InsetText::setUpdateStatus(BufferView * bv, int what) const
577 // this does nothing dangerous so use only a localized buffer
578 LyXText * llt = getLyXText(bv);
581 // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
582 // if we don't break row so that we only have one row to update!
583 if ((llt->status() == LyXText::NEED_MORE_REFRESH) ||
585 (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
588 } else if (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
589 need_update |= CURSOR_PAR;
592 // this to not draw a selection when we redraw all of it!
593 if (need_update & CURSOR && !(need_update & SELECTION)) {
594 if (llt->selection.set())
596 llt->clearSelection();
601 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
603 if (!autoBreakRows && par->next())
604 collapseParagraphs(bv->buffer()->params);
611 setUpdateStatus(bv, what);
612 bool flag = (((need_update != CURSOR) && (need_update != NONE)) ||
613 (lt->status() != LyXText::UNCHANGED) || lt->selection.set());
617 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
618 if (need_update == CURSOR)
620 bv->owner()->showState();
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 = static_cast<UpdatableInset*>(inset);
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();
965 bv->owner()->setLayout(cpar(bv)->getLayout());
967 // Insert primary selection with middle mouse
968 // if there is a local selection in the current buffer,
971 if (paste_internally)
972 localDispatch(bv, LFUN_PASTE, "");
974 localDispatch(bv, LFUN_PASTESELECTION,
978 getLyXText(bv)->clearSelection();
981 no_selection = false;
985 bool InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
987 if (the_locking_inset) {
988 return the_locking_inset->insetButtonRelease(bv,
989 x - inset_x, y - inset_y,
992 int tmp_x = x - drawTextXOffset;
993 int tmp_y = y + insetAscent - getLyXText(bv)->first;
994 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
997 if (isHighlyEditableInset(inset)) {
998 ret = inset->insetButtonRelease(bv, x - inset_x,
999 y - inset_y, button);
1001 inset_x = cx(bv) - top_x + drawTextXOffset;
1002 inset_y = cy(bv) + drawTextYOffset;
1003 ret = inset->insetButtonRelease(bv, x - inset_x,
1004 y - inset_y, button);
1005 inset->edit(bv, x - inset_x,
1006 y - inset_y, button);
1008 updateLocal(bv, CURSOR_PAR, false);
1014 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
1016 if (no_selection || ((mouse_x == x) && (mouse_y == y)))
1018 if (the_locking_inset) {
1019 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1025 lt = getLyXText(bv);
1028 hideInsetCursor(bv);
1029 LyXCursor cur = lt->cursor;
1030 lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1031 if (cur == lt->cursor) {
1036 lt->setSelection(bv);
1037 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1038 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1042 updateLocal(bv, SELECTION, false);
1044 showInsetCursor(bv);
1048 void InsetText::insetKeyPress(XKeyEvent * xke)
1050 if (the_locking_inset) {
1051 the_locking_inset->insetKeyPress(xke);
1057 UpdatableInset::RESULT
1058 InsetText::localDispatch(BufferView * bv,
1059 kb_action action, string const & arg)
1061 bool was_empty = par->size() == 0 && !par->next();
1062 no_selection = false;
1063 UpdatableInset::RESULT
1064 result= UpdatableInset::localDispatch(bv, action, arg);
1065 if (result != UNDISPATCHED) {
1069 result = DISPATCHED;
1070 if ((action < 0) && arg.empty())
1073 if (the_locking_inset) {
1074 result = the_locking_inset->localDispatch(bv, action, arg);
1075 if (result == DISPATCHED_NOUPDATE)
1077 else if (result == DISPATCHED) {
1078 updateLocal(bv, CURSOR_PAR, false);
1080 } else if (result >= FINISHED) {
1082 case FINISHED_RIGHT:
1083 moveRightIntern(bv, false, false);
1084 result = DISPATCHED;
1087 if ((result = moveUp(bv)) >= FINISHED) {
1088 updateLocal(bv, CURSOR, false);
1089 bv->unlockInset(this);
1093 if ((result = moveDown(bv)) >= FINISHED) {
1094 updateLocal(bv, CURSOR, false);
1095 bv->unlockInset(this);
1099 result = DISPATCHED;
1102 the_locking_inset = 0;
1103 #ifdef WITH_WARNINGS
1104 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
1109 hideInsetCursor(bv);
1112 lt = getLyXText(bv);
1116 int updflag = false;
1119 case LFUN_SELFINSERT:
1120 if (bv->buffer()->isReadonly()) {
1121 // setErrorMessage(N_("Document is read only"));
1125 /* Automatically delete the currently selected
1126 * text and replace it with what is being
1127 * typed in now. Depends on lyxrc settings
1128 * "auto_region_delete", which defaults to
1131 setUndo(bv, Undo::INSERT,
1132 lt->cursor.par(), lt->cursor.par()->next());
1134 if (lyxrc.auto_region_delete) {
1135 if (lt->selection.set()) {
1136 lt->cutSelection(bv, false);
1139 lt->clearSelection();
1140 for (string::size_type i = 0; i < arg.length(); ++i) {
1141 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1144 lt->selection.cursor = lt->cursor;
1145 updwhat = CURSOR_PAR;
1147 result = DISPATCHED_NOUPDATE;
1149 // --- Cursor Movements -----------------------------------
1152 moveRight(bv, false, true);
1153 lt->setSelection(bv);
1154 updwhat = SELECTION;
1157 result = moveRight(bv);
1163 moveLeft(bv, false, true);
1164 lt->setSelection(bv);
1165 updwhat = SELECTION;
1169 result = moveLeft(bv);
1175 lt->setSelection(bv);
1176 updwhat = SELECTION;
1180 result = moveDown(bv);
1186 lt->setSelection(bv);
1187 updwhat = SELECTION;
1191 result = moveUp(bv);
1203 case LFUN_BACKSPACE: {
1204 setUndo(bv, Undo::DELETE,
1205 lt->cursor.par(), lt->cursor.par()->next());
1206 if (lt->selection.set())
1207 lt->cutSelection(bv);
1210 updwhat = CURSOR_PAR;
1216 setUndo(bv, Undo::DELETE,
1217 lt->cursor.par(), lt->cursor.par()->next());
1218 if (lt->selection.set()) {
1219 lt->cutSelection(bv);
1223 updwhat = CURSOR_PAR;
1229 setUndo(bv, Undo::DELETE,
1230 lt->cursor.par(), lt->cursor.par()->next());
1231 lt->cutSelection(bv);
1232 updwhat = CURSOR_PAR;
1239 lt->copySelection(bv);
1240 updwhat = CURSOR_PAR;
1242 case LFUN_PASTESELECTION:
1244 string const clip(bv->getClipboard());
1248 if (arg == "paragraph") {
1249 lt->insertStringAsParagraphs(bv, clip);
1251 lt->insertStringAsLines(bv, clip);
1253 updwhat = CURSOR_PAR;
1258 if (!autoBreakRows) {
1260 if (CutAndPaste::nrOfParagraphs() > 1) {
1261 Alert::alert(_("Impossible operation"),
1262 _("Cannot include more than one paragraph!"),
1267 setUndo(bv, Undo::INSERT,
1268 lt->cursor.par(), lt->cursor.par()->next());
1269 lt->pasteSelection(bv);
1270 updwhat = CURSOR_PAR;
1275 case LFUN_BREAKPARAGRAPH:
1276 if (!autoBreakRows) {
1277 result = DISPATCHED;
1280 lt->breakParagraph(bv, 0);
1284 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1285 if (!autoBreakRows) {
1286 result = DISPATCHED;
1289 lt->breakParagraph(bv, 1);
1294 case LFUN_BREAKLINE: {
1295 if (!autoBreakRows) {
1296 result = DISPATCHED;
1299 setUndo(bv, Undo::INSERT,
1300 lt->cursor.par(), lt->cursor.par()->next());
1301 lt->insertChar(bv, Paragraph::META_NEWLINE);
1302 updwhat = CURSOR_PAR;
1308 // do not set layouts on non breakable textinsets
1309 if (autoBreakRows) {
1310 layout_type cur_layout = cpar(bv)->layout;
1312 // Derive layout number from given argument (string)
1313 // and current buffer's textclass (number). */
1314 textclass_type tclass = bv->buffer()->params.textclass;
1315 std::pair <bool, layout_type> layout =
1316 textclasslist.NumberOfLayout(tclass, arg);
1318 // If the entry is obsolete, use the new one instead.
1320 string obs = textclasslist.Style(tclass,layout.second).
1323 layout = textclasslist.NumberOfLayout(tclass, obs);
1326 // see if we found the layout number:
1327 if (!layout.first) {
1328 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1329 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1333 if (cur_layout != layout.second) {
1334 cur_layout = layout.second;
1335 lt->setLayout(bv, layout.second);
1336 bv->owner()->setLayout(cpar(bv)->getLayout());
1337 updwhat = CURSOR_PAR;
1341 // reset the layout box
1342 bv->owner()->setLayout(cpar(bv)->getLayout());
1345 case LFUN_PARAGRAPH_SPACING:
1346 // This one is absolutely not working. When fiddling with this
1347 // it also seems to me that the paragraphs inside the insettext
1348 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1350 Paragraph * par = lt->cursor.par();
1351 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1352 float cur_value = 1.0;
1353 if (cur_spacing == Spacing::Other) {
1354 cur_value = par->params().spacing().getValue();
1357 istringstream istr(arg.c_str());
1360 Spacing::Space new_spacing = cur_spacing;
1361 float new_value = cur_value;
1363 lyxerr << "Missing argument to `paragraph-spacing'"
1365 } else if (tmp == "single") {
1366 new_spacing = Spacing::Single;
1367 } else if (tmp == "onehalf") {
1368 new_spacing = Spacing::Onehalf;
1369 } else if (tmp == "double") {
1370 new_spacing = Spacing::Double;
1371 } else if (tmp == "other") {
1372 new_spacing = Spacing::Other;
1375 lyxerr << "new_value = " << tmpval << endl;
1378 } else if (tmp == "default") {
1379 new_spacing = Spacing::Default;
1381 lyxerr << _("Unknown spacing argument: ")
1384 if (cur_spacing != new_spacing || cur_value != new_value) {
1385 par->params().spacing(Spacing(new_spacing, new_value));
1386 updwhat = CURSOR_PAR;
1393 if (!bv->Dispatch(action, arg))
1394 result = UNDISPATCHED;
1401 updateLocal(bv, updwhat, updflag);
1402 /// If the action has deleted all text in the inset, we need to change the
1403 // language to the language of the surronding text.
1404 if (!was_empty && par->size() == 0 && !par->next()) {
1405 LyXFont font(LyXFont::ALL_IGNORE);
1406 font.setLanguage(bv->getParentLanguage(this));
1407 setFont(bv, font, false);
1410 if (result < FINISHED) {
1411 showInsetCursor(bv);
1413 bv->unlockInset(this);
1418 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1421 buf->latexParagraphs(os, par, 0, texrow);
1422 return texrow.rows();
1426 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1428 Paragraph * p = par;
1429 unsigned int lines = 0;
1432 string const tmp = buf->asciiParagraph(p, linelen, p->previous()==0);
1433 lines += countChar(tmp, '\n');
1441 int InsetText::docbook(Buffer const * buf, ostream & os) const
1443 Paragraph * p = par;
1444 unsigned int lines = 0;
1446 vector<string> environment_stack(10);
1447 vector<string> environment_inner(10);
1449 int const command_depth = 0;
1452 Paragraph::depth_type depth = 0; // paragraph depth
1456 int desc_on = 0; // description mode
1458 LyXLayout const & style =
1459 textclasslist.Style(buf->params.textclass,
1462 // environment tag closing
1463 for (; depth > p->params().depth(); --depth) {
1464 if (environment_inner[depth] != "!-- --") {
1465 item_name = "listitem";
1466 buf->sgmlCloseTag(os, command_depth + depth,
1468 if (environment_inner[depth] == "varlistentry")
1469 buf->sgmlCloseTag(os, depth+command_depth,
1470 environment_inner[depth]);
1472 buf->sgmlCloseTag(os, depth + command_depth,
1473 environment_stack[depth]);
1474 environment_stack[depth].erase();
1475 environment_inner[depth].erase();
1478 if (depth == p->params().depth()
1479 && environment_stack[depth] != style.latexname()
1480 && !environment_stack[depth].empty()) {
1481 if (environment_inner[depth] != "!-- --") {
1482 item_name= "listitem";
1483 buf->sgmlCloseTag(os, command_depth+depth,
1485 if (environment_inner[depth] == "varlistentry")
1486 buf->sgmlCloseTag(os,
1487 depth + command_depth,
1488 environment_inner[depth]);
1491 buf->sgmlCloseTag(os, depth + command_depth,
1492 environment_stack[depth]);
1494 environment_stack[depth].erase();
1495 environment_inner[depth].erase();
1498 // Write opening SGML tags.
1499 switch (style.latextype) {
1500 case LATEX_PARAGRAPH:
1501 buf->sgmlOpenTag(os, depth + command_depth,
1506 buf->sgmlError(p, 0,
1507 _("Error : LatexType Command not allowed here.\n"));
1511 case LATEX_ENVIRONMENT:
1512 case LATEX_ITEM_ENVIRONMENT:
1513 if (depth < p->params().depth()) {
1514 depth = p->params().depth();
1515 environment_stack[depth].erase();
1518 if (environment_stack[depth] != style.latexname()) {
1519 if(environment_stack.size() == depth + 1) {
1520 environment_stack.push_back("!-- --");
1521 environment_inner.push_back("!-- --");
1523 environment_stack[depth] = style.latexname();
1524 environment_inner[depth] = "!-- --";
1525 buf->sgmlOpenTag(os, depth + command_depth,
1526 environment_stack[depth]);
1528 if (environment_inner[depth] != "!-- --") {
1529 item_name= "listitem";
1530 buf->sgmlCloseTag(os,
1531 command_depth + depth,
1533 if (environment_inner[depth] == "varlistentry")
1534 buf->sgmlCloseTag(os,
1535 depth + command_depth,
1536 environment_inner[depth]);
1540 if (style.latextype == LATEX_ENVIRONMENT) {
1541 if (!style.latexparam().empty()) {
1542 if(style.latexparam() == "CDATA")
1545 buf->sgmlOpenTag(os, depth + command_depth,
1546 style.latexparam());
1551 desc_on = (style.labeltype == LABEL_MANUAL);
1554 environment_inner[depth]= "varlistentry";
1556 environment_inner[depth]= "listitem";
1558 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1559 environment_inner[depth]);
1563 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1567 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1572 buf->sgmlOpenTag(os, depth + command_depth,
1577 buf->simpleDocBookOnePar(os, p, desc_on,
1578 depth + 1 + command_depth);
1582 // write closing SGML tags
1583 switch (style.latextype) {
1584 case LATEX_ENVIRONMENT:
1585 if (!style.latexparam().empty()) {
1586 if(style.latexparam() == "CDATA")
1589 buf->sgmlCloseTag(os, depth + command_depth,
1590 style.latexparam());
1593 case LATEX_ITEM_ENVIRONMENT:
1594 if (desc_on == 1) break;
1596 buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1598 case LATEX_PARAGRAPH:
1599 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1602 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1608 for (int d = depth; d >= 0; --d) {
1609 if (!environment_stack[depth].empty()) {
1610 if (environment_inner[depth] != "!-- --") {
1611 item_name = "listitem";
1612 buf->sgmlCloseTag(os, command_depth + depth,
1614 if (environment_inner[depth] == "varlistentry")
1615 buf->sgmlCloseTag(os, depth + command_depth,
1616 environment_inner[depth]);
1619 buf->sgmlCloseTag(os, depth + command_depth,
1620 environment_stack[depth]);
1628 void InsetText::validate(LaTeXFeatures & features) const
1630 Paragraph * p = par;
1632 p->validate(features);
1638 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1640 if (textclasslist.Style(buf->params.textclass,
1641 p->getLayout()).labeltype != LABEL_MANUAL)
1644 return p->beginningOfMainBody();
1648 void InsetText::getCursorPos(BufferView * bv,
1649 int & x, int & y) const
1651 if (the_locking_inset) {
1652 the_locking_inset->getCursorPos(bv, x, y);
1660 unsigned int InsetText::insetInInsetY()
1662 if (!the_locking_inset)
1665 return (inset_y + the_locking_inset->insetInInsetY());
1669 void InsetText::toggleInsetCursor(BufferView * bv)
1671 if (the_locking_inset) {
1672 the_locking_inset->toggleInsetCursor(bv);
1676 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1678 int const asc = lyxfont::maxAscent(font);
1679 int const desc = lyxfont::maxDescent(font);
1681 if (isCursorVisible())
1682 bv->hideLockedInsetCursor();
1684 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1685 toggleCursorVisible();
1689 void InsetText::showInsetCursor(BufferView * bv, bool show)
1691 if (the_locking_inset) {
1692 the_locking_inset->showInsetCursor(bv, show);
1695 if (!isCursorVisible()) {
1696 LyXFont const font =
1697 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1699 int const asc = lyxfont::maxAscent(font);
1700 int const desc = lyxfont::maxDescent(font);
1702 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1704 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1705 setCursorVisible(true);
1710 void InsetText::hideInsetCursor(BufferView * bv)
1712 if (isCursorVisible()) {
1713 bv->hideLockedInsetCursor();
1714 setCursorVisible(false);
1716 if (the_locking_inset)
1717 the_locking_inset->hideInsetCursor(bv);
1721 void InsetText::fitInsetCursor(BufferView * bv) const
1723 if (the_locking_inset) {
1724 the_locking_inset->fitInsetCursor(bv);
1727 LyXFont const font =
1728 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1730 int const asc = lyxfont::maxAscent(font);
1731 int const desc = lyxfont::maxDescent(font);
1733 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1737 UpdatableInset::RESULT
1738 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1740 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1741 return moveLeftIntern(bv, false, activate_inset, selecting);
1743 return moveRightIntern(bv, false, activate_inset, selecting);
1747 UpdatableInset::RESULT
1748 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1750 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1751 return moveRightIntern(bv, true, activate_inset, selecting);
1753 return moveLeftIntern(bv, true, activate_inset, selecting);
1757 UpdatableInset::RESULT
1758 InsetText::moveRightIntern(BufferView * bv, bool behind,
1759 bool activate_inset, bool selecting)
1761 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1762 return FINISHED_RIGHT;
1763 if (activate_inset && checkAndActivateInset(bv, behind))
1765 getLyXText(bv)->cursorRight(bv);
1767 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1768 return DISPATCHED_NOUPDATE;
1772 UpdatableInset::RESULT
1773 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1774 bool activate_inset, bool selecting)
1776 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1778 getLyXText(bv)->cursorLeft(bv);
1780 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1781 if (activate_inset && checkAndActivateInset(bv, behind))
1783 return DISPATCHED_NOUPDATE;
1787 UpdatableInset::RESULT
1788 InsetText::moveUp(BufferView * bv)
1790 if (!crow(bv)->previous())
1792 getLyXText(bv)->cursorUp(bv);
1793 return DISPATCHED_NOUPDATE;
1797 UpdatableInset::RESULT
1798 InsetText::moveDown(BufferView * bv)
1800 if (!crow(bv)->next())
1801 return FINISHED_DOWN;
1802 getLyXText(bv)->cursorDown(bv);
1803 return DISPATCHED_NOUPDATE;
1807 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1809 if (the_locking_inset) {
1810 if (the_locking_inset->insetAllowed(inset))
1811 return the_locking_inset->insertInset(bv, inset);
1816 lt = getLyXText(bv);
1819 setUndo(bv, Undo::FINISH, lt->cursor.par(), lt->cursor.par()->next());
1821 inset->setOwner(this);
1822 hideInsetCursor(bv);
1823 lt->insertInset(bv, inset);
1827 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1833 bool InsetText::insetAllowed(Inset::Code code) const
1835 // in_insetAllowed is a really gross hack,
1836 // to allow us to call the owner's insetAllowed
1837 // without stack overflow, which can happen
1838 // when the owner uses InsetCollapsable::insetAllowed()
1840 if (in_insetAllowed)
1842 in_insetAllowed = true;
1843 if (the_locking_inset)
1844 ret = the_locking_inset->insetAllowed(code);
1846 ret = owner()->insetAllowed(code);
1847 in_insetAllowed = false;
1852 UpdatableInset * InsetText::getLockingInset() const
1854 return the_locking_inset ? the_locking_inset->getLockingInset() :
1855 const_cast<InsetText *>(this);
1859 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1863 if (the_locking_inset)
1864 return the_locking_inset->getFirstLockingInsetOfType(c);
1869 bool InsetText::showInsetDialog(BufferView * bv) const
1871 if (the_locking_inset)
1872 return the_locking_inset->showInsetDialog(bv);
1877 std::vector<string> const InsetText::getLabelList() const
1879 std::vector<string> label_list;
1881 Paragraph * tpar = par;
1883 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1884 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1885 for (; beg != end; ++beg) {
1886 std::vector<string> const l = (*beg)->getLabelList();
1887 label_list.insert(label_list.end(), l.begin(), l.end());
1889 tpar = tpar->next();
1895 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1898 if (the_locking_inset) {
1899 the_locking_inset->setFont(bv, font, toggleall, selectall);
1902 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
1903 getLyXText(bv)->setFont(bv, font, toggleall);
1908 lt = getLyXText(bv);
1911 if (lt->selection.set()) {
1912 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1916 lt->toggleFree(bv, font, toggleall);
1918 lt->clearSelection();
1920 bool flag = (selectall || lt->selection.set());
1924 updateLocal(bv, FULL, true);
1926 updateLocal(bv, CURSOR_PAR, true);
1930 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1932 if (cpar(bv)->isInset(cpos(bv))) {
1936 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1937 if (!isHighlyEditableInset(inset))
1939 LyXFont const font =
1940 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1942 x = inset->width(bv, font);
1943 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1946 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1948 //inset_x = cx(bv) - top_x + drawTextXOffset;
1949 //inset_y = cy(bv) + drawTextYOffset;
1950 inset->edit(bv, x, y, 0);
1951 if (!the_locking_inset)
1953 updateLocal(bv, CURSOR, false);
1960 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1963 x -= drawTextXOffset;
1965 int dummyy = y + insetAscent;
1966 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
1973 inset_x = cx(bv) - top_x + drawTextXOffset;
1974 inset_y = cy(bv) + drawTextYOffset;
1975 inset->edit(bv, x - inset_x, y - inset_y, button);
1976 if (!the_locking_inset)
1978 updateLocal(bv, CURSOR, false);
1985 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1988 int w = UpdatableInset::getMaxWidth(bv, inset);
1993 w = w - top_x + owner()->x();
1996 w -= (2 * TEXT_TO_INSET_OFFSET);
1999 return UpdatableInset::getMaxWidth(bv, inset);
2004 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2006 // we have to unlock any locked inset otherwise we're in troubles
2007 the_locking_inset = 0;
2009 Paragraph * tmp = par->next();
2014 par = new Paragraph(*p, same_id);
2015 par->setInsetOwner(this);
2016 Paragraph * np = par;
2019 np->next(new Paragraph(*p, same_id));
2020 np->next()->previous(np);
2022 np->setInsetOwner(this);
2029 void InsetText::setText(string const & data)
2032 LyXFont font(LyXFont::ALL_SANE);
2033 for (unsigned int i=0; i < data.length(); ++i)
2034 par->insertChar(i, data[i], font);
2038 void InsetText::setAutoBreakRows(bool flag)
2040 if (flag != autoBreakRows) {
2041 autoBreakRows = flag;
2049 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2051 if (how != drawFrame_) {
2054 updateLocal(bv, DRAW_FRAME, false);
2059 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2061 if (frame_color != col) {
2064 updateLocal(bv, DRAW_FRAME, false);
2069 int InsetText::cx(BufferView * bv) const
2071 // we do nothing dangerous so we use a local cache
2072 LyXText * llt = getLyXText(bv);
2073 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2074 if (the_locking_inset) {
2075 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2077 if (font.isVisibleRightToLeft())
2078 x -= the_locking_inset->width(bv, font);
2084 int InsetText::cy(BufferView * bv) const
2087 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2091 pos_type InsetText::cpos(BufferView * bv) const
2093 return getLyXText(bv)->cursor.pos();
2097 Paragraph * InsetText::cpar(BufferView * bv) const
2099 return getLyXText(bv)->cursor.par();
2103 bool InsetText::cboundary(BufferView * bv) const
2105 return getLyXText(bv)->cursor.boundary();
2109 Row * InsetText::crow(BufferView * bv) const
2111 return getLyXText(bv)->cursor.row();
2115 LyXText * InsetText::getLyXText(BufferView const * lbv,
2116 bool const recursive) const
2118 if (!recursive && (cached_bview == lbv)) {
2119 LyXText * lt = cached_text.get();
2120 lyx::Assert(lt && lt->firstRow()->par() == par);
2121 return cached_text.get();
2124 // Super UGLY! (Lgb)
2125 BufferView * bv = const_cast<BufferView *>(lbv);
2128 Cache::iterator it = cache.find(bv);
2130 if (it != cache.end()) {
2134 resizeLyXText(do_resize);
2135 if (lt || !it->second.remove) {
2136 lyx::Assert(it->second.text.get());
2137 cached_text = it->second.text;
2138 if (recursive && the_locking_inset) {
2139 return the_locking_inset->getLyXText(bv, true);
2141 return cached_text.get();
2142 } else if (it->second.remove) {
2144 saveLyXTextState(it->second.text.get());
2151 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2152 cached_text->init(bv);
2153 restoreLyXTextState(bv, cached_text.get());
2155 cache.insert(make_pair(bv, cached_text));
2157 if (the_locking_inset && recursive) {
2158 return the_locking_inset->getLyXText(bv);
2160 return cached_text.get();
2164 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2168 Cache::iterator it = cache.find(bv);
2170 if (it == cache.end()) {
2174 lyx::Assert(it->second.text.get());
2176 it->second.remove = true;
2178 /// then remove all LyXText in text-insets
2179 Paragraph * p = par;
2180 for (; p; p = p->next()) {
2181 p->deleteInsetsLyXText(bv);
2187 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2190 // we cannot resize this because we are in use!
2191 // so do this on the next possible getLyXText()
2196 // lyxerr << "InsetText::resizeLyXText\n";
2197 if (!par->next() && !par->size()) { // no data, resize not neccessary!
2198 // we have to do this as a fixed width may have changed!
2199 LyXText * t = getLyXText(bv);
2200 saveLyXTextState(t);
2202 restoreLyXTextState(bv, t);
2205 // one endless line, resize normally not necessary
2206 if (!force && getMaxWidth(bv, this) < 0)
2209 Cache::iterator it = cache.find(bv);
2210 if (it == cache.end()) {
2213 lyx::Assert(it->second.text.get());
2215 LyXText * t = it->second.text.get();
2216 saveLyXTextState(t);
2217 for (Paragraph * p = par; p; p = p->next()) {
2218 p->resizeInsetsLyXText(bv);
2221 restoreLyXTextState(bv, t);
2222 if (the_locking_inset) {
2223 inset_x = cx(bv) - top_x + drawTextXOffset;
2224 inset_y = cy(bv) + drawTextYOffset;
2228 t->first = bv->screen()->topCursorVisible(t);
2231 updateLocal(bv, FULL, false);
2232 // this will scroll the screen such that the cursor becomes visible
2233 bv->updateScrollbar();
2235 need_update |= FULL;
2240 void InsetText::reinitLyXText() const
2243 // we cannot resize this because we are in use!
2244 // so do this on the next possible getLyXText()
2250 // lyxerr << "InsetText::reinitLyXText\n";
2251 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2252 lyx::Assert(it->second.text.get());
2254 LyXText * t = it->second.text.get();
2255 BufferView * bv = it->first;
2257 saveLyXTextState(t);
2258 for (Paragraph * p = par; p; p = p->next()) {
2259 p->resizeInsetsLyXText(bv);
2262 restoreLyXTextState(bv, t);
2263 if (the_locking_inset) {
2264 inset_x = cx(bv) - top_x + drawTextXOffset;
2265 inset_y = cy(bv) + drawTextYOffset;
2268 t->first = bv->screen()->topCursorVisible(t);
2271 updateLocal(bv, FULL, false);
2272 // this will scroll the screen such that the cursor becomes visible
2273 bv->updateScrollbar();
2281 void InsetText::removeNewlines()
2283 bool changed = false;
2285 for (Paragraph * p = par; p; p = p->next()) {
2286 for (int i = 0; i < p->size(); ++i) {
2287 if (p->getChar(i) == Paragraph::META_NEWLINE) {
2298 bool InsetText::nodraw() const
2300 if (the_locking_inset)
2301 return the_locking_inset->nodraw();
2302 return UpdatableInset::nodraw();
2306 int InsetText::scroll(bool recursive) const
2308 int sx = UpdatableInset::scroll(false);
2310 if (recursive && the_locking_inset)
2311 sx += the_locking_inset->scroll(recursive);
2317 bool InsetText::doClearArea() const
2319 return !locked || (need_update & (FULL|INIT));
2323 void InsetText::selectAll(BufferView * bv)
2325 getLyXText(bv)->cursorTop(bv);
2326 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2327 getLyXText(bv)->cursorBottom(bv);
2328 getLyXText(bv)->setSelection(bv);
2332 void InsetText::clearSelection(BufferView * bv)
2334 getLyXText(bv)->clearSelection();
2338 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2341 Painter & pain = bv->painter();
2343 int h = ascent(bv, dummy) + descent(bv, dummy);
2344 int ty = baseline - insetAscent;
2350 if ((ty + h) > pain.paperHeight())
2351 h = pain.paperHeight();
2352 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2353 w = pain.paperWidth();
2354 // w -= TEXT_TO_INSET_OFFSET;
2355 pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2358 frame_is_visible = false;
2362 Paragraph * InsetText::getParFromID(int id) const
2365 Paragraph * result = par;
2366 Paragraph * ires = 0;
2367 while (result && result->id() != id) {
2368 if ((ires = result->getParFromID(id)))
2370 result = result->next();
2374 Paragraph * tmp = par;
2376 if (tmp->id() == id) {
2379 Paragraph * tmp2 = tmp->getParFromID(id);
2390 Paragraph * InsetText::firstParagraph() const
2393 if (the_locking_inset)
2394 if ((result = the_locking_inset->firstParagraph()))
2400 Paragraph * InsetText::getFirstParagraph(int i) const
2402 return (i == 0) ? par : 0;
2406 LyXCursor const & InsetText::cursor(BufferView * bv) const
2408 if (the_locking_inset)
2409 return the_locking_inset->cursor(bv);
2410 return getLyXText(bv)->cursor;
2414 Paragraph * InsetText::paragraph() const
2420 void InsetText::paragraph(Paragraph * p)
2422 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2423 // caller of this function has to take care of it. This IS important
2424 // as we could have to insert a paragraph before this one and just
2425 // link the actual to a new ones next and set it with this function
2428 // set ourself as owner for all the paragraphs inserted!
2429 Paragraph * np = par;
2431 np->setInsetOwner(this);
2435 // redraw myself when asked for
2440 Inset * InsetText::getInsetFromID(int id_arg) const
2443 return const_cast<InsetText *>(this);
2445 Paragraph * lp = par;
2448 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2449 en = lp->inset_iterator_end();
2452 if ((*it)->id() == id_arg)
2454 Inset * in = (*it)->getInsetFromID(id_arg);
2464 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2470 lt = getLyXText(bv);
2473 if (the_locking_inset) {
2474 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2481 #warning Dekel please have a look on this one RTL? (Jug)
2483 // we have to go on checking so move cusor to the right
2484 lt->cursor.pos(lt->cursor.pos() + 1);
2486 str = lt->selectNextWordToSpellcheck(bv, value);
2488 bv->unlockInset(const_cast<InsetText *>(this));
2497 void InsetText::selectSelectedWord(BufferView * bv)
2499 if (the_locking_inset) {
2500 the_locking_inset->selectSelectedWord(bv);
2503 getLyXText(bv)->selectSelectedWord(bv);
2504 updateLocal(bv, SELECTION, false);
2508 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2510 if (the_locking_inset) {
2511 the_locking_inset->toggleSelection(bv, kill_selection);
2515 lt = getLyXText(bv);
2519 int x = top_x + TEXT_TO_INSET_OFFSET;
2521 Row * row = lt->firstRow();
2522 int y_offset = top_baseline - row->ascent_of_text();
2524 while ((row != 0) && ((y+row->height()) <= 0)) {
2531 if (need_update & SELECTION)
2533 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2539 bool InsetText::searchForward(BufferView * bv, string const & str,
2542 if (the_locking_inset) {
2543 if (the_locking_inset->searchForward(bv, str, cs, mw))
2547 lt = getLyXText(bv);
2550 Paragraph * lpar = lt->cursor.par();
2551 pos_type pos = lt->cursor.pos();
2552 if (pos < lpar->size() - 1)
2556 lpar = lpar->next();
2561 // we have to unlock ourself in this function by default!
2562 bv->unlockInset(const_cast<InsetText *>(this));
2565 lt->setCursor(bv, lpar, pos);
2569 if (LyXFind(bv, str, true, true, cs , mw)) {
2572 // we have to unlock ourself in this function by default!
2573 bv->unlockInset(const_cast<InsetText *>(this));
2577 bool InsetText::searchBackward(BufferView * bv, string const & str,
2580 if (the_locking_inset)
2581 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2583 if (LyXFind(bv, str, false, true, cs, mw)) {
2586 // we have to unlock ourself in this function by default!
2587 bv->unlockInset(const_cast<InsetText *>(this));
2592 bool InsetText::checkInsertChar(LyXFont & font)
2595 return owner()->checkInsertChar(font);
2600 void InsetText::collapseParagraphs(BufferParams const & bparams) const
2602 while(par->next()) {
2603 if (!par->isSeparator(par->size()-1))
2604 par->insertChar(par->size()-1, ' ');
2605 par->pasteParagraph(bparams);