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 for(Cache::const_iterator cit = cache.begin(); cit != cache.end(); ++cit)
116 delete (*cit).second;
117 // deleteLyXText((*cit).first);
118 LyXParagraph * p = par->next;
128 void InsetText::clear()
130 LyXParagraph * p = par->next;
137 par = new LyXParagraph();
141 Inset * InsetText::Clone(Buffer const &) const
143 InsetText * t = new InsetText(*this);
148 void InsetText::Write(Buffer const * buf, ostream & os) const
151 WriteParagraphData(buf, os);
155 void InsetText::WriteParagraphData(Buffer const * buf, ostream & os) const
157 par->writeFile(buf, os, buf->params, 0, 0);
161 void InsetText::Read(Buffer const * buf, LyXLex & lex)
165 LyXParagraph * return_par = 0;
166 char depth = 0; // signed or unsigned?
168 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
169 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
171 LyXFont font(LyXFont::ALL_INHERIT);
173 LyXParagraph * p = par->next;
180 par = new LyXParagraph;
183 token = lex.GetString();
186 if (token == "\\end_inset")
188 if (const_cast<Buffer*>(buf)->
189 parseSingleLyXformat2Token(lex, par, return_par,token, pos, depth,
192 , footnoteflag, footnotekind
196 // the_end read this should NEVER happen
197 lex.printError("\\the_end read in inset! Error in document!");
205 return_par->SetInsetOwner(this);
206 return_par = return_par->next;
209 if (token != "\\end_inset") {
210 lex.printError("Missing \\end_inset at this point. "
217 int InsetText::ascent(BufferView * bv, LyXFont const &) const
220 Row * row = TEXT(bv)->GetRowNearY(y_temp);
221 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
226 int InsetText::descent(BufferView * bv, LyXFont const &) const
229 Row * row = TEXT(bv)->GetRowNearY(y_temp);
230 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
231 TEXT_TO_INSET_OFFSET;
236 int InsetText::width(BufferView * bv, LyXFont const &) const
238 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
243 int InsetText::textWidth(Painter & pain) const
245 int w = getMaxWidth(pain, this);
250 void InsetText::draw(BufferView * bv, LyXFont const & f,
251 int baseline, float & x, bool cleared) const
253 Painter & pain = bv->painter();
255 // no draw is necessary !!!
256 if ((drawFrame == LOCKED) && !locked && !par->size()) {
257 if (!cleared && (need_update == CLEAR_FRAME)) {
258 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
260 insetAscent + insetDescent - 1,
264 top_baseline = baseline;
271 UpdatableInset::draw(bv, f, baseline, x, cleared);
273 if (!cleared && ((need_update==FULL) || (top_x!=int(x)) ||
274 (top_baseline!=baseline))) {
276 int h = insetAscent + insetDescent;
277 int ty = baseline - insetAscent;
283 if ((ty + h) > pain.paperHeight())
284 h = pain.paperHeight();
285 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
286 w = pain.paperWidth();
287 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
291 if (!cleared && (need_update == NONE))
294 if (top_x != int(x)) {
297 bv->text->status = LyXText::CHANGED_IN_DRAW;
301 top_baseline = baseline;
302 top_y = baseline - ascent(bv, f);
303 last_width = width(bv, f);
304 last_height = ascent(bv, f) + descent(bv, f);
306 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
307 inset_x = cx(bv) - top_x + drawTextXOffset;
308 inset_y = cy(bv) + drawTextYOffset;
310 if (!cleared && (need_update == CURSOR) && !TEXT(bv)->selection) {
315 x += TEXT_TO_INSET_OFFSET;
318 Row * row = TEXT(bv)->GetRowNearY(y);
319 int y_offset = baseline - row->ascent_of_text();
320 int ph = pain.paperHeight();
323 while ((row != 0) && ((y+row->height()) <= 0)) {
325 first += row->height();
330 TEXT(bv)->first = first;
331 if (cleared || !locked || (need_update == FULL)) {
334 while ((row != 0) && (yf < ph)) {
335 TEXT(bv)->GetVisibleRow(bv, y+y_offset, int(x), row,
341 } else if (need_update == SELECTION) {
342 bv->screen()->ToggleToggle(TEXT(bv), y_offset, int(x));
345 if (need_update == CURSOR) {
346 bv->screen()->ToggleSelection(TEXT(bv), true, y_offset,int(x));
347 TEXT(bv)->ClearSelection();
348 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
350 bv->screen()->Update(TEXT(bv), y_offset, int(x));
354 TEXT(bv)->refresh_y = 0;
355 TEXT(bv)->status = LyXText::UNCHANGED;
356 if ((need_update != CURSOR_PAR) &&
357 ((drawFrame == ALWAYS) || ((drawFrame == LOCKED) && locked)))
359 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
360 width(bv, f) - 1, insetAscent + insetDescent - 1,
362 } else if (need_update == CLEAR_FRAME) {
363 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
364 width(bv, f) - 1, insetAscent + insetDescent - 1,
367 x += width(bv, f) - TEXT_TO_INSET_OFFSET;
368 if (bv->text->status==LyXText::CHANGED_IN_DRAW)
370 else if (need_update != INIT)
375 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
377 if (reinit) { // && (need_update != CURSOR)) {
381 owner()->update(bv, font, true);
384 if (the_locking_inset) {
385 inset_x = cx(bv) - top_x + drawTextXOffset;
386 inset_y = cy(bv) + drawTextYOffset;
387 the_locking_inset->update(bv, font, reinit);
389 if (need_update == INIT) {
392 // if (!owner() && bv->text)
393 // bv->text->UpdateInset(bv, this);
395 int oldw = insetWidth;
397 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
398 // max(textWidth(bv->painter()),
399 // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
400 // (2 * TEXT_TO_INSET_OFFSET);
402 insetWidth = textWidth(bv->painter());
404 insetWidth = static_cast<int>(TEXT(bv)->width);
406 if (oldw != insetWidth) {
407 // printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
408 // textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
413 owner()->update(bv, font, reinit);
416 update(bv, font, reinit);
420 update(bv, font, reinit);
422 UpdateLocal(bv, INIT, false);
427 if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
430 TEXT(bv)->UpdateInset(bv, the_locking_inset);
433 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
437 Row * row = TEXT(bv)->GetRowNearY(y_temp);
438 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
439 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
440 TEXT_TO_INSET_OFFSET;
444 void InsetText::UpdateLocal(BufferView * bv, UpdateCodes what, bool mark_dirty)
446 TEXT(bv)->FullRebreak(bv);
447 if (need_update != INIT) {
448 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
450 else if (!the_locking_inset || (what != CURSOR))
453 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
455 bv->updateInset(this, mark_dirty);
456 bv->owner()->showState();
457 if (old_par != cpar(bv)) {
458 bv->owner()->setLayout(cpar(bv)->GetLayout());
464 string const InsetText::EditMessage() const
466 return _("Opened Text Inset");
470 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
472 // par->SetInsetOwner(this);
473 UpdatableInset::Edit(bv, x, y, button);
475 if (!bv->lockInset(this)) {
476 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
480 the_locking_inset = 0;
481 inset_pos = inset_x = inset_y = 0;
484 if (!checkAndActivateInset(bv, x, y, button))
485 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
487 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
488 bv->text->FinishUndo();
490 UpdateLocal(bv, FULL, false);
494 void InsetText::InsetUnlock(BufferView * bv)
496 if (the_locking_inset) {
497 the_locking_inset->InsetUnlock(bv);
498 the_locking_inset = 0;
501 no_selection = false;
503 TEXT(bv)->selection = 0;
504 UpdateLocal(bv, CLEAR_FRAME, false);
506 bv->owner()->setLayout(owner()->getLyXText(bv)
507 ->cursor.par()->GetLayout());
509 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
513 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
515 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
518 if (inset == cpar(bv)->GetInset(cpos(bv))) {
519 lyxerr[Debug::INSETS] << "OK" << endl;
520 the_locking_inset = inset;
521 inset_x = cx(bv) - top_x + drawTextXOffset;
522 inset_y = cy(bv) + drawTextYOffset;
523 inset_pos = cpos(bv);
524 inset_par = cpar(bv);
525 TEXT(bv)->UpdateInset(bv, the_locking_inset);
527 } else if (the_locking_inset && (the_locking_inset == inset)) {
528 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
529 lyxerr[Debug::INSETS] << "OK" << endl;
530 inset_x = cx(bv) - top_x + drawTextXOffset;
531 inset_y = cy(bv) + drawTextYOffset;
533 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
535 } else if (the_locking_inset) {
536 lyxerr[Debug::INSETS] << "MAYBE" << endl;
537 return the_locking_inset->LockInsetInInset(bv, inset);
539 lyxerr[Debug::INSETS] << "NOT OK" << endl;
544 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
547 if (!the_locking_inset)
549 if (the_locking_inset == inset) {
550 the_locking_inset->InsetUnlock(bv);
551 TEXT(bv)->UpdateInset(bv, inset);
552 the_locking_inset = 0;
554 moveRight(bv, false);
555 old_par = 0; // force layout setting
556 UpdateLocal(bv, CURSOR_PAR, false);
559 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
563 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
565 if (!the_locking_inset)
567 if (the_locking_inset != inset) {
568 TEXT(bv)->UpdateInset(bv, the_locking_inset);
569 need_update = CURSOR_PAR;
570 return the_locking_inset->UpdateInsetInInset(bv, inset);
572 // UpdateLocal(bv, FULL, false);
573 if (TEXT(bv)->UpdateInset(bv, inset))
574 UpdateLocal(bv, CURSOR_PAR, false);
575 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
576 inset_x = cx(bv) - top_x + drawTextXOffset;
577 inset_y = cy(bv) + drawTextYOffset;
583 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
585 no_selection = false;
587 int tmp_x = x - drawTextXOffset;
588 int tmp_y = y + insetAscent;
589 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
592 if (the_locking_inset) {
593 if (the_locking_inset == inset) {
594 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
597 // otherwise unlock the_locking_inset and lock the new inset
598 the_locking_inset->InsetUnlock(bv);
599 inset_x = cx(bv) - top_x + drawTextXOffset;
600 inset_y = cy(bv) + drawTextYOffset;
601 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
602 inset->Edit(bv, x - inset_x, y - inset_y, button);
603 if (the_locking_inset) {
604 UpdateLocal(bv, CURSOR_PAR, false);
608 // otherwise only unlock the_locking_inset
609 the_locking_inset->InsetUnlock(bv);
610 the_locking_inset = 0;
612 if (bv->theLockingInset()) {
613 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
614 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
615 inset_x = cx(bv) - top_x + drawTextXOffset;
616 inset_y = cy(bv) + drawTextYOffset;
617 inset_pos = cpos(bv);
618 inset_par = cpar(bv);
619 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
620 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
621 if (the_locking_inset) {
622 UpdateLocal(bv, CURSOR_PAR, false);
628 bool paste_internally = false;
629 if ((button == 2) && TEXT(bv)->selection) {
630 LocalDispatch(bv, LFUN_COPY, "");
631 paste_internally = true;
633 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
635 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
636 UpdateLocal(bv, CURSOR, false);
637 bv->owner()->setLayout(cpar(bv)->GetLayout());
639 // Insert primary selection with middle mouse
640 // if there is a local selection in the current buffer,
643 if (paste_internally)
644 LocalDispatch(bv, LFUN_PASTE, "");
646 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
653 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
655 UpdatableInset * inset = 0;
657 if (the_locking_inset) {
658 the_locking_inset->InsetButtonRelease(bv,
659 x - inset_x, y - inset_y,
662 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
663 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
664 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
665 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
667 inset_x = cx(bv) - top_x + drawTextXOffset;
668 inset_y = cy(bv) + drawTextYOffset;
669 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
670 inset->Edit(bv, x - inset_x, y - inset_y, button);
672 UpdateLocal(bv, CURSOR_PAR, false);
675 no_selection = false;
679 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
681 if (the_locking_inset) {
682 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
688 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
690 TEXT(bv)->SetSelection();
691 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
692 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
693 UpdateLocal(bv, SELECTION, false);
696 no_selection = false;
700 void InsetText::InsetKeyPress(XKeyEvent * xke)
702 if (the_locking_inset) {
703 the_locking_inset->InsetKeyPress(xke);
709 UpdatableInset::RESULT
710 InsetText::LocalDispatch(BufferView * bv,
711 int action, string const & arg)
713 no_selection = false;
714 UpdatableInset::RESULT
715 result= UpdatableInset::LocalDispatch(bv, action, arg);
716 if (result != UNDISPATCHED) {
721 if ((action < 0) && arg.empty())
724 if (the_locking_inset) {
725 result = the_locking_inset->LocalDispatch(bv, action, arg);
726 if (result == DISPATCHED_NOUPDATE)
728 else if (result == DISPATCHED) {
729 UpdateLocal(bv, CURSOR_PAR, false);
731 } else if (result == FINISHED) {
735 moveRight(bv, false);
741 the_locking_inset = 0;
749 if (bv->buffer()->isReadonly()) {
751 // setErrorMessage(N_("Document is read only"));
755 /* Automatically delete the currently selected
756 * text and replace it with what is being
757 * typed in now. Depends on lyxrc settings
758 * "auto_region_delete", which defaults to
761 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
763 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
764 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
766 bv->text->cursor.par()->previous,
767 bv->text->cursor.par()->next
770 // if an empty paragraph set the language to the surronding
771 // paragraph language on insertion of the first character!
772 if (!par->Last() && !par->next) {
775 Inset * inset = owner();
776 while(inset && inset->getLyXText(bv) == TEXT(bv))
777 inset = inset->owner();
779 text = inset->getLyXText(bv);
783 LyXFont font(LyXFont::ALL_IGNORE);
784 font.setLanguage(text->cursor.par()->getParLanguage(bv->buffer()->params));
785 SetFont(bv, font, false);
787 if (lyxrc.auto_region_delete) {
788 if (TEXT(bv)->selection){
789 TEXT(bv)->CutSelection(bv, false);
792 TEXT(bv)->ClearSelection();
793 for (string::size_type i = 0; i < arg.length(); ++i) {
794 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
797 UpdateLocal(bv, CURSOR_PAR, true);
799 // --- Cursor Movements ---------------------------------------------
801 bv->text->FinishUndo();
802 moveRight(bv, false, true);
803 TEXT(bv)->SetSelection();
804 UpdateLocal(bv, SELECTION, false);
807 result = moveRight(bv);
808 bv->text->FinishUndo();
809 TEXT(bv)->ClearSelection();
810 UpdateLocal(bv, CURSOR, false);
813 bv->text->FinishUndo();
814 moveLeft(bv, false, true);
815 TEXT(bv)->SetSelection();
816 UpdateLocal(bv, SELECTION, false);
819 bv->text->FinishUndo();
820 result= moveLeft(bv);
821 TEXT(bv)->ClearSelection();
822 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
823 UpdateLocal(bv, CURSOR, false);
826 bv->text->FinishUndo();
828 TEXT(bv)->SetSelection();
829 UpdateLocal(bv, SELECTION, false);
832 bv->text->FinishUndo();
833 result = moveDown(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();
847 TEXT(bv)->ClearSelection();
848 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
849 UpdateLocal(bv, CURSOR, false);
852 bv->text->FinishUndo();
853 TEXT(bv)->CursorHome(bv);
854 TEXT(bv)->ClearSelection();
855 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
856 UpdateLocal(bv, CURSOR, false);
859 TEXT(bv)->CursorEnd(bv);
860 TEXT(bv)->ClearSelection();
861 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
862 UpdateLocal(bv, CURSOR, false);
865 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
867 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
868 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
870 bv->text->cursor.par()->previous,
871 bv->text->cursor.par()->next
874 if (TEXT(bv)->selection)
875 TEXT(bv)->CutSelection(bv);
877 TEXT(bv)->Backspace(bv);
878 UpdateLocal(bv, CURSOR_PAR, true);
881 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
883 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
884 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
886 bv->text->cursor.par()->previous,
887 bv->text->cursor.par()->next
890 if (TEXT(bv)->selection)
891 TEXT(bv)->CutSelection(bv);
893 TEXT(bv)->Delete(bv);
894 UpdateLocal(bv, CURSOR_PAR, true);
897 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
899 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
900 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
902 bv->text->cursor.par()->previous,
903 bv->text->cursor.par()->next
906 TEXT(bv)->CutSelection(bv);
907 UpdateLocal(bv, CURSOR_PAR, true);
910 bv->text->FinishUndo();
911 TEXT(bv)->CopySelection(bv);
912 UpdateLocal(bv, CURSOR_PAR, false);
914 case LFUN_PASTESELECTION:
916 string clip(bv->workarea()->getClipboard());
920 if (arg == "paragraph") {
921 TEXT(bv)->InsertStringB(bv, clip);
923 TEXT(bv)->InsertStringA(bv, clip);
925 UpdateLocal(bv, CURSOR_PAR, true);
929 if (!autoBreakRows) {
932 if (cap.nrOfParagraphs() > 1) {
933 WriteAlert(_("Impossible operation"),
934 _("Cannot include more than one paragraph!"),
939 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
941 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
942 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
944 bv->text->cursor.par()->previous,
945 bv->text->cursor.par()->next
948 TEXT(bv)->PasteSelection(bv);
949 UpdateLocal(bv, CURSOR_PAR, true);
951 case LFUN_BREAKPARAGRAPH:
954 TEXT(bv)->BreakParagraph(bv, 0);
955 UpdateLocal(bv, FULL, true);
960 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
962 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
963 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
965 bv->text->cursor.par()->previous,
966 bv->text->cursor.par()->next
969 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
970 UpdateLocal(bv, CURSOR_PAR, true);
973 // do not set layouts on non breakable textinsets
975 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
977 // Derive layout number from given argument (string)
978 // and current buffer's textclass (number). */
979 LyXTextClassList::ClassList::size_type tclass =
980 bv->buffer()->params.textclass;
981 std::pair <bool, LyXTextClass::size_type> layout =
982 textclasslist.NumberOfLayout(tclass, arg);
984 // If the entry is obsolete, use the new one instead.
986 string obs = textclasslist.Style(tclass,layout.second).
989 layout = textclasslist.NumberOfLayout(tclass, obs);
992 // see if we found the layout number:
994 string msg = string(N_("Layout ")) + arg + N_(" not known");
996 bv->owner()->getMiniBuffer()->Set(msg);
1000 if (cur_layout != layout.second) {
1001 cur_layout = layout.second;
1002 TEXT(bv)->SetLayout(bv, layout.second);
1003 bv->owner()->setLayout(cpar(bv)->GetLayout());
1004 UpdateLocal(bv, CURSOR_PAR, true);
1007 // reset the layout box
1008 bv->owner()->setLayout(cpar(bv)->GetLayout());
1011 case LFUN_PARAGRAPH_SPACING:
1012 // This one is absolutely not working. When fiddling with this
1013 // it also seems to me that the paragraphs inside the insettext
1014 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1016 LyXParagraph * par = TEXT(bv)->cursor.par();
1017 Spacing::Space cur_spacing = par->spacing.getSpace();
1018 float cur_value = 1.0;
1019 if (cur_spacing == Spacing::Other) {
1020 cur_value = par->spacing.getValue();
1023 std::istringstream istr(arg.c_str());
1026 Spacing::Space new_spacing = cur_spacing;
1027 float new_value = cur_value;
1029 lyxerr << "Missing argument to `paragraph-spacing'"
1031 } else if (tmp == "single") {
1032 new_spacing = Spacing::Single;
1033 } else if (tmp == "onehalf") {
1034 new_spacing = Spacing::Onehalf;
1035 } else if (tmp == "double") {
1036 new_spacing = Spacing::Double;
1037 } else if (tmp == "other") {
1038 new_spacing = Spacing::Other;
1041 lyxerr << "new_value = " << tmpval << endl;
1044 } else if (tmp == "default") {
1045 new_spacing = Spacing::Default;
1047 lyxerr << _("Unknown spacing argument: ")
1050 if (cur_spacing != new_spacing || cur_value != new_value) {
1051 par->spacing.set(new_spacing, new_value);
1052 //TEXT(bv)->RedoParagraph(owner->view());
1053 UpdateLocal(bv, CURSOR_PAR, true);
1054 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1060 result = UNDISPATCHED;
1063 if (result != FINISHED) {
1064 ShowInsetCursor(bv);
1066 bv->unlockInset(this);
1071 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1074 buf->latexParagraphs(os, par, 0, texrow);
1075 return texrow.rows();
1079 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1081 LyXParagraph * p = par;
1082 unsigned int lines = 0;
1086 tmp = buf->asciiParagraph(p, linelen);
1087 lines += countChar(tmp, '\n');
1095 void InsetText::Validate(LaTeXFeatures & features) const
1097 LyXParagraph * p = par;
1099 p->validate(features);
1105 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1107 if (textclasslist.Style(buf->params.textclass,
1108 p->GetLayout()).labeltype != LABEL_MANUAL)
1111 return p->BeginningOfMainBody();
1115 void InsetText::GetCursorPos(BufferView * bv,
1116 int & x, int & y) const
1123 unsigned int InsetText::InsetInInsetY()
1125 if (!the_locking_inset)
1128 return (inset_y + the_locking_inset->InsetInInsetY());
1132 void InsetText::ToggleInsetCursor(BufferView * bv)
1134 if (the_locking_inset) {
1135 the_locking_inset->ToggleInsetCursor(bv);
1139 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1141 int asc = lyxfont::maxAscent(font);
1142 int desc = lyxfont::maxDescent(font);
1145 bv->hideLockedInsetCursor();
1147 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1148 cursor_visible = !cursor_visible;
1152 void InsetText::ShowInsetCursor(BufferView * bv, bool show)
1154 if (the_locking_inset) {
1155 the_locking_inset->ShowInsetCursor(bv);
1158 if (!cursor_visible) {
1159 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1161 int asc = lyxfont::maxAscent(font);
1162 int desc = lyxfont::maxDescent(font);
1164 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1166 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1167 cursor_visible = true;
1172 void InsetText::HideInsetCursor(BufferView * bv)
1174 if (cursor_visible) {
1175 bv->hideLockedInsetCursor();
1176 cursor_visible = false;
1178 if (the_locking_inset)
1179 the_locking_inset->HideInsetCursor(bv);
1183 UpdatableInset::RESULT
1184 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1186 if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1188 if (activate_inset && checkAndActivateInset(bv, false))
1190 TEXT(bv)->CursorRight(bv, selecting);
1191 return DISPATCHED_NOUPDATE;
1195 UpdatableInset::RESULT
1196 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1198 if (!cpar(bv)->previous && (cpos(bv) <= 0))
1200 TEXT(bv)->CursorLeft(bv, selecting);
1201 if (activate_inset && checkAndActivateInset(bv, true))
1203 return DISPATCHED_NOUPDATE;
1207 UpdatableInset::RESULT
1208 InsetText::moveUp(BufferView * bv)
1210 if (!crow(bv)->previous())
1212 TEXT(bv)->CursorUp(bv);
1213 return DISPATCHED_NOUPDATE;
1217 UpdatableInset::RESULT
1218 InsetText::moveDown(BufferView * bv)
1220 if (!crow(bv)->next())
1222 TEXT(bv)->CursorDown(bv);
1223 return DISPATCHED_NOUPDATE;
1227 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1229 if (the_locking_inset) {
1230 if (the_locking_inset->InsertInsetAllowed(inset))
1231 return the_locking_inset->InsertInset(bv, inset);
1234 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1236 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1237 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1239 bv->text->cursor.par()->previous,
1240 bv->text->cursor.par()->next
1243 if (inset->Editable() == Inset::IS_EDITABLE) {
1244 UpdatableInset * i = static_cast<UpdatableInset *>(inset);
1245 i->setOwner(static_cast<UpdatableInset *>(this));
1247 HideInsetCursor(bv);
1248 TEXT(bv)->InsertInset(bv, inset);
1249 TEXT(bv)->selection = 0;
1250 bv->fitCursor(TEXT(bv));
1251 UpdateLocal(bv, CURSOR_PAR, true);
1252 static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1253 ShowInsetCursor(bv);
1258 UpdatableInset * InsetText::GetLockingInset()
1260 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1264 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1268 if (the_locking_inset)
1269 return the_locking_inset->GetFirstLockingInsetOfType(c);
1274 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1276 bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1278 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1279 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1281 bv->text->cursor.par()->previous,
1282 bv->text->cursor.par()->next
1285 TEXT(bv)->SetFont(bv, font, toggleall);
1286 bv->fitCursor(TEXT(bv));
1287 UpdateLocal(bv, CURSOR_PAR, true);
1291 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1293 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1297 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1298 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1300 LyXFont const font =
1301 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1303 x = inset->width(bv, font);
1304 y = inset->descent(bv, font);
1308 inset_x = cx(bv) - top_x + drawTextXOffset;
1309 inset_y = cy(bv) + drawTextYOffset;
1310 inset->Edit(bv, x - inset_x, y - inset_y, 0);
1311 if (!the_locking_inset)
1313 UpdateLocal(bv, CURSOR_PAR, false);
1320 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1323 x = x - drawTextXOffset;
1324 y = y + insetAscent;
1325 Inset * inset = bv->checkInsetHit(TEXT(bv), x, y, button);
1332 inset_x = cx(bv) - top_x + drawTextXOffset;
1333 inset_y = cy(bv) + drawTextYOffset;
1334 inset->Edit(bv, x - inset_x, y - inset_y, button);
1335 if (!the_locking_inset)
1337 UpdateLocal(bv, CURSOR_PAR, false);
1344 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1346 int w = UpdatableInset::getMaxWidth(pain, inset);
1351 w = w - top_x + owner()->x();
1354 w -= (2 * TEXT_TO_INSET_OFFSET);
1356 // return w - (2*TEXT_TO_INSET_OFFSET);
1360 void InsetText::SetParagraphData(LyXParagraph *p)
1374 par->SetInsetOwner(this);
1378 np->next = p->Clone();
1379 np->next->previous = np;
1381 np->SetInsetOwner(this);
1387 void InsetText::SetAutoBreakRows(bool flag)
1389 if (flag != autoBreakRows) {
1390 autoBreakRows = flag;
1398 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1400 if (how != drawFrame) {
1403 UpdateLocal(bv, DRAW_FRAME, false);
1408 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1410 if (frame_color != col) {
1413 UpdateLocal(bv, DRAW_FRAME, false);
1418 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1420 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1425 int InsetText::cx(BufferView * bv) const
1427 return TEXT(bv)->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1431 int InsetText::cy(BufferView * bv) const
1434 return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1438 LyXParagraph::size_type InsetText::cpos(BufferView * bv) const
1440 return TEXT(bv)->cursor.pos();
1444 LyXParagraph * InsetText::cpar(BufferView * bv) const
1446 return TEXT(bv)->cursor.par();
1450 Row * InsetText::crow(BufferView * bv) const
1452 return TEXT(bv)->cursor.row();
1456 LyXText * InsetText::getLyXText(BufferView * bv) const
1458 if (cache.find(bv) != cache.end())
1460 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1463 if (the_locking_inset) {
1464 lt->SetCursor(bv, inset_par, inset_pos);
1470 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1472 if (cache.find(bv) == cache.end())
1477 /// then remove all LyXText in text-insets
1478 LyXParagraph * p = par;
1479 for(;p;p = p->next) {
1480 p->deleteInsetsLyXText(bv);
1486 void InsetText::resizeLyXText(BufferView * bv) const
1488 if (!par->next && !par->size()) // resize not neccessary!
1490 if (cache.find(bv) == cache.end())
1493 LyXParagraph * lpar = 0;
1494 LyXParagraph * selstartpar = 0;
1495 LyXParagraph * selendpar = 0;
1496 LyXParagraph::size_type pos = 0;
1497 LyXParagraph::size_type selstartpos = 0;
1498 LyXParagraph::size_type selendpos = 0;
1502 // ProhibitInput(bv);
1505 lpar = TEXT(bv)->cursor.par();
1506 pos = TEXT(bv)->cursor.pos();
1507 selstartpar = TEXT(bv)->sel_start_cursor.par();
1508 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1509 selendpar = TEXT(bv)->sel_end_cursor.par();
1510 selendpos = TEXT(bv)->sel_end_cursor.pos();
1511 selection = TEXT(bv)->selection;
1512 mark_set = TEXT(bv)->mark_set;
1514 deleteLyXText(bv, (the_locking_inset == 0));
1517 TEXT(bv)->selection = true;
1518 /* at this point just to avoid the Delete-Empty-Paragraph
1519 * Mechanism when setting the cursor */
1520 TEXT(bv)->mark_set = mark_set;
1522 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos);
1523 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1524 TEXT(bv)->SetCursor(bv, selendpar, selendpos);
1525 TEXT(bv)->SetSelection();
1526 TEXT(bv)->SetCursor(bv, lpar, pos);
1528 TEXT(bv)->SetCursor(bv, lpar, pos);
1529 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1530 TEXT(bv)->selection = false;
1534 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1535 // this will scroll the screen such that the cursor becomes visible
1536 bv->updateScrollbar();
1538 if (the_locking_inset) {
1539 /// then resize all LyXText in text-insets
1540 inset_x = cx(bv) - top_x + drawTextXOffset;
1541 inset_y = cy(bv) + drawTextYOffset;
1542 for(LyXParagraph * p = par; p; p = p->next) {
1543 p->resizeInsetsLyXText(bv);
1550 void InsetText::removeNewlines()
1552 for(LyXParagraph * p = par; p; p = p->next) {
1553 for(int i = 0; i < p->Last(); ++i) {
1554 if (p->GetChar(i) == LyXParagraph::META_NEWLINE)