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::const_iterator cit = cache.begin(); cit != cache.end(); ++cit)
118 delete (*cit).second;
119 LyXParagraph * p = par->next;
129 void InsetText::clear()
131 // delete all instances of LyXText before deleting the paragraps used
133 for(Cache::const_iterator cit = cache.begin(); cit != cache.end(); ++cit)
134 delete (*cit).second;
135 LyXParagraph * p = par->next;
142 par = new LyXParagraph();
146 Inset * InsetText::Clone(Buffer const &) const
148 InsetText * t = new InsetText(*this);
153 void InsetText::Write(Buffer const * buf, ostream & os) const
156 WriteParagraphData(buf, os);
160 void InsetText::WriteParagraphData(Buffer const * buf, ostream & os) const
162 par->writeFile(buf, os, buf->params, 0, 0);
166 void InsetText::Read(Buffer const * buf, LyXLex & lex)
170 LyXParagraph * return_par = 0;
171 char depth = 0; // signed or unsigned?
173 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
174 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
176 LyXFont font(LyXFont::ALL_INHERIT);
178 // delete all instances of LyXText before deleting the paragraps used
180 for(Cache::const_iterator cit = cache.begin(); cit != cache.end(); ++cit)
181 delete (*cit).second;
183 LyXParagraph * p = par->next;
190 par = new LyXParagraph;
193 token = lex.GetString();
196 if (token == "\\end_inset")
198 if (const_cast<Buffer*>(buf)->
199 parseSingleLyXformat2Token(lex, par, return_par,token, pos, depth,
202 , footnoteflag, footnotekind
206 // the_end read this should NEVER happen
207 lex.printError("\\the_end read in inset! Error in document!");
215 return_par->SetInsetOwner(this);
216 return_par = return_par->next;
219 if (token != "\\end_inset") {
220 lex.printError("Missing \\end_inset at this point. "
227 int InsetText::ascent(BufferView * bv, LyXFont const &) const
230 Row * row = TEXT(bv)->GetRowNearY(y_temp);
231 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
236 int InsetText::descent(BufferView * bv, LyXFont const &) const
239 Row * row = TEXT(bv)->GetRowNearY(y_temp);
240 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
241 TEXT_TO_INSET_OFFSET;
246 int InsetText::width(BufferView * bv, LyXFont const &) const
248 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
253 int InsetText::textWidth(Painter & pain) const
255 int w = getMaxWidth(pain, this);
260 void InsetText::draw(BufferView * bv, LyXFont const & f,
261 int baseline, float & x, bool cleared) const
263 Painter & pain = bv->painter();
265 // no draw is necessary !!!
266 if ((drawFrame == LOCKED) && !locked && !par->size()) {
267 if (!cleared && (need_update == CLEAR_FRAME)) {
268 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
270 insetAscent + insetDescent - 1,
274 top_baseline = baseline;
281 UpdatableInset::draw(bv, f, baseline, x, cleared);
283 if (!cleared && ((need_update==FULL) || (top_x!=int(x)) ||
284 (top_baseline!=baseline))) {
286 int h = insetAscent + insetDescent;
287 int ty = baseline - insetAscent;
293 if ((ty + h) > pain.paperHeight())
294 h = pain.paperHeight();
295 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
296 w = pain.paperWidth();
297 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
301 if (!cleared && (need_update == NONE))
304 if (top_x != int(x)) {
307 bv->text->status = LyXText::CHANGED_IN_DRAW;
311 top_baseline = baseline;
312 top_y = baseline - ascent(bv, f);
313 last_width = width(bv, f);
314 last_height = ascent(bv, f) + descent(bv, f);
316 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
317 inset_x = cx(bv) - top_x + drawTextXOffset;
318 inset_y = cy(bv) + drawTextYOffset;
320 if (!cleared && (need_update == CURSOR) && !TEXT(bv)->selection) {
325 x += TEXT_TO_INSET_OFFSET;
328 Row * row = TEXT(bv)->GetRowNearY(y);
329 int y_offset = baseline - row->ascent_of_text();
330 int ph = pain.paperHeight();
333 while ((row != 0) && ((y+row->height()) <= 0)) {
335 first += row->height();
340 TEXT(bv)->first = first;
341 if (cleared || !locked || (need_update == FULL)) {
344 while ((row != 0) && (yf < ph)) {
345 TEXT(bv)->GetVisibleRow(bv, y+y_offset, int(x), row,
351 } else if (need_update == SELECTION) {
352 bv->screen()->ToggleToggle(TEXT(bv), y_offset, int(x));
355 if (need_update == CURSOR) {
356 bv->screen()->ToggleSelection(TEXT(bv), true, y_offset,int(x));
357 TEXT(bv)->ClearSelection();
358 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
360 bv->screen()->Update(TEXT(bv), y_offset, int(x));
364 TEXT(bv)->refresh_y = 0;
365 TEXT(bv)->status = LyXText::UNCHANGED;
366 if ((need_update != CURSOR_PAR) &&
367 ((drawFrame == ALWAYS) || ((drawFrame == LOCKED) && locked)))
369 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
370 width(bv, f) - 1, insetAscent + insetDescent - 1,
372 } else if (need_update == CLEAR_FRAME) {
373 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
374 width(bv, f) - 1, insetAscent + insetDescent - 1,
377 x += width(bv, f) - TEXT_TO_INSET_OFFSET;
378 if (bv->text->status==LyXText::CHANGED_IN_DRAW)
380 else if (need_update != INIT)
385 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
387 if (reinit) { // && (need_update != CURSOR)) {
391 owner()->update(bv, font, true);
394 if (the_locking_inset) {
395 inset_x = cx(bv) - top_x + drawTextXOffset;
396 inset_y = cy(bv) + drawTextYOffset;
397 the_locking_inset->update(bv, font, reinit);
399 if (need_update == INIT) {
402 // if (!owner() && bv->text)
403 // bv->text->UpdateInset(bv, this);
405 int oldw = insetWidth;
407 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
408 // max(textWidth(bv->painter()),
409 // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
410 // (2 * TEXT_TO_INSET_OFFSET);
412 insetWidth = textWidth(bv->painter());
414 insetWidth = static_cast<int>(TEXT(bv)->width);
416 if (oldw != insetWidth) {
417 // printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
418 // textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
423 owner()->update(bv, font, reinit);
426 update(bv, font, reinit);
430 update(bv, font, reinit);
432 UpdateLocal(bv, INIT, false);
437 if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
440 TEXT(bv)->UpdateInset(bv, the_locking_inset);
443 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
447 Row * row = TEXT(bv)->GetRowNearY(y_temp);
448 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
449 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
450 TEXT_TO_INSET_OFFSET;
454 void InsetText::UpdateLocal(BufferView * bv, UpdateCodes what, bool mark_dirty)
456 TEXT(bv)->FullRebreak(bv);
457 if (need_update != INIT) {
458 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
460 else if (!the_locking_inset || (what != CURSOR))
463 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
465 bv->updateInset(this, mark_dirty);
466 bv->owner()->showState();
467 if (old_par != cpar(bv)) {
468 bv->owner()->setLayout(cpar(bv)->GetLayout());
474 string const InsetText::EditMessage() const
476 return _("Opened Text Inset");
480 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
482 // par->SetInsetOwner(this);
483 UpdatableInset::Edit(bv, x, y, button);
485 if (!bv->lockInset(this)) {
486 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
490 the_locking_inset = 0;
491 inset_pos = inset_x = inset_y = 0;
494 if (!checkAndActivateInset(bv, x, y, button))
495 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
497 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
498 bv->text->FinishUndo();
500 UpdateLocal(bv, FULL, false);
504 void InsetText::InsetUnlock(BufferView * bv)
506 if (the_locking_inset) {
507 the_locking_inset->InsetUnlock(bv);
508 the_locking_inset = 0;
511 no_selection = false;
513 TEXT(bv)->selection = 0;
514 UpdateLocal(bv, CLEAR_FRAME, false);
516 bv->owner()->setLayout(owner()->getLyXText(bv)
517 ->cursor.par()->GetLayout());
519 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
523 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
525 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
528 if (inset == cpar(bv)->GetInset(cpos(bv))) {
529 lyxerr[Debug::INSETS] << "OK" << endl;
530 the_locking_inset = inset;
531 inset_x = cx(bv) - top_x + drawTextXOffset;
532 inset_y = cy(bv) + drawTextYOffset;
533 inset_pos = cpos(bv);
534 inset_par = cpar(bv);
535 TEXT(bv)->UpdateInset(bv, the_locking_inset);
537 } else if (the_locking_inset && (the_locking_inset == inset)) {
538 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
539 lyxerr[Debug::INSETS] << "OK" << endl;
540 inset_x = cx(bv) - top_x + drawTextXOffset;
541 inset_y = cy(bv) + drawTextYOffset;
543 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
545 } else if (the_locking_inset) {
546 lyxerr[Debug::INSETS] << "MAYBE" << endl;
547 return the_locking_inset->LockInsetInInset(bv, inset);
549 lyxerr[Debug::INSETS] << "NOT OK" << endl;
554 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
557 if (!the_locking_inset)
559 if (the_locking_inset == inset) {
560 the_locking_inset->InsetUnlock(bv);
561 TEXT(bv)->UpdateInset(bv, inset);
562 the_locking_inset = 0;
564 moveRight(bv, false);
565 old_par = 0; // force layout setting
566 UpdateLocal(bv, CURSOR_PAR, false);
569 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
573 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
575 if (!the_locking_inset)
577 if (the_locking_inset != inset) {
578 TEXT(bv)->UpdateInset(bv, the_locking_inset);
579 need_update = CURSOR_PAR;
580 return the_locking_inset->UpdateInsetInInset(bv, inset);
582 // UpdateLocal(bv, FULL, false);
583 if (TEXT(bv)->UpdateInset(bv, inset))
584 UpdateLocal(bv, CURSOR_PAR, false);
585 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
586 inset_x = cx(bv) - top_x + drawTextXOffset;
587 inset_y = cy(bv) + drawTextYOffset;
593 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
595 no_selection = false;
597 int tmp_x = x - drawTextXOffset;
598 int tmp_y = y + insetAscent - TEXT(bv)->first;
599 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
602 if (the_locking_inset) {
603 if (the_locking_inset == inset) {
604 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
607 // otherwise unlock the_locking_inset and lock the new inset
608 the_locking_inset->InsetUnlock(bv);
609 inset_x = cx(bv) - top_x + drawTextXOffset;
610 inset_y = cy(bv) + drawTextYOffset;
611 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
612 inset->Edit(bv, x - inset_x, y - inset_y, button);
613 if (the_locking_inset) {
614 UpdateLocal(bv, CURSOR_PAR, false);
618 // otherwise only unlock the_locking_inset
619 the_locking_inset->InsetUnlock(bv);
620 the_locking_inset = 0;
622 if (bv->theLockingInset()) {
623 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
624 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
625 inset_x = cx(bv) - top_x + drawTextXOffset;
626 inset_y = cy(bv) + drawTextYOffset;
627 inset_pos = cpos(bv);
628 inset_par = cpar(bv);
629 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
630 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
631 // TEXT(bv)->ClearSelection();
632 if (the_locking_inset) {
633 UpdateLocal(bv, CURSOR_PAR, false);
639 bool paste_internally = false;
640 if ((button == 2) && TEXT(bv)->selection) {
641 LocalDispatch(bv, LFUN_COPY, "");
642 paste_internally = true;
644 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
646 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
647 UpdateLocal(bv, CURSOR, false);
648 bv->owner()->setLayout(cpar(bv)->GetLayout());
650 // Insert primary selection with middle mouse
651 // if there is a local selection in the current buffer,
654 if (paste_internally)
655 LocalDispatch(bv, LFUN_PASTE, "");
657 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
664 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
666 UpdatableInset * inset = 0;
668 if (the_locking_inset) {
669 the_locking_inset->InsetButtonRelease(bv,
670 x - inset_x, y - inset_y,
673 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
674 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
675 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
676 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
678 inset_x = cx(bv) - top_x + drawTextXOffset;
679 inset_y = cy(bv) + drawTextYOffset;
680 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
681 inset->Edit(bv, x - inset_x, y - inset_y, button);
683 UpdateLocal(bv, CURSOR_PAR, false);
686 no_selection = false;
690 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
692 if (the_locking_inset) {
693 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
699 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
701 TEXT(bv)->SetSelection();
702 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
703 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
704 UpdateLocal(bv, SELECTION, false);
707 no_selection = false;
711 void InsetText::InsetKeyPress(XKeyEvent * xke)
713 if (the_locking_inset) {
714 the_locking_inset->InsetKeyPress(xke);
720 UpdatableInset::RESULT
721 InsetText::LocalDispatch(BufferView * bv,
722 int action, string const & arg)
724 no_selection = false;
725 UpdatableInset::RESULT
726 result= UpdatableInset::LocalDispatch(bv, action, arg);
727 if (result != UNDISPATCHED) {
732 if ((action < 0) && arg.empty())
735 if (the_locking_inset) {
736 result = the_locking_inset->LocalDispatch(bv, action, arg);
737 if (result == DISPATCHED_NOUPDATE)
739 else if (result == DISPATCHED) {
740 UpdateLocal(bv, CURSOR_PAR, false);
742 } else if (result == FINISHED) {
746 moveRight(bv, false);
752 the_locking_inset = 0;
760 if (bv->buffer()->isReadonly()) {
762 // setErrorMessage(N_("Document is read only"));
766 /* Automatically delete the currently selected
767 * text and replace it with what is being
768 * typed in now. Depends on lyxrc settings
769 * "auto_region_delete", which defaults to
772 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
774 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
775 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
777 bv->text->cursor.par()->previous,
778 bv->text->cursor.par()->next
781 // if an empty paragraph set the language to the surronding
782 // paragraph language on insertion of the first character!
783 if (!par->Last() && !par->next) {
786 Inset * inset = owner();
787 while(inset && inset->getLyXText(bv) == TEXT(bv))
788 inset = inset->owner();
790 text = inset->getLyXText(bv);
794 LyXFont font(LyXFont::ALL_IGNORE);
795 font.setLanguage(text->cursor.par()->getParLanguage(bv->buffer()->params));
796 SetFont(bv, font, false);
798 if (lyxrc.auto_region_delete) {
799 if (TEXT(bv)->selection){
800 TEXT(bv)->CutSelection(bv, false);
803 TEXT(bv)->ClearSelection();
804 for (string::size_type i = 0; i < arg.length(); ++i) {
805 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
808 UpdateLocal(bv, CURSOR_PAR, true);
809 result=DISPATCHED_NOUPDATE;
811 // --- Cursor Movements ---------------------------------------------
813 bv->text->FinishUndo();
814 moveRight(bv, false, true);
815 TEXT(bv)->SetSelection();
816 UpdateLocal(bv, SELECTION, false);
819 result = moveRight(bv);
820 bv->text->FinishUndo();
821 TEXT(bv)->ClearSelection();
822 UpdateLocal(bv, CURSOR, false);
825 bv->text->FinishUndo();
826 moveLeft(bv, false, true);
827 TEXT(bv)->SetSelection();
828 UpdateLocal(bv, SELECTION, false);
831 bv->text->FinishUndo();
832 result= moveLeft(bv);
833 TEXT(bv)->ClearSelection();
834 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
835 UpdateLocal(bv, CURSOR, false);
838 bv->text->FinishUndo();
840 TEXT(bv)->SetSelection();
841 UpdateLocal(bv, SELECTION, false);
844 bv->text->FinishUndo();
845 result = moveDown(bv);
846 TEXT(bv)->ClearSelection();
847 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
848 UpdateLocal(bv, CURSOR, false);
851 bv->text->FinishUndo();
853 TEXT(bv)->SetSelection();
854 UpdateLocal(bv, SELECTION, false);
857 bv->text->FinishUndo();
859 TEXT(bv)->ClearSelection();
860 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
861 UpdateLocal(bv, CURSOR, false);
864 bv->text->FinishUndo();
865 TEXT(bv)->CursorHome(bv);
866 TEXT(bv)->ClearSelection();
867 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
868 UpdateLocal(bv, CURSOR, false);
871 TEXT(bv)->CursorEnd(bv);
872 TEXT(bv)->ClearSelection();
873 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
874 UpdateLocal(bv, CURSOR, false);
877 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
879 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
880 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
882 bv->text->cursor.par()->previous,
883 bv->text->cursor.par()->next
886 if (TEXT(bv)->selection)
887 TEXT(bv)->CutSelection(bv);
889 TEXT(bv)->Backspace(bv);
890 UpdateLocal(bv, CURSOR_PAR, true);
893 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
895 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
896 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
898 bv->text->cursor.par()->previous,
899 bv->text->cursor.par()->next
902 if (TEXT(bv)->selection)
903 TEXT(bv)->CutSelection(bv);
905 TEXT(bv)->Delete(bv);
906 UpdateLocal(bv, CURSOR_PAR, true);
909 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
911 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
912 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
914 bv->text->cursor.par()->previous,
915 bv->text->cursor.par()->next
918 TEXT(bv)->CutSelection(bv);
919 UpdateLocal(bv, CURSOR_PAR, true);
922 bv->text->FinishUndo();
923 TEXT(bv)->CopySelection(bv);
924 UpdateLocal(bv, CURSOR_PAR, false);
926 case LFUN_PASTESELECTION:
928 string clip(bv->workarea()->getClipboard());
932 if (arg == "paragraph") {
933 TEXT(bv)->InsertStringB(bv, clip);
935 TEXT(bv)->InsertStringA(bv, clip);
937 UpdateLocal(bv, CURSOR_PAR, true);
941 if (!autoBreakRows) {
944 if (cap.nrOfParagraphs() > 1) {
945 WriteAlert(_("Impossible operation"),
946 _("Cannot include more than one paragraph!"),
951 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
953 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
954 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
956 bv->text->cursor.par()->previous,
957 bv->text->cursor.par()->next
960 TEXT(bv)->PasteSelection(bv);
961 UpdateLocal(bv, CURSOR_PAR, true);
963 case LFUN_BREAKPARAGRAPH:
966 TEXT(bv)->BreakParagraph(bv, 0);
967 UpdateLocal(bv, FULL, true);
972 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
974 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
975 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
977 bv->text->cursor.par()->previous,
978 bv->text->cursor.par()->next
981 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
982 UpdateLocal(bv, CURSOR_PAR, true);
985 // do not set layouts on non breakable textinsets
987 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
989 // Derive layout number from given argument (string)
990 // and current buffer's textclass (number). */
991 LyXTextClassList::ClassList::size_type tclass =
992 bv->buffer()->params.textclass;
993 std::pair <bool, LyXTextClass::size_type> layout =
994 textclasslist.NumberOfLayout(tclass, arg);
996 // If the entry is obsolete, use the new one instead.
998 string obs = textclasslist.Style(tclass,layout.second).
1001 layout = textclasslist.NumberOfLayout(tclass, obs);
1004 // see if we found the layout number:
1005 if (!layout.first) {
1006 string msg = string(N_("Layout ")) + arg + N_(" not known");
1008 bv->owner()->getMiniBuffer()->Set(msg);
1012 if (cur_layout != layout.second) {
1013 cur_layout = layout.second;
1014 TEXT(bv)->SetLayout(bv, layout.second);
1015 bv->owner()->setLayout(cpar(bv)->GetLayout());
1016 UpdateLocal(bv, CURSOR_PAR, true);
1019 // reset the layout box
1020 bv->owner()->setLayout(cpar(bv)->GetLayout());
1023 case LFUN_PARAGRAPH_SPACING:
1024 // This one is absolutely not working. When fiddling with this
1025 // it also seems to me that the paragraphs inside the insettext
1026 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1028 LyXParagraph * par = TEXT(bv)->cursor.par();
1029 Spacing::Space cur_spacing = par->spacing.getSpace();
1030 float cur_value = 1.0;
1031 if (cur_spacing == Spacing::Other) {
1032 cur_value = par->spacing.getValue();
1035 std::istringstream istr(arg.c_str());
1038 Spacing::Space new_spacing = cur_spacing;
1039 float new_value = cur_value;
1041 lyxerr << "Missing argument to `paragraph-spacing'"
1043 } else if (tmp == "single") {
1044 new_spacing = Spacing::Single;
1045 } else if (tmp == "onehalf") {
1046 new_spacing = Spacing::Onehalf;
1047 } else if (tmp == "double") {
1048 new_spacing = Spacing::Double;
1049 } else if (tmp == "other") {
1050 new_spacing = Spacing::Other;
1053 lyxerr << "new_value = " << tmpval << endl;
1056 } else if (tmp == "default") {
1057 new_spacing = Spacing::Default;
1059 lyxerr << _("Unknown spacing argument: ")
1062 if (cur_spacing != new_spacing || cur_value != new_value) {
1063 par->spacing.set(new_spacing, new_value);
1064 //TEXT(bv)->RedoParagraph(owner->view());
1065 UpdateLocal(bv, CURSOR_PAR, true);
1066 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1072 result = UNDISPATCHED;
1075 if (result != FINISHED) {
1076 ShowInsetCursor(bv);
1078 bv->unlockInset(this);
1083 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1086 buf->latexParagraphs(os, par, 0, texrow);
1087 return texrow.rows();
1091 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1093 LyXParagraph * p = par;
1094 unsigned int lines = 0;
1098 tmp = buf->asciiParagraph(p, linelen);
1099 lines += countChar(tmp, '\n');
1107 int InsetText::DocBook(Buffer const * buf, ostream & os) const
1109 LyXParagraph * p = par;
1110 unsigned int lines = 0;
1115 buf->SimpleDocBookOnePar(os,tmp,p,desc,0);
1123 void InsetText::Validate(LaTeXFeatures & features) const
1125 LyXParagraph * p = par;
1127 p->validate(features);
1133 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1135 if (textclasslist.Style(buf->params.textclass,
1136 p->GetLayout()).labeltype != LABEL_MANUAL)
1139 return p->BeginningOfMainBody();
1143 void InsetText::GetCursorPos(BufferView * bv,
1144 int & x, int & y) const
1151 unsigned int InsetText::InsetInInsetY()
1153 if (!the_locking_inset)
1156 return (inset_y + the_locking_inset->InsetInInsetY());
1160 void InsetText::ToggleInsetCursor(BufferView * bv)
1162 if (the_locking_inset) {
1163 the_locking_inset->ToggleInsetCursor(bv);
1167 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1169 int asc = lyxfont::maxAscent(font);
1170 int desc = lyxfont::maxDescent(font);
1173 bv->hideLockedInsetCursor();
1175 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1176 cursor_visible = !cursor_visible;
1180 void InsetText::ShowInsetCursor(BufferView * bv, bool show)
1182 if (the_locking_inset) {
1183 the_locking_inset->ShowInsetCursor(bv);
1186 if (!cursor_visible) {
1187 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1189 int asc = lyxfont::maxAscent(font);
1190 int desc = lyxfont::maxDescent(font);
1192 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1194 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1195 cursor_visible = true;
1200 void InsetText::HideInsetCursor(BufferView * bv)
1202 if (cursor_visible) {
1203 bv->hideLockedInsetCursor();
1204 cursor_visible = false;
1206 if (the_locking_inset)
1207 the_locking_inset->HideInsetCursor(bv);
1211 UpdatableInset::RESULT
1212 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1214 if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1216 if (activate_inset && checkAndActivateInset(bv, false))
1218 TEXT(bv)->CursorRight(bv, selecting);
1219 return DISPATCHED_NOUPDATE;
1223 UpdatableInset::RESULT
1224 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1226 if (!cpar(bv)->previous && (cpos(bv) <= 0))
1228 TEXT(bv)->CursorLeft(bv, selecting);
1229 if (activate_inset && checkAndActivateInset(bv, true))
1231 return DISPATCHED_NOUPDATE;
1235 UpdatableInset::RESULT
1236 InsetText::moveUp(BufferView * bv)
1238 if (!crow(bv)->previous())
1240 TEXT(bv)->CursorUp(bv);
1241 return DISPATCHED_NOUPDATE;
1245 UpdatableInset::RESULT
1246 InsetText::moveDown(BufferView * bv)
1248 if (!crow(bv)->next())
1250 TEXT(bv)->CursorDown(bv);
1251 return DISPATCHED_NOUPDATE;
1255 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1257 if (the_locking_inset) {
1258 if (the_locking_inset->InsertInsetAllowed(inset))
1259 return the_locking_inset->InsertInset(bv, inset);
1262 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1264 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1265 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1267 bv->text->cursor.par()->previous,
1268 bv->text->cursor.par()->next
1271 inset->setOwner(this);
1272 HideInsetCursor(bv);
1273 TEXT(bv)->InsertInset(bv, inset);
1274 if ((cpar(bv)->GetChar(cpos(bv)) != LyXParagraph::META_INSET) ||
1275 (cpar(bv)->GetInset(cpos(bv)) != inset))
1276 TEXT(bv)->CursorLeft(bv);
1277 TEXT(bv)->selection = 0;
1278 bv->fitCursor(TEXT(bv));
1279 UpdateLocal(bv, CURSOR_PAR, true);
1280 ShowInsetCursor(bv);
1285 UpdatableInset * InsetText::GetLockingInset()
1287 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1291 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1295 if (the_locking_inset)
1296 return the_locking_inset->GetFirstLockingInsetOfType(c);
1301 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1303 bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1305 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1306 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1308 bv->text->cursor.par()->previous,
1309 bv->text->cursor.par()->next
1312 TEXT(bv)->SetFont(bv, font, toggleall);
1313 bv->fitCursor(TEXT(bv));
1314 UpdateLocal(bv, CURSOR_PAR, true);
1318 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1320 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1324 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1325 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1327 LyXFont const font =
1328 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1330 x = inset->width(bv, font);
1331 y = inset->descent(bv, font);
1335 inset_x = cx(bv) - top_x + drawTextXOffset;
1336 inset_y = cy(bv) + drawTextYOffset;
1337 inset->Edit(bv, x - inset_x, y - inset_y, 0);
1338 if (!the_locking_inset)
1340 UpdateLocal(bv, CURSOR_PAR, false);
1347 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1350 x = x - drawTextXOffset;
1351 y = y + insetAscent;
1352 Inset * inset = bv->checkInsetHit(TEXT(bv), x, y, button);
1359 inset_x = cx(bv) - top_x + drawTextXOffset;
1360 inset_y = cy(bv) + drawTextYOffset;
1361 inset->Edit(bv, x - inset_x, y - inset_y, button);
1362 if (!the_locking_inset)
1364 UpdateLocal(bv, CURSOR_PAR, false);
1371 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1373 int w = UpdatableInset::getMaxWidth(pain, inset);
1378 w = w - top_x + owner()->x();
1381 w -= (2 * TEXT_TO_INSET_OFFSET);
1383 // return w - (2*TEXT_TO_INSET_OFFSET);
1387 void InsetText::SetParagraphData(LyXParagraph *p)
1389 // delete all instances of LyXText before deleting the paragraps used
1391 for(Cache::const_iterator cit = cache.begin(); cit != cache.end(); ++cit)
1392 delete (*cit).second;
1405 par->SetInsetOwner(this);
1409 np->next = p->Clone();
1410 np->next->previous = np;
1412 np->SetInsetOwner(this);
1418 void InsetText::SetAutoBreakRows(bool flag)
1420 if (flag != autoBreakRows) {
1421 autoBreakRows = flag;
1429 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1431 if (how != drawFrame) {
1434 UpdateLocal(bv, DRAW_FRAME, false);
1439 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1441 if (frame_color != col) {
1444 UpdateLocal(bv, DRAW_FRAME, false);
1449 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1451 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1456 int InsetText::cx(BufferView * bv) const
1458 return TEXT(bv)->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1462 int InsetText::cy(BufferView * bv) const
1465 return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1469 LyXParagraph::size_type InsetText::cpos(BufferView * bv) const
1471 return TEXT(bv)->cursor.pos();
1475 LyXParagraph * InsetText::cpar(BufferView * bv) const
1477 return TEXT(bv)->cursor.par();
1481 Row * InsetText::crow(BufferView * bv) const
1483 return TEXT(bv)->cursor.row();
1487 LyXText * InsetText::getLyXText(BufferView * bv) const
1489 if (cache.find(bv) != cache.end())
1491 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1494 if (the_locking_inset) {
1495 lt->SetCursor(bv, inset_par, inset_pos);
1501 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1503 if (cache.find(bv) == cache.end())
1508 /// then remove all LyXText in text-insets
1509 LyXParagraph * p = par;
1510 for(;p;p = p->next) {
1511 p->deleteInsetsLyXText(bv);
1517 void InsetText::resizeLyXText(BufferView * bv) const
1519 if (!par->next && !par->size()) // resize not neccessary!
1521 if (cache.find(bv) == cache.end())
1524 LyXParagraph * lpar = 0;
1525 LyXParagraph * selstartpar = 0;
1526 LyXParagraph * selendpar = 0;
1527 LyXParagraph::size_type pos = 0;
1528 LyXParagraph::size_type selstartpos = 0;
1529 LyXParagraph::size_type selendpos = 0;
1533 // ProhibitInput(bv);
1536 lpar = TEXT(bv)->cursor.par();
1537 pos = TEXT(bv)->cursor.pos();
1538 selstartpar = TEXT(bv)->sel_start_cursor.par();
1539 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1540 selendpar = TEXT(bv)->sel_end_cursor.par();
1541 selendpos = TEXT(bv)->sel_end_cursor.pos();
1542 selection = TEXT(bv)->selection;
1543 mark_set = TEXT(bv)->mark_set;
1545 deleteLyXText(bv, (the_locking_inset == 0));
1548 TEXT(bv)->selection = true;
1549 /* at this point just to avoid the Delete-Empty-Paragraph
1550 * Mechanism when setting the cursor */
1551 TEXT(bv)->mark_set = mark_set;
1553 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos);
1554 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1555 TEXT(bv)->SetCursor(bv, selendpar, selendpos);
1556 TEXT(bv)->SetSelection();
1557 TEXT(bv)->SetCursor(bv, lpar, pos);
1559 TEXT(bv)->SetCursor(bv, lpar, pos);
1560 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1561 TEXT(bv)->selection = false;
1565 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1566 // this will scroll the screen such that the cursor becomes visible
1567 bv->updateScrollbar();
1569 if (the_locking_inset) {
1570 /// then resize all LyXText in text-insets
1571 inset_x = cx(bv) - top_x + drawTextXOffset;
1572 inset_y = cy(bv) + drawTextYOffset;
1573 for(LyXParagraph * p = par; p; p = p->next) {
1574 p->resizeInsetsLyXText(bv);
1581 void InsetText::removeNewlines()
1583 for(LyXParagraph * p = par; p; p = p->next) {
1584 for(int i = 0; i < p->Last(); ++i) {
1585 if (p->GetChar(i) == LyXParagraph::META_NEWLINE)