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 "frontends/Alert.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"
66 extern unsigned char getCurrentTextClass(Buffer *);
67 extern bool math_insert_greek(BufferView *, char);
68 extern int greek_kb_flag;
71 // These functions should probably go into bufferview_funcs somehow (Jug)
73 void InsetText::saveLyXTextState(LyXText * t) const
75 // check if my paragraphs are still valid
78 if (p == t->cursor.par())
83 if (p && t->cursor.pos() <= p->size()) {
84 sstate.lpar = t->cursor.par();
85 sstate.pos = t->cursor.pos();
86 sstate.boundary = t->cursor.boundary();
87 sstate.selstartpar = t->selection.start.par();
88 sstate.selstartpos = t->selection.start.pos();
89 sstate.selstartboundary = t->selection.start.boundary();
90 sstate.selendpar = t->selection.end.par();
91 sstate.selendpos = t->selection.end.pos();
92 sstate.selendboundary = t->selection.end.boundary();
93 sstate.selection = t->selection.set();
94 sstate.mark_set = t->selection.mark();
95 sstate.refresh = t->refresh_row != 0;
101 void InsetText::restoreLyXTextState(BufferView * bv, LyXText * t) const
104 t->selection.set(true);
105 /* at this point just to avoid the Delete-Empty-Paragraph
106 * Mechanism when setting the cursor */
107 t->selection.mark(sstate.mark_set);
108 if (sstate.selection) {
109 t->setCursor(bv, sstate.selstartpar, sstate.selstartpos,
110 true, sstate.selstartboundary);
111 t->selection.cursor = t->cursor;
112 t->setCursor(bv, sstate.selendpar, sstate.selendpos,
113 true, sstate.selendboundary);
115 t->setCursor(bv, sstate.lpar, sstate.pos);
117 t->setCursor(bv, sstate.lpar, sstate.pos, true, sstate.boundary);
118 t->selection.cursor = t->cursor;
119 t->selection.set(false);
121 if (sstate.refresh) {
127 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
134 InsetText::InsetText()
135 : UpdatableInset(), lt(0), in_update(false), do_resize(0),
143 InsetText::InsetText(InsetText const & in, bool same_id)
144 : UpdatableInset(in, same_id), lt(0), in_update(false), do_resize(0),
152 InsetText & InsetText::operator=(InsetText const & it)
159 void InsetText::init(InsetText const * ins, bool same_id)
162 setParagraphData(ins->par);
163 autoBreakRows = ins->autoBreakRows;
164 drawFrame_ = ins->drawFrame_;
165 frame_color = ins->frame_color;
171 p->setInsetOwner(this);
174 the_locking_inset = 0;
176 frame_color = LColor::insetframe;
177 autoBreakRows = false;
186 no_selection = false;
193 last_drawn_width = -1;
194 frame_is_visible = false;
200 InsetText::~InsetText()
207 Paragraph * tmp = par->next();
214 void InsetText::clear()
217 Paragraph * tmp = par->next();
227 Inset * InsetText::clone(Buffer const &, bool same_id) const
229 return new InsetText(*this, same_id);
233 void InsetText::write(Buffer const * buf, ostream & os) const
236 writeParagraphData(buf, os);
240 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
242 par->writeFile(buf, os, buf->params, 0);
246 void InsetText::read(Buffer const * buf, LyXLex & lex)
250 Paragraph * return_par = 0;
251 Paragraph::depth_type depth = 0;
252 LyXFont font(LyXFont::ALL_INHERIT);
258 token = lex.getString();
261 if (token == "\\end_inset") {
262 #ifndef NO_COMPABILITY
263 const_cast<Buffer*>(buf)->insertErtContents(par, pos, font, false);
268 if (const_cast<Buffer*>(buf)->
269 parseSingleLyXformat2Token(lex, par, return_par,
270 token, pos, depth, font)) {
271 // the_end read this should NEVER happen
272 lex.printError("\\the_end read in inset! Error in document!");
280 return_par->setInsetOwner(this);
281 return_par = return_par->next();
284 if (token != "\\end_inset") {
285 lex.printError("Missing \\end_inset at this point. "
292 int InsetText::ascent(BufferView * bv, LyXFont const &) const
299 insetAscent = lt->firstRow()->ascent_of_text() + TEXT_TO_INSET_OFFSET;
306 int InsetText::descent(BufferView * bv, LyXFont const &) const
313 insetDescent = lt->height - lt->firstRow()->ascent_of_text() +
314 TEXT_TO_INSET_OFFSET;
321 int InsetText::width(BufferView * bv, LyXFont const &) const
323 insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
324 (2 * TEXT_TO_INSET_OFFSET);
325 insetWidth = max(insetWidth, 10);
330 int InsetText::textWidth(BufferView * bv, bool fordraw) const
333 if (!autoBreakRows) {
336 w = getMaxWidth(bv, this);
339 return max(w - (2 * TEXT_TO_INSET_OFFSET),
340 (int)getLyXText(bv)->width);
344 return w - (2 * TEXT_TO_INSET_OFFSET);
348 void InsetText::draw(BufferView * bv, LyXFont const & f,
349 int baseline, float & x, bool cleared) const
354 Painter & pain = bv->painter();
356 // this is the first thing we have to ask because if the x pos
357 // changed we have to do a complete rebreak of the text as we
358 // may have few space to draw in. Well we should check on this too
360 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);
374 // repaint the background if needed
375 if (cleared && backgroundColor() != LColor::background) {
377 clearInset(pain, baseline, cleared);
381 // no draw is necessary !!!
382 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
385 top_baseline = baseline;
387 if (need_update & CLEAR_FRAME)
388 clearFrame(pain, cleared);
395 x += static_cast<float>(scroll());
397 // if top_x differs we did it already
398 if (!cleared && (top_x == int(x)) &&
399 ((need_update&(INIT|FULL)) || (top_baseline!=baseline) ||
400 (last_drawn_width!=insetWidth))) {
401 clearInset(pain, 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 - ascent(bv, f);
417 last_width = width(bv, f);
418 last_height = ascent(bv, f) + descent(bv, f);
420 if (cleared || (last_drawn_width != insetWidth)) {
422 clearInset(pain, baseline, cleared);
424 last_drawn_width = insetWidth;
427 if (the_locking_inset && (cpar(bv) == inset_par)
428 && (cpos(bv) == inset_pos)) {
429 inset_x = cx(bv) - top_x + drawTextXOffset;
430 inset_y = cy(bv) + drawTextYOffset;
432 if (!cleared && (need_update == CURSOR)
433 && !getLyXText(bv)->selection.set()) {
434 drawFrame(pain, cleared);
435 x += last_width; // was width(bv, f);
444 x += TEXT_TO_INSET_OFFSET;
446 Row * row = lt->firstRow();
447 int y_offset = baseline - row->ascent_of_text();
448 int ph = pain.paperHeight();
451 while ((row != 0) && ((y+row->height()) <= 0)) {
453 first += row->height();
459 if (cleared || (need_update&(INIT|FULL))) {
462 while ((row != 0) && (yf < ph)) {
463 lt->getVisibleRow(bv, y+y_offset, int(x), row,
469 } else if (!locked) {
470 if (need_update & CURSOR) {
471 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
472 lt->clearSelection();
473 lt->selection.cursor = lt->cursor;
475 bv->screen()->update(lt, bv, y_offset, int(x));
478 if (need_update & SELECTION) {
479 bv->screen()->toggleToggle(lt, bv, y_offset, int(x));
480 } else if (need_update & CURSOR) {
481 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
482 lt->clearSelection();
483 lt->selection.cursor = lt->cursor;
485 bv->screen()->update(lt, bv, y_offset, int(x));
490 lt->status(bv, LyXText::UNCHANGED);
491 if ((need_update != CURSOR_PAR) &&
492 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked))) {
493 drawFrame(pain, cleared);
494 } else if (need_update & CLEAR_FRAME) {
495 clearFrame(pain, cleared);
498 x += last_width /* was width(bv, f) */ - TEXT_TO_INSET_OFFSET;
500 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
502 } else if (need_update != INIT) {
511 void InsetText::drawFrame(Painter & pain, bool cleared) const
513 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
514 if (!frame_is_visible || cleared) {
515 frame_x = top_x + ttoD2;
516 frame_y = top_baseline - insetAscent + ttoD2;
517 frame_w = last_width - TEXT_TO_INSET_OFFSET;
518 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
519 pain.rectangle(frame_x, frame_y,
522 frame_is_visible = true;
527 void InsetText::clearFrame(Painter & pain, bool cleared) const
529 if (frame_is_visible) {
531 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
534 frame_is_visible = false;
539 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
542 if (reinit && owner()) {
544 owner()->update(bv, font, true);
549 if (reinit || need_update == INIT) {
551 // we should put this call where we set need_update to INIT!
554 owner()->update(bv, font, true);
558 if (the_locking_inset) {
559 inset_x = cx(bv) - top_x + drawTextXOffset;
560 inset_y = cy(bv) + drawTextYOffset;
561 the_locking_inset->update(bv, font, reinit);
570 int oldw = insetWidth;
571 insetWidth = lt->width + (2 * TEXT_TO_INSET_OFFSET);
572 if (oldw != insetWidth) {
581 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
584 lt->updateInset(bv, the_locking_inset);
586 if (lt->status() == LyXText::NEED_MORE_REFRESH)
594 void InsetText::setUpdateStatus(BufferView * bv, int what) const
602 // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
603 // if we don't break row so that we only have one row to update!
604 if ((lt->status() == LyXText::NEED_MORE_REFRESH) ||
606 (lt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
609 } else if (lt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
610 need_update |= CURSOR_PAR;
613 // this to not draw a selection when we redraw all of it!
614 if (need_update & CURSOR && !(need_update & SELECTION)) {
615 if (lt->selection.set())
617 lt->clearSelection();
624 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
632 setUpdateStatus(bv, what);
633 if (((need_update != CURSOR) && (need_update != NONE)) ||
634 (lt->status() != LyXText::UNCHANGED) || lt->selection.set())
636 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
638 if (need_update == CURSOR)
640 bv->owner()->showState();
641 if (old_par != cpar(bv)) {
642 bv->owner()->setLayout(cpar(bv)->getLayout());
650 string const InsetText::editMessage() const
652 return _("Opened Text Inset");
656 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
658 UpdatableInset::edit(bv, x, y, button);
660 if (!bv->lockInset(this)) {
661 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
665 the_locking_inset = 0;
666 inset_pos = inset_x = inset_y = 0;
667 inset_boundary = false;
670 int tmp_y = (y < 0) ? 0 : y;
677 if (!checkAndActivateInset(bv, x, tmp_y, button))
678 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
680 lt->clearSelection();
683 updateLocal(bv, CURSOR, false);
685 // If the inset is empty set the language of the current font to the
686 // language to the surronding text (if different).
687 if (par->size() == 0 && !par->next() &&
688 bv->getParentLanguage(this) != lt->current_font.language()) {
689 LyXFont font(LyXFont::ALL_IGNORE);
690 font.setLanguage(bv->getParentLanguage(this));
691 setFont(bv, font, false);
698 void InsetText::edit(BufferView * bv, bool front)
700 UpdatableInset::edit(bv, front);
702 if (!bv->lockInset(this)) {
703 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
707 the_locking_inset = 0;
708 inset_pos = inset_x = inset_y = 0;
709 inset_boundary = false;
718 lt->setCursor(bv, par, 0);
723 // int const pos = (p->size() ? p->size()-1 : p->size());
724 lt->setCursor(bv, p, p->size());
726 lt->clearSelection();
729 updateLocal(bv, CURSOR, false);
731 // If the inset is empty set the language of the current font to the
732 // language to the surronding text (if different).
733 if (par->size() == 0 && !par->next() &&
734 bv->getParentLanguage(this) != lt->current_font.language()) {
735 LyXFont font(LyXFont::ALL_IGNORE);
736 font.setLanguage(bv->getParentLanguage(this));
737 setFont(bv, font, false);
744 void InsetText::insetUnlock(BufferView * bv)
746 if (the_locking_inset) {
747 the_locking_inset->insetUnlock(bv);
748 the_locking_inset = 0;
751 no_selection = false;
754 if (drawFrame_ == LOCKED)
755 code = CURSOR|CLEAR_FRAME;
763 if (lt->selection.set()) {
764 lt->clearSelection();
766 } else if (owner()) {
767 bv->owner()->setLayout(owner()->getLyXText(bv)
768 ->cursor.par()->getLayout());
770 bv->owner()->setLayout(bv->text->cursor.par()->getLayout());
771 // hack for deleteEmptyParMech
772 lt->setCursor(bv, par, 0);
773 updateLocal(bv, code, false);
779 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
781 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
785 if (inset == cpar(bv)->getInset(cpos(bv))) {
786 lyxerr[Debug::INSETS] << "OK" << endl;
787 the_locking_inset = inset;
788 inset_x = cx(bv) - top_x + drawTextXOffset;
789 inset_y = cy(bv) + drawTextYOffset;
790 inset_pos = cpos(bv);
791 inset_par = cpar(bv);
792 inset_boundary = cboundary(bv);
793 updateLocal(bv, CURSOR, false);
795 } else if (the_locking_inset && (the_locking_inset == inset)) {
796 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
797 lyxerr[Debug::INSETS] << "OK" << endl;
798 inset_x = cx(bv) - top_x + drawTextXOffset;
799 inset_y = cy(bv) + drawTextYOffset;
801 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
803 } else if (the_locking_inset) {
804 lyxerr[Debug::INSETS] << "MAYBE" << endl;
805 return the_locking_inset->lockInsetInInset(bv, inset);
807 lyxerr[Debug::INSETS] << "NOT OK" << endl;
812 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
815 if (!the_locking_inset)
817 if (the_locking_inset == inset) {
818 the_locking_inset->insetUnlock(bv);
819 getLyXText(bv)->updateInset(bv, inset);
820 the_locking_inset = 0;
822 moveRight(bv, false);
823 old_par = 0; // force layout setting
827 updateLocal(bv, CURSOR, false);
830 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
834 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
836 if (!the_locking_inset)
838 if (the_locking_inset != inset) {
839 getLyXText(bv)->updateInset(bv, the_locking_inset);
840 setUpdateStatus(bv, CURSOR_PAR);
841 return the_locking_inset->updateInsetInInset(bv, inset);
843 if (getLyXText(bv)->updateInset(bv, inset))
844 updateLocal(bv, CURSOR_PAR, false);
845 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
846 inset_x = cx(bv) - top_x + drawTextXOffset;
847 inset_y = cy(bv) + drawTextYOffset;
853 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
857 int tmp_x = x - drawTextXOffset;
858 int tmp_y = y + insetAscent - getLyXText(bv)->first;
859 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y, button);
862 if (the_locking_inset) {
863 if (the_locking_inset == inset) {
864 the_locking_inset->insetButtonPress(bv,x-inset_x,y-inset_y,button);
865 no_selection = false;
868 // otherwise unlock the_locking_inset and lock the new inset
869 the_locking_inset->insetUnlock(bv);
870 inset_x = cx(bv) - top_x + drawTextXOffset;
871 inset_y = cy(bv) + drawTextYOffset;
872 the_locking_inset = static_cast<UpdatableInset*>(inset);
873 inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
874 inset->edit(bv, x - inset_x, y - inset_y, button);
875 if (the_locking_inset)
876 updateLocal(bv, CURSOR, false);
877 no_selection = false;
880 // otherwise only unlock the_locking_inset
881 the_locking_inset->insetUnlock(bv);
882 the_locking_inset = 0;
884 if (bv->theLockingInset()) {
885 if (isHighlyEditableInset(inset)) {
886 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
887 inset_x = cx(bv) - top_x + drawTextXOffset;
888 inset_y = cy(bv) + drawTextYOffset;
889 inset_pos = cpos(bv);
890 inset_par = cpar(bv);
891 inset_boundary = cboundary(bv);
892 the_locking_inset = uinset;
893 uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
895 uinset->edit(bv, x - inset_x, y - inset_y, 0);
896 if (the_locking_inset)
897 updateLocal(bv, CURSOR, false);
898 no_selection = false;
902 if (!inset) { // && (button == 2)) {
903 bool paste_internally = false;
904 if ((button == 2) && getLyXText(bv)->selection.set()) {
905 localDispatch(bv, LFUN_COPY, "");
906 paste_internally = true;
913 lt->setCursorFromCoordinates(bv, x-drawTextXOffset, y + insetAscent);
914 if (lt->selection.set()) {
915 lt->clearSelection();
916 updateLocal(bv, FULL, false);
918 lt->clearSelection();
920 bv->owner()->setLayout(cpar(bv)->getLayout());
922 // Insert primary selection with middle mouse
923 // if there is a local selection in the current buffer,
926 if (paste_internally)
927 localDispatch(bv, LFUN_PASTE, "");
929 localDispatch(bv, LFUN_PASTESELECTION,
936 no_selection = false;
940 void InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
942 UpdatableInset * inset = 0;
944 if (the_locking_inset) {
945 the_locking_inset->insetButtonRelease(bv,
946 x - inset_x, y - inset_y,
949 if (cpar(bv)->isInset(cpos(bv))) {
950 inset = static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
951 if (isHighlyEditableInset(inset)) {
952 inset->insetButtonRelease(bv,
954 y - inset_y, button);
956 inset_x = cx(bv) - top_x + drawTextXOffset;
957 inset_y = cy(bv) + drawTextYOffset;
958 inset->insetButtonRelease(bv,
960 y - inset_y, button);
962 x - inset_x, y - inset_y, button);
964 updateLocal(bv, CURSOR_PAR, false);
967 no_selection = false;
971 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
975 if (the_locking_inset) {
976 the_locking_inset->insetMotionNotify(bv, x - inset_x,
980 LyXText * t = getLyXText(bv);
982 t->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
984 if (t->toggle_cursor.par() != t->toggle_end_cursor.par() ||
985 t->toggle_cursor.pos() != t->toggle_end_cursor.pos())
986 updateLocal(bv, SELECTION, false);
991 void InsetText::insetKeyPress(XKeyEvent * xke)
993 if (the_locking_inset) {
994 the_locking_inset->insetKeyPress(xke);
1000 UpdatableInset::RESULT
1001 InsetText::localDispatch(BufferView * bv,
1002 kb_action action, string const & arg)
1004 bool was_empty = par->size() == 0 && !par->next();
1005 no_selection = false;
1006 UpdatableInset::RESULT
1007 result= UpdatableInset::localDispatch(bv, action, arg);
1008 if (result != UNDISPATCHED) {
1012 result = DISPATCHED;
1013 if ((action < 0) && arg.empty())
1016 if (the_locking_inset) {
1017 result = the_locking_inset->localDispatch(bv, action, arg);
1018 if (result == DISPATCHED_NOUPDATE)
1020 else if (result == DISPATCHED) {
1021 updateLocal(bv, CURSOR_PAR, false);
1023 } else if (result >= FINISHED) {
1025 case FINISHED_RIGHT:
1026 moveRightIntern(bv, false, false);
1027 result = DISPATCHED;
1030 if ((result = moveUp(bv)) >= FINISHED) {
1031 updateLocal(bv, CURSOR, false);
1032 bv->unlockInset(this);
1036 if ((result = moveDown(bv)) >= FINISHED) {
1037 updateLocal(bv, CURSOR, false);
1038 bv->unlockInset(this);
1042 result = DISPATCHED;
1045 the_locking_inset = 0;
1046 #ifdef WITH_WARNINGS
1047 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
1052 hideInsetCursor(bv);
1055 lt = getLyXText(bv);
1060 case LFUN_SELFINSERT:
1061 if (bv->buffer()->isReadonly()) {
1062 // setErrorMessage(N_("Document is read only"));
1066 /* Automatically delete the currently selected
1067 * text and replace it with what is being
1068 * typed in now. Depends on lyxrc settings
1069 * "auto_region_delete", which defaults to
1072 setUndo(bv, Undo::INSERT,
1073 lt->cursor.par(), lt->cursor.par()->next());
1075 if (lyxrc.auto_region_delete) {
1076 if (lt->selection.set()) {
1077 lt->cutSelection(bv, false);
1080 lt->clearSelection();
1081 for (string::size_type i = 0; i < arg.length(); ++i) {
1082 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1085 lt->selection.cursor = lt->cursor;
1086 updateLocal(bv, CURSOR_PAR, true);
1087 result = DISPATCHED_NOUPDATE;
1089 // --- Cursor Movements -----------------------------------
1092 moveRight(bv, false, true);
1093 lt->setSelection(bv);
1094 updateLocal(bv, SELECTION, false);
1097 result = moveRight(bv);
1099 updateLocal(bv, CURSOR, false);
1103 moveLeft(bv, false, true);
1104 lt->setSelection(bv);
1105 updateLocal(bv, SELECTION, false);
1109 result = moveLeft(bv);
1110 updateLocal(bv, CURSOR, false);
1115 lt->setSelection(bv);
1116 updateLocal(bv, SELECTION, false);
1120 result = moveDown(bv);
1121 updateLocal(bv, CURSOR, false);
1126 lt->setSelection(bv);
1127 updateLocal(bv, SELECTION, false);
1131 result = moveUp(bv);
1132 updateLocal(bv, CURSOR, false);
1137 updateLocal(bv, CURSOR, false);
1141 updateLocal(bv, CURSOR, false);
1143 case LFUN_BACKSPACE: {
1144 setUndo(bv, Undo::DELETE,
1145 lt->cursor.par(), lt->cursor.par()->next());
1146 if (lt->selection.set())
1147 lt->cutSelection(bv);
1150 updateLocal(bv, CURSOR_PAR, true);
1155 setUndo(bv, Undo::DELETE,
1156 lt->cursor.par(), lt->cursor.par()->next());
1157 if (lt->selection.set()) {
1158 lt->cutSelection(bv);
1162 updateLocal(bv, CURSOR_PAR, true);
1167 setUndo(bv, Undo::DELETE,
1168 lt->cursor.par(), lt->cursor.par()->next());
1169 lt->cutSelection(bv);
1170 updateLocal(bv, CURSOR_PAR, true);
1176 lt->copySelection(bv);
1177 updateLocal(bv, CURSOR_PAR, false);
1179 case LFUN_PASTESELECTION:
1181 string const clip(bv->getClipboard());
1185 if (arg == "paragraph") {
1186 lt->insertStringAsParagraphs(bv, clip);
1188 lt->insertStringAsLines(bv, clip);
1190 updateLocal(bv, CURSOR_PAR, true);
1194 if (!autoBreakRows) {
1196 if (CutAndPaste::nrOfParagraphs() > 1) {
1197 Alert::alert(_("Impossible operation"),
1198 _("Cannot include more than one paragraph!"),
1203 setUndo(bv, Undo::INSERT,
1204 lt->cursor.par(), lt->cursor.par()->next());
1205 lt->pasteSelection(bv);
1206 updateLocal(bv, CURSOR_PAR, true);
1210 case LFUN_BREAKPARAGRAPH:
1211 if (!autoBreakRows) {
1212 result = DISPATCHED;
1215 lt->breakParagraph(bv, 0);
1216 updateLocal(bv, FULL, true);
1218 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1219 if (!autoBreakRows) {
1220 result = DISPATCHED;
1223 lt->breakParagraph(bv, 1);
1224 updateLocal(bv, FULL, true);
1227 case LFUN_BREAKLINE: {
1228 if (!autoBreakRows) {
1229 result = DISPATCHED;
1232 setUndo(bv, Undo::INSERT,
1233 lt->cursor.par(), lt->cursor.par()->next());
1234 lt->insertChar(bv, Paragraph::META_NEWLINE);
1235 updateLocal(bv, CURSOR_PAR, true);
1240 // do not set layouts on non breakable textinsets
1241 if (autoBreakRows) {
1242 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1244 // Derive layout number from given argument (string)
1245 // and current buffer's textclass (number). */
1246 LyXTextClassList::ClassList::size_type tclass =
1247 bv->buffer()->params.textclass;
1248 std::pair <bool, LyXTextClass::size_type> layout =
1249 textclasslist.NumberOfLayout(tclass, arg);
1251 // If the entry is obsolete, use the new one instead.
1253 string obs = textclasslist.Style(tclass,layout.second).
1256 layout = textclasslist.NumberOfLayout(tclass, obs);
1259 // see if we found the layout number:
1260 if (!layout.first) {
1261 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1262 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1266 if (cur_layout != layout.second) {
1267 cur_layout = layout.second;
1268 lt->setLayout(bv, layout.second);
1269 bv->owner()->setLayout(cpar(bv)->getLayout());
1270 updateLocal(bv, CURSOR_PAR, true);
1273 // reset the layout box
1274 bv->owner()->setLayout(cpar(bv)->getLayout());
1277 case LFUN_PARAGRAPH_SPACING:
1278 // This one is absolutely not working. When fiddling with this
1279 // it also seems to me that the paragraphs inside the insettext
1280 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1282 Paragraph * par = lt->cursor.par();
1283 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1284 float cur_value = 1.0;
1285 if (cur_spacing == Spacing::Other) {
1286 cur_value = par->params().spacing().getValue();
1289 istringstream istr(arg.c_str());
1292 Spacing::Space new_spacing = cur_spacing;
1293 float new_value = cur_value;
1295 lyxerr << "Missing argument to `paragraph-spacing'"
1297 } else if (tmp == "single") {
1298 new_spacing = Spacing::Single;
1299 } else if (tmp == "onehalf") {
1300 new_spacing = Spacing::Onehalf;
1301 } else if (tmp == "double") {
1302 new_spacing = Spacing::Double;
1303 } else if (tmp == "other") {
1304 new_spacing = Spacing::Other;
1307 lyxerr << "new_value = " << tmpval << endl;
1310 } else if (tmp == "default") {
1311 new_spacing = Spacing::Default;
1313 lyxerr << _("Unknown spacing argument: ")
1316 if (cur_spacing != new_spacing || cur_value != new_value) {
1317 par->params().spacing(Spacing(new_spacing, new_value));
1318 updateLocal(bv, CURSOR_PAR, true);
1324 if (!bv->Dispatch(action, arg))
1325 result = UNDISPATCHED;
1329 /// If the action has deleted all text in the inset, we need to change the
1330 // language to the language of the surronding text.
1331 if (!was_empty && par->size() == 0 && !par->next()) {
1332 LyXFont font(LyXFont::ALL_IGNORE);
1333 font.setLanguage(bv->getParentLanguage(this));
1334 setFont(bv, font, false);
1337 if (result < FINISHED) {
1338 showInsetCursor(bv);
1340 bv->unlockInset(this);
1347 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1350 buf->latexParagraphs(os, par, 0, texrow);
1351 return texrow.rows();
1355 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1357 Paragraph * p = par;
1358 unsigned int lines = 0;
1361 string const tmp = buf->asciiParagraph(p, linelen);
1362 lines += countChar(tmp, '\n');
1370 int InsetText::docbook(Buffer const * buf, ostream & os) const
1372 Paragraph * p = par;
1373 unsigned int lines = 0;
1375 vector<string> environment_stack(10);
1376 vector<string> environment_inner(10);
1378 int const command_depth = 0;
1381 Paragraph::depth_type depth = 0; // paragraph depth
1385 int desc_on = 0; // description mode
1387 LyXLayout const & style =
1388 textclasslist.Style(buf->params.textclass,
1391 // environment tag closing
1392 for (; depth > p->params().depth(); --depth) {
1393 if (environment_inner[depth] != "!-- --") {
1394 item_name = "listitem";
1395 buf->sgmlCloseTag(os, command_depth + depth,
1397 if (environment_inner[depth] == "varlistentry")
1398 buf->sgmlCloseTag(os, depth+command_depth,
1399 environment_inner[depth]);
1401 buf->sgmlCloseTag(os, depth + command_depth,
1402 environment_stack[depth]);
1403 environment_stack[depth].erase();
1404 environment_inner[depth].erase();
1407 if (depth == p->params().depth()
1408 && environment_stack[depth] != style.latexname()
1409 && !environment_stack[depth].empty()) {
1410 if (environment_inner[depth] != "!-- --") {
1411 item_name= "listitem";
1412 buf->sgmlCloseTag(os, command_depth+depth,
1414 if (environment_inner[depth] == "varlistentry")
1415 buf->sgmlCloseTag(os,
1416 depth + command_depth,
1417 environment_inner[depth]);
1420 buf->sgmlCloseTag(os, depth + command_depth,
1421 environment_stack[depth]);
1423 environment_stack[depth].erase();
1424 environment_inner[depth].erase();
1427 // Write opening SGML tags.
1428 switch (style.latextype) {
1429 case LATEX_PARAGRAPH:
1430 buf->sgmlOpenTag(os, depth + command_depth,
1435 buf->sgmlError(p, 0,
1436 _("Error : LatexType Command not allowed here.\n"));
1440 case LATEX_ENVIRONMENT:
1441 case LATEX_ITEM_ENVIRONMENT:
1442 if (depth < p->params().depth()) {
1443 depth = p->params().depth();
1444 environment_stack[depth].erase();
1447 if (environment_stack[depth] != style.latexname()) {
1448 if(environment_stack.size() == depth + 1) {
1449 environment_stack.push_back("!-- --");
1450 environment_inner.push_back("!-- --");
1452 environment_stack[depth] = style.latexname();
1453 environment_inner[depth] = "!-- --";
1454 buf->sgmlOpenTag(os, depth + command_depth,
1455 environment_stack[depth]);
1457 if (environment_inner[depth] != "!-- --") {
1458 item_name= "listitem";
1459 buf->sgmlCloseTag(os,
1460 command_depth + depth,
1462 if (environment_inner[depth] == "varlistentry")
1463 buf->sgmlCloseTag(os,
1464 depth + command_depth,
1465 environment_inner[depth]);
1469 if (style.latextype == LATEX_ENVIRONMENT) {
1470 if (!style.latexparam().empty()) {
1471 if(style.latexparam() == "CDATA")
1474 buf->sgmlOpenTag(os, depth + command_depth,
1475 style.latexparam());
1480 desc_on = (style.labeltype == LABEL_MANUAL);
1483 environment_inner[depth]= "varlistentry";
1485 environment_inner[depth]= "listitem";
1487 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1488 environment_inner[depth]);
1492 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1496 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1501 buf->sgmlOpenTag(os, depth + command_depth,
1506 buf->simpleDocBookOnePar(os, p, desc_on,
1507 depth + 1 + command_depth);
1511 // write closing SGML tags
1512 switch (style.latextype) {
1513 case LATEX_ENVIRONMENT:
1514 if (!style.latexparam().empty()) {
1515 if(style.latexparam() == "CDATA")
1518 buf->sgmlCloseTag(os, depth + command_depth,
1519 style.latexparam());
1522 case LATEX_ITEM_ENVIRONMENT:
1523 if (desc_on == 1) break;
1525 buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1527 case LATEX_PARAGRAPH:
1528 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1531 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1537 for (int d = depth; d >= 0; --d) {
1538 if (!environment_stack[depth].empty()) {
1539 if (environment_inner[depth] != "!-- --") {
1540 item_name = "listitem";
1541 buf->sgmlCloseTag(os, command_depth + depth,
1543 if (environment_inner[depth] == "varlistentry")
1544 buf->sgmlCloseTag(os, depth + command_depth,
1545 environment_inner[depth]);
1548 buf->sgmlCloseTag(os, depth + command_depth,
1549 environment_stack[depth]);
1557 void InsetText::validate(LaTeXFeatures & features) const
1559 Paragraph * p = par;
1561 p->validate(features);
1567 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1569 if (textclasslist.Style(buf->params.textclass,
1570 p->getLayout()).labeltype != LABEL_MANUAL)
1573 return p->beginningOfMainBody();
1577 void InsetText::getCursorPos(BufferView * bv,
1578 int & x, int & y) const
1580 if (the_locking_inset) {
1581 the_locking_inset->getCursorPos(bv, x, y);
1589 unsigned int InsetText::insetInInsetY()
1591 if (!the_locking_inset)
1594 return (inset_y + the_locking_inset->insetInInsetY());
1598 void InsetText::toggleInsetCursor(BufferView * bv)
1600 if (the_locking_inset) {
1601 the_locking_inset->toggleInsetCursor(bv);
1605 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1607 int const asc = lyxfont::maxAscent(font);
1608 int const desc = lyxfont::maxDescent(font);
1610 if (isCursorVisible())
1611 bv->hideLockedInsetCursor();
1613 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1614 toggleCursorVisible();
1618 void InsetText::showInsetCursor(BufferView * bv, bool show)
1620 if (the_locking_inset) {
1621 the_locking_inset->showInsetCursor(bv, show);
1624 if (!isCursorVisible()) {
1625 LyXFont const font =
1626 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1628 int const asc = lyxfont::maxAscent(font);
1629 int const desc = lyxfont::maxDescent(font);
1631 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1633 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1634 setCursorVisible(true);
1639 void InsetText::hideInsetCursor(BufferView * bv)
1641 if (isCursorVisible()) {
1642 bv->hideLockedInsetCursor();
1643 setCursorVisible(false);
1645 if (the_locking_inset)
1646 the_locking_inset->hideInsetCursor(bv);
1650 void InsetText::fitInsetCursor(BufferView * bv) const
1652 if (the_locking_inset) {
1653 the_locking_inset->fitInsetCursor(bv);
1656 LyXFont const font =
1657 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1659 int const asc = lyxfont::maxAscent(font);
1660 int const desc = lyxfont::maxDescent(font);
1662 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1666 UpdatableInset::RESULT
1667 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1669 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1670 return moveLeftIntern(bv, false, activate_inset, selecting);
1672 return moveRightIntern(bv, false, activate_inset, selecting);
1676 UpdatableInset::RESULT
1677 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1679 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1680 return moveRightIntern(bv, true, activate_inset, selecting);
1682 return moveLeftIntern(bv, true, activate_inset, selecting);
1686 UpdatableInset::RESULT
1687 InsetText::moveRightIntern(BufferView * bv, bool behind,
1688 bool activate_inset, bool selecting)
1690 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1691 return FINISHED_RIGHT;
1692 if (activate_inset && checkAndActivateInset(bv, behind))
1694 getLyXText(bv)->cursorRight(bv);
1696 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1697 return DISPATCHED_NOUPDATE;
1701 UpdatableInset::RESULT
1702 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1703 bool activate_inset, bool selecting)
1705 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1707 getLyXText(bv)->cursorLeft(bv);
1709 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1710 if (activate_inset && checkAndActivateInset(bv, behind))
1712 return DISPATCHED_NOUPDATE;
1716 UpdatableInset::RESULT
1717 InsetText::moveUp(BufferView * bv)
1719 if (!crow(bv)->previous())
1721 getLyXText(bv)->cursorUp(bv);
1722 return DISPATCHED_NOUPDATE;
1726 UpdatableInset::RESULT
1727 InsetText::moveDown(BufferView * bv)
1729 if (!crow(bv)->next())
1730 return FINISHED_DOWN;
1731 getLyXText(bv)->cursorDown(bv);
1732 return DISPATCHED_NOUPDATE;
1736 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1738 if (the_locking_inset) {
1739 if (the_locking_inset->insetAllowed(inset))
1740 return the_locking_inset->insertInset(bv, inset);
1743 inset->setOwner(this);
1744 hideInsetCursor(bv);
1748 lt = getLyXText(bv);
1751 lt->insertInset(bv, inset);
1753 if ((!cpar(bv)->isInset(cpos(bv))) ||
1754 (cpar(bv)->getInset(cpos(bv)) != inset))
1758 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1759 showInsetCursor(bv);
1766 bool InsetText::insetAllowed(Inset::Code code) const
1768 if (the_locking_inset)
1769 return the_locking_inset->insetAllowed(code);
1774 UpdatableInset * InsetText::getLockingInset() const
1776 return the_locking_inset ? the_locking_inset->getLockingInset() :
1777 const_cast<InsetText *>(this);
1781 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1785 if (the_locking_inset)
1786 return the_locking_inset->getFirstLockingInsetOfType(c);
1791 bool InsetText::showInsetDialog(BufferView * bv) const
1793 if (the_locking_inset)
1794 return the_locking_inset->showInsetDialog(bv);
1799 std::vector<string> const InsetText::getLabelList() const
1801 std::vector<string> label_list;
1803 Paragraph * tpar = par;
1805 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1806 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1807 for (; beg != end; ++beg) {
1808 std::vector<string> const l = (*beg)->getLabelList();
1809 label_list.insert(label_list.end(), l.begin(), l.end());
1811 tpar = tpar->next();
1817 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1820 if (the_locking_inset) {
1821 the_locking_inset->setFont(bv, font, toggleall, selectall);
1824 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
1825 getLyXText(bv)->setFont(bv, font, toggleall);
1830 lt = getLyXText(bv);
1833 if (lt->selection.set()) {
1834 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1838 lt->toggleFree(bv, font, toggleall);
1840 lt->clearSelection();
1842 if (selectall || lt->selection.set())
1843 updateLocal(bv, FULL, true);
1845 updateLocal(bv, CURSOR_PAR, true);
1851 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1853 if (cpar(bv)->isInset(cpos(bv))) {
1857 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1858 if (!isHighlyEditableInset(inset))
1860 LyXFont const font =
1861 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1863 x = inset->width(bv, font);
1864 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1867 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1869 //inset_x = cx(bv) - top_x + drawTextXOffset;
1870 //inset_y = cy(bv) + drawTextYOffset;
1871 inset->edit(bv, x, y, 0);
1872 if (!the_locking_inset)
1874 updateLocal(bv, CURSOR, false);
1881 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1884 x -= drawTextXOffset;
1886 int dummyy = y + insetAscent;
1887 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy, button);
1894 inset_x = cx(bv) - top_x + drawTextXOffset;
1895 inset_y = cy(bv) + drawTextYOffset;
1896 inset->edit(bv, x - inset_x, y - inset_y, button);
1897 if (!the_locking_inset)
1899 updateLocal(bv, CURSOR, false);
1906 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1909 int w = UpdatableInset::getMaxWidth(bv, inset);
1914 w = w - top_x + owner()->x();
1917 w -= (2 * TEXT_TO_INSET_OFFSET);
1920 return UpdatableInset::getMaxWidth(bv, inset);
1925 void InsetText::setParagraphData(Paragraph * p)
1927 // we have to unlock any locked inset otherwise we're in troubles
1928 the_locking_inset = 0;
1930 Paragraph * tmp = par->next();
1935 par = new Paragraph(*p, false);
1936 par->setInsetOwner(this);
1937 Paragraph * np = par;
1940 np->next(new Paragraph(*p, false));
1941 np->next()->previous(np);
1943 np->setInsetOwner(this);
1950 void InsetText::setText(string const & data)
1953 LyXFont font(LyXFont::ALL_SANE);
1954 for (unsigned int i=0; i < data.length(); ++i)
1955 par->insertChar(i, data[i], font);
1959 void InsetText::setAutoBreakRows(bool flag)
1961 if (flag != autoBreakRows) {
1962 autoBreakRows = flag;
1970 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1972 if (how != drawFrame_) {
1975 updateLocal(bv, DRAW_FRAME, false);
1980 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1982 if (frame_color != col) {
1985 updateLocal(bv, DRAW_FRAME, false);
1990 int InsetText::cx(BufferView * bv) const
1994 lt = getLyXText(bv);
1997 int x = lt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1998 if (the_locking_inset) {
1999 LyXFont font = lt->getFont(bv->buffer(),
2002 if (font.isVisibleRightToLeft())
2003 x -= the_locking_inset->width(bv, font);
2011 int InsetText::cy(BufferView * bv) const
2014 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2018 pos_type InsetText::cpos(BufferView * bv) const
2020 return getLyXText(bv)->cursor.pos();
2024 Paragraph * InsetText::cpar(BufferView * bv) const
2026 return getLyXText(bv)->cursor.par();
2030 bool InsetText::cboundary(BufferView * bv) const
2032 return getLyXText(bv)->cursor.boundary();
2036 Row * InsetText::crow(BufferView * bv) const
2038 return getLyXText(bv)->cursor.row();
2042 LyXText * InsetText::getLyXText(BufferView const * lbv,
2043 bool const recursive) const
2045 if (!recursive && (cached_bview == lbv)) {
2046 LyXText * lt = cached_text.get();
2047 lyx::Assert(lt && lt->firstRow()->par() == par);
2048 return cached_text.get();
2051 // Super UGLY! (Lgb)
2052 BufferView * bv = const_cast<BufferView *>(lbv);
2055 Cache::iterator it = cache.find(bv);
2057 if (it != cache.end()) {
2061 resizeLyXText(do_resize);
2062 if (lt || !it->second.remove) {
2063 lyx::Assert(it->second.text.get());
2064 cached_text = it->second.text;
2065 if (recursive && the_locking_inset) {
2066 return the_locking_inset->getLyXText(bv, true);
2068 return cached_text.get();
2069 } else if (it->second.remove) {
2071 saveLyXTextState(it->second.text.get());
2078 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2079 cached_text->init(bv);
2080 restoreLyXTextState(bv, cached_text.get());
2082 cache.insert(make_pair(bv, cached_text));
2084 if (the_locking_inset && recursive) {
2085 return the_locking_inset->getLyXText(bv);
2087 return cached_text.get();
2091 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2095 Cache::iterator it = cache.find(bv);
2097 if (it == cache.end()) {
2101 lyx::Assert(it->second.text.get());
2103 it->second.remove = true;
2105 /// then remove all LyXText in text-insets
2106 Paragraph * p = par;
2107 for (; p; p = p->next()) {
2108 p->deleteInsetsLyXText(bv);
2114 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2117 // we cannot resize this because we are in use!
2118 // so do this on the next possible getLyXText()
2123 // lyxerr << "InsetText::resizeLyXText\n";
2124 if (!par->next() && !par->size()) // no data, resize not neccessary!
2126 // one endless line, resize normally not necessary
2127 if (!force && getMaxWidth(bv, this) < 0)
2130 Cache::iterator it = cache.find(bv);
2131 if (it == cache.end()) {
2134 lyx::Assert(it->second.text.get());
2136 LyXText * t = it->second.text.get();
2137 saveLyXTextState(t);
2138 for (Paragraph * p = par; p; p = p->next()) {
2139 p->resizeInsetsLyXText(bv);
2142 restoreLyXTextState(bv, t);
2143 if (the_locking_inset) {
2144 inset_x = cx(bv) - top_x + drawTextXOffset;
2145 inset_y = cy(bv) + drawTextYOffset;
2149 t->first = bv->screen()->topCursorVisible(t);
2152 updateLocal(bv, FULL, false);
2153 // this will scroll the screen such that the cursor becomes visible
2154 bv->updateScrollbar();
2156 need_update |= FULL;
2161 void InsetText::reinitLyXText() const
2164 // we cannot resize this because we are in use!
2165 // so do this on the next possible getLyXText()
2171 // lyxerr << "InsetText::reinitLyXText\n";
2172 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2173 lyx::Assert(it->second.text.get());
2175 LyXText * t = it->second.text.get();
2176 BufferView * bv = it->first;
2178 saveLyXTextState(t);
2179 for (Paragraph * p = par; p; p = p->next()) {
2180 p->resizeInsetsLyXText(bv);
2183 restoreLyXTextState(bv, t);
2184 if (the_locking_inset) {
2185 inset_x = cx(bv) - top_x + drawTextXOffset;
2186 inset_y = cy(bv) + drawTextYOffset;
2189 t->first = bv->screen()->topCursorVisible(t);
2192 updateLocal(bv, FULL, false);
2193 // this will scroll the screen such that the cursor becomes visible
2194 bv->updateScrollbar();
2202 void InsetText::removeNewlines()
2204 for (Paragraph * p = par; p; p = p->next()) {
2205 for (int i = 0; i < p->size(); ++i) {
2206 if (p->getChar(i) == Paragraph::META_NEWLINE)
2213 bool InsetText::nodraw() const
2215 if (the_locking_inset)
2216 return the_locking_inset->nodraw();
2217 return UpdatableInset::nodraw();
2221 int InsetText::scroll(bool recursive) const
2223 int sx = UpdatableInset::scroll(false);
2225 if (recursive && the_locking_inset)
2226 sx += the_locking_inset->scroll(recursive);
2232 bool InsetText::doClearArea() const
2234 return !locked || (need_update & (FULL|INIT));
2238 void InsetText::selectAll(BufferView * bv)
2240 getLyXText(bv)->cursorTop(bv);
2241 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2242 getLyXText(bv)->cursorBottom(bv);
2243 getLyXText(bv)->setSelection(bv);
2247 void InsetText::clearSelection(BufferView * bv)
2249 getLyXText(bv)->clearSelection();
2253 void InsetText::clearInset(Painter & pain, int baseline, bool & cleared) const
2256 int h = insetAscent + insetDescent;
2257 int ty = baseline - insetAscent;
2263 if ((ty + h) > pain.paperHeight())
2264 h = pain.paperHeight();
2265 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2266 w = pain.paperWidth();
2267 // w -= TEXT_TO_INSET_OFFSET;
2268 pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2271 frame_is_visible = false;
2275 Paragraph * InsetText::getParFromID(int id) const
2278 Paragraph * result = par;
2279 Paragraph * ires = 0;
2280 while (result && result->id() != id) {
2281 if ((ires = result->getParFromID(id)))
2283 result = result->next();
2287 Paragraph * tmp = par;
2289 int tmp_id = tmp->id();
2290 lyxerr << "Looking at paragraph: " << tmp_id << endl;
2291 if (tmp->id() == id) {
2294 Paragraph * tmp2 = tmp->getParFromID(id);
2305 Paragraph * InsetText::firstParagraph() const
2308 if (the_locking_inset)
2309 if ((result = the_locking_inset->firstParagraph()))
2315 Paragraph * InsetText::getFirstParagraph(int i) const
2317 return (i == 0) ? par : 0;
2321 LyXCursor const & InsetText::cursor(BufferView * bv) const
2323 if (the_locking_inset)
2324 return the_locking_inset->cursor(bv);
2325 return getLyXText(bv)->cursor;
2329 Paragraph * InsetText::paragraph() const
2335 void InsetText::paragraph(Paragraph * p)
2338 // set ourself as owner for all the paragraphs inserted!
2339 Paragraph * np = par;
2341 np->setInsetOwner(this);
2345 // redraw myself when asked for
2350 Inset * InsetText::getInsetFromID(int id_arg) const
2353 return const_cast<InsetText *>(this);
2355 Paragraph * lp = par;
2358 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2359 en = lp->inset_iterator_end();
2362 if ((*it)->id() == id_arg)
2364 Inset * in = (*it)->getInsetFromID(id_arg);
2374 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2380 lt = getLyXText(bv);
2383 if (the_locking_inset) {
2384 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2391 #warning Dekel please have a look on this one RTL? (Jug)
2393 // we have to go on checking so move cusor to the right
2394 lt->cursor.pos(lt->cursor.pos() + 1);
2396 str = lt->selectNextWordToSpellcheck(bv, value);
2398 bv->unlockInset(const_cast<InsetText *>(this));
2407 void InsetText::selectSelectedWord(BufferView * bv)
2409 if (the_locking_inset) {
2410 the_locking_inset->selectSelectedWord(bv);
2413 getLyXText(bv)->selectSelectedWord(bv);
2414 updateLocal(bv, SELECTION, false);
2418 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2420 if (the_locking_inset) {
2421 the_locking_inset->toggleSelection(bv, kill_selection);
2425 lt = getLyXText(bv);
2429 int x = top_x + TEXT_TO_INSET_OFFSET;
2431 Row * row = lt->firstRow();
2432 int y_offset = top_baseline - row->ascent_of_text();
2434 while ((row != 0) && ((y+row->height()) <= 0)) {
2441 if (need_update & SELECTION)
2443 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2449 bool InsetText::searchForward(BufferView * bv, string const & str,
2450 bool const & cs, bool const & mw)
2452 if (the_locking_inset) {
2453 if (the_locking_inset->searchForward(bv, str, cs, mw))
2457 lt = getLyXText(bv);
2460 Paragraph * lpar = lt->cursor.par();
2461 pos_type pos = lt->cursor.pos();
2462 if (pos < lpar->size() - 1)
2466 lpar = lpar->next();
2471 // we have to unlock ourself in this function by default!
2472 bv->unlockInset(const_cast<InsetText *>(this));
2475 lt->setCursor(bv, lpar, pos);
2479 if (LyXFind(bv, str, true, true, cs , mw)) {
2482 // we have to unlock ourself in this function by default!
2483 bv->unlockInset(const_cast<InsetText *>(this));
2487 bool InsetText::searchBackward(BufferView * bv, string const & str,
2488 bool const & cs, bool const & mw)
2490 if (the_locking_inset)
2491 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2493 if (LyXFind(bv, str, false, true, cs, mw)) {
2496 // we have to unlock ourself in this function by default!
2497 bv->unlockInset(const_cast<InsetText *>(this));
2502 bool InsetText::checkInsertChar(LyXFont & font)
2505 return owner()->checkInsertChar(font);