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 = max(textWidth(bv->painter()),
257 (int)TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET));
262 int InsetText::textWidth(Painter & pain) const
264 int const w = getMaxWidth(pain, this);
269 void InsetText::draw(BufferView * bv, LyXFont const & f,
270 int baseline, float & x, bool cleared) const
272 Painter & pain = bv->painter();
274 // no draw is necessary !!!
275 if ((drawFrame == LOCKED) && !locked && !par->size()) {
276 if (!cleared && (need_update & CLEAR_FRAME)) {
277 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
279 insetAscent + insetDescent - 1,
283 top_baseline = baseline;
290 UpdatableInset::draw(bv, f, baseline, x, cleared);
292 // if top_x differs we have a rule down and we don't have to clear anything
293 if (!cleared && (top_x == int(x)) &&
294 ((need_update==INIT)||(need_update==FULL)||(top_baseline!=baseline)))
297 int h = insetAscent + insetDescent;
298 int ty = baseline - insetAscent;
304 if ((ty + h) > pain.paperHeight())
305 h = pain.paperHeight();
306 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
307 w = pain.paperWidth();
308 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
312 if (!cleared && (need_update == NONE))
315 if (top_x != int(x)) {
318 bv->text->status = LyXText::CHANGED_IN_DRAW;
322 top_baseline = baseline;
323 top_y = baseline - ascent(bv, f);
324 last_width = width(bv, f);
325 last_height = ascent(bv, f) + descent(bv, f);
327 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
328 inset_x = cx(bv) - top_x + drawTextXOffset;
329 inset_y = cy(bv) + drawTextYOffset;
331 if (!cleared && (need_update == CURSOR) && !TEXT(bv)->selection) {
336 x += TEXT_TO_INSET_OFFSET;
339 Row * row = TEXT(bv)->GetRowNearY(y);
340 int y_offset = baseline - row->ascent_of_text();
341 int ph = pain.paperHeight();
344 while ((row != 0) && ((y+row->height()) <= 0)) {
346 first += row->height();
351 TEXT(bv)->first = first;
352 if (cleared) { // (need_update&FULL) || (need_update&INIT)
355 while ((row != 0) && (yf < ph)) {
356 TEXT(bv)->GetVisibleRow(bv, y+y_offset, int(x), row,
362 } else if (!locked) {
363 if (need_update & CURSOR) {
364 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
365 TEXT(bv)->ClearSelection(bv);
366 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
368 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
371 if (need_update & SELECTION)
372 bv->screen()->ToggleToggle(TEXT(bv), bv, y_offset, int(x));
373 else if (need_update & CURSOR) {
374 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
375 TEXT(bv)->ClearSelection(bv);
376 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
378 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
382 TEXT(bv)->refresh_y = 0;
383 TEXT(bv)->status = LyXText::UNCHANGED;
384 if ((need_update != CURSOR_PAR) &&
385 ((drawFrame == ALWAYS) || ((drawFrame == LOCKED) && locked)))
387 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
388 width(bv, f) - 1, insetAscent + insetDescent - 1,
390 } else if (need_update & CLEAR_FRAME) {
391 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
392 width(bv, f) - 1, insetAscent + insetDescent - 1,
395 x += width(bv, f) - TEXT_TO_INSET_OFFSET;
396 if (bv->text->status==LyXText::CHANGED_IN_DRAW) {
398 } else if (need_update != INIT)
403 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
405 if (reinit) { // && (need_update != CURSOR)) {
409 owner()->update(bv, font, true);
412 if (the_locking_inset) {
413 inset_x = cx(bv) - top_x + drawTextXOffset;
414 inset_y = cy(bv) + drawTextYOffset;
415 the_locking_inset->update(bv, font, reinit);
417 if (need_update == INIT) {
421 int oldw = insetWidth;
423 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
424 // max(textWidth(bv->painter()),
425 // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
426 // (2 * TEXT_TO_INSET_OFFSET);
428 insetWidth = textWidth(bv->painter());
430 insetWidth = static_cast<int>(TEXT(bv)->width);
432 if (oldw != insetWidth) {
433 // printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
434 // textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
437 update(bv, font, reinit);
440 if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
443 TEXT(bv)->UpdateInset(bv, the_locking_inset);
446 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
450 Row * row = TEXT(bv)->GetRowNearY(y_temp);
451 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
452 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
453 TEXT_TO_INSET_OFFSET;
456 void InsetText::SetUpdateStatus(BufferView * bv, int what)
459 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
461 else if (TEXT(bv)->status == LyXText::NEED_VERY_LITTLE_REFRESH)
462 need_update |= CURSOR_PAR;
464 // this to not draw a selection when we redraw all of it!
465 if ((need_update & (INIT|FULL)) && (need_update & CURSOR))
466 TEXT(bv)->ClearSelection(bv);
469 void InsetText::UpdateLocal(BufferView * bv, int what, bool mark_dirty)
471 TEXT(bv)->FullRebreak(bv);
472 SetUpdateStatus(bv, what);
473 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
475 bv->updateInset(this, mark_dirty);
476 bv->owner()->showState();
477 if (old_par != cpar(bv)) {
478 bv->owner()->setLayout(cpar(bv)->GetLayout());
484 string const InsetText::EditMessage() const
486 return _("Opened Text Inset");
490 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
492 // par->SetInsetOwner(this);
493 UpdatableInset::Edit(bv, x, y, button);
495 if (!bv->lockInset(this)) {
496 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
500 the_locking_inset = 0;
501 inset_pos = inset_x = inset_y = 0;
502 inset_boundary = false;
505 if (!checkAndActivateInset(bv, x, y, button))
506 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
508 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
509 bv->text->FinishUndo();
511 UpdateLocal(bv, FULL, false);
513 // If the inset is empty set the language of the current font to the
514 // language to the surronding text.
515 if (par->Last() == 0 && !par->next) {
516 LyXFont font(LyXFont::ALL_IGNORE);
517 font.setLanguage(bv->getParentLanguage(this));
518 SetFont(bv, font, false);
523 void InsetText::InsetUnlock(BufferView * bv)
525 if (the_locking_inset) {
526 the_locking_inset->InsetUnlock(bv);
527 the_locking_inset = 0;
530 no_selection = false;
532 UpdateLocal(bv, CLEAR_FRAME|CURSOR, false);
534 bv->owner()->setLayout(owner()->getLyXText(bv)
535 ->cursor.par()->GetLayout());
537 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
541 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
543 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
546 if (inset == cpar(bv)->GetInset(cpos(bv))) {
547 lyxerr[Debug::INSETS] << "OK" << endl;
548 the_locking_inset = inset;
549 inset_x = cx(bv) - top_x + drawTextXOffset;
550 inset_y = cy(bv) + drawTextYOffset;
551 inset_pos = cpos(bv);
552 inset_par = cpar(bv);
553 inset_boundary = cboundary(bv);
555 TEXT(bv)->ClearSelection(bv);
556 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
557 TEXT(bv)->UpdateInset(bv, the_locking_inset);
559 UpdateLocal(bv, CURSOR, false);
562 } else if (the_locking_inset && (the_locking_inset == inset)) {
563 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
564 lyxerr[Debug::INSETS] << "OK" << endl;
565 inset_x = cx(bv) - top_x + drawTextXOffset;
566 inset_y = cy(bv) + drawTextYOffset;
568 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
570 } else if (the_locking_inset) {
571 lyxerr[Debug::INSETS] << "MAYBE" << endl;
572 return the_locking_inset->LockInsetInInset(bv, inset);
574 lyxerr[Debug::INSETS] << "NOT OK" << endl;
579 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
582 if (!the_locking_inset)
584 if (the_locking_inset == inset) {
585 the_locking_inset->InsetUnlock(bv);
586 TEXT(bv)->UpdateInset(bv, inset);
587 the_locking_inset = 0;
589 moveRight(bv, false);
590 old_par = 0; // force layout setting
591 UpdateLocal(bv, CURSOR_PAR, false);
594 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
598 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
600 if (!the_locking_inset)
602 if (the_locking_inset != inset) {
603 TEXT(bv)->UpdateInset(bv, the_locking_inset);
604 SetUpdateStatus(bv, CURSOR_PAR);
605 return the_locking_inset->UpdateInsetInInset(bv, inset);
607 // UpdateLocal(bv, FULL, false);
608 if (TEXT(bv)->UpdateInset(bv, inset))
609 UpdateLocal(bv, CURSOR_PAR, false);
610 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
611 inset_x = cx(bv) - top_x + drawTextXOffset;
612 inset_y = cy(bv) + drawTextYOffset;
618 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
622 int tmp_x = x - drawTextXOffset;
623 int tmp_y = y + insetAscent - TEXT(bv)->first;
624 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
627 if (the_locking_inset) {
628 if (the_locking_inset == inset) {
629 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
630 no_selection = false;
633 // otherwise unlock the_locking_inset and lock the new inset
634 the_locking_inset->InsetUnlock(bv);
635 inset_x = cx(bv) - top_x + drawTextXOffset;
636 inset_y = cy(bv) + drawTextYOffset;
637 the_locking_inset = static_cast<UpdatableInset*>(inset);
638 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
639 inset->Edit(bv, x - inset_x, y - inset_y, button);
640 if (the_locking_inset)
641 UpdateLocal(bv, CURSOR, false);
642 no_selection = false;
645 // otherwise only unlock the_locking_inset
646 the_locking_inset->InsetUnlock(bv);
647 the_locking_inset = 0;
649 if (bv->theLockingInset()) {
650 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
651 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
652 inset_x = cx(bv) - top_x + drawTextXOffset;
653 inset_y = cy(bv) + drawTextYOffset;
654 inset_pos = cpos(bv);
655 inset_par = cpar(bv);
656 inset_boundary = cboundary(bv);
657 the_locking_inset = uinset;
658 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
659 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
660 if (the_locking_inset)
661 UpdateLocal(bv, CURSOR, false);
662 no_selection = false;
667 bool paste_internally = false;
668 if ((button == 2) && TEXT(bv)->selection) {
669 LocalDispatch(bv, LFUN_COPY, "");
670 paste_internally = true;
672 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
674 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
675 UpdateLocal(bv, CURSOR, false);
676 bv->owner()->setLayout(cpar(bv)->GetLayout());
678 // Insert primary selection with middle mouse
679 // if there is a local selection in the current buffer,
682 if (paste_internally)
683 LocalDispatch(bv, LFUN_PASTE, "");
685 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
689 no_selection = false;
693 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
695 UpdatableInset * inset = 0;
697 if (the_locking_inset) {
698 the_locking_inset->InsetButtonRelease(bv,
699 x - inset_x, y - inset_y,
702 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
703 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
704 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
705 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
707 inset_x = cx(bv) - top_x + drawTextXOffset;
708 inset_y = cy(bv) + drawTextYOffset;
709 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
710 inset->Edit(bv, x - inset_x, y - inset_y, button);
712 UpdateLocal(bv, CURSOR_PAR, false);
715 no_selection = false;
719 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
723 if (the_locking_inset) {
724 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
729 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
731 TEXT(bv)->SetSelection(bv);
732 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
733 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
734 UpdateLocal(bv, SELECTION, false);
739 void InsetText::InsetKeyPress(XKeyEvent * xke)
741 if (the_locking_inset) {
742 the_locking_inset->InsetKeyPress(xke);
748 UpdatableInset::RESULT
749 InsetText::LocalDispatch(BufferView * bv,
750 kb_action action, string const & arg)
752 no_selection = false;
753 UpdatableInset::RESULT
754 result= UpdatableInset::LocalDispatch(bv, action, arg);
755 if (result != UNDISPATCHED) {
760 if ((action < 0) && arg.empty())
763 if (the_locking_inset) {
764 result = the_locking_inset->LocalDispatch(bv, action, arg);
765 if (result == DISPATCHED_NOUPDATE)
767 else if (result == DISPATCHED) {
768 UpdateLocal(bv, CURSOR_PAR, false);
770 } else if (result == FINISHED) {
771 bool dispatched = false;
773 case LFUN_UNKNOWN_ACTION:
774 case LFUN_BREAKPARAGRAPH:
776 moveRightIntern(bv, false, false);
779 if (!TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
780 moveRightIntern(bv, false, false);
784 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
785 moveRightIntern(bv, false, false);
791 the_locking_inset = 0;
799 case LFUN_UNKNOWN_ACTION:
800 if (bv->buffer()->isReadonly()) {
801 // setErrorMessage(N_("Document is read only"));
805 /* Automatically delete the currently selected
806 * text and replace it with what is being
807 * typed in now. Depends on lyxrc settings
808 * "auto_region_delete", which defaults to
811 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
813 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
814 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
816 bv->text->cursor.par()->previous,
817 bv->text->cursor.par()->next
821 if (lyxrc.auto_region_delete) {
822 if (TEXT(bv)->selection){
823 TEXT(bv)->CutSelection(bv, false);
826 TEXT(bv)->ClearSelection(bv);
827 for (string::size_type i = 0; i < arg.length(); ++i) {
829 if (!math_insert_greek(bv, arg[i])) {
830 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
831 } else if (!the_locking_inset) {
832 (void)moveRight(bv, false);
835 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
839 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
840 UpdateLocal(bv, CURSOR_PAR, true);
841 result=DISPATCHED_NOUPDATE;
843 // --- Cursor Movements ---------------------------------------------
845 bv->text->FinishUndo();
846 moveRight(bv, false, true);
847 TEXT(bv)->SetSelection(bv);
848 UpdateLocal(bv, SELECTION, false);
851 result = moveRight(bv);
852 bv->text->FinishUndo();
853 UpdateLocal(bv, CURSOR, false);
856 bv->text->FinishUndo();
857 moveLeft(bv, false, true);
858 TEXT(bv)->SetSelection(bv);
859 UpdateLocal(bv, SELECTION, false);
862 bv->text->FinishUndo();
863 result = moveLeft(bv);
864 UpdateLocal(bv, CURSOR, false);
867 bv->text->FinishUndo();
869 TEXT(bv)->SetSelection(bv);
870 UpdateLocal(bv, SELECTION, false);
873 bv->text->FinishUndo();
874 result = moveDown(bv);
875 UpdateLocal(bv, CURSOR, false);
878 bv->text->FinishUndo();
880 TEXT(bv)->SetSelection(bv);
881 UpdateLocal(bv, SELECTION, false);
884 bv->text->FinishUndo();
886 UpdateLocal(bv, CURSOR, false);
889 bv->text->FinishUndo();
890 TEXT(bv)->CursorHome(bv);
891 UpdateLocal(bv, CURSOR, false);
894 TEXT(bv)->CursorEnd(bv);
895 UpdateLocal(bv, CURSOR, false);
898 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
900 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
901 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
903 bv->text->cursor.par()->previous,
904 bv->text->cursor.par()->next
907 if (TEXT(bv)->selection)
908 TEXT(bv)->CutSelection(bv);
910 TEXT(bv)->Backspace(bv);
911 UpdateLocal(bv, CURSOR_PAR, true);
914 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
916 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
917 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
919 bv->text->cursor.par()->previous,
920 bv->text->cursor.par()->next
923 if (TEXT(bv)->selection)
924 TEXT(bv)->CutSelection(bv);
926 TEXT(bv)->Delete(bv);
927 UpdateLocal(bv, CURSOR_PAR, true);
930 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
932 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
933 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
935 bv->text->cursor.par()->previous,
936 bv->text->cursor.par()->next
939 TEXT(bv)->CutSelection(bv);
940 UpdateLocal(bv, CURSOR_PAR, true);
943 bv->text->FinishUndo();
944 TEXT(bv)->CopySelection(bv);
945 UpdateLocal(bv, CURSOR_PAR, false);
947 case LFUN_PASTESELECTION:
949 string clip(bv->workarea()->getClipboard());
953 if (arg == "paragraph") {
954 TEXT(bv)->InsertStringB(bv, clip);
956 TEXT(bv)->InsertStringA(bv, clip);
958 UpdateLocal(bv, CURSOR_PAR, true);
962 if (!autoBreakRows) {
965 if (cap.nrOfParagraphs() > 1) {
966 WriteAlert(_("Impossible operation"),
967 _("Cannot include more than one paragraph!"),
972 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
974 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
975 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
977 bv->text->cursor.par()->previous,
978 bv->text->cursor.par()->next
981 TEXT(bv)->PasteSelection(bv);
982 UpdateLocal(bv, CURSOR_PAR, true);
984 case LFUN_BREAKPARAGRAPH:
987 TEXT(bv)->BreakParagraph(bv, 0);
988 UpdateLocal(bv, FULL, true);
990 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
993 TEXT(bv)->BreakParagraph(bv, 1);
994 UpdateLocal(bv, FULL, true);
999 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1001 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1002 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1004 bv->text->cursor.par()->previous,
1005 bv->text->cursor.par()->next
1008 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
1009 UpdateLocal(bv, CURSOR_PAR, true);
1012 // do not set layouts on non breakable textinsets
1013 if (autoBreakRows) {
1014 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1016 // Derive layout number from given argument (string)
1017 // and current buffer's textclass (number). */
1018 LyXTextClassList::ClassList::size_type tclass =
1019 bv->buffer()->params.textclass;
1020 std::pair <bool, LyXTextClass::size_type> layout =
1021 textclasslist.NumberOfLayout(tclass, arg);
1023 // If the entry is obsolete, use the new one instead.
1025 string obs = textclasslist.Style(tclass,layout.second).
1028 layout = textclasslist.NumberOfLayout(tclass, obs);
1031 // see if we found the layout number:
1032 if (!layout.first) {
1033 string msg = string(N_("Layout ")) + arg + N_(" not known");
1035 bv->owner()->getMiniBuffer()->Set(msg);
1039 if (cur_layout != layout.second) {
1040 cur_layout = layout.second;
1041 TEXT(bv)->SetLayout(bv, layout.second);
1042 bv->owner()->setLayout(cpar(bv)->GetLayout());
1043 UpdateLocal(bv, CURSOR_PAR, true);
1046 // reset the layout box
1047 bv->owner()->setLayout(cpar(bv)->GetLayout());
1050 case LFUN_PARAGRAPH_SPACING:
1051 // This one is absolutely not working. When fiddling with this
1052 // it also seems to me that the paragraphs inside the insettext
1053 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1055 LyXParagraph * par = TEXT(bv)->cursor.par();
1056 Spacing::Space cur_spacing = par->spacing.getSpace();
1057 float cur_value = 1.0;
1058 if (cur_spacing == Spacing::Other) {
1059 cur_value = par->spacing.getValue();
1062 std::istringstream istr(arg.c_str());
1065 Spacing::Space new_spacing = cur_spacing;
1066 float new_value = cur_value;
1068 lyxerr << "Missing argument to `paragraph-spacing'"
1070 } else if (tmp == "single") {
1071 new_spacing = Spacing::Single;
1072 } else if (tmp == "onehalf") {
1073 new_spacing = Spacing::Onehalf;
1074 } else if (tmp == "double") {
1075 new_spacing = Spacing::Double;
1076 } else if (tmp == "other") {
1077 new_spacing = Spacing::Other;
1080 lyxerr << "new_value = " << tmpval << endl;
1083 } else if (tmp == "default") {
1084 new_spacing = Spacing::Default;
1086 lyxerr << _("Unknown spacing argument: ")
1089 if (cur_spacing != new_spacing || cur_value != new_value) {
1090 par->spacing.set(new_spacing, new_value);
1091 //TEXT(bv)->RedoParagraph(owner->view());
1092 UpdateLocal(bv, CURSOR_PAR, true);
1093 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1099 if (!bv->Dispatch(action, arg))
1100 result = UNDISPATCHED;
1104 /// If the action has deleted all text in the inset, we need to change the
1105 // language to the language to the surronding text.
1106 if (par->Last() == 0 && !par->next) {
1107 LyXFont font(LyXFont::ALL_IGNORE);
1108 font.setLanguage(bv->getParentLanguage(this));
1109 SetFont(bv, font, false);
1112 if (result != FINISHED) {
1113 ShowInsetCursor(bv);
1115 bv->unlockInset(this);
1120 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1123 buf->latexParagraphs(os, par, 0, texrow);
1124 return texrow.rows();
1128 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1130 LyXParagraph * p = par;
1131 unsigned int lines = 0;
1135 tmp = buf->asciiParagraph(p, linelen);
1136 lines += countChar(tmp, '\n');
1144 int InsetText::DocBook(Buffer const * buf, ostream & os) const
1146 LyXParagraph * p = par;
1147 unsigned int lines = 0;
1152 buf->SimpleDocBookOnePar(os,tmp,p,desc,0);
1160 void InsetText::Validate(LaTeXFeatures & features) const
1162 LyXParagraph * p = par;
1164 p->validate(features);
1170 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1172 if (textclasslist.Style(buf->params.textclass,
1173 p->GetLayout()).labeltype != LABEL_MANUAL)
1176 return p->BeginningOfMainBody();
1180 void InsetText::GetCursorPos(BufferView * bv,
1181 int & x, int & y) const
1188 unsigned int InsetText::InsetInInsetY()
1190 if (!the_locking_inset)
1193 return (inset_y + the_locking_inset->InsetInInsetY());
1197 void InsetText::ToggleInsetCursor(BufferView * bv)
1199 if (the_locking_inset) {
1200 the_locking_inset->ToggleInsetCursor(bv);
1204 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1206 int asc = lyxfont::maxAscent(font);
1207 int desc = lyxfont::maxDescent(font);
1210 bv->hideLockedInsetCursor();
1212 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1213 cursor_visible = !cursor_visible;
1217 void InsetText::ShowInsetCursor(BufferView * bv, bool show)
1219 if (the_locking_inset) {
1220 the_locking_inset->ShowInsetCursor(bv);
1223 if (!cursor_visible) {
1224 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1226 int asc = lyxfont::maxAscent(font);
1227 int desc = lyxfont::maxDescent(font);
1229 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1231 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1232 cursor_visible = true;
1237 void InsetText::HideInsetCursor(BufferView * bv)
1239 if (cursor_visible) {
1240 bv->hideLockedInsetCursor();
1241 cursor_visible = false;
1243 if (the_locking_inset)
1244 the_locking_inset->HideInsetCursor(bv);
1248 UpdatableInset::RESULT
1249 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1251 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1252 return moveLeftIntern(bv, false, activate_inset, selecting);
1254 return moveRightIntern(bv, false, activate_inset, selecting);
1257 UpdatableInset::RESULT
1258 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1260 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1261 return moveRightIntern(bv, true, activate_inset, selecting);
1263 return moveLeftIntern(bv, true, activate_inset, selecting);
1267 UpdatableInset::RESULT
1268 InsetText::moveRightIntern(BufferView * bv, bool behind,
1269 bool activate_inset, bool selecting)
1271 if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1273 if (activate_inset && checkAndActivateInset(bv, behind))
1275 TEXT(bv)->CursorRight(bv);
1277 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1278 return DISPATCHED_NOUPDATE;
1282 UpdatableInset::RESULT
1283 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1284 bool activate_inset, bool selecting)
1286 if (!cpar(bv)->previous && (cpos(bv) <= 0))
1288 TEXT(bv)->CursorLeft(bv);
1290 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1291 if (activate_inset && checkAndActivateInset(bv, behind))
1293 return DISPATCHED_NOUPDATE;
1297 UpdatableInset::RESULT
1298 InsetText::moveUp(BufferView * bv)
1300 if (!crow(bv)->previous())
1302 TEXT(bv)->CursorUp(bv);
1303 return DISPATCHED_NOUPDATE;
1307 UpdatableInset::RESULT
1308 InsetText::moveDown(BufferView * bv)
1310 if (!crow(bv)->next())
1312 TEXT(bv)->CursorDown(bv);
1313 return DISPATCHED_NOUPDATE;
1317 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1319 if (the_locking_inset) {
1320 if (the_locking_inset->InsertInsetAllowed(inset))
1321 return the_locking_inset->InsertInset(bv, inset);
1324 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1326 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1327 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1329 bv->text->cursor.par()->previous,
1330 bv->text->cursor.par()->next
1333 inset->setOwner(this);
1334 HideInsetCursor(bv);
1335 TEXT(bv)->InsertInset(bv, inset);
1337 if ((cpar(bv)->GetChar(cpos(bv)) != LyXParagraph::META_INSET) ||
1338 (cpar(bv)->GetInset(cpos(bv)) != inset))
1339 TEXT(bv)->CursorLeft(bv);
1341 bv->fitCursor(TEXT(bv));
1342 UpdateLocal(bv, CURSOR_PAR|CURSOR, true);
1343 ShowInsetCursor(bv);
1348 UpdatableInset * InsetText::GetLockingInset()
1350 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1354 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1358 if (the_locking_inset)
1359 return the_locking_inset->GetFirstLockingInsetOfType(c);
1364 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1366 bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1368 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1369 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1371 bv->text->cursor.par()->previous,
1372 bv->text->cursor.par()->next
1375 TEXT(bv)->SetFont(bv, font, toggleall);
1376 bv->fitCursor(TEXT(bv));
1377 UpdateLocal(bv, CURSOR_PAR, true);
1381 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1383 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1387 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1388 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1390 LyXFont const font =
1391 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1393 x = inset->width(bv, font);
1394 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1397 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1399 //inset_x = cx(bv) - top_x + drawTextXOffset;
1400 //inset_y = cy(bv) + drawTextYOffset;
1401 inset->Edit(bv, x, y, 0);
1402 if (!the_locking_inset)
1404 UpdateLocal(bv, CURSOR_PAR, false);
1411 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1414 x = x - drawTextXOffset;
1415 y = y + insetAscent;
1416 Inset * inset = bv->checkInsetHit(TEXT(bv), x, y, button);
1423 inset_x = cx(bv) - top_x + drawTextXOffset;
1424 inset_y = cy(bv) + drawTextYOffset;
1425 inset->Edit(bv, x - inset_x, y - inset_y, button);
1426 if (!the_locking_inset)
1428 UpdateLocal(bv, CURSOR_PAR, false);
1435 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1437 int w = UpdatableInset::getMaxWidth(pain, inset);
1442 w = w - top_x + owner()->x();
1445 w -= (2 * TEXT_TO_INSET_OFFSET);
1447 // return w - (2*TEXT_TO_INSET_OFFSET);
1451 void InsetText::SetParagraphData(LyXParagraph *p)
1453 // delete all instances of LyXText before deleting the paragraps used
1455 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
1456 delete (*cit).second;
1471 par->SetInsetOwner(this);
1475 np->next = p->Clone();
1476 np->next->previous = np;
1478 np->SetInsetOwner(this);
1484 void InsetText::SetText(string const & data)
1487 LyXFont font(LyXFont::ALL_SANE);
1488 for(unsigned int i=0; i < data.length(); ++i)
1489 par->InsertChar(i, data[i], font);
1493 void InsetText::SetAutoBreakRows(bool flag)
1495 if (flag != autoBreakRows) {
1496 autoBreakRows = flag;
1504 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1506 if (how != drawFrame) {
1509 UpdateLocal(bv, DRAW_FRAME, false);
1514 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1516 if (frame_color != col) {
1519 UpdateLocal(bv, DRAW_FRAME, false);
1524 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1526 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1531 int InsetText::cx(BufferView * bv) const
1533 LyXText * text = TEXT(bv);
1534 int x = text->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1535 if (the_locking_inset) {
1536 LyXFont font = text->GetFont(bv->buffer(),
1537 text->cursor.par(), text->cursor.pos());
1538 if (font.isVisibleRightToLeft())
1539 x -= the_locking_inset->width(bv, font);
1545 int InsetText::cy(BufferView * bv) const
1548 return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1552 LyXParagraph::size_type InsetText::cpos(BufferView * bv) const
1554 return TEXT(bv)->cursor.pos();
1558 LyXParagraph * InsetText::cpar(BufferView * bv) const
1560 return TEXT(bv)->cursor.par();
1563 bool InsetText::cboundary(BufferView * bv) const
1565 return TEXT(bv)->cursor.boundary();
1569 Row * InsetText::crow(BufferView * bv) const
1571 return TEXT(bv)->cursor.row();
1575 LyXText * InsetText::getLyXText(BufferView const * lbv, bool const recursive) const
1577 // Super UGLY! (Lgb)
1578 BufferView * bv = const_cast<BufferView *>(lbv);
1580 if ((cache.find(bv) != cache.end()) && cache[bv]) {
1581 if (recursive && the_locking_inset)
1582 return the_locking_inset->getLyXText(bv);
1585 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1588 if (the_locking_inset) {
1589 lt->SetCursor(bv, inset_par, inset_pos, true, inset_boundary);
1591 return the_locking_inset->getLyXText(bv);
1597 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1599 if ((cache.find(bv) == cache.end()) || !cache[bv])
1604 /// then remove all LyXText in text-insets
1605 LyXParagraph * p = par;
1606 for (;p;p = p->next) {
1607 p->deleteInsetsLyXText(bv);
1613 void InsetText::resizeLyXText(BufferView * bv) const
1615 if (!par->next && !par->size()) // resize not neccessary!
1617 if ((cache.find(bv) == cache.end()) || !cache[bv])
1620 LyXParagraph * lpar = 0;
1621 LyXParagraph * selstartpar = 0;
1622 LyXParagraph * selendpar = 0;
1623 LyXParagraph::size_type pos = 0;
1624 LyXParagraph::size_type selstartpos = 0;
1625 LyXParagraph::size_type selendpos = 0;
1626 bool boundary = false;
1627 bool selstartboundary = false;
1628 bool selendboundary = false;
1632 // ProhibitInput(bv);
1635 lpar = TEXT(bv)->cursor.par();
1636 pos = TEXT(bv)->cursor.pos();
1637 boundary = TEXT(bv)->cursor.boundary();
1638 selstartpar = TEXT(bv)->sel_start_cursor.par();
1639 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1640 selstartboundary = TEXT(bv)->sel_start_cursor.boundary();
1641 selendpar = TEXT(bv)->sel_end_cursor.par();
1642 selendpos = TEXT(bv)->sel_end_cursor.pos();
1643 selendboundary = TEXT(bv)->sel_end_cursor.boundary();
1644 selection = TEXT(bv)->selection;
1645 mark_set = TEXT(bv)->mark_set;
1647 deleteLyXText(bv, (the_locking_inset == 0));
1650 TEXT(bv)->selection = true;
1651 /* at this point just to avoid the Delete-Empty-Paragraph
1652 * Mechanism when setting the cursor */
1653 TEXT(bv)->mark_set = mark_set;
1655 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos,true,
1657 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1658 TEXT(bv)->SetCursor(bv, selendpar, selendpos, true, selendboundary);
1659 TEXT(bv)->SetSelection(bv);
1660 TEXT(bv)->SetCursor(bv, lpar, pos);
1662 TEXT(bv)->SetCursor(bv, lpar, pos, true, boundary);
1663 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1664 TEXT(bv)->selection = false;
1668 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1669 // this will scroll the screen such that the cursor becomes visible
1670 bv->updateScrollbar();
1672 if (the_locking_inset) {
1673 /// then resize all LyXText in text-insets
1674 inset_x = cx(bv) - top_x + drawTextXOffset;
1675 inset_y = cy(bv) + drawTextYOffset;
1676 for (LyXParagraph * p = par; p; p = p->next) {
1677 p->resizeInsetsLyXText(bv);
1684 void InsetText::removeNewlines()
1686 for (LyXParagraph * p = par; p; p = p->next) {
1687 for (int i = 0; i < p->Last(); ++i) {
1688 if (p->GetChar(i) == LyXParagraph::META_NEWLINE)