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;
112 last_drawn_width = -1;
116 InsetText::~InsetText()
118 // delete all instances of LyXText before deleting the paragraps used
120 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
121 delete (*cit).second;
125 LyXParagraph * p = par->next_;
133 LyXParagraph * p = par->next();
144 void InsetText::clear()
146 // delete all instances of LyXText before deleting the paragraps used
148 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
149 delete (*cit).second;
153 LyXParagraph * p = par->next_;
161 LyXParagraph * p = par->next();
169 par = new LyXParagraph();
173 Inset * InsetText::Clone(Buffer const &) const
175 InsetText * t = new InsetText(*this);
180 void InsetText::Write(Buffer const * buf, ostream & os) const
183 WriteParagraphData(buf, os);
187 void InsetText::WriteParagraphData(Buffer const * buf, ostream & os) const
189 par->writeFile(buf, os, buf->params, 0, 0);
193 void InsetText::Read(Buffer const * buf, LyXLex & lex)
197 LyXParagraph * return_par = 0;
198 char depth = 0; // signed or unsigned?
200 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
201 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
203 LyXFont font(LyXFont::ALL_INHERIT);
205 // delete all instances of LyXText before deleting the paragraps used
207 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
208 delete (*cit).second;
213 LyXParagraph * p = par->next_;
221 LyXParagraph * p = par->next();
229 par = new LyXParagraph;
232 token = lex.GetString();
235 if (token == "\\end_inset")
237 if (const_cast<Buffer*>(buf)->
238 parseSingleLyXformat2Token(lex, par, return_par,token, pos, depth,
241 , footnoteflag, footnotekind
245 // the_end read this should NEVER happen
246 lex.printError("\\the_end read in inset! Error in document!");
254 return_par->SetInsetOwner(this);
256 return_par = return_par->next_;
258 return_par = return_par->next();
262 if (token != "\\end_inset") {
263 lex.printError("Missing \\end_inset at this point. "
270 int InsetText::ascent(BufferView * bv, LyXFont const &) const
273 Row * row = TEXT(bv)->GetRowNearY(y_temp);
274 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
279 int InsetText::descent(BufferView * bv, LyXFont const &) const
282 Row * row = TEXT(bv)->GetRowNearY(y_temp);
283 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
284 TEXT_TO_INSET_OFFSET;
289 int InsetText::width(BufferView * bv, LyXFont const &) const
291 insetWidth = max(textWidth(bv->painter()),
292 (int)TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET));
297 int InsetText::textWidth(Painter & pain) const
299 int const w = getMaxWidth(pain, this);
304 void InsetText::draw(BufferView * bv, LyXFont const & f,
305 int baseline, float & x, bool cleared) const
307 Painter & pain = bv->painter();
309 // no draw is necessary !!!
310 if ((drawFrame == LOCKED) && !locked && !par->size()) {
311 if (!cleared && (need_update & CLEAR_FRAME)) {
312 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
314 insetAscent + insetDescent - 1,
318 top_baseline = baseline;
325 UpdatableInset::draw(bv, f, baseline, x, cleared);
327 // update insetWidth and insetHeight with dummy calls
329 (void)descent(bv, f);
332 // if top_x differs we have a rule down and we don't have to clear anything
333 if (!cleared && (top_x == int(x)) &&
334 ((need_update==INIT)||(need_update==FULL)||(top_baseline!=baseline)||
335 (last_drawn_width!=insetWidth)))
338 int h = insetAscent + insetDescent;
339 int ty = baseline - insetAscent;
345 if ((ty + h) > pain.paperHeight())
346 h = pain.paperHeight();
347 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
348 w = pain.paperWidth();
349 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
353 if (!cleared && (need_update == NONE))
356 if (top_x != int(x)) {
359 bv->text->status = LyXText::CHANGED_IN_DRAW;
362 if (cleared || (last_drawn_width != insetWidth)) {
364 last_drawn_width = insetWidth;
367 top_baseline = baseline;
368 top_y = baseline - ascent(bv, f);
369 last_width = width(bv, f);
370 last_height = ascent(bv, f) + descent(bv, f);
372 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
373 inset_x = cx(bv) - top_x + drawTextXOffset;
374 inset_y = cy(bv) + drawTextYOffset;
376 if (!cleared && (need_update == CURSOR) && !TEXT(bv)->selection) {
381 x += TEXT_TO_INSET_OFFSET;
384 Row * row = TEXT(bv)->GetRowNearY(y);
385 int y_offset = baseline - row->ascent_of_text();
386 int ph = pain.paperHeight();
389 while ((row != 0) && ((y+row->height()) <= 0)) {
391 first += row->height();
396 TEXT(bv)->first = first;
397 if (cleared) { // (need_update&FULL) || (need_update&INIT)
400 while ((row != 0) && (yf < ph)) {
401 TEXT(bv)->GetVisibleRow(bv, y+y_offset, int(x), row,
407 } else if (!locked) {
408 if (need_update & CURSOR) {
409 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
410 TEXT(bv)->ClearSelection(bv);
411 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
413 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
416 if (need_update & SELECTION)
417 bv->screen()->ToggleToggle(TEXT(bv), bv, y_offset, int(x));
418 else if (need_update & CURSOR) {
419 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
420 TEXT(bv)->ClearSelection(bv);
421 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
423 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
427 TEXT(bv)->refresh_y = 0;
428 TEXT(bv)->status = LyXText::UNCHANGED;
429 if ((need_update != CURSOR_PAR) &&
430 ((drawFrame == ALWAYS) || ((drawFrame == LOCKED) && locked)))
432 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
433 width(bv, f) - 1, insetAscent + insetDescent - 1,
435 } else if (need_update & CLEAR_FRAME) {
436 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
437 width(bv, f) - 1, insetAscent + insetDescent - 1,
440 x += width(bv, f) - TEXT_TO_INSET_OFFSET;
441 if (bv->text->status==LyXText::CHANGED_IN_DRAW) {
443 } else if (need_update != INIT)
448 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
450 if (reinit) { // && (need_update != CURSOR)) {
454 owner()->update(bv, font, true);
457 if (the_locking_inset) {
458 inset_x = cx(bv) - top_x + drawTextXOffset;
459 inset_y = cy(bv) + drawTextYOffset;
460 the_locking_inset->update(bv, font, reinit);
462 if (need_update == INIT) {
466 int oldw = insetWidth;
468 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
469 // max(textWidth(bv->painter()),
470 // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
471 // (2 * TEXT_TO_INSET_OFFSET);
473 insetWidth = textWidth(bv->painter());
475 insetWidth = static_cast<int>(TEXT(bv)->width);
477 if (oldw != insetWidth) {
478 // printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
479 // textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
482 update(bv, font, reinit);
485 if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
488 TEXT(bv)->UpdateInset(bv, the_locking_inset);
491 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
495 Row * row = TEXT(bv)->GetRowNearY(y_temp);
496 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
497 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
498 TEXT_TO_INSET_OFFSET;
501 void InsetText::SetUpdateStatus(BufferView * bv, int what)
504 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
506 else if (TEXT(bv)->status == LyXText::NEED_VERY_LITTLE_REFRESH)
507 need_update |= CURSOR_PAR;
509 // this to not draw a selection when we redraw all of it!
510 if ((need_update & (INIT|FULL)) && (need_update & CURSOR))
511 TEXT(bv)->ClearSelection(bv);
514 void InsetText::UpdateLocal(BufferView * bv, int what, bool mark_dirty)
516 TEXT(bv)->FullRebreak(bv);
517 SetUpdateStatus(bv, what);
518 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
520 bv->updateInset(this, mark_dirty);
521 bv->owner()->showState();
522 if (old_par != cpar(bv)) {
523 bv->owner()->setLayout(cpar(bv)->GetLayout());
529 string const InsetText::EditMessage() const
531 return _("Opened Text Inset");
535 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
537 // par->SetInsetOwner(this);
538 UpdatableInset::Edit(bv, x, y, button);
540 if (!bv->lockInset(this)) {
541 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
545 the_locking_inset = 0;
546 inset_pos = inset_x = inset_y = 0;
547 inset_boundary = false;
550 if (!checkAndActivateInset(bv, x, y, button))
551 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
553 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
554 bv->text->FinishUndo();
556 UpdateLocal(bv, FULL, false);
558 // If the inset is empty set the language of the current font to the
559 // language to the surronding text.
561 if (par->Last() == 0 && !par->next_) {
563 if (par->size() == 0 && !par->next()) {
565 LyXFont font(LyXFont::ALL_IGNORE);
566 font.setLanguage(bv->getParentLanguage(this));
567 SetFont(bv, font, false);
572 void InsetText::InsetUnlock(BufferView * bv)
574 if (the_locking_inset) {
575 the_locking_inset->InsetUnlock(bv);
576 the_locking_inset = 0;
579 no_selection = false;
581 UpdateLocal(bv, CLEAR_FRAME|CURSOR, false);
583 bv->owner()->setLayout(owner()->getLyXText(bv)
584 ->cursor.par()->GetLayout());
586 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
590 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
592 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
595 if (inset == cpar(bv)->GetInset(cpos(bv))) {
596 lyxerr[Debug::INSETS] << "OK" << endl;
597 the_locking_inset = inset;
598 inset_x = cx(bv) - top_x + drawTextXOffset;
599 inset_y = cy(bv) + drawTextYOffset;
600 inset_pos = cpos(bv);
601 inset_par = cpar(bv);
602 inset_boundary = cboundary(bv);
604 TEXT(bv)->ClearSelection(bv);
605 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
606 TEXT(bv)->UpdateInset(bv, the_locking_inset);
608 UpdateLocal(bv, CURSOR, false);
611 } else if (the_locking_inset && (the_locking_inset == inset)) {
612 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
613 lyxerr[Debug::INSETS] << "OK" << endl;
614 inset_x = cx(bv) - top_x + drawTextXOffset;
615 inset_y = cy(bv) + drawTextYOffset;
617 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
619 } else if (the_locking_inset) {
620 lyxerr[Debug::INSETS] << "MAYBE" << endl;
621 return the_locking_inset->LockInsetInInset(bv, inset);
623 lyxerr[Debug::INSETS] << "NOT OK" << endl;
628 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
631 if (!the_locking_inset)
633 if (the_locking_inset == inset) {
634 the_locking_inset->InsetUnlock(bv);
635 TEXT(bv)->UpdateInset(bv, inset);
636 the_locking_inset = 0;
638 moveRight(bv, false);
639 old_par = 0; // force layout setting
640 UpdateLocal(bv, CURSOR_PAR, false);
643 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
647 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
649 if (!the_locking_inset)
651 if (the_locking_inset != inset) {
652 TEXT(bv)->UpdateInset(bv, the_locking_inset);
653 SetUpdateStatus(bv, CURSOR_PAR);
654 return the_locking_inset->UpdateInsetInInset(bv, inset);
656 // UpdateLocal(bv, FULL, false);
657 if (TEXT(bv)->UpdateInset(bv, inset))
658 UpdateLocal(bv, CURSOR_PAR, false);
659 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
660 inset_x = cx(bv) - top_x + drawTextXOffset;
661 inset_y = cy(bv) + drawTextYOffset;
667 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
671 int tmp_x = x - drawTextXOffset;
672 int tmp_y = y + insetAscent - TEXT(bv)->first;
673 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
676 if (the_locking_inset) {
677 if (the_locking_inset == inset) {
678 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
679 no_selection = false;
682 // otherwise unlock the_locking_inset and lock the new inset
683 the_locking_inset->InsetUnlock(bv);
684 inset_x = cx(bv) - top_x + drawTextXOffset;
685 inset_y = cy(bv) + drawTextYOffset;
686 the_locking_inset = static_cast<UpdatableInset*>(inset);
687 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
688 inset->Edit(bv, x - inset_x, y - inset_y, button);
689 if (the_locking_inset)
690 UpdateLocal(bv, CURSOR, false);
691 no_selection = false;
694 // otherwise only unlock the_locking_inset
695 the_locking_inset->InsetUnlock(bv);
696 the_locking_inset = 0;
698 if (bv->theLockingInset()) {
699 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
700 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
701 inset_x = cx(bv) - top_x + drawTextXOffset;
702 inset_y = cy(bv) + drawTextYOffset;
703 inset_pos = cpos(bv);
704 inset_par = cpar(bv);
705 inset_boundary = cboundary(bv);
706 the_locking_inset = uinset;
707 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
708 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
709 if (the_locking_inset)
710 UpdateLocal(bv, CURSOR, false);
711 no_selection = false;
715 if (!inset && (button == 2)) {
716 bool paste_internally = false;
717 if ((button == 2) && TEXT(bv)->selection) {
718 LocalDispatch(bv, LFUN_COPY, "");
719 paste_internally = true;
721 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
723 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
724 UpdateLocal(bv, CURSOR, false);
725 bv->owner()->setLayout(cpar(bv)->GetLayout());
727 // Insert primary selection with middle mouse
728 // if there is a local selection in the current buffer,
731 if (paste_internally)
732 LocalDispatch(bv, LFUN_PASTE, "");
734 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
738 no_selection = false;
742 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
744 UpdatableInset * inset = 0;
746 if (the_locking_inset) {
747 the_locking_inset->InsetButtonRelease(bv,
748 x - inset_x, y - inset_y,
751 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
752 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
753 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
754 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
756 inset_x = cx(bv) - top_x + drawTextXOffset;
757 inset_y = cy(bv) + drawTextYOffset;
758 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
759 inset->Edit(bv, x - inset_x, y - inset_y, button);
761 UpdateLocal(bv, CURSOR_PAR, false);
764 no_selection = false;
768 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
772 if (the_locking_inset) {
773 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
778 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
780 TEXT(bv)->SetSelection(bv);
781 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
782 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
783 UpdateLocal(bv, SELECTION, false);
788 void InsetText::InsetKeyPress(XKeyEvent * xke)
790 if (the_locking_inset) {
791 the_locking_inset->InsetKeyPress(xke);
797 UpdatableInset::RESULT
798 InsetText::LocalDispatch(BufferView * bv,
799 kb_action action, string const & arg)
801 no_selection = false;
802 UpdatableInset::RESULT
803 result= UpdatableInset::LocalDispatch(bv, action, arg);
804 if (result != UNDISPATCHED) {
809 if ((action < 0) && arg.empty())
812 if (the_locking_inset) {
813 result = the_locking_inset->LocalDispatch(bv, action, arg);
814 if (result == DISPATCHED_NOUPDATE)
816 else if (result == DISPATCHED) {
817 UpdateLocal(bv, CURSOR_PAR, false);
819 } else if (result == FINISHED) {
820 bool dispatched = false;
822 case LFUN_UNKNOWN_ACTION:
823 case LFUN_BREAKPARAGRAPH:
825 moveRightIntern(bv, false, false);
828 if (!TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
829 moveRightIntern(bv, false, false);
833 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
834 moveRightIntern(bv, false, false);
840 the_locking_inset = 0;
848 case LFUN_UNKNOWN_ACTION:
849 if (bv->buffer()->isReadonly()) {
850 // setErrorMessage(N_("Document is read only"));
854 /* Automatically delete the currently selected
855 * text and replace it with what is being
856 * typed in now. Depends on lyxrc settings
857 * "auto_region_delete", which defaults to
860 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
862 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
863 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
865 bv->text->cursor.par()->previous(),
866 bv->text->cursor.par()->next()
870 if (lyxrc.auto_region_delete) {
871 if (TEXT(bv)->selection){
872 TEXT(bv)->CutSelection(bv, false);
875 TEXT(bv)->ClearSelection(bv);
876 for (string::size_type i = 0; i < arg.length(); ++i) {
878 if (!math_insert_greek(bv, arg[i])) {
879 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
880 } else if (!the_locking_inset) {
881 (void)moveRight(bv, false);
884 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
888 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
889 UpdateLocal(bv, CURSOR_PAR, true);
890 result=DISPATCHED_NOUPDATE;
892 // --- Cursor Movements ---------------------------------------------
894 bv->text->FinishUndo();
895 moveRight(bv, false, true);
896 TEXT(bv)->SetSelection(bv);
897 UpdateLocal(bv, SELECTION, false);
900 result = moveRight(bv);
901 bv->text->FinishUndo();
902 UpdateLocal(bv, CURSOR, false);
905 bv->text->FinishUndo();
906 moveLeft(bv, false, true);
907 TEXT(bv)->SetSelection(bv);
908 UpdateLocal(bv, SELECTION, false);
911 bv->text->FinishUndo();
912 result = moveLeft(bv);
913 UpdateLocal(bv, CURSOR, false);
916 bv->text->FinishUndo();
918 TEXT(bv)->SetSelection(bv);
919 UpdateLocal(bv, SELECTION, false);
922 bv->text->FinishUndo();
923 result = moveDown(bv);
924 UpdateLocal(bv, CURSOR, false);
927 bv->text->FinishUndo();
929 TEXT(bv)->SetSelection(bv);
930 UpdateLocal(bv, SELECTION, false);
933 bv->text->FinishUndo();
935 UpdateLocal(bv, CURSOR, false);
938 bv->text->FinishUndo();
939 TEXT(bv)->CursorHome(bv);
940 UpdateLocal(bv, CURSOR, false);
943 TEXT(bv)->CursorEnd(bv);
944 UpdateLocal(bv, CURSOR, false);
947 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
949 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
950 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
952 bv->text->cursor.par()->previous(),
953 bv->text->cursor.par()->next()
956 if (TEXT(bv)->selection)
957 TEXT(bv)->CutSelection(bv);
959 TEXT(bv)->Backspace(bv);
960 UpdateLocal(bv, CURSOR_PAR, true);
963 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
965 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
966 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
968 bv->text->cursor.par()->previous(),
969 bv->text->cursor.par()->next()
972 if (TEXT(bv)->selection)
973 TEXT(bv)->CutSelection(bv);
975 TEXT(bv)->Delete(bv);
976 UpdateLocal(bv, CURSOR_PAR, true);
979 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
981 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
982 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
984 bv->text->cursor.par()->previous(),
985 bv->text->cursor.par()->next()
988 TEXT(bv)->CutSelection(bv);
989 UpdateLocal(bv, CURSOR_PAR, true);
992 bv->text->FinishUndo();
993 TEXT(bv)->CopySelection(bv);
994 UpdateLocal(bv, CURSOR_PAR, false);
996 case LFUN_PASTESELECTION:
998 string clip(bv->workarea()->getClipboard());
1002 if (arg == "paragraph") {
1003 TEXT(bv)->InsertStringB(bv, clip);
1005 TEXT(bv)->InsertStringA(bv, clip);
1007 UpdateLocal(bv, CURSOR_PAR, true);
1011 if (!autoBreakRows) {
1014 if (cap.nrOfParagraphs() > 1) {
1015 WriteAlert(_("Impossible operation"),
1016 _("Cannot include more than one paragraph!"),
1021 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1023 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1024 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1026 bv->text->cursor.par()->previous(),
1027 bv->text->cursor.par()->next()
1030 TEXT(bv)->PasteSelection(bv);
1031 UpdateLocal(bv, CURSOR_PAR, true);
1033 case LFUN_BREAKPARAGRAPH:
1036 TEXT(bv)->BreakParagraph(bv, 0);
1037 UpdateLocal(bv, FULL, true);
1039 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1042 TEXT(bv)->BreakParagraph(bv, 1);
1043 UpdateLocal(bv, FULL, true);
1045 case LFUN_BREAKLINE:
1048 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1050 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1051 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1053 bv->text->cursor.par()->previous(),
1054 bv->text->cursor.par()->next()
1057 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
1058 UpdateLocal(bv, CURSOR_PAR, true);
1061 // do not set layouts on non breakable textinsets
1062 if (autoBreakRows) {
1063 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1065 // Derive layout number from given argument (string)
1066 // and current buffer's textclass (number). */
1067 LyXTextClassList::ClassList::size_type tclass =
1068 bv->buffer()->params.textclass;
1069 std::pair <bool, LyXTextClass::size_type> layout =
1070 textclasslist.NumberOfLayout(tclass, arg);
1072 // If the entry is obsolete, use the new one instead.
1074 string obs = textclasslist.Style(tclass,layout.second).
1077 layout = textclasslist.NumberOfLayout(tclass, obs);
1080 // see if we found the layout number:
1081 if (!layout.first) {
1082 string msg = string(N_("Layout ")) + arg + N_(" not known");
1084 bv->owner()->getMiniBuffer()->Set(msg);
1088 if (cur_layout != layout.second) {
1089 cur_layout = layout.second;
1090 TEXT(bv)->SetLayout(bv, layout.second);
1091 bv->owner()->setLayout(cpar(bv)->GetLayout());
1092 UpdateLocal(bv, CURSOR_PAR, true);
1095 // reset the layout box
1096 bv->owner()->setLayout(cpar(bv)->GetLayout());
1099 case LFUN_PARAGRAPH_SPACING:
1100 // This one is absolutely not working. When fiddling with this
1101 // it also seems to me that the paragraphs inside the insettext
1102 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1104 LyXParagraph * par = TEXT(bv)->cursor.par();
1105 Spacing::Space cur_spacing = par->params.spacing().getSpace();
1106 float cur_value = 1.0;
1107 if (cur_spacing == Spacing::Other) {
1108 cur_value = par->params.spacing().getValue();
1111 std::istringstream istr(arg.c_str());
1114 Spacing::Space new_spacing = cur_spacing;
1115 float new_value = cur_value;
1117 lyxerr << "Missing argument to `paragraph-spacing'"
1119 } else if (tmp == "single") {
1120 new_spacing = Spacing::Single;
1121 } else if (tmp == "onehalf") {
1122 new_spacing = Spacing::Onehalf;
1123 } else if (tmp == "double") {
1124 new_spacing = Spacing::Double;
1125 } else if (tmp == "other") {
1126 new_spacing = Spacing::Other;
1129 lyxerr << "new_value = " << tmpval << endl;
1132 } else if (tmp == "default") {
1133 new_spacing = Spacing::Default;
1135 lyxerr << _("Unknown spacing argument: ")
1138 if (cur_spacing != new_spacing || cur_value != new_value) {
1139 par->params.spacing(Spacing(new_spacing, new_value));
1140 //TEXT(bv)->RedoParagraph(owner->view());
1141 UpdateLocal(bv, CURSOR_PAR, true);
1142 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1148 if (!bv->Dispatch(action, arg))
1149 result = UNDISPATCHED;
1153 /// If the action has deleted all text in the inset, we need to change the
1154 // language to the language to the surronding text.
1156 if (par->Last() == 0 && !par->next_) {
1158 if (par->size() == 0 && !par->next()) {
1160 LyXFont font(LyXFont::ALL_IGNORE);
1161 font.setLanguage(bv->getParentLanguage(this));
1162 SetFont(bv, font, false);
1165 if (result != FINISHED) {
1166 ShowInsetCursor(bv);
1168 bv->unlockInset(this);
1173 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1176 buf->latexParagraphs(os, par, 0, texrow);
1177 return texrow.rows();
1181 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1183 LyXParagraph * p = par;
1184 unsigned int lines = 0;
1188 tmp = buf->asciiParagraph(p, linelen);
1189 lines += countChar(tmp, '\n');
1201 int InsetText::DocBook(Buffer const * buf, ostream & os) const
1203 LyXParagraph * p = par;
1204 unsigned int lines = 0;
1209 buf->SimpleDocBookOnePar(os,tmp,p,desc,0);
1221 void InsetText::Validate(LaTeXFeatures & features) const
1223 LyXParagraph * p = par;
1225 p->validate(features);
1235 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1237 if (textclasslist.Style(buf->params.textclass,
1238 p->GetLayout()).labeltype != LABEL_MANUAL)
1241 return p->BeginningOfMainBody();
1245 void InsetText::GetCursorPos(BufferView * bv,
1246 int & x, int & y) const
1253 unsigned int InsetText::InsetInInsetY()
1255 if (!the_locking_inset)
1258 return (inset_y + the_locking_inset->InsetInInsetY());
1262 void InsetText::ToggleInsetCursor(BufferView * bv)
1264 if (the_locking_inset) {
1265 the_locking_inset->ToggleInsetCursor(bv);
1269 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1271 int asc = lyxfont::maxAscent(font);
1272 int desc = lyxfont::maxDescent(font);
1275 bv->hideLockedInsetCursor();
1277 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1278 cursor_visible = !cursor_visible;
1282 void InsetText::ShowInsetCursor(BufferView * bv, bool show)
1284 if (the_locking_inset) {
1285 the_locking_inset->ShowInsetCursor(bv);
1288 if (!cursor_visible) {
1289 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1291 int asc = lyxfont::maxAscent(font);
1292 int desc = lyxfont::maxDescent(font);
1294 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1296 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1297 cursor_visible = true;
1302 void InsetText::HideInsetCursor(BufferView * bv)
1304 if (cursor_visible) {
1305 bv->hideLockedInsetCursor();
1306 cursor_visible = false;
1308 if (the_locking_inset)
1309 the_locking_inset->HideInsetCursor(bv);
1313 UpdatableInset::RESULT
1314 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1316 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1317 return moveLeftIntern(bv, false, activate_inset, selecting);
1319 return moveRightIntern(bv, false, activate_inset, selecting);
1322 UpdatableInset::RESULT
1323 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1325 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1326 return moveRightIntern(bv, true, activate_inset, selecting);
1328 return moveLeftIntern(bv, true, activate_inset, selecting);
1332 UpdatableInset::RESULT
1333 InsetText::moveRightIntern(BufferView * bv, bool behind,
1334 bool activate_inset, bool selecting)
1337 if (!cpar(bv)->next_ && (cpos(bv) >= cpar(bv)->Last()))
1339 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1342 if (activate_inset && checkAndActivateInset(bv, behind))
1344 TEXT(bv)->CursorRight(bv);
1346 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1347 return DISPATCHED_NOUPDATE;
1351 UpdatableInset::RESULT
1352 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1353 bool activate_inset, bool selecting)
1356 if (!cpar(bv)->previous_ && (cpos(bv) <= 0))
1358 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1361 TEXT(bv)->CursorLeft(bv);
1363 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1364 if (activate_inset && checkAndActivateInset(bv, behind))
1366 return DISPATCHED_NOUPDATE;
1370 UpdatableInset::RESULT
1371 InsetText::moveUp(BufferView * bv)
1373 if (!crow(bv)->previous())
1375 TEXT(bv)->CursorUp(bv);
1376 return DISPATCHED_NOUPDATE;
1380 UpdatableInset::RESULT
1381 InsetText::moveDown(BufferView * bv)
1383 if (!crow(bv)->next())
1385 TEXT(bv)->CursorDown(bv);
1386 return DISPATCHED_NOUPDATE;
1390 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1392 if (the_locking_inset) {
1393 if (the_locking_inset->InsertInsetAllowed(inset))
1394 return the_locking_inset->InsertInset(bv, inset);
1397 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1399 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1400 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1402 bv->text->cursor.par()->previous(),
1403 bv->text->cursor.par()->next()
1406 inset->setOwner(this);
1407 HideInsetCursor(bv);
1408 TEXT(bv)->InsertInset(bv, inset);
1410 if ((cpar(bv)->GetChar(cpos(bv)) != LyXParagraph::META_INSET) ||
1411 (cpar(bv)->GetInset(cpos(bv)) != inset))
1412 TEXT(bv)->CursorLeft(bv);
1414 bv->fitCursor(TEXT(bv));
1415 UpdateLocal(bv, CURSOR_PAR|CURSOR, true);
1416 ShowInsetCursor(bv);
1421 UpdatableInset * InsetText::GetLockingInset()
1423 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1427 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1431 if (the_locking_inset)
1432 return the_locking_inset->GetFirstLockingInsetOfType(c);
1437 bool InsetText::ShowInsetDialog(BufferView * bv) const
1439 if (the_locking_inset)
1440 return the_locking_inset->ShowInsetDialog(bv);
1445 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1447 if (TEXT(bv)->selection) {
1448 bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1450 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1451 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1453 bv->text->cursor.par()->previous(),
1454 bv->text->cursor.par()->next()
1458 TEXT(bv)->SetFont(bv, font, toggleall);
1459 bv->fitCursor(TEXT(bv));
1460 UpdateLocal(bv, CURSOR_PAR, true);
1464 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1466 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1470 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1471 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1473 LyXFont const font =
1474 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1476 x = inset->width(bv, font);
1477 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1480 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1482 //inset_x = cx(bv) - top_x + drawTextXOffset;
1483 //inset_y = cy(bv) + drawTextYOffset;
1484 inset->Edit(bv, x, y, 0);
1485 if (!the_locking_inset)
1487 UpdateLocal(bv, CURSOR_PAR, false);
1494 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1499 dummyx = x = x - drawTextXOffset;
1500 dummyy = y + insetAscent;
1501 Inset * inset = bv->checkInsetHit(TEXT(bv), dummyx, dummyy, button);
1508 inset_x = cx(bv) - top_x + drawTextXOffset;
1509 inset_y = cy(bv) + drawTextYOffset;
1510 inset->Edit(bv, x - inset_x, y - inset_y, button);
1511 if (!the_locking_inset)
1513 UpdateLocal(bv, CURSOR_PAR, false);
1520 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1522 int w = UpdatableInset::getMaxWidth(pain, inset);
1527 w = w - top_x + owner()->x();
1530 w -= (2 * TEXT_TO_INSET_OFFSET);
1532 // return w - (2*TEXT_TO_INSET_OFFSET);
1536 void InsetText::SetParagraphData(LyXParagraph * p)
1538 // delete all instances of LyXText before deleting the paragraps used
1540 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
1541 delete (*cit).second;
1557 par->SetInsetOwner(this);
1561 np->next(p->Clone());
1562 np->next_->previous(np);
1564 np->SetInsetOwner(this);
1578 par->SetInsetOwner(this);
1582 np->next(p->Clone());
1583 np->next()->previous(np);
1585 np->SetInsetOwner(this);
1592 void InsetText::SetText(string const & data)
1595 LyXFont font(LyXFont::ALL_SANE);
1596 for(unsigned int i=0; i < data.length(); ++i)
1597 par->InsertChar(i, data[i], font);
1601 void InsetText::SetAutoBreakRows(bool flag)
1603 if (flag != autoBreakRows) {
1604 autoBreakRows = flag;
1612 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1614 if (how != drawFrame) {
1617 UpdateLocal(bv, DRAW_FRAME, false);
1622 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1624 if (frame_color != col) {
1627 UpdateLocal(bv, DRAW_FRAME, false);
1632 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1634 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1639 int InsetText::cx(BufferView * bv) const
1641 LyXText * text = TEXT(bv);
1642 int x = text->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1643 if (the_locking_inset) {
1644 LyXFont font = text->GetFont(bv->buffer(),
1645 text->cursor.par(), text->cursor.pos());
1646 if (font.isVisibleRightToLeft())
1647 x -= the_locking_inset->width(bv, font);
1653 int InsetText::cy(BufferView * bv) const
1656 return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1660 LyXParagraph::size_type InsetText::cpos(BufferView * bv) const
1662 return TEXT(bv)->cursor.pos();
1666 LyXParagraph * InsetText::cpar(BufferView * bv) const
1668 return TEXT(bv)->cursor.par();
1671 bool InsetText::cboundary(BufferView * bv) const
1673 return TEXT(bv)->cursor.boundary();
1677 Row * InsetText::crow(BufferView * bv) const
1679 return TEXT(bv)->cursor.row();
1683 LyXText * InsetText::getLyXText(BufferView const * lbv, bool const recursive) const
1685 // Super UGLY! (Lgb)
1686 BufferView * bv = const_cast<BufferView *>(lbv);
1688 if ((cache.find(bv) != cache.end()) && cache[bv]) {
1689 if (recursive && the_locking_inset)
1690 return the_locking_inset->getLyXText(bv);
1693 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1696 if (the_locking_inset) {
1697 lt->SetCursor(bv, inset_par, inset_pos, true, inset_boundary);
1699 return the_locking_inset->getLyXText(bv);
1705 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1707 if ((cache.find(bv) == cache.end()) || !cache[bv])
1712 /// then remove all LyXText in text-insets
1713 LyXParagraph * p = par;
1715 for (; p; p = p->next_) {
1716 p->deleteInsetsLyXText(bv);
1719 for (; p; p = p->next()) {
1720 p->deleteInsetsLyXText(bv);
1727 void InsetText::resizeLyXText(BufferView * bv) const
1730 if (!par->next_ && !par->size()) // resize not neccessary!
1732 if (!par->next() && !par->size()) // resize not neccessary!
1735 if ((cache.find(bv) == cache.end()) || !cache[bv])
1738 LyXParagraph * lpar = 0;
1739 LyXParagraph * selstartpar = 0;
1740 LyXParagraph * selendpar = 0;
1741 LyXParagraph::size_type pos = 0;
1742 LyXParagraph::size_type selstartpos = 0;
1743 LyXParagraph::size_type selendpos = 0;
1744 bool boundary = false;
1745 bool selstartboundary = false;
1746 bool selendboundary = false;
1750 // ProhibitInput(bv);
1753 lpar = TEXT(bv)->cursor.par();
1754 pos = TEXT(bv)->cursor.pos();
1755 boundary = TEXT(bv)->cursor.boundary();
1756 selstartpar = TEXT(bv)->sel_start_cursor.par();
1757 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1758 selstartboundary = TEXT(bv)->sel_start_cursor.boundary();
1759 selendpar = TEXT(bv)->sel_end_cursor.par();
1760 selendpos = TEXT(bv)->sel_end_cursor.pos();
1761 selendboundary = TEXT(bv)->sel_end_cursor.boundary();
1762 selection = TEXT(bv)->selection;
1763 mark_set = TEXT(bv)->mark_set;
1765 deleteLyXText(bv, (the_locking_inset == 0));
1768 TEXT(bv)->selection = true;
1769 /* at this point just to avoid the Delete-Empty-Paragraph
1770 * Mechanism when setting the cursor */
1771 TEXT(bv)->mark_set = mark_set;
1773 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos,true,
1775 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1776 TEXT(bv)->SetCursor(bv, selendpar, selendpos, true, selendboundary);
1777 TEXT(bv)->SetSelection(bv);
1778 TEXT(bv)->SetCursor(bv, lpar, pos);
1780 TEXT(bv)->SetCursor(bv, lpar, pos, true, boundary);
1781 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1782 TEXT(bv)->selection = false;
1786 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1787 // this will scroll the screen such that the cursor becomes visible
1788 bv->updateScrollbar();
1790 if (the_locking_inset) {
1791 /// then resize all LyXText in text-insets
1792 inset_x = cx(bv) - top_x + drawTextXOffset;
1793 inset_y = cy(bv) + drawTextYOffset;
1795 for (LyXParagraph * p = par; p; p = p->next_) {
1796 p->resizeInsetsLyXText(bv);
1799 for (LyXParagraph * p = par; p; p = p->next()) {
1800 p->resizeInsetsLyXText(bv);
1808 void InsetText::removeNewlines()
1811 for (LyXParagraph * p = par; p; p = p->next_) {
1812 for (int i = 0; i < p->Last(); ++i) {
1814 for (LyXParagraph * p = par; p; p = p->next()) {
1815 for (int i = 0; i < p->size(); ++i) {
1817 if (p->GetChar(i) == LyXParagraph::META_NEWLINE)