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() 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 y += baseline - row->ascent_of_text();
319 if (cleared || !locked || (need_update == FULL)) {
321 TEXT(bv)->GetVisibleRow(bv, int(y), int(x), row, y, cleared);
325 } else if (need_update == SELECTION) {
326 bv->screen()->ToggleToggle(TEXT(bv), int(y), int(x));
329 if (need_update == CURSOR) {
330 bv->screen()->ToggleSelection(TEXT(bv), true, int(y), int(x));
331 TEXT(bv)->ClearSelection();
332 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
334 bv->screen()->Update(TEXT(bv), int(y), int(x));
337 TEXT(bv)->refresh_y = 0;
338 TEXT(bv)->status = LyXText::UNCHANGED;
339 if ((need_update != CURSOR_PAR) &&
340 ((drawFrame == ALWAYS) || ((drawFrame == LOCKED) && locked)))
342 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
343 width(bv, f) - 1, insetAscent + insetDescent - 1,
345 } else if (need_update == CLEAR_FRAME) {
346 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
347 width(bv, f) - 1, insetAscent + insetDescent - 1,
350 x += width(bv, f) - TEXT_TO_INSET_OFFSET;
351 if (bv->text->status==LyXText::CHANGED_IN_DRAW)
353 else if (need_update != INIT)
358 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
360 if (reinit) { // && (need_update != CURSOR)) {
364 owner()->update(bv, font, true);
367 if (the_locking_inset) {
368 inset_x = cx(bv) - top_x + drawTextXOffset;
369 inset_y = cy(bv) + drawTextYOffset;
370 the_locking_inset->update(bv, font, reinit);
372 if (need_update == INIT) {
375 // if (!owner() && bv->text)
376 // bv->text->UpdateInset(bv, this);
378 int oldw = insetWidth;
380 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
381 // max(textWidth(bv->painter()),
382 // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
383 // (2 * TEXT_TO_INSET_OFFSET);
385 insetWidth = textWidth(bv->painter());
387 insetWidth = static_cast<int>(TEXT(bv)->width);
389 if (oldw != insetWidth) {
390 // printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
391 // textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
396 owner()->update(bv, font, reinit);
399 update(bv, font, reinit);
403 update(bv, font, reinit);
405 UpdateLocal(bv, INIT, false);
410 if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
413 TEXT(bv)->UpdateInset(bv, the_locking_inset);
416 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
420 Row * row = TEXT(bv)->GetRowNearY(y_temp);
421 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
422 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
423 TEXT_TO_INSET_OFFSET;
427 void InsetText::UpdateLocal(BufferView * bv, UpdateCodes what, bool mark_dirty)
429 TEXT(bv)->FullRebreak(bv);
430 if (need_update != INIT) {
431 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
433 else if (!the_locking_inset || (what != CURSOR))
436 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
438 bv->updateInset(this, mark_dirty);
439 bv->owner()->showState();
440 if (old_par != cpar(bv)) {
441 bv->owner()->setLayout(cpar(bv)->GetLayout());
447 string const InsetText::EditMessage() const
449 return _("Opened Text Inset");
453 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
455 // par->SetInsetOwner(this);
456 UpdatableInset::Edit(bv, x, y, button);
458 if (!bv->lockInset(this)) {
459 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
463 the_locking_inset = 0;
464 inset_pos = inset_x = inset_y = 0;
467 if (!checkAndActivateInset(bv, x, y, button))
468 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
469 y+TEXT(bv)->first+insetAscent);
470 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
471 bv->text->FinishUndo();
473 UpdateLocal(bv, FULL, false);
477 void InsetText::InsetUnlock(BufferView * bv)
479 if (the_locking_inset) {
480 the_locking_inset->InsetUnlock(bv);
481 the_locking_inset = 0;
484 no_selection = false;
486 TEXT(bv)->selection = 0;
487 UpdateLocal(bv, CLEAR_FRAME, false);
489 bv->owner()->setLayout(owner()->getLyXText(bv)
490 ->cursor.par()->GetLayout());
492 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
496 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
498 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
501 if (inset == cpar(bv)->GetInset(cpos(bv))) {
502 lyxerr[Debug::INSETS] << "OK" << endl;
503 the_locking_inset = inset;
504 inset_x = cx(bv) - top_x + drawTextXOffset;
505 inset_y = cy(bv) + drawTextYOffset;
506 inset_pos = cpos(bv);
507 inset_par = cpar(bv);
508 TEXT(bv)->UpdateInset(bv, the_locking_inset);
510 } else if (the_locking_inset && (the_locking_inset == inset)) {
511 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
512 lyxerr[Debug::INSETS] << "OK" << endl;
513 inset_x = cx(bv) - top_x + drawTextXOffset;
514 inset_y = cy(bv) + drawTextYOffset;
516 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
518 } else if (the_locking_inset) {
519 lyxerr[Debug::INSETS] << "MAYBE" << endl;
520 return the_locking_inset->LockInsetInInset(bv, inset);
522 lyxerr[Debug::INSETS] << "NOT OK" << endl;
527 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
530 if (!the_locking_inset)
532 if (the_locking_inset == inset) {
533 the_locking_inset->InsetUnlock(bv);
534 TEXT(bv)->UpdateInset(bv, inset);
535 the_locking_inset = 0;
537 moveRight(bv, false);
538 old_par = 0; // force layout setting
539 UpdateLocal(bv, CURSOR_PAR, false);
542 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
546 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
548 if (!the_locking_inset)
550 if (the_locking_inset != inset) {
551 TEXT(bv)->UpdateInset(bv, the_locking_inset);
552 need_update = CURSOR_PAR;
553 return the_locking_inset->UpdateInsetInInset(bv, inset);
555 // UpdateLocal(bv, FULL, false);
556 if (TEXT(bv)->UpdateInset(bv, inset))
557 UpdateLocal(bv, CURSOR_PAR, false);
558 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
559 inset_x = cx(bv) - top_x + drawTextXOffset;
560 inset_y = cy(bv) + drawTextYOffset;
566 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
568 no_selection = false;
570 int tmp_x = x - drawTextXOffset;
571 int tmp_y = y + insetAscent;
572 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
575 if (the_locking_inset) {
576 if (the_locking_inset == inset) {
577 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
580 // otherwise unlock the_locking_inset and lock the new inset
581 the_locking_inset->InsetUnlock(bv);
582 inset_x = cx(bv) - top_x + drawTextXOffset;
583 inset_y = cy(bv) + drawTextYOffset;
584 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
585 inset->Edit(bv, x - inset_x, y - inset_y, button);
586 if (the_locking_inset) {
587 UpdateLocal(bv, CURSOR_PAR, false);
591 // otherwise only unlock the_locking_inset
592 the_locking_inset->InsetUnlock(bv);
593 the_locking_inset = 0;
595 if (bv->theLockingInset()) {
596 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
597 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
598 inset_x = cx(bv) - top_x + drawTextXOffset;
599 inset_y = cy(bv) + drawTextYOffset;
600 inset_pos = cpos(bv);
601 inset_par = cpar(bv);
602 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
603 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
604 if (the_locking_inset) {
605 UpdateLocal(bv, CURSOR_PAR, false);
611 bool paste_internally = false;
612 if ((button == 2) && TEXT(bv)->selection) {
613 LocalDispatch(bv, LFUN_COPY, "");
614 paste_internally = true;
616 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
617 y+TEXT(bv)->first+insetAscent);
618 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
619 UpdateLocal(bv, CURSOR, false);
620 bv->owner()->setLayout(cpar(bv)->GetLayout());
622 // Insert primary selection with middle mouse
623 // if there is a local selection in the current buffer,
626 if (paste_internally)
627 LocalDispatch(bv, LFUN_PASTE, "");
629 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
636 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
638 UpdatableInset * inset = 0;
640 if (the_locking_inset) {
641 the_locking_inset->InsetButtonRelease(bv,
642 x - inset_x, y - inset_y,
645 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
646 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
647 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
648 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
650 inset_x = cx(bv) - top_x + drawTextXOffset;
651 inset_y = cy(bv) + drawTextYOffset;
652 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
653 inset->Edit(bv, x - inset_x, y - inset_y, button);
655 UpdateLocal(bv, CURSOR_PAR, false);
658 no_selection = false;
662 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
664 if (the_locking_inset) {
665 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
671 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
672 y+TEXT(bv)->first+insetAscent);
673 TEXT(bv)->SetSelection();
674 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
675 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
676 UpdateLocal(bv, SELECTION, false);
679 no_selection = false;
683 void InsetText::InsetKeyPress(XKeyEvent * xke)
685 if (the_locking_inset) {
686 the_locking_inset->InsetKeyPress(xke);
692 UpdatableInset::RESULT
693 InsetText::LocalDispatch(BufferView * bv,
694 int action, string const & arg)
696 no_selection = false;
697 UpdatableInset::RESULT
698 result= UpdatableInset::LocalDispatch(bv, action, arg);
699 if (result != UNDISPATCHED) {
704 if ((action < 0) && arg.empty())
707 if (the_locking_inset) {
708 result = the_locking_inset->LocalDispatch(bv, action, arg);
709 if (result == DISPATCHED_NOUPDATE)
711 else if (result == DISPATCHED) {
712 UpdateLocal(bv, CURSOR_PAR, false);
714 } else if (result == FINISHED) {
718 moveRight(bv, false);
724 the_locking_inset = 0;
732 if (bv->buffer()->isReadonly()) {
734 // setErrorMessage(N_("Document is read only"));
738 /* Automatically delete the currently selected
739 * text and replace it with what is being
740 * typed in now. Depends on lyxrc settings
741 * "auto_region_delete", which defaults to
744 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
746 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
747 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
749 bv->text->cursor.par()->previous,
750 bv->text->cursor.par()->next
753 // if an empty paragraph set the language to the surronding
754 // paragraph language on insertion of the first character!
755 if (!par->Last() && !par->next) {
758 Inset * inset = owner();
759 while(inset && inset->getLyXText(bv) == TEXT(bv))
760 inset = inset->owner();
762 text = inset->getLyXText(bv);
766 LyXFont font(LyXFont::ALL_IGNORE);
767 font.setLanguage(text->cursor.par()->getParLanguage(bv->buffer()->params));
768 SetFont(bv, font, false);
770 if (lyxrc.auto_region_delete) {
771 if (TEXT(bv)->selection){
772 TEXT(bv)->CutSelection(bv, false);
775 TEXT(bv)->ClearSelection();
776 for (string::size_type i = 0; i < arg.length(); ++i) {
777 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
780 UpdateLocal(bv, CURSOR_PAR, true);
782 // --- Cursor Movements ---------------------------------------------
784 bv->text->FinishUndo();
785 moveRight(bv, false, true);
786 TEXT(bv)->SetSelection();
787 UpdateLocal(bv, SELECTION, false);
790 result = moveRight(bv);
791 bv->text->FinishUndo();
792 TEXT(bv)->ClearSelection();
793 UpdateLocal(bv, CURSOR, false);
796 bv->text->FinishUndo();
797 moveLeft(bv, false, true);
798 TEXT(bv)->SetSelection();
799 UpdateLocal(bv, SELECTION, false);
802 bv->text->FinishUndo();
803 result= moveLeft(bv);
804 TEXT(bv)->ClearSelection();
805 UpdateLocal(bv, CURSOR, false);
808 bv->text->FinishUndo();
810 TEXT(bv)->SetSelection();
811 UpdateLocal(bv, SELECTION, false);
814 bv->text->FinishUndo();
815 result = moveDown(bv);
816 TEXT(bv)->ClearSelection();
817 UpdateLocal(bv, CURSOR, false);
820 bv->text->FinishUndo();
822 TEXT(bv)->SetSelection();
823 UpdateLocal(bv, SELECTION, false);
826 bv->text->FinishUndo();
828 TEXT(bv)->ClearSelection();
829 UpdateLocal(bv, CURSOR, false);
832 bv->text->FinishUndo();
833 TEXT(bv)->CursorHome(bv);
834 UpdateLocal(bv, CURSOR, false);
837 TEXT(bv)->CursorEnd(bv);
838 UpdateLocal(bv, CURSOR, false);
841 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
843 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
844 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
846 bv->text->cursor.par()->previous,
847 bv->text->cursor.par()->next
850 if (TEXT(bv)->selection)
851 TEXT(bv)->CutSelection(bv);
853 TEXT(bv)->Backspace(bv);
854 UpdateLocal(bv, CURSOR_PAR, true);
857 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
859 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
860 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
862 bv->text->cursor.par()->previous,
863 bv->text->cursor.par()->next
866 if (TEXT(bv)->selection)
867 TEXT(bv)->CutSelection(bv);
869 TEXT(bv)->Delete(bv);
870 UpdateLocal(bv, CURSOR_PAR, true);
873 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
875 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
876 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
878 bv->text->cursor.par()->previous,
879 bv->text->cursor.par()->next
882 TEXT(bv)->CutSelection(bv);
883 UpdateLocal(bv, CURSOR_PAR, true);
886 bv->text->FinishUndo();
887 TEXT(bv)->CopySelection(bv);
888 UpdateLocal(bv, CURSOR_PAR, false);
890 case LFUN_PASTESELECTION:
892 string clip(bv->workarea()->getClipboard());
896 if (arg == "paragraph") {
897 TEXT(bv)->InsertStringB(bv, clip);
899 TEXT(bv)->InsertStringA(bv, clip);
901 UpdateLocal(bv, CURSOR_PAR, true);
905 if (!autoBreakRows) {
908 if (cap.nrOfParagraphs() > 1) {
909 WriteAlert(_("Impossible operation"),
910 _("Cannot include more than one paragraph!"),
915 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
917 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
918 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
920 bv->text->cursor.par()->previous,
921 bv->text->cursor.par()->next
924 TEXT(bv)->PasteSelection(bv);
925 UpdateLocal(bv, CURSOR_PAR, true);
927 case LFUN_BREAKPARAGRAPH:
930 TEXT(bv)->BreakParagraph(bv, 0);
931 UpdateLocal(bv, FULL, true);
936 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
938 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
939 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
941 bv->text->cursor.par()->previous,
942 bv->text->cursor.par()->next
945 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
946 UpdateLocal(bv, CURSOR_PAR, true);
949 // do not set layouts on non breakable textinsets
951 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
953 // Derive layout number from given argument (string)
954 // and current buffer's textclass (number). */
955 LyXTextClassList::ClassList::size_type tclass =
956 bv->buffer()->params.textclass;
957 std::pair <bool, LyXTextClass::size_type> layout =
958 textclasslist.NumberOfLayout(tclass, arg);
960 // If the entry is obsolete, use the new one instead.
962 string obs = textclasslist.Style(tclass,layout.second).
965 layout = textclasslist.NumberOfLayout(tclass, obs);
968 // see if we found the layout number:
970 string msg = string(N_("Layout ")) + arg + N_(" not known");
972 bv->owner()->getMiniBuffer()->Set(msg);
976 if (cur_layout != layout.second) {
977 cur_layout = layout.second;
978 TEXT(bv)->SetLayout(bv, layout.second);
979 bv->owner()->setLayout(cpar(bv)->GetLayout());
980 UpdateLocal(bv, CURSOR_PAR, true);
983 // reset the layout box
984 bv->owner()->setLayout(cpar(bv)->GetLayout());
987 case LFUN_PARAGRAPH_SPACING:
988 // This one is absolutely not working. When fiddling with this
989 // it also seems to me that the paragraphs inside the insettext
990 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
992 LyXParagraph * par = TEXT(bv)->cursor.par();
993 Spacing::Space cur_spacing = par->spacing.getSpace();
994 float cur_value = 1.0;
995 if (cur_spacing == Spacing::Other) {
996 cur_value = par->spacing.getValue();
999 std::istringstream istr(arg.c_str());
1002 Spacing::Space new_spacing = cur_spacing;
1003 float new_value = cur_value;
1005 lyxerr << "Missing argument to `paragraph-spacing'"
1007 } else if (tmp == "single") {
1008 new_spacing = Spacing::Single;
1009 } else if (tmp == "onehalf") {
1010 new_spacing = Spacing::Onehalf;
1011 } else if (tmp == "double") {
1012 new_spacing = Spacing::Double;
1013 } else if (tmp == "other") {
1014 new_spacing = Spacing::Other;
1017 lyxerr << "new_value = " << tmpval << endl;
1020 } else if (tmp == "default") {
1021 new_spacing = Spacing::Default;
1023 lyxerr << _("Unknown spacing argument: ")
1026 if (cur_spacing != new_spacing || cur_value != new_value) {
1027 par->spacing.set(new_spacing, new_value);
1028 //TEXT(bv)->RedoParagraph(owner->view());
1029 UpdateLocal(bv, CURSOR_PAR, true);
1030 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1036 result = UNDISPATCHED;
1039 if (result != FINISHED) {
1040 ShowInsetCursor(bv);
1042 bv->unlockInset(this);
1047 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1050 buf->latexParagraphs(os, par, 0, texrow);
1051 return texrow.rows();
1055 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1057 LyXParagraph * p = par;
1058 unsigned int lines = 0;
1061 string const tmp = buf->asciiParagraph(p, linelen);
1062 lines += countChar(tmp, '\n');
1072 void InsetText::Validate(LaTeXFeatures & features) const
1074 LyXParagraph * p = par;
1076 p->validate(features);
1082 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1084 if (textclasslist.Style(buf->params.textclass,
1085 p->GetLayout()).labeltype != LABEL_MANUAL)
1088 return p->BeginningOfMainBody();
1092 void InsetText::GetCursorPos(BufferView * bv,
1093 int & x, int & y) const
1100 unsigned int InsetText::InsetInInsetY()
1102 if (!the_locking_inset)
1105 return (inset_y + the_locking_inset->InsetInInsetY());
1109 void InsetText::ToggleInsetCursor(BufferView * bv)
1111 if (the_locking_inset) {
1112 the_locking_inset->ToggleInsetCursor(bv);
1116 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1118 int asc = lyxfont::maxAscent(font);
1119 int desc = lyxfont::maxDescent(font);
1122 bv->hideLockedInsetCursor();
1124 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1125 cursor_visible = !cursor_visible;
1129 void InsetText::ShowInsetCursor(BufferView * bv, bool show)
1131 if (the_locking_inset) {
1132 the_locking_inset->ShowInsetCursor(bv);
1135 if (!cursor_visible) {
1136 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1138 int asc = lyxfont::maxAscent(font);
1139 int desc = lyxfont::maxDescent(font);
1141 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1143 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1144 cursor_visible = true;
1149 void InsetText::HideInsetCursor(BufferView * bv)
1151 if (cursor_visible) {
1152 bv->hideLockedInsetCursor();
1153 cursor_visible = false;
1155 if (the_locking_inset)
1156 the_locking_inset->HideInsetCursor(bv);
1160 UpdatableInset::RESULT
1161 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1163 if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1165 if (activate_inset && checkAndActivateInset(bv, false))
1167 TEXT(bv)->CursorRight(bv, selecting);
1168 return DISPATCHED_NOUPDATE;
1172 UpdatableInset::RESULT
1173 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1175 if (!cpar(bv)->previous && (cpos(bv) <= 0))
1177 TEXT(bv)->CursorLeft(bv, selecting);
1178 if (activate_inset && checkAndActivateInset(bv, true))
1180 return DISPATCHED_NOUPDATE;
1184 UpdatableInset::RESULT
1185 InsetText::moveUp(BufferView * bv)
1187 if (!crow(bv)->previous())
1189 TEXT(bv)->CursorUp(bv);
1190 return DISPATCHED_NOUPDATE;
1194 UpdatableInset::RESULT
1195 InsetText::moveDown(BufferView * bv)
1197 if (!crow(bv)->next())
1199 TEXT(bv)->CursorDown(bv);
1200 return DISPATCHED_NOUPDATE;
1204 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1206 if (the_locking_inset) {
1207 if (the_locking_inset->InsertInsetAllowed(inset))
1208 return the_locking_inset->InsertInset(bv, inset);
1211 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1213 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1214 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1216 bv->text->cursor.par()->previous,
1217 bv->text->cursor.par()->next
1220 if (inset->Editable() == Inset::IS_EDITABLE) {
1221 UpdatableInset * i = static_cast<UpdatableInset *>(inset);
1222 i->setOwner(static_cast<UpdatableInset *>(this));
1224 HideInsetCursor(bv);
1225 TEXT(bv)->InsertInset(bv, inset);
1226 TEXT(bv)->selection = 0;
1227 bv->fitCursor(TEXT(bv));
1228 UpdateLocal(bv, CURSOR_PAR, true);
1229 static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1230 ShowInsetCursor(bv);
1235 UpdatableInset * InsetText::GetLockingInset()
1237 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1241 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1245 if (the_locking_inset)
1246 return the_locking_inset->GetFirstLockingInsetOfType(c);
1251 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1253 bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1255 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1256 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1258 bv->text->cursor.par()->previous,
1259 bv->text->cursor.par()->next
1262 TEXT(bv)->SetFont(bv, font, toggleall);
1263 bv->fitCursor(TEXT(bv));
1264 UpdateLocal(bv, CURSOR_PAR, true);
1268 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1270 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1274 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1275 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1277 LyXFont const font =
1278 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1280 x = inset->width(bv, font);
1281 y = inset->descent(bv, font);
1285 inset_x = cx(bv) - top_x + drawTextXOffset;
1286 inset_y = cy(bv) + drawTextYOffset;
1287 inset->Edit(bv, x - inset_x, y - inset_y, 0);
1288 if (!the_locking_inset)
1290 UpdateLocal(bv, CURSOR_PAR, false);
1297 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1300 x = x - drawTextXOffset;
1301 y = y + insetAscent;
1302 Inset * inset = bv->checkInsetHit(TEXT(bv), x, y, button);
1309 inset_x = cx(bv) - top_x + drawTextXOffset;
1310 inset_y = cy(bv) + drawTextYOffset;
1311 inset->Edit(bv, x - inset_x, y - inset_y, button);
1312 if (!the_locking_inset)
1314 UpdateLocal(bv, CURSOR_PAR, false);
1321 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1323 int w = UpdatableInset::getMaxWidth(pain, inset);
1328 w = w - top_x + owner()->x();
1331 w -= (2 * TEXT_TO_INSET_OFFSET);
1333 // return w - (2*TEXT_TO_INSET_OFFSET);
1337 void InsetText::SetParagraphData(LyXParagraph *p)
1351 par->SetInsetOwner(this);
1355 np->next = p->Clone();
1356 np->next->previous = np;
1358 np->SetInsetOwner(this);
1364 void InsetText::SetAutoBreakRows(bool flag)
1366 if (flag != autoBreakRows) {
1367 autoBreakRows = flag;
1375 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1377 if (how != drawFrame) {
1380 UpdateLocal(bv, DRAW_FRAME, false);
1385 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1387 if (frame_color != col) {
1390 UpdateLocal(bv, DRAW_FRAME, false);
1395 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1397 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1402 int InsetText::cx(BufferView * bv) const
1404 return TEXT(bv)->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1408 int InsetText::cy(BufferView * bv) const
1411 return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1415 int InsetText::cpos(BufferView * bv) const
1417 return TEXT(bv)->cursor.pos();
1421 LyXParagraph * InsetText::cpar(BufferView * bv) const
1423 return TEXT(bv)->cursor.par();
1427 Row * InsetText::crow(BufferView * bv) const
1429 return TEXT(bv)->cursor.row();
1433 LyXText * InsetText::getLyXText(BufferView * bv) const
1435 if (cache.find(bv) != cache.end())
1437 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1440 if (the_locking_inset) {
1441 lt->SetCursor(bv, inset_par, inset_pos);
1447 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1449 if (cache.find(bv) == cache.end())
1454 /// then remove all LyXText in text-insets
1455 LyXParagraph * p = par;
1456 for(;p;p = p->next) {
1457 p->deleteInsetsLyXText(bv);
1463 void InsetText::resizeLyXText(BufferView * bv) const
1465 if (!par->next && !par->size()) // resize not neccessary!
1467 if (cache.find(bv) == cache.end())
1470 LyXParagraph * lpar = 0;
1471 LyXParagraph * selstartpar = 0;
1472 LyXParagraph * selendpar = 0;
1474 int selstartpos = 0;
1479 // ProhibitInput(bv);
1482 lpar = TEXT(bv)->cursor.par();
1483 pos = TEXT(bv)->cursor.pos();
1484 selstartpar = TEXT(bv)->sel_start_cursor.par();
1485 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1486 selendpar = TEXT(bv)->sel_end_cursor.par();
1487 selendpos = TEXT(bv)->sel_end_cursor.pos();
1488 selection = TEXT(bv)->selection;
1489 mark_set = TEXT(bv)->mark_set;
1491 deleteLyXText(bv, (the_locking_inset == 0));
1494 TEXT(bv)->selection = true;
1495 /* at this point just to avoid the Delete-Empty-Paragraph
1496 * Mechanism when setting the cursor */
1497 TEXT(bv)->mark_set = mark_set;
1499 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos);
1500 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1501 TEXT(bv)->SetCursor(bv, selendpar, selendpos);
1502 TEXT(bv)->SetSelection();
1503 TEXT(bv)->SetCursor(bv, lpar, pos);
1505 TEXT(bv)->SetCursor(bv, lpar, pos);
1506 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1507 TEXT(bv)->selection = false;
1511 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1512 // this will scroll the screen such that the cursor becomes visible
1513 bv->updateScrollbar();
1515 if (the_locking_inset) {
1516 /// then resize all LyXText in text-insets
1517 inset_x = cx(bv) - top_x + drawTextXOffset;
1518 inset_y = cy(bv) + drawTextYOffset;
1519 for(LyXParagraph * p = par; p; p = p->next) {
1520 p->resizeInsetsLyXText(bv);
1527 void InsetText::removeNewlines()
1529 for(LyXParagraph * p = par; p; p = p->next) {
1530 for(int i = 0; i < p->Last(); ++i) {
1531 if (p->GetChar(i) == LyXParagraph::META_NEWLINE)