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->the_locking_inset) {
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 (lyxrc.auto_region_delete) {
754 if (TEXT(bv)->selection){
755 TEXT(bv)->CutSelection(bv, false);
758 TEXT(bv)->ClearSelection();
759 for (string::size_type i = 0; i < arg.length(); ++i) {
760 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
763 UpdateLocal(bv, CURSOR_PAR, true);
765 // --- Cursor Movements ---------------------------------------------
767 bv->text->FinishUndo();
768 moveRight(bv, false, true);
769 TEXT(bv)->SetSelection();
770 UpdateLocal(bv, SELECTION, false);
773 result = moveRight(bv);
774 bv->text->FinishUndo();
775 TEXT(bv)->ClearSelection();
776 UpdateLocal(bv, CURSOR, false);
779 bv->text->FinishUndo();
780 moveLeft(bv, false, true);
781 TEXT(bv)->SetSelection();
782 UpdateLocal(bv, SELECTION, false);
785 bv->text->FinishUndo();
786 result= moveLeft(bv);
787 TEXT(bv)->ClearSelection();
788 UpdateLocal(bv, CURSOR, false);
791 bv->text->FinishUndo();
793 TEXT(bv)->SetSelection();
794 UpdateLocal(bv, SELECTION, false);
797 bv->text->FinishUndo();
798 result = moveDown(bv);
799 TEXT(bv)->ClearSelection();
800 UpdateLocal(bv, CURSOR, false);
803 bv->text->FinishUndo();
805 TEXT(bv)->SetSelection();
806 UpdateLocal(bv, SELECTION, false);
809 bv->text->FinishUndo();
811 TEXT(bv)->ClearSelection();
812 UpdateLocal(bv, CURSOR, false);
815 bv->text->FinishUndo();
816 TEXT(bv)->CursorHome(bv);
817 UpdateLocal(bv, CURSOR, false);
820 TEXT(bv)->CursorEnd(bv);
821 UpdateLocal(bv, CURSOR, false);
824 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
826 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
827 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
829 bv->text->cursor.par()->previous,
830 bv->text->cursor.par()->next
833 if (TEXT(bv)->selection)
834 TEXT(bv)->CutSelection(bv);
836 TEXT(bv)->Backspace(bv);
837 UpdateLocal(bv, CURSOR_PAR, true);
840 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
842 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
843 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
845 bv->text->cursor.par()->previous,
846 bv->text->cursor.par()->next
849 if (TEXT(bv)->selection)
850 TEXT(bv)->CutSelection(bv);
852 TEXT(bv)->Delete(bv);
853 UpdateLocal(bv, CURSOR_PAR, true);
856 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
858 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
859 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
861 bv->text->cursor.par()->previous,
862 bv->text->cursor.par()->next
865 TEXT(bv)->CutSelection(bv);
866 UpdateLocal(bv, CURSOR_PAR, true);
869 bv->text->FinishUndo();
870 TEXT(bv)->CopySelection(bv);
871 UpdateLocal(bv, CURSOR_PAR, false);
873 case LFUN_PASTESELECTION:
875 string clip(bv->workarea()->getClipboard());
879 if (arg == "paragraph") {
880 TEXT(bv)->InsertStringB(bv, clip);
882 TEXT(bv)->InsertStringA(bv, clip);
884 UpdateLocal(bv, CURSOR_PAR, true);
888 if (!autoBreakRows) {
891 if (cap.nrOfParagraphs() > 1) {
892 WriteAlert(_("Impossible operation"),
893 _("Cannot include more than one paragraph!"),
898 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
900 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
901 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
903 bv->text->cursor.par()->previous,
904 bv->text->cursor.par()->next
907 TEXT(bv)->PasteSelection(bv);
908 UpdateLocal(bv, CURSOR_PAR, true);
910 case LFUN_BREAKPARAGRAPH:
913 TEXT(bv)->BreakParagraph(bv, 0);
914 UpdateLocal(bv, FULL, true);
919 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
921 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
922 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
924 bv->text->cursor.par()->previous,
925 bv->text->cursor.par()->next
928 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
929 UpdateLocal(bv, CURSOR_PAR, true);
932 // do not set layouts on non breakable textinsets
934 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
936 // Derive layout number from given argument (string)
937 // and current buffer's textclass (number). */
938 LyXTextClassList::ClassList::size_type tclass =
939 bv->buffer()->params.textclass;
940 std::pair <bool, LyXTextClass::size_type> layout =
941 textclasslist.NumberOfLayout(tclass, arg);
943 // If the entry is obsolete, use the new one instead.
945 string obs = textclasslist.Style(tclass,layout.second).
948 layout = textclasslist.NumberOfLayout(tclass, obs);
951 // see if we found the layout number:
953 string msg = string(N_("Layout ")) + arg + N_(" not known");
955 bv->owner()->getMiniBuffer()->Set(msg);
959 if (cur_layout != layout.second) {
960 cur_layout = layout.second;
961 TEXT(bv)->SetLayout(bv, layout.second);
962 bv->owner()->setLayout(cpar(bv)->GetLayout());
963 UpdateLocal(bv, CURSOR_PAR, true);
966 // reset the layout box
967 bv->owner()->setLayout(cpar(bv)->GetLayout());
970 case LFUN_PARAGRAPH_SPACING:
971 // This one is absolutely not working. When fiddling with this
972 // it also seems to me that the paragraphs inside the insettext
973 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
975 LyXParagraph * par = TEXT(bv)->cursor.par();
976 Spacing::Space cur_spacing = par->spacing.getSpace();
977 float cur_value = 1.0;
978 if (cur_spacing == Spacing::Other) {
979 cur_value = par->spacing.getValue();
982 std::istringstream istr(arg.c_str());
985 Spacing::Space new_spacing = cur_spacing;
986 float new_value = cur_value;
988 lyxerr << "Missing argument to `paragraph-spacing'"
990 } else if (tmp == "single") {
991 new_spacing = Spacing::Single;
992 } else if (tmp == "onehalf") {
993 new_spacing = Spacing::Onehalf;
994 } else if (tmp == "double") {
995 new_spacing = Spacing::Double;
996 } else if (tmp == "other") {
997 new_spacing = Spacing::Other;
1000 lyxerr << "new_value = " << tmpval << endl;
1003 } else if (tmp == "default") {
1004 new_spacing = Spacing::Default;
1006 lyxerr << _("Unknown spacing argument: ")
1009 if (cur_spacing != new_spacing || cur_value != new_value) {
1010 par->spacing.set(new_spacing, new_value);
1011 //TEXT(bv)->RedoParagraph(owner->view());
1012 UpdateLocal(bv, CURSOR_PAR, true);
1013 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1019 result = UNDISPATCHED;
1022 if (result != FINISHED) {
1023 ShowInsetCursor(bv);
1025 bv->unlockInset(this);
1030 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1033 buf->latexParagraphs(os, par, 0, texrow);
1034 return texrow.rows();
1038 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1040 LyXParagraph * p = par;
1041 unsigned int lines = 0;
1044 string const tmp = buf->asciiParagraph(p, linelen);
1045 lines += countChar(tmp, '\n');
1055 void InsetText::Validate(LaTeXFeatures & features) const
1057 LyXParagraph * p = par;
1059 p->validate(features);
1065 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1067 if (textclasslist.Style(buf->params.textclass,
1068 p->GetLayout()).labeltype != LABEL_MANUAL)
1071 return p->BeginningOfMainBody();
1075 void InsetText::GetCursorPos(BufferView * bv,
1076 int & x, int & y) const
1083 unsigned int InsetText::InsetInInsetY()
1085 if (!the_locking_inset)
1088 return (inset_y + the_locking_inset->InsetInInsetY());
1092 void InsetText::ToggleInsetCursor(BufferView * bv)
1094 if (the_locking_inset) {
1095 the_locking_inset->ToggleInsetCursor(bv);
1099 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1101 int asc = lyxfont::maxAscent(font);
1102 int desc = lyxfont::maxDescent(font);
1105 bv->hideLockedInsetCursor();
1107 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1108 cursor_visible = !cursor_visible;
1112 void InsetText::ShowInsetCursor(BufferView * bv)
1114 if (the_locking_inset) {
1115 the_locking_inset->ShowInsetCursor(bv);
1118 if (!cursor_visible) {
1119 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1121 int asc = lyxfont::maxAscent(font);
1122 int desc = lyxfont::maxDescent(font);
1124 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1125 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1126 cursor_visible = true;
1131 void InsetText::HideInsetCursor(BufferView * bv)
1133 if (cursor_visible) {
1134 bv->hideLockedInsetCursor();
1135 cursor_visible = false;
1137 if (the_locking_inset)
1138 the_locking_inset->HideInsetCursor(bv);
1142 UpdatableInset::RESULT
1143 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1145 if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1147 if (activate_inset && checkAndActivateInset(bv, false))
1149 TEXT(bv)->CursorRight(bv, selecting);
1150 return DISPATCHED_NOUPDATE;
1154 UpdatableInset::RESULT
1155 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1157 if (!cpar(bv)->previous && (cpos(bv) <= 0))
1159 TEXT(bv)->CursorLeft(bv, selecting);
1160 if (activate_inset && checkAndActivateInset(bv, true))
1162 return DISPATCHED_NOUPDATE;
1166 UpdatableInset::RESULT
1167 InsetText::moveUp(BufferView * bv)
1169 if (!crow(bv)->previous())
1171 TEXT(bv)->CursorUp(bv);
1172 return DISPATCHED_NOUPDATE;
1176 UpdatableInset::RESULT
1177 InsetText::moveDown(BufferView * bv)
1179 if (!crow(bv)->next())
1181 TEXT(bv)->CursorDown(bv);
1182 return DISPATCHED_NOUPDATE;
1186 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1188 if (the_locking_inset) {
1189 if (the_locking_inset->InsertInsetAllowed(inset))
1190 return the_locking_inset->InsertInset(bv, inset);
1193 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1195 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1196 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1198 bv->text->cursor.par()->previous,
1199 bv->text->cursor.par()->next
1202 if (inset->Editable() == Inset::IS_EDITABLE) {
1203 UpdatableInset * i = static_cast<UpdatableInset *>(inset);
1204 i->setOwner(static_cast<UpdatableInset *>(this));
1206 HideInsetCursor(bv);
1207 TEXT(bv)->InsertInset(bv, inset);
1208 TEXT(bv)->selection = 0;
1209 bv->fitCursor(TEXT(bv));
1210 UpdateLocal(bv, CURSOR_PAR, true);
1211 static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1212 ShowInsetCursor(bv);
1217 UpdatableInset * InsetText::GetLockingInset()
1219 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1223 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1227 if (the_locking_inset)
1228 return the_locking_inset->GetFirstLockingInsetOfType(c);
1233 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1235 bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1237 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1238 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1240 bv->text->cursor.par()->previous,
1241 bv->text->cursor.par()->next
1244 TEXT(bv)->SetFont(bv, font, toggleall);
1245 bv->fitCursor(TEXT(bv));
1246 UpdateLocal(bv, CURSOR_PAR, true);
1250 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1252 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1256 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1257 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1259 LyXFont const font =
1260 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1262 x = inset->width(bv, font);
1263 y = inset->descent(bv, font);
1267 inset_x = cx(bv) - top_x + drawTextXOffset;
1268 inset_y = cy(bv) + drawTextYOffset;
1269 inset->Edit(bv, x - inset_x, y - inset_y, 0);
1270 if (!the_locking_inset)
1272 UpdateLocal(bv, CURSOR_PAR, false);
1279 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1282 x = x - drawTextXOffset;
1283 y = y + insetAscent;
1284 Inset * inset = bv->checkInsetHit(TEXT(bv), x, y, button);
1291 inset_x = cx(bv) - top_x + drawTextXOffset;
1292 inset_y = cy(bv) + drawTextYOffset;
1293 inset->Edit(bv, x - inset_x, y - inset_y, button);
1294 if (!the_locking_inset)
1296 UpdateLocal(bv, CURSOR_PAR, false);
1303 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1305 int w = UpdatableInset::getMaxWidth(pain, inset);
1310 w = w - top_x + owner()->x();
1313 w -= (2 * TEXT_TO_INSET_OFFSET);
1315 // return w - (2*TEXT_TO_INSET_OFFSET);
1319 void InsetText::SetParagraphData(LyXParagraph *p)
1333 par->SetInsetOwner(this);
1337 np->next = p->Clone();
1338 np->next->previous = np;
1340 np->SetInsetOwner(this);
1346 void InsetText::SetAutoBreakRows(bool flag)
1348 if (flag != autoBreakRows) {
1349 autoBreakRows = flag;
1357 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1359 if (how != drawFrame) {
1362 UpdateLocal(bv, DRAW_FRAME, false);
1367 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1369 if (frame_color != col) {
1372 UpdateLocal(bv, DRAW_FRAME, false);
1377 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1379 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1384 int InsetText::cx(BufferView * bv) const
1386 return TEXT(bv)->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1390 int InsetText::cy(BufferView * bv) const
1393 return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1397 int InsetText::cpos(BufferView * bv) const
1399 return TEXT(bv)->cursor.pos();
1403 LyXParagraph * InsetText::cpar(BufferView * bv) const
1405 return TEXT(bv)->cursor.par();
1409 Row * InsetText::crow(BufferView * bv) const
1411 return TEXT(bv)->cursor.row();
1415 LyXText * InsetText::getLyXText(BufferView * bv) const
1417 if (cache.find(bv) != cache.end())
1419 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1422 if (the_locking_inset) {
1423 lt->SetCursor(bv, inset_par, inset_pos);
1429 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1431 if (cache.find(bv) == cache.end())
1436 /// then remove all LyXText in text-insets
1437 LyXParagraph * p = par;
1438 for(;p;p = p->next) {
1439 p->deleteInsetsLyXText(bv);
1445 void InsetText::resizeLyXText(BufferView * bv) const
1447 if (!par->next && !par->size()) // resize not neccessary!
1449 if (cache.find(bv) == cache.end())
1452 LyXParagraph * lpar = 0;
1453 LyXParagraph * selstartpar = 0;
1454 LyXParagraph * selendpar = 0;
1456 int selstartpos = 0;
1461 // ProhibitInput(bv);
1464 lpar = TEXT(bv)->cursor.par();
1465 pos = TEXT(bv)->cursor.pos();
1466 selstartpar = TEXT(bv)->sel_start_cursor.par();
1467 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1468 selendpar = TEXT(bv)->sel_end_cursor.par();
1469 selendpos = TEXT(bv)->sel_end_cursor.pos();
1470 selection = TEXT(bv)->selection;
1471 mark_set = TEXT(bv)->mark_set;
1473 deleteLyXText(bv, (the_locking_inset == 0));
1476 TEXT(bv)->selection = true;
1477 /* at this point just to avoid the Delete-Empty-Paragraph
1478 * Mechanism when setting the cursor */
1479 TEXT(bv)->mark_set = mark_set;
1481 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos);
1482 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1483 TEXT(bv)->SetCursor(bv, selendpar, selendpos);
1484 TEXT(bv)->SetSelection();
1485 TEXT(bv)->SetCursor(bv, lpar, pos);
1487 TEXT(bv)->SetCursor(bv, lpar, pos);
1488 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1489 TEXT(bv)->selection = false;
1493 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1494 // this will scroll the screen such that the cursor becomes visible
1495 bv->updateScrollbar();
1497 if (the_locking_inset) {
1498 /// then resize all LyXText in text-insets
1499 inset_x = cx(bv) - top_x + drawTextXOffset;
1500 inset_y = cy(bv) + drawTextYOffset;
1501 for(LyXParagraph * p = par; p; p = p->next) {
1502 p->resizeInsetsLyXText(bv);
1509 void InsetText::removeNewlines()
1511 for(LyXParagraph * p = par; p; p = p->next) {
1512 for(int i = 0; i < p->Last(); ++i) {
1513 if (p->GetChar(i) == LyXParagraph::META_NEWLINE)