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 int InsetText::DocBook(Buffer const * buf, ostream & os) const
1096 LyXParagraph * p = par;
1097 unsigned int lines = 0;
1102 buf->SimpleDocBookOnePar(os,tmp,p,desc,0);
1110 void InsetText::Validate(LaTeXFeatures & features) const
1112 LyXParagraph * p = par;
1114 p->validate(features);
1120 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1122 if (textclasslist.Style(buf->params.textclass,
1123 p->GetLayout()).labeltype != LABEL_MANUAL)
1126 return p->BeginningOfMainBody();
1130 void InsetText::GetCursorPos(BufferView * bv,
1131 int & x, int & y) const
1138 unsigned int InsetText::InsetInInsetY()
1140 if (!the_locking_inset)
1143 return (inset_y + the_locking_inset->InsetInInsetY());
1147 void InsetText::ToggleInsetCursor(BufferView * bv)
1149 if (the_locking_inset) {
1150 the_locking_inset->ToggleInsetCursor(bv);
1154 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1156 int asc = lyxfont::maxAscent(font);
1157 int desc = lyxfont::maxDescent(font);
1160 bv->hideLockedInsetCursor();
1162 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1163 cursor_visible = !cursor_visible;
1167 void InsetText::ShowInsetCursor(BufferView * bv, bool show)
1169 if (the_locking_inset) {
1170 the_locking_inset->ShowInsetCursor(bv);
1173 if (!cursor_visible) {
1174 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1176 int asc = lyxfont::maxAscent(font);
1177 int desc = lyxfont::maxDescent(font);
1179 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1181 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1182 cursor_visible = true;
1187 void InsetText::HideInsetCursor(BufferView * bv)
1189 if (cursor_visible) {
1190 bv->hideLockedInsetCursor();
1191 cursor_visible = false;
1193 if (the_locking_inset)
1194 the_locking_inset->HideInsetCursor(bv);
1198 UpdatableInset::RESULT
1199 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1201 if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1203 if (activate_inset && checkAndActivateInset(bv, false))
1205 TEXT(bv)->CursorRight(bv, selecting);
1206 return DISPATCHED_NOUPDATE;
1210 UpdatableInset::RESULT
1211 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1213 if (!cpar(bv)->previous && (cpos(bv) <= 0))
1215 TEXT(bv)->CursorLeft(bv, selecting);
1216 if (activate_inset && checkAndActivateInset(bv, true))
1218 return DISPATCHED_NOUPDATE;
1222 UpdatableInset::RESULT
1223 InsetText::moveUp(BufferView * bv)
1225 if (!crow(bv)->previous())
1227 TEXT(bv)->CursorUp(bv);
1228 return DISPATCHED_NOUPDATE;
1232 UpdatableInset::RESULT
1233 InsetText::moveDown(BufferView * bv)
1235 if (!crow(bv)->next())
1237 TEXT(bv)->CursorDown(bv);
1238 return DISPATCHED_NOUPDATE;
1242 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1244 if (the_locking_inset) {
1245 if (the_locking_inset->InsertInsetAllowed(inset))
1246 return the_locking_inset->InsertInset(bv, inset);
1249 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1251 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1252 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1254 bv->text->cursor.par()->previous,
1255 bv->text->cursor.par()->next
1258 if (inset->Editable() == Inset::IS_EDITABLE) {
1259 UpdatableInset * i = static_cast<UpdatableInset *>(inset);
1260 i->setOwner(static_cast<UpdatableInset *>(this));
1262 HideInsetCursor(bv);
1263 TEXT(bv)->InsertInset(bv, inset);
1264 TEXT(bv)->selection = 0;
1265 bv->fitCursor(TEXT(bv));
1266 UpdateLocal(bv, CURSOR_PAR, true);
1267 static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1268 ShowInsetCursor(bv);
1273 UpdatableInset * InsetText::GetLockingInset()
1275 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1279 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1283 if (the_locking_inset)
1284 return the_locking_inset->GetFirstLockingInsetOfType(c);
1289 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1291 bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1293 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1294 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1296 bv->text->cursor.par()->previous,
1297 bv->text->cursor.par()->next
1300 TEXT(bv)->SetFont(bv, font, toggleall);
1301 bv->fitCursor(TEXT(bv));
1302 UpdateLocal(bv, CURSOR_PAR, true);
1306 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1308 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1312 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1313 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1315 LyXFont const font =
1316 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1318 x = inset->width(bv, font);
1319 y = inset->descent(bv, font);
1323 inset_x = cx(bv) - top_x + drawTextXOffset;
1324 inset_y = cy(bv) + drawTextYOffset;
1325 inset->Edit(bv, x - inset_x, y - inset_y, 0);
1326 if (!the_locking_inset)
1328 UpdateLocal(bv, CURSOR_PAR, false);
1335 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1338 x = x - drawTextXOffset;
1339 y = y + insetAscent;
1340 Inset * inset = bv->checkInsetHit(TEXT(bv), x, y, button);
1347 inset_x = cx(bv) - top_x + drawTextXOffset;
1348 inset_y = cy(bv) + drawTextYOffset;
1349 inset->Edit(bv, x - inset_x, y - inset_y, button);
1350 if (!the_locking_inset)
1352 UpdateLocal(bv, CURSOR_PAR, false);
1359 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1361 int w = UpdatableInset::getMaxWidth(pain, inset);
1366 w = w - top_x + owner()->x();
1369 w -= (2 * TEXT_TO_INSET_OFFSET);
1371 // return w - (2*TEXT_TO_INSET_OFFSET);
1375 void InsetText::SetParagraphData(LyXParagraph *p)
1389 par->SetInsetOwner(this);
1393 np->next = p->Clone();
1394 np->next->previous = np;
1396 np->SetInsetOwner(this);
1402 void InsetText::SetAutoBreakRows(bool flag)
1404 if (flag != autoBreakRows) {
1405 autoBreakRows = flag;
1413 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1415 if (how != drawFrame) {
1418 UpdateLocal(bv, DRAW_FRAME, false);
1423 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1425 if (frame_color != col) {
1428 UpdateLocal(bv, DRAW_FRAME, false);
1433 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1435 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1440 int InsetText::cx(BufferView * bv) const
1442 return TEXT(bv)->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1446 int InsetText::cy(BufferView * bv) const
1449 return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1453 LyXParagraph::size_type InsetText::cpos(BufferView * bv) const
1455 return TEXT(bv)->cursor.pos();
1459 LyXParagraph * InsetText::cpar(BufferView * bv) const
1461 return TEXT(bv)->cursor.par();
1465 Row * InsetText::crow(BufferView * bv) const
1467 return TEXT(bv)->cursor.row();
1471 LyXText * InsetText::getLyXText(BufferView * bv) const
1473 if (cache.find(bv) != cache.end())
1475 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1478 if (the_locking_inset) {
1479 lt->SetCursor(bv, inset_par, inset_pos);
1485 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1487 if (cache.find(bv) == cache.end())
1492 /// then remove all LyXText in text-insets
1493 LyXParagraph * p = par;
1494 for(;p;p = p->next) {
1495 p->deleteInsetsLyXText(bv);
1501 void InsetText::resizeLyXText(BufferView * bv) const
1503 if (!par->next && !par->size()) // resize not neccessary!
1505 if (cache.find(bv) == cache.end())
1508 LyXParagraph * lpar = 0;
1509 LyXParagraph * selstartpar = 0;
1510 LyXParagraph * selendpar = 0;
1511 LyXParagraph::size_type pos = 0;
1512 LyXParagraph::size_type selstartpos = 0;
1513 LyXParagraph::size_type selendpos = 0;
1517 // ProhibitInput(bv);
1520 lpar = TEXT(bv)->cursor.par();
1521 pos = TEXT(bv)->cursor.pos();
1522 selstartpar = TEXT(bv)->sel_start_cursor.par();
1523 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1524 selendpar = TEXT(bv)->sel_end_cursor.par();
1525 selendpos = TEXT(bv)->sel_end_cursor.pos();
1526 selection = TEXT(bv)->selection;
1527 mark_set = TEXT(bv)->mark_set;
1529 deleteLyXText(bv, (the_locking_inset == 0));
1532 TEXT(bv)->selection = true;
1533 /* at this point just to avoid the Delete-Empty-Paragraph
1534 * Mechanism when setting the cursor */
1535 TEXT(bv)->mark_set = mark_set;
1537 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos);
1538 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1539 TEXT(bv)->SetCursor(bv, selendpar, selendpos);
1540 TEXT(bv)->SetSelection();
1541 TEXT(bv)->SetCursor(bv, lpar, pos);
1543 TEXT(bv)->SetCursor(bv, lpar, pos);
1544 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1545 TEXT(bv)->selection = false;
1549 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1550 // this will scroll the screen such that the cursor becomes visible
1551 bv->updateScrollbar();
1553 if (the_locking_inset) {
1554 /// then resize all LyXText in text-insets
1555 inset_x = cx(bv) - top_x + drawTextXOffset;
1556 inset_y = cy(bv) + drawTextYOffset;
1557 for(LyXParagraph * p = par; p; p = p->next) {
1558 p->resizeInsetsLyXText(bv);
1565 void InsetText::removeNewlines()
1567 for(LyXParagraph * p = par; p; p = p->next) {
1568 for(int i = 0; i < p->Last(); ++i) {
1569 if (p->GetChar(i) == LyXParagraph::META_NEWLINE)