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) {
409 int oldw = insetWidth;
411 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
412 // max(textWidth(bv->painter()),
413 // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
414 // (2 * TEXT_TO_INSET_OFFSET);
416 insetWidth = textWidth(bv->painter());
418 insetWidth = static_cast<int>(TEXT(bv)->width);
420 if (oldw != insetWidth) {
421 // printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
422 // textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
425 update(bv, font, reinit);
428 if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
431 TEXT(bv)->UpdateInset(bv, the_locking_inset);
434 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
438 Row * row = TEXT(bv)->GetRowNearY(y_temp);
439 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
440 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
441 TEXT_TO_INSET_OFFSET;
445 void InsetText::UpdateLocal(BufferView * bv, UpdateCodes what, bool mark_dirty)
447 TEXT(bv)->FullRebreak(bv);
448 if (need_update != INIT) {
449 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
451 else if (!the_locking_inset || (what != CURSOR))
454 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
456 bv->updateInset(this, mark_dirty);
457 bv->owner()->showState();
458 if (old_par != cpar(bv)) {
459 bv->owner()->setLayout(cpar(bv)->GetLayout());
465 string const InsetText::EditMessage() const
467 return _("Opened Text Inset");
471 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
473 // par->SetInsetOwner(this);
474 UpdatableInset::Edit(bv, x, y, button);
476 if (!bv->lockInset(this)) {
477 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
481 the_locking_inset = 0;
482 inset_pos = inset_x = inset_y = 0;
485 if (!checkAndActivateInset(bv, x, y, button))
486 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
488 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
489 bv->text->FinishUndo();
491 UpdateLocal(bv, FULL, false);
495 void InsetText::InsetUnlock(BufferView * bv)
497 if (the_locking_inset) {
498 the_locking_inset->InsetUnlock(bv);
499 the_locking_inset = 0;
502 no_selection = false;
504 TEXT(bv)->selection = 0;
505 UpdateLocal(bv, CLEAR_FRAME, false);
507 bv->owner()->setLayout(owner()->getLyXText(bv)
508 ->cursor.par()->GetLayout());
510 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
514 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
516 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
519 if (inset == cpar(bv)->GetInset(cpos(bv))) {
520 lyxerr[Debug::INSETS] << "OK" << endl;
521 the_locking_inset = inset;
522 inset_x = cx(bv) - top_x + drawTextXOffset;
523 inset_y = cy(bv) + drawTextYOffset;
524 inset_pos = cpos(bv);
525 inset_par = cpar(bv);
526 TEXT(bv)->UpdateInset(bv, the_locking_inset);
528 } else if (the_locking_inset && (the_locking_inset == inset)) {
529 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
530 lyxerr[Debug::INSETS] << "OK" << endl;
531 inset_x = cx(bv) - top_x + drawTextXOffset;
532 inset_y = cy(bv) + drawTextYOffset;
534 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
536 } else if (the_locking_inset) {
537 lyxerr[Debug::INSETS] << "MAYBE" << endl;
538 return the_locking_inset->LockInsetInInset(bv, inset);
540 lyxerr[Debug::INSETS] << "NOT OK" << endl;
545 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
548 if (!the_locking_inset)
550 if (the_locking_inset == inset) {
551 the_locking_inset->InsetUnlock(bv);
552 TEXT(bv)->UpdateInset(bv, inset);
553 the_locking_inset = 0;
555 moveRight(bv, false);
556 old_par = 0; // force layout setting
557 UpdateLocal(bv, CURSOR_PAR, false);
560 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
564 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
566 if (!the_locking_inset)
568 if (the_locking_inset != inset) {
569 TEXT(bv)->UpdateInset(bv, the_locking_inset);
570 need_update = CURSOR_PAR;
571 return the_locking_inset->UpdateInsetInInset(bv, inset);
573 // UpdateLocal(bv, FULL, false);
574 if (TEXT(bv)->UpdateInset(bv, inset))
575 UpdateLocal(bv, CURSOR_PAR, false);
576 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
577 inset_x = cx(bv) - top_x + drawTextXOffset;
578 inset_y = cy(bv) + drawTextYOffset;
584 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
586 no_selection = false;
588 int tmp_x = x - drawTextXOffset;
589 int tmp_y = y + insetAscent - TEXT(bv)->first;
590 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
593 if (the_locking_inset) {
594 if (the_locking_inset == inset) {
595 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
598 // otherwise unlock the_locking_inset and lock the new inset
599 the_locking_inset->InsetUnlock(bv);
600 inset_x = cx(bv) - top_x + drawTextXOffset;
601 inset_y = cy(bv) + drawTextYOffset;
602 the_locking_inset = static_cast<UpdatableInset*>(inset);
603 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
604 inset->Edit(bv, x - inset_x, y - inset_y, button);
605 if (the_locking_inset) {
606 UpdateLocal(bv, CURSOR_PAR, false);
610 // otherwise only unlock the_locking_inset
611 the_locking_inset->InsetUnlock(bv);
612 the_locking_inset = 0;
614 if (bv->theLockingInset()) {
615 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
616 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
617 inset_x = cx(bv) - top_x + drawTextXOffset;
618 inset_y = cy(bv) + drawTextYOffset;
619 inset_pos = cpos(bv);
620 inset_par = cpar(bv);
621 the_locking_inset = uinset;
622 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
623 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
624 // TEXT(bv)->ClearSelection();
625 if (the_locking_inset) {
626 UpdateLocal(bv, CURSOR_PAR, false);
632 bool paste_internally = false;
633 if ((button == 2) && TEXT(bv)->selection) {
634 LocalDispatch(bv, LFUN_COPY, "");
635 paste_internally = true;
637 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
639 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
640 UpdateLocal(bv, CURSOR, false);
641 bv->owner()->setLayout(cpar(bv)->GetLayout());
643 // Insert primary selection with middle mouse
644 // if there is a local selection in the current buffer,
647 if (paste_internally)
648 LocalDispatch(bv, LFUN_PASTE, "");
650 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
657 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
659 UpdatableInset * inset = 0;
661 if (the_locking_inset) {
662 the_locking_inset->InsetButtonRelease(bv,
663 x - inset_x, y - inset_y,
666 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
667 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
668 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
669 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
671 inset_x = cx(bv) - top_x + drawTextXOffset;
672 inset_y = cy(bv) + drawTextYOffset;
673 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
674 inset->Edit(bv, x - inset_x, y - inset_y, button);
676 UpdateLocal(bv, CURSOR_PAR, false);
679 no_selection = false;
683 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
685 if (the_locking_inset) {
686 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
692 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
694 TEXT(bv)->SetSelection();
695 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
696 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
697 UpdateLocal(bv, SELECTION, false);
700 no_selection = false;
704 void InsetText::InsetKeyPress(XKeyEvent * xke)
706 if (the_locking_inset) {
707 the_locking_inset->InsetKeyPress(xke);
713 UpdatableInset::RESULT
714 InsetText::LocalDispatch(BufferView * bv,
715 int action, string const & arg)
717 no_selection = false;
718 UpdatableInset::RESULT
719 result= UpdatableInset::LocalDispatch(bv, action, arg);
720 if (result != UNDISPATCHED) {
725 if ((action < 0) && arg.empty())
728 if (the_locking_inset) {
729 result = the_locking_inset->LocalDispatch(bv, action, arg);
730 if (result == DISPATCHED_NOUPDATE)
732 else if (result == DISPATCHED) {
733 UpdateLocal(bv, CURSOR_PAR, false);
735 } else if (result == FINISHED) {
739 moveRight(bv, false);
745 the_locking_inset = 0;
753 if (bv->buffer()->isReadonly()) {
755 // setErrorMessage(N_("Document is read only"));
759 /* Automatically delete the currently selected
760 * text and replace it with what is being
761 * typed in now. Depends on lyxrc settings
762 * "auto_region_delete", which defaults to
765 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
767 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
768 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
770 bv->text->cursor.par()->previous,
771 bv->text->cursor.par()->next
774 // if an empty paragraph set the language to the surronding
775 // paragraph language on insertion of the first character!
776 if (!par->Last() && !par->next) {
779 Inset * inset = owner();
780 while(inset && inset->getLyXText(bv) == TEXT(bv))
781 inset = inset->owner();
783 text = inset->getLyXText(bv);
787 LyXFont font(LyXFont::ALL_IGNORE);
788 font.setLanguage(text->cursor.par()->getParLanguage(bv->buffer()->params));
789 SetFont(bv, font, false);
791 if (lyxrc.auto_region_delete) {
792 if (TEXT(bv)->selection){
793 TEXT(bv)->CutSelection(bv, false);
796 TEXT(bv)->ClearSelection();
797 for (string::size_type i = 0; i < arg.length(); ++i) {
798 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
801 UpdateLocal(bv, CURSOR_PAR, true);
802 result=DISPATCHED_NOUPDATE;
804 // --- Cursor Movements ---------------------------------------------
806 bv->text->FinishUndo();
807 moveRight(bv, false, true);
808 TEXT(bv)->SetSelection();
809 UpdateLocal(bv, SELECTION, false);
812 result = moveRight(bv);
813 bv->text->FinishUndo();
814 TEXT(bv)->ClearSelection();
815 UpdateLocal(bv, CURSOR, false);
818 bv->text->FinishUndo();
819 moveLeft(bv, false, true);
820 TEXT(bv)->SetSelection();
821 UpdateLocal(bv, SELECTION, false);
824 bv->text->FinishUndo();
825 result= moveLeft(bv);
826 TEXT(bv)->ClearSelection();
827 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
828 UpdateLocal(bv, CURSOR, false);
831 bv->text->FinishUndo();
833 TEXT(bv)->SetSelection();
834 UpdateLocal(bv, SELECTION, false);
837 bv->text->FinishUndo();
838 result = moveDown(bv);
839 TEXT(bv)->ClearSelection();
840 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
841 UpdateLocal(bv, CURSOR, false);
844 bv->text->FinishUndo();
846 TEXT(bv)->SetSelection();
847 UpdateLocal(bv, SELECTION, false);
850 bv->text->FinishUndo();
852 TEXT(bv)->ClearSelection();
853 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
854 UpdateLocal(bv, CURSOR, false);
857 bv->text->FinishUndo();
858 TEXT(bv)->CursorHome(bv);
859 TEXT(bv)->ClearSelection();
860 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
861 UpdateLocal(bv, CURSOR, false);
864 TEXT(bv)->CursorEnd(bv);
865 TEXT(bv)->ClearSelection();
866 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
867 UpdateLocal(bv, CURSOR, false);
870 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
872 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
873 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
875 bv->text->cursor.par()->previous,
876 bv->text->cursor.par()->next
879 if (TEXT(bv)->selection)
880 TEXT(bv)->CutSelection(bv);
882 TEXT(bv)->Backspace(bv);
883 UpdateLocal(bv, CURSOR_PAR, true);
886 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
888 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
889 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
891 bv->text->cursor.par()->previous,
892 bv->text->cursor.par()->next
895 if (TEXT(bv)->selection)
896 TEXT(bv)->CutSelection(bv);
898 TEXT(bv)->Delete(bv);
899 UpdateLocal(bv, CURSOR_PAR, true);
902 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
904 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
905 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
907 bv->text->cursor.par()->previous,
908 bv->text->cursor.par()->next
911 TEXT(bv)->CutSelection(bv);
912 UpdateLocal(bv, CURSOR_PAR, true);
915 bv->text->FinishUndo();
916 TEXT(bv)->CopySelection(bv);
917 UpdateLocal(bv, CURSOR_PAR, false);
919 case LFUN_PASTESELECTION:
921 string clip(bv->workarea()->getClipboard());
925 if (arg == "paragraph") {
926 TEXT(bv)->InsertStringB(bv, clip);
928 TEXT(bv)->InsertStringA(bv, clip);
930 UpdateLocal(bv, CURSOR_PAR, true);
934 if (!autoBreakRows) {
937 if (cap.nrOfParagraphs() > 1) {
938 WriteAlert(_("Impossible operation"),
939 _("Cannot include more than one paragraph!"),
944 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
946 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
947 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
949 bv->text->cursor.par()->previous,
950 bv->text->cursor.par()->next
953 TEXT(bv)->PasteSelection(bv);
954 UpdateLocal(bv, CURSOR_PAR, true);
956 case LFUN_BREAKPARAGRAPH:
959 TEXT(bv)->BreakParagraph(bv, 0);
960 UpdateLocal(bv, FULL, true);
965 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
967 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
968 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
970 bv->text->cursor.par()->previous,
971 bv->text->cursor.par()->next
974 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
975 UpdateLocal(bv, CURSOR_PAR, true);
978 // do not set layouts on non breakable textinsets
980 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
982 // Derive layout number from given argument (string)
983 // and current buffer's textclass (number). */
984 LyXTextClassList::ClassList::size_type tclass =
985 bv->buffer()->params.textclass;
986 std::pair <bool, LyXTextClass::size_type> layout =
987 textclasslist.NumberOfLayout(tclass, arg);
989 // If the entry is obsolete, use the new one instead.
991 string obs = textclasslist.Style(tclass,layout.second).
994 layout = textclasslist.NumberOfLayout(tclass, obs);
997 // see if we found the layout number:
999 string msg = string(N_("Layout ")) + arg + N_(" not known");
1001 bv->owner()->getMiniBuffer()->Set(msg);
1005 if (cur_layout != layout.second) {
1006 cur_layout = layout.second;
1007 TEXT(bv)->SetLayout(bv, layout.second);
1008 bv->owner()->setLayout(cpar(bv)->GetLayout());
1009 UpdateLocal(bv, CURSOR_PAR, true);
1012 // reset the layout box
1013 bv->owner()->setLayout(cpar(bv)->GetLayout());
1016 case LFUN_PARAGRAPH_SPACING:
1017 // This one is absolutely not working. When fiddling with this
1018 // it also seems to me that the paragraphs inside the insettext
1019 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1021 LyXParagraph * par = TEXT(bv)->cursor.par();
1022 Spacing::Space cur_spacing = par->spacing.getSpace();
1023 float cur_value = 1.0;
1024 if (cur_spacing == Spacing::Other) {
1025 cur_value = par->spacing.getValue();
1028 std::istringstream istr(arg.c_str());
1031 Spacing::Space new_spacing = cur_spacing;
1032 float new_value = cur_value;
1034 lyxerr << "Missing argument to `paragraph-spacing'"
1036 } else if (tmp == "single") {
1037 new_spacing = Spacing::Single;
1038 } else if (tmp == "onehalf") {
1039 new_spacing = Spacing::Onehalf;
1040 } else if (tmp == "double") {
1041 new_spacing = Spacing::Double;
1042 } else if (tmp == "other") {
1043 new_spacing = Spacing::Other;
1046 lyxerr << "new_value = " << tmpval << endl;
1049 } else if (tmp == "default") {
1050 new_spacing = Spacing::Default;
1052 lyxerr << _("Unknown spacing argument: ")
1055 if (cur_spacing != new_spacing || cur_value != new_value) {
1056 par->spacing.set(new_spacing, new_value);
1057 //TEXT(bv)->RedoParagraph(owner->view());
1058 UpdateLocal(bv, CURSOR_PAR, true);
1059 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1065 result = UNDISPATCHED;
1068 if (result != FINISHED) {
1069 ShowInsetCursor(bv);
1071 bv->unlockInset(this);
1076 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1079 buf->latexParagraphs(os, par, 0, texrow);
1080 return texrow.rows();
1084 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1086 LyXParagraph * p = par;
1087 unsigned int lines = 0;
1091 tmp = buf->asciiParagraph(p, linelen);
1092 lines += countChar(tmp, '\n');
1100 int InsetText::DocBook(Buffer const * buf, ostream & os) const
1102 LyXParagraph * p = par;
1103 unsigned int lines = 0;
1108 buf->SimpleDocBookOnePar(os,tmp,p,desc,0);
1116 void InsetText::Validate(LaTeXFeatures & features) const
1118 LyXParagraph * p = par;
1120 p->validate(features);
1126 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1128 if (textclasslist.Style(buf->params.textclass,
1129 p->GetLayout()).labeltype != LABEL_MANUAL)
1132 return p->BeginningOfMainBody();
1136 void InsetText::GetCursorPos(BufferView * bv,
1137 int & x, int & y) const
1144 unsigned int InsetText::InsetInInsetY()
1146 if (!the_locking_inset)
1149 return (inset_y + the_locking_inset->InsetInInsetY());
1153 void InsetText::ToggleInsetCursor(BufferView * bv)
1155 if (the_locking_inset) {
1156 the_locking_inset->ToggleInsetCursor(bv);
1160 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1162 int asc = lyxfont::maxAscent(font);
1163 int desc = lyxfont::maxDescent(font);
1166 bv->hideLockedInsetCursor();
1168 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1169 cursor_visible = !cursor_visible;
1173 void InsetText::ShowInsetCursor(BufferView * bv, bool show)
1175 if (the_locking_inset) {
1176 the_locking_inset->ShowInsetCursor(bv);
1179 if (!cursor_visible) {
1180 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1182 int asc = lyxfont::maxAscent(font);
1183 int desc = lyxfont::maxDescent(font);
1185 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1187 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1188 cursor_visible = true;
1193 void InsetText::HideInsetCursor(BufferView * bv)
1195 if (cursor_visible) {
1196 bv->hideLockedInsetCursor();
1197 cursor_visible = false;
1199 if (the_locking_inset)
1200 the_locking_inset->HideInsetCursor(bv);
1204 UpdatableInset::RESULT
1205 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1207 if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1209 if (activate_inset && checkAndActivateInset(bv, false))
1211 TEXT(bv)->CursorRight(bv, selecting);
1212 return DISPATCHED_NOUPDATE;
1216 UpdatableInset::RESULT
1217 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1219 if (!cpar(bv)->previous && (cpos(bv) <= 0))
1221 TEXT(bv)->CursorLeft(bv, selecting);
1222 if (activate_inset && checkAndActivateInset(bv, true))
1224 return DISPATCHED_NOUPDATE;
1228 UpdatableInset::RESULT
1229 InsetText::moveUp(BufferView * bv)
1231 if (!crow(bv)->previous())
1233 TEXT(bv)->CursorUp(bv);
1234 return DISPATCHED_NOUPDATE;
1238 UpdatableInset::RESULT
1239 InsetText::moveDown(BufferView * bv)
1241 if (!crow(bv)->next())
1243 TEXT(bv)->CursorDown(bv);
1244 return DISPATCHED_NOUPDATE;
1248 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1250 if (the_locking_inset) {
1251 if (the_locking_inset->InsertInsetAllowed(inset))
1252 return the_locking_inset->InsertInset(bv, inset);
1255 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1257 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1258 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1260 bv->text->cursor.par()->previous,
1261 bv->text->cursor.par()->next
1264 inset->setOwner(this);
1265 HideInsetCursor(bv);
1266 TEXT(bv)->InsertInset(bv, inset);
1267 if ((cpar(bv)->GetChar(cpos(bv)) != LyXParagraph::META_INSET) ||
1268 (cpar(bv)->GetInset(cpos(bv)) != inset))
1269 TEXT(bv)->CursorLeft(bv);
1270 TEXT(bv)->selection = 0;
1271 bv->fitCursor(TEXT(bv));
1272 UpdateLocal(bv, CURSOR_PAR, true);
1273 ShowInsetCursor(bv);
1278 UpdatableInset * InsetText::GetLockingInset()
1280 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1284 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1288 if (the_locking_inset)
1289 return the_locking_inset->GetFirstLockingInsetOfType(c);
1294 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1296 bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1298 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1299 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1301 bv->text->cursor.par()->previous,
1302 bv->text->cursor.par()->next
1305 TEXT(bv)->SetFont(bv, font, toggleall);
1306 bv->fitCursor(TEXT(bv));
1307 UpdateLocal(bv, CURSOR_PAR, true);
1311 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1313 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1317 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1318 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1320 LyXFont const font =
1321 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1323 x = inset->width(bv, font);
1324 y = inset->descent(bv, font);
1328 inset_x = cx(bv) - top_x + drawTextXOffset;
1329 inset_y = cy(bv) + drawTextYOffset;
1330 inset->Edit(bv, x - inset_x, y - inset_y, 0);
1331 if (!the_locking_inset)
1333 UpdateLocal(bv, CURSOR_PAR, false);
1340 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1343 x = x - drawTextXOffset;
1344 y = y + insetAscent;
1345 Inset * inset = bv->checkInsetHit(TEXT(bv), x, y, button);
1352 inset_x = cx(bv) - top_x + drawTextXOffset;
1353 inset_y = cy(bv) + drawTextYOffset;
1354 inset->Edit(bv, x - inset_x, y - inset_y, button);
1355 if (!the_locking_inset)
1357 UpdateLocal(bv, CURSOR_PAR, false);
1364 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1366 int w = UpdatableInset::getMaxWidth(pain, inset);
1371 w = w - top_x + owner()->x();
1374 w -= (2 * TEXT_TO_INSET_OFFSET);
1376 // return w - (2*TEXT_TO_INSET_OFFSET);
1380 void InsetText::SetParagraphData(LyXParagraph *p)
1382 // delete all instances of LyXText before deleting the paragraps used
1384 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
1385 delete (*cit).second;
1400 par->SetInsetOwner(this);
1404 np->next = p->Clone();
1405 np->next->previous = np;
1407 np->SetInsetOwner(this);
1413 void InsetText::SetAutoBreakRows(bool flag)
1415 if (flag != autoBreakRows) {
1416 autoBreakRows = flag;
1424 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1426 if (how != drawFrame) {
1429 UpdateLocal(bv, DRAW_FRAME, false);
1434 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1436 if (frame_color != col) {
1439 UpdateLocal(bv, DRAW_FRAME, false);
1444 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1446 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1451 int InsetText::cx(BufferView * bv) const
1453 return TEXT(bv)->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1457 int InsetText::cy(BufferView * bv) const
1460 return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1464 LyXParagraph::size_type InsetText::cpos(BufferView * bv) const
1466 return TEXT(bv)->cursor.pos();
1470 LyXParagraph * InsetText::cpar(BufferView * bv) const
1472 return TEXT(bv)->cursor.par();
1476 Row * InsetText::crow(BufferView * bv) const
1478 return TEXT(bv)->cursor.row();
1482 LyXText * InsetText::getLyXText(BufferView * bv) const
1484 if ((cache.find(bv) != cache.end()) && cache[bv])
1486 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1489 if (the_locking_inset) {
1490 lt->SetCursor(bv, inset_par, inset_pos);
1496 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1498 if ((cache.find(bv) == cache.end()) || !cache[bv])
1503 /// then remove all LyXText in text-insets
1504 LyXParagraph * p = par;
1505 for (;p;p = p->next) {
1506 p->deleteInsetsLyXText(bv);
1512 void InsetText::resizeLyXText(BufferView * bv) const
1514 if (!par->next && !par->size()) // resize not neccessary!
1516 if ((cache.find(bv) == cache.end()) || !cache[bv])
1519 LyXParagraph * lpar = 0;
1520 LyXParagraph * selstartpar = 0;
1521 LyXParagraph * selendpar = 0;
1522 LyXParagraph::size_type pos = 0;
1523 LyXParagraph::size_type selstartpos = 0;
1524 LyXParagraph::size_type selendpos = 0;
1528 // ProhibitInput(bv);
1531 lpar = TEXT(bv)->cursor.par();
1532 pos = TEXT(bv)->cursor.pos();
1533 selstartpar = TEXT(bv)->sel_start_cursor.par();
1534 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1535 selendpar = TEXT(bv)->sel_end_cursor.par();
1536 selendpos = TEXT(bv)->sel_end_cursor.pos();
1537 selection = TEXT(bv)->selection;
1538 mark_set = TEXT(bv)->mark_set;
1540 deleteLyXText(bv, (the_locking_inset == 0));
1543 TEXT(bv)->selection = true;
1544 /* at this point just to avoid the Delete-Empty-Paragraph
1545 * Mechanism when setting the cursor */
1546 TEXT(bv)->mark_set = mark_set;
1548 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos);
1549 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1550 TEXT(bv)->SetCursor(bv, selendpar, selendpos);
1551 TEXT(bv)->SetSelection();
1552 TEXT(bv)->SetCursor(bv, lpar, pos);
1554 TEXT(bv)->SetCursor(bv, lpar, pos);
1555 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1556 TEXT(bv)->selection = false;
1560 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1561 // this will scroll the screen such that the cursor becomes visible
1562 bv->updateScrollbar();
1564 if (the_locking_inset) {
1565 /// then resize all LyXText in text-insets
1566 inset_x = cx(bv) - top_x + drawTextXOffset;
1567 inset_y = cy(bv) + drawTextYOffset;
1568 for (LyXParagraph * p = par; p; p = p->next) {
1569 p->resizeInsetsLyXText(bv);
1576 void InsetText::removeNewlines()
1578 for (LyXParagraph * p = par; p; p = p->next) {
1579 for (int i = 0; i < p->Last(); ++i) {
1580 if (p->GetChar(i) == LyXParagraph::META_NEWLINE)