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->cursor.x_fix(lt->cursor.x());
683 lt->clearSelection();
685 // If the inset is empty set the language of the current font to the
686 // language to the surronding text (if different).
687 if (par->size() == 0 && !par->next() &&
688 bv->getParentLanguage(this) != lt->current_font.language())
690 LyXFont font(LyXFont::ALL_IGNORE);
691 font.setLanguage(bv->getParentLanguage(this));
692 setFont(bv, font, false);
698 if (drawFrame_ == LOCKED)
699 code = CURSOR|DRAW_FRAME;
700 updateLocal(bv, code, false);
703 // Tell the paragraph dialog that we've entered an insettext.
704 bv->owner()->getDialogs()->updateParagraph();
708 void InsetText::edit(BufferView * bv, bool front)
710 UpdatableInset::edit(bv, front);
712 if (!bv->lockInset(this)) {
713 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
717 the_locking_inset = 0;
718 inset_pos = inset_x = inset_y = 0;
719 inset_boundary = false;
728 lt->setCursor(bv, par, 0);
733 // int const pos = (p->size() ? p->size()-1 : p->size());
734 lt->setCursor(bv, p, p->size());
736 lt->clearSelection();
738 // If the inset is empty set the language of the current font to the
739 // language to the surronding text (if different).
740 if (par->size() == 0 && !par->next() &&
741 bv->getParentLanguage(this) != lt->current_font.language()) {
742 LyXFont font(LyXFont::ALL_IGNORE);
743 font.setLanguage(bv->getParentLanguage(this));
744 setFont(bv, font, false);
749 if (drawFrame_ == LOCKED)
750 code = CURSOR|DRAW_FRAME;
751 updateLocal(bv, code, false);
756 void InsetText::insetUnlock(BufferView * bv)
758 if (the_locking_inset) {
759 the_locking_inset->insetUnlock(bv);
760 the_locking_inset = 0;
761 updateLocal(bv, CURSOR_PAR, false);
768 if (drawFrame_ == LOCKED)
769 code = CURSOR|CLEAR_FRAME;
773 if (drawFrame_ == LOCKED)
781 if (lt->selection.set()) {
782 lt->clearSelection();
784 } else if (owner()) {
785 bv->owner()->setLayout(owner()->getLyXText(bv)
786 ->cursor.par()->layout());
788 bv->owner()->setLayout(bv->text->cursor.par()->layout());
789 // hack for deleteEmptyParMech
791 lt->setCursor(bv, par, 0);
792 } else if (par->next()) {
793 lt->setCursor(bv, par->next(), 0);
798 updateLocal(bv, code, false);
800 setUpdateStatus(bv, code);
805 void InsetText::lockInset(BufferView * bv)
808 the_locking_inset = 0;
809 inset_pos = inset_x = inset_y = 0;
810 inset_boundary = false;
818 lt->setCursor(bv, par, 0);
819 lt->clearSelection();
821 // If the inset is empty set the language of the current font to the
822 // language to the surronding text (if different).
823 if (par->size() == 0 && !par->next() &&
824 bv->getParentLanguage(this) != lt->current_font.language()) {
825 LyXFont font(LyXFont::ALL_IGNORE);
826 font.setLanguage(bv->getParentLanguage(this));
827 setFont(bv, font, false);
832 if (drawFrame_ == LOCKED)
833 code = CURSOR|DRAW_FRAME;
834 setUpdateStatus(bv, code);
838 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
840 the_locking_inset = inset;
841 inset_x = cix(bv) - top_x + drawTextXOffset;
842 inset_y = ciy(bv) + drawTextYOffset;
843 inset_pos = cpos(bv);
844 inset_par = cpar(bv);
845 inset_boundary = cboundary(bv);
846 updateLocal(bv, CURSOR, false);
850 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
852 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
856 if (!the_locking_inset) {
858 int const id = inset->id();
860 Paragraph::inset_iterator it =
861 p->inset_iterator_begin();
862 Paragraph::inset_iterator const end =
863 p->inset_iterator_end();
864 for (; it != end; ++it) {
865 if ((*it) == inset) {
866 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
867 lockInset(bv, inset);
870 if ((*it)->getInsetFromID(id)) {
871 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
873 return the_locking_inset->lockInsetInInset(bv, inset);
880 if (inset == cpar(bv)->getInset(cpos(bv))) {
881 lyxerr[Debug::INSETS] << "OK" << endl;
882 lockInset(bv, inset);
884 } else if (the_locking_inset && (the_locking_inset == inset)) {
885 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
886 lyxerr[Debug::INSETS] << "OK" << endl;
887 inset_x = cix(bv) - top_x + drawTextXOffset;
888 inset_y = ciy(bv) + drawTextYOffset;
890 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
892 } else if (the_locking_inset) {
893 lyxerr[Debug::INSETS] << "MAYBE" << endl;
894 return the_locking_inset->lockInsetInInset(bv, inset);
896 lyxerr[Debug::INSETS] << "NOT OK" << endl;
901 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
904 if (!the_locking_inset)
906 if (the_locking_inset == inset) {
907 the_locking_inset->insetUnlock(bv);
908 getLyXText(bv)->updateInset(bv, inset);
909 the_locking_inset = 0;
911 moveRight(bv, false);
912 old_par = 0; // force layout setting
916 updateLocal(bv, CURSOR, false);
919 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
923 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
925 if (!autoBreakRows && par->next())
926 collapseParagraphs(bv);
934 if (inset->owner() != this) {
935 int ustat = CURSOR_PAR;
937 UpdatableInset * tl_inset = the_locking_inset;
939 found = tl_inset->updateInsetInInset(bv, inset);
941 tl_inset = static_cast<UpdatableInset *>(inset);
942 while(tl_inset->owner() && tl_inset->owner() != this)
943 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
944 if (!tl_inset->owner())
946 found = tl_inset->updateInsetInInset(bv, inset);
950 lt->updateInset(bv, tl_inset);
954 setUpdateStatus(bv, ustat);
957 bool found = lt->updateInset(bv, inset);
961 setUpdateStatus(bv, CURSOR_PAR);
962 if (the_locking_inset &&
963 cpar(bv) == inset_par && cpos(bv) == inset_pos)
965 inset_x = cix(bv) - top_x + drawTextXOffset;
966 inset_y = ciy(bv) + drawTextYOffset;
973 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
977 // use this to check mouse motion for selection!
984 int tmp_x = x - drawTextXOffset;
985 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
986 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
989 if (the_locking_inset) {
990 if (the_locking_inset == inset) {
991 the_locking_inset->insetButtonPress(bv,
999 // otherwise unlock the_locking_inset and lock the new inset
1000 the_locking_inset->insetUnlock(bv);
1001 inset_x = cix(bv) - top_x + drawTextXOffset;
1002 inset_y = ciy(bv) + drawTextYOffset;
1003 the_locking_inset = 0;
1004 inset->insetButtonPress(bv, x - inset_x,
1005 y - inset_y, button);
1006 // inset->edit(bv, x - inset_x, y - inset_y, button);
1007 if (the_locking_inset)
1008 updateLocal(bv, CURSOR, false);
1012 // otherwise only unlock the_locking_inset
1013 the_locking_inset->insetUnlock(bv);
1014 the_locking_inset = 0;
1017 no_selection = false;
1019 if (bv->theLockingInset()) {
1020 if (isHighlyEditableInset(inset)) {
1021 // We just have to lock the inset before calling a
1022 // PressEvent on it!
1023 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
1024 if (!bv->lockInset(uinset)) {
1025 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1027 inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
1028 if (the_locking_inset)
1029 updateLocal(bv, CURSOR, false);
1033 if (!inset) { // && (button == 2)) {
1034 bool paste_internally = false;
1035 if ((button == 2) && getLyXText(bv)->selection.set()) {
1036 localDispatch(bv, LFUN_COPY, "");
1037 paste_internally = true;
1041 lt = getLyXText(bv);
1044 int old_first_y = lt->first_y;
1046 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
1048 // set the selection cursor!
1049 lt->selection.cursor = lt->cursor;
1050 lt->cursor.x_fix(lt->cursor.x());
1052 if (lt->selection.set()) {
1053 lt->clearSelection();
1056 updateLocal(bv, FULL, false);
1058 lt->clearSelection();
1061 updateLocal(bv, CURSOR, false);
1063 bv->owner()->setLayout(cpar(bv)->layout());
1064 // we moved the view we cannot do mouse selection in this case!
1065 if (getLyXText(bv)->first_y != old_first_y)
1066 no_selection = true;
1068 // Insert primary selection with middle mouse
1069 // if there is a local selection in the current buffer,
1072 if (paste_internally)
1073 localDispatch(bv, LFUN_PASTE, "");
1075 localDispatch(bv, LFUN_PASTESELECTION,
1079 getLyXText(bv)->clearSelection();
1081 showInsetCursor(bv);
1085 bool InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
1087 no_selection = true;
1088 if (the_locking_inset) {
1089 return the_locking_inset->insetButtonRelease(bv,
1090 x - inset_x, y - inset_y,
1093 int tmp_x = x - drawTextXOffset;
1094 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
1095 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1098 if (isHighlyEditableInset(inset)) {
1099 ret = inset->insetButtonRelease(bv, x - inset_x,
1100 y - inset_y, button);
1102 inset_x = cix(bv) - top_x + drawTextXOffset;
1103 inset_y = ciy(bv) + drawTextYOffset;
1104 ret = inset->insetButtonRelease(bv, x - inset_x,
1105 y - inset_y, button);
1106 inset->edit(bv, x - inset_x,
1107 y - inset_y, button);
1109 updateLocal(bv, CURSOR_PAR, false);
1115 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
1117 if (the_locking_inset) {
1118 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1123 if (no_selection || ((mouse_x == x) && (mouse_y == y)))
1128 lt = getLyXText(bv);
1131 hideInsetCursor(bv);
1132 LyXCursor cur = lt->cursor;
1133 lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1134 lt->cursor.x_fix(lt->cursor.x());
1135 if (cur == lt->cursor) {
1140 lt->setSelection(bv);
1141 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1142 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1146 updateLocal(bv, SELECTION, false);
1148 showInsetCursor(bv);
1152 void InsetText::insetKeyPress(XKeyEvent * xke)
1154 if (the_locking_inset) {
1155 the_locking_inset->insetKeyPress(xke);
1161 UpdatableInset::RESULT
1162 InsetText::localDispatch(BufferView * bv,
1163 kb_action action, string const & arg)
1165 bool was_empty = par->size() == 0 && !par->next();
1166 no_selection = false;
1167 UpdatableInset::RESULT
1168 result= UpdatableInset::localDispatch(bv, action, arg);
1169 if (result != UNDISPATCHED) {
1173 result = DISPATCHED;
1174 if ((action < 0) && arg.empty())
1177 if (the_locking_inset) {
1178 result = the_locking_inset->localDispatch(bv, action, arg);
1179 if (result == DISPATCHED_NOUPDATE)
1181 else if (result == DISPATCHED) {
1182 updateLocal(bv, CURSOR_PAR, false);
1184 } else if (result >= FINISHED) {
1186 case FINISHED_RIGHT:
1187 moveRightIntern(bv, false, false);
1188 result = DISPATCHED;
1191 if ((result = moveUp(bv)) >= FINISHED) {
1192 updateLocal(bv, CURSOR, false);
1193 bv->unlockInset(this);
1197 if ((result = moveDown(bv)) >= FINISHED) {
1198 updateLocal(bv, CURSOR, false);
1199 bv->unlockInset(this);
1203 result = DISPATCHED;
1206 the_locking_inset = 0;
1207 #ifdef WITH_WARNINGS
1208 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
1210 updateLocal(bv, CURSOR, false);
1214 hideInsetCursor(bv);
1217 lt = getLyXText(bv);
1221 int updflag = false;
1224 case LFUN_SELFINSERT:
1225 if (bv->buffer()->isReadonly()) {
1226 // setErrorMessage(N_("Document is read only"));
1230 /* Automatically delete the currently selected
1231 * text and replace it with what is being
1232 * typed in now. Depends on lyxrc settings
1233 * "auto_region_delete", which defaults to
1236 setUndo(bv, Undo::INSERT,
1237 lt->cursor.par(), lt->cursor.par()->next());
1239 if (lyxrc.auto_region_delete) {
1240 if (lt->selection.set()) {
1241 lt->cutSelection(bv, false);
1244 lt->clearSelection();
1245 for (string::size_type i = 0; i < arg.length(); ++i) {
1246 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1249 lt->selection.cursor = lt->cursor;
1250 updwhat = CURSOR | CURSOR_PAR;
1252 result = DISPATCHED_NOUPDATE;
1254 // --- Cursor Movements -----------------------------------
1257 moveRight(bv, false, true);
1258 lt->setSelection(bv);
1259 updwhat = SELECTION;
1262 result = moveRight(bv);
1268 moveLeft(bv, false, true);
1269 lt->setSelection(bv);
1270 updwhat = SELECTION;
1274 result = moveLeft(bv);
1280 lt->setSelection(bv);
1281 updwhat = SELECTION;
1285 result = moveDown(bv);
1291 lt->setSelection(bv);
1292 updwhat = SELECTION;
1296 result = moveUp(bv);
1308 case LFUN_BACKSPACE: {
1309 setUndo(bv, Undo::DELETE,
1310 lt->cursor.par(), lt->cursor.par()->next());
1311 if (lt->selection.set())
1312 lt->cutSelection(bv);
1315 updwhat = CURSOR_PAR;
1321 setUndo(bv, Undo::DELETE,
1322 lt->cursor.par(), lt->cursor.par()->next());
1323 if (lt->selection.set()) {
1324 lt->cutSelection(bv);
1328 updwhat = CURSOR_PAR;
1334 setUndo(bv, Undo::DELETE,
1335 lt->cursor.par(), lt->cursor.par()->next());
1336 lt->cutSelection(bv);
1337 updwhat = CURSOR_PAR;
1344 lt->copySelection(bv);
1345 updwhat = CURSOR_PAR;
1347 case LFUN_PASTESELECTION:
1349 string const clip(bv->getClipboard());
1353 if (arg == "paragraph") {
1354 lt->insertStringAsParagraphs(bv, clip);
1356 lt->insertStringAsLines(bv, clip);
1358 updwhat = CURSOR_PAR;
1363 if (!autoBreakRows) {
1365 if (CutAndPaste::nrOfParagraphs() > 1) {
1366 Alert::alert(_("Impossible operation"),
1367 _("Cannot include more than one paragraph!"),
1372 setUndo(bv, Undo::INSERT,
1373 lt->cursor.par(), lt->cursor.par()->next());
1374 lt->pasteSelection(bv);
1375 updwhat = CURSOR_PAR;
1380 case LFUN_BREAKPARAGRAPH:
1381 if (!autoBreakRows) {
1382 result = DISPATCHED;
1385 lt->breakParagraph(bv, 0);
1386 updwhat = CURSOR | FULL;
1389 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1390 if (!autoBreakRows) {
1391 result = DISPATCHED;
1394 lt->breakParagraph(bv, 1);
1395 updwhat = CURSOR | FULL;
1399 case LFUN_BREAKLINE: {
1400 if (!autoBreakRows) {
1401 result = DISPATCHED;
1404 setUndo(bv, Undo::INSERT,
1405 lt->cursor.par(), lt->cursor.par()->next());
1406 lt->insertChar(bv, Paragraph::META_NEWLINE);
1407 updwhat = CURSOR | CURSOR_PAR;
1413 // do not set layouts on non breakable textinsets
1414 if (autoBreakRows) {
1415 string cur_layout = cpar(bv)->layout();
1417 // Derive layout number from given argument (string)
1418 // and current buffer's textclass (number). */
1419 textclass_type tclass = bv->buffer()->params.textclass;
1420 string layout = arg;
1421 bool hasLayout = textclasslist[tclass].hasLayout(layout);
1423 // If the entry is obsolete, use the new one instead.
1425 string const & obs =
1426 textclasslist[tclass][layout].
1432 // see if we found the layout number:
1434 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1435 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1439 if (cur_layout != layout) {
1440 cur_layout = layout;
1441 lt->setLayout(bv, layout);
1442 bv->owner()->setLayout(cpar(bv)->layout());
1443 updwhat = CURSOR_PAR;
1447 // reset the layout box
1448 bv->owner()->setLayout(cpar(bv)->layout());
1451 case LFUN_PARAGRAPH_SPACING:
1452 // This one is absolutely not working. When fiddling with this
1453 // it also seems to me that the paragraphs inside the insettext
1454 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1456 Paragraph * par = lt->cursor.par();
1457 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1458 float cur_value = 1.0;
1459 if (cur_spacing == Spacing::Other) {
1460 cur_value = par->params().spacing().getValue();
1463 istringstream istr(arg.c_str());
1466 Spacing::Space new_spacing = cur_spacing;
1467 float new_value = cur_value;
1469 lyxerr << "Missing argument to `paragraph-spacing'"
1471 } else if (tmp == "single") {
1472 new_spacing = Spacing::Single;
1473 } else if (tmp == "onehalf") {
1474 new_spacing = Spacing::Onehalf;
1475 } else if (tmp == "double") {
1476 new_spacing = Spacing::Double;
1477 } else if (tmp == "other") {
1478 new_spacing = Spacing::Other;
1481 lyxerr << "new_value = " << tmpval << endl;
1484 } else if (tmp == "default") {
1485 new_spacing = Spacing::Default;
1487 lyxerr << _("Unknown spacing argument: ")
1490 if (cur_spacing != new_spacing || cur_value != new_value) {
1491 par->params().spacing(Spacing(new_spacing, new_value));
1492 updwhat = CURSOR_PAR;
1499 if (!bv->Dispatch(action, arg))
1500 result = UNDISPATCHED;
1507 updateLocal(bv, updwhat, updflag);
1508 /// If the action has deleted all text in the inset, we need to change the
1509 // language to the language of the surronding text.
1510 if (!was_empty && par->size() == 0 && !par->next()) {
1511 LyXFont font(LyXFont::ALL_IGNORE);
1512 font.setLanguage(bv->getParentLanguage(this));
1513 setFont(bv, font, false);
1516 if (result >= FINISHED)
1517 bv->unlockInset(this);
1519 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1520 result = DISPATCHED;
1525 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1528 buf->latexParagraphs(os, par, 0, texrow);
1529 return texrow.rows();
1533 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1535 Paragraph * p = par;
1536 unsigned int lines = 0;
1539 string const tmp = buf->asciiParagraph(p, linelen, p->previous()==0);
1540 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1548 int InsetText::docbook(Buffer const * buf, ostream & os) const
1550 Paragraph * p = par;
1551 unsigned int lines = 0;
1553 vector<string> environment_stack(10);
1554 vector<string> environment_inner(10);
1556 int const command_depth = 0;
1559 Paragraph::depth_type depth = 0; // paragraph depth
1563 int desc_on = 0; // description mode
1565 LyXLayout const & style =
1566 textclasslist[buf->params.textclass][p->layout()];
1568 // environment tag closing
1569 for (; depth > p->params().depth(); --depth) {
1570 if (environment_inner[depth] != "!-- --") {
1571 item_name = "listitem";
1572 buf->sgmlCloseTag(os, command_depth + depth,
1574 if (environment_inner[depth] == "varlistentry")
1575 buf->sgmlCloseTag(os, depth+command_depth,
1576 environment_inner[depth]);
1578 buf->sgmlCloseTag(os, depth + command_depth,
1579 environment_stack[depth]);
1580 environment_stack[depth].erase();
1581 environment_inner[depth].erase();
1584 if (depth == p->params().depth()
1585 && environment_stack[depth] != style.latexname()
1586 && !environment_stack[depth].empty()) {
1587 if (environment_inner[depth] != "!-- --") {
1588 item_name= "listitem";
1589 buf->sgmlCloseTag(os, command_depth+depth,
1591 if (environment_inner[depth] == "varlistentry")
1592 buf->sgmlCloseTag(os,
1593 depth + command_depth,
1594 environment_inner[depth]);
1597 buf->sgmlCloseTag(os, depth + command_depth,
1598 environment_stack[depth]);
1600 environment_stack[depth].erase();
1601 environment_inner[depth].erase();
1604 // Write opening SGML tags.
1605 switch (style.latextype) {
1606 case LATEX_PARAGRAPH:
1607 buf->sgmlOpenTag(os, depth + command_depth,
1612 buf->sgmlError(p, 0,
1613 _("Error : LatexType Command not allowed here.\n"));
1617 case LATEX_ENVIRONMENT:
1618 case LATEX_ITEM_ENVIRONMENT:
1619 if (depth < p->params().depth()) {
1620 depth = p->params().depth();
1621 environment_stack[depth].erase();
1624 if (environment_stack[depth] != style.latexname()) {
1625 if (environment_stack.size() == depth + 1) {
1626 environment_stack.push_back("!-- --");
1627 environment_inner.push_back("!-- --");
1629 environment_stack[depth] = style.latexname();
1630 environment_inner[depth] = "!-- --";
1631 buf->sgmlOpenTag(os, depth + command_depth,
1632 environment_stack[depth]);
1634 if (environment_inner[depth] != "!-- --") {
1635 item_name= "listitem";
1636 buf->sgmlCloseTag(os,
1637 command_depth + depth,
1639 if (environment_inner[depth] == "varlistentry")
1640 buf->sgmlCloseTag(os,
1641 depth + command_depth,
1642 environment_inner[depth]);
1646 if (style.latextype == LATEX_ENVIRONMENT) {
1647 if (!style.latexparam().empty()) {
1648 if (style.latexparam() == "CDATA")
1651 buf->sgmlOpenTag(os, depth + command_depth,
1652 style.latexparam());
1657 desc_on = (style.labeltype == LABEL_MANUAL);
1660 environment_inner[depth]= "varlistentry";
1662 environment_inner[depth]= "listitem";
1664 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1665 environment_inner[depth]);
1669 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1673 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1678 buf->sgmlOpenTag(os, depth + command_depth,
1683 buf->simpleDocBookOnePar(os, p, desc_on,
1684 depth + 1 + command_depth);
1688 // write closing SGML tags
1689 switch (style.latextype) {
1690 case LATEX_ENVIRONMENT:
1691 if (!style.latexparam().empty()) {
1692 if (style.latexparam() == "CDATA")
1695 buf->sgmlCloseTag(os, depth + command_depth,
1696 style.latexparam());
1699 case LATEX_ITEM_ENVIRONMENT:
1700 if (desc_on == 1) break;
1702 buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1704 case LATEX_PARAGRAPH:
1705 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1708 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1714 for (int d = depth; d >= 0; --d) {
1715 if (!environment_stack[depth].empty()) {
1716 if (environment_inner[depth] != "!-- --") {
1717 item_name = "listitem";
1718 buf->sgmlCloseTag(os, command_depth + depth,
1720 if (environment_inner[depth] == "varlistentry")
1721 buf->sgmlCloseTag(os, depth + command_depth,
1722 environment_inner[depth]);
1725 buf->sgmlCloseTag(os, depth + command_depth,
1726 environment_stack[depth]);
1734 void InsetText::validate(LaTeXFeatures & features) const
1736 Paragraph * p = par;
1738 p->validate(features);
1744 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1746 if (textclasslist[buf->params.textclass][p->layout()].labeltype != LABEL_MANUAL)
1749 return p->beginningOfMainBody();
1753 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1755 if (the_locking_inset) {
1756 the_locking_inset->getCursorPos(bv, x, y);
1759 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1760 y = cy(bv) - TEXT_TO_INSET_OFFSET;
1764 int InsetText::insetInInsetY() const
1766 if (!the_locking_inset)
1769 return (inset_y + the_locking_inset->insetInInsetY());
1773 void InsetText::toggleInsetCursor(BufferView * bv)
1775 if (the_locking_inset) {
1776 the_locking_inset->toggleInsetCursor(bv);
1780 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1782 int const asc = lyxfont::maxAscent(font);
1783 int const desc = lyxfont::maxDescent(font);
1785 if (isCursorVisible())
1786 bv->hideLockedInsetCursor();
1788 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1789 toggleCursorVisible();
1793 void InsetText::showInsetCursor(BufferView * bv, bool show)
1795 if (the_locking_inset) {
1796 the_locking_inset->showInsetCursor(bv, show);
1799 if (!isCursorVisible()) {
1800 LyXFont const font =
1801 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1803 int const asc = lyxfont::maxAscent(font);
1804 int const desc = lyxfont::maxDescent(font);
1806 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1808 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1809 setCursorVisible(true);
1814 void InsetText::hideInsetCursor(BufferView * bv)
1816 if (isCursorVisible()) {
1817 bv->hideLockedInsetCursor();
1818 setCursorVisible(false);
1820 if (the_locking_inset)
1821 the_locking_inset->hideInsetCursor(bv);
1825 void InsetText::fitInsetCursor(BufferView * bv) const
1827 if (the_locking_inset) {
1828 the_locking_inset->fitInsetCursor(bv);
1831 LyXFont const font =
1832 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1834 int const asc = lyxfont::maxAscent(font);
1835 int const desc = lyxfont::maxDescent(font);
1837 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1838 need_update |= FULL;
1842 UpdatableInset::RESULT
1843 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1845 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1846 return moveLeftIntern(bv, true, activate_inset, selecting);
1848 return moveRightIntern(bv, true, activate_inset, selecting);
1852 UpdatableInset::RESULT
1853 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1855 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1856 return moveRightIntern(bv, false, activate_inset, selecting);
1858 return moveLeftIntern(bv, false, activate_inset, selecting);
1862 UpdatableInset::RESULT
1863 InsetText::moveRightIntern(BufferView * bv, bool front,
1864 bool activate_inset, bool selecting)
1866 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1867 return FINISHED_RIGHT;
1868 if (activate_inset && checkAndActivateInset(bv, front))
1870 getLyXText(bv)->cursorRight(bv);
1872 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1873 return DISPATCHED_NOUPDATE;
1877 UpdatableInset::RESULT
1878 InsetText::moveLeftIntern(BufferView * bv, bool front,
1879 bool activate_inset, bool selecting)
1881 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1883 getLyXText(bv)->cursorLeft(bv);
1885 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1886 if (activate_inset && checkAndActivateInset(bv, front))
1888 return DISPATCHED_NOUPDATE;
1892 UpdatableInset::RESULT
1893 InsetText::moveUp(BufferView * bv)
1895 if (!crow(bv)->previous())
1897 getLyXText(bv)->cursorUp(bv);
1898 return DISPATCHED_NOUPDATE;
1902 UpdatableInset::RESULT
1903 InsetText::moveDown(BufferView * bv)
1905 if (!crow(bv)->next())
1906 return FINISHED_DOWN;
1907 getLyXText(bv)->cursorDown(bv);
1908 return DISPATCHED_NOUPDATE;
1912 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1914 if (the_locking_inset) {
1915 if (the_locking_inset->insetAllowed(inset))
1916 return the_locking_inset->insertInset(bv, inset);
1919 inset->setOwner(this);
1920 hideInsetCursor(bv);
1921 getLyXText(bv)->insertInset(bv, inset);
1923 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1928 bool InsetText::insetAllowed(Inset::Code code) const
1930 // in_insetAllowed is a really gross hack,
1931 // to allow us to call the owner's insetAllowed
1932 // without stack overflow, which can happen
1933 // when the owner uses InsetCollapsable::insetAllowed()
1935 if (in_insetAllowed)
1937 in_insetAllowed = true;
1938 if (the_locking_inset)
1939 ret = the_locking_inset->insetAllowed(code);
1941 ret = owner()->insetAllowed(code);
1942 in_insetAllowed = false;
1947 UpdatableInset * InsetText::getLockingInset() const
1949 return the_locking_inset ? the_locking_inset->getLockingInset() :
1950 const_cast<InsetText *>(this);
1954 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1958 if (the_locking_inset)
1959 return the_locking_inset->getFirstLockingInsetOfType(c);
1964 bool InsetText::showInsetDialog(BufferView * bv) const
1966 if (the_locking_inset)
1967 return the_locking_inset->showInsetDialog(bv);
1972 vector<string> const InsetText::getLabelList() const
1974 vector<string> label_list;
1976 Paragraph * tpar = par;
1978 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1979 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1980 for (; beg != end; ++beg) {
1981 vector<string> const l = (*beg)->getLabelList();
1982 label_list.insert(label_list.end(), l.begin(), l.end());
1984 tpar = tpar->next();
1990 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1993 if (the_locking_inset) {
1994 the_locking_inset->setFont(bv, font, toggleall, selectall);
1997 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
1998 getLyXText(bv)->setFont(bv, font, toggleall);
2003 lt = getLyXText(bv);
2006 if (lt->selection.set()) {
2007 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
2011 lt->toggleFree(bv, font, toggleall);
2013 lt->clearSelection();
2015 bool flag = (selectall || lt->selection.set());
2019 updateLocal(bv, FULL, true);
2021 updateLocal(bv, CURSOR_PAR, true);
2025 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
2027 if (cpar(bv)->isInset(cpos(bv))) {
2029 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
2030 if (!isHighlyEditableInset(inset))
2032 inset->edit(bv, front);
2033 if (!the_locking_inset)
2035 updateLocal(bv, CURSOR, false);
2042 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
2045 x -= drawTextXOffset;
2047 int dummyy = y + insetAscent;
2048 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
2055 inset_x = cix(bv) - top_x + drawTextXOffset;
2056 inset_y = ciy(bv) + drawTextYOffset;
2057 inset->edit(bv, x - inset_x, y - inset_y, button);
2058 if (!the_locking_inset)
2060 updateLocal(bv, CURSOR, false);
2067 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
2070 int w = UpdatableInset::getMaxWidth(bv, inset);
2075 w = w - top_x + owner()->x();
2078 w -= (2 * TEXT_TO_INSET_OFFSET);
2081 return UpdatableInset::getMaxWidth(bv, inset);
2086 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2088 // we have to unlock any locked inset otherwise we're in troubles
2089 the_locking_inset = 0;
2091 Paragraph * tmp = par->next();
2096 par = new Paragraph(*p, same_id);
2097 par->setInsetOwner(this);
2098 Paragraph * np = par;
2101 np->next(new Paragraph(*p, same_id));
2102 np->next()->previous(np);
2104 np->setInsetOwner(this);
2111 void InsetText::setText(string const & data, LyXFont const & font)
2114 for (unsigned int i=0; i < data.length(); ++i)
2115 par->insertChar(i, data[i], font);
2120 void InsetText::setAutoBreakRows(bool flag)
2122 if (flag != autoBreakRows) {
2123 autoBreakRows = flag;
2131 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2133 if (how != drawFrame_) {
2136 updateLocal(bv, DRAW_FRAME, false);
2141 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2143 if (frame_color != col) {
2146 updateLocal(bv, DRAW_FRAME, false);
2151 int InsetText::cx(BufferView * bv) const
2153 // we do nothing dangerous so we use a local cache
2154 LyXText * llt = getLyXText(bv);
2155 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2156 if (the_locking_inset) {
2157 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2159 if (font.isVisibleRightToLeft())
2160 x -= the_locking_inset->width(bv, font);
2166 int InsetText::cix(BufferView * bv) const
2168 // we do nothing dangerous so we use a local cache
2169 LyXText * llt = getLyXText(bv);
2170 int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2171 if (the_locking_inset) {
2172 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2174 if (font.isVisibleRightToLeft())
2175 x -= the_locking_inset->width(bv, font);
2181 int InsetText::cy(BufferView * bv) const
2184 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2188 int InsetText::ciy(BufferView * bv) const
2191 return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2195 pos_type InsetText::cpos(BufferView * bv) const
2197 return getLyXText(bv)->cursor.pos();
2201 Paragraph * InsetText::cpar(BufferView * bv) const
2203 return getLyXText(bv)->cursor.par();
2207 bool InsetText::cboundary(BufferView * bv) const
2209 return getLyXText(bv)->cursor.boundary();
2213 Row * InsetText::crow(BufferView * bv) const
2215 return getLyXText(bv)->cursor.row();
2219 LyXText * InsetText::getLyXText(BufferView const * lbv,
2220 bool const recursive) const
2222 if (cached_bview == lbv) {
2223 if (recursive && the_locking_inset)
2224 return the_locking_inset->getLyXText(lbv, true);
2225 LyXText * lt = cached_text.get();
2226 lyx::Assert(lt && lt->firstRow()->par() == par);
2229 // Super UGLY! (Lgb)
2230 BufferView * bv = const_cast<BufferView *>(lbv);
2233 Cache::iterator it = cache.find(bv);
2235 if (it != cache.end()) {
2238 } else if (do_resize) {
2239 resizeLyXText(do_resize);
2241 if (lt || !it->second.remove) {
2242 lyx::Assert(it->second.text.get());
2243 cached_text = it->second.text;
2244 if (recursive && the_locking_inset) {
2245 return the_locking_inset->getLyXText(bv, true);
2247 return cached_text.get();
2248 } else if (it->second.remove) {
2250 saveLyXTextState(it->second.text.get());
2256 // when we have to reinit the existing LyXText!
2258 it->second.text->init(bv);
2259 restoreLyXTextState(bv, it->second.text.get());
2260 it->second.remove = false;
2262 cached_text = it->second.text;
2263 if (the_locking_inset && recursive) {
2264 return the_locking_inset->getLyXText(bv);
2266 return cached_text.get();
2269 // we are here only if we don't have a BufferView * in the cache!!!
2271 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2272 cached_text->init(bv);
2273 restoreLyXTextState(bv, cached_text.get());
2275 cache.insert(make_pair(bv, cached_text));
2277 if (the_locking_inset && recursive) {
2278 return the_locking_inset->getLyXText(bv);
2280 return cached_text.get();
2284 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2288 Cache::iterator it = cache.find(bv);
2290 if (it == cache.end()) {
2294 lyx::Assert(it->second.text.get());
2296 it->second.remove = true;
2298 /// then remove all LyXText in text-insets
2299 Paragraph * p = par;
2300 for (; p; p = p->next()) {
2301 p->deleteInsetsLyXText(bv);
2307 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2310 // we cannot resize this because we are in use!
2311 // so do this on the next possible getLyXText()
2316 // lyxerr << "InsetText::resizeLyXText\n";
2317 if (!par->next() && !par->size()) { // no data, resize not neccessary!
2318 // we have to do this as a fixed width may have changed!
2319 LyXText * t = getLyXText(bv);
2320 saveLyXTextState(t);
2322 restoreLyXTextState(bv, t);
2325 // one endless line, resize normally not necessary
2326 if (!force && getMaxWidth(bv, this) < 0)
2329 Cache::iterator it = cache.find(bv);
2330 if (it == cache.end()) {
2333 lyx::Assert(it->second.text.get());
2335 LyXText * t = it->second.text.get();
2336 saveLyXTextState(t);
2337 for (Paragraph * p = par; p; p = p->next()) {
2338 p->resizeInsetsLyXText(bv);
2341 restoreLyXTextState(bv, t);
2342 if (the_locking_inset) {
2343 inset_x = cix(bv) - top_x + drawTextXOffset;
2344 inset_y = ciy(bv) + drawTextYOffset;
2348 t->first_y = bv->screen()->topCursorVisible(t);
2351 updateLocal(bv, FULL, false);
2352 // this will scroll the screen such that the cursor becomes visible
2353 bv->updateScrollbar();
2355 need_update |= FULL;
2360 void InsetText::reinitLyXText() const
2363 // we cannot resize this because we are in use!
2364 // so do this on the next possible getLyXText()
2370 // lyxerr << "InsetText::reinitLyXText\n";
2371 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2372 lyx::Assert(it->second.text.get());
2374 LyXText * t = it->second.text.get();
2375 BufferView * bv = it->first;
2377 saveLyXTextState(t);
2378 for (Paragraph * p = par; p; p = p->next()) {
2379 p->resizeInsetsLyXText(bv);
2382 restoreLyXTextState(bv, t);
2383 if (the_locking_inset) {
2384 inset_x = cix(bv) - top_x + drawTextXOffset;
2385 inset_y = ciy(bv) + drawTextYOffset;
2388 t->first_y = bv->screen()->topCursorVisible(t);
2391 updateLocal(bv, FULL, false);
2392 // this will scroll the screen such that the cursor becomes visible
2393 bv->updateScrollbar();
2401 void InsetText::removeNewlines()
2403 bool changed = false;
2405 for (Paragraph * p = par; p; p = p->next()) {
2406 for (int i = 0; i < p->size(); ++i) {
2407 if (p->getChar(i) == Paragraph::META_NEWLINE) {
2418 bool InsetText::nodraw() const
2420 if (the_locking_inset)
2421 return the_locking_inset->nodraw();
2422 return UpdatableInset::nodraw();
2426 int InsetText::scroll(bool recursive) const
2428 int sx = UpdatableInset::scroll(false);
2430 if (recursive && the_locking_inset)
2431 sx += the_locking_inset->scroll(recursive);
2437 bool InsetText::doClearArea() const
2439 return !locked || (need_update & (FULL|INIT));
2443 void InsetText::selectAll(BufferView * bv)
2445 getLyXText(bv)->cursorTop(bv);
2446 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2447 getLyXText(bv)->cursorBottom(bv);
2448 getLyXText(bv)->setSelection(bv);
2452 void InsetText::clearSelection(BufferView * bv)
2454 getLyXText(bv)->clearSelection();
2458 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2460 Painter & pain = bv->painter();
2462 int h = insetAscent + insetDescent;
2463 int ty = baseline - insetAscent;
2469 if ((ty + h) > pain.paperHeight())
2470 h = pain.paperHeight();
2471 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2472 w = pain.paperWidth();
2473 // w -= TEXT_TO_INSET_OFFSET;
2474 pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2477 frame_is_visible = false;
2481 Paragraph * InsetText::getParFromID(int id) const
2484 Paragraph * result = par;
2485 Paragraph * ires = 0;
2486 while (result && result->id() != id) {
2487 if ((ires = result->getParFromID(id)))
2489 result = result->next();
2493 Paragraph * tmp = par;
2495 if (tmp->id() == id) {
2498 Paragraph * tmp2 = tmp->getParFromID(id);
2509 Paragraph * InsetText::firstParagraph() const
2512 if (the_locking_inset)
2513 if ((result = the_locking_inset->firstParagraph()))
2519 Paragraph * InsetText::getFirstParagraph(int i) const
2521 return (i == 0) ? par : 0;
2525 LyXCursor const & InsetText::cursor(BufferView * bv) const
2527 if (the_locking_inset)
2528 return the_locking_inset->cursor(bv);
2529 return getLyXText(bv)->cursor;
2533 Paragraph * InsetText::paragraph() const
2539 void InsetText::paragraph(Paragraph * p)
2541 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2542 // caller of this function has to take care of it. This IS important
2543 // as we could have to insert a paragraph before this one and just
2544 // link the actual to a new ones next and set it with this function
2547 // set ourself as owner for all the paragraphs inserted!
2548 Paragraph * np = par;
2550 np->setInsetOwner(this);
2554 // redraw myself when asked for
2559 Inset * InsetText::getInsetFromID(int id_arg) const
2562 return const_cast<InsetText *>(this);
2564 Paragraph * lp = par;
2567 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2568 en = lp->inset_iterator_end();
2571 if ((*it)->id() == id_arg)
2573 Inset * in = (*it)->getInsetFromID(id_arg);
2583 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2589 lt = getLyXText(bv);
2592 if (the_locking_inset) {
2593 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2600 // we have to go on checking so move cusor to the next char
2601 lt->cursor.pos(lt->cursor.pos() + 1);
2603 str = lt->selectNextWordToSpellcheck(bv, value);
2605 bv->unlockInset(const_cast<InsetText *>(this));
2614 void InsetText::selectSelectedWord(BufferView * bv)
2616 if (the_locking_inset) {
2617 the_locking_inset->selectSelectedWord(bv);
2620 getLyXText(bv)->selectSelectedWord(bv);
2621 updateLocal(bv, SELECTION, false);
2625 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2627 if (the_locking_inset) {
2628 the_locking_inset->toggleSelection(bv, kill_selection);
2632 lt = getLyXText(bv);
2636 int x = top_x + TEXT_TO_INSET_OFFSET;
2638 Row * row = lt->firstRow();
2639 int y_offset = top_baseline - row->ascent_of_text();
2641 while ((row != 0) && ((y+row->height()) <= 0)) {
2648 if (need_update & SELECTION)
2650 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2656 bool InsetText::searchForward(BufferView * bv, string const & str,
2659 if (the_locking_inset) {
2660 if (the_locking_inset->searchForward(bv, str, cs, mw))
2664 lt = getLyXText(bv);
2667 Paragraph * lpar = lt->cursor.par();
2668 pos_type pos = lt->cursor.pos();
2669 if (pos < lpar->size() - 1)
2673 lpar = lpar->next();
2678 // we have to unlock ourself in this function by default!
2679 bv->unlockInset(const_cast<InsetText *>(this));
2682 lt->setCursor(bv, lpar, pos);
2686 if (LyXFind(bv, str, true, true, cs , mw)) {
2689 // we have to unlock ourself in this function by default!
2690 bv->unlockInset(const_cast<InsetText *>(this));
2694 bool InsetText::searchBackward(BufferView * bv, string const & str,
2697 if (the_locking_inset)
2698 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2700 if (LyXFind(bv, str, false, true, cs, mw)) {
2703 // we have to unlock ourself in this function by default!
2704 bv->unlockInset(const_cast<InsetText *>(this));
2709 bool InsetText::checkInsertChar(LyXFont & font)
2712 return owner()->checkInsertChar(font);
2717 void InsetText::collapseParagraphs(BufferView * bv) const
2719 BufferParams const & bparams = bv->buffer()->params;
2720 LyXText * llt = getLyXText(bv);
2722 while(par->next()) {
2723 if (par->size() && par->next()->size() &&
2724 !par->isSeparator(par->size()-1))
2726 par->insertChar(par->size(), ' ');
2728 if (llt->selection.set()) {
2729 if (llt->selection.start.par() == par->next()) {
2730 llt->selection.start.par(par);
2731 llt->selection.start.pos(
2732 llt->selection.start.pos() + par->size());
2734 if (llt->selection.end.par() == par->next()) {
2735 llt->selection.end.par(par);
2736 llt->selection.end.pos(
2737 llt->selection.end.pos() + par->size());
2740 par->pasteParagraph(bparams);
2746 void InsetText::getDrawFont(LyXFont & font) const
2750 owner()->getDrawFont(font);
2754 void InsetText::appendParagraphs(BufferParams const & bparams,
2758 Paragraph * tmpbuf = newpar;
2759 Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2761 while (tmpbuf->next()) {
2762 tmpbuf = tmpbuf->next();
2763 lastbuffer->next(new Paragraph(*tmpbuf, false));
2764 lastbuffer->next()->previous(lastbuffer);
2765 lastbuffer = lastbuffer->next();
2768 while (lastbuffer->next())
2769 lastbuffer = lastbuffer->next();
2770 if (newpar->size() && lastbuffer->size() &&
2771 !lastbuffer->isSeparator(lastbuffer->size()-1))
2773 lastbuffer->insertChar(lastbuffer->size(), ' ');
2776 // make the buf exactly the same layout than our last paragraph
2777 buf->makeSameLayout(lastbuffer);
2780 lastbuffer->next(buf);
2781 buf->previous(lastbuffer);
2782 lastbuffer->pasteParagraph(bparams);