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();
455 lt->first_y = -y_offset;
462 if (cleared || (need_update&(INIT|FULL))) {
463 int yf = y_offset + first;
465 while ((row != 0) && (yf < ph)) {
466 lt->getVisibleRow(bv, y+y_offset+first, int(x), row,
467 y+lt->first_y, cleared);
468 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
469 lt->need_break_row = row;
470 if (lt->fullRebreak(bv)) {
471 lt->setCursor(bv, lt->cursor.par(),lt->cursor.pos());
472 if (lt->selection.set()) {
473 lt->setCursor(bv, lt->selection.start,
474 lt->selection.start.par(),
475 lt->selection.start.pos());
476 lt->setCursor(bv, lt->selection.end,
477 lt->selection.end.par(),
478 lt->selection.end.pos());
487 } else if (!locked) {
488 if (need_update & CURSOR) {
489 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
490 lt->clearSelection();
491 lt->selection.cursor = lt->cursor;
493 bv->screen()->update(lt, bv, y_offset, int(x));
496 if (need_update & SELECTION) {
497 bv->screen()->toggleToggle(lt, bv, y_offset, int(x));
498 } else if (need_update & CURSOR) {
499 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
500 lt->clearSelection();
501 lt->selection.cursor = lt->cursor;
503 bv->screen()->update(lt, bv, y_offset, int(x));
508 lt->status(bv, LyXText::UNCHANGED);
509 if ((drawFrame_ == ALWAYS) ||
510 ((cleared || (need_update != CURSOR_PAR)) &&
511 (drawFrame_ == LOCKED) && locked))
513 drawFrame(pain, cleared);
514 } else if (need_update & CLEAR_FRAME) {
515 clearFrame(pain, cleared);
518 x += insetWidth - TEXT_TO_INSET_OFFSET;
520 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
522 } else if (need_update != INIT) {
530 void InsetText::drawFrame(Painter & pain, bool cleared) const
532 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
533 if (!frame_is_visible || cleared) {
534 frame_x = top_x + ttoD2;
535 frame_y = top_baseline - insetAscent + ttoD2;
536 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
537 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
538 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
540 frame_is_visible = true;
545 void InsetText::clearFrame(Painter & pain, bool cleared) const
547 if (frame_is_visible) {
549 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
552 frame_is_visible = false;
557 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
560 if (reinit && owner()) {
562 owner()->update(bv, font, true);
567 if (reinit || need_update == INIT) {
569 // we should put this call where we set need_update to INIT!
572 owner()->update(bv, font, true);
577 if (!autoBreakRows && par->next())
578 collapseParagraphs(bv);
580 if (the_locking_inset) {
581 inset_x = cix(bv) - top_x + drawTextXOffset;
582 inset_y = ciy(bv) + drawTextYOffset;
583 the_locking_inset->update(bv, font, reinit);
591 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
594 lt->updateInset(bv, the_locking_inset);
596 if (lt->status() == LyXText::NEED_MORE_REFRESH)
604 void InsetText::setUpdateStatus(BufferView * bv, int what) const
606 // this does nothing dangerous so use only a localized buffer
607 LyXText * llt = getLyXText(bv);
610 // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
611 // if we don't break row so that we only have one row to update!
612 if ((llt->status() == LyXText::NEED_MORE_REFRESH) ||
614 (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
617 } else if (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
618 need_update |= CURSOR_PAR;
621 // this to not draw a selection when we redraw all of it!
622 if (need_update & CURSOR && !(need_update & SELECTION)) {
623 if (llt->selection.set())
625 llt->clearSelection();
630 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
632 if (!autoBreakRows && par->next())
633 collapseParagraphs(bv);
640 setUpdateStatus(bv, what);
641 bool flag = mark_dirty ||
642 (((need_update != CURSOR) && (need_update != NONE)) ||
643 (lt->status() != LyXText::UNCHANGED) || lt->selection.set());
644 if (!lt->selection.set())
645 lt->selection.cursor = lt->cursor;
649 // IMO this is not anymore needed as we do this in fitInsetCursor!
650 // and we always get "true" as returnvalue of this function in the
651 // case of a locking inset (Jug 20020412)
652 if (locked && (need_update & CURSOR) && bv->fitCursor())
658 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
660 if (need_update == CURSOR)
662 bv->owner()->showState();
663 bv->owner()->updateMenubar();
664 bv->owner()->updateToolbar();
665 if (old_par != cpar(bv)) {
666 bv->owner()->setLayout(cpar(bv)->layout());
672 string const InsetText::editMessage() const
674 return _("Opened Text Inset");
678 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
680 UpdatableInset::edit(bv, x, y, button);
682 if (!bv->lockInset(this)) {
683 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
687 the_locking_inset = 0;
688 inset_pos = inset_x = inset_y = 0;
689 inset_boundary = false;
692 int tmp_y = (y < 0) ? 0 : y;
698 if (!checkAndActivateInset(bv, x, tmp_y, button)) {
699 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
701 lt->cursor.x_fix(lt->cursor.x());
703 lt->clearSelection();
705 // If the inset is empty set the language of the current font to the
706 // language to the surronding text (if different).
707 if (par->size() == 0 && !par->next() &&
708 bv->getParentLanguage(this) != lt->current_font.language())
710 LyXFont font(LyXFont::ALL_IGNORE);
711 font.setLanguage(bv->getParentLanguage(this));
712 setFont(bv, font, false);
718 if (drawFrame_ == LOCKED)
719 code = CURSOR|DRAW_FRAME;
720 updateLocal(bv, code, false);
723 // Tell the paragraph dialog that we've entered an insettext.
724 bv->owner()->getDialogs()->updateParagraph();
728 void InsetText::edit(BufferView * bv, bool front)
730 UpdatableInset::edit(bv, front);
732 if (!bv->lockInset(this)) {
733 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
737 the_locking_inset = 0;
738 inset_pos = inset_x = inset_y = 0;
739 inset_boundary = false;
748 lt->setCursor(bv, par, 0);
753 // int const pos = (p->size() ? p->size()-1 : p->size());
754 lt->setCursor(bv, p, p->size());
756 lt->clearSelection();
758 // If the inset is empty set the language of the current font to the
759 // language to the surronding text (if different).
760 if (par->size() == 0 && !par->next() &&
761 bv->getParentLanguage(this) != lt->current_font.language()) {
762 LyXFont font(LyXFont::ALL_IGNORE);
763 font.setLanguage(bv->getParentLanguage(this));
764 setFont(bv, font, false);
769 if (drawFrame_ == LOCKED)
770 code = CURSOR|DRAW_FRAME;
771 updateLocal(bv, code, false);
776 void InsetText::insetUnlock(BufferView * bv)
778 if (the_locking_inset) {
779 the_locking_inset->insetUnlock(bv);
780 the_locking_inset = 0;
781 updateLocal(bv, CURSOR_PAR, false);
788 if (drawFrame_ == LOCKED)
789 code = CURSOR|CLEAR_FRAME;
793 if (drawFrame_ == LOCKED)
801 if (lt->selection.set()) {
802 lt->clearSelection();
804 } else if (owner()) {
805 bv->owner()->setLayout(owner()->getLyXText(bv)
806 ->cursor.par()->layout());
808 bv->owner()->setLayout(bv->text->cursor.par()->layout());
809 // hack for deleteEmptyParMech
811 lt->setCursor(bv, par, 0);
812 } else if (par->next()) {
813 lt->setCursor(bv, par->next(), 0);
818 updateLocal(bv, code, false);
821 setUpdateStatus(bv, code);
826 void InsetText::lockInset(BufferView * bv)
829 the_locking_inset = 0;
830 inset_pos = inset_x = inset_y = 0;
831 inset_boundary = false;
839 lt->setCursor(bv, par, 0);
840 lt->clearSelection();
842 // If the inset is empty set the language of the current font to the
843 // language to the surronding text (if different).
844 if (par->size() == 0 && !par->next() &&
845 bv->getParentLanguage(this) != lt->current_font.language()) {
846 LyXFont font(LyXFont::ALL_IGNORE);
847 font.setLanguage(bv->getParentLanguage(this));
848 setFont(bv, font, false);
853 if (drawFrame_ == LOCKED)
854 code = CURSOR|DRAW_FRAME;
855 setUpdateStatus(bv, code);
859 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
861 the_locking_inset = inset;
862 inset_x = cix(bv) - top_x + drawTextXOffset;
863 inset_y = ciy(bv) + drawTextYOffset;
864 inset_pos = cpos(bv);
865 inset_par = cpar(bv);
866 inset_boundary = cboundary(bv);
867 updateLocal(bv, CURSOR, false);
871 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
873 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
877 if (!the_locking_inset) {
879 int const id = inset->id();
881 Paragraph::inset_iterator it =
882 p->inset_iterator_begin();
883 Paragraph::inset_iterator const end =
884 p->inset_iterator_end();
885 for (; it != end; ++it) {
886 if ((*it) == inset) {
887 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
888 lockInset(bv, inset);
891 if ((*it)->getInsetFromID(id)) {
892 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
894 return the_locking_inset->lockInsetInInset(bv, inset);
901 if (inset == cpar(bv)->getInset(cpos(bv))) {
902 lyxerr[Debug::INSETS] << "OK" << endl;
903 lockInset(bv, inset);
905 } else if (the_locking_inset && (the_locking_inset == inset)) {
906 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
907 lyxerr[Debug::INSETS] << "OK" << endl;
908 inset_x = cix(bv) - top_x + drawTextXOffset;
909 inset_y = ciy(bv) + drawTextYOffset;
911 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
913 } else if (the_locking_inset) {
914 lyxerr[Debug::INSETS] << "MAYBE" << endl;
915 return the_locking_inset->lockInsetInInset(bv, inset);
917 lyxerr[Debug::INSETS] << "NOT OK" << endl;
922 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
925 if (!the_locking_inset)
927 if (the_locking_inset == inset) {
928 the_locking_inset->insetUnlock(bv);
929 getLyXText(bv)->updateInset(bv, inset);
930 the_locking_inset = 0;
932 moveRight(bv, false);
933 old_par = 0; // force layout setting
937 updateLocal(bv, CURSOR, false);
940 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
944 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
946 if (!autoBreakRows && par->next())
947 collapseParagraphs(bv);
955 if (inset->owner() != this) {
956 int ustat = CURSOR_PAR;
958 UpdatableInset * tl_inset = the_locking_inset;
960 found = tl_inset->updateInsetInInset(bv, inset);
962 tl_inset = static_cast<UpdatableInset *>(inset);
963 while(tl_inset->owner() && tl_inset->owner() != this)
964 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
965 if (!tl_inset->owner())
967 found = tl_inset->updateInsetInInset(bv, inset);
971 lt->updateInset(bv, tl_inset);
975 setUpdateStatus(bv, ustat);
978 bool found = lt->updateInset(bv, inset);
982 setUpdateStatus(bv, CURSOR_PAR);
983 if (the_locking_inset &&
984 cpar(bv) == inset_par && cpos(bv) == inset_pos)
986 inset_x = cix(bv) - top_x + drawTextXOffset;
987 inset_y = ciy(bv) + drawTextYOffset;
994 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
998 // use this to check mouse motion for selection!
1005 int tmp_x = x - drawTextXOffset;
1006 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
1007 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1009 hideInsetCursor(bv);
1010 if (the_locking_inset) {
1011 if (the_locking_inset == inset) {
1012 the_locking_inset->insetButtonPress(bv,
1020 // otherwise unlock the_locking_inset and lock the new inset
1021 the_locking_inset->insetUnlock(bv);
1022 inset_x = cix(bv) - top_x + drawTextXOffset;
1023 inset_y = ciy(bv) + drawTextYOffset;
1024 the_locking_inset = 0;
1025 inset->insetButtonPress(bv, x - inset_x,
1026 y - inset_y, button);
1027 // inset->edit(bv, x - inset_x, y - inset_y, button);
1028 if (the_locking_inset)
1029 updateLocal(bv, CURSOR, false);
1033 // otherwise only unlock the_locking_inset
1034 the_locking_inset->insetUnlock(bv);
1035 the_locking_inset = 0;
1038 no_selection = false;
1040 if (bv->theLockingInset()) {
1041 if (isHighlyEditableInset(inset)) {
1042 // We just have to lock the inset before calling a
1043 // PressEvent on it!
1044 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
1045 if (!bv->lockInset(uinset)) {
1046 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1048 inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
1049 if (the_locking_inset)
1050 updateLocal(bv, CURSOR, false);
1054 if (!inset) { // && (button == 2)) {
1055 bool paste_internally = false;
1056 if ((button == 2) && getLyXText(bv)->selection.set()) {
1057 localDispatch(bv, LFUN_COPY, "");
1058 paste_internally = true;
1062 lt = getLyXText(bv);
1065 int old_first_y = lt->first_y;
1067 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
1069 // set the selection cursor!
1070 lt->selection.cursor = lt->cursor;
1071 lt->cursor.x_fix(lt->cursor.x());
1073 if (lt->selection.set()) {
1074 lt->clearSelection();
1077 updateLocal(bv, FULL, false);
1079 lt->clearSelection();
1082 updateLocal(bv, CURSOR, false);
1084 bv->owner()->setLayout(cpar(bv)->layout());
1085 // we moved the view we cannot do mouse selection in this case!
1086 if (getLyXText(bv)->first_y != old_first_y)
1087 no_selection = true;
1089 // Insert primary selection with middle mouse
1090 // if there is a local selection in the current buffer,
1093 if (paste_internally)
1094 localDispatch(bv, LFUN_PASTE, "");
1096 localDispatch(bv, LFUN_PASTESELECTION,
1100 getLyXText(bv)->clearSelection();
1102 showInsetCursor(bv);
1106 bool InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
1108 no_selection = true;
1109 if (the_locking_inset) {
1110 return the_locking_inset->insetButtonRelease(bv,
1111 x - inset_x, y - inset_y,
1114 int tmp_x = x - drawTextXOffset;
1115 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
1116 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1119 if (isHighlyEditableInset(inset)) {
1120 ret = inset->insetButtonRelease(bv, x - inset_x,
1121 y - inset_y, button);
1123 inset_x = cix(bv) - top_x + drawTextXOffset;
1124 inset_y = ciy(bv) + drawTextYOffset;
1125 ret = inset->insetButtonRelease(bv, x - inset_x,
1126 y - inset_y, button);
1127 inset->edit(bv, x - inset_x,
1128 y - inset_y, button);
1130 updateLocal(bv, CURSOR_PAR, false);
1136 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
1138 if (the_locking_inset) {
1139 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1144 if (no_selection || ((mouse_x == x) && (mouse_y == y)))
1149 lt = getLyXText(bv);
1152 hideInsetCursor(bv);
1153 LyXCursor cur = lt->cursor;
1154 lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1155 lt->cursor.x_fix(lt->cursor.x());
1156 if (cur == lt->cursor) {
1161 lt->setSelection(bv);
1162 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1163 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1167 updateLocal(bv, SELECTION, false);
1169 showInsetCursor(bv);
1173 void InsetText::insetKeyPress(XKeyEvent * xke)
1175 if (the_locking_inset) {
1176 the_locking_inset->insetKeyPress(xke);
1182 UpdatableInset::RESULT
1183 InsetText::localDispatch(BufferView * bv,
1184 kb_action action, string const & arg)
1186 bool was_empty = par->size() == 0 && !par->next();
1187 no_selection = false;
1188 UpdatableInset::RESULT
1189 result= UpdatableInset::localDispatch(bv, action, arg);
1190 if (result != UNDISPATCHED) {
1194 result = DISPATCHED;
1195 if ((action < 0) && arg.empty())
1198 if (the_locking_inset) {
1199 result = the_locking_inset->localDispatch(bv, action, arg);
1200 if (result == DISPATCHED_NOUPDATE)
1202 else if (result == DISPATCHED) {
1203 updateLocal(bv, CURSOR_PAR, false);
1205 } else if (result >= FINISHED) {
1207 case FINISHED_RIGHT:
1208 moveRightIntern(bv, false, false);
1209 result = DISPATCHED;
1212 if ((result = moveUp(bv)) >= FINISHED) {
1213 updateLocal(bv, CURSOR, false);
1214 bv->unlockInset(this);
1218 if ((result = moveDown(bv)) >= FINISHED) {
1219 updateLocal(bv, CURSOR, false);
1220 bv->unlockInset(this);
1224 result = DISPATCHED;
1227 the_locking_inset = 0;
1228 #ifdef WITH_WARNINGS
1229 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
1231 updateLocal(bv, CURSOR, false);
1235 hideInsetCursor(bv);
1238 lt = getLyXText(bv);
1242 int updflag = false;
1245 case LFUN_SELFINSERT:
1246 if (bv->buffer()->isReadonly()) {
1247 // setErrorMessage(N_("Document is read only"));
1251 /* Automatically delete the currently selected
1252 * text and replace it with what is being
1253 * typed in now. Depends on lyxrc settings
1254 * "auto_region_delete", which defaults to
1257 setUndo(bv, Undo::INSERT,
1258 lt->cursor.par(), lt->cursor.par()->next());
1260 if (lyxrc.auto_region_delete) {
1261 if (lt->selection.set()) {
1262 lt->cutSelection(bv, false);
1265 lt->clearSelection();
1266 for (string::size_type i = 0; i < arg.length(); ++i) {
1267 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1270 lt->selection.cursor = lt->cursor;
1271 updwhat = CURSOR | CURSOR_PAR;
1273 result = DISPATCHED_NOUPDATE;
1275 // --- Cursor Movements -----------------------------------
1278 moveRight(bv, false, true);
1279 lt->setSelection(bv);
1280 updwhat = SELECTION;
1283 result = moveRight(bv);
1289 moveLeft(bv, false, true);
1290 lt->setSelection(bv);
1291 updwhat = SELECTION;
1295 result = moveLeft(bv);
1301 lt->setSelection(bv);
1302 updwhat = SELECTION;
1306 result = moveDown(bv);
1312 lt->setSelection(bv);
1313 updwhat = SELECTION;
1317 result = moveUp(bv);
1329 case LFUN_BACKSPACE: {
1330 setUndo(bv, Undo::DELETE,
1331 lt->cursor.par(), lt->cursor.par()->next());
1332 if (lt->selection.set())
1333 lt->cutSelection(bv);
1336 updwhat = CURSOR_PAR;
1342 setUndo(bv, Undo::DELETE,
1343 lt->cursor.par(), lt->cursor.par()->next());
1344 if (lt->selection.set()) {
1345 lt->cutSelection(bv);
1349 updwhat = CURSOR_PAR;
1355 setUndo(bv, Undo::DELETE,
1356 lt->cursor.par(), lt->cursor.par()->next());
1357 lt->cutSelection(bv);
1358 updwhat = CURSOR_PAR;
1365 lt->copySelection(bv);
1366 updwhat = CURSOR_PAR;
1368 case LFUN_PASTESELECTION:
1370 string const clip(bv->getClipboard());
1374 if (arg == "paragraph") {
1375 lt->insertStringAsParagraphs(bv, clip);
1377 lt->insertStringAsLines(bv, clip);
1379 updwhat = CURSOR_PAR;
1384 if (!autoBreakRows) {
1386 if (CutAndPaste::nrOfParagraphs() > 1) {
1387 Alert::alert(_("Impossible operation"),
1388 _("Cannot include more than one paragraph!"),
1393 setUndo(bv, Undo::INSERT,
1394 lt->cursor.par(), lt->cursor.par()->next());
1395 lt->pasteSelection(bv);
1396 updwhat = CURSOR_PAR;
1401 case LFUN_BREAKPARAGRAPH:
1402 if (!autoBreakRows) {
1403 result = DISPATCHED;
1406 lt->breakParagraph(bv, 0);
1407 updwhat = CURSOR | FULL;
1410 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1411 if (!autoBreakRows) {
1412 result = DISPATCHED;
1415 lt->breakParagraph(bv, 1);
1416 updwhat = CURSOR | FULL;
1420 case LFUN_BREAKLINE: {
1421 if (!autoBreakRows) {
1422 result = DISPATCHED;
1425 setUndo(bv, Undo::INSERT,
1426 lt->cursor.par(), lt->cursor.par()->next());
1427 lt->insertChar(bv, Paragraph::META_NEWLINE);
1428 updwhat = CURSOR | CURSOR_PAR;
1434 // do not set layouts on non breakable textinsets
1435 if (autoBreakRows) {
1436 string cur_layout = cpar(bv)->layout();
1438 // Derive layout number from given argument (string)
1439 // and current buffer's textclass (number). */
1440 textclass_type tclass = bv->buffer()->params.textclass;
1441 string layout = arg;
1442 bool hasLayout = textclasslist[tclass].hasLayout(layout);
1444 // If the entry is obsolete, use the new one instead.
1446 string const & obs =
1447 textclasslist[tclass][layout].
1453 // see if we found the layout number:
1455 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1456 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1460 if (cur_layout != layout) {
1461 cur_layout = layout;
1462 lt->setLayout(bv, layout);
1463 bv->owner()->setLayout(cpar(bv)->layout());
1464 updwhat = CURSOR_PAR;
1468 // reset the layout box
1469 bv->owner()->setLayout(cpar(bv)->layout());
1472 case LFUN_PARAGRAPH_SPACING:
1473 // This one is absolutely not working. When fiddling with this
1474 // it also seems to me that the paragraphs inside the insettext
1475 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1477 Paragraph * par = lt->cursor.par();
1478 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1479 float cur_value = 1.0;
1480 if (cur_spacing == Spacing::Other) {
1481 cur_value = par->params().spacing().getValue();
1484 istringstream istr(arg.c_str());
1487 Spacing::Space new_spacing = cur_spacing;
1488 float new_value = cur_value;
1490 lyxerr << "Missing argument to `paragraph-spacing'"
1492 } else if (tmp == "single") {
1493 new_spacing = Spacing::Single;
1494 } else if (tmp == "onehalf") {
1495 new_spacing = Spacing::Onehalf;
1496 } else if (tmp == "double") {
1497 new_spacing = Spacing::Double;
1498 } else if (tmp == "other") {
1499 new_spacing = Spacing::Other;
1502 lyxerr << "new_value = " << tmpval << endl;
1505 } else if (tmp == "default") {
1506 new_spacing = Spacing::Default;
1508 lyxerr << _("Unknown spacing argument: ")
1511 if (cur_spacing != new_spacing || cur_value != new_value) {
1512 par->params().spacing(Spacing(new_spacing, new_value));
1513 updwhat = CURSOR_PAR;
1520 if (!bv->Dispatch(action, arg))
1521 result = UNDISPATCHED;
1528 updateLocal(bv, updwhat, updflag);
1529 /// If the action has deleted all text in the inset, we need to change the
1530 // language to the language of the surronding text.
1531 if (!was_empty && par->size() == 0 && !par->next()) {
1532 LyXFont font(LyXFont::ALL_IGNORE);
1533 font.setLanguage(bv->getParentLanguage(this));
1534 setFont(bv, font, false);
1537 if (result >= FINISHED)
1538 bv->unlockInset(this);
1540 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1541 result = DISPATCHED;
1546 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1549 buf->latexParagraphs(os, par, 0, texrow);
1550 return texrow.rows();
1554 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1556 Paragraph * p = par;
1557 unsigned int lines = 0;
1560 string const tmp = buf->asciiParagraph(p, linelen, p->previous()==0);
1561 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1569 int InsetText::docbook(Buffer const * buf, ostream & os) const
1571 Paragraph * p = par;
1572 unsigned int lines = 0;
1574 vector<string> environment_stack(10);
1575 vector<string> environment_inner(10);
1577 int const command_depth = 0;
1580 Paragraph::depth_type depth = 0; // paragraph depth
1584 int desc_on = 0; // description mode
1586 LyXLayout const & style =
1587 textclasslist[buf->params.textclass][p->layout()];
1589 // environment tag closing
1590 for (; depth > p->params().depth(); --depth) {
1591 if (environment_inner[depth] != "!-- --") {
1592 item_name = "listitem";
1593 buf->sgmlCloseTag(os, command_depth + depth,
1595 if (environment_inner[depth] == "varlistentry")
1596 buf->sgmlCloseTag(os, depth+command_depth,
1597 environment_inner[depth]);
1599 buf->sgmlCloseTag(os, depth + command_depth,
1600 environment_stack[depth]);
1601 environment_stack[depth].erase();
1602 environment_inner[depth].erase();
1605 if (depth == p->params().depth()
1606 && environment_stack[depth] != style.latexname()
1607 && !environment_stack[depth].empty()) {
1608 if (environment_inner[depth] != "!-- --") {
1609 item_name= "listitem";
1610 buf->sgmlCloseTag(os, command_depth+depth,
1612 if (environment_inner[depth] == "varlistentry")
1613 buf->sgmlCloseTag(os,
1614 depth + command_depth,
1615 environment_inner[depth]);
1618 buf->sgmlCloseTag(os, depth + command_depth,
1619 environment_stack[depth]);
1621 environment_stack[depth].erase();
1622 environment_inner[depth].erase();
1625 // Write opening SGML tags.
1626 switch (style.latextype) {
1627 case LATEX_PARAGRAPH:
1628 buf->sgmlOpenTag(os, depth + command_depth,
1633 buf->sgmlError(p, 0,
1634 _("Error : LatexType Command not allowed here.\n"));
1638 case LATEX_ENVIRONMENT:
1639 case LATEX_ITEM_ENVIRONMENT:
1640 if (depth < p->params().depth()) {
1641 depth = p->params().depth();
1642 environment_stack[depth].erase();
1645 if (environment_stack[depth] != style.latexname()) {
1646 if (environment_stack.size() == depth + 1) {
1647 environment_stack.push_back("!-- --");
1648 environment_inner.push_back("!-- --");
1650 environment_stack[depth] = style.latexname();
1651 environment_inner[depth] = "!-- --";
1652 buf->sgmlOpenTag(os, depth + command_depth,
1653 environment_stack[depth]);
1655 if (environment_inner[depth] != "!-- --") {
1656 item_name= "listitem";
1657 buf->sgmlCloseTag(os,
1658 command_depth + depth,
1660 if (environment_inner[depth] == "varlistentry")
1661 buf->sgmlCloseTag(os,
1662 depth + command_depth,
1663 environment_inner[depth]);
1667 if (style.latextype == LATEX_ENVIRONMENT) {
1668 if (!style.latexparam().empty()) {
1669 if (style.latexparam() == "CDATA")
1672 buf->sgmlOpenTag(os, depth + command_depth,
1673 style.latexparam());
1678 desc_on = (style.labeltype == LABEL_MANUAL);
1681 environment_inner[depth]= "varlistentry";
1683 environment_inner[depth]= "listitem";
1685 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1686 environment_inner[depth]);
1690 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1694 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1699 buf->sgmlOpenTag(os, depth + command_depth,
1704 buf->simpleDocBookOnePar(os, p, desc_on,
1705 depth + 1 + command_depth);
1709 // write closing SGML tags
1710 switch (style.latextype) {
1711 case LATEX_ENVIRONMENT:
1712 if (!style.latexparam().empty()) {
1713 if (style.latexparam() == "CDATA")
1716 buf->sgmlCloseTag(os, depth + command_depth,
1717 style.latexparam());
1720 case LATEX_ITEM_ENVIRONMENT:
1721 if (desc_on == 1) break;
1723 buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1725 case LATEX_PARAGRAPH:
1726 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1729 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1735 for (int d = depth; d >= 0; --d) {
1736 if (!environment_stack[depth].empty()) {
1737 if (environment_inner[depth] != "!-- --") {
1738 item_name = "listitem";
1739 buf->sgmlCloseTag(os, command_depth + depth,
1741 if (environment_inner[depth] == "varlistentry")
1742 buf->sgmlCloseTag(os, depth + command_depth,
1743 environment_inner[depth]);
1746 buf->sgmlCloseTag(os, depth + command_depth,
1747 environment_stack[depth]);
1755 void InsetText::validate(LaTeXFeatures & features) const
1757 Paragraph * p = par;
1759 p->validate(features);
1765 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1767 if (textclasslist[buf->params.textclass][p->layout()].labeltype != LABEL_MANUAL)
1770 return p->beginningOfMainBody();
1774 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1776 if (the_locking_inset) {
1777 the_locking_inset->getCursorPos(bv, x, y);
1780 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1781 y = cy(bv) - TEXT_TO_INSET_OFFSET;
1785 int InsetText::insetInInsetY() const
1787 if (!the_locking_inset)
1790 return (inset_y + the_locking_inset->insetInInsetY());
1794 void InsetText::toggleInsetCursor(BufferView * bv)
1796 if (the_locking_inset) {
1797 the_locking_inset->toggleInsetCursor(bv);
1801 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1803 int const asc = lyxfont::maxAscent(font);
1804 int const desc = lyxfont::maxDescent(font);
1806 if (isCursorVisible())
1807 bv->hideLockedInsetCursor();
1809 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1810 toggleCursorVisible();
1814 void InsetText::showInsetCursor(BufferView * bv, bool show)
1816 if (the_locking_inset) {
1817 the_locking_inset->showInsetCursor(bv, show);
1820 if (!isCursorVisible()) {
1821 LyXFont const font =
1822 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1824 int const asc = lyxfont::maxAscent(font);
1825 int const desc = lyxfont::maxDescent(font);
1827 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1829 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1830 setCursorVisible(true);
1835 void InsetText::hideInsetCursor(BufferView * bv)
1837 if (isCursorVisible()) {
1838 bv->hideLockedInsetCursor();
1839 setCursorVisible(false);
1841 if (the_locking_inset)
1842 the_locking_inset->hideInsetCursor(bv);
1846 void InsetText::fitInsetCursor(BufferView * bv) const
1848 if (the_locking_inset) {
1849 the_locking_inset->fitInsetCursor(bv);
1852 LyXFont const font =
1853 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1855 int const asc = lyxfont::maxAscent(font);
1856 int const desc = lyxfont::maxDescent(font);
1858 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1859 need_update |= FULL;
1863 UpdatableInset::RESULT
1864 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1866 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1867 return moveLeftIntern(bv, true, activate_inset, selecting);
1869 return moveRightIntern(bv, true, activate_inset, selecting);
1873 UpdatableInset::RESULT
1874 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1876 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1877 return moveRightIntern(bv, false, activate_inset, selecting);
1879 return moveLeftIntern(bv, false, activate_inset, selecting);
1883 UpdatableInset::RESULT
1884 InsetText::moveRightIntern(BufferView * bv, bool front,
1885 bool activate_inset, bool selecting)
1887 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1888 return FINISHED_RIGHT;
1889 if (activate_inset && checkAndActivateInset(bv, front))
1891 getLyXText(bv)->cursorRight(bv);
1893 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1894 return DISPATCHED_NOUPDATE;
1898 UpdatableInset::RESULT
1899 InsetText::moveLeftIntern(BufferView * bv, bool front,
1900 bool activate_inset, bool selecting)
1902 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1904 getLyXText(bv)->cursorLeft(bv);
1906 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1907 if (activate_inset && checkAndActivateInset(bv, front))
1909 return DISPATCHED_NOUPDATE;
1913 UpdatableInset::RESULT
1914 InsetText::moveUp(BufferView * bv)
1916 if (!crow(bv)->previous())
1918 getLyXText(bv)->cursorUp(bv);
1919 return DISPATCHED_NOUPDATE;
1923 UpdatableInset::RESULT
1924 InsetText::moveDown(BufferView * bv)
1926 if (!crow(bv)->next())
1927 return FINISHED_DOWN;
1928 getLyXText(bv)->cursorDown(bv);
1929 return DISPATCHED_NOUPDATE;
1933 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1935 if (the_locking_inset) {
1936 if (the_locking_inset->insetAllowed(inset))
1937 return the_locking_inset->insertInset(bv, inset);
1940 inset->setOwner(this);
1941 hideInsetCursor(bv);
1942 getLyXText(bv)->insertInset(bv, inset);
1944 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1949 bool InsetText::insetAllowed(Inset::Code code) const
1951 // in_insetAllowed is a really gross hack,
1952 // to allow us to call the owner's insetAllowed
1953 // without stack overflow, which can happen
1954 // when the owner uses InsetCollapsable::insetAllowed()
1956 if (in_insetAllowed)
1958 in_insetAllowed = true;
1959 if (the_locking_inset)
1960 ret = the_locking_inset->insetAllowed(code);
1962 ret = owner()->insetAllowed(code);
1963 in_insetAllowed = false;
1968 UpdatableInset * InsetText::getLockingInset() const
1970 return the_locking_inset ? the_locking_inset->getLockingInset() :
1971 const_cast<InsetText *>(this);
1975 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1979 if (the_locking_inset)
1980 return the_locking_inset->getFirstLockingInsetOfType(c);
1985 bool InsetText::showInsetDialog(BufferView * bv) const
1987 if (the_locking_inset)
1988 return the_locking_inset->showInsetDialog(bv);
1993 vector<string> const InsetText::getLabelList() const
1995 vector<string> label_list;
1997 Paragraph * tpar = par;
1999 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
2000 Paragraph::inset_iterator end = tpar->inset_iterator_end();
2001 for (; beg != end; ++beg) {
2002 vector<string> const l = (*beg)->getLabelList();
2003 label_list.insert(label_list.end(), l.begin(), l.end());
2005 tpar = tpar->next();
2011 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
2014 if (the_locking_inset) {
2015 the_locking_inset->setFont(bv, font, toggleall, selectall);
2018 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
2019 getLyXText(bv)->setFont(bv, font, toggleall);
2024 lt = getLyXText(bv);
2027 if (lt->selection.set()) {
2028 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
2032 lt->toggleFree(bv, font, toggleall);
2034 lt->clearSelection();
2036 bool flag = (selectall || lt->selection.set());
2040 updateLocal(bv, FULL, true);
2042 updateLocal(bv, CURSOR_PAR, true);
2046 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
2048 if (cpar(bv)->isInset(cpos(bv))) {
2050 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
2051 if (!isHighlyEditableInset(inset))
2053 inset->edit(bv, front);
2054 if (!the_locking_inset)
2056 updateLocal(bv, CURSOR, false);
2063 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
2066 x -= drawTextXOffset;
2068 int dummyy = y + insetAscent;
2069 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
2076 inset_x = cix(bv) - top_x + drawTextXOffset;
2077 inset_y = ciy(bv) + drawTextYOffset;
2078 inset->edit(bv, x - inset_x, y - inset_y, button);
2079 if (!the_locking_inset)
2081 updateLocal(bv, CURSOR, false);
2088 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
2091 int w = UpdatableInset::getMaxWidth(bv, inset);
2096 w = w - top_x + owner()->x();
2099 w -= (2 * TEXT_TO_INSET_OFFSET);
2102 return UpdatableInset::getMaxWidth(bv, inset);
2107 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2109 // we have to unlock any locked inset otherwise we're in troubles
2110 the_locking_inset = 0;
2112 Paragraph * tmp = par->next();
2117 par = new Paragraph(*p, same_id);
2118 par->setInsetOwner(this);
2119 Paragraph * np = par;
2122 np->next(new Paragraph(*p, same_id));
2123 np->next()->previous(np);
2125 np->setInsetOwner(this);
2132 void InsetText::setText(string const & data, LyXFont const & font)
2135 for (unsigned int i=0; i < data.length(); ++i)
2136 par->insertChar(i, data[i], font);
2141 void InsetText::setAutoBreakRows(bool flag)
2143 if (flag != autoBreakRows) {
2144 autoBreakRows = flag;
2152 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2154 if (how != drawFrame_) {
2157 updateLocal(bv, DRAW_FRAME, false);
2162 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2164 if (frame_color != col) {
2167 updateLocal(bv, DRAW_FRAME, false);
2172 int InsetText::cx(BufferView * bv) const
2174 // we do nothing dangerous so we use a local cache
2175 LyXText * llt = getLyXText(bv);
2176 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2177 if (the_locking_inset) {
2178 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2180 if (font.isVisibleRightToLeft())
2181 x -= the_locking_inset->width(bv, font);
2187 int InsetText::cix(BufferView * bv) const
2189 // we do nothing dangerous so we use a local cache
2190 LyXText * llt = getLyXText(bv);
2191 int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2192 if (the_locking_inset) {
2193 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2195 if (font.isVisibleRightToLeft())
2196 x -= the_locking_inset->width(bv, font);
2202 int InsetText::cy(BufferView * bv) const
2205 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2209 int InsetText::ciy(BufferView * bv) const
2212 return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2216 pos_type InsetText::cpos(BufferView * bv) const
2218 return getLyXText(bv)->cursor.pos();
2222 Paragraph * InsetText::cpar(BufferView * bv) const
2224 return getLyXText(bv)->cursor.par();
2228 bool InsetText::cboundary(BufferView * bv) const
2230 return getLyXText(bv)->cursor.boundary();
2234 Row * InsetText::crow(BufferView * bv) const
2236 return getLyXText(bv)->cursor.row();
2240 LyXText * InsetText::getLyXText(BufferView const * lbv,
2241 bool const recursive) const
2243 if (cached_bview == lbv) {
2244 if (recursive && the_locking_inset)
2245 return the_locking_inset->getLyXText(lbv, true);
2246 LyXText * lt = cached_text.get();
2247 lyx::Assert(lt && lt->firstRow()->par() == par);
2250 // Super UGLY! (Lgb)
2251 BufferView * bv = const_cast<BufferView *>(lbv);
2254 Cache::iterator it = cache.find(bv);
2256 if (it != cache.end()) {
2259 } else if (do_resize) {
2260 resizeLyXText(do_resize);
2262 if (lt || !it->second.remove) {
2263 lyx::Assert(it->second.text.get());
2264 cached_text = it->second.text;
2265 if (recursive && the_locking_inset) {
2266 return the_locking_inset->getLyXText(bv, true);
2268 return cached_text.get();
2269 } else if (it->second.remove) {
2271 saveLyXTextState(it->second.text.get());
2277 // when we have to reinit the existing LyXText!
2279 it->second.text->init(bv);
2280 restoreLyXTextState(bv, it->second.text.get());
2281 it->second.remove = false;
2283 cached_text = it->second.text;
2284 if (the_locking_inset && recursive) {
2285 return the_locking_inset->getLyXText(bv);
2287 return cached_text.get();
2290 // we are here only if we don't have a BufferView * in the cache!!!
2292 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2293 cached_text->init(bv);
2294 restoreLyXTextState(bv, cached_text.get());
2296 cache.insert(make_pair(bv, cached_text));
2298 if (the_locking_inset && recursive) {
2299 return the_locking_inset->getLyXText(bv);
2301 return cached_text.get();
2305 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2309 Cache::iterator it = cache.find(bv);
2311 if (it == cache.end()) {
2315 lyx::Assert(it->second.text.get());
2317 it->second.remove = true;
2319 /// then remove all LyXText in text-insets
2320 Paragraph * p = par;
2321 for (; p; p = p->next()) {
2322 p->deleteInsetsLyXText(bv);
2328 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2331 // we cannot resize this because we are in use!
2332 // so do this on the next possible getLyXText()
2337 // lyxerr << "InsetText::resizeLyXText\n";
2338 if (!par->next() && !par->size()) { // no data, resize not neccessary!
2339 // we have to do this as a fixed width may have changed!
2340 LyXText * t = getLyXText(bv);
2341 saveLyXTextState(t);
2343 restoreLyXTextState(bv, t);
2346 // one endless line, resize normally not necessary
2347 if (!force && getMaxWidth(bv, this) < 0)
2350 Cache::iterator it = cache.find(bv);
2351 if (it == cache.end()) {
2354 lyx::Assert(it->second.text.get());
2356 LyXText * t = it->second.text.get();
2357 saveLyXTextState(t);
2358 for (Paragraph * p = par; p; p = p->next()) {
2359 p->resizeInsetsLyXText(bv);
2362 restoreLyXTextState(bv, t);
2363 if (the_locking_inset) {
2364 inset_x = cix(bv) - top_x + drawTextXOffset;
2365 inset_y = ciy(bv) + drawTextYOffset;
2369 t->first_y = bv->screen()->topCursorVisible(t);
2372 updateLocal(bv, FULL, false);
2373 // this will scroll the screen such that the cursor becomes visible
2374 bv->updateScrollbar();
2376 need_update |= FULL;
2381 void InsetText::reinitLyXText() const
2384 // we cannot resize this because we are in use!
2385 // so do this on the next possible getLyXText()
2391 // lyxerr << "InsetText::reinitLyXText\n";
2392 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2393 lyx::Assert(it->second.text.get());
2395 LyXText * t = it->second.text.get();
2396 BufferView * bv = it->first;
2398 saveLyXTextState(t);
2399 for (Paragraph * p = par; p; p = p->next()) {
2400 p->resizeInsetsLyXText(bv);
2403 restoreLyXTextState(bv, t);
2404 if (the_locking_inset) {
2405 inset_x = cix(bv) - top_x + drawTextXOffset;
2406 inset_y = ciy(bv) + drawTextYOffset;
2409 t->first_y = bv->screen()->topCursorVisible(t);
2412 updateLocal(bv, FULL, false);
2413 // this will scroll the screen such that the cursor becomes visible
2414 bv->updateScrollbar();
2422 void InsetText::removeNewlines()
2424 bool changed = false;
2426 for (Paragraph * p = par; p; p = p->next()) {
2427 for (int i = 0; i < p->size(); ++i) {
2428 if (p->getChar(i) == Paragraph::META_NEWLINE) {
2439 bool InsetText::nodraw() const
2441 if (the_locking_inset)
2442 return the_locking_inset->nodraw();
2443 return UpdatableInset::nodraw();
2447 int InsetText::scroll(bool recursive) const
2449 int sx = UpdatableInset::scroll(false);
2451 if (recursive && the_locking_inset)
2452 sx += the_locking_inset->scroll(recursive);
2458 bool InsetText::doClearArea() const
2460 return !locked || (need_update & (FULL|INIT));
2464 void InsetText::selectAll(BufferView * bv)
2466 getLyXText(bv)->cursorTop(bv);
2467 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2468 getLyXText(bv)->cursorBottom(bv);
2469 getLyXText(bv)->setSelection(bv);
2473 void InsetText::clearSelection(BufferView * bv)
2475 getLyXText(bv)->clearSelection();
2479 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2481 Painter & pain = bv->painter();
2483 int h = insetAscent + insetDescent;
2484 int ty = baseline - insetAscent;
2490 if ((ty + h) > pain.paperHeight())
2491 h = pain.paperHeight();
2492 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2493 w = pain.paperWidth();
2494 // w -= TEXT_TO_INSET_OFFSET;
2495 pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2498 frame_is_visible = false;
2502 Paragraph * InsetText::getParFromID(int id) const
2505 Paragraph * result = par;
2506 Paragraph * ires = 0;
2507 while (result && result->id() != id) {
2508 if ((ires = result->getParFromID(id)))
2510 result = result->next();
2514 Paragraph * tmp = par;
2516 if (tmp->id() == id) {
2519 Paragraph * tmp2 = tmp->getParFromID(id);
2530 Paragraph * InsetText::firstParagraph() const
2533 if (the_locking_inset)
2534 if ((result = the_locking_inset->firstParagraph()))
2540 Paragraph * InsetText::getFirstParagraph(int i) const
2542 return (i == 0) ? par : 0;
2546 LyXCursor const & InsetText::cursor(BufferView * bv) const
2548 if (the_locking_inset)
2549 return the_locking_inset->cursor(bv);
2550 return getLyXText(bv)->cursor;
2554 Paragraph * InsetText::paragraph() const
2560 void InsetText::paragraph(Paragraph * p)
2562 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2563 // caller of this function has to take care of it. This IS important
2564 // as we could have to insert a paragraph before this one and just
2565 // link the actual to a new ones next and set it with this function
2568 // set ourself as owner for all the paragraphs inserted!
2569 Paragraph * np = par;
2571 np->setInsetOwner(this);
2575 // redraw myself when asked for
2580 Inset * InsetText::getInsetFromID(int id_arg) const
2583 return const_cast<InsetText *>(this);
2585 Paragraph * lp = par;
2588 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2589 en = lp->inset_iterator_end();
2592 if ((*it)->id() == id_arg)
2594 Inset * in = (*it)->getInsetFromID(id_arg);
2604 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2610 lt = getLyXText(bv);
2613 if (the_locking_inset) {
2614 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2621 // we have to go on checking so move cusor to the next char
2622 lt->cursor.pos(lt->cursor.pos() + 1);
2624 str = lt->selectNextWordToSpellcheck(bv, value);
2626 bv->unlockInset(const_cast<InsetText *>(this));
2635 void InsetText::selectSelectedWord(BufferView * bv)
2637 if (the_locking_inset) {
2638 the_locking_inset->selectSelectedWord(bv);
2641 getLyXText(bv)->selectSelectedWord(bv);
2642 updateLocal(bv, SELECTION, false);
2646 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2648 if (the_locking_inset) {
2649 the_locking_inset->toggleSelection(bv, kill_selection);
2653 lt = getLyXText(bv);
2657 int x = top_x + TEXT_TO_INSET_OFFSET;
2659 Row * row = lt->firstRow();
2660 int y_offset = top_baseline - row->ascent_of_text();
2662 while ((row != 0) && ((y+row->height()) <= 0)) {
2669 if (need_update & SELECTION)
2671 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2677 bool InsetText::searchForward(BufferView * bv, string const & str,
2680 if (the_locking_inset) {
2681 if (the_locking_inset->searchForward(bv, str, cs, mw))
2685 lt = getLyXText(bv);
2688 Paragraph * lpar = lt->cursor.par();
2689 pos_type pos = lt->cursor.pos();
2690 if (pos < lpar->size() - 1)
2694 lpar = lpar->next();
2699 // we have to unlock ourself in this function by default!
2700 bv->unlockInset(const_cast<InsetText *>(this));
2703 lt->setCursor(bv, lpar, pos);
2707 if (LyXFind(bv, str, true, true, cs , mw)) {
2710 // we have to unlock ourself in this function by default!
2711 bv->unlockInset(const_cast<InsetText *>(this));
2715 bool InsetText::searchBackward(BufferView * bv, string const & str,
2718 if (the_locking_inset)
2719 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2721 if (LyXFind(bv, str, false, true, cs, mw)) {
2724 // we have to unlock ourself in this function by default!
2725 bv->unlockInset(const_cast<InsetText *>(this));
2730 bool InsetText::checkInsertChar(LyXFont & font)
2733 return owner()->checkInsertChar(font);
2738 void InsetText::collapseParagraphs(BufferView * bv) const
2740 BufferParams const & bparams = bv->buffer()->params;
2741 LyXText * llt = getLyXText(bv);
2743 while(par->next()) {
2744 if (par->size() && par->next()->size() &&
2745 !par->isSeparator(par->size()-1))
2747 par->insertChar(par->size(), ' ');
2749 if (llt->selection.set()) {
2750 if (llt->selection.start.par() == par->next()) {
2751 llt->selection.start.par(par);
2752 llt->selection.start.pos(
2753 llt->selection.start.pos() + par->size());
2755 if (llt->selection.end.par() == par->next()) {
2756 llt->selection.end.par(par);
2757 llt->selection.end.pos(
2758 llt->selection.end.pos() + par->size());
2761 par->pasteParagraph(bparams);
2767 void InsetText::getDrawFont(LyXFont & font) const
2771 owner()->getDrawFont(font);
2775 void InsetText::appendParagraphs(BufferParams const & bparams,
2779 Paragraph * tmpbuf = newpar;
2780 Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2782 while (tmpbuf->next()) {
2783 tmpbuf = tmpbuf->next();
2784 lastbuffer->next(new Paragraph(*tmpbuf, false));
2785 lastbuffer->next()->previous(lastbuffer);
2786 lastbuffer = lastbuffer->next();
2789 while (lastbuffer->next())
2790 lastbuffer = lastbuffer->next();
2791 if (newpar->size() && lastbuffer->size() &&
2792 !lastbuffer->isSeparator(lastbuffer->size()-1))
2794 lastbuffer->insertChar(lastbuffer->size(), ' ');
2797 // make the buf exactly the same layout than our last paragraph
2798 buf->makeSameLayout(lastbuffer);
2801 lastbuffer->next(buf);
2802 buf->previous(lastbuffer);
2803 lastbuffer->pasteParagraph(bparams);