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),
292 (int)TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET));
297 int InsetText::textWidth(BufferView * bv) const
299 int const w = getMaxWidth(bv, 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);
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())
566 LyXFont font(LyXFont::ALL_IGNORE);
567 font.setLanguage(bv->getParentLanguage(this));
568 SetFont(bv, font, false);
573 void InsetText::InsetUnlock(BufferView * bv)
575 if (the_locking_inset) {
576 the_locking_inset->InsetUnlock(bv);
577 the_locking_inset = 0;
580 no_selection = false;
582 UpdateLocal(bv, CLEAR_FRAME|CURSOR, false);
584 bv->owner()->setLayout(owner()->getLyXText(bv)
585 ->cursor.par()->GetLayout());
587 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
591 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
593 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
596 if (inset == cpar(bv)->GetInset(cpos(bv))) {
597 lyxerr[Debug::INSETS] << "OK" << endl;
598 the_locking_inset = inset;
599 inset_x = cx(bv) - top_x + drawTextXOffset;
600 inset_y = cy(bv) + drawTextYOffset;
601 inset_pos = cpos(bv);
602 inset_par = cpar(bv);
603 inset_boundary = cboundary(bv);
605 TEXT(bv)->ClearSelection(bv);
606 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
607 TEXT(bv)->UpdateInset(bv, the_locking_inset);
609 UpdateLocal(bv, CURSOR, false);
612 } else if (the_locking_inset && (the_locking_inset == inset)) {
613 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
614 lyxerr[Debug::INSETS] << "OK" << endl;
615 inset_x = cx(bv) - top_x + drawTextXOffset;
616 inset_y = cy(bv) + drawTextYOffset;
618 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
620 } else if (the_locking_inset) {
621 lyxerr[Debug::INSETS] << "MAYBE" << endl;
622 return the_locking_inset->LockInsetInInset(bv, inset);
624 lyxerr[Debug::INSETS] << "NOT OK" << endl;
629 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
632 if (!the_locking_inset)
634 if (the_locking_inset == inset) {
635 the_locking_inset->InsetUnlock(bv);
636 TEXT(bv)->UpdateInset(bv, inset);
637 the_locking_inset = 0;
639 moveRight(bv, false);
640 old_par = 0; // force layout setting
641 UpdateLocal(bv, CURSOR_PAR, false);
644 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
648 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
650 if (!the_locking_inset)
652 if (the_locking_inset != inset) {
653 TEXT(bv)->UpdateInset(bv, the_locking_inset);
654 SetUpdateStatus(bv, CURSOR_PAR);
655 return the_locking_inset->UpdateInsetInInset(bv, inset);
657 // UpdateLocal(bv, FULL, false);
658 if (TEXT(bv)->UpdateInset(bv, inset))
659 UpdateLocal(bv, CURSOR_PAR, false);
660 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
661 inset_x = cx(bv) - top_x + drawTextXOffset;
662 inset_y = cy(bv) + drawTextYOffset;
668 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
672 int tmp_x = x - drawTextXOffset;
673 int tmp_y = y + insetAscent - TEXT(bv)->first;
674 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
677 if (the_locking_inset) {
678 if (the_locking_inset == inset) {
679 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
680 no_selection = false;
683 // otherwise unlock the_locking_inset and lock the new inset
684 the_locking_inset->InsetUnlock(bv);
685 inset_x = cx(bv) - top_x + drawTextXOffset;
686 inset_y = cy(bv) + drawTextYOffset;
687 the_locking_inset = static_cast<UpdatableInset*>(inset);
688 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
689 inset->Edit(bv, x - inset_x, y - inset_y, button);
690 if (the_locking_inset)
691 UpdateLocal(bv, CURSOR, false);
692 no_selection = false;
695 // otherwise only unlock the_locking_inset
696 the_locking_inset->InsetUnlock(bv);
697 the_locking_inset = 0;
699 if (bv->theLockingInset()) {
700 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
701 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
702 inset_x = cx(bv) - top_x + drawTextXOffset;
703 inset_y = cy(bv) + drawTextYOffset;
704 inset_pos = cpos(bv);
705 inset_par = cpar(bv);
706 inset_boundary = cboundary(bv);
707 the_locking_inset = uinset;
708 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
709 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
710 if (the_locking_inset)
711 UpdateLocal(bv, CURSOR, false);
712 no_selection = false;
716 if (!inset && (button == 2)) {
717 bool paste_internally = false;
718 if ((button == 2) && TEXT(bv)->selection) {
719 LocalDispatch(bv, LFUN_COPY, "");
720 paste_internally = true;
722 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
724 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
725 UpdateLocal(bv, CURSOR, false);
726 bv->owner()->setLayout(cpar(bv)->GetLayout());
728 // Insert primary selection with middle mouse
729 // if there is a local selection in the current buffer,
732 if (paste_internally)
733 LocalDispatch(bv, LFUN_PASTE, "");
735 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
739 no_selection = false;
743 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
745 UpdatableInset * inset = 0;
747 if (the_locking_inset) {
748 the_locking_inset->InsetButtonRelease(bv,
749 x - inset_x, y - inset_y,
752 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
753 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
754 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
755 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
757 inset_x = cx(bv) - top_x + drawTextXOffset;
758 inset_y = cy(bv) + drawTextYOffset;
759 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
760 inset->Edit(bv, x - inset_x, y - inset_y, button);
762 UpdateLocal(bv, CURSOR_PAR, false);
765 no_selection = false;
769 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
773 if (the_locking_inset) {
774 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
779 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
781 TEXT(bv)->SetSelection(bv);
782 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
783 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
784 UpdateLocal(bv, SELECTION, false);
789 void InsetText::InsetKeyPress(XKeyEvent * xke)
791 if (the_locking_inset) {
792 the_locking_inset->InsetKeyPress(xke);
798 UpdatableInset::RESULT
799 InsetText::LocalDispatch(BufferView * bv,
800 kb_action action, string const & arg)
802 no_selection = false;
803 UpdatableInset::RESULT
804 result= UpdatableInset::LocalDispatch(bv, action, arg);
805 if (result != UNDISPATCHED) {
810 if ((action < 0) && arg.empty())
813 if (the_locking_inset) {
814 result = the_locking_inset->LocalDispatch(bv, action, arg);
815 if (result == DISPATCHED_NOUPDATE)
817 else if (result == DISPATCHED) {
818 UpdateLocal(bv, CURSOR_PAR, false);
820 } else if (result == FINISHED) {
821 bool dispatched = false;
823 case LFUN_UNKNOWN_ACTION:
824 case LFUN_BREAKPARAGRAPH:
826 moveRightIntern(bv, false, false);
829 if (!TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
830 moveRightIntern(bv, false, false);
834 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
835 moveRightIntern(bv, false, false);
841 the_locking_inset = 0;
849 case LFUN_UNKNOWN_ACTION:
850 if (bv->buffer()->isReadonly()) {
851 // setErrorMessage(N_("Document is read only"));
855 /* Automatically delete the currently selected
856 * text and replace it with what is being
857 * typed in now. Depends on lyxrc settings
858 * "auto_region_delete", which defaults to
861 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
863 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
864 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
866 bv->text->cursor.par()->previous(),
867 bv->text->cursor.par()->next()
871 if (lyxrc.auto_region_delete) {
872 if (TEXT(bv)->selection){
873 TEXT(bv)->CutSelection(bv, false);
876 TEXT(bv)->ClearSelection(bv);
877 for (string::size_type i = 0; i < arg.length(); ++i) {
879 if (!math_insert_greek(bv, arg[i])) {
880 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
881 } else if (!the_locking_inset) {
882 (void)moveRight(bv, false);
885 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
889 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
890 UpdateLocal(bv, CURSOR_PAR, true);
891 result=DISPATCHED_NOUPDATE;
893 // --- Cursor Movements ---------------------------------------------
895 bv->text->FinishUndo();
896 moveRight(bv, false, true);
897 TEXT(bv)->SetSelection(bv);
898 UpdateLocal(bv, SELECTION, false);
901 result = moveRight(bv);
902 bv->text->FinishUndo();
903 UpdateLocal(bv, CURSOR, false);
906 bv->text->FinishUndo();
907 moveLeft(bv, false, true);
908 TEXT(bv)->SetSelection(bv);
909 UpdateLocal(bv, SELECTION, false);
912 bv->text->FinishUndo();
913 result = moveLeft(bv);
914 UpdateLocal(bv, CURSOR, false);
917 bv->text->FinishUndo();
919 TEXT(bv)->SetSelection(bv);
920 UpdateLocal(bv, SELECTION, false);
923 bv->text->FinishUndo();
924 result = moveDown(bv);
925 UpdateLocal(bv, CURSOR, false);
928 bv->text->FinishUndo();
930 TEXT(bv)->SetSelection(bv);
931 UpdateLocal(bv, SELECTION, false);
934 bv->text->FinishUndo();
936 UpdateLocal(bv, CURSOR, false);
939 bv->text->FinishUndo();
940 TEXT(bv)->CursorHome(bv);
941 UpdateLocal(bv, CURSOR, false);
944 TEXT(bv)->CursorEnd(bv);
945 UpdateLocal(bv, CURSOR, false);
948 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
950 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
951 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
953 bv->text->cursor.par()->previous(),
954 bv->text->cursor.par()->next()
957 if (TEXT(bv)->selection)
958 TEXT(bv)->CutSelection(bv);
960 TEXT(bv)->Backspace(bv);
961 UpdateLocal(bv, CURSOR_PAR, true);
964 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
966 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
967 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
969 bv->text->cursor.par()->previous(),
970 bv->text->cursor.par()->next()
973 if (TEXT(bv)->selection)
974 TEXT(bv)->CutSelection(bv);
976 TEXT(bv)->Delete(bv);
977 UpdateLocal(bv, CURSOR_PAR, true);
980 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
982 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
983 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
985 bv->text->cursor.par()->previous(),
986 bv->text->cursor.par()->next()
989 TEXT(bv)->CutSelection(bv);
990 UpdateLocal(bv, CURSOR_PAR, true);
993 bv->text->FinishUndo();
994 TEXT(bv)->CopySelection(bv);
995 UpdateLocal(bv, CURSOR_PAR, false);
997 case LFUN_PASTESELECTION:
999 string clip(bv->workarea()->getClipboard());
1003 if (arg == "paragraph") {
1004 TEXT(bv)->InsertStringB(bv, clip);
1006 TEXT(bv)->InsertStringA(bv, clip);
1008 UpdateLocal(bv, CURSOR_PAR, true);
1012 if (!autoBreakRows) {
1015 if (cap.nrOfParagraphs() > 1) {
1016 WriteAlert(_("Impossible operation"),
1017 _("Cannot include more than one paragraph!"),
1022 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1024 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1025 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1027 bv->text->cursor.par()->previous(),
1028 bv->text->cursor.par()->next()
1031 TEXT(bv)->PasteSelection(bv);
1032 UpdateLocal(bv, CURSOR_PAR, true);
1034 case LFUN_BREAKPARAGRAPH:
1037 TEXT(bv)->BreakParagraph(bv, 0);
1038 UpdateLocal(bv, FULL, true);
1040 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1043 TEXT(bv)->BreakParagraph(bv, 1);
1044 UpdateLocal(bv, FULL, true);
1046 case LFUN_BREAKLINE:
1049 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1051 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1052 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1054 bv->text->cursor.par()->previous(),
1055 bv->text->cursor.par()->next()
1058 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
1059 UpdateLocal(bv, CURSOR_PAR, true);
1062 // do not set layouts on non breakable textinsets
1063 if (autoBreakRows) {
1064 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1066 // Derive layout number from given argument (string)
1067 // and current buffer's textclass (number). */
1068 LyXTextClassList::ClassList::size_type tclass =
1069 bv->buffer()->params.textclass;
1070 std::pair <bool, LyXTextClass::size_type> layout =
1071 textclasslist.NumberOfLayout(tclass, arg);
1073 // If the entry is obsolete, use the new one instead.
1075 string obs = textclasslist.Style(tclass,layout.second).
1078 layout = textclasslist.NumberOfLayout(tclass, obs);
1081 // see if we found the layout number:
1082 if (!layout.first) {
1083 string msg = string(N_("Layout ")) + arg + N_(" not known");
1085 bv->owner()->getMiniBuffer()->Set(msg);
1089 if (cur_layout != layout.second) {
1090 cur_layout = layout.second;
1091 TEXT(bv)->SetLayout(bv, layout.second);
1092 bv->owner()->setLayout(cpar(bv)->GetLayout());
1093 UpdateLocal(bv, CURSOR_PAR, true);
1096 // reset the layout box
1097 bv->owner()->setLayout(cpar(bv)->GetLayout());
1100 case LFUN_PARAGRAPH_SPACING:
1101 // This one is absolutely not working. When fiddling with this
1102 // it also seems to me that the paragraphs inside the insettext
1103 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1105 LyXParagraph * par = TEXT(bv)->cursor.par();
1106 Spacing::Space cur_spacing = par->params.spacing().getSpace();
1107 float cur_value = 1.0;
1108 if (cur_spacing == Spacing::Other) {
1109 cur_value = par->params.spacing().getValue();
1112 std::istringstream istr(arg.c_str());
1115 Spacing::Space new_spacing = cur_spacing;
1116 float new_value = cur_value;
1118 lyxerr << "Missing argument to `paragraph-spacing'"
1120 } else if (tmp == "single") {
1121 new_spacing = Spacing::Single;
1122 } else if (tmp == "onehalf") {
1123 new_spacing = Spacing::Onehalf;
1124 } else if (tmp == "double") {
1125 new_spacing = Spacing::Double;
1126 } else if (tmp == "other") {
1127 new_spacing = Spacing::Other;
1130 lyxerr << "new_value = " << tmpval << endl;
1133 } else if (tmp == "default") {
1134 new_spacing = Spacing::Default;
1136 lyxerr << _("Unknown spacing argument: ")
1139 if (cur_spacing != new_spacing || cur_value != new_value) {
1140 par->params.spacing(Spacing(new_spacing, new_value));
1141 //TEXT(bv)->RedoParagraph(owner->view());
1142 UpdateLocal(bv, CURSOR_PAR, true);
1143 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1149 if (!bv->Dispatch(action, arg))
1150 result = UNDISPATCHED;
1154 /// If the action has deleted all text in the inset, we need to change the
1155 // language to the language to the surronding text.
1157 if (par->Last() == 0 && !par->next_)
1159 if (par->size() == 0 && !par->next())
1162 LyXFont font(LyXFont::ALL_IGNORE);
1163 font.setLanguage(bv->getParentLanguage(this));
1164 SetFont(bv, font, false);
1167 if (result != FINISHED) {
1168 ShowInsetCursor(bv);
1170 bv->unlockInset(this);
1175 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1178 buf->latexParagraphs(os, par, 0, texrow);
1179 return texrow.rows();
1183 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1185 LyXParagraph * p = par;
1186 unsigned int lines = 0;
1190 tmp = buf->asciiParagraph(p, linelen);
1191 lines += countChar(tmp, '\n');
1203 int InsetText::DocBook(Buffer const * buf, ostream & os) const
1205 LyXParagraph * p = par;
1206 unsigned int lines = 0;
1211 buf->SimpleDocBookOnePar(os,tmp,p,desc,0);
1223 void InsetText::Validate(LaTeXFeatures & features) const
1225 LyXParagraph * p = par;
1227 p->validate(features);
1237 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1239 if (textclasslist.Style(buf->params.textclass,
1240 p->GetLayout()).labeltype != LABEL_MANUAL)
1243 return p->BeginningOfMainBody();
1247 void InsetText::GetCursorPos(BufferView * bv,
1248 int & x, int & y) const
1255 unsigned int InsetText::InsetInInsetY()
1257 if (!the_locking_inset)
1260 return (inset_y + the_locking_inset->InsetInInsetY());
1264 void InsetText::ToggleInsetCursor(BufferView * bv)
1266 if (the_locking_inset) {
1267 the_locking_inset->ToggleInsetCursor(bv);
1271 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1273 int asc = lyxfont::maxAscent(font);
1274 int desc = lyxfont::maxDescent(font);
1277 bv->hideLockedInsetCursor();
1279 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1280 cursor_visible = !cursor_visible;
1284 void InsetText::ShowInsetCursor(BufferView * bv, bool show)
1286 if (the_locking_inset) {
1287 the_locking_inset->ShowInsetCursor(bv);
1290 if (!cursor_visible) {
1291 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1293 int asc = lyxfont::maxAscent(font);
1294 int desc = lyxfont::maxDescent(font);
1296 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1298 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1299 cursor_visible = true;
1304 void InsetText::HideInsetCursor(BufferView * bv)
1306 if (cursor_visible) {
1307 bv->hideLockedInsetCursor();
1308 cursor_visible = false;
1310 if (the_locking_inset)
1311 the_locking_inset->HideInsetCursor(bv);
1315 UpdatableInset::RESULT
1316 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1318 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1319 return moveLeftIntern(bv, false, activate_inset, selecting);
1321 return moveRightIntern(bv, false, activate_inset, selecting);
1324 UpdatableInset::RESULT
1325 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1327 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1328 return moveRightIntern(bv, true, activate_inset, selecting);
1330 return moveLeftIntern(bv, true, activate_inset, selecting);
1334 UpdatableInset::RESULT
1335 InsetText::moveRightIntern(BufferView * bv, bool behind,
1336 bool activate_inset, bool selecting)
1339 if (!cpar(bv)->next_ && (cpos(bv) >= cpar(bv)->Last()))
1341 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1344 if (activate_inset && checkAndActivateInset(bv, behind))
1346 TEXT(bv)->CursorRight(bv);
1348 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1349 return DISPATCHED_NOUPDATE;
1353 UpdatableInset::RESULT
1354 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1355 bool activate_inset, bool selecting)
1358 if (!cpar(bv)->previous_ && (cpos(bv) <= 0))
1360 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1363 TEXT(bv)->CursorLeft(bv);
1365 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1366 if (activate_inset && checkAndActivateInset(bv, behind))
1368 return DISPATCHED_NOUPDATE;
1372 UpdatableInset::RESULT
1373 InsetText::moveUp(BufferView * bv)
1375 if (!crow(bv)->previous())
1377 TEXT(bv)->CursorUp(bv);
1378 return DISPATCHED_NOUPDATE;
1382 UpdatableInset::RESULT
1383 InsetText::moveDown(BufferView * bv)
1385 if (!crow(bv)->next())
1387 TEXT(bv)->CursorDown(bv);
1388 return DISPATCHED_NOUPDATE;
1392 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1394 if (the_locking_inset) {
1395 if (the_locking_inset->InsertInsetAllowed(inset))
1396 return the_locking_inset->InsertInset(bv, inset);
1399 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1401 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1402 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1404 bv->text->cursor.par()->previous(),
1405 bv->text->cursor.par()->next()
1408 inset->setOwner(this);
1409 HideInsetCursor(bv);
1410 TEXT(bv)->InsertInset(bv, inset);
1412 if ((cpar(bv)->GetChar(cpos(bv)) != LyXParagraph::META_INSET) ||
1413 (cpar(bv)->GetInset(cpos(bv)) != inset))
1414 TEXT(bv)->CursorLeft(bv);
1416 bv->fitCursor(TEXT(bv));
1417 UpdateLocal(bv, CURSOR_PAR|CURSOR, true);
1418 ShowInsetCursor(bv);
1423 UpdatableInset * InsetText::GetLockingInset()
1425 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1429 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1433 if (the_locking_inset)
1434 return the_locking_inset->GetFirstLockingInsetOfType(c);
1439 bool InsetText::ShowInsetDialog(BufferView * bv) const
1441 if (the_locking_inset)
1442 return the_locking_inset->ShowInsetDialog(bv);
1447 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1449 if (TEXT(bv)->selection) {
1450 bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1452 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1453 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1455 bv->text->cursor.par()->previous(),
1456 bv->text->cursor.par()->next()
1460 TEXT(bv)->SetFont(bv, font, toggleall);
1461 bv->fitCursor(TEXT(bv));
1462 UpdateLocal(bv, CURSOR_PAR, true);
1466 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1468 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1472 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1473 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1475 LyXFont const font =
1476 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1478 x = inset->width(bv, font);
1479 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1482 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1484 //inset_x = cx(bv) - top_x + drawTextXOffset;
1485 //inset_y = cy(bv) + drawTextYOffset;
1486 inset->Edit(bv, x, y, 0);
1487 if (!the_locking_inset)
1489 UpdateLocal(bv, CURSOR_PAR, false);
1496 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1501 dummyx = x = x - drawTextXOffset;
1502 dummyy = y + insetAscent;
1503 Inset * inset = bv->checkInsetHit(TEXT(bv), dummyx, dummyy, button);
1510 inset_x = cx(bv) - top_x + drawTextXOffset;
1511 inset_y = cy(bv) + drawTextYOffset;
1512 inset->Edit(bv, x - inset_x, y - inset_y, button);
1513 if (!the_locking_inset)
1515 UpdateLocal(bv, CURSOR_PAR, false);
1522 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1524 int w = UpdatableInset::getMaxWidth(bv, inset);
1529 w = w - top_x + owner()->x();
1532 w -= (2 * TEXT_TO_INSET_OFFSET);
1534 // return w - (2*TEXT_TO_INSET_OFFSET);
1538 void InsetText::SetParagraphData(LyXParagraph * p)
1540 // delete all instances of LyXText before deleting the paragraps used
1542 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
1543 delete (*cit).second;
1559 par->SetInsetOwner(this);
1563 np->next(p->Clone());
1564 np->next_->previous(np);
1566 np->SetInsetOwner(this);
1580 par->SetInsetOwner(this);
1584 np->next(p->Clone());
1585 np->next()->previous(np);
1587 np->SetInsetOwner(this);
1594 void InsetText::SetText(string const & data)
1597 LyXFont font(LyXFont::ALL_SANE);
1598 for(unsigned int i=0; i < data.length(); ++i)
1599 par->InsertChar(i, data[i], font);
1603 void InsetText::SetAutoBreakRows(bool flag)
1605 if (flag != autoBreakRows) {
1606 autoBreakRows = flag;
1614 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1616 if (how != drawFrame) {
1619 UpdateLocal(bv, DRAW_FRAME, false);
1624 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1626 if (frame_color != col) {
1629 UpdateLocal(bv, DRAW_FRAME, false);
1634 int InsetText::cx(BufferView * bv) const
1636 LyXText * text = TEXT(bv);
1637 int x = text->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1638 if (the_locking_inset) {
1639 LyXFont font = text->GetFont(bv->buffer(),
1640 text->cursor.par(), text->cursor.pos());
1641 if (font.isVisibleRightToLeft())
1642 x -= the_locking_inset->width(bv, font);
1648 int InsetText::cy(BufferView * bv) const
1651 return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1655 LyXParagraph::size_type InsetText::cpos(BufferView * bv) const
1657 return TEXT(bv)->cursor.pos();
1661 LyXParagraph * InsetText::cpar(BufferView * bv) const
1663 return TEXT(bv)->cursor.par();
1666 bool InsetText::cboundary(BufferView * bv) const
1668 return TEXT(bv)->cursor.boundary();
1672 Row * InsetText::crow(BufferView * bv) const
1674 return TEXT(bv)->cursor.row();
1678 LyXText * InsetText::getLyXText(BufferView const * lbv, bool const recursive) const
1680 // Super UGLY! (Lgb)
1681 BufferView * bv = const_cast<BufferView *>(lbv);
1683 if ((cache.find(bv) != cache.end()) && cache[bv]) {
1684 if (recursive && the_locking_inset)
1685 return the_locking_inset->getLyXText(bv);
1688 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1691 if (the_locking_inset) {
1692 lt->SetCursor(bv, inset_par, inset_pos, true, inset_boundary);
1694 return the_locking_inset->getLyXText(bv);
1700 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1702 if ((cache.find(bv) == cache.end()) || !cache[bv])
1707 /// then remove all LyXText in text-insets
1708 LyXParagraph * p = par;
1710 for (; p; p = p->next_) {
1711 p->deleteInsetsLyXText(bv);
1714 for (; p; p = p->next()) {
1715 p->deleteInsetsLyXText(bv);
1722 void InsetText::resizeLyXText(BufferView * bv) const
1725 if (!par->next_ && !par->size()) // resize not neccessary!
1727 if (!par->next() && !par->size()) // resize not neccessary!
1730 if ((cache.find(bv) == cache.end()) || !cache[bv])
1733 LyXParagraph * lpar = 0;
1734 LyXParagraph * selstartpar = 0;
1735 LyXParagraph * selendpar = 0;
1736 LyXParagraph::size_type pos = 0;
1737 LyXParagraph::size_type selstartpos = 0;
1738 LyXParagraph::size_type selendpos = 0;
1739 bool boundary = false;
1740 bool selstartboundary = false;
1741 bool selendboundary = false;
1745 // ProhibitInput(bv);
1748 lpar = TEXT(bv)->cursor.par();
1749 pos = TEXT(bv)->cursor.pos();
1750 boundary = TEXT(bv)->cursor.boundary();
1751 selstartpar = TEXT(bv)->sel_start_cursor.par();
1752 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1753 selstartboundary = TEXT(bv)->sel_start_cursor.boundary();
1754 selendpar = TEXT(bv)->sel_end_cursor.par();
1755 selendpos = TEXT(bv)->sel_end_cursor.pos();
1756 selendboundary = TEXT(bv)->sel_end_cursor.boundary();
1757 selection = TEXT(bv)->selection;
1758 mark_set = TEXT(bv)->mark_set;
1760 deleteLyXText(bv, (the_locking_inset == 0));
1763 TEXT(bv)->selection = true;
1764 /* at this point just to avoid the Delete-Empty-Paragraph
1765 * Mechanism when setting the cursor */
1766 TEXT(bv)->mark_set = mark_set;
1768 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos,true,
1770 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1771 TEXT(bv)->SetCursor(bv, selendpar, selendpos, true, selendboundary);
1772 TEXT(bv)->SetSelection(bv);
1773 TEXT(bv)->SetCursor(bv, lpar, pos);
1775 TEXT(bv)->SetCursor(bv, lpar, pos, true, boundary);
1776 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1777 TEXT(bv)->selection = false;
1781 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1782 // this will scroll the screen such that the cursor becomes visible
1783 bv->updateScrollbar();
1785 if (the_locking_inset) {
1786 /// then resize all LyXText in text-insets
1787 inset_x = cx(bv) - top_x + drawTextXOffset;
1788 inset_y = cy(bv) + drawTextYOffset;
1790 for (LyXParagraph * p = par; p; p = p->next_) {
1791 p->resizeInsetsLyXText(bv);
1794 for (LyXParagraph * p = par; p; p = p->next()) {
1795 p->resizeInsetsLyXText(bv);
1803 void InsetText::removeNewlines()
1806 for (LyXParagraph * p = par; p; p = p->next_) {
1807 for (int i = 0; i < p->Last(); ++i) {
1809 for (LyXParagraph * p = par; p; p = p->next()) {
1810 for (int i = 0; i < p->size(); ++i) {
1812 if (p->GetChar(i) == LyXParagraph::META_NEWLINE)