2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1998-2000 The LyX Team.
9 * ======================================================
20 #pragma implementation
23 #include "insettext.h"
24 #include "lyxparagraph.h"
28 #include "commandtags.h"
31 #include "BufferView.h"
33 #include "LaTeXFeatures.h"
35 #include "lyx_gui_misc.h"
37 #include "lyxcursor.h"
38 #include "CutAndPaste.h"
40 #include "minibuffer.h"
42 #include "support/textutils.h"
43 #include "support/LAssert.h"
47 #include "trans_mgr.h"
48 #include "lyxscreen.h"
57 extern unsigned char getCurrentTextClass(Buffer *);
58 extern bool math_insert_greek(BufferView *, char);
59 extern int greek_kb_flag;
61 InsetText::InsetText()
63 par = new LyXParagraph();
68 InsetText::InsetText(InsetText const & ins)
73 autoBreakRows = ins.autoBreakRows;
77 InsetText & InsetText::operator=(InsetText const & it)
80 autoBreakRows = it.autoBreakRows;
85 void InsetText::init(InsetText const * ins)
93 the_locking_inset = 0;
94 cursor_visible = false;
100 autoBreakRows = false;
104 SetParagraphData(ins->par);
105 autoBreakRows = ins->autoBreakRows;
106 drawFrame = ins->drawFrame;
108 par->SetInsetOwner(this);
109 frame_color = LColor::insetframe;
115 InsetText::~InsetText()
117 // delete all instances of LyXText before deleting the paragraps used
119 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
120 delete (*cit).second;
123 LyXParagraph * p = par->next;
133 void InsetText::clear()
135 // delete all instances of LyXText before deleting the paragraps used
137 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
138 delete (*cit).second;
141 LyXParagraph * p = par->next;
148 par = new LyXParagraph();
152 Inset * InsetText::Clone(Buffer const &) const
154 InsetText * t = new InsetText(*this);
159 void InsetText::Write(Buffer const * buf, ostream & os) const
162 WriteParagraphData(buf, os);
166 void InsetText::WriteParagraphData(Buffer const * buf, ostream & os) const
168 par->writeFile(buf, os, buf->params, 0, 0);
172 void InsetText::Read(Buffer const * buf, LyXLex & lex)
176 LyXParagraph * return_par = 0;
177 char depth = 0; // signed or unsigned?
179 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
180 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
182 LyXFont font(LyXFont::ALL_INHERIT);
184 // delete all instances of LyXText before deleting the paragraps used
186 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
187 delete (*cit).second;
191 LyXParagraph * p = par->next;
198 par = new LyXParagraph;
201 token = lex.GetString();
204 if (token == "\\end_inset")
206 if (const_cast<Buffer*>(buf)->
207 parseSingleLyXformat2Token(lex, par, return_par,token, pos, depth,
210 , footnoteflag, footnotekind
214 // the_end read this should NEVER happen
215 lex.printError("\\the_end read in inset! Error in document!");
223 return_par->SetInsetOwner(this);
224 return_par = return_par->next;
227 if (token != "\\end_inset") {
228 lex.printError("Missing \\end_inset at this point. "
235 int InsetText::ascent(BufferView * bv, LyXFont const &) const
238 Row * row = TEXT(bv)->GetRowNearY(y_temp);
239 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
244 int InsetText::descent(BufferView * bv, LyXFont const &) const
247 Row * row = TEXT(bv)->GetRowNearY(y_temp);
248 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
249 TEXT_TO_INSET_OFFSET;
254 int InsetText::width(BufferView * bv, LyXFont const &) const
256 insetWidth = max(textWidth(bv->painter()),
257 (int)TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET));
262 int InsetText::textWidth(Painter & pain) const
264 int const w = getMaxWidth(pain, this);
269 void InsetText::draw(BufferView * bv, LyXFont const & f,
270 int baseline, float & x, bool cleared) const
272 Painter & pain = bv->painter();
274 // no draw is necessary !!!
275 if ((drawFrame == LOCKED) && !locked && !par->size()) {
276 if (!cleared && (need_update == CLEAR_FRAME)) {
277 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
279 insetAscent + insetDescent - 1,
283 top_baseline = baseline;
290 UpdatableInset::draw(bv, f, baseline, x, cleared);
292 // if top_x differs we have a rule down and we don't have to clear anything
293 if (!cleared && (top_x == int(x)) &&
294 ((need_update==INIT)||(need_update==FULL)||(top_baseline!=baseline)))
297 int h = insetAscent + insetDescent;
298 int ty = baseline - insetAscent;
304 if ((ty + h) > pain.paperHeight())
305 h = pain.paperHeight();
306 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
307 w = pain.paperWidth();
308 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
312 if (!cleared && (need_update == NONE))
315 if (top_x != int(x)) {
318 bv->text->status = LyXText::CHANGED_IN_DRAW;
322 top_baseline = baseline;
323 top_y = baseline - ascent(bv, f);
324 last_width = width(bv, f);
325 last_height = ascent(bv, f) + descent(bv, f);
327 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
328 inset_x = cx(bv) - top_x + drawTextXOffset;
329 inset_y = cy(bv) + drawTextYOffset;
331 if (!cleared && (need_update == CURSOR) && !TEXT(bv)->selection) {
336 x += TEXT_TO_INSET_OFFSET;
339 Row * row = TEXT(bv)->GetRowNearY(y);
340 int y_offset = baseline - row->ascent_of_text();
341 int ph = pain.paperHeight();
344 while ((row != 0) && ((y+row->height()) <= 0)) {
346 first += row->height();
351 TEXT(bv)->first = first;
352 if (cleared || !locked || (need_update&FULL) || (need_update&INIT)) {
355 while ((row != 0) && (yf < ph)) {
356 TEXT(bv)->GetVisibleRow(bv, y+y_offset, int(x), row,
364 if (need_update & SELECTION)
365 bv->screen()->ToggleToggle(TEXT(bv), bv, y_offset, int(x));
366 else if (need_update & CURSOR) {
367 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
368 TEXT(bv)->ClearSelection(bv);
369 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
371 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
375 TEXT(bv)->refresh_y = 0;
376 TEXT(bv)->status = LyXText::UNCHANGED;
377 if ((need_update != CURSOR_PAR) &&
378 ((drawFrame == ALWAYS) || ((drawFrame == LOCKED) && locked)))
380 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
381 width(bv, f) - 1, insetAscent + insetDescent - 1,
383 } else if (need_update & CLEAR_FRAME) {
384 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
385 width(bv, f) - 1, insetAscent + insetDescent - 1,
388 x += width(bv, f) - TEXT_TO_INSET_OFFSET;
389 if (bv->text->status==LyXText::CHANGED_IN_DRAW) {
391 } else if (need_update != INIT)
396 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
398 if (reinit) { // && (need_update != CURSOR)) {
402 owner()->update(bv, font, true);
405 if (the_locking_inset) {
406 inset_x = cx(bv) - top_x + drawTextXOffset;
407 inset_y = cy(bv) + drawTextYOffset;
408 the_locking_inset->update(bv, font, reinit);
410 if (need_update == INIT) {
414 int oldw = insetWidth;
416 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
417 // max(textWidth(bv->painter()),
418 // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
419 // (2 * TEXT_TO_INSET_OFFSET);
421 insetWidth = textWidth(bv->painter());
423 insetWidth = static_cast<int>(TEXT(bv)->width);
425 if (oldw != insetWidth) {
426 // printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
427 // textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
430 update(bv, font, reinit);
433 if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
436 TEXT(bv)->UpdateInset(bv, the_locking_inset);
439 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
443 Row * row = TEXT(bv)->GetRowNearY(y_temp);
444 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
445 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
446 TEXT_TO_INSET_OFFSET;
449 void InsetText::SetUpdateStatus(BufferView * bv, int what)
452 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
454 else if (TEXT(bv)->status == LyXText::NEED_VERY_LITTLE_REFRESH)
455 need_update |= CURSOR_PAR;
457 // this to not draw a selection when we redraw all of it!
458 if ((need_update & (INIT|FULL)) && (need_update & CURSOR))
459 TEXT(bv)->ClearSelection(bv);
462 void InsetText::UpdateLocal(BufferView * bv, int what, bool mark_dirty)
464 TEXT(bv)->FullRebreak(bv);
465 SetUpdateStatus(bv, what);
466 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
468 bv->updateInset(this, mark_dirty);
469 bv->owner()->showState();
470 if (old_par != cpar(bv)) {
471 bv->owner()->setLayout(cpar(bv)->GetLayout());
477 string const InsetText::EditMessage() const
479 return _("Opened Text Inset");
483 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
485 // par->SetInsetOwner(this);
486 UpdatableInset::Edit(bv, x, y, button);
488 if (!bv->lockInset(this)) {
489 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
493 the_locking_inset = 0;
494 inset_pos = inset_x = inset_y = 0;
495 inset_boundary = false;
498 if (!checkAndActivateInset(bv, x, y, button))
499 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
501 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
502 bv->text->FinishUndo();
504 UpdateLocal(bv, FULL, false);
506 // If the inset is empty set the language of the current font to the
507 // language to the surronding text.
508 if (par->Last() == 0 && !par->next) {
509 LyXFont font(LyXFont::ALL_IGNORE);
510 font.setLanguage(bv->getParentLanguage(this));
511 SetFont(bv, font, false);
516 void InsetText::InsetUnlock(BufferView * bv)
518 if (the_locking_inset) {
519 the_locking_inset->InsetUnlock(bv);
520 the_locking_inset = 0;
523 no_selection = false;
525 UpdateLocal(bv, CLEAR_FRAME|CURSOR, false);
527 bv->owner()->setLayout(owner()->getLyXText(bv)
528 ->cursor.par()->GetLayout());
530 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
534 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
536 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
539 if (inset == cpar(bv)->GetInset(cpos(bv))) {
540 lyxerr[Debug::INSETS] << "OK" << endl;
541 the_locking_inset = inset;
542 inset_x = cx(bv) - top_x + drawTextXOffset;
543 inset_y = cy(bv) + drawTextYOffset;
544 inset_pos = cpos(bv);
545 inset_par = cpar(bv);
546 inset_boundary = cboundary(bv);
548 TEXT(bv)->ClearSelection(bv);
549 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
550 TEXT(bv)->UpdateInset(bv, the_locking_inset);
552 UpdateLocal(bv, CURSOR, false);
555 } else if (the_locking_inset && (the_locking_inset == inset)) {
556 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
557 lyxerr[Debug::INSETS] << "OK" << endl;
558 inset_x = cx(bv) - top_x + drawTextXOffset;
559 inset_y = cy(bv) + drawTextYOffset;
561 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
563 } else if (the_locking_inset) {
564 lyxerr[Debug::INSETS] << "MAYBE" << endl;
565 return the_locking_inset->LockInsetInInset(bv, inset);
567 lyxerr[Debug::INSETS] << "NOT OK" << endl;
572 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
575 if (!the_locking_inset)
577 if (the_locking_inset == inset) {
578 the_locking_inset->InsetUnlock(bv);
579 TEXT(bv)->UpdateInset(bv, inset);
580 the_locking_inset = 0;
582 moveRight(bv, false);
583 old_par = 0; // force layout setting
584 UpdateLocal(bv, CURSOR_PAR, false);
587 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
591 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
593 if (!the_locking_inset)
595 if (the_locking_inset != inset) {
596 TEXT(bv)->UpdateInset(bv, the_locking_inset);
597 SetUpdateStatus(bv, CURSOR_PAR);
598 return the_locking_inset->UpdateInsetInInset(bv, inset);
600 // UpdateLocal(bv, FULL, false);
601 if (TEXT(bv)->UpdateInset(bv, inset))
602 UpdateLocal(bv, CURSOR_PAR, false);
603 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
604 inset_x = cx(bv) - top_x + drawTextXOffset;
605 inset_y = cy(bv) + drawTextYOffset;
611 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
615 int tmp_x = x - drawTextXOffset;
616 int tmp_y = y + insetAscent - TEXT(bv)->first;
617 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
620 if (the_locking_inset) {
621 if (the_locking_inset == inset) {
622 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
623 no_selection = false;
626 // otherwise unlock the_locking_inset and lock the new inset
627 the_locking_inset->InsetUnlock(bv);
628 inset_x = cx(bv) - top_x + drawTextXOffset;
629 inset_y = cy(bv) + drawTextYOffset;
630 the_locking_inset = static_cast<UpdatableInset*>(inset);
631 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
632 inset->Edit(bv, x - inset_x, y - inset_y, button);
633 if (the_locking_inset)
634 UpdateLocal(bv, CURSOR, false);
635 no_selection = false;
638 // otherwise only unlock the_locking_inset
639 the_locking_inset->InsetUnlock(bv);
640 the_locking_inset = 0;
642 if (bv->theLockingInset()) {
643 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
644 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
645 inset_x = cx(bv) - top_x + drawTextXOffset;
646 inset_y = cy(bv) + drawTextYOffset;
647 inset_pos = cpos(bv);
648 inset_par = cpar(bv);
649 inset_boundary = cboundary(bv);
650 the_locking_inset = uinset;
651 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
652 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
653 if (the_locking_inset)
654 UpdateLocal(bv, CURSOR, false);
655 no_selection = false;
660 bool paste_internally = false;
661 if ((button == 2) && TEXT(bv)->selection) {
662 LocalDispatch(bv, LFUN_COPY, "");
663 paste_internally = true;
665 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
667 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
668 UpdateLocal(bv, CURSOR, false);
669 bv->owner()->setLayout(cpar(bv)->GetLayout());
671 // Insert primary selection with middle mouse
672 // if there is a local selection in the current buffer,
675 if (paste_internally)
676 LocalDispatch(bv, LFUN_PASTE, "");
678 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
682 no_selection = false;
686 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
688 UpdatableInset * inset = 0;
690 if (the_locking_inset) {
691 the_locking_inset->InsetButtonRelease(bv,
692 x - inset_x, y - inset_y,
695 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
696 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
697 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
698 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
700 inset_x = cx(bv) - top_x + drawTextXOffset;
701 inset_y = cy(bv) + drawTextYOffset;
702 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
703 inset->Edit(bv, x - inset_x, y - inset_y, button);
705 UpdateLocal(bv, CURSOR_PAR, false);
708 no_selection = false;
712 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
716 if (the_locking_inset) {
717 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
722 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
724 TEXT(bv)->SetSelection(bv);
725 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
726 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
727 UpdateLocal(bv, SELECTION, false);
732 void InsetText::InsetKeyPress(XKeyEvent * xke)
734 if (the_locking_inset) {
735 the_locking_inset->InsetKeyPress(xke);
741 UpdatableInset::RESULT
742 InsetText::LocalDispatch(BufferView * bv,
743 int action, string const & arg)
745 no_selection = false;
746 UpdatableInset::RESULT
747 result= UpdatableInset::LocalDispatch(bv, action, arg);
748 if (result != UNDISPATCHED) {
753 if ((action < 0) && arg.empty())
756 if (the_locking_inset) {
757 result = the_locking_inset->LocalDispatch(bv, action, arg);
758 if (result == DISPATCHED_NOUPDATE)
760 else if (result == DISPATCHED) {
761 UpdateLocal(bv, CURSOR_PAR, false);
763 } else if (result == FINISHED) {
764 bool dispatched = false;
766 case LFUN_UNKNOWN_ACTION:
767 case LFUN_BREAKPARAGRAPH:
769 moveRightIntern(bv, false, false);
772 if (!TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
773 moveRightIntern(bv, false, false);
777 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
778 moveRightIntern(bv, false, false);
782 the_locking_inset = 0;
790 case LFUN_UNKNOWN_ACTION:
791 if (bv->buffer()->isReadonly()) {
792 // setErrorMessage(N_("Document is read only"));
796 /* Automatically delete the currently selected
797 * text and replace it with what is being
798 * typed in now. Depends on lyxrc settings
799 * "auto_region_delete", which defaults to
802 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
804 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
805 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
807 bv->text->cursor.par()->previous,
808 bv->text->cursor.par()->next
812 if (lyxrc.auto_region_delete) {
813 if (TEXT(bv)->selection){
814 TEXT(bv)->CutSelection(bv, false);
817 TEXT(bv)->ClearSelection(bv);
818 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
819 for (string::size_type i = 0; i < arg.length(); ++i) {
821 if (!math_insert_greek(bv, arg[i])) {
822 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
823 } else if (!the_locking_inset) {
824 (void)moveRight(bv, false);
827 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
831 UpdateLocal(bv, CURSOR_PAR, true);
832 result=DISPATCHED_NOUPDATE;
834 // --- Cursor Movements ---------------------------------------------
836 bv->text->FinishUndo();
837 moveRight(bv, false, true);
838 TEXT(bv)->SetSelection(bv);
839 UpdateLocal(bv, SELECTION, false);
842 result = moveRight(bv);
843 bv->text->FinishUndo();
844 UpdateLocal(bv, CURSOR, false);
847 bv->text->FinishUndo();
848 moveLeft(bv, false, true);
849 TEXT(bv)->SetSelection(bv);
850 UpdateLocal(bv, SELECTION, false);
853 bv->text->FinishUndo();
854 result = moveLeft(bv);
855 UpdateLocal(bv, CURSOR, false);
858 bv->text->FinishUndo();
860 TEXT(bv)->SetSelection(bv);
861 UpdateLocal(bv, SELECTION, false);
864 bv->text->FinishUndo();
865 result = moveDown(bv);
866 UpdateLocal(bv, CURSOR, false);
869 bv->text->FinishUndo();
871 TEXT(bv)->SetSelection(bv);
872 UpdateLocal(bv, SELECTION, false);
875 bv->text->FinishUndo();
877 UpdateLocal(bv, CURSOR, false);
880 bv->text->FinishUndo();
881 TEXT(bv)->CursorHome(bv);
882 UpdateLocal(bv, CURSOR, false);
885 TEXT(bv)->CursorEnd(bv);
886 UpdateLocal(bv, CURSOR, false);
889 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
891 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
892 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
894 bv->text->cursor.par()->previous,
895 bv->text->cursor.par()->next
898 if (TEXT(bv)->selection)
899 TEXT(bv)->CutSelection(bv);
901 TEXT(bv)->Backspace(bv);
902 UpdateLocal(bv, CURSOR_PAR, true);
905 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
907 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
908 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
910 bv->text->cursor.par()->previous,
911 bv->text->cursor.par()->next
914 if (TEXT(bv)->selection)
915 TEXT(bv)->CutSelection(bv);
917 TEXT(bv)->Delete(bv);
918 UpdateLocal(bv, CURSOR_PAR, true);
921 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
923 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
924 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
926 bv->text->cursor.par()->previous,
927 bv->text->cursor.par()->next
930 TEXT(bv)->CutSelection(bv);
931 UpdateLocal(bv, CURSOR_PAR, true);
934 bv->text->FinishUndo();
935 TEXT(bv)->CopySelection(bv);
936 UpdateLocal(bv, CURSOR_PAR, false);
938 case LFUN_PASTESELECTION:
940 string clip(bv->workarea()->getClipboard());
944 if (arg == "paragraph") {
945 TEXT(bv)->InsertStringB(bv, clip);
947 TEXT(bv)->InsertStringA(bv, clip);
949 UpdateLocal(bv, CURSOR_PAR, true);
953 if (!autoBreakRows) {
956 if (cap.nrOfParagraphs() > 1) {
957 WriteAlert(_("Impossible operation"),
958 _("Cannot include more than one paragraph!"),
963 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
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 TEXT(bv)->PasteSelection(bv);
973 UpdateLocal(bv, CURSOR_PAR, true);
975 case LFUN_BREAKPARAGRAPH:
978 TEXT(bv)->BreakParagraph(bv, 0);
979 UpdateLocal(bv, FULL, true);
981 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
984 TEXT(bv)->BreakParagraph(bv, 1);
985 UpdateLocal(bv, FULL, true);
990 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
992 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
993 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
995 bv->text->cursor.par()->previous,
996 bv->text->cursor.par()->next
999 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
1000 UpdateLocal(bv, CURSOR_PAR, true);
1003 // do not set layouts on non breakable textinsets
1004 if (autoBreakRows) {
1005 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1007 // Derive layout number from given argument (string)
1008 // and current buffer's textclass (number). */
1009 LyXTextClassList::ClassList::size_type tclass =
1010 bv->buffer()->params.textclass;
1011 std::pair <bool, LyXTextClass::size_type> layout =
1012 textclasslist.NumberOfLayout(tclass, arg);
1014 // If the entry is obsolete, use the new one instead.
1016 string obs = textclasslist.Style(tclass,layout.second).
1019 layout = textclasslist.NumberOfLayout(tclass, obs);
1022 // see if we found the layout number:
1023 if (!layout.first) {
1024 string msg = string(N_("Layout ")) + arg + N_(" not known");
1026 bv->owner()->getMiniBuffer()->Set(msg);
1030 if (cur_layout != layout.second) {
1031 cur_layout = layout.second;
1032 TEXT(bv)->SetLayout(bv, layout.second);
1033 bv->owner()->setLayout(cpar(bv)->GetLayout());
1034 UpdateLocal(bv, CURSOR_PAR, true);
1037 // reset the layout box
1038 bv->owner()->setLayout(cpar(bv)->GetLayout());
1041 case LFUN_PARAGRAPH_SPACING:
1042 // This one is absolutely not working. When fiddling with this
1043 // it also seems to me that the paragraphs inside the insettext
1044 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1046 LyXParagraph * par = TEXT(bv)->cursor.par();
1047 Spacing::Space cur_spacing = par->spacing.getSpace();
1048 float cur_value = 1.0;
1049 if (cur_spacing == Spacing::Other) {
1050 cur_value = par->spacing.getValue();
1053 std::istringstream istr(arg.c_str());
1056 Spacing::Space new_spacing = cur_spacing;
1057 float new_value = cur_value;
1059 lyxerr << "Missing argument to `paragraph-spacing'"
1061 } else if (tmp == "single") {
1062 new_spacing = Spacing::Single;
1063 } else if (tmp == "onehalf") {
1064 new_spacing = Spacing::Onehalf;
1065 } else if (tmp == "double") {
1066 new_spacing = Spacing::Double;
1067 } else if (tmp == "other") {
1068 new_spacing = Spacing::Other;
1071 lyxerr << "new_value = " << tmpval << endl;
1074 } else if (tmp == "default") {
1075 new_spacing = Spacing::Default;
1077 lyxerr << _("Unknown spacing argument: ")
1080 if (cur_spacing != new_spacing || cur_value != new_value) {
1081 par->spacing.set(new_spacing, new_value);
1082 //TEXT(bv)->RedoParagraph(owner->view());
1083 UpdateLocal(bv, CURSOR_PAR, true);
1084 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1090 result = UNDISPATCHED;
1094 /// If the action has deleted all text in the inset, we need to change the
1095 // language to the language to the surronding text.
1096 if (par->Last() == 0 && !par->next) {
1097 LyXFont font(LyXFont::ALL_IGNORE);
1098 font.setLanguage(bv->getParentLanguage(this));
1099 SetFont(bv, font, false);
1102 if (result != FINISHED) {
1103 ShowInsetCursor(bv);
1105 bv->unlockInset(this);
1110 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1113 buf->latexParagraphs(os, par, 0, texrow);
1114 return texrow.rows();
1118 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1120 LyXParagraph * p = par;
1121 unsigned int lines = 0;
1125 tmp = buf->asciiParagraph(p, linelen);
1126 lines += countChar(tmp, '\n');
1134 int InsetText::DocBook(Buffer const * buf, ostream & os) const
1136 LyXParagraph * p = par;
1137 unsigned int lines = 0;
1142 buf->SimpleDocBookOnePar(os,tmp,p,desc,0);
1150 void InsetText::Validate(LaTeXFeatures & features) const
1152 LyXParagraph * p = par;
1154 p->validate(features);
1160 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1162 if (textclasslist.Style(buf->params.textclass,
1163 p->GetLayout()).labeltype != LABEL_MANUAL)
1166 return p->BeginningOfMainBody();
1170 void InsetText::GetCursorPos(BufferView * bv,
1171 int & x, int & y) const
1178 unsigned int InsetText::InsetInInsetY()
1180 if (!the_locking_inset)
1183 return (inset_y + the_locking_inset->InsetInInsetY());
1187 void InsetText::ToggleInsetCursor(BufferView * bv)
1189 if (the_locking_inset) {
1190 the_locking_inset->ToggleInsetCursor(bv);
1194 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1196 int asc = lyxfont::maxAscent(font);
1197 int desc = lyxfont::maxDescent(font);
1200 bv->hideLockedInsetCursor();
1202 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1203 cursor_visible = !cursor_visible;
1207 void InsetText::ShowInsetCursor(BufferView * bv, bool show)
1209 if (the_locking_inset) {
1210 the_locking_inset->ShowInsetCursor(bv);
1213 if (!cursor_visible) {
1214 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1216 int asc = lyxfont::maxAscent(font);
1217 int desc = lyxfont::maxDescent(font);
1219 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1221 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1222 cursor_visible = true;
1227 void InsetText::HideInsetCursor(BufferView * bv)
1229 if (cursor_visible) {
1230 bv->hideLockedInsetCursor();
1231 cursor_visible = false;
1233 if (the_locking_inset)
1234 the_locking_inset->HideInsetCursor(bv);
1238 UpdatableInset::RESULT
1239 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1241 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1242 return moveLeftIntern(bv, false, activate_inset, selecting);
1244 return moveRightIntern(bv, false, activate_inset, selecting);
1247 UpdatableInset::RESULT
1248 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1250 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1251 return moveRightIntern(bv, true, activate_inset, selecting);
1253 return moveLeftIntern(bv, true, activate_inset, selecting);
1257 UpdatableInset::RESULT
1258 InsetText::moveRightIntern(BufferView * bv, bool behind,
1259 bool activate_inset, bool selecting)
1261 if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1263 if (activate_inset && checkAndActivateInset(bv, behind))
1265 TEXT(bv)->CursorRight(bv);
1267 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1268 return DISPATCHED_NOUPDATE;
1272 UpdatableInset::RESULT
1273 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1274 bool activate_inset, bool selecting)
1276 if (!cpar(bv)->previous && (cpos(bv) <= 0))
1278 TEXT(bv)->CursorLeft(bv);
1280 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1281 if (activate_inset && checkAndActivateInset(bv, behind))
1283 return DISPATCHED_NOUPDATE;
1287 UpdatableInset::RESULT
1288 InsetText::moveUp(BufferView * bv)
1290 if (!crow(bv)->previous())
1292 TEXT(bv)->CursorUp(bv);
1293 return DISPATCHED_NOUPDATE;
1297 UpdatableInset::RESULT
1298 InsetText::moveDown(BufferView * bv)
1300 if (!crow(bv)->next())
1302 TEXT(bv)->CursorDown(bv);
1303 return DISPATCHED_NOUPDATE;
1307 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1309 if (the_locking_inset) {
1310 if (the_locking_inset->InsertInsetAllowed(inset))
1311 return the_locking_inset->InsertInset(bv, inset);
1314 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1316 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1317 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1319 bv->text->cursor.par()->previous,
1320 bv->text->cursor.par()->next
1323 inset->setOwner(this);
1324 HideInsetCursor(bv);
1325 TEXT(bv)->InsertInset(bv, inset);
1327 if ((cpar(bv)->GetChar(cpos(bv)) != LyXParagraph::META_INSET) ||
1328 (cpar(bv)->GetInset(cpos(bv)) != inset))
1329 TEXT(bv)->CursorLeft(bv);
1331 bv->fitCursor(TEXT(bv));
1332 UpdateLocal(bv, CURSOR_PAR|CURSOR, true);
1333 ShowInsetCursor(bv);
1338 UpdatableInset * InsetText::GetLockingInset()
1340 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1344 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1348 if (the_locking_inset)
1349 return the_locking_inset->GetFirstLockingInsetOfType(c);
1354 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1356 bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1358 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1359 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1361 bv->text->cursor.par()->previous,
1362 bv->text->cursor.par()->next
1365 TEXT(bv)->SetFont(bv, font, toggleall);
1366 bv->fitCursor(TEXT(bv));
1367 UpdateLocal(bv, CURSOR_PAR, true);
1371 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1373 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1377 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1378 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1380 LyXFont const font =
1381 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1383 x = inset->width(bv, font);
1384 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1387 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1389 //inset_x = cx(bv) - top_x + drawTextXOffset;
1390 //inset_y = cy(bv) + drawTextYOffset;
1391 inset->Edit(bv, x, y, 0);
1392 if (!the_locking_inset)
1394 UpdateLocal(bv, CURSOR_PAR, false);
1401 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1404 x = x - drawTextXOffset;
1405 y = y + insetAscent;
1406 Inset * inset = bv->checkInsetHit(TEXT(bv), x, y, button);
1413 inset_x = cx(bv) - top_x + drawTextXOffset;
1414 inset_y = cy(bv) + drawTextYOffset;
1415 inset->Edit(bv, x - inset_x, y - inset_y, button);
1416 if (!the_locking_inset)
1418 UpdateLocal(bv, CURSOR_PAR, false);
1425 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1427 int w = UpdatableInset::getMaxWidth(pain, inset);
1432 w = w - top_x + owner()->x();
1435 w -= (2 * TEXT_TO_INSET_OFFSET);
1437 // return w - (2*TEXT_TO_INSET_OFFSET);
1441 void InsetText::SetParagraphData(LyXParagraph *p)
1443 // delete all instances of LyXText before deleting the paragraps used
1445 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
1446 delete (*cit).second;
1461 par->SetInsetOwner(this);
1465 np->next = p->Clone();
1466 np->next->previous = np;
1468 np->SetInsetOwner(this);
1474 void InsetText::SetText(string const & data)
1477 LyXFont font(LyXFont::ALL_SANE);
1478 for(unsigned int i=0; i < data.length(); ++i)
1479 par->InsertChar(i, data[i], font);
1483 void InsetText::SetAutoBreakRows(bool flag)
1485 if (flag != autoBreakRows) {
1486 autoBreakRows = flag;
1494 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1496 if (how != drawFrame) {
1499 UpdateLocal(bv, DRAW_FRAME, false);
1504 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1506 if (frame_color != col) {
1509 UpdateLocal(bv, DRAW_FRAME, false);
1514 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1516 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1521 int InsetText::cx(BufferView * bv) const
1523 LyXText * text = TEXT(bv);
1524 int x = text->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1525 if (the_locking_inset) {
1526 LyXFont font = text->GetFont(bv->buffer(),
1527 text->cursor.par(), text->cursor.pos());
1528 if (font.isVisibleRightToLeft())
1529 x -= the_locking_inset->width(bv, font);
1535 int InsetText::cy(BufferView * bv) const
1538 return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1542 LyXParagraph::size_type InsetText::cpos(BufferView * bv) const
1544 return TEXT(bv)->cursor.pos();
1548 LyXParagraph * InsetText::cpar(BufferView * bv) const
1550 return TEXT(bv)->cursor.par();
1553 bool InsetText::cboundary(BufferView * bv) const
1555 return TEXT(bv)->cursor.boundary();
1559 Row * InsetText::crow(BufferView * bv) const
1561 return TEXT(bv)->cursor.row();
1565 LyXText * InsetText::getLyXText(BufferView const * lbv, bool const recursive) const
1567 // Super UGLY! (Lgb)
1568 BufferView * bv = const_cast<BufferView *>(lbv);
1570 if ((cache.find(bv) != cache.end()) && cache[bv]) {
1571 if (recursive && the_locking_inset)
1572 return the_locking_inset->getLyXText(bv);
1575 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1578 if (the_locking_inset) {
1579 lt->SetCursor(bv, inset_par, inset_pos, true, inset_boundary);
1581 return the_locking_inset->getLyXText(bv);
1587 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1589 if ((cache.find(bv) == cache.end()) || !cache[bv])
1594 /// then remove all LyXText in text-insets
1595 LyXParagraph * p = par;
1596 for (;p;p = p->next) {
1597 p->deleteInsetsLyXText(bv);
1603 void InsetText::resizeLyXText(BufferView * bv) const
1605 if (!par->next && !par->size()) // resize not neccessary!
1607 if ((cache.find(bv) == cache.end()) || !cache[bv])
1610 LyXParagraph * lpar = 0;
1611 LyXParagraph * selstartpar = 0;
1612 LyXParagraph * selendpar = 0;
1613 LyXParagraph::size_type pos = 0;
1614 LyXParagraph::size_type selstartpos = 0;
1615 LyXParagraph::size_type selendpos = 0;
1616 bool boundary = false;
1617 bool selstartboundary = false;
1618 bool selendboundary = false;
1622 // ProhibitInput(bv);
1625 lpar = TEXT(bv)->cursor.par();
1626 pos = TEXT(bv)->cursor.pos();
1627 boundary = TEXT(bv)->cursor.boundary();
1628 selstartpar = TEXT(bv)->sel_start_cursor.par();
1629 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1630 selstartboundary = TEXT(bv)->sel_start_cursor.boundary();
1631 selendpar = TEXT(bv)->sel_end_cursor.par();
1632 selendpos = TEXT(bv)->sel_end_cursor.pos();
1633 selendboundary = TEXT(bv)->sel_end_cursor.boundary();
1634 selection = TEXT(bv)->selection;
1635 mark_set = TEXT(bv)->mark_set;
1637 deleteLyXText(bv, (the_locking_inset == 0));
1640 TEXT(bv)->selection = true;
1641 /* at this point just to avoid the Delete-Empty-Paragraph
1642 * Mechanism when setting the cursor */
1643 TEXT(bv)->mark_set = mark_set;
1645 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos,true,
1647 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1648 TEXT(bv)->SetCursor(bv, selendpar, selendpos, true, selendboundary);
1649 TEXT(bv)->SetSelection(bv);
1650 TEXT(bv)->SetCursor(bv, lpar, pos);
1652 TEXT(bv)->SetCursor(bv, lpar, pos, true, boundary);
1653 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1654 TEXT(bv)->selection = false;
1658 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1659 // this will scroll the screen such that the cursor becomes visible
1660 bv->updateScrollbar();
1662 if (the_locking_inset) {
1663 /// then resize all LyXText in text-insets
1664 inset_x = cx(bv) - top_x + drawTextXOffset;
1665 inset_y = cy(bv) + drawTextYOffset;
1666 for (LyXParagraph * p = par; p; p = p->next) {
1667 p->resizeInsetsLyXText(bv);
1674 void InsetText::removeNewlines()
1676 for (LyXParagraph * p = par; p; p = p->next) {
1677 for (int i = 0; i < p->Last(); ++i) {
1678 if (p->GetChar(i) == LyXParagraph::META_NEWLINE)