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 *);
58 extern bool math_insert_greek(BufferView *, char);
59 extern int greek_kb_flag;
61 InsetText::InsetText()
63 par = new LyXParagraph();
68 InsetText::InsetText(InsetText const & ins)
73 autoBreakRows = ins.autoBreakRows;
77 InsetText & InsetText::operator=(InsetText const & it)
80 autoBreakRows = it.autoBreakRows;
85 void InsetText::init(InsetText const * ins)
93 the_locking_inset = 0;
94 cursor_visible = false;
100 autoBreakRows = false;
104 SetParagraphData(ins->par);
105 autoBreakRows = ins->autoBreakRows;
106 drawFrame = ins->drawFrame;
108 par->SetInsetOwner(this);
109 frame_color = LColor::insetframe;
115 InsetText::~InsetText()
117 // delete all instances of LyXText before deleting the paragraps used
119 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
120 delete (*cit).second;
123 LyXParagraph * p = par->next;
133 void InsetText::clear()
135 // delete all instances of LyXText before deleting the paragraps used
137 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
138 delete (*cit).second;
141 LyXParagraph * p = par->next;
148 par = new LyXParagraph();
152 Inset * InsetText::Clone(Buffer const &) const
154 InsetText * t = new InsetText(*this);
159 void InsetText::Write(Buffer const * buf, ostream & os) const
162 WriteParagraphData(buf, os);
166 void InsetText::WriteParagraphData(Buffer const * buf, ostream & os) const
168 par->writeFile(buf, os, buf->params, 0, 0);
172 void InsetText::Read(Buffer const * buf, LyXLex & lex)
176 LyXParagraph * return_par = 0;
177 char depth = 0; // signed or unsigned?
179 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
180 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
182 LyXFont font(LyXFont::ALL_INHERIT);
184 // delete all instances of LyXText before deleting the paragraps used
186 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
187 delete (*cit).second;
191 LyXParagraph * p = par->next;
198 par = new LyXParagraph;
201 token = lex.GetString();
204 if (token == "\\end_inset")
206 if (const_cast<Buffer*>(buf)->
207 parseSingleLyXformat2Token(lex, par, return_par,token, pos, depth,
210 , footnoteflag, footnotekind
214 // the_end read this should NEVER happen
215 lex.printError("\\the_end read in inset! Error in document!");
223 return_par->SetInsetOwner(this);
224 return_par = return_par->next;
227 if (token != "\\end_inset") {
228 lex.printError("Missing \\end_inset at this point. "
235 int InsetText::ascent(BufferView * bv, LyXFont const &) const
238 Row * row = TEXT(bv)->GetRowNearY(y_temp);
239 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
244 int InsetText::descent(BufferView * bv, LyXFont const &) const
247 Row * row = TEXT(bv)->GetRowNearY(y_temp);
248 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
249 TEXT_TO_INSET_OFFSET;
254 int InsetText::width(BufferView * bv, LyXFont const &) const
256 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
261 int InsetText::textWidth(Painter & pain) const
263 int w = getMaxWidth(pain, this);
268 void InsetText::draw(BufferView * bv, LyXFont const & f,
269 int baseline, float & x, bool cleared) const
271 Painter & pain = bv->painter();
273 // no draw is necessary !!!
274 if ((drawFrame == LOCKED) && !locked && !par->size()) {
275 if (!cleared && (need_update == CLEAR_FRAME)) {
276 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
278 insetAscent + insetDescent - 1,
282 top_baseline = baseline;
289 UpdatableInset::draw(bv, f, baseline, x, cleared);
291 if (!cleared && ((need_update==FULL) || (top_x!=int(x)) ||
292 (top_baseline!=baseline))) {
294 int h = insetAscent + insetDescent;
295 int ty = baseline - insetAscent;
301 if ((ty + h) > pain.paperHeight())
302 h = pain.paperHeight();
303 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
304 w = pain.paperWidth();
305 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
309 if (!cleared && (need_update == NONE))
312 if (top_x != int(x)) {
315 bv->text->status = LyXText::CHANGED_IN_DRAW;
319 top_baseline = baseline;
320 top_y = baseline - ascent(bv, f);
321 last_width = width(bv, f);
322 last_height = ascent(bv, f) + descent(bv, f);
324 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
325 inset_x = cx(bv) - top_x + drawTextXOffset;
326 inset_y = cy(bv) + drawTextYOffset;
328 if (!cleared && (need_update == CURSOR) && !TEXT(bv)->selection) {
333 x += TEXT_TO_INSET_OFFSET;
336 Row * row = TEXT(bv)->GetRowNearY(y);
337 int y_offset = baseline - row->ascent_of_text();
338 int ph = pain.paperHeight();
341 while ((row != 0) && ((y+row->height()) <= 0)) {
343 first += row->height();
348 TEXT(bv)->first = first;
349 if (cleared || !locked || (need_update == FULL)) {
352 while ((row != 0) && (yf < ph)) {
353 TEXT(bv)->GetVisibleRow(bv, y+y_offset, int(x), row,
359 } else if (need_update == SELECTION) {
360 bv->screen()->ToggleToggle(TEXT(bv), bv, y_offset, int(x));
363 if (need_update == CURSOR) {
364 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
365 TEXT(bv)->ClearSelection();
366 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
368 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
372 TEXT(bv)->refresh_y = 0;
373 TEXT(bv)->status = LyXText::UNCHANGED;
374 if ((need_update != CURSOR_PAR) &&
375 ((drawFrame == ALWAYS) || ((drawFrame == LOCKED) && locked)))
377 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
378 width(bv, f) - 1, insetAscent + insetDescent - 1,
380 } else if (need_update == CLEAR_FRAME) {
381 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
382 width(bv, f) - 1, insetAscent + insetDescent - 1,
385 x += width(bv, f) - TEXT_TO_INSET_OFFSET;
386 if (bv->text->status==LyXText::CHANGED_IN_DRAW)
388 else if (need_update != INIT)
393 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
395 if (reinit) { // && (need_update != CURSOR)) {
399 owner()->update(bv, font, true);
402 if (the_locking_inset) {
403 inset_x = cx(bv) - top_x + drawTextXOffset;
404 inset_y = cy(bv) + drawTextYOffset;
405 the_locking_inset->update(bv, font, reinit);
407 if (need_update == INIT) {
411 int oldw = insetWidth;
413 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
414 // max(textWidth(bv->painter()),
415 // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
416 // (2 * TEXT_TO_INSET_OFFSET);
418 insetWidth = textWidth(bv->painter());
420 insetWidth = static_cast<int>(TEXT(bv)->width);
422 if (oldw != insetWidth) {
423 // printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
424 // textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
427 update(bv, font, reinit);
430 if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
433 TEXT(bv)->UpdateInset(bv, the_locking_inset);
436 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
440 Row * row = TEXT(bv)->GetRowNearY(y_temp);
441 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
442 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
443 TEXT_TO_INSET_OFFSET;
447 void InsetText::UpdateLocal(BufferView * bv, UpdateCodes what, bool mark_dirty)
449 TEXT(bv)->FullRebreak(bv);
450 if (need_update != INIT) {
451 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
453 else if (!the_locking_inset || (what != CURSOR))
456 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
458 bv->updateInset(this, mark_dirty);
459 bv->owner()->showState();
460 if (old_par != cpar(bv)) {
461 bv->owner()->setLayout(cpar(bv)->GetLayout());
467 string const InsetText::EditMessage() const
469 return _("Opened Text Inset");
473 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
475 // par->SetInsetOwner(this);
476 UpdatableInset::Edit(bv, x, y, button);
478 if (!bv->lockInset(this)) {
479 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
483 the_locking_inset = 0;
484 inset_pos = inset_x = inset_y = 0;
485 inset_boundary = false;
488 if (!checkAndActivateInset(bv, x, y, button))
489 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
491 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
492 bv->text->FinishUndo();
494 UpdateLocal(bv, FULL, false);
498 void InsetText::InsetUnlock(BufferView * bv)
500 if (the_locking_inset) {
501 the_locking_inset->InsetUnlock(bv);
502 the_locking_inset = 0;
505 no_selection = false;
507 TEXT(bv)->selection = 0;
508 UpdateLocal(bv, CLEAR_FRAME, false);
510 bv->owner()->setLayout(owner()->getLyXText(bv)
511 ->cursor.par()->GetLayout());
513 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
517 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
519 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
522 if (inset == cpar(bv)->GetInset(cpos(bv))) {
523 lyxerr[Debug::INSETS] << "OK" << endl;
524 the_locking_inset = inset;
525 inset_x = cx(bv) - top_x + drawTextXOffset;
526 inset_y = cy(bv) + drawTextYOffset;
527 inset_pos = cpos(bv);
528 inset_par = cpar(bv);
529 inset_boundary = cboundary(bv);
530 TEXT(bv)->UpdateInset(bv, the_locking_inset);
532 } else if (the_locking_inset && (the_locking_inset == inset)) {
533 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
534 lyxerr[Debug::INSETS] << "OK" << endl;
535 inset_x = cx(bv) - top_x + drawTextXOffset;
536 inset_y = cy(bv) + drawTextYOffset;
538 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
540 } else if (the_locking_inset) {
541 lyxerr[Debug::INSETS] << "MAYBE" << endl;
542 return the_locking_inset->LockInsetInInset(bv, inset);
544 lyxerr[Debug::INSETS] << "NOT OK" << endl;
549 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
552 if (!the_locking_inset)
554 if (the_locking_inset == inset) {
555 the_locking_inset->InsetUnlock(bv);
556 TEXT(bv)->UpdateInset(bv, inset);
557 the_locking_inset = 0;
559 moveRight(bv, false);
560 old_par = 0; // force layout setting
561 UpdateLocal(bv, CURSOR_PAR, false);
564 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
568 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
570 if (!the_locking_inset)
572 if (the_locking_inset != inset) {
573 TEXT(bv)->UpdateInset(bv, the_locking_inset);
574 need_update = CURSOR_PAR;
575 return the_locking_inset->UpdateInsetInInset(bv, inset);
577 // UpdateLocal(bv, FULL, false);
578 if (TEXT(bv)->UpdateInset(bv, inset))
579 UpdateLocal(bv, CURSOR_PAR, false);
580 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
581 inset_x = cx(bv) - top_x + drawTextXOffset;
582 inset_y = cy(bv) + drawTextYOffset;
588 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
590 no_selection = false;
592 int tmp_x = x - drawTextXOffset;
593 int tmp_y = y + insetAscent - TEXT(bv)->first;
594 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
597 if (the_locking_inset) {
598 if (the_locking_inset == inset) {
599 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
602 // otherwise unlock the_locking_inset and lock the new inset
603 the_locking_inset->InsetUnlock(bv);
604 inset_x = cx(bv) - top_x + drawTextXOffset;
605 inset_y = cy(bv) + drawTextYOffset;
606 the_locking_inset = static_cast<UpdatableInset*>(inset);
607 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
608 inset->Edit(bv, x - inset_x, y - inset_y, button);
609 if (the_locking_inset) {
610 UpdateLocal(bv, CURSOR_PAR, false);
614 // otherwise only unlock the_locking_inset
615 the_locking_inset->InsetUnlock(bv);
616 the_locking_inset = 0;
618 if (bv->theLockingInset()) {
619 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
620 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
621 inset_x = cx(bv) - top_x + drawTextXOffset;
622 inset_y = cy(bv) + drawTextYOffset;
623 inset_pos = cpos(bv);
624 inset_par = cpar(bv);
625 inset_boundary = cboundary(bv);
626 the_locking_inset = uinset;
627 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
628 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
629 // TEXT(bv)->ClearSelection();
630 if (the_locking_inset) {
631 UpdateLocal(bv, CURSOR_PAR, false);
637 bool paste_internally = false;
638 if ((button == 2) && TEXT(bv)->selection) {
639 LocalDispatch(bv, LFUN_COPY, "");
640 paste_internally = true;
642 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
644 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
645 UpdateLocal(bv, CURSOR, false);
646 bv->owner()->setLayout(cpar(bv)->GetLayout());
648 // Insert primary selection with middle mouse
649 // if there is a local selection in the current buffer,
652 if (paste_internally)
653 LocalDispatch(bv, LFUN_PASTE, "");
655 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
662 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
664 UpdatableInset * inset = 0;
666 if (the_locking_inset) {
667 the_locking_inset->InsetButtonRelease(bv,
668 x - inset_x, y - inset_y,
671 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
672 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
673 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
674 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
676 inset_x = cx(bv) - top_x + drawTextXOffset;
677 inset_y = cy(bv) + drawTextYOffset;
678 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
679 inset->Edit(bv, x - inset_x, y - inset_y, button);
681 UpdateLocal(bv, CURSOR_PAR, false);
684 no_selection = false;
688 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
690 if (the_locking_inset) {
691 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
697 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
699 TEXT(bv)->SetSelection();
700 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
701 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
702 UpdateLocal(bv, SELECTION, false);
705 no_selection = false;
709 void InsetText::InsetKeyPress(XKeyEvent * xke)
711 if (the_locking_inset) {
712 the_locking_inset->InsetKeyPress(xke);
718 UpdatableInset::RESULT
719 InsetText::LocalDispatch(BufferView * bv,
720 int action, string const & arg)
722 no_selection = false;
723 UpdatableInset::RESULT
724 result= UpdatableInset::LocalDispatch(bv, action, arg);
725 if (result != UNDISPATCHED) {
730 if ((action < 0) && arg.empty())
733 if (the_locking_inset) {
734 result = the_locking_inset->LocalDispatch(bv, action, arg);
735 if (result == DISPATCHED_NOUPDATE)
737 else if (result == DISPATCHED) {
738 UpdateLocal(bv, CURSOR_PAR, false);
740 } else if (result == FINISHED) {
744 moveRight(bv, false);
750 the_locking_inset = 0;
757 case LFUN_UNKNOWN_ACTION:
758 if (bv->buffer()->isReadonly()) {
760 // setErrorMessage(N_("Document is read only"));
764 /* Automatically delete the currently selected
765 * text and replace it with what is being
766 * typed in now. Depends on lyxrc settings
767 * "auto_region_delete", which defaults to
770 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
772 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
773 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
775 bv->text->cursor.par()->previous,
776 bv->text->cursor.par()->next
779 // if an empty paragraph set the language to the surronding
780 // paragraph language on insertion of the first character!
781 if (!par->Last() && !par->next) {
784 Inset * inset = owner();
785 while(inset && inset->getLyXText(bv) == TEXT(bv))
786 inset = inset->owner();
788 text = inset->getLyXText(bv);
792 LyXFont font(LyXFont::ALL_IGNORE);
793 font.setLanguage(text->cursor.par()->getParLanguage(bv->buffer()->params));
794 SetFont(bv, font, false);
797 if (lyxrc.auto_region_delete) {
798 if (TEXT(bv)->selection){
799 TEXT(bv)->CutSelection(bv, false);
802 TEXT(bv)->ClearSelection();
803 for (string::size_type i = 0; i < arg.length(); ++i) {
805 if (!math_insert_greek(bv, arg[i])) {
807 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
809 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
811 } else if (!the_locking_inset) {
812 (void)moveRight(bv, false);
816 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
818 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
823 UpdateLocal(bv, CURSOR_PAR, true);
824 result=DISPATCHED_NOUPDATE;
826 // --- Cursor Movements ---------------------------------------------
828 bv->text->FinishUndo();
829 moveRight(bv, false, true);
830 TEXT(bv)->SetSelection();
831 UpdateLocal(bv, SELECTION, false);
834 result = moveRight(bv);
835 bv->text->FinishUndo();
836 TEXT(bv)->ClearSelection();
837 UpdateLocal(bv, CURSOR, false);
840 bv->text->FinishUndo();
841 moveLeft(bv, false, true);
842 TEXT(bv)->SetSelection();
843 UpdateLocal(bv, SELECTION, false);
846 bv->text->FinishUndo();
847 result= moveLeft(bv);
848 TEXT(bv)->ClearSelection();
849 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
850 UpdateLocal(bv, CURSOR, false);
853 bv->text->FinishUndo();
855 TEXT(bv)->SetSelection();
856 UpdateLocal(bv, SELECTION, false);
859 bv->text->FinishUndo();
860 result = moveDown(bv);
861 TEXT(bv)->ClearSelection();
862 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
863 UpdateLocal(bv, CURSOR, false);
866 bv->text->FinishUndo();
868 TEXT(bv)->SetSelection();
869 UpdateLocal(bv, SELECTION, false);
872 bv->text->FinishUndo();
874 TEXT(bv)->ClearSelection();
875 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
876 UpdateLocal(bv, CURSOR, false);
879 bv->text->FinishUndo();
880 TEXT(bv)->CursorHome(bv);
881 TEXT(bv)->ClearSelection();
882 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
883 UpdateLocal(bv, CURSOR, false);
886 TEXT(bv)->CursorEnd(bv);
887 TEXT(bv)->ClearSelection();
888 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
889 UpdateLocal(bv, CURSOR, false);
892 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
894 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
895 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
897 bv->text->cursor.par()->previous,
898 bv->text->cursor.par()->next
901 if (TEXT(bv)->selection)
902 TEXT(bv)->CutSelection(bv);
904 TEXT(bv)->Backspace(bv);
905 UpdateLocal(bv, CURSOR_PAR, true);
908 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
910 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
911 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
913 bv->text->cursor.par()->previous,
914 bv->text->cursor.par()->next
917 if (TEXT(bv)->selection)
918 TEXT(bv)->CutSelection(bv);
920 TEXT(bv)->Delete(bv);
921 UpdateLocal(bv, CURSOR_PAR, true);
924 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
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)->CutSelection(bv);
934 UpdateLocal(bv, CURSOR_PAR, true);
937 bv->text->FinishUndo();
938 TEXT(bv)->CopySelection(bv);
939 UpdateLocal(bv, CURSOR_PAR, false);
941 case LFUN_PASTESELECTION:
943 string clip(bv->workarea()->getClipboard());
947 if (arg == "paragraph") {
948 TEXT(bv)->InsertStringB(bv, clip);
950 TEXT(bv)->InsertStringA(bv, clip);
952 UpdateLocal(bv, CURSOR_PAR, true);
956 if (!autoBreakRows) {
959 if (cap.nrOfParagraphs() > 1) {
960 WriteAlert(_("Impossible operation"),
961 _("Cannot include more than one paragraph!"),
966 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
968 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
969 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
971 bv->text->cursor.par()->previous,
972 bv->text->cursor.par()->next
975 TEXT(bv)->PasteSelection(bv);
976 UpdateLocal(bv, CURSOR_PAR, true);
978 case LFUN_BREAKPARAGRAPH:
981 TEXT(bv)->BreakParagraph(bv, 0);
982 UpdateLocal(bv, FULL, true);
987 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
989 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
990 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
992 bv->text->cursor.par()->previous,
993 bv->text->cursor.par()->next
996 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
997 UpdateLocal(bv, CURSOR_PAR, true);
1000 // do not set layouts on non breakable textinsets
1001 if (autoBreakRows) {
1002 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1004 // Derive layout number from given argument (string)
1005 // and current buffer's textclass (number). */
1006 LyXTextClassList::ClassList::size_type tclass =
1007 bv->buffer()->params.textclass;
1008 std::pair <bool, LyXTextClass::size_type> layout =
1009 textclasslist.NumberOfLayout(tclass, arg);
1011 // If the entry is obsolete, use the new one instead.
1013 string obs = textclasslist.Style(tclass,layout.second).
1016 layout = textclasslist.NumberOfLayout(tclass, obs);
1019 // see if we found the layout number:
1020 if (!layout.first) {
1021 string msg = string(N_("Layout ")) + arg + N_(" not known");
1023 bv->owner()->getMiniBuffer()->Set(msg);
1027 if (cur_layout != layout.second) {
1028 cur_layout = layout.second;
1029 TEXT(bv)->SetLayout(bv, layout.second);
1030 bv->owner()->setLayout(cpar(bv)->GetLayout());
1031 UpdateLocal(bv, CURSOR_PAR, true);
1034 // reset the layout box
1035 bv->owner()->setLayout(cpar(bv)->GetLayout());
1038 case LFUN_PARAGRAPH_SPACING:
1039 // This one is absolutely not working. When fiddling with this
1040 // it also seems to me that the paragraphs inside the insettext
1041 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1043 LyXParagraph * par = TEXT(bv)->cursor.par();
1044 Spacing::Space cur_spacing = par->spacing.getSpace();
1045 float cur_value = 1.0;
1046 if (cur_spacing == Spacing::Other) {
1047 cur_value = par->spacing.getValue();
1050 std::istringstream istr(arg.c_str());
1053 Spacing::Space new_spacing = cur_spacing;
1054 float new_value = cur_value;
1056 lyxerr << "Missing argument to `paragraph-spacing'"
1058 } else if (tmp == "single") {
1059 new_spacing = Spacing::Single;
1060 } else if (tmp == "onehalf") {
1061 new_spacing = Spacing::Onehalf;
1062 } else if (tmp == "double") {
1063 new_spacing = Spacing::Double;
1064 } else if (tmp == "other") {
1065 new_spacing = Spacing::Other;
1068 lyxerr << "new_value = " << tmpval << endl;
1071 } else if (tmp == "default") {
1072 new_spacing = Spacing::Default;
1074 lyxerr << _("Unknown spacing argument: ")
1077 if (cur_spacing != new_spacing || cur_value != new_value) {
1078 par->spacing.set(new_spacing, new_value);
1079 //TEXT(bv)->RedoParagraph(owner->view());
1080 UpdateLocal(bv, CURSOR_PAR, true);
1081 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1087 result = UNDISPATCHED;
1090 if (result != FINISHED) {
1091 ShowInsetCursor(bv);
1093 bv->unlockInset(this);
1098 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1101 buf->latexParagraphs(os, par, 0, texrow);
1102 return texrow.rows();
1106 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1108 LyXParagraph * p = par;
1109 unsigned int lines = 0;
1113 tmp = buf->asciiParagraph(p, linelen);
1114 lines += countChar(tmp, '\n');
1122 int InsetText::DocBook(Buffer const * buf, ostream & os) const
1124 LyXParagraph * p = par;
1125 unsigned int lines = 0;
1130 buf->SimpleDocBookOnePar(os,tmp,p,desc,0);
1138 void InsetText::Validate(LaTeXFeatures & features) const
1140 LyXParagraph * p = par;
1142 p->validate(features);
1148 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1150 if (textclasslist.Style(buf->params.textclass,
1151 p->GetLayout()).labeltype != LABEL_MANUAL)
1154 return p->BeginningOfMainBody();
1158 void InsetText::GetCursorPos(BufferView * bv,
1159 int & x, int & y) const
1166 unsigned int InsetText::InsetInInsetY()
1168 if (!the_locking_inset)
1171 return (inset_y + the_locking_inset->InsetInInsetY());
1175 void InsetText::ToggleInsetCursor(BufferView * bv)
1177 if (the_locking_inset) {
1178 the_locking_inset->ToggleInsetCursor(bv);
1182 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1184 int asc = lyxfont::maxAscent(font);
1185 int desc = lyxfont::maxDescent(font);
1188 bv->hideLockedInsetCursor();
1190 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1191 cursor_visible = !cursor_visible;
1195 void InsetText::ShowInsetCursor(BufferView * bv, bool show)
1197 if (the_locking_inset) {
1198 the_locking_inset->ShowInsetCursor(bv);
1201 if (!cursor_visible) {
1202 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1204 int asc = lyxfont::maxAscent(font);
1205 int desc = lyxfont::maxDescent(font);
1207 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1209 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1210 cursor_visible = true;
1215 void InsetText::HideInsetCursor(BufferView * bv)
1217 if (cursor_visible) {
1218 bv->hideLockedInsetCursor();
1219 cursor_visible = false;
1221 if (the_locking_inset)
1222 the_locking_inset->HideInsetCursor(bv);
1226 UpdatableInset::RESULT
1227 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1229 if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1231 if (activate_inset && checkAndActivateInset(bv, false))
1233 TEXT(bv)->CursorRight(bv, selecting);
1234 return DISPATCHED_NOUPDATE;
1238 UpdatableInset::RESULT
1239 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1241 if (!cpar(bv)->previous && (cpos(bv) <= 0))
1243 TEXT(bv)->CursorLeft(bv, selecting);
1244 if (activate_inset && checkAndActivateInset(bv, true))
1246 return DISPATCHED_NOUPDATE;
1250 UpdatableInset::RESULT
1251 InsetText::moveUp(BufferView * bv)
1253 if (!crow(bv)->previous())
1255 TEXT(bv)->CursorUp(bv);
1256 return DISPATCHED_NOUPDATE;
1260 UpdatableInset::RESULT
1261 InsetText::moveDown(BufferView * bv)
1263 if (!crow(bv)->next())
1265 TEXT(bv)->CursorDown(bv);
1266 return DISPATCHED_NOUPDATE;
1270 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1272 if (the_locking_inset) {
1273 if (the_locking_inset->InsertInsetAllowed(inset))
1274 return the_locking_inset->InsertInset(bv, inset);
1277 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1279 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1280 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1282 bv->text->cursor.par()->previous,
1283 bv->text->cursor.par()->next
1286 inset->setOwner(this);
1287 HideInsetCursor(bv);
1288 TEXT(bv)->InsertInset(bv, inset);
1289 if ((cpar(bv)->GetChar(cpos(bv)) != LyXParagraph::META_INSET) ||
1290 (cpar(bv)->GetInset(cpos(bv)) != inset))
1291 TEXT(bv)->CursorLeft(bv);
1292 TEXT(bv)->selection = 0;
1293 bv->fitCursor(TEXT(bv));
1294 UpdateLocal(bv, CURSOR_PAR, true);
1295 ShowInsetCursor(bv);
1300 UpdatableInset * InsetText::GetLockingInset()
1302 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1306 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1310 if (the_locking_inset)
1311 return the_locking_inset->GetFirstLockingInsetOfType(c);
1316 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1318 bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1320 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1321 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1323 bv->text->cursor.par()->previous,
1324 bv->text->cursor.par()->next
1327 TEXT(bv)->SetFont(bv, font, toggleall);
1328 bv->fitCursor(TEXT(bv));
1329 UpdateLocal(bv, CURSOR_PAR, true);
1333 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1335 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1339 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1340 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1342 LyXFont const font =
1343 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1345 x = inset->width(bv, font);
1346 y = inset->descent(bv, font);
1350 inset_x = cx(bv) - top_x + drawTextXOffset;
1351 inset_y = cy(bv) + drawTextYOffset;
1352 inset->Edit(bv, x - inset_x, y - inset_y, 0);
1353 if (!the_locking_inset)
1355 UpdateLocal(bv, CURSOR_PAR, false);
1362 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1365 x = x - drawTextXOffset;
1366 y = y + insetAscent;
1367 Inset * inset = bv->checkInsetHit(TEXT(bv), x, y, button);
1374 inset_x = cx(bv) - top_x + drawTextXOffset;
1375 inset_y = cy(bv) + drawTextYOffset;
1376 inset->Edit(bv, x - inset_x, y - inset_y, button);
1377 if (!the_locking_inset)
1379 UpdateLocal(bv, CURSOR_PAR, false);
1386 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1388 int w = UpdatableInset::getMaxWidth(pain, inset);
1393 w = w - top_x + owner()->x();
1396 w -= (2 * TEXT_TO_INSET_OFFSET);
1398 // return w - (2*TEXT_TO_INSET_OFFSET);
1402 void InsetText::SetParagraphData(LyXParagraph *p)
1404 // delete all instances of LyXText before deleting the paragraps used
1406 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
1407 delete (*cit).second;
1422 par->SetInsetOwner(this);
1426 np->next = p->Clone();
1427 np->next->previous = np;
1429 np->SetInsetOwner(this);
1435 void InsetText::SetAutoBreakRows(bool flag)
1437 if (flag != autoBreakRows) {
1438 autoBreakRows = flag;
1446 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1448 if (how != drawFrame) {
1451 UpdateLocal(bv, DRAW_FRAME, false);
1456 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1458 if (frame_color != col) {
1461 UpdateLocal(bv, DRAW_FRAME, false);
1466 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1468 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1473 int InsetText::cx(BufferView * bv) const
1475 LyXText * text = TEXT(bv);
1476 int x = text->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1477 if (the_locking_inset) {
1478 LyXFont font = text->GetFont(bv->buffer(),
1479 text->cursor.par(), text->cursor.pos());
1480 if (font.isVisibleRightToLeft())
1481 x -= the_locking_inset->width(bv, font);
1487 int InsetText::cy(BufferView * bv) const
1490 return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1494 LyXParagraph::size_type InsetText::cpos(BufferView * bv) const
1496 return TEXT(bv)->cursor.pos();
1500 LyXParagraph * InsetText::cpar(BufferView * bv) const
1502 return TEXT(bv)->cursor.par();
1505 bool InsetText::cboundary(BufferView * bv) const
1507 return TEXT(bv)->cursor.boundary();
1511 Row * InsetText::crow(BufferView * bv) const
1513 return TEXT(bv)->cursor.row();
1517 LyXText * InsetText::getLyXText(BufferView const * lbv) const
1519 // Super UGLY! (Lgb)
1520 BufferView * bv = const_cast<BufferView *>(lbv);
1522 if ((cache.find(bv) != cache.end()) && cache[bv])
1524 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1527 if (the_locking_inset) {
1528 lt->SetCursor(bv, inset_par, inset_pos, true, inset_boundary);
1534 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1536 if ((cache.find(bv) == cache.end()) || !cache[bv])
1541 /// then remove all LyXText in text-insets
1542 LyXParagraph * p = par;
1543 for (;p;p = p->next) {
1544 p->deleteInsetsLyXText(bv);
1550 void InsetText::resizeLyXText(BufferView * bv) const
1552 if (!par->next && !par->size()) // resize not neccessary!
1554 if ((cache.find(bv) == cache.end()) || !cache[bv])
1557 LyXParagraph * lpar = 0;
1558 LyXParagraph * selstartpar = 0;
1559 LyXParagraph * selendpar = 0;
1560 LyXParagraph::size_type pos = 0;
1561 LyXParagraph::size_type selstartpos = 0;
1562 LyXParagraph::size_type selendpos = 0;
1563 bool boundary = false;
1564 bool selstartboundary = false;
1565 bool selendboundary = false;
1569 // ProhibitInput(bv);
1572 lpar = TEXT(bv)->cursor.par();
1573 pos = TEXT(bv)->cursor.pos();
1574 boundary = TEXT(bv)->cursor.boundary();
1575 selstartpar = TEXT(bv)->sel_start_cursor.par();
1576 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1577 selstartboundary = TEXT(bv)->sel_start_cursor.boundary();
1578 selendpar = TEXT(bv)->sel_end_cursor.par();
1579 selendpos = TEXT(bv)->sel_end_cursor.pos();
1580 selendboundary = TEXT(bv)->sel_end_cursor.boundary();
1581 selection = TEXT(bv)->selection;
1582 mark_set = TEXT(bv)->mark_set;
1584 deleteLyXText(bv, (the_locking_inset == 0));
1587 TEXT(bv)->selection = true;
1588 /* at this point just to avoid the Delete-Empty-Paragraph
1589 * Mechanism when setting the cursor */
1590 TEXT(bv)->mark_set = mark_set;
1592 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos,true,
1594 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1595 TEXT(bv)->SetCursor(bv, selendpar, selendpos, true, selendboundary);
1596 TEXT(bv)->SetSelection();
1597 TEXT(bv)->SetCursor(bv, lpar, pos);
1599 TEXT(bv)->SetCursor(bv, lpar, pos, true, boundary);
1600 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1601 TEXT(bv)->selection = false;
1605 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1606 // this will scroll the screen such that the cursor becomes visible
1607 bv->updateScrollbar();
1609 if (the_locking_inset) {
1610 /// then resize all LyXText in text-insets
1611 inset_x = cx(bv) - top_x + drawTextXOffset;
1612 inset_y = cy(bv) + drawTextYOffset;
1613 for (LyXParagraph * p = par; p; p = p->next) {
1614 p->resizeInsetsLyXText(bv);
1621 void InsetText::removeNewlines()
1623 for (LyXParagraph * p = par; p; p = p->next) {
1624 for (int i = 0; i < p->Last(); ++i) {
1625 if (p->GetChar(i) == LyXParagraph::META_NEWLINE)