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 LyXParagraph * p = par->next;
127 void InsetText::clear()
129 LyXParagraph * p = par->next;
136 par = new LyXParagraph();
140 Inset * InsetText::Clone(Buffer const &) const
142 InsetText * t = new InsetText(*this);
147 void InsetText::Write(Buffer const * buf, ostream & os) const
150 WriteParagraphData(buf, os);
154 void InsetText::WriteParagraphData(Buffer const * buf, ostream & os) const
156 par->writeFile(buf, os, buf->params, 0, 0);
160 void InsetText::Read(Buffer const * buf, LyXLex & lex)
164 LyXParagraph * return_par = 0;
165 char depth = 0; // signed or unsigned?
167 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
168 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
170 LyXFont font(LyXFont::ALL_INHERIT);
172 LyXParagraph * p = par->next;
179 par = new LyXParagraph;
182 token = lex.GetString();
185 if (token == "\\end_inset")
187 if (const_cast<Buffer*>(buf)->
188 parseSingleLyXformat2Token(lex, par, return_par,token, pos, depth,
191 , footnoteflag, footnotekind
195 // the_end read this should NEVER happen
196 lex.printError("\\the_end read in inset! Error in document!");
204 return_par->SetInsetOwner(this);
205 return_par = return_par->next;
208 if (token != "\\end_inset") {
209 lex.printError("Missing \\end_inset at this point. "
216 int InsetText::ascent(BufferView * bv, LyXFont const &) const
219 Row * row = TEXT(bv)->GetRowNearY(y_temp);
220 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
225 int InsetText::descent(BufferView * bv, LyXFont const &) const
228 Row * row = TEXT(bv)->GetRowNearY(y_temp);
229 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
230 TEXT_TO_INSET_OFFSET;
235 int InsetText::width(BufferView * bv, LyXFont const &) const
237 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
242 int InsetText::textWidth(Painter & pain) const
244 int w = getMaxWidth(pain, this);
249 void InsetText::draw(BufferView * bv, LyXFont const & f,
250 int baseline, float & x, bool cleared) const
252 Painter & pain = bv->painter();
254 // no draw is necessary !!!
255 if ((drawFrame == LOCKED) && !locked && !par->size()) {
256 if (!cleared && (need_update == CLEAR_FRAME)) {
257 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
259 insetAscent + insetDescent - 1,
263 top_baseline = baseline;
270 UpdatableInset::draw(bv, f, baseline, x, cleared);
272 if (!cleared && ((need_update==FULL) || (top_x!=int(x)) ||
273 (top_baseline!=baseline))) {
275 int h = insetAscent + insetDescent;
276 int ty = baseline - insetAscent;
282 if ((ty + h) > pain.paperHeight())
283 h = pain.paperHeight();
284 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
285 w = pain.paperWidth();
286 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
290 if (!cleared && (need_update == NONE))
293 if (top_x != int(x)) {
296 bv->text->status = LyXText::CHANGED_IN_DRAW;
300 top_baseline = baseline;
301 top_y = baseline - ascent(bv, f);
302 last_width = width(bv, f);
303 last_height = ascent(bv, f) + descent(bv, f);
305 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
306 inset_x = cx(bv) - top_x + drawTextXOffset;
307 inset_y = cy(bv) + drawTextYOffset;
309 if (!cleared && (need_update == CURSOR) && !TEXT(bv)->selection) {
314 x += TEXT_TO_INSET_OFFSET;
317 Row * row = TEXT(bv)->GetRowNearY(y);
318 int y_offset = baseline - row->ascent_of_text();
319 int ph = pain.paperHeight();
322 while ((row != 0) && ((y+row->height()) <= 0)) {
324 first += row->height();
329 TEXT(bv)->first = first;
330 if (cleared || !locked || (need_update == FULL)) {
333 while ((row != 0) && (yf < ph)) {
334 TEXT(bv)->GetVisibleRow(bv, y+y_offset, int(x), row,
340 } else if (need_update == SELECTION) {
341 bv->screen()->ToggleToggle(TEXT(bv), y_offset, int(x));
344 if (need_update == CURSOR) {
345 bv->screen()->ToggleSelection(TEXT(bv), true, y_offset,int(x));
346 TEXT(bv)->ClearSelection();
347 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
349 bv->screen()->Update(TEXT(bv), y_offset, int(x));
353 TEXT(bv)->refresh_y = 0;
354 TEXT(bv)->status = LyXText::UNCHANGED;
355 if ((need_update != CURSOR_PAR) &&
356 ((drawFrame == ALWAYS) || ((drawFrame == LOCKED) && locked)))
358 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
359 width(bv, f) - 1, insetAscent + insetDescent - 1,
361 } else if (need_update == CLEAR_FRAME) {
362 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
363 width(bv, f) - 1, insetAscent + insetDescent - 1,
366 x += width(bv, f) - TEXT_TO_INSET_OFFSET;
367 if (bv->text->status==LyXText::CHANGED_IN_DRAW)
369 else if (need_update != INIT)
374 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
376 if (reinit) { // && (need_update != CURSOR)) {
380 owner()->update(bv, font, true);
383 if (the_locking_inset) {
384 inset_x = cx(bv) - top_x + drawTextXOffset;
385 inset_y = cy(bv) + drawTextYOffset;
386 the_locking_inset->update(bv, font, reinit);
388 if (need_update == INIT) {
391 // if (!owner() && bv->text)
392 // bv->text->UpdateInset(bv, this);
394 int oldw = insetWidth;
396 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
397 // max(textWidth(bv->painter()),
398 // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
399 // (2 * TEXT_TO_INSET_OFFSET);
401 insetWidth = textWidth(bv->painter());
403 insetWidth = static_cast<int>(TEXT(bv)->width);
405 if (oldw != insetWidth) {
406 // printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
407 // textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
412 owner()->update(bv, font, reinit);
415 update(bv, font, reinit);
419 update(bv, font, reinit);
421 UpdateLocal(bv, INIT, false);
426 if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
429 TEXT(bv)->UpdateInset(bv, the_locking_inset);
432 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
436 Row * row = TEXT(bv)->GetRowNearY(y_temp);
437 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
438 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
439 TEXT_TO_INSET_OFFSET;
443 void InsetText::UpdateLocal(BufferView * bv, UpdateCodes what, bool mark_dirty)
445 TEXT(bv)->FullRebreak(bv);
446 if (need_update != INIT) {
447 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
449 else if (!the_locking_inset || (what != CURSOR))
452 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
454 bv->updateInset(this, mark_dirty);
455 bv->owner()->showState();
456 if (old_par != cpar(bv)) {
457 bv->owner()->setLayout(cpar(bv)->GetLayout());
463 string const InsetText::EditMessage() const
465 return _("Opened Text Inset");
469 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
471 // par->SetInsetOwner(this);
472 UpdatableInset::Edit(bv, x, y, button);
474 if (!bv->lockInset(this)) {
475 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
479 the_locking_inset = 0;
480 inset_pos = inset_x = inset_y = 0;
483 if (!checkAndActivateInset(bv, x, y, button))
484 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
486 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
487 bv->text->FinishUndo();
489 UpdateLocal(bv, FULL, false);
493 void InsetText::InsetUnlock(BufferView * bv)
495 if (the_locking_inset) {
496 the_locking_inset->InsetUnlock(bv);
497 the_locking_inset = 0;
500 no_selection = false;
502 TEXT(bv)->selection = 0;
503 UpdateLocal(bv, CLEAR_FRAME, false);
505 bv->owner()->setLayout(owner()->getLyXText(bv)
506 ->cursor.par()->GetLayout());
508 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
512 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
514 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
517 if (inset == cpar(bv)->GetInset(cpos(bv))) {
518 lyxerr[Debug::INSETS] << "OK" << endl;
519 the_locking_inset = inset;
520 inset_x = cx(bv) - top_x + drawTextXOffset;
521 inset_y = cy(bv) + drawTextYOffset;
522 inset_pos = cpos(bv);
523 inset_par = cpar(bv);
524 TEXT(bv)->UpdateInset(bv, the_locking_inset);
526 } else if (the_locking_inset && (the_locking_inset == inset)) {
527 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
528 lyxerr[Debug::INSETS] << "OK" << endl;
529 inset_x = cx(bv) - top_x + drawTextXOffset;
530 inset_y = cy(bv) + drawTextYOffset;
532 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
534 } else if (the_locking_inset) {
535 lyxerr[Debug::INSETS] << "MAYBE" << endl;
536 return the_locking_inset->LockInsetInInset(bv, inset);
538 lyxerr[Debug::INSETS] << "NOT OK" << endl;
543 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
546 if (!the_locking_inset)
548 if (the_locking_inset == inset) {
549 the_locking_inset->InsetUnlock(bv);
550 TEXT(bv)->UpdateInset(bv, inset);
551 the_locking_inset = 0;
553 moveRight(bv, false);
554 old_par = 0; // force layout setting
555 UpdateLocal(bv, CURSOR_PAR, false);
558 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
562 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
564 if (!the_locking_inset)
566 if (the_locking_inset != inset) {
567 TEXT(bv)->UpdateInset(bv, the_locking_inset);
568 need_update = CURSOR_PAR;
569 return the_locking_inset->UpdateInsetInInset(bv, inset);
571 // UpdateLocal(bv, FULL, false);
572 if (TEXT(bv)->UpdateInset(bv, inset))
573 UpdateLocal(bv, CURSOR_PAR, false);
574 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
575 inset_x = cx(bv) - top_x + drawTextXOffset;
576 inset_y = cy(bv) + drawTextYOffset;
582 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
584 no_selection = false;
586 int tmp_x = x - drawTextXOffset;
587 int tmp_y = y + insetAscent;
588 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
591 if (the_locking_inset) {
592 if (the_locking_inset == inset) {
593 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
596 // otherwise unlock the_locking_inset and lock the new inset
597 the_locking_inset->InsetUnlock(bv);
598 inset_x = cx(bv) - top_x + drawTextXOffset;
599 inset_y = cy(bv) + drawTextYOffset;
600 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
601 inset->Edit(bv, x - inset_x, y - inset_y, button);
602 if (the_locking_inset) {
603 UpdateLocal(bv, CURSOR_PAR, false);
607 // otherwise only unlock the_locking_inset
608 the_locking_inset->InsetUnlock(bv);
609 the_locking_inset = 0;
611 if (bv->theLockingInset()) {
612 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
613 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
614 inset_x = cx(bv) - top_x + drawTextXOffset;
615 inset_y = cy(bv) + drawTextYOffset;
616 inset_pos = cpos(bv);
617 inset_par = cpar(bv);
618 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
619 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
620 if (the_locking_inset) {
621 UpdateLocal(bv, CURSOR_PAR, false);
627 bool paste_internally = false;
628 if ((button == 2) && TEXT(bv)->selection) {
629 LocalDispatch(bv, LFUN_COPY, "");
630 paste_internally = true;
632 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
634 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
635 UpdateLocal(bv, CURSOR, false);
636 bv->owner()->setLayout(cpar(bv)->GetLayout());
638 // Insert primary selection with middle mouse
639 // if there is a local selection in the current buffer,
642 if (paste_internally)
643 LocalDispatch(bv, LFUN_PASTE, "");
645 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
652 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
654 UpdatableInset * inset = 0;
656 if (the_locking_inset) {
657 the_locking_inset->InsetButtonRelease(bv,
658 x - inset_x, y - inset_y,
661 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
662 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
663 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
664 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
666 inset_x = cx(bv) - top_x + drawTextXOffset;
667 inset_y = cy(bv) + drawTextYOffset;
668 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
669 inset->Edit(bv, x - inset_x, y - inset_y, button);
671 UpdateLocal(bv, CURSOR_PAR, false);
674 no_selection = false;
678 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
680 if (the_locking_inset) {
681 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
687 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
689 TEXT(bv)->SetSelection();
690 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
691 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
692 UpdateLocal(bv, SELECTION, false);
695 no_selection = false;
699 void InsetText::InsetKeyPress(XKeyEvent * xke)
701 if (the_locking_inset) {
702 the_locking_inset->InsetKeyPress(xke);
708 UpdatableInset::RESULT
709 InsetText::LocalDispatch(BufferView * bv,
710 int action, string const & arg)
712 no_selection = false;
713 UpdatableInset::RESULT
714 result= UpdatableInset::LocalDispatch(bv, action, arg);
715 if (result != UNDISPATCHED) {
720 if ((action < 0) && arg.empty())
723 if (the_locking_inset) {
724 result = the_locking_inset->LocalDispatch(bv, action, arg);
725 if (result == DISPATCHED_NOUPDATE)
727 else if (result == DISPATCHED) {
728 UpdateLocal(bv, CURSOR_PAR, false);
730 } else if (result == FINISHED) {
734 moveRight(bv, false);
740 the_locking_inset = 0;
748 if (bv->buffer()->isReadonly()) {
750 // setErrorMessage(N_("Document is read only"));
754 /* Automatically delete the currently selected
755 * text and replace it with what is being
756 * typed in now. Depends on lyxrc settings
757 * "auto_region_delete", which defaults to
760 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
762 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
763 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
765 bv->text->cursor.par()->previous,
766 bv->text->cursor.par()->next
769 // if an empty paragraph set the language to the surronding
770 // paragraph language on insertion of the first character!
771 if (!par->Last() && !par->next) {
774 Inset * inset = owner();
775 while(inset && inset->getLyXText(bv) == TEXT(bv))
776 inset = inset->owner();
778 text = inset->getLyXText(bv);
782 LyXFont font(LyXFont::ALL_IGNORE);
783 font.setLanguage(text->cursor.par()->getParLanguage(bv->buffer()->params));
784 SetFont(bv, font, false);
786 if (lyxrc.auto_region_delete) {
787 if (TEXT(bv)->selection){
788 TEXT(bv)->CutSelection(bv, false);
791 TEXT(bv)->ClearSelection();
792 for (string::size_type i = 0; i < arg.length(); ++i) {
793 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
796 UpdateLocal(bv, CURSOR_PAR, true);
798 // --- Cursor Movements ---------------------------------------------
800 bv->text->FinishUndo();
801 moveRight(bv, false, true);
802 TEXT(bv)->SetSelection();
803 UpdateLocal(bv, SELECTION, false);
806 result = moveRight(bv);
807 bv->text->FinishUndo();
808 TEXT(bv)->ClearSelection();
809 UpdateLocal(bv, CURSOR, false);
812 bv->text->FinishUndo();
813 moveLeft(bv, false, true);
814 TEXT(bv)->SetSelection();
815 UpdateLocal(bv, SELECTION, false);
818 bv->text->FinishUndo();
819 result= moveLeft(bv);
820 TEXT(bv)->ClearSelection();
821 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
822 UpdateLocal(bv, CURSOR, false);
825 bv->text->FinishUndo();
827 TEXT(bv)->SetSelection();
828 UpdateLocal(bv, SELECTION, false);
831 bv->text->FinishUndo();
832 result = moveDown(bv);
833 TEXT(bv)->ClearSelection();
834 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
835 UpdateLocal(bv, CURSOR, false);
838 bv->text->FinishUndo();
840 TEXT(bv)->SetSelection();
841 UpdateLocal(bv, SELECTION, false);
844 bv->text->FinishUndo();
846 TEXT(bv)->ClearSelection();
847 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
848 UpdateLocal(bv, CURSOR, false);
851 bv->text->FinishUndo();
852 TEXT(bv)->CursorHome(bv);
853 TEXT(bv)->ClearSelection();
854 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
855 UpdateLocal(bv, CURSOR, false);
858 TEXT(bv)->CursorEnd(bv);
859 TEXT(bv)->ClearSelection();
860 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
861 UpdateLocal(bv, CURSOR, false);
864 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
866 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
867 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
869 bv->text->cursor.par()->previous,
870 bv->text->cursor.par()->next
873 if (TEXT(bv)->selection)
874 TEXT(bv)->CutSelection(bv);
876 TEXT(bv)->Backspace(bv);
877 UpdateLocal(bv, CURSOR_PAR, true);
880 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
882 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
883 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
885 bv->text->cursor.par()->previous,
886 bv->text->cursor.par()->next
889 if (TEXT(bv)->selection)
890 TEXT(bv)->CutSelection(bv);
892 TEXT(bv)->Delete(bv);
893 UpdateLocal(bv, CURSOR_PAR, true);
896 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
898 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
899 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
901 bv->text->cursor.par()->previous,
902 bv->text->cursor.par()->next
905 TEXT(bv)->CutSelection(bv);
906 UpdateLocal(bv, CURSOR_PAR, true);
909 bv->text->FinishUndo();
910 TEXT(bv)->CopySelection(bv);
911 UpdateLocal(bv, CURSOR_PAR, false);
913 case LFUN_PASTESELECTION:
915 string clip(bv->workarea()->getClipboard());
919 if (arg == "paragraph") {
920 TEXT(bv)->InsertStringB(bv, clip);
922 TEXT(bv)->InsertStringA(bv, clip);
924 UpdateLocal(bv, CURSOR_PAR, true);
928 if (!autoBreakRows) {
931 if (cap.nrOfParagraphs() > 1) {
932 WriteAlert(_("Impossible operation"),
933 _("Cannot include more than one paragraph!"),
938 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
940 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
941 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
943 bv->text->cursor.par()->previous,
944 bv->text->cursor.par()->next
947 TEXT(bv)->PasteSelection(bv);
948 UpdateLocal(bv, CURSOR_PAR, true);
950 case LFUN_BREAKPARAGRAPH:
953 TEXT(bv)->BreakParagraph(bv, 0);
954 UpdateLocal(bv, FULL, true);
959 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
961 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
962 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
964 bv->text->cursor.par()->previous,
965 bv->text->cursor.par()->next
968 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
969 UpdateLocal(bv, CURSOR_PAR, true);
972 // do not set layouts on non breakable textinsets
974 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
976 // Derive layout number from given argument (string)
977 // and current buffer's textclass (number). */
978 LyXTextClassList::ClassList::size_type tclass =
979 bv->buffer()->params.textclass;
980 std::pair <bool, LyXTextClass::size_type> layout =
981 textclasslist.NumberOfLayout(tclass, arg);
983 // If the entry is obsolete, use the new one instead.
985 string obs = textclasslist.Style(tclass,layout.second).
988 layout = textclasslist.NumberOfLayout(tclass, obs);
991 // see if we found the layout number:
993 string msg = string(N_("Layout ")) + arg + N_(" not known");
995 bv->owner()->getMiniBuffer()->Set(msg);
999 if (cur_layout != layout.second) {
1000 cur_layout = layout.second;
1001 TEXT(bv)->SetLayout(bv, layout.second);
1002 bv->owner()->setLayout(cpar(bv)->GetLayout());
1003 UpdateLocal(bv, CURSOR_PAR, true);
1006 // reset the layout box
1007 bv->owner()->setLayout(cpar(bv)->GetLayout());
1010 case LFUN_PARAGRAPH_SPACING:
1011 // This one is absolutely not working. When fiddling with this
1012 // it also seems to me that the paragraphs inside the insettext
1013 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1015 LyXParagraph * par = TEXT(bv)->cursor.par();
1016 Spacing::Space cur_spacing = par->spacing.getSpace();
1017 float cur_value = 1.0;
1018 if (cur_spacing == Spacing::Other) {
1019 cur_value = par->spacing.getValue();
1022 std::istringstream istr(arg.c_str());
1025 Spacing::Space new_spacing = cur_spacing;
1026 float new_value = cur_value;
1028 lyxerr << "Missing argument to `paragraph-spacing'"
1030 } else if (tmp == "single") {
1031 new_spacing = Spacing::Single;
1032 } else if (tmp == "onehalf") {
1033 new_spacing = Spacing::Onehalf;
1034 } else if (tmp == "double") {
1035 new_spacing = Spacing::Double;
1036 } else if (tmp == "other") {
1037 new_spacing = Spacing::Other;
1040 lyxerr << "new_value = " << tmpval << endl;
1043 } else if (tmp == "default") {
1044 new_spacing = Spacing::Default;
1046 lyxerr << _("Unknown spacing argument: ")
1049 if (cur_spacing != new_spacing || cur_value != new_value) {
1050 par->spacing.set(new_spacing, new_value);
1051 //TEXT(bv)->RedoParagraph(owner->view());
1052 UpdateLocal(bv, CURSOR_PAR, true);
1053 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1059 result = UNDISPATCHED;
1062 if (result != FINISHED) {
1063 ShowInsetCursor(bv);
1065 bv->unlockInset(this);
1070 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1073 buf->latexParagraphs(os, par, 0, texrow);
1074 return texrow.rows();
1078 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1080 LyXParagraph * p = par;
1081 unsigned int lines = 0;
1085 tmp = buf->asciiParagraph(p, linelen);
1086 lines += countChar(tmp, '\n');
1094 void InsetText::Validate(LaTeXFeatures & features) const
1096 LyXParagraph * p = par;
1098 p->validate(features);
1104 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1106 if (textclasslist.Style(buf->params.textclass,
1107 p->GetLayout()).labeltype != LABEL_MANUAL)
1110 return p->BeginningOfMainBody();
1114 void InsetText::GetCursorPos(BufferView * bv,
1115 int & x, int & y) const
1122 unsigned int InsetText::InsetInInsetY()
1124 if (!the_locking_inset)
1127 return (inset_y + the_locking_inset->InsetInInsetY());
1131 void InsetText::ToggleInsetCursor(BufferView * bv)
1133 if (the_locking_inset) {
1134 the_locking_inset->ToggleInsetCursor(bv);
1138 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1140 int asc = lyxfont::maxAscent(font);
1141 int desc = lyxfont::maxDescent(font);
1144 bv->hideLockedInsetCursor();
1146 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1147 cursor_visible = !cursor_visible;
1151 void InsetText::ShowInsetCursor(BufferView * bv, bool show)
1153 if (the_locking_inset) {
1154 the_locking_inset->ShowInsetCursor(bv);
1157 if (!cursor_visible) {
1158 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1160 int asc = lyxfont::maxAscent(font);
1161 int desc = lyxfont::maxDescent(font);
1163 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1165 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1166 cursor_visible = true;
1171 void InsetText::HideInsetCursor(BufferView * bv)
1173 if (cursor_visible) {
1174 bv->hideLockedInsetCursor();
1175 cursor_visible = false;
1177 if (the_locking_inset)
1178 the_locking_inset->HideInsetCursor(bv);
1182 UpdatableInset::RESULT
1183 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1185 if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1187 if (activate_inset && checkAndActivateInset(bv, false))
1189 TEXT(bv)->CursorRight(bv, selecting);
1190 return DISPATCHED_NOUPDATE;
1194 UpdatableInset::RESULT
1195 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1197 if (!cpar(bv)->previous && (cpos(bv) <= 0))
1199 TEXT(bv)->CursorLeft(bv, selecting);
1200 if (activate_inset && checkAndActivateInset(bv, true))
1202 return DISPATCHED_NOUPDATE;
1206 UpdatableInset::RESULT
1207 InsetText::moveUp(BufferView * bv)
1209 if (!crow(bv)->previous())
1211 TEXT(bv)->CursorUp(bv);
1212 return DISPATCHED_NOUPDATE;
1216 UpdatableInset::RESULT
1217 InsetText::moveDown(BufferView * bv)
1219 if (!crow(bv)->next())
1221 TEXT(bv)->CursorDown(bv);
1222 return DISPATCHED_NOUPDATE;
1226 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1228 if (the_locking_inset) {
1229 if (the_locking_inset->InsertInsetAllowed(inset))
1230 return the_locking_inset->InsertInset(bv, inset);
1233 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1235 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1236 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1238 bv->text->cursor.par()->previous,
1239 bv->text->cursor.par()->next
1242 if (inset->Editable() == Inset::IS_EDITABLE) {
1243 UpdatableInset * i = static_cast<UpdatableInset *>(inset);
1244 i->setOwner(static_cast<UpdatableInset *>(this));
1246 HideInsetCursor(bv);
1247 TEXT(bv)->InsertInset(bv, inset);
1248 TEXT(bv)->selection = 0;
1249 bv->fitCursor(TEXT(bv));
1250 UpdateLocal(bv, CURSOR_PAR, true);
1251 static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1252 ShowInsetCursor(bv);
1257 UpdatableInset * InsetText::GetLockingInset()
1259 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1263 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1267 if (the_locking_inset)
1268 return the_locking_inset->GetFirstLockingInsetOfType(c);
1273 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1275 bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1277 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1278 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1280 bv->text->cursor.par()->previous,
1281 bv->text->cursor.par()->next
1284 TEXT(bv)->SetFont(bv, font, toggleall);
1285 bv->fitCursor(TEXT(bv));
1286 UpdateLocal(bv, CURSOR_PAR, true);
1290 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1292 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1296 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1297 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1299 LyXFont const font =
1300 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1302 x = inset->width(bv, font);
1303 y = inset->descent(bv, font);
1307 inset_x = cx(bv) - top_x + drawTextXOffset;
1308 inset_y = cy(bv) + drawTextYOffset;
1309 inset->Edit(bv, x - inset_x, y - inset_y, 0);
1310 if (!the_locking_inset)
1312 UpdateLocal(bv, CURSOR_PAR, false);
1319 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1322 x = x - drawTextXOffset;
1323 y = y + insetAscent;
1324 Inset * inset = bv->checkInsetHit(TEXT(bv), x, y, button);
1331 inset_x = cx(bv) - top_x + drawTextXOffset;
1332 inset_y = cy(bv) + drawTextYOffset;
1333 inset->Edit(bv, x - inset_x, y - inset_y, button);
1334 if (!the_locking_inset)
1336 UpdateLocal(bv, CURSOR_PAR, false);
1343 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1345 int w = UpdatableInset::getMaxWidth(pain, inset);
1350 w = w - top_x + owner()->x();
1353 w -= (2 * TEXT_TO_INSET_OFFSET);
1355 // return w - (2*TEXT_TO_INSET_OFFSET);
1359 void InsetText::SetParagraphData(LyXParagraph *p)
1373 par->SetInsetOwner(this);
1377 np->next = p->Clone();
1378 np->next->previous = np;
1380 np->SetInsetOwner(this);
1386 void InsetText::SetAutoBreakRows(bool flag)
1388 if (flag != autoBreakRows) {
1389 autoBreakRows = flag;
1397 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1399 if (how != drawFrame) {
1402 UpdateLocal(bv, DRAW_FRAME, false);
1407 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1409 if (frame_color != col) {
1412 UpdateLocal(bv, DRAW_FRAME, false);
1417 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1419 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1424 int InsetText::cx(BufferView * bv) const
1426 return TEXT(bv)->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1430 int InsetText::cy(BufferView * bv) const
1433 return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1437 LyXParagraph::size_type InsetText::cpos(BufferView * bv) const
1439 return TEXT(bv)->cursor.pos();
1443 LyXParagraph * InsetText::cpar(BufferView * bv) const
1445 return TEXT(bv)->cursor.par();
1449 Row * InsetText::crow(BufferView * bv) const
1451 return TEXT(bv)->cursor.row();
1455 LyXText * InsetText::getLyXText(BufferView * bv) const
1457 if (cache.find(bv) != cache.end())
1459 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1462 if (the_locking_inset) {
1463 lt->SetCursor(bv, inset_par, inset_pos);
1469 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1471 if (cache.find(bv) == cache.end())
1476 /// then remove all LyXText in text-insets
1477 LyXParagraph * p = par;
1478 for(;p;p = p->next) {
1479 p->deleteInsetsLyXText(bv);
1485 void InsetText::resizeLyXText(BufferView * bv) const
1487 if (!par->next && !par->size()) // resize not neccessary!
1489 if (cache.find(bv) == cache.end())
1492 LyXParagraph * lpar = 0;
1493 LyXParagraph * selstartpar = 0;
1494 LyXParagraph * selendpar = 0;
1495 LyXParagraph::size_type pos = 0;
1496 LyXParagraph::size_type selstartpos = 0;
1497 LyXParagraph::size_type selendpos = 0;
1501 // ProhibitInput(bv);
1504 lpar = TEXT(bv)->cursor.par();
1505 pos = TEXT(bv)->cursor.pos();
1506 selstartpar = TEXT(bv)->sel_start_cursor.par();
1507 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1508 selendpar = TEXT(bv)->sel_end_cursor.par();
1509 selendpos = TEXT(bv)->sel_end_cursor.pos();
1510 selection = TEXT(bv)->selection;
1511 mark_set = TEXT(bv)->mark_set;
1513 deleteLyXText(bv, (the_locking_inset == 0));
1516 TEXT(bv)->selection = true;
1517 /* at this point just to avoid the Delete-Empty-Paragraph
1518 * Mechanism when setting the cursor */
1519 TEXT(bv)->mark_set = mark_set;
1521 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos);
1522 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1523 TEXT(bv)->SetCursor(bv, selendpar, selendpos);
1524 TEXT(bv)->SetSelection();
1525 TEXT(bv)->SetCursor(bv, lpar, pos);
1527 TEXT(bv)->SetCursor(bv, lpar, pos);
1528 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1529 TEXT(bv)->selection = false;
1533 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1534 // this will scroll the screen such that the cursor becomes visible
1535 bv->updateScrollbar();
1537 if (the_locking_inset) {
1538 /// then resize all LyXText in text-insets
1539 inset_x = cx(bv) - top_x + drawTextXOffset;
1540 inset_y = cy(bv) + drawTextYOffset;
1541 for(LyXParagraph * p = par; p; p = p->next) {
1542 p->resizeInsetsLyXText(bv);
1549 void InsetText::removeNewlines()
1551 for(LyXParagraph * p = par; p; p = p->next) {
1552 for(int i = 0; i < p->Last(); ++i) {
1553 if (p->GetChar(i) == LyXParagraph::META_NEWLINE)