2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1998-2000 The LyX Team.
9 * ======================================================
20 #pragma implementation
23 #include "insettext.h"
24 #include "lyxparagraph.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"
40 #include "minibuffer.h"
42 #include "support/textutils.h"
43 #include "support/LAssert.h"
47 #include "trans_mgr.h"
48 #include "lyxscreen.h"
57 extern unsigned char getCurrentTextClass(Buffer *);
59 InsetText::InsetText()
61 par = new LyXParagraph();
66 InsetText::InsetText(InsetText const & ins)
71 autoBreakRows = ins.autoBreakRows;
75 InsetText & InsetText::operator=(InsetText const & it)
78 autoBreakRows = it.autoBreakRows;
83 void InsetText::init(InsetText const * ins)
91 the_locking_inset = 0;
92 cursor_visible = false;
98 autoBreakRows = false;
102 SetParagraphData(ins->par);
103 autoBreakRows = ins->autoBreakRows;
104 drawFrame = ins->drawFrame;
106 par->SetInsetOwner(this);
107 frame_color = LColor::insetframe;
113 InsetText::~InsetText()
115 // delete all instances of LyXText before deleting the paragraps used
117 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
118 delete (*cit).second;
121 LyXParagraph * p = par->next;
131 void InsetText::clear()
133 // delete all instances of LyXText before deleting the paragraps used
135 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
136 delete (*cit).second;
139 LyXParagraph * p = par->next;
146 par = new LyXParagraph();
150 Inset * InsetText::Clone(Buffer const &) const
152 InsetText * t = new InsetText(*this);
157 void InsetText::Write(Buffer const * buf, ostream & os) const
160 WriteParagraphData(buf, os);
164 void InsetText::WriteParagraphData(Buffer const * buf, ostream & os) const
166 par->writeFile(buf, os, buf->params, 0, 0);
170 void InsetText::Read(Buffer const * buf, LyXLex & lex)
174 LyXParagraph * return_par = 0;
175 char depth = 0; // signed or unsigned?
177 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
178 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
180 LyXFont font(LyXFont::ALL_INHERIT);
182 // delete all instances of LyXText before deleting the paragraps used
184 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
185 delete (*cit).second;
189 LyXParagraph * p = par->next;
196 par = new LyXParagraph;
199 token = lex.GetString();
202 if (token == "\\end_inset")
204 if (const_cast<Buffer*>(buf)->
205 parseSingleLyXformat2Token(lex, par, return_par,token, pos, depth,
208 , footnoteflag, footnotekind
212 // the_end read this should NEVER happen
213 lex.printError("\\the_end read in inset! Error in document!");
221 return_par->SetInsetOwner(this);
222 return_par = return_par->next;
225 if (token != "\\end_inset") {
226 lex.printError("Missing \\end_inset at this point. "
233 int InsetText::ascent(BufferView * bv, LyXFont const &) const
236 Row * row = TEXT(bv)->GetRowNearY(y_temp);
237 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
242 int InsetText::descent(BufferView * bv, LyXFont const &) const
245 Row * row = TEXT(bv)->GetRowNearY(y_temp);
246 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
247 TEXT_TO_INSET_OFFSET;
252 int InsetText::width(BufferView * bv, LyXFont const &) const
254 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
259 int InsetText::textWidth(Painter & pain) const
261 int w = getMaxWidth(pain, this);
266 void InsetText::draw(BufferView * bv, LyXFont const & f,
267 int baseline, float & x, bool cleared) const
269 Painter & pain = bv->painter();
271 // no draw is necessary !!!
272 if ((drawFrame == LOCKED) && !locked && !par->size()) {
273 if (!cleared && (need_update == CLEAR_FRAME)) {
274 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
276 insetAscent + insetDescent - 1,
280 top_baseline = baseline;
287 UpdatableInset::draw(bv, f, baseline, x, cleared);
289 if (!cleared && ((need_update==FULL) || (top_x!=int(x)) ||
290 (top_baseline!=baseline))) {
292 int h = insetAscent + insetDescent;
293 int ty = baseline - insetAscent;
299 if ((ty + h) > pain.paperHeight())
300 h = pain.paperHeight();
301 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
302 w = pain.paperWidth();
303 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
307 if (!cleared && (need_update == NONE))
310 if (top_x != int(x)) {
313 bv->text->status = LyXText::CHANGED_IN_DRAW;
317 top_baseline = baseline;
318 top_y = baseline - ascent(bv, f);
319 last_width = width(bv, f);
320 last_height = ascent(bv, f) + descent(bv, f);
322 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
323 inset_x = cx(bv) - top_x + drawTextXOffset;
324 inset_y = cy(bv) + drawTextYOffset;
326 if (!cleared && (need_update == CURSOR) && !TEXT(bv)->selection) {
331 x += TEXT_TO_INSET_OFFSET;
334 Row * row = TEXT(bv)->GetRowNearY(y);
335 int y_offset = baseline - row->ascent_of_text();
336 int ph = pain.paperHeight();
339 while ((row != 0) && ((y+row->height()) <= 0)) {
341 first += row->height();
346 TEXT(bv)->first = first;
347 if (cleared || !locked || (need_update == FULL)) {
350 while ((row != 0) && (yf < ph)) {
351 TEXT(bv)->GetVisibleRow(bv, y+y_offset, int(x), row,
357 } else if (need_update == SELECTION) {
358 bv->screen()->ToggleToggle(TEXT(bv), y_offset, int(x));
361 if (need_update == CURSOR) {
362 bv->screen()->ToggleSelection(TEXT(bv), true, y_offset,int(x));
363 TEXT(bv)->ClearSelection();
364 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
366 bv->screen()->Update(TEXT(bv), y_offset, int(x));
370 TEXT(bv)->refresh_y = 0;
371 TEXT(bv)->status = LyXText::UNCHANGED;
372 if ((need_update != CURSOR_PAR) &&
373 ((drawFrame == ALWAYS) || ((drawFrame == LOCKED) && locked)))
375 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
376 width(bv, f) - 1, insetAscent + insetDescent - 1,
378 } else if (need_update == CLEAR_FRAME) {
379 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
380 width(bv, f) - 1, insetAscent + insetDescent - 1,
383 x += width(bv, f) - TEXT_TO_INSET_OFFSET;
384 if (bv->text->status==LyXText::CHANGED_IN_DRAW)
386 else if (need_update != INIT)
391 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
393 if (reinit) { // && (need_update != CURSOR)) {
397 owner()->update(bv, font, true);
400 if (the_locking_inset) {
401 inset_x = cx(bv) - top_x + drawTextXOffset;
402 inset_y = cy(bv) + drawTextYOffset;
403 the_locking_inset->update(bv, font, reinit);
405 if (need_update == INIT) {
408 // if (!owner() && bv->text)
409 // bv->text->UpdateInset(bv, this);
411 int oldw = insetWidth;
413 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
414 // max(textWidth(bv->painter()),
415 // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
416 // (2 * TEXT_TO_INSET_OFFSET);
418 insetWidth = textWidth(bv->painter());
420 insetWidth = static_cast<int>(TEXT(bv)->width);
422 if (oldw != insetWidth) {
423 // printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
424 // textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
429 owner()->update(bv, font, reinit);
432 update(bv, font, reinit);
436 update(bv, font, reinit);
438 UpdateLocal(bv, INIT, false);
443 if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
446 TEXT(bv)->UpdateInset(bv, the_locking_inset);
449 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
453 Row * row = TEXT(bv)->GetRowNearY(y_temp);
454 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
455 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
456 TEXT_TO_INSET_OFFSET;
460 void InsetText::UpdateLocal(BufferView * bv, UpdateCodes what, bool mark_dirty)
462 TEXT(bv)->FullRebreak(bv);
463 if (need_update != INIT) {
464 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
466 else if (!the_locking_inset || (what != CURSOR))
469 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
471 bv->updateInset(this, mark_dirty);
472 bv->owner()->showState();
473 if (old_par != cpar(bv)) {
474 bv->owner()->setLayout(cpar(bv)->GetLayout());
480 string const InsetText::EditMessage() const
482 return _("Opened Text Inset");
486 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
488 // par->SetInsetOwner(this);
489 UpdatableInset::Edit(bv, x, y, button);
491 if (!bv->lockInset(this)) {
492 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
496 the_locking_inset = 0;
497 inset_pos = inset_x = inset_y = 0;
500 if (!checkAndActivateInset(bv, x, y, button))
501 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
503 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
504 bv->text->FinishUndo();
506 UpdateLocal(bv, FULL, false);
510 void InsetText::InsetUnlock(BufferView * bv)
512 if (the_locking_inset) {
513 the_locking_inset->InsetUnlock(bv);
514 the_locking_inset = 0;
517 no_selection = false;
519 TEXT(bv)->selection = 0;
520 UpdateLocal(bv, CLEAR_FRAME, false);
522 bv->owner()->setLayout(owner()->getLyXText(bv)
523 ->cursor.par()->GetLayout());
525 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
529 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
531 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
534 if (inset == cpar(bv)->GetInset(cpos(bv))) {
535 lyxerr[Debug::INSETS] << "OK" << endl;
536 the_locking_inset = inset;
537 inset_x = cx(bv) - top_x + drawTextXOffset;
538 inset_y = cy(bv) + drawTextYOffset;
539 inset_pos = cpos(bv);
540 inset_par = cpar(bv);
541 TEXT(bv)->UpdateInset(bv, the_locking_inset);
543 } else if (the_locking_inset && (the_locking_inset == inset)) {
544 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
545 lyxerr[Debug::INSETS] << "OK" << endl;
546 inset_x = cx(bv) - top_x + drawTextXOffset;
547 inset_y = cy(bv) + drawTextYOffset;
549 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
551 } else if (the_locking_inset) {
552 lyxerr[Debug::INSETS] << "MAYBE" << endl;
553 return the_locking_inset->LockInsetInInset(bv, inset);
555 lyxerr[Debug::INSETS] << "NOT OK" << endl;
560 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
563 if (!the_locking_inset)
565 if (the_locking_inset == inset) {
566 the_locking_inset->InsetUnlock(bv);
567 TEXT(bv)->UpdateInset(bv, inset);
568 the_locking_inset = 0;
570 moveRight(bv, false);
571 old_par = 0; // force layout setting
572 UpdateLocal(bv, CURSOR_PAR, false);
575 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
579 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
581 if (!the_locking_inset)
583 if (the_locking_inset != inset) {
584 TEXT(bv)->UpdateInset(bv, the_locking_inset);
585 need_update = CURSOR_PAR;
586 return the_locking_inset->UpdateInsetInInset(bv, inset);
588 // UpdateLocal(bv, FULL, false);
589 if (TEXT(bv)->UpdateInset(bv, inset))
590 UpdateLocal(bv, CURSOR_PAR, false);
591 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
592 inset_x = cx(bv) - top_x + drawTextXOffset;
593 inset_y = cy(bv) + drawTextYOffset;
599 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
601 no_selection = false;
603 int tmp_x = x - drawTextXOffset;
604 int tmp_y = y + insetAscent - TEXT(bv)->first;
605 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
608 if (the_locking_inset) {
609 if (the_locking_inset == inset) {
610 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
613 // otherwise unlock the_locking_inset and lock the new inset
614 the_locking_inset->InsetUnlock(bv);
615 inset_x = cx(bv) - top_x + drawTextXOffset;
616 inset_y = cy(bv) + drawTextYOffset;
617 the_locking_inset = static_cast<UpdatableInset*>(inset);
618 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
619 inset->Edit(bv, x - inset_x, y - inset_y, button);
620 if (the_locking_inset) {
621 UpdateLocal(bv, CURSOR_PAR, false);
625 // otherwise only unlock the_locking_inset
626 the_locking_inset->InsetUnlock(bv);
627 the_locking_inset = 0;
629 if (bv->theLockingInset()) {
630 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
631 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
632 inset_x = cx(bv) - top_x + drawTextXOffset;
633 inset_y = cy(bv) + drawTextYOffset;
634 inset_pos = cpos(bv);
635 inset_par = cpar(bv);
636 the_locking_inset = uinset;
637 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
638 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
639 // TEXT(bv)->ClearSelection();
640 if (the_locking_inset) {
641 UpdateLocal(bv, CURSOR_PAR, false);
647 bool paste_internally = false;
648 if ((button == 2) && TEXT(bv)->selection) {
649 LocalDispatch(bv, LFUN_COPY, "");
650 paste_internally = true;
652 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
654 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
655 UpdateLocal(bv, CURSOR, false);
656 bv->owner()->setLayout(cpar(bv)->GetLayout());
658 // Insert primary selection with middle mouse
659 // if there is a local selection in the current buffer,
662 if (paste_internally)
663 LocalDispatch(bv, LFUN_PASTE, "");
665 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
672 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
674 UpdatableInset * inset = 0;
676 if (the_locking_inset) {
677 the_locking_inset->InsetButtonRelease(bv,
678 x - inset_x, y - inset_y,
681 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
682 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
683 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
684 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
686 inset_x = cx(bv) - top_x + drawTextXOffset;
687 inset_y = cy(bv) + drawTextYOffset;
688 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
689 inset->Edit(bv, x - inset_x, y - inset_y, button);
691 UpdateLocal(bv, CURSOR_PAR, false);
694 no_selection = false;
698 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
700 if (the_locking_inset) {
701 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
707 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
709 TEXT(bv)->SetSelection();
710 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
711 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
712 UpdateLocal(bv, SELECTION, false);
715 no_selection = false;
719 void InsetText::InsetKeyPress(XKeyEvent * xke)
721 if (the_locking_inset) {
722 the_locking_inset->InsetKeyPress(xke);
728 UpdatableInset::RESULT
729 InsetText::LocalDispatch(BufferView * bv,
730 int action, string const & arg)
732 no_selection = false;
733 UpdatableInset::RESULT
734 result= UpdatableInset::LocalDispatch(bv, action, arg);
735 if (result != UNDISPATCHED) {
740 if ((action < 0) && arg.empty())
743 if (the_locking_inset) {
744 result = the_locking_inset->LocalDispatch(bv, action, arg);
745 if (result == DISPATCHED_NOUPDATE)
747 else if (result == DISPATCHED) {
748 UpdateLocal(bv, CURSOR_PAR, false);
750 } else if (result == FINISHED) {
754 moveRight(bv, false);
760 the_locking_inset = 0;
768 if (bv->buffer()->isReadonly()) {
770 // setErrorMessage(N_("Document is read only"));
774 /* Automatically delete the currently selected
775 * text and replace it with what is being
776 * typed in now. Depends on lyxrc settings
777 * "auto_region_delete", which defaults to
780 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
782 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
783 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
785 bv->text->cursor.par()->previous,
786 bv->text->cursor.par()->next
789 // if an empty paragraph set the language to the surronding
790 // paragraph language on insertion of the first character!
791 if (!par->Last() && !par->next) {
794 Inset * inset = owner();
795 while(inset && inset->getLyXText(bv) == TEXT(bv))
796 inset = inset->owner();
798 text = inset->getLyXText(bv);
802 LyXFont font(LyXFont::ALL_IGNORE);
803 font.setLanguage(text->cursor.par()->getParLanguage(bv->buffer()->params));
804 SetFont(bv, font, false);
806 if (lyxrc.auto_region_delete) {
807 if (TEXT(bv)->selection){
808 TEXT(bv)->CutSelection(bv, false);
811 TEXT(bv)->ClearSelection();
812 for (string::size_type i = 0; i < arg.length(); ++i) {
813 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
816 UpdateLocal(bv, CURSOR_PAR, true);
817 result=DISPATCHED_NOUPDATE;
819 // --- Cursor Movements ---------------------------------------------
821 bv->text->FinishUndo();
822 moveRight(bv, false, true);
823 TEXT(bv)->SetSelection();
824 UpdateLocal(bv, SELECTION, false);
827 result = moveRight(bv);
828 bv->text->FinishUndo();
829 TEXT(bv)->ClearSelection();
830 UpdateLocal(bv, CURSOR, false);
833 bv->text->FinishUndo();
834 moveLeft(bv, false, true);
835 TEXT(bv)->SetSelection();
836 UpdateLocal(bv, SELECTION, false);
839 bv->text->FinishUndo();
840 result= moveLeft(bv);
841 TEXT(bv)->ClearSelection();
842 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
843 UpdateLocal(bv, CURSOR, false);
846 bv->text->FinishUndo();
848 TEXT(bv)->SetSelection();
849 UpdateLocal(bv, SELECTION, false);
852 bv->text->FinishUndo();
853 result = moveDown(bv);
854 TEXT(bv)->ClearSelection();
855 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
856 UpdateLocal(bv, CURSOR, false);
859 bv->text->FinishUndo();
861 TEXT(bv)->SetSelection();
862 UpdateLocal(bv, SELECTION, false);
865 bv->text->FinishUndo();
867 TEXT(bv)->ClearSelection();
868 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
869 UpdateLocal(bv, CURSOR, false);
872 bv->text->FinishUndo();
873 TEXT(bv)->CursorHome(bv);
874 TEXT(bv)->ClearSelection();
875 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
876 UpdateLocal(bv, CURSOR, false);
879 TEXT(bv)->CursorEnd(bv);
880 TEXT(bv)->ClearSelection();
881 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
882 UpdateLocal(bv, CURSOR, false);
885 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
887 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
888 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
890 bv->text->cursor.par()->previous,
891 bv->text->cursor.par()->next
894 if (TEXT(bv)->selection)
895 TEXT(bv)->CutSelection(bv);
897 TEXT(bv)->Backspace(bv);
898 UpdateLocal(bv, CURSOR_PAR, true);
901 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
903 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
904 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
906 bv->text->cursor.par()->previous,
907 bv->text->cursor.par()->next
910 if (TEXT(bv)->selection)
911 TEXT(bv)->CutSelection(bv);
913 TEXT(bv)->Delete(bv);
914 UpdateLocal(bv, CURSOR_PAR, true);
917 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
919 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
920 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
922 bv->text->cursor.par()->previous,
923 bv->text->cursor.par()->next
926 TEXT(bv)->CutSelection(bv);
927 UpdateLocal(bv, CURSOR_PAR, true);
930 bv->text->FinishUndo();
931 TEXT(bv)->CopySelection(bv);
932 UpdateLocal(bv, CURSOR_PAR, false);
934 case LFUN_PASTESELECTION:
936 string clip(bv->workarea()->getClipboard());
940 if (arg == "paragraph") {
941 TEXT(bv)->InsertStringB(bv, clip);
943 TEXT(bv)->InsertStringA(bv, clip);
945 UpdateLocal(bv, CURSOR_PAR, true);
949 if (!autoBreakRows) {
952 if (cap.nrOfParagraphs() > 1) {
953 WriteAlert(_("Impossible operation"),
954 _("Cannot include more than one paragraph!"),
959 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
961 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
962 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
964 bv->text->cursor.par()->previous,
965 bv->text->cursor.par()->next
968 TEXT(bv)->PasteSelection(bv);
969 UpdateLocal(bv, CURSOR_PAR, true);
971 case LFUN_BREAKPARAGRAPH:
974 TEXT(bv)->BreakParagraph(bv, 0);
975 UpdateLocal(bv, FULL, true);
980 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
982 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
983 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
985 bv->text->cursor.par()->previous,
986 bv->text->cursor.par()->next
989 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
990 UpdateLocal(bv, CURSOR_PAR, true);
993 // do not set layouts on non breakable textinsets
995 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
997 // Derive layout number from given argument (string)
998 // and current buffer's textclass (number). */
999 LyXTextClassList::ClassList::size_type tclass =
1000 bv->buffer()->params.textclass;
1001 std::pair <bool, LyXTextClass::size_type> layout =
1002 textclasslist.NumberOfLayout(tclass, arg);
1004 // If the entry is obsolete, use the new one instead.
1006 string obs = textclasslist.Style(tclass,layout.second).
1009 layout = textclasslist.NumberOfLayout(tclass, obs);
1012 // see if we found the layout number:
1013 if (!layout.first) {
1014 string msg = string(N_("Layout ")) + arg + N_(" not known");
1016 bv->owner()->getMiniBuffer()->Set(msg);
1020 if (cur_layout != layout.second) {
1021 cur_layout = layout.second;
1022 TEXT(bv)->SetLayout(bv, layout.second);
1023 bv->owner()->setLayout(cpar(bv)->GetLayout());
1024 UpdateLocal(bv, CURSOR_PAR, true);
1027 // reset the layout box
1028 bv->owner()->setLayout(cpar(bv)->GetLayout());
1031 case LFUN_PARAGRAPH_SPACING:
1032 // This one is absolutely not working. When fiddling with this
1033 // it also seems to me that the paragraphs inside the insettext
1034 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1036 LyXParagraph * par = TEXT(bv)->cursor.par();
1037 Spacing::Space cur_spacing = par->spacing.getSpace();
1038 float cur_value = 1.0;
1039 if (cur_spacing == Spacing::Other) {
1040 cur_value = par->spacing.getValue();
1043 std::istringstream istr(arg.c_str());
1046 Spacing::Space new_spacing = cur_spacing;
1047 float new_value = cur_value;
1049 lyxerr << "Missing argument to `paragraph-spacing'"
1051 } else if (tmp == "single") {
1052 new_spacing = Spacing::Single;
1053 } else if (tmp == "onehalf") {
1054 new_spacing = Spacing::Onehalf;
1055 } else if (tmp == "double") {
1056 new_spacing = Spacing::Double;
1057 } else if (tmp == "other") {
1058 new_spacing = Spacing::Other;
1061 lyxerr << "new_value = " << tmpval << endl;
1064 } else if (tmp == "default") {
1065 new_spacing = Spacing::Default;
1067 lyxerr << _("Unknown spacing argument: ")
1070 if (cur_spacing != new_spacing || cur_value != new_value) {
1071 par->spacing.set(new_spacing, new_value);
1072 //TEXT(bv)->RedoParagraph(owner->view());
1073 UpdateLocal(bv, CURSOR_PAR, true);
1074 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1080 result = UNDISPATCHED;
1083 if (result != FINISHED) {
1084 ShowInsetCursor(bv);
1086 bv->unlockInset(this);
1091 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1094 buf->latexParagraphs(os, par, 0, texrow);
1095 return texrow.rows();
1099 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1101 LyXParagraph * p = par;
1102 unsigned int lines = 0;
1106 tmp = buf->asciiParagraph(p, linelen);
1107 lines += countChar(tmp, '\n');
1115 int InsetText::DocBook(Buffer const * buf, ostream & os) const
1117 LyXParagraph * p = par;
1118 unsigned int lines = 0;
1123 buf->SimpleDocBookOnePar(os,tmp,p,desc,0);
1131 void InsetText::Validate(LaTeXFeatures & features) const
1133 LyXParagraph * p = par;
1135 p->validate(features);
1141 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1143 if (textclasslist.Style(buf->params.textclass,
1144 p->GetLayout()).labeltype != LABEL_MANUAL)
1147 return p->BeginningOfMainBody();
1151 void InsetText::GetCursorPos(BufferView * bv,
1152 int & x, int & y) const
1159 unsigned int InsetText::InsetInInsetY()
1161 if (!the_locking_inset)
1164 return (inset_y + the_locking_inset->InsetInInsetY());
1168 void InsetText::ToggleInsetCursor(BufferView * bv)
1170 if (the_locking_inset) {
1171 the_locking_inset->ToggleInsetCursor(bv);
1175 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1177 int asc = lyxfont::maxAscent(font);
1178 int desc = lyxfont::maxDescent(font);
1181 bv->hideLockedInsetCursor();
1183 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1184 cursor_visible = !cursor_visible;
1188 void InsetText::ShowInsetCursor(BufferView * bv, bool show)
1190 if (the_locking_inset) {
1191 the_locking_inset->ShowInsetCursor(bv);
1194 if (!cursor_visible) {
1195 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1197 int asc = lyxfont::maxAscent(font);
1198 int desc = lyxfont::maxDescent(font);
1200 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1202 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1203 cursor_visible = true;
1208 void InsetText::HideInsetCursor(BufferView * bv)
1210 if (cursor_visible) {
1211 bv->hideLockedInsetCursor();
1212 cursor_visible = false;
1214 if (the_locking_inset)
1215 the_locking_inset->HideInsetCursor(bv);
1219 UpdatableInset::RESULT
1220 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1222 if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1224 if (activate_inset && checkAndActivateInset(bv, false))
1226 TEXT(bv)->CursorRight(bv, selecting);
1227 return DISPATCHED_NOUPDATE;
1231 UpdatableInset::RESULT
1232 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1234 if (!cpar(bv)->previous && (cpos(bv) <= 0))
1236 TEXT(bv)->CursorLeft(bv, selecting);
1237 if (activate_inset && checkAndActivateInset(bv, true))
1239 return DISPATCHED_NOUPDATE;
1243 UpdatableInset::RESULT
1244 InsetText::moveUp(BufferView * bv)
1246 if (!crow(bv)->previous())
1248 TEXT(bv)->CursorUp(bv);
1249 return DISPATCHED_NOUPDATE;
1253 UpdatableInset::RESULT
1254 InsetText::moveDown(BufferView * bv)
1256 if (!crow(bv)->next())
1258 TEXT(bv)->CursorDown(bv);
1259 return DISPATCHED_NOUPDATE;
1263 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1265 if (the_locking_inset) {
1266 if (the_locking_inset->InsertInsetAllowed(inset))
1267 return the_locking_inset->InsertInset(bv, inset);
1270 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1272 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1273 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1275 bv->text->cursor.par()->previous,
1276 bv->text->cursor.par()->next
1279 inset->setOwner(this);
1280 HideInsetCursor(bv);
1281 TEXT(bv)->InsertInset(bv, inset);
1282 if ((cpar(bv)->GetChar(cpos(bv)) != LyXParagraph::META_INSET) ||
1283 (cpar(bv)->GetInset(cpos(bv)) != inset))
1284 TEXT(bv)->CursorLeft(bv);
1285 TEXT(bv)->selection = 0;
1286 bv->fitCursor(TEXT(bv));
1287 UpdateLocal(bv, CURSOR_PAR, true);
1288 ShowInsetCursor(bv);
1293 UpdatableInset * InsetText::GetLockingInset()
1295 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1299 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1303 if (the_locking_inset)
1304 return the_locking_inset->GetFirstLockingInsetOfType(c);
1309 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1311 bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1313 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1314 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1316 bv->text->cursor.par()->previous,
1317 bv->text->cursor.par()->next
1320 TEXT(bv)->SetFont(bv, font, toggleall);
1321 bv->fitCursor(TEXT(bv));
1322 UpdateLocal(bv, CURSOR_PAR, true);
1326 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1328 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1332 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1333 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1335 LyXFont const font =
1336 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1338 x = inset->width(bv, font);
1339 y = inset->descent(bv, font);
1343 inset_x = cx(bv) - top_x + drawTextXOffset;
1344 inset_y = cy(bv) + drawTextYOffset;
1345 inset->Edit(bv, x - inset_x, y - inset_y, 0);
1346 if (!the_locking_inset)
1348 UpdateLocal(bv, CURSOR_PAR, false);
1355 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1358 x = x - drawTextXOffset;
1359 y = y + insetAscent;
1360 Inset * inset = bv->checkInsetHit(TEXT(bv), x, y, button);
1367 inset_x = cx(bv) - top_x + drawTextXOffset;
1368 inset_y = cy(bv) + drawTextYOffset;
1369 inset->Edit(bv, x - inset_x, y - inset_y, button);
1370 if (!the_locking_inset)
1372 UpdateLocal(bv, CURSOR_PAR, false);
1379 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1381 int w = UpdatableInset::getMaxWidth(pain, inset);
1386 w = w - top_x + owner()->x();
1389 w -= (2 * TEXT_TO_INSET_OFFSET);
1391 // return w - (2*TEXT_TO_INSET_OFFSET);
1395 void InsetText::SetParagraphData(LyXParagraph *p)
1397 // delete all instances of LyXText before deleting the paragraps used
1399 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
1400 delete (*cit).second;
1415 par->SetInsetOwner(this);
1419 np->next = p->Clone();
1420 np->next->previous = np;
1422 np->SetInsetOwner(this);
1428 void InsetText::SetAutoBreakRows(bool flag)
1430 if (flag != autoBreakRows) {
1431 autoBreakRows = flag;
1439 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1441 if (how != drawFrame) {
1444 UpdateLocal(bv, DRAW_FRAME, false);
1449 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1451 if (frame_color != col) {
1454 UpdateLocal(bv, DRAW_FRAME, false);
1459 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1461 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1466 int InsetText::cx(BufferView * bv) const
1468 return TEXT(bv)->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1472 int InsetText::cy(BufferView * bv) const
1475 return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1479 LyXParagraph::size_type InsetText::cpos(BufferView * bv) const
1481 return TEXT(bv)->cursor.pos();
1485 LyXParagraph * InsetText::cpar(BufferView * bv) const
1487 return TEXT(bv)->cursor.par();
1491 Row * InsetText::crow(BufferView * bv) const
1493 return TEXT(bv)->cursor.row();
1497 LyXText * InsetText::getLyXText(BufferView * bv) const
1499 if ((cache.find(bv) != cache.end()) && cache[bv])
1501 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1504 if (the_locking_inset) {
1505 lt->SetCursor(bv, inset_par, inset_pos);
1511 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1513 if (cache.find(bv) == cache.end())
1518 /// then remove all LyXText in text-insets
1519 LyXParagraph * p = par;
1520 for (;p;p = p->next) {
1521 p->deleteInsetsLyXText(bv);
1527 void InsetText::resizeLyXText(BufferView * bv) const
1529 if (!par->next && !par->size()) // resize not neccessary!
1531 if (cache.find(bv) == cache.end())
1534 LyXParagraph * lpar = 0;
1535 LyXParagraph * selstartpar = 0;
1536 LyXParagraph * selendpar = 0;
1537 LyXParagraph::size_type pos = 0;
1538 LyXParagraph::size_type selstartpos = 0;
1539 LyXParagraph::size_type selendpos = 0;
1543 // ProhibitInput(bv);
1546 lpar = TEXT(bv)->cursor.par();
1547 pos = TEXT(bv)->cursor.pos();
1548 selstartpar = TEXT(bv)->sel_start_cursor.par();
1549 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1550 selendpar = TEXT(bv)->sel_end_cursor.par();
1551 selendpos = TEXT(bv)->sel_end_cursor.pos();
1552 selection = TEXT(bv)->selection;
1553 mark_set = TEXT(bv)->mark_set;
1555 deleteLyXText(bv, (the_locking_inset == 0));
1558 TEXT(bv)->selection = true;
1559 /* at this point just to avoid the Delete-Empty-Paragraph
1560 * Mechanism when setting the cursor */
1561 TEXT(bv)->mark_set = mark_set;
1563 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos);
1564 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1565 TEXT(bv)->SetCursor(bv, selendpar, selendpos);
1566 TEXT(bv)->SetSelection();
1567 TEXT(bv)->SetCursor(bv, lpar, pos);
1569 TEXT(bv)->SetCursor(bv, lpar, pos);
1570 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1571 TEXT(bv)->selection = false;
1575 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1576 // this will scroll the screen such that the cursor becomes visible
1577 bv->updateScrollbar();
1579 if (the_locking_inset) {
1580 /// then resize all LyXText in text-insets
1581 inset_x = cx(bv) - top_x + drawTextXOffset;
1582 inset_y = cy(bv) + drawTextYOffset;
1583 for (LyXParagraph * p = par; p; p = p->next) {
1584 p->resizeInsetsLyXText(bv);
1591 void InsetText::removeNewlines()
1593 for (LyXParagraph * p = par; p; p = p->next) {
1594 for (int i = 0; i < p->Last(); ++i) {
1595 if (p->GetChar(i) == LyXParagraph::META_NEWLINE)