1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998-2001 The LyX Team.
8 * ======================================================
14 #pragma implementation
17 #include "insettext.h"
18 #include "paragraph.h"
22 #include "commandtags.h"
24 #include "frontends/LyXView.h"
25 #include "BufferView.h"
26 #include "lyxtextclasslist.h"
27 #include "LaTeXFeatures.h"
28 #include "frontends/Painter.h"
30 #include "lyxcursor.h"
31 #include "CutAndPaste.h"
32 #include "frontends/font_metrics.h"
37 #include "trans_mgr.h"
38 #include "frontends/screen.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, mouse_button::state 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 // FIXME: GUII I've changed this to none: probably WRONG
702 if (!checkAndActivateInset(bv, x, tmp_y, mouse_button::none)) {
703 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
705 lt->cursor.x_fix(lt->cursor.x());
707 lt->clearSelection();
709 // If the inset is empty set the language of the current font to the
710 // language to the surronding text (if different).
711 if (par->size() == 0 && !par->next() &&
712 bv->getParentLanguage(this) != lt->current_font.language())
714 LyXFont font(LyXFont::ALL_IGNORE);
715 font.setLanguage(bv->getParentLanguage(this));
716 setFont(bv, font, false);
722 if (drawFrame_ == LOCKED)
723 code = CURSOR|DRAW_FRAME;
724 updateLocal(bv, code, false);
727 // Tell the paragraph dialog that we've entered an insettext.
728 bv->owner()->getDialogs()->updateParagraph();
732 void InsetText::edit(BufferView * bv, bool front)
734 UpdatableInset::edit(bv, front);
736 if (!bv->lockInset(this)) {
737 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
741 the_locking_inset = 0;
742 inset_pos = inset_x = inset_y = 0;
743 inset_boundary = false;
752 lt->setCursor(bv, par, 0);
757 // int const pos = (p->size() ? p->size()-1 : p->size());
758 lt->setCursor(bv, p, p->size());
760 lt->clearSelection();
762 // If the inset is empty set the language of the current font to the
763 // language to the surronding text (if different).
764 if (par->size() == 0 && !par->next() &&
765 bv->getParentLanguage(this) != lt->current_font.language()) {
766 LyXFont font(LyXFont::ALL_IGNORE);
767 font.setLanguage(bv->getParentLanguage(this));
768 setFont(bv, font, false);
773 if (drawFrame_ == LOCKED)
774 code = CURSOR|DRAW_FRAME;
775 updateLocal(bv, code, false);
780 void InsetText::insetUnlock(BufferView * bv)
782 if (the_locking_inset) {
783 the_locking_inset->insetUnlock(bv);
784 the_locking_inset = 0;
785 updateLocal(bv, CURSOR_PAR, false);
792 if (drawFrame_ == LOCKED)
793 code = CURSOR|CLEAR_FRAME;
797 if (drawFrame_ == LOCKED)
805 if (lt->selection.set()) {
806 lt->clearSelection();
808 } else if (owner()) {
809 bv->owner()->setLayout(owner()->getLyXText(bv)
810 ->cursor.par()->layout());
812 bv->owner()->setLayout(bv->text->cursor.par()->layout());
813 // hack for deleteEmptyParMech
815 lt->setCursor(bv, par, 0);
816 } else if (par->next()) {
817 lt->setCursor(bv, par->next(), 0);
822 updateLocal(bv, code, false);
825 setUpdateStatus(bv, code);
830 void InsetText::lockInset(BufferView * bv)
833 the_locking_inset = 0;
834 inset_pos = inset_x = inset_y = 0;
835 inset_boundary = false;
843 lt->setCursor(bv, par, 0);
844 lt->clearSelection();
846 // If the inset is empty set the language of the current font to the
847 // language to the surronding text (if different).
848 if (par->size() == 0 && !par->next() &&
849 bv->getParentLanguage(this) != lt->current_font.language()) {
850 LyXFont font(LyXFont::ALL_IGNORE);
851 font.setLanguage(bv->getParentLanguage(this));
852 setFont(bv, font, false);
857 if (drawFrame_ == LOCKED)
858 code = CURSOR|DRAW_FRAME;
859 setUpdateStatus(bv, code);
863 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
865 the_locking_inset = inset;
866 inset_x = cix(bv) - top_x + drawTextXOffset;
867 inset_y = ciy(bv) + drawTextYOffset;
868 inset_pos = cpos(bv);
869 inset_par = cpar(bv);
870 inset_boundary = cboundary(bv);
871 updateLocal(bv, CURSOR, false);
875 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
877 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
881 if (!the_locking_inset) {
883 int const id = inset->id();
885 Paragraph::inset_iterator it =
886 p->inset_iterator_begin();
887 Paragraph::inset_iterator const end =
888 p->inset_iterator_end();
889 for (; it != end; ++it) {
890 if ((*it) == inset) {
891 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
892 lockInset(bv, inset);
895 if ((*it)->getInsetFromID(id)) {
896 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
898 return the_locking_inset->lockInsetInInset(bv, inset);
905 if (inset == cpar(bv)->getInset(cpos(bv))) {
906 lyxerr[Debug::INSETS] << "OK" << endl;
907 lockInset(bv, inset);
909 } else if (the_locking_inset && (the_locking_inset == inset)) {
910 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
911 lyxerr[Debug::INSETS] << "OK" << endl;
912 inset_x = cix(bv) - top_x + drawTextXOffset;
913 inset_y = ciy(bv) + drawTextYOffset;
915 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
917 } else if (the_locking_inset) {
918 lyxerr[Debug::INSETS] << "MAYBE" << endl;
919 return the_locking_inset->lockInsetInInset(bv, inset);
921 lyxerr[Debug::INSETS] << "NOT OK" << endl;
926 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
929 if (!the_locking_inset)
931 if (the_locking_inset == inset) {
932 the_locking_inset->insetUnlock(bv);
933 getLyXText(bv)->updateInset(bv, inset);
934 the_locking_inset = 0;
936 moveRight(bv, false);
937 old_par = 0; // force layout setting
941 updateLocal(bv, CURSOR, false);
944 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
948 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
950 if (!autoBreakRows && par->next())
951 collapseParagraphs(bv);
959 if (inset->owner() != this) {
960 int ustat = CURSOR_PAR;
962 UpdatableInset * tl_inset = the_locking_inset;
964 found = tl_inset->updateInsetInInset(bv, inset);
966 tl_inset = static_cast<UpdatableInset *>(inset);
967 while(tl_inset->owner() && tl_inset->owner() != this)
968 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
969 if (!tl_inset->owner())
971 found = tl_inset->updateInsetInInset(bv, inset);
975 lt->updateInset(bv, tl_inset);
979 setUpdateStatus(bv, ustat);
982 bool found = lt->updateInset(bv, inset);
986 setUpdateStatus(bv, CURSOR_PAR);
987 if (the_locking_inset &&
988 cpar(bv) == inset_par && cpos(bv) == inset_pos)
990 inset_x = cix(bv) - top_x + drawTextXOffset;
991 inset_y = ciy(bv) + drawTextYOffset;
998 void InsetText::insetButtonPress(BufferView * bv,
999 int x, int y, mouse_button::state button)
1001 no_selection = true;
1003 // use this to check mouse motion for selection!
1010 int tmp_x = x - drawTextXOffset;
1011 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
1012 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1014 hideInsetCursor(bv);
1015 if (the_locking_inset) {
1016 if (the_locking_inset == inset) {
1017 the_locking_inset->insetButtonPress(bv,
1025 // otherwise unlock the_locking_inset and lock the new inset
1026 the_locking_inset->insetUnlock(bv);
1027 inset_x = cix(bv) - top_x + drawTextXOffset;
1028 inset_y = ciy(bv) + drawTextYOffset;
1029 the_locking_inset = 0;
1030 inset->insetButtonPress(bv, x - inset_x,
1031 y - inset_y, button);
1032 // inset->edit(bv, x - inset_x, y - inset_y, button);
1033 if (the_locking_inset)
1034 updateLocal(bv, CURSOR, false);
1038 // otherwise only unlock the_locking_inset
1039 the_locking_inset->insetUnlock(bv);
1040 the_locking_inset = 0;
1043 no_selection = false;
1045 if (bv->theLockingInset()) {
1046 if (isHighlyEditableInset(inset)) {
1047 // We just have to lock the inset before calling a
1048 // PressEvent on it!
1049 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
1050 if (!bv->lockInset(uinset)) {
1051 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1053 inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
1054 if (the_locking_inset)
1055 updateLocal(bv, CURSOR, false);
1059 if (!inset) { // && (button == mouse_button::button2)) {
1060 bool paste_internally = false;
1061 if ((button == mouse_button::button2) && getLyXText(bv)->selection.set()) {
1062 localDispatch(bv, LFUN_COPY, "");
1063 paste_internally = true;
1067 lt = getLyXText(bv);
1070 int old_first_y = lt->first_y;
1072 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
1074 // set the selection cursor!
1075 lt->selection.cursor = lt->cursor;
1076 lt->cursor.x_fix(lt->cursor.x());
1078 if (lt->selection.set()) {
1079 lt->clearSelection();
1082 updateLocal(bv, FULL, false);
1084 lt->clearSelection();
1087 updateLocal(bv, CURSOR, false);
1089 bv->owner()->setLayout(cpar(bv)->layout());
1090 // we moved the view we cannot do mouse selection in this case!
1091 if (getLyXText(bv)->first_y != old_first_y)
1092 no_selection = true;
1094 // Insert primary selection with middle mouse
1095 // if there is a local selection in the current buffer,
1097 if (button == mouse_button::button2) {
1098 if (paste_internally)
1099 localDispatch(bv, LFUN_PASTE, "");
1101 localDispatch(bv, LFUN_PASTESELECTION,
1105 getLyXText(bv)->clearSelection();
1107 showInsetCursor(bv);
1111 bool InsetText::insetButtonRelease(BufferView * bv,
1112 int x, int y, mouse_button::state button)
1114 no_selection = true;
1115 if (the_locking_inset) {
1116 return the_locking_inset->insetButtonRelease(bv,
1117 x - inset_x, y - inset_y,
1120 int tmp_x = x - drawTextXOffset;
1121 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
1122 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1125 if (isHighlyEditableInset(inset)) {
1126 ret = inset->insetButtonRelease(bv, x - inset_x,
1127 y - inset_y, button);
1129 inset_x = cix(bv) - top_x + drawTextXOffset;
1130 inset_y = ciy(bv) + drawTextYOffset;
1131 ret = inset->insetButtonRelease(bv, x - inset_x,
1132 y - inset_y, button);
1133 inset->edit(bv, x - inset_x,
1134 y - inset_y, button);
1136 updateLocal(bv, CURSOR_PAR, false);
1142 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, mouse_button::state state)
1144 if (the_locking_inset) {
1145 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1150 if (no_selection || ((mouse_x == x) && (mouse_y == y)))
1155 lt = getLyXText(bv);
1158 hideInsetCursor(bv);
1159 LyXCursor cur = lt->cursor;
1160 lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1161 lt->cursor.x_fix(lt->cursor.x());
1162 if (cur == lt->cursor) {
1167 lt->setSelection(bv);
1168 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1169 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1173 updateLocal(bv, SELECTION, false);
1175 showInsetCursor(bv);
1179 UpdatableInset::RESULT
1180 InsetText::localDispatch(BufferView * bv,
1181 kb_action action, string const & arg)
1183 bool was_empty = par->size() == 0 && !par->next();
1184 no_selection = false;
1185 UpdatableInset::RESULT
1186 result= UpdatableInset::localDispatch(bv, action, arg);
1187 if (result != UNDISPATCHED) {
1191 result = DISPATCHED;
1192 if ((action < 0) && arg.empty())
1195 if (the_locking_inset) {
1196 result = the_locking_inset->localDispatch(bv, action, arg);
1197 if (result == DISPATCHED_NOUPDATE)
1199 else if (result == DISPATCHED) {
1200 updateLocal(bv, CURSOR_PAR, false);
1202 } else if (result >= FINISHED) {
1204 case FINISHED_RIGHT:
1205 moveRightIntern(bv, false, false);
1206 result = DISPATCHED;
1209 if ((result = moveUp(bv)) >= FINISHED) {
1210 updateLocal(bv, CURSOR, false);
1211 bv->unlockInset(this);
1216 LyXText *lt = getLyXText(bv);
1217 if (lt->cursor.irow()->next()) {
1218 lt->setCursorFromCoordinates(
1219 bv, lt->cursor.ix() + inset_x,
1221 lt->cursor.irow()->baseline() +
1222 lt->cursor.irow()->height() + 1);
1223 lt->cursor.x_fix(lt->cursor.x());
1224 updateLocal(bv, CURSOR, false);
1226 bv->unlockInset(this);
1231 result = DISPATCHED;
1234 the_locking_inset = 0;
1235 updateLocal(bv, CURSOR, false);
1239 hideInsetCursor(bv);
1242 lt = getLyXText(bv);
1246 int updflag = false;
1249 case LFUN_SELFINSERT:
1250 if (bv->buffer()->isReadonly()) {
1251 // setErrorMessage(N_("Document is read only"));
1255 /* Automatically delete the currently selected
1256 * text and replace it with what is being
1257 * typed in now. Depends on lyxrc settings
1258 * "auto_region_delete", which defaults to
1261 setUndo(bv, Undo::INSERT,
1262 lt->cursor.par(), lt->cursor.par()->next());
1264 if (lyxrc.auto_region_delete) {
1265 if (lt->selection.set()) {
1266 lt->cutSelection(bv, false);
1269 lt->clearSelection();
1270 for (string::size_type i = 0; i < arg.length(); ++i) {
1271 bv->owner()->getIntl()->getTransManager().
1272 TranslateAndInsert(arg[i], lt);
1275 lt->selection.cursor = lt->cursor;
1276 updwhat = CURSOR | CURSOR_PAR;
1278 result = DISPATCHED_NOUPDATE;
1280 // --- Cursor Movements -----------------------------------
1283 moveRight(bv, false, true);
1284 lt->setSelection(bv);
1285 updwhat = SELECTION;
1288 result = moveRight(bv);
1294 moveLeft(bv, false, true);
1295 lt->setSelection(bv);
1296 updwhat = SELECTION;
1300 result = moveLeft(bv);
1306 lt->setSelection(bv);
1307 updwhat = SELECTION;
1311 result = moveDown(bv);
1317 lt->setSelection(bv);
1318 updwhat = SELECTION;
1322 result = moveUp(bv);
1334 case LFUN_BACKSPACE: {
1335 setUndo(bv, Undo::DELETE,
1336 lt->cursor.par(), lt->cursor.par()->next());
1337 if (lt->selection.set())
1338 lt->cutSelection(bv);
1341 updwhat = CURSOR_PAR;
1347 setUndo(bv, Undo::DELETE,
1348 lt->cursor.par(), lt->cursor.par()->next());
1349 if (lt->selection.set()) {
1350 lt->cutSelection(bv);
1354 updwhat = CURSOR_PAR;
1360 setUndo(bv, Undo::DELETE,
1361 lt->cursor.par(), lt->cursor.par()->next());
1362 lt->cutSelection(bv);
1363 updwhat = CURSOR_PAR;
1370 lt->copySelection(bv);
1371 updwhat = CURSOR_PAR;
1373 case LFUN_PASTESELECTION:
1375 string const clip(bv->getClipboard());
1379 if (arg == "paragraph") {
1380 lt->insertStringAsParagraphs(bv, clip);
1382 lt->insertStringAsLines(bv, clip);
1385 lt->clearSelection();
1387 updwhat = CURSOR_PAR;
1392 if (!autoBreakRows) {
1394 if (CutAndPaste::nrOfParagraphs() > 1) {
1395 Alert::alert(_("Impossible operation"),
1396 _("Cannot include more than one paragraph!"),
1401 setUndo(bv, Undo::INSERT,
1402 lt->cursor.par(), lt->cursor.par()->next());
1403 lt->pasteSelection(bv);
1405 lt->clearSelection();
1406 updwhat = CURSOR_PAR;
1411 case LFUN_BREAKPARAGRAPH:
1412 if (!autoBreakRows) {
1413 result = DISPATCHED;
1416 lt->breakParagraph(bv, 0);
1417 updwhat = CURSOR | FULL;
1420 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1421 if (!autoBreakRows) {
1422 result = DISPATCHED;
1425 lt->breakParagraph(bv, 1);
1426 updwhat = CURSOR | FULL;
1430 case LFUN_BREAKLINE: {
1431 if (!autoBreakRows) {
1432 result = DISPATCHED;
1435 setUndo(bv, Undo::INSERT,
1436 lt->cursor.par(), lt->cursor.par()->next());
1437 lt->insertChar(bv, Paragraph::META_NEWLINE);
1438 updwhat = CURSOR | CURSOR_PAR;
1444 // do not set layouts on non breakable textinsets
1445 if (autoBreakRows) {
1446 string cur_layout = cpar(bv)->layout();
1448 // Derive layout number from given argument (string)
1449 // and current buffer's textclass (number). */
1450 textclass_type tclass = bv->buffer()->params.textclass;
1451 string layout = arg;
1452 bool hasLayout = textclasslist[tclass].hasLayout(layout);
1454 // If the entry is obsolete, use the new one instead.
1456 string const & obs =
1457 textclasslist[tclass][layout].
1463 // see if we found the layout number:
1465 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1466 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1470 if (cur_layout != layout) {
1471 cur_layout = layout;
1472 lt->setLayout(bv, layout);
1473 bv->owner()->setLayout(cpar(bv)->layout());
1474 updwhat = CURSOR_PAR;
1478 // reset the layout box
1479 bv->owner()->setLayout(cpar(bv)->layout());
1482 case LFUN_PARAGRAPH_SPACING:
1483 // This one is absolutely not working. When fiddling with this
1484 // it also seems to me that the paragraphs inside the insettext
1485 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1487 Paragraph * par = lt->cursor.par();
1488 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1489 float cur_value = 1.0;
1490 if (cur_spacing == Spacing::Other) {
1491 cur_value = par->params().spacing().getValue();
1494 istringstream istr(arg.c_str());
1497 Spacing::Space new_spacing = cur_spacing;
1498 float new_value = cur_value;
1500 lyxerr << "Missing argument to `paragraph-spacing'"
1502 } else if (tmp == "single") {
1503 new_spacing = Spacing::Single;
1504 } else if (tmp == "onehalf") {
1505 new_spacing = Spacing::Onehalf;
1506 } else if (tmp == "double") {
1507 new_spacing = Spacing::Double;
1508 } else if (tmp == "other") {
1509 new_spacing = Spacing::Other;
1512 lyxerr << "new_value = " << tmpval << endl;
1515 } else if (tmp == "default") {
1516 new_spacing = Spacing::Default;
1518 lyxerr << _("Unknown spacing argument: ")
1521 if (cur_spacing != new_spacing || cur_value != new_value) {
1522 par->params().spacing(Spacing(new_spacing, new_value));
1523 updwhat = CURSOR_PAR;
1530 if (!bv->Dispatch(action, arg))
1531 result = UNDISPATCHED;
1538 updateLocal(bv, updwhat, updflag);
1539 /// If the action has deleted all text in the inset, we need to change the
1540 // language to the language of the surronding text.
1541 if (!was_empty && par->size() == 0 && !par->next()) {
1542 LyXFont font(LyXFont::ALL_IGNORE);
1543 font.setLanguage(bv->getParentLanguage(this));
1544 setFont(bv, font, false);
1547 if (result >= FINISHED)
1548 bv->unlockInset(this);
1550 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1551 result = DISPATCHED;
1556 int InsetText::latex(Buffer const * buf, ostream & os, bool moving_arg, bool) const
1559 buf->latexParagraphs(os, par, 0, texrow, moving_arg);
1560 return texrow.rows();
1564 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1566 Paragraph * p = par;
1567 unsigned int lines = 0;
1570 string const tmp = buf->asciiParagraph(p, linelen, p->previous()==0);
1571 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1579 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1581 Paragraph * p = par;
1582 unsigned int lines = 0;
1584 vector<string> environment_stack(10);
1585 vector<string> environment_inner(10);
1587 int const command_depth = 0;
1590 Paragraph::depth_type depth = 0; // paragraph depth
1594 int desc_on = 0; // description mode
1596 LyXLayout const & style =
1597 textclasslist[buf->params.textclass][p->layout()];
1599 // environment tag closing
1600 for (; depth > p->params().depth(); --depth) {
1601 if (environment_inner[depth] != "!-- --") {
1602 item_name = "listitem";
1603 lines += buf->sgmlCloseTag(os, command_depth + depth, mixcont, item_name);
1604 if (environment_inner[depth] == "varlistentry")
1605 lines += buf->sgmlCloseTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1607 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1608 environment_stack[depth].erase();
1609 environment_inner[depth].erase();
1612 if (depth == p->params().depth()
1613 && environment_stack[depth] != style.latexname()
1614 && !environment_stack[depth].empty()) {
1615 if (environment_inner[depth] != "!-- --") {
1616 item_name= "listitem";
1617 lines += buf->sgmlCloseTag(os, command_depth+depth, mixcont, item_name);
1618 if (environment_inner[depth] == "varlistentry")
1619 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1622 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, 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 lines += buf->sgmlOpenTag(os, depth + command_depth, mixcont, style.latexname());
1635 buf->sgmlError(p, 0, _("Error : LatexType Command not allowed here.\n"));
1639 case LATEX_ENVIRONMENT:
1640 case LATEX_ITEM_ENVIRONMENT:
1641 if (depth < p->params().depth()) {
1642 depth = p->params().depth();
1643 environment_stack[depth].erase();
1646 if (environment_stack[depth] != style.latexname()) {
1647 if (environment_stack.size() == depth + 1) {
1648 environment_stack.push_back("!-- --");
1649 environment_inner.push_back("!-- --");
1651 environment_stack[depth] = style.latexname();
1652 environment_inner[depth] = "!-- --";
1653 lines += buf->sgmlOpenTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1655 if (environment_inner[depth] != "!-- --") {
1656 item_name= "listitem";
1657 lines += buf->sgmlCloseTag(os, command_depth + depth, mixcont, item_name);
1658 if (environment_inner[depth] == "varlistentry")
1659 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1663 if (style.latextype == LATEX_ENVIRONMENT) {
1664 if (!style.latexparam().empty()) {
1665 if (style.latexparam() == "CDATA")
1668 lines += buf->sgmlOpenTag(os, depth + command_depth, mixcont, style.latexparam());
1673 desc_on = (style.labeltype == LABEL_MANUAL);
1675 environment_inner[depth] = desc_on?"varlistentry":"listitem";
1676 lines += buf->sgmlOpenTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1678 item_name = desc_on?"term":"para";
1679 lines += buf->sgmlOpenTag(os, depth + 1 + command_depth, mixcont, item_name);
1683 lines += buf->sgmlOpenTag(os, depth + command_depth, mixcont, style.latexname());
1687 buf->simpleDocBookOnePar(os, p, desc_on, depth + 1 + command_depth);
1691 // write closing SGML tags
1692 switch (style.latextype) {
1693 case LATEX_ENVIRONMENT:
1694 if (!style.latexparam().empty()) {
1695 if (style.latexparam() == "CDATA")
1698 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, style.latexparam());
1701 case LATEX_ITEM_ENVIRONMENT:
1702 if (desc_on == 1) break;
1704 lines += buf->sgmlCloseTag(os, depth + 1 + command_depth, mixcont, end_tag);
1706 case LATEX_PARAGRAPH:
1707 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, style.latexname());
1710 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, style.latexname());
1716 for (int d = depth; d >= 0; --d) {
1717 if (!environment_stack[depth].empty()) {
1718 if (environment_inner[depth] != "!-- --") {
1719 item_name = "listitem";
1720 lines += buf->sgmlCloseTag(os, command_depth + depth, mixcont, item_name);
1721 if (environment_inner[depth] == "varlistentry")
1722 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1725 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1733 void InsetText::validate(LaTeXFeatures & features) const
1735 Paragraph * p = par;
1737 p->validate(features);
1743 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1745 if (textclasslist[buf->params.textclass][p->layout()].labeltype != LABEL_MANUAL)
1748 return p->beginningOfMainBody();
1752 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1754 if (the_locking_inset) {
1755 the_locking_inset->getCursorPos(bv, x, y);
1758 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1759 y = cy(bv) - TEXT_TO_INSET_OFFSET;
1763 int InsetText::insetInInsetY() const
1765 if (!the_locking_inset)
1768 return (inset_y + the_locking_inset->insetInInsetY());
1772 void InsetText::toggleInsetCursor(BufferView * bv)
1774 if (the_locking_inset) {
1775 the_locking_inset->toggleInsetCursor(bv);
1779 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1781 int const asc = font_metrics::maxAscent(font);
1782 int const desc = font_metrics::maxDescent(font);
1784 if (isCursorVisible())
1785 bv->hideLockedInsetCursor();
1787 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1788 toggleCursorVisible();
1792 void InsetText::showInsetCursor(BufferView * bv, bool show)
1794 if (the_locking_inset) {
1795 the_locking_inset->showInsetCursor(bv, show);
1798 if (!isCursorVisible()) {
1799 LyXFont const font =
1800 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1802 int const asc = font_metrics::maxAscent(font);
1803 int const desc = font_metrics::maxDescent(font);
1805 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1807 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1808 setCursorVisible(true);
1813 void InsetText::hideInsetCursor(BufferView * bv)
1815 if (isCursorVisible()) {
1816 bv->hideLockedInsetCursor();
1817 setCursorVisible(false);
1819 if (the_locking_inset)
1820 the_locking_inset->hideInsetCursor(bv);
1824 void InsetText::fitInsetCursor(BufferView * bv) const
1826 if (the_locking_inset) {
1827 the_locking_inset->fitInsetCursor(bv);
1830 LyXFont const font =
1831 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1833 int const asc = font_metrics::maxAscent(font);
1834 int const desc = font_metrics::maxDescent(font);
1836 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1837 need_update |= FULL;
1841 UpdatableInset::RESULT
1842 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1844 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1845 return moveLeftIntern(bv, true, activate_inset, selecting);
1847 return moveRightIntern(bv, true, activate_inset, selecting);
1851 UpdatableInset::RESULT
1852 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1854 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1855 return moveRightIntern(bv, false, activate_inset, selecting);
1857 return moveLeftIntern(bv, false, activate_inset, selecting);
1861 UpdatableInset::RESULT
1862 InsetText::moveRightIntern(BufferView * bv, bool front,
1863 bool activate_inset, bool selecting)
1865 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1866 return FINISHED_RIGHT;
1867 if (activate_inset && checkAndActivateInset(bv, front))
1869 getLyXText(bv)->cursorRight(bv);
1871 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1872 return DISPATCHED_NOUPDATE;
1876 UpdatableInset::RESULT
1877 InsetText::moveLeftIntern(BufferView * bv, bool front,
1878 bool activate_inset, bool selecting)
1880 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1882 getLyXText(bv)->cursorLeft(bv);
1884 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1885 if (activate_inset && checkAndActivateInset(bv, front))
1887 return DISPATCHED_NOUPDATE;
1891 UpdatableInset::RESULT
1892 InsetText::moveUp(BufferView * bv)
1894 if (!crow(bv)->previous())
1896 getLyXText(bv)->cursorUp(bv);
1897 return DISPATCHED_NOUPDATE;
1901 UpdatableInset::RESULT
1902 InsetText::moveDown(BufferView * bv)
1904 if (!crow(bv)->next())
1905 return FINISHED_DOWN;
1906 getLyXText(bv)->cursorDown(bv);
1907 return DISPATCHED_NOUPDATE;
1911 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1913 if (the_locking_inset) {
1914 if (the_locking_inset->insetAllowed(inset))
1915 return the_locking_inset->insertInset(bv, inset);
1918 inset->setOwner(this);
1919 hideInsetCursor(bv);
1920 getLyXText(bv)->insertInset(bv, inset);
1922 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1927 bool InsetText::insetAllowed(Inset::Code code) const
1929 // in_insetAllowed is a really gross hack,
1930 // to allow us to call the owner's insetAllowed
1931 // without stack overflow, which can happen
1932 // when the owner uses InsetCollapsable::insetAllowed()
1934 if (in_insetAllowed)
1936 in_insetAllowed = true;
1937 if (the_locking_inset)
1938 ret = the_locking_inset->insetAllowed(code);
1940 ret = owner()->insetAllowed(code);
1941 in_insetAllowed = false;
1946 UpdatableInset * InsetText::getLockingInset() const
1948 return the_locking_inset ? the_locking_inset->getLockingInset() :
1949 const_cast<InsetText *>(this);
1953 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1957 if (the_locking_inset)
1958 return the_locking_inset->getFirstLockingInsetOfType(c);
1963 bool InsetText::showInsetDialog(BufferView * bv) const
1965 if (the_locking_inset)
1966 return the_locking_inset->showInsetDialog(bv);
1971 vector<string> const InsetText::getLabelList() const
1973 vector<string> label_list;
1975 Paragraph * tpar = par;
1977 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1978 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1979 for (; beg != end; ++beg) {
1980 vector<string> const l = (*beg)->getLabelList();
1981 label_list.insert(label_list.end(), l.begin(), l.end());
1983 tpar = tpar->next();
1989 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1992 if (the_locking_inset) {
1993 the_locking_inset->setFont(bv, font, toggleall, selectall);
1996 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
1997 getLyXText(bv)->setFont(bv, font, toggleall);
2002 lt = getLyXText(bv);
2005 if (lt->selection.set()) {
2006 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
2010 lt->toggleFree(bv, font, toggleall);
2012 lt->clearSelection();
2014 bool flag = (selectall || lt->selection.set());
2018 updateLocal(bv, FULL, true);
2020 updateLocal(bv, CURSOR_PAR, true);
2024 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
2026 if (cpar(bv)->isInset(cpos(bv))) {
2028 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
2029 if (!isHighlyEditableInset(inset))
2031 inset->edit(bv, front);
2032 if (!the_locking_inset)
2034 updateLocal(bv, CURSOR, false);
2041 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
2042 mouse_button::state button)
2044 x -= drawTextXOffset;
2046 int dummyy = y + insetAscent;
2047 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
2048 // we only do the edit() call if the inset was hit by the mouse
2049 // or if it is a highly editable inset. So we should call this
2050 // function from our own edit with button < 0.
2051 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
2053 if (button == mouse_button::none && !isHighlyEditableInset(inset))
2061 inset_x = cix(bv) - top_x + drawTextXOffset;
2062 inset_y = ciy(bv) + drawTextYOffset;
2063 inset->edit(bv, x - inset_x, y - inset_y, button);
2064 if (!the_locking_inset)
2066 updateLocal(bv, CURSOR, false);
2073 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
2076 int w = UpdatableInset::getMaxWidth(bv, inset);
2081 w = w - top_x + owner()->x();
2084 w -= (2 * TEXT_TO_INSET_OFFSET);
2087 return UpdatableInset::getMaxWidth(bv, inset);
2092 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2094 // we have to unlock any locked inset otherwise we're in troubles
2095 the_locking_inset = 0;
2097 Paragraph * tmp = par->next();
2102 par = new Paragraph(*p, same_id);
2103 par->setInsetOwner(this);
2104 Paragraph * np = par;
2107 np->next(new Paragraph(*p, same_id));
2108 np->next()->previous(np);
2110 np->setInsetOwner(this);
2117 void InsetText::setText(string const & data, LyXFont const & font)
2120 for (unsigned int i=0; i < data.length(); ++i)
2121 par->insertChar(i, data[i], font);
2126 void InsetText::setAutoBreakRows(bool flag)
2128 if (flag != autoBreakRows) {
2129 autoBreakRows = flag;
2137 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2139 if (how != drawFrame_) {
2142 updateLocal(bv, DRAW_FRAME, false);
2147 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2149 if (frame_color != col) {
2152 updateLocal(bv, DRAW_FRAME, false);
2157 int InsetText::cx(BufferView * bv) const
2159 // we do nothing dangerous so we use a local cache
2160 LyXText * llt = getLyXText(bv);
2161 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2162 if (the_locking_inset) {
2163 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2165 if (font.isVisibleRightToLeft())
2166 x -= the_locking_inset->width(bv, font);
2172 int InsetText::cix(BufferView * bv) const
2174 // we do nothing dangerous so we use a local cache
2175 LyXText * llt = getLyXText(bv);
2176 int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2177 if (the_locking_inset) {
2178 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2180 if (font.isVisibleRightToLeft())
2181 x -= the_locking_inset->width(bv, font);
2187 int InsetText::cy(BufferView * bv) const
2190 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2194 int InsetText::ciy(BufferView * bv) const
2197 return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2201 pos_type InsetText::cpos(BufferView * bv) const
2203 return getLyXText(bv)->cursor.pos();
2207 Paragraph * InsetText::cpar(BufferView * bv) const
2209 return getLyXText(bv)->cursor.par();
2213 bool InsetText::cboundary(BufferView * bv) const
2215 return getLyXText(bv)->cursor.boundary();
2219 Row * InsetText::crow(BufferView * bv) const
2221 return getLyXText(bv)->cursor.row();
2225 LyXText * InsetText::getLyXText(BufferView const * lbv,
2226 bool const recursive) const
2228 if (cached_bview == lbv) {
2229 if (recursive && the_locking_inset)
2230 return the_locking_inset->getLyXText(lbv, true);
2231 LyXText * lt = cached_text.get();
2232 lyx::Assert(lt && lt->firstRow()->par() == par);
2235 // Super UGLY! (Lgb)
2236 BufferView * bv = const_cast<BufferView *>(lbv);
2239 Cache::iterator it = cache.find(bv);
2241 if (it != cache.end()) {
2244 } else if (do_resize) {
2245 resizeLyXText(do_resize);
2247 if (lt || !it->second.remove) {
2248 lyx::Assert(it->second.text.get());
2249 cached_text = it->second.text;
2250 if (recursive && the_locking_inset) {
2251 return the_locking_inset->getLyXText(bv, true);
2253 return cached_text.get();
2254 } else if (it->second.remove) {
2256 saveLyXTextState(it->second.text.get());
2262 // when we have to reinit the existing LyXText!
2264 it->second.text->init(bv);
2265 restoreLyXTextState(bv, it->second.text.get());
2266 it->second.remove = false;
2268 cached_text = it->second.text;
2269 if (the_locking_inset && recursive) {
2270 return the_locking_inset->getLyXText(bv);
2272 return cached_text.get();
2275 // we are here only if we don't have a BufferView * in the cache!!!
2277 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2278 cached_text->init(bv);
2279 restoreLyXTextState(bv, cached_text.get());
2281 cache.insert(make_pair(bv, cached_text));
2283 if (the_locking_inset && recursive) {
2284 return the_locking_inset->getLyXText(bv);
2286 return cached_text.get();
2290 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2294 Cache::iterator it = cache.find(bv);
2296 if (it == cache.end()) {
2300 lyx::Assert(it->second.text.get());
2302 it->second.remove = true;
2304 /// then remove all LyXText in text-insets
2305 Paragraph * p = par;
2306 for (; p; p = p->next()) {
2307 p->deleteInsetsLyXText(bv);
2313 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2316 // we cannot resize this because we are in use!
2317 // so do this on the next possible getLyXText()
2322 // lyxerr << "InsetText::resizeLyXText\n";
2323 if (!par->next() && !par->size()) { // no data, resize not neccessary!
2324 // we have to do this as a fixed width may have changed!
2325 LyXText * t = getLyXText(bv);
2326 saveLyXTextState(t);
2328 restoreLyXTextState(bv, t);
2331 // one endless line, resize normally not necessary
2332 if (!force && getMaxWidth(bv, this) < 0)
2335 Cache::iterator it = cache.find(bv);
2336 if (it == cache.end()) {
2339 lyx::Assert(it->second.text.get());
2341 LyXText * t = it->second.text.get();
2342 saveLyXTextState(t);
2343 for (Paragraph * p = par; p; p = p->next()) {
2344 p->resizeInsetsLyXText(bv);
2347 restoreLyXTextState(bv, t);
2348 if (the_locking_inset) {
2349 inset_x = cix(bv) - top_x + drawTextXOffset;
2350 inset_y = ciy(bv) + drawTextYOffset;
2353 t->first_y = bv->screen().topCursorVisible(t->cursor, t->first_y);
2355 updateLocal(bv, FULL, false);
2356 // this will scroll the screen such that the cursor becomes visible
2357 bv->updateScrollbar();
2359 need_update |= FULL;
2364 void InsetText::reinitLyXText() const
2367 // we cannot resize this because we are in use!
2368 // so do this on the next possible getLyXText()
2374 // lyxerr << "InsetText::reinitLyXText\n";
2375 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2376 lyx::Assert(it->second.text.get());
2378 LyXText * t = it->second.text.get();
2379 BufferView * bv = it->first;
2381 saveLyXTextState(t);
2382 for (Paragraph * p = par; p; p = p->next()) {
2383 p->resizeInsetsLyXText(bv);
2386 restoreLyXTextState(bv, t);
2387 if (the_locking_inset) {
2388 inset_x = cix(bv) - top_x + drawTextXOffset;
2389 inset_y = ciy(bv) + drawTextYOffset;
2391 t->first_y = bv->screen().topCursorVisible(t->cursor, t->first_y);
2393 updateLocal(bv, FULL, false);
2394 // this will scroll the screen such that the cursor becomes visible
2395 bv->updateScrollbar();
2403 void InsetText::removeNewlines()
2405 bool changed = false;
2407 for (Paragraph * p = par; p; p = p->next()) {
2408 for (int i = 0; i < p->size(); ++i) {
2409 if (p->getChar(i) == Paragraph::META_NEWLINE) {
2420 bool InsetText::nodraw() const
2422 if (the_locking_inset)
2423 return the_locking_inset->nodraw();
2424 return UpdatableInset::nodraw();
2428 int InsetText::scroll(bool recursive) const
2430 int sx = UpdatableInset::scroll(false);
2432 if (recursive && the_locking_inset)
2433 sx += the_locking_inset->scroll(recursive);
2439 bool InsetText::doClearArea() const
2441 return !locked || (need_update & (FULL|INIT));
2445 void InsetText::selectAll(BufferView * bv)
2447 getLyXText(bv)->cursorTop(bv);
2448 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2449 getLyXText(bv)->cursorBottom(bv);
2450 getLyXText(bv)->setSelection(bv);
2454 void InsetText::clearSelection(BufferView * bv)
2456 getLyXText(bv)->clearSelection();
2460 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2462 Painter & pain = bv->painter();
2464 int h = insetAscent + insetDescent;
2465 int ty = baseline - insetAscent;
2471 if ((ty + h) > pain.paperHeight())
2472 h = pain.paperHeight();
2473 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2474 w = pain.paperWidth();
2475 // w -= TEXT_TO_INSET_OFFSET;
2476 pain.fillRectangle(top_x + 1, ty + 1, w - 1, h - 1, backgroundColor());
2479 frame_is_visible = false;
2483 Paragraph * InsetText::getParFromID(int id) const
2486 Paragraph * result = par;
2487 Paragraph * ires = 0;
2488 while (result && result->id() != id) {
2489 if ((ires = result->getParFromID(id)))
2491 result = result->next();
2495 Paragraph * tmp = par;
2497 if (tmp->id() == id) {
2500 Paragraph * tmp2 = tmp->getParFromID(id);
2511 Paragraph * InsetText::firstParagraph() const
2514 if (the_locking_inset)
2515 if ((result = the_locking_inset->firstParagraph()))
2521 Paragraph * InsetText::getFirstParagraph(int i) const
2523 return (i == 0) ? par : 0;
2527 LyXCursor const & InsetText::cursor(BufferView * bv) const
2529 if (the_locking_inset)
2530 return the_locking_inset->cursor(bv);
2531 return getLyXText(bv)->cursor;
2535 Paragraph * InsetText::paragraph() const
2541 void InsetText::paragraph(Paragraph * p)
2543 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2544 // caller of this function has to take care of it. This IS important
2545 // as we could have to insert a paragraph before this one and just
2546 // link the actual to a new ones next and set it with this function
2549 // set ourself as owner for all the paragraphs inserted!
2550 Paragraph * np = par;
2552 np->setInsetOwner(this);
2556 // redraw myself when asked for
2561 Inset * InsetText::getInsetFromID(int id_arg) const
2564 return const_cast<InsetText *>(this);
2566 Paragraph * lp = par;
2569 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2570 en = lp->inset_iterator_end();
2573 if ((*it)->id() == id_arg)
2575 Inset * in = (*it)->getInsetFromID(id_arg);
2585 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2591 lt = getLyXText(bv);
2594 if (the_locking_inset) {
2595 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2602 // we have to go on checking so move cusor to the next char
2603 lt->cursor.pos(lt->cursor.pos() + 1);
2605 str = lt->selectNextWordToSpellcheck(bv, value);
2607 bv->unlockInset(const_cast<InsetText *>(this));
2616 void InsetText::selectSelectedWord(BufferView * bv)
2618 if (the_locking_inset) {
2619 the_locking_inset->selectSelectedWord(bv);
2622 getLyXText(bv)->selectSelectedWord(bv);
2623 updateLocal(bv, SELECTION, false);
2627 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2629 if (the_locking_inset) {
2630 the_locking_inset->toggleSelection(bv, kill_selection);
2634 lt = getLyXText(bv);
2638 int x = top_x + TEXT_TO_INSET_OFFSET;
2640 Row * row = lt->firstRow();
2641 int y_offset = top_baseline - row->ascent_of_text();
2643 while ((row != 0) && ((y+row->height()) <= 0)) {
2650 if (need_update & SELECTION)
2652 bv->screen().toggleSelection(lt, bv, kill_selection, y_offset, x);
2658 bool InsetText::searchForward(BufferView * bv, string const & str,
2663 lt = getLyXText(bv);
2666 if (the_locking_inset) {
2667 if (the_locking_inset->searchForward(bv, str, cs, mw))
2669 lt->cursorRight(bv, true);
2671 lyxfind::SearchResult result =
2672 lyxfind::LyXFind(bv, lt, str, true, cs, mw);
2674 if (result == lyxfind::SR_FOUND) {
2675 LyXCursor cur = lt->cursor;
2676 bv->unlockInset(bv->theLockingInset());
2677 if (bv->lockInset(this))
2680 lt->setSelectionOverString(bv, str);
2681 updateLocal(bv, SELECTION, false);
2685 return (result != lyxfind::SR_NOT_FOUND);
2688 bool InsetText::searchBackward(BufferView * bv, string const & str,
2691 if (the_locking_inset) {
2692 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2697 lt = getLyXText(bv);
2701 Paragraph * p = par;
2704 lt->setCursor(bv, p, p->size());
2706 lyxfind::SearchResult result =
2707 lyxfind::LyXFind(bv, lt, str, false, cs, mw);
2709 if (result == lyxfind::SR_FOUND) {
2710 LyXCursor cur = lt->cursor;
2711 bv->unlockInset(bv->theLockingInset());
2712 if (bv->lockInset(this))
2715 lt->setSelectionOverString(bv, str);
2716 updateLocal(bv, SELECTION, false);
2720 return (result != lyxfind::SR_NOT_FOUND);
2724 bool InsetText::checkInsertChar(LyXFont & font)
2727 return owner()->checkInsertChar(font);
2732 void InsetText::collapseParagraphs(BufferView * bv) const
2734 BufferParams const & bparams = bv->buffer()->params;
2735 LyXText * llt = getLyXText(bv);
2737 while(par->next()) {
2738 if (par->size() && par->next()->size() &&
2739 !par->isSeparator(par->size()-1))
2741 par->insertChar(par->size(), ' ');
2743 if (llt->selection.set()) {
2744 if (llt->selection.start.par() == par->next()) {
2745 llt->selection.start.par(par);
2746 llt->selection.start.pos(
2747 llt->selection.start.pos() + par->size());
2749 if (llt->selection.end.par() == par->next()) {
2750 llt->selection.end.par(par);
2751 llt->selection.end.pos(
2752 llt->selection.end.pos() + par->size());
2755 par->pasteParagraph(bparams);
2761 void InsetText::getDrawFont(LyXFont & font) const
2765 owner()->getDrawFont(font);
2769 void InsetText::appendParagraphs(BufferParams const & bparams,
2773 Paragraph * tmpbuf = newpar;
2774 Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2776 while (tmpbuf->next()) {
2777 tmpbuf = tmpbuf->next();
2778 lastbuffer->next(new Paragraph(*tmpbuf, false));
2779 lastbuffer->next()->previous(lastbuffer);
2780 lastbuffer = lastbuffer->next();
2783 while (lastbuffer->next())
2784 lastbuffer = lastbuffer->next();
2785 if (newpar->size() && lastbuffer->size() &&
2786 !lastbuffer->isSeparator(lastbuffer->size()-1))
2788 lastbuffer->insertChar(lastbuffer->size(), ' ');
2791 // make the buf exactly the same layout than our last paragraph
2792 buf->makeSameLayout(lastbuffer);
2795 lastbuffer->next(buf);
2796 buf->previous(lastbuffer);
2797 lastbuffer->pasteParagraph(bparams);