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"
24 #include "frontends/LyXView.h"
25 #include "BufferView.h"
26 #include "lyxtextclasslist.h"
27 #include "LaTeXFeatures.h"
28 #include "frontends/Painter.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),
467 row, y + lt->first_y, cleared);
468 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
469 lt->need_break_row = row;
471 lt->setCursor(bv, lt->cursor.par(),
473 if (lt->selection.set()) {
474 lt->setCursor(bv, lt->selection.start,
475 lt->selection.start.par(),
476 lt->selection.start.pos());
477 lt->setCursor(bv, lt->selection.end,
478 lt->selection.end.par(),
479 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 // we put here -1 and not button as now the button in the
699 // edit call should not be needed we will fix this in 1.3.x
700 // cycle hopefully (Jug 20020509)
701 if (!checkAndActivateInset(bv, x, tmp_y, -1)) {
702 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
704 lt->cursor.x_fix(lt->cursor.x());
706 lt->clearSelection();
708 // If the inset is empty set the language of the current font to the
709 // language to the surronding text (if different).
710 if (par->size() == 0 && !par->next() &&
711 bv->getParentLanguage(this) != lt->current_font.language())
713 LyXFont font(LyXFont::ALL_IGNORE);
714 font.setLanguage(bv->getParentLanguage(this));
715 setFont(bv, font, false);
721 if (drawFrame_ == LOCKED)
722 code = CURSOR|DRAW_FRAME;
723 updateLocal(bv, code, false);
726 // Tell the paragraph dialog that we've entered an insettext.
727 bv->owner()->getDialogs()->updateParagraph();
731 void InsetText::edit(BufferView * bv, bool front)
733 UpdatableInset::edit(bv, front);
735 if (!bv->lockInset(this)) {
736 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
740 the_locking_inset = 0;
741 inset_pos = inset_x = inset_y = 0;
742 inset_boundary = false;
751 lt->setCursor(bv, par, 0);
756 // int const pos = (p->size() ? p->size()-1 : p->size());
757 lt->setCursor(bv, p, p->size());
759 lt->clearSelection();
761 // If the inset is empty set the language of the current font to the
762 // language to the surronding text (if different).
763 if (par->size() == 0 && !par->next() &&
764 bv->getParentLanguage(this) != lt->current_font.language()) {
765 LyXFont font(LyXFont::ALL_IGNORE);
766 font.setLanguage(bv->getParentLanguage(this));
767 setFont(bv, font, false);
772 if (drawFrame_ == LOCKED)
773 code = CURSOR|DRAW_FRAME;
774 updateLocal(bv, code, false);
779 void InsetText::insetUnlock(BufferView * bv)
781 if (the_locking_inset) {
782 the_locking_inset->insetUnlock(bv);
783 the_locking_inset = 0;
784 updateLocal(bv, CURSOR_PAR, false);
791 if (drawFrame_ == LOCKED)
792 code = CURSOR|CLEAR_FRAME;
796 if (drawFrame_ == LOCKED)
804 if (lt->selection.set()) {
805 lt->clearSelection();
807 } else if (owner()) {
808 bv->owner()->setLayout(owner()->getLyXText(bv)
809 ->cursor.par()->layout());
811 bv->owner()->setLayout(bv->text->cursor.par()->layout());
812 // hack for deleteEmptyParMech
814 lt->setCursor(bv, par, 0);
815 } else if (par->next()) {
816 lt->setCursor(bv, par->next(), 0);
821 updateLocal(bv, code, false);
824 setUpdateStatus(bv, code);
829 void InsetText::lockInset(BufferView * bv)
832 the_locking_inset = 0;
833 inset_pos = inset_x = inset_y = 0;
834 inset_boundary = false;
842 lt->setCursor(bv, par, 0);
843 lt->clearSelection();
845 // If the inset is empty set the language of the current font to the
846 // language to the surronding text (if different).
847 if (par->size() == 0 && !par->next() &&
848 bv->getParentLanguage(this) != lt->current_font.language()) {
849 LyXFont font(LyXFont::ALL_IGNORE);
850 font.setLanguage(bv->getParentLanguage(this));
851 setFont(bv, font, false);
856 if (drawFrame_ == LOCKED)
857 code = CURSOR|DRAW_FRAME;
858 setUpdateStatus(bv, code);
862 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
864 the_locking_inset = inset;
865 inset_x = cix(bv) - top_x + drawTextXOffset;
866 inset_y = ciy(bv) + drawTextYOffset;
867 inset_pos = cpos(bv);
868 inset_par = cpar(bv);
869 inset_boundary = cboundary(bv);
870 updateLocal(bv, CURSOR, false);
874 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
876 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
880 if (!the_locking_inset) {
882 int const id = inset->id();
884 Paragraph::inset_iterator it =
885 p->inset_iterator_begin();
886 Paragraph::inset_iterator const end =
887 p->inset_iterator_end();
888 for (; it != end; ++it) {
889 if ((*it) == inset) {
890 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
891 lockInset(bv, inset);
894 if ((*it)->getInsetFromID(id)) {
895 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
897 return the_locking_inset->lockInsetInInset(bv, inset);
904 if (inset == cpar(bv)->getInset(cpos(bv))) {
905 lyxerr[Debug::INSETS] << "OK" << endl;
906 lockInset(bv, inset);
908 } else if (the_locking_inset && (the_locking_inset == inset)) {
909 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
910 lyxerr[Debug::INSETS] << "OK" << endl;
911 inset_x = cix(bv) - top_x + drawTextXOffset;
912 inset_y = ciy(bv) + drawTextYOffset;
914 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
916 } else if (the_locking_inset) {
917 lyxerr[Debug::INSETS] << "MAYBE" << endl;
918 return the_locking_inset->lockInsetInInset(bv, inset);
920 lyxerr[Debug::INSETS] << "NOT OK" << endl;
925 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
928 if (!the_locking_inset)
930 if (the_locking_inset == inset) {
931 the_locking_inset->insetUnlock(bv);
932 getLyXText(bv)->updateInset(bv, inset);
933 the_locking_inset = 0;
935 moveRight(bv, false);
936 old_par = 0; // force layout setting
940 updateLocal(bv, CURSOR, false);
943 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
947 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
949 if (!autoBreakRows && par->next())
950 collapseParagraphs(bv);
958 if (inset->owner() != this) {
959 int ustat = CURSOR_PAR;
961 UpdatableInset * tl_inset = the_locking_inset;
963 found = tl_inset->updateInsetInInset(bv, inset);
965 tl_inset = static_cast<UpdatableInset *>(inset);
966 while(tl_inset->owner() && tl_inset->owner() != this)
967 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
968 if (!tl_inset->owner())
970 found = tl_inset->updateInsetInInset(bv, inset);
974 lt->updateInset(bv, tl_inset);
978 setUpdateStatus(bv, ustat);
981 bool found = lt->updateInset(bv, inset);
985 setUpdateStatus(bv, CURSOR_PAR);
986 if (the_locking_inset &&
987 cpar(bv) == inset_par && cpos(bv) == inset_pos)
989 inset_x = cix(bv) - top_x + drawTextXOffset;
990 inset_y = ciy(bv) + drawTextYOffset;
997 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
1001 // use this to check mouse motion for selection!
1008 int tmp_x = x - drawTextXOffset;
1009 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
1010 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1012 hideInsetCursor(bv);
1013 if (the_locking_inset) {
1014 if (the_locking_inset == inset) {
1015 the_locking_inset->insetButtonPress(bv,
1023 // otherwise unlock the_locking_inset and lock the new inset
1024 the_locking_inset->insetUnlock(bv);
1025 inset_x = cix(bv) - top_x + drawTextXOffset;
1026 inset_y = ciy(bv) + drawTextYOffset;
1027 the_locking_inset = 0;
1028 inset->insetButtonPress(bv, x - inset_x,
1029 y - inset_y, button);
1030 // inset->edit(bv, x - inset_x, y - inset_y, button);
1031 if (the_locking_inset)
1032 updateLocal(bv, CURSOR, false);
1036 // otherwise only unlock the_locking_inset
1037 the_locking_inset->insetUnlock(bv);
1038 the_locking_inset = 0;
1041 no_selection = false;
1043 if (bv->theLockingInset()) {
1044 if (isHighlyEditableInset(inset)) {
1045 // We just have to lock the inset before calling a
1046 // PressEvent on it!
1047 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
1048 if (!bv->lockInset(uinset)) {
1049 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1051 inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
1052 if (the_locking_inset)
1053 updateLocal(bv, CURSOR, false);
1057 if (!inset) { // && (button == 2)) {
1058 bool paste_internally = false;
1059 if ((button == 2) && getLyXText(bv)->selection.set()) {
1060 localDispatch(bv, LFUN_COPY, "");
1061 paste_internally = true;
1065 lt = getLyXText(bv);
1068 int old_first_y = lt->first_y;
1070 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
1072 // set the selection cursor!
1073 lt->selection.cursor = lt->cursor;
1074 lt->cursor.x_fix(lt->cursor.x());
1076 if (lt->selection.set()) {
1077 lt->clearSelection();
1080 updateLocal(bv, FULL, false);
1082 lt->clearSelection();
1085 updateLocal(bv, CURSOR, false);
1087 bv->owner()->setLayout(cpar(bv)->layout());
1088 // we moved the view we cannot do mouse selection in this case!
1089 if (getLyXText(bv)->first_y != old_first_y)
1090 no_selection = true;
1092 // Insert primary selection with middle mouse
1093 // if there is a local selection in the current buffer,
1096 if (paste_internally)
1097 localDispatch(bv, LFUN_PASTE, "");
1099 localDispatch(bv, LFUN_PASTESELECTION,
1103 getLyXText(bv)->clearSelection();
1105 showInsetCursor(bv);
1109 bool InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
1111 no_selection = true;
1112 if (the_locking_inset) {
1113 return the_locking_inset->insetButtonRelease(bv,
1114 x - inset_x, y - inset_y,
1117 int tmp_x = x - drawTextXOffset;
1118 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
1119 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1122 if (isHighlyEditableInset(inset)) {
1123 ret = inset->insetButtonRelease(bv, x - inset_x,
1124 y - inset_y, button);
1126 inset_x = cix(bv) - top_x + drawTextXOffset;
1127 inset_y = ciy(bv) + drawTextYOffset;
1128 ret = inset->insetButtonRelease(bv, x - inset_x,
1129 y - inset_y, button);
1130 inset->edit(bv, x - inset_x,
1131 y - inset_y, button);
1133 updateLocal(bv, CURSOR_PAR, false);
1139 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
1141 if (the_locking_inset) {
1142 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1147 if (no_selection || ((mouse_x == x) && (mouse_y == y)))
1152 lt = getLyXText(bv);
1155 hideInsetCursor(bv);
1156 LyXCursor cur = lt->cursor;
1157 lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1158 lt->cursor.x_fix(lt->cursor.x());
1159 if (cur == lt->cursor) {
1164 lt->setSelection(bv);
1165 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1166 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1170 updateLocal(bv, SELECTION, false);
1172 showInsetCursor(bv);
1176 void InsetText::insetKeyPress(XKeyEvent * xke)
1178 if (the_locking_inset) {
1179 the_locking_inset->insetKeyPress(xke);
1185 UpdatableInset::RESULT
1186 InsetText::localDispatch(BufferView * bv,
1187 kb_action action, string const & arg)
1189 bool was_empty = par->size() == 0 && !par->next();
1190 no_selection = false;
1191 UpdatableInset::RESULT
1192 result= UpdatableInset::localDispatch(bv, action, arg);
1193 if (result != UNDISPATCHED) {
1197 result = DISPATCHED;
1198 if ((action < 0) && arg.empty())
1201 if (the_locking_inset) {
1202 result = the_locking_inset->localDispatch(bv, action, arg);
1203 if (result == DISPATCHED_NOUPDATE)
1205 else if (result == DISPATCHED) {
1206 updateLocal(bv, CURSOR_PAR, false);
1208 } else if (result >= FINISHED) {
1210 case FINISHED_RIGHT:
1211 moveRightIntern(bv, false, false);
1212 result = DISPATCHED;
1215 if ((result = moveUp(bv)) >= FINISHED) {
1216 updateLocal(bv, CURSOR, false);
1217 bv->unlockInset(this);
1221 if ((result = moveDown(bv)) >= FINISHED) {
1222 updateLocal(bv, CURSOR, false);
1223 bv->unlockInset(this);
1227 result = DISPATCHED;
1230 the_locking_inset = 0;
1231 #ifdef WITH_WARNINGS
1232 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
1234 updateLocal(bv, CURSOR, false);
1238 hideInsetCursor(bv);
1241 lt = getLyXText(bv);
1245 int updflag = false;
1248 case LFUN_SELFINSERT:
1249 if (bv->buffer()->isReadonly()) {
1250 // setErrorMessage(N_("Document is read only"));
1254 /* Automatically delete the currently selected
1255 * text and replace it with what is being
1256 * typed in now. Depends on lyxrc settings
1257 * "auto_region_delete", which defaults to
1260 setUndo(bv, Undo::INSERT,
1261 lt->cursor.par(), lt->cursor.par()->next());
1263 if (lyxrc.auto_region_delete) {
1264 if (lt->selection.set()) {
1265 lt->cutSelection(bv, false);
1268 lt->clearSelection();
1269 for (string::size_type i = 0; i < arg.length(); ++i) {
1270 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1273 lt->selection.cursor = lt->cursor;
1274 updwhat = CURSOR | CURSOR_PAR;
1276 result = DISPATCHED_NOUPDATE;
1278 // --- Cursor Movements -----------------------------------
1281 moveRight(bv, false, true);
1282 lt->setSelection(bv);
1283 updwhat = SELECTION;
1286 result = moveRight(bv);
1292 moveLeft(bv, false, true);
1293 lt->setSelection(bv);
1294 updwhat = SELECTION;
1298 result = moveLeft(bv);
1304 lt->setSelection(bv);
1305 updwhat = SELECTION;
1309 result = moveDown(bv);
1315 lt->setSelection(bv);
1316 updwhat = SELECTION;
1320 result = moveUp(bv);
1332 case LFUN_BACKSPACE: {
1333 setUndo(bv, Undo::DELETE,
1334 lt->cursor.par(), lt->cursor.par()->next());
1335 if (lt->selection.set())
1336 lt->cutSelection(bv);
1339 updwhat = CURSOR_PAR;
1345 setUndo(bv, Undo::DELETE,
1346 lt->cursor.par(), lt->cursor.par()->next());
1347 if (lt->selection.set()) {
1348 lt->cutSelection(bv);
1352 updwhat = CURSOR_PAR;
1358 setUndo(bv, Undo::DELETE,
1359 lt->cursor.par(), lt->cursor.par()->next());
1360 lt->cutSelection(bv);
1361 updwhat = CURSOR_PAR;
1368 lt->copySelection(bv);
1369 updwhat = CURSOR_PAR;
1371 case LFUN_PASTESELECTION:
1373 string const clip(bv->getClipboard());
1377 if (arg == "paragraph") {
1378 lt->insertStringAsParagraphs(bv, clip);
1380 lt->insertStringAsLines(bv, clip);
1383 lt->clearSelection();
1385 updwhat = CURSOR_PAR;
1390 if (!autoBreakRows) {
1392 if (CutAndPaste::nrOfParagraphs() > 1) {
1393 Alert::alert(_("Impossible operation"),
1394 _("Cannot include more than one paragraph!"),
1399 setUndo(bv, Undo::INSERT,
1400 lt->cursor.par(), lt->cursor.par()->next());
1401 lt->pasteSelection(bv);
1403 lt->clearSelection();
1404 updwhat = CURSOR_PAR;
1409 case LFUN_BREAKPARAGRAPH:
1410 if (!autoBreakRows) {
1411 result = DISPATCHED;
1414 lt->breakParagraph(bv, 0);
1415 updwhat = CURSOR | FULL;
1418 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1419 if (!autoBreakRows) {
1420 result = DISPATCHED;
1423 lt->breakParagraph(bv, 1);
1424 updwhat = CURSOR | FULL;
1428 case LFUN_BREAKLINE: {
1429 if (!autoBreakRows) {
1430 result = DISPATCHED;
1433 setUndo(bv, Undo::INSERT,
1434 lt->cursor.par(), lt->cursor.par()->next());
1435 lt->insertChar(bv, Paragraph::META_NEWLINE);
1436 updwhat = CURSOR | CURSOR_PAR;
1442 // do not set layouts on non breakable textinsets
1443 if (autoBreakRows) {
1444 string cur_layout = cpar(bv)->layout();
1446 // Derive layout number from given argument (string)
1447 // and current buffer's textclass (number). */
1448 textclass_type tclass = bv->buffer()->params.textclass;
1449 string layout = arg;
1450 bool hasLayout = textclasslist[tclass].hasLayout(layout);
1452 // If the entry is obsolete, use the new one instead.
1454 string const & obs =
1455 textclasslist[tclass][layout].
1461 // see if we found the layout number:
1463 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1464 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1468 if (cur_layout != layout) {
1469 cur_layout = layout;
1470 lt->setLayout(bv, layout);
1471 bv->owner()->setLayout(cpar(bv)->layout());
1472 updwhat = CURSOR_PAR;
1476 // reset the layout box
1477 bv->owner()->setLayout(cpar(bv)->layout());
1480 case LFUN_PARAGRAPH_SPACING:
1481 // This one is absolutely not working. When fiddling with this
1482 // it also seems to me that the paragraphs inside the insettext
1483 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1485 Paragraph * par = lt->cursor.par();
1486 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1487 float cur_value = 1.0;
1488 if (cur_spacing == Spacing::Other) {
1489 cur_value = par->params().spacing().getValue();
1492 istringstream istr(arg.c_str());
1495 Spacing::Space new_spacing = cur_spacing;
1496 float new_value = cur_value;
1498 lyxerr << "Missing argument to `paragraph-spacing'"
1500 } else if (tmp == "single") {
1501 new_spacing = Spacing::Single;
1502 } else if (tmp == "onehalf") {
1503 new_spacing = Spacing::Onehalf;
1504 } else if (tmp == "double") {
1505 new_spacing = Spacing::Double;
1506 } else if (tmp == "other") {
1507 new_spacing = Spacing::Other;
1510 lyxerr << "new_value = " << tmpval << endl;
1513 } else if (tmp == "default") {
1514 new_spacing = Spacing::Default;
1516 lyxerr << _("Unknown spacing argument: ")
1519 if (cur_spacing != new_spacing || cur_value != new_value) {
1520 par->params().spacing(Spacing(new_spacing, new_value));
1521 updwhat = CURSOR_PAR;
1528 if (!bv->Dispatch(action, arg))
1529 result = UNDISPATCHED;
1536 updateLocal(bv, updwhat, updflag);
1537 /// If the action has deleted all text in the inset, we need to change the
1538 // language to the language of the surronding text.
1539 if (!was_empty && par->size() == 0 && !par->next()) {
1540 LyXFont font(LyXFont::ALL_IGNORE);
1541 font.setLanguage(bv->getParentLanguage(this));
1542 setFont(bv, font, false);
1545 if (result >= FINISHED)
1546 bv->unlockInset(this);
1548 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1549 result = DISPATCHED;
1554 int InsetText::latex(Buffer const * buf, ostream & os, bool moving_arg, bool) const
1557 buf->latexParagraphs(os, par, 0, texrow, moving_arg);
1558 return texrow.rows();
1562 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1564 Paragraph * p = par;
1565 unsigned int lines = 0;
1568 string const tmp = buf->asciiParagraph(p, linelen, p->previous()==0);
1569 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1577 int InsetText::docbook(Buffer const * buf, ostream & os) const
1579 Paragraph * p = par;
1580 unsigned int lines = 0;
1582 vector<string> environment_stack(10);
1583 vector<string> environment_inner(10);
1585 int const command_depth = 0;
1588 Paragraph::depth_type depth = 0; // paragraph depth
1592 int desc_on = 0; // description mode
1594 LyXLayout const & style =
1595 textclasslist[buf->params.textclass][p->layout()];
1597 // environment tag closing
1598 for (; depth > p->params().depth(); --depth) {
1599 if (environment_inner[depth] != "!-- --") {
1600 item_name = "listitem";
1601 buf->sgmlCloseTag(os, command_depth + depth,
1603 if (environment_inner[depth] == "varlistentry")
1604 buf->sgmlCloseTag(os, depth+command_depth,
1605 environment_inner[depth]);
1607 buf->sgmlCloseTag(os, depth + command_depth,
1608 environment_stack[depth]);
1609 environment_stack[depth].erase();
1610 environment_inner[depth].erase();
1613 if (depth == p->params().depth()
1614 && environment_stack[depth] != style.latexname()
1615 && !environment_stack[depth].empty()) {
1616 if (environment_inner[depth] != "!-- --") {
1617 item_name= "listitem";
1618 buf->sgmlCloseTag(os, command_depth+depth,
1620 if (environment_inner[depth] == "varlistentry")
1621 buf->sgmlCloseTag(os,
1622 depth + command_depth,
1623 environment_inner[depth]);
1626 buf->sgmlCloseTag(os, depth + command_depth,
1627 environment_stack[depth]);
1629 environment_stack[depth].erase();
1630 environment_inner[depth].erase();
1633 // Write opening SGML tags.
1634 switch (style.latextype) {
1635 case LATEX_PARAGRAPH:
1636 buf->sgmlOpenTag(os, depth + command_depth,
1641 buf->sgmlError(p, 0,
1642 _("Error : LatexType Command not allowed here.\n"));
1646 case LATEX_ENVIRONMENT:
1647 case LATEX_ITEM_ENVIRONMENT:
1648 if (depth < p->params().depth()) {
1649 depth = p->params().depth();
1650 environment_stack[depth].erase();
1653 if (environment_stack[depth] != style.latexname()) {
1654 if (environment_stack.size() == depth + 1) {
1655 environment_stack.push_back("!-- --");
1656 environment_inner.push_back("!-- --");
1658 environment_stack[depth] = style.latexname();
1659 environment_inner[depth] = "!-- --";
1660 buf->sgmlOpenTag(os, depth + command_depth,
1661 environment_stack[depth]);
1663 if (environment_inner[depth] != "!-- --") {
1664 item_name= "listitem";
1665 buf->sgmlCloseTag(os,
1666 command_depth + depth,
1668 if (environment_inner[depth] == "varlistentry")
1669 buf->sgmlCloseTag(os,
1670 depth + command_depth,
1671 environment_inner[depth]);
1675 if (style.latextype == LATEX_ENVIRONMENT) {
1676 if (!style.latexparam().empty()) {
1677 if (style.latexparam() == "CDATA")
1680 buf->sgmlOpenTag(os, depth + command_depth,
1681 style.latexparam());
1686 desc_on = (style.labeltype == LABEL_MANUAL);
1689 environment_inner[depth]= "varlistentry";
1691 environment_inner[depth]= "listitem";
1693 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1694 environment_inner[depth]);
1698 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1702 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1707 buf->sgmlOpenTag(os, depth + command_depth,
1712 buf->simpleDocBookOnePar(os, p, desc_on,
1713 depth + 1 + command_depth);
1717 // write closing SGML tags
1718 switch (style.latextype) {
1719 case LATEX_ENVIRONMENT:
1720 if (!style.latexparam().empty()) {
1721 if (style.latexparam() == "CDATA")
1724 buf->sgmlCloseTag(os, depth + command_depth,
1725 style.latexparam());
1728 case LATEX_ITEM_ENVIRONMENT:
1729 if (desc_on == 1) break;
1731 buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1733 case LATEX_PARAGRAPH:
1734 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1737 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1743 for (int d = depth; d >= 0; --d) {
1744 if (!environment_stack[depth].empty()) {
1745 if (environment_inner[depth] != "!-- --") {
1746 item_name = "listitem";
1747 buf->sgmlCloseTag(os, command_depth + depth,
1749 if (environment_inner[depth] == "varlistentry")
1750 buf->sgmlCloseTag(os, depth + command_depth,
1751 environment_inner[depth]);
1754 buf->sgmlCloseTag(os, depth + command_depth,
1755 environment_stack[depth]);
1763 void InsetText::validate(LaTeXFeatures & features) const
1765 Paragraph * p = par;
1767 p->validate(features);
1773 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1775 if (textclasslist[buf->params.textclass][p->layout()].labeltype != LABEL_MANUAL)
1778 return p->beginningOfMainBody();
1782 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1784 if (the_locking_inset) {
1785 the_locking_inset->getCursorPos(bv, x, y);
1788 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1789 y = cy(bv) - TEXT_TO_INSET_OFFSET;
1793 int InsetText::insetInInsetY() const
1795 if (!the_locking_inset)
1798 return (inset_y + the_locking_inset->insetInInsetY());
1802 void InsetText::toggleInsetCursor(BufferView * bv)
1804 if (the_locking_inset) {
1805 the_locking_inset->toggleInsetCursor(bv);
1809 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1811 int const asc = lyxfont::maxAscent(font);
1812 int const desc = lyxfont::maxDescent(font);
1814 if (isCursorVisible())
1815 bv->hideLockedInsetCursor();
1817 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1818 toggleCursorVisible();
1822 void InsetText::showInsetCursor(BufferView * bv, bool show)
1824 if (the_locking_inset) {
1825 the_locking_inset->showInsetCursor(bv, show);
1828 if (!isCursorVisible()) {
1829 LyXFont const font =
1830 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1832 int const asc = lyxfont::maxAscent(font);
1833 int const desc = lyxfont::maxDescent(font);
1835 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1837 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1838 setCursorVisible(true);
1843 void InsetText::hideInsetCursor(BufferView * bv)
1845 if (isCursorVisible()) {
1846 bv->hideLockedInsetCursor();
1847 setCursorVisible(false);
1849 if (the_locking_inset)
1850 the_locking_inset->hideInsetCursor(bv);
1854 void InsetText::fitInsetCursor(BufferView * bv) const
1856 if (the_locking_inset) {
1857 the_locking_inset->fitInsetCursor(bv);
1860 LyXFont const font =
1861 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1863 int const asc = lyxfont::maxAscent(font);
1864 int const desc = lyxfont::maxDescent(font);
1866 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1867 need_update |= FULL;
1871 UpdatableInset::RESULT
1872 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1874 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1875 return moveLeftIntern(bv, true, activate_inset, selecting);
1877 return moveRightIntern(bv, true, activate_inset, selecting);
1881 UpdatableInset::RESULT
1882 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1884 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1885 return moveRightIntern(bv, false, activate_inset, selecting);
1887 return moveLeftIntern(bv, false, activate_inset, selecting);
1891 UpdatableInset::RESULT
1892 InsetText::moveRightIntern(BufferView * bv, bool front,
1893 bool activate_inset, bool selecting)
1895 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1896 return FINISHED_RIGHT;
1897 if (activate_inset && checkAndActivateInset(bv, front))
1899 getLyXText(bv)->cursorRight(bv);
1901 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1902 return DISPATCHED_NOUPDATE;
1906 UpdatableInset::RESULT
1907 InsetText::moveLeftIntern(BufferView * bv, bool front,
1908 bool activate_inset, bool selecting)
1910 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1912 getLyXText(bv)->cursorLeft(bv);
1914 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1915 if (activate_inset && checkAndActivateInset(bv, front))
1917 return DISPATCHED_NOUPDATE;
1921 UpdatableInset::RESULT
1922 InsetText::moveUp(BufferView * bv)
1924 if (!crow(bv)->previous())
1926 getLyXText(bv)->cursorUp(bv);
1927 return DISPATCHED_NOUPDATE;
1931 UpdatableInset::RESULT
1932 InsetText::moveDown(BufferView * bv)
1934 if (!crow(bv)->next())
1935 return FINISHED_DOWN;
1936 getLyXText(bv)->cursorDown(bv);
1937 return DISPATCHED_NOUPDATE;
1941 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1943 if (the_locking_inset) {
1944 if (the_locking_inset->insetAllowed(inset))
1945 return the_locking_inset->insertInset(bv, inset);
1948 inset->setOwner(this);
1949 hideInsetCursor(bv);
1950 getLyXText(bv)->insertInset(bv, inset);
1952 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1957 bool InsetText::insetAllowed(Inset::Code code) const
1959 // in_insetAllowed is a really gross hack,
1960 // to allow us to call the owner's insetAllowed
1961 // without stack overflow, which can happen
1962 // when the owner uses InsetCollapsable::insetAllowed()
1964 if (in_insetAllowed)
1966 in_insetAllowed = true;
1967 if (the_locking_inset)
1968 ret = the_locking_inset->insetAllowed(code);
1970 ret = owner()->insetAllowed(code);
1971 in_insetAllowed = false;
1976 UpdatableInset * InsetText::getLockingInset() const
1978 return the_locking_inset ? the_locking_inset->getLockingInset() :
1979 const_cast<InsetText *>(this);
1983 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1987 if (the_locking_inset)
1988 return the_locking_inset->getFirstLockingInsetOfType(c);
1993 bool InsetText::showInsetDialog(BufferView * bv) const
1995 if (the_locking_inset)
1996 return the_locking_inset->showInsetDialog(bv);
2001 vector<string> const InsetText::getLabelList() const
2003 vector<string> label_list;
2005 Paragraph * tpar = par;
2007 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
2008 Paragraph::inset_iterator end = tpar->inset_iterator_end();
2009 for (; beg != end; ++beg) {
2010 vector<string> const l = (*beg)->getLabelList();
2011 label_list.insert(label_list.end(), l.begin(), l.end());
2013 tpar = tpar->next();
2019 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
2022 if (the_locking_inset) {
2023 the_locking_inset->setFont(bv, font, toggleall, selectall);
2026 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
2027 getLyXText(bv)->setFont(bv, font, toggleall);
2032 lt = getLyXText(bv);
2035 if (lt->selection.set()) {
2036 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
2040 lt->toggleFree(bv, font, toggleall);
2042 lt->clearSelection();
2044 bool flag = (selectall || lt->selection.set());
2048 updateLocal(bv, FULL, true);
2050 updateLocal(bv, CURSOR_PAR, true);
2054 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
2056 if (cpar(bv)->isInset(cpos(bv))) {
2058 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
2059 if (!isHighlyEditableInset(inset))
2061 inset->edit(bv, front);
2062 if (!the_locking_inset)
2064 updateLocal(bv, CURSOR, false);
2071 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
2074 x -= drawTextXOffset;
2076 int dummyy = y + insetAscent;
2077 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
2078 // we only do the edit() call if the inset was hit by the mouse
2079 // or if it is a highly editable inset. So we should call this
2080 // function from our own edit with button < 0.
2081 if (button < 0 && !isHighlyEditableInset(inset))
2089 inset_x = cix(bv) - top_x + drawTextXOffset;
2090 inset_y = ciy(bv) + drawTextYOffset;
2091 inset->edit(bv, x - inset_x, y - inset_y, button);
2092 if (!the_locking_inset)
2094 updateLocal(bv, CURSOR, false);
2101 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
2104 int w = UpdatableInset::getMaxWidth(bv, inset);
2109 w = w - top_x + owner()->x();
2112 w -= (2 * TEXT_TO_INSET_OFFSET);
2115 return UpdatableInset::getMaxWidth(bv, inset);
2120 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2122 // we have to unlock any locked inset otherwise we're in troubles
2123 the_locking_inset = 0;
2125 Paragraph * tmp = par->next();
2130 par = new Paragraph(*p, same_id);
2131 par->setInsetOwner(this);
2132 Paragraph * np = par;
2135 np->next(new Paragraph(*p, same_id));
2136 np->next()->previous(np);
2138 np->setInsetOwner(this);
2145 void InsetText::setText(string const & data, LyXFont const & font)
2148 for (unsigned int i=0; i < data.length(); ++i)
2149 par->insertChar(i, data[i], font);
2154 void InsetText::setAutoBreakRows(bool flag)
2156 if (flag != autoBreakRows) {
2157 autoBreakRows = flag;
2165 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2167 if (how != drawFrame_) {
2170 updateLocal(bv, DRAW_FRAME, false);
2175 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2177 if (frame_color != col) {
2180 updateLocal(bv, DRAW_FRAME, false);
2185 int InsetText::cx(BufferView * bv) const
2187 // we do nothing dangerous so we use a local cache
2188 LyXText * llt = getLyXText(bv);
2189 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2190 if (the_locking_inset) {
2191 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2193 if (font.isVisibleRightToLeft())
2194 x -= the_locking_inset->width(bv, font);
2200 int InsetText::cix(BufferView * bv) const
2202 // we do nothing dangerous so we use a local cache
2203 LyXText * llt = getLyXText(bv);
2204 int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2205 if (the_locking_inset) {
2206 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2208 if (font.isVisibleRightToLeft())
2209 x -= the_locking_inset->width(bv, font);
2215 int InsetText::cy(BufferView * bv) const
2218 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2222 int InsetText::ciy(BufferView * bv) const
2225 return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2229 pos_type InsetText::cpos(BufferView * bv) const
2231 return getLyXText(bv)->cursor.pos();
2235 Paragraph * InsetText::cpar(BufferView * bv) const
2237 return getLyXText(bv)->cursor.par();
2241 bool InsetText::cboundary(BufferView * bv) const
2243 return getLyXText(bv)->cursor.boundary();
2247 Row * InsetText::crow(BufferView * bv) const
2249 return getLyXText(bv)->cursor.row();
2253 LyXText * InsetText::getLyXText(BufferView const * lbv,
2254 bool const recursive) const
2256 if (cached_bview == lbv) {
2257 if (recursive && the_locking_inset)
2258 return the_locking_inset->getLyXText(lbv, true);
2259 LyXText * lt = cached_text.get();
2260 lyx::Assert(lt && lt->firstRow()->par() == par);
2263 // Super UGLY! (Lgb)
2264 BufferView * bv = const_cast<BufferView *>(lbv);
2267 Cache::iterator it = cache.find(bv);
2269 if (it != cache.end()) {
2272 } else if (do_resize) {
2273 resizeLyXText(do_resize);
2275 if (lt || !it->second.remove) {
2276 lyx::Assert(it->second.text.get());
2277 cached_text = it->second.text;
2278 if (recursive && the_locking_inset) {
2279 return the_locking_inset->getLyXText(bv, true);
2281 return cached_text.get();
2282 } else if (it->second.remove) {
2284 saveLyXTextState(it->second.text.get());
2290 // when we have to reinit the existing LyXText!
2292 it->second.text->init(bv);
2293 restoreLyXTextState(bv, it->second.text.get());
2294 it->second.remove = false;
2296 cached_text = it->second.text;
2297 if (the_locking_inset && recursive) {
2298 return the_locking_inset->getLyXText(bv);
2300 return cached_text.get();
2303 // we are here only if we don't have a BufferView * in the cache!!!
2305 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2306 cached_text->init(bv);
2307 restoreLyXTextState(bv, cached_text.get());
2309 cache.insert(make_pair(bv, cached_text));
2311 if (the_locking_inset && recursive) {
2312 return the_locking_inset->getLyXText(bv);
2314 return cached_text.get();
2318 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2322 Cache::iterator it = cache.find(bv);
2324 if (it == cache.end()) {
2328 lyx::Assert(it->second.text.get());
2330 it->second.remove = true;
2332 /// then remove all LyXText in text-insets
2333 Paragraph * p = par;
2334 for (; p; p = p->next()) {
2335 p->deleteInsetsLyXText(bv);
2341 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2344 // we cannot resize this because we are in use!
2345 // so do this on the next possible getLyXText()
2350 // lyxerr << "InsetText::resizeLyXText\n";
2351 if (!par->next() && !par->size()) { // no data, resize not neccessary!
2352 // we have to do this as a fixed width may have changed!
2353 LyXText * t = getLyXText(bv);
2354 saveLyXTextState(t);
2356 restoreLyXTextState(bv, t);
2359 // one endless line, resize normally not necessary
2360 if (!force && getMaxWidth(bv, this) < 0)
2363 Cache::iterator it = cache.find(bv);
2364 if (it == cache.end()) {
2367 lyx::Assert(it->second.text.get());
2369 LyXText * t = it->second.text.get();
2370 saveLyXTextState(t);
2371 for (Paragraph * p = par; p; p = p->next()) {
2372 p->resizeInsetsLyXText(bv);
2375 restoreLyXTextState(bv, t);
2376 if (the_locking_inset) {
2377 inset_x = cix(bv) - top_x + drawTextXOffset;
2378 inset_y = ciy(bv) + drawTextYOffset;
2382 t->first_y = bv->screen()->topCursorVisible(t);
2385 updateLocal(bv, FULL, false);
2386 // this will scroll the screen such that the cursor becomes visible
2387 bv->updateScrollbar();
2389 need_update |= FULL;
2394 void InsetText::reinitLyXText() const
2397 // we cannot resize this because we are in use!
2398 // so do this on the next possible getLyXText()
2404 // lyxerr << "InsetText::reinitLyXText\n";
2405 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2406 lyx::Assert(it->second.text.get());
2408 LyXText * t = it->second.text.get();
2409 BufferView * bv = it->first;
2411 saveLyXTextState(t);
2412 for (Paragraph * p = par; p; p = p->next()) {
2413 p->resizeInsetsLyXText(bv);
2416 restoreLyXTextState(bv, t);
2417 if (the_locking_inset) {
2418 inset_x = cix(bv) - top_x + drawTextXOffset;
2419 inset_y = ciy(bv) + drawTextYOffset;
2422 t->first_y = bv->screen()->topCursorVisible(t);
2425 updateLocal(bv, FULL, false);
2426 // this will scroll the screen such that the cursor becomes visible
2427 bv->updateScrollbar();
2435 void InsetText::removeNewlines()
2437 bool changed = false;
2439 for (Paragraph * p = par; p; p = p->next()) {
2440 for (int i = 0; i < p->size(); ++i) {
2441 if (p->getChar(i) == Paragraph::META_NEWLINE) {
2452 bool InsetText::nodraw() const
2454 if (the_locking_inset)
2455 return the_locking_inset->nodraw();
2456 return UpdatableInset::nodraw();
2460 int InsetText::scroll(bool recursive) const
2462 int sx = UpdatableInset::scroll(false);
2464 if (recursive && the_locking_inset)
2465 sx += the_locking_inset->scroll(recursive);
2471 bool InsetText::doClearArea() const
2473 return !locked || (need_update & (FULL|INIT));
2477 void InsetText::selectAll(BufferView * bv)
2479 getLyXText(bv)->cursorTop(bv);
2480 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2481 getLyXText(bv)->cursorBottom(bv);
2482 getLyXText(bv)->setSelection(bv);
2486 void InsetText::clearSelection(BufferView * bv)
2488 getLyXText(bv)->clearSelection();
2492 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2494 Painter & pain = bv->painter();
2496 int h = insetAscent + insetDescent;
2497 int ty = baseline - insetAscent;
2503 if ((ty + h) > pain.paperHeight())
2504 h = pain.paperHeight();
2505 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2506 w = pain.paperWidth();
2507 // w -= TEXT_TO_INSET_OFFSET;
2508 pain.fillRectangle(top_x + 1, ty + 1, w - 1, h - 1, backgroundColor());
2511 frame_is_visible = false;
2515 Paragraph * InsetText::getParFromID(int id) const
2518 Paragraph * result = par;
2519 Paragraph * ires = 0;
2520 while (result && result->id() != id) {
2521 if ((ires = result->getParFromID(id)))
2523 result = result->next();
2527 Paragraph * tmp = par;
2529 if (tmp->id() == id) {
2532 Paragraph * tmp2 = tmp->getParFromID(id);
2543 Paragraph * InsetText::firstParagraph() const
2546 if (the_locking_inset)
2547 if ((result = the_locking_inset->firstParagraph()))
2553 Paragraph * InsetText::getFirstParagraph(int i) const
2555 return (i == 0) ? par : 0;
2559 LyXCursor const & InsetText::cursor(BufferView * bv) const
2561 if (the_locking_inset)
2562 return the_locking_inset->cursor(bv);
2563 return getLyXText(bv)->cursor;
2567 Paragraph * InsetText::paragraph() const
2573 void InsetText::paragraph(Paragraph * p)
2575 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2576 // caller of this function has to take care of it. This IS important
2577 // as we could have to insert a paragraph before this one and just
2578 // link the actual to a new ones next and set it with this function
2581 // set ourself as owner for all the paragraphs inserted!
2582 Paragraph * np = par;
2584 np->setInsetOwner(this);
2588 // redraw myself when asked for
2593 Inset * InsetText::getInsetFromID(int id_arg) const
2596 return const_cast<InsetText *>(this);
2598 Paragraph * lp = par;
2601 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2602 en = lp->inset_iterator_end();
2605 if ((*it)->id() == id_arg)
2607 Inset * in = (*it)->getInsetFromID(id_arg);
2617 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2623 lt = getLyXText(bv);
2626 if (the_locking_inset) {
2627 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2634 // we have to go on checking so move cusor to the next char
2635 lt->cursor.pos(lt->cursor.pos() + 1);
2637 str = lt->selectNextWordToSpellcheck(bv, value);
2639 bv->unlockInset(const_cast<InsetText *>(this));
2648 void InsetText::selectSelectedWord(BufferView * bv)
2650 if (the_locking_inset) {
2651 the_locking_inset->selectSelectedWord(bv);
2654 getLyXText(bv)->selectSelectedWord(bv);
2655 updateLocal(bv, SELECTION, false);
2659 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2661 if (the_locking_inset) {
2662 the_locking_inset->toggleSelection(bv, kill_selection);
2666 lt = getLyXText(bv);
2670 int x = top_x + TEXT_TO_INSET_OFFSET;
2672 Row * row = lt->firstRow();
2673 int y_offset = top_baseline - row->ascent_of_text();
2675 while ((row != 0) && ((y+row->height()) <= 0)) {
2682 if (need_update & SELECTION)
2684 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2690 bool InsetText::searchForward(BufferView * bv, string const & str,
2693 if (the_locking_inset) {
2694 if (the_locking_inset->searchForward(bv, str, cs, mw))
2698 lt = getLyXText(bv);
2701 Paragraph * lpar = lt->cursor.par();
2702 pos_type pos = lt->cursor.pos();
2703 if (pos < lpar->size() - 1)
2707 lpar = lpar->next();
2712 // we have to unlock ourself in this function by default!
2713 bv->unlockInset(const_cast<InsetText *>(this));
2716 lt->setCursor(bv, lpar, pos);
2720 if (LyXFind(bv, str, true, true, cs , mw)) {
2723 // we have to unlock ourself in this function by default!
2724 bv->unlockInset(const_cast<InsetText *>(this));
2728 bool InsetText::searchBackward(BufferView * bv, string const & str,
2731 if (the_locking_inset)
2732 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2734 if (LyXFind(bv, str, false, true, cs, mw)) {
2737 // we have to unlock ourself in this function by default!
2738 bv->unlockInset(const_cast<InsetText *>(this));
2743 bool InsetText::checkInsertChar(LyXFont & font)
2746 return owner()->checkInsertChar(font);
2751 void InsetText::collapseParagraphs(BufferView * bv) const
2753 BufferParams const & bparams = bv->buffer()->params;
2754 LyXText * llt = getLyXText(bv);
2756 while(par->next()) {
2757 if (par->size() && par->next()->size() &&
2758 !par->isSeparator(par->size()-1))
2760 par->insertChar(par->size(), ' ');
2762 if (llt->selection.set()) {
2763 if (llt->selection.start.par() == par->next()) {
2764 llt->selection.start.par(par);
2765 llt->selection.start.pos(
2766 llt->selection.start.pos() + par->size());
2768 if (llt->selection.end.par() == par->next()) {
2769 llt->selection.end.par(par);
2770 llt->selection.end.pos(
2771 llt->selection.end.pos() + par->size());
2774 par->pasteParagraph(bparams);
2780 void InsetText::getDrawFont(LyXFont & font) const
2784 owner()->getDrawFont(font);
2788 void InsetText::appendParagraphs(BufferParams const & bparams,
2792 Paragraph * tmpbuf = newpar;
2793 Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2795 while (tmpbuf->next()) {
2796 tmpbuf = tmpbuf->next();
2797 lastbuffer->next(new Paragraph(*tmpbuf, false));
2798 lastbuffer->next()->previous(lastbuffer);
2799 lastbuffer = lastbuffer->next();
2802 while (lastbuffer->next())
2803 lastbuffer = lastbuffer->next();
2804 if (newpar->size() && lastbuffer->size() &&
2805 !lastbuffer->isSeparator(lastbuffer->size()-1))
2807 lastbuffer->insertChar(lastbuffer->size(), ' ');
2810 // make the buf exactly the same layout than our last paragraph
2811 buf->makeSameLayout(lastbuffer);
2814 lastbuffer->next(buf);
2815 buf->previous(lastbuffer);
2816 lastbuffer->pasteParagraph(bparams);