1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998-2001 The LyX Team.
8 * ======================================================
14 #pragma implementation
17 #include "insettext.h"
18 #include "paragraph.h"
22 #include "commandtags.h"
25 #include "BufferView.h"
26 #include "lyxtextclasslist.h"
27 #include "LaTeXFeatures.h"
30 #include "lyxcursor.h"
31 #include "CutAndPaste.h"
37 #include "trans_mgr.h"
38 #include "lyxscreen.h"
42 #include "ParagraphParameters.h"
43 #include "undo_funcs.h"
46 #include "frontends/Alert.h"
48 #include "support/textutils.h"
49 #include "support/LAssert.h"
50 #include "support/lstrings.h"
51 #include "support/lyxalgo.h" // lyx::count
68 using lyx::textclass_type;
70 extern unsigned char getCurrentTextClass(Buffer *);
71 extern bool math_insert_greek(BufferView *, char);
72 extern int greek_kb_flag;
75 // These functions should probably go into bufferview_funcs somehow (Jug)
77 void InsetText::saveLyXTextState(LyXText * t) const
79 // check if my paragraphs are still valid
82 if (p == t->cursor.par())
87 if (p && t->cursor.pos() <= p->size()) {
88 sstate.lpar = t->cursor.par();
89 sstate.pos = t->cursor.pos();
90 sstate.boundary = t->cursor.boundary();
91 sstate.selstartpar = t->selection.start.par();
92 sstate.selstartpos = t->selection.start.pos();
93 sstate.selstartboundary = t->selection.start.boundary();
94 sstate.selendpar = t->selection.end.par();
95 sstate.selendpos = t->selection.end.pos();
96 sstate.selendboundary = t->selection.end.boundary();
97 sstate.selection = t->selection.set();
98 sstate.mark_set = t->selection.mark();
99 sstate.refresh = t->refresh_row != 0;
105 void InsetText::restoreLyXTextState(BufferView * bv, LyXText * t) const
108 t->selection.set(true);
109 /* at this point just to avoid the Delete-Empty-Paragraph
110 * Mechanism when setting the cursor */
111 t->selection.mark(sstate.mark_set);
112 if (sstate.selection) {
113 t->setCursor(bv, sstate.selstartpar, sstate.selstartpos,
114 true, sstate.selstartboundary);
115 t->selection.cursor = t->cursor;
116 t->setCursor(bv, sstate.selendpar, sstate.selendpos,
117 true, sstate.selendboundary);
119 t->setCursor(bv, sstate.lpar, sstate.pos);
121 t->setCursor(bv, sstate.lpar, sstate.pos, true, sstate.boundary);
122 t->selection.cursor = t->cursor;
123 t->selection.set(false);
125 if (sstate.refresh) {
131 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
138 InsetText::InsetText()
139 : UpdatableInset(), lt(0), in_update(false), do_resize(0),
147 InsetText::InsetText(InsetText const & in, bool same_id)
148 : UpdatableInset(in, same_id), lt(0), in_update(false), do_resize(0),
156 InsetText & InsetText::operator=(InsetText const & it)
163 void InsetText::init(InsetText const * ins, bool same_id)
166 setParagraphData(ins->par, same_id);
167 autoBreakRows = ins->autoBreakRows;
168 drawFrame_ = ins->drawFrame_;
169 frame_color = ins->frame_color;
175 p->setInsetOwner(this);
178 the_locking_inset = 0;
180 frame_color = LColor::insetframe;
181 autoBreakRows = false;
188 no_selection = false;
195 last_drawn_width = -1;
196 frame_is_visible = false;
199 in_insetAllowed = false;
203 InsetText::~InsetText()
210 Paragraph * tmp = par->next();
217 void InsetText::clear()
220 Paragraph * tmp = par->next();
230 Inset * InsetText::clone(Buffer const &, bool same_id) const
232 return new InsetText(*this, same_id);
236 void InsetText::write(Buffer const * buf, ostream & os) const
239 writeParagraphData(buf, os);
243 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
245 par->writeFile(buf, os, buf->params, 0);
249 void InsetText::read(Buffer const * buf, LyXLex & lex)
253 Paragraph * return_par = 0;
254 Paragraph::depth_type depth = 0;
255 LyXFont font(LyXFont::ALL_INHERIT);
261 token = lex.getString();
264 if (token == "\\end_inset") {
265 #ifndef NO_COMPABILITY
266 const_cast<Buffer*>(buf)->insertErtContents(par, pos, false);
271 if (const_cast<Buffer*>(buf)->
272 parseSingleLyXformat2Token(lex, par, return_par,
273 token, pos, depth, font)) {
274 // the_end read this should NEVER happen
275 lex.printError("\\the_end read in inset! Error in document!");
283 return_par->setInsetOwner(this);
284 return_par = return_par->next();
287 if (token != "\\end_inset") {
288 lex.printError("Missing \\end_inset at this point. "
295 int InsetText::ascent(BufferView * bv, LyXFont const &) const
297 insetAscent = getLyXText(bv)->firstRow()->ascent_of_text() +
298 TEXT_TO_INSET_OFFSET;
303 int InsetText::descent(BufferView * bv, LyXFont const &) const
305 LyXText * llt = getLyXText(bv);
306 insetDescent = llt->height - llt->firstRow()->ascent_of_text() +
307 TEXT_TO_INSET_OFFSET;
312 int InsetText::width(BufferView * bv, LyXFont const &) const
314 insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
315 (2 * TEXT_TO_INSET_OFFSET);
316 insetWidth = max(insetWidth, 10);
321 int InsetText::textWidth(BufferView * bv, bool fordraw) const
324 if (!autoBreakRows) {
327 w = getMaxWidth(bv, this);
330 return max(w - (2 * TEXT_TO_INSET_OFFSET),
331 (int)getLyXText(bv)->width);
335 return w - (2 * TEXT_TO_INSET_OFFSET);
339 void InsetText::draw(BufferView * bv, LyXFont const & f,
340 int baseline, float & x, bool cleared) const
345 Painter & pain = bv->painter();
347 // this is the first thing we have to ask because if the x pos
348 // changed we have to do a complete rebreak of the text as we
349 // may have few space to draw in. Well we should check on this too
351 if (top_x != int(x)) {
354 int nw = getMaxWidth(bv, this);
355 if (nw > 0 && old_max_width != nw) {
358 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
363 // call these methods so that insetWidth, insetAscent and
364 // insetDescent have the right values.
369 // repaint the background if needed
370 if (cleared && backgroundColor() != LColor::background) {
371 clearInset(bv, baseline, cleared);
374 // no draw is necessary !!!
375 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
376 top_baseline = baseline;
378 if (need_update & CLEAR_FRAME)
379 clearFrame(pain, cleared);
386 x += static_cast<float>(scroll());
388 // if top_x differs we did it already
389 if (!cleared && (old_x == int(x))
390 && ((need_update&(INIT|FULL)) || (top_baseline != baseline)
391 ||(last_drawn_width != insetWidth)))
393 // Condition necessary to eliminate bug 59 attachment 37
395 clearInset(bv, baseline, cleared);
399 frame_is_visible = false;
401 if (!cleared && (need_update == NONE)) {
403 drawFrame(pain, cleared);
407 top_baseline = baseline;
408 top_y = baseline - insetAscent;
410 if (last_drawn_width != insetWidth) {
412 clearInset(bv, baseline, cleared);
414 last_drawn_width = insetWidth;
417 if (the_locking_inset && (cpar(bv) == inset_par)
418 && (cpos(bv) == inset_pos)) {
419 inset_x = cx(bv) - top_x + drawTextXOffset;
420 inset_y = cy(bv) + drawTextYOffset;
422 if (!cleared && (need_update == CURSOR)
423 && !getLyXText(bv)->selection.set()) {
424 drawFrame(pain, cleared);
434 x += TEXT_TO_INSET_OFFSET;
436 Row * row = lt->firstRow();
437 int y_offset = baseline - row->ascent_of_text();
438 int ph = pain.paperHeight();
441 while ((row != 0) && ((y+row->height()) <= 0)) {
443 first += row->height();
449 if (cleared || (need_update&(INIT|FULL))) {
452 while ((row != 0) && (yf < ph)) {
453 lt->getVisibleRow(bv, y+y_offset, int(x), row,
459 } else if (!locked) {
460 if (need_update & CURSOR) {
461 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
462 lt->clearSelection();
463 lt->selection.cursor = lt->cursor;
465 bv->screen()->update(lt, bv, y_offset, int(x));
468 if (need_update & SELECTION) {
469 bv->screen()->toggleToggle(lt, bv, y_offset, int(x));
470 } else if (need_update & CURSOR) {
471 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
472 lt->clearSelection();
473 lt->selection.cursor = lt->cursor;
475 bv->screen()->update(lt, bv, y_offset, int(x));
480 lt->status(bv, LyXText::UNCHANGED);
481 if ((need_update != CURSOR_PAR) &&
482 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked))) {
483 drawFrame(pain, cleared);
484 } else if (need_update & CLEAR_FRAME) {
485 clearFrame(pain, cleared);
488 x += insetWidth - TEXT_TO_INSET_OFFSET;
490 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
492 } else if (need_update != INIT) {
500 void InsetText::drawFrame(Painter & pain, bool cleared) const
502 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
503 if (!frame_is_visible || cleared) {
504 frame_x = top_x + ttoD2;
505 frame_y = top_baseline - insetAscent + ttoD2;
506 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
507 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
508 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
510 frame_is_visible = true;
515 void InsetText::clearFrame(Painter & pain, bool cleared) const
517 if (frame_is_visible) {
519 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
522 frame_is_visible = false;
527 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
530 if (reinit && owner()) {
532 owner()->update(bv, font, true);
537 if (reinit || need_update == INIT) {
539 // we should put this call where we set need_update to INIT!
542 owner()->update(bv, font, true);
546 if (the_locking_inset) {
547 inset_x = cx(bv) - top_x + drawTextXOffset;
548 inset_y = cy(bv) + drawTextYOffset;
549 the_locking_inset->update(bv, font, reinit);
557 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
560 lt->updateInset(bv, the_locking_inset);
562 if (lt->status() == LyXText::NEED_MORE_REFRESH)
570 void InsetText::setUpdateStatus(BufferView * bv, int what) const
572 // this does nothing dangerous so use only a localized buffer
573 LyXText * llt = getLyXText(bv);
576 // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
577 // if we don't break row so that we only have one row to update!
578 if ((llt->status() == LyXText::NEED_MORE_REFRESH) ||
580 (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
583 } else if (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
584 need_update |= CURSOR_PAR;
587 // this to not draw a selection when we redraw all of it!
588 if (need_update & CURSOR && !(need_update & SELECTION)) {
589 if (llt->selection.set())
591 llt->clearSelection();
596 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
598 if (!autoBreakRows && par->next())
599 collapseParagraphs(bv->buffer()->params);
606 setUpdateStatus(bv, what);
607 bool flag = (((need_update != CURSOR) && (need_update != NONE)) ||
608 (lt->status() != LyXText::UNCHANGED) || lt->selection.set());
612 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
616 if (need_update == CURSOR)
618 bv->owner()->showState();
619 bv->owner()->updateMenubar();
620 bv->owner()->updateToolbar();
621 if (old_par != cpar(bv)) {
622 bv->owner()->setLayout(cpar(bv)->layout());
628 string const InsetText::editMessage() const
630 return _("Opened Text Inset");
634 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
636 UpdatableInset::edit(bv, x, y, button);
638 if (!bv->lockInset(this)) {
639 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
643 the_locking_inset = 0;
644 inset_pos = inset_x = inset_y = 0;
645 inset_boundary = false;
648 int tmp_y = (y < 0) ? 0 : y;
654 if (!checkAndActivateInset(bv, x, tmp_y, button))
655 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
657 lt->clearSelection();
659 // If the inset is empty set the language of the current font to the
660 // language to the surronding text (if different).
661 if (par->size() == 0 && !par->next() &&
662 bv->getParentLanguage(this) != lt->current_font.language())
664 LyXFont font(LyXFont::ALL_IGNORE);
665 font.setLanguage(bv->getParentLanguage(this));
666 setFont(bv, font, false);
673 if (drawFrame_ == LOCKED)
674 code = CURSOR|DRAW_FRAME;
675 updateLocal(bv, code, false);
679 void InsetText::edit(BufferView * bv, bool front)
681 UpdatableInset::edit(bv, front);
683 if (!bv->lockInset(this)) {
684 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
688 the_locking_inset = 0;
689 inset_pos = inset_x = inset_y = 0;
690 inset_boundary = false;
699 lt->setCursor(bv, par, 0);
704 // int const pos = (p->size() ? p->size()-1 : p->size());
705 lt->setCursor(bv, p, p->size());
707 lt->clearSelection();
709 // If the inset is empty set the language of the current font to the
710 // language to the surronding text (if different).
711 if (par->size() == 0 && !par->next() &&
712 bv->getParentLanguage(this) != lt->current_font.language()) {
713 LyXFont font(LyXFont::ALL_IGNORE);
714 font.setLanguage(bv->getParentLanguage(this));
715 setFont(bv, font, false);
721 if (drawFrame_ == LOCKED)
722 code = CURSOR|DRAW_FRAME;
723 updateLocal(bv, code, false);
727 void InsetText::insetUnlock(BufferView * bv)
729 if (the_locking_inset) {
730 the_locking_inset->insetUnlock(bv);
731 the_locking_inset = 0;
734 no_selection = false;
737 if (drawFrame_ == LOCKED)
738 code = CURSOR|CLEAR_FRAME;
746 if (lt->selection.set()) {
747 lt->clearSelection();
749 } else if (owner()) {
750 bv->owner()->setLayout(owner()->getLyXText(bv)
751 ->cursor.par()->layout());
753 bv->owner()->setLayout(bv->text->cursor.par()->layout());
754 // hack for deleteEmptyParMech
756 lt->setCursor(bv, par, 0);
757 } else if (par->next()) {
758 lt->setCursor(bv, par->next(), 0);
762 updateLocal(bv, code, false);
765 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
767 the_locking_inset = inset;
768 inset_x = cx(bv) - top_x + drawTextXOffset;
769 inset_y = cy(bv) + drawTextYOffset;
770 inset_pos = cpos(bv);
771 inset_par = cpar(bv);
772 inset_boundary = cboundary(bv);
773 updateLocal(bv, CURSOR, false);
777 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
779 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
783 if (!the_locking_inset) {
785 int const id = inset->id();
787 Paragraph::inset_iterator it =
788 p->inset_iterator_begin();
789 Paragraph::inset_iterator const end =
790 p->inset_iterator_end();
791 for (; it != end; ++it) {
792 if ((*it) == inset) {
793 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
794 lockInset(bv, inset);
797 if ((*it)->getInsetFromID(id)) {
798 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
800 return the_locking_inset->lockInsetInInset(bv, inset);
807 if (inset == cpar(bv)->getInset(cpos(bv))) {
808 lyxerr[Debug::INSETS] << "OK" << endl;
809 lockInset(bv, inset);
811 } else if (the_locking_inset && (the_locking_inset == inset)) {
812 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
813 lyxerr[Debug::INSETS] << "OK" << endl;
814 inset_x = cx(bv) - top_x + drawTextXOffset;
815 inset_y = cy(bv) + drawTextYOffset;
817 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
819 } else if (the_locking_inset) {
820 lyxerr[Debug::INSETS] << "MAYBE" << endl;
821 return the_locking_inset->lockInsetInInset(bv, inset);
823 lyxerr[Debug::INSETS] << "NOT OK" << endl;
828 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
831 if (!the_locking_inset)
833 if (the_locking_inset == inset) {
834 the_locking_inset->insetUnlock(bv);
835 getLyXText(bv)->updateInset(bv, inset);
836 the_locking_inset = 0;
838 moveRight(bv, false);
839 old_par = 0; // force layout setting
843 updateLocal(bv, CURSOR, false);
846 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
850 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
852 if (!autoBreakRows && par->next())
853 collapseParagraphs(bv->buffer()->params);
861 if (!the_locking_inset) {
862 bool found = lt->updateInset(bv, inset);
866 setUpdateStatus(bv, NONE);
869 if (the_locking_inset != inset) {
870 bool found = the_locking_inset->updateInsetInInset(bv, inset);
874 setUpdateStatus(bv, CURSOR_PAR);
877 bool found = lt->updateInset(bv, inset);
881 setUpdateStatus(bv, CURSOR_PAR);
882 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
883 inset_x = cx(bv) - top_x + drawTextXOffset;
884 inset_y = cy(bv) + drawTextYOffset;
891 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
895 // use this to check mouse motion for selection!
899 int tmp_x = x - drawTextXOffset;
900 int tmp_y = y + insetAscent - getLyXText(bv)->first;
901 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
904 if (the_locking_inset) {
905 if (the_locking_inset == inset) {
906 the_locking_inset->insetButtonPress(bv,
910 no_selection = false;
913 // otherwise unlock the_locking_inset and lock the new inset
914 the_locking_inset->insetUnlock(bv);
915 inset_x = cx(bv) - top_x + drawTextXOffset;
916 inset_y = cy(bv) + drawTextYOffset;
917 the_locking_inset = 0;
918 inset->insetButtonPress(bv, x - inset_x,
919 y - inset_y, button);
920 inset->edit(bv, x - inset_x, y - inset_y, button);
921 if (the_locking_inset)
922 updateLocal(bv, CURSOR, false);
923 no_selection = false;
926 // otherwise only unlock the_locking_inset
927 the_locking_inset->insetUnlock(bv);
928 the_locking_inset = 0;
930 if (bv->theLockingInset()) {
931 if (isHighlyEditableInset(inset)) {
932 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
933 inset_x = cx(bv) - top_x + drawTextXOffset;
934 inset_y = cy(bv) + drawTextYOffset;
935 inset_pos = cpos(bv);
936 inset_par = cpar(bv);
937 inset_boundary = cboundary(bv);
938 the_locking_inset = uinset;
939 uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
941 uinset->edit(bv, x - inset_x, y - inset_y, 0);
942 if (the_locking_inset)
943 updateLocal(bv, CURSOR, false);
944 no_selection = false;
948 if (!inset) { // && (button == 2)) {
949 bool paste_internally = false;
950 if ((button == 2) && getLyXText(bv)->selection.set()) {
951 localDispatch(bv, LFUN_COPY, "");
952 paste_internally = true;
960 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
962 // set the selection cursor!
963 lt->selection.cursor = lt->cursor;
964 lt->cursor.x_fix(lt->cursor.x());
966 if (lt->selection.set()) {
967 lt->clearSelection();
970 updateLocal(bv, FULL, false);
972 lt->clearSelection();
975 updateLocal(bv, CURSOR, false);
977 bv->owner()->setLayout(cpar(bv)->layout());
979 // Insert primary selection with middle mouse
980 // if there is a local selection in the current buffer,
983 if (paste_internally)
984 localDispatch(bv, LFUN_PASTE, "");
986 localDispatch(bv, LFUN_PASTESELECTION,
990 getLyXText(bv)->clearSelection();
993 no_selection = false;
997 bool InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
999 if (the_locking_inset) {
1000 return the_locking_inset->insetButtonRelease(bv,
1001 x - inset_x, y - inset_y,
1004 int tmp_x = x - drawTextXOffset;
1005 int tmp_y = y + insetAscent - getLyXText(bv)->first;
1006 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1009 if (isHighlyEditableInset(inset)) {
1010 ret = inset->insetButtonRelease(bv, x - inset_x,
1011 y - inset_y, button);
1013 inset_x = cx(bv) - top_x + drawTextXOffset;
1014 inset_y = cy(bv) + drawTextYOffset;
1015 ret = inset->insetButtonRelease(bv, x - inset_x,
1016 y - inset_y, button);
1017 inset->edit(bv, x - inset_x,
1018 y - inset_y, button);
1020 updateLocal(bv, CURSOR_PAR, false);
1026 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
1028 if (no_selection || ((mouse_x == x) && (mouse_y == y)))
1030 if (the_locking_inset) {
1031 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1037 lt = getLyXText(bv);
1040 hideInsetCursor(bv);
1041 LyXCursor cur = lt->cursor;
1042 lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1043 if (cur == lt->cursor) {
1048 lt->setSelection(bv);
1049 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1050 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1054 updateLocal(bv, SELECTION, false);
1056 showInsetCursor(bv);
1060 void InsetText::insetKeyPress(XKeyEvent * xke)
1062 if (the_locking_inset) {
1063 the_locking_inset->insetKeyPress(xke);
1069 UpdatableInset::RESULT
1070 InsetText::localDispatch(BufferView * bv,
1071 kb_action action, string const & arg)
1073 bool was_empty = par->size() == 0 && !par->next();
1074 no_selection = false;
1075 UpdatableInset::RESULT
1076 result= UpdatableInset::localDispatch(bv, action, arg);
1077 if (result != UNDISPATCHED) {
1081 result = DISPATCHED;
1082 if ((action < 0) && arg.empty())
1085 if (the_locking_inset) {
1086 result = the_locking_inset->localDispatch(bv, action, arg);
1087 if (result == DISPATCHED_NOUPDATE)
1089 else if (result == DISPATCHED) {
1090 updateLocal(bv, CURSOR_PAR, false);
1092 } else if (result >= FINISHED) {
1094 case FINISHED_RIGHT:
1095 moveRightIntern(bv, false, false);
1096 result = DISPATCHED;
1099 if ((result = moveUp(bv)) >= FINISHED) {
1100 updateLocal(bv, CURSOR, false);
1101 bv->unlockInset(this);
1105 if ((result = moveDown(bv)) >= FINISHED) {
1106 updateLocal(bv, CURSOR, false);
1107 bv->unlockInset(this);
1111 result = DISPATCHED;
1114 the_locking_inset = 0;
1115 #ifdef WITH_WARNINGS
1116 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
1121 hideInsetCursor(bv);
1124 lt = getLyXText(bv);
1128 int updflag = false;
1131 case LFUN_SELFINSERT:
1132 if (bv->buffer()->isReadonly()) {
1133 // setErrorMessage(N_("Document is read only"));
1137 /* Automatically delete the currently selected
1138 * text and replace it with what is being
1139 * typed in now. Depends on lyxrc settings
1140 * "auto_region_delete", which defaults to
1143 setUndo(bv, Undo::INSERT,
1144 lt->cursor.par(), lt->cursor.par()->next());
1146 if (lyxrc.auto_region_delete) {
1147 if (lt->selection.set()) {
1148 lt->cutSelection(bv, false);
1151 lt->clearSelection();
1152 for (string::size_type i = 0; i < arg.length(); ++i) {
1153 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1156 lt->selection.cursor = lt->cursor;
1157 updwhat = CURSOR_PAR;
1159 result = DISPATCHED_NOUPDATE;
1161 // --- Cursor Movements -----------------------------------
1164 moveRight(bv, false, true);
1165 lt->setSelection(bv);
1166 updwhat = SELECTION;
1169 result = moveRight(bv);
1175 moveLeft(bv, false, true);
1176 lt->setSelection(bv);
1177 updwhat = SELECTION;
1181 result = moveLeft(bv);
1187 lt->setSelection(bv);
1188 updwhat = SELECTION;
1192 result = moveDown(bv);
1198 lt->setSelection(bv);
1199 updwhat = SELECTION;
1203 result = moveUp(bv);
1215 case LFUN_BACKSPACE: {
1216 setUndo(bv, Undo::DELETE,
1217 lt->cursor.par(), lt->cursor.par()->next());
1218 if (lt->selection.set())
1219 lt->cutSelection(bv);
1222 updwhat = CURSOR_PAR;
1228 setUndo(bv, Undo::DELETE,
1229 lt->cursor.par(), lt->cursor.par()->next());
1230 if (lt->selection.set()) {
1231 lt->cutSelection(bv);
1235 updwhat = CURSOR_PAR;
1241 setUndo(bv, Undo::DELETE,
1242 lt->cursor.par(), lt->cursor.par()->next());
1243 lt->cutSelection(bv);
1244 updwhat = CURSOR_PAR;
1251 lt->copySelection(bv);
1252 updwhat = CURSOR_PAR;
1254 case LFUN_PASTESELECTION:
1256 string const clip(bv->getClipboard());
1260 if (arg == "paragraph") {
1261 lt->insertStringAsParagraphs(bv, clip);
1263 lt->insertStringAsLines(bv, clip);
1265 updwhat = CURSOR_PAR;
1270 if (!autoBreakRows) {
1272 if (CutAndPaste::nrOfParagraphs() > 1) {
1273 Alert::alert(_("Impossible operation"),
1274 _("Cannot include more than one paragraph!"),
1279 setUndo(bv, Undo::INSERT,
1280 lt->cursor.par(), lt->cursor.par()->next());
1281 lt->pasteSelection(bv);
1282 updwhat = CURSOR_PAR;
1287 case LFUN_BREAKPARAGRAPH:
1288 if (!autoBreakRows) {
1289 result = DISPATCHED;
1292 lt->breakParagraph(bv, 0);
1296 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1297 if (!autoBreakRows) {
1298 result = DISPATCHED;
1301 lt->breakParagraph(bv, 1);
1306 case LFUN_BREAKLINE: {
1307 if (!autoBreakRows) {
1308 result = DISPATCHED;
1311 setUndo(bv, Undo::INSERT,
1312 lt->cursor.par(), lt->cursor.par()->next());
1313 lt->insertChar(bv, Paragraph::META_NEWLINE);
1314 updwhat = CURSOR_PAR;
1320 // do not set layouts on non breakable textinsets
1321 if (autoBreakRows) {
1322 string cur_layout = cpar(bv)->layout();
1324 // Derive layout number from given argument (string)
1325 // and current buffer's textclass (number). */
1326 textclass_type tclass = bv->buffer()->params.textclass;
1327 string layout = lowercase(arg);
1328 bool hasLayout = textclasslist[tclass].hasLayout(layout);
1330 // If the entry is obsolete, use the new one instead.
1332 string const & obs =
1333 textclasslist[tclass][layout].
1336 layout = lowercase(obs);
1339 // see if we found the layout number:
1341 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1342 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1346 if (cur_layout != layout) {
1347 cur_layout = layout;
1348 lt->setLayout(bv, layout);
1349 bv->owner()->setLayout(cpar(bv)->layout());
1350 updwhat = CURSOR_PAR;
1354 // reset the layout box
1355 bv->owner()->setLayout(cpar(bv)->layout());
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[buf->params.textclass][p->layout()];
1474 // environment tag closing
1475 for (; depth > p->params().depth(); --depth) {
1476 if (environment_inner[depth] != "!-- --") {
1477 item_name = "listitem";
1478 buf->sgmlCloseTag(os, command_depth + depth,
1480 if (environment_inner[depth] == "varlistentry")
1481 buf->sgmlCloseTag(os, depth+command_depth,
1482 environment_inner[depth]);
1484 buf->sgmlCloseTag(os, depth + command_depth,
1485 environment_stack[depth]);
1486 environment_stack[depth].erase();
1487 environment_inner[depth].erase();
1490 if (depth == p->params().depth()
1491 && environment_stack[depth] != style.latexname()
1492 && !environment_stack[depth].empty()) {
1493 if (environment_inner[depth] != "!-- --") {
1494 item_name= "listitem";
1495 buf->sgmlCloseTag(os, command_depth+depth,
1497 if (environment_inner[depth] == "varlistentry")
1498 buf->sgmlCloseTag(os,
1499 depth + command_depth,
1500 environment_inner[depth]);
1503 buf->sgmlCloseTag(os, depth + command_depth,
1504 environment_stack[depth]);
1506 environment_stack[depth].erase();
1507 environment_inner[depth].erase();
1510 // Write opening SGML tags.
1511 switch (style.latextype) {
1512 case LATEX_PARAGRAPH:
1513 buf->sgmlOpenTag(os, depth + command_depth,
1518 buf->sgmlError(p, 0,
1519 _("Error : LatexType Command not allowed here.\n"));
1523 case LATEX_ENVIRONMENT:
1524 case LATEX_ITEM_ENVIRONMENT:
1525 if (depth < p->params().depth()) {
1526 depth = p->params().depth();
1527 environment_stack[depth].erase();
1530 if (environment_stack[depth] != style.latexname()) {
1531 if (environment_stack.size() == depth + 1) {
1532 environment_stack.push_back("!-- --");
1533 environment_inner.push_back("!-- --");
1535 environment_stack[depth] = style.latexname();
1536 environment_inner[depth] = "!-- --";
1537 buf->sgmlOpenTag(os, depth + command_depth,
1538 environment_stack[depth]);
1540 if (environment_inner[depth] != "!-- --") {
1541 item_name= "listitem";
1542 buf->sgmlCloseTag(os,
1543 command_depth + depth,
1545 if (environment_inner[depth] == "varlistentry")
1546 buf->sgmlCloseTag(os,
1547 depth + command_depth,
1548 environment_inner[depth]);
1552 if (style.latextype == LATEX_ENVIRONMENT) {
1553 if (!style.latexparam().empty()) {
1554 if (style.latexparam() == "CDATA")
1557 buf->sgmlOpenTag(os, depth + command_depth,
1558 style.latexparam());
1563 desc_on = (style.labeltype == LABEL_MANUAL);
1566 environment_inner[depth]= "varlistentry";
1568 environment_inner[depth]= "listitem";
1570 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1571 environment_inner[depth]);
1575 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1579 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1584 buf->sgmlOpenTag(os, depth + command_depth,
1589 buf->simpleDocBookOnePar(os, p, desc_on,
1590 depth + 1 + command_depth);
1594 // write closing SGML tags
1595 switch (style.latextype) {
1596 case LATEX_ENVIRONMENT:
1597 if (!style.latexparam().empty()) {
1598 if (style.latexparam() == "CDATA")
1601 buf->sgmlCloseTag(os, depth + command_depth,
1602 style.latexparam());
1605 case LATEX_ITEM_ENVIRONMENT:
1606 if (desc_on == 1) break;
1608 buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1610 case LATEX_PARAGRAPH:
1611 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1614 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1620 for (int d = depth; d >= 0; --d) {
1621 if (!environment_stack[depth].empty()) {
1622 if (environment_inner[depth] != "!-- --") {
1623 item_name = "listitem";
1624 buf->sgmlCloseTag(os, command_depth + depth,
1626 if (environment_inner[depth] == "varlistentry")
1627 buf->sgmlCloseTag(os, depth + command_depth,
1628 environment_inner[depth]);
1631 buf->sgmlCloseTag(os, depth + command_depth,
1632 environment_stack[depth]);
1640 void InsetText::validate(LaTeXFeatures & features) const
1642 Paragraph * p = par;
1644 p->validate(features);
1650 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1652 if (textclasslist[buf->params.textclass][p->layout()].labeltype != LABEL_MANUAL)
1655 return p->beginningOfMainBody();
1659 void InsetText::getCursorPos(BufferView * bv,
1660 int & x, int & y) const
1662 if (the_locking_inset) {
1663 the_locking_inset->getCursorPos(bv, x, y);
1671 unsigned int InsetText::insetInInsetY()
1673 if (!the_locking_inset)
1676 return (inset_y + the_locking_inset->insetInInsetY());
1680 void InsetText::toggleInsetCursor(BufferView * bv)
1682 if (the_locking_inset) {
1683 the_locking_inset->toggleInsetCursor(bv);
1687 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1689 int const asc = lyxfont::maxAscent(font);
1690 int const desc = lyxfont::maxDescent(font);
1692 if (isCursorVisible())
1693 bv->hideLockedInsetCursor();
1695 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1696 toggleCursorVisible();
1700 void InsetText::showInsetCursor(BufferView * bv, bool show)
1702 if (the_locking_inset) {
1703 the_locking_inset->showInsetCursor(bv, show);
1706 if (!isCursorVisible()) {
1707 LyXFont const font =
1708 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1710 int const asc = lyxfont::maxAscent(font);
1711 int const desc = lyxfont::maxDescent(font);
1713 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1715 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1716 setCursorVisible(true);
1721 void InsetText::hideInsetCursor(BufferView * bv)
1723 if (isCursorVisible()) {
1724 bv->hideLockedInsetCursor();
1725 setCursorVisible(false);
1727 if (the_locking_inset)
1728 the_locking_inset->hideInsetCursor(bv);
1732 void InsetText::fitInsetCursor(BufferView * bv) const
1734 if (the_locking_inset) {
1735 the_locking_inset->fitInsetCursor(bv);
1738 LyXFont const font =
1739 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1741 int const asc = lyxfont::maxAscent(font);
1742 int const desc = lyxfont::maxDescent(font);
1744 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1748 UpdatableInset::RESULT
1749 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1751 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1752 return moveLeftIntern(bv, false, activate_inset, selecting);
1754 return moveRightIntern(bv, false, activate_inset, selecting);
1758 UpdatableInset::RESULT
1759 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1761 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1762 return moveRightIntern(bv, true, activate_inset, selecting);
1764 return moveLeftIntern(bv, true, activate_inset, selecting);
1768 UpdatableInset::RESULT
1769 InsetText::moveRightIntern(BufferView * bv, bool behind,
1770 bool activate_inset, bool selecting)
1772 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1773 return FINISHED_RIGHT;
1774 if (activate_inset && checkAndActivateInset(bv, behind))
1776 getLyXText(bv)->cursorRight(bv);
1778 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1779 return DISPATCHED_NOUPDATE;
1783 UpdatableInset::RESULT
1784 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1785 bool activate_inset, bool selecting)
1787 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1789 getLyXText(bv)->cursorLeft(bv);
1791 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1792 if (activate_inset && checkAndActivateInset(bv, behind))
1794 return DISPATCHED_NOUPDATE;
1798 UpdatableInset::RESULT
1799 InsetText::moveUp(BufferView * bv)
1801 if (!crow(bv)->previous())
1803 getLyXText(bv)->cursorUp(bv);
1804 return DISPATCHED_NOUPDATE;
1808 UpdatableInset::RESULT
1809 InsetText::moveDown(BufferView * bv)
1811 if (!crow(bv)->next())
1812 return FINISHED_DOWN;
1813 getLyXText(bv)->cursorDown(bv);
1814 return DISPATCHED_NOUPDATE;
1818 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1820 if (the_locking_inset) {
1821 if (the_locking_inset->insetAllowed(inset))
1822 return the_locking_inset->insertInset(bv, inset);
1827 lt = getLyXText(bv);
1830 setUndo(bv, Undo::FINISH, lt->cursor.par(), lt->cursor.par()->next());
1832 inset->setOwner(this);
1833 hideInsetCursor(bv);
1834 lt->insertInset(bv, inset);
1838 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1844 bool InsetText::insetAllowed(Inset::Code code) const
1846 // in_insetAllowed is a really gross hack,
1847 // to allow us to call the owner's insetAllowed
1848 // without stack overflow, which can happen
1849 // when the owner uses InsetCollapsable::insetAllowed()
1851 if (in_insetAllowed)
1853 in_insetAllowed = true;
1854 if (the_locking_inset)
1855 ret = the_locking_inset->insetAllowed(code);
1857 ret = owner()->insetAllowed(code);
1858 in_insetAllowed = false;
1863 UpdatableInset * InsetText::getLockingInset() const
1865 return the_locking_inset ? the_locking_inset->getLockingInset() :
1866 const_cast<InsetText *>(this);
1870 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1874 if (the_locking_inset)
1875 return the_locking_inset->getFirstLockingInsetOfType(c);
1880 bool InsetText::showInsetDialog(BufferView * bv) const
1882 if (the_locking_inset)
1883 return the_locking_inset->showInsetDialog(bv);
1888 vector<string> const InsetText::getLabelList() const
1890 vector<string> label_list;
1892 Paragraph * tpar = par;
1894 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1895 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1896 for (; beg != end; ++beg) {
1897 vector<string> const l = (*beg)->getLabelList();
1898 label_list.insert(label_list.end(), l.begin(), l.end());
1900 tpar = tpar->next();
1906 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1909 if (the_locking_inset) {
1910 the_locking_inset->setFont(bv, font, toggleall, selectall);
1913 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
1914 getLyXText(bv)->setFont(bv, font, toggleall);
1919 lt = getLyXText(bv);
1922 if (lt->selection.set()) {
1923 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1927 lt->toggleFree(bv, font, toggleall);
1929 lt->clearSelection();
1931 bool flag = (selectall || lt->selection.set());
1935 updateLocal(bv, FULL, true);
1937 updateLocal(bv, CURSOR_PAR, true);
1941 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1943 if (cpar(bv)->isInset(cpos(bv))) {
1947 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1948 if (!isHighlyEditableInset(inset))
1950 LyXFont const font =
1951 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1953 x = inset->width(bv, font);
1954 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1957 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1959 //inset_x = cx(bv) - top_x + drawTextXOffset;
1960 //inset_y = cy(bv) + drawTextYOffset;
1961 inset->edit(bv, x, y, 0);
1962 if (!the_locking_inset)
1964 updateLocal(bv, CURSOR, false);
1971 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1974 x -= drawTextXOffset;
1976 int dummyy = y + insetAscent;
1977 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
1984 inset_x = cx(bv) - top_x + drawTextXOffset;
1985 inset_y = cy(bv) + drawTextYOffset;
1986 inset->edit(bv, x - inset_x, y - inset_y, button);
1987 if (!the_locking_inset)
1989 updateLocal(bv, CURSOR, false);
1996 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1999 int w = UpdatableInset::getMaxWidth(bv, inset);
2004 w = w - top_x + owner()->x();
2007 w -= (2 * TEXT_TO_INSET_OFFSET);
2010 return UpdatableInset::getMaxWidth(bv, inset);
2015 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2017 // we have to unlock any locked inset otherwise we're in troubles
2018 the_locking_inset = 0;
2020 Paragraph * tmp = par->next();
2025 par = new Paragraph(*p, same_id);
2026 par->setInsetOwner(this);
2027 Paragraph * np = par;
2030 np->next(new Paragraph(*p, same_id));
2031 np->next()->previous(np);
2033 np->setInsetOwner(this);
2040 void InsetText::setText(string const & data)
2043 LyXFont font(LyXFont::ALL_SANE);
2044 for (unsigned int i=0; i < data.length(); ++i)
2045 par->insertChar(i, data[i], font);
2049 void InsetText::setAutoBreakRows(bool flag)
2051 if (flag != autoBreakRows) {
2052 autoBreakRows = flag;
2060 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2062 if (how != drawFrame_) {
2065 updateLocal(bv, DRAW_FRAME, false);
2070 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2072 if (frame_color != col) {
2075 updateLocal(bv, DRAW_FRAME, false);
2080 int InsetText::cx(BufferView * bv) const
2082 // we do nothing dangerous so we use a local cache
2083 LyXText * llt = getLyXText(bv);
2084 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2085 if (the_locking_inset) {
2086 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2088 if (font.isVisibleRightToLeft())
2089 x -= the_locking_inset->width(bv, font);
2095 int InsetText::cy(BufferView * bv) const
2098 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2102 pos_type InsetText::cpos(BufferView * bv) const
2104 return getLyXText(bv)->cursor.pos();
2108 Paragraph * InsetText::cpar(BufferView * bv) const
2110 return getLyXText(bv)->cursor.par();
2114 bool InsetText::cboundary(BufferView * bv) const
2116 return getLyXText(bv)->cursor.boundary();
2120 Row * InsetText::crow(BufferView * bv) const
2122 return getLyXText(bv)->cursor.row();
2126 LyXText * InsetText::getLyXText(BufferView const * lbv,
2127 bool const recursive) const
2129 if (!recursive && (cached_bview == lbv)) {
2130 LyXText * lt = cached_text.get();
2131 lyx::Assert(lt && lt->firstRow()->par() == par);
2135 // Super UGLY! (Lgb)
2136 BufferView * bv = const_cast<BufferView *>(lbv);
2139 Cache::iterator it = cache.find(bv);
2141 if (it != cache.end()) {
2144 } else if (do_resize) {
2145 resizeLyXText(do_resize);
2147 if (lt || !it->second.remove) {
2148 lyx::Assert(it->second.text.get());
2149 cached_text = it->second.text;
2150 if (recursive && the_locking_inset) {
2151 return the_locking_inset->getLyXText(bv, true);
2153 return cached_text.get();
2154 } else if (it->second.remove) {
2156 saveLyXTextState(it->second.text.get());
2162 // when we have to reinit the existing LyXText!
2164 it->second.text->init(bv);
2165 restoreLyXTextState(bv, it->second.text.get());
2166 it->second.remove = false;
2168 cached_text = it->second.text;
2169 if (the_locking_inset && recursive) {
2170 return the_locking_inset->getLyXText(bv);
2172 return cached_text.get();
2175 // we are here only if we don't have a BufferView * in the cache!!!
2177 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2178 cached_text->init(bv);
2179 restoreLyXTextState(bv, cached_text.get());
2181 cache.insert(make_pair(bv, cached_text));
2183 if (the_locking_inset && recursive) {
2184 return the_locking_inset->getLyXText(bv);
2186 return cached_text.get();
2190 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2194 Cache::iterator it = cache.find(bv);
2196 if (it == cache.end()) {
2200 lyx::Assert(it->second.text.get());
2202 it->second.remove = true;
2204 /// then remove all LyXText in text-insets
2205 Paragraph * p = par;
2206 for (; p; p = p->next()) {
2207 p->deleteInsetsLyXText(bv);
2213 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2216 // we cannot resize this because we are in use!
2217 // so do this on the next possible getLyXText()
2222 // lyxerr << "InsetText::resizeLyXText\n";
2223 if (!par->next() && !par->size()) { // no data, resize not neccessary!
2224 // we have to do this as a fixed width may have changed!
2225 LyXText * t = getLyXText(bv);
2226 saveLyXTextState(t);
2228 restoreLyXTextState(bv, t);
2231 // one endless line, resize normally not necessary
2232 if (!force && getMaxWidth(bv, this) < 0)
2235 Cache::iterator it = cache.find(bv);
2236 if (it == cache.end()) {
2239 lyx::Assert(it->second.text.get());
2241 LyXText * t = it->second.text.get();
2242 saveLyXTextState(t);
2243 for (Paragraph * p = par; p; p = p->next()) {
2244 p->resizeInsetsLyXText(bv);
2247 restoreLyXTextState(bv, t);
2248 if (the_locking_inset) {
2249 inset_x = cx(bv) - top_x + drawTextXOffset;
2250 inset_y = cy(bv) + drawTextYOffset;
2254 t->first = bv->screen()->topCursorVisible(t);
2257 updateLocal(bv, FULL, false);
2258 // this will scroll the screen such that the cursor becomes visible
2259 bv->updateScrollbar();
2261 need_update |= FULL;
2266 void InsetText::reinitLyXText() const
2269 // we cannot resize this because we are in use!
2270 // so do this on the next possible getLyXText()
2276 // lyxerr << "InsetText::reinitLyXText\n";
2277 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2278 lyx::Assert(it->second.text.get());
2280 LyXText * t = it->second.text.get();
2281 BufferView * bv = it->first;
2283 saveLyXTextState(t);
2284 for (Paragraph * p = par; p; p = p->next()) {
2285 p->resizeInsetsLyXText(bv);
2288 restoreLyXTextState(bv, t);
2289 if (the_locking_inset) {
2290 inset_x = cx(bv) - top_x + drawTextXOffset;
2291 inset_y = cy(bv) + drawTextYOffset;
2294 t->first = bv->screen()->topCursorVisible(t);
2297 updateLocal(bv, FULL, false);
2298 // this will scroll the screen such that the cursor becomes visible
2299 bv->updateScrollbar();
2307 void InsetText::removeNewlines()
2309 bool changed = false;
2311 for (Paragraph * p = par; p; p = p->next()) {
2312 for (int i = 0; i < p->size(); ++i) {
2313 if (p->getChar(i) == Paragraph::META_NEWLINE) {
2324 bool InsetText::nodraw() const
2326 if (the_locking_inset)
2327 return the_locking_inset->nodraw();
2328 return UpdatableInset::nodraw();
2332 int InsetText::scroll(bool recursive) const
2334 int sx = UpdatableInset::scroll(false);
2336 if (recursive && the_locking_inset)
2337 sx += the_locking_inset->scroll(recursive);
2343 bool InsetText::doClearArea() const
2345 return !locked || (need_update & (FULL|INIT));
2349 void InsetText::selectAll(BufferView * bv)
2351 getLyXText(bv)->cursorTop(bv);
2352 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2353 getLyXText(bv)->cursorBottom(bv);
2354 getLyXText(bv)->setSelection(bv);
2358 void InsetText::clearSelection(BufferView * bv)
2360 getLyXText(bv)->clearSelection();
2364 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2366 Painter & pain = bv->painter();
2368 int h = insetAscent + insetDescent;
2369 int ty = baseline - insetAscent;
2375 if ((ty + h) > pain.paperHeight())
2376 h = pain.paperHeight();
2377 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2378 w = pain.paperWidth();
2379 // w -= TEXT_TO_INSET_OFFSET;
2380 pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2383 frame_is_visible = false;
2387 Paragraph * InsetText::getParFromID(int id) const
2390 Paragraph * result = par;
2391 Paragraph * ires = 0;
2392 while (result && result->id() != id) {
2393 if ((ires = result->getParFromID(id)))
2395 result = result->next();
2399 Paragraph * tmp = par;
2401 if (tmp->id() == id) {
2404 Paragraph * tmp2 = tmp->getParFromID(id);
2415 Paragraph * InsetText::firstParagraph() const
2418 if (the_locking_inset)
2419 if ((result = the_locking_inset->firstParagraph()))
2425 Paragraph * InsetText::getFirstParagraph(int i) const
2427 return (i == 0) ? par : 0;
2431 LyXCursor const & InsetText::cursor(BufferView * bv) const
2433 if (the_locking_inset)
2434 return the_locking_inset->cursor(bv);
2435 return getLyXText(bv)->cursor;
2439 Paragraph * InsetText::paragraph() const
2445 void InsetText::paragraph(Paragraph * p)
2447 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2448 // caller of this function has to take care of it. This IS important
2449 // as we could have to insert a paragraph before this one and just
2450 // link the actual to a new ones next and set it with this function
2453 // set ourself as owner for all the paragraphs inserted!
2454 Paragraph * np = par;
2456 np->setInsetOwner(this);
2460 // redraw myself when asked for
2465 Inset * InsetText::getInsetFromID(int id_arg) const
2468 return const_cast<InsetText *>(this);
2470 Paragraph * lp = par;
2473 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2474 en = lp->inset_iterator_end();
2477 if ((*it)->id() == id_arg)
2479 Inset * in = (*it)->getInsetFromID(id_arg);
2489 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2495 lt = getLyXText(bv);
2498 if (the_locking_inset) {
2499 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2506 // we have to go on checking so move cusor to the next char
2507 lt->cursor.pos(lt->cursor.pos() + 1);
2509 str = lt->selectNextWordToSpellcheck(bv, value);
2511 bv->unlockInset(const_cast<InsetText *>(this));
2520 void InsetText::selectSelectedWord(BufferView * bv)
2522 if (the_locking_inset) {
2523 the_locking_inset->selectSelectedWord(bv);
2526 getLyXText(bv)->selectSelectedWord(bv);
2527 updateLocal(bv, SELECTION, false);
2531 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2533 if (the_locking_inset) {
2534 the_locking_inset->toggleSelection(bv, kill_selection);
2538 lt = getLyXText(bv);
2542 int x = top_x + TEXT_TO_INSET_OFFSET;
2544 Row * row = lt->firstRow();
2545 int y_offset = top_baseline - row->ascent_of_text();
2547 while ((row != 0) && ((y+row->height()) <= 0)) {
2554 if (need_update & SELECTION)
2556 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2562 bool InsetText::searchForward(BufferView * bv, string const & str,
2565 if (the_locking_inset) {
2566 if (the_locking_inset->searchForward(bv, str, cs, mw))
2570 lt = getLyXText(bv);
2573 Paragraph * lpar = lt->cursor.par();
2574 pos_type pos = lt->cursor.pos();
2575 if (pos < lpar->size() - 1)
2579 lpar = lpar->next();
2584 // we have to unlock ourself in this function by default!
2585 bv->unlockInset(const_cast<InsetText *>(this));
2588 lt->setCursor(bv, lpar, pos);
2592 if (LyXFind(bv, str, true, true, cs , mw)) {
2595 // we have to unlock ourself in this function by default!
2596 bv->unlockInset(const_cast<InsetText *>(this));
2600 bool InsetText::searchBackward(BufferView * bv, string const & str,
2603 if (the_locking_inset)
2604 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2606 if (LyXFind(bv, str, false, true, cs, mw)) {
2609 // we have to unlock ourself in this function by default!
2610 bv->unlockInset(const_cast<InsetText *>(this));
2615 bool InsetText::checkInsertChar(LyXFont & font)
2618 return owner()->checkInsertChar(font);
2623 void InsetText::collapseParagraphs(BufferParams const & bparams) const
2625 while(par->next()) {
2626 if (!par->isSeparator(par->size()-1))
2627 par->insertChar(par->size()-1, ' ');
2628 par->pasteParagraph(bparams);
2634 void InsetText::getDrawFont(LyXFont & font) const
2638 owner()->getDrawFont(font);