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;
483 inset_boundary = false;
486 if (!checkAndActivateInset(bv, x, y, button))
487 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
489 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
490 bv->text->FinishUndo();
492 UpdateLocal(bv, FULL, false);
496 void InsetText::InsetUnlock(BufferView * bv)
498 if (the_locking_inset) {
499 the_locking_inset->InsetUnlock(bv);
500 the_locking_inset = 0;
503 no_selection = false;
505 TEXT(bv)->selection = 0;
506 UpdateLocal(bv, CLEAR_FRAME, false);
508 bv->owner()->setLayout(owner()->getLyXText(bv)
509 ->cursor.par()->GetLayout());
511 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
515 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
517 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
520 if (inset == cpar(bv)->GetInset(cpos(bv))) {
521 lyxerr[Debug::INSETS] << "OK" << endl;
522 the_locking_inset = inset;
523 inset_x = cx(bv) - top_x + drawTextXOffset;
524 inset_y = cy(bv) + drawTextYOffset;
525 inset_pos = cpos(bv);
526 inset_par = cpar(bv);
527 inset_boundary = cboundary(bv);
528 TEXT(bv)->UpdateInset(bv, the_locking_inset);
530 } else if (the_locking_inset && (the_locking_inset == inset)) {
531 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
532 lyxerr[Debug::INSETS] << "OK" << endl;
533 inset_x = cx(bv) - top_x + drawTextXOffset;
534 inset_y = cy(bv) + drawTextYOffset;
536 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
538 } else if (the_locking_inset) {
539 lyxerr[Debug::INSETS] << "MAYBE" << endl;
540 return the_locking_inset->LockInsetInInset(bv, inset);
542 lyxerr[Debug::INSETS] << "NOT OK" << endl;
547 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
550 if (!the_locking_inset)
552 if (the_locking_inset == inset) {
553 the_locking_inset->InsetUnlock(bv);
554 TEXT(bv)->UpdateInset(bv, inset);
555 the_locking_inset = 0;
557 moveRight(bv, false);
558 old_par = 0; // force layout setting
559 UpdateLocal(bv, CURSOR_PAR, false);
562 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
566 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
568 if (!the_locking_inset)
570 if (the_locking_inset != inset) {
571 TEXT(bv)->UpdateInset(bv, the_locking_inset);
572 need_update = CURSOR_PAR;
573 return the_locking_inset->UpdateInsetInInset(bv, inset);
575 // UpdateLocal(bv, FULL, false);
576 if (TEXT(bv)->UpdateInset(bv, inset))
577 UpdateLocal(bv, CURSOR_PAR, false);
578 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
579 inset_x = cx(bv) - top_x + drawTextXOffset;
580 inset_y = cy(bv) + drawTextYOffset;
586 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
588 no_selection = false;
590 int tmp_x = x - drawTextXOffset;
591 int tmp_y = y + insetAscent - TEXT(bv)->first;
592 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
595 if (the_locking_inset) {
596 if (the_locking_inset == inset) {
597 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
600 // otherwise unlock the_locking_inset and lock the new inset
601 the_locking_inset->InsetUnlock(bv);
602 inset_x = cx(bv) - top_x + drawTextXOffset;
603 inset_y = cy(bv) + drawTextYOffset;
604 the_locking_inset = static_cast<UpdatableInset*>(inset);
605 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
606 inset->Edit(bv, x - inset_x, y - inset_y, button);
607 if (the_locking_inset) {
608 UpdateLocal(bv, CURSOR_PAR, false);
612 // otherwise only unlock the_locking_inset
613 the_locking_inset->InsetUnlock(bv);
614 the_locking_inset = 0;
616 if (bv->theLockingInset()) {
617 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
618 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
619 inset_x = cx(bv) - top_x + drawTextXOffset;
620 inset_y = cy(bv) + drawTextYOffset;
621 inset_pos = cpos(bv);
622 inset_par = cpar(bv);
623 inset_boundary = cboundary(bv);
624 the_locking_inset = uinset;
625 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
626 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
627 // TEXT(bv)->ClearSelection();
628 if (the_locking_inset) {
629 UpdateLocal(bv, CURSOR_PAR, false);
635 bool paste_internally = false;
636 if ((button == 2) && TEXT(bv)->selection) {
637 LocalDispatch(bv, LFUN_COPY, "");
638 paste_internally = true;
640 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
642 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
643 UpdateLocal(bv, CURSOR, false);
644 bv->owner()->setLayout(cpar(bv)->GetLayout());
646 // Insert primary selection with middle mouse
647 // if there is a local selection in the current buffer,
650 if (paste_internally)
651 LocalDispatch(bv, LFUN_PASTE, "");
653 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
660 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
662 UpdatableInset * inset = 0;
664 if (the_locking_inset) {
665 the_locking_inset->InsetButtonRelease(bv,
666 x - inset_x, y - inset_y,
669 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
670 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
671 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
672 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
674 inset_x = cx(bv) - top_x + drawTextXOffset;
675 inset_y = cy(bv) + drawTextYOffset;
676 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
677 inset->Edit(bv, x - inset_x, y - inset_y, button);
679 UpdateLocal(bv, CURSOR_PAR, false);
682 no_selection = false;
686 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
688 if (the_locking_inset) {
689 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
695 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
697 TEXT(bv)->SetSelection();
698 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
699 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
700 UpdateLocal(bv, SELECTION, false);
703 no_selection = false;
707 void InsetText::InsetKeyPress(XKeyEvent * xke)
709 if (the_locking_inset) {
710 the_locking_inset->InsetKeyPress(xke);
716 UpdatableInset::RESULT
717 InsetText::LocalDispatch(BufferView * bv,
718 int action, string const & arg)
720 no_selection = false;
721 UpdatableInset::RESULT
722 result= UpdatableInset::LocalDispatch(bv, action, arg);
723 if (result != UNDISPATCHED) {
728 if ((action < 0) && arg.empty())
731 if (the_locking_inset) {
732 result = the_locking_inset->LocalDispatch(bv, action, arg);
733 if (result == DISPATCHED_NOUPDATE)
735 else if (result == DISPATCHED) {
736 UpdateLocal(bv, CURSOR_PAR, false);
738 } else if (result == FINISHED) {
742 moveRight(bv, false);
748 the_locking_inset = 0;
755 case LFUN_UNKNOWN_ACTION:
756 if (bv->buffer()->isReadonly()) {
758 // setErrorMessage(N_("Document is read only"));
762 /* Automatically delete the currently selected
763 * text and replace it with what is being
764 * typed in now. Depends on lyxrc settings
765 * "auto_region_delete", which defaults to
768 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
770 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
771 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
773 bv->text->cursor.par()->previous,
774 bv->text->cursor.par()->next
777 // if an empty paragraph set the language to the surronding
778 // paragraph language on insertion of the first character!
779 if (!par->Last() && !par->next) {
782 Inset * inset = owner();
783 while(inset && inset->getLyXText(bv) == TEXT(bv))
784 inset = inset->owner();
786 text = inset->getLyXText(bv);
790 LyXFont font(LyXFont::ALL_IGNORE);
791 font.setLanguage(text->cursor.par()->getParLanguage(bv->buffer()->params));
792 SetFont(bv, font, false);
795 if (lyxrc.auto_region_delete) {
796 if (TEXT(bv)->selection){
797 TEXT(bv)->CutSelection(bv, false);
800 TEXT(bv)->ClearSelection();
801 for (string::size_type i = 0; i < arg.length(); ++i) {
803 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
805 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
809 UpdateLocal(bv, CURSOR_PAR, true);
810 result=DISPATCHED_NOUPDATE;
812 // --- Cursor Movements ---------------------------------------------
814 bv->text->FinishUndo();
815 moveRight(bv, false, true);
816 TEXT(bv)->SetSelection();
817 UpdateLocal(bv, SELECTION, false);
820 result = moveRight(bv);
821 bv->text->FinishUndo();
822 TEXT(bv)->ClearSelection();
823 UpdateLocal(bv, CURSOR, false);
826 bv->text->FinishUndo();
827 moveLeft(bv, false, true);
828 TEXT(bv)->SetSelection();
829 UpdateLocal(bv, SELECTION, false);
832 bv->text->FinishUndo();
833 result= moveLeft(bv);
834 TEXT(bv)->ClearSelection();
835 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
836 UpdateLocal(bv, CURSOR, false);
839 bv->text->FinishUndo();
841 TEXT(bv)->SetSelection();
842 UpdateLocal(bv, SELECTION, false);
845 bv->text->FinishUndo();
846 result = moveDown(bv);
847 TEXT(bv)->ClearSelection();
848 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
849 UpdateLocal(bv, CURSOR, false);
852 bv->text->FinishUndo();
854 TEXT(bv)->SetSelection();
855 UpdateLocal(bv, SELECTION, false);
858 bv->text->FinishUndo();
860 TEXT(bv)->ClearSelection();
861 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
862 UpdateLocal(bv, CURSOR, false);
865 bv->text->FinishUndo();
866 TEXT(bv)->CursorHome(bv);
867 TEXT(bv)->ClearSelection();
868 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
869 UpdateLocal(bv, CURSOR, false);
872 TEXT(bv)->CursorEnd(bv);
873 TEXT(bv)->ClearSelection();
874 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
875 UpdateLocal(bv, CURSOR, false);
878 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
880 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
881 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
883 bv->text->cursor.par()->previous,
884 bv->text->cursor.par()->next
887 if (TEXT(bv)->selection)
888 TEXT(bv)->CutSelection(bv);
890 TEXT(bv)->Backspace(bv);
891 UpdateLocal(bv, CURSOR_PAR, true);
894 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
896 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
897 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
899 bv->text->cursor.par()->previous,
900 bv->text->cursor.par()->next
903 if (TEXT(bv)->selection)
904 TEXT(bv)->CutSelection(bv);
906 TEXT(bv)->Delete(bv);
907 UpdateLocal(bv, CURSOR_PAR, true);
910 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
912 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
913 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
915 bv->text->cursor.par()->previous,
916 bv->text->cursor.par()->next
919 TEXT(bv)->CutSelection(bv);
920 UpdateLocal(bv, CURSOR_PAR, true);
923 bv->text->FinishUndo();
924 TEXT(bv)->CopySelection(bv);
925 UpdateLocal(bv, CURSOR_PAR, false);
927 case LFUN_PASTESELECTION:
929 string clip(bv->workarea()->getClipboard());
933 if (arg == "paragraph") {
934 TEXT(bv)->InsertStringB(bv, clip);
936 TEXT(bv)->InsertStringA(bv, clip);
938 UpdateLocal(bv, CURSOR_PAR, true);
942 if (!autoBreakRows) {
945 if (cap.nrOfParagraphs() > 1) {
946 WriteAlert(_("Impossible operation"),
947 _("Cannot include more than one paragraph!"),
952 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
954 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
955 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
957 bv->text->cursor.par()->previous,
958 bv->text->cursor.par()->next
961 TEXT(bv)->PasteSelection(bv);
962 UpdateLocal(bv, CURSOR_PAR, true);
964 case LFUN_BREAKPARAGRAPH:
967 TEXT(bv)->BreakParagraph(bv, 0);
968 UpdateLocal(bv, FULL, true);
973 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
975 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
976 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
978 bv->text->cursor.par()->previous,
979 bv->text->cursor.par()->next
982 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
983 UpdateLocal(bv, CURSOR_PAR, true);
986 // do not set layouts on non breakable textinsets
988 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
990 // Derive layout number from given argument (string)
991 // and current buffer's textclass (number). */
992 LyXTextClassList::ClassList::size_type tclass =
993 bv->buffer()->params.textclass;
994 std::pair <bool, LyXTextClass::size_type> layout =
995 textclasslist.NumberOfLayout(tclass, arg);
997 // If the entry is obsolete, use the new one instead.
999 string obs = textclasslist.Style(tclass,layout.second).
1002 layout = textclasslist.NumberOfLayout(tclass, obs);
1005 // see if we found the layout number:
1006 if (!layout.first) {
1007 string msg = string(N_("Layout ")) + arg + N_(" not known");
1009 bv->owner()->getMiniBuffer()->Set(msg);
1013 if (cur_layout != layout.second) {
1014 cur_layout = layout.second;
1015 TEXT(bv)->SetLayout(bv, layout.second);
1016 bv->owner()->setLayout(cpar(bv)->GetLayout());
1017 UpdateLocal(bv, CURSOR_PAR, true);
1020 // reset the layout box
1021 bv->owner()->setLayout(cpar(bv)->GetLayout());
1024 case LFUN_PARAGRAPH_SPACING:
1025 // This one is absolutely not working. When fiddling with this
1026 // it also seems to me that the paragraphs inside the insettext
1027 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1029 LyXParagraph * par = TEXT(bv)->cursor.par();
1030 Spacing::Space cur_spacing = par->spacing.getSpace();
1031 float cur_value = 1.0;
1032 if (cur_spacing == Spacing::Other) {
1033 cur_value = par->spacing.getValue();
1036 std::istringstream istr(arg.c_str());
1039 Spacing::Space new_spacing = cur_spacing;
1040 float new_value = cur_value;
1042 lyxerr << "Missing argument to `paragraph-spacing'"
1044 } else if (tmp == "single") {
1045 new_spacing = Spacing::Single;
1046 } else if (tmp == "onehalf") {
1047 new_spacing = Spacing::Onehalf;
1048 } else if (tmp == "double") {
1049 new_spacing = Spacing::Double;
1050 } else if (tmp == "other") {
1051 new_spacing = Spacing::Other;
1054 lyxerr << "new_value = " << tmpval << endl;
1057 } else if (tmp == "default") {
1058 new_spacing = Spacing::Default;
1060 lyxerr << _("Unknown spacing argument: ")
1063 if (cur_spacing != new_spacing || cur_value != new_value) {
1064 par->spacing.set(new_spacing, new_value);
1065 //TEXT(bv)->RedoParagraph(owner->view());
1066 UpdateLocal(bv, CURSOR_PAR, true);
1067 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1073 result = UNDISPATCHED;
1076 if (result != FINISHED) {
1077 ShowInsetCursor(bv);
1079 bv->unlockInset(this);
1084 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1087 buf->latexParagraphs(os, par, 0, texrow);
1088 return texrow.rows();
1092 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1094 LyXParagraph * p = par;
1095 unsigned int lines = 0;
1099 tmp = buf->asciiParagraph(p, linelen);
1100 lines += countChar(tmp, '\n');
1108 int InsetText::DocBook(Buffer const * buf, ostream & os) const
1110 LyXParagraph * p = par;
1111 unsigned int lines = 0;
1116 buf->SimpleDocBookOnePar(os,tmp,p,desc,0);
1124 void InsetText::Validate(LaTeXFeatures & features) const
1126 LyXParagraph * p = par;
1128 p->validate(features);
1134 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1136 if (textclasslist.Style(buf->params.textclass,
1137 p->GetLayout()).labeltype != LABEL_MANUAL)
1140 return p->BeginningOfMainBody();
1144 void InsetText::GetCursorPos(BufferView * bv,
1145 int & x, int & y) const
1152 unsigned int InsetText::InsetInInsetY()
1154 if (!the_locking_inset)
1157 return (inset_y + the_locking_inset->InsetInInsetY());
1161 void InsetText::ToggleInsetCursor(BufferView * bv)
1163 if (the_locking_inset) {
1164 the_locking_inset->ToggleInsetCursor(bv);
1168 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1170 int asc = lyxfont::maxAscent(font);
1171 int desc = lyxfont::maxDescent(font);
1174 bv->hideLockedInsetCursor();
1176 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1177 cursor_visible = !cursor_visible;
1181 void InsetText::ShowInsetCursor(BufferView * bv, bool show)
1183 if (the_locking_inset) {
1184 the_locking_inset->ShowInsetCursor(bv);
1187 if (!cursor_visible) {
1188 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1190 int asc = lyxfont::maxAscent(font);
1191 int desc = lyxfont::maxDescent(font);
1193 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1195 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1196 cursor_visible = true;
1201 void InsetText::HideInsetCursor(BufferView * bv)
1203 if (cursor_visible) {
1204 bv->hideLockedInsetCursor();
1205 cursor_visible = false;
1207 if (the_locking_inset)
1208 the_locking_inset->HideInsetCursor(bv);
1212 UpdatableInset::RESULT
1213 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1215 if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1217 if (activate_inset && checkAndActivateInset(bv, false))
1219 TEXT(bv)->CursorRight(bv, selecting);
1220 return DISPATCHED_NOUPDATE;
1224 UpdatableInset::RESULT
1225 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1227 if (!cpar(bv)->previous && (cpos(bv) <= 0))
1229 TEXT(bv)->CursorLeft(bv, selecting);
1230 if (activate_inset && checkAndActivateInset(bv, true))
1232 return DISPATCHED_NOUPDATE;
1236 UpdatableInset::RESULT
1237 InsetText::moveUp(BufferView * bv)
1239 if (!crow(bv)->previous())
1241 TEXT(bv)->CursorUp(bv);
1242 return DISPATCHED_NOUPDATE;
1246 UpdatableInset::RESULT
1247 InsetText::moveDown(BufferView * bv)
1249 if (!crow(bv)->next())
1251 TEXT(bv)->CursorDown(bv);
1252 return DISPATCHED_NOUPDATE;
1256 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1258 if (the_locking_inset) {
1259 if (the_locking_inset->InsertInsetAllowed(inset))
1260 return the_locking_inset->InsertInset(bv, inset);
1263 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1265 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1266 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1268 bv->text->cursor.par()->previous,
1269 bv->text->cursor.par()->next
1272 inset->setOwner(this);
1273 HideInsetCursor(bv);
1274 TEXT(bv)->InsertInset(bv, inset);
1275 if ((cpar(bv)->GetChar(cpos(bv)) != LyXParagraph::META_INSET) ||
1276 (cpar(bv)->GetInset(cpos(bv)) != inset))
1277 TEXT(bv)->CursorLeft(bv);
1278 TEXT(bv)->selection = 0;
1279 bv->fitCursor(TEXT(bv));
1280 UpdateLocal(bv, CURSOR_PAR, true);
1281 ShowInsetCursor(bv);
1286 UpdatableInset * InsetText::GetLockingInset()
1288 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1292 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1296 if (the_locking_inset)
1297 return the_locking_inset->GetFirstLockingInsetOfType(c);
1302 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1304 bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1306 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1307 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1309 bv->text->cursor.par()->previous,
1310 bv->text->cursor.par()->next
1313 TEXT(bv)->SetFont(bv, font, toggleall);
1314 bv->fitCursor(TEXT(bv));
1315 UpdateLocal(bv, CURSOR_PAR, true);
1319 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1321 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1325 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1326 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1328 LyXFont const font =
1329 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1331 x = inset->width(bv, font);
1332 y = inset->descent(bv, font);
1336 inset_x = cx(bv) - top_x + drawTextXOffset;
1337 inset_y = cy(bv) + drawTextYOffset;
1338 inset->Edit(bv, x - inset_x, y - inset_y, 0);
1339 if (!the_locking_inset)
1341 UpdateLocal(bv, CURSOR_PAR, false);
1348 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1351 x = x - drawTextXOffset;
1352 y = y + insetAscent;
1353 Inset * inset = bv->checkInsetHit(TEXT(bv), x, y, button);
1360 inset_x = cx(bv) - top_x + drawTextXOffset;
1361 inset_y = cy(bv) + drawTextYOffset;
1362 inset->Edit(bv, x - inset_x, y - inset_y, button);
1363 if (!the_locking_inset)
1365 UpdateLocal(bv, CURSOR_PAR, false);
1372 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1374 int w = UpdatableInset::getMaxWidth(pain, inset);
1379 w = w - top_x + owner()->x();
1382 w -= (2 * TEXT_TO_INSET_OFFSET);
1384 // return w - (2*TEXT_TO_INSET_OFFSET);
1388 void InsetText::SetParagraphData(LyXParagraph *p)
1390 // delete all instances of LyXText before deleting the paragraps used
1392 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
1393 delete (*cit).second;
1408 par->SetInsetOwner(this);
1412 np->next = p->Clone();
1413 np->next->previous = np;
1415 np->SetInsetOwner(this);
1421 void InsetText::SetAutoBreakRows(bool flag)
1423 if (flag != autoBreakRows) {
1424 autoBreakRows = flag;
1432 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1434 if (how != drawFrame) {
1437 UpdateLocal(bv, DRAW_FRAME, false);
1442 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1444 if (frame_color != col) {
1447 UpdateLocal(bv, DRAW_FRAME, false);
1452 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1454 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1459 int InsetText::cx(BufferView * bv) const
1461 LyXText * text = TEXT(bv);
1462 int x = text->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1463 if (the_locking_inset) {
1464 LyXFont font = text->GetFont(bv->buffer(),
1465 text->cursor.par(), text->cursor.pos());
1466 if (font.isVisibleRightToLeft())
1467 x -= the_locking_inset->width(bv, font);
1473 int InsetText::cy(BufferView * bv) const
1476 return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1480 LyXParagraph::size_type InsetText::cpos(BufferView * bv) const
1482 return TEXT(bv)->cursor.pos();
1486 LyXParagraph * InsetText::cpar(BufferView * bv) const
1488 return TEXT(bv)->cursor.par();
1491 bool InsetText::cboundary(BufferView * bv) const
1493 return TEXT(bv)->cursor.boundary();
1497 Row * InsetText::crow(BufferView * bv) const
1499 return TEXT(bv)->cursor.row();
1503 LyXText * InsetText::getLyXText(BufferView const * lbv) const
1505 // Super UGLY! (Lgb)
1506 BufferView * bv = const_cast<BufferView *>(lbv);
1508 if ((cache.find(bv) != cache.end()) && cache[bv])
1510 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1513 if (the_locking_inset) {
1514 lt->SetCursor(bv, inset_par, inset_pos, true, inset_boundary);
1520 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1522 if ((cache.find(bv) == cache.end()) || !cache[bv])
1527 /// then remove all LyXText in text-insets
1528 LyXParagraph * p = par;
1529 for (;p;p = p->next) {
1530 p->deleteInsetsLyXText(bv);
1536 void InsetText::resizeLyXText(BufferView * bv) const
1538 if (!par->next && !par->size()) // resize not neccessary!
1540 if ((cache.find(bv) == cache.end()) || !cache[bv])
1543 LyXParagraph * lpar = 0;
1544 LyXParagraph * selstartpar = 0;
1545 LyXParagraph * selendpar = 0;
1546 LyXParagraph::size_type pos = 0;
1547 LyXParagraph::size_type selstartpos = 0;
1548 LyXParagraph::size_type selendpos = 0;
1549 bool boundary = false;
1550 bool selstartboundary = false;
1551 bool selendboundary = false;
1555 // ProhibitInput(bv);
1558 lpar = TEXT(bv)->cursor.par();
1559 pos = TEXT(bv)->cursor.pos();
1560 boundary = TEXT(bv)->cursor.boundary();
1561 selstartpar = TEXT(bv)->sel_start_cursor.par();
1562 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1563 selstartboundary = TEXT(bv)->sel_start_cursor.boundary();
1564 selendpar = TEXT(bv)->sel_end_cursor.par();
1565 selendpos = TEXT(bv)->sel_end_cursor.pos();
1566 selendboundary = TEXT(bv)->sel_end_cursor.boundary();
1567 selection = TEXT(bv)->selection;
1568 mark_set = TEXT(bv)->mark_set;
1570 deleteLyXText(bv, (the_locking_inset == 0));
1573 TEXT(bv)->selection = true;
1574 /* at this point just to avoid the Delete-Empty-Paragraph
1575 * Mechanism when setting the cursor */
1576 TEXT(bv)->mark_set = mark_set;
1578 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos,true,
1580 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1581 TEXT(bv)->SetCursor(bv, selendpar, selendpos, true, selendboundary);
1582 TEXT(bv)->SetSelection();
1583 TEXT(bv)->SetCursor(bv, lpar, pos);
1585 TEXT(bv)->SetCursor(bv, lpar, pos, true, boundary);
1586 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1587 TEXT(bv)->selection = false;
1591 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1592 // this will scroll the screen such that the cursor becomes visible
1593 bv->updateScrollbar();
1595 if (the_locking_inset) {
1596 /// then resize all LyXText in text-insets
1597 inset_x = cx(bv) - top_x + drawTextXOffset;
1598 inset_y = cy(bv) + drawTextYOffset;
1599 for (LyXParagraph * p = par; p; p = p->next) {
1600 p->resizeInsetsLyXText(bv);
1607 void InsetText::removeNewlines()
1609 for (LyXParagraph * p = par; p; p = p->next) {
1610 for (int i = 0; i < p->Last(); ++i) {
1611 if (p->GetChar(i) == LyXParagraph::META_NEWLINE)