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 fa = row->ascent_of_text();
321 if (((y+row->height()) > 0) && (y < pain.paperHeight()))
326 TEXT(bv)->first = y - top_baseline + fa;
327 if (cleared || !locked || (need_update == FULL)) {
329 if (((y+row->height()) > 0) && (y < pain.paperHeight()))
330 TEXT(bv)->GetVisibleRow(bv, int(y), int(x), row, y, cleared);
334 } else if (need_update == SELECTION) {
335 bv->screen()->ToggleToggle(TEXT(bv), int(y), int(x));
338 if (need_update == CURSOR) {
339 bv->screen()->ToggleSelection(TEXT(bv), true, int(y), int(x));
340 TEXT(bv)->ClearSelection();
341 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
343 bv->screen()->Update(TEXT(bv), int(y), int(x));
346 TEXT(bv)->refresh_y = 0;
347 TEXT(bv)->status = LyXText::UNCHANGED;
348 if ((need_update != CURSOR_PAR) &&
349 ((drawFrame == ALWAYS) || ((drawFrame == LOCKED) && locked)))
351 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
352 width(bv, f) - 1, insetAscent + insetDescent - 1,
354 } else if (need_update == CLEAR_FRAME) {
355 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
356 width(bv, f) - 1, insetAscent + insetDescent - 1,
359 x += width(bv, f) - TEXT_TO_INSET_OFFSET;
360 if (bv->text->status==LyXText::CHANGED_IN_DRAW)
362 else if (need_update != INIT)
367 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
369 if (reinit) { // && (need_update != CURSOR)) {
373 owner()->update(bv, font, true);
376 if (the_locking_inset) {
377 inset_x = cx(bv) - top_x + drawTextXOffset;
378 inset_y = cy(bv) + drawTextYOffset;
379 the_locking_inset->update(bv, font, reinit);
381 if (need_update == INIT) {
384 // if (!owner() && bv->text)
385 // bv->text->UpdateInset(bv, this);
387 int oldw = insetWidth;
389 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
390 // max(textWidth(bv->painter()),
391 // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
392 // (2 * TEXT_TO_INSET_OFFSET);
394 insetWidth = textWidth(bv->painter());
396 insetWidth = static_cast<int>(TEXT(bv)->width);
398 if (oldw != insetWidth) {
399 // printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
400 // textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
405 owner()->update(bv, font, reinit);
408 update(bv, font, reinit);
412 update(bv, font, reinit);
414 UpdateLocal(bv, INIT, false);
419 if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
422 TEXT(bv)->UpdateInset(bv, the_locking_inset);
425 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
429 Row * row = TEXT(bv)->GetRowNearY(y_temp);
430 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
431 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
432 TEXT_TO_INSET_OFFSET;
436 void InsetText::UpdateLocal(BufferView * bv, UpdateCodes what, bool mark_dirty)
438 TEXT(bv)->FullRebreak(bv);
439 if (need_update != INIT) {
440 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
442 else if (!the_locking_inset || (what != CURSOR))
445 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
447 bv->updateInset(this, mark_dirty);
448 bv->owner()->showState();
449 if (old_par != cpar(bv)) {
450 bv->owner()->setLayout(cpar(bv)->GetLayout());
456 string const InsetText::EditMessage() const
458 return _("Opened Text Inset");
462 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
464 // par->SetInsetOwner(this);
465 UpdatableInset::Edit(bv, x, y, button);
467 if (!bv->lockInset(this)) {
468 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
472 the_locking_inset = 0;
473 inset_pos = inset_x = inset_y = 0;
476 if (!checkAndActivateInset(bv, x, y, button))
477 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
479 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
480 bv->text->FinishUndo();
482 UpdateLocal(bv, FULL, false);
486 void InsetText::InsetUnlock(BufferView * bv)
488 if (the_locking_inset) {
489 the_locking_inset->InsetUnlock(bv);
490 the_locking_inset = 0;
493 no_selection = false;
495 TEXT(bv)->selection = 0;
496 UpdateLocal(bv, CLEAR_FRAME, false);
498 bv->owner()->setLayout(owner()->getLyXText(bv)
499 ->cursor.par()->GetLayout());
501 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
505 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
507 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
510 if (inset == cpar(bv)->GetInset(cpos(bv))) {
511 lyxerr[Debug::INSETS] << "OK" << endl;
512 the_locking_inset = inset;
513 inset_x = cx(bv) - top_x + drawTextXOffset;
514 inset_y = cy(bv) + drawTextYOffset;
515 inset_pos = cpos(bv);
516 inset_par = cpar(bv);
517 TEXT(bv)->UpdateInset(bv, the_locking_inset);
519 } else if (the_locking_inset && (the_locking_inset == inset)) {
520 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
521 lyxerr[Debug::INSETS] << "OK" << endl;
522 inset_x = cx(bv) - top_x + drawTextXOffset;
523 inset_y = cy(bv) + drawTextYOffset;
525 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
527 } else if (the_locking_inset) {
528 lyxerr[Debug::INSETS] << "MAYBE" << endl;
529 return the_locking_inset->LockInsetInInset(bv, inset);
531 lyxerr[Debug::INSETS] << "NOT OK" << endl;
536 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
539 if (!the_locking_inset)
541 if (the_locking_inset == inset) {
542 the_locking_inset->InsetUnlock(bv);
543 TEXT(bv)->UpdateInset(bv, inset);
544 the_locking_inset = 0;
546 moveRight(bv, false);
547 old_par = 0; // force layout setting
548 UpdateLocal(bv, CURSOR_PAR, false);
551 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
555 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
557 if (!the_locking_inset)
559 if (the_locking_inset != inset) {
560 TEXT(bv)->UpdateInset(bv, the_locking_inset);
561 need_update = CURSOR_PAR;
562 return the_locking_inset->UpdateInsetInInset(bv, inset);
564 // UpdateLocal(bv, FULL, false);
565 if (TEXT(bv)->UpdateInset(bv, inset))
566 UpdateLocal(bv, CURSOR_PAR, false);
567 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
568 inset_x = cx(bv) - top_x + drawTextXOffset;
569 inset_y = cy(bv) + drawTextYOffset;
575 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
577 no_selection = false;
579 int tmp_x = x - drawTextXOffset;
580 int tmp_y = y + insetAscent;
581 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
584 if (the_locking_inset) {
585 if (the_locking_inset == inset) {
586 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
589 // otherwise unlock the_locking_inset and lock the new inset
590 the_locking_inset->InsetUnlock(bv);
591 inset_x = cx(bv) - top_x + drawTextXOffset;
592 inset_y = cy(bv) + drawTextYOffset;
593 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
594 inset->Edit(bv, x - inset_x, y - inset_y, button);
595 if (the_locking_inset) {
596 UpdateLocal(bv, CURSOR_PAR, false);
600 // otherwise only unlock the_locking_inset
601 the_locking_inset->InsetUnlock(bv);
602 the_locking_inset = 0;
604 if (bv->theLockingInset()) {
605 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
606 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
607 inset_x = cx(bv) - top_x + drawTextXOffset;
608 inset_y = cy(bv) + drawTextYOffset;
609 inset_pos = cpos(bv);
610 inset_par = cpar(bv);
611 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
612 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
613 if (the_locking_inset) {
614 UpdateLocal(bv, CURSOR_PAR, false);
620 bool paste_internally = false;
621 if ((button == 2) && TEXT(bv)->selection) {
622 LocalDispatch(bv, LFUN_COPY, "");
623 paste_internally = true;
625 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
627 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
628 UpdateLocal(bv, CURSOR, false);
629 bv->owner()->setLayout(cpar(bv)->GetLayout());
631 // Insert primary selection with middle mouse
632 // if there is a local selection in the current buffer,
635 if (paste_internally)
636 LocalDispatch(bv, LFUN_PASTE, "");
638 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
645 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
647 UpdatableInset * inset = 0;
649 if (the_locking_inset) {
650 the_locking_inset->InsetButtonRelease(bv,
651 x - inset_x, y - inset_y,
654 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
655 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
656 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
657 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
659 inset_x = cx(bv) - top_x + drawTextXOffset;
660 inset_y = cy(bv) + drawTextYOffset;
661 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
662 inset->Edit(bv, x - inset_x, y - inset_y, button);
664 UpdateLocal(bv, CURSOR_PAR, false);
667 no_selection = false;
671 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
673 if (the_locking_inset) {
674 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
680 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
682 TEXT(bv)->SetSelection();
683 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
684 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
685 UpdateLocal(bv, SELECTION, false);
688 no_selection = false;
692 void InsetText::InsetKeyPress(XKeyEvent * xke)
694 if (the_locking_inset) {
695 the_locking_inset->InsetKeyPress(xke);
701 UpdatableInset::RESULT
702 InsetText::LocalDispatch(BufferView * bv,
703 int action, string const & arg)
705 no_selection = false;
706 UpdatableInset::RESULT
707 result= UpdatableInset::LocalDispatch(bv, action, arg);
708 if (result != UNDISPATCHED) {
713 if ((action < 0) && arg.empty())
716 if (the_locking_inset) {
717 result = the_locking_inset->LocalDispatch(bv, action, arg);
718 if (result == DISPATCHED_NOUPDATE)
720 else if (result == DISPATCHED) {
721 UpdateLocal(bv, CURSOR_PAR, false);
723 } else if (result == FINISHED) {
727 moveRight(bv, false);
733 the_locking_inset = 0;
741 if (bv->buffer()->isReadonly()) {
743 // setErrorMessage(N_("Document is read only"));
747 /* Automatically delete the currently selected
748 * text and replace it with what is being
749 * typed in now. Depends on lyxrc settings
750 * "auto_region_delete", which defaults to
753 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
755 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
756 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
758 bv->text->cursor.par()->previous,
759 bv->text->cursor.par()->next
762 // if an empty paragraph set the language to the surronding
763 // paragraph language on insertion of the first character!
764 if (!par->Last() && !par->next) {
767 Inset * inset = owner();
768 while(inset && inset->getLyXText(bv) == TEXT(bv))
769 inset = inset->owner();
771 text = inset->getLyXText(bv);
775 LyXFont font(LyXFont::ALL_IGNORE);
776 font.setLanguage(text->cursor.par()->getParLanguage(bv->buffer()->params));
777 SetFont(bv, font, false);
779 if (lyxrc.auto_region_delete) {
780 if (TEXT(bv)->selection){
781 TEXT(bv)->CutSelection(bv, false);
784 TEXT(bv)->ClearSelection();
785 for (string::size_type i = 0; i < arg.length(); ++i) {
786 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
789 UpdateLocal(bv, CURSOR_PAR, true);
791 // --- Cursor Movements ---------------------------------------------
793 bv->text->FinishUndo();
794 moveRight(bv, false, true);
795 TEXT(bv)->SetSelection();
796 UpdateLocal(bv, SELECTION, false);
799 result = moveRight(bv);
800 bv->text->FinishUndo();
801 TEXT(bv)->ClearSelection();
802 UpdateLocal(bv, CURSOR, false);
805 bv->text->FinishUndo();
806 moveLeft(bv, false, true);
807 TEXT(bv)->SetSelection();
808 UpdateLocal(bv, SELECTION, false);
811 bv->text->FinishUndo();
812 result= moveLeft(bv);
813 TEXT(bv)->ClearSelection();
814 UpdateLocal(bv, CURSOR, false);
817 bv->text->FinishUndo();
819 TEXT(bv)->SetSelection();
820 UpdateLocal(bv, SELECTION, false);
823 bv->text->FinishUndo();
824 result = moveDown(bv);
825 TEXT(bv)->ClearSelection();
826 UpdateLocal(bv, CURSOR, false);
829 bv->text->FinishUndo();
831 TEXT(bv)->SetSelection();
832 UpdateLocal(bv, SELECTION, false);
835 bv->text->FinishUndo();
837 TEXT(bv)->ClearSelection();
838 UpdateLocal(bv, CURSOR, false);
841 bv->text->FinishUndo();
842 TEXT(bv)->CursorHome(bv);
843 UpdateLocal(bv, CURSOR, false);
846 TEXT(bv)->CursorEnd(bv);
847 UpdateLocal(bv, CURSOR, false);
850 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
852 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
853 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
855 bv->text->cursor.par()->previous,
856 bv->text->cursor.par()->next
859 if (TEXT(bv)->selection)
860 TEXT(bv)->CutSelection(bv);
862 TEXT(bv)->Backspace(bv);
863 UpdateLocal(bv, CURSOR_PAR, true);
866 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
868 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
869 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
871 bv->text->cursor.par()->previous,
872 bv->text->cursor.par()->next
875 if (TEXT(bv)->selection)
876 TEXT(bv)->CutSelection(bv);
878 TEXT(bv)->Delete(bv);
879 UpdateLocal(bv, CURSOR_PAR, true);
882 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
884 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
885 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
887 bv->text->cursor.par()->previous,
888 bv->text->cursor.par()->next
891 TEXT(bv)->CutSelection(bv);
892 UpdateLocal(bv, CURSOR_PAR, true);
895 bv->text->FinishUndo();
896 TEXT(bv)->CopySelection(bv);
897 UpdateLocal(bv, CURSOR_PAR, false);
899 case LFUN_PASTESELECTION:
901 string clip(bv->workarea()->getClipboard());
905 if (arg == "paragraph") {
906 TEXT(bv)->InsertStringB(bv, clip);
908 TEXT(bv)->InsertStringA(bv, clip);
910 UpdateLocal(bv, CURSOR_PAR, true);
914 if (!autoBreakRows) {
917 if (cap.nrOfParagraphs() > 1) {
918 WriteAlert(_("Impossible operation"),
919 _("Cannot include more than one paragraph!"),
924 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
926 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
927 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
929 bv->text->cursor.par()->previous,
930 bv->text->cursor.par()->next
933 TEXT(bv)->PasteSelection(bv);
934 UpdateLocal(bv, CURSOR_PAR, true);
936 case LFUN_BREAKPARAGRAPH:
939 TEXT(bv)->BreakParagraph(bv, 0);
940 UpdateLocal(bv, FULL, true);
945 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
947 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
948 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
950 bv->text->cursor.par()->previous,
951 bv->text->cursor.par()->next
954 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
955 UpdateLocal(bv, CURSOR_PAR, true);
958 // do not set layouts on non breakable textinsets
960 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
962 // Derive layout number from given argument (string)
963 // and current buffer's textclass (number). */
964 LyXTextClassList::ClassList::size_type tclass =
965 bv->buffer()->params.textclass;
966 std::pair <bool, LyXTextClass::size_type> layout =
967 textclasslist.NumberOfLayout(tclass, arg);
969 // If the entry is obsolete, use the new one instead.
971 string obs = textclasslist.Style(tclass,layout.second).
974 layout = textclasslist.NumberOfLayout(tclass, obs);
977 // see if we found the layout number:
979 string msg = string(N_("Layout ")) + arg + N_(" not known");
981 bv->owner()->getMiniBuffer()->Set(msg);
985 if (cur_layout != layout.second) {
986 cur_layout = layout.second;
987 TEXT(bv)->SetLayout(bv, layout.second);
988 bv->owner()->setLayout(cpar(bv)->GetLayout());
989 UpdateLocal(bv, CURSOR_PAR, true);
992 // reset the layout box
993 bv->owner()->setLayout(cpar(bv)->GetLayout());
996 case LFUN_PARAGRAPH_SPACING:
997 // This one is absolutely not working. When fiddling with this
998 // it also seems to me that the paragraphs inside the insettext
999 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1001 LyXParagraph * par = TEXT(bv)->cursor.par();
1002 Spacing::Space cur_spacing = par->spacing.getSpace();
1003 float cur_value = 1.0;
1004 if (cur_spacing == Spacing::Other) {
1005 cur_value = par->spacing.getValue();
1008 std::istringstream istr(arg.c_str());
1011 Spacing::Space new_spacing = cur_spacing;
1012 float new_value = cur_value;
1014 lyxerr << "Missing argument to `paragraph-spacing'"
1016 } else if (tmp == "single") {
1017 new_spacing = Spacing::Single;
1018 } else if (tmp == "onehalf") {
1019 new_spacing = Spacing::Onehalf;
1020 } else if (tmp == "double") {
1021 new_spacing = Spacing::Double;
1022 } else if (tmp == "other") {
1023 new_spacing = Spacing::Other;
1026 lyxerr << "new_value = " << tmpval << endl;
1029 } else if (tmp == "default") {
1030 new_spacing = Spacing::Default;
1032 lyxerr << _("Unknown spacing argument: ")
1035 if (cur_spacing != new_spacing || cur_value != new_value) {
1036 par->spacing.set(new_spacing, new_value);
1037 //TEXT(bv)->RedoParagraph(owner->view());
1038 UpdateLocal(bv, CURSOR_PAR, true);
1039 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1045 result = UNDISPATCHED;
1048 if (result != FINISHED) {
1049 ShowInsetCursor(bv);
1051 bv->unlockInset(this);
1056 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1059 buf->latexParagraphs(os, par, 0, texrow);
1060 return texrow.rows();
1064 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1066 LyXParagraph * p = par;
1067 unsigned int lines = 0;
1071 tmp = buf->asciiParagraph(p, linelen);
1072 lines += countChar(tmp, '\n');
1080 void InsetText::Validate(LaTeXFeatures & features) const
1082 LyXParagraph * p = par;
1084 p->validate(features);
1090 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1092 if (textclasslist.Style(buf->params.textclass,
1093 p->GetLayout()).labeltype != LABEL_MANUAL)
1096 return p->BeginningOfMainBody();
1100 void InsetText::GetCursorPos(BufferView * bv,
1101 int & x, int & y) const
1108 unsigned int InsetText::InsetInInsetY()
1110 if (!the_locking_inset)
1113 return (inset_y + the_locking_inset->InsetInInsetY());
1117 void InsetText::ToggleInsetCursor(BufferView * bv)
1119 if (the_locking_inset) {
1120 the_locking_inset->ToggleInsetCursor(bv);
1124 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1126 int asc = lyxfont::maxAscent(font);
1127 int desc = lyxfont::maxDescent(font);
1130 bv->hideLockedInsetCursor();
1132 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1133 cursor_visible = !cursor_visible;
1137 void InsetText::ShowInsetCursor(BufferView * bv, bool show)
1139 if (the_locking_inset) {
1140 the_locking_inset->ShowInsetCursor(bv);
1143 if (!cursor_visible) {
1144 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1146 int asc = lyxfont::maxAscent(font);
1147 int desc = lyxfont::maxDescent(font);
1149 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1151 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1152 cursor_visible = true;
1157 void InsetText::HideInsetCursor(BufferView * bv)
1159 if (cursor_visible) {
1160 bv->hideLockedInsetCursor();
1161 cursor_visible = false;
1163 if (the_locking_inset)
1164 the_locking_inset->HideInsetCursor(bv);
1168 UpdatableInset::RESULT
1169 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1171 if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1173 if (activate_inset && checkAndActivateInset(bv, false))
1175 TEXT(bv)->CursorRight(bv, selecting);
1176 return DISPATCHED_NOUPDATE;
1180 UpdatableInset::RESULT
1181 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1183 if (!cpar(bv)->previous && (cpos(bv) <= 0))
1185 TEXT(bv)->CursorLeft(bv, selecting);
1186 if (activate_inset && checkAndActivateInset(bv, true))
1188 return DISPATCHED_NOUPDATE;
1192 UpdatableInset::RESULT
1193 InsetText::moveUp(BufferView * bv)
1195 if (!crow(bv)->previous())
1197 TEXT(bv)->CursorUp(bv);
1198 return DISPATCHED_NOUPDATE;
1202 UpdatableInset::RESULT
1203 InsetText::moveDown(BufferView * bv)
1205 if (!crow(bv)->next())
1207 TEXT(bv)->CursorDown(bv);
1208 return DISPATCHED_NOUPDATE;
1212 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1214 if (the_locking_inset) {
1215 if (the_locking_inset->InsertInsetAllowed(inset))
1216 return the_locking_inset->InsertInset(bv, inset);
1219 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1221 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1222 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1224 bv->text->cursor.par()->previous,
1225 bv->text->cursor.par()->next
1228 if (inset->Editable() == Inset::IS_EDITABLE) {
1229 UpdatableInset * i = static_cast<UpdatableInset *>(inset);
1230 i->setOwner(static_cast<UpdatableInset *>(this));
1232 HideInsetCursor(bv);
1233 TEXT(bv)->InsertInset(bv, inset);
1234 TEXT(bv)->selection = 0;
1235 bv->fitCursor(TEXT(bv));
1236 UpdateLocal(bv, CURSOR_PAR, true);
1237 static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1238 ShowInsetCursor(bv);
1243 UpdatableInset * InsetText::GetLockingInset()
1245 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1249 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1253 if (the_locking_inset)
1254 return the_locking_inset->GetFirstLockingInsetOfType(c);
1259 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1261 bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1263 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1264 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1266 bv->text->cursor.par()->previous,
1267 bv->text->cursor.par()->next
1270 TEXT(bv)->SetFont(bv, font, toggleall);
1271 bv->fitCursor(TEXT(bv));
1272 UpdateLocal(bv, CURSOR_PAR, true);
1276 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1278 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1282 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1283 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1285 LyXFont const font =
1286 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1288 x = inset->width(bv, font);
1289 y = inset->descent(bv, font);
1293 inset_x = cx(bv) - top_x + drawTextXOffset;
1294 inset_y = cy(bv) + drawTextYOffset;
1295 inset->Edit(bv, x - inset_x, y - inset_y, 0);
1296 if (!the_locking_inset)
1298 UpdateLocal(bv, CURSOR_PAR, false);
1305 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1308 x = x - drawTextXOffset;
1309 y = y + insetAscent;
1310 Inset * inset = bv->checkInsetHit(TEXT(bv), x, y, button);
1317 inset_x = cx(bv) - top_x + drawTextXOffset;
1318 inset_y = cy(bv) + drawTextYOffset;
1319 inset->Edit(bv, x - inset_x, y - inset_y, button);
1320 if (!the_locking_inset)
1322 UpdateLocal(bv, CURSOR_PAR, false);
1329 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1331 int w = UpdatableInset::getMaxWidth(pain, inset);
1336 w = w - top_x + owner()->x();
1339 w -= (2 * TEXT_TO_INSET_OFFSET);
1341 // return w - (2*TEXT_TO_INSET_OFFSET);
1345 void InsetText::SetParagraphData(LyXParagraph *p)
1359 par->SetInsetOwner(this);
1363 np->next = p->Clone();
1364 np->next->previous = np;
1366 np->SetInsetOwner(this);
1372 void InsetText::SetAutoBreakRows(bool flag)
1374 if (flag != autoBreakRows) {
1375 autoBreakRows = flag;
1383 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1385 if (how != drawFrame) {
1388 UpdateLocal(bv, DRAW_FRAME, false);
1393 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1395 if (frame_color != col) {
1398 UpdateLocal(bv, DRAW_FRAME, false);
1403 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1405 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1410 int InsetText::cx(BufferView * bv) const
1412 return TEXT(bv)->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1416 int InsetText::cy(BufferView * bv) const
1419 return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1423 int InsetText::cpos(BufferView * bv) const
1425 return TEXT(bv)->cursor.pos();
1429 LyXParagraph * InsetText::cpar(BufferView * bv) const
1431 return TEXT(bv)->cursor.par();
1435 Row * InsetText::crow(BufferView * bv) const
1437 return TEXT(bv)->cursor.row();
1441 LyXText * InsetText::getLyXText(BufferView * bv) const
1443 if (cache.find(bv) != cache.end())
1445 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1448 if (the_locking_inset) {
1449 lt->SetCursor(bv, inset_par, inset_pos);
1455 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1457 if (cache.find(bv) == cache.end())
1462 /// then remove all LyXText in text-insets
1463 LyXParagraph * p = par;
1464 for(;p;p = p->next) {
1465 p->deleteInsetsLyXText(bv);
1471 void InsetText::resizeLyXText(BufferView * bv) const
1473 if (!par->next && !par->size()) // resize not neccessary!
1475 if (cache.find(bv) == cache.end())
1478 LyXParagraph * lpar = 0;
1479 LyXParagraph * selstartpar = 0;
1480 LyXParagraph * selendpar = 0;
1482 int selstartpos = 0;
1487 // ProhibitInput(bv);
1490 lpar = TEXT(bv)->cursor.par();
1491 pos = TEXT(bv)->cursor.pos();
1492 selstartpar = TEXT(bv)->sel_start_cursor.par();
1493 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1494 selendpar = TEXT(bv)->sel_end_cursor.par();
1495 selendpos = TEXT(bv)->sel_end_cursor.pos();
1496 selection = TEXT(bv)->selection;
1497 mark_set = TEXT(bv)->mark_set;
1499 deleteLyXText(bv, (the_locking_inset == 0));
1502 TEXT(bv)->selection = true;
1503 /* at this point just to avoid the Delete-Empty-Paragraph
1504 * Mechanism when setting the cursor */
1505 TEXT(bv)->mark_set = mark_set;
1507 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos);
1508 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1509 TEXT(bv)->SetCursor(bv, selendpar, selendpos);
1510 TEXT(bv)->SetSelection();
1511 TEXT(bv)->SetCursor(bv, lpar, pos);
1513 TEXT(bv)->SetCursor(bv, lpar, pos);
1514 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1515 TEXT(bv)->selection = false;
1519 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1520 // this will scroll the screen such that the cursor becomes visible
1521 bv->updateScrollbar();
1523 if (the_locking_inset) {
1524 /// then resize all LyXText in text-insets
1525 inset_x = cx(bv) - top_x + drawTextXOffset;
1526 inset_y = cy(bv) + drawTextYOffset;
1527 for(LyXParagraph * p = par; p; p = p->next) {
1528 p->resizeInsetsLyXText(bv);
1535 void InsetText::removeNewlines()
1537 for(LyXParagraph * p = par; p; p = p->next) {
1538 for(int i = 0; i < p->Last(); ++i) {
1539 if (p->GetChar(i) == LyXParagraph::META_NEWLINE)