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 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==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)) {
316 // printf("InsetText::draw1 -> INIT(%d)\n",insetWidth);
319 bv->text->status = LyXText::CHANGED_IN_DRAW;
323 top_baseline = baseline;
324 top_y = baseline - ascent(bv, f);
325 last_width = width(bv, f);
326 last_height = ascent(bv, f) + descent(bv, f);
328 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
329 inset_x = cx(bv) - top_x + drawTextXOffset;
330 inset_y = cy(bv) + drawTextYOffset;
332 if (!cleared && (need_update == CURSOR) && !TEXT(bv)->selection) {
337 x += TEXT_TO_INSET_OFFSET;
340 Row * row = TEXT(bv)->GetRowNearY(y);
341 int y_offset = baseline - row->ascent_of_text();
342 int ph = pain.paperHeight();
345 while ((row != 0) && ((y+row->height()) <= 0)) {
347 first += row->height();
352 TEXT(bv)->first = first;
353 if (cleared || !locked || (need_update==FULL) || (need_update==INIT)) {
356 while ((row != 0) && (yf < ph)) {
357 TEXT(bv)->GetVisibleRow(bv, y+y_offset, int(x), row,
363 } else if (need_update == SELECTION) {
364 bv->screen()->ToggleToggle(TEXT(bv), bv, y_offset, int(x));
367 if (need_update == CURSOR) {
368 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
369 TEXT(bv)->ClearSelection();
370 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
372 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
376 TEXT(bv)->refresh_y = 0;
377 TEXT(bv)->status = LyXText::UNCHANGED;
378 if ((need_update != CURSOR_PAR) &&
379 ((drawFrame == ALWAYS) || ((drawFrame == LOCKED) && locked)))
381 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
382 width(bv, f) - 1, insetAscent + insetDescent - 1,
384 } else if (need_update == CLEAR_FRAME) {
385 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
386 width(bv, f) - 1, insetAscent + insetDescent - 1,
389 x += width(bv, f) - TEXT_TO_INSET_OFFSET;
390 if (bv->text->status==LyXText::CHANGED_IN_DRAW)
393 // printf("InsetText::draw2 -> INIT(%d)\n",insetWidth);
395 else if (need_update != INIT)
400 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
402 if (reinit) { // && (need_update != CURSOR)) {
406 owner()->update(bv, font, true);
409 if (the_locking_inset) {
410 inset_x = cx(bv) - top_x + drawTextXOffset;
411 inset_y = cy(bv) + drawTextYOffset;
412 the_locking_inset->update(bv, font, reinit);
414 if (need_update == INIT) {
418 int oldw = insetWidth;
420 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
421 // max(textWidth(bv->painter()),
422 // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
423 // (2 * TEXT_TO_INSET_OFFSET);
425 insetWidth = textWidth(bv->painter());
427 insetWidth = static_cast<int>(TEXT(bv)->width);
429 if (oldw != insetWidth) {
430 // printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
431 // textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
434 update(bv, font, reinit);
437 if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
440 TEXT(bv)->UpdateInset(bv, the_locking_inset);
443 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
447 Row * row = TEXT(bv)->GetRowNearY(y_temp);
448 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
449 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
450 TEXT_TO_INSET_OFFSET;
454 void InsetText::UpdateLocal(BufferView * bv, UpdateCodes what, bool mark_dirty)
457 // printf("InsetText::UpdateLocal -> INIT(%d)\n",insetWidth);
458 TEXT(bv)->FullRebreak(bv);
459 if (need_update != INIT) {
460 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
462 else if (!the_locking_inset || (what != CURSOR))
465 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
467 bv->updateInset(this, mark_dirty);
468 bv->owner()->showState();
469 if (old_par != cpar(bv)) {
470 bv->owner()->setLayout(cpar(bv)->GetLayout());
476 string const InsetText::EditMessage() const
478 return _("Opened Text Inset");
482 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
484 // par->SetInsetOwner(this);
485 UpdatableInset::Edit(bv, x, y, button);
487 if (!bv->lockInset(this)) {
488 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
492 the_locking_inset = 0;
493 inset_pos = inset_x = inset_y = 0;
494 inset_boundary = false;
497 if (!checkAndActivateInset(bv, x, y, button))
498 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
500 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
501 bv->text->FinishUndo();
503 UpdateLocal(bv, FULL, false);
505 // If the inset is empty set the language of the current font to the
506 // language to the surronding text.
507 if (par->Last() == 0 && !par->next) {
508 LyXFont font(LyXFont::ALL_IGNORE);
509 font.setLanguage(bv->getParentLanguage(this));
510 SetFont(bv, font, false);
515 void InsetText::InsetUnlock(BufferView * bv)
517 if (the_locking_inset) {
518 the_locking_inset->InsetUnlock(bv);
519 the_locking_inset = 0;
522 no_selection = false;
524 TEXT(bv)->selection = 0;
525 UpdateLocal(bv, CLEAR_FRAME, 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);
547 TEXT(bv)->UpdateInset(bv, the_locking_inset);
549 } else if (the_locking_inset && (the_locking_inset == inset)) {
550 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
551 lyxerr[Debug::INSETS] << "OK" << endl;
552 inset_x = cx(bv) - top_x + drawTextXOffset;
553 inset_y = cy(bv) + drawTextYOffset;
555 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
557 } else if (the_locking_inset) {
558 lyxerr[Debug::INSETS] << "MAYBE" << endl;
559 return the_locking_inset->LockInsetInInset(bv, inset);
561 lyxerr[Debug::INSETS] << "NOT OK" << endl;
566 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
569 if (!the_locking_inset)
571 if (the_locking_inset == inset) {
572 the_locking_inset->InsetUnlock(bv);
573 TEXT(bv)->UpdateInset(bv, inset);
574 the_locking_inset = 0;
576 moveRight(bv, false);
577 old_par = 0; // force layout setting
578 UpdateLocal(bv, CURSOR_PAR, false);
581 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
585 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
587 if (!the_locking_inset)
589 if (the_locking_inset != inset) {
590 TEXT(bv)->UpdateInset(bv, the_locking_inset);
591 need_update = CURSOR_PAR;
592 return the_locking_inset->UpdateInsetInInset(bv, inset);
594 // UpdateLocal(bv, FULL, false);
595 if (TEXT(bv)->UpdateInset(bv, inset))
596 UpdateLocal(bv, CURSOR_PAR, false);
597 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
598 inset_x = cx(bv) - top_x + drawTextXOffset;
599 inset_y = cy(bv) + drawTextYOffset;
605 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
607 no_selection = false;
609 int tmp_x = x - drawTextXOffset;
610 int tmp_y = y + insetAscent - TEXT(bv)->first;
611 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
614 if (the_locking_inset) {
615 if (the_locking_inset == inset) {
616 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
619 // otherwise unlock the_locking_inset and lock the new inset
620 the_locking_inset->InsetUnlock(bv);
621 inset_x = cx(bv) - top_x + drawTextXOffset;
622 inset_y = cy(bv) + drawTextYOffset;
623 the_locking_inset = static_cast<UpdatableInset*>(inset);
624 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
625 inset->Edit(bv, x - inset_x, y - inset_y, button);
626 if (the_locking_inset) {
627 UpdateLocal(bv, CURSOR_PAR, false);
631 // otherwise only unlock the_locking_inset
632 the_locking_inset->InsetUnlock(bv);
633 the_locking_inset = 0;
635 if (bv->theLockingInset()) {
636 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
637 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
638 inset_x = cx(bv) - top_x + drawTextXOffset;
639 inset_y = cy(bv) + drawTextYOffset;
640 inset_pos = cpos(bv);
641 inset_par = cpar(bv);
642 inset_boundary = cboundary(bv);
643 the_locking_inset = uinset;
644 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
645 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
646 // TEXT(bv)->ClearSelection();
647 if (the_locking_inset) {
648 UpdateLocal(bv, CURSOR_PAR, false);
654 bool paste_internally = false;
655 if ((button == 2) && TEXT(bv)->selection) {
656 LocalDispatch(bv, LFUN_COPY, "");
657 paste_internally = true;
659 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
661 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
662 UpdateLocal(bv, CURSOR, false);
663 bv->owner()->setLayout(cpar(bv)->GetLayout());
665 // Insert primary selection with middle mouse
666 // if there is a local selection in the current buffer,
669 if (paste_internally)
670 LocalDispatch(bv, LFUN_PASTE, "");
672 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
679 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
681 UpdatableInset * inset = 0;
683 if (the_locking_inset) {
684 the_locking_inset->InsetButtonRelease(bv,
685 x - inset_x, y - inset_y,
688 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
689 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
690 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
691 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
693 inset_x = cx(bv) - top_x + drawTextXOffset;
694 inset_y = cy(bv) + drawTextYOffset;
695 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
696 inset->Edit(bv, x - inset_x, y - inset_y, button);
698 UpdateLocal(bv, CURSOR_PAR, false);
701 no_selection = false;
705 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
707 if (the_locking_inset) {
708 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
714 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
716 TEXT(bv)->SetSelection();
717 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
718 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
719 UpdateLocal(bv, SELECTION, false);
722 no_selection = false;
726 void InsetText::InsetKeyPress(XKeyEvent * xke)
728 if (the_locking_inset) {
729 the_locking_inset->InsetKeyPress(xke);
735 UpdatableInset::RESULT
736 InsetText::LocalDispatch(BufferView * bv,
737 int action, string const & arg)
739 no_selection = false;
740 UpdatableInset::RESULT
741 result= UpdatableInset::LocalDispatch(bv, action, arg);
742 if (result != UNDISPATCHED) {
747 if ((action < 0) && arg.empty())
750 if (the_locking_inset) {
751 result = the_locking_inset->LocalDispatch(bv, action, arg);
752 if (result == DISPATCHED_NOUPDATE)
754 else if (result == DISPATCHED) {
755 UpdateLocal(bv, CURSOR_PAR, false);
757 } else if (result == FINISHED) {
758 bool dispatched = false;
760 case LFUN_UNKNOWN_ACTION:
761 case LFUN_BREAKPARAGRAPH:
763 moveRightIntern(bv, false, false);
766 if (!TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
767 moveRightIntern(bv, false, false);
771 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
772 moveRightIntern(bv, false, false);
776 the_locking_inset = 0;
784 case LFUN_UNKNOWN_ACTION:
785 if (bv->buffer()->isReadonly()) {
787 // setErrorMessage(N_("Document is read only"));
791 /* Automatically delete the currently selected
792 * text and replace it with what is being
793 * typed in now. Depends on lyxrc settings
794 * "auto_region_delete", which defaults to
797 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
799 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
800 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
802 bv->text->cursor.par()->previous,
803 bv->text->cursor.par()->next
807 if (lyxrc.auto_region_delete) {
808 if (TEXT(bv)->selection){
809 TEXT(bv)->CutSelection(bv, false);
812 TEXT(bv)->ClearSelection();
813 for (string::size_type i = 0; i < arg.length(); ++i) {
815 if (!math_insert_greek(bv, arg[i])) {
817 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
819 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
821 } else if (!the_locking_inset) {
822 (void)moveRight(bv, false);
826 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
828 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
833 UpdateLocal(bv, CURSOR_PAR, true);
834 result=DISPATCHED_NOUPDATE;
836 // --- Cursor Movements ---------------------------------------------
838 bv->text->FinishUndo();
839 moveRight(bv, false, true);
840 TEXT(bv)->SetSelection();
841 UpdateLocal(bv, SELECTION, false);
844 result = moveRight(bv);
845 bv->text->FinishUndo();
846 TEXT(bv)->ClearSelection();
847 UpdateLocal(bv, CURSOR, false);
850 bv->text->FinishUndo();
851 moveLeft(bv, false, true);
852 TEXT(bv)->SetSelection();
853 UpdateLocal(bv, SELECTION, false);
856 bv->text->FinishUndo();
857 result = moveLeft(bv);
858 TEXT(bv)->ClearSelection();
859 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
860 UpdateLocal(bv, CURSOR, false);
863 bv->text->FinishUndo();
865 TEXT(bv)->SetSelection();
866 UpdateLocal(bv, SELECTION, false);
869 bv->text->FinishUndo();
870 result = moveDown(bv);
871 TEXT(bv)->ClearSelection();
872 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
873 UpdateLocal(bv, CURSOR, false);
876 bv->text->FinishUndo();
878 TEXT(bv)->SetSelection();
879 UpdateLocal(bv, SELECTION, false);
882 bv->text->FinishUndo();
884 TEXT(bv)->ClearSelection();
885 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
886 UpdateLocal(bv, CURSOR, false);
889 bv->text->FinishUndo();
890 TEXT(bv)->CursorHome(bv);
891 TEXT(bv)->ClearSelection();
892 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
893 UpdateLocal(bv, CURSOR, false);
896 TEXT(bv)->CursorEnd(bv);
897 TEXT(bv)->ClearSelection();
898 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
899 UpdateLocal(bv, CURSOR, false);
902 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
904 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
905 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
907 bv->text->cursor.par()->previous,
908 bv->text->cursor.par()->next
911 if (TEXT(bv)->selection)
912 TEXT(bv)->CutSelection(bv);
914 TEXT(bv)->Backspace(bv);
915 UpdateLocal(bv, CURSOR_PAR, true);
918 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
920 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
921 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
923 bv->text->cursor.par()->previous,
924 bv->text->cursor.par()->next
927 if (TEXT(bv)->selection)
928 TEXT(bv)->CutSelection(bv);
930 TEXT(bv)->Delete(bv);
931 UpdateLocal(bv, CURSOR_PAR, true);
934 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
936 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
937 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
939 bv->text->cursor.par()->previous,
940 bv->text->cursor.par()->next
943 TEXT(bv)->CutSelection(bv);
944 UpdateLocal(bv, CURSOR_PAR, true);
947 bv->text->FinishUndo();
948 TEXT(bv)->CopySelection(bv);
949 UpdateLocal(bv, CURSOR_PAR, false);
951 case LFUN_PASTESELECTION:
953 string clip(bv->workarea()->getClipboard());
957 if (arg == "paragraph") {
958 TEXT(bv)->InsertStringB(bv, clip);
960 TEXT(bv)->InsertStringA(bv, clip);
962 UpdateLocal(bv, CURSOR_PAR, true);
966 if (!autoBreakRows) {
969 if (cap.nrOfParagraphs() > 1) {
970 WriteAlert(_("Impossible operation"),
971 _("Cannot include more than one paragraph!"),
976 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
978 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
979 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
981 bv->text->cursor.par()->previous,
982 bv->text->cursor.par()->next
985 TEXT(bv)->PasteSelection(bv);
986 UpdateLocal(bv, CURSOR_PAR, true);
988 case LFUN_BREAKPARAGRAPH:
991 TEXT(bv)->BreakParagraph(bv, 0);
992 UpdateLocal(bv, FULL, true);
994 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
997 TEXT(bv)->BreakParagraph(bv, 1);
998 UpdateLocal(bv, FULL, true);
1000 case LFUN_BREAKLINE:
1003 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1005 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1006 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1008 bv->text->cursor.par()->previous,
1009 bv->text->cursor.par()->next
1012 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
1013 UpdateLocal(bv, CURSOR_PAR, true);
1016 // do not set layouts on non breakable textinsets
1017 if (autoBreakRows) {
1018 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1020 // Derive layout number from given argument (string)
1021 // and current buffer's textclass (number). */
1022 LyXTextClassList::ClassList::size_type tclass =
1023 bv->buffer()->params.textclass;
1024 std::pair <bool, LyXTextClass::size_type> layout =
1025 textclasslist.NumberOfLayout(tclass, arg);
1027 // If the entry is obsolete, use the new one instead.
1029 string obs = textclasslist.Style(tclass,layout.second).
1032 layout = textclasslist.NumberOfLayout(tclass, obs);
1035 // see if we found the layout number:
1036 if (!layout.first) {
1037 string msg = string(N_("Layout ")) + arg + N_(" not known");
1039 bv->owner()->getMiniBuffer()->Set(msg);
1043 if (cur_layout != layout.second) {
1044 cur_layout = layout.second;
1045 TEXT(bv)->SetLayout(bv, layout.second);
1046 bv->owner()->setLayout(cpar(bv)->GetLayout());
1047 UpdateLocal(bv, CURSOR_PAR, true);
1050 // reset the layout box
1051 bv->owner()->setLayout(cpar(bv)->GetLayout());
1054 case LFUN_PARAGRAPH_SPACING:
1055 // This one is absolutely not working. When fiddling with this
1056 // it also seems to me that the paragraphs inside the insettext
1057 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1059 LyXParagraph * par = TEXT(bv)->cursor.par();
1060 Spacing::Space cur_spacing = par->spacing.getSpace();
1061 float cur_value = 1.0;
1062 if (cur_spacing == Spacing::Other) {
1063 cur_value = par->spacing.getValue();
1066 std::istringstream istr(arg.c_str());
1069 Spacing::Space new_spacing = cur_spacing;
1070 float new_value = cur_value;
1072 lyxerr << "Missing argument to `paragraph-spacing'"
1074 } else if (tmp == "single") {
1075 new_spacing = Spacing::Single;
1076 } else if (tmp == "onehalf") {
1077 new_spacing = Spacing::Onehalf;
1078 } else if (tmp == "double") {
1079 new_spacing = Spacing::Double;
1080 } else if (tmp == "other") {
1081 new_spacing = Spacing::Other;
1084 lyxerr << "new_value = " << tmpval << endl;
1087 } else if (tmp == "default") {
1088 new_spacing = Spacing::Default;
1090 lyxerr << _("Unknown spacing argument: ")
1093 if (cur_spacing != new_spacing || cur_value != new_value) {
1094 par->spacing.set(new_spacing, new_value);
1095 //TEXT(bv)->RedoParagraph(owner->view());
1096 UpdateLocal(bv, CURSOR_PAR, true);
1097 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1103 result = UNDISPATCHED;
1106 if (result != FINISHED) {
1107 ShowInsetCursor(bv);
1109 bv->unlockInset(this);
1114 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1117 buf->latexParagraphs(os, par, 0, texrow);
1118 return texrow.rows();
1122 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1124 LyXParagraph * p = par;
1125 unsigned int lines = 0;
1129 tmp = buf->asciiParagraph(p, linelen);
1130 lines += countChar(tmp, '\n');
1138 int InsetText::DocBook(Buffer const * buf, ostream & os) const
1140 LyXParagraph * p = par;
1141 unsigned int lines = 0;
1146 buf->SimpleDocBookOnePar(os,tmp,p,desc,0);
1154 void InsetText::Validate(LaTeXFeatures & features) const
1156 LyXParagraph * p = par;
1158 p->validate(features);
1164 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1166 if (textclasslist.Style(buf->params.textclass,
1167 p->GetLayout()).labeltype != LABEL_MANUAL)
1170 return p->BeginningOfMainBody();
1174 void InsetText::GetCursorPos(BufferView * bv,
1175 int & x, int & y) const
1182 unsigned int InsetText::InsetInInsetY()
1184 if (!the_locking_inset)
1187 return (inset_y + the_locking_inset->InsetInInsetY());
1191 void InsetText::ToggleInsetCursor(BufferView * bv)
1193 if (the_locking_inset) {
1194 the_locking_inset->ToggleInsetCursor(bv);
1198 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1200 int asc = lyxfont::maxAscent(font);
1201 int desc = lyxfont::maxDescent(font);
1204 bv->hideLockedInsetCursor();
1206 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1207 cursor_visible = !cursor_visible;
1211 void InsetText::ShowInsetCursor(BufferView * bv, bool show)
1213 if (the_locking_inset) {
1214 the_locking_inset->ShowInsetCursor(bv);
1217 if (!cursor_visible) {
1218 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1220 int asc = lyxfont::maxAscent(font);
1221 int desc = lyxfont::maxDescent(font);
1223 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1225 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1226 cursor_visible = true;
1231 void InsetText::HideInsetCursor(BufferView * bv)
1233 if (cursor_visible) {
1234 bv->hideLockedInsetCursor();
1235 cursor_visible = false;
1237 if (the_locking_inset)
1238 the_locking_inset->HideInsetCursor(bv);
1242 UpdatableInset::RESULT
1243 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1245 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1246 return moveLeftIntern(bv, false, activate_inset, selecting);
1248 return moveRightIntern(bv, false, activate_inset, selecting);
1251 UpdatableInset::RESULT
1252 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1254 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1255 return moveRightIntern(bv, true, activate_inset, selecting);
1257 return moveLeftIntern(bv, true, activate_inset, selecting);
1261 UpdatableInset::RESULT
1262 InsetText::moveRightIntern(BufferView * bv, bool behind,
1263 bool activate_inset, bool selecting)
1265 if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1267 if (activate_inset && checkAndActivateInset(bv, behind))
1269 TEXT(bv)->CursorRight(bv, selecting);
1270 return DISPATCHED_NOUPDATE;
1274 UpdatableInset::RESULT
1275 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1276 bool activate_inset, bool selecting)
1278 if (!cpar(bv)->previous && (cpos(bv) <= 0))
1280 TEXT(bv)->CursorLeft(bv, selecting);
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);
1326 if ((cpar(bv)->GetChar(cpos(bv)) != LyXParagraph::META_INSET) ||
1327 (cpar(bv)->GetInset(cpos(bv)) != inset))
1328 TEXT(bv)->CursorLeft(bv);
1329 TEXT(bv)->selection = 0;
1330 bv->fitCursor(TEXT(bv));
1331 UpdateLocal(bv, CURSOR_PAR, true);
1332 ShowInsetCursor(bv);
1337 UpdatableInset * InsetText::GetLockingInset()
1339 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1343 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1347 if (the_locking_inset)
1348 return the_locking_inset->GetFirstLockingInsetOfType(c);
1353 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1355 bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1357 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1358 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1360 bv->text->cursor.par()->previous,
1361 bv->text->cursor.par()->next
1364 TEXT(bv)->SetFont(bv, font, toggleall);
1365 bv->fitCursor(TEXT(bv));
1366 UpdateLocal(bv, CURSOR_PAR, true);
1370 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1372 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1376 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1377 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1379 LyXFont const font =
1380 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1382 x = inset->width(bv, font);
1383 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1386 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1388 //inset_x = cx(bv) - top_x + drawTextXOffset;
1389 //inset_y = cy(bv) + drawTextYOffset;
1390 inset->Edit(bv, x, y, 0);
1391 if (!the_locking_inset)
1393 UpdateLocal(bv, CURSOR_PAR, false);
1400 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1403 x = x - drawTextXOffset;
1404 y = y + insetAscent;
1405 Inset * inset = bv->checkInsetHit(TEXT(bv), x, y, button);
1412 inset_x = cx(bv) - top_x + drawTextXOffset;
1413 inset_y = cy(bv) + drawTextYOffset;
1414 inset->Edit(bv, x - inset_x, y - inset_y, button);
1415 if (!the_locking_inset)
1417 UpdateLocal(bv, CURSOR_PAR, false);
1424 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1426 int w = UpdatableInset::getMaxWidth(pain, inset);
1431 w = w - top_x + owner()->x();
1434 w -= (2 * TEXT_TO_INSET_OFFSET);
1436 // return w - (2*TEXT_TO_INSET_OFFSET);
1440 void InsetText::SetParagraphData(LyXParagraph *p)
1442 // delete all instances of LyXText before deleting the paragraps used
1444 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
1445 delete (*cit).second;
1460 par->SetInsetOwner(this);
1464 np->next = p->Clone();
1465 np->next->previous = np;
1467 np->SetInsetOwner(this);
1473 void InsetText::SetText(string const & data)
1476 LyXFont font(LyXFont::ALL_SANE);
1477 for(unsigned int i=0; i < data.length(); ++i)
1478 par->InsertChar(i, data[i], font);
1482 void InsetText::SetAutoBreakRows(bool flag)
1484 if (flag != autoBreakRows) {
1485 autoBreakRows = flag;
1493 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1495 if (how != drawFrame) {
1498 UpdateLocal(bv, DRAW_FRAME, false);
1503 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1505 if (frame_color != col) {
1508 UpdateLocal(bv, DRAW_FRAME, false);
1513 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1515 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1520 int InsetText::cx(BufferView * bv) const
1522 LyXText * text = TEXT(bv);
1523 int x = text->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1524 if (the_locking_inset) {
1525 LyXFont font = text->GetFont(bv->buffer(),
1526 text->cursor.par(), text->cursor.pos());
1527 if (font.isVisibleRightToLeft())
1528 x -= the_locking_inset->width(bv, font);
1534 int InsetText::cy(BufferView * bv) const
1537 return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1541 LyXParagraph::size_type InsetText::cpos(BufferView * bv) const
1543 return TEXT(bv)->cursor.pos();
1547 LyXParagraph * InsetText::cpar(BufferView * bv) const
1549 return TEXT(bv)->cursor.par();
1552 bool InsetText::cboundary(BufferView * bv) const
1554 return TEXT(bv)->cursor.boundary();
1558 Row * InsetText::crow(BufferView * bv) const
1560 return TEXT(bv)->cursor.row();
1564 LyXText * InsetText::getLyXText(BufferView const * lbv) const
1566 // Super UGLY! (Lgb)
1567 BufferView * bv = const_cast<BufferView *>(lbv);
1569 if ((cache.find(bv) != cache.end()) && cache[bv])
1571 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1574 if (the_locking_inset) {
1575 lt->SetCursor(bv, inset_par, inset_pos, true, inset_boundary);
1581 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1583 if ((cache.find(bv) == cache.end()) || !cache[bv])
1588 /// then remove all LyXText in text-insets
1589 LyXParagraph * p = par;
1590 for (;p;p = p->next) {
1591 p->deleteInsetsLyXText(bv);
1597 void InsetText::resizeLyXText(BufferView * bv) const
1599 if (!par->next && !par->size()) // resize not neccessary!
1601 if ((cache.find(bv) == cache.end()) || !cache[bv])
1604 LyXParagraph * lpar = 0;
1605 LyXParagraph * selstartpar = 0;
1606 LyXParagraph * selendpar = 0;
1607 LyXParagraph::size_type pos = 0;
1608 LyXParagraph::size_type selstartpos = 0;
1609 LyXParagraph::size_type selendpos = 0;
1610 bool boundary = false;
1611 bool selstartboundary = false;
1612 bool selendboundary = false;
1616 // ProhibitInput(bv);
1619 lpar = TEXT(bv)->cursor.par();
1620 pos = TEXT(bv)->cursor.pos();
1621 boundary = TEXT(bv)->cursor.boundary();
1622 selstartpar = TEXT(bv)->sel_start_cursor.par();
1623 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1624 selstartboundary = TEXT(bv)->sel_start_cursor.boundary();
1625 selendpar = TEXT(bv)->sel_end_cursor.par();
1626 selendpos = TEXT(bv)->sel_end_cursor.pos();
1627 selendboundary = TEXT(bv)->sel_end_cursor.boundary();
1628 selection = TEXT(bv)->selection;
1629 mark_set = TEXT(bv)->mark_set;
1631 deleteLyXText(bv, (the_locking_inset == 0));
1634 TEXT(bv)->selection = true;
1635 /* at this point just to avoid the Delete-Empty-Paragraph
1636 * Mechanism when setting the cursor */
1637 TEXT(bv)->mark_set = mark_set;
1639 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos,true,
1641 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1642 TEXT(bv)->SetCursor(bv, selendpar, selendpos, true, selendboundary);
1643 TEXT(bv)->SetSelection();
1644 TEXT(bv)->SetCursor(bv, lpar, pos);
1646 TEXT(bv)->SetCursor(bv, lpar, pos, true, boundary);
1647 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1648 TEXT(bv)->selection = false;
1652 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1653 // this will scroll the screen such that the cursor becomes visible
1654 bv->updateScrollbar();
1656 if (the_locking_inset) {
1657 /// then resize all LyXText in text-insets
1658 inset_x = cx(bv) - top_x + drawTextXOffset;
1659 inset_y = cy(bv) + drawTextYOffset;
1660 for (LyXParagraph * p = par; p; p = p->next) {
1661 p->resizeInsetsLyXText(bv);
1668 void InsetText::removeNewlines()
1670 for (LyXParagraph * p = par; p; p = p->next) {
1671 for (int i = 0; i < p->Last(); ++i) {
1672 if (p->GetChar(i) == LyXParagraph::META_NEWLINE)