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;
124 LyXParagraph * p = par->next_;
132 LyXParagraph * p = par->next();
143 void InsetText::clear()
145 // delete all instances of LyXText before deleting the paragraps used
147 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
148 delete (*cit).second;
152 LyXParagraph * p = par->next_;
160 LyXParagraph * p = par->next();
168 par = new LyXParagraph();
172 Inset * InsetText::Clone(Buffer const &) const
174 InsetText * t = new InsetText(*this);
179 void InsetText::Write(Buffer const * buf, ostream & os) const
182 WriteParagraphData(buf, os);
186 void InsetText::WriteParagraphData(Buffer const * buf, ostream & os) const
188 par->writeFile(buf, os, buf->params, 0, 0);
192 void InsetText::Read(Buffer const * buf, LyXLex & lex)
196 LyXParagraph * return_par = 0;
197 char depth = 0; // signed or unsigned?
199 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
200 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
202 LyXFont font(LyXFont::ALL_INHERIT);
204 // delete all instances of LyXText before deleting the paragraps used
206 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
207 delete (*cit).second;
212 LyXParagraph * p = par->next_;
220 LyXParagraph * p = par->next();
228 par = new LyXParagraph;
231 token = lex.GetString();
234 if (token == "\\end_inset")
236 if (const_cast<Buffer*>(buf)->
237 parseSingleLyXformat2Token(lex, par, return_par,token, pos, depth,
240 , footnoteflag, footnotekind
244 // the_end read this should NEVER happen
245 lex.printError("\\the_end read in inset! Error in document!");
253 return_par->SetInsetOwner(this);
255 return_par = return_par->next_;
257 return_par = return_par->next();
261 if (token != "\\end_inset") {
262 lex.printError("Missing \\end_inset at this point. "
269 int InsetText::ascent(BufferView * bv, LyXFont const &) const
272 Row * row = TEXT(bv)->GetRowNearY(y_temp);
273 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
278 int InsetText::descent(BufferView * bv, LyXFont const &) const
281 Row * row = TEXT(bv)->GetRowNearY(y_temp);
282 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
283 TEXT_TO_INSET_OFFSET;
288 int InsetText::width(BufferView * bv, LyXFont const &) const
290 insetWidth = max(textWidth(bv->painter()),
291 (int)TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET));
296 int InsetText::textWidth(Painter & pain) const
298 int const w = getMaxWidth(pain, this);
303 void InsetText::draw(BufferView * bv, LyXFont const & f,
304 int baseline, float & x, bool cleared) const
306 Painter & pain = bv->painter();
308 // no draw is necessary !!!
309 if ((drawFrame == LOCKED) && !locked && !par->size()) {
310 if (!cleared && (need_update & CLEAR_FRAME)) {
311 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
313 insetAscent + insetDescent - 1,
317 top_baseline = baseline;
324 UpdatableInset::draw(bv, f, baseline, x, cleared);
326 // if top_x differs we have a rule down and we don't have to clear anything
327 if (!cleared && (top_x == int(x)) &&
328 ((need_update==INIT)||(need_update==FULL)||(top_baseline!=baseline)))
331 int h = insetAscent + insetDescent;
332 int ty = baseline - insetAscent;
338 if ((ty + h) > pain.paperHeight())
339 h = pain.paperHeight();
340 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
341 w = pain.paperWidth();
342 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
346 if (!cleared && (need_update == NONE))
349 if (top_x != int(x)) {
352 bv->text->status = LyXText::CHANGED_IN_DRAW;
358 top_baseline = baseline;
359 top_y = baseline - ascent(bv, f);
360 last_width = width(bv, f);
361 last_height = ascent(bv, f) + descent(bv, f);
363 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
364 inset_x = cx(bv) - top_x + drawTextXOffset;
365 inset_y = cy(bv) + drawTextYOffset;
367 if (!cleared && (need_update == CURSOR) && !TEXT(bv)->selection) {
372 x += TEXT_TO_INSET_OFFSET;
375 Row * row = TEXT(bv)->GetRowNearY(y);
376 int y_offset = baseline - row->ascent_of_text();
377 int ph = pain.paperHeight();
380 while ((row != 0) && ((y+row->height()) <= 0)) {
382 first += row->height();
387 TEXT(bv)->first = first;
388 if (cleared) { // (need_update&FULL) || (need_update&INIT)
391 while ((row != 0) && (yf < ph)) {
392 TEXT(bv)->GetVisibleRow(bv, y+y_offset, int(x), row,
398 } else if (!locked) {
399 if (need_update & CURSOR) {
400 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
401 TEXT(bv)->ClearSelection(bv);
402 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
404 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
407 if (need_update & SELECTION)
408 bv->screen()->ToggleToggle(TEXT(bv), bv, y_offset, int(x));
409 else if (need_update & CURSOR) {
410 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
411 TEXT(bv)->ClearSelection(bv);
412 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
414 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
418 TEXT(bv)->refresh_y = 0;
419 TEXT(bv)->status = LyXText::UNCHANGED;
420 if ((need_update != CURSOR_PAR) &&
421 ((drawFrame == ALWAYS) || ((drawFrame == LOCKED) && locked)))
423 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
424 width(bv, f) - 1, insetAscent + insetDescent - 1,
426 } else if (need_update & CLEAR_FRAME) {
427 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
428 width(bv, f) - 1, insetAscent + insetDescent - 1,
431 x += width(bv, f) - TEXT_TO_INSET_OFFSET;
432 if (bv->text->status==LyXText::CHANGED_IN_DRAW) {
434 } else if (need_update != INIT)
439 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
441 if (reinit) { // && (need_update != CURSOR)) {
445 owner()->update(bv, font, true);
448 if (the_locking_inset) {
449 inset_x = cx(bv) - top_x + drawTextXOffset;
450 inset_y = cy(bv) + drawTextYOffset;
451 the_locking_inset->update(bv, font, reinit);
453 if (need_update == INIT) {
457 int oldw = insetWidth;
459 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
460 // max(textWidth(bv->painter()),
461 // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
462 // (2 * TEXT_TO_INSET_OFFSET);
464 insetWidth = textWidth(bv->painter());
466 insetWidth = static_cast<int>(TEXT(bv)->width);
468 if (oldw != insetWidth) {
469 // printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
470 // textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
473 update(bv, font, reinit);
476 if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
479 TEXT(bv)->UpdateInset(bv, the_locking_inset);
482 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
486 Row * row = TEXT(bv)->GetRowNearY(y_temp);
487 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
488 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
489 TEXT_TO_INSET_OFFSET;
492 void InsetText::SetUpdateStatus(BufferView * bv, int what)
495 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
497 else if (TEXT(bv)->status == LyXText::NEED_VERY_LITTLE_REFRESH)
498 need_update |= CURSOR_PAR;
500 // this to not draw a selection when we redraw all of it!
501 if ((need_update & (INIT|FULL)) && (need_update & CURSOR))
502 TEXT(bv)->ClearSelection(bv);
505 void InsetText::UpdateLocal(BufferView * bv, int what, bool mark_dirty)
507 TEXT(bv)->FullRebreak(bv);
508 SetUpdateStatus(bv, what);
509 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
511 bv->updateInset(this, mark_dirty);
512 bv->owner()->showState();
513 if (old_par != cpar(bv)) {
514 bv->owner()->setLayout(cpar(bv)->GetLayout());
520 string const InsetText::EditMessage() const
522 return _("Opened Text Inset");
526 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
528 // par->SetInsetOwner(this);
529 UpdatableInset::Edit(bv, x, y, button);
531 if (!bv->lockInset(this)) {
532 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
536 the_locking_inset = 0;
537 inset_pos = inset_x = inset_y = 0;
538 inset_boundary = false;
541 if (!checkAndActivateInset(bv, x, y, button))
542 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
544 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
545 bv->text->FinishUndo();
547 UpdateLocal(bv, FULL, false);
549 // If the inset is empty set the language of the current font to the
550 // language to the surronding text.
552 if (par->Last() == 0 && !par->next_) {
554 if (par->size() == 0 && !par->next()) {
556 LyXFont font(LyXFont::ALL_IGNORE);
557 font.setLanguage(bv->getParentLanguage(this));
558 SetFont(bv, font, false);
563 void InsetText::InsetUnlock(BufferView * bv)
565 if (the_locking_inset) {
566 the_locking_inset->InsetUnlock(bv);
567 the_locking_inset = 0;
570 no_selection = false;
572 UpdateLocal(bv, CLEAR_FRAME|CURSOR, false);
574 bv->owner()->setLayout(owner()->getLyXText(bv)
575 ->cursor.par()->GetLayout());
577 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
581 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
583 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
586 if (inset == cpar(bv)->GetInset(cpos(bv))) {
587 lyxerr[Debug::INSETS] << "OK" << endl;
588 the_locking_inset = inset;
589 inset_x = cx(bv) - top_x + drawTextXOffset;
590 inset_y = cy(bv) + drawTextYOffset;
591 inset_pos = cpos(bv);
592 inset_par = cpar(bv);
593 inset_boundary = cboundary(bv);
595 TEXT(bv)->ClearSelection(bv);
596 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
597 TEXT(bv)->UpdateInset(bv, the_locking_inset);
599 UpdateLocal(bv, CURSOR, false);
602 } else if (the_locking_inset && (the_locking_inset == inset)) {
603 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
604 lyxerr[Debug::INSETS] << "OK" << endl;
605 inset_x = cx(bv) - top_x + drawTextXOffset;
606 inset_y = cy(bv) + drawTextYOffset;
608 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
610 } else if (the_locking_inset) {
611 lyxerr[Debug::INSETS] << "MAYBE" << endl;
612 return the_locking_inset->LockInsetInInset(bv, inset);
614 lyxerr[Debug::INSETS] << "NOT OK" << endl;
619 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
622 if (!the_locking_inset)
624 if (the_locking_inset == inset) {
625 the_locking_inset->InsetUnlock(bv);
626 TEXT(bv)->UpdateInset(bv, inset);
627 the_locking_inset = 0;
629 moveRight(bv, false);
630 old_par = 0; // force layout setting
631 UpdateLocal(bv, CURSOR_PAR, false);
634 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
638 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
640 if (!the_locking_inset)
642 if (the_locking_inset != inset) {
643 TEXT(bv)->UpdateInset(bv, the_locking_inset);
644 SetUpdateStatus(bv, CURSOR_PAR);
645 return the_locking_inset->UpdateInsetInInset(bv, inset);
647 // UpdateLocal(bv, FULL, false);
648 if (TEXT(bv)->UpdateInset(bv, inset))
649 UpdateLocal(bv, CURSOR_PAR, false);
650 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
651 inset_x = cx(bv) - top_x + drawTextXOffset;
652 inset_y = cy(bv) + drawTextYOffset;
658 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
662 int tmp_x = x - drawTextXOffset;
663 int tmp_y = y + insetAscent - TEXT(bv)->first;
664 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
667 if (the_locking_inset) {
668 if (the_locking_inset == inset) {
669 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
670 no_selection = false;
673 // otherwise unlock the_locking_inset and lock the new inset
674 the_locking_inset->InsetUnlock(bv);
675 inset_x = cx(bv) - top_x + drawTextXOffset;
676 inset_y = cy(bv) + drawTextYOffset;
677 the_locking_inset = static_cast<UpdatableInset*>(inset);
678 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
679 inset->Edit(bv, x - inset_x, y - inset_y, button);
680 if (the_locking_inset)
681 UpdateLocal(bv, CURSOR, false);
682 no_selection = false;
685 // otherwise only unlock the_locking_inset
686 the_locking_inset->InsetUnlock(bv);
687 the_locking_inset = 0;
689 if (bv->theLockingInset()) {
690 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
691 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
692 inset_x = cx(bv) - top_x + drawTextXOffset;
693 inset_y = cy(bv) + drawTextYOffset;
694 inset_pos = cpos(bv);
695 inset_par = cpar(bv);
696 inset_boundary = cboundary(bv);
697 the_locking_inset = uinset;
698 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
699 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
700 if (the_locking_inset)
701 UpdateLocal(bv, CURSOR, false);
702 no_selection = false;
707 bool paste_internally = false;
708 if ((button == 2) && TEXT(bv)->selection) {
709 LocalDispatch(bv, LFUN_COPY, "");
710 paste_internally = true;
712 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
714 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
715 UpdateLocal(bv, CURSOR, false);
716 bv->owner()->setLayout(cpar(bv)->GetLayout());
718 // Insert primary selection with middle mouse
719 // if there is a local selection in the current buffer,
722 if (paste_internally)
723 LocalDispatch(bv, LFUN_PASTE, "");
725 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
729 no_selection = false;
733 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
735 UpdatableInset * inset = 0;
737 if (the_locking_inset) {
738 the_locking_inset->InsetButtonRelease(bv,
739 x - inset_x, y - inset_y,
742 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
743 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
744 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
745 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
747 inset_x = cx(bv) - top_x + drawTextXOffset;
748 inset_y = cy(bv) + drawTextYOffset;
749 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
750 inset->Edit(bv, x - inset_x, y - inset_y, button);
752 UpdateLocal(bv, CURSOR_PAR, false);
755 no_selection = false;
759 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
763 if (the_locking_inset) {
764 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
769 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
771 TEXT(bv)->SetSelection(bv);
772 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
773 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
774 UpdateLocal(bv, SELECTION, false);
779 void InsetText::InsetKeyPress(XKeyEvent * xke)
781 if (the_locking_inset) {
782 the_locking_inset->InsetKeyPress(xke);
788 UpdatableInset::RESULT
789 InsetText::LocalDispatch(BufferView * bv,
790 kb_action action, string const & arg)
792 no_selection = false;
793 UpdatableInset::RESULT
794 result= UpdatableInset::LocalDispatch(bv, action, arg);
795 if (result != UNDISPATCHED) {
800 if ((action < 0) && arg.empty())
803 if (the_locking_inset) {
804 result = the_locking_inset->LocalDispatch(bv, action, arg);
805 if (result == DISPATCHED_NOUPDATE)
807 else if (result == DISPATCHED) {
808 UpdateLocal(bv, CURSOR_PAR, false);
810 } else if (result == FINISHED) {
811 bool dispatched = false;
813 case LFUN_UNKNOWN_ACTION:
814 case LFUN_BREAKPARAGRAPH:
816 moveRightIntern(bv, false, false);
819 if (!TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
820 moveRightIntern(bv, false, false);
824 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
825 moveRightIntern(bv, false, false);
831 the_locking_inset = 0;
839 case LFUN_UNKNOWN_ACTION:
840 if (bv->buffer()->isReadonly()) {
841 // setErrorMessage(N_("Document is read only"));
845 /* Automatically delete the currently selected
846 * text and replace it with what is being
847 * typed in now. Depends on lyxrc settings
848 * "auto_region_delete", which defaults to
851 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
853 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
854 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
856 bv->text->cursor.par()->previous(),
857 bv->text->cursor.par()->next()
861 if (lyxrc.auto_region_delete) {
862 if (TEXT(bv)->selection){
863 TEXT(bv)->CutSelection(bv, false);
866 TEXT(bv)->ClearSelection(bv);
867 for (string::size_type i = 0; i < arg.length(); ++i) {
869 if (!math_insert_greek(bv, arg[i])) {
870 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
871 } else if (!the_locking_inset) {
872 (void)moveRight(bv, false);
875 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
879 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
880 UpdateLocal(bv, CURSOR_PAR, true);
881 result=DISPATCHED_NOUPDATE;
883 // --- Cursor Movements ---------------------------------------------
885 bv->text->FinishUndo();
886 moveRight(bv, false, true);
887 TEXT(bv)->SetSelection(bv);
888 UpdateLocal(bv, SELECTION, false);
891 result = moveRight(bv);
892 bv->text->FinishUndo();
893 UpdateLocal(bv, CURSOR, false);
896 bv->text->FinishUndo();
897 moveLeft(bv, false, true);
898 TEXT(bv)->SetSelection(bv);
899 UpdateLocal(bv, SELECTION, false);
902 bv->text->FinishUndo();
903 result = moveLeft(bv);
904 UpdateLocal(bv, CURSOR, false);
907 bv->text->FinishUndo();
909 TEXT(bv)->SetSelection(bv);
910 UpdateLocal(bv, SELECTION, false);
913 bv->text->FinishUndo();
914 result = moveDown(bv);
915 UpdateLocal(bv, CURSOR, false);
918 bv->text->FinishUndo();
920 TEXT(bv)->SetSelection(bv);
921 UpdateLocal(bv, SELECTION, false);
924 bv->text->FinishUndo();
926 UpdateLocal(bv, CURSOR, false);
929 bv->text->FinishUndo();
930 TEXT(bv)->CursorHome(bv);
931 UpdateLocal(bv, CURSOR, false);
934 TEXT(bv)->CursorEnd(bv);
935 UpdateLocal(bv, CURSOR, false);
938 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
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 if (TEXT(bv)->selection)
948 TEXT(bv)->CutSelection(bv);
950 TEXT(bv)->Backspace(bv);
951 UpdateLocal(bv, CURSOR_PAR, true);
954 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
956 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
957 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
959 bv->text->cursor.par()->previous(),
960 bv->text->cursor.par()->next()
963 if (TEXT(bv)->selection)
964 TEXT(bv)->CutSelection(bv);
966 TEXT(bv)->Delete(bv);
967 UpdateLocal(bv, CURSOR_PAR, true);
970 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
972 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
973 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
975 bv->text->cursor.par()->previous(),
976 bv->text->cursor.par()->next()
979 TEXT(bv)->CutSelection(bv);
980 UpdateLocal(bv, CURSOR_PAR, true);
983 bv->text->FinishUndo();
984 TEXT(bv)->CopySelection(bv);
985 UpdateLocal(bv, CURSOR_PAR, false);
987 case LFUN_PASTESELECTION:
989 string clip(bv->workarea()->getClipboard());
993 if (arg == "paragraph") {
994 TEXT(bv)->InsertStringB(bv, clip);
996 TEXT(bv)->InsertStringA(bv, clip);
998 UpdateLocal(bv, CURSOR_PAR, true);
1002 if (!autoBreakRows) {
1005 if (cap.nrOfParagraphs() > 1) {
1006 WriteAlert(_("Impossible operation"),
1007 _("Cannot include more than one paragraph!"),
1012 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1014 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1015 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1017 bv->text->cursor.par()->previous(),
1018 bv->text->cursor.par()->next()
1021 TEXT(bv)->PasteSelection(bv);
1022 UpdateLocal(bv, CURSOR_PAR, true);
1024 case LFUN_BREAKPARAGRAPH:
1027 TEXT(bv)->BreakParagraph(bv, 0);
1028 UpdateLocal(bv, FULL, true);
1030 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1033 TEXT(bv)->BreakParagraph(bv, 1);
1034 UpdateLocal(bv, FULL, true);
1036 case LFUN_BREAKLINE:
1039 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1041 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1042 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1044 bv->text->cursor.par()->previous(),
1045 bv->text->cursor.par()->next()
1048 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
1049 UpdateLocal(bv, CURSOR_PAR, true);
1052 // do not set layouts on non breakable textinsets
1053 if (autoBreakRows) {
1054 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1056 // Derive layout number from given argument (string)
1057 // and current buffer's textclass (number). */
1058 LyXTextClassList::ClassList::size_type tclass =
1059 bv->buffer()->params.textclass;
1060 std::pair <bool, LyXTextClass::size_type> layout =
1061 textclasslist.NumberOfLayout(tclass, arg);
1063 // If the entry is obsolete, use the new one instead.
1065 string obs = textclasslist.Style(tclass,layout.second).
1068 layout = textclasslist.NumberOfLayout(tclass, obs);
1071 // see if we found the layout number:
1072 if (!layout.first) {
1073 string msg = string(N_("Layout ")) + arg + N_(" not known");
1075 bv->owner()->getMiniBuffer()->Set(msg);
1079 if (cur_layout != layout.second) {
1080 cur_layout = layout.second;
1081 TEXT(bv)->SetLayout(bv, layout.second);
1082 bv->owner()->setLayout(cpar(bv)->GetLayout());
1083 UpdateLocal(bv, CURSOR_PAR, true);
1086 // reset the layout box
1087 bv->owner()->setLayout(cpar(bv)->GetLayout());
1090 case LFUN_PARAGRAPH_SPACING:
1091 // This one is absolutely not working. When fiddling with this
1092 // it also seems to me that the paragraphs inside the insettext
1093 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1095 LyXParagraph * par = TEXT(bv)->cursor.par();
1096 Spacing::Space cur_spacing = par->params.spacing().getSpace();
1097 float cur_value = 1.0;
1098 if (cur_spacing == Spacing::Other) {
1099 cur_value = par->params.spacing().getValue();
1102 std::istringstream istr(arg.c_str());
1105 Spacing::Space new_spacing = cur_spacing;
1106 float new_value = cur_value;
1108 lyxerr << "Missing argument to `paragraph-spacing'"
1110 } else if (tmp == "single") {
1111 new_spacing = Spacing::Single;
1112 } else if (tmp == "onehalf") {
1113 new_spacing = Spacing::Onehalf;
1114 } else if (tmp == "double") {
1115 new_spacing = Spacing::Double;
1116 } else if (tmp == "other") {
1117 new_spacing = Spacing::Other;
1120 lyxerr << "new_value = " << tmpval << endl;
1123 } else if (tmp == "default") {
1124 new_spacing = Spacing::Default;
1126 lyxerr << _("Unknown spacing argument: ")
1129 if (cur_spacing != new_spacing || cur_value != new_value) {
1130 par->params.spacing(Spacing(new_spacing, new_value));
1131 //TEXT(bv)->RedoParagraph(owner->view());
1132 UpdateLocal(bv, CURSOR_PAR, true);
1133 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1139 if (!bv->Dispatch(action, arg))
1140 result = UNDISPATCHED;
1144 /// If the action has deleted all text in the inset, we need to change the
1145 // language to the language to the surronding text.
1147 if (par->Last() == 0 && !par->next_) {
1149 if (par->size() == 0 && !par->next()) {
1151 LyXFont font(LyXFont::ALL_IGNORE);
1152 font.setLanguage(bv->getParentLanguage(this));
1153 SetFont(bv, font, false);
1156 if (result != FINISHED) {
1157 ShowInsetCursor(bv);
1159 bv->unlockInset(this);
1164 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1167 buf->latexParagraphs(os, par, 0, texrow);
1168 return texrow.rows();
1172 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1174 LyXParagraph * p = par;
1175 unsigned int lines = 0;
1179 tmp = buf->asciiParagraph(p, linelen);
1180 lines += countChar(tmp, '\n');
1192 int InsetText::DocBook(Buffer const * buf, ostream & os) const
1194 LyXParagraph * p = par;
1195 unsigned int lines = 0;
1200 buf->SimpleDocBookOnePar(os,tmp,p,desc,0);
1212 void InsetText::Validate(LaTeXFeatures & features) const
1214 LyXParagraph * p = par;
1216 p->validate(features);
1226 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1228 if (textclasslist.Style(buf->params.textclass,
1229 p->GetLayout()).labeltype != LABEL_MANUAL)
1232 return p->BeginningOfMainBody();
1236 void InsetText::GetCursorPos(BufferView * bv,
1237 int & x, int & y) const
1244 unsigned int InsetText::InsetInInsetY()
1246 if (!the_locking_inset)
1249 return (inset_y + the_locking_inset->InsetInInsetY());
1253 void InsetText::ToggleInsetCursor(BufferView * bv)
1255 if (the_locking_inset) {
1256 the_locking_inset->ToggleInsetCursor(bv);
1260 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1262 int asc = lyxfont::maxAscent(font);
1263 int desc = lyxfont::maxDescent(font);
1266 bv->hideLockedInsetCursor();
1268 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1269 cursor_visible = !cursor_visible;
1273 void InsetText::ShowInsetCursor(BufferView * bv, bool show)
1275 if (the_locking_inset) {
1276 the_locking_inset->ShowInsetCursor(bv);
1279 if (!cursor_visible) {
1280 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1282 int asc = lyxfont::maxAscent(font);
1283 int desc = lyxfont::maxDescent(font);
1285 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1287 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1288 cursor_visible = true;
1293 void InsetText::HideInsetCursor(BufferView * bv)
1295 if (cursor_visible) {
1296 bv->hideLockedInsetCursor();
1297 cursor_visible = false;
1299 if (the_locking_inset)
1300 the_locking_inset->HideInsetCursor(bv);
1304 UpdatableInset::RESULT
1305 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1307 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1308 return moveLeftIntern(bv, false, activate_inset, selecting);
1310 return moveRightIntern(bv, false, activate_inset, selecting);
1313 UpdatableInset::RESULT
1314 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1316 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1317 return moveRightIntern(bv, true, activate_inset, selecting);
1319 return moveLeftIntern(bv, true, activate_inset, selecting);
1323 UpdatableInset::RESULT
1324 InsetText::moveRightIntern(BufferView * bv, bool behind,
1325 bool activate_inset, bool selecting)
1328 if (!cpar(bv)->next_ && (cpos(bv) >= cpar(bv)->Last()))
1330 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1333 if (activate_inset && checkAndActivateInset(bv, behind))
1335 TEXT(bv)->CursorRight(bv);
1337 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1338 return DISPATCHED_NOUPDATE;
1342 UpdatableInset::RESULT
1343 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1344 bool activate_inset, bool selecting)
1347 if (!cpar(bv)->previous_ && (cpos(bv) <= 0))
1349 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1352 TEXT(bv)->CursorLeft(bv);
1354 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1355 if (activate_inset && checkAndActivateInset(bv, behind))
1357 return DISPATCHED_NOUPDATE;
1361 UpdatableInset::RESULT
1362 InsetText::moveUp(BufferView * bv)
1364 if (!crow(bv)->previous())
1366 TEXT(bv)->CursorUp(bv);
1367 return DISPATCHED_NOUPDATE;
1371 UpdatableInset::RESULT
1372 InsetText::moveDown(BufferView * bv)
1374 if (!crow(bv)->next())
1376 TEXT(bv)->CursorDown(bv);
1377 return DISPATCHED_NOUPDATE;
1381 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1383 if (the_locking_inset) {
1384 if (the_locking_inset->InsertInsetAllowed(inset))
1385 return the_locking_inset->InsertInset(bv, inset);
1388 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1390 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1391 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1393 bv->text->cursor.par()->previous(),
1394 bv->text->cursor.par()->next()
1397 inset->setOwner(this);
1398 HideInsetCursor(bv);
1399 TEXT(bv)->InsertInset(bv, inset);
1401 if ((cpar(bv)->GetChar(cpos(bv)) != LyXParagraph::META_INSET) ||
1402 (cpar(bv)->GetInset(cpos(bv)) != inset))
1403 TEXT(bv)->CursorLeft(bv);
1405 bv->fitCursor(TEXT(bv));
1406 UpdateLocal(bv, CURSOR_PAR|CURSOR, true);
1407 ShowInsetCursor(bv);
1412 UpdatableInset * InsetText::GetLockingInset()
1414 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1418 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1422 if (the_locking_inset)
1423 return the_locking_inset->GetFirstLockingInsetOfType(c);
1428 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1430 if (TEXT(bv)->selection) {
1431 bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1433 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1434 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1436 bv->text->cursor.par()->previous(),
1437 bv->text->cursor.par()->next()
1441 TEXT(bv)->SetFont(bv, font, toggleall);
1442 bv->fitCursor(TEXT(bv));
1443 UpdateLocal(bv, CURSOR_PAR, true);
1447 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1449 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1453 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1454 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1456 LyXFont const font =
1457 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1459 x = inset->width(bv, font);
1460 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1463 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1465 //inset_x = cx(bv) - top_x + drawTextXOffset;
1466 //inset_y = cy(bv) + drawTextYOffset;
1467 inset->Edit(bv, x, y, 0);
1468 if (!the_locking_inset)
1470 UpdateLocal(bv, CURSOR_PAR, false);
1477 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1480 x = x - drawTextXOffset;
1481 y = y + insetAscent;
1482 Inset * inset = bv->checkInsetHit(TEXT(bv), x, y, button);
1489 inset_x = cx(bv) - top_x + drawTextXOffset;
1490 inset_y = cy(bv) + drawTextYOffset;
1491 inset->Edit(bv, x - inset_x, y - inset_y, button);
1492 if (!the_locking_inset)
1494 UpdateLocal(bv, CURSOR_PAR, false);
1501 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1503 int w = UpdatableInset::getMaxWidth(pain, inset);
1508 w = w - top_x + owner()->x();
1511 w -= (2 * TEXT_TO_INSET_OFFSET);
1513 // return w - (2*TEXT_TO_INSET_OFFSET);
1517 void InsetText::SetParagraphData(LyXParagraph * p)
1519 // delete all instances of LyXText before deleting the paragraps used
1521 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
1522 delete (*cit).second;
1538 par->SetInsetOwner(this);
1542 np->next(p->Clone());
1543 np->next_->previous(np);
1545 np->SetInsetOwner(this);
1559 par->SetInsetOwner(this);
1563 np->next(p->Clone());
1564 np->next()->previous(np);
1566 np->SetInsetOwner(this);
1573 void InsetText::SetText(string const & data)
1576 LyXFont font(LyXFont::ALL_SANE);
1577 for(unsigned int i=0; i < data.length(); ++i)
1578 par->InsertChar(i, data[i], font);
1582 void InsetText::SetAutoBreakRows(bool flag)
1584 if (flag != autoBreakRows) {
1585 autoBreakRows = flag;
1593 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1595 if (how != drawFrame) {
1598 UpdateLocal(bv, DRAW_FRAME, false);
1603 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1605 if (frame_color != col) {
1608 UpdateLocal(bv, DRAW_FRAME, false);
1613 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1615 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1620 int InsetText::cx(BufferView * bv) const
1622 LyXText * text = TEXT(bv);
1623 int x = text->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1624 if (the_locking_inset) {
1625 LyXFont font = text->GetFont(bv->buffer(),
1626 text->cursor.par(), text->cursor.pos());
1627 if (font.isVisibleRightToLeft())
1628 x -= the_locking_inset->width(bv, font);
1634 int InsetText::cy(BufferView * bv) const
1637 return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1641 LyXParagraph::size_type InsetText::cpos(BufferView * bv) const
1643 return TEXT(bv)->cursor.pos();
1647 LyXParagraph * InsetText::cpar(BufferView * bv) const
1649 return TEXT(bv)->cursor.par();
1652 bool InsetText::cboundary(BufferView * bv) const
1654 return TEXT(bv)->cursor.boundary();
1658 Row * InsetText::crow(BufferView * bv) const
1660 return TEXT(bv)->cursor.row();
1664 LyXText * InsetText::getLyXText(BufferView const * lbv, bool const recursive) const
1666 // Super UGLY! (Lgb)
1667 BufferView * bv = const_cast<BufferView *>(lbv);
1669 if ((cache.find(bv) != cache.end()) && cache[bv]) {
1670 if (recursive && the_locking_inset)
1671 return the_locking_inset->getLyXText(bv);
1674 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1677 if (the_locking_inset) {
1678 lt->SetCursor(bv, inset_par, inset_pos, true, inset_boundary);
1680 return the_locking_inset->getLyXText(bv);
1686 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1688 if ((cache.find(bv) == cache.end()) || !cache[bv])
1693 /// then remove all LyXText in text-insets
1694 LyXParagraph * p = par;
1696 for (; p; p = p->next_) {
1697 p->deleteInsetsLyXText(bv);
1700 for (; p; p = p->next()) {
1701 p->deleteInsetsLyXText(bv);
1708 void InsetText::resizeLyXText(BufferView * bv) const
1711 if (!par->next_ && !par->size()) // resize not neccessary!
1713 if (!par->next() && !par->size()) // resize not neccessary!
1716 if ((cache.find(bv) == cache.end()) || !cache[bv])
1719 LyXParagraph * lpar = 0;
1720 LyXParagraph * selstartpar = 0;
1721 LyXParagraph * selendpar = 0;
1722 LyXParagraph::size_type pos = 0;
1723 LyXParagraph::size_type selstartpos = 0;
1724 LyXParagraph::size_type selendpos = 0;
1725 bool boundary = false;
1726 bool selstartboundary = false;
1727 bool selendboundary = false;
1731 // ProhibitInput(bv);
1734 lpar = TEXT(bv)->cursor.par();
1735 pos = TEXT(bv)->cursor.pos();
1736 boundary = TEXT(bv)->cursor.boundary();
1737 selstartpar = TEXT(bv)->sel_start_cursor.par();
1738 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1739 selstartboundary = TEXT(bv)->sel_start_cursor.boundary();
1740 selendpar = TEXT(bv)->sel_end_cursor.par();
1741 selendpos = TEXT(bv)->sel_end_cursor.pos();
1742 selendboundary = TEXT(bv)->sel_end_cursor.boundary();
1743 selection = TEXT(bv)->selection;
1744 mark_set = TEXT(bv)->mark_set;
1746 deleteLyXText(bv, (the_locking_inset == 0));
1749 TEXT(bv)->selection = true;
1750 /* at this point just to avoid the Delete-Empty-Paragraph
1751 * Mechanism when setting the cursor */
1752 TEXT(bv)->mark_set = mark_set;
1754 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos,true,
1756 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1757 TEXT(bv)->SetCursor(bv, selendpar, selendpos, true, selendboundary);
1758 TEXT(bv)->SetSelection(bv);
1759 TEXT(bv)->SetCursor(bv, lpar, pos);
1761 TEXT(bv)->SetCursor(bv, lpar, pos, true, boundary);
1762 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1763 TEXT(bv)->selection = false;
1767 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1768 // this will scroll the screen such that the cursor becomes visible
1769 bv->updateScrollbar();
1771 if (the_locking_inset) {
1772 /// then resize all LyXText in text-insets
1773 inset_x = cx(bv) - top_x + drawTextXOffset;
1774 inset_y = cy(bv) + drawTextYOffset;
1776 for (LyXParagraph * p = par; p; p = p->next_) {
1777 p->resizeInsetsLyXText(bv);
1780 for (LyXParagraph * p = par; p; p = p->next()) {
1781 p->resizeInsetsLyXText(bv);
1789 void InsetText::removeNewlines()
1792 for (LyXParagraph * p = par; p; p = p->next_) {
1793 for (int i = 0; i < p->Last(); ++i) {
1795 for (LyXParagraph * p = par; p; p = p->next()) {
1796 for (int i = 0; i < p->size(); ++i) {
1798 if (p->GetChar(i) == LyXParagraph::META_NEWLINE)