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"
41 #include "ParagraphParameters.h"
42 #include "undo_funcs.h"
45 #include "frontends/Alert.h"
46 #include "frontends/Dialogs.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;
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->setInsetOwner(this);
231 par->layout(old_layout);
238 Inset * InsetText::clone(Buffer const &, bool same_id) const
240 return new InsetText(*this, same_id);
244 void InsetText::write(Buffer const * buf, ostream & os) const
247 writeParagraphData(buf, os);
251 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
253 par->writeFile(buf, os, buf->params, 0);
257 void InsetText::read(Buffer const * buf, LyXLex & lex)
261 Paragraph * return_par = 0;
262 Paragraph::depth_type depth = 0;
263 LyXFont font(LyXFont::ALL_INHERIT);
269 token = lex.getString();
272 if (token == "\\end_inset") {
273 #ifndef NO_COMPABILITY
274 const_cast<Buffer*>(buf)->insertErtContents(par, pos, false);
279 if (const_cast<Buffer*>(buf)->
280 parseSingleLyXformat2Token(lex, par, return_par,
281 token, pos, depth, font)) {
282 // the_end read this should NEVER happen
283 lex.printError("\\the_end read in inset! Error in document!");
291 return_par->setInsetOwner(this);
292 return_par = return_par->next();
295 if (token != "\\end_inset") {
296 lex.printError("Missing \\end_inset at this point. "
303 int InsetText::ascent(BufferView * bv, LyXFont const &) const
305 insetAscent = getLyXText(bv)->firstRow()->ascent_of_text() +
306 TEXT_TO_INSET_OFFSET;
311 int InsetText::descent(BufferView * bv, LyXFont const &) const
313 LyXText * llt = getLyXText(bv);
314 insetDescent = llt->height - llt->firstRow()->ascent_of_text() +
315 TEXT_TO_INSET_OFFSET;
320 int InsetText::width(BufferView * bv, LyXFont const &) const
322 insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
323 (2 * TEXT_TO_INSET_OFFSET);
324 insetWidth = max(insetWidth, 10);
329 int InsetText::textWidth(BufferView * bv, bool fordraw) const
332 if (!autoBreakRows) {
335 w = getMaxWidth(bv, this);
338 return max(w - (2 * TEXT_TO_INSET_OFFSET),
339 (int)getLyXText(bv)->width);
343 return w - (2 * TEXT_TO_INSET_OFFSET);
347 void InsetText::draw(BufferView * bv, LyXFont const & f,
348 int baseline, float & x, bool cleared) const
353 Painter & pain = bv->painter();
355 // this is the first thing we have to ask because if the x pos
356 // changed we have to do a complete rebreak of the text as we
357 // may have few space to draw in. Well we should check on this too
359 if (top_x != int(x)) {
362 int nw = getMaxWidth(bv, this);
363 if (nw > 0 && old_max_width != nw) {
366 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
371 // call these methods so that insetWidth, insetAscent and
372 // insetDescent have the right values.
377 // repaint the background if needed
378 if (cleared && backgroundColor() != LColor::background) {
379 clearInset(bv, baseline, cleared);
382 // no draw is necessary !!!
383 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
384 top_baseline = baseline;
386 if (need_update & CLEAR_FRAME)
387 clearFrame(pain, cleared);
394 x += static_cast<float>(scroll());
396 // if top_x differs we did it already
397 if (!cleared && (old_x == int(x))
398 && ((need_update&(INIT|FULL)) || (top_baseline != baseline)
399 ||(last_drawn_width != insetWidth)))
401 // Condition necessary to eliminate bug 59 attachment 37
403 clearInset(bv, baseline, cleared);
407 frame_is_visible = false;
409 if (!cleared && (need_update == NONE)) {
411 drawFrame(pain, cleared);
415 top_baseline = baseline;
416 top_y = baseline - insetAscent;
418 if (last_drawn_width != insetWidth) {
420 clearInset(bv, baseline, cleared);
422 last_drawn_width = insetWidth;
425 if (the_locking_inset && (cpar(bv) == inset_par)
426 && (cpos(bv) == inset_pos)) {
427 inset_x = cix(bv) - top_x + drawTextXOffset;
428 inset_y = ciy(bv) + drawTextYOffset;
430 if (!cleared && (need_update == CURSOR)
431 && !getLyXText(bv)->selection.set()) {
432 drawFrame(pain, cleared);
442 x += TEXT_TO_INSET_OFFSET;
444 Row * row = lt->firstRow();
445 int y_offset = baseline - row->ascent_of_text();
446 int ph = pain.paperHeight();
449 while ((row != 0) && ((y+row->height()) <= 0)) {
451 first += row->height();
457 if (cleared || (need_update&(INIT|FULL))) {
460 while ((row != 0) && (yf < ph)) {
461 lt->getVisibleRow(bv, y+y_offset, int(x), row,
467 } else if (!locked) {
468 if (need_update & CURSOR) {
469 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
470 lt->clearSelection();
471 lt->selection.cursor = lt->cursor;
473 bv->screen()->update(lt, bv, y_offset, int(x));
476 if (need_update & SELECTION) {
477 bv->screen()->toggleToggle(lt, bv, y_offset, int(x));
478 } else if (need_update & CURSOR) {
479 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
480 lt->clearSelection();
481 lt->selection.cursor = lt->cursor;
483 bv->screen()->update(lt, bv, y_offset, int(x));
488 lt->status(bv, LyXText::UNCHANGED);
489 if ((drawFrame_ == ALWAYS) ||
490 ((cleared || (need_update != CURSOR_PAR)) &&
491 (drawFrame_ == LOCKED) && locked))
493 drawFrame(pain, cleared);
494 } else if (need_update & CLEAR_FRAME) {
495 clearFrame(pain, cleared);
498 x += insetWidth - TEXT_TO_INSET_OFFSET;
500 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
502 } else if (need_update != INIT) {
510 void InsetText::drawFrame(Painter & pain, bool cleared) const
512 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
513 if (!frame_is_visible || cleared) {
514 frame_x = top_x + ttoD2;
515 frame_y = top_baseline - insetAscent + ttoD2;
516 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
517 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
518 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
520 frame_is_visible = true;
525 void InsetText::clearFrame(Painter & pain, bool cleared) const
527 if (frame_is_visible) {
529 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
532 frame_is_visible = false;
537 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
540 if (reinit && owner()) {
542 owner()->update(bv, font, true);
547 if (reinit || need_update == INIT) {
549 // we should put this call where we set need_update to INIT!
552 owner()->update(bv, font, true);
557 if (!autoBreakRows && par->next())
558 collapseParagraphs(bv);
560 if (the_locking_inset) {
561 inset_x = cix(bv) - top_x + drawTextXOffset;
562 inset_y = ciy(bv) + drawTextYOffset;
563 the_locking_inset->update(bv, font, reinit);
571 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
574 lt->updateInset(bv, the_locking_inset);
576 if (lt->status() == LyXText::NEED_MORE_REFRESH)
584 void InsetText::setUpdateStatus(BufferView * bv, int what) const
586 // this does nothing dangerous so use only a localized buffer
587 LyXText * llt = getLyXText(bv);
590 // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
591 // if we don't break row so that we only have one row to update!
592 if ((llt->status() == LyXText::NEED_MORE_REFRESH) ||
594 (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
597 } else if (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
598 need_update |= CURSOR_PAR;
601 // this to not draw a selection when we redraw all of it!
602 if (need_update & CURSOR && !(need_update & SELECTION)) {
603 if (llt->selection.set())
605 llt->clearSelection();
610 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
612 if (!autoBreakRows && par->next())
613 collapseParagraphs(bv);
620 setUpdateStatus(bv, what);
621 bool flag = mark_dirty ||
622 (((need_update != CURSOR) && (need_update != NONE)) ||
623 (lt->status() != LyXText::UNCHANGED) || lt->selection.set());
624 if (!lt->selection.set())
625 lt->selection.cursor = lt->cursor;
629 // IMO this is not anymore needed as we do this in fitInsetCursor!
630 // and we always get "true" as returnvalue of this function in the
631 // case of a locking inset (Jug 20020412)
632 if (locked && (need_update & CURSOR) && bv->fitCursor())
638 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
640 if (need_update == CURSOR)
642 bv->owner()->showState();
643 bv->owner()->updateMenubar();
644 bv->owner()->updateToolbar();
645 if (old_par != cpar(bv)) {
646 bv->owner()->setLayout(cpar(bv)->layout());
652 string const InsetText::editMessage() const
654 return _("Opened Text Inset");
658 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
660 UpdatableInset::edit(bv, x, y, button);
662 if (!bv->lockInset(this)) {
663 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
667 the_locking_inset = 0;
668 inset_pos = inset_x = inset_y = 0;
669 inset_boundary = false;
672 int tmp_y = (y < 0) ? 0 : y;
678 if (!checkAndActivateInset(bv, x, tmp_y, button))
679 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
681 lt->clearSelection();
683 // If the inset is empty set the language of the current font to the
684 // language to the surronding text (if different).
685 if (par->size() == 0 && !par->next() &&
686 bv->getParentLanguage(this) != lt->current_font.language())
688 LyXFont font(LyXFont::ALL_IGNORE);
689 font.setLanguage(bv->getParentLanguage(this));
690 setFont(bv, font, false);
696 if (drawFrame_ == LOCKED)
697 code = CURSOR|DRAW_FRAME;
698 updateLocal(bv, code, false);
701 // Tell the paragraph dialog that we've entered an insettext.
702 bv->owner()->getDialogs()->updateParagraph();
706 void InsetText::edit(BufferView * bv, bool front)
708 UpdatableInset::edit(bv, front);
710 if (!bv->lockInset(this)) {
711 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
715 the_locking_inset = 0;
716 inset_pos = inset_x = inset_y = 0;
717 inset_boundary = false;
726 lt->setCursor(bv, par, 0);
731 // int const pos = (p->size() ? p->size()-1 : p->size());
732 lt->setCursor(bv, p, p->size());
734 lt->clearSelection();
736 // If the inset is empty set the language of the current font to the
737 // language to the surronding text (if different).
738 if (par->size() == 0 && !par->next() &&
739 bv->getParentLanguage(this) != lt->current_font.language()) {
740 LyXFont font(LyXFont::ALL_IGNORE);
741 font.setLanguage(bv->getParentLanguage(this));
742 setFont(bv, font, false);
747 if (drawFrame_ == LOCKED)
748 code = CURSOR|DRAW_FRAME;
749 updateLocal(bv, code, false);
754 void InsetText::insetUnlock(BufferView * bv)
756 if (the_locking_inset) {
757 the_locking_inset->insetUnlock(bv);
758 the_locking_inset = 0;
764 if (drawFrame_ == LOCKED)
765 code = CURSOR|CLEAR_FRAME;
773 if (lt->selection.set()) {
774 lt->clearSelection();
776 } else if (owner()) {
777 bv->owner()->setLayout(owner()->getLyXText(bv)
778 ->cursor.par()->layout());
780 bv->owner()->setLayout(bv->text->cursor.par()->layout());
781 // hack for deleteEmptyParMech
783 lt->setCursor(bv, par, 0);
784 } else if (par->next()) {
785 lt->setCursor(bv, par->next(), 0);
789 updateLocal(bv, code, false);
792 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
794 the_locking_inset = inset;
795 inset_x = cix(bv) - top_x + drawTextXOffset;
796 inset_y = ciy(bv) + drawTextYOffset;
797 inset_pos = cpos(bv);
798 inset_par = cpar(bv);
799 inset_boundary = cboundary(bv);
800 updateLocal(bv, CURSOR, false);
804 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
806 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
810 if (!the_locking_inset) {
812 int const id = inset->id();
814 Paragraph::inset_iterator it =
815 p->inset_iterator_begin();
816 Paragraph::inset_iterator const end =
817 p->inset_iterator_end();
818 for (; it != end; ++it) {
819 if ((*it) == inset) {
820 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
821 lockInset(bv, inset);
824 if ((*it)->getInsetFromID(id)) {
825 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
827 return the_locking_inset->lockInsetInInset(bv, inset);
834 if (inset == cpar(bv)->getInset(cpos(bv))) {
835 lyxerr[Debug::INSETS] << "OK" << endl;
836 lockInset(bv, inset);
838 } else if (the_locking_inset && (the_locking_inset == inset)) {
839 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
840 lyxerr[Debug::INSETS] << "OK" << endl;
841 inset_x = cix(bv) - top_x + drawTextXOffset;
842 inset_y = ciy(bv) + drawTextYOffset;
844 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
846 } else if (the_locking_inset) {
847 lyxerr[Debug::INSETS] << "MAYBE" << endl;
848 return the_locking_inset->lockInsetInInset(bv, inset);
850 lyxerr[Debug::INSETS] << "NOT OK" << endl;
855 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
858 if (!the_locking_inset)
860 if (the_locking_inset == inset) {
861 the_locking_inset->insetUnlock(bv);
862 getLyXText(bv)->updateInset(bv, inset);
863 the_locking_inset = 0;
865 moveRight(bv, false);
866 old_par = 0; // force layout setting
870 updateLocal(bv, CURSOR, false);
873 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
877 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
879 if (!autoBreakRows && par->next())
880 collapseParagraphs(bv);
888 if (inset->owner() != this) {
889 int ustat = CURSOR_PAR;
891 UpdatableInset * tl_inset = the_locking_inset;
893 found = tl_inset->updateInsetInInset(bv, inset);
895 tl_inset = static_cast<UpdatableInset *>(inset);
896 while(tl_inset->owner() && tl_inset->owner() != this)
897 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
898 if (!tl_inset->owner())
900 found = tl_inset->updateInsetInInset(bv, inset);
904 lt->updateInset(bv, tl_inset);
908 setUpdateStatus(bv, ustat);
911 bool found = lt->updateInset(bv, inset);
915 setUpdateStatus(bv, CURSOR_PAR);
916 if (the_locking_inset &&
917 cpar(bv) == inset_par && cpos(bv) == inset_pos)
919 inset_x = cix(bv) - top_x + drawTextXOffset;
920 inset_y = ciy(bv) + drawTextYOffset;
927 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
931 // use this to check mouse motion for selection!
935 int tmp_x = x - drawTextXOffset;
936 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
937 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
940 if (the_locking_inset) {
941 if (the_locking_inset == inset) {
942 the_locking_inset->insetButtonPress(bv,
950 // otherwise unlock the_locking_inset and lock the new inset
951 the_locking_inset->insetUnlock(bv);
952 inset_x = cix(bv) - top_x + drawTextXOffset;
953 inset_y = ciy(bv) + drawTextYOffset;
954 the_locking_inset = 0;
955 inset->insetButtonPress(bv, x - inset_x,
956 y - inset_y, button);
957 // inset->edit(bv, x - inset_x, y - inset_y, button);
958 if (the_locking_inset)
959 updateLocal(bv, CURSOR, false);
963 // otherwise only unlock the_locking_inset
964 the_locking_inset->insetUnlock(bv);
965 the_locking_inset = 0;
968 no_selection = false;
970 if (bv->theLockingInset()) {
971 if (isHighlyEditableInset(inset)) {
972 // We just have to lock the inset before calling a
974 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
975 if (!bv->lockInset(uinset)) {
976 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
978 inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
979 if (the_locking_inset)
980 updateLocal(bv, CURSOR, false);
984 if (!inset) { // && (button == 2)) {
985 bool paste_internally = false;
986 if ((button == 2) && getLyXText(bv)->selection.set()) {
987 localDispatch(bv, LFUN_COPY, "");
988 paste_internally = true;
995 int old_first_y = lt->first_y;
997 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
999 // set the selection cursor!
1000 lt->selection.cursor = lt->cursor;
1001 lt->cursor.x_fix(lt->cursor.x());
1003 if (lt->selection.set()) {
1004 lt->clearSelection();
1007 updateLocal(bv, FULL, false);
1009 lt->clearSelection();
1012 updateLocal(bv, CURSOR, false);
1014 bv->owner()->setLayout(cpar(bv)->layout());
1015 // we moved the view we cannot do mouse selection in this case!
1016 if (getLyXText(bv)->first_y != old_first_y)
1017 no_selection = true;
1019 // Insert primary selection with middle mouse
1020 // if there is a local selection in the current buffer,
1023 if (paste_internally)
1024 localDispatch(bv, LFUN_PASTE, "");
1026 localDispatch(bv, LFUN_PASTESELECTION,
1030 getLyXText(bv)->clearSelection();
1032 showInsetCursor(bv);
1036 bool InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
1038 no_selection = true;
1039 if (the_locking_inset) {
1040 return the_locking_inset->insetButtonRelease(bv,
1041 x - inset_x, y - inset_y,
1044 int tmp_x = x - drawTextXOffset;
1045 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
1046 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1049 if (isHighlyEditableInset(inset)) {
1050 ret = inset->insetButtonRelease(bv, x - inset_x,
1051 y - inset_y, button);
1053 inset_x = cix(bv) - top_x + drawTextXOffset;
1054 inset_y = ciy(bv) + drawTextYOffset;
1055 ret = inset->insetButtonRelease(bv, x - inset_x,
1056 y - inset_y, button);
1057 inset->edit(bv, x - inset_x,
1058 y - inset_y, button);
1060 updateLocal(bv, CURSOR_PAR, false);
1066 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
1068 if (the_locking_inset) {
1069 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1074 if (no_selection || ((mouse_x == x) && (mouse_y == y)))
1079 lt = getLyXText(bv);
1082 hideInsetCursor(bv);
1083 LyXCursor cur = lt->cursor;
1084 lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1085 if (cur == lt->cursor) {
1090 lt->setSelection(bv);
1091 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1092 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1096 updateLocal(bv, SELECTION, false);
1098 showInsetCursor(bv);
1102 void InsetText::insetKeyPress(XKeyEvent * xke)
1104 if (the_locking_inset) {
1105 the_locking_inset->insetKeyPress(xke);
1111 UpdatableInset::RESULT
1112 InsetText::localDispatch(BufferView * bv,
1113 kb_action action, string const & arg)
1115 bool was_empty = par->size() == 0 && !par->next();
1116 no_selection = false;
1117 UpdatableInset::RESULT
1118 result= UpdatableInset::localDispatch(bv, action, arg);
1119 if (result != UNDISPATCHED) {
1123 result = DISPATCHED;
1124 if ((action < 0) && arg.empty())
1127 if (the_locking_inset) {
1128 result = the_locking_inset->localDispatch(bv, action, arg);
1129 if (result == DISPATCHED_NOUPDATE)
1131 else if (result == DISPATCHED) {
1132 updateLocal(bv, CURSOR_PAR, false);
1134 } else if (result >= FINISHED) {
1136 case FINISHED_RIGHT:
1137 moveRightIntern(bv, false, false);
1138 result = DISPATCHED;
1141 if ((result = moveUp(bv)) >= FINISHED) {
1142 updateLocal(bv, CURSOR, false);
1143 bv->unlockInset(this);
1147 if ((result = moveDown(bv)) >= FINISHED) {
1148 updateLocal(bv, CURSOR, false);
1149 bv->unlockInset(this);
1153 result = DISPATCHED;
1156 the_locking_inset = 0;
1157 #ifdef WITH_WARNINGS
1158 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
1160 updateLocal(bv, CURSOR, false);
1164 hideInsetCursor(bv);
1167 lt = getLyXText(bv);
1171 int updflag = false;
1174 case LFUN_SELFINSERT:
1175 if (bv->buffer()->isReadonly()) {
1176 // setErrorMessage(N_("Document is read only"));
1180 /* Automatically delete the currently selected
1181 * text and replace it with what is being
1182 * typed in now. Depends on lyxrc settings
1183 * "auto_region_delete", which defaults to
1186 setUndo(bv, Undo::INSERT,
1187 lt->cursor.par(), lt->cursor.par()->next());
1189 if (lyxrc.auto_region_delete) {
1190 if (lt->selection.set()) {
1191 lt->cutSelection(bv, false);
1194 lt->clearSelection();
1195 for (string::size_type i = 0; i < arg.length(); ++i) {
1196 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1199 lt->selection.cursor = lt->cursor;
1200 updwhat = CURSOR | CURSOR_PAR;
1202 result = DISPATCHED_NOUPDATE;
1204 // --- Cursor Movements -----------------------------------
1207 moveRight(bv, false, true);
1208 lt->setSelection(bv);
1209 updwhat = SELECTION;
1212 result = moveRight(bv);
1218 moveLeft(bv, false, true);
1219 lt->setSelection(bv);
1220 updwhat = SELECTION;
1224 result = moveLeft(bv);
1230 lt->setSelection(bv);
1231 updwhat = SELECTION;
1235 result = moveDown(bv);
1241 lt->setSelection(bv);
1242 updwhat = SELECTION;
1246 result = moveUp(bv);
1258 case LFUN_BACKSPACE: {
1259 setUndo(bv, Undo::DELETE,
1260 lt->cursor.par(), lt->cursor.par()->next());
1261 if (lt->selection.set())
1262 lt->cutSelection(bv);
1265 updwhat = CURSOR_PAR;
1271 setUndo(bv, Undo::DELETE,
1272 lt->cursor.par(), lt->cursor.par()->next());
1273 if (lt->selection.set()) {
1274 lt->cutSelection(bv);
1278 updwhat = CURSOR_PAR;
1284 setUndo(bv, Undo::DELETE,
1285 lt->cursor.par(), lt->cursor.par()->next());
1286 lt->cutSelection(bv);
1287 updwhat = CURSOR_PAR;
1294 lt->copySelection(bv);
1295 updwhat = CURSOR_PAR;
1297 case LFUN_PASTESELECTION:
1299 string const clip(bv->getClipboard());
1303 if (arg == "paragraph") {
1304 lt->insertStringAsParagraphs(bv, clip);
1306 lt->insertStringAsLines(bv, clip);
1308 updwhat = CURSOR_PAR;
1313 if (!autoBreakRows) {
1315 if (CutAndPaste::nrOfParagraphs() > 1) {
1316 Alert::alert(_("Impossible operation"),
1317 _("Cannot include more than one paragraph!"),
1322 setUndo(bv, Undo::INSERT,
1323 lt->cursor.par(), lt->cursor.par()->next());
1324 lt->pasteSelection(bv);
1325 updwhat = CURSOR_PAR;
1330 case LFUN_BREAKPARAGRAPH:
1331 if (!autoBreakRows) {
1332 result = DISPATCHED;
1335 lt->breakParagraph(bv, 0);
1336 updwhat = CURSOR | FULL;
1339 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1340 if (!autoBreakRows) {
1341 result = DISPATCHED;
1344 lt->breakParagraph(bv, 1);
1345 updwhat = CURSOR | FULL;
1349 case LFUN_BREAKLINE: {
1350 if (!autoBreakRows) {
1351 result = DISPATCHED;
1354 setUndo(bv, Undo::INSERT,
1355 lt->cursor.par(), lt->cursor.par()->next());
1356 lt->insertChar(bv, Paragraph::META_NEWLINE);
1357 updwhat = CURSOR | CURSOR_PAR;
1363 // do not set layouts on non breakable textinsets
1364 if (autoBreakRows) {
1365 string cur_layout = cpar(bv)->layout();
1367 // Derive layout number from given argument (string)
1368 // and current buffer's textclass (number). */
1369 textclass_type tclass = bv->buffer()->params.textclass;
1370 string layout = arg;
1371 bool hasLayout = textclasslist[tclass].hasLayout(layout);
1373 // If the entry is obsolete, use the new one instead.
1375 string const & obs =
1376 textclasslist[tclass][layout].
1382 // see if we found the layout number:
1384 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1385 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1389 if (cur_layout != layout) {
1390 cur_layout = layout;
1391 lt->setLayout(bv, layout);
1392 bv->owner()->setLayout(cpar(bv)->layout());
1393 updwhat = CURSOR_PAR;
1397 // reset the layout box
1398 bv->owner()->setLayout(cpar(bv)->layout());
1401 case LFUN_PARAGRAPH_SPACING:
1402 // This one is absolutely not working. When fiddling with this
1403 // it also seems to me that the paragraphs inside the insettext
1404 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1406 Paragraph * par = lt->cursor.par();
1407 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1408 float cur_value = 1.0;
1409 if (cur_spacing == Spacing::Other) {
1410 cur_value = par->params().spacing().getValue();
1413 istringstream istr(arg.c_str());
1416 Spacing::Space new_spacing = cur_spacing;
1417 float new_value = cur_value;
1419 lyxerr << "Missing argument to `paragraph-spacing'"
1421 } else if (tmp == "single") {
1422 new_spacing = Spacing::Single;
1423 } else if (tmp == "onehalf") {
1424 new_spacing = Spacing::Onehalf;
1425 } else if (tmp == "double") {
1426 new_spacing = Spacing::Double;
1427 } else if (tmp == "other") {
1428 new_spacing = Spacing::Other;
1431 lyxerr << "new_value = " << tmpval << endl;
1434 } else if (tmp == "default") {
1435 new_spacing = Spacing::Default;
1437 lyxerr << _("Unknown spacing argument: ")
1440 if (cur_spacing != new_spacing || cur_value != new_value) {
1441 par->params().spacing(Spacing(new_spacing, new_value));
1442 updwhat = CURSOR_PAR;
1449 if (!bv->Dispatch(action, arg))
1450 result = UNDISPATCHED;
1457 updateLocal(bv, updwhat, updflag);
1458 /// If the action has deleted all text in the inset, we need to change the
1459 // language to the language of the surronding text.
1460 if (!was_empty && par->size() == 0 && !par->next()) {
1461 LyXFont font(LyXFont::ALL_IGNORE);
1462 font.setLanguage(bv->getParentLanguage(this));
1463 setFont(bv, font, false);
1466 if (result >= FINISHED)
1467 bv->unlockInset(this);
1469 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1470 result = DISPATCHED;
1475 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1478 buf->latexParagraphs(os, par, 0, texrow);
1479 return texrow.rows();
1483 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1485 Paragraph * p = par;
1486 unsigned int lines = 0;
1489 string const tmp = buf->asciiParagraph(p, linelen, p->previous()==0);
1490 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1498 int InsetText::docbook(Buffer const * buf, ostream & os) const
1500 Paragraph * p = par;
1501 unsigned int lines = 0;
1503 vector<string> environment_stack(10);
1504 vector<string> environment_inner(10);
1506 int const command_depth = 0;
1509 Paragraph::depth_type depth = 0; // paragraph depth
1513 int desc_on = 0; // description mode
1515 LyXLayout const & style =
1516 textclasslist[buf->params.textclass][p->layout()];
1518 // environment tag closing
1519 for (; depth > p->params().depth(); --depth) {
1520 if (environment_inner[depth] != "!-- --") {
1521 item_name = "listitem";
1522 buf->sgmlCloseTag(os, command_depth + depth,
1524 if (environment_inner[depth] == "varlistentry")
1525 buf->sgmlCloseTag(os, depth+command_depth,
1526 environment_inner[depth]);
1528 buf->sgmlCloseTag(os, depth + command_depth,
1529 environment_stack[depth]);
1530 environment_stack[depth].erase();
1531 environment_inner[depth].erase();
1534 if (depth == p->params().depth()
1535 && environment_stack[depth] != style.latexname()
1536 && !environment_stack[depth].empty()) {
1537 if (environment_inner[depth] != "!-- --") {
1538 item_name= "listitem";
1539 buf->sgmlCloseTag(os, command_depth+depth,
1541 if (environment_inner[depth] == "varlistentry")
1542 buf->sgmlCloseTag(os,
1543 depth + command_depth,
1544 environment_inner[depth]);
1547 buf->sgmlCloseTag(os, depth + command_depth,
1548 environment_stack[depth]);
1550 environment_stack[depth].erase();
1551 environment_inner[depth].erase();
1554 // Write opening SGML tags.
1555 switch (style.latextype) {
1556 case LATEX_PARAGRAPH:
1557 buf->sgmlOpenTag(os, depth + command_depth,
1562 buf->sgmlError(p, 0,
1563 _("Error : LatexType Command not allowed here.\n"));
1567 case LATEX_ENVIRONMENT:
1568 case LATEX_ITEM_ENVIRONMENT:
1569 if (depth < p->params().depth()) {
1570 depth = p->params().depth();
1571 environment_stack[depth].erase();
1574 if (environment_stack[depth] != style.latexname()) {
1575 if (environment_stack.size() == depth + 1) {
1576 environment_stack.push_back("!-- --");
1577 environment_inner.push_back("!-- --");
1579 environment_stack[depth] = style.latexname();
1580 environment_inner[depth] = "!-- --";
1581 buf->sgmlOpenTag(os, depth + command_depth,
1582 environment_stack[depth]);
1584 if (environment_inner[depth] != "!-- --") {
1585 item_name= "listitem";
1586 buf->sgmlCloseTag(os,
1587 command_depth + depth,
1589 if (environment_inner[depth] == "varlistentry")
1590 buf->sgmlCloseTag(os,
1591 depth + command_depth,
1592 environment_inner[depth]);
1596 if (style.latextype == LATEX_ENVIRONMENT) {
1597 if (!style.latexparam().empty()) {
1598 if (style.latexparam() == "CDATA")
1601 buf->sgmlOpenTag(os, depth + command_depth,
1602 style.latexparam());
1607 desc_on = (style.labeltype == LABEL_MANUAL);
1610 environment_inner[depth]= "varlistentry";
1612 environment_inner[depth]= "listitem";
1614 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1615 environment_inner[depth]);
1619 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1623 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1628 buf->sgmlOpenTag(os, depth + command_depth,
1633 buf->simpleDocBookOnePar(os, p, desc_on,
1634 depth + 1 + command_depth);
1638 // write closing SGML tags
1639 switch (style.latextype) {
1640 case LATEX_ENVIRONMENT:
1641 if (!style.latexparam().empty()) {
1642 if (style.latexparam() == "CDATA")
1645 buf->sgmlCloseTag(os, depth + command_depth,
1646 style.latexparam());
1649 case LATEX_ITEM_ENVIRONMENT:
1650 if (desc_on == 1) break;
1652 buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1654 case LATEX_PARAGRAPH:
1655 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1658 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1664 for (int d = depth; d >= 0; --d) {
1665 if (!environment_stack[depth].empty()) {
1666 if (environment_inner[depth] != "!-- --") {
1667 item_name = "listitem";
1668 buf->sgmlCloseTag(os, command_depth + depth,
1670 if (environment_inner[depth] == "varlistentry")
1671 buf->sgmlCloseTag(os, depth + command_depth,
1672 environment_inner[depth]);
1675 buf->sgmlCloseTag(os, depth + command_depth,
1676 environment_stack[depth]);
1684 void InsetText::validate(LaTeXFeatures & features) const
1686 Paragraph * p = par;
1688 p->validate(features);
1694 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1696 if (textclasslist[buf->params.textclass][p->layout()].labeltype != LABEL_MANUAL)
1699 return p->beginningOfMainBody();
1703 void InsetText::getCursorPos(BufferView * bv,
1704 int & x, int & y) const
1706 if (the_locking_inset) {
1707 the_locking_inset->getCursorPos(bv, x, y);
1715 unsigned int InsetText::insetInInsetY()
1717 if (!the_locking_inset)
1720 return (inset_y + the_locking_inset->insetInInsetY());
1724 void InsetText::toggleInsetCursor(BufferView * bv)
1726 if (the_locking_inset) {
1727 the_locking_inset->toggleInsetCursor(bv);
1731 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1733 int const asc = lyxfont::maxAscent(font);
1734 int const desc = lyxfont::maxDescent(font);
1736 if (isCursorVisible())
1737 bv->hideLockedInsetCursor();
1739 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1740 toggleCursorVisible();
1744 void InsetText::showInsetCursor(BufferView * bv, bool show)
1746 if (the_locking_inset) {
1747 the_locking_inset->showInsetCursor(bv, show);
1750 if (!isCursorVisible()) {
1751 LyXFont const font =
1752 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1754 int const asc = lyxfont::maxAscent(font);
1755 int const desc = lyxfont::maxDescent(font);
1757 bv->fitLockedInsetCursor(cx(bv), ciy(bv), asc, desc);
1759 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1760 setCursorVisible(true);
1765 void InsetText::hideInsetCursor(BufferView * bv)
1767 if (isCursorVisible()) {
1768 bv->hideLockedInsetCursor();
1769 setCursorVisible(false);
1771 if (the_locking_inset)
1772 the_locking_inset->hideInsetCursor(bv);
1776 void InsetText::fitInsetCursor(BufferView * bv) const
1778 if (the_locking_inset) {
1779 the_locking_inset->fitInsetCursor(bv);
1782 LyXFont const font =
1783 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1785 int const asc = lyxfont::maxAscent(font);
1786 int const desc = lyxfont::maxDescent(font);
1788 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1789 need_update |= FULL;
1793 UpdatableInset::RESULT
1794 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1796 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1797 return moveLeftIntern(bv, false, activate_inset, selecting);
1799 return moveRightIntern(bv, false, activate_inset, selecting);
1803 UpdatableInset::RESULT
1804 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1806 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1807 return moveRightIntern(bv, true, activate_inset, selecting);
1809 return moveLeftIntern(bv, true, activate_inset, selecting);
1813 UpdatableInset::RESULT
1814 InsetText::moveRightIntern(BufferView * bv, bool behind,
1815 bool activate_inset, bool selecting)
1817 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1818 return FINISHED_RIGHT;
1819 if (activate_inset && checkAndActivateInset(bv, behind))
1821 getLyXText(bv)->cursorRight(bv);
1823 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1824 return DISPATCHED_NOUPDATE;
1828 UpdatableInset::RESULT
1829 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1830 bool activate_inset, bool selecting)
1832 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1834 getLyXText(bv)->cursorLeft(bv);
1836 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1837 if (activate_inset && checkAndActivateInset(bv, behind))
1839 return DISPATCHED_NOUPDATE;
1843 UpdatableInset::RESULT
1844 InsetText::moveUp(BufferView * bv)
1846 if (!crow(bv)->previous())
1848 getLyXText(bv)->cursorUp(bv);
1849 return DISPATCHED_NOUPDATE;
1853 UpdatableInset::RESULT
1854 InsetText::moveDown(BufferView * bv)
1856 if (!crow(bv)->next())
1857 return FINISHED_DOWN;
1858 getLyXText(bv)->cursorDown(bv);
1859 return DISPATCHED_NOUPDATE;
1863 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1865 if (the_locking_inset) {
1866 if (the_locking_inset->insetAllowed(inset))
1867 return the_locking_inset->insertInset(bv, inset);
1870 inset->setOwner(this);
1871 hideInsetCursor(bv);
1872 getLyXText(bv)->insertInset(bv, inset);
1874 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1879 bool InsetText::insetAllowed(Inset::Code code) const
1881 // in_insetAllowed is a really gross hack,
1882 // to allow us to call the owner's insetAllowed
1883 // without stack overflow, which can happen
1884 // when the owner uses InsetCollapsable::insetAllowed()
1886 if (in_insetAllowed)
1888 in_insetAllowed = true;
1889 if (the_locking_inset)
1890 ret = the_locking_inset->insetAllowed(code);
1892 ret = owner()->insetAllowed(code);
1893 in_insetAllowed = false;
1898 UpdatableInset * InsetText::getLockingInset() const
1900 return the_locking_inset ? the_locking_inset->getLockingInset() :
1901 const_cast<InsetText *>(this);
1905 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1909 if (the_locking_inset)
1910 return the_locking_inset->getFirstLockingInsetOfType(c);
1915 bool InsetText::showInsetDialog(BufferView * bv) const
1917 if (the_locking_inset)
1918 return the_locking_inset->showInsetDialog(bv);
1923 vector<string> const InsetText::getLabelList() const
1925 vector<string> label_list;
1927 Paragraph * tpar = par;
1929 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1930 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1931 for (; beg != end; ++beg) {
1932 vector<string> const l = (*beg)->getLabelList();
1933 label_list.insert(label_list.end(), l.begin(), l.end());
1935 tpar = tpar->next();
1941 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1944 if (the_locking_inset) {
1945 the_locking_inset->setFont(bv, font, toggleall, selectall);
1948 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
1949 getLyXText(bv)->setFont(bv, font, toggleall);
1954 lt = getLyXText(bv);
1957 if (lt->selection.set()) {
1958 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1962 lt->toggleFree(bv, font, toggleall);
1964 lt->clearSelection();
1966 bool flag = (selectall || lt->selection.set());
1970 updateLocal(bv, FULL, true);
1972 updateLocal(bv, CURSOR_PAR, true);
1976 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1978 if (cpar(bv)->isInset(cpos(bv))) {
1982 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1983 if (!isHighlyEditableInset(inset))
1985 LyXFont const font =
1986 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1988 x = inset->width(bv, font);
1989 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1992 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1994 //inset_x = cx(bv) - top_x + drawTextXOffset;
1995 //inset_y = cy(bv) + drawTextYOffset;
1996 inset->edit(bv, x, y, 0);
1997 if (!the_locking_inset)
1999 updateLocal(bv, CURSOR, false);
2006 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
2009 x -= drawTextXOffset;
2011 int dummyy = y + insetAscent;
2012 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
2019 inset_x = cix(bv) - top_x + drawTextXOffset;
2020 inset_y = ciy(bv) + drawTextYOffset;
2021 inset->edit(bv, x - inset_x, y - inset_y, button);
2022 if (!the_locking_inset)
2024 updateLocal(bv, CURSOR, false);
2031 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
2034 int w = UpdatableInset::getMaxWidth(bv, inset);
2039 w = w - top_x + owner()->x();
2042 w -= (2 * TEXT_TO_INSET_OFFSET);
2045 return UpdatableInset::getMaxWidth(bv, inset);
2050 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2052 // we have to unlock any locked inset otherwise we're in troubles
2053 the_locking_inset = 0;
2055 Paragraph * tmp = par->next();
2060 par = new Paragraph(*p, same_id);
2061 par->setInsetOwner(this);
2062 Paragraph * np = par;
2065 np->next(new Paragraph(*p, same_id));
2066 np->next()->previous(np);
2068 np->setInsetOwner(this);
2075 void InsetText::setText(string const & data, LyXFont const & font)
2078 for (unsigned int i=0; i < data.length(); ++i)
2079 par->insertChar(i, data[i], font);
2084 void InsetText::setAutoBreakRows(bool flag)
2086 if (flag != autoBreakRows) {
2087 autoBreakRows = flag;
2095 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2097 if (how != drawFrame_) {
2100 updateLocal(bv, DRAW_FRAME, false);
2105 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2107 if (frame_color != col) {
2110 updateLocal(bv, DRAW_FRAME, false);
2115 int InsetText::cx(BufferView * bv) const
2117 // we do nothing dangerous so we use a local cache
2118 LyXText * llt = getLyXText(bv);
2119 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2120 if (the_locking_inset) {
2121 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2123 if (font.isVisibleRightToLeft())
2124 x -= the_locking_inset->width(bv, font);
2130 int InsetText::cix(BufferView * bv) const
2132 // we do nothing dangerous so we use a local cache
2133 LyXText * llt = getLyXText(bv);
2134 int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2135 if (the_locking_inset) {
2136 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2138 if (font.isVisibleRightToLeft())
2139 x -= the_locking_inset->width(bv, font);
2145 int InsetText::cy(BufferView * bv) const
2148 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2152 int InsetText::ciy(BufferView * bv) const
2155 return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2159 pos_type InsetText::cpos(BufferView * bv) const
2161 return getLyXText(bv)->cursor.pos();
2165 Paragraph * InsetText::cpar(BufferView * bv) const
2167 return getLyXText(bv)->cursor.par();
2171 bool InsetText::cboundary(BufferView * bv) const
2173 return getLyXText(bv)->cursor.boundary();
2177 Row * InsetText::crow(BufferView * bv) const
2179 return getLyXText(bv)->cursor.row();
2183 LyXText * InsetText::getLyXText(BufferView const * lbv,
2184 bool const recursive) const
2186 if (cached_bview == lbv) {
2187 if (recursive && the_locking_inset)
2188 return the_locking_inset->getLyXText(lbv, true);
2189 LyXText * lt = cached_text.get();
2190 lyx::Assert(lt && lt->firstRow()->par() == par);
2193 // Super UGLY! (Lgb)
2194 BufferView * bv = const_cast<BufferView *>(lbv);
2197 Cache::iterator it = cache.find(bv);
2199 if (it != cache.end()) {
2202 } else if (do_resize) {
2203 resizeLyXText(do_resize);
2205 if (lt || !it->second.remove) {
2206 lyx::Assert(it->second.text.get());
2207 cached_text = it->second.text;
2208 if (recursive && the_locking_inset) {
2209 return the_locking_inset->getLyXText(bv, true);
2211 return cached_text.get();
2212 } else if (it->second.remove) {
2214 saveLyXTextState(it->second.text.get());
2220 // when we have to reinit the existing LyXText!
2222 it->second.text->init(bv);
2223 restoreLyXTextState(bv, it->second.text.get());
2224 it->second.remove = false;
2226 cached_text = it->second.text;
2227 if (the_locking_inset && recursive) {
2228 return the_locking_inset->getLyXText(bv);
2230 return cached_text.get();
2233 // we are here only if we don't have a BufferView * in the cache!!!
2235 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2236 cached_text->init(bv);
2237 restoreLyXTextState(bv, cached_text.get());
2239 cache.insert(make_pair(bv, cached_text));
2241 if (the_locking_inset && recursive) {
2242 return the_locking_inset->getLyXText(bv);
2244 return cached_text.get();
2248 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2252 Cache::iterator it = cache.find(bv);
2254 if (it == cache.end()) {
2258 lyx::Assert(it->second.text.get());
2260 it->second.remove = true;
2262 /// then remove all LyXText in text-insets
2263 Paragraph * p = par;
2264 for (; p; p = p->next()) {
2265 p->deleteInsetsLyXText(bv);
2271 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2274 // we cannot resize this because we are in use!
2275 // so do this on the next possible getLyXText()
2280 // lyxerr << "InsetText::resizeLyXText\n";
2281 if (!par->next() && !par->size()) { // no data, resize not neccessary!
2282 // we have to do this as a fixed width may have changed!
2283 LyXText * t = getLyXText(bv);
2284 saveLyXTextState(t);
2286 restoreLyXTextState(bv, t);
2289 // one endless line, resize normally not necessary
2290 if (!force && getMaxWidth(bv, this) < 0)
2293 Cache::iterator it = cache.find(bv);
2294 if (it == cache.end()) {
2297 lyx::Assert(it->second.text.get());
2299 LyXText * t = it->second.text.get();
2300 saveLyXTextState(t);
2301 for (Paragraph * p = par; p; p = p->next()) {
2302 p->resizeInsetsLyXText(bv);
2305 restoreLyXTextState(bv, t);
2306 if (the_locking_inset) {
2307 inset_x = cix(bv) - top_x + drawTextXOffset;
2308 inset_y = ciy(bv) + drawTextYOffset;
2312 t->first_y = bv->screen()->topCursorVisible(t);
2315 updateLocal(bv, FULL, false);
2316 // this will scroll the screen such that the cursor becomes visible
2317 bv->updateScrollbar();
2319 need_update |= FULL;
2324 void InsetText::reinitLyXText() const
2327 // we cannot resize this because we are in use!
2328 // so do this on the next possible getLyXText()
2334 // lyxerr << "InsetText::reinitLyXText\n";
2335 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2336 lyx::Assert(it->second.text.get());
2338 LyXText * t = it->second.text.get();
2339 BufferView * bv = it->first;
2341 saveLyXTextState(t);
2342 for (Paragraph * p = par; p; p = p->next()) {
2343 p->resizeInsetsLyXText(bv);
2346 restoreLyXTextState(bv, t);
2347 if (the_locking_inset) {
2348 inset_x = cix(bv) - top_x + drawTextXOffset;
2349 inset_y = ciy(bv) + drawTextYOffset;
2352 t->first_y = bv->screen()->topCursorVisible(t);
2355 updateLocal(bv, FULL, false);
2356 // this will scroll the screen such that the cursor becomes visible
2357 bv->updateScrollbar();
2365 void InsetText::removeNewlines()
2367 bool changed = false;
2369 for (Paragraph * p = par; p; p = p->next()) {
2370 for (int i = 0; i < p->size(); ++i) {
2371 if (p->getChar(i) == Paragraph::META_NEWLINE) {
2382 bool InsetText::nodraw() const
2384 if (the_locking_inset)
2385 return the_locking_inset->nodraw();
2386 return UpdatableInset::nodraw();
2390 int InsetText::scroll(bool recursive) const
2392 int sx = UpdatableInset::scroll(false);
2394 if (recursive && the_locking_inset)
2395 sx += the_locking_inset->scroll(recursive);
2401 bool InsetText::doClearArea() const
2403 return !locked || (need_update & (FULL|INIT));
2407 void InsetText::selectAll(BufferView * bv)
2409 getLyXText(bv)->cursorTop(bv);
2410 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2411 getLyXText(bv)->cursorBottom(bv);
2412 getLyXText(bv)->setSelection(bv);
2416 void InsetText::clearSelection(BufferView * bv)
2418 getLyXText(bv)->clearSelection();
2422 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2424 Painter & pain = bv->painter();
2426 int h = insetAscent + insetDescent;
2427 int ty = baseline - insetAscent;
2433 if ((ty + h) > pain.paperHeight())
2434 h = pain.paperHeight();
2435 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2436 w = pain.paperWidth();
2437 // w -= TEXT_TO_INSET_OFFSET;
2438 pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2441 frame_is_visible = false;
2445 Paragraph * InsetText::getParFromID(int id) const
2448 Paragraph * result = par;
2449 Paragraph * ires = 0;
2450 while (result && result->id() != id) {
2451 if ((ires = result->getParFromID(id)))
2453 result = result->next();
2457 Paragraph * tmp = par;
2459 if (tmp->id() == id) {
2462 Paragraph * tmp2 = tmp->getParFromID(id);
2473 Paragraph * InsetText::firstParagraph() const
2476 if (the_locking_inset)
2477 if ((result = the_locking_inset->firstParagraph()))
2483 Paragraph * InsetText::getFirstParagraph(int i) const
2485 return (i == 0) ? par : 0;
2489 LyXCursor const & InsetText::cursor(BufferView * bv) const
2491 if (the_locking_inset)
2492 return the_locking_inset->cursor(bv);
2493 return getLyXText(bv)->cursor;
2497 Paragraph * InsetText::paragraph() const
2503 void InsetText::paragraph(Paragraph * p)
2505 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2506 // caller of this function has to take care of it. This IS important
2507 // as we could have to insert a paragraph before this one and just
2508 // link the actual to a new ones next and set it with this function
2511 // set ourself as owner for all the paragraphs inserted!
2512 Paragraph * np = par;
2514 np->setInsetOwner(this);
2518 // redraw myself when asked for
2523 Inset * InsetText::getInsetFromID(int id_arg) const
2526 return const_cast<InsetText *>(this);
2528 Paragraph * lp = par;
2531 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2532 en = lp->inset_iterator_end();
2535 if ((*it)->id() == id_arg)
2537 Inset * in = (*it)->getInsetFromID(id_arg);
2547 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2553 lt = getLyXText(bv);
2556 if (the_locking_inset) {
2557 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2564 // we have to go on checking so move cusor to the next char
2565 lt->cursor.pos(lt->cursor.pos() + 1);
2567 str = lt->selectNextWordToSpellcheck(bv, value);
2569 bv->unlockInset(const_cast<InsetText *>(this));
2578 void InsetText::selectSelectedWord(BufferView * bv)
2580 if (the_locking_inset) {
2581 the_locking_inset->selectSelectedWord(bv);
2584 getLyXText(bv)->selectSelectedWord(bv);
2585 updateLocal(bv, SELECTION, false);
2589 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2591 if (the_locking_inset) {
2592 the_locking_inset->toggleSelection(bv, kill_selection);
2596 lt = getLyXText(bv);
2600 int x = top_x + TEXT_TO_INSET_OFFSET;
2602 Row * row = lt->firstRow();
2603 int y_offset = top_baseline - row->ascent_of_text();
2605 while ((row != 0) && ((y+row->height()) <= 0)) {
2612 if (need_update & SELECTION)
2614 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2620 bool InsetText::searchForward(BufferView * bv, string const & str,
2623 if (the_locking_inset) {
2624 if (the_locking_inset->searchForward(bv, str, cs, mw))
2628 lt = getLyXText(bv);
2631 Paragraph * lpar = lt->cursor.par();
2632 pos_type pos = lt->cursor.pos();
2633 if (pos < lpar->size() - 1)
2637 lpar = lpar->next();
2642 // we have to unlock ourself in this function by default!
2643 bv->unlockInset(const_cast<InsetText *>(this));
2646 lt->setCursor(bv, lpar, pos);
2650 if (LyXFind(bv, str, true, true, cs , mw)) {
2653 // we have to unlock ourself in this function by default!
2654 bv->unlockInset(const_cast<InsetText *>(this));
2658 bool InsetText::searchBackward(BufferView * bv, string const & str,
2661 if (the_locking_inset)
2662 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2664 if (LyXFind(bv, str, false, true, cs, mw)) {
2667 // we have to unlock ourself in this function by default!
2668 bv->unlockInset(const_cast<InsetText *>(this));
2673 bool InsetText::checkInsertChar(LyXFont & font)
2676 return owner()->checkInsertChar(font);
2681 void InsetText::collapseParagraphs(BufferView * bv) const
2683 BufferParams const & bparams = bv->buffer()->params;
2684 LyXText * llt = getLyXText(bv);
2686 while(par->next()) {
2687 if (par->size() && par->next()->size() &&
2688 !par->isSeparator(par->size()-1))
2690 par->insertChar(par->size(), ' ');
2692 if (llt->selection.set()) {
2693 if (llt->selection.start.par() == par->next()) {
2694 llt->selection.start.par(par);
2695 llt->selection.start.pos(
2696 llt->selection.start.pos() + par->size());
2698 if (llt->selection.end.par() == par->next()) {
2699 llt->selection.end.par(par);
2700 llt->selection.end.pos(
2701 llt->selection.end.pos() + par->size());
2704 par->pasteParagraph(bparams);
2710 void InsetText::getDrawFont(LyXFont & font) const
2714 owner()->getDrawFont(font);
2718 void InsetText::appendParagraphs(BufferParams const & bparams,
2722 Paragraph * tmpbuf = newpar;
2723 Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2725 while (tmpbuf->next()) {
2726 tmpbuf = tmpbuf->next();
2727 lastbuffer->next(new Paragraph(*tmpbuf, false));
2728 lastbuffer->next()->previous(lastbuffer);
2729 lastbuffer = lastbuffer->next();
2732 while (lastbuffer->next())
2733 lastbuffer = lastbuffer->next();
2734 if (newpar->size() && lastbuffer->size() &&
2735 !lastbuffer->isSeparator(lastbuffer->size()-1))
2737 lastbuffer->insertChar(lastbuffer->size(), ' ');
2740 // make the buf exactly the same layout than our last paragraph
2741 buf->makeSameLayout(lastbuffer);
2744 lastbuffer->next(buf);
2745 buf->previous(lastbuffer);
2746 lastbuffer->pasteParagraph(bparams);