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);
820 setUpdateStatus(bv, code);
825 void InsetText::lockInset(BufferView * bv)
828 the_locking_inset = 0;
829 inset_pos = inset_x = inset_y = 0;
830 inset_boundary = false;
838 lt->setCursor(bv, par, 0);
839 lt->clearSelection();
841 // If the inset is empty set the language of the current font to the
842 // language to the surronding text (if different).
843 if (par->size() == 0 && !par->next() &&
844 bv->getParentLanguage(this) != lt->current_font.language()) {
845 LyXFont font(LyXFont::ALL_IGNORE);
846 font.setLanguage(bv->getParentLanguage(this));
847 setFont(bv, font, false);
852 if (drawFrame_ == LOCKED)
853 code = CURSOR|DRAW_FRAME;
854 setUpdateStatus(bv, code);
858 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
860 the_locking_inset = inset;
861 inset_x = cix(bv) - top_x + drawTextXOffset;
862 inset_y = ciy(bv) + drawTextYOffset;
863 inset_pos = cpos(bv);
864 inset_par = cpar(bv);
865 inset_boundary = cboundary(bv);
866 updateLocal(bv, CURSOR, false);
870 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
872 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
876 if (!the_locking_inset) {
878 int const id = inset->id();
880 Paragraph::inset_iterator it =
881 p->inset_iterator_begin();
882 Paragraph::inset_iterator const end =
883 p->inset_iterator_end();
884 for (; it != end; ++it) {
885 if ((*it) == inset) {
886 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
887 lockInset(bv, inset);
890 if ((*it)->getInsetFromID(id)) {
891 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
893 return the_locking_inset->lockInsetInInset(bv, inset);
900 if (inset == cpar(bv)->getInset(cpos(bv))) {
901 lyxerr[Debug::INSETS] << "OK" << endl;
902 lockInset(bv, inset);
904 } else if (the_locking_inset && (the_locking_inset == inset)) {
905 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
906 lyxerr[Debug::INSETS] << "OK" << endl;
907 inset_x = cix(bv) - top_x + drawTextXOffset;
908 inset_y = ciy(bv) + drawTextYOffset;
910 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
912 } else if (the_locking_inset) {
913 lyxerr[Debug::INSETS] << "MAYBE" << endl;
914 return the_locking_inset->lockInsetInInset(bv, inset);
916 lyxerr[Debug::INSETS] << "NOT OK" << endl;
921 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
924 if (!the_locking_inset)
926 if (the_locking_inset == inset) {
927 the_locking_inset->insetUnlock(bv);
928 getLyXText(bv)->updateInset(bv, inset);
929 the_locking_inset = 0;
931 moveRight(bv, false);
932 old_par = 0; // force layout setting
936 updateLocal(bv, CURSOR, false);
939 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
943 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
945 if (!autoBreakRows && par->next())
946 collapseParagraphs(bv);
954 if (inset->owner() != this) {
955 int ustat = CURSOR_PAR;
957 UpdatableInset * tl_inset = the_locking_inset;
959 found = tl_inset->updateInsetInInset(bv, inset);
961 tl_inset = static_cast<UpdatableInset *>(inset);
962 while(tl_inset->owner() && tl_inset->owner() != this)
963 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
964 if (!tl_inset->owner())
966 found = tl_inset->updateInsetInInset(bv, inset);
970 lt->updateInset(bv, tl_inset);
974 setUpdateStatus(bv, ustat);
977 bool found = lt->updateInset(bv, inset);
981 setUpdateStatus(bv, CURSOR_PAR);
982 if (the_locking_inset &&
983 cpar(bv) == inset_par && cpos(bv) == inset_pos)
985 inset_x = cix(bv) - top_x + drawTextXOffset;
986 inset_y = ciy(bv) + drawTextYOffset;
993 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
997 // use this to check mouse motion for selection!
1004 int tmp_x = x - drawTextXOffset;
1005 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
1006 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1008 hideInsetCursor(bv);
1009 if (the_locking_inset) {
1010 if (the_locking_inset == inset) {
1011 the_locking_inset->insetButtonPress(bv,
1019 // otherwise unlock the_locking_inset and lock the new inset
1020 the_locking_inset->insetUnlock(bv);
1021 inset_x = cix(bv) - top_x + drawTextXOffset;
1022 inset_y = ciy(bv) + drawTextYOffset;
1023 the_locking_inset = 0;
1024 inset->insetButtonPress(bv, x - inset_x,
1025 y - inset_y, button);
1026 // inset->edit(bv, x - inset_x, y - inset_y, button);
1027 if (the_locking_inset)
1028 updateLocal(bv, CURSOR, false);
1032 // otherwise only unlock the_locking_inset
1033 the_locking_inset->insetUnlock(bv);
1034 the_locking_inset = 0;
1037 no_selection = false;
1039 if (bv->theLockingInset()) {
1040 if (isHighlyEditableInset(inset)) {
1041 // We just have to lock the inset before calling a
1042 // PressEvent on it!
1043 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
1044 if (!bv->lockInset(uinset)) {
1045 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1047 inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
1048 if (the_locking_inset)
1049 updateLocal(bv, CURSOR, false);
1053 if (!inset) { // && (button == 2)) {
1054 bool paste_internally = false;
1055 if ((button == 2) && getLyXText(bv)->selection.set()) {
1056 localDispatch(bv, LFUN_COPY, "");
1057 paste_internally = true;
1061 lt = getLyXText(bv);
1064 int old_first_y = lt->first_y;
1066 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
1068 // set the selection cursor!
1069 lt->selection.cursor = lt->cursor;
1070 lt->cursor.x_fix(lt->cursor.x());
1072 if (lt->selection.set()) {
1073 lt->clearSelection();
1076 updateLocal(bv, FULL, false);
1078 lt->clearSelection();
1081 updateLocal(bv, CURSOR, false);
1083 bv->owner()->setLayout(cpar(bv)->layout());
1084 // we moved the view we cannot do mouse selection in this case!
1085 if (getLyXText(bv)->first_y != old_first_y)
1086 no_selection = true;
1088 // Insert primary selection with middle mouse
1089 // if there is a local selection in the current buffer,
1092 if (paste_internally)
1093 localDispatch(bv, LFUN_PASTE, "");
1095 localDispatch(bv, LFUN_PASTESELECTION,
1099 getLyXText(bv)->clearSelection();
1101 showInsetCursor(bv);
1105 bool InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
1107 no_selection = true;
1108 if (the_locking_inset) {
1109 return the_locking_inset->insetButtonRelease(bv,
1110 x - inset_x, y - inset_y,
1113 int tmp_x = x - drawTextXOffset;
1114 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
1115 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1118 if (isHighlyEditableInset(inset)) {
1119 ret = inset->insetButtonRelease(bv, x - inset_x,
1120 y - inset_y, button);
1122 inset_x = cix(bv) - top_x + drawTextXOffset;
1123 inset_y = ciy(bv) + drawTextYOffset;
1124 ret = inset->insetButtonRelease(bv, x - inset_x,
1125 y - inset_y, button);
1126 inset->edit(bv, x - inset_x,
1127 y - inset_y, button);
1129 updateLocal(bv, CURSOR_PAR, false);
1135 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
1137 if (the_locking_inset) {
1138 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1143 if (no_selection || ((mouse_x == x) && (mouse_y == y)))
1148 lt = getLyXText(bv);
1151 hideInsetCursor(bv);
1152 LyXCursor cur = lt->cursor;
1153 lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1154 lt->cursor.x_fix(lt->cursor.x());
1155 if (cur == lt->cursor) {
1160 lt->setSelection(bv);
1161 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1162 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1166 updateLocal(bv, SELECTION, false);
1168 showInsetCursor(bv);
1172 void InsetText::insetKeyPress(XKeyEvent * xke)
1174 if (the_locking_inset) {
1175 the_locking_inset->insetKeyPress(xke);
1181 UpdatableInset::RESULT
1182 InsetText::localDispatch(BufferView * bv,
1183 kb_action action, string const & arg)
1185 bool was_empty = par->size() == 0 && !par->next();
1186 no_selection = false;
1187 UpdatableInset::RESULT
1188 result= UpdatableInset::localDispatch(bv, action, arg);
1189 if (result != UNDISPATCHED) {
1193 result = DISPATCHED;
1194 if ((action < 0) && arg.empty())
1197 if (the_locking_inset) {
1198 result = the_locking_inset->localDispatch(bv, action, arg);
1199 if (result == DISPATCHED_NOUPDATE)
1201 else if (result == DISPATCHED) {
1202 updateLocal(bv, CURSOR_PAR, false);
1204 } else if (result >= FINISHED) {
1206 case FINISHED_RIGHT:
1207 moveRightIntern(bv, false, false);
1208 result = DISPATCHED;
1211 if ((result = moveUp(bv)) >= FINISHED) {
1212 updateLocal(bv, CURSOR, false);
1213 bv->unlockInset(this);
1217 if ((result = moveDown(bv)) >= FINISHED) {
1218 updateLocal(bv, CURSOR, false);
1219 bv->unlockInset(this);
1223 result = DISPATCHED;
1226 the_locking_inset = 0;
1227 #ifdef WITH_WARNINGS
1228 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
1230 updateLocal(bv, CURSOR, false);
1234 hideInsetCursor(bv);
1237 lt = getLyXText(bv);
1241 int updflag = false;
1244 case LFUN_SELFINSERT:
1245 if (bv->buffer()->isReadonly()) {
1246 // setErrorMessage(N_("Document is read only"));
1250 /* Automatically delete the currently selected
1251 * text and replace it with what is being
1252 * typed in now. Depends on lyxrc settings
1253 * "auto_region_delete", which defaults to
1256 setUndo(bv, Undo::INSERT,
1257 lt->cursor.par(), lt->cursor.par()->next());
1259 if (lyxrc.auto_region_delete) {
1260 if (lt->selection.set()) {
1261 lt->cutSelection(bv, false);
1264 lt->clearSelection();
1265 for (string::size_type i = 0; i < arg.length(); ++i) {
1266 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1269 lt->selection.cursor = lt->cursor;
1270 updwhat = CURSOR | CURSOR_PAR;
1272 result = DISPATCHED_NOUPDATE;
1274 // --- Cursor Movements -----------------------------------
1277 moveRight(bv, false, true);
1278 lt->setSelection(bv);
1279 updwhat = SELECTION;
1282 result = moveRight(bv);
1288 moveLeft(bv, false, true);
1289 lt->setSelection(bv);
1290 updwhat = SELECTION;
1294 result = moveLeft(bv);
1300 lt->setSelection(bv);
1301 updwhat = SELECTION;
1305 result = moveDown(bv);
1311 lt->setSelection(bv);
1312 updwhat = SELECTION;
1316 result = moveUp(bv);
1328 case LFUN_BACKSPACE: {
1329 setUndo(bv, Undo::DELETE,
1330 lt->cursor.par(), lt->cursor.par()->next());
1331 if (lt->selection.set())
1332 lt->cutSelection(bv);
1335 updwhat = CURSOR_PAR;
1341 setUndo(bv, Undo::DELETE,
1342 lt->cursor.par(), lt->cursor.par()->next());
1343 if (lt->selection.set()) {
1344 lt->cutSelection(bv);
1348 updwhat = CURSOR_PAR;
1354 setUndo(bv, Undo::DELETE,
1355 lt->cursor.par(), lt->cursor.par()->next());
1356 lt->cutSelection(bv);
1357 updwhat = CURSOR_PAR;
1364 lt->copySelection(bv);
1365 updwhat = CURSOR_PAR;
1367 case LFUN_PASTESELECTION:
1369 string const clip(bv->getClipboard());
1373 if (arg == "paragraph") {
1374 lt->insertStringAsParagraphs(bv, clip);
1376 lt->insertStringAsLines(bv, clip);
1378 updwhat = CURSOR_PAR;
1383 if (!autoBreakRows) {
1385 if (CutAndPaste::nrOfParagraphs() > 1) {
1386 Alert::alert(_("Impossible operation"),
1387 _("Cannot include more than one paragraph!"),
1392 setUndo(bv, Undo::INSERT,
1393 lt->cursor.par(), lt->cursor.par()->next());
1394 lt->pasteSelection(bv);
1395 updwhat = CURSOR_PAR;
1400 case LFUN_BREAKPARAGRAPH:
1401 if (!autoBreakRows) {
1402 result = DISPATCHED;
1405 lt->breakParagraph(bv, 0);
1406 updwhat = CURSOR | FULL;
1409 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1410 if (!autoBreakRows) {
1411 result = DISPATCHED;
1414 lt->breakParagraph(bv, 1);
1415 updwhat = CURSOR | FULL;
1419 case LFUN_BREAKLINE: {
1420 if (!autoBreakRows) {
1421 result = DISPATCHED;
1424 setUndo(bv, Undo::INSERT,
1425 lt->cursor.par(), lt->cursor.par()->next());
1426 lt->insertChar(bv, Paragraph::META_NEWLINE);
1427 updwhat = CURSOR | CURSOR_PAR;
1433 // do not set layouts on non breakable textinsets
1434 if (autoBreakRows) {
1435 string cur_layout = cpar(bv)->layout();
1437 // Derive layout number from given argument (string)
1438 // and current buffer's textclass (number). */
1439 textclass_type tclass = bv->buffer()->params.textclass;
1440 string layout = arg;
1441 bool hasLayout = textclasslist[tclass].hasLayout(layout);
1443 // If the entry is obsolete, use the new one instead.
1445 string const & obs =
1446 textclasslist[tclass][layout].
1452 // see if we found the layout number:
1454 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1455 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1459 if (cur_layout != layout) {
1460 cur_layout = layout;
1461 lt->setLayout(bv, layout);
1462 bv->owner()->setLayout(cpar(bv)->layout());
1463 updwhat = CURSOR_PAR;
1467 // reset the layout box
1468 bv->owner()->setLayout(cpar(bv)->layout());
1471 case LFUN_PARAGRAPH_SPACING:
1472 // This one is absolutely not working. When fiddling with this
1473 // it also seems to me that the paragraphs inside the insettext
1474 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1476 Paragraph * par = lt->cursor.par();
1477 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1478 float cur_value = 1.0;
1479 if (cur_spacing == Spacing::Other) {
1480 cur_value = par->params().spacing().getValue();
1483 istringstream istr(arg.c_str());
1486 Spacing::Space new_spacing = cur_spacing;
1487 float new_value = cur_value;
1489 lyxerr << "Missing argument to `paragraph-spacing'"
1491 } else if (tmp == "single") {
1492 new_spacing = Spacing::Single;
1493 } else if (tmp == "onehalf") {
1494 new_spacing = Spacing::Onehalf;
1495 } else if (tmp == "double") {
1496 new_spacing = Spacing::Double;
1497 } else if (tmp == "other") {
1498 new_spacing = Spacing::Other;
1501 lyxerr << "new_value = " << tmpval << endl;
1504 } else if (tmp == "default") {
1505 new_spacing = Spacing::Default;
1507 lyxerr << _("Unknown spacing argument: ")
1510 if (cur_spacing != new_spacing || cur_value != new_value) {
1511 par->params().spacing(Spacing(new_spacing, new_value));
1512 updwhat = CURSOR_PAR;
1519 if (!bv->Dispatch(action, arg))
1520 result = UNDISPATCHED;
1527 updateLocal(bv, updwhat, updflag);
1528 /// If the action has deleted all text in the inset, we need to change the
1529 // language to the language of the surronding text.
1530 if (!was_empty && par->size() == 0 && !par->next()) {
1531 LyXFont font(LyXFont::ALL_IGNORE);
1532 font.setLanguage(bv->getParentLanguage(this));
1533 setFont(bv, font, false);
1536 if (result >= FINISHED)
1537 bv->unlockInset(this);
1539 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1540 result = DISPATCHED;
1545 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1548 buf->latexParagraphs(os, par, 0, texrow);
1549 return texrow.rows();
1553 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1555 Paragraph * p = par;
1556 unsigned int lines = 0;
1559 string const tmp = buf->asciiParagraph(p, linelen, p->previous()==0);
1560 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1568 int InsetText::docbook(Buffer const * buf, ostream & os) const
1570 Paragraph * p = par;
1571 unsigned int lines = 0;
1573 vector<string> environment_stack(10);
1574 vector<string> environment_inner(10);
1576 int const command_depth = 0;
1579 Paragraph::depth_type depth = 0; // paragraph depth
1583 int desc_on = 0; // description mode
1585 LyXLayout const & style =
1586 textclasslist[buf->params.textclass][p->layout()];
1588 // environment tag closing
1589 for (; depth > p->params().depth(); --depth) {
1590 if (environment_inner[depth] != "!-- --") {
1591 item_name = "listitem";
1592 buf->sgmlCloseTag(os, command_depth + depth,
1594 if (environment_inner[depth] == "varlistentry")
1595 buf->sgmlCloseTag(os, depth+command_depth,
1596 environment_inner[depth]);
1598 buf->sgmlCloseTag(os, depth + command_depth,
1599 environment_stack[depth]);
1600 environment_stack[depth].erase();
1601 environment_inner[depth].erase();
1604 if (depth == p->params().depth()
1605 && environment_stack[depth] != style.latexname()
1606 && !environment_stack[depth].empty()) {
1607 if (environment_inner[depth] != "!-- --") {
1608 item_name= "listitem";
1609 buf->sgmlCloseTag(os, command_depth+depth,
1611 if (environment_inner[depth] == "varlistentry")
1612 buf->sgmlCloseTag(os,
1613 depth + command_depth,
1614 environment_inner[depth]);
1617 buf->sgmlCloseTag(os, depth + command_depth,
1618 environment_stack[depth]);
1620 environment_stack[depth].erase();
1621 environment_inner[depth].erase();
1624 // Write opening SGML tags.
1625 switch (style.latextype) {
1626 case LATEX_PARAGRAPH:
1627 buf->sgmlOpenTag(os, depth + command_depth,
1632 buf->sgmlError(p, 0,
1633 _("Error : LatexType Command not allowed here.\n"));
1637 case LATEX_ENVIRONMENT:
1638 case LATEX_ITEM_ENVIRONMENT:
1639 if (depth < p->params().depth()) {
1640 depth = p->params().depth();
1641 environment_stack[depth].erase();
1644 if (environment_stack[depth] != style.latexname()) {
1645 if (environment_stack.size() == depth + 1) {
1646 environment_stack.push_back("!-- --");
1647 environment_inner.push_back("!-- --");
1649 environment_stack[depth] = style.latexname();
1650 environment_inner[depth] = "!-- --";
1651 buf->sgmlOpenTag(os, depth + command_depth,
1652 environment_stack[depth]);
1654 if (environment_inner[depth] != "!-- --") {
1655 item_name= "listitem";
1656 buf->sgmlCloseTag(os,
1657 command_depth + depth,
1659 if (environment_inner[depth] == "varlistentry")
1660 buf->sgmlCloseTag(os,
1661 depth + command_depth,
1662 environment_inner[depth]);
1666 if (style.latextype == LATEX_ENVIRONMENT) {
1667 if (!style.latexparam().empty()) {
1668 if (style.latexparam() == "CDATA")
1671 buf->sgmlOpenTag(os, depth + command_depth,
1672 style.latexparam());
1677 desc_on = (style.labeltype == LABEL_MANUAL);
1680 environment_inner[depth]= "varlistentry";
1682 environment_inner[depth]= "listitem";
1684 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1685 environment_inner[depth]);
1689 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1693 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1698 buf->sgmlOpenTag(os, depth + command_depth,
1703 buf->simpleDocBookOnePar(os, p, desc_on,
1704 depth + 1 + command_depth);
1708 // write closing SGML tags
1709 switch (style.latextype) {
1710 case LATEX_ENVIRONMENT:
1711 if (!style.latexparam().empty()) {
1712 if (style.latexparam() == "CDATA")
1715 buf->sgmlCloseTag(os, depth + command_depth,
1716 style.latexparam());
1719 case LATEX_ITEM_ENVIRONMENT:
1720 if (desc_on == 1) break;
1722 buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1724 case LATEX_PARAGRAPH:
1725 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1728 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1734 for (int d = depth; d >= 0; --d) {
1735 if (!environment_stack[depth].empty()) {
1736 if (environment_inner[depth] != "!-- --") {
1737 item_name = "listitem";
1738 buf->sgmlCloseTag(os, command_depth + depth,
1740 if (environment_inner[depth] == "varlistentry")
1741 buf->sgmlCloseTag(os, depth + command_depth,
1742 environment_inner[depth]);
1745 buf->sgmlCloseTag(os, depth + command_depth,
1746 environment_stack[depth]);
1754 void InsetText::validate(LaTeXFeatures & features) const
1756 Paragraph * p = par;
1758 p->validate(features);
1764 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1766 if (textclasslist[buf->params.textclass][p->layout()].labeltype != LABEL_MANUAL)
1769 return p->beginningOfMainBody();
1773 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1775 if (the_locking_inset) {
1776 the_locking_inset->getCursorPos(bv, x, y);
1779 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1780 y = cy(bv) - TEXT_TO_INSET_OFFSET;
1784 int InsetText::insetInInsetY() const
1786 if (!the_locking_inset)
1789 return (inset_y + the_locking_inset->insetInInsetY());
1793 void InsetText::toggleInsetCursor(BufferView * bv)
1795 if (the_locking_inset) {
1796 the_locking_inset->toggleInsetCursor(bv);
1800 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1802 int const asc = lyxfont::maxAscent(font);
1803 int const desc = lyxfont::maxDescent(font);
1805 if (isCursorVisible())
1806 bv->hideLockedInsetCursor();
1808 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1809 toggleCursorVisible();
1813 void InsetText::showInsetCursor(BufferView * bv, bool show)
1815 if (the_locking_inset) {
1816 the_locking_inset->showInsetCursor(bv, show);
1819 if (!isCursorVisible()) {
1820 LyXFont const font =
1821 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1823 int const asc = lyxfont::maxAscent(font);
1824 int const desc = lyxfont::maxDescent(font);
1826 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1828 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1829 setCursorVisible(true);
1834 void InsetText::hideInsetCursor(BufferView * bv)
1836 if (isCursorVisible()) {
1837 bv->hideLockedInsetCursor();
1838 setCursorVisible(false);
1840 if (the_locking_inset)
1841 the_locking_inset->hideInsetCursor(bv);
1845 void InsetText::fitInsetCursor(BufferView * bv) const
1847 if (the_locking_inset) {
1848 the_locking_inset->fitInsetCursor(bv);
1851 LyXFont const font =
1852 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1854 int const asc = lyxfont::maxAscent(font);
1855 int const desc = lyxfont::maxDescent(font);
1857 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1858 need_update |= FULL;
1862 UpdatableInset::RESULT
1863 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1865 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1866 return moveLeftIntern(bv, true, activate_inset, selecting);
1868 return moveRightIntern(bv, true, activate_inset, selecting);
1872 UpdatableInset::RESULT
1873 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1875 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1876 return moveRightIntern(bv, false, activate_inset, selecting);
1878 return moveLeftIntern(bv, false, activate_inset, selecting);
1882 UpdatableInset::RESULT
1883 InsetText::moveRightIntern(BufferView * bv, bool front,
1884 bool activate_inset, bool selecting)
1886 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1887 return FINISHED_RIGHT;
1888 if (activate_inset && checkAndActivateInset(bv, front))
1890 getLyXText(bv)->cursorRight(bv);
1892 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1893 return DISPATCHED_NOUPDATE;
1897 UpdatableInset::RESULT
1898 InsetText::moveLeftIntern(BufferView * bv, bool front,
1899 bool activate_inset, bool selecting)
1901 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1903 getLyXText(bv)->cursorLeft(bv);
1905 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1906 if (activate_inset && checkAndActivateInset(bv, front))
1908 return DISPATCHED_NOUPDATE;
1912 UpdatableInset::RESULT
1913 InsetText::moveUp(BufferView * bv)
1915 if (!crow(bv)->previous())
1917 getLyXText(bv)->cursorUp(bv);
1918 return DISPATCHED_NOUPDATE;
1922 UpdatableInset::RESULT
1923 InsetText::moveDown(BufferView * bv)
1925 if (!crow(bv)->next())
1926 return FINISHED_DOWN;
1927 getLyXText(bv)->cursorDown(bv);
1928 return DISPATCHED_NOUPDATE;
1932 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1934 if (the_locking_inset) {
1935 if (the_locking_inset->insetAllowed(inset))
1936 return the_locking_inset->insertInset(bv, inset);
1939 inset->setOwner(this);
1940 hideInsetCursor(bv);
1941 getLyXText(bv)->insertInset(bv, inset);
1943 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1948 bool InsetText::insetAllowed(Inset::Code code) const
1950 // in_insetAllowed is a really gross hack,
1951 // to allow us to call the owner's insetAllowed
1952 // without stack overflow, which can happen
1953 // when the owner uses InsetCollapsable::insetAllowed()
1955 if (in_insetAllowed)
1957 in_insetAllowed = true;
1958 if (the_locking_inset)
1959 ret = the_locking_inset->insetAllowed(code);
1961 ret = owner()->insetAllowed(code);
1962 in_insetAllowed = false;
1967 UpdatableInset * InsetText::getLockingInset() const
1969 return the_locking_inset ? the_locking_inset->getLockingInset() :
1970 const_cast<InsetText *>(this);
1974 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1978 if (the_locking_inset)
1979 return the_locking_inset->getFirstLockingInsetOfType(c);
1984 bool InsetText::showInsetDialog(BufferView * bv) const
1986 if (the_locking_inset)
1987 return the_locking_inset->showInsetDialog(bv);
1992 vector<string> const InsetText::getLabelList() const
1994 vector<string> label_list;
1996 Paragraph * tpar = par;
1998 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1999 Paragraph::inset_iterator end = tpar->inset_iterator_end();
2000 for (; beg != end; ++beg) {
2001 vector<string> const l = (*beg)->getLabelList();
2002 label_list.insert(label_list.end(), l.begin(), l.end());
2004 tpar = tpar->next();
2010 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
2013 if (the_locking_inset) {
2014 the_locking_inset->setFont(bv, font, toggleall, selectall);
2017 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
2018 getLyXText(bv)->setFont(bv, font, toggleall);
2023 lt = getLyXText(bv);
2026 if (lt->selection.set()) {
2027 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
2031 lt->toggleFree(bv, font, toggleall);
2033 lt->clearSelection();
2035 bool flag = (selectall || lt->selection.set());
2039 updateLocal(bv, FULL, true);
2041 updateLocal(bv, CURSOR_PAR, true);
2045 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
2047 if (cpar(bv)->isInset(cpos(bv))) {
2049 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
2050 if (!isHighlyEditableInset(inset))
2052 inset->edit(bv, front);
2053 if (!the_locking_inset)
2055 updateLocal(bv, CURSOR, false);
2062 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
2065 x -= drawTextXOffset;
2067 int dummyy = y + insetAscent;
2068 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
2075 inset_x = cix(bv) - top_x + drawTextXOffset;
2076 inset_y = ciy(bv) + drawTextYOffset;
2077 inset->edit(bv, x - inset_x, y - inset_y, button);
2078 if (!the_locking_inset)
2080 updateLocal(bv, CURSOR, false);
2087 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
2090 int w = UpdatableInset::getMaxWidth(bv, inset);
2095 w = w - top_x + owner()->x();
2098 w -= (2 * TEXT_TO_INSET_OFFSET);
2101 return UpdatableInset::getMaxWidth(bv, inset);
2106 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2108 // we have to unlock any locked inset otherwise we're in troubles
2109 the_locking_inset = 0;
2111 Paragraph * tmp = par->next();
2116 par = new Paragraph(*p, same_id);
2117 par->setInsetOwner(this);
2118 Paragraph * np = par;
2121 np->next(new Paragraph(*p, same_id));
2122 np->next()->previous(np);
2124 np->setInsetOwner(this);
2131 void InsetText::setText(string const & data, LyXFont const & font)
2134 for (unsigned int i=0; i < data.length(); ++i)
2135 par->insertChar(i, data[i], font);
2140 void InsetText::setAutoBreakRows(bool flag)
2142 if (flag != autoBreakRows) {
2143 autoBreakRows = flag;
2151 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2153 if (how != drawFrame_) {
2156 updateLocal(bv, DRAW_FRAME, false);
2161 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2163 if (frame_color != col) {
2166 updateLocal(bv, DRAW_FRAME, false);
2171 int InsetText::cx(BufferView * bv) const
2173 // we do nothing dangerous so we use a local cache
2174 LyXText * llt = getLyXText(bv);
2175 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2176 if (the_locking_inset) {
2177 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2179 if (font.isVisibleRightToLeft())
2180 x -= the_locking_inset->width(bv, font);
2186 int InsetText::cix(BufferView * bv) const
2188 // we do nothing dangerous so we use a local cache
2189 LyXText * llt = getLyXText(bv);
2190 int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2191 if (the_locking_inset) {
2192 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2194 if (font.isVisibleRightToLeft())
2195 x -= the_locking_inset->width(bv, font);
2201 int InsetText::cy(BufferView * bv) const
2204 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2208 int InsetText::ciy(BufferView * bv) const
2211 return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2215 pos_type InsetText::cpos(BufferView * bv) const
2217 return getLyXText(bv)->cursor.pos();
2221 Paragraph * InsetText::cpar(BufferView * bv) const
2223 return getLyXText(bv)->cursor.par();
2227 bool InsetText::cboundary(BufferView * bv) const
2229 return getLyXText(bv)->cursor.boundary();
2233 Row * InsetText::crow(BufferView * bv) const
2235 return getLyXText(bv)->cursor.row();
2239 LyXText * InsetText::getLyXText(BufferView const * lbv,
2240 bool const recursive) const
2242 if (cached_bview == lbv) {
2243 if (recursive && the_locking_inset)
2244 return the_locking_inset->getLyXText(lbv, true);
2245 LyXText * lt = cached_text.get();
2246 lyx::Assert(lt && lt->firstRow()->par() == par);
2249 // Super UGLY! (Lgb)
2250 BufferView * bv = const_cast<BufferView *>(lbv);
2253 Cache::iterator it = cache.find(bv);
2255 if (it != cache.end()) {
2258 } else if (do_resize) {
2259 resizeLyXText(do_resize);
2261 if (lt || !it->second.remove) {
2262 lyx::Assert(it->second.text.get());
2263 cached_text = it->second.text;
2264 if (recursive && the_locking_inset) {
2265 return the_locking_inset->getLyXText(bv, true);
2267 return cached_text.get();
2268 } else if (it->second.remove) {
2270 saveLyXTextState(it->second.text.get());
2276 // when we have to reinit the existing LyXText!
2278 it->second.text->init(bv);
2279 restoreLyXTextState(bv, it->second.text.get());
2280 it->second.remove = false;
2282 cached_text = it->second.text;
2283 if (the_locking_inset && recursive) {
2284 return the_locking_inset->getLyXText(bv);
2286 return cached_text.get();
2289 // we are here only if we don't have a BufferView * in the cache!!!
2291 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2292 cached_text->init(bv);
2293 restoreLyXTextState(bv, cached_text.get());
2295 cache.insert(make_pair(bv, cached_text));
2297 if (the_locking_inset && recursive) {
2298 return the_locking_inset->getLyXText(bv);
2300 return cached_text.get();
2304 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2308 Cache::iterator it = cache.find(bv);
2310 if (it == cache.end()) {
2314 lyx::Assert(it->second.text.get());
2316 it->second.remove = true;
2318 /// then remove all LyXText in text-insets
2319 Paragraph * p = par;
2320 for (; p; p = p->next()) {
2321 p->deleteInsetsLyXText(bv);
2327 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2330 // we cannot resize this because we are in use!
2331 // so do this on the next possible getLyXText()
2336 // lyxerr << "InsetText::resizeLyXText\n";
2337 if (!par->next() && !par->size()) { // no data, resize not neccessary!
2338 // we have to do this as a fixed width may have changed!
2339 LyXText * t = getLyXText(bv);
2340 saveLyXTextState(t);
2342 restoreLyXTextState(bv, t);
2345 // one endless line, resize normally not necessary
2346 if (!force && getMaxWidth(bv, this) < 0)
2349 Cache::iterator it = cache.find(bv);
2350 if (it == cache.end()) {
2353 lyx::Assert(it->second.text.get());
2355 LyXText * t = it->second.text.get();
2356 saveLyXTextState(t);
2357 for (Paragraph * p = par; p; p = p->next()) {
2358 p->resizeInsetsLyXText(bv);
2361 restoreLyXTextState(bv, t);
2362 if (the_locking_inset) {
2363 inset_x = cix(bv) - top_x + drawTextXOffset;
2364 inset_y = ciy(bv) + drawTextYOffset;
2368 t->first_y = bv->screen()->topCursorVisible(t);
2371 updateLocal(bv, FULL, false);
2372 // this will scroll the screen such that the cursor becomes visible
2373 bv->updateScrollbar();
2375 need_update |= FULL;
2380 void InsetText::reinitLyXText() const
2383 // we cannot resize this because we are in use!
2384 // so do this on the next possible getLyXText()
2390 // lyxerr << "InsetText::reinitLyXText\n";
2391 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2392 lyx::Assert(it->second.text.get());
2394 LyXText * t = it->second.text.get();
2395 BufferView * bv = it->first;
2397 saveLyXTextState(t);
2398 for (Paragraph * p = par; p; p = p->next()) {
2399 p->resizeInsetsLyXText(bv);
2402 restoreLyXTextState(bv, t);
2403 if (the_locking_inset) {
2404 inset_x = cix(bv) - top_x + drawTextXOffset;
2405 inset_y = ciy(bv) + drawTextYOffset;
2408 t->first_y = bv->screen()->topCursorVisible(t);
2411 updateLocal(bv, FULL, false);
2412 // this will scroll the screen such that the cursor becomes visible
2413 bv->updateScrollbar();
2421 void InsetText::removeNewlines()
2423 bool changed = false;
2425 for (Paragraph * p = par; p; p = p->next()) {
2426 for (int i = 0; i < p->size(); ++i) {
2427 if (p->getChar(i) == Paragraph::META_NEWLINE) {
2438 bool InsetText::nodraw() const
2440 if (the_locking_inset)
2441 return the_locking_inset->nodraw();
2442 return UpdatableInset::nodraw();
2446 int InsetText::scroll(bool recursive) const
2448 int sx = UpdatableInset::scroll(false);
2450 if (recursive && the_locking_inset)
2451 sx += the_locking_inset->scroll(recursive);
2457 bool InsetText::doClearArea() const
2459 return !locked || (need_update & (FULL|INIT));
2463 void InsetText::selectAll(BufferView * bv)
2465 getLyXText(bv)->cursorTop(bv);
2466 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2467 getLyXText(bv)->cursorBottom(bv);
2468 getLyXText(bv)->setSelection(bv);
2472 void InsetText::clearSelection(BufferView * bv)
2474 getLyXText(bv)->clearSelection();
2478 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2480 Painter & pain = bv->painter();
2482 int h = insetAscent + insetDescent;
2483 int ty = baseline - insetAscent;
2489 if ((ty + h) > pain.paperHeight())
2490 h = pain.paperHeight();
2491 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2492 w = pain.paperWidth();
2493 // w -= TEXT_TO_INSET_OFFSET;
2494 pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2497 frame_is_visible = false;
2501 Paragraph * InsetText::getParFromID(int id) const
2504 Paragraph * result = par;
2505 Paragraph * ires = 0;
2506 while (result && result->id() != id) {
2507 if ((ires = result->getParFromID(id)))
2509 result = result->next();
2513 Paragraph * tmp = par;
2515 if (tmp->id() == id) {
2518 Paragraph * tmp2 = tmp->getParFromID(id);
2529 Paragraph * InsetText::firstParagraph() const
2532 if (the_locking_inset)
2533 if ((result = the_locking_inset->firstParagraph()))
2539 Paragraph * InsetText::getFirstParagraph(int i) const
2541 return (i == 0) ? par : 0;
2545 LyXCursor const & InsetText::cursor(BufferView * bv) const
2547 if (the_locking_inset)
2548 return the_locking_inset->cursor(bv);
2549 return getLyXText(bv)->cursor;
2553 Paragraph * InsetText::paragraph() const
2559 void InsetText::paragraph(Paragraph * p)
2561 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2562 // caller of this function has to take care of it. This IS important
2563 // as we could have to insert a paragraph before this one and just
2564 // link the actual to a new ones next and set it with this function
2567 // set ourself as owner for all the paragraphs inserted!
2568 Paragraph * np = par;
2570 np->setInsetOwner(this);
2574 // redraw myself when asked for
2579 Inset * InsetText::getInsetFromID(int id_arg) const
2582 return const_cast<InsetText *>(this);
2584 Paragraph * lp = par;
2587 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2588 en = lp->inset_iterator_end();
2591 if ((*it)->id() == id_arg)
2593 Inset * in = (*it)->getInsetFromID(id_arg);
2603 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2609 lt = getLyXText(bv);
2612 if (the_locking_inset) {
2613 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2620 // we have to go on checking so move cusor to the next char
2621 lt->cursor.pos(lt->cursor.pos() + 1);
2623 str = lt->selectNextWordToSpellcheck(bv, value);
2625 bv->unlockInset(const_cast<InsetText *>(this));
2634 void InsetText::selectSelectedWord(BufferView * bv)
2636 if (the_locking_inset) {
2637 the_locking_inset->selectSelectedWord(bv);
2640 getLyXText(bv)->selectSelectedWord(bv);
2641 updateLocal(bv, SELECTION, false);
2645 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2647 if (the_locking_inset) {
2648 the_locking_inset->toggleSelection(bv, kill_selection);
2652 lt = getLyXText(bv);
2656 int x = top_x + TEXT_TO_INSET_OFFSET;
2658 Row * row = lt->firstRow();
2659 int y_offset = top_baseline - row->ascent_of_text();
2661 while ((row != 0) && ((y+row->height()) <= 0)) {
2668 if (need_update & SELECTION)
2670 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2676 bool InsetText::searchForward(BufferView * bv, string const & str,
2679 if (the_locking_inset) {
2680 if (the_locking_inset->searchForward(bv, str, cs, mw))
2684 lt = getLyXText(bv);
2687 Paragraph * lpar = lt->cursor.par();
2688 pos_type pos = lt->cursor.pos();
2689 if (pos < lpar->size() - 1)
2693 lpar = lpar->next();
2698 // we have to unlock ourself in this function by default!
2699 bv->unlockInset(const_cast<InsetText *>(this));
2702 lt->setCursor(bv, lpar, pos);
2706 if (LyXFind(bv, str, true, true, cs , mw)) {
2709 // we have to unlock ourself in this function by default!
2710 bv->unlockInset(const_cast<InsetText *>(this));
2714 bool InsetText::searchBackward(BufferView * bv, string const & str,
2717 if (the_locking_inset)
2718 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2720 if (LyXFind(bv, str, false, true, cs, mw)) {
2723 // we have to unlock ourself in this function by default!
2724 bv->unlockInset(const_cast<InsetText *>(this));
2729 bool InsetText::checkInsertChar(LyXFont & font)
2732 return owner()->checkInsertChar(font);
2737 void InsetText::collapseParagraphs(BufferView * bv) const
2739 BufferParams const & bparams = bv->buffer()->params;
2740 LyXText * llt = getLyXText(bv);
2742 while(par->next()) {
2743 if (par->size() && par->next()->size() &&
2744 !par->isSeparator(par->size()-1))
2746 par->insertChar(par->size(), ' ');
2748 if (llt->selection.set()) {
2749 if (llt->selection.start.par() == par->next()) {
2750 llt->selection.start.par(par);
2751 llt->selection.start.pos(
2752 llt->selection.start.pos() + par->size());
2754 if (llt->selection.end.par() == par->next()) {
2755 llt->selection.end.par(par);
2756 llt->selection.end.pos(
2757 llt->selection.end.pos() + par->size());
2760 par->pasteParagraph(bparams);
2766 void InsetText::getDrawFont(LyXFont & font) const
2770 owner()->getDrawFont(font);
2774 void InsetText::appendParagraphs(BufferParams const & bparams,
2778 Paragraph * tmpbuf = newpar;
2779 Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2781 while (tmpbuf->next()) {
2782 tmpbuf = tmpbuf->next();
2783 lastbuffer->next(new Paragraph(*tmpbuf, false));
2784 lastbuffer->next()->previous(lastbuffer);
2785 lastbuffer = lastbuffer->next();
2788 while (lastbuffer->next())
2789 lastbuffer = lastbuffer->next();
2790 if (newpar->size() && lastbuffer->size() &&
2791 !lastbuffer->isSeparator(lastbuffer->size()-1))
2793 lastbuffer->insertChar(lastbuffer->size(), ' ');
2796 // make the buf exactly the same layout than our last paragraph
2797 buf->makeSameLayout(lastbuffer);
2800 lastbuffer->next(buf);
2801 buf->previous(lastbuffer);
2802 lastbuffer->pasteParagraph(bparams);