1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998-2001 The LyX Team.
8 * ======================================================
20 #pragma implementation
23 #include "insettext.h"
24 #include "paragraph.h"
28 #include "commandtags.h"
31 #include "BufferView.h"
33 #include "LaTeXFeatures.h"
35 #include "lyx_gui_misc.h"
37 #include "lyxcursor.h"
38 #include "CutAndPaste.h"
41 #include "support/textutils.h"
42 #include "support/LAssert.h"
43 #include "support/lstrings.h"
47 #include "trans_mgr.h"
48 #include "lyxscreen.h"
52 #include "ParagraphParameters.h"
53 #include "undo_funcs.h"
64 extern unsigned char getCurrentTextClass(Buffer *);
65 extern bool math_insert_greek(BufferView *, char);
66 extern int greek_kb_flag;
69 // These functions should probably go into bufferview_funcs somehow (Jug)
71 void InsetText::saveLyXTextState(LyXText * t) const
73 // check if my paragraphs are still valid
76 if (p == t->cursor.par())
81 if (p && t->cursor.pos() <= p->size()) {
82 sstate.lpar = t->cursor.par();
83 sstate.pos = t->cursor.pos();
84 sstate.boundary = t->cursor.boundary();
85 sstate.selstartpar = t->selection.start.par();
86 sstate.selstartpos = t->selection.start.pos();
87 sstate.selstartboundary = t->selection.start.boundary();
88 sstate.selendpar = t->selection.end.par();
89 sstate.selendpos = t->selection.end.pos();
90 sstate.selendboundary = t->selection.end.boundary();
91 sstate.selection = t->selection.set();
92 sstate.mark_set = t->selection.mark();
93 sstate.refresh = t->refresh_row != 0;
99 void InsetText::restoreLyXTextState(BufferView * bv, LyXText * t) const
102 t->selection.set(true);
103 /* at this point just to avoid the Delete-Empty-Paragraph
104 * Mechanism when setting the cursor */
105 t->selection.mark(sstate.mark_set);
106 if (sstate.selection) {
107 t->setCursor(bv, sstate.selstartpar, sstate.selstartpos,
108 true, sstate.selstartboundary);
109 t->selection.cursor = t->cursor;
110 t->setCursor(bv, sstate.selendpar, sstate.selendpos,
111 true, sstate.selendboundary);
113 t->setCursor(bv, sstate.lpar, sstate.pos);
115 t->setCursor(bv, sstate.lpar, sstate.pos, true, sstate.boundary);
116 t->selection.cursor = t->cursor;
117 t->selection.set(false);
119 if (sstate.refresh) {
125 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
132 InsetText::InsetText()
133 : UpdatableInset(), lt(0), in_update(false), do_resize(0),
141 InsetText::InsetText(InsetText const & in, bool same_id)
142 : UpdatableInset(in, same_id), lt(0), in_update(false), do_resize(0),
150 InsetText & InsetText::operator=(InsetText const & it)
157 void InsetText::init(InsetText const * ins, bool same_id)
160 setParagraphData(ins->par);
161 autoBreakRows = ins->autoBreakRows;
162 drawFrame_ = ins->drawFrame_;
163 frame_color = ins->frame_color;
169 p->setInsetOwner(this);
172 the_locking_inset = 0;
174 frame_color = LColor::insetframe;
175 autoBreakRows = false;
184 no_selection = false;
191 last_drawn_width = -1;
192 frame_is_visible = false;
198 InsetText::~InsetText()
205 Paragraph * tmp = par->next();
212 void InsetText::clear()
215 Paragraph * tmp = par->next();
224 Inset * InsetText::clone(Buffer const &, bool same_id) const
226 return new InsetText(*this, same_id);
230 void InsetText::write(Buffer const * buf, ostream & os) const
233 writeParagraphData(buf, os);
237 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
239 par->writeFile(buf, os, buf->params, 0);
243 void InsetText::read(Buffer const * buf, LyXLex & lex)
247 Paragraph * return_par = 0;
248 Paragraph::depth_type depth = 0;
249 LyXFont font(LyXFont::ALL_INHERIT);
255 token = lex.getString();
258 if (token == "\\end_inset") {
259 #ifndef NO_COMPABILITY
260 const_cast<Buffer*>(buf)->insertErtContents(par, pos, font, false);
265 if (const_cast<Buffer*>(buf)->
266 parseSingleLyXformat2Token(lex, par, return_par,
267 token, pos, depth, font)) {
268 // the_end read this should NEVER happen
269 lex.printError("\\the_end read in inset! Error in document!");
277 return_par->setInsetOwner(this);
278 return_par = return_par->next();
281 if (token != "\\end_inset") {
282 lex.printError("Missing \\end_inset at this point. "
289 int InsetText::ascent(BufferView * bv, LyXFont const &) const
292 Row * row = getLyXText(bv)->getRowNearY(y_temp);
293 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
298 int InsetText::descent(BufferView * bv, LyXFont const &) const
306 Row * row = lt->getRowNearY(y_temp);
307 insetDescent = lt->height - row->ascent_of_text() +
308 TEXT_TO_INSET_OFFSET;
315 int InsetText::width(BufferView * bv, LyXFont const &) const
317 insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
318 (2 * TEXT_TO_INSET_OFFSET);
319 insetWidth = max(insetWidth, 10);
324 int InsetText::textWidth(BufferView * bv, bool fordraw) const
327 if (!autoBreakRows) {
330 w = getMaxWidth(bv, this);
333 return max(w - (2 * TEXT_TO_INSET_OFFSET),
334 (int)getLyXText(bv)->width);
338 return w - (2 * TEXT_TO_INSET_OFFSET);
342 void InsetText::draw(BufferView * bv, LyXFont const & f,
343 int baseline, float & x, bool cleared) const
348 Painter & pain = bv->painter();
350 // this is the first thing we have to ask because if the x pos
351 // changed we have to do a complete rebreak of the text as we
352 // may have few space to draw in. Well we should check on this too
354 if (top_x != int(x)) {
356 int nw = getMaxWidth(bv, this);
357 if (nw > 0 && old_max_width != nw) {
360 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
368 // repaint the background if needed
369 if (cleared && backgroundColor() != LColor::background) {
371 clearInset(pain, baseline, cleared);
375 // no draw is necessary !!!
376 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
379 top_baseline = baseline;
381 if (need_update & CLEAR_FRAME)
382 clearFrame(pain, cleared);
389 x += static_cast<float>(scroll());
391 // if top_x differs we did it already
392 if (!cleared && (top_x == int(x)) &&
393 ((need_update&(INIT|FULL)) || (top_baseline!=baseline) ||
394 (last_drawn_width!=insetWidth))) {
395 clearInset(pain, baseline, cleared);
401 frame_is_visible = false;
403 if (!cleared && (need_update == NONE)) {
405 drawFrame(pain, cleared);
409 top_baseline = baseline;
410 top_y = baseline - ascent(bv, f);
411 last_width = width(bv, f);
412 last_height = ascent(bv, f) + descent(bv, f);
414 if (cleared || (last_drawn_width != insetWidth)) {
416 clearInset(pain, baseline, cleared);
418 last_drawn_width = insetWidth;
421 if (the_locking_inset && (cpar(bv) == inset_par)
422 && (cpos(bv) == inset_pos)) {
423 inset_x = cx(bv) - top_x + drawTextXOffset;
424 inset_y = cy(bv) + drawTextYOffset;
426 if (!cleared && (need_update == CURSOR)
427 && !getLyXText(bv)->selection.set()) {
428 drawFrame(pain, cleared);
429 x += last_width; // was width(bv, f);
438 x += TEXT_TO_INSET_OFFSET;
441 Row * row = lt->getRowNearY(y);
442 int y_offset = baseline - row->ascent_of_text();
443 int ph = pain.paperHeight();
446 while ((row != 0) && ((y+row->height()) <= 0)) {
448 first += row->height();
454 if (cleared || (need_update&(INIT|FULL))) {
457 while ((row != 0) && (yf < ph)) {
458 lt->getVisibleRow(bv, y+y_offset, int(x), row,
464 } else if (!locked) {
465 if (need_update & CURSOR) {
466 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
467 lt->clearSelection();
468 lt->selection.cursor = lt->cursor;
470 bv->screen()->update(lt, bv, y_offset, int(x));
473 if (need_update & SELECTION) {
474 bv->screen()->toggleToggle(lt, bv, y_offset, int(x));
475 } else if (need_update & CURSOR) {
476 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
477 lt->clearSelection();
478 lt->selection.cursor = lt->cursor;
480 bv->screen()->update(lt, bv, y_offset, int(x));
485 lt->status(bv, LyXText::UNCHANGED);
486 if ((need_update != CURSOR_PAR) &&
487 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked))) {
488 drawFrame(pain, cleared);
489 } else if (need_update & CLEAR_FRAME) {
490 clearFrame(pain, cleared);
493 x += last_width /* was width(bv, f) */ - TEXT_TO_INSET_OFFSET;
495 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
497 } else if (need_update != INIT) {
506 void InsetText::drawFrame(Painter & pain, bool cleared) const
508 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
509 if (!frame_is_visible || cleared) {
510 frame_x = top_x + ttoD2;
511 frame_y = top_baseline - insetAscent + ttoD2;
512 frame_w = last_width - TEXT_TO_INSET_OFFSET;
513 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
514 pain.rectangle(frame_x, frame_y,
517 frame_is_visible = true;
522 void InsetText::clearFrame(Painter & pain, bool cleared) const
524 if (frame_is_visible) {
526 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
529 frame_is_visible = false;
534 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
537 if (reinit && owner()) {
539 owner()->update(bv, font, true);
544 if (reinit || need_update == INIT) {
548 owner()->update(bv, font, true);
552 if (the_locking_inset) {
553 inset_x = cx(bv) - top_x + drawTextXOffset;
554 inset_y = cy(bv) + drawTextYOffset;
555 the_locking_inset->update(bv, font, reinit);
564 int oldw = insetWidth;
565 insetWidth = lt->width + (2 * TEXT_TO_INSET_OFFSET);
566 if (oldw != insetWidth) {
575 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
578 lt->updateInset(bv, the_locking_inset);
580 if (lt->status() == LyXText::NEED_MORE_REFRESH)
588 void InsetText::setUpdateStatus(BufferView * bv, int what) const
596 // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
597 // if we don't break row so that we only have one row to update!
598 if ((lt->status() == LyXText::NEED_MORE_REFRESH) ||
600 (lt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
603 } else if (lt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
604 need_update |= CURSOR_PAR;
607 // this to not draw a selection when we redraw all of it!
608 if (need_update & CURSOR && !(need_update & SELECTION)) {
609 if (lt->selection.set())
611 lt->clearSelection();
618 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
626 setUpdateStatus(bv, what);
627 if (((need_update != CURSOR) && (need_update != NONE)) ||
628 (lt->status() != LyXText::UNCHANGED) || lt->selection.set())
630 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
632 if (need_update == CURSOR)
634 bv->owner()->showState();
635 if (old_par != cpar(bv)) {
636 bv->owner()->setLayout(cpar(bv)->getLayout());
644 string const InsetText::editMessage() const
646 return _("Opened Text Inset");
650 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
652 UpdatableInset::edit(bv, x, y, button);
654 if (!bv->lockInset(this)) {
655 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
659 the_locking_inset = 0;
660 inset_pos = inset_x = inset_y = 0;
661 inset_boundary = false;
664 int tmp_y = (y < 0) ? 0 : y;
671 if (!checkAndActivateInset(bv, x, tmp_y, button))
672 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
674 lt->clearSelection();
677 updateLocal(bv, CURSOR, false);
679 // If the inset is empty set the language of the current font to the
680 // language to the surronding text (if different).
681 if (par->size() == 0 && !par->next() &&
682 bv->getParentLanguage(this) != lt->current_font.language()) {
683 LyXFont font(LyXFont::ALL_IGNORE);
684 font.setLanguage(bv->getParentLanguage(this));
685 setFont(bv, font, false);
692 void InsetText::edit(BufferView * bv, bool front)
694 UpdatableInset::edit(bv, front);
696 if (!bv->lockInset(this)) {
697 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
701 the_locking_inset = 0;
702 inset_pos = inset_x = inset_y = 0;
703 inset_boundary = false;
712 lt->setCursor(bv, par, 0);
717 // int const pos = (p->size() ? p->size()-1 : p->size());
718 lt->setCursor(bv, p, p->size());
720 lt->clearSelection();
723 updateLocal(bv, CURSOR, false);
725 // If the inset is empty set the language of the current font to the
726 // language to the surronding text (if different).
727 if (par->size() == 0 && !par->next() &&
728 bv->getParentLanguage(this) != lt->current_font.language()) {
729 LyXFont font(LyXFont::ALL_IGNORE);
730 font.setLanguage(bv->getParentLanguage(this));
731 setFont(bv, font, false);
738 void InsetText::insetUnlock(BufferView * bv)
740 if (the_locking_inset) {
741 the_locking_inset->insetUnlock(bv);
742 the_locking_inset = 0;
745 no_selection = false;
748 if (drawFrame_ == LOCKED)
749 code = CURSOR|CLEAR_FRAME;
757 if (lt->selection.set()) {
758 lt->clearSelection();
760 } else if (owner()) {
761 bv->owner()->setLayout(owner()->getLyXText(bv)
762 ->cursor.par()->getLayout());
764 bv->owner()->setLayout(bv->text->cursor.par()->getLayout());
765 // hack for deleteEmptyParMech
766 lt->setCursor(bv, par, 0);
767 updateLocal(bv, code, false);
773 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
775 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
779 if (inset == cpar(bv)->getInset(cpos(bv))) {
780 lyxerr[Debug::INSETS] << "OK" << endl;
781 the_locking_inset = inset;
782 inset_x = cx(bv) - top_x + drawTextXOffset;
783 inset_y = cy(bv) + drawTextYOffset;
784 inset_pos = cpos(bv);
785 inset_par = cpar(bv);
786 inset_boundary = cboundary(bv);
787 updateLocal(bv, CURSOR, false);
789 } else if (the_locking_inset && (the_locking_inset == inset)) {
790 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
791 lyxerr[Debug::INSETS] << "OK" << endl;
792 inset_x = cx(bv) - top_x + drawTextXOffset;
793 inset_y = cy(bv) + drawTextYOffset;
795 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
797 } else if (the_locking_inset) {
798 lyxerr[Debug::INSETS] << "MAYBE" << endl;
799 return the_locking_inset->lockInsetInInset(bv, inset);
801 lyxerr[Debug::INSETS] << "NOT OK" << endl;
806 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
809 if (!the_locking_inset)
811 if (the_locking_inset == inset) {
812 the_locking_inset->insetUnlock(bv);
813 getLyXText(bv)->updateInset(bv, inset);
814 the_locking_inset = 0;
816 moveRight(bv, false);
817 old_par = 0; // force layout setting
821 updateLocal(bv, CURSOR, false);
824 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
828 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
830 if (!the_locking_inset)
832 if (the_locking_inset != inset) {
833 getLyXText(bv)->updateInset(bv, the_locking_inset);
834 setUpdateStatus(bv, CURSOR_PAR);
835 return the_locking_inset->updateInsetInInset(bv, inset);
837 if (getLyXText(bv)->updateInset(bv, inset))
838 updateLocal(bv, CURSOR_PAR, false);
839 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
840 inset_x = cx(bv) - top_x + drawTextXOffset;
841 inset_y = cy(bv) + drawTextYOffset;
847 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
851 int tmp_x = x - drawTextXOffset;
852 int tmp_y = y + insetAscent - getLyXText(bv)->first;
853 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y, button);
856 if (the_locking_inset) {
857 if (the_locking_inset == inset) {
858 the_locking_inset->insetButtonPress(bv,x-inset_x,y-inset_y,button);
859 no_selection = false;
862 // otherwise unlock the_locking_inset and lock the new inset
863 the_locking_inset->insetUnlock(bv);
864 inset_x = cx(bv) - top_x + drawTextXOffset;
865 inset_y = cy(bv) + drawTextYOffset;
866 the_locking_inset = static_cast<UpdatableInset*>(inset);
867 inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
868 inset->edit(bv, x - inset_x, y - inset_y, button);
869 if (the_locking_inset)
870 updateLocal(bv, CURSOR, false);
871 no_selection = false;
874 // otherwise only unlock the_locking_inset
875 the_locking_inset->insetUnlock(bv);
876 the_locking_inset = 0;
878 if (bv->theLockingInset()) {
879 if (isHighlyEditableInset(inset)) {
880 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
881 inset_x = cx(bv) - top_x + drawTextXOffset;
882 inset_y = cy(bv) + drawTextYOffset;
883 inset_pos = cpos(bv);
884 inset_par = cpar(bv);
885 inset_boundary = cboundary(bv);
886 the_locking_inset = uinset;
887 uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
889 uinset->edit(bv, x - inset_x, y - inset_y, 0);
890 if (the_locking_inset)
891 updateLocal(bv, CURSOR, false);
892 no_selection = false;
896 if (!inset) { // && (button == 2)) {
897 bool paste_internally = false;
898 if ((button == 2) && getLyXText(bv)->selection.set()) {
899 localDispatch(bv, LFUN_COPY, "");
900 paste_internally = true;
907 lt->setCursorFromCoordinates(bv, x-drawTextXOffset, y + insetAscent);
908 if (lt->selection.set()) {
909 lt->clearSelection();
910 updateLocal(bv, FULL, false);
912 lt->clearSelection();
914 bv->owner()->setLayout(cpar(bv)->getLayout());
916 // Insert primary selection with middle mouse
917 // if there is a local selection in the current buffer,
920 if (paste_internally)
921 localDispatch(bv, LFUN_PASTE, "");
923 localDispatch(bv, LFUN_PASTESELECTION,
930 no_selection = false;
934 void InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
936 UpdatableInset * inset = 0;
938 if (the_locking_inset) {
939 the_locking_inset->insetButtonRelease(bv,
940 x - inset_x, y - inset_y,
943 if (isMetaInset(cpar(bv), cpos(bv))) {
944 inset = static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
945 if (isHighlyEditableInset(inset)) {
946 inset->insetButtonRelease(bv,
948 y - inset_y, button);
950 inset_x = cx(bv) - top_x + drawTextXOffset;
951 inset_y = cy(bv) + drawTextYOffset;
952 inset->insetButtonRelease(bv,
954 y - inset_y, button);
956 x - inset_x, y - inset_y, button);
958 updateLocal(bv, CURSOR_PAR, false);
961 no_selection = false;
965 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
969 if (the_locking_inset) {
970 the_locking_inset->insetMotionNotify(bv, x - inset_x,
974 LyXText * t = getLyXText(bv);
976 t->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
978 if (t->toggle_cursor.par() != t->toggle_end_cursor.par() ||
979 t->toggle_cursor.pos() != t->toggle_end_cursor.pos())
980 updateLocal(bv, SELECTION, false);
985 void InsetText::insetKeyPress(XKeyEvent * xke)
987 if (the_locking_inset) {
988 the_locking_inset->insetKeyPress(xke);
994 UpdatableInset::RESULT
995 InsetText::localDispatch(BufferView * bv,
996 kb_action action, string const & arg)
998 bool was_empty = par->size() == 0 && !par->next();
999 no_selection = false;
1000 UpdatableInset::RESULT
1001 result= UpdatableInset::localDispatch(bv, action, arg);
1002 if (result != UNDISPATCHED) {
1006 result = DISPATCHED;
1007 if ((action < 0) && arg.empty())
1010 if (the_locking_inset) {
1011 result = the_locking_inset->localDispatch(bv, action, arg);
1012 if (result == DISPATCHED_NOUPDATE)
1014 else if (result == DISPATCHED) {
1015 updateLocal(bv, CURSOR_PAR, false);
1017 } else if (result >= FINISHED) {
1019 case FINISHED_RIGHT:
1020 moveRightIntern(bv, false, false);
1021 result = DISPATCHED;
1024 if ((result = moveUp(bv)) >= FINISHED) {
1025 updateLocal(bv, CURSOR, false);
1026 bv->unlockInset(this);
1030 if ((result = moveDown(bv)) >= FINISHED) {
1031 updateLocal(bv, CURSOR, false);
1032 bv->unlockInset(this);
1036 result = DISPATCHED;
1039 the_locking_inset = 0;
1040 #ifdef WITH_WARNINGS
1041 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
1046 hideInsetCursor(bv);
1049 lt = getLyXText(bv);
1054 case LFUN_SELFINSERT:
1055 if (bv->buffer()->isReadonly()) {
1056 // setErrorMessage(N_("Document is read only"));
1060 /* Automatically delete the currently selected
1061 * text and replace it with what is being
1062 * typed in now. Depends on lyxrc settings
1063 * "auto_region_delete", which defaults to
1066 setUndo(bv, Undo::INSERT,
1067 lt->cursor.par(), lt->cursor.par()->next());
1069 if (lyxrc.auto_region_delete) {
1070 if (lt->selection.set()) {
1071 lt->cutSelection(bv, false);
1074 lt->clearSelection();
1075 for (string::size_type i = 0; i < arg.length(); ++i) {
1076 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1079 lt->selection.cursor = lt->cursor;
1080 updateLocal(bv, CURSOR_PAR, true);
1081 result = DISPATCHED_NOUPDATE;
1083 // --- Cursor Movements -----------------------------------
1086 moveRight(bv, false, true);
1087 lt->setSelection(bv);
1088 updateLocal(bv, SELECTION, false);
1091 result = moveRight(bv);
1093 updateLocal(bv, CURSOR, false);
1097 moveLeft(bv, false, true);
1098 lt->setSelection(bv);
1099 updateLocal(bv, SELECTION, false);
1103 result = moveLeft(bv);
1104 updateLocal(bv, CURSOR, false);
1109 lt->setSelection(bv);
1110 updateLocal(bv, SELECTION, false);
1114 result = moveDown(bv);
1115 updateLocal(bv, CURSOR, false);
1120 lt->setSelection(bv);
1121 updateLocal(bv, SELECTION, false);
1125 result = moveUp(bv);
1126 updateLocal(bv, CURSOR, false);
1131 updateLocal(bv, CURSOR, false);
1135 updateLocal(bv, CURSOR, false);
1137 case LFUN_BACKSPACE: {
1138 setUndo(bv, Undo::DELETE,
1139 lt->cursor.par(), lt->cursor.par()->next());
1140 if (lt->selection.set())
1141 lt->cutSelection(bv);
1144 updateLocal(bv, CURSOR_PAR, true);
1149 setUndo(bv, Undo::DELETE,
1150 lt->cursor.par(), lt->cursor.par()->next());
1151 if (lt->selection.set()) {
1152 lt->cutSelection(bv);
1156 updateLocal(bv, CURSOR_PAR, true);
1161 setUndo(bv, Undo::DELETE,
1162 lt->cursor.par(), lt->cursor.par()->next());
1163 lt->cutSelection(bv);
1164 updateLocal(bv, CURSOR_PAR, true);
1170 lt->copySelection(bv);
1171 updateLocal(bv, CURSOR_PAR, false);
1173 case LFUN_PASTESELECTION:
1175 string const clip(bv->getClipboard());
1179 if (arg == "paragraph") {
1180 lt->insertStringAsParagraphs(bv, clip);
1182 lt->insertStringAsLines(bv, clip);
1184 updateLocal(bv, CURSOR_PAR, true);
1188 if (!autoBreakRows) {
1190 if (CutAndPaste::nrOfParagraphs() > 1) {
1191 WriteAlert(_("Impossible operation"),
1192 _("Cannot include more than one paragraph!"),
1197 setUndo(bv, Undo::INSERT,
1198 lt->cursor.par(), lt->cursor.par()->next());
1199 lt->pasteSelection(bv);
1200 updateLocal(bv, CURSOR_PAR, true);
1204 case LFUN_BREAKPARAGRAPH:
1205 if (!autoBreakRows) {
1206 result = DISPATCHED;
1209 lt->breakParagraph(bv, 0);
1210 updateLocal(bv, FULL, true);
1212 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1213 if (!autoBreakRows) {
1214 result = DISPATCHED;
1217 lt->breakParagraph(bv, 1);
1218 updateLocal(bv, FULL, true);
1221 case LFUN_BREAKLINE: {
1222 if (!autoBreakRows) {
1223 result = DISPATCHED;
1226 setUndo(bv, Undo::INSERT,
1227 lt->cursor.par(), lt->cursor.par()->next());
1228 lt->insertChar(bv, Paragraph::META_NEWLINE);
1229 updateLocal(bv, CURSOR_PAR, true);
1234 // do not set layouts on non breakable textinsets
1235 if (autoBreakRows) {
1236 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1238 // Derive layout number from given argument (string)
1239 // and current buffer's textclass (number). */
1240 LyXTextClassList::ClassList::size_type tclass =
1241 bv->buffer()->params.textclass;
1242 std::pair <bool, LyXTextClass::size_type> layout =
1243 textclasslist.NumberOfLayout(tclass, arg);
1245 // If the entry is obsolete, use the new one instead.
1247 string obs = textclasslist.Style(tclass,layout.second).
1250 layout = textclasslist.NumberOfLayout(tclass, obs);
1253 // see if we found the layout number:
1254 if (!layout.first) {
1255 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1256 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1260 if (cur_layout != layout.second) {
1261 cur_layout = layout.second;
1262 lt->setLayout(bv, layout.second);
1263 bv->owner()->setLayout(cpar(bv)->getLayout());
1264 updateLocal(bv, CURSOR_PAR, true);
1267 // reset the layout box
1268 bv->owner()->setLayout(cpar(bv)->getLayout());
1271 case LFUN_PARAGRAPH_SPACING:
1272 // This one is absolutely not working. When fiddling with this
1273 // it also seems to me that the paragraphs inside the insettext
1274 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1276 Paragraph * par = lt->cursor.par();
1277 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1278 float cur_value = 1.0;
1279 if (cur_spacing == Spacing::Other) {
1280 cur_value = par->params().spacing().getValue();
1283 istringstream istr(arg.c_str());
1286 Spacing::Space new_spacing = cur_spacing;
1287 float new_value = cur_value;
1289 lyxerr << "Missing argument to `paragraph-spacing'"
1291 } else if (tmp == "single") {
1292 new_spacing = Spacing::Single;
1293 } else if (tmp == "onehalf") {
1294 new_spacing = Spacing::Onehalf;
1295 } else if (tmp == "double") {
1296 new_spacing = Spacing::Double;
1297 } else if (tmp == "other") {
1298 new_spacing = Spacing::Other;
1301 lyxerr << "new_value = " << tmpval << endl;
1304 } else if (tmp == "default") {
1305 new_spacing = Spacing::Default;
1307 lyxerr << _("Unknown spacing argument: ")
1310 if (cur_spacing != new_spacing || cur_value != new_value) {
1311 par->params().spacing(Spacing(new_spacing, new_value));
1312 updateLocal(bv, CURSOR_PAR, true);
1318 if (!bv->Dispatch(action, arg))
1319 result = UNDISPATCHED;
1323 /// If the action has deleted all text in the inset, we need to change the
1324 // language to the language of the surronding text.
1325 if (!was_empty && par->size() == 0 && !par->next()) {
1326 LyXFont font(LyXFont::ALL_IGNORE);
1327 font.setLanguage(bv->getParentLanguage(this));
1328 setFont(bv, font, false);
1331 if (result < FINISHED) {
1332 showInsetCursor(bv);
1334 bv->unlockInset(this);
1341 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1344 buf->latexParagraphs(os, par, 0, texrow);
1345 return texrow.rows();
1349 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1351 Paragraph * p = par;
1352 unsigned int lines = 0;
1355 string const tmp = buf->asciiParagraph(p, linelen);
1356 lines += countChar(tmp, '\n');
1364 int InsetText::docbook(Buffer const * buf, ostream & os) const
1366 Paragraph * p = par;
1367 unsigned int lines = 0;
1369 vector<string> environment_stack(10);
1370 vector<string> environment_inner(10);
1372 int const command_depth = 0;
1375 Paragraph::depth_type depth = 0; // paragraph depth
1379 int desc_on = 0; // description mode
1381 LyXLayout const & style =
1382 textclasslist.Style(buf->params.textclass,
1385 // environment tag closing
1386 for (; depth > p->params().depth(); --depth) {
1387 if (environment_inner[depth] != "!-- --") {
1388 item_name = "listitem";
1389 buf->sgmlCloseTag(os, command_depth + depth,
1391 if (environment_inner[depth] == "varlistentry")
1392 buf->sgmlCloseTag(os, depth+command_depth,
1393 environment_inner[depth]);
1395 buf->sgmlCloseTag(os, depth + command_depth,
1396 environment_stack[depth]);
1397 environment_stack[depth].erase();
1398 environment_inner[depth].erase();
1401 if (depth == p->params().depth()
1402 && environment_stack[depth] != style.latexname()
1403 && !environment_stack[depth].empty()) {
1404 if (environment_inner[depth] != "!-- --") {
1405 item_name= "listitem";
1406 buf->sgmlCloseTag(os, command_depth+depth,
1408 if (environment_inner[depth] == "varlistentry")
1409 buf->sgmlCloseTag(os,
1410 depth + command_depth,
1411 environment_inner[depth]);
1414 buf->sgmlCloseTag(os, depth + command_depth,
1415 environment_stack[depth]);
1417 environment_stack[depth].erase();
1418 environment_inner[depth].erase();
1421 // Write opening SGML tags.
1422 switch (style.latextype) {
1423 case LATEX_PARAGRAPH:
1424 buf->sgmlOpenTag(os, depth + command_depth,
1429 buf->sgmlError(p, 0,
1430 _("Error : LatexType Command not allowed here.\n"));
1434 case LATEX_ENVIRONMENT:
1435 case LATEX_ITEM_ENVIRONMENT:
1436 if (depth < p->params().depth()) {
1437 depth = p->params().depth();
1438 environment_stack[depth].erase();
1441 if (environment_stack[depth] != style.latexname()) {
1442 if(environment_stack.size() == depth + 1) {
1443 environment_stack.push_back("!-- --");
1444 environment_inner.push_back("!-- --");
1446 environment_stack[depth] = style.latexname();
1447 environment_inner[depth] = "!-- --";
1448 buf->sgmlOpenTag(os, depth + command_depth,
1449 environment_stack[depth]);
1451 if (environment_inner[depth] != "!-- --") {
1452 item_name= "listitem";
1453 buf->sgmlCloseTag(os,
1454 command_depth + depth,
1456 if (environment_inner[depth] == "varlistentry")
1457 buf->sgmlCloseTag(os,
1458 depth + command_depth,
1459 environment_inner[depth]);
1463 if (style.latextype == LATEX_ENVIRONMENT) {
1464 if (!style.latexparam().empty()) {
1465 if(style.latexparam() == "CDATA")
1468 buf->sgmlOpenTag(os, depth + command_depth,
1469 style.latexparam());
1474 desc_on = (style.labeltype == LABEL_MANUAL);
1477 environment_inner[depth]= "varlistentry";
1479 environment_inner[depth]= "listitem";
1481 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1482 environment_inner[depth]);
1486 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1490 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1495 buf->sgmlOpenTag(os, depth + command_depth,
1500 buf->simpleDocBookOnePar(os, p, desc_on,
1501 depth + 1 + command_depth);
1505 // write closing SGML tags
1506 switch (style.latextype) {
1507 case LATEX_ENVIRONMENT:
1508 if (!style.latexparam().empty()) {
1509 if(style.latexparam() == "CDATA")
1512 buf->sgmlCloseTag(os, depth + command_depth,
1513 style.latexparam());
1516 case LATEX_ITEM_ENVIRONMENT:
1517 if (desc_on == 1) break;
1519 buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1521 case LATEX_PARAGRAPH:
1522 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1525 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1531 for (int d = depth; d >= 0; --d) {
1532 if (!environment_stack[depth].empty()) {
1533 if (environment_inner[depth] != "!-- --") {
1534 item_name = "listitem";
1535 buf->sgmlCloseTag(os, command_depth + depth,
1537 if (environment_inner[depth] == "varlistentry")
1538 buf->sgmlCloseTag(os, depth + command_depth,
1539 environment_inner[depth]);
1542 buf->sgmlCloseTag(os, depth + command_depth,
1543 environment_stack[depth]);
1551 void InsetText::validate(LaTeXFeatures & features) const
1553 Paragraph * p = par;
1555 p->validate(features);
1561 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1563 if (textclasslist.Style(buf->params.textclass,
1564 p->getLayout()).labeltype != LABEL_MANUAL)
1567 return p->beginningOfMainBody();
1571 void InsetText::getCursorPos(BufferView * bv,
1572 int & x, int & y) const
1574 if (the_locking_inset) {
1575 the_locking_inset->getCursorPos(bv, x, y);
1583 unsigned int InsetText::insetInInsetY()
1585 if (!the_locking_inset)
1588 return (inset_y + the_locking_inset->insetInInsetY());
1592 void InsetText::toggleInsetCursor(BufferView * bv)
1594 if (the_locking_inset) {
1595 the_locking_inset->toggleInsetCursor(bv);
1599 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1601 int const asc = lyxfont::maxAscent(font);
1602 int const desc = lyxfont::maxDescent(font);
1604 if (isCursorVisible())
1605 bv->hideLockedInsetCursor();
1607 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1608 toggleCursorVisible();
1612 void InsetText::showInsetCursor(BufferView * bv, bool show)
1614 if (the_locking_inset) {
1615 the_locking_inset->showInsetCursor(bv, show);
1618 if (!isCursorVisible()) {
1619 LyXFont const font =
1620 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1622 int const asc = lyxfont::maxAscent(font);
1623 int const desc = lyxfont::maxDescent(font);
1625 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1627 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1628 setCursorVisible(true);
1633 void InsetText::hideInsetCursor(BufferView * bv)
1635 if (isCursorVisible()) {
1636 bv->hideLockedInsetCursor();
1637 setCursorVisible(false);
1639 if (the_locking_inset)
1640 the_locking_inset->hideInsetCursor(bv);
1644 void InsetText::fitInsetCursor(BufferView * bv) const
1646 if (the_locking_inset) {
1647 the_locking_inset->fitInsetCursor(bv);
1650 LyXFont const font =
1651 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1653 int const asc = lyxfont::maxAscent(font);
1654 int const desc = lyxfont::maxDescent(font);
1656 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1660 UpdatableInset::RESULT
1661 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1663 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1664 return moveLeftIntern(bv, false, activate_inset, selecting);
1666 return moveRightIntern(bv, false, activate_inset, selecting);
1670 UpdatableInset::RESULT
1671 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1673 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1674 return moveRightIntern(bv, true, activate_inset, selecting);
1676 return moveLeftIntern(bv, true, activate_inset, selecting);
1680 UpdatableInset::RESULT
1681 InsetText::moveRightIntern(BufferView * bv, bool behind,
1682 bool activate_inset, bool selecting)
1684 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1685 return FINISHED_RIGHT;
1686 if (activate_inset && checkAndActivateInset(bv, behind))
1688 getLyXText(bv)->cursorRight(bv);
1690 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1691 return DISPATCHED_NOUPDATE;
1695 UpdatableInset::RESULT
1696 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1697 bool activate_inset, bool selecting)
1699 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1701 getLyXText(bv)->cursorLeft(bv);
1703 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1704 if (activate_inset && checkAndActivateInset(bv, behind))
1706 return DISPATCHED_NOUPDATE;
1710 UpdatableInset::RESULT
1711 InsetText::moveUp(BufferView * bv)
1713 if (!crow(bv)->previous())
1715 getLyXText(bv)->cursorUp(bv);
1716 return DISPATCHED_NOUPDATE;
1720 UpdatableInset::RESULT
1721 InsetText::moveDown(BufferView * bv)
1723 if (!crow(bv)->next())
1724 return FINISHED_DOWN;
1725 getLyXText(bv)->cursorDown(bv);
1726 return DISPATCHED_NOUPDATE;
1730 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1732 if (the_locking_inset) {
1733 if (the_locking_inset->insetAllowed(inset))
1734 return the_locking_inset->insertInset(bv, inset);
1737 inset->setOwner(this);
1738 hideInsetCursor(bv);
1742 lt = getLyXText(bv);
1745 lt->insertInset(bv, inset);
1747 if ((!isMetaInset(cpar(bv), cpos(bv))) ||
1748 (cpar(bv)->getInset(cpos(bv)) != inset))
1752 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1753 showInsetCursor(bv);
1760 bool InsetText::insetAllowed(Inset::Code code) const
1762 if (the_locking_inset)
1763 return the_locking_inset->insetAllowed(code);
1768 UpdatableInset * InsetText::getLockingInset() const
1770 return the_locking_inset ? the_locking_inset->getLockingInset() :
1771 const_cast<InsetText *>(this);
1775 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1779 if (the_locking_inset)
1780 return the_locking_inset->getFirstLockingInsetOfType(c);
1785 bool InsetText::showInsetDialog(BufferView * bv) const
1787 if (the_locking_inset)
1788 return the_locking_inset->showInsetDialog(bv);
1793 std::vector<string> const InsetText::getLabelList() const
1795 std::vector<string> label_list;
1797 Paragraph * tpar = par;
1799 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1800 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1801 for (; beg != end; ++beg) {
1802 std::vector<string> const l = (*beg)->getLabelList();
1803 label_list.insert(label_list.end(), l.begin(), l.end());
1805 tpar = tpar->next();
1811 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1814 if (the_locking_inset) {
1815 the_locking_inset->setFont(bv, font, toggleall, selectall);
1818 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
1819 getLyXText(bv)->setFont(bv, font, toggleall);
1824 lt = getLyXText(bv);
1827 if (lt->selection.set()) {
1828 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1832 lt->toggleFree(bv, font, toggleall);
1834 lt->clearSelection();
1836 if (selectall || lt->selection.set())
1837 updateLocal(bv, FULL, true);
1839 updateLocal(bv, CURSOR_PAR, true);
1845 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1847 if (isMetaInset(cpar(bv), cpos(bv))) {
1851 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1852 if (!isHighlyEditableInset(inset))
1854 LyXFont const font =
1855 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1857 x = inset->width(bv, font);
1858 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1861 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1863 //inset_x = cx(bv) - top_x + drawTextXOffset;
1864 //inset_y = cy(bv) + drawTextYOffset;
1865 inset->edit(bv, x, y, 0);
1866 if (!the_locking_inset)
1868 updateLocal(bv, CURSOR, false);
1875 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1878 x -= drawTextXOffset;
1880 int dummyy = y + insetAscent;
1881 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy, button);
1888 inset_x = cx(bv) - top_x + drawTextXOffset;
1889 inset_y = cy(bv) + drawTextYOffset;
1890 inset->edit(bv, x - inset_x, y - inset_y, button);
1891 if (!the_locking_inset)
1893 updateLocal(bv, CURSOR, false);
1900 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1903 int w = UpdatableInset::getMaxWidth(bv, inset);
1908 w = w - top_x + owner()->x();
1911 w -= (2 * TEXT_TO_INSET_OFFSET);
1914 return UpdatableInset::getMaxWidth(bv, inset);
1919 void InsetText::setParagraphData(Paragraph * p)
1921 // we have to unlock any locked inset otherwise we're in troubles
1922 the_locking_inset = 0;
1924 Paragraph * tmp = par->next();
1929 par = new Paragraph(*p, false);
1930 par->setInsetOwner(this);
1931 Paragraph * np = par;
1934 np->next(new Paragraph(*p, false));
1935 np->next()->previous(np);
1937 np->setInsetOwner(this);
1943 void InsetText::setText(string const & data)
1946 LyXFont font(LyXFont::ALL_SANE);
1947 for (unsigned int i=0; i < data.length(); ++i)
1948 par->insertChar(i, data[i], font);
1952 void InsetText::setAutoBreakRows(bool flag)
1954 if (flag != autoBreakRows) {
1955 autoBreakRows = flag;
1963 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1965 if (how != drawFrame_) {
1968 updateLocal(bv, DRAW_FRAME, false);
1973 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1975 if (frame_color != col) {
1978 updateLocal(bv, DRAW_FRAME, false);
1983 int InsetText::cx(BufferView * bv) const
1987 lt = getLyXText(bv);
1990 int x = lt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1991 if (the_locking_inset) {
1992 LyXFont font = lt->getFont(bv->buffer(),
1995 if (font.isVisibleRightToLeft())
1996 x -= the_locking_inset->width(bv, font);
2004 int InsetText::cy(BufferView * bv) const
2007 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2011 Paragraph::size_type InsetText::cpos(BufferView * bv) const
2013 return getLyXText(bv)->cursor.pos();
2017 Paragraph * InsetText::cpar(BufferView * bv) const
2019 return getLyXText(bv)->cursor.par();
2023 bool InsetText::cboundary(BufferView * bv) const
2025 return getLyXText(bv)->cursor.boundary();
2029 Row * InsetText::crow(BufferView * bv) const
2031 return getLyXText(bv)->cursor.row();
2035 LyXText * InsetText::getLyXText(BufferView const * lbv,
2036 bool const recursive) const
2038 if (!recursive && (cached_bview == lbv))
2039 return cached_text.get();
2041 // Super UGLY! (Lgb)
2042 BufferView * bv = const_cast<BufferView *>(lbv);
2045 Cache::iterator it = cache.find(bv);
2047 if (it != cache.end()) {
2051 resizeLyXText(do_resize);
2052 if (lt || !it->second.remove) {
2053 lyx::Assert(it->second.text.get());
2054 cached_text = it->second.text;
2055 if (recursive && the_locking_inset) {
2056 return the_locking_inset->getLyXText(bv, true);
2058 return cached_text.get();
2059 } else if (it->second.remove) {
2061 saveLyXTextState(it->second.text.get());
2068 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2069 cached_text->init(bv);
2070 restoreLyXTextState(bv, cached_text.get());
2072 cache.insert(make_pair(bv, cached_text));
2074 if (the_locking_inset && recursive) {
2075 return the_locking_inset->getLyXText(bv);
2077 return cached_text.get();
2081 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2085 Cache::iterator it = cache.find(bv);
2087 if (it == cache.end()) {
2091 lyx::Assert(it->second.text.get());
2093 it->second.remove = true;
2095 /// then remove all LyXText in text-insets
2096 Paragraph * p = par;
2097 for (; p; p = p->next()) {
2098 p->deleteInsetsLyXText(bv);
2104 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2107 // we cannot resize this because we are in use!
2108 // so do this on the next possible getLyXText()
2113 // lyxerr << "InsetText::resizeLyXText\n";
2114 if (!par->next() && !par->size()) // no data, resize not neccessary!
2116 // one endless line, resize normally not necessary
2117 if (!force && getMaxWidth(bv, this) < 0)
2120 Cache::iterator it = cache.find(bv);
2121 if (it == cache.end()) {
2124 lyx::Assert(it->second.text.get());
2126 LyXText * t = it->second.text.get();
2127 saveLyXTextState(t);
2128 for (Paragraph * p = par; p; p = p->next()) {
2129 p->resizeInsetsLyXText(bv);
2132 restoreLyXTextState(bv, t);
2133 if (the_locking_inset) {
2134 inset_x = cx(bv) - top_x + drawTextXOffset;
2135 inset_y = cy(bv) + drawTextYOffset;
2139 t->first = bv->screen()->topCursorVisible(t);
2142 updateLocal(bv, FULL, false);
2143 // this will scroll the screen such that the cursor becomes visible
2144 bv->updateScrollbar();
2146 need_update |= FULL;
2151 void InsetText::reinitLyXText() const
2154 // we cannot resize this because we are in use!
2155 // so do this on the next possible getLyXText()
2161 // lyxerr << "InsetText::reinitLyXText\n";
2162 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2163 lyx::Assert(it->second.text.get());
2165 LyXText * t = it->second.text.get();
2166 BufferView * bv = it->first;
2168 saveLyXTextState(t);
2169 for (Paragraph * p = par; p; p = p->next()) {
2170 p->resizeInsetsLyXText(bv);
2173 restoreLyXTextState(bv, t);
2174 if (the_locking_inset) {
2175 inset_x = cx(bv) - top_x + drawTextXOffset;
2176 inset_y = cy(bv) + drawTextYOffset;
2179 t->first = bv->screen()->topCursorVisible(t);
2182 updateLocal(bv, FULL, false);
2183 // this will scroll the screen such that the cursor becomes visible
2184 bv->updateScrollbar();
2192 void InsetText::removeNewlines()
2194 for (Paragraph * p = par; p; p = p->next()) {
2195 for (int i = 0; i < p->size(); ++i) {
2196 if (p->getChar(i) == Paragraph::META_NEWLINE)
2203 bool InsetText::nodraw() const
2205 if (the_locking_inset)
2206 return the_locking_inset->nodraw();
2207 return UpdatableInset::nodraw();
2211 int InsetText::scroll(bool recursive) const
2213 int sx = UpdatableInset::scroll(false);
2215 if (recursive && the_locking_inset)
2216 sx += the_locking_inset->scroll(recursive);
2222 bool InsetText::doClearArea() const
2224 return !locked || (need_update & (FULL|INIT));
2228 void InsetText::selectAll(BufferView * bv)
2230 getLyXText(bv)->cursorTop(bv);
2231 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2232 getLyXText(bv)->cursorBottom(bv);
2233 getLyXText(bv)->setSelection(bv);
2237 void InsetText::clearSelection(BufferView * bv)
2239 getLyXText(bv)->clearSelection();
2243 void InsetText::clearInset(Painter & pain, int baseline, bool & cleared) const
2246 int h = insetAscent + insetDescent;
2247 int ty = baseline - insetAscent;
2253 if ((ty + h) > pain.paperHeight())
2254 h = pain.paperHeight();
2255 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2256 w = pain.paperWidth();
2257 // w -= TEXT_TO_INSET_OFFSET;
2258 pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2261 frame_is_visible = false;
2265 Paragraph * InsetText::getParFromID(int id) const
2268 Paragraph * result = par;
2269 Paragraph * ires = 0;
2270 while (result && result->id() != id) {
2271 if ((ires = result->getParFromID(id)))
2273 result = result->next();
2277 Paragraph * tmp = par;
2279 int tmp_id = tmp->id();
2280 lyxerr << "Looking at paragraph: " << tmp_id << endl;
2281 if (tmp->id() == id) {
2284 Paragraph * tmp2 = tmp->getParFromID(id);
2295 Paragraph * InsetText::firstParagraph() const
2298 if (the_locking_inset)
2299 if ((result = the_locking_inset->firstParagraph()))
2305 Paragraph * InsetText::getFirstParagraph(int i) const
2307 return (i == 0) ? par : 0;
2311 LyXCursor const & InsetText::cursor(BufferView * bv) const
2313 if (the_locking_inset)
2314 return the_locking_inset->cursor(bv);
2315 return getLyXText(bv)->cursor;
2319 Paragraph * InsetText::paragraph() const
2325 void InsetText::paragraph(Paragraph * p)
2329 // we now have to update/redraw all instances
2330 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
2335 // redraw myself when asked for
2340 Inset * InsetText::getInsetFromID(int id_arg) const
2343 return const_cast<InsetText *>(this);
2345 Paragraph * lp = par;
2348 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2349 en = lp->inset_iterator_end();
2352 if ((*it)->id() == id_arg)
2354 Inset * in = (*it)->getInsetFromID(id_arg);
2364 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2370 lt = getLyXText(bv);
2373 if (the_locking_inset) {
2374 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2381 #warning Dekel please have a look on this one RTL? (Jug)
2383 // we have to go on checking so move cusor to the right
2384 lt->cursor.pos(lt->cursor.pos() + 1);
2386 str = lt->selectNextWordToSpellcheck(bv, value);
2388 bv->unlockInset(const_cast<InsetText *>(this));
2397 void InsetText::selectSelectedWord(BufferView * bv)
2399 if (the_locking_inset) {
2400 the_locking_inset->selectSelectedWord(bv);
2403 getLyXText(bv)->selectSelectedWord(bv);
2404 updateLocal(bv, SELECTION, false);
2408 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2410 if (the_locking_inset) {
2411 the_locking_inset->toggleSelection(bv, kill_selection);
2415 lt = getLyXText(bv);
2419 int x = top_x + TEXT_TO_INSET_OFFSET;
2422 Row * row = lt->getRowNearY(y);
2423 int y_offset = top_baseline - row->ascent_of_text();
2425 while ((row != 0) && ((y+row->height()) <= 0)) {
2432 if (need_update & SELECTION)
2434 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2440 bool InsetText::searchForward(BufferView * bv, string const & str,
2441 bool const & cs, bool const & mw)
2443 if (the_locking_inset) {
2444 if (the_locking_inset->searchForward(bv, str, cs, mw))
2448 lt = getLyXText(bv);
2451 Paragraph * lpar = lt->cursor.par();
2452 Paragraph::size_type pos = lt->cursor.pos();
2453 if (pos < lpar->size() - 1)
2457 lpar = lpar->next();
2462 // we have to unlock ourself in this function by default!
2463 bv->unlockInset(const_cast<InsetText *>(this));
2466 lt->setCursor(bv, lpar, pos);
2470 if (LyXFind(bv, str, true, true, cs , mw)) {
2473 // we have to unlock ourself in this function by default!
2474 bv->unlockInset(const_cast<InsetText *>(this));
2478 bool InsetText::searchBackward(BufferView * bv, string const & str,
2479 bool const & cs, bool const & mw)
2481 if (the_locking_inset)
2482 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2484 if (LyXFind(bv, str, false, true, cs, mw)) {
2487 // we have to unlock ourself in this function by default!
2488 bv->unlockInset(const_cast<InsetText *>(this));
2493 bool InsetText::checkInsertChar(LyXFont & font)
2496 return owner()->checkInsertChar(font);