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;
317 Row * row = TEXT(bv)->GetRowNearY(y);
318 int y_offset = baseline - row->ascent_of_text();
319 int ph = pain.paperHeight();
321 while ((row != 0) && ((y+row->height()) <= 0)) {
327 if (cleared || !locked || (need_update == FULL)) {
330 while ((row != 0) && (y < ph)) {
331 TEXT(bv)->GetVisibleRow(bv, y+first+y_offset, int(x), row,
336 } else if (need_update == SELECTION) {
337 bv->screen()->ToggleToggle(TEXT(bv), y+y_offset, int(x));
340 if (need_update == CURSOR) {
341 bv->screen()->ToggleSelection(TEXT(bv), true, y+y_offset, int(x));
342 TEXT(bv)->ClearSelection();
343 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
345 bv->screen()->Update(TEXT(bv), y, int(x));
348 TEXT(bv)->refresh_y = 0;
349 TEXT(bv)->status = LyXText::UNCHANGED;
350 if ((need_update != CURSOR_PAR) &&
351 ((drawFrame == ALWAYS) || ((drawFrame == LOCKED) && locked)))
353 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
354 width(bv, f) - 1, insetAscent + insetDescent - 1,
356 } else if (need_update == CLEAR_FRAME) {
357 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
358 width(bv, f) - 1, insetAscent + insetDescent - 1,
361 x += width(bv, f) - TEXT_TO_INSET_OFFSET;
362 if (bv->text->status==LyXText::CHANGED_IN_DRAW)
364 else if (need_update != INIT)
369 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
371 if (reinit) { // && (need_update != CURSOR)) {
375 owner()->update(bv, font, true);
378 if (the_locking_inset) {
379 inset_x = cx(bv) - top_x + drawTextXOffset;
380 inset_y = cy(bv) + drawTextYOffset;
381 the_locking_inset->update(bv, font, reinit);
383 if (need_update == INIT) {
386 // if (!owner() && bv->text)
387 // bv->text->UpdateInset(bv, this);
389 int oldw = insetWidth;
391 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
392 // max(textWidth(bv->painter()),
393 // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
394 // (2 * TEXT_TO_INSET_OFFSET);
396 insetWidth = textWidth(bv->painter());
398 insetWidth = static_cast<int>(TEXT(bv)->width);
400 if (oldw != insetWidth) {
401 // printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
402 // textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
407 owner()->update(bv, font, reinit);
410 update(bv, font, reinit);
414 update(bv, font, reinit);
416 UpdateLocal(bv, INIT, false);
421 if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
424 TEXT(bv)->UpdateInset(bv, the_locking_inset);
427 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
431 Row * row = TEXT(bv)->GetRowNearY(y_temp);
432 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
433 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
434 TEXT_TO_INSET_OFFSET;
438 void InsetText::UpdateLocal(BufferView * bv, UpdateCodes what, bool mark_dirty)
440 TEXT(bv)->FullRebreak(bv);
441 if (need_update != INIT) {
442 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
444 else if (!the_locking_inset || (what != CURSOR))
447 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
449 bv->updateInset(this, mark_dirty);
450 bv->owner()->showState();
451 if (old_par != cpar(bv)) {
452 bv->owner()->setLayout(cpar(bv)->GetLayout());
458 string const InsetText::EditMessage() const
460 return _("Opened Text Inset");
464 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
466 // par->SetInsetOwner(this);
467 UpdatableInset::Edit(bv, x, y, button);
469 if (!bv->lockInset(this)) {
470 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
474 the_locking_inset = 0;
475 inset_pos = inset_x = inset_y = 0;
478 if (!checkAndActivateInset(bv, x, y, button))
479 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
481 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
482 bv->text->FinishUndo();
484 UpdateLocal(bv, FULL, false);
488 void InsetText::InsetUnlock(BufferView * bv)
490 if (the_locking_inset) {
491 the_locking_inset->InsetUnlock(bv);
492 the_locking_inset = 0;
495 no_selection = false;
497 TEXT(bv)->selection = 0;
498 UpdateLocal(bv, CLEAR_FRAME, false);
500 bv->owner()->setLayout(owner()->getLyXText(bv)
501 ->cursor.par()->GetLayout());
503 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
507 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
509 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
512 if (inset == cpar(bv)->GetInset(cpos(bv))) {
513 lyxerr[Debug::INSETS] << "OK" << endl;
514 the_locking_inset = inset;
515 inset_x = cx(bv) - top_x + drawTextXOffset;
516 inset_y = cy(bv) + drawTextYOffset;
517 inset_pos = cpos(bv);
518 inset_par = cpar(bv);
519 TEXT(bv)->UpdateInset(bv, the_locking_inset);
521 } else if (the_locking_inset && (the_locking_inset == inset)) {
522 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
523 lyxerr[Debug::INSETS] << "OK" << endl;
524 inset_x = cx(bv) - top_x + drawTextXOffset;
525 inset_y = cy(bv) + drawTextYOffset;
527 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
529 } else if (the_locking_inset) {
530 lyxerr[Debug::INSETS] << "MAYBE" << endl;
531 return the_locking_inset->LockInsetInInset(bv, inset);
533 lyxerr[Debug::INSETS] << "NOT OK" << endl;
538 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
541 if (!the_locking_inset)
543 if (the_locking_inset == inset) {
544 the_locking_inset->InsetUnlock(bv);
545 TEXT(bv)->UpdateInset(bv, inset);
546 the_locking_inset = 0;
548 moveRight(bv, false);
549 old_par = 0; // force layout setting
550 UpdateLocal(bv, CURSOR_PAR, false);
553 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
557 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
559 if (!the_locking_inset)
561 if (the_locking_inset != inset) {
562 TEXT(bv)->UpdateInset(bv, the_locking_inset);
563 need_update = CURSOR_PAR;
564 return the_locking_inset->UpdateInsetInInset(bv, inset);
566 // UpdateLocal(bv, FULL, false);
567 if (TEXT(bv)->UpdateInset(bv, inset))
568 UpdateLocal(bv, CURSOR_PAR, false);
569 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
570 inset_x = cx(bv) - top_x + drawTextXOffset;
571 inset_y = cy(bv) + drawTextYOffset;
577 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
579 no_selection = false;
581 int tmp_x = x - drawTextXOffset;
582 int tmp_y = y + insetAscent;
583 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
586 if (the_locking_inset) {
587 if (the_locking_inset == inset) {
588 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
591 // otherwise unlock the_locking_inset and lock the new inset
592 the_locking_inset->InsetUnlock(bv);
593 inset_x = cx(bv) - top_x + drawTextXOffset;
594 inset_y = cy(bv) + drawTextYOffset;
595 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
596 inset->Edit(bv, x - inset_x, y - inset_y, button);
597 if (the_locking_inset) {
598 UpdateLocal(bv, CURSOR_PAR, false);
602 // otherwise only unlock the_locking_inset
603 the_locking_inset->InsetUnlock(bv);
604 the_locking_inset = 0;
606 if (bv->theLockingInset()) {
607 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
608 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
609 inset_x = cx(bv) - top_x + drawTextXOffset;
610 inset_y = cy(bv) + drawTextYOffset;
611 inset_pos = cpos(bv);
612 inset_par = cpar(bv);
613 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
614 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
615 if (the_locking_inset) {
616 UpdateLocal(bv, CURSOR_PAR, false);
622 bool paste_internally = false;
623 if ((button == 2) && TEXT(bv)->selection) {
624 LocalDispatch(bv, LFUN_COPY, "");
625 paste_internally = true;
627 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
629 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
630 UpdateLocal(bv, CURSOR, false);
631 bv->owner()->setLayout(cpar(bv)->GetLayout());
633 // Insert primary selection with middle mouse
634 // if there is a local selection in the current buffer,
637 if (paste_internally)
638 LocalDispatch(bv, LFUN_PASTE, "");
640 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
647 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
649 UpdatableInset * inset = 0;
651 if (the_locking_inset) {
652 the_locking_inset->InsetButtonRelease(bv,
653 x - inset_x, y - inset_y,
656 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
657 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
658 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
659 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
661 inset_x = cx(bv) - top_x + drawTextXOffset;
662 inset_y = cy(bv) + drawTextYOffset;
663 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
664 inset->Edit(bv, x - inset_x, y - inset_y, button);
666 UpdateLocal(bv, CURSOR_PAR, false);
669 no_selection = false;
673 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
675 if (the_locking_inset) {
676 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
682 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
684 TEXT(bv)->SetSelection();
685 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
686 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
687 UpdateLocal(bv, SELECTION, false);
690 no_selection = false;
694 void InsetText::InsetKeyPress(XKeyEvent * xke)
696 if (the_locking_inset) {
697 the_locking_inset->InsetKeyPress(xke);
703 UpdatableInset::RESULT
704 InsetText::LocalDispatch(BufferView * bv,
705 int action, string const & arg)
707 no_selection = false;
708 UpdatableInset::RESULT
709 result= UpdatableInset::LocalDispatch(bv, action, arg);
710 if (result != UNDISPATCHED) {
715 if ((action < 0) && arg.empty())
718 if (the_locking_inset) {
719 result = the_locking_inset->LocalDispatch(bv, action, arg);
720 if (result == DISPATCHED_NOUPDATE)
722 else if (result == DISPATCHED) {
723 UpdateLocal(bv, CURSOR_PAR, false);
725 } else if (result == FINISHED) {
729 moveRight(bv, false);
735 the_locking_inset = 0;
743 if (bv->buffer()->isReadonly()) {
745 // setErrorMessage(N_("Document is read only"));
749 /* Automatically delete the currently selected
750 * text and replace it with what is being
751 * typed in now. Depends on lyxrc settings
752 * "auto_region_delete", which defaults to
755 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
757 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
758 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
760 bv->text->cursor.par()->previous,
761 bv->text->cursor.par()->next
764 // if an empty paragraph set the language to the surronding
765 // paragraph language on insertion of the first character!
766 if (!par->Last() && !par->next) {
769 Inset * inset = owner();
770 while(inset && inset->getLyXText(bv) == TEXT(bv))
771 inset = inset->owner();
773 text = inset->getLyXText(bv);
777 LyXFont font(LyXFont::ALL_IGNORE);
778 font.setLanguage(text->cursor.par()->getParLanguage(bv->buffer()->params));
779 SetFont(bv, font, false);
781 if (lyxrc.auto_region_delete) {
782 if (TEXT(bv)->selection){
783 TEXT(bv)->CutSelection(bv, false);
786 TEXT(bv)->ClearSelection();
787 for (string::size_type i = 0; i < arg.length(); ++i) {
788 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
791 UpdateLocal(bv, CURSOR_PAR, true);
793 // --- Cursor Movements ---------------------------------------------
795 bv->text->FinishUndo();
796 moveRight(bv, false, true);
797 TEXT(bv)->SetSelection();
798 UpdateLocal(bv, SELECTION, false);
801 result = moveRight(bv);
802 bv->text->FinishUndo();
803 TEXT(bv)->ClearSelection();
804 UpdateLocal(bv, CURSOR, false);
807 bv->text->FinishUndo();
808 moveLeft(bv, false, true);
809 TEXT(bv)->SetSelection();
810 UpdateLocal(bv, SELECTION, false);
813 bv->text->FinishUndo();
814 result= moveLeft(bv);
815 TEXT(bv)->ClearSelection();
816 UpdateLocal(bv, CURSOR, false);
819 bv->text->FinishUndo();
821 TEXT(bv)->SetSelection();
822 UpdateLocal(bv, SELECTION, false);
825 bv->text->FinishUndo();
826 result = moveDown(bv);
827 TEXT(bv)->ClearSelection();
828 UpdateLocal(bv, CURSOR, false);
831 bv->text->FinishUndo();
833 TEXT(bv)->SetSelection();
834 UpdateLocal(bv, SELECTION, false);
837 bv->text->FinishUndo();
839 TEXT(bv)->ClearSelection();
840 UpdateLocal(bv, CURSOR, false);
843 bv->text->FinishUndo();
844 TEXT(bv)->CursorHome(bv);
845 UpdateLocal(bv, CURSOR, false);
848 TEXT(bv)->CursorEnd(bv);
849 UpdateLocal(bv, CURSOR, false);
852 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
854 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
855 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
857 bv->text->cursor.par()->previous,
858 bv->text->cursor.par()->next
861 if (TEXT(bv)->selection)
862 TEXT(bv)->CutSelection(bv);
864 TEXT(bv)->Backspace(bv);
865 UpdateLocal(bv, CURSOR_PAR, true);
868 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
870 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
871 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
873 bv->text->cursor.par()->previous,
874 bv->text->cursor.par()->next
877 if (TEXT(bv)->selection)
878 TEXT(bv)->CutSelection(bv);
880 TEXT(bv)->Delete(bv);
881 UpdateLocal(bv, CURSOR_PAR, true);
884 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
886 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
887 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
889 bv->text->cursor.par()->previous,
890 bv->text->cursor.par()->next
893 TEXT(bv)->CutSelection(bv);
894 UpdateLocal(bv, CURSOR_PAR, true);
897 bv->text->FinishUndo();
898 TEXT(bv)->CopySelection(bv);
899 UpdateLocal(bv, CURSOR_PAR, false);
901 case LFUN_PASTESELECTION:
903 string clip(bv->workarea()->getClipboard());
907 if (arg == "paragraph") {
908 TEXT(bv)->InsertStringB(bv, clip);
910 TEXT(bv)->InsertStringA(bv, clip);
912 UpdateLocal(bv, CURSOR_PAR, true);
916 if (!autoBreakRows) {
919 if (cap.nrOfParagraphs() > 1) {
920 WriteAlert(_("Impossible operation"),
921 _("Cannot include more than one paragraph!"),
926 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
928 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
929 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
931 bv->text->cursor.par()->previous,
932 bv->text->cursor.par()->next
935 TEXT(bv)->PasteSelection(bv);
936 UpdateLocal(bv, CURSOR_PAR, true);
938 case LFUN_BREAKPARAGRAPH:
941 TEXT(bv)->BreakParagraph(bv, 0);
942 UpdateLocal(bv, FULL, true);
947 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
949 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
950 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
952 bv->text->cursor.par()->previous,
953 bv->text->cursor.par()->next
956 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
957 UpdateLocal(bv, CURSOR_PAR, true);
960 // do not set layouts on non breakable textinsets
962 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
964 // Derive layout number from given argument (string)
965 // and current buffer's textclass (number). */
966 LyXTextClassList::ClassList::size_type tclass =
967 bv->buffer()->params.textclass;
968 std::pair <bool, LyXTextClass::size_type> layout =
969 textclasslist.NumberOfLayout(tclass, arg);
971 // If the entry is obsolete, use the new one instead.
973 string obs = textclasslist.Style(tclass,layout.second).
976 layout = textclasslist.NumberOfLayout(tclass, obs);
979 // see if we found the layout number:
981 string msg = string(N_("Layout ")) + arg + N_(" not known");
983 bv->owner()->getMiniBuffer()->Set(msg);
987 if (cur_layout != layout.second) {
988 cur_layout = layout.second;
989 TEXT(bv)->SetLayout(bv, layout.second);
990 bv->owner()->setLayout(cpar(bv)->GetLayout());
991 UpdateLocal(bv, CURSOR_PAR, true);
994 // reset the layout box
995 bv->owner()->setLayout(cpar(bv)->GetLayout());
998 case LFUN_PARAGRAPH_SPACING:
999 // This one is absolutely not working. When fiddling with this
1000 // it also seems to me that the paragraphs inside the insettext
1001 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1003 LyXParagraph * par = TEXT(bv)->cursor.par();
1004 Spacing::Space cur_spacing = par->spacing.getSpace();
1005 float cur_value = 1.0;
1006 if (cur_spacing == Spacing::Other) {
1007 cur_value = par->spacing.getValue();
1010 std::istringstream istr(arg.c_str());
1013 Spacing::Space new_spacing = cur_spacing;
1014 float new_value = cur_value;
1016 lyxerr << "Missing argument to `paragraph-spacing'"
1018 } else if (tmp == "single") {
1019 new_spacing = Spacing::Single;
1020 } else if (tmp == "onehalf") {
1021 new_spacing = Spacing::Onehalf;
1022 } else if (tmp == "double") {
1023 new_spacing = Spacing::Double;
1024 } else if (tmp == "other") {
1025 new_spacing = Spacing::Other;
1028 lyxerr << "new_value = " << tmpval << endl;
1031 } else if (tmp == "default") {
1032 new_spacing = Spacing::Default;
1034 lyxerr << _("Unknown spacing argument: ")
1037 if (cur_spacing != new_spacing || cur_value != new_value) {
1038 par->spacing.set(new_spacing, new_value);
1039 //TEXT(bv)->RedoParagraph(owner->view());
1040 UpdateLocal(bv, CURSOR_PAR, true);
1041 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1047 result = UNDISPATCHED;
1050 if (result != FINISHED) {
1051 ShowInsetCursor(bv);
1053 bv->unlockInset(this);
1058 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1061 buf->latexParagraphs(os, par, 0, texrow);
1062 return texrow.rows();
1066 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1068 LyXParagraph * p = par;
1069 unsigned int lines = 0;
1073 tmp = buf->asciiParagraph(p, linelen);
1074 lines += countChar(tmp, '\n');
1082 void InsetText::Validate(LaTeXFeatures & features) const
1084 LyXParagraph * p = par;
1086 p->validate(features);
1092 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1094 if (textclasslist.Style(buf->params.textclass,
1095 p->GetLayout()).labeltype != LABEL_MANUAL)
1098 return p->BeginningOfMainBody();
1102 void InsetText::GetCursorPos(BufferView * bv,
1103 int & x, int & y) const
1110 unsigned int InsetText::InsetInInsetY()
1112 if (!the_locking_inset)
1115 return (inset_y + the_locking_inset->InsetInInsetY());
1119 void InsetText::ToggleInsetCursor(BufferView * bv)
1121 if (the_locking_inset) {
1122 the_locking_inset->ToggleInsetCursor(bv);
1126 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1128 int asc = lyxfont::maxAscent(font);
1129 int desc = lyxfont::maxDescent(font);
1132 bv->hideLockedInsetCursor();
1134 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1135 cursor_visible = !cursor_visible;
1139 void InsetText::ShowInsetCursor(BufferView * bv, bool show)
1141 if (the_locking_inset) {
1142 the_locking_inset->ShowInsetCursor(bv);
1145 if (!cursor_visible) {
1146 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1148 int asc = lyxfont::maxAscent(font);
1149 int desc = lyxfont::maxDescent(font);
1151 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1153 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1154 cursor_visible = true;
1159 void InsetText::HideInsetCursor(BufferView * bv)
1161 if (cursor_visible) {
1162 bv->hideLockedInsetCursor();
1163 cursor_visible = false;
1165 if (the_locking_inset)
1166 the_locking_inset->HideInsetCursor(bv);
1170 UpdatableInset::RESULT
1171 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1173 if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1175 if (activate_inset && checkAndActivateInset(bv, false))
1177 TEXT(bv)->CursorRight(bv, selecting);
1178 return DISPATCHED_NOUPDATE;
1182 UpdatableInset::RESULT
1183 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1185 if (!cpar(bv)->previous && (cpos(bv) <= 0))
1187 TEXT(bv)->CursorLeft(bv, selecting);
1188 if (activate_inset && checkAndActivateInset(bv, true))
1190 return DISPATCHED_NOUPDATE;
1194 UpdatableInset::RESULT
1195 InsetText::moveUp(BufferView * bv)
1197 if (!crow(bv)->previous())
1199 TEXT(bv)->CursorUp(bv);
1200 return DISPATCHED_NOUPDATE;
1204 UpdatableInset::RESULT
1205 InsetText::moveDown(BufferView * bv)
1207 if (!crow(bv)->next())
1209 TEXT(bv)->CursorDown(bv);
1210 return DISPATCHED_NOUPDATE;
1214 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1216 if (the_locking_inset) {
1217 if (the_locking_inset->InsertInsetAllowed(inset))
1218 return the_locking_inset->InsertInset(bv, inset);
1221 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1223 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1224 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1226 bv->text->cursor.par()->previous,
1227 bv->text->cursor.par()->next
1230 if (inset->Editable() == Inset::IS_EDITABLE) {
1231 UpdatableInset * i = static_cast<UpdatableInset *>(inset);
1232 i->setOwner(static_cast<UpdatableInset *>(this));
1234 HideInsetCursor(bv);
1235 TEXT(bv)->InsertInset(bv, inset);
1236 TEXT(bv)->selection = 0;
1237 bv->fitCursor(TEXT(bv));
1238 UpdateLocal(bv, CURSOR_PAR, true);
1239 static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1240 ShowInsetCursor(bv);
1245 UpdatableInset * InsetText::GetLockingInset()
1247 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1251 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1255 if (the_locking_inset)
1256 return the_locking_inset->GetFirstLockingInsetOfType(c);
1261 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1263 bv->text->SetUndo(bv->buffer(), Undo::EDIT,
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 TEXT(bv)->SetFont(bv, font, toggleall);
1273 bv->fitCursor(TEXT(bv));
1274 UpdateLocal(bv, CURSOR_PAR, true);
1278 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1280 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1284 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1285 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1287 LyXFont const font =
1288 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1290 x = inset->width(bv, font);
1291 y = inset->descent(bv, font);
1295 inset_x = cx(bv) - top_x + drawTextXOffset;
1296 inset_y = cy(bv) + drawTextYOffset;
1297 inset->Edit(bv, x - inset_x, y - inset_y, 0);
1298 if (!the_locking_inset)
1300 UpdateLocal(bv, CURSOR_PAR, false);
1307 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1310 x = x - drawTextXOffset;
1311 y = y + insetAscent;
1312 Inset * inset = bv->checkInsetHit(TEXT(bv), x, y, button);
1319 inset_x = cx(bv) - top_x + drawTextXOffset;
1320 inset_y = cy(bv) + drawTextYOffset;
1321 inset->Edit(bv, x - inset_x, y - inset_y, button);
1322 if (!the_locking_inset)
1324 UpdateLocal(bv, CURSOR_PAR, false);
1331 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1333 int w = UpdatableInset::getMaxWidth(pain, inset);
1338 w = w - top_x + owner()->x();
1341 w -= (2 * TEXT_TO_INSET_OFFSET);
1343 // return w - (2*TEXT_TO_INSET_OFFSET);
1347 void InsetText::SetParagraphData(LyXParagraph *p)
1361 par->SetInsetOwner(this);
1365 np->next = p->Clone();
1366 np->next->previous = np;
1368 np->SetInsetOwner(this);
1374 void InsetText::SetAutoBreakRows(bool flag)
1376 if (flag != autoBreakRows) {
1377 autoBreakRows = flag;
1385 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1387 if (how != drawFrame) {
1390 UpdateLocal(bv, DRAW_FRAME, false);
1395 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1397 if (frame_color != col) {
1400 UpdateLocal(bv, DRAW_FRAME, false);
1405 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1407 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1412 int InsetText::cx(BufferView * bv) const
1414 return TEXT(bv)->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1418 int InsetText::cy(BufferView * bv) const
1421 return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1425 LyXParagraph::size_type InsetText::cpos(BufferView * bv) const
1427 return TEXT(bv)->cursor.pos();
1431 LyXParagraph * InsetText::cpar(BufferView * bv) const
1433 return TEXT(bv)->cursor.par();
1437 Row * InsetText::crow(BufferView * bv) const
1439 return TEXT(bv)->cursor.row();
1443 LyXText * InsetText::getLyXText(BufferView * bv) const
1445 if (cache.find(bv) != cache.end())
1447 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1450 if (the_locking_inset) {
1451 lt->SetCursor(bv, inset_par, inset_pos);
1457 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1459 if (cache.find(bv) == cache.end())
1464 /// then remove all LyXText in text-insets
1465 LyXParagraph * p = par;
1466 for(;p;p = p->next) {
1467 p->deleteInsetsLyXText(bv);
1473 void InsetText::resizeLyXText(BufferView * bv) const
1475 if (!par->next && !par->size()) // resize not neccessary!
1477 if (cache.find(bv) == cache.end())
1480 LyXParagraph * lpar = 0;
1481 LyXParagraph * selstartpar = 0;
1482 LyXParagraph * selendpar = 0;
1483 LyXParagraph::size_type pos = 0;
1484 LyXParagraph::size_type selstartpos = 0;
1485 LyXParagraph::size_type selendpos = 0;
1489 // ProhibitInput(bv);
1492 lpar = TEXT(bv)->cursor.par();
1493 pos = TEXT(bv)->cursor.pos();
1494 selstartpar = TEXT(bv)->sel_start_cursor.par();
1495 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1496 selendpar = TEXT(bv)->sel_end_cursor.par();
1497 selendpos = TEXT(bv)->sel_end_cursor.pos();
1498 selection = TEXT(bv)->selection;
1499 mark_set = TEXT(bv)->mark_set;
1501 deleteLyXText(bv, (the_locking_inset == 0));
1504 TEXT(bv)->selection = true;
1505 /* at this point just to avoid the Delete-Empty-Paragraph
1506 * Mechanism when setting the cursor */
1507 TEXT(bv)->mark_set = mark_set;
1509 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos);
1510 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1511 TEXT(bv)->SetCursor(bv, selendpar, selendpos);
1512 TEXT(bv)->SetSelection();
1513 TEXT(bv)->SetCursor(bv, lpar, pos);
1515 TEXT(bv)->SetCursor(bv, lpar, pos);
1516 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1517 TEXT(bv)->selection = false;
1521 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1522 // this will scroll the screen such that the cursor becomes visible
1523 bv->updateScrollbar();
1525 if (the_locking_inset) {
1526 /// then resize all LyXText in text-insets
1527 inset_x = cx(bv) - top_x + drawTextXOffset;
1528 inset_y = cy(bv) + drawTextYOffset;
1529 for(LyXParagraph * p = par; p; p = p->next) {
1530 p->resizeInsetsLyXText(bv);
1537 void InsetText::removeNewlines()
1539 for(LyXParagraph * p = par; p; p = p->next) {
1540 for(int i = 0; i < p->Last(); ++i) {
1541 if (p->GetChar(i) == LyXParagraph::META_NEWLINE)