1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998-2001 The LyX Team.
8 * ======================================================
14 #pragma implementation
17 #include "insettext.h"
18 #include "paragraph.h"
22 #include "commandtags.h"
25 #include "BufferView.h"
26 #include "lyxtextclasslist.h"
27 #include "LaTeXFeatures.h"
30 #include "lyxcursor.h"
31 #include "CutAndPaste.h"
37 #include "trans_mgr.h"
38 #include "lyxscreen.h"
42 #include "ParagraphParameters.h"
43 #include "undo_funcs.h"
46 #include "frontends/Alert.h"
48 #include "support/textutils.h"
49 #include "support/LAssert.h"
50 #include "support/lstrings.h"
51 #include "support/lyxalgo.h" // lyx::count
68 using lyx::textclass_type;
70 extern unsigned char getCurrentTextClass(Buffer *);
71 extern bool math_insert_greek(BufferView *, char);
72 extern int greek_kb_flag;
75 // These functions should probably go into bufferview_funcs somehow (Jug)
77 void InsetText::saveLyXTextState(LyXText * t) const
79 // check if my paragraphs are still valid
82 if (p == t->cursor.par())
87 if (p && t->cursor.pos() <= p->size()) {
88 sstate.lpar = t->cursor.par();
89 sstate.pos = t->cursor.pos();
90 sstate.boundary = t->cursor.boundary();
91 sstate.selstartpar = t->selection.start.par();
92 sstate.selstartpos = t->selection.start.pos();
93 sstate.selstartboundary = t->selection.start.boundary();
94 sstate.selendpar = t->selection.end.par();
95 sstate.selendpos = t->selection.end.pos();
96 sstate.selendboundary = t->selection.end.boundary();
97 sstate.selection = t->selection.set();
98 sstate.mark_set = t->selection.mark();
99 sstate.refresh = t->refresh_row != 0;
105 void InsetText::restoreLyXTextState(BufferView * bv, LyXText * t) const
108 t->selection.set(true);
109 /* at this point just to avoid the Delete-Empty-Paragraph
110 * Mechanism when setting the cursor */
111 t->selection.mark(sstate.mark_set);
112 if (sstate.selection) {
113 t->setCursor(bv, sstate.selstartpar, sstate.selstartpos,
114 true, sstate.selstartboundary);
115 t->selection.cursor = t->cursor;
116 t->setCursor(bv, sstate.selendpar, sstate.selendpos,
117 true, sstate.selendboundary);
119 t->setCursor(bv, sstate.lpar, sstate.pos);
121 t->setCursor(bv, sstate.lpar, sstate.pos, true, sstate.boundary);
122 t->selection.cursor = t->cursor;
123 t->selection.set(false);
125 if (sstate.refresh) {
131 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
138 InsetText::InsetText(BufferParams const & bp)
139 : UpdatableInset(), lt(0), in_update(false), do_resize(0),
143 par->layout(textclasslist[bp.textclass].defaultLayoutName());
149 InsetText::InsetText(InsetText const & in, bool same_id)
150 : UpdatableInset(in, same_id), lt(0), in_update(false), do_resize(0),
158 InsetText & InsetText::operator=(InsetText const & it)
165 void InsetText::init(InsetText const * ins, bool same_id)
168 setParagraphData(ins->par, same_id);
169 autoBreakRows = ins->autoBreakRows;
170 drawFrame_ = ins->drawFrame_;
171 frame_color = ins->frame_color;
177 p->setInsetOwner(this);
180 the_locking_inset = 0;
182 frame_color = LColor::insetframe;
183 autoBreakRows = false;
190 no_selection = false;
197 last_drawn_width = -1;
198 frame_is_visible = false;
201 in_insetAllowed = false;
205 InsetText::~InsetText()
212 Paragraph * tmp = par->next();
219 void InsetText::clear()
221 // This is a gross hack...
222 string old_layout = par->layout();
225 Paragraph * tmp = par->next();
230 par->layout(old_layout);
237 Inset * InsetText::clone(Buffer const &, bool same_id) const
239 return new InsetText(*this, same_id);
243 void InsetText::write(Buffer const * buf, ostream & os) const
246 writeParagraphData(buf, os);
250 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
252 par->writeFile(buf, os, buf->params, 0);
256 void InsetText::read(Buffer const * buf, LyXLex & lex)
260 Paragraph * return_par = 0;
261 Paragraph::depth_type depth = 0;
262 LyXFont font(LyXFont::ALL_INHERIT);
268 token = lex.getString();
271 if (token == "\\end_inset") {
272 #ifndef NO_COMPABILITY
273 const_cast<Buffer*>(buf)->insertErtContents(par, pos, false);
278 if (const_cast<Buffer*>(buf)->
279 parseSingleLyXformat2Token(lex, par, return_par,
280 token, pos, depth, font)) {
281 // the_end read this should NEVER happen
282 lex.printError("\\the_end read in inset! Error in document!");
290 return_par->setInsetOwner(this);
291 return_par = return_par->next();
294 if (token != "\\end_inset") {
295 lex.printError("Missing \\end_inset at this point. "
302 int InsetText::ascent(BufferView * bv, LyXFont const &) const
304 insetAscent = getLyXText(bv)->firstRow()->ascent_of_text() +
305 TEXT_TO_INSET_OFFSET;
310 int InsetText::descent(BufferView * bv, LyXFont const &) const
312 LyXText * llt = getLyXText(bv);
313 insetDescent = llt->height - llt->firstRow()->ascent_of_text() +
314 TEXT_TO_INSET_OFFSET;
319 int InsetText::width(BufferView * bv, LyXFont const &) const
321 insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
322 (2 * TEXT_TO_INSET_OFFSET);
323 insetWidth = max(insetWidth, 10);
328 int InsetText::textWidth(BufferView * bv, bool fordraw) const
331 if (!autoBreakRows) {
334 w = getMaxWidth(bv, this);
337 return max(w - (2 * TEXT_TO_INSET_OFFSET),
338 (int)getLyXText(bv)->width);
342 return w - (2 * TEXT_TO_INSET_OFFSET);
346 void InsetText::draw(BufferView * bv, LyXFont const & f,
347 int baseline, float & x, bool cleared) const
352 Painter & pain = bv->painter();
354 // this is the first thing we have to ask because if the x pos
355 // changed we have to do a complete rebreak of the text as we
356 // may have few space to draw in. Well we should check on this too
358 if (top_x != int(x)) {
361 int nw = getMaxWidth(bv, this);
362 if (nw > 0 && old_max_width != nw) {
365 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
370 // call these methods so that insetWidth, insetAscent and
371 // insetDescent have the right values.
376 // repaint the background if needed
377 if (cleared && backgroundColor() != LColor::background) {
378 clearInset(bv, baseline, cleared);
381 // no draw is necessary !!!
382 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
383 top_baseline = baseline;
385 if (need_update & CLEAR_FRAME)
386 clearFrame(pain, cleared);
393 x += static_cast<float>(scroll());
395 // if top_x differs we did it already
396 if (!cleared && (old_x == int(x))
397 && ((need_update&(INIT|FULL)) || (top_baseline != baseline)
398 ||(last_drawn_width != insetWidth)))
400 // Condition necessary to eliminate bug 59 attachment 37
402 clearInset(bv, baseline, cleared);
406 frame_is_visible = false;
408 if (!cleared && (need_update == NONE)) {
410 drawFrame(pain, cleared);
414 top_baseline = baseline;
415 top_y = baseline - insetAscent;
417 if (last_drawn_width != insetWidth) {
419 clearInset(bv, baseline, cleared);
421 last_drawn_width = insetWidth;
424 if (the_locking_inset && (cpar(bv) == inset_par)
425 && (cpos(bv) == inset_pos)) {
426 inset_x = cx(bv) - top_x + drawTextXOffset;
427 inset_y = cy(bv) + drawTextYOffset;
429 if (!cleared && (need_update == CURSOR)
430 && !getLyXText(bv)->selection.set()) {
431 drawFrame(pain, cleared);
441 x += TEXT_TO_INSET_OFFSET;
443 Row * row = lt->firstRow();
444 int y_offset = baseline - row->ascent_of_text();
445 int ph = pain.paperHeight();
448 while ((row != 0) && ((y+row->height()) <= 0)) {
450 first += row->height();
456 if (cleared || (need_update&(INIT|FULL))) {
459 while ((row != 0) && (yf < ph)) {
460 lt->getVisibleRow(bv, y+y_offset, int(x), row,
466 } else if (!locked) {
467 if (need_update & CURSOR) {
468 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
469 lt->clearSelection();
470 lt->selection.cursor = lt->cursor;
472 bv->screen()->update(lt, bv, y_offset, int(x));
475 if (need_update & SELECTION) {
476 bv->screen()->toggleToggle(lt, bv, y_offset, int(x));
477 } else if (need_update & CURSOR) {
478 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
479 lt->clearSelection();
480 lt->selection.cursor = lt->cursor;
482 bv->screen()->update(lt, bv, y_offset, int(x));
487 lt->status(bv, LyXText::UNCHANGED);
488 if ((need_update != CURSOR_PAR) &&
489 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked))) {
490 drawFrame(pain, cleared);
491 } else if (need_update & CLEAR_FRAME) {
492 clearFrame(pain, cleared);
495 x += insetWidth - TEXT_TO_INSET_OFFSET;
497 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
499 } else if (need_update != INIT) {
507 void InsetText::drawFrame(Painter & pain, bool cleared) const
509 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
510 if (!frame_is_visible || cleared) {
511 frame_x = top_x + ttoD2;
512 frame_y = top_baseline - insetAscent + ttoD2;
513 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
514 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
515 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
517 frame_is_visible = true;
522 void InsetText::clearFrame(Painter & pain, bool cleared) const
524 if (frame_is_visible) {
526 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
529 frame_is_visible = false;
534 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
537 if (reinit && owner()) {
539 owner()->update(bv, font, true);
544 if (reinit || need_update == INIT) {
546 // we should put this call where we set need_update to INIT!
549 owner()->update(bv, font, true);
553 if (the_locking_inset) {
554 inset_x = cx(bv) - top_x + drawTextXOffset;
555 inset_y = cy(bv) + drawTextYOffset;
556 the_locking_inset->update(bv, font, reinit);
564 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
567 lt->updateInset(bv, the_locking_inset);
569 if (lt->status() == LyXText::NEED_MORE_REFRESH)
577 void InsetText::setUpdateStatus(BufferView * bv, int what) const
579 // this does nothing dangerous so use only a localized buffer
580 LyXText * llt = getLyXText(bv);
583 // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
584 // if we don't break row so that we only have one row to update!
585 if ((llt->status() == LyXText::NEED_MORE_REFRESH) ||
587 (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
590 } else if (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
591 need_update |= CURSOR_PAR;
594 // this to not draw a selection when we redraw all of it!
595 if (need_update & CURSOR && !(need_update & SELECTION)) {
596 if (llt->selection.set())
598 llt->clearSelection();
603 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
605 if (!autoBreakRows && par->next())
606 collapseParagraphs(bv->buffer()->params);
613 setUpdateStatus(bv, what);
614 bool flag = (((need_update != CURSOR) && (need_update != NONE)) ||
615 (lt->status() != LyXText::UNCHANGED) || lt->selection.set());
619 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
623 if (need_update == CURSOR)
625 bv->owner()->showState();
626 bv->owner()->updateMenubar();
627 bv->owner()->updateToolbar();
628 if (old_par != cpar(bv)) {
629 bv->owner()->setLayout(cpar(bv)->layout());
635 string const InsetText::editMessage() const
637 return _("Opened Text Inset");
641 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
643 UpdatableInset::edit(bv, x, y, button);
645 if (!bv->lockInset(this)) {
646 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
650 the_locking_inset = 0;
651 inset_pos = inset_x = inset_y = 0;
652 inset_boundary = false;
655 int tmp_y = (y < 0) ? 0 : y;
661 if (!checkAndActivateInset(bv, x, tmp_y, button))
662 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
664 lt->clearSelection();
666 // If the inset is empty set the language of the current font to the
667 // language to the surronding text (if different).
668 if (par->size() == 0 && !par->next() &&
669 bv->getParentLanguage(this) != lt->current_font.language())
671 LyXFont font(LyXFont::ALL_IGNORE);
672 font.setLanguage(bv->getParentLanguage(this));
673 setFont(bv, font, false);
680 if (drawFrame_ == LOCKED)
681 code = CURSOR|DRAW_FRAME;
682 updateLocal(bv, code, false);
686 void InsetText::edit(BufferView * bv, bool front)
688 UpdatableInset::edit(bv, front);
690 if (!bv->lockInset(this)) {
691 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
695 the_locking_inset = 0;
696 inset_pos = inset_x = inset_y = 0;
697 inset_boundary = false;
706 lt->setCursor(bv, par, 0);
711 // int const pos = (p->size() ? p->size()-1 : p->size());
712 lt->setCursor(bv, p, p->size());
714 lt->clearSelection();
716 // If the inset is empty set the language of the current font to the
717 // language to the surronding text (if different).
718 if (par->size() == 0 && !par->next() &&
719 bv->getParentLanguage(this) != lt->current_font.language()) {
720 LyXFont font(LyXFont::ALL_IGNORE);
721 font.setLanguage(bv->getParentLanguage(this));
722 setFont(bv, font, false);
728 if (drawFrame_ == LOCKED)
729 code = CURSOR|DRAW_FRAME;
730 updateLocal(bv, code, false);
734 void InsetText::insetUnlock(BufferView * bv)
736 if (the_locking_inset) {
737 the_locking_inset->insetUnlock(bv);
738 the_locking_inset = 0;
741 no_selection = false;
744 if (drawFrame_ == LOCKED)
745 code = CURSOR|CLEAR_FRAME;
753 if (lt->selection.set()) {
754 lt->clearSelection();
756 } else if (owner()) {
757 bv->owner()->setLayout(owner()->getLyXText(bv)
758 ->cursor.par()->layout());
760 bv->owner()->setLayout(bv->text->cursor.par()->layout());
761 // hack for deleteEmptyParMech
763 lt->setCursor(bv, par, 0);
764 } else if (par->next()) {
765 lt->setCursor(bv, par->next(), 0);
769 updateLocal(bv, code, false);
772 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
774 the_locking_inset = inset;
775 inset_x = cx(bv) - top_x + drawTextXOffset;
776 inset_y = cy(bv) + drawTextYOffset;
777 inset_pos = cpos(bv);
778 inset_par = cpar(bv);
779 inset_boundary = cboundary(bv);
780 updateLocal(bv, CURSOR, false);
784 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
786 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
790 if (!the_locking_inset) {
792 int const id = inset->id();
794 Paragraph::inset_iterator it =
795 p->inset_iterator_begin();
796 Paragraph::inset_iterator const end =
797 p->inset_iterator_end();
798 for (; it != end; ++it) {
799 if ((*it) == inset) {
800 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
801 lockInset(bv, inset);
804 if ((*it)->getInsetFromID(id)) {
805 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
807 return the_locking_inset->lockInsetInInset(bv, inset);
814 if (inset == cpar(bv)->getInset(cpos(bv))) {
815 lyxerr[Debug::INSETS] << "OK" << endl;
816 lockInset(bv, inset);
818 } else if (the_locking_inset && (the_locking_inset == inset)) {
819 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
820 lyxerr[Debug::INSETS] << "OK" << endl;
821 inset_x = cx(bv) - top_x + drawTextXOffset;
822 inset_y = cy(bv) + drawTextYOffset;
824 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
826 } else if (the_locking_inset) {
827 lyxerr[Debug::INSETS] << "MAYBE" << endl;
828 return the_locking_inset->lockInsetInInset(bv, inset);
830 lyxerr[Debug::INSETS] << "NOT OK" << endl;
835 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
838 if (!the_locking_inset)
840 if (the_locking_inset == inset) {
841 the_locking_inset->insetUnlock(bv);
842 getLyXText(bv)->updateInset(bv, inset);
843 the_locking_inset = 0;
845 moveRight(bv, false);
846 old_par = 0; // force layout setting
850 updateLocal(bv, CURSOR, false);
853 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
857 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
859 if (!autoBreakRows && par->next())
860 collapseParagraphs(bv->buffer()->params);
868 if (!the_locking_inset) {
869 bool found = lt->updateInset(bv, inset);
873 setUpdateStatus(bv, NONE);
876 if (the_locking_inset != inset) {
877 bool found = the_locking_inset->updateInsetInInset(bv, inset);
881 setUpdateStatus(bv, CURSOR_PAR);
884 bool found = lt->updateInset(bv, inset);
888 setUpdateStatus(bv, CURSOR_PAR);
889 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
890 inset_x = cx(bv) - top_x + drawTextXOffset;
891 inset_y = cy(bv) + drawTextYOffset;
898 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
902 // use this to check mouse motion for selection!
906 int tmp_x = x - drawTextXOffset;
907 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
908 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
911 if (the_locking_inset) {
912 if (the_locking_inset == inset) {
913 the_locking_inset->insetButtonPress(bv,
917 no_selection = false;
920 // otherwise unlock the_locking_inset and lock the new inset
921 the_locking_inset->insetUnlock(bv);
922 inset_x = cx(bv) - top_x + drawTextXOffset;
923 inset_y = cy(bv) + drawTextYOffset;
924 the_locking_inset = 0;
925 inset->insetButtonPress(bv, x - inset_x,
926 y - inset_y, button);
927 // inset->edit(bv, x - inset_x, y - inset_y, button);
928 if (the_locking_inset)
929 updateLocal(bv, CURSOR, false);
930 no_selection = false;
933 // otherwise only unlock the_locking_inset
934 the_locking_inset->insetUnlock(bv);
935 the_locking_inset = 0;
937 if (bv->theLockingInset()) {
938 if (isHighlyEditableInset(inset)) {
939 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
940 inset_x = cx(bv) - top_x + drawTextXOffset;
941 inset_y = cy(bv) + drawTextYOffset;
942 inset_pos = cpos(bv);
943 inset_par = cpar(bv);
944 inset_boundary = cboundary(bv);
945 the_locking_inset = uinset;
946 uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
948 uinset->edit(bv, x - inset_x, y - inset_y, 0);
949 if (the_locking_inset)
950 updateLocal(bv, CURSOR, false);
951 no_selection = false;
955 if (!inset) { // && (button == 2)) {
956 bool paste_internally = false;
957 if ((button == 2) && getLyXText(bv)->selection.set()) {
958 localDispatch(bv, LFUN_COPY, "");
959 paste_internally = true;
967 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
969 // set the selection cursor!
970 lt->selection.cursor = lt->cursor;
971 lt->cursor.x_fix(lt->cursor.x());
973 if (lt->selection.set()) {
974 lt->clearSelection();
977 updateLocal(bv, FULL, false);
979 lt->clearSelection();
982 updateLocal(bv, CURSOR, false);
984 bv->owner()->setLayout(cpar(bv)->layout());
986 // Insert primary selection with middle mouse
987 // if there is a local selection in the current buffer,
990 if (paste_internally)
991 localDispatch(bv, LFUN_PASTE, "");
993 localDispatch(bv, LFUN_PASTESELECTION,
997 getLyXText(bv)->clearSelection();
1000 no_selection = false;
1004 bool InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
1006 if (the_locking_inset) {
1007 return the_locking_inset->insetButtonRelease(bv,
1008 x - inset_x, y - inset_y,
1011 int tmp_x = x - drawTextXOffset;
1012 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
1013 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1016 if (isHighlyEditableInset(inset)) {
1017 ret = inset->insetButtonRelease(bv, x - inset_x,
1018 y - inset_y, button);
1020 inset_x = cx(bv) - top_x + drawTextXOffset;
1021 inset_y = cy(bv) + drawTextYOffset;
1022 ret = inset->insetButtonRelease(bv, x - inset_x,
1023 y - inset_y, button);
1024 inset->edit(bv, x - inset_x,
1025 y - inset_y, button);
1027 updateLocal(bv, CURSOR_PAR, false);
1033 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
1035 if (no_selection || ((mouse_x == x) && (mouse_y == y)))
1037 if (the_locking_inset) {
1038 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1044 lt = getLyXText(bv);
1047 hideInsetCursor(bv);
1048 LyXCursor cur = lt->cursor;
1049 lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1050 if (cur == lt->cursor) {
1055 lt->setSelection(bv);
1056 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1057 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1061 updateLocal(bv, SELECTION, false);
1063 showInsetCursor(bv);
1067 void InsetText::insetKeyPress(XKeyEvent * xke)
1069 if (the_locking_inset) {
1070 the_locking_inset->insetKeyPress(xke);
1076 UpdatableInset::RESULT
1077 InsetText::localDispatch(BufferView * bv,
1078 kb_action action, string const & arg)
1080 bool was_empty = par->size() == 0 && !par->next();
1081 no_selection = false;
1082 UpdatableInset::RESULT
1083 result= UpdatableInset::localDispatch(bv, action, arg);
1084 if (result != UNDISPATCHED) {
1088 result = DISPATCHED;
1089 if ((action < 0) && arg.empty())
1092 if (the_locking_inset) {
1093 result = the_locking_inset->localDispatch(bv, action, arg);
1094 if (result == DISPATCHED_NOUPDATE)
1096 else if (result == DISPATCHED) {
1097 updateLocal(bv, CURSOR_PAR, false);
1099 } else if (result >= FINISHED) {
1101 case FINISHED_RIGHT:
1102 moveRightIntern(bv, false, false);
1103 result = DISPATCHED;
1106 if ((result = moveUp(bv)) >= FINISHED) {
1107 updateLocal(bv, CURSOR, false);
1108 bv->unlockInset(this);
1112 if ((result = moveDown(bv)) >= FINISHED) {
1113 updateLocal(bv, CURSOR, false);
1114 bv->unlockInset(this);
1118 result = DISPATCHED;
1121 the_locking_inset = 0;
1122 #ifdef WITH_WARNINGS
1123 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
1128 hideInsetCursor(bv);
1131 lt = getLyXText(bv);
1135 int updflag = false;
1138 case LFUN_SELFINSERT:
1139 if (bv->buffer()->isReadonly()) {
1140 // setErrorMessage(N_("Document is read only"));
1144 /* Automatically delete the currently selected
1145 * text and replace it with what is being
1146 * typed in now. Depends on lyxrc settings
1147 * "auto_region_delete", which defaults to
1150 setUndo(bv, Undo::INSERT,
1151 lt->cursor.par(), lt->cursor.par()->next());
1153 if (lyxrc.auto_region_delete) {
1154 if (lt->selection.set()) {
1155 lt->cutSelection(bv, false);
1158 lt->clearSelection();
1159 for (string::size_type i = 0; i < arg.length(); ++i) {
1160 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1163 lt->selection.cursor = lt->cursor;
1164 updwhat = CURSOR_PAR;
1166 result = DISPATCHED_NOUPDATE;
1168 // --- Cursor Movements -----------------------------------
1171 moveRight(bv, false, true);
1172 lt->setSelection(bv);
1173 updwhat = SELECTION;
1176 result = moveRight(bv);
1182 moveLeft(bv, false, true);
1183 lt->setSelection(bv);
1184 updwhat = SELECTION;
1188 result = moveLeft(bv);
1194 lt->setSelection(bv);
1195 updwhat = SELECTION;
1199 result = moveDown(bv);
1205 lt->setSelection(bv);
1206 updwhat = SELECTION;
1210 result = moveUp(bv);
1222 case LFUN_BACKSPACE: {
1223 setUndo(bv, Undo::DELETE,
1224 lt->cursor.par(), lt->cursor.par()->next());
1225 if (lt->selection.set())
1226 lt->cutSelection(bv);
1229 updwhat = CURSOR_PAR;
1235 setUndo(bv, Undo::DELETE,
1236 lt->cursor.par(), lt->cursor.par()->next());
1237 if (lt->selection.set()) {
1238 lt->cutSelection(bv);
1242 updwhat = CURSOR_PAR;
1248 setUndo(bv, Undo::DELETE,
1249 lt->cursor.par(), lt->cursor.par()->next());
1250 lt->cutSelection(bv);
1251 updwhat = CURSOR_PAR;
1258 lt->copySelection(bv);
1259 updwhat = CURSOR_PAR;
1261 case LFUN_PASTESELECTION:
1263 string const clip(bv->getClipboard());
1267 if (arg == "paragraph") {
1268 lt->insertStringAsParagraphs(bv, clip);
1270 lt->insertStringAsLines(bv, clip);
1272 updwhat = CURSOR_PAR;
1277 if (!autoBreakRows) {
1279 if (CutAndPaste::nrOfParagraphs() > 1) {
1280 Alert::alert(_("Impossible operation"),
1281 _("Cannot include more than one paragraph!"),
1286 setUndo(bv, Undo::INSERT,
1287 lt->cursor.par(), lt->cursor.par()->next());
1288 lt->pasteSelection(bv);
1289 updwhat = CURSOR_PAR;
1294 case LFUN_BREAKPARAGRAPH:
1295 if (!autoBreakRows) {
1296 result = DISPATCHED;
1299 lt->breakParagraph(bv, 0);
1303 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1304 if (!autoBreakRows) {
1305 result = DISPATCHED;
1308 lt->breakParagraph(bv, 1);
1313 case LFUN_BREAKLINE: {
1314 if (!autoBreakRows) {
1315 result = DISPATCHED;
1318 setUndo(bv, Undo::INSERT,
1319 lt->cursor.par(), lt->cursor.par()->next());
1320 lt->insertChar(bv, Paragraph::META_NEWLINE);
1321 updwhat = CURSOR_PAR;
1327 // do not set layouts on non breakable textinsets
1328 if (autoBreakRows) {
1329 string cur_layout = cpar(bv)->layout();
1331 // Derive layout number from given argument (string)
1332 // and current buffer's textclass (number). */
1333 textclass_type tclass = bv->buffer()->params.textclass;
1334 string layout = arg;
1335 bool hasLayout = textclasslist[tclass].hasLayout(layout);
1337 // If the entry is obsolete, use the new one instead.
1339 string const & obs =
1340 textclasslist[tclass][layout].
1346 // see if we found the layout number:
1348 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1349 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1353 if (cur_layout != layout) {
1354 cur_layout = layout;
1355 lt->setLayout(bv, layout);
1356 bv->owner()->setLayout(cpar(bv)->layout());
1357 updwhat = CURSOR_PAR;
1361 // reset the layout box
1362 bv->owner()->setLayout(cpar(bv)->layout());
1365 case LFUN_PARAGRAPH_SPACING:
1366 // This one is absolutely not working. When fiddling with this
1367 // it also seems to me that the paragraphs inside the insettext
1368 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1370 Paragraph * par = lt->cursor.par();
1371 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1372 float cur_value = 1.0;
1373 if (cur_spacing == Spacing::Other) {
1374 cur_value = par->params().spacing().getValue();
1377 istringstream istr(arg.c_str());
1380 Spacing::Space new_spacing = cur_spacing;
1381 float new_value = cur_value;
1383 lyxerr << "Missing argument to `paragraph-spacing'"
1385 } else if (tmp == "single") {
1386 new_spacing = Spacing::Single;
1387 } else if (tmp == "onehalf") {
1388 new_spacing = Spacing::Onehalf;
1389 } else if (tmp == "double") {
1390 new_spacing = Spacing::Double;
1391 } else if (tmp == "other") {
1392 new_spacing = Spacing::Other;
1395 lyxerr << "new_value = " << tmpval << endl;
1398 } else if (tmp == "default") {
1399 new_spacing = Spacing::Default;
1401 lyxerr << _("Unknown spacing argument: ")
1404 if (cur_spacing != new_spacing || cur_value != new_value) {
1405 par->params().spacing(Spacing(new_spacing, new_value));
1406 updwhat = CURSOR_PAR;
1413 if (!bv->Dispatch(action, arg))
1414 result = UNDISPATCHED;
1421 updateLocal(bv, updwhat, updflag);
1422 /// If the action has deleted all text in the inset, we need to change the
1423 // language to the language of the surronding text.
1424 if (!was_empty && par->size() == 0 && !par->next()) {
1425 LyXFont font(LyXFont::ALL_IGNORE);
1426 font.setLanguage(bv->getParentLanguage(this));
1427 setFont(bv, font, false);
1430 if (result < FINISHED) {
1431 showInsetCursor(bv);
1433 bv->unlockInset(this);
1438 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1441 buf->latexParagraphs(os, par, 0, texrow);
1442 return texrow.rows();
1446 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1448 Paragraph * p = par;
1449 unsigned int lines = 0;
1452 string const tmp = buf->asciiParagraph(p, linelen, p->previous()==0);
1453 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1461 int InsetText::docbook(Buffer const * buf, ostream & os) const
1463 Paragraph * p = par;
1464 unsigned int lines = 0;
1466 vector<string> environment_stack(10);
1467 vector<string> environment_inner(10);
1469 int const command_depth = 0;
1472 Paragraph::depth_type depth = 0; // paragraph depth
1476 int desc_on = 0; // description mode
1478 LyXLayout const & style =
1479 textclasslist[buf->params.textclass][p->layout()];
1481 // environment tag closing
1482 for (; depth > p->params().depth(); --depth) {
1483 if (environment_inner[depth] != "!-- --") {
1484 item_name = "listitem";
1485 buf->sgmlCloseTag(os, command_depth + depth,
1487 if (environment_inner[depth] == "varlistentry")
1488 buf->sgmlCloseTag(os, depth+command_depth,
1489 environment_inner[depth]);
1491 buf->sgmlCloseTag(os, depth + command_depth,
1492 environment_stack[depth]);
1493 environment_stack[depth].erase();
1494 environment_inner[depth].erase();
1497 if (depth == p->params().depth()
1498 && environment_stack[depth] != style.latexname()
1499 && !environment_stack[depth].empty()) {
1500 if (environment_inner[depth] != "!-- --") {
1501 item_name= "listitem";
1502 buf->sgmlCloseTag(os, command_depth+depth,
1504 if (environment_inner[depth] == "varlistentry")
1505 buf->sgmlCloseTag(os,
1506 depth + command_depth,
1507 environment_inner[depth]);
1510 buf->sgmlCloseTag(os, depth + command_depth,
1511 environment_stack[depth]);
1513 environment_stack[depth].erase();
1514 environment_inner[depth].erase();
1517 // Write opening SGML tags.
1518 switch (style.latextype) {
1519 case LATEX_PARAGRAPH:
1520 buf->sgmlOpenTag(os, depth + command_depth,
1525 buf->sgmlError(p, 0,
1526 _("Error : LatexType Command not allowed here.\n"));
1530 case LATEX_ENVIRONMENT:
1531 case LATEX_ITEM_ENVIRONMENT:
1532 if (depth < p->params().depth()) {
1533 depth = p->params().depth();
1534 environment_stack[depth].erase();
1537 if (environment_stack[depth] != style.latexname()) {
1538 if (environment_stack.size() == depth + 1) {
1539 environment_stack.push_back("!-- --");
1540 environment_inner.push_back("!-- --");
1542 environment_stack[depth] = style.latexname();
1543 environment_inner[depth] = "!-- --";
1544 buf->sgmlOpenTag(os, depth + command_depth,
1545 environment_stack[depth]);
1547 if (environment_inner[depth] != "!-- --") {
1548 item_name= "listitem";
1549 buf->sgmlCloseTag(os,
1550 command_depth + depth,
1552 if (environment_inner[depth] == "varlistentry")
1553 buf->sgmlCloseTag(os,
1554 depth + command_depth,
1555 environment_inner[depth]);
1559 if (style.latextype == LATEX_ENVIRONMENT) {
1560 if (!style.latexparam().empty()) {
1561 if (style.latexparam() == "CDATA")
1564 buf->sgmlOpenTag(os, depth + command_depth,
1565 style.latexparam());
1570 desc_on = (style.labeltype == LABEL_MANUAL);
1573 environment_inner[depth]= "varlistentry";
1575 environment_inner[depth]= "listitem";
1577 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1578 environment_inner[depth]);
1582 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1586 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1591 buf->sgmlOpenTag(os, depth + command_depth,
1596 buf->simpleDocBookOnePar(os, p, desc_on,
1597 depth + 1 + command_depth);
1601 // write closing SGML tags
1602 switch (style.latextype) {
1603 case LATEX_ENVIRONMENT:
1604 if (!style.latexparam().empty()) {
1605 if (style.latexparam() == "CDATA")
1608 buf->sgmlCloseTag(os, depth + command_depth,
1609 style.latexparam());
1612 case LATEX_ITEM_ENVIRONMENT:
1613 if (desc_on == 1) break;
1615 buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1617 case LATEX_PARAGRAPH:
1618 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1621 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1627 for (int d = depth; d >= 0; --d) {
1628 if (!environment_stack[depth].empty()) {
1629 if (environment_inner[depth] != "!-- --") {
1630 item_name = "listitem";
1631 buf->sgmlCloseTag(os, command_depth + depth,
1633 if (environment_inner[depth] == "varlistentry")
1634 buf->sgmlCloseTag(os, depth + command_depth,
1635 environment_inner[depth]);
1638 buf->sgmlCloseTag(os, depth + command_depth,
1639 environment_stack[depth]);
1647 void InsetText::validate(LaTeXFeatures & features) const
1649 Paragraph * p = par;
1651 p->validate(features);
1657 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1659 if (textclasslist[buf->params.textclass][p->layout()].labeltype != LABEL_MANUAL)
1662 return p->beginningOfMainBody();
1666 void InsetText::getCursorPos(BufferView * bv,
1667 int & x, int & y) const
1669 if (the_locking_inset) {
1670 the_locking_inset->getCursorPos(bv, x, y);
1678 unsigned int InsetText::insetInInsetY()
1680 if (!the_locking_inset)
1683 return (inset_y + the_locking_inset->insetInInsetY());
1687 void InsetText::toggleInsetCursor(BufferView * bv)
1689 if (the_locking_inset) {
1690 the_locking_inset->toggleInsetCursor(bv);
1694 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1696 int const asc = lyxfont::maxAscent(font);
1697 int const desc = lyxfont::maxDescent(font);
1699 if (isCursorVisible())
1700 bv->hideLockedInsetCursor();
1702 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1703 toggleCursorVisible();
1707 void InsetText::showInsetCursor(BufferView * bv, bool show)
1709 if (the_locking_inset) {
1710 the_locking_inset->showInsetCursor(bv, show);
1713 if (!isCursorVisible()) {
1714 LyXFont const font =
1715 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1717 int const asc = lyxfont::maxAscent(font);
1718 int const desc = lyxfont::maxDescent(font);
1720 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1722 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1723 setCursorVisible(true);
1728 void InsetText::hideInsetCursor(BufferView * bv)
1730 if (isCursorVisible()) {
1731 bv->hideLockedInsetCursor();
1732 setCursorVisible(false);
1734 if (the_locking_inset)
1735 the_locking_inset->hideInsetCursor(bv);
1739 void InsetText::fitInsetCursor(BufferView * bv) const
1741 if (the_locking_inset) {
1742 the_locking_inset->fitInsetCursor(bv);
1745 LyXFont const font =
1746 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1748 int const asc = lyxfont::maxAscent(font);
1749 int const desc = lyxfont::maxDescent(font);
1751 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1755 UpdatableInset::RESULT
1756 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1758 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1759 return moveLeftIntern(bv, false, activate_inset, selecting);
1761 return moveRightIntern(bv, false, activate_inset, selecting);
1765 UpdatableInset::RESULT
1766 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1768 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1769 return moveRightIntern(bv, true, activate_inset, selecting);
1771 return moveLeftIntern(bv, true, activate_inset, selecting);
1775 UpdatableInset::RESULT
1776 InsetText::moveRightIntern(BufferView * bv, bool behind,
1777 bool activate_inset, bool selecting)
1779 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1780 return FINISHED_RIGHT;
1781 if (activate_inset && checkAndActivateInset(bv, behind))
1783 getLyXText(bv)->cursorRight(bv);
1785 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1786 return DISPATCHED_NOUPDATE;
1790 UpdatableInset::RESULT
1791 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1792 bool activate_inset, bool selecting)
1794 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1796 getLyXText(bv)->cursorLeft(bv);
1798 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1799 if (activate_inset && checkAndActivateInset(bv, behind))
1801 return DISPATCHED_NOUPDATE;
1805 UpdatableInset::RESULT
1806 InsetText::moveUp(BufferView * bv)
1808 if (!crow(bv)->previous())
1810 getLyXText(bv)->cursorUp(bv);
1811 return DISPATCHED_NOUPDATE;
1815 UpdatableInset::RESULT
1816 InsetText::moveDown(BufferView * bv)
1818 if (!crow(bv)->next())
1819 return FINISHED_DOWN;
1820 getLyXText(bv)->cursorDown(bv);
1821 return DISPATCHED_NOUPDATE;
1825 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1827 if (the_locking_inset) {
1828 if (the_locking_inset->insetAllowed(inset))
1829 return the_locking_inset->insertInset(bv, inset);
1834 lt = getLyXText(bv);
1837 setUndo(bv, Undo::FINISH, lt->cursor.par(), lt->cursor.par()->next());
1839 inset->setOwner(this);
1840 hideInsetCursor(bv);
1841 lt->insertInset(bv, inset);
1845 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1851 bool InsetText::insetAllowed(Inset::Code code) const
1853 // in_insetAllowed is a really gross hack,
1854 // to allow us to call the owner's insetAllowed
1855 // without stack overflow, which can happen
1856 // when the owner uses InsetCollapsable::insetAllowed()
1858 if (in_insetAllowed)
1860 in_insetAllowed = true;
1861 if (the_locking_inset)
1862 ret = the_locking_inset->insetAllowed(code);
1864 ret = owner()->insetAllowed(code);
1865 in_insetAllowed = false;
1870 UpdatableInset * InsetText::getLockingInset() const
1872 return the_locking_inset ? the_locking_inset->getLockingInset() :
1873 const_cast<InsetText *>(this);
1877 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1881 if (the_locking_inset)
1882 return the_locking_inset->getFirstLockingInsetOfType(c);
1887 bool InsetText::showInsetDialog(BufferView * bv) const
1889 if (the_locking_inset)
1890 return the_locking_inset->showInsetDialog(bv);
1895 vector<string> const InsetText::getLabelList() const
1897 vector<string> label_list;
1899 Paragraph * tpar = par;
1901 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1902 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1903 for (; beg != end; ++beg) {
1904 vector<string> const l = (*beg)->getLabelList();
1905 label_list.insert(label_list.end(), l.begin(), l.end());
1907 tpar = tpar->next();
1913 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1916 if (the_locking_inset) {
1917 the_locking_inset->setFont(bv, font, toggleall, selectall);
1920 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
1921 getLyXText(bv)->setFont(bv, font, toggleall);
1926 lt = getLyXText(bv);
1929 if (lt->selection.set()) {
1930 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1934 lt->toggleFree(bv, font, toggleall);
1936 lt->clearSelection();
1938 bool flag = (selectall || lt->selection.set());
1942 updateLocal(bv, FULL, true);
1944 updateLocal(bv, CURSOR_PAR, true);
1948 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1950 if (cpar(bv)->isInset(cpos(bv))) {
1954 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1955 if (!isHighlyEditableInset(inset))
1957 LyXFont const font =
1958 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1960 x = inset->width(bv, font);
1961 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1964 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1966 //inset_x = cx(bv) - top_x + drawTextXOffset;
1967 //inset_y = cy(bv) + drawTextYOffset;
1968 inset->edit(bv, x, y, 0);
1969 if (!the_locking_inset)
1971 updateLocal(bv, CURSOR, false);
1978 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1981 x -= drawTextXOffset;
1983 int dummyy = y + insetAscent;
1984 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
1991 inset_x = cx(bv) - top_x + drawTextXOffset;
1992 inset_y = cy(bv) + drawTextYOffset;
1993 inset->edit(bv, x - inset_x, y - inset_y, button);
1994 if (!the_locking_inset)
1996 updateLocal(bv, CURSOR, false);
2003 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
2006 int w = UpdatableInset::getMaxWidth(bv, inset);
2011 w = w - top_x + owner()->x();
2014 w -= (2 * TEXT_TO_INSET_OFFSET);
2017 return UpdatableInset::getMaxWidth(bv, inset);
2022 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2024 // we have to unlock any locked inset otherwise we're in troubles
2025 the_locking_inset = 0;
2027 Paragraph * tmp = par->next();
2032 par = new Paragraph(*p, same_id);
2033 par->setInsetOwner(this);
2034 Paragraph * np = par;
2037 np->next(new Paragraph(*p, same_id));
2038 np->next()->previous(np);
2040 np->setInsetOwner(this);
2047 void InsetText::setText(string const & data)
2050 LyXFont font(LyXFont::ALL_SANE);
2051 for (unsigned int i=0; i < data.length(); ++i)
2052 par->insertChar(i, data[i], font);
2056 void InsetText::setAutoBreakRows(bool flag)
2058 if (flag != autoBreakRows) {
2059 autoBreakRows = flag;
2067 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2069 if (how != drawFrame_) {
2072 updateLocal(bv, DRAW_FRAME, false);
2077 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2079 if (frame_color != col) {
2082 updateLocal(bv, DRAW_FRAME, false);
2087 int InsetText::cx(BufferView * bv) const
2089 // we do nothing dangerous so we use a local cache
2090 LyXText * llt = getLyXText(bv);
2091 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2092 if (the_locking_inset) {
2093 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2095 if (font.isVisibleRightToLeft())
2096 x -= the_locking_inset->width(bv, font);
2102 int InsetText::cy(BufferView * bv) const
2105 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2109 pos_type InsetText::cpos(BufferView * bv) const
2111 return getLyXText(bv)->cursor.pos();
2115 Paragraph * InsetText::cpar(BufferView * bv) const
2117 return getLyXText(bv)->cursor.par();
2121 bool InsetText::cboundary(BufferView * bv) const
2123 return getLyXText(bv)->cursor.boundary();
2127 Row * InsetText::crow(BufferView * bv) const
2129 return getLyXText(bv)->cursor.row();
2133 LyXText * InsetText::getLyXText(BufferView const * lbv,
2134 bool const recursive) const
2136 if (!recursive && (cached_bview == lbv)) {
2137 LyXText * lt = cached_text.get();
2138 lyx::Assert(lt && lt->firstRow()->par() == par);
2142 // Super UGLY! (Lgb)
2143 BufferView * bv = const_cast<BufferView *>(lbv);
2146 Cache::iterator it = cache.find(bv);
2148 if (it != cache.end()) {
2151 } else if (do_resize) {
2152 resizeLyXText(do_resize);
2154 if (lt || !it->second.remove) {
2155 lyx::Assert(it->second.text.get());
2156 cached_text = it->second.text;
2157 if (recursive && the_locking_inset) {
2158 return the_locking_inset->getLyXText(bv, true);
2160 return cached_text.get();
2161 } else if (it->second.remove) {
2163 saveLyXTextState(it->second.text.get());
2169 // when we have to reinit the existing LyXText!
2171 it->second.text->init(bv);
2172 restoreLyXTextState(bv, it->second.text.get());
2173 it->second.remove = false;
2175 cached_text = it->second.text;
2176 if (the_locking_inset && recursive) {
2177 return the_locking_inset->getLyXText(bv);
2179 return cached_text.get();
2182 // we are here only if we don't have a BufferView * in the cache!!!
2184 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2185 cached_text->init(bv);
2186 restoreLyXTextState(bv, cached_text.get());
2188 cache.insert(make_pair(bv, cached_text));
2190 if (the_locking_inset && recursive) {
2191 return the_locking_inset->getLyXText(bv);
2193 return cached_text.get();
2197 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2201 Cache::iterator it = cache.find(bv);
2203 if (it == cache.end()) {
2207 lyx::Assert(it->second.text.get());
2209 it->second.remove = true;
2211 /// then remove all LyXText in text-insets
2212 Paragraph * p = par;
2213 for (; p; p = p->next()) {
2214 p->deleteInsetsLyXText(bv);
2220 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2223 // we cannot resize this because we are in use!
2224 // so do this on the next possible getLyXText()
2229 // lyxerr << "InsetText::resizeLyXText\n";
2230 if (!par->next() && !par->size()) { // no data, resize not neccessary!
2231 // we have to do this as a fixed width may have changed!
2232 LyXText * t = getLyXText(bv);
2233 saveLyXTextState(t);
2235 restoreLyXTextState(bv, t);
2238 // one endless line, resize normally not necessary
2239 if (!force && getMaxWidth(bv, this) < 0)
2242 Cache::iterator it = cache.find(bv);
2243 if (it == cache.end()) {
2246 lyx::Assert(it->second.text.get());
2248 LyXText * t = it->second.text.get();
2249 saveLyXTextState(t);
2250 for (Paragraph * p = par; p; p = p->next()) {
2251 p->resizeInsetsLyXText(bv);
2254 restoreLyXTextState(bv, t);
2255 if (the_locking_inset) {
2256 inset_x = cx(bv) - top_x + drawTextXOffset;
2257 inset_y = cy(bv) + drawTextYOffset;
2261 t->first_y = bv->screen()->topCursorVisible(t);
2264 updateLocal(bv, FULL, false);
2265 // this will scroll the screen such that the cursor becomes visible
2266 bv->updateScrollbar();
2268 need_update |= FULL;
2273 void InsetText::reinitLyXText() const
2276 // we cannot resize this because we are in use!
2277 // so do this on the next possible getLyXText()
2283 // lyxerr << "InsetText::reinitLyXText\n";
2284 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2285 lyx::Assert(it->second.text.get());
2287 LyXText * t = it->second.text.get();
2288 BufferView * bv = it->first;
2290 saveLyXTextState(t);
2291 for (Paragraph * p = par; p; p = p->next()) {
2292 p->resizeInsetsLyXText(bv);
2295 restoreLyXTextState(bv, t);
2296 if (the_locking_inset) {
2297 inset_x = cx(bv) - top_x + drawTextXOffset;
2298 inset_y = cy(bv) + drawTextYOffset;
2301 t->first_y = bv->screen()->topCursorVisible(t);
2304 updateLocal(bv, FULL, false);
2305 // this will scroll the screen such that the cursor becomes visible
2306 bv->updateScrollbar();
2314 void InsetText::removeNewlines()
2316 bool changed = false;
2318 for (Paragraph * p = par; p; p = p->next()) {
2319 for (int i = 0; i < p->size(); ++i) {
2320 if (p->getChar(i) == Paragraph::META_NEWLINE) {
2331 bool InsetText::nodraw() const
2333 if (the_locking_inset)
2334 return the_locking_inset->nodraw();
2335 return UpdatableInset::nodraw();
2339 int InsetText::scroll(bool recursive) const
2341 int sx = UpdatableInset::scroll(false);
2343 if (recursive && the_locking_inset)
2344 sx += the_locking_inset->scroll(recursive);
2350 bool InsetText::doClearArea() const
2352 return !locked || (need_update & (FULL|INIT));
2356 void InsetText::selectAll(BufferView * bv)
2358 getLyXText(bv)->cursorTop(bv);
2359 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2360 getLyXText(bv)->cursorBottom(bv);
2361 getLyXText(bv)->setSelection(bv);
2365 void InsetText::clearSelection(BufferView * bv)
2367 getLyXText(bv)->clearSelection();
2371 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2373 Painter & pain = bv->painter();
2375 int h = insetAscent + insetDescent;
2376 int ty = baseline - insetAscent;
2382 if ((ty + h) > pain.paperHeight())
2383 h = pain.paperHeight();
2384 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2385 w = pain.paperWidth();
2386 // w -= TEXT_TO_INSET_OFFSET;
2387 pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2390 frame_is_visible = false;
2394 Paragraph * InsetText::getParFromID(int id) const
2397 Paragraph * result = par;
2398 Paragraph * ires = 0;
2399 while (result && result->id() != id) {
2400 if ((ires = result->getParFromID(id)))
2402 result = result->next();
2406 Paragraph * tmp = par;
2408 if (tmp->id() == id) {
2411 Paragraph * tmp2 = tmp->getParFromID(id);
2422 Paragraph * InsetText::firstParagraph() const
2425 if (the_locking_inset)
2426 if ((result = the_locking_inset->firstParagraph()))
2432 Paragraph * InsetText::getFirstParagraph(int i) const
2434 return (i == 0) ? par : 0;
2438 LyXCursor const & InsetText::cursor(BufferView * bv) const
2440 if (the_locking_inset)
2441 return the_locking_inset->cursor(bv);
2442 return getLyXText(bv)->cursor;
2446 Paragraph * InsetText::paragraph() const
2452 void InsetText::paragraph(Paragraph * p)
2454 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2455 // caller of this function has to take care of it. This IS important
2456 // as we could have to insert a paragraph before this one and just
2457 // link the actual to a new ones next and set it with this function
2460 // set ourself as owner for all the paragraphs inserted!
2461 Paragraph * np = par;
2463 np->setInsetOwner(this);
2467 // redraw myself when asked for
2472 Inset * InsetText::getInsetFromID(int id_arg) const
2475 return const_cast<InsetText *>(this);
2477 Paragraph * lp = par;
2480 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2481 en = lp->inset_iterator_end();
2484 if ((*it)->id() == id_arg)
2486 Inset * in = (*it)->getInsetFromID(id_arg);
2496 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2502 lt = getLyXText(bv);
2505 if (the_locking_inset) {
2506 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2513 // we have to go on checking so move cusor to the next char
2514 lt->cursor.pos(lt->cursor.pos() + 1);
2516 str = lt->selectNextWordToSpellcheck(bv, value);
2518 bv->unlockInset(const_cast<InsetText *>(this));
2527 void InsetText::selectSelectedWord(BufferView * bv)
2529 if (the_locking_inset) {
2530 the_locking_inset->selectSelectedWord(bv);
2533 getLyXText(bv)->selectSelectedWord(bv);
2534 updateLocal(bv, SELECTION, false);
2538 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2540 if (the_locking_inset) {
2541 the_locking_inset->toggleSelection(bv, kill_selection);
2545 lt = getLyXText(bv);
2549 int x = top_x + TEXT_TO_INSET_OFFSET;
2551 Row * row = lt->firstRow();
2552 int y_offset = top_baseline - row->ascent_of_text();
2554 while ((row != 0) && ((y+row->height()) <= 0)) {
2561 if (need_update & SELECTION)
2563 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2569 bool InsetText::searchForward(BufferView * bv, string const & str,
2572 if (the_locking_inset) {
2573 if (the_locking_inset->searchForward(bv, str, cs, mw))
2577 lt = getLyXText(bv);
2580 Paragraph * lpar = lt->cursor.par();
2581 pos_type pos = lt->cursor.pos();
2582 if (pos < lpar->size() - 1)
2586 lpar = lpar->next();
2591 // we have to unlock ourself in this function by default!
2592 bv->unlockInset(const_cast<InsetText *>(this));
2595 lt->setCursor(bv, lpar, pos);
2599 if (LyXFind(bv, str, true, true, cs , mw)) {
2602 // we have to unlock ourself in this function by default!
2603 bv->unlockInset(const_cast<InsetText *>(this));
2607 bool InsetText::searchBackward(BufferView * bv, string const & str,
2610 if (the_locking_inset)
2611 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2613 if (LyXFind(bv, str, false, true, cs, mw)) {
2616 // we have to unlock ourself in this function by default!
2617 bv->unlockInset(const_cast<InsetText *>(this));
2622 bool InsetText::checkInsertChar(LyXFont & font)
2625 return owner()->checkInsertChar(font);
2630 void InsetText::collapseParagraphs(BufferParams const & bparams) const
2632 while(par->next()) {
2633 if (!par->isSeparator(par->size()-1))
2634 par->insertChar(par->size()-1, ' ');
2635 par->pasteParagraph(bparams);
2641 void InsetText::getDrawFont(LyXFont & font) const
2645 owner()->getDrawFont(font);