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;
95 cursor_visible = false;
102 autoBreakRows = false;
106 SetParagraphData(ins->par);
107 autoBreakRows = ins->autoBreakRows;
108 drawFrame = ins->drawFrame;
110 par->SetInsetOwner(this);
111 frame_color = LColor::insetframe;
114 last_drawn_width = -1;
118 InsetText::~InsetText()
120 // delete all instances of LyXText before deleting the paragraps used
122 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
123 delete (*cit).second;
127 LyXParagraph * p = par->next_;
135 LyXParagraph * p = par->next();
146 void InsetText::clear()
148 // delete all instances of LyXText before deleting the paragraps used
150 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
151 delete (*cit).second;
155 LyXParagraph * p = par->next_;
163 LyXParagraph * p = par->next();
171 par = new LyXParagraph();
175 Inset * InsetText::Clone(Buffer const &) const
177 InsetText * t = new InsetText(*this);
182 void InsetText::Write(Buffer const * buf, ostream & os) const
185 WriteParagraphData(buf, os);
189 void InsetText::WriteParagraphData(Buffer const * buf, ostream & os) const
191 par->writeFile(buf, os, buf->params, 0, 0);
195 void InsetText::Read(Buffer const * buf, LyXLex & lex)
199 LyXParagraph * return_par = 0;
200 char depth = 0; // signed or unsigned?
202 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
203 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
205 LyXFont font(LyXFont::ALL_INHERIT);
207 // delete all instances of LyXText before deleting the paragraps used
209 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
210 delete (*cit).second;
215 LyXParagraph * p = par->next_;
223 LyXParagraph * p = par->next();
231 par = new LyXParagraph;
234 token = lex.GetString();
237 if (token == "\\end_inset")
239 if (const_cast<Buffer*>(buf)->
240 parseSingleLyXformat2Token(lex, par, return_par,token, pos, depth,
243 , footnoteflag, footnotekind
247 // the_end read this should NEVER happen
248 lex.printError("\\the_end read in inset! Error in document!");
256 return_par->SetInsetOwner(this);
258 return_par = return_par->next_;
260 return_par = return_par->next();
264 if (token != "\\end_inset") {
265 lex.printError("Missing \\end_inset at this point. "
272 int InsetText::ascent(BufferView * bv, LyXFont const &) const
275 Row * row = TEXT(bv)->GetRowNearY(y_temp);
276 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
281 int InsetText::descent(BufferView * bv, LyXFont const &) const
284 Row * row = TEXT(bv)->GetRowNearY(y_temp);
285 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
286 TEXT_TO_INSET_OFFSET;
291 int InsetText::width(BufferView * bv, LyXFont const &) const
293 insetWidth = max(textWidth(bv),
294 (int)TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET));
299 int InsetText::textWidth(BufferView * bv) const
301 int const w = getMaxWidth(bv, this);
306 void InsetText::draw(BufferView * bv, LyXFont const & f,
307 int baseline, float & x, bool cleared) const
309 Painter & pain = bv->painter();
311 // no draw is necessary !!!
312 if ((drawFrame == LOCKED) && !locked && !par->size()) {
313 if (!cleared && (need_update & CLEAR_FRAME)) {
314 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
316 insetAscent + insetDescent - 1,
320 top_baseline = baseline;
327 UpdatableInset::draw(bv, f, baseline, x, cleared);
329 // update insetWidth and insetHeight with dummy calls
331 (void)descent(bv, f);
334 // if top_x differs we have a rule down and we don't have to clear anything
335 if (!cleared && (top_x == int(x)) &&
336 ((need_update==INIT)||(need_update==FULL)||(top_baseline!=baseline) ||
337 (last_drawn_width!=insetWidth)))
340 int h = insetAscent + insetDescent;
341 int ty = baseline - insetAscent;
347 if ((ty + h) > pain.paperHeight())
348 h = pain.paperHeight();
349 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
350 w = pain.paperWidth();
351 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
355 if (!cleared && (need_update == NONE))
358 if (top_x != int(x)) {
361 bv->text->status = LyXText::CHANGED_IN_DRAW;
364 if (cleared || (last_drawn_width != insetWidth)) {
366 last_drawn_width = insetWidth;
369 top_baseline = baseline;
370 top_y = baseline - ascent(bv, f);
371 last_width = width(bv, f);
372 last_height = ascent(bv, f) + descent(bv, f);
374 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
375 inset_x = cx(bv) - top_x + drawTextXOffset;
376 inset_y = cy(bv) + drawTextYOffset;
378 if (!cleared && (need_update == CURSOR) && !TEXT(bv)->selection) {
383 x += TEXT_TO_INSET_OFFSET;
386 Row * row = TEXT(bv)->GetRowNearY(y);
387 int y_offset = baseline - row->ascent_of_text();
388 int ph = pain.paperHeight();
391 while ((row != 0) && ((y+row->height()) <= 0)) {
393 first += row->height();
398 TEXT(bv)->first = first;
399 if (cleared) { // (need_update&FULL) || (need_update&INIT)
402 while ((row != 0) && (yf < ph)) {
403 TEXT(bv)->GetVisibleRow(bv, y+y_offset, int(x), row,
409 } else if (!locked) {
410 if (need_update & CURSOR) {
411 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
412 TEXT(bv)->ClearSelection(bv);
413 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
415 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
418 if (need_update & SELECTION)
419 bv->screen()->ToggleToggle(TEXT(bv), bv, y_offset, int(x));
420 else if (need_update & CURSOR) {
421 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
422 TEXT(bv)->ClearSelection(bv);
423 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
425 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
429 TEXT(bv)->refresh_y = 0;
430 TEXT(bv)->status = LyXText::UNCHANGED;
431 if ((need_update != CURSOR_PAR) &&
432 ((drawFrame == ALWAYS) || ((drawFrame == LOCKED) && locked)))
434 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
435 width(bv, f) - 1, insetAscent + insetDescent - 1,
437 } else if (need_update & CLEAR_FRAME) {
438 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
439 width(bv, f) - 1, insetAscent + insetDescent - 1,
442 x += width(bv, f) - TEXT_TO_INSET_OFFSET;
443 if (bv->text->status==LyXText::CHANGED_IN_DRAW) {
445 } else if (need_update != INIT)
450 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
452 if (reinit) { // && (need_update != CURSOR)) {
456 owner()->update(bv, font, true);
459 if (the_locking_inset) {
460 inset_x = cx(bv) - top_x + drawTextXOffset;
461 inset_y = cy(bv) + drawTextYOffset;
462 the_locking_inset->update(bv, font, reinit);
464 if (need_update == INIT) {
468 int oldw = insetWidth;
470 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
471 // max(textWidth(bv->painter()),
472 // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
473 // (2 * TEXT_TO_INSET_OFFSET);
475 insetWidth = textWidth(bv);
477 insetWidth = static_cast<int>(TEXT(bv)->width);
479 if (oldw != insetWidth) {
480 // printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
481 // textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
484 update(bv, font, reinit);
487 if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
490 TEXT(bv)->UpdateInset(bv, the_locking_inset);
493 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
497 Row * row = TEXT(bv)->GetRowNearY(y_temp);
498 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
499 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
500 TEXT_TO_INSET_OFFSET;
503 void InsetText::SetUpdateStatus(BufferView * bv, int what)
506 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
508 else if (TEXT(bv)->status == LyXText::NEED_VERY_LITTLE_REFRESH)
509 need_update |= CURSOR_PAR;
511 // this to not draw a selection when we redraw all of it!
512 if ((need_update & (INIT|FULL)) && (need_update & CURSOR))
513 TEXT(bv)->ClearSelection(bv);
516 void InsetText::UpdateLocal(BufferView * bv, int what, bool mark_dirty)
518 TEXT(bv)->FullRebreak(bv);
519 SetUpdateStatus(bv, what);
520 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
522 bv->updateInset(this, mark_dirty);
523 bv->owner()->showState();
524 if (old_par != cpar(bv)) {
525 bv->owner()->setLayout(cpar(bv)->GetLayout());
531 string const InsetText::EditMessage() const
533 return _("Opened Text Inset");
537 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
539 // par->SetInsetOwner(this);
540 UpdatableInset::Edit(bv, x, y, button);
542 if (!bv->lockInset(this)) {
543 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
547 the_locking_inset = 0;
548 inset_pos = inset_x = inset_y = 0;
549 inset_boundary = false;
552 if (!checkAndActivateInset(bv, x, y, button))
553 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
555 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
556 bv->text->FinishUndo();
558 UpdateLocal(bv, FULL, false);
560 // If the inset is empty set the language of the current font to the
561 // language to the surronding text.
563 if (par->Last() == 0 && !par->next_)
565 if (par->size() == 0 && !par->next())
568 LyXFont font(LyXFont::ALL_IGNORE);
569 font.setLanguage(bv->getParentLanguage(this));
570 SetFont(bv, font, false);
575 void InsetText::InsetUnlock(BufferView * bv)
577 if (the_locking_inset) {
578 the_locking_inset->InsetUnlock(bv);
579 the_locking_inset = 0;
582 no_selection = false;
584 UpdateLocal(bv, CLEAR_FRAME|CURSOR, false);
586 bv->owner()->setLayout(owner()->getLyXText(bv)
587 ->cursor.par()->GetLayout());
589 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
593 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
595 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
598 if (inset == cpar(bv)->GetInset(cpos(bv))) {
599 lyxerr[Debug::INSETS] << "OK" << endl;
600 the_locking_inset = inset;
601 inset_x = cx(bv) - top_x + drawTextXOffset;
602 inset_y = cy(bv) + drawTextYOffset;
603 inset_pos = cpos(bv);
604 inset_par = cpar(bv);
605 inset_boundary = cboundary(bv);
607 TEXT(bv)->ClearSelection(bv);
608 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
609 TEXT(bv)->UpdateInset(bv, the_locking_inset);
611 UpdateLocal(bv, CURSOR, false);
614 } else if (the_locking_inset && (the_locking_inset == inset)) {
615 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
616 lyxerr[Debug::INSETS] << "OK" << endl;
617 inset_x = cx(bv) - top_x + drawTextXOffset;
618 inset_y = cy(bv) + drawTextYOffset;
620 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
622 } else if (the_locking_inset) {
623 lyxerr[Debug::INSETS] << "MAYBE" << endl;
624 return the_locking_inset->LockInsetInInset(bv, inset);
626 lyxerr[Debug::INSETS] << "NOT OK" << endl;
631 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
634 if (!the_locking_inset)
636 if (the_locking_inset == inset) {
637 the_locking_inset->InsetUnlock(bv);
638 TEXT(bv)->UpdateInset(bv, inset);
639 the_locking_inset = 0;
641 moveRight(bv, false);
642 old_par = 0; // force layout setting
643 UpdateLocal(bv, CURSOR_PAR, false);
646 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
650 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
652 if (!the_locking_inset)
654 if (the_locking_inset != inset) {
655 TEXT(bv)->UpdateInset(bv, the_locking_inset);
656 SetUpdateStatus(bv, CURSOR_PAR);
657 return the_locking_inset->UpdateInsetInInset(bv, inset);
659 // UpdateLocal(bv, FULL, false);
660 if (TEXT(bv)->UpdateInset(bv, inset))
661 UpdateLocal(bv, CURSOR_PAR, false);
662 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
663 inset_x = cx(bv) - top_x + drawTextXOffset;
664 inset_y = cy(bv) + drawTextYOffset;
670 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
674 int tmp_x = x - drawTextXOffset;
675 int tmp_y = y + insetAscent - TEXT(bv)->first;
676 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
679 if (the_locking_inset) {
680 if (the_locking_inset == inset) {
681 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
682 no_selection = false;
685 // otherwise unlock the_locking_inset and lock the new inset
686 the_locking_inset->InsetUnlock(bv);
687 inset_x = cx(bv) - top_x + drawTextXOffset;
688 inset_y = cy(bv) + drawTextYOffset;
689 the_locking_inset = static_cast<UpdatableInset*>(inset);
690 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
691 inset->Edit(bv, x - inset_x, y - inset_y, button);
692 if (the_locking_inset)
693 UpdateLocal(bv, CURSOR, false);
694 no_selection = false;
697 // otherwise only unlock the_locking_inset
698 the_locking_inset->InsetUnlock(bv);
699 the_locking_inset = 0;
701 if (bv->theLockingInset()) {
702 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
703 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
704 inset_x = cx(bv) - top_x + drawTextXOffset;
705 inset_y = cy(bv) + drawTextYOffset;
706 inset_pos = cpos(bv);
707 inset_par = cpar(bv);
708 inset_boundary = cboundary(bv);
709 the_locking_inset = uinset;
710 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
711 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
712 if (the_locking_inset)
713 UpdateLocal(bv, CURSOR, false);
714 no_selection = false;
718 if (!inset && (button == 2)) {
719 bool paste_internally = false;
720 if ((button == 2) && TEXT(bv)->selection) {
721 LocalDispatch(bv, LFUN_COPY, "");
722 paste_internally = true;
724 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
726 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
727 UpdateLocal(bv, CURSOR, false);
728 bv->owner()->setLayout(cpar(bv)->GetLayout());
730 // Insert primary selection with middle mouse
731 // if there is a local selection in the current buffer,
734 if (paste_internally)
735 LocalDispatch(bv, LFUN_PASTE, "");
737 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
741 no_selection = false;
745 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
747 UpdatableInset * inset = 0;
749 if (the_locking_inset) {
750 the_locking_inset->InsetButtonRelease(bv,
751 x - inset_x, y - inset_y,
754 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
755 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
756 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
757 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
759 inset_x = cx(bv) - top_x + drawTextXOffset;
760 inset_y = cy(bv) + drawTextYOffset;
761 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
762 inset->Edit(bv, x - inset_x, y - inset_y, button);
764 UpdateLocal(bv, CURSOR_PAR, false);
767 no_selection = false;
771 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
775 if (the_locking_inset) {
776 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
781 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
783 TEXT(bv)->SetSelection(bv);
784 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
785 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
786 UpdateLocal(bv, SELECTION, false);
791 void InsetText::InsetKeyPress(XKeyEvent * xke)
793 if (the_locking_inset) {
794 the_locking_inset->InsetKeyPress(xke);
800 UpdatableInset::RESULT
801 InsetText::LocalDispatch(BufferView * bv,
802 kb_action action, string const & arg)
804 no_selection = false;
805 UpdatableInset::RESULT
806 result= UpdatableInset::LocalDispatch(bv, action, arg);
807 if (result != UNDISPATCHED) {
812 if ((action < 0) && arg.empty())
815 if (the_locking_inset) {
816 result = the_locking_inset->LocalDispatch(bv, action, arg);
817 if (result == DISPATCHED_NOUPDATE)
819 else if (result == DISPATCHED) {
820 UpdateLocal(bv, CURSOR_PAR, false);
822 } else if (result == FINISHED) {
823 bool dispatched = false;
825 case LFUN_UNKNOWN_ACTION:
826 case LFUN_BREAKPARAGRAPH:
828 moveRightIntern(bv, false, false);
831 if (!TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
832 moveRightIntern(bv, false, false);
836 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
837 moveRightIntern(bv, false, false);
843 the_locking_inset = 0;
851 case LFUN_UNKNOWN_ACTION:
852 if (bv->buffer()->isReadonly()) {
853 // setErrorMessage(N_("Document is read only"));
857 /* Automatically delete the currently selected
858 * text and replace it with what is being
859 * typed in now. Depends on lyxrc settings
860 * "auto_region_delete", which defaults to
863 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
865 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
866 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
868 bv->text->cursor.par()->previous(),
869 bv->text->cursor.par()->next()
873 if (lyxrc.auto_region_delete) {
874 if (TEXT(bv)->selection){
875 TEXT(bv)->CutSelection(bv, false);
878 TEXT(bv)->ClearSelection(bv);
879 for (string::size_type i = 0; i < arg.length(); ++i) {
881 if (!math_insert_greek(bv, arg[i])) {
882 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
883 } else if (!the_locking_inset) {
884 (void)moveRight(bv, false);
887 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
891 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
892 UpdateLocal(bv, CURSOR_PAR, true);
893 result=DISPATCHED_NOUPDATE;
895 // --- Cursor Movements ---------------------------------------------
897 bv->text->FinishUndo();
898 moveRight(bv, false, true);
899 TEXT(bv)->SetSelection(bv);
900 UpdateLocal(bv, SELECTION, false);
903 result = moveRight(bv);
904 bv->text->FinishUndo();
905 UpdateLocal(bv, CURSOR, false);
908 bv->text->FinishUndo();
909 moveLeft(bv, false, true);
910 TEXT(bv)->SetSelection(bv);
911 UpdateLocal(bv, SELECTION, false);
914 bv->text->FinishUndo();
915 result = moveLeft(bv);
916 UpdateLocal(bv, CURSOR, false);
919 bv->text->FinishUndo();
921 TEXT(bv)->SetSelection(bv);
922 UpdateLocal(bv, SELECTION, false);
925 bv->text->FinishUndo();
926 result = moveDown(bv);
927 UpdateLocal(bv, CURSOR, false);
930 bv->text->FinishUndo();
932 TEXT(bv)->SetSelection(bv);
933 UpdateLocal(bv, SELECTION, false);
936 bv->text->FinishUndo();
938 UpdateLocal(bv, CURSOR, false);
941 bv->text->FinishUndo();
942 TEXT(bv)->CursorHome(bv);
943 UpdateLocal(bv, CURSOR, false);
946 TEXT(bv)->CursorEnd(bv);
947 UpdateLocal(bv, CURSOR, false);
950 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
952 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
953 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
955 bv->text->cursor.par()->previous(),
956 bv->text->cursor.par()->next()
959 if (TEXT(bv)->selection)
960 TEXT(bv)->CutSelection(bv);
962 TEXT(bv)->Backspace(bv);
963 UpdateLocal(bv, CURSOR_PAR, true);
966 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
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 if (TEXT(bv)->selection)
976 TEXT(bv)->CutSelection(bv);
978 TEXT(bv)->Delete(bv);
979 UpdateLocal(bv, CURSOR_PAR, true);
982 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
984 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
985 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
987 bv->text->cursor.par()->previous(),
988 bv->text->cursor.par()->next()
991 TEXT(bv)->CutSelection(bv);
992 UpdateLocal(bv, CURSOR_PAR, true);
995 bv->text->FinishUndo();
996 TEXT(bv)->CopySelection(bv);
997 UpdateLocal(bv, CURSOR_PAR, false);
999 case LFUN_PASTESELECTION:
1001 string clip(bv->workarea()->getClipboard());
1005 if (arg == "paragraph") {
1006 TEXT(bv)->InsertStringB(bv, clip);
1008 TEXT(bv)->InsertStringA(bv, clip);
1010 UpdateLocal(bv, CURSOR_PAR, true);
1014 if (!autoBreakRows) {
1017 if (cap.nrOfParagraphs() > 1) {
1018 WriteAlert(_("Impossible operation"),
1019 _("Cannot include more than one paragraph!"),
1024 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1026 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1027 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1029 bv->text->cursor.par()->previous(),
1030 bv->text->cursor.par()->next()
1033 TEXT(bv)->PasteSelection(bv);
1034 UpdateLocal(bv, CURSOR_PAR, true);
1036 case LFUN_BREAKPARAGRAPH:
1039 TEXT(bv)->BreakParagraph(bv, 0);
1040 UpdateLocal(bv, FULL, true);
1042 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1045 TEXT(bv)->BreakParagraph(bv, 1);
1046 UpdateLocal(bv, FULL, true);
1048 case LFUN_BREAKLINE:
1051 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1053 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1054 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1056 bv->text->cursor.par()->previous(),
1057 bv->text->cursor.par()->next()
1060 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
1061 UpdateLocal(bv, CURSOR_PAR, true);
1064 // do not set layouts on non breakable textinsets
1065 if (autoBreakRows) {
1066 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1068 // Derive layout number from given argument (string)
1069 // and current buffer's textclass (number). */
1070 LyXTextClassList::ClassList::size_type tclass =
1071 bv->buffer()->params.textclass;
1072 std::pair <bool, LyXTextClass::size_type> layout =
1073 textclasslist.NumberOfLayout(tclass, arg);
1075 // If the entry is obsolete, use the new one instead.
1077 string obs = textclasslist.Style(tclass,layout.second).
1080 layout = textclasslist.NumberOfLayout(tclass, obs);
1083 // see if we found the layout number:
1084 if (!layout.first) {
1085 string msg = string(N_("Layout ")) + arg + N_(" not known");
1087 bv->owner()->getMiniBuffer()->Set(msg);
1091 if (cur_layout != layout.second) {
1092 cur_layout = layout.second;
1093 TEXT(bv)->SetLayout(bv, layout.second);
1094 bv->owner()->setLayout(cpar(bv)->GetLayout());
1095 UpdateLocal(bv, CURSOR_PAR, true);
1098 // reset the layout box
1099 bv->owner()->setLayout(cpar(bv)->GetLayout());
1102 case LFUN_PARAGRAPH_SPACING:
1103 // This one is absolutely not working. When fiddling with this
1104 // it also seems to me that the paragraphs inside the insettext
1105 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1107 LyXParagraph * par = TEXT(bv)->cursor.par();
1108 Spacing::Space cur_spacing = par->params.spacing().getSpace();
1109 float cur_value = 1.0;
1110 if (cur_spacing == Spacing::Other) {
1111 cur_value = par->params.spacing().getValue();
1114 std::istringstream istr(arg.c_str());
1117 Spacing::Space new_spacing = cur_spacing;
1118 float new_value = cur_value;
1120 lyxerr << "Missing argument to `paragraph-spacing'"
1122 } else if (tmp == "single") {
1123 new_spacing = Spacing::Single;
1124 } else if (tmp == "onehalf") {
1125 new_spacing = Spacing::Onehalf;
1126 } else if (tmp == "double") {
1127 new_spacing = Spacing::Double;
1128 } else if (tmp == "other") {
1129 new_spacing = Spacing::Other;
1132 lyxerr << "new_value = " << tmpval << endl;
1135 } else if (tmp == "default") {
1136 new_spacing = Spacing::Default;
1138 lyxerr << _("Unknown spacing argument: ")
1141 if (cur_spacing != new_spacing || cur_value != new_value) {
1142 par->params.spacing(Spacing(new_spacing, new_value));
1143 //TEXT(bv)->RedoParagraph(owner->view());
1144 UpdateLocal(bv, CURSOR_PAR, true);
1145 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1151 if (!bv->Dispatch(action, arg))
1152 result = UNDISPATCHED;
1156 /// If the action has deleted all text in the inset, we need to change the
1157 // language to the language to the surronding text.
1159 if (par->Last() == 0 && !par->next_)
1161 if (par->size() == 0 && !par->next())
1164 LyXFont font(LyXFont::ALL_IGNORE);
1165 font.setLanguage(bv->getParentLanguage(this));
1166 SetFont(bv, font, false);
1169 if (result != FINISHED) {
1170 ShowInsetCursor(bv);
1172 bv->unlockInset(this);
1177 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1180 buf->latexParagraphs(os, par, 0, texrow);
1181 return texrow.rows();
1185 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1187 LyXParagraph * p = par;
1188 unsigned int lines = 0;
1192 tmp = buf->asciiParagraph(p, linelen);
1193 lines += countChar(tmp, '\n');
1205 int InsetText::DocBook(Buffer const * buf, ostream & os) const
1207 LyXParagraph * p = par;
1208 unsigned int lines = 0;
1213 buf->SimpleDocBookOnePar(os,tmp,p,desc,0);
1225 void InsetText::Validate(LaTeXFeatures & features) const
1227 LyXParagraph * p = par;
1229 p->validate(features);
1239 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1241 if (textclasslist.Style(buf->params.textclass,
1242 p->GetLayout()).labeltype != LABEL_MANUAL)
1245 return p->BeginningOfMainBody();
1249 void InsetText::GetCursorPos(BufferView * bv,
1250 int & x, int & y) const
1257 unsigned int InsetText::InsetInInsetY()
1259 if (!the_locking_inset)
1262 return (inset_y + the_locking_inset->InsetInInsetY());
1266 void InsetText::ToggleInsetCursor(BufferView * bv)
1268 if (the_locking_inset) {
1269 the_locking_inset->ToggleInsetCursor(bv);
1273 LyXFont const font(TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv)));
1275 int const asc = lyxfont::maxAscent(font);
1276 int const desc = lyxfont::maxDescent(font);
1278 if (isCursorVisible())
1279 bv->hideLockedInsetCursor();
1281 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1282 toggleCursorVisible();
1286 void InsetText::ShowInsetCursor(BufferView * bv, bool show)
1288 if (the_locking_inset) {
1289 the_locking_inset->ShowInsetCursor(bv);
1292 if (!isCursorVisible()) {
1293 LyXFont const font =
1294 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1296 int const asc = lyxfont::maxAscent(font);
1297 int const desc = lyxfont::maxDescent(font);
1299 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1301 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1302 setCursorVisible(true);
1307 void InsetText::HideInsetCursor(BufferView * bv)
1309 if (isCursorVisible()) {
1310 bv->hideLockedInsetCursor();
1311 setCursorVisible(false);
1313 if (the_locking_inset)
1314 the_locking_inset->HideInsetCursor(bv);
1318 UpdatableInset::RESULT
1319 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1321 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1322 return moveLeftIntern(bv, false, activate_inset, selecting);
1324 return moveRightIntern(bv, false, activate_inset, selecting);
1327 UpdatableInset::RESULT
1328 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1330 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1331 return moveRightIntern(bv, true, activate_inset, selecting);
1333 return moveLeftIntern(bv, true, activate_inset, selecting);
1337 UpdatableInset::RESULT
1338 InsetText::moveRightIntern(BufferView * bv, bool behind,
1339 bool activate_inset, bool selecting)
1342 if (!cpar(bv)->next_ && (cpos(bv) >= cpar(bv)->Last()))
1344 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1347 if (activate_inset && checkAndActivateInset(bv, behind))
1349 TEXT(bv)->CursorRight(bv);
1351 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1352 return DISPATCHED_NOUPDATE;
1356 UpdatableInset::RESULT
1357 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1358 bool activate_inset, bool selecting)
1361 if (!cpar(bv)->previous_ && (cpos(bv) <= 0))
1363 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1366 TEXT(bv)->CursorLeft(bv);
1368 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1369 if (activate_inset && checkAndActivateInset(bv, behind))
1371 return DISPATCHED_NOUPDATE;
1375 UpdatableInset::RESULT
1376 InsetText::moveUp(BufferView * bv)
1378 if (!crow(bv)->previous())
1380 TEXT(bv)->CursorUp(bv);
1381 return DISPATCHED_NOUPDATE;
1385 UpdatableInset::RESULT
1386 InsetText::moveDown(BufferView * bv)
1388 if (!crow(bv)->next())
1390 TEXT(bv)->CursorDown(bv);
1391 return DISPATCHED_NOUPDATE;
1395 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1397 if (the_locking_inset) {
1398 if (the_locking_inset->InsertInsetAllowed(inset))
1399 return the_locking_inset->InsertInset(bv, inset);
1402 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1404 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1405 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1407 bv->text->cursor.par()->previous(),
1408 bv->text->cursor.par()->next()
1411 inset->setOwner(this);
1412 HideInsetCursor(bv);
1413 TEXT(bv)->InsertInset(bv, inset);
1415 if ((cpar(bv)->GetChar(cpos(bv)) != LyXParagraph::META_INSET) ||
1416 (cpar(bv)->GetInset(cpos(bv)) != inset))
1417 TEXT(bv)->CursorLeft(bv);
1419 bv->fitCursor(TEXT(bv));
1420 UpdateLocal(bv, CURSOR_PAR|CURSOR, true);
1421 ShowInsetCursor(bv);
1426 UpdatableInset * InsetText::GetLockingInset()
1428 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1432 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1436 if (the_locking_inset)
1437 return the_locking_inset->GetFirstLockingInsetOfType(c);
1442 bool InsetText::ShowInsetDialog(BufferView * bv) const
1444 if (the_locking_inset)
1445 return the_locking_inset->ShowInsetDialog(bv);
1450 std::vector<string> const InsetText::getLabelList() const
1452 std::vector<string> label_list;
1454 LyXParagraph * tpar = par;
1456 LyXParagraph::inset_iterator beg = tpar->inset_iterator_begin();
1457 LyXParagraph::inset_iterator end = tpar->inset_iterator_end();
1458 for (; beg != end; ++beg) {
1459 std::vector<string> const l = (*beg)->getLabelList();
1460 label_list.insert(label_list.end(), l.begin(), l.end());
1462 tpar = tpar->next();
1468 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1470 if (TEXT(bv)->selection) {
1471 bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1473 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1474 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1476 bv->text->cursor.par()->previous(),
1477 bv->text->cursor.par()->next()
1481 TEXT(bv)->SetFont(bv, font, toggleall);
1482 bv->fitCursor(TEXT(bv));
1483 UpdateLocal(bv, CURSOR_PAR, true);
1487 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1489 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1493 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1494 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1496 LyXFont const font =
1497 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1499 x = inset->width(bv, font);
1500 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1503 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1505 //inset_x = cx(bv) - top_x + drawTextXOffset;
1506 //inset_y = cy(bv) + drawTextYOffset;
1507 inset->Edit(bv, x, y, 0);
1508 if (!the_locking_inset)
1510 UpdateLocal(bv, CURSOR_PAR, false);
1517 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1522 dummyx = x = x - drawTextXOffset;
1523 dummyy = y + insetAscent;
1524 Inset * inset = bv->checkInsetHit(TEXT(bv), dummyx, dummyy, button);
1531 inset_x = cx(bv) - top_x + drawTextXOffset;
1532 inset_y = cy(bv) + drawTextYOffset;
1533 inset->Edit(bv, x - inset_x, y - inset_y, button);
1534 if (!the_locking_inset)
1536 UpdateLocal(bv, CURSOR_PAR, false);
1543 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1545 int w = UpdatableInset::getMaxWidth(bv, inset);
1550 w = w - top_x + owner()->x();
1553 w -= (2 * TEXT_TO_INSET_OFFSET);
1555 // return w - (2*TEXT_TO_INSET_OFFSET);
1559 void InsetText::SetParagraphData(LyXParagraph * p)
1561 // delete all instances of LyXText before deleting the paragraps used
1563 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
1564 delete (*cit).second;
1580 par->SetInsetOwner(this);
1584 np->next(p->Clone());
1585 np->next_->previous(np);
1587 np->SetInsetOwner(this);
1601 par->SetInsetOwner(this);
1605 np->next(p->Clone());
1606 np->next()->previous(np);
1608 np->SetInsetOwner(this);
1615 void InsetText::SetText(string const & data)
1618 LyXFont font(LyXFont::ALL_SANE);
1619 for(unsigned int i=0; i < data.length(); ++i)
1620 par->InsertChar(i, data[i], font);
1624 void InsetText::SetAutoBreakRows(bool flag)
1626 if (flag != autoBreakRows) {
1627 autoBreakRows = flag;
1635 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1637 if (how != drawFrame) {
1640 UpdateLocal(bv, DRAW_FRAME, false);
1645 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1647 if (frame_color != col) {
1650 UpdateLocal(bv, DRAW_FRAME, false);
1655 int InsetText::cx(BufferView * bv) const
1657 LyXText * text = TEXT(bv);
1658 int x = text->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1659 if (the_locking_inset) {
1660 LyXFont font = text->GetFont(bv->buffer(),
1661 text->cursor.par(), text->cursor.pos());
1662 if (font.isVisibleRightToLeft())
1663 x -= the_locking_inset->width(bv, font);
1669 int InsetText::cy(BufferView * bv) const
1672 return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1676 LyXParagraph::size_type InsetText::cpos(BufferView * bv) const
1678 return TEXT(bv)->cursor.pos();
1682 LyXParagraph * InsetText::cpar(BufferView * bv) const
1684 return TEXT(bv)->cursor.par();
1687 bool InsetText::cboundary(BufferView * bv) const
1689 return TEXT(bv)->cursor.boundary();
1693 Row * InsetText::crow(BufferView * bv) const
1695 return TEXT(bv)->cursor.row();
1699 LyXText * InsetText::getLyXText(BufferView const * lbv, bool const recursive) const
1701 // Super UGLY! (Lgb)
1702 BufferView * bv = const_cast<BufferView *>(lbv);
1704 if ((cache.find(bv) != cache.end()) && cache[bv]) {
1705 if (recursive && the_locking_inset)
1706 return the_locking_inset->getLyXText(bv);
1709 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1712 if (the_locking_inset) {
1713 lt->SetCursor(bv, inset_par, inset_pos, true, inset_boundary);
1715 return the_locking_inset->getLyXText(bv);
1721 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1723 if ((cache.find(bv) == cache.end()) || !cache[bv])
1728 /// then remove all LyXText in text-insets
1729 LyXParagraph * p = par;
1731 for (; p; p = p->next_) {
1732 p->deleteInsetsLyXText(bv);
1735 for (; p; p = p->next()) {
1736 p->deleteInsetsLyXText(bv);
1743 void InsetText::resizeLyXText(BufferView * bv) const
1746 if (!par->next_ && !par->size()) // resize not neccessary!
1748 if (!par->next() && !par->size()) // resize not neccessary!
1751 if ((cache.find(bv) == cache.end()) || !cache[bv])
1754 LyXParagraph * lpar = 0;
1755 LyXParagraph * selstartpar = 0;
1756 LyXParagraph * selendpar = 0;
1757 LyXParagraph::size_type pos = 0;
1758 LyXParagraph::size_type selstartpos = 0;
1759 LyXParagraph::size_type selendpos = 0;
1760 bool boundary = false;
1761 bool selstartboundary = false;
1762 bool selendboundary = false;
1766 // ProhibitInput(bv);
1769 lpar = TEXT(bv)->cursor.par();
1770 pos = TEXT(bv)->cursor.pos();
1771 boundary = TEXT(bv)->cursor.boundary();
1772 selstartpar = TEXT(bv)->sel_start_cursor.par();
1773 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1774 selstartboundary = TEXT(bv)->sel_start_cursor.boundary();
1775 selendpar = TEXT(bv)->sel_end_cursor.par();
1776 selendpos = TEXT(bv)->sel_end_cursor.pos();
1777 selendboundary = TEXT(bv)->sel_end_cursor.boundary();
1778 selection = TEXT(bv)->selection;
1779 mark_set = TEXT(bv)->mark_set;
1781 deleteLyXText(bv, (the_locking_inset == 0));
1784 TEXT(bv)->selection = true;
1785 /* at this point just to avoid the Delete-Empty-Paragraph
1786 * Mechanism when setting the cursor */
1787 TEXT(bv)->mark_set = mark_set;
1789 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos,true,
1791 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1792 TEXT(bv)->SetCursor(bv, selendpar, selendpos, true, selendboundary);
1793 TEXT(bv)->SetSelection(bv);
1794 TEXT(bv)->SetCursor(bv, lpar, pos);
1796 TEXT(bv)->SetCursor(bv, lpar, pos, true, boundary);
1797 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1798 TEXT(bv)->selection = false;
1802 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1803 // this will scroll the screen such that the cursor becomes visible
1804 bv->updateScrollbar();
1806 if (the_locking_inset) {
1807 /// then resize all LyXText in text-insets
1808 inset_x = cx(bv) - top_x + drawTextXOffset;
1809 inset_y = cy(bv) + drawTextYOffset;
1811 for (LyXParagraph * p = par; p; p = p->next_) {
1812 p->resizeInsetsLyXText(bv);
1815 for (LyXParagraph * p = par; p; p = p->next()) {
1816 p->resizeInsetsLyXText(bv);
1824 void InsetText::removeNewlines()
1827 for (LyXParagraph * p = par; p; p = p->next_) {
1828 for (int i = 0; i < p->Last(); ++i) {
1830 for (LyXParagraph * p = par; p; p = p->next()) {
1831 for (int i = 0; i < p->size(); ++i) {
1833 if (p->GetChar(i) == LyXParagraph::META_NEWLINE)