1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998-2001 The LyX Team.
8 * ======================================================
14 #pragma implementation
17 #include "insettext.h"
18 #include "paragraph.h"
22 #include "commandtags.h"
25 #include "BufferView.h"
26 #include "lyxtextclasslist.h"
27 #include "LaTeXFeatures.h"
30 #include "lyxcursor.h"
31 #include "CutAndPaste.h"
37 #include "trans_mgr.h"
38 #include "lyxscreen.h"
42 #include "ParagraphParameters.h"
43 #include "undo_funcs.h"
46 #include "frontends/Alert.h"
48 #include "support/textutils.h"
49 #include "support/LAssert.h"
50 #include "support/lstrings.h"
51 #include "support/lyxalgo.h" // lyx::count
68 using lyx::layout_type;
69 using lyx::textclass_type;
71 extern unsigned char getCurrentTextClass(Buffer *);
72 extern bool math_insert_greek(BufferView *, char);
73 extern int greek_kb_flag;
76 // These functions should probably go into bufferview_funcs somehow (Jug)
78 void InsetText::saveLyXTextState(LyXText * t) const
80 // check if my paragraphs are still valid
83 if (p == t->cursor.par())
88 if (p && t->cursor.pos() <= p->size()) {
89 sstate.lpar = t->cursor.par();
90 sstate.pos = t->cursor.pos();
91 sstate.boundary = t->cursor.boundary();
92 sstate.selstartpar = t->selection.start.par();
93 sstate.selstartpos = t->selection.start.pos();
94 sstate.selstartboundary = t->selection.start.boundary();
95 sstate.selendpar = t->selection.end.par();
96 sstate.selendpos = t->selection.end.pos();
97 sstate.selendboundary = t->selection.end.boundary();
98 sstate.selection = t->selection.set();
99 sstate.mark_set = t->selection.mark();
100 sstate.refresh = t->refresh_row != 0;
106 void InsetText::restoreLyXTextState(BufferView * bv, LyXText * t) const
109 t->selection.set(true);
110 /* at this point just to avoid the Delete-Empty-Paragraph
111 * Mechanism when setting the cursor */
112 t->selection.mark(sstate.mark_set);
113 if (sstate.selection) {
114 t->setCursor(bv, sstate.selstartpar, sstate.selstartpos,
115 true, sstate.selstartboundary);
116 t->selection.cursor = t->cursor;
117 t->setCursor(bv, sstate.selendpar, sstate.selendpos,
118 true, sstate.selendboundary);
120 t->setCursor(bv, sstate.lpar, sstate.pos);
122 t->setCursor(bv, sstate.lpar, sstate.pos, true, sstate.boundary);
123 t->selection.cursor = t->cursor;
124 t->selection.set(false);
126 if (sstate.refresh) {
132 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
139 InsetText::InsetText()
140 : UpdatableInset(), lt(0), in_update(false), do_resize(0),
148 InsetText::InsetText(InsetText const & in, bool same_id)
149 : UpdatableInset(in, same_id), lt(0), in_update(false), do_resize(0),
157 InsetText & InsetText::operator=(InsetText const & it)
164 void InsetText::init(InsetText const * ins, bool same_id)
167 setParagraphData(ins->par, same_id);
168 autoBreakRows = ins->autoBreakRows;
169 drawFrame_ = ins->drawFrame_;
170 frame_color = ins->frame_color;
176 p->setInsetOwner(this);
179 the_locking_inset = 0;
181 frame_color = LColor::insetframe;
182 autoBreakRows = false;
189 no_selection = false;
196 last_drawn_width = -1;
197 frame_is_visible = false;
200 in_insetAllowed = false;
204 InsetText::~InsetText()
211 Paragraph * tmp = par->next();
218 void InsetText::clear()
221 Paragraph * tmp = par->next();
231 Inset * InsetText::clone(Buffer const &, bool same_id) const
233 return new InsetText(*this, same_id);
237 void InsetText::write(Buffer const * buf, ostream & os) const
240 writeParagraphData(buf, os);
244 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
246 par->writeFile(buf, os, buf->params, 0);
250 void InsetText::read(Buffer const * buf, LyXLex & lex)
254 Paragraph * return_par = 0;
255 Paragraph::depth_type depth = 0;
256 LyXFont font(LyXFont::ALL_INHERIT);
262 token = lex.getString();
265 if (token == "\\end_inset") {
266 #ifndef NO_COMPABILITY
267 const_cast<Buffer*>(buf)->insertErtContents(par, pos, false);
272 if (const_cast<Buffer*>(buf)->
273 parseSingleLyXformat2Token(lex, par, return_par,
274 token, pos, depth, font)) {
275 // the_end read this should NEVER happen
276 lex.printError("\\the_end read in inset! Error in document!");
284 return_par->setInsetOwner(this);
285 return_par = return_par->next();
288 if (token != "\\end_inset") {
289 lex.printError("Missing \\end_inset at this point. "
296 int InsetText::ascent(BufferView * bv, LyXFont const &) const
298 insetAscent = getLyXText(bv)->firstRow()->ascent_of_text() +
299 TEXT_TO_INSET_OFFSET;
304 int InsetText::descent(BufferView * bv, LyXFont const &) const
306 LyXText * llt = getLyXText(bv);
307 insetDescent = llt->height - llt->firstRow()->ascent_of_text() +
308 TEXT_TO_INSET_OFFSET;
313 int InsetText::width(BufferView * bv, LyXFont const &) const
315 insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
316 (2 * TEXT_TO_INSET_OFFSET);
317 insetWidth = max(insetWidth, 10);
322 int InsetText::textWidth(BufferView * bv, bool fordraw) const
325 if (!autoBreakRows) {
328 w = getMaxWidth(bv, this);
331 return max(w - (2 * TEXT_TO_INSET_OFFSET),
332 (int)getLyXText(bv)->width);
336 return w - (2 * TEXT_TO_INSET_OFFSET);
340 void InsetText::draw(BufferView * bv, LyXFont const & f,
341 int baseline, float & x, bool cleared) const
346 Painter & pain = bv->painter();
348 // this is the first thing we have to ask because if the x pos
349 // changed we have to do a complete rebreak of the text as we
350 // may have few space to draw in. Well we should check on this too
352 if (top_x != int(x)) {
355 int nw = getMaxWidth(bv, this);
356 if (nw > 0 && old_max_width != nw) {
359 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
364 // call these methods so that insetWidth, insetAscent and
365 // insetDescent have the right values.
370 // repaint the background if needed
371 if (cleared && backgroundColor() != LColor::background) {
372 clearInset(bv, baseline, cleared);
375 // no draw is necessary !!!
376 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
377 top_baseline = baseline;
379 if (need_update & CLEAR_FRAME)
380 clearFrame(pain, cleared);
387 x += static_cast<float>(scroll());
389 // if top_x differs we did it already
390 if (!cleared && (old_x == int(x))
391 && ((need_update&(INIT|FULL)) || (top_baseline != baseline)
392 ||(last_drawn_width != insetWidth)))
394 // Condition necessary to eliminate bug 59 attachment 37
396 clearInset(bv, baseline, cleared);
400 frame_is_visible = false;
402 if (!cleared && (need_update == NONE)) {
404 drawFrame(pain, cleared);
408 top_baseline = baseline;
409 top_y = baseline - insetAscent;
411 if (last_drawn_width != insetWidth) {
413 clearInset(bv, baseline, cleared);
415 last_drawn_width = insetWidth;
418 if (the_locking_inset && (cpar(bv) == inset_par)
419 && (cpos(bv) == inset_pos)) {
420 inset_x = cx(bv) - top_x + drawTextXOffset;
421 inset_y = cy(bv) + drawTextYOffset;
423 if (!cleared && (need_update == CURSOR)
424 && !getLyXText(bv)->selection.set()) {
425 drawFrame(pain, cleared);
435 x += TEXT_TO_INSET_OFFSET;
437 Row * row = lt->firstRow();
438 int y_offset = baseline - row->ascent_of_text();
439 int ph = pain.paperHeight();
442 while ((row != 0) && ((y+row->height()) <= 0)) {
444 first += row->height();
450 if (cleared || (need_update&(INIT|FULL))) {
453 while ((row != 0) && (yf < ph)) {
454 lt->getVisibleRow(bv, y+y_offset, int(x), row,
460 } else if (!locked) {
461 if (need_update & CURSOR) {
462 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
463 lt->clearSelection();
464 lt->selection.cursor = lt->cursor;
466 bv->screen()->update(lt, bv, y_offset, int(x));
469 if (need_update & SELECTION) {
470 bv->screen()->toggleToggle(lt, bv, y_offset, int(x));
471 } else if (need_update & CURSOR) {
472 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
473 lt->clearSelection();
474 lt->selection.cursor = lt->cursor;
476 bv->screen()->update(lt, bv, y_offset, int(x));
481 lt->status(bv, LyXText::UNCHANGED);
482 if ((need_update != CURSOR_PAR) &&
483 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked))) {
484 drawFrame(pain, cleared);
485 } else if (need_update & CLEAR_FRAME) {
486 clearFrame(pain, cleared);
489 x += insetWidth - TEXT_TO_INSET_OFFSET;
491 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
493 } else if (need_update != INIT) {
501 void InsetText::drawFrame(Painter & pain, bool cleared) const
503 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
504 if (!frame_is_visible || cleared) {
505 frame_x = top_x + ttoD2;
506 frame_y = top_baseline - insetAscent + ttoD2;
507 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
508 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
509 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
511 frame_is_visible = true;
516 void InsetText::clearFrame(Painter & pain, bool cleared) const
518 if (frame_is_visible) {
520 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
523 frame_is_visible = false;
528 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
531 if (reinit && owner()) {
533 owner()->update(bv, font, true);
538 if (reinit || need_update == INIT) {
540 // we should put this call where we set need_update to INIT!
543 owner()->update(bv, font, true);
547 if (the_locking_inset) {
548 inset_x = cx(bv) - top_x + drawTextXOffset;
549 inset_y = cy(bv) + drawTextYOffset;
550 the_locking_inset->update(bv, font, reinit);
558 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
561 lt->updateInset(bv, the_locking_inset);
563 if (lt->status() == LyXText::NEED_MORE_REFRESH)
571 void InsetText::setUpdateStatus(BufferView * bv, int what) const
573 // this does nothing dangerous so use only a localized buffer
574 LyXText * llt = getLyXText(bv);
577 // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
578 // if we don't break row so that we only have one row to update!
579 if ((llt->status() == LyXText::NEED_MORE_REFRESH) ||
581 (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
584 } else if (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
585 need_update |= CURSOR_PAR;
588 // this to not draw a selection when we redraw all of it!
589 if (need_update & CURSOR && !(need_update & SELECTION)) {
590 if (llt->selection.set())
592 llt->clearSelection();
597 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
599 if (!autoBreakRows && par->next())
600 collapseParagraphs(bv->buffer()->params);
607 setUpdateStatus(bv, what);
608 bool flag = (((need_update != CURSOR) && (need_update != NONE)) ||
609 (lt->status() != LyXText::UNCHANGED) || lt->selection.set());
613 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
617 if (need_update == CURSOR)
619 bv->owner()->showState();
620 bv->owner()->updateMenubar();
621 bv->owner()->updateToolbar();
622 if (old_par != cpar(bv)) {
623 bv->owner()->setLayout(cpar(bv)->getLayout());
629 string const InsetText::editMessage() const
631 return _("Opened Text Inset");
635 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
637 UpdatableInset::edit(bv, x, y, button);
639 if (!bv->lockInset(this)) {
640 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
644 the_locking_inset = 0;
645 inset_pos = inset_x = inset_y = 0;
646 inset_boundary = false;
649 int tmp_y = (y < 0) ? 0 : y;
655 if (!checkAndActivateInset(bv, x, tmp_y, button))
656 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
658 lt->clearSelection();
660 // If the inset is empty set the language of the current font to the
661 // language to the surronding text (if different).
662 if (par->size() == 0 && !par->next() &&
663 bv->getParentLanguage(this) != lt->current_font.language())
665 LyXFont font(LyXFont::ALL_IGNORE);
666 font.setLanguage(bv->getParentLanguage(this));
667 setFont(bv, font, false);
674 if (drawFrame_ == LOCKED)
675 code = CURSOR|DRAW_FRAME;
676 updateLocal(bv, code, false);
680 void InsetText::edit(BufferView * bv, bool front)
682 UpdatableInset::edit(bv, front);
684 if (!bv->lockInset(this)) {
685 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
689 the_locking_inset = 0;
690 inset_pos = inset_x = inset_y = 0;
691 inset_boundary = false;
700 lt->setCursor(bv, par, 0);
705 // int const pos = (p->size() ? p->size()-1 : p->size());
706 lt->setCursor(bv, p, p->size());
708 lt->clearSelection();
710 // If the inset is empty set the language of the current font to the
711 // language to the surronding text (if different).
712 if (par->size() == 0 && !par->next() &&
713 bv->getParentLanguage(this) != lt->current_font.language()) {
714 LyXFont font(LyXFont::ALL_IGNORE);
715 font.setLanguage(bv->getParentLanguage(this));
716 setFont(bv, font, false);
722 if (drawFrame_ == LOCKED)
723 code = CURSOR|DRAW_FRAME;
724 updateLocal(bv, code, false);
728 void InsetText::insetUnlock(BufferView * bv)
730 if (the_locking_inset) {
731 the_locking_inset->insetUnlock(bv);
732 the_locking_inset = 0;
735 no_selection = false;
738 if (drawFrame_ == LOCKED)
739 code = CURSOR|CLEAR_FRAME;
747 if (lt->selection.set()) {
748 lt->clearSelection();
750 } else if (owner()) {
751 bv->owner()->setLayout(owner()->getLyXText(bv)
752 ->cursor.par()->getLayout());
754 bv->owner()->setLayout(bv->text->cursor.par()->getLayout());
755 // hack for deleteEmptyParMech
757 lt->setCursor(bv, par, 0);
758 } else if (par->next()) {
759 lt->setCursor(bv, par->next(), 0);
763 updateLocal(bv, code, false);
766 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
768 the_locking_inset = inset;
769 inset_x = cx(bv) - top_x + drawTextXOffset;
770 inset_y = cy(bv) + drawTextYOffset;
771 inset_pos = cpos(bv);
772 inset_par = cpar(bv);
773 inset_boundary = cboundary(bv);
774 updateLocal(bv, CURSOR, false);
778 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
780 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
784 if (!the_locking_inset) {
786 int const id = inset->id();
788 Paragraph::inset_iterator it =
789 p->inset_iterator_begin();
790 Paragraph::inset_iterator const end =
791 p->inset_iterator_end();
792 for (; it != end; ++it) {
793 if ((*it) == inset) {
794 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
795 lockInset(bv, inset);
798 if ((*it)->getInsetFromID(id)) {
799 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
801 return the_locking_inset->lockInsetInInset(bv, inset);
808 if (inset == cpar(bv)->getInset(cpos(bv))) {
809 lyxerr[Debug::INSETS] << "OK" << endl;
810 lockInset(bv, inset);
812 } else if (the_locking_inset && (the_locking_inset == inset)) {
813 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
814 lyxerr[Debug::INSETS] << "OK" << endl;
815 inset_x = cx(bv) - top_x + drawTextXOffset;
816 inset_y = cy(bv) + drawTextYOffset;
818 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
820 } else if (the_locking_inset) {
821 lyxerr[Debug::INSETS] << "MAYBE" << endl;
822 return the_locking_inset->lockInsetInInset(bv, inset);
824 lyxerr[Debug::INSETS] << "NOT OK" << endl;
829 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
832 if (!the_locking_inset)
834 if (the_locking_inset == inset) {
835 the_locking_inset->insetUnlock(bv);
836 getLyXText(bv)->updateInset(bv, inset);
837 the_locking_inset = 0;
839 moveRight(bv, false);
840 old_par = 0; // force layout setting
844 updateLocal(bv, CURSOR, false);
847 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
851 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
853 if (!autoBreakRows && par->next())
854 collapseParagraphs(bv->buffer()->params);
862 if (!the_locking_inset) {
863 bool found = lt->updateInset(bv, inset);
867 setUpdateStatus(bv, NONE);
870 if (the_locking_inset != inset) {
871 bool found = the_locking_inset->updateInsetInInset(bv, inset);
875 setUpdateStatus(bv, CURSOR_PAR);
878 bool found = lt->updateInset(bv, inset);
882 setUpdateStatus(bv, CURSOR_PAR);
883 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
884 inset_x = cx(bv) - top_x + drawTextXOffset;
885 inset_y = cy(bv) + drawTextYOffset;
892 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
896 // use this to check mouse motion for selection!
900 int tmp_x = x - drawTextXOffset;
901 int tmp_y = y + insetAscent - getLyXText(bv)->first;
902 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
905 if (the_locking_inset) {
906 if (the_locking_inset == inset) {
907 the_locking_inset->insetButtonPress(bv,
911 no_selection = false;
914 // otherwise unlock the_locking_inset and lock the new inset
915 the_locking_inset->insetUnlock(bv);
916 inset_x = cx(bv) - top_x + drawTextXOffset;
917 inset_y = cy(bv) + drawTextYOffset;
918 the_locking_inset = 0;
919 inset->insetButtonPress(bv, x - inset_x,
920 y - inset_y, button);
921 inset->edit(bv, x - inset_x, y - inset_y, button);
922 if (the_locking_inset)
923 updateLocal(bv, CURSOR, false);
924 no_selection = false;
927 // otherwise only unlock the_locking_inset
928 the_locking_inset->insetUnlock(bv);
929 the_locking_inset = 0;
931 if (bv->theLockingInset()) {
932 if (isHighlyEditableInset(inset)) {
933 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
934 inset_x = cx(bv) - top_x + drawTextXOffset;
935 inset_y = cy(bv) + drawTextYOffset;
936 inset_pos = cpos(bv);
937 inset_par = cpar(bv);
938 inset_boundary = cboundary(bv);
939 the_locking_inset = uinset;
940 uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
942 uinset->edit(bv, x - inset_x, y - inset_y, 0);
943 if (the_locking_inset)
944 updateLocal(bv, CURSOR, false);
945 no_selection = false;
949 if (!inset) { // && (button == 2)) {
950 bool paste_internally = false;
951 if ((button == 2) && getLyXText(bv)->selection.set()) {
952 localDispatch(bv, LFUN_COPY, "");
953 paste_internally = true;
961 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
963 // set the selection cursor!
964 lt->selection.cursor = lt->cursor;
965 lt->cursor.x_fix(lt->cursor.x());
967 if (lt->selection.set()) {
968 lt->clearSelection();
971 updateLocal(bv, FULL, false);
973 lt->clearSelection();
976 updateLocal(bv, CURSOR, false);
978 bv->owner()->setLayout(cpar(bv)->getLayout());
980 // Insert primary selection with middle mouse
981 // if there is a local selection in the current buffer,
984 if (paste_internally)
985 localDispatch(bv, LFUN_PASTE, "");
987 localDispatch(bv, LFUN_PASTESELECTION,
991 getLyXText(bv)->clearSelection();
994 no_selection = false;
998 bool InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
1000 if (the_locking_inset) {
1001 return the_locking_inset->insetButtonRelease(bv,
1002 x - inset_x, y - inset_y,
1005 int tmp_x = x - drawTextXOffset;
1006 int tmp_y = y + insetAscent - getLyXText(bv)->first;
1007 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1010 if (isHighlyEditableInset(inset)) {
1011 ret = inset->insetButtonRelease(bv, x - inset_x,
1012 y - inset_y, button);
1014 inset_x = cx(bv) - top_x + drawTextXOffset;
1015 inset_y = cy(bv) + drawTextYOffset;
1016 ret = inset->insetButtonRelease(bv, x - inset_x,
1017 y - inset_y, button);
1018 inset->edit(bv, x - inset_x,
1019 y - inset_y, button);
1021 updateLocal(bv, CURSOR_PAR, false);
1027 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
1029 if (no_selection || ((mouse_x == x) && (mouse_y == y)))
1031 if (the_locking_inset) {
1032 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1038 lt = getLyXText(bv);
1041 hideInsetCursor(bv);
1042 LyXCursor cur = lt->cursor;
1043 lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1044 if (cur == lt->cursor) {
1049 lt->setSelection(bv);
1050 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1051 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1055 updateLocal(bv, SELECTION, false);
1057 showInsetCursor(bv);
1061 void InsetText::insetKeyPress(XKeyEvent * xke)
1063 if (the_locking_inset) {
1064 the_locking_inset->insetKeyPress(xke);
1070 UpdatableInset::RESULT
1071 InsetText::localDispatch(BufferView * bv,
1072 kb_action action, string const & arg)
1074 bool was_empty = par->size() == 0 && !par->next();
1075 no_selection = false;
1076 UpdatableInset::RESULT
1077 result= UpdatableInset::localDispatch(bv, action, arg);
1078 if (result != UNDISPATCHED) {
1082 result = DISPATCHED;
1083 if ((action < 0) && arg.empty())
1086 if (the_locking_inset) {
1087 result = the_locking_inset->localDispatch(bv, action, arg);
1088 if (result == DISPATCHED_NOUPDATE)
1090 else if (result == DISPATCHED) {
1091 updateLocal(bv, CURSOR_PAR, false);
1093 } else if (result >= FINISHED) {
1095 case FINISHED_RIGHT:
1096 moveRightIntern(bv, false, false);
1097 result = DISPATCHED;
1100 if ((result = moveUp(bv)) >= FINISHED) {
1101 updateLocal(bv, CURSOR, false);
1102 bv->unlockInset(this);
1106 if ((result = moveDown(bv)) >= FINISHED) {
1107 updateLocal(bv, CURSOR, false);
1108 bv->unlockInset(this);
1112 result = DISPATCHED;
1115 the_locking_inset = 0;
1116 #ifdef WITH_WARNINGS
1117 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
1122 hideInsetCursor(bv);
1125 lt = getLyXText(bv);
1129 int updflag = false;
1132 case LFUN_SELFINSERT:
1133 if (bv->buffer()->isReadonly()) {
1134 // setErrorMessage(N_("Document is read only"));
1138 /* Automatically delete the currently selected
1139 * text and replace it with what is being
1140 * typed in now. Depends on lyxrc settings
1141 * "auto_region_delete", which defaults to
1144 setUndo(bv, Undo::INSERT,
1145 lt->cursor.par(), lt->cursor.par()->next());
1147 if (lyxrc.auto_region_delete) {
1148 if (lt->selection.set()) {
1149 lt->cutSelection(bv, false);
1152 lt->clearSelection();
1153 for (string::size_type i = 0; i < arg.length(); ++i) {
1154 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1157 lt->selection.cursor = lt->cursor;
1158 updwhat = CURSOR_PAR;
1160 result = DISPATCHED_NOUPDATE;
1162 // --- Cursor Movements -----------------------------------
1165 moveRight(bv, false, true);
1166 lt->setSelection(bv);
1167 updwhat = SELECTION;
1170 result = moveRight(bv);
1176 moveLeft(bv, false, true);
1177 lt->setSelection(bv);
1178 updwhat = SELECTION;
1182 result = moveLeft(bv);
1188 lt->setSelection(bv);
1189 updwhat = SELECTION;
1193 result = moveDown(bv);
1199 lt->setSelection(bv);
1200 updwhat = SELECTION;
1204 result = moveUp(bv);
1216 case LFUN_BACKSPACE: {
1217 setUndo(bv, Undo::DELETE,
1218 lt->cursor.par(), lt->cursor.par()->next());
1219 if (lt->selection.set())
1220 lt->cutSelection(bv);
1223 updwhat = CURSOR_PAR;
1229 setUndo(bv, Undo::DELETE,
1230 lt->cursor.par(), lt->cursor.par()->next());
1231 if (lt->selection.set()) {
1232 lt->cutSelection(bv);
1236 updwhat = CURSOR_PAR;
1242 setUndo(bv, Undo::DELETE,
1243 lt->cursor.par(), lt->cursor.par()->next());
1244 lt->cutSelection(bv);
1245 updwhat = CURSOR_PAR;
1252 lt->copySelection(bv);
1253 updwhat = CURSOR_PAR;
1255 case LFUN_PASTESELECTION:
1257 string const clip(bv->getClipboard());
1261 if (arg == "paragraph") {
1262 lt->insertStringAsParagraphs(bv, clip);
1264 lt->insertStringAsLines(bv, clip);
1266 updwhat = CURSOR_PAR;
1271 if (!autoBreakRows) {
1273 if (CutAndPaste::nrOfParagraphs() > 1) {
1274 Alert::alert(_("Impossible operation"),
1275 _("Cannot include more than one paragraph!"),
1280 setUndo(bv, Undo::INSERT,
1281 lt->cursor.par(), lt->cursor.par()->next());
1282 lt->pasteSelection(bv);
1283 updwhat = CURSOR_PAR;
1288 case LFUN_BREAKPARAGRAPH:
1289 if (!autoBreakRows) {
1290 result = DISPATCHED;
1293 lt->breakParagraph(bv, 0);
1297 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1298 if (!autoBreakRows) {
1299 result = DISPATCHED;
1302 lt->breakParagraph(bv, 1);
1307 case LFUN_BREAKLINE: {
1308 if (!autoBreakRows) {
1309 result = DISPATCHED;
1312 setUndo(bv, Undo::INSERT,
1313 lt->cursor.par(), lt->cursor.par()->next());
1314 lt->insertChar(bv, Paragraph::META_NEWLINE);
1315 updwhat = CURSOR_PAR;
1321 // do not set layouts on non breakable textinsets
1322 if (autoBreakRows) {
1323 layout_type cur_layout = cpar(bv)->layout;
1325 // Derive layout number from given argument (string)
1326 // and current buffer's textclass (number). */
1327 textclass_type tclass = bv->buffer()->params.textclass;
1328 pair<bool, layout_type> layout =
1329 textclasslist.NumberOfLayout(tclass, arg);
1331 // If the entry is obsolete, use the new one instead.
1333 string obs = textclasslist.Style(tclass,layout.second).
1336 layout = textclasslist.NumberOfLayout(tclass, obs);
1339 // see if we found the layout number:
1340 if (!layout.first) {
1341 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1342 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1346 if (cur_layout != layout.second) {
1347 cur_layout = layout.second;
1348 lt->setLayout(bv, layout.second);
1349 bv->owner()->setLayout(cpar(bv)->getLayout());
1350 updwhat = CURSOR_PAR;
1354 // reset the layout box
1355 bv->owner()->setLayout(cpar(bv)->getLayout());
1358 case LFUN_PARAGRAPH_SPACING:
1359 // This one is absolutely not working. When fiddling with this
1360 // it also seems to me that the paragraphs inside the insettext
1361 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1363 Paragraph * par = lt->cursor.par();
1364 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1365 float cur_value = 1.0;
1366 if (cur_spacing == Spacing::Other) {
1367 cur_value = par->params().spacing().getValue();
1370 istringstream istr(arg.c_str());
1373 Spacing::Space new_spacing = cur_spacing;
1374 float new_value = cur_value;
1376 lyxerr << "Missing argument to `paragraph-spacing'"
1378 } else if (tmp == "single") {
1379 new_spacing = Spacing::Single;
1380 } else if (tmp == "onehalf") {
1381 new_spacing = Spacing::Onehalf;
1382 } else if (tmp == "double") {
1383 new_spacing = Spacing::Double;
1384 } else if (tmp == "other") {
1385 new_spacing = Spacing::Other;
1388 lyxerr << "new_value = " << tmpval << endl;
1391 } else if (tmp == "default") {
1392 new_spacing = Spacing::Default;
1394 lyxerr << _("Unknown spacing argument: ")
1397 if (cur_spacing != new_spacing || cur_value != new_value) {
1398 par->params().spacing(Spacing(new_spacing, new_value));
1399 updwhat = CURSOR_PAR;
1406 if (!bv->Dispatch(action, arg))
1407 result = UNDISPATCHED;
1414 updateLocal(bv, updwhat, updflag);
1415 /// If the action has deleted all text in the inset, we need to change the
1416 // language to the language of the surronding text.
1417 if (!was_empty && par->size() == 0 && !par->next()) {
1418 LyXFont font(LyXFont::ALL_IGNORE);
1419 font.setLanguage(bv->getParentLanguage(this));
1420 setFont(bv, font, false);
1423 if (result < FINISHED) {
1424 showInsetCursor(bv);
1426 bv->unlockInset(this);
1431 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1434 buf->latexParagraphs(os, par, 0, texrow);
1435 return texrow.rows();
1439 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1441 Paragraph * p = par;
1442 unsigned int lines = 0;
1445 string const tmp = buf->asciiParagraph(p, linelen, p->previous()==0);
1446 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1454 int InsetText::docbook(Buffer const * buf, ostream & os) const
1456 Paragraph * p = par;
1457 unsigned int lines = 0;
1459 vector<string> environment_stack(10);
1460 vector<string> environment_inner(10);
1462 int const command_depth = 0;
1465 Paragraph::depth_type depth = 0; // paragraph depth
1469 int desc_on = 0; // description mode
1471 LyXLayout const & style =
1472 textclasslist.Style(buf->params.textclass,
1475 // environment tag closing
1476 for (; depth > p->params().depth(); --depth) {
1477 if (environment_inner[depth] != "!-- --") {
1478 item_name = "listitem";
1479 buf->sgmlCloseTag(os, command_depth + depth,
1481 if (environment_inner[depth] == "varlistentry")
1482 buf->sgmlCloseTag(os, depth+command_depth,
1483 environment_inner[depth]);
1485 buf->sgmlCloseTag(os, depth + command_depth,
1486 environment_stack[depth]);
1487 environment_stack[depth].erase();
1488 environment_inner[depth].erase();
1491 if (depth == p->params().depth()
1492 && environment_stack[depth] != style.latexname()
1493 && !environment_stack[depth].empty()) {
1494 if (environment_inner[depth] != "!-- --") {
1495 item_name= "listitem";
1496 buf->sgmlCloseTag(os, command_depth+depth,
1498 if (environment_inner[depth] == "varlistentry")
1499 buf->sgmlCloseTag(os,
1500 depth + command_depth,
1501 environment_inner[depth]);
1504 buf->sgmlCloseTag(os, depth + command_depth,
1505 environment_stack[depth]);
1507 environment_stack[depth].erase();
1508 environment_inner[depth].erase();
1511 // Write opening SGML tags.
1512 switch (style.latextype) {
1513 case LATEX_PARAGRAPH:
1514 buf->sgmlOpenTag(os, depth + command_depth,
1519 buf->sgmlError(p, 0,
1520 _("Error : LatexType Command not allowed here.\n"));
1524 case LATEX_ENVIRONMENT:
1525 case LATEX_ITEM_ENVIRONMENT:
1526 if (depth < p->params().depth()) {
1527 depth = p->params().depth();
1528 environment_stack[depth].erase();
1531 if (environment_stack[depth] != style.latexname()) {
1532 if (environment_stack.size() == depth + 1) {
1533 environment_stack.push_back("!-- --");
1534 environment_inner.push_back("!-- --");
1536 environment_stack[depth] = style.latexname();
1537 environment_inner[depth] = "!-- --";
1538 buf->sgmlOpenTag(os, depth + command_depth,
1539 environment_stack[depth]);
1541 if (environment_inner[depth] != "!-- --") {
1542 item_name= "listitem";
1543 buf->sgmlCloseTag(os,
1544 command_depth + depth,
1546 if (environment_inner[depth] == "varlistentry")
1547 buf->sgmlCloseTag(os,
1548 depth + command_depth,
1549 environment_inner[depth]);
1553 if (style.latextype == LATEX_ENVIRONMENT) {
1554 if (!style.latexparam().empty()) {
1555 if (style.latexparam() == "CDATA")
1558 buf->sgmlOpenTag(os, depth + command_depth,
1559 style.latexparam());
1564 desc_on = (style.labeltype == LABEL_MANUAL);
1567 environment_inner[depth]= "varlistentry";
1569 environment_inner[depth]= "listitem";
1571 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1572 environment_inner[depth]);
1576 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1580 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1585 buf->sgmlOpenTag(os, depth + command_depth,
1590 buf->simpleDocBookOnePar(os, p, desc_on,
1591 depth + 1 + command_depth);
1595 // write closing SGML tags
1596 switch (style.latextype) {
1597 case LATEX_ENVIRONMENT:
1598 if (!style.latexparam().empty()) {
1599 if (style.latexparam() == "CDATA")
1602 buf->sgmlCloseTag(os, depth + command_depth,
1603 style.latexparam());
1606 case LATEX_ITEM_ENVIRONMENT:
1607 if (desc_on == 1) break;
1609 buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1611 case LATEX_PARAGRAPH:
1612 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1615 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1621 for (int d = depth; d >= 0; --d) {
1622 if (!environment_stack[depth].empty()) {
1623 if (environment_inner[depth] != "!-- --") {
1624 item_name = "listitem";
1625 buf->sgmlCloseTag(os, command_depth + depth,
1627 if (environment_inner[depth] == "varlistentry")
1628 buf->sgmlCloseTag(os, depth + command_depth,
1629 environment_inner[depth]);
1632 buf->sgmlCloseTag(os, depth + command_depth,
1633 environment_stack[depth]);
1641 void InsetText::validate(LaTeXFeatures & features) const
1643 Paragraph * p = par;
1645 p->validate(features);
1651 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1653 if (textclasslist.Style(buf->params.textclass,
1654 p->getLayout()).labeltype != LABEL_MANUAL)
1657 return p->beginningOfMainBody();
1661 void InsetText::getCursorPos(BufferView * bv,
1662 int & x, int & y) const
1664 if (the_locking_inset) {
1665 the_locking_inset->getCursorPos(bv, x, y);
1673 unsigned int InsetText::insetInInsetY()
1675 if (!the_locking_inset)
1678 return (inset_y + the_locking_inset->insetInInsetY());
1682 void InsetText::toggleInsetCursor(BufferView * bv)
1684 if (the_locking_inset) {
1685 the_locking_inset->toggleInsetCursor(bv);
1689 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1691 int const asc = lyxfont::maxAscent(font);
1692 int const desc = lyxfont::maxDescent(font);
1694 if (isCursorVisible())
1695 bv->hideLockedInsetCursor();
1697 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1698 toggleCursorVisible();
1702 void InsetText::showInsetCursor(BufferView * bv, bool show)
1704 if (the_locking_inset) {
1705 the_locking_inset->showInsetCursor(bv, show);
1708 if (!isCursorVisible()) {
1709 LyXFont const font =
1710 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1712 int const asc = lyxfont::maxAscent(font);
1713 int const desc = lyxfont::maxDescent(font);
1715 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1717 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1718 setCursorVisible(true);
1723 void InsetText::hideInsetCursor(BufferView * bv)
1725 if (isCursorVisible()) {
1726 bv->hideLockedInsetCursor();
1727 setCursorVisible(false);
1729 if (the_locking_inset)
1730 the_locking_inset->hideInsetCursor(bv);
1734 void InsetText::fitInsetCursor(BufferView * bv) const
1736 if (the_locking_inset) {
1737 the_locking_inset->fitInsetCursor(bv);
1740 LyXFont const font =
1741 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1743 int const asc = lyxfont::maxAscent(font);
1744 int const desc = lyxfont::maxDescent(font);
1746 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1750 UpdatableInset::RESULT
1751 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1753 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1754 return moveLeftIntern(bv, false, activate_inset, selecting);
1756 return moveRightIntern(bv, false, activate_inset, selecting);
1760 UpdatableInset::RESULT
1761 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1763 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1764 return moveRightIntern(bv, true, activate_inset, selecting);
1766 return moveLeftIntern(bv, true, activate_inset, selecting);
1770 UpdatableInset::RESULT
1771 InsetText::moveRightIntern(BufferView * bv, bool behind,
1772 bool activate_inset, bool selecting)
1774 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1775 return FINISHED_RIGHT;
1776 if (activate_inset && checkAndActivateInset(bv, behind))
1778 getLyXText(bv)->cursorRight(bv);
1780 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1781 return DISPATCHED_NOUPDATE;
1785 UpdatableInset::RESULT
1786 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1787 bool activate_inset, bool selecting)
1789 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1791 getLyXText(bv)->cursorLeft(bv);
1793 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1794 if (activate_inset && checkAndActivateInset(bv, behind))
1796 return DISPATCHED_NOUPDATE;
1800 UpdatableInset::RESULT
1801 InsetText::moveUp(BufferView * bv)
1803 if (!crow(bv)->previous())
1805 getLyXText(bv)->cursorUp(bv);
1806 return DISPATCHED_NOUPDATE;
1810 UpdatableInset::RESULT
1811 InsetText::moveDown(BufferView * bv)
1813 if (!crow(bv)->next())
1814 return FINISHED_DOWN;
1815 getLyXText(bv)->cursorDown(bv);
1816 return DISPATCHED_NOUPDATE;
1820 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1822 if (the_locking_inset) {
1823 if (the_locking_inset->insetAllowed(inset))
1824 return the_locking_inset->insertInset(bv, inset);
1829 lt = getLyXText(bv);
1832 setUndo(bv, Undo::FINISH, lt->cursor.par(), lt->cursor.par()->next());
1834 inset->setOwner(this);
1835 hideInsetCursor(bv);
1836 lt->insertInset(bv, inset);
1840 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1846 bool InsetText::insetAllowed(Inset::Code code) const
1848 // in_insetAllowed is a really gross hack,
1849 // to allow us to call the owner's insetAllowed
1850 // without stack overflow, which can happen
1851 // when the owner uses InsetCollapsable::insetAllowed()
1853 if (in_insetAllowed)
1855 in_insetAllowed = true;
1856 if (the_locking_inset)
1857 ret = the_locking_inset->insetAllowed(code);
1859 ret = owner()->insetAllowed(code);
1860 in_insetAllowed = false;
1865 UpdatableInset * InsetText::getLockingInset() const
1867 return the_locking_inset ? the_locking_inset->getLockingInset() :
1868 const_cast<InsetText *>(this);
1872 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1876 if (the_locking_inset)
1877 return the_locking_inset->getFirstLockingInsetOfType(c);
1882 bool InsetText::showInsetDialog(BufferView * bv) const
1884 if (the_locking_inset)
1885 return the_locking_inset->showInsetDialog(bv);
1890 vector<string> const InsetText::getLabelList() const
1892 vector<string> label_list;
1894 Paragraph * tpar = par;
1896 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1897 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1898 for (; beg != end; ++beg) {
1899 vector<string> const l = (*beg)->getLabelList();
1900 label_list.insert(label_list.end(), l.begin(), l.end());
1902 tpar = tpar->next();
1908 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1911 if (the_locking_inset) {
1912 the_locking_inset->setFont(bv, font, toggleall, selectall);
1915 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
1916 getLyXText(bv)->setFont(bv, font, toggleall);
1921 lt = getLyXText(bv);
1924 if (lt->selection.set()) {
1925 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1929 lt->toggleFree(bv, font, toggleall);
1931 lt->clearSelection();
1933 bool flag = (selectall || lt->selection.set());
1937 updateLocal(bv, FULL, true);
1939 updateLocal(bv, CURSOR_PAR, true);
1943 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1945 if (cpar(bv)->isInset(cpos(bv))) {
1949 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1950 if (!isHighlyEditableInset(inset))
1952 LyXFont const font =
1953 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1955 x = inset->width(bv, font);
1956 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1959 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1961 //inset_x = cx(bv) - top_x + drawTextXOffset;
1962 //inset_y = cy(bv) + drawTextYOffset;
1963 inset->edit(bv, x, y, 0);
1964 if (!the_locking_inset)
1966 updateLocal(bv, CURSOR, false);
1973 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1976 x -= drawTextXOffset;
1978 int dummyy = y + insetAscent;
1979 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
1986 inset_x = cx(bv) - top_x + drawTextXOffset;
1987 inset_y = cy(bv) + drawTextYOffset;
1988 inset->edit(bv, x - inset_x, y - inset_y, button);
1989 if (!the_locking_inset)
1991 updateLocal(bv, CURSOR, false);
1998 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
2001 int w = UpdatableInset::getMaxWidth(bv, inset);
2006 w = w - top_x + owner()->x();
2009 w -= (2 * TEXT_TO_INSET_OFFSET);
2012 return UpdatableInset::getMaxWidth(bv, inset);
2017 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2019 // we have to unlock any locked inset otherwise we're in troubles
2020 the_locking_inset = 0;
2022 Paragraph * tmp = par->next();
2027 par = new Paragraph(*p, same_id);
2028 par->setInsetOwner(this);
2029 Paragraph * np = par;
2032 np->next(new Paragraph(*p, same_id));
2033 np->next()->previous(np);
2035 np->setInsetOwner(this);
2042 void InsetText::setText(string const & data)
2045 LyXFont font(LyXFont::ALL_SANE);
2046 for (unsigned int i=0; i < data.length(); ++i)
2047 par->insertChar(i, data[i], font);
2051 void InsetText::setAutoBreakRows(bool flag)
2053 if (flag != autoBreakRows) {
2054 autoBreakRows = flag;
2062 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2064 if (how != drawFrame_) {
2067 updateLocal(bv, DRAW_FRAME, false);
2072 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2074 if (frame_color != col) {
2077 updateLocal(bv, DRAW_FRAME, false);
2082 int InsetText::cx(BufferView * bv) const
2084 // we do nothing dangerous so we use a local cache
2085 LyXText * llt = getLyXText(bv);
2086 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2087 if (the_locking_inset) {
2088 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2090 if (font.isVisibleRightToLeft())
2091 x -= the_locking_inset->width(bv, font);
2097 int InsetText::cy(BufferView * bv) const
2100 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2104 pos_type InsetText::cpos(BufferView * bv) const
2106 return getLyXText(bv)->cursor.pos();
2110 Paragraph * InsetText::cpar(BufferView * bv) const
2112 return getLyXText(bv)->cursor.par();
2116 bool InsetText::cboundary(BufferView * bv) const
2118 return getLyXText(bv)->cursor.boundary();
2122 Row * InsetText::crow(BufferView * bv) const
2124 return getLyXText(bv)->cursor.row();
2128 LyXText * InsetText::getLyXText(BufferView const * lbv,
2129 bool const recursive) const
2131 if (!recursive && (cached_bview == lbv)) {
2132 LyXText * lt = cached_text.get();
2133 lyx::Assert(lt && lt->firstRow()->par() == par);
2137 // Super UGLY! (Lgb)
2138 BufferView * bv = const_cast<BufferView *>(lbv);
2141 Cache::iterator it = cache.find(bv);
2143 if (it != cache.end()) {
2146 } else if (do_resize) {
2147 resizeLyXText(do_resize);
2149 if (lt || !it->second.remove) {
2150 lyx::Assert(it->second.text.get());
2151 cached_text = it->second.text;
2152 if (recursive && the_locking_inset) {
2153 return the_locking_inset->getLyXText(bv, true);
2155 return cached_text.get();
2156 } else if (it->second.remove) {
2158 saveLyXTextState(it->second.text.get());
2164 // when we have to reinit the existing LyXText!
2166 it->second.text->init(bv);
2167 restoreLyXTextState(bv, it->second.text.get());
2168 it->second.remove = false;
2170 cached_text = it->second.text;
2171 if (the_locking_inset && recursive) {
2172 return the_locking_inset->getLyXText(bv);
2174 return cached_text.get();
2177 // we are here only if we don't have a BufferView * in the cache!!!
2179 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2180 cached_text->init(bv);
2181 restoreLyXTextState(bv, cached_text.get());
2183 cache.insert(make_pair(bv, cached_text));
2185 if (the_locking_inset && recursive) {
2186 return the_locking_inset->getLyXText(bv);
2188 return cached_text.get();
2192 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2196 Cache::iterator it = cache.find(bv);
2198 if (it == cache.end()) {
2202 lyx::Assert(it->second.text.get());
2204 it->second.remove = true;
2206 /// then remove all LyXText in text-insets
2207 Paragraph * p = par;
2208 for (; p; p = p->next()) {
2209 p->deleteInsetsLyXText(bv);
2215 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2218 // we cannot resize this because we are in use!
2219 // so do this on the next possible getLyXText()
2224 // lyxerr << "InsetText::resizeLyXText\n";
2225 if (!par->next() && !par->size()) { // no data, resize not neccessary!
2226 // we have to do this as a fixed width may have changed!
2227 LyXText * t = getLyXText(bv);
2228 saveLyXTextState(t);
2230 restoreLyXTextState(bv, t);
2233 // one endless line, resize normally not necessary
2234 if (!force && getMaxWidth(bv, this) < 0)
2237 Cache::iterator it = cache.find(bv);
2238 if (it == cache.end()) {
2241 lyx::Assert(it->second.text.get());
2243 LyXText * t = it->second.text.get();
2244 saveLyXTextState(t);
2245 for (Paragraph * p = par; p; p = p->next()) {
2246 p->resizeInsetsLyXText(bv);
2249 restoreLyXTextState(bv, t);
2250 if (the_locking_inset) {
2251 inset_x = cx(bv) - top_x + drawTextXOffset;
2252 inset_y = cy(bv) + drawTextYOffset;
2256 t->first = bv->screen()->topCursorVisible(t);
2259 updateLocal(bv, FULL, false);
2260 // this will scroll the screen such that the cursor becomes visible
2261 bv->updateScrollbar();
2263 need_update |= FULL;
2268 void InsetText::reinitLyXText() const
2271 // we cannot resize this because we are in use!
2272 // so do this on the next possible getLyXText()
2278 // lyxerr << "InsetText::reinitLyXText\n";
2279 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2280 lyx::Assert(it->second.text.get());
2282 LyXText * t = it->second.text.get();
2283 BufferView * bv = it->first;
2285 saveLyXTextState(t);
2286 for (Paragraph * p = par; p; p = p->next()) {
2287 p->resizeInsetsLyXText(bv);
2290 restoreLyXTextState(bv, t);
2291 if (the_locking_inset) {
2292 inset_x = cx(bv) - top_x + drawTextXOffset;
2293 inset_y = cy(bv) + drawTextYOffset;
2296 t->first = bv->screen()->topCursorVisible(t);
2299 updateLocal(bv, FULL, false);
2300 // this will scroll the screen such that the cursor becomes visible
2301 bv->updateScrollbar();
2309 void InsetText::removeNewlines()
2311 bool changed = false;
2313 for (Paragraph * p = par; p; p = p->next()) {
2314 for (int i = 0; i < p->size(); ++i) {
2315 if (p->getChar(i) == Paragraph::META_NEWLINE) {
2326 bool InsetText::nodraw() const
2328 if (the_locking_inset)
2329 return the_locking_inset->nodraw();
2330 return UpdatableInset::nodraw();
2334 int InsetText::scroll(bool recursive) const
2336 int sx = UpdatableInset::scroll(false);
2338 if (recursive && the_locking_inset)
2339 sx += the_locking_inset->scroll(recursive);
2345 bool InsetText::doClearArea() const
2347 return !locked || (need_update & (FULL|INIT));
2351 void InsetText::selectAll(BufferView * bv)
2353 getLyXText(bv)->cursorTop(bv);
2354 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2355 getLyXText(bv)->cursorBottom(bv);
2356 getLyXText(bv)->setSelection(bv);
2360 void InsetText::clearSelection(BufferView * bv)
2362 getLyXText(bv)->clearSelection();
2366 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2368 Painter & pain = bv->painter();
2370 int h = insetAscent + insetDescent;
2371 int ty = baseline - insetAscent;
2377 if ((ty + h) > pain.paperHeight())
2378 h = pain.paperHeight();
2379 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2380 w = pain.paperWidth();
2381 // w -= TEXT_TO_INSET_OFFSET;
2382 pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2385 frame_is_visible = false;
2389 Paragraph * InsetText::getParFromID(int id) const
2392 Paragraph * result = par;
2393 Paragraph * ires = 0;
2394 while (result && result->id() != id) {
2395 if ((ires = result->getParFromID(id)))
2397 result = result->next();
2401 Paragraph * tmp = par;
2403 if (tmp->id() == id) {
2406 Paragraph * tmp2 = tmp->getParFromID(id);
2417 Paragraph * InsetText::firstParagraph() const
2420 if (the_locking_inset)
2421 if ((result = the_locking_inset->firstParagraph()))
2427 Paragraph * InsetText::getFirstParagraph(int i) const
2429 return (i == 0) ? par : 0;
2433 LyXCursor const & InsetText::cursor(BufferView * bv) const
2435 if (the_locking_inset)
2436 return the_locking_inset->cursor(bv);
2437 return getLyXText(bv)->cursor;
2441 Paragraph * InsetText::paragraph() const
2447 void InsetText::paragraph(Paragraph * p)
2449 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2450 // caller of this function has to take care of it. This IS important
2451 // as we could have to insert a paragraph before this one and just
2452 // link the actual to a new ones next and set it with this function
2455 // set ourself as owner for all the paragraphs inserted!
2456 Paragraph * np = par;
2458 np->setInsetOwner(this);
2462 // redraw myself when asked for
2467 Inset * InsetText::getInsetFromID(int id_arg) const
2470 return const_cast<InsetText *>(this);
2472 Paragraph * lp = par;
2475 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2476 en = lp->inset_iterator_end();
2479 if ((*it)->id() == id_arg)
2481 Inset * in = (*it)->getInsetFromID(id_arg);
2491 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2497 lt = getLyXText(bv);
2500 if (the_locking_inset) {
2501 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2508 // we have to go on checking so move cusor to the next char
2509 lt->cursor.pos(lt->cursor.pos() + 1);
2511 str = lt->selectNextWordToSpellcheck(bv, value);
2513 bv->unlockInset(const_cast<InsetText *>(this));
2522 void InsetText::selectSelectedWord(BufferView * bv)
2524 if (the_locking_inset) {
2525 the_locking_inset->selectSelectedWord(bv);
2528 getLyXText(bv)->selectSelectedWord(bv);
2529 updateLocal(bv, SELECTION, false);
2533 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2535 if (the_locking_inset) {
2536 the_locking_inset->toggleSelection(bv, kill_selection);
2540 lt = getLyXText(bv);
2544 int x = top_x + TEXT_TO_INSET_OFFSET;
2546 Row * row = lt->firstRow();
2547 int y_offset = top_baseline - row->ascent_of_text();
2549 while ((row != 0) && ((y+row->height()) <= 0)) {
2556 if (need_update & SELECTION)
2558 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2564 bool InsetText::searchForward(BufferView * bv, string const & str,
2567 if (the_locking_inset) {
2568 if (the_locking_inset->searchForward(bv, str, cs, mw))
2572 lt = getLyXText(bv);
2575 Paragraph * lpar = lt->cursor.par();
2576 pos_type pos = lt->cursor.pos();
2577 if (pos < lpar->size() - 1)
2581 lpar = lpar->next();
2586 // we have to unlock ourself in this function by default!
2587 bv->unlockInset(const_cast<InsetText *>(this));
2590 lt->setCursor(bv, lpar, pos);
2594 if (LyXFind(bv, str, true, true, cs , mw)) {
2597 // we have to unlock ourself in this function by default!
2598 bv->unlockInset(const_cast<InsetText *>(this));
2602 bool InsetText::searchBackward(BufferView * bv, string const & str,
2605 if (the_locking_inset)
2606 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2608 if (LyXFind(bv, str, false, true, cs, mw)) {
2611 // we have to unlock ourself in this function by default!
2612 bv->unlockInset(const_cast<InsetText *>(this));
2617 bool InsetText::checkInsertChar(LyXFont & font)
2620 return owner()->checkInsertChar(font);
2625 void InsetText::collapseParagraphs(BufferParams const & bparams) const
2627 while(par->next()) {
2628 if (!par->isSeparator(par->size()-1))
2629 par->insertChar(par->size()-1, ' ');
2630 par->pasteParagraph(bparams);
2636 void InsetText::getDrawFont(LyXFont & font) const
2640 owner()->getDrawFont(font);