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;
95 cursor_visible = false;
102 autoBreakRows = false;
106 SetParagraphData(ins->par);
107 autoBreakRows = ins->autoBreakRows;
108 drawFrame_ = ins->drawFrame_;
110 par->SetInsetOwner(this);
111 frame_color = LColor::insetframe;
114 last_drawn_width = -1;
115 frame_is_visible = false;
119 InsetText::~InsetText()
121 // delete all instances of LyXText before deleting the paragraps used
123 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
124 delete (*cit).second;
128 LyXParagraph * p = par->next_;
137 LyXParagraph * tmp = par->next();
145 void InsetText::clear()
147 // delete all instances of LyXText before deleting the paragraps used
149 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
150 delete (*cit).second;
154 LyXParagraph * p = par->next_;
163 LyXParagraph * tmp = par->next();
168 par = new LyXParagraph;
172 Inset * InsetText::Clone(Buffer const &) const
174 InsetText * t = new InsetText(*this);
179 void InsetText::Write(Buffer const * buf, ostream & os) const
182 WriteParagraphData(buf, os);
186 void InsetText::WriteParagraphData(Buffer const * buf, ostream & os) const
188 par->writeFile(buf, os, buf->params, 0, 0);
192 void InsetText::Read(Buffer const * buf, LyXLex & lex)
196 LyXParagraph * return_par = 0;
197 char depth = 0; // signed or unsigned?
199 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
200 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
202 LyXFont font(LyXFont::ALL_INHERIT);
204 // delete all instances of LyXText before deleting the paragraps used
206 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
207 delete (*cit).second;
212 LyXParagraph * p = par->next_;
221 LyXParagraph * tmp = par->next();
226 par = new LyXParagraph;
229 token = lex.GetString();
232 if (token == "\\end_inset")
234 if (const_cast<Buffer*>(buf)->
235 parseSingleLyXformat2Token(lex, par, return_par,
236 token, pos, depth, font
238 , footnoteflag, footnotekind
242 // the_end read this should NEVER happen
243 lex.printError("\\the_end read in inset! Error in document!");
251 return_par->SetInsetOwner(this);
253 return_par = return_par->next_;
255 return_par = return_par->next();
259 if (token != "\\end_inset") {
260 lex.printError("Missing \\end_inset at this point. "
267 int InsetText::ascent(BufferView * bv, LyXFont const &) const
270 Row * row = TEXT(bv)->GetRowNearY(y_temp);
271 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
276 int InsetText::descent(BufferView * bv, LyXFont const &) const
279 Row * row = TEXT(bv)->GetRowNearY(y_temp);
280 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
281 TEXT_TO_INSET_OFFSET;
286 int InsetText::width(BufferView * bv, LyXFont const &) const
288 insetWidth = max(textWidth(bv),
289 (int)TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET));
294 int InsetText::textWidth(BufferView * bv) const
296 int const w = getMaxWidth(bv, this);
301 void InsetText::draw(BufferView * bv, LyXFont const & f,
302 int baseline, float & x, bool cleared) const
307 Painter & pain = bv->painter();
309 // no draw is necessary !!!
310 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
312 top_baseline = baseline;
314 if (!cleared && (need_update & CLEAR_FRAME))
315 clearFrame(pain, cleared);
322 UpdatableInset::draw(bv, f, baseline, x, cleared);
325 x += (float)scroll();
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|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;
363 // lyxerr << "InsetText::draw[" << this << "](" << need_update << ":" << int(x) << ":" << top_x << ")\n";
365 if (cleared || (last_drawn_width != insetWidth)) {
367 last_drawn_width = insetWidth;
370 top_baseline = baseline;
371 top_y = baseline - ascent(bv, f);
372 last_width = width(bv, f);
373 last_height = ascent(bv, f) + descent(bv, f);
375 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
376 inset_x = cx(bv) - top_x + drawTextXOffset;
377 inset_y = cy(bv) + drawTextYOffset;
379 if (!cleared && (need_update == CURSOR) && !TEXT(bv)->selection) {
380 drawFrame(pain, cleared);
385 x += TEXT_TO_INSET_OFFSET;
387 #warning Jürgen, why is this a block of its own? (Lgb)
388 #warning because you told me to define variables only in local contest (Jug)!
391 Row * row = TEXT(bv)->GetRowNearY(y);
392 int y_offset = baseline - row->ascent_of_text();
393 int ph = pain.paperHeight();
396 while ((row != 0) && ((y+row->height()) <= 0)) {
398 first += row->height();
403 TEXT(bv)->first = first;
407 while ((row != 0) && (yf < ph)) {
408 TEXT(bv)->GetVisibleRow(bv, y+y_offset, int(x), row,
414 } else if (!locked) {
415 if (need_update & CURSOR) {
416 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
417 TEXT(bv)->ClearSelection(bv);
418 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
420 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
423 if (need_update & SELECTION)
424 bv->screen()->ToggleToggle(TEXT(bv), bv, y_offset, int(x));
425 else if (need_update & CURSOR) {
426 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
427 TEXT(bv)->ClearSelection(bv);
428 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
430 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
434 TEXT(bv)->refresh_y = 0;
435 TEXT(bv)->status = LyXText::UNCHANGED;
436 if ((need_update != CURSOR_PAR) &&
437 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked)))
438 drawFrame(pain, cleared);
439 else if (need_update & CLEAR_FRAME)
440 clearFrame(pain, cleared);
441 x += width(bv, f) - TEXT_TO_INSET_OFFSET;
442 if (bv->text->status==LyXText::CHANGED_IN_DRAW) {
444 } else if (need_update != INIT)
449 void InsetText::drawFrame(Painter & pain, bool cleared) const
451 if (!frame_is_visible || cleared) {
452 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
453 insetWidth - 1, insetAscent + insetDescent - 1,
455 frame_is_visible = true;
460 void InsetText::clearFrame(Painter & pain, bool cleared) const
462 if (frame_is_visible) {
464 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
465 insetWidth - 1, insetAscent + insetDescent - 1,
468 frame_is_visible = false;
473 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
479 owner()->update(bv, font, true);
482 if (the_locking_inset) {
483 inset_x = cx(bv) - top_x + drawTextXOffset;
484 inset_y = cy(bv) + drawTextYOffset;
485 the_locking_inset->update(bv, font, reinit);
488 if (need_update == INIT) {
493 int oldw = insetWidth;
494 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
495 if (oldw != insetWidth) {
498 // update(bv, font, reinit);
501 if ((need_update&CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
504 TEXT(bv)->UpdateInset(bv, the_locking_inset);
507 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
511 Row * row = TEXT(bv)->GetRowNearY(y_temp);
512 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
513 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
514 TEXT_TO_INSET_OFFSET;
519 void InsetText::SetUpdateStatus(BufferView * bv, int what) const
522 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
524 else if (TEXT(bv)->status == LyXText::NEED_VERY_LITTLE_REFRESH)
525 need_update |= CURSOR_PAR;
527 // this to not draw a selection when we redraw all of it!
528 if ((need_update & (INIT|FULL)) && (need_update & CURSOR))
529 TEXT(bv)->ClearSelection(bv);
533 void InsetText::UpdateLocal(BufferView * bv, int what, bool mark_dirty)
535 TEXT(bv)->FullRebreak(bv);
536 SetUpdateStatus(bv, what);
537 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
539 bv->updateInset(this, mark_dirty);
540 bv->owner()->showState();
541 if (old_par != cpar(bv)) {
542 bv->owner()->setLayout(cpar(bv)->GetLayout());
548 string const InsetText::EditMessage() const
550 return _("Opened Text Inset");
554 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
556 // par->SetInsetOwner(this);
557 UpdatableInset::Edit(bv, x, y, button);
559 if (!bv->lockInset(this)) {
560 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
564 the_locking_inset = 0;
565 inset_pos = inset_x = inset_y = 0;
566 inset_boundary = false;
569 if (!checkAndActivateInset(bv, x, y, button))
570 TEXT(bv)->SetCursorFromCoordinates(bv, x - drawTextXOffset,
572 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
573 bv->text->FinishUndo();
575 UpdateLocal(bv, CURSOR, false);
577 // If the inset is empty set the language of the current font to the
578 // language to the surronding text.
580 if (par->Last() == 0 && !par->next_)
582 if (par->size() == 0 && !par->next())
585 LyXFont font(LyXFont::ALL_IGNORE);
586 font.setLanguage(bv->getParentLanguage(this));
587 SetFont(bv, font, false);
592 void InsetText::InsetUnlock(BufferView * bv)
594 if (the_locking_inset) {
595 the_locking_inset->InsetUnlock(bv);
596 the_locking_inset = 0;
599 no_selection = false;
601 UpdateLocal(bv, CLEAR_FRAME|CURSOR, false);
603 bv->owner()->setLayout(owner()->getLyXText(bv)
604 ->cursor.par()->GetLayout());
606 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
610 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
612 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
616 if (inset == cpar(bv)->GetInset(cpos(bv))) {
617 lyxerr[Debug::INSETS] << "OK" << endl;
618 the_locking_inset = inset;
619 inset_x = cx(bv) - top_x + drawTextXOffset;
620 inset_y = cy(bv) + drawTextYOffset;
621 inset_pos = cpos(bv);
622 inset_par = cpar(bv);
623 inset_boundary = cboundary(bv);
625 TEXT(bv)->ClearSelection(bv);
626 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
627 TEXT(bv)->UpdateInset(bv, the_locking_inset);
629 UpdateLocal(bv, CURSOR, false);
632 } else if (the_locking_inset && (the_locking_inset == inset)) {
633 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
634 lyxerr[Debug::INSETS] << "OK" << endl;
635 inset_x = cx(bv) - top_x + drawTextXOffset;
636 inset_y = cy(bv) + drawTextYOffset;
638 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
640 } else if (the_locking_inset) {
641 lyxerr[Debug::INSETS] << "MAYBE" << endl;
642 return the_locking_inset->LockInsetInInset(bv, inset);
644 lyxerr[Debug::INSETS] << "NOT OK" << endl;
649 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
652 if (!the_locking_inset)
654 if (the_locking_inset == inset) {
655 the_locking_inset->InsetUnlock(bv);
656 TEXT(bv)->UpdateInset(bv, inset);
657 the_locking_inset = 0;
659 moveRight(bv, false);
660 old_par = 0; // force layout setting
664 UpdateLocal(bv, CURSOR, false);
667 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
671 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
673 if (!the_locking_inset)
675 if (the_locking_inset != inset) {
676 TEXT(bv)->UpdateInset(bv, the_locking_inset);
677 SetUpdateStatus(bv, CURSOR_PAR);
678 return the_locking_inset->UpdateInsetInInset(bv, inset);
680 // UpdateLocal(bv, FULL, false);
681 if (TEXT(bv)->UpdateInset(bv, inset))
682 UpdateLocal(bv, CURSOR_PAR, false);
683 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
684 inset_x = cx(bv) - top_x + drawTextXOffset;
685 inset_y = cy(bv) + drawTextYOffset;
691 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
695 int tmp_x = x - drawTextXOffset;
696 int tmp_y = y + insetAscent - TEXT(bv)->first;
697 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
700 if (the_locking_inset) {
701 if (the_locking_inset == inset) {
702 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
703 no_selection = false;
706 // otherwise unlock the_locking_inset and lock the new inset
707 the_locking_inset->InsetUnlock(bv);
708 inset_x = cx(bv) - top_x + drawTextXOffset;
709 inset_y = cy(bv) + drawTextYOffset;
710 the_locking_inset = static_cast<UpdatableInset*>(inset);
711 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
712 inset->Edit(bv, x - inset_x, y - inset_y, button);
713 if (the_locking_inset)
714 UpdateLocal(bv, CURSOR, false);
715 no_selection = false;
718 // otherwise only unlock the_locking_inset
719 the_locking_inset->InsetUnlock(bv);
720 the_locking_inset = 0;
722 if (bv->theLockingInset()) {
723 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
724 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
725 inset_x = cx(bv) - top_x + drawTextXOffset;
726 inset_y = cy(bv) + drawTextYOffset;
727 inset_pos = cpos(bv);
728 inset_par = cpar(bv);
729 inset_boundary = cboundary(bv);
730 the_locking_inset = uinset;
731 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y,
733 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
734 if (the_locking_inset)
735 UpdateLocal(bv, CURSOR, false);
736 no_selection = false;
740 if (!inset && (button == 2)) {
741 bool paste_internally = false;
742 if ((button == 2) && TEXT(bv)->selection) {
743 LocalDispatch(bv, LFUN_COPY, "");
744 paste_internally = true;
746 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
748 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
749 UpdateLocal(bv, CURSOR, false);
750 bv->owner()->setLayout(cpar(bv)->GetLayout());
752 // Insert primary selection with middle mouse
753 // if there is a local selection in the current buffer,
756 if (paste_internally)
757 LocalDispatch(bv, LFUN_PASTE, "");
759 LocalDispatch(bv, LFUN_PASTESELECTION,
764 no_selection = false;
768 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
770 UpdatableInset * inset = 0;
772 if (the_locking_inset) {
773 the_locking_inset->InsetButtonRelease(bv,
774 x - inset_x, y - inset_y,
777 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
778 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
779 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
780 inset->InsetButtonRelease(bv,
782 y - inset_y, button);
784 inset_x = cx(bv) - top_x + drawTextXOffset;
785 inset_y = cy(bv) + drawTextYOffset;
786 inset->InsetButtonRelease(bv,
788 y - inset_y, button);
790 x - inset_x, y - inset_y, button);
792 UpdateLocal(bv, CURSOR_PAR, false);
795 no_selection = false;
799 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
803 if (the_locking_inset) {
804 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
809 TEXT(bv)->SetCursorFromCoordinates(bv, x - drawTextXOffset,
811 TEXT(bv)->SetSelection(bv);
812 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
813 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
814 UpdateLocal(bv, SELECTION, false);
819 void InsetText::InsetKeyPress(XKeyEvent * xke)
821 if (the_locking_inset) {
822 the_locking_inset->InsetKeyPress(xke);
828 UpdatableInset::RESULT
829 InsetText::LocalDispatch(BufferView * bv,
830 kb_action action, string const & arg)
832 no_selection = false;
833 UpdatableInset::RESULT
834 result= UpdatableInset::LocalDispatch(bv, action, arg);
835 if (result != UNDISPATCHED) {
840 if ((action < 0) && arg.empty())
843 if (the_locking_inset) {
844 result = the_locking_inset->LocalDispatch(bv, action, arg);
845 if (result == DISPATCHED_NOUPDATE)
847 else if (result == DISPATCHED) {
848 UpdateLocal(bv, CURSOR_PAR, false);
850 } else if (result == FINISHED) {
851 bool dispatched = false;
853 case LFUN_UNKNOWN_ACTION:
854 case LFUN_BREAKPARAGRAPH:
856 moveRightIntern(bv, false, false);
859 if (!TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
860 moveRightIntern(bv, false, false);
864 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
865 moveRightIntern(bv, false, false);
871 the_locking_inset = 0;
879 case LFUN_UNKNOWN_ACTION:
880 if (bv->buffer()->isReadonly()) {
881 // setErrorMessage(N_("Document is read only"));
885 /* Automatically delete the currently selected
886 * text and replace it with what is being
887 * typed in now. Depends on lyxrc settings
888 * "auto_region_delete", which defaults to
891 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
893 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
894 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
896 bv->text->cursor.par()->previous(),
897 bv->text->cursor.par()->next()
901 if (lyxrc.auto_region_delete) {
902 if (TEXT(bv)->selection){
903 TEXT(bv)->CutSelection(bv, false);
906 TEXT(bv)->ClearSelection(bv);
907 for (string::size_type i = 0; i < arg.length(); ++i) {
909 if (!math_insert_greek(bv, arg[i])) {
910 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
911 } else if (!the_locking_inset) {
912 (void)moveRight(bv, false);
915 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
919 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
920 UpdateLocal(bv, CURSOR_PAR, true);
921 result=DISPATCHED_NOUPDATE;
923 // --- Cursor Movements -----------------------------------
925 bv->text->FinishUndo();
926 moveRight(bv, false, true);
927 TEXT(bv)->SetSelection(bv);
928 UpdateLocal(bv, SELECTION, false);
931 result = moveRight(bv);
932 bv->text->FinishUndo();
933 UpdateLocal(bv, CURSOR, false);
936 bv->text->FinishUndo();
937 moveLeft(bv, false, true);
938 TEXT(bv)->SetSelection(bv);
939 UpdateLocal(bv, SELECTION, false);
942 bv->text->FinishUndo();
943 result = moveLeft(bv);
944 UpdateLocal(bv, CURSOR, false);
947 bv->text->FinishUndo();
949 TEXT(bv)->SetSelection(bv);
950 UpdateLocal(bv, SELECTION, false);
953 bv->text->FinishUndo();
954 result = moveDown(bv);
955 UpdateLocal(bv, CURSOR, false);
958 bv->text->FinishUndo();
960 TEXT(bv)->SetSelection(bv);
961 UpdateLocal(bv, SELECTION, false);
964 bv->text->FinishUndo();
966 UpdateLocal(bv, CURSOR, false);
969 bv->text->FinishUndo();
970 TEXT(bv)->CursorHome(bv);
971 UpdateLocal(bv, CURSOR, false);
974 TEXT(bv)->CursorEnd(bv);
975 UpdateLocal(bv, CURSOR, false);
978 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
980 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
981 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
983 bv->text->cursor.par()->previous(),
984 bv->text->cursor.par()->next()
987 if (TEXT(bv)->selection)
988 TEXT(bv)->CutSelection(bv);
990 TEXT(bv)->Backspace(bv);
991 UpdateLocal(bv, CURSOR_PAR, true);
994 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
996 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
997 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
999 bv->text->cursor.par()->previous(),
1000 bv->text->cursor.par()->next()
1003 if (TEXT(bv)->selection)
1004 TEXT(bv)->CutSelection(bv);
1006 TEXT(bv)->Delete(bv);
1007 UpdateLocal(bv, CURSOR_PAR, true);
1010 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
1012 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1013 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1015 bv->text->cursor.par()->previous(),
1016 bv->text->cursor.par()->next()
1019 TEXT(bv)->CutSelection(bv);
1020 UpdateLocal(bv, CURSOR_PAR, true);
1023 bv->text->FinishUndo();
1024 TEXT(bv)->CopySelection(bv);
1025 UpdateLocal(bv, CURSOR_PAR, false);
1027 case LFUN_PASTESELECTION:
1029 string clip(bv->workarea()->getClipboard());
1033 if (arg == "paragraph") {
1034 TEXT(bv)->InsertStringB(bv, clip);
1036 TEXT(bv)->InsertStringA(bv, clip);
1038 UpdateLocal(bv, CURSOR_PAR, true);
1042 if (!autoBreakRows) {
1045 if (cap.nrOfParagraphs() > 1) {
1046 WriteAlert(_("Impossible operation"),
1047 _("Cannot include more than one paragraph!"),
1052 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1054 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1055 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1057 bv->text->cursor.par()->previous(),
1058 bv->text->cursor.par()->next()
1061 TEXT(bv)->PasteSelection(bv);
1062 UpdateLocal(bv, CURSOR_PAR, true);
1064 case LFUN_BREAKPARAGRAPH:
1067 TEXT(bv)->BreakParagraph(bv, 0);
1068 UpdateLocal(bv, FULL, true);
1070 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1073 TEXT(bv)->BreakParagraph(bv, 1);
1074 UpdateLocal(bv, FULL, true);
1076 case LFUN_BREAKLINE:
1079 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1081 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1082 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1084 bv->text->cursor.par()->previous(),
1085 bv->text->cursor.par()->next()
1088 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
1089 UpdateLocal(bv, CURSOR_PAR, true);
1092 // do not set layouts on non breakable textinsets
1093 if (autoBreakRows) {
1094 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1096 // Derive layout number from given argument (string)
1097 // and current buffer's textclass (number). */
1098 LyXTextClassList::ClassList::size_type tclass =
1099 bv->buffer()->params.textclass;
1100 std::pair <bool, LyXTextClass::size_type> layout =
1101 textclasslist.NumberOfLayout(tclass, arg);
1103 // If the entry is obsolete, use the new one instead.
1105 string obs = textclasslist.Style(tclass,layout.second).
1108 layout = textclasslist.NumberOfLayout(tclass, obs);
1111 // see if we found the layout number:
1112 if (!layout.first) {
1113 string msg = string(N_("Layout ")) + arg + N_(" not known");
1115 bv->owner()->getMiniBuffer()->Set(msg);
1119 if (cur_layout != layout.second) {
1120 cur_layout = layout.second;
1121 TEXT(bv)->SetLayout(bv, layout.second);
1122 bv->owner()->setLayout(cpar(bv)->GetLayout());
1123 UpdateLocal(bv, CURSOR_PAR, true);
1126 // reset the layout box
1127 bv->owner()->setLayout(cpar(bv)->GetLayout());
1130 case LFUN_PARAGRAPH_SPACING:
1131 // This one is absolutely not working. When fiddling with this
1132 // it also seems to me that the paragraphs inside the insettext
1133 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1135 LyXParagraph * par = TEXT(bv)->cursor.par();
1136 Spacing::Space cur_spacing = par->params.spacing().getSpace();
1137 float cur_value = 1.0;
1138 if (cur_spacing == Spacing::Other) {
1139 cur_value = par->params.spacing().getValue();
1142 std::istringstream istr(arg.c_str());
1145 Spacing::Space new_spacing = cur_spacing;
1146 float new_value = cur_value;
1148 lyxerr << "Missing argument to `paragraph-spacing'"
1150 } else if (tmp == "single") {
1151 new_spacing = Spacing::Single;
1152 } else if (tmp == "onehalf") {
1153 new_spacing = Spacing::Onehalf;
1154 } else if (tmp == "double") {
1155 new_spacing = Spacing::Double;
1156 } else if (tmp == "other") {
1157 new_spacing = Spacing::Other;
1160 lyxerr << "new_value = " << tmpval << endl;
1163 } else if (tmp == "default") {
1164 new_spacing = Spacing::Default;
1166 lyxerr << _("Unknown spacing argument: ")
1169 if (cur_spacing != new_spacing || cur_value != new_value) {
1170 par->params.spacing(Spacing(new_spacing, new_value));
1171 //TEXT(bv)->RedoParagraph(owner->view());
1172 UpdateLocal(bv, CURSOR_PAR, true);
1173 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1179 if (!bv->Dispatch(action, arg))
1180 result = UNDISPATCHED;
1184 /// If the action has deleted all text in the inset, we need to change the
1185 // language to the language to the surronding text.
1187 if (par->Last() == 0 && !par->next_)
1189 if (par->size() == 0 && !par->next())
1192 LyXFont font(LyXFont::ALL_IGNORE);
1193 font.setLanguage(bv->getParentLanguage(this));
1194 SetFont(bv, font, false);
1197 if (result != FINISHED) {
1198 ShowInsetCursor(bv);
1200 bv->unlockInset(this);
1205 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1208 buf->latexParagraphs(os, par, 0, texrow);
1209 return texrow.rows();
1213 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1215 LyXParagraph * p = par;
1216 unsigned int lines = 0;
1220 tmp = buf->asciiParagraph(p, linelen);
1221 lines += countChar(tmp, '\n');
1233 int InsetText::DocBook(Buffer const * buf, ostream & os) const
1235 LyXParagraph * p = par;
1236 unsigned int lines = 0;
1241 buf->SimpleDocBookOnePar(os,tmp,p,desc,0);
1253 void InsetText::Validate(LaTeXFeatures & features) const
1255 LyXParagraph * p = par;
1257 p->validate(features);
1267 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1269 if (textclasslist.Style(buf->params.textclass,
1270 p->GetLayout()).labeltype != LABEL_MANUAL)
1273 return p->BeginningOfMainBody();
1277 void InsetText::GetCursorPos(BufferView * bv,
1278 int & x, int & y) const
1285 unsigned int InsetText::InsetInInsetY()
1287 if (!the_locking_inset)
1290 return (inset_y + the_locking_inset->InsetInInsetY());
1294 void InsetText::ToggleInsetCursor(BufferView * bv)
1296 if (the_locking_inset) {
1297 the_locking_inset->ToggleInsetCursor(bv);
1301 LyXFont const font(TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv)));
1303 int const asc = lyxfont::maxAscent(font);
1304 int const desc = lyxfont::maxDescent(font);
1306 if (isCursorVisible())
1307 bv->hideLockedInsetCursor();
1309 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1310 toggleCursorVisible();
1314 void InsetText::ShowInsetCursor(BufferView * bv, bool show)
1316 if (the_locking_inset) {
1317 the_locking_inset->ShowInsetCursor(bv);
1320 if (!isCursorVisible()) {
1321 LyXFont const font =
1322 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1324 int const asc = lyxfont::maxAscent(font);
1325 int const desc = lyxfont::maxDescent(font);
1327 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1329 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1330 setCursorVisible(true);
1335 void InsetText::HideInsetCursor(BufferView * bv)
1337 if (isCursorVisible()) {
1338 bv->hideLockedInsetCursor();
1339 setCursorVisible(false);
1341 if (the_locking_inset)
1342 the_locking_inset->HideInsetCursor(bv);
1346 UpdatableInset::RESULT
1347 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1349 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1350 return moveLeftIntern(bv, false, activate_inset, selecting);
1352 return moveRightIntern(bv, false, activate_inset, selecting);
1355 UpdatableInset::RESULT
1356 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1358 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1359 return moveRightIntern(bv, true, activate_inset, selecting);
1361 return moveLeftIntern(bv, true, activate_inset, selecting);
1365 UpdatableInset::RESULT
1366 InsetText::moveRightIntern(BufferView * bv, bool behind,
1367 bool activate_inset, bool selecting)
1370 if (!cpar(bv)->next_ && (cpos(bv) >= cpar(bv)->Last()))
1372 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1375 if (activate_inset && checkAndActivateInset(bv, behind))
1377 TEXT(bv)->CursorRight(bv);
1379 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1380 return DISPATCHED_NOUPDATE;
1384 UpdatableInset::RESULT
1385 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1386 bool activate_inset, bool selecting)
1389 if (!cpar(bv)->previous_ && (cpos(bv) <= 0))
1391 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1394 TEXT(bv)->CursorLeft(bv);
1396 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1397 if (activate_inset && checkAndActivateInset(bv, behind))
1399 return DISPATCHED_NOUPDATE;
1403 UpdatableInset::RESULT
1404 InsetText::moveUp(BufferView * bv)
1406 if (!crow(bv)->previous())
1408 TEXT(bv)->CursorUp(bv);
1409 return DISPATCHED_NOUPDATE;
1413 UpdatableInset::RESULT
1414 InsetText::moveDown(BufferView * bv)
1416 if (!crow(bv)->next())
1418 TEXT(bv)->CursorDown(bv);
1419 return DISPATCHED_NOUPDATE;
1423 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1425 if (the_locking_inset) {
1426 if (the_locking_inset->InsertInsetAllowed(inset))
1427 return the_locking_inset->InsertInset(bv, inset);
1430 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1432 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1433 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1435 bv->text->cursor.par()->previous(),
1436 bv->text->cursor.par()->next()
1439 inset->setOwner(this);
1440 HideInsetCursor(bv);
1441 TEXT(bv)->InsertInset(bv, inset);
1443 if ((cpar(bv)->GetChar(cpos(bv)) != LyXParagraph::META_INSET) ||
1444 (cpar(bv)->GetInset(cpos(bv)) != inset))
1445 TEXT(bv)->CursorLeft(bv);
1447 bv->fitCursor(TEXT(bv));
1448 UpdateLocal(bv, CURSOR_PAR|CURSOR, true);
1449 ShowInsetCursor(bv);
1454 UpdatableInset * InsetText::GetLockingInset()
1456 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1460 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1464 if (the_locking_inset)
1465 return the_locking_inset->GetFirstLockingInsetOfType(c);
1470 bool InsetText::ShowInsetDialog(BufferView * bv) const
1472 if (the_locking_inset)
1473 return the_locking_inset->ShowInsetDialog(bv);
1478 std::vector<string> const InsetText::getLabelList() const
1480 std::vector<string> label_list;
1482 LyXParagraph * tpar = par;
1484 LyXParagraph::inset_iterator beg = tpar->inset_iterator_begin();
1485 LyXParagraph::inset_iterator end = tpar->inset_iterator_end();
1486 for (; beg != end; ++beg) {
1487 std::vector<string> const l = (*beg)->getLabelList();
1488 label_list.insert(label_list.end(), l.begin(), l.end());
1490 tpar = tpar->next();
1496 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1498 if (TEXT(bv)->selection) {
1499 bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1501 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1502 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1504 bv->text->cursor.par()->previous(),
1505 bv->text->cursor.par()->next()
1509 TEXT(bv)->SetFont(bv, font, toggleall);
1510 bv->fitCursor(TEXT(bv));
1511 UpdateLocal(bv, CURSOR_PAR, true);
1515 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1517 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1521 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1522 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1524 LyXFont const font =
1525 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1527 x = inset->width(bv, font);
1528 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1531 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1533 //inset_x = cx(bv) - top_x + drawTextXOffset;
1534 //inset_y = cy(bv) + drawTextYOffset;
1535 inset->Edit(bv, x, y, 0);
1536 if (!the_locking_inset)
1538 UpdateLocal(bv, CURSOR, false);
1545 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1550 dummyx = x = x - drawTextXOffset;
1551 dummyy = y + insetAscent;
1552 Inset * inset = bv->checkInsetHit(TEXT(bv), dummyx, dummyy, button);
1559 inset_x = cx(bv) - top_x + drawTextXOffset;
1560 inset_y = cy(bv) + drawTextYOffset;
1561 inset->Edit(bv, x - inset_x, y - inset_y, button);
1562 if (!the_locking_inset)
1564 UpdateLocal(bv, CURSOR, false);
1571 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1573 int w = UpdatableInset::getMaxWidth(bv, inset);
1578 w = w - top_x + owner()->x();
1581 w -= (2 * TEXT_TO_INSET_OFFSET);
1583 // return w - (2*TEXT_TO_INSET_OFFSET);
1587 void InsetText::SetParagraphData(LyXParagraph * p)
1589 // delete all instances of LyXText before deleting the paragraps used
1591 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
1592 delete (*cit).second;
1608 par->SetInsetOwner(this);
1612 np->next(p->Clone());
1613 np->next_->previous(np);
1615 np->SetInsetOwner(this);
1619 LyXParagraph * tmp = par->next();
1625 par->SetInsetOwner(this);
1626 LyXParagraph * np = par;
1629 np->next(p->Clone());
1630 np->next()->previous(np);
1632 np->SetInsetOwner(this);
1639 void InsetText::SetText(string const & data)
1642 LyXFont font(LyXFont::ALL_SANE);
1643 for (unsigned int i=0; i < data.length(); ++i)
1644 par->InsertChar(i, data[i], font);
1648 void InsetText::SetAutoBreakRows(bool flag)
1650 if (flag != autoBreakRows) {
1651 autoBreakRows = flag;
1659 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1661 if (how != drawFrame_) {
1664 UpdateLocal(bv, DRAW_FRAME, false);
1669 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1671 if (frame_color != col) {
1674 UpdateLocal(bv, DRAW_FRAME, false);
1679 int InsetText::cx(BufferView * bv) const
1681 LyXText * text = TEXT(bv);
1682 int x = text->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1683 if (the_locking_inset) {
1684 LyXFont font = text->GetFont(bv->buffer(),
1686 text->cursor.pos());
1687 if (font.isVisibleRightToLeft())
1688 x -= the_locking_inset->width(bv, font);
1694 int InsetText::cy(BufferView * bv) const
1697 return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1701 LyXParagraph::size_type InsetText::cpos(BufferView * bv) const
1703 return TEXT(bv)->cursor.pos();
1707 LyXParagraph * InsetText::cpar(BufferView * bv) const
1709 return TEXT(bv)->cursor.par();
1712 bool InsetText::cboundary(BufferView * bv) const
1714 return TEXT(bv)->cursor.boundary();
1718 Row * InsetText::crow(BufferView * bv) const
1720 return TEXT(bv)->cursor.row();
1724 LyXText * InsetText::getLyXText(BufferView const * lbv,
1725 bool const recursive) const
1727 // Super UGLY! (Lgb)
1728 BufferView * bv = const_cast<BufferView *>(lbv);
1730 if ((cache.find(bv) != cache.end()) && cache[bv]) {
1731 if (recursive && the_locking_inset)
1732 return the_locking_inset->getLyXText(bv);
1735 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1738 if (the_locking_inset) {
1739 lt->SetCursor(bv, inset_par, inset_pos, true, inset_boundary);
1741 return the_locking_inset->getLyXText(bv);
1747 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1749 if ((cache.find(bv) == cache.end()) || !cache[bv])
1754 /// then remove all LyXText in text-insets
1755 LyXParagraph * p = par;
1757 for (; p; p = p->next_) {
1758 p->deleteInsetsLyXText(bv);
1761 for (; p; p = p->next()) {
1762 p->deleteInsetsLyXText(bv);
1769 void InsetText::resizeLyXText(BufferView * bv) const
1772 if (!par->next_ && !par->size()) // resize not neccessary!
1774 if (!par->next() && !par->size()) // resize not neccessary!
1777 if ((cache.find(bv) == cache.end()) || !cache[bv])
1780 LyXParagraph * lpar = 0;
1781 LyXParagraph * selstartpar = 0;
1782 LyXParagraph * selendpar = 0;
1783 LyXParagraph::size_type pos = 0;
1784 LyXParagraph::size_type selstartpos = 0;
1785 LyXParagraph::size_type selendpos = 0;
1786 bool boundary = false;
1787 bool selstartboundary = false;
1788 bool selendboundary = false;
1792 // ProhibitInput(bv);
1795 lpar = TEXT(bv)->cursor.par();
1796 pos = TEXT(bv)->cursor.pos();
1797 boundary = TEXT(bv)->cursor.boundary();
1798 selstartpar = TEXT(bv)->sel_start_cursor.par();
1799 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1800 selstartboundary = TEXT(bv)->sel_start_cursor.boundary();
1801 selendpar = TEXT(bv)->sel_end_cursor.par();
1802 selendpos = TEXT(bv)->sel_end_cursor.pos();
1803 selendboundary = TEXT(bv)->sel_end_cursor.boundary();
1804 selection = TEXT(bv)->selection;
1805 mark_set = TEXT(bv)->mark_set;
1807 deleteLyXText(bv, (the_locking_inset == 0));
1810 TEXT(bv)->selection = true;
1811 /* at this point just to avoid the Delete-Empty-Paragraph
1812 * Mechanism when setting the cursor */
1813 TEXT(bv)->mark_set = mark_set;
1815 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos,
1816 true, selstartboundary);
1817 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1818 TEXT(bv)->SetCursor(bv, selendpar, selendpos,
1819 true, selendboundary);
1820 TEXT(bv)->SetSelection(bv);
1821 TEXT(bv)->SetCursor(bv, lpar, pos);
1823 TEXT(bv)->SetCursor(bv, lpar, pos, true, boundary);
1824 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1825 TEXT(bv)->selection = false;
1829 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1830 // this will scroll the screen such that the cursor becomes visible
1831 bv->updateScrollbar();
1833 if (the_locking_inset) {
1834 /// then resize all LyXText in text-insets
1835 inset_x = cx(bv) - top_x + drawTextXOffset;
1836 inset_y = cy(bv) + drawTextYOffset;
1838 for (LyXParagraph * p = par; p; p = p->next_) {
1839 p->resizeInsetsLyXText(bv);
1842 for (LyXParagraph * p = par; p; p = p->next()) {
1843 p->resizeInsetsLyXText(bv);
1851 void InsetText::removeNewlines()
1854 for (LyXParagraph * p = par; p; p = p->next_) {
1855 for (int i = 0; i < p->Last(); ++i) {
1857 for (LyXParagraph * p = par; p; p = p->next()) {
1858 for (int i = 0; i < p->size(); ++i) {
1860 if (p->GetChar(i) == LyXParagraph::META_NEWLINE)
1866 bool InsetText::nodraw() const
1868 if (the_locking_inset)
1869 return the_locking_inset->nodraw();
1870 return UpdatableInset::nodraw();
1873 int InsetText::scroll(bool recursive) const
1875 int sx = UpdatableInset::scroll(false);
1877 if (recursive && the_locking_inset)
1878 sx += the_locking_inset->scroll(recursive);
1883 bool InsetText::doClearArea() const
1885 return !locked || (need_update & (FULL|INIT));