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 = cx(bv) - top_x + drawTextXOffset;
428 inset_y = cy(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->buffer()->params);
560 if (the_locking_inset) {
561 inset_x = cx(bv) - top_x + drawTextXOffset;
562 inset_y = cy(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->buffer()->params);
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;
628 if (locked && (need_update & CURSOR) && bv->fitCursor())
631 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
633 if (need_update == CURSOR)
635 bv->owner()->showState();
636 bv->owner()->updateMenubar();
637 bv->owner()->updateToolbar();
638 if (old_par != cpar(bv)) {
639 bv->owner()->setLayout(cpar(bv)->layout());
645 string const InsetText::editMessage() const
647 return _("Opened Text Inset");
651 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
653 UpdatableInset::edit(bv, x, y, button);
655 if (!bv->lockInset(this)) {
656 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
660 the_locking_inset = 0;
661 inset_pos = inset_x = inset_y = 0;
662 inset_boundary = false;
665 int tmp_y = (y < 0) ? 0 : y;
671 if (!checkAndActivateInset(bv, x, tmp_y, button))
672 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
674 lt->clearSelection();
676 // If the inset is empty set the language of the current font to the
677 // language to the surronding text (if different).
678 if (par->size() == 0 && !par->next() &&
679 bv->getParentLanguage(this) != lt->current_font.language())
681 LyXFont font(LyXFont::ALL_IGNORE);
682 font.setLanguage(bv->getParentLanguage(this));
683 setFont(bv, font, false);
689 if (drawFrame_ == LOCKED)
690 code = CURSOR|DRAW_FRAME;
691 updateLocal(bv, code, false);
694 // Tell the paragraph dialog that we've entered an insettext.
695 bv->owner()->getDialogs()->updateParagraph();
699 void InsetText::edit(BufferView * bv, bool front)
701 UpdatableInset::edit(bv, front);
703 if (!bv->lockInset(this)) {
704 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
708 the_locking_inset = 0;
709 inset_pos = inset_x = inset_y = 0;
710 inset_boundary = false;
719 lt->setCursor(bv, par, 0);
724 // int const pos = (p->size() ? p->size()-1 : p->size());
725 lt->setCursor(bv, p, p->size());
727 lt->clearSelection();
729 // If the inset is empty set the language of the current font to the
730 // language to the surronding text (if different).
731 if (par->size() == 0 && !par->next() &&
732 bv->getParentLanguage(this) != lt->current_font.language()) {
733 LyXFont font(LyXFont::ALL_IGNORE);
734 font.setLanguage(bv->getParentLanguage(this));
735 setFont(bv, font, false);
740 if (drawFrame_ == LOCKED)
741 code = CURSOR|DRAW_FRAME;
742 updateLocal(bv, code, false);
747 void InsetText::insetUnlock(BufferView * bv)
749 if (the_locking_inset) {
750 the_locking_inset->insetUnlock(bv);
751 the_locking_inset = 0;
757 if (drawFrame_ == LOCKED)
758 code = CURSOR|CLEAR_FRAME;
766 if (lt->selection.set()) {
767 lt->clearSelection();
769 } else if (owner()) {
770 bv->owner()->setLayout(owner()->getLyXText(bv)
771 ->cursor.par()->layout());
773 bv->owner()->setLayout(bv->text->cursor.par()->layout());
774 // hack for deleteEmptyParMech
776 lt->setCursor(bv, par, 0);
777 } else if (par->next()) {
778 lt->setCursor(bv, par->next(), 0);
782 updateLocal(bv, code, false);
785 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
787 the_locking_inset = inset;
788 inset_x = cx(bv) - top_x + drawTextXOffset;
789 inset_y = cy(bv) + drawTextYOffset;
790 inset_pos = cpos(bv);
791 inset_par = cpar(bv);
792 inset_boundary = cboundary(bv);
793 updateLocal(bv, CURSOR, false);
797 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
799 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
803 if (!the_locking_inset) {
805 int const id = inset->id();
807 Paragraph::inset_iterator it =
808 p->inset_iterator_begin();
809 Paragraph::inset_iterator const end =
810 p->inset_iterator_end();
811 for (; it != end; ++it) {
812 if ((*it) == inset) {
813 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
814 lockInset(bv, inset);
817 if ((*it)->getInsetFromID(id)) {
818 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
820 return the_locking_inset->lockInsetInInset(bv, inset);
827 if (inset == cpar(bv)->getInset(cpos(bv))) {
828 lyxerr[Debug::INSETS] << "OK" << endl;
829 lockInset(bv, inset);
831 } else if (the_locking_inset && (the_locking_inset == inset)) {
832 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
833 lyxerr[Debug::INSETS] << "OK" << endl;
834 inset_x = cx(bv) - top_x + drawTextXOffset;
835 inset_y = cy(bv) + drawTextYOffset;
837 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
839 } else if (the_locking_inset) {
840 lyxerr[Debug::INSETS] << "MAYBE" << endl;
841 return the_locking_inset->lockInsetInInset(bv, inset);
843 lyxerr[Debug::INSETS] << "NOT OK" << endl;
848 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
851 if (!the_locking_inset)
853 if (the_locking_inset == inset) {
854 the_locking_inset->insetUnlock(bv);
855 getLyXText(bv)->updateInset(bv, inset);
856 the_locking_inset = 0;
858 moveRight(bv, false);
859 old_par = 0; // force layout setting
863 updateLocal(bv, CURSOR, false);
866 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
870 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
872 if (!autoBreakRows && par->next())
873 collapseParagraphs(bv->buffer()->params);
881 if (inset->owner() != this) {
882 int ustat = CURSOR_PAR;
884 UpdatableInset * tl_inset = the_locking_inset;
886 found = tl_inset->updateInsetInInset(bv, inset);
888 tl_inset = static_cast<UpdatableInset *>(inset);
889 while(tl_inset->owner() && tl_inset->owner() != this)
890 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
891 if (!tl_inset->owner())
893 found = tl_inset->updateInsetInInset(bv, inset);
897 lt->updateInset(bv, tl_inset);
901 setUpdateStatus(bv, ustat);
904 bool found = lt->updateInset(bv, inset);
908 setUpdateStatus(bv, CURSOR_PAR);
909 if (the_locking_inset &&
910 cpar(bv) == inset_par && cpos(bv) == inset_pos)
912 inset_x = cx(bv) - top_x + drawTextXOffset;
913 inset_y = cy(bv) + drawTextYOffset;
920 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
924 // use this to check mouse motion for selection!
928 int tmp_x = x - drawTextXOffset;
929 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
930 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
933 if (the_locking_inset) {
934 if (the_locking_inset == inset) {
935 the_locking_inset->insetButtonPress(bv,
943 // otherwise unlock the_locking_inset and lock the new inset
944 the_locking_inset->insetUnlock(bv);
945 inset_x = cx(bv) - top_x + drawTextXOffset;
946 inset_y = cy(bv) + drawTextYOffset;
947 the_locking_inset = 0;
948 inset->insetButtonPress(bv, x - inset_x,
949 y - inset_y, button);
950 // inset->edit(bv, x - inset_x, y - inset_y, button);
951 if (the_locking_inset)
952 updateLocal(bv, CURSOR, false);
956 // otherwise only unlock the_locking_inset
957 the_locking_inset->insetUnlock(bv);
958 the_locking_inset = 0;
961 no_selection = false;
963 if (bv->theLockingInset()) {
964 if (isHighlyEditableInset(inset)) {
965 // We just have to lock the inset before calling a
967 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
968 if (!bv->lockInset(uinset)) {
969 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
971 inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
972 if (the_locking_inset)
973 updateLocal(bv, CURSOR, false);
977 if (!inset) { // && (button == 2)) {
978 bool paste_internally = false;
979 if ((button == 2) && getLyXText(bv)->selection.set()) {
980 localDispatch(bv, LFUN_COPY, "");
981 paste_internally = true;
988 int old_first_y = lt->first_y;
990 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
992 // set the selection cursor!
993 lt->selection.cursor = lt->cursor;
994 lt->cursor.x_fix(lt->cursor.x());
996 if (lt->selection.set()) {
997 lt->clearSelection();
1000 updateLocal(bv, FULL, false);
1002 lt->clearSelection();
1005 updateLocal(bv, CURSOR, false);
1007 bv->owner()->setLayout(cpar(bv)->layout());
1008 // we moved the view we cannot do mouse selection in this case!
1009 if (getLyXText(bv)->first_y != old_first_y)
1010 no_selection = true;
1012 // Insert primary selection with middle mouse
1013 // if there is a local selection in the current buffer,
1016 if (paste_internally)
1017 localDispatch(bv, LFUN_PASTE, "");
1019 localDispatch(bv, LFUN_PASTESELECTION,
1023 getLyXText(bv)->clearSelection();
1025 showInsetCursor(bv);
1029 bool InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
1031 no_selection = true;
1032 if (the_locking_inset) {
1033 return the_locking_inset->insetButtonRelease(bv,
1034 x - inset_x, y - inset_y,
1037 int tmp_x = x - drawTextXOffset;
1038 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
1039 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1042 if (isHighlyEditableInset(inset)) {
1043 ret = inset->insetButtonRelease(bv, x - inset_x,
1044 y - inset_y, button);
1046 inset_x = cx(bv) - top_x + drawTextXOffset;
1047 inset_y = cy(bv) + drawTextYOffset;
1048 ret = inset->insetButtonRelease(bv, x - inset_x,
1049 y - inset_y, button);
1050 inset->edit(bv, x - inset_x,
1051 y - inset_y, button);
1053 updateLocal(bv, CURSOR_PAR, false);
1059 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
1061 if (the_locking_inset) {
1062 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1067 if (no_selection || ((mouse_x == x) && (mouse_y == y)))
1072 lt = getLyXText(bv);
1075 hideInsetCursor(bv);
1076 LyXCursor cur = lt->cursor;
1077 lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1078 if (cur == lt->cursor) {
1083 lt->setSelection(bv);
1084 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1085 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1089 updateLocal(bv, SELECTION, false);
1091 showInsetCursor(bv);
1095 void InsetText::insetKeyPress(XKeyEvent * xke)
1097 if (the_locking_inset) {
1098 the_locking_inset->insetKeyPress(xke);
1104 UpdatableInset::RESULT
1105 InsetText::localDispatch(BufferView * bv,
1106 kb_action action, string const & arg)
1108 bool was_empty = par->size() == 0 && !par->next();
1109 no_selection = false;
1110 UpdatableInset::RESULT
1111 result= UpdatableInset::localDispatch(bv, action, arg);
1112 if (result != UNDISPATCHED) {
1116 result = DISPATCHED;
1117 if ((action < 0) && arg.empty())
1120 if (the_locking_inset) {
1121 result = the_locking_inset->localDispatch(bv, action, arg);
1122 if (result == DISPATCHED_NOUPDATE)
1124 else if (result == DISPATCHED) {
1125 updateLocal(bv, CURSOR_PAR, false);
1127 } else if (result >= FINISHED) {
1129 case FINISHED_RIGHT:
1130 moveRightIntern(bv, false, false);
1131 result = DISPATCHED;
1134 if ((result = moveUp(bv)) >= FINISHED) {
1135 updateLocal(bv, CURSOR, false);
1136 bv->unlockInset(this);
1140 if ((result = moveDown(bv)) >= FINISHED) {
1141 updateLocal(bv, CURSOR, false);
1142 bv->unlockInset(this);
1146 result = DISPATCHED;
1149 the_locking_inset = 0;
1150 #ifdef WITH_WARNINGS
1151 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
1153 updateLocal(bv, CURSOR, false);
1157 hideInsetCursor(bv);
1160 lt = getLyXText(bv);
1164 int updflag = false;
1167 case LFUN_SELFINSERT:
1168 if (bv->buffer()->isReadonly()) {
1169 // setErrorMessage(N_("Document is read only"));
1173 /* Automatically delete the currently selected
1174 * text and replace it with what is being
1175 * typed in now. Depends on lyxrc settings
1176 * "auto_region_delete", which defaults to
1179 setUndo(bv, Undo::INSERT,
1180 lt->cursor.par(), lt->cursor.par()->next());
1182 if (lyxrc.auto_region_delete) {
1183 if (lt->selection.set()) {
1184 lt->cutSelection(bv, false);
1187 lt->clearSelection();
1188 for (string::size_type i = 0; i < arg.length(); ++i) {
1189 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1192 lt->selection.cursor = lt->cursor;
1193 updwhat = CURSOR | CURSOR_PAR;
1195 result = DISPATCHED_NOUPDATE;
1197 // --- Cursor Movements -----------------------------------
1200 moveRight(bv, false, true);
1201 lt->setSelection(bv);
1202 updwhat = SELECTION;
1205 result = moveRight(bv);
1211 moveLeft(bv, false, true);
1212 lt->setSelection(bv);
1213 updwhat = SELECTION;
1217 result = moveLeft(bv);
1223 lt->setSelection(bv);
1224 updwhat = SELECTION;
1228 result = moveDown(bv);
1234 lt->setSelection(bv);
1235 updwhat = SELECTION;
1239 result = moveUp(bv);
1251 case LFUN_BACKSPACE: {
1252 setUndo(bv, Undo::DELETE,
1253 lt->cursor.par(), lt->cursor.par()->next());
1254 if (lt->selection.set())
1255 lt->cutSelection(bv);
1258 updwhat = CURSOR_PAR;
1264 setUndo(bv, Undo::DELETE,
1265 lt->cursor.par(), lt->cursor.par()->next());
1266 if (lt->selection.set()) {
1267 lt->cutSelection(bv);
1271 updwhat = CURSOR_PAR;
1277 setUndo(bv, Undo::DELETE,
1278 lt->cursor.par(), lt->cursor.par()->next());
1279 lt->cutSelection(bv);
1280 updwhat = CURSOR_PAR;
1287 lt->copySelection(bv);
1288 updwhat = CURSOR_PAR;
1290 case LFUN_PASTESELECTION:
1292 string const clip(bv->getClipboard());
1296 if (arg == "paragraph") {
1297 lt->insertStringAsParagraphs(bv, clip);
1299 lt->insertStringAsLines(bv, clip);
1301 updwhat = CURSOR_PAR;
1306 if (!autoBreakRows) {
1308 if (CutAndPaste::nrOfParagraphs() > 1) {
1309 Alert::alert(_("Impossible operation"),
1310 _("Cannot include more than one paragraph!"),
1315 setUndo(bv, Undo::INSERT,
1316 lt->cursor.par(), lt->cursor.par()->next());
1317 lt->pasteSelection(bv);
1318 updwhat = CURSOR_PAR;
1323 case LFUN_BREAKPARAGRAPH:
1324 if (!autoBreakRows) {
1325 result = DISPATCHED;
1328 lt->breakParagraph(bv, 0);
1329 updwhat = CURSOR | FULL;
1332 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1333 if (!autoBreakRows) {
1334 result = DISPATCHED;
1337 lt->breakParagraph(bv, 1);
1338 updwhat = CURSOR | FULL;
1342 case LFUN_BREAKLINE: {
1343 if (!autoBreakRows) {
1344 result = DISPATCHED;
1347 setUndo(bv, Undo::INSERT,
1348 lt->cursor.par(), lt->cursor.par()->next());
1349 lt->insertChar(bv, Paragraph::META_NEWLINE);
1350 updwhat = CURSOR | CURSOR_PAR;
1356 // do not set layouts on non breakable textinsets
1357 if (autoBreakRows) {
1358 string cur_layout = cpar(bv)->layout();
1360 // Derive layout number from given argument (string)
1361 // and current buffer's textclass (number). */
1362 textclass_type tclass = bv->buffer()->params.textclass;
1363 string layout = arg;
1364 bool hasLayout = textclasslist[tclass].hasLayout(layout);
1366 // If the entry is obsolete, use the new one instead.
1368 string const & obs =
1369 textclasslist[tclass][layout].
1375 // see if we found the layout number:
1377 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1378 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1382 if (cur_layout != layout) {
1383 cur_layout = layout;
1384 lt->setLayout(bv, layout);
1385 bv->owner()->setLayout(cpar(bv)->layout());
1386 updwhat = CURSOR_PAR;
1390 // reset the layout box
1391 bv->owner()->setLayout(cpar(bv)->layout());
1394 case LFUN_PARAGRAPH_SPACING:
1395 // This one is absolutely not working. When fiddling with this
1396 // it also seems to me that the paragraphs inside the insettext
1397 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1399 Paragraph * par = lt->cursor.par();
1400 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1401 float cur_value = 1.0;
1402 if (cur_spacing == Spacing::Other) {
1403 cur_value = par->params().spacing().getValue();
1406 istringstream istr(arg.c_str());
1409 Spacing::Space new_spacing = cur_spacing;
1410 float new_value = cur_value;
1412 lyxerr << "Missing argument to `paragraph-spacing'"
1414 } else if (tmp == "single") {
1415 new_spacing = Spacing::Single;
1416 } else if (tmp == "onehalf") {
1417 new_spacing = Spacing::Onehalf;
1418 } else if (tmp == "double") {
1419 new_spacing = Spacing::Double;
1420 } else if (tmp == "other") {
1421 new_spacing = Spacing::Other;
1424 lyxerr << "new_value = " << tmpval << endl;
1427 } else if (tmp == "default") {
1428 new_spacing = Spacing::Default;
1430 lyxerr << _("Unknown spacing argument: ")
1433 if (cur_spacing != new_spacing || cur_value != new_value) {
1434 par->params().spacing(Spacing(new_spacing, new_value));
1435 updwhat = CURSOR_PAR;
1442 if (!bv->Dispatch(action, arg))
1443 result = UNDISPATCHED;
1450 updateLocal(bv, updwhat, updflag);
1451 /// If the action has deleted all text in the inset, we need to change the
1452 // language to the language of the surronding text.
1453 if (!was_empty && par->size() == 0 && !par->next()) {
1454 LyXFont font(LyXFont::ALL_IGNORE);
1455 font.setLanguage(bv->getParentLanguage(this));
1456 setFont(bv, font, false);
1459 if (result >= FINISHED)
1460 bv->unlockInset(this);
1462 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1463 result = DISPATCHED;
1468 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1471 buf->latexParagraphs(os, par, 0, texrow);
1472 return texrow.rows();
1476 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1478 Paragraph * p = par;
1479 unsigned int lines = 0;
1482 string const tmp = buf->asciiParagraph(p, linelen, p->previous()==0);
1483 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1491 int InsetText::docbook(Buffer const * buf, ostream & os) const
1493 Paragraph * p = par;
1494 unsigned int lines = 0;
1496 vector<string> environment_stack(10);
1497 vector<string> environment_inner(10);
1499 int const command_depth = 0;
1502 Paragraph::depth_type depth = 0; // paragraph depth
1506 int desc_on = 0; // description mode
1508 LyXLayout const & style =
1509 textclasslist[buf->params.textclass][p->layout()];
1511 // environment tag closing
1512 for (; depth > p->params().depth(); --depth) {
1513 if (environment_inner[depth] != "!-- --") {
1514 item_name = "listitem";
1515 buf->sgmlCloseTag(os, command_depth + depth,
1517 if (environment_inner[depth] == "varlistentry")
1518 buf->sgmlCloseTag(os, depth+command_depth,
1519 environment_inner[depth]);
1521 buf->sgmlCloseTag(os, depth + command_depth,
1522 environment_stack[depth]);
1523 environment_stack[depth].erase();
1524 environment_inner[depth].erase();
1527 if (depth == p->params().depth()
1528 && environment_stack[depth] != style.latexname()
1529 && !environment_stack[depth].empty()) {
1530 if (environment_inner[depth] != "!-- --") {
1531 item_name= "listitem";
1532 buf->sgmlCloseTag(os, command_depth+depth,
1534 if (environment_inner[depth] == "varlistentry")
1535 buf->sgmlCloseTag(os,
1536 depth + command_depth,
1537 environment_inner[depth]);
1540 buf->sgmlCloseTag(os, depth + command_depth,
1541 environment_stack[depth]);
1543 environment_stack[depth].erase();
1544 environment_inner[depth].erase();
1547 // Write opening SGML tags.
1548 switch (style.latextype) {
1549 case LATEX_PARAGRAPH:
1550 buf->sgmlOpenTag(os, depth + command_depth,
1555 buf->sgmlError(p, 0,
1556 _("Error : LatexType Command not allowed here.\n"));
1560 case LATEX_ENVIRONMENT:
1561 case LATEX_ITEM_ENVIRONMENT:
1562 if (depth < p->params().depth()) {
1563 depth = p->params().depth();
1564 environment_stack[depth].erase();
1567 if (environment_stack[depth] != style.latexname()) {
1568 if (environment_stack.size() == depth + 1) {
1569 environment_stack.push_back("!-- --");
1570 environment_inner.push_back("!-- --");
1572 environment_stack[depth] = style.latexname();
1573 environment_inner[depth] = "!-- --";
1574 buf->sgmlOpenTag(os, depth + command_depth,
1575 environment_stack[depth]);
1577 if (environment_inner[depth] != "!-- --") {
1578 item_name= "listitem";
1579 buf->sgmlCloseTag(os,
1580 command_depth + depth,
1582 if (environment_inner[depth] == "varlistentry")
1583 buf->sgmlCloseTag(os,
1584 depth + command_depth,
1585 environment_inner[depth]);
1589 if (style.latextype == LATEX_ENVIRONMENT) {
1590 if (!style.latexparam().empty()) {
1591 if (style.latexparam() == "CDATA")
1594 buf->sgmlOpenTag(os, depth + command_depth,
1595 style.latexparam());
1600 desc_on = (style.labeltype == LABEL_MANUAL);
1603 environment_inner[depth]= "varlistentry";
1605 environment_inner[depth]= "listitem";
1607 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1608 environment_inner[depth]);
1612 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1616 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1621 buf->sgmlOpenTag(os, depth + command_depth,
1626 buf->simpleDocBookOnePar(os, p, desc_on,
1627 depth + 1 + command_depth);
1631 // write closing SGML tags
1632 switch (style.latextype) {
1633 case LATEX_ENVIRONMENT:
1634 if (!style.latexparam().empty()) {
1635 if (style.latexparam() == "CDATA")
1638 buf->sgmlCloseTag(os, depth + command_depth,
1639 style.latexparam());
1642 case LATEX_ITEM_ENVIRONMENT:
1643 if (desc_on == 1) break;
1645 buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1647 case LATEX_PARAGRAPH:
1648 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1651 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1657 for (int d = depth; d >= 0; --d) {
1658 if (!environment_stack[depth].empty()) {
1659 if (environment_inner[depth] != "!-- --") {
1660 item_name = "listitem";
1661 buf->sgmlCloseTag(os, command_depth + depth,
1663 if (environment_inner[depth] == "varlistentry")
1664 buf->sgmlCloseTag(os, depth + command_depth,
1665 environment_inner[depth]);
1668 buf->sgmlCloseTag(os, depth + command_depth,
1669 environment_stack[depth]);
1677 void InsetText::validate(LaTeXFeatures & features) const
1679 Paragraph * p = par;
1681 p->validate(features);
1687 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1689 if (textclasslist[buf->params.textclass][p->layout()].labeltype != LABEL_MANUAL)
1692 return p->beginningOfMainBody();
1696 void InsetText::getCursorPos(BufferView * bv,
1697 int & x, int & y) const
1699 if (the_locking_inset) {
1700 the_locking_inset->getCursorPos(bv, x, y);
1708 unsigned int InsetText::insetInInsetY()
1710 if (!the_locking_inset)
1713 return (inset_y + the_locking_inset->insetInInsetY());
1717 void InsetText::toggleInsetCursor(BufferView * bv)
1719 if (the_locking_inset) {
1720 the_locking_inset->toggleInsetCursor(bv);
1724 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1726 int const asc = lyxfont::maxAscent(font);
1727 int const desc = lyxfont::maxDescent(font);
1729 if (isCursorVisible())
1730 bv->hideLockedInsetCursor();
1732 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1733 toggleCursorVisible();
1737 void InsetText::showInsetCursor(BufferView * bv, bool show)
1739 if (the_locking_inset) {
1740 the_locking_inset->showInsetCursor(bv, show);
1743 if (!isCursorVisible()) {
1744 LyXFont const font =
1745 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1747 int const asc = lyxfont::maxAscent(font);
1748 int const desc = lyxfont::maxDescent(font);
1750 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1752 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1753 setCursorVisible(true);
1758 void InsetText::hideInsetCursor(BufferView * bv)
1760 if (isCursorVisible()) {
1761 bv->hideLockedInsetCursor();
1762 setCursorVisible(false);
1764 if (the_locking_inset)
1765 the_locking_inset->hideInsetCursor(bv);
1769 void InsetText::fitInsetCursor(BufferView * bv) const
1771 if (the_locking_inset) {
1772 the_locking_inset->fitInsetCursor(bv);
1775 LyXFont const font =
1776 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1778 int const asc = lyxfont::maxAscent(font);
1779 int const desc = lyxfont::maxDescent(font);
1781 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1782 need_update |= FULL;
1786 UpdatableInset::RESULT
1787 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1789 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1790 return moveLeftIntern(bv, false, activate_inset, selecting);
1792 return moveRightIntern(bv, false, activate_inset, selecting);
1796 UpdatableInset::RESULT
1797 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1799 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1800 return moveRightIntern(bv, true, activate_inset, selecting);
1802 return moveLeftIntern(bv, true, activate_inset, selecting);
1806 UpdatableInset::RESULT
1807 InsetText::moveRightIntern(BufferView * bv, bool behind,
1808 bool activate_inset, bool selecting)
1810 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1811 return FINISHED_RIGHT;
1812 if (activate_inset && checkAndActivateInset(bv, behind))
1814 getLyXText(bv)->cursorRight(bv);
1816 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1817 return DISPATCHED_NOUPDATE;
1821 UpdatableInset::RESULT
1822 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1823 bool activate_inset, bool selecting)
1825 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1827 getLyXText(bv)->cursorLeft(bv);
1829 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1830 if (activate_inset && checkAndActivateInset(bv, behind))
1832 return DISPATCHED_NOUPDATE;
1836 UpdatableInset::RESULT
1837 InsetText::moveUp(BufferView * bv)
1839 if (!crow(bv)->previous())
1841 getLyXText(bv)->cursorUp(bv);
1842 return DISPATCHED_NOUPDATE;
1846 UpdatableInset::RESULT
1847 InsetText::moveDown(BufferView * bv)
1849 if (!crow(bv)->next())
1850 return FINISHED_DOWN;
1851 getLyXText(bv)->cursorDown(bv);
1852 return DISPATCHED_NOUPDATE;
1856 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1858 if (the_locking_inset) {
1859 if (the_locking_inset->insetAllowed(inset))
1860 return the_locking_inset->insertInset(bv, inset);
1865 lt = getLyXText(bv);
1868 setUndo(bv, Undo::FINISH, lt->cursor.par(), lt->cursor.par()->next());
1870 inset->setOwner(this);
1871 hideInsetCursor(bv);
1872 lt->insertInset(bv, inset);
1876 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1882 bool InsetText::insetAllowed(Inset::Code code) const
1884 // in_insetAllowed is a really gross hack,
1885 // to allow us to call the owner's insetAllowed
1886 // without stack overflow, which can happen
1887 // when the owner uses InsetCollapsable::insetAllowed()
1889 if (in_insetAllowed)
1891 in_insetAllowed = true;
1892 if (the_locking_inset)
1893 ret = the_locking_inset->insetAllowed(code);
1895 ret = owner()->insetAllowed(code);
1896 in_insetAllowed = false;
1901 UpdatableInset * InsetText::getLockingInset() const
1903 return the_locking_inset ? the_locking_inset->getLockingInset() :
1904 const_cast<InsetText *>(this);
1908 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1912 if (the_locking_inset)
1913 return the_locking_inset->getFirstLockingInsetOfType(c);
1918 bool InsetText::showInsetDialog(BufferView * bv) const
1920 if (the_locking_inset)
1921 return the_locking_inset->showInsetDialog(bv);
1926 vector<string> const InsetText::getLabelList() const
1928 vector<string> label_list;
1930 Paragraph * tpar = par;
1932 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1933 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1934 for (; beg != end; ++beg) {
1935 vector<string> const l = (*beg)->getLabelList();
1936 label_list.insert(label_list.end(), l.begin(), l.end());
1938 tpar = tpar->next();
1944 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1947 if (the_locking_inset) {
1948 the_locking_inset->setFont(bv, font, toggleall, selectall);
1951 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
1952 getLyXText(bv)->setFont(bv, font, toggleall);
1957 lt = getLyXText(bv);
1960 if (lt->selection.set()) {
1961 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1965 lt->toggleFree(bv, font, toggleall);
1967 lt->clearSelection();
1969 bool flag = (selectall || lt->selection.set());
1973 updateLocal(bv, FULL, true);
1975 updateLocal(bv, CURSOR_PAR, true);
1979 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1981 if (cpar(bv)->isInset(cpos(bv))) {
1985 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1986 if (!isHighlyEditableInset(inset))
1988 LyXFont const font =
1989 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1991 x = inset->width(bv, font);
1992 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1995 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1997 //inset_x = cx(bv) - top_x + drawTextXOffset;
1998 //inset_y = cy(bv) + drawTextYOffset;
1999 inset->edit(bv, x, y, 0);
2000 if (!the_locking_inset)
2002 updateLocal(bv, CURSOR, false);
2009 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
2012 x -= drawTextXOffset;
2014 int dummyy = y + insetAscent;
2015 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
2022 inset_x = cx(bv) - top_x + drawTextXOffset;
2023 inset_y = cy(bv) + drawTextYOffset;
2024 inset->edit(bv, x - inset_x, y - inset_y, button);
2025 if (!the_locking_inset)
2027 updateLocal(bv, CURSOR, false);
2034 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
2037 int w = UpdatableInset::getMaxWidth(bv, inset);
2042 w = w - top_x + owner()->x();
2045 w -= (2 * TEXT_TO_INSET_OFFSET);
2048 return UpdatableInset::getMaxWidth(bv, inset);
2053 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2055 // we have to unlock any locked inset otherwise we're in troubles
2056 the_locking_inset = 0;
2058 Paragraph * tmp = par->next();
2063 par = new Paragraph(*p, same_id);
2064 par->setInsetOwner(this);
2065 Paragraph * np = par;
2068 np->next(new Paragraph(*p, same_id));
2069 np->next()->previous(np);
2071 np->setInsetOwner(this);
2078 void InsetText::setText(string const & data, LyXFont const & font)
2081 for (unsigned int i=0; i < data.length(); ++i)
2082 par->insertChar(i, data[i], font);
2087 void InsetText::setAutoBreakRows(bool flag)
2089 if (flag != autoBreakRows) {
2090 autoBreakRows = flag;
2098 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2100 if (how != drawFrame_) {
2103 updateLocal(bv, DRAW_FRAME, false);
2108 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2110 if (frame_color != col) {
2113 updateLocal(bv, DRAW_FRAME, false);
2118 int InsetText::cx(BufferView * bv) const
2120 // we do nothing dangerous so we use a local cache
2121 LyXText * llt = getLyXText(bv);
2122 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2123 if (the_locking_inset) {
2124 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2126 if (font.isVisibleRightToLeft())
2127 x -= the_locking_inset->width(bv, font);
2133 int InsetText::cy(BufferView * bv) const
2136 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2140 pos_type InsetText::cpos(BufferView * bv) const
2142 return getLyXText(bv)->cursor.pos();
2146 Paragraph * InsetText::cpar(BufferView * bv) const
2148 return getLyXText(bv)->cursor.par();
2152 bool InsetText::cboundary(BufferView * bv) const
2154 return getLyXText(bv)->cursor.boundary();
2158 Row * InsetText::crow(BufferView * bv) const
2160 return getLyXText(bv)->cursor.row();
2164 LyXText * InsetText::getLyXText(BufferView const * lbv,
2165 bool const recursive) const
2167 if (cached_bview == lbv) {
2168 if (recursive && the_locking_inset)
2169 return the_locking_inset->getLyXText(lbv, true);
2170 LyXText * lt = cached_text.get();
2171 lyx::Assert(lt && lt->firstRow()->par() == par);
2174 // Super UGLY! (Lgb)
2175 BufferView * bv = const_cast<BufferView *>(lbv);
2178 Cache::iterator it = cache.find(bv);
2180 if (it != cache.end()) {
2183 } else if (do_resize) {
2184 resizeLyXText(do_resize);
2186 if (lt || !it->second.remove) {
2187 lyx::Assert(it->second.text.get());
2188 cached_text = it->second.text;
2189 if (recursive && the_locking_inset) {
2190 return the_locking_inset->getLyXText(bv, true);
2192 return cached_text.get();
2193 } else if (it->second.remove) {
2195 saveLyXTextState(it->second.text.get());
2201 // when we have to reinit the existing LyXText!
2203 it->second.text->init(bv);
2204 restoreLyXTextState(bv, it->second.text.get());
2205 it->second.remove = false;
2207 cached_text = it->second.text;
2208 if (the_locking_inset && recursive) {
2209 return the_locking_inset->getLyXText(bv);
2211 return cached_text.get();
2214 // we are here only if we don't have a BufferView * in the cache!!!
2216 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2217 cached_text->init(bv);
2218 restoreLyXTextState(bv, cached_text.get());
2220 cache.insert(make_pair(bv, cached_text));
2222 if (the_locking_inset && recursive) {
2223 return the_locking_inset->getLyXText(bv);
2225 return cached_text.get();
2229 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2233 Cache::iterator it = cache.find(bv);
2235 if (it == cache.end()) {
2239 lyx::Assert(it->second.text.get());
2241 it->second.remove = true;
2243 /// then remove all LyXText in text-insets
2244 Paragraph * p = par;
2245 for (; p; p = p->next()) {
2246 p->deleteInsetsLyXText(bv);
2252 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2255 // we cannot resize this because we are in use!
2256 // so do this on the next possible getLyXText()
2261 // lyxerr << "InsetText::resizeLyXText\n";
2262 if (!par->next() && !par->size()) { // no data, resize not neccessary!
2263 // we have to do this as a fixed width may have changed!
2264 LyXText * t = getLyXText(bv);
2265 saveLyXTextState(t);
2267 restoreLyXTextState(bv, t);
2270 // one endless line, resize normally not necessary
2271 if (!force && getMaxWidth(bv, this) < 0)
2274 Cache::iterator it = cache.find(bv);
2275 if (it == cache.end()) {
2278 lyx::Assert(it->second.text.get());
2280 LyXText * t = it->second.text.get();
2281 saveLyXTextState(t);
2282 for (Paragraph * p = par; p; p = p->next()) {
2283 p->resizeInsetsLyXText(bv);
2286 restoreLyXTextState(bv, t);
2287 if (the_locking_inset) {
2288 inset_x = cx(bv) - top_x + drawTextXOffset;
2289 inset_y = cy(bv) + drawTextYOffset;
2293 t->first_y = bv->screen()->topCursorVisible(t);
2296 updateLocal(bv, FULL, false);
2297 // this will scroll the screen such that the cursor becomes visible
2298 bv->updateScrollbar();
2300 need_update |= FULL;
2305 void InsetText::reinitLyXText() const
2308 // we cannot resize this because we are in use!
2309 // so do this on the next possible getLyXText()
2315 // lyxerr << "InsetText::reinitLyXText\n";
2316 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2317 lyx::Assert(it->second.text.get());
2319 LyXText * t = it->second.text.get();
2320 BufferView * bv = it->first;
2322 saveLyXTextState(t);
2323 for (Paragraph * p = par; p; p = p->next()) {
2324 p->resizeInsetsLyXText(bv);
2327 restoreLyXTextState(bv, t);
2328 if (the_locking_inset) {
2329 inset_x = cx(bv) - top_x + drawTextXOffset;
2330 inset_y = cy(bv) + drawTextYOffset;
2333 t->first_y = bv->screen()->topCursorVisible(t);
2336 updateLocal(bv, FULL, false);
2337 // this will scroll the screen such that the cursor becomes visible
2338 bv->updateScrollbar();
2346 void InsetText::removeNewlines()
2348 bool changed = false;
2350 for (Paragraph * p = par; p; p = p->next()) {
2351 for (int i = 0; i < p->size(); ++i) {
2352 if (p->getChar(i) == Paragraph::META_NEWLINE) {
2363 bool InsetText::nodraw() const
2365 if (the_locking_inset)
2366 return the_locking_inset->nodraw();
2367 return UpdatableInset::nodraw();
2371 int InsetText::scroll(bool recursive) const
2373 int sx = UpdatableInset::scroll(false);
2375 if (recursive && the_locking_inset)
2376 sx += the_locking_inset->scroll(recursive);
2382 bool InsetText::doClearArea() const
2384 return !locked || (need_update & (FULL|INIT));
2388 void InsetText::selectAll(BufferView * bv)
2390 getLyXText(bv)->cursorTop(bv);
2391 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2392 getLyXText(bv)->cursorBottom(bv);
2393 getLyXText(bv)->setSelection(bv);
2397 void InsetText::clearSelection(BufferView * bv)
2399 getLyXText(bv)->clearSelection();
2403 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2405 Painter & pain = bv->painter();
2407 int h = insetAscent + insetDescent;
2408 int ty = baseline - insetAscent;
2414 if ((ty + h) > pain.paperHeight())
2415 h = pain.paperHeight();
2416 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2417 w = pain.paperWidth();
2418 // w -= TEXT_TO_INSET_OFFSET;
2419 pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2422 frame_is_visible = false;
2426 Paragraph * InsetText::getParFromID(int id) const
2429 Paragraph * result = par;
2430 Paragraph * ires = 0;
2431 while (result && result->id() != id) {
2432 if ((ires = result->getParFromID(id)))
2434 result = result->next();
2438 Paragraph * tmp = par;
2440 if (tmp->id() == id) {
2443 Paragraph * tmp2 = tmp->getParFromID(id);
2454 Paragraph * InsetText::firstParagraph() const
2457 if (the_locking_inset)
2458 if ((result = the_locking_inset->firstParagraph()))
2464 Paragraph * InsetText::getFirstParagraph(int i) const
2466 return (i == 0) ? par : 0;
2470 LyXCursor const & InsetText::cursor(BufferView * bv) const
2472 if (the_locking_inset)
2473 return the_locking_inset->cursor(bv);
2474 return getLyXText(bv)->cursor;
2478 Paragraph * InsetText::paragraph() const
2484 void InsetText::paragraph(Paragraph * p)
2486 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2487 // caller of this function has to take care of it. This IS important
2488 // as we could have to insert a paragraph before this one and just
2489 // link the actual to a new ones next and set it with this function
2492 // set ourself as owner for all the paragraphs inserted!
2493 Paragraph * np = par;
2495 np->setInsetOwner(this);
2499 // redraw myself when asked for
2504 Inset * InsetText::getInsetFromID(int id_arg) const
2507 return const_cast<InsetText *>(this);
2509 Paragraph * lp = par;
2512 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2513 en = lp->inset_iterator_end();
2516 if ((*it)->id() == id_arg)
2518 Inset * in = (*it)->getInsetFromID(id_arg);
2528 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2534 lt = getLyXText(bv);
2537 if (the_locking_inset) {
2538 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2545 // we have to go on checking so move cusor to the next char
2546 lt->cursor.pos(lt->cursor.pos() + 1);
2548 str = lt->selectNextWordToSpellcheck(bv, value);
2550 bv->unlockInset(const_cast<InsetText *>(this));
2559 void InsetText::selectSelectedWord(BufferView * bv)
2561 if (the_locking_inset) {
2562 the_locking_inset->selectSelectedWord(bv);
2565 getLyXText(bv)->selectSelectedWord(bv);
2566 updateLocal(bv, SELECTION, false);
2570 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2572 if (the_locking_inset) {
2573 the_locking_inset->toggleSelection(bv, kill_selection);
2577 lt = getLyXText(bv);
2581 int x = top_x + TEXT_TO_INSET_OFFSET;
2583 Row * row = lt->firstRow();
2584 int y_offset = top_baseline - row->ascent_of_text();
2586 while ((row != 0) && ((y+row->height()) <= 0)) {
2593 if (need_update & SELECTION)
2595 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2601 bool InsetText::searchForward(BufferView * bv, string const & str,
2604 if (the_locking_inset) {
2605 if (the_locking_inset->searchForward(bv, str, cs, mw))
2609 lt = getLyXText(bv);
2612 Paragraph * lpar = lt->cursor.par();
2613 pos_type pos = lt->cursor.pos();
2614 if (pos < lpar->size() - 1)
2618 lpar = lpar->next();
2623 // we have to unlock ourself in this function by default!
2624 bv->unlockInset(const_cast<InsetText *>(this));
2627 lt->setCursor(bv, lpar, pos);
2631 if (LyXFind(bv, str, true, true, cs , mw)) {
2634 // we have to unlock ourself in this function by default!
2635 bv->unlockInset(const_cast<InsetText *>(this));
2639 bool InsetText::searchBackward(BufferView * bv, string const & str,
2642 if (the_locking_inset)
2643 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2645 if (LyXFind(bv, str, false, true, cs, mw)) {
2648 // we have to unlock ourself in this function by default!
2649 bv->unlockInset(const_cast<InsetText *>(this));
2654 bool InsetText::checkInsertChar(LyXFont & font)
2657 return owner()->checkInsertChar(font);
2662 void InsetText::collapseParagraphs(BufferParams const & bparams) const
2664 while(par->next()) {
2665 if (par->size() && par->next()->size() &&
2666 !par->isSeparator(par->size()-1))
2668 par->insertChar(par->size(), ' ');
2670 par->pasteParagraph(bparams);
2676 void InsetText::getDrawFont(LyXFont & font) const
2680 owner()->getDrawFont(font);
2684 void InsetText::appendParagraphs(BufferParams const & bparams,
2688 Paragraph * tmpbuf = newpar;
2689 Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2691 while (tmpbuf->next()) {
2692 tmpbuf = tmpbuf->next();
2693 lastbuffer->next(new Paragraph(*tmpbuf, false));
2694 lastbuffer->next()->previous(lastbuffer);
2695 lastbuffer = lastbuffer->next();
2698 while (lastbuffer->next())
2699 lastbuffer = lastbuffer->next();
2700 if (newpar->size() && lastbuffer->size() &&
2701 !lastbuffer->isSeparator(lastbuffer->size()-1))
2703 lastbuffer->insertChar(lastbuffer->size(), ' ');
2706 // make the buf exactly the same layout than our last paragraph
2707 buf->makeSameLayout(lastbuffer);
2710 lastbuffer->next(buf);
2711 buf->previous(lastbuffer);
2712 lastbuffer->pasteParagraph(bparams);