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),
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);
1382 updwhat = CURSOR_PAR;
1387 if (!autoBreakRows) {
1389 if (CutAndPaste::nrOfParagraphs() > 1) {
1390 Alert::alert(_("Impossible operation"),
1391 _("Cannot include more than one paragraph!"),
1396 setUndo(bv, Undo::INSERT,
1397 lt->cursor.par(), lt->cursor.par()->next());
1398 lt->pasteSelection(bv);
1399 updwhat = CURSOR_PAR;
1404 case LFUN_BREAKPARAGRAPH:
1405 if (!autoBreakRows) {
1406 result = DISPATCHED;
1409 lt->breakParagraph(bv, 0);
1410 updwhat = CURSOR | FULL;
1413 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1414 if (!autoBreakRows) {
1415 result = DISPATCHED;
1418 lt->breakParagraph(bv, 1);
1419 updwhat = CURSOR | FULL;
1423 case LFUN_BREAKLINE: {
1424 if (!autoBreakRows) {
1425 result = DISPATCHED;
1428 setUndo(bv, Undo::INSERT,
1429 lt->cursor.par(), lt->cursor.par()->next());
1430 lt->insertChar(bv, Paragraph::META_NEWLINE);
1431 updwhat = CURSOR | CURSOR_PAR;
1437 // do not set layouts on non breakable textinsets
1438 if (autoBreakRows) {
1439 string cur_layout = cpar(bv)->layout();
1441 // Derive layout number from given argument (string)
1442 // and current buffer's textclass (number). */
1443 textclass_type tclass = bv->buffer()->params.textclass;
1444 string layout = arg;
1445 bool hasLayout = textclasslist[tclass].hasLayout(layout);
1447 // If the entry is obsolete, use the new one instead.
1449 string const & obs =
1450 textclasslist[tclass][layout].
1456 // see if we found the layout number:
1458 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1459 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1463 if (cur_layout != layout) {
1464 cur_layout = layout;
1465 lt->setLayout(bv, layout);
1466 bv->owner()->setLayout(cpar(bv)->layout());
1467 updwhat = CURSOR_PAR;
1471 // reset the layout box
1472 bv->owner()->setLayout(cpar(bv)->layout());
1475 case LFUN_PARAGRAPH_SPACING:
1476 // This one is absolutely not working. When fiddling with this
1477 // it also seems to me that the paragraphs inside the insettext
1478 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1480 Paragraph * par = lt->cursor.par();
1481 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1482 float cur_value = 1.0;
1483 if (cur_spacing == Spacing::Other) {
1484 cur_value = par->params().spacing().getValue();
1487 istringstream istr(arg.c_str());
1490 Spacing::Space new_spacing = cur_spacing;
1491 float new_value = cur_value;
1493 lyxerr << "Missing argument to `paragraph-spacing'"
1495 } else if (tmp == "single") {
1496 new_spacing = Spacing::Single;
1497 } else if (tmp == "onehalf") {
1498 new_spacing = Spacing::Onehalf;
1499 } else if (tmp == "double") {
1500 new_spacing = Spacing::Double;
1501 } else if (tmp == "other") {
1502 new_spacing = Spacing::Other;
1505 lyxerr << "new_value = " << tmpval << endl;
1508 } else if (tmp == "default") {
1509 new_spacing = Spacing::Default;
1511 lyxerr << _("Unknown spacing argument: ")
1514 if (cur_spacing != new_spacing || cur_value != new_value) {
1515 par->params().spacing(Spacing(new_spacing, new_value));
1516 updwhat = CURSOR_PAR;
1523 if (!bv->Dispatch(action, arg))
1524 result = UNDISPATCHED;
1531 updateLocal(bv, updwhat, updflag);
1532 /// If the action has deleted all text in the inset, we need to change the
1533 // language to the language of the surronding text.
1534 if (!was_empty && par->size() == 0 && !par->next()) {
1535 LyXFont font(LyXFont::ALL_IGNORE);
1536 font.setLanguage(bv->getParentLanguage(this));
1537 setFont(bv, font, false);
1540 if (result >= FINISHED)
1541 bv->unlockInset(this);
1543 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1544 result = DISPATCHED;
1549 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1552 buf->latexParagraphs(os, par, 0, texrow);
1553 return texrow.rows();
1557 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1559 Paragraph * p = par;
1560 unsigned int lines = 0;
1563 string const tmp = buf->asciiParagraph(p, linelen, p->previous()==0);
1564 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1572 int InsetText::docbook(Buffer const * buf, ostream & os) const
1574 Paragraph * p = par;
1575 unsigned int lines = 0;
1577 vector<string> environment_stack(10);
1578 vector<string> environment_inner(10);
1580 int const command_depth = 0;
1583 Paragraph::depth_type depth = 0; // paragraph depth
1587 int desc_on = 0; // description mode
1589 LyXLayout const & style =
1590 textclasslist[buf->params.textclass][p->layout()];
1592 // environment tag closing
1593 for (; depth > p->params().depth(); --depth) {
1594 if (environment_inner[depth] != "!-- --") {
1595 item_name = "listitem";
1596 buf->sgmlCloseTag(os, command_depth + depth,
1598 if (environment_inner[depth] == "varlistentry")
1599 buf->sgmlCloseTag(os, depth+command_depth,
1600 environment_inner[depth]);
1602 buf->sgmlCloseTag(os, depth + command_depth,
1603 environment_stack[depth]);
1604 environment_stack[depth].erase();
1605 environment_inner[depth].erase();
1608 if (depth == p->params().depth()
1609 && environment_stack[depth] != style.latexname()
1610 && !environment_stack[depth].empty()) {
1611 if (environment_inner[depth] != "!-- --") {
1612 item_name= "listitem";
1613 buf->sgmlCloseTag(os, command_depth+depth,
1615 if (environment_inner[depth] == "varlistentry")
1616 buf->sgmlCloseTag(os,
1617 depth + command_depth,
1618 environment_inner[depth]);
1621 buf->sgmlCloseTag(os, depth + command_depth,
1622 environment_stack[depth]);
1624 environment_stack[depth].erase();
1625 environment_inner[depth].erase();
1628 // Write opening SGML tags.
1629 switch (style.latextype) {
1630 case LATEX_PARAGRAPH:
1631 buf->sgmlOpenTag(os, depth + command_depth,
1636 buf->sgmlError(p, 0,
1637 _("Error : LatexType Command not allowed here.\n"));
1641 case LATEX_ENVIRONMENT:
1642 case LATEX_ITEM_ENVIRONMENT:
1643 if (depth < p->params().depth()) {
1644 depth = p->params().depth();
1645 environment_stack[depth].erase();
1648 if (environment_stack[depth] != style.latexname()) {
1649 if (environment_stack.size() == depth + 1) {
1650 environment_stack.push_back("!-- --");
1651 environment_inner.push_back("!-- --");
1653 environment_stack[depth] = style.latexname();
1654 environment_inner[depth] = "!-- --";
1655 buf->sgmlOpenTag(os, depth + command_depth,
1656 environment_stack[depth]);
1658 if (environment_inner[depth] != "!-- --") {
1659 item_name= "listitem";
1660 buf->sgmlCloseTag(os,
1661 command_depth + depth,
1663 if (environment_inner[depth] == "varlistentry")
1664 buf->sgmlCloseTag(os,
1665 depth + command_depth,
1666 environment_inner[depth]);
1670 if (style.latextype == LATEX_ENVIRONMENT) {
1671 if (!style.latexparam().empty()) {
1672 if (style.latexparam() == "CDATA")
1675 buf->sgmlOpenTag(os, depth + command_depth,
1676 style.latexparam());
1681 desc_on = (style.labeltype == LABEL_MANUAL);
1684 environment_inner[depth]= "varlistentry";
1686 environment_inner[depth]= "listitem";
1688 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1689 environment_inner[depth]);
1693 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1697 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1702 buf->sgmlOpenTag(os, depth + command_depth,
1707 buf->simpleDocBookOnePar(os, p, desc_on,
1708 depth + 1 + command_depth);
1712 // write closing SGML tags
1713 switch (style.latextype) {
1714 case LATEX_ENVIRONMENT:
1715 if (!style.latexparam().empty()) {
1716 if (style.latexparam() == "CDATA")
1719 buf->sgmlCloseTag(os, depth + command_depth,
1720 style.latexparam());
1723 case LATEX_ITEM_ENVIRONMENT:
1724 if (desc_on == 1) break;
1726 buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1728 case LATEX_PARAGRAPH:
1729 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1732 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1738 for (int d = depth; d >= 0; --d) {
1739 if (!environment_stack[depth].empty()) {
1740 if (environment_inner[depth] != "!-- --") {
1741 item_name = "listitem";
1742 buf->sgmlCloseTag(os, command_depth + depth,
1744 if (environment_inner[depth] == "varlistentry")
1745 buf->sgmlCloseTag(os, depth + command_depth,
1746 environment_inner[depth]);
1749 buf->sgmlCloseTag(os, depth + command_depth,
1750 environment_stack[depth]);
1758 void InsetText::validate(LaTeXFeatures & features) const
1760 Paragraph * p = par;
1762 p->validate(features);
1768 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1770 if (textclasslist[buf->params.textclass][p->layout()].labeltype != LABEL_MANUAL)
1773 return p->beginningOfMainBody();
1777 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1779 if (the_locking_inset) {
1780 the_locking_inset->getCursorPos(bv, x, y);
1783 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1784 y = cy(bv) - TEXT_TO_INSET_OFFSET;
1788 int InsetText::insetInInsetY() const
1790 if (!the_locking_inset)
1793 return (inset_y + the_locking_inset->insetInInsetY());
1797 void InsetText::toggleInsetCursor(BufferView * bv)
1799 if (the_locking_inset) {
1800 the_locking_inset->toggleInsetCursor(bv);
1804 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1806 int const asc = lyxfont::maxAscent(font);
1807 int const desc = lyxfont::maxDescent(font);
1809 if (isCursorVisible())
1810 bv->hideLockedInsetCursor();
1812 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1813 toggleCursorVisible();
1817 void InsetText::showInsetCursor(BufferView * bv, bool show)
1819 if (the_locking_inset) {
1820 the_locking_inset->showInsetCursor(bv, show);
1823 if (!isCursorVisible()) {
1824 LyXFont const font =
1825 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1827 int const asc = lyxfont::maxAscent(font);
1828 int const desc = lyxfont::maxDescent(font);
1830 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1832 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1833 setCursorVisible(true);
1838 void InsetText::hideInsetCursor(BufferView * bv)
1840 if (isCursorVisible()) {
1841 bv->hideLockedInsetCursor();
1842 setCursorVisible(false);
1844 if (the_locking_inset)
1845 the_locking_inset->hideInsetCursor(bv);
1849 void InsetText::fitInsetCursor(BufferView * bv) const
1851 if (the_locking_inset) {
1852 the_locking_inset->fitInsetCursor(bv);
1855 LyXFont const font =
1856 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1858 int const asc = lyxfont::maxAscent(font);
1859 int const desc = lyxfont::maxDescent(font);
1861 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1862 need_update |= FULL;
1866 UpdatableInset::RESULT
1867 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1869 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1870 return moveLeftIntern(bv, true, activate_inset, selecting);
1872 return moveRightIntern(bv, true, activate_inset, selecting);
1876 UpdatableInset::RESULT
1877 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1879 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1880 return moveRightIntern(bv, false, activate_inset, selecting);
1882 return moveLeftIntern(bv, false, activate_inset, selecting);
1886 UpdatableInset::RESULT
1887 InsetText::moveRightIntern(BufferView * bv, bool front,
1888 bool activate_inset, bool selecting)
1890 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1891 return FINISHED_RIGHT;
1892 if (activate_inset && checkAndActivateInset(bv, front))
1894 getLyXText(bv)->cursorRight(bv);
1896 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1897 return DISPATCHED_NOUPDATE;
1901 UpdatableInset::RESULT
1902 InsetText::moveLeftIntern(BufferView * bv, bool front,
1903 bool activate_inset, bool selecting)
1905 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1907 getLyXText(bv)->cursorLeft(bv);
1909 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1910 if (activate_inset && checkAndActivateInset(bv, front))
1912 return DISPATCHED_NOUPDATE;
1916 UpdatableInset::RESULT
1917 InsetText::moveUp(BufferView * bv)
1919 if (!crow(bv)->previous())
1921 getLyXText(bv)->cursorUp(bv);
1922 return DISPATCHED_NOUPDATE;
1926 UpdatableInset::RESULT
1927 InsetText::moveDown(BufferView * bv)
1929 if (!crow(bv)->next())
1930 return FINISHED_DOWN;
1931 getLyXText(bv)->cursorDown(bv);
1932 return DISPATCHED_NOUPDATE;
1936 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1938 if (the_locking_inset) {
1939 if (the_locking_inset->insetAllowed(inset))
1940 return the_locking_inset->insertInset(bv, inset);
1943 inset->setOwner(this);
1944 hideInsetCursor(bv);
1945 getLyXText(bv)->insertInset(bv, inset);
1947 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1952 bool InsetText::insetAllowed(Inset::Code code) const
1954 // in_insetAllowed is a really gross hack,
1955 // to allow us to call the owner's insetAllowed
1956 // without stack overflow, which can happen
1957 // when the owner uses InsetCollapsable::insetAllowed()
1959 if (in_insetAllowed)
1961 in_insetAllowed = true;
1962 if (the_locking_inset)
1963 ret = the_locking_inset->insetAllowed(code);
1965 ret = owner()->insetAllowed(code);
1966 in_insetAllowed = false;
1971 UpdatableInset * InsetText::getLockingInset() const
1973 return the_locking_inset ? the_locking_inset->getLockingInset() :
1974 const_cast<InsetText *>(this);
1978 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1982 if (the_locking_inset)
1983 return the_locking_inset->getFirstLockingInsetOfType(c);
1988 bool InsetText::showInsetDialog(BufferView * bv) const
1990 if (the_locking_inset)
1991 return the_locking_inset->showInsetDialog(bv);
1996 vector<string> const InsetText::getLabelList() const
1998 vector<string> label_list;
2000 Paragraph * tpar = par;
2002 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
2003 Paragraph::inset_iterator end = tpar->inset_iterator_end();
2004 for (; beg != end; ++beg) {
2005 vector<string> const l = (*beg)->getLabelList();
2006 label_list.insert(label_list.end(), l.begin(), l.end());
2008 tpar = tpar->next();
2014 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
2017 if (the_locking_inset) {
2018 the_locking_inset->setFont(bv, font, toggleall, selectall);
2021 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
2022 getLyXText(bv)->setFont(bv, font, toggleall);
2027 lt = getLyXText(bv);
2030 if (lt->selection.set()) {
2031 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
2035 lt->toggleFree(bv, font, toggleall);
2037 lt->clearSelection();
2039 bool flag = (selectall || lt->selection.set());
2043 updateLocal(bv, FULL, true);
2045 updateLocal(bv, CURSOR_PAR, true);
2049 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
2051 if (cpar(bv)->isInset(cpos(bv))) {
2053 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
2054 if (!isHighlyEditableInset(inset))
2056 inset->edit(bv, front);
2057 if (!the_locking_inset)
2059 updateLocal(bv, CURSOR, false);
2066 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
2069 x -= drawTextXOffset;
2071 int dummyy = y + insetAscent;
2072 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
2073 // we only do the edit() call if the inset was hit by the mouse
2074 // or if it is a highly editable inset. So we should call this
2075 // function from our own edit with button < 0.
2076 if (button < 0 && !isHighlyEditableInset(inset))
2084 inset_x = cix(bv) - top_x + drawTextXOffset;
2085 inset_y = ciy(bv) + drawTextYOffset;
2086 inset->edit(bv, x - inset_x, y - inset_y, button);
2087 if (!the_locking_inset)
2089 updateLocal(bv, CURSOR, false);
2096 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
2099 int w = UpdatableInset::getMaxWidth(bv, inset);
2104 w = w - top_x + owner()->x();
2107 w -= (2 * TEXT_TO_INSET_OFFSET);
2110 return UpdatableInset::getMaxWidth(bv, inset);
2115 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2117 // we have to unlock any locked inset otherwise we're in troubles
2118 the_locking_inset = 0;
2120 Paragraph * tmp = par->next();
2125 par = new Paragraph(*p, same_id);
2126 par->setInsetOwner(this);
2127 Paragraph * np = par;
2130 np->next(new Paragraph(*p, same_id));
2131 np->next()->previous(np);
2133 np->setInsetOwner(this);
2140 void InsetText::setText(string const & data, LyXFont const & font)
2143 for (unsigned int i=0; i < data.length(); ++i)
2144 par->insertChar(i, data[i], font);
2149 void InsetText::setAutoBreakRows(bool flag)
2151 if (flag != autoBreakRows) {
2152 autoBreakRows = flag;
2160 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2162 if (how != drawFrame_) {
2165 updateLocal(bv, DRAW_FRAME, false);
2170 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2172 if (frame_color != col) {
2175 updateLocal(bv, DRAW_FRAME, false);
2180 int InsetText::cx(BufferView * bv) const
2182 // we do nothing dangerous so we use a local cache
2183 LyXText * llt = getLyXText(bv);
2184 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2185 if (the_locking_inset) {
2186 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2188 if (font.isVisibleRightToLeft())
2189 x -= the_locking_inset->width(bv, font);
2195 int InsetText::cix(BufferView * bv) const
2197 // we do nothing dangerous so we use a local cache
2198 LyXText * llt = getLyXText(bv);
2199 int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2200 if (the_locking_inset) {
2201 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2203 if (font.isVisibleRightToLeft())
2204 x -= the_locking_inset->width(bv, font);
2210 int InsetText::cy(BufferView * bv) const
2213 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2217 int InsetText::ciy(BufferView * bv) const
2220 return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2224 pos_type InsetText::cpos(BufferView * bv) const
2226 return getLyXText(bv)->cursor.pos();
2230 Paragraph * InsetText::cpar(BufferView * bv) const
2232 return getLyXText(bv)->cursor.par();
2236 bool InsetText::cboundary(BufferView * bv) const
2238 return getLyXText(bv)->cursor.boundary();
2242 Row * InsetText::crow(BufferView * bv) const
2244 return getLyXText(bv)->cursor.row();
2248 LyXText * InsetText::getLyXText(BufferView const * lbv,
2249 bool const recursive) const
2251 if (cached_bview == lbv) {
2252 if (recursive && the_locking_inset)
2253 return the_locking_inset->getLyXText(lbv, true);
2254 LyXText * lt = cached_text.get();
2255 lyx::Assert(lt && lt->firstRow()->par() == par);
2258 // Super UGLY! (Lgb)
2259 BufferView * bv = const_cast<BufferView *>(lbv);
2262 Cache::iterator it = cache.find(bv);
2264 if (it != cache.end()) {
2267 } else if (do_resize) {
2268 resizeLyXText(do_resize);
2270 if (lt || !it->second.remove) {
2271 lyx::Assert(it->second.text.get());
2272 cached_text = it->second.text;
2273 if (recursive && the_locking_inset) {
2274 return the_locking_inset->getLyXText(bv, true);
2276 return cached_text.get();
2277 } else if (it->second.remove) {
2279 saveLyXTextState(it->second.text.get());
2285 // when we have to reinit the existing LyXText!
2287 it->second.text->init(bv);
2288 restoreLyXTextState(bv, it->second.text.get());
2289 it->second.remove = false;
2291 cached_text = it->second.text;
2292 if (the_locking_inset && recursive) {
2293 return the_locking_inset->getLyXText(bv);
2295 return cached_text.get();
2298 // we are here only if we don't have a BufferView * in the cache!!!
2300 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2301 cached_text->init(bv);
2302 restoreLyXTextState(bv, cached_text.get());
2304 cache.insert(make_pair(bv, cached_text));
2306 if (the_locking_inset && recursive) {
2307 return the_locking_inset->getLyXText(bv);
2309 return cached_text.get();
2313 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2317 Cache::iterator it = cache.find(bv);
2319 if (it == cache.end()) {
2323 lyx::Assert(it->second.text.get());
2325 it->second.remove = true;
2327 /// then remove all LyXText in text-insets
2328 Paragraph * p = par;
2329 for (; p; p = p->next()) {
2330 p->deleteInsetsLyXText(bv);
2336 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2339 // we cannot resize this because we are in use!
2340 // so do this on the next possible getLyXText()
2345 // lyxerr << "InsetText::resizeLyXText\n";
2346 if (!par->next() && !par->size()) { // no data, resize not neccessary!
2347 // we have to do this as a fixed width may have changed!
2348 LyXText * t = getLyXText(bv);
2349 saveLyXTextState(t);
2351 restoreLyXTextState(bv, t);
2354 // one endless line, resize normally not necessary
2355 if (!force && getMaxWidth(bv, this) < 0)
2358 Cache::iterator it = cache.find(bv);
2359 if (it == cache.end()) {
2362 lyx::Assert(it->second.text.get());
2364 LyXText * t = it->second.text.get();
2365 saveLyXTextState(t);
2366 for (Paragraph * p = par; p; p = p->next()) {
2367 p->resizeInsetsLyXText(bv);
2370 restoreLyXTextState(bv, t);
2371 if (the_locking_inset) {
2372 inset_x = cix(bv) - top_x + drawTextXOffset;
2373 inset_y = ciy(bv) + drawTextYOffset;
2377 t->first_y = bv->screen()->topCursorVisible(t);
2380 updateLocal(bv, FULL, false);
2381 // this will scroll the screen such that the cursor becomes visible
2382 bv->updateScrollbar();
2384 need_update |= FULL;
2389 void InsetText::reinitLyXText() const
2392 // we cannot resize this because we are in use!
2393 // so do this on the next possible getLyXText()
2399 // lyxerr << "InsetText::reinitLyXText\n";
2400 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2401 lyx::Assert(it->second.text.get());
2403 LyXText * t = it->second.text.get();
2404 BufferView * bv = it->first;
2406 saveLyXTextState(t);
2407 for (Paragraph * p = par; p; p = p->next()) {
2408 p->resizeInsetsLyXText(bv);
2411 restoreLyXTextState(bv, t);
2412 if (the_locking_inset) {
2413 inset_x = cix(bv) - top_x + drawTextXOffset;
2414 inset_y = ciy(bv) + drawTextYOffset;
2417 t->first_y = bv->screen()->topCursorVisible(t);
2420 updateLocal(bv, FULL, false);
2421 // this will scroll the screen such that the cursor becomes visible
2422 bv->updateScrollbar();
2430 void InsetText::removeNewlines()
2432 bool changed = false;
2434 for (Paragraph * p = par; p; p = p->next()) {
2435 for (int i = 0; i < p->size(); ++i) {
2436 if (p->getChar(i) == Paragraph::META_NEWLINE) {
2447 bool InsetText::nodraw() const
2449 if (the_locking_inset)
2450 return the_locking_inset->nodraw();
2451 return UpdatableInset::nodraw();
2455 int InsetText::scroll(bool recursive) const
2457 int sx = UpdatableInset::scroll(false);
2459 if (recursive && the_locking_inset)
2460 sx += the_locking_inset->scroll(recursive);
2466 bool InsetText::doClearArea() const
2468 return !locked || (need_update & (FULL|INIT));
2472 void InsetText::selectAll(BufferView * bv)
2474 getLyXText(bv)->cursorTop(bv);
2475 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2476 getLyXText(bv)->cursorBottom(bv);
2477 getLyXText(bv)->setSelection(bv);
2481 void InsetText::clearSelection(BufferView * bv)
2483 getLyXText(bv)->clearSelection();
2487 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2489 Painter & pain = bv->painter();
2491 int h = insetAscent + insetDescent;
2492 int ty = baseline - insetAscent;
2498 if ((ty + h) > pain.paperHeight())
2499 h = pain.paperHeight();
2500 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2501 w = pain.paperWidth();
2502 // w -= TEXT_TO_INSET_OFFSET;
2503 pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2506 frame_is_visible = false;
2510 Paragraph * InsetText::getParFromID(int id) const
2513 Paragraph * result = par;
2514 Paragraph * ires = 0;
2515 while (result && result->id() != id) {
2516 if ((ires = result->getParFromID(id)))
2518 result = result->next();
2522 Paragraph * tmp = par;
2524 if (tmp->id() == id) {
2527 Paragraph * tmp2 = tmp->getParFromID(id);
2538 Paragraph * InsetText::firstParagraph() const
2541 if (the_locking_inset)
2542 if ((result = the_locking_inset->firstParagraph()))
2548 Paragraph * InsetText::getFirstParagraph(int i) const
2550 return (i == 0) ? par : 0;
2554 LyXCursor const & InsetText::cursor(BufferView * bv) const
2556 if (the_locking_inset)
2557 return the_locking_inset->cursor(bv);
2558 return getLyXText(bv)->cursor;
2562 Paragraph * InsetText::paragraph() const
2568 void InsetText::paragraph(Paragraph * p)
2570 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2571 // caller of this function has to take care of it. This IS important
2572 // as we could have to insert a paragraph before this one and just
2573 // link the actual to a new ones next and set it with this function
2576 // set ourself as owner for all the paragraphs inserted!
2577 Paragraph * np = par;
2579 np->setInsetOwner(this);
2583 // redraw myself when asked for
2588 Inset * InsetText::getInsetFromID(int id_arg) const
2591 return const_cast<InsetText *>(this);
2593 Paragraph * lp = par;
2596 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2597 en = lp->inset_iterator_end();
2600 if ((*it)->id() == id_arg)
2602 Inset * in = (*it)->getInsetFromID(id_arg);
2612 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2618 lt = getLyXText(bv);
2621 if (the_locking_inset) {
2622 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2629 // we have to go on checking so move cusor to the next char
2630 lt->cursor.pos(lt->cursor.pos() + 1);
2632 str = lt->selectNextWordToSpellcheck(bv, value);
2634 bv->unlockInset(const_cast<InsetText *>(this));
2643 void InsetText::selectSelectedWord(BufferView * bv)
2645 if (the_locking_inset) {
2646 the_locking_inset->selectSelectedWord(bv);
2649 getLyXText(bv)->selectSelectedWord(bv);
2650 updateLocal(bv, SELECTION, false);
2654 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2656 if (the_locking_inset) {
2657 the_locking_inset->toggleSelection(bv, kill_selection);
2661 lt = getLyXText(bv);
2665 int x = top_x + TEXT_TO_INSET_OFFSET;
2667 Row * row = lt->firstRow();
2668 int y_offset = top_baseline - row->ascent_of_text();
2670 while ((row != 0) && ((y+row->height()) <= 0)) {
2677 if (need_update & SELECTION)
2679 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2685 bool InsetText::searchForward(BufferView * bv, string const & str,
2688 if (the_locking_inset) {
2689 if (the_locking_inset->searchForward(bv, str, cs, mw))
2693 lt = getLyXText(bv);
2696 Paragraph * lpar = lt->cursor.par();
2697 pos_type pos = lt->cursor.pos();
2698 if (pos < lpar->size() - 1)
2702 lpar = lpar->next();
2707 // we have to unlock ourself in this function by default!
2708 bv->unlockInset(const_cast<InsetText *>(this));
2711 lt->setCursor(bv, lpar, pos);
2715 if (LyXFind(bv, str, true, true, cs , mw)) {
2718 // we have to unlock ourself in this function by default!
2719 bv->unlockInset(const_cast<InsetText *>(this));
2723 bool InsetText::searchBackward(BufferView * bv, string const & str,
2726 if (the_locking_inset)
2727 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2729 if (LyXFind(bv, str, false, true, cs, mw)) {
2732 // we have to unlock ourself in this function by default!
2733 bv->unlockInset(const_cast<InsetText *>(this));
2738 bool InsetText::checkInsertChar(LyXFont & font)
2741 return owner()->checkInsertChar(font);
2746 void InsetText::collapseParagraphs(BufferView * bv) const
2748 BufferParams const & bparams = bv->buffer()->params;
2749 LyXText * llt = getLyXText(bv);
2751 while(par->next()) {
2752 if (par->size() && par->next()->size() &&
2753 !par->isSeparator(par->size()-1))
2755 par->insertChar(par->size(), ' ');
2757 if (llt->selection.set()) {
2758 if (llt->selection.start.par() == par->next()) {
2759 llt->selection.start.par(par);
2760 llt->selection.start.pos(
2761 llt->selection.start.pos() + par->size());
2763 if (llt->selection.end.par() == par->next()) {
2764 llt->selection.end.par(par);
2765 llt->selection.end.pos(
2766 llt->selection.end.pos() + par->size());
2769 par->pasteParagraph(bparams);
2775 void InsetText::getDrawFont(LyXFont & font) const
2779 owner()->getDrawFont(font);
2783 void InsetText::appendParagraphs(BufferParams const & bparams,
2787 Paragraph * tmpbuf = newpar;
2788 Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2790 while (tmpbuf->next()) {
2791 tmpbuf = tmpbuf->next();
2792 lastbuffer->next(new Paragraph(*tmpbuf, false));
2793 lastbuffer->next()->previous(lastbuffer);
2794 lastbuffer = lastbuffer->next();
2797 while (lastbuffer->next())
2798 lastbuffer = lastbuffer->next();
2799 if (newpar->size() && lastbuffer->size() &&
2800 !lastbuffer->isSeparator(lastbuffer->size()-1))
2802 lastbuffer->insertChar(lastbuffer->size(), ' ');
2805 // make the buf exactly the same layout than our last paragraph
2806 buf->makeSameLayout(lastbuffer);
2809 lastbuffer->next(buf);
2810 buf->previous(lastbuffer);
2811 lastbuffer->pasteParagraph(bparams);