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 || !locked || (need_update&FULL) || (need_update&INIT)) {
355 while ((row != 0) && (yf < ph)) {
356 TEXT(bv)->GetVisibleRow(bv, y+y_offset, int(x), row,
364 if (need_update & SELECTION)
365 bv->screen()->ToggleToggle(TEXT(bv), bv, y_offset, int(x));
366 else if (need_update & CURSOR) {
367 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
368 TEXT(bv)->ClearSelection(bv);
369 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
371 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
375 TEXT(bv)->refresh_y = 0;
376 TEXT(bv)->status = LyXText::UNCHANGED;
377 if ((need_update != CURSOR_PAR) &&
378 ((drawFrame == ALWAYS) || ((drawFrame == LOCKED) && locked)))
380 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
381 width(bv, f) - 1, insetAscent + insetDescent - 1,
383 } else if (need_update & CLEAR_FRAME) {
384 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
385 width(bv, f) - 1, insetAscent + insetDescent - 1,
388 x += width(bv, f) - TEXT_TO_INSET_OFFSET;
389 if (bv->text->status==LyXText::CHANGED_IN_DRAW) {
391 } else if (need_update != INIT)
396 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
398 if (reinit) { // && (need_update != CURSOR)) {
402 owner()->update(bv, font, true);
405 if (the_locking_inset) {
406 inset_x = cx(bv) - top_x + drawTextXOffset;
407 inset_y = cy(bv) + drawTextYOffset;
408 the_locking_inset->update(bv, font, reinit);
410 if (need_update == INIT) {
414 int oldw = insetWidth;
416 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
417 // max(textWidth(bv->painter()),
418 // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
419 // (2 * TEXT_TO_INSET_OFFSET);
421 insetWidth = textWidth(bv->painter());
423 insetWidth = static_cast<int>(TEXT(bv)->width);
425 if (oldw != insetWidth) {
426 // printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
427 // textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
430 update(bv, font, reinit);
433 if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
436 TEXT(bv)->UpdateInset(bv, the_locking_inset);
439 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
443 Row * row = TEXT(bv)->GetRowNearY(y_temp);
444 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
445 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
446 TEXT_TO_INSET_OFFSET;
449 void InsetText::SetUpdateStatus(BufferView * bv, int what)
452 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
454 // this to not draw a selection when we redraw all of it!
455 if ((need_update & (INIT|FULL)) && (need_update & CURSOR))
456 TEXT(bv)->ClearSelection(bv);
459 void InsetText::UpdateLocal(BufferView * bv, int what, bool mark_dirty)
461 TEXT(bv)->FullRebreak(bv);
462 SetUpdateStatus(bv, what);
463 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
465 bv->updateInset(this, mark_dirty);
466 bv->owner()->showState();
467 if (old_par != cpar(bv)) {
468 bv->owner()->setLayout(cpar(bv)->GetLayout());
474 string const InsetText::EditMessage() const
476 return _("Opened Text Inset");
480 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
482 // par->SetInsetOwner(this);
483 UpdatableInset::Edit(bv, x, y, button);
485 if (!bv->lockInset(this)) {
486 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
490 the_locking_inset = 0;
491 inset_pos = inset_x = inset_y = 0;
492 inset_boundary = false;
495 if (!checkAndActivateInset(bv, x, y, button))
496 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
498 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
499 bv->text->FinishUndo();
501 UpdateLocal(bv, FULL, false);
503 // If the inset is empty set the language of the current font to the
504 // language to the surronding text.
505 if (par->Last() == 0 && !par->next) {
506 LyXFont font(LyXFont::ALL_IGNORE);
507 font.setLanguage(bv->getParentLanguage(this));
508 SetFont(bv, font, false);
513 void InsetText::InsetUnlock(BufferView * bv)
515 if (the_locking_inset) {
516 the_locking_inset->InsetUnlock(bv);
517 the_locking_inset = 0;
520 no_selection = false;
522 UpdateLocal(bv, CLEAR_FRAME|CURSOR, false);
524 bv->owner()->setLayout(owner()->getLyXText(bv)
525 ->cursor.par()->GetLayout());
527 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
531 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
533 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
536 if (inset == cpar(bv)->GetInset(cpos(bv))) {
537 lyxerr[Debug::INSETS] << "OK" << endl;
538 the_locking_inset = inset;
539 inset_x = cx(bv) - top_x + drawTextXOffset;
540 inset_y = cy(bv) + drawTextYOffset;
541 inset_pos = cpos(bv);
542 inset_par = cpar(bv);
543 inset_boundary = cboundary(bv);
545 TEXT(bv)->ClearSelection(bv);
546 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
547 TEXT(bv)->UpdateInset(bv, the_locking_inset);
549 UpdateLocal(bv, CURSOR, false);
552 } else if (the_locking_inset && (the_locking_inset == inset)) {
553 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
554 lyxerr[Debug::INSETS] << "OK" << endl;
555 inset_x = cx(bv) - top_x + drawTextXOffset;
556 inset_y = cy(bv) + drawTextYOffset;
558 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
560 } else if (the_locking_inset) {
561 lyxerr[Debug::INSETS] << "MAYBE" << endl;
562 return the_locking_inset->LockInsetInInset(bv, inset);
564 lyxerr[Debug::INSETS] << "NOT OK" << endl;
569 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
572 if (!the_locking_inset)
574 if (the_locking_inset == inset) {
575 the_locking_inset->InsetUnlock(bv);
576 TEXT(bv)->UpdateInset(bv, inset);
577 the_locking_inset = 0;
579 moveRight(bv, false);
580 old_par = 0; // force layout setting
581 UpdateLocal(bv, CURSOR_PAR, false);
584 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
588 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
590 if (!the_locking_inset)
592 if (the_locking_inset != inset) {
593 TEXT(bv)->UpdateInset(bv, the_locking_inset);
594 SetUpdateStatus(bv, CURSOR_PAR);
595 return the_locking_inset->UpdateInsetInInset(bv, inset);
597 // UpdateLocal(bv, FULL, false);
598 if (TEXT(bv)->UpdateInset(bv, inset))
599 UpdateLocal(bv, CURSOR_PAR, false);
600 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
601 inset_x = cx(bv) - top_x + drawTextXOffset;
602 inset_y = cy(bv) + drawTextYOffset;
608 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
612 int tmp_x = x - drawTextXOffset;
613 int tmp_y = y + insetAscent - TEXT(bv)->first;
614 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
617 if (the_locking_inset) {
618 if (the_locking_inset == inset) {
619 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
620 no_selection = false;
623 // otherwise unlock the_locking_inset and lock the new inset
624 the_locking_inset->InsetUnlock(bv);
625 inset_x = cx(bv) - top_x + drawTextXOffset;
626 inset_y = cy(bv) + drawTextYOffset;
627 the_locking_inset = static_cast<UpdatableInset*>(inset);
628 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
629 inset->Edit(bv, x - inset_x, y - inset_y, button);
630 if (the_locking_inset)
631 UpdateLocal(bv, CURSOR, false);
632 no_selection = false;
635 // otherwise only unlock the_locking_inset
636 the_locking_inset->InsetUnlock(bv);
637 the_locking_inset = 0;
639 if (bv->theLockingInset()) {
640 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
641 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
642 inset_x = cx(bv) - top_x + drawTextXOffset;
643 inset_y = cy(bv) + drawTextYOffset;
644 inset_pos = cpos(bv);
645 inset_par = cpar(bv);
646 inset_boundary = cboundary(bv);
647 the_locking_inset = uinset;
648 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
649 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
650 if (the_locking_inset)
651 UpdateLocal(bv, CURSOR, false);
652 no_selection = false;
657 bool paste_internally = false;
658 if ((button == 2) && TEXT(bv)->selection) {
659 LocalDispatch(bv, LFUN_COPY, "");
660 paste_internally = true;
662 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
664 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
665 UpdateLocal(bv, CURSOR, false);
666 bv->owner()->setLayout(cpar(bv)->GetLayout());
668 // Insert primary selection with middle mouse
669 // if there is a local selection in the current buffer,
672 if (paste_internally)
673 LocalDispatch(bv, LFUN_PASTE, "");
675 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
679 no_selection = false;
683 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
685 UpdatableInset * inset = 0;
687 if (the_locking_inset) {
688 the_locking_inset->InsetButtonRelease(bv,
689 x - inset_x, y - inset_y,
692 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
693 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
694 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
695 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
697 inset_x = cx(bv) - top_x + drawTextXOffset;
698 inset_y = cy(bv) + drawTextYOffset;
699 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
700 inset->Edit(bv, x - inset_x, y - inset_y, button);
702 UpdateLocal(bv, CURSOR_PAR, false);
705 no_selection = false;
709 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
713 if (the_locking_inset) {
714 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
719 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
721 TEXT(bv)->SetSelection(bv);
722 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
723 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
724 UpdateLocal(bv, SELECTION, false);
729 void InsetText::InsetKeyPress(XKeyEvent * xke)
731 if (the_locking_inset) {
732 the_locking_inset->InsetKeyPress(xke);
738 UpdatableInset::RESULT
739 InsetText::LocalDispatch(BufferView * bv,
740 int action, string const & arg)
742 no_selection = false;
743 UpdatableInset::RESULT
744 result= UpdatableInset::LocalDispatch(bv, action, arg);
745 if (result != UNDISPATCHED) {
750 if ((action < 0) && arg.empty())
753 if (the_locking_inset) {
754 result = the_locking_inset->LocalDispatch(bv, action, arg);
755 if (result == DISPATCHED_NOUPDATE)
757 else if (result == DISPATCHED) {
758 UpdateLocal(bv, CURSOR_PAR, false);
760 } else if (result == FINISHED) {
761 bool dispatched = false;
763 case LFUN_UNKNOWN_ACTION:
764 case LFUN_BREAKPARAGRAPH:
766 moveRightIntern(bv, false, false);
769 if (!TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
770 moveRightIntern(bv, false, false);
774 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
775 moveRightIntern(bv, false, false);
779 the_locking_inset = 0;
787 case LFUN_UNKNOWN_ACTION:
788 if (bv->buffer()->isReadonly()) {
789 // setErrorMessage(N_("Document is read only"));
793 /* Automatically delete the currently selected
794 * text and replace it with what is being
795 * typed in now. Depends on lyxrc settings
796 * "auto_region_delete", which defaults to
799 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
801 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
802 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
804 bv->text->cursor.par()->previous,
805 bv->text->cursor.par()->next
809 if (lyxrc.auto_region_delete) {
810 if (TEXT(bv)->selection){
811 TEXT(bv)->CutSelection(bv, false);
814 TEXT(bv)->ClearSelection(bv);
815 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
816 for (string::size_type i = 0; i < arg.length(); ++i) {
818 if (!math_insert_greek(bv, arg[i])) {
819 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
820 } else if (!the_locking_inset) {
821 (void)moveRight(bv, false);
824 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
828 UpdateLocal(bv, CURSOR_PAR, true);
829 result=DISPATCHED_NOUPDATE;
831 // --- Cursor Movements ---------------------------------------------
833 bv->text->FinishUndo();
834 moveRight(bv, false, true);
835 TEXT(bv)->SetSelection(bv);
836 UpdateLocal(bv, SELECTION, false);
839 result = moveRight(bv);
840 bv->text->FinishUndo();
841 UpdateLocal(bv, CURSOR, false);
844 bv->text->FinishUndo();
845 moveLeft(bv, false, true);
846 TEXT(bv)->SetSelection(bv);
847 UpdateLocal(bv, SELECTION, false);
850 bv->text->FinishUndo();
851 result = moveLeft(bv);
852 UpdateLocal(bv, CURSOR, false);
855 bv->text->FinishUndo();
857 TEXT(bv)->SetSelection(bv);
858 UpdateLocal(bv, SELECTION, false);
861 bv->text->FinishUndo();
862 result = moveDown(bv);
863 UpdateLocal(bv, CURSOR, false);
866 bv->text->FinishUndo();
868 TEXT(bv)->SetSelection(bv);
869 UpdateLocal(bv, SELECTION, false);
872 bv->text->FinishUndo();
874 UpdateLocal(bv, CURSOR, false);
877 bv->text->FinishUndo();
878 TEXT(bv)->CursorHome(bv);
879 UpdateLocal(bv, CURSOR, false);
882 TEXT(bv)->CursorEnd(bv);
883 UpdateLocal(bv, CURSOR, false);
886 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
888 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
889 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
891 bv->text->cursor.par()->previous,
892 bv->text->cursor.par()->next
895 if (TEXT(bv)->selection)
896 TEXT(bv)->CutSelection(bv);
898 TEXT(bv)->Backspace(bv);
899 UpdateLocal(bv, CURSOR_PAR, true);
902 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
904 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
905 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
907 bv->text->cursor.par()->previous,
908 bv->text->cursor.par()->next
911 if (TEXT(bv)->selection)
912 TEXT(bv)->CutSelection(bv);
914 TEXT(bv)->Delete(bv);
915 UpdateLocal(bv, CURSOR_PAR, true);
918 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
920 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
921 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
923 bv->text->cursor.par()->previous,
924 bv->text->cursor.par()->next
927 TEXT(bv)->CutSelection(bv);
928 UpdateLocal(bv, CURSOR_PAR, true);
931 bv->text->FinishUndo();
932 TEXT(bv)->CopySelection(bv);
933 UpdateLocal(bv, CURSOR_PAR, false);
935 case LFUN_PASTESELECTION:
937 string clip(bv->workarea()->getClipboard());
941 if (arg == "paragraph") {
942 TEXT(bv)->InsertStringB(bv, clip);
944 TEXT(bv)->InsertStringA(bv, clip);
946 UpdateLocal(bv, CURSOR_PAR, true);
950 if (!autoBreakRows) {
953 if (cap.nrOfParagraphs() > 1) {
954 WriteAlert(_("Impossible operation"),
955 _("Cannot include more than one paragraph!"),
960 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
962 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
963 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
965 bv->text->cursor.par()->previous,
966 bv->text->cursor.par()->next
969 TEXT(bv)->PasteSelection(bv);
970 UpdateLocal(bv, CURSOR_PAR, true);
972 case LFUN_BREAKPARAGRAPH:
975 TEXT(bv)->BreakParagraph(bv, 0);
976 UpdateLocal(bv, FULL, true);
978 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
981 TEXT(bv)->BreakParagraph(bv, 1);
982 UpdateLocal(bv, FULL, true);
987 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
989 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
990 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
992 bv->text->cursor.par()->previous,
993 bv->text->cursor.par()->next
996 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
997 UpdateLocal(bv, CURSOR_PAR, true);
1000 // do not set layouts on non breakable textinsets
1001 if (autoBreakRows) {
1002 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1004 // Derive layout number from given argument (string)
1005 // and current buffer's textclass (number). */
1006 LyXTextClassList::ClassList::size_type tclass =
1007 bv->buffer()->params.textclass;
1008 std::pair <bool, LyXTextClass::size_type> layout =
1009 textclasslist.NumberOfLayout(tclass, arg);
1011 // If the entry is obsolete, use the new one instead.
1013 string obs = textclasslist.Style(tclass,layout.second).
1016 layout = textclasslist.NumberOfLayout(tclass, obs);
1019 // see if we found the layout number:
1020 if (!layout.first) {
1021 string msg = string(N_("Layout ")) + arg + N_(" not known");
1023 bv->owner()->getMiniBuffer()->Set(msg);
1027 if (cur_layout != layout.second) {
1028 cur_layout = layout.second;
1029 TEXT(bv)->SetLayout(bv, layout.second);
1030 bv->owner()->setLayout(cpar(bv)->GetLayout());
1031 UpdateLocal(bv, CURSOR_PAR, true);
1034 // reset the layout box
1035 bv->owner()->setLayout(cpar(bv)->GetLayout());
1038 case LFUN_PARAGRAPH_SPACING:
1039 // This one is absolutely not working. When fiddling with this
1040 // it also seems to me that the paragraphs inside the insettext
1041 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1043 LyXParagraph * par = TEXT(bv)->cursor.par();
1044 Spacing::Space cur_spacing = par->spacing.getSpace();
1045 float cur_value = 1.0;
1046 if (cur_spacing == Spacing::Other) {
1047 cur_value = par->spacing.getValue();
1050 std::istringstream istr(arg.c_str());
1053 Spacing::Space new_spacing = cur_spacing;
1054 float new_value = cur_value;
1056 lyxerr << "Missing argument to `paragraph-spacing'"
1058 } else if (tmp == "single") {
1059 new_spacing = Spacing::Single;
1060 } else if (tmp == "onehalf") {
1061 new_spacing = Spacing::Onehalf;
1062 } else if (tmp == "double") {
1063 new_spacing = Spacing::Double;
1064 } else if (tmp == "other") {
1065 new_spacing = Spacing::Other;
1068 lyxerr << "new_value = " << tmpval << endl;
1071 } else if (tmp == "default") {
1072 new_spacing = Spacing::Default;
1074 lyxerr << _("Unknown spacing argument: ")
1077 if (cur_spacing != new_spacing || cur_value != new_value) {
1078 par->spacing.set(new_spacing, new_value);
1079 //TEXT(bv)->RedoParagraph(owner->view());
1080 UpdateLocal(bv, CURSOR_PAR, true);
1081 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1087 result = UNDISPATCHED;
1091 /// If the action has deleted all text in the inset, we need to change the
1092 // language to the language to the surronding text.
1093 if (par->Last() == 0 && !par->next) {
1094 LyXFont font(LyXFont::ALL_IGNORE);
1095 font.setLanguage(bv->getParentLanguage(this));
1096 SetFont(bv, font, false);
1099 if (result != FINISHED) {
1100 ShowInsetCursor(bv);
1102 bv->unlockInset(this);
1107 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1110 buf->latexParagraphs(os, par, 0, texrow);
1111 return texrow.rows();
1115 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1117 LyXParagraph * p = par;
1118 unsigned int lines = 0;
1122 tmp = buf->asciiParagraph(p, linelen);
1123 lines += countChar(tmp, '\n');
1131 int InsetText::DocBook(Buffer const * buf, ostream & os) const
1133 LyXParagraph * p = par;
1134 unsigned int lines = 0;
1139 buf->SimpleDocBookOnePar(os,tmp,p,desc,0);
1147 void InsetText::Validate(LaTeXFeatures & features) const
1149 LyXParagraph * p = par;
1151 p->validate(features);
1157 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1159 if (textclasslist.Style(buf->params.textclass,
1160 p->GetLayout()).labeltype != LABEL_MANUAL)
1163 return p->BeginningOfMainBody();
1167 void InsetText::GetCursorPos(BufferView * bv,
1168 int & x, int & y) const
1175 unsigned int InsetText::InsetInInsetY()
1177 if (!the_locking_inset)
1180 return (inset_y + the_locking_inset->InsetInInsetY());
1184 void InsetText::ToggleInsetCursor(BufferView * bv)
1186 if (the_locking_inset) {
1187 the_locking_inset->ToggleInsetCursor(bv);
1191 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1193 int asc = lyxfont::maxAscent(font);
1194 int desc = lyxfont::maxDescent(font);
1197 bv->hideLockedInsetCursor();
1199 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1200 cursor_visible = !cursor_visible;
1204 void InsetText::ShowInsetCursor(BufferView * bv, bool show)
1206 if (the_locking_inset) {
1207 the_locking_inset->ShowInsetCursor(bv);
1210 if (!cursor_visible) {
1211 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1213 int asc = lyxfont::maxAscent(font);
1214 int desc = lyxfont::maxDescent(font);
1216 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1218 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1219 cursor_visible = true;
1224 void InsetText::HideInsetCursor(BufferView * bv)
1226 if (cursor_visible) {
1227 bv->hideLockedInsetCursor();
1228 cursor_visible = false;
1230 if (the_locking_inset)
1231 the_locking_inset->HideInsetCursor(bv);
1235 UpdatableInset::RESULT
1236 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1238 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1239 return moveLeftIntern(bv, false, activate_inset, selecting);
1241 return moveRightIntern(bv, false, activate_inset, selecting);
1244 UpdatableInset::RESULT
1245 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1247 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1248 return moveRightIntern(bv, true, activate_inset, selecting);
1250 return moveLeftIntern(bv, true, activate_inset, selecting);
1254 UpdatableInset::RESULT
1255 InsetText::moveRightIntern(BufferView * bv, bool behind,
1256 bool activate_inset, bool selecting)
1258 if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1260 if (activate_inset && checkAndActivateInset(bv, behind))
1262 TEXT(bv)->CursorRight(bv);
1264 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1265 return DISPATCHED_NOUPDATE;
1269 UpdatableInset::RESULT
1270 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1271 bool activate_inset, bool selecting)
1273 if (!cpar(bv)->previous && (cpos(bv) <= 0))
1275 TEXT(bv)->CursorLeft(bv);
1277 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1278 if (activate_inset && checkAndActivateInset(bv, behind))
1280 return DISPATCHED_NOUPDATE;
1284 UpdatableInset::RESULT
1285 InsetText::moveUp(BufferView * bv)
1287 if (!crow(bv)->previous())
1289 TEXT(bv)->CursorUp(bv);
1290 return DISPATCHED_NOUPDATE;
1294 UpdatableInset::RESULT
1295 InsetText::moveDown(BufferView * bv)
1297 if (!crow(bv)->next())
1299 TEXT(bv)->CursorDown(bv);
1300 return DISPATCHED_NOUPDATE;
1304 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1306 if (the_locking_inset) {
1307 if (the_locking_inset->InsertInsetAllowed(inset))
1308 return the_locking_inset->InsertInset(bv, inset);
1311 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1313 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1314 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1316 bv->text->cursor.par()->previous,
1317 bv->text->cursor.par()->next
1320 inset->setOwner(this);
1321 HideInsetCursor(bv);
1322 TEXT(bv)->InsertInset(bv, inset);
1324 if ((cpar(bv)->GetChar(cpos(bv)) != LyXParagraph::META_INSET) ||
1325 (cpar(bv)->GetInset(cpos(bv)) != inset))
1326 TEXT(bv)->CursorLeft(bv);
1328 bv->fitCursor(TEXT(bv));
1329 UpdateLocal(bv, CURSOR_PAR|CURSOR, true);
1330 ShowInsetCursor(bv);
1335 UpdatableInset * InsetText::GetLockingInset()
1337 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1341 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1345 if (the_locking_inset)
1346 return the_locking_inset->GetFirstLockingInsetOfType(c);
1351 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1353 bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1355 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1356 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1358 bv->text->cursor.par()->previous,
1359 bv->text->cursor.par()->next
1362 TEXT(bv)->SetFont(bv, font, toggleall);
1363 bv->fitCursor(TEXT(bv));
1364 UpdateLocal(bv, CURSOR_PAR, true);
1368 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1370 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1374 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1375 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1377 LyXFont const font =
1378 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1380 x = inset->width(bv, font);
1381 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1384 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1386 //inset_x = cx(bv) - top_x + drawTextXOffset;
1387 //inset_y = cy(bv) + drawTextYOffset;
1388 inset->Edit(bv, x, y, 0);
1389 if (!the_locking_inset)
1391 UpdateLocal(bv, CURSOR_PAR, false);
1398 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1401 x = x - drawTextXOffset;
1402 y = y + insetAscent;
1403 Inset * inset = bv->checkInsetHit(TEXT(bv), x, y, button);
1410 inset_x = cx(bv) - top_x + drawTextXOffset;
1411 inset_y = cy(bv) + drawTextYOffset;
1412 inset->Edit(bv, x - inset_x, y - inset_y, button);
1413 if (!the_locking_inset)
1415 UpdateLocal(bv, CURSOR_PAR, false);
1422 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1424 int w = UpdatableInset::getMaxWidth(pain, inset);
1429 w = w - top_x + owner()->x();
1432 w -= (2 * TEXT_TO_INSET_OFFSET);
1434 // return w - (2*TEXT_TO_INSET_OFFSET);
1438 void InsetText::SetParagraphData(LyXParagraph *p)
1440 // delete all instances of LyXText before deleting the paragraps used
1442 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
1443 delete (*cit).second;
1458 par->SetInsetOwner(this);
1462 np->next = p->Clone();
1463 np->next->previous = np;
1465 np->SetInsetOwner(this);
1471 void InsetText::SetText(string const & data)
1474 LyXFont font(LyXFont::ALL_SANE);
1475 for(unsigned int i=0; i < data.length(); ++i)
1476 par->InsertChar(i, data[i], font);
1480 void InsetText::SetAutoBreakRows(bool flag)
1482 if (flag != autoBreakRows) {
1483 autoBreakRows = flag;
1491 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1493 if (how != drawFrame) {
1496 UpdateLocal(bv, DRAW_FRAME, false);
1501 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1503 if (frame_color != col) {
1506 UpdateLocal(bv, DRAW_FRAME, false);
1511 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1513 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1518 int InsetText::cx(BufferView * bv) const
1520 LyXText * text = TEXT(bv);
1521 int x = text->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1522 if (the_locking_inset) {
1523 LyXFont font = text->GetFont(bv->buffer(),
1524 text->cursor.par(), text->cursor.pos());
1525 if (font.isVisibleRightToLeft())
1526 x -= the_locking_inset->width(bv, font);
1532 int InsetText::cy(BufferView * bv) const
1535 return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1539 LyXParagraph::size_type InsetText::cpos(BufferView * bv) const
1541 return TEXT(bv)->cursor.pos();
1545 LyXParagraph * InsetText::cpar(BufferView * bv) const
1547 return TEXT(bv)->cursor.par();
1550 bool InsetText::cboundary(BufferView * bv) const
1552 return TEXT(bv)->cursor.boundary();
1556 Row * InsetText::crow(BufferView * bv) const
1558 return TEXT(bv)->cursor.row();
1562 LyXText * InsetText::getLyXText(BufferView const * lbv, bool const recursive) const
1564 // Super UGLY! (Lgb)
1565 BufferView * bv = const_cast<BufferView *>(lbv);
1567 if ((cache.find(bv) != cache.end()) && cache[bv]) {
1568 if (recursive && the_locking_inset)
1569 return the_locking_inset->getLyXText(bv);
1572 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1575 if (the_locking_inset) {
1576 lt->SetCursor(bv, inset_par, inset_pos, true, inset_boundary);
1578 return the_locking_inset->getLyXText(bv);
1584 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1586 if ((cache.find(bv) == cache.end()) || !cache[bv])
1591 /// then remove all LyXText in text-insets
1592 LyXParagraph * p = par;
1593 for (;p;p = p->next) {
1594 p->deleteInsetsLyXText(bv);
1600 void InsetText::resizeLyXText(BufferView * bv) const
1602 if (!par->next && !par->size()) // resize not neccessary!
1604 if ((cache.find(bv) == cache.end()) || !cache[bv])
1607 LyXParagraph * lpar = 0;
1608 LyXParagraph * selstartpar = 0;
1609 LyXParagraph * selendpar = 0;
1610 LyXParagraph::size_type pos = 0;
1611 LyXParagraph::size_type selstartpos = 0;
1612 LyXParagraph::size_type selendpos = 0;
1613 bool boundary = false;
1614 bool selstartboundary = false;
1615 bool selendboundary = false;
1619 // ProhibitInput(bv);
1622 lpar = TEXT(bv)->cursor.par();
1623 pos = TEXT(bv)->cursor.pos();
1624 boundary = TEXT(bv)->cursor.boundary();
1625 selstartpar = TEXT(bv)->sel_start_cursor.par();
1626 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1627 selstartboundary = TEXT(bv)->sel_start_cursor.boundary();
1628 selendpar = TEXT(bv)->sel_end_cursor.par();
1629 selendpos = TEXT(bv)->sel_end_cursor.pos();
1630 selendboundary = TEXT(bv)->sel_end_cursor.boundary();
1631 selection = TEXT(bv)->selection;
1632 mark_set = TEXT(bv)->mark_set;
1634 deleteLyXText(bv, (the_locking_inset == 0));
1637 TEXT(bv)->selection = true;
1638 /* at this point just to avoid the Delete-Empty-Paragraph
1639 * Mechanism when setting the cursor */
1640 TEXT(bv)->mark_set = mark_set;
1642 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos,true,
1644 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1645 TEXT(bv)->SetCursor(bv, selendpar, selendpos, true, selendboundary);
1646 TEXT(bv)->SetSelection(bv);
1647 TEXT(bv)->SetCursor(bv, lpar, pos);
1649 TEXT(bv)->SetCursor(bv, lpar, pos, true, boundary);
1650 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1651 TEXT(bv)->selection = false;
1655 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1656 // this will scroll the screen such that the cursor becomes visible
1657 bv->updateScrollbar();
1659 if (the_locking_inset) {
1660 /// then resize all LyXText in text-insets
1661 inset_x = cx(bv) - top_x + drawTextXOffset;
1662 inset_y = cy(bv) + drawTextYOffset;
1663 for (LyXParagraph * p = par; p; p = p->next) {
1664 p->resizeInsetsLyXText(bv);
1671 void InsetText::removeNewlines()
1673 for (LyXParagraph * p = par; p; p = p->next) {
1674 for (int i = 0; i < p->Last(); ++i) {
1675 if (p->GetChar(i) == LyXParagraph::META_NEWLINE)