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;
124 LyXParagraph * p = par->next_;
132 LyXParagraph * p = par->next();
143 void InsetText::clear()
145 // delete all instances of LyXText before deleting the paragraps used
147 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
148 delete (*cit).second;
152 LyXParagraph * p = par->next_;
160 LyXParagraph * p = 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_;
220 LyXParagraph * p = par->next();
228 par = new LyXParagraph;
231 token = lex.GetString();
234 if (token == "\\end_inset")
236 if (const_cast<Buffer*>(buf)->
237 parseSingleLyXformat2Token(lex, par, return_par,token, pos, depth,
240 , footnoteflag, footnotekind
244 // the_end read this should NEVER happen
245 lex.printError("\\the_end read in inset! Error in document!");
253 return_par->SetInsetOwner(this);
255 return_par = return_par->next_;
257 return_par = return_par->next();
261 if (token != "\\end_inset") {
262 lex.printError("Missing \\end_inset at this point. "
269 int InsetText::ascent(BufferView * bv, LyXFont const &) const
272 Row * row = TEXT(bv)->GetRowNearY(y_temp);
273 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
278 int InsetText::descent(BufferView * bv, LyXFont const &) const
281 Row * row = TEXT(bv)->GetRowNearY(y_temp);
282 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
283 TEXT_TO_INSET_OFFSET;
288 int InsetText::width(BufferView * bv, LyXFont const &) const
290 insetWidth = max(textWidth(bv->painter()),
291 (int)TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET));
296 int InsetText::textWidth(Painter & pain) const
298 int const w = getMaxWidth(pain, this);
303 void InsetText::draw(BufferView * bv, LyXFont const & f,
304 int baseline, float & x, bool cleared) const
306 Painter & pain = bv->painter();
308 // no draw is necessary !!!
309 if ((drawFrame == LOCKED) && !locked && !par->size()) {
310 if (!cleared && (need_update & CLEAR_FRAME)) {
311 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
313 insetAscent + insetDescent - 1,
317 top_baseline = baseline;
324 UpdatableInset::draw(bv, f, baseline, x, cleared);
326 // if top_x differs we have a rule down and we don't have to clear anything
327 if (!cleared && (top_x == int(x)) &&
328 ((need_update==INIT)||(need_update==FULL)||(top_baseline!=baseline)))
331 int h = insetAscent + insetDescent;
332 int ty = baseline - insetAscent;
338 if ((ty + h) > pain.paperHeight())
339 h = pain.paperHeight();
340 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
341 w = pain.paperWidth();
342 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
346 if (!cleared && (need_update == NONE))
349 if (top_x != int(x)) {
352 bv->text->status = LyXText::CHANGED_IN_DRAW;
356 top_baseline = baseline;
357 top_y = baseline - ascent(bv, f);
358 last_width = width(bv, f);
359 last_height = ascent(bv, f) + descent(bv, f);
361 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
362 inset_x = cx(bv) - top_x + drawTextXOffset;
363 inset_y = cy(bv) + drawTextYOffset;
365 if (!cleared && (need_update == CURSOR) && !TEXT(bv)->selection) {
370 x += TEXT_TO_INSET_OFFSET;
373 Row * row = TEXT(bv)->GetRowNearY(y);
374 int y_offset = baseline - row->ascent_of_text();
375 int ph = pain.paperHeight();
378 while ((row != 0) && ((y+row->height()) <= 0)) {
380 first += row->height();
385 TEXT(bv)->first = first;
386 if (cleared) { // (need_update&FULL) || (need_update&INIT)
389 while ((row != 0) && (yf < ph)) {
390 TEXT(bv)->GetVisibleRow(bv, y+y_offset, int(x), row,
396 } else if (!locked) {
397 if (need_update & CURSOR) {
398 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
399 TEXT(bv)->ClearSelection(bv);
400 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
402 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
405 if (need_update & SELECTION)
406 bv->screen()->ToggleToggle(TEXT(bv), bv, y_offset, int(x));
407 else if (need_update & CURSOR) {
408 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
409 TEXT(bv)->ClearSelection(bv);
410 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
412 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
416 TEXT(bv)->refresh_y = 0;
417 TEXT(bv)->status = LyXText::UNCHANGED;
418 if ((need_update != CURSOR_PAR) &&
419 ((drawFrame == ALWAYS) || ((drawFrame == LOCKED) && locked)))
421 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
422 width(bv, f) - 1, insetAscent + insetDescent - 1,
424 } else if (need_update & CLEAR_FRAME) {
425 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
426 width(bv, f) - 1, insetAscent + insetDescent - 1,
429 x += width(bv, f) - TEXT_TO_INSET_OFFSET;
430 if (bv->text->status==LyXText::CHANGED_IN_DRAW) {
432 } else if (need_update != INIT)
437 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
439 if (reinit) { // && (need_update != CURSOR)) {
443 owner()->update(bv, font, true);
446 if (the_locking_inset) {
447 inset_x = cx(bv) - top_x + drawTextXOffset;
448 inset_y = cy(bv) + drawTextYOffset;
449 the_locking_inset->update(bv, font, reinit);
451 if (need_update == INIT) {
455 int oldw = insetWidth;
457 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
458 // max(textWidth(bv->painter()),
459 // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
460 // (2 * TEXT_TO_INSET_OFFSET);
462 insetWidth = textWidth(bv->painter());
464 insetWidth = static_cast<int>(TEXT(bv)->width);
466 if (oldw != insetWidth) {
467 // printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
468 // textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
471 update(bv, font, reinit);
474 if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
477 TEXT(bv)->UpdateInset(bv, the_locking_inset);
480 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
484 Row * row = TEXT(bv)->GetRowNearY(y_temp);
485 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
486 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
487 TEXT_TO_INSET_OFFSET;
490 void InsetText::SetUpdateStatus(BufferView * bv, int what)
493 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
495 else if (TEXT(bv)->status == LyXText::NEED_VERY_LITTLE_REFRESH)
496 need_update |= CURSOR_PAR;
498 // this to not draw a selection when we redraw all of it!
499 if ((need_update & (INIT|FULL)) && (need_update & CURSOR))
500 TEXT(bv)->ClearSelection(bv);
503 void InsetText::UpdateLocal(BufferView * bv, int what, bool mark_dirty)
505 TEXT(bv)->FullRebreak(bv);
506 SetUpdateStatus(bv, what);
507 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
509 bv->updateInset(this, mark_dirty);
510 bv->owner()->showState();
511 if (old_par != cpar(bv)) {
512 bv->owner()->setLayout(cpar(bv)->GetLayout());
518 string const InsetText::EditMessage() const
520 return _("Opened Text Inset");
524 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
526 // par->SetInsetOwner(this);
527 UpdatableInset::Edit(bv, x, y, button);
529 if (!bv->lockInset(this)) {
530 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
534 the_locking_inset = 0;
535 inset_pos = inset_x = inset_y = 0;
536 inset_boundary = false;
539 if (!checkAndActivateInset(bv, x, y, button))
540 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
542 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
543 bv->text->FinishUndo();
545 UpdateLocal(bv, FULL, false);
547 // If the inset is empty set the language of the current font to the
548 // language to the surronding text.
550 if (par->Last() == 0 && !par->next_) {
552 if (par->Last() == 0 && !par->next()) {
554 LyXFont font(LyXFont::ALL_IGNORE);
555 font.setLanguage(bv->getParentLanguage(this));
556 SetFont(bv, font, false);
561 void InsetText::InsetUnlock(BufferView * bv)
563 if (the_locking_inset) {
564 the_locking_inset->InsetUnlock(bv);
565 the_locking_inset = 0;
568 no_selection = false;
570 UpdateLocal(bv, CLEAR_FRAME|CURSOR, false);
572 bv->owner()->setLayout(owner()->getLyXText(bv)
573 ->cursor.par()->GetLayout());
575 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
579 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
581 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
584 if (inset == cpar(bv)->GetInset(cpos(bv))) {
585 lyxerr[Debug::INSETS] << "OK" << endl;
586 the_locking_inset = inset;
587 inset_x = cx(bv) - top_x + drawTextXOffset;
588 inset_y = cy(bv) + drawTextYOffset;
589 inset_pos = cpos(bv);
590 inset_par = cpar(bv);
591 inset_boundary = cboundary(bv);
593 TEXT(bv)->ClearSelection(bv);
594 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
595 TEXT(bv)->UpdateInset(bv, the_locking_inset);
597 UpdateLocal(bv, CURSOR, false);
600 } else if (the_locking_inset && (the_locking_inset == inset)) {
601 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
602 lyxerr[Debug::INSETS] << "OK" << endl;
603 inset_x = cx(bv) - top_x + drawTextXOffset;
604 inset_y = cy(bv) + drawTextYOffset;
606 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
608 } else if (the_locking_inset) {
609 lyxerr[Debug::INSETS] << "MAYBE" << endl;
610 return the_locking_inset->LockInsetInInset(bv, inset);
612 lyxerr[Debug::INSETS] << "NOT OK" << endl;
617 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
620 if (!the_locking_inset)
622 if (the_locking_inset == inset) {
623 the_locking_inset->InsetUnlock(bv);
624 TEXT(bv)->UpdateInset(bv, inset);
625 the_locking_inset = 0;
627 moveRight(bv, false);
628 old_par = 0; // force layout setting
629 UpdateLocal(bv, CURSOR_PAR, false);
632 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
636 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
638 if (!the_locking_inset)
640 if (the_locking_inset != inset) {
641 TEXT(bv)->UpdateInset(bv, the_locking_inset);
642 SetUpdateStatus(bv, CURSOR_PAR);
643 return the_locking_inset->UpdateInsetInInset(bv, inset);
645 // UpdateLocal(bv, FULL, false);
646 if (TEXT(bv)->UpdateInset(bv, inset))
647 UpdateLocal(bv, CURSOR_PAR, false);
648 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
649 inset_x = cx(bv) - top_x + drawTextXOffset;
650 inset_y = cy(bv) + drawTextYOffset;
656 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
660 int tmp_x = x - drawTextXOffset;
661 int tmp_y = y + insetAscent - TEXT(bv)->first;
662 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
665 if (the_locking_inset) {
666 if (the_locking_inset == inset) {
667 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
668 no_selection = false;
671 // otherwise unlock the_locking_inset and lock the new inset
672 the_locking_inset->InsetUnlock(bv);
673 inset_x = cx(bv) - top_x + drawTextXOffset;
674 inset_y = cy(bv) + drawTextYOffset;
675 the_locking_inset = static_cast<UpdatableInset*>(inset);
676 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
677 inset->Edit(bv, x - inset_x, y - inset_y, button);
678 if (the_locking_inset)
679 UpdateLocal(bv, CURSOR, false);
680 no_selection = false;
683 // otherwise only unlock the_locking_inset
684 the_locking_inset->InsetUnlock(bv);
685 the_locking_inset = 0;
687 if (bv->theLockingInset()) {
688 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
689 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
690 inset_x = cx(bv) - top_x + drawTextXOffset;
691 inset_y = cy(bv) + drawTextYOffset;
692 inset_pos = cpos(bv);
693 inset_par = cpar(bv);
694 inset_boundary = cboundary(bv);
695 the_locking_inset = uinset;
696 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
697 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
698 if (the_locking_inset)
699 UpdateLocal(bv, CURSOR, false);
700 no_selection = false;
705 bool paste_internally = false;
706 if ((button == 2) && TEXT(bv)->selection) {
707 LocalDispatch(bv, LFUN_COPY, "");
708 paste_internally = true;
710 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
712 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
713 UpdateLocal(bv, CURSOR, false);
714 bv->owner()->setLayout(cpar(bv)->GetLayout());
716 // Insert primary selection with middle mouse
717 // if there is a local selection in the current buffer,
720 if (paste_internally)
721 LocalDispatch(bv, LFUN_PASTE, "");
723 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
727 no_selection = false;
731 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
733 UpdatableInset * inset = 0;
735 if (the_locking_inset) {
736 the_locking_inset->InsetButtonRelease(bv,
737 x - inset_x, y - inset_y,
740 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
741 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
742 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
743 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
745 inset_x = cx(bv) - top_x + drawTextXOffset;
746 inset_y = cy(bv) + drawTextYOffset;
747 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
748 inset->Edit(bv, x - inset_x, y - inset_y, button);
750 UpdateLocal(bv, CURSOR_PAR, false);
753 no_selection = false;
757 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
761 if (the_locking_inset) {
762 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
767 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
769 TEXT(bv)->SetSelection(bv);
770 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
771 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
772 UpdateLocal(bv, SELECTION, false);
777 void InsetText::InsetKeyPress(XKeyEvent * xke)
779 if (the_locking_inset) {
780 the_locking_inset->InsetKeyPress(xke);
786 UpdatableInset::RESULT
787 InsetText::LocalDispatch(BufferView * bv,
788 kb_action action, string const & arg)
790 no_selection = false;
791 UpdatableInset::RESULT
792 result= UpdatableInset::LocalDispatch(bv, action, arg);
793 if (result != UNDISPATCHED) {
798 if ((action < 0) && arg.empty())
801 if (the_locking_inset) {
802 result = the_locking_inset->LocalDispatch(bv, action, arg);
803 if (result == DISPATCHED_NOUPDATE)
805 else if (result == DISPATCHED) {
806 UpdateLocal(bv, CURSOR_PAR, false);
808 } else if (result == FINISHED) {
809 bool dispatched = false;
811 case LFUN_UNKNOWN_ACTION:
812 case LFUN_BREAKPARAGRAPH:
814 moveRightIntern(bv, false, false);
817 if (!TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
818 moveRightIntern(bv, false, false);
822 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
823 moveRightIntern(bv, false, false);
829 the_locking_inset = 0;
837 case LFUN_UNKNOWN_ACTION:
838 if (bv->buffer()->isReadonly()) {
839 // setErrorMessage(N_("Document is read only"));
843 /* Automatically delete the currently selected
844 * text and replace it with what is being
845 * typed in now. Depends on lyxrc settings
846 * "auto_region_delete", which defaults to
849 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
851 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
852 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
854 bv->text->cursor.par()->previous(),
855 bv->text->cursor.par()->next()
859 if (lyxrc.auto_region_delete) {
860 if (TEXT(bv)->selection){
861 TEXT(bv)->CutSelection(bv, false);
864 TEXT(bv)->ClearSelection(bv);
865 for (string::size_type i = 0; i < arg.length(); ++i) {
867 if (!math_insert_greek(bv, arg[i])) {
868 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
869 } else if (!the_locking_inset) {
870 (void)moveRight(bv, false);
873 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
877 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
878 UpdateLocal(bv, CURSOR_PAR, true);
879 result=DISPATCHED_NOUPDATE;
881 // --- Cursor Movements ---------------------------------------------
883 bv->text->FinishUndo();
884 moveRight(bv, false, true);
885 TEXT(bv)->SetSelection(bv);
886 UpdateLocal(bv, SELECTION, false);
889 result = moveRight(bv);
890 bv->text->FinishUndo();
891 UpdateLocal(bv, CURSOR, false);
894 bv->text->FinishUndo();
895 moveLeft(bv, false, true);
896 TEXT(bv)->SetSelection(bv);
897 UpdateLocal(bv, SELECTION, false);
900 bv->text->FinishUndo();
901 result = moveLeft(bv);
902 UpdateLocal(bv, CURSOR, false);
905 bv->text->FinishUndo();
907 TEXT(bv)->SetSelection(bv);
908 UpdateLocal(bv, SELECTION, false);
911 bv->text->FinishUndo();
912 result = moveDown(bv);
913 UpdateLocal(bv, CURSOR, false);
916 bv->text->FinishUndo();
918 TEXT(bv)->SetSelection(bv);
919 UpdateLocal(bv, SELECTION, false);
922 bv->text->FinishUndo();
924 UpdateLocal(bv, CURSOR, false);
927 bv->text->FinishUndo();
928 TEXT(bv)->CursorHome(bv);
929 UpdateLocal(bv, CURSOR, false);
932 TEXT(bv)->CursorEnd(bv);
933 UpdateLocal(bv, CURSOR, false);
936 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
938 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
939 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
941 bv->text->cursor.par()->previous(),
942 bv->text->cursor.par()->next()
945 if (TEXT(bv)->selection)
946 TEXT(bv)->CutSelection(bv);
948 TEXT(bv)->Backspace(bv);
949 UpdateLocal(bv, CURSOR_PAR, true);
952 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
954 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
955 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
957 bv->text->cursor.par()->previous(),
958 bv->text->cursor.par()->next()
961 if (TEXT(bv)->selection)
962 TEXT(bv)->CutSelection(bv);
964 TEXT(bv)->Delete(bv);
965 UpdateLocal(bv, CURSOR_PAR, true);
968 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
970 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
971 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
973 bv->text->cursor.par()->previous(),
974 bv->text->cursor.par()->next()
977 TEXT(bv)->CutSelection(bv);
978 UpdateLocal(bv, CURSOR_PAR, true);
981 bv->text->FinishUndo();
982 TEXT(bv)->CopySelection(bv);
983 UpdateLocal(bv, CURSOR_PAR, false);
985 case LFUN_PASTESELECTION:
987 string clip(bv->workarea()->getClipboard());
991 if (arg == "paragraph") {
992 TEXT(bv)->InsertStringB(bv, clip);
994 TEXT(bv)->InsertStringA(bv, clip);
996 UpdateLocal(bv, CURSOR_PAR, true);
1000 if (!autoBreakRows) {
1003 if (cap.nrOfParagraphs() > 1) {
1004 WriteAlert(_("Impossible operation"),
1005 _("Cannot include more than one paragraph!"),
1010 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
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)->PasteSelection(bv);
1020 UpdateLocal(bv, CURSOR_PAR, true);
1022 case LFUN_BREAKPARAGRAPH:
1025 TEXT(bv)->BreakParagraph(bv, 0);
1026 UpdateLocal(bv, FULL, true);
1028 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1031 TEXT(bv)->BreakParagraph(bv, 1);
1032 UpdateLocal(bv, FULL, true);
1034 case LFUN_BREAKLINE:
1037 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1039 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1040 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1042 bv->text->cursor.par()->previous(),
1043 bv->text->cursor.par()->next()
1046 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
1047 UpdateLocal(bv, CURSOR_PAR, true);
1050 // do not set layouts on non breakable textinsets
1051 if (autoBreakRows) {
1052 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1054 // Derive layout number from given argument (string)
1055 // and current buffer's textclass (number). */
1056 LyXTextClassList::ClassList::size_type tclass =
1057 bv->buffer()->params.textclass;
1058 std::pair <bool, LyXTextClass::size_type> layout =
1059 textclasslist.NumberOfLayout(tclass, arg);
1061 // If the entry is obsolete, use the new one instead.
1063 string obs = textclasslist.Style(tclass,layout.second).
1066 layout = textclasslist.NumberOfLayout(tclass, obs);
1069 // see if we found the layout number:
1070 if (!layout.first) {
1071 string msg = string(N_("Layout ")) + arg + N_(" not known");
1073 bv->owner()->getMiniBuffer()->Set(msg);
1077 if (cur_layout != layout.second) {
1078 cur_layout = layout.second;
1079 TEXT(bv)->SetLayout(bv, layout.second);
1080 bv->owner()->setLayout(cpar(bv)->GetLayout());
1081 UpdateLocal(bv, CURSOR_PAR, true);
1084 // reset the layout box
1085 bv->owner()->setLayout(cpar(bv)->GetLayout());
1088 case LFUN_PARAGRAPH_SPACING:
1089 // This one is absolutely not working. When fiddling with this
1090 // it also seems to me that the paragraphs inside the insettext
1091 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1093 LyXParagraph * par = TEXT(bv)->cursor.par();
1094 Spacing::Space cur_spacing = par->params.spacing().getSpace();
1095 float cur_value = 1.0;
1096 if (cur_spacing == Spacing::Other) {
1097 cur_value = par->params.spacing().getValue();
1100 std::istringstream istr(arg.c_str());
1103 Spacing::Space new_spacing = cur_spacing;
1104 float new_value = cur_value;
1106 lyxerr << "Missing argument to `paragraph-spacing'"
1108 } else if (tmp == "single") {
1109 new_spacing = Spacing::Single;
1110 } else if (tmp == "onehalf") {
1111 new_spacing = Spacing::Onehalf;
1112 } else if (tmp == "double") {
1113 new_spacing = Spacing::Double;
1114 } else if (tmp == "other") {
1115 new_spacing = Spacing::Other;
1118 lyxerr << "new_value = " << tmpval << endl;
1121 } else if (tmp == "default") {
1122 new_spacing = Spacing::Default;
1124 lyxerr << _("Unknown spacing argument: ")
1127 if (cur_spacing != new_spacing || cur_value != new_value) {
1128 par->params.spacing(Spacing(new_spacing, new_value));
1129 //TEXT(bv)->RedoParagraph(owner->view());
1130 UpdateLocal(bv, CURSOR_PAR, true);
1131 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1137 if (!bv->Dispatch(action, arg))
1138 result = UNDISPATCHED;
1142 /// If the action has deleted all text in the inset, we need to change the
1143 // language to the language to the surronding text.
1145 if (par->Last() == 0 && !par->next_) {
1147 if (par->Last() == 0 && !par->next()) {
1149 LyXFont font(LyXFont::ALL_IGNORE);
1150 font.setLanguage(bv->getParentLanguage(this));
1151 SetFont(bv, font, false);
1154 if (result != FINISHED) {
1155 ShowInsetCursor(bv);
1157 bv->unlockInset(this);
1162 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1165 buf->latexParagraphs(os, par, 0, texrow);
1166 return texrow.rows();
1170 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1172 LyXParagraph * p = par;
1173 unsigned int lines = 0;
1177 tmp = buf->asciiParagraph(p, linelen);
1178 lines += countChar(tmp, '\n');
1190 int InsetText::DocBook(Buffer const * buf, ostream & os) const
1192 LyXParagraph * p = par;
1193 unsigned int lines = 0;
1198 buf->SimpleDocBookOnePar(os,tmp,p,desc,0);
1210 void InsetText::Validate(LaTeXFeatures & features) const
1212 LyXParagraph * p = par;
1214 p->validate(features);
1224 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1226 if (textclasslist.Style(buf->params.textclass,
1227 p->GetLayout()).labeltype != LABEL_MANUAL)
1230 return p->BeginningOfMainBody();
1234 void InsetText::GetCursorPos(BufferView * bv,
1235 int & x, int & y) const
1242 unsigned int InsetText::InsetInInsetY()
1244 if (!the_locking_inset)
1247 return (inset_y + the_locking_inset->InsetInInsetY());
1251 void InsetText::ToggleInsetCursor(BufferView * bv)
1253 if (the_locking_inset) {
1254 the_locking_inset->ToggleInsetCursor(bv);
1258 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1260 int asc = lyxfont::maxAscent(font);
1261 int desc = lyxfont::maxDescent(font);
1264 bv->hideLockedInsetCursor();
1266 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1267 cursor_visible = !cursor_visible;
1271 void InsetText::ShowInsetCursor(BufferView * bv, bool show)
1273 if (the_locking_inset) {
1274 the_locking_inset->ShowInsetCursor(bv);
1277 if (!cursor_visible) {
1278 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1280 int asc = lyxfont::maxAscent(font);
1281 int desc = lyxfont::maxDescent(font);
1283 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1285 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1286 cursor_visible = true;
1291 void InsetText::HideInsetCursor(BufferView * bv)
1293 if (cursor_visible) {
1294 bv->hideLockedInsetCursor();
1295 cursor_visible = false;
1297 if (the_locking_inset)
1298 the_locking_inset->HideInsetCursor(bv);
1302 UpdatableInset::RESULT
1303 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1305 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1306 return moveLeftIntern(bv, false, activate_inset, selecting);
1308 return moveRightIntern(bv, false, activate_inset, selecting);
1311 UpdatableInset::RESULT
1312 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1314 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1315 return moveRightIntern(bv, true, activate_inset, selecting);
1317 return moveLeftIntern(bv, true, activate_inset, selecting);
1321 UpdatableInset::RESULT
1322 InsetText::moveRightIntern(BufferView * bv, bool behind,
1323 bool activate_inset, bool selecting)
1326 if (!cpar(bv)->next_ && (cpos(bv) >= cpar(bv)->Last()))
1328 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->Last()))
1331 if (activate_inset && checkAndActivateInset(bv, behind))
1333 TEXT(bv)->CursorRight(bv);
1335 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1336 return DISPATCHED_NOUPDATE;
1340 UpdatableInset::RESULT
1341 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1342 bool activate_inset, bool selecting)
1345 if (!cpar(bv)->previous_ && (cpos(bv) <= 0))
1347 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1350 TEXT(bv)->CursorLeft(bv);
1352 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1353 if (activate_inset && checkAndActivateInset(bv, behind))
1355 return DISPATCHED_NOUPDATE;
1359 UpdatableInset::RESULT
1360 InsetText::moveUp(BufferView * bv)
1362 if (!crow(bv)->previous())
1364 TEXT(bv)->CursorUp(bv);
1365 return DISPATCHED_NOUPDATE;
1369 UpdatableInset::RESULT
1370 InsetText::moveDown(BufferView * bv)
1372 if (!crow(bv)->next())
1374 TEXT(bv)->CursorDown(bv);
1375 return DISPATCHED_NOUPDATE;
1379 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1381 if (the_locking_inset) {
1382 if (the_locking_inset->InsertInsetAllowed(inset))
1383 return the_locking_inset->InsertInset(bv, inset);
1386 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1388 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1389 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1391 bv->text->cursor.par()->previous(),
1392 bv->text->cursor.par()->next()
1395 inset->setOwner(this);
1396 HideInsetCursor(bv);
1397 TEXT(bv)->InsertInset(bv, inset);
1399 if ((cpar(bv)->GetChar(cpos(bv)) != LyXParagraph::META_INSET) ||
1400 (cpar(bv)->GetInset(cpos(bv)) != inset))
1401 TEXT(bv)->CursorLeft(bv);
1403 bv->fitCursor(TEXT(bv));
1404 UpdateLocal(bv, CURSOR_PAR|CURSOR, true);
1405 ShowInsetCursor(bv);
1410 UpdatableInset * InsetText::GetLockingInset()
1412 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1416 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1420 if (the_locking_inset)
1421 return the_locking_inset->GetFirstLockingInsetOfType(c);
1426 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1428 if (TEXT(bv)->selection) {
1429 bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1431 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1432 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1434 bv->text->cursor.par()->previous(),
1435 bv->text->cursor.par()->next()
1439 TEXT(bv)->SetFont(bv, font, toggleall);
1440 bv->fitCursor(TEXT(bv));
1441 UpdateLocal(bv, CURSOR_PAR, true);
1445 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1447 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1451 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1452 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1454 LyXFont const font =
1455 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1457 x = inset->width(bv, font);
1458 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1461 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1463 //inset_x = cx(bv) - top_x + drawTextXOffset;
1464 //inset_y = cy(bv) + drawTextYOffset;
1465 inset->Edit(bv, x, y, 0);
1466 if (!the_locking_inset)
1468 UpdateLocal(bv, CURSOR_PAR, false);
1475 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1478 x = x - drawTextXOffset;
1479 y = y + insetAscent;
1480 Inset * inset = bv->checkInsetHit(TEXT(bv), x, y, button);
1487 inset_x = cx(bv) - top_x + drawTextXOffset;
1488 inset_y = cy(bv) + drawTextYOffset;
1489 inset->Edit(bv, x - inset_x, y - inset_y, button);
1490 if (!the_locking_inset)
1492 UpdateLocal(bv, CURSOR_PAR, false);
1499 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1501 int w = UpdatableInset::getMaxWidth(pain, inset);
1506 w = w - top_x + owner()->x();
1509 w -= (2 * TEXT_TO_INSET_OFFSET);
1511 // return w - (2*TEXT_TO_INSET_OFFSET);
1515 void InsetText::SetParagraphData(LyXParagraph * p)
1517 // delete all instances of LyXText before deleting the paragraps used
1519 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
1520 delete (*cit).second;
1536 par->SetInsetOwner(this);
1540 np->next(p->Clone());
1541 np->next_->previous(np);
1543 np->SetInsetOwner(this);
1557 par->SetInsetOwner(this);
1561 np->next(p->Clone());
1562 np->next()->previous(np);
1564 np->SetInsetOwner(this);
1571 void InsetText::SetText(string const & data)
1574 LyXFont font(LyXFont::ALL_SANE);
1575 for(unsigned int i=0; i < data.length(); ++i)
1576 par->InsertChar(i, data[i], font);
1580 void InsetText::SetAutoBreakRows(bool flag)
1582 if (flag != autoBreakRows) {
1583 autoBreakRows = flag;
1591 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1593 if (how != drawFrame) {
1596 UpdateLocal(bv, DRAW_FRAME, false);
1601 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1603 if (frame_color != col) {
1606 UpdateLocal(bv, DRAW_FRAME, false);
1611 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1613 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1618 int InsetText::cx(BufferView * bv) const
1620 LyXText * text = TEXT(bv);
1621 int x = text->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1622 if (the_locking_inset) {
1623 LyXFont font = text->GetFont(bv->buffer(),
1624 text->cursor.par(), text->cursor.pos());
1625 if (font.isVisibleRightToLeft())
1626 x -= the_locking_inset->width(bv, font);
1632 int InsetText::cy(BufferView * bv) const
1635 return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1639 LyXParagraph::size_type InsetText::cpos(BufferView * bv) const
1641 return TEXT(bv)->cursor.pos();
1645 LyXParagraph * InsetText::cpar(BufferView * bv) const
1647 return TEXT(bv)->cursor.par();
1650 bool InsetText::cboundary(BufferView * bv) const
1652 return TEXT(bv)->cursor.boundary();
1656 Row * InsetText::crow(BufferView * bv) const
1658 return TEXT(bv)->cursor.row();
1662 LyXText * InsetText::getLyXText(BufferView const * lbv, bool const recursive) const
1664 // Super UGLY! (Lgb)
1665 BufferView * bv = const_cast<BufferView *>(lbv);
1667 if ((cache.find(bv) != cache.end()) && cache[bv]) {
1668 if (recursive && the_locking_inset)
1669 return the_locking_inset->getLyXText(bv);
1672 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1675 if (the_locking_inset) {
1676 lt->SetCursor(bv, inset_par, inset_pos, true, inset_boundary);
1678 return the_locking_inset->getLyXText(bv);
1684 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1686 if ((cache.find(bv) == cache.end()) || !cache[bv])
1691 /// then remove all LyXText in text-insets
1692 LyXParagraph * p = par;
1694 for (; p; p = p->next_) {
1695 p->deleteInsetsLyXText(bv);
1698 for (; p; p = p->next()) {
1699 p->deleteInsetsLyXText(bv);
1706 void InsetText::resizeLyXText(BufferView * bv) const
1709 if (!par->next_ && !par->size()) // resize not neccessary!
1711 if (!par->next() && !par->size()) // resize not neccessary!
1714 if ((cache.find(bv) == cache.end()) || !cache[bv])
1717 LyXParagraph * lpar = 0;
1718 LyXParagraph * selstartpar = 0;
1719 LyXParagraph * selendpar = 0;
1720 LyXParagraph::size_type pos = 0;
1721 LyXParagraph::size_type selstartpos = 0;
1722 LyXParagraph::size_type selendpos = 0;
1723 bool boundary = false;
1724 bool selstartboundary = false;
1725 bool selendboundary = false;
1729 // ProhibitInput(bv);
1732 lpar = TEXT(bv)->cursor.par();
1733 pos = TEXT(bv)->cursor.pos();
1734 boundary = TEXT(bv)->cursor.boundary();
1735 selstartpar = TEXT(bv)->sel_start_cursor.par();
1736 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1737 selstartboundary = TEXT(bv)->sel_start_cursor.boundary();
1738 selendpar = TEXT(bv)->sel_end_cursor.par();
1739 selendpos = TEXT(bv)->sel_end_cursor.pos();
1740 selendboundary = TEXT(bv)->sel_end_cursor.boundary();
1741 selection = TEXT(bv)->selection;
1742 mark_set = TEXT(bv)->mark_set;
1744 deleteLyXText(bv, (the_locking_inset == 0));
1747 TEXT(bv)->selection = true;
1748 /* at this point just to avoid the Delete-Empty-Paragraph
1749 * Mechanism when setting the cursor */
1750 TEXT(bv)->mark_set = mark_set;
1752 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos,true,
1754 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1755 TEXT(bv)->SetCursor(bv, selendpar, selendpos, true, selendboundary);
1756 TEXT(bv)->SetSelection(bv);
1757 TEXT(bv)->SetCursor(bv, lpar, pos);
1759 TEXT(bv)->SetCursor(bv, lpar, pos, true, boundary);
1760 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1761 TEXT(bv)->selection = false;
1765 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1766 // this will scroll the screen such that the cursor becomes visible
1767 bv->updateScrollbar();
1769 if (the_locking_inset) {
1770 /// then resize all LyXText in text-insets
1771 inset_x = cx(bv) - top_x + drawTextXOffset;
1772 inset_y = cy(bv) + drawTextYOffset;
1774 for (LyXParagraph * p = par; p; p = p->next_) {
1775 p->resizeInsetsLyXText(bv);
1778 for (LyXParagraph * p = par; p; p = p->next()) {
1779 p->resizeInsetsLyXText(bv);
1787 void InsetText::removeNewlines()
1790 for (LyXParagraph * p = par; p; p = p->next_) {
1792 for (LyXParagraph * p = par; p; p = p->next()) {
1794 for (int i = 0; i < p->Last(); ++i) {
1795 if (p->GetChar(i) == LyXParagraph::META_NEWLINE)