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"
56 extern unsigned char getCurrentTextClass(Buffer *);
58 InsetText::InsetText()
60 par = new LyXParagraph();
65 InsetText::InsetText(InsetText const & ins)
70 autoBreakRows = ins.autoBreakRows;
74 InsetText & InsetText::operator=(InsetText const & it)
77 autoBreakRows = it.autoBreakRows;
82 void InsetText::init(InsetText const * ins)
84 top_y = last_width = last_height = 0;
85 insetAscent = insetDescent = insetWidth = 0;
86 the_locking_inset = 0;
87 cursor_visible = false;
91 drawTextXOffset = drawTextYOffset = 0;
92 autoBreakRows = false;
96 SetParagraphData(ins->par);
97 autoBreakRows = ins->autoBreakRows;
98 drawFrame = ins->drawFrame;
100 par->SetInsetOwner(this);
101 frame_color = LColor::insetframe;
107 InsetText::~InsetText()
109 LyXParagraph * p = par->next;
119 void InsetText::clear()
121 LyXParagraph * p = par->next;
128 par = new LyXParagraph();
132 Inset * InsetText::Clone() const
134 InsetText * t = new InsetText(*this);
139 void InsetText::Write(Buffer const * buf, ostream & os) const
142 WriteParagraphData(buf, os);
146 void InsetText::WriteParagraphData(Buffer const * buf, ostream & os) const
148 par->writeFile(buf, os, buf->params, 0, 0);
152 void InsetText::Read(Buffer const * buf, LyXLex & lex)
156 LyXParagraph * return_par = 0;
157 char depth = 0; // signed or unsigned?
159 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
160 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
162 LyXFont font(LyXFont::ALL_INHERIT);
164 LyXParagraph * p = par->next;
171 par = new LyXParagraph;
174 token = lex.GetString();
177 if (token == "\\end_inset")
179 if (const_cast<Buffer*>(buf)->
180 parseSingleLyXformat2Token(lex, par, return_par,token, pos, depth,
183 , footnoteflag, footnotekind
187 // the_end read this should NEVER happen
188 lex.printError("\\the_end read in inset! Error in document!");
196 return_par->SetInsetOwner(this);
197 return_par = return_par->next;
200 if (token != "\\end_inset") {
201 lex.printError("Missing \\end_inset at this point. "
208 int InsetText::ascent(BufferView * bv, LyXFont const &) const
211 Row * row = TEXT(bv)->GetRowNearY(y_temp);
212 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
217 int InsetText::descent(BufferView * bv, LyXFont const &) const
220 Row * row = TEXT(bv)->GetRowNearY(y_temp);
221 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
222 TEXT_TO_INSET_OFFSET;
227 int InsetText::width(BufferView * bv, LyXFont const &) const
229 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
234 int InsetText::textWidth(Painter & pain) const
236 int w = getMaxWidth(pain, this);
241 void InsetText::draw(BufferView * bv, LyXFont const & f,
242 int baseline, float & x, bool cleared) const
244 Painter & pain = bv->painter();
246 // no draw is necessary !!!
247 if ((drawFrame == LOCKED) && !locked && !par->size()) {
248 if (!cleared && (need_update == CLEAR_FRAME)) {
249 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
251 insetAscent + insetDescent - 1,
255 top_baseline = baseline;
262 UpdatableInset::draw(bv, f, baseline, x, cleared);
264 if (!cleared && ((need_update==FULL) || (top_x!=int(x)) ||
265 (top_baseline!=baseline))) {
267 int h = insetAscent + insetDescent;
268 int ty = baseline - insetAscent;
274 if ((ty + h) > pain.paperHeight())
275 h = pain.paperHeight();
276 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
277 w = pain.paperWidth();
278 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
282 if (!cleared && (need_update == NONE))
285 if (top_x != int(x)) {
288 bv->text->status = LyXText::CHANGED_IN_DRAW;
292 top_baseline = baseline;
293 top_y = baseline - ascent(bv, f);
294 last_width = width(bv, f);
295 last_height = ascent(bv, f) + descent(bv, f);
297 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
298 inset_x = cx(bv) - top_x + drawTextXOffset;
299 inset_y = cy(bv) + drawTextYOffset;
301 if (!cleared && (need_update == CURSOR) && !TEXT(bv)->selection) {
306 x += TEXT_TO_INSET_OFFSET;
308 Row * row = TEXT(bv)->GetRowNearY(y);
309 y += baseline - row->ascent_of_text();
310 if (cleared || !locked || (need_update == FULL)) {
312 TEXT(bv)->GetVisibleRow(bv, y, int(x), row, y, cleared);
316 } else if (need_update == SELECTION) {
317 bv->screen()->ToggleToggle(TEXT(bv), y, int(x));
320 if (need_update == CURSOR) {
321 bv->screen()->ToggleSelection(TEXT(bv), true, y, int(x));
322 TEXT(bv)->ClearSelection();
323 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
325 bv->screen()->Update(TEXT(bv), y, int(x));
328 TEXT(bv)->refresh_y = 0;
329 TEXT(bv)->status = LyXText::UNCHANGED;
330 if ((drawFrame == ALWAYS) || ((drawFrame == LOCKED) && locked)) {
331 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
332 width(bv, f) - 1, insetAscent + insetDescent - 1,
334 } else if (need_update == CLEAR_FRAME) {
335 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
336 width(bv, f) - 1, insetAscent + insetDescent - 1,
339 x += width(bv, f) - TEXT_TO_INSET_OFFSET;
340 if (bv->text->status==LyXText::CHANGED_IN_DRAW)
342 else if (need_update != INIT)
347 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
349 if (reinit) { // && (need_update != CURSOR)) {
353 owner()->update(bv, font, true);
356 if (the_locking_inset) {
357 inset_x = cx(bv) - top_x + drawTextXOffset;
358 inset_y = cy(bv) + drawTextYOffset;
359 the_locking_inset->update(bv, font, reinit);
361 if (need_update == INIT) {
364 // if (!owner() && bv->text)
365 // bv->text->UpdateInset(bv, this);
367 int oldw = insetWidth;
369 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
370 // max(textWidth(bv->painter()),
371 // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
372 // (2 * TEXT_TO_INSET_OFFSET);
374 insetWidth = textWidth(bv->painter());
376 insetWidth = static_cast<int>(TEXT(bv)->width);
378 if (oldw != insetWidth) {
379 // printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
380 // textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
385 owner()->update(bv, font, reinit);
388 update(bv, font, reinit);
392 update(bv, font, reinit);
394 UpdateLocal(bv, INIT, false);
399 if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
402 TEXT(bv)->UpdateInset(bv, the_locking_inset);
405 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
409 Row * row = TEXT(bv)->GetRowNearY(y_temp);
410 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
411 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
412 TEXT_TO_INSET_OFFSET;
416 void InsetText::UpdateLocal(BufferView * bv, UpdateCodes what, bool mark_dirty)
418 TEXT(bv)->FullRebreak(bv);
419 if (need_update != INIT) {
420 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
422 else if (!the_locking_inset || (what != CURSOR))
425 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
427 bv->updateInset(this, mark_dirty);
428 bv->owner()->showState();
429 if (old_par != cpar(bv)) {
430 bv->owner()->setLayout(cpar(bv)->GetLayout());
436 string const InsetText::EditMessage() const
438 return _("Opened Text Inset");
442 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
444 // par->SetInsetOwner(this);
445 UpdatableInset::Edit(bv, x, y, button);
447 if (!bv->lockInset(this)) {
448 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
452 the_locking_inset = 0;
453 inset_pos = inset_x = inset_y = 0;
456 if (!checkAndActivateInset(bv, x, y, button))
457 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
458 y+TEXT(bv)->first+insetAscent);
459 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
460 bv->text->FinishUndo();
461 UpdateLocal(bv, FULL, false);
465 void InsetText::InsetUnlock(BufferView * bv)
467 if (the_locking_inset) {
468 the_locking_inset->InsetUnlock(bv);
469 the_locking_inset = 0;
472 no_selection = false;
474 TEXT(bv)->selection = 0;
475 UpdateLocal(bv, CLEAR_FRAME, false);
477 bv->owner()->setLayout(owner()->getLyXText(bv)
478 ->cursor.par()->GetLayout());
480 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
484 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
486 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
489 if (inset == cpar(bv)->GetInset(cpos(bv))) {
490 lyxerr[Debug::INSETS] << "OK" << endl;
491 the_locking_inset = inset;
492 inset_x = cx(bv) - top_x + drawTextXOffset;
493 inset_y = cy(bv) + drawTextYOffset;
494 inset_pos = cpos(bv);
495 inset_par = cpar(bv);
496 TEXT(bv)->UpdateInset(bv, the_locking_inset);
498 } else if (the_locking_inset && (the_locking_inset == inset)) {
499 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
500 lyxerr[Debug::INSETS] << "OK" << endl;
501 inset_x = cx(bv) - top_x + drawTextXOffset;
502 inset_y = cy(bv) + drawTextYOffset;
504 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
506 } else if (the_locking_inset) {
507 lyxerr[Debug::INSETS] << "MAYBE" << endl;
508 return the_locking_inset->LockInsetInInset(bv, inset);
510 lyxerr[Debug::INSETS] << "NOT OK" << endl;
515 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
518 if (!the_locking_inset)
520 if (the_locking_inset == inset) {
521 the_locking_inset->InsetUnlock(bv);
522 TEXT(bv)->UpdateInset(bv, inset);
523 the_locking_inset = 0;
525 moveRight(bv, false);
526 old_par = 0; // force layout setting
527 UpdateLocal(bv, CURSOR_PAR, false);
530 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
534 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
536 if (!the_locking_inset)
538 if (the_locking_inset != inset) {
539 TEXT(bv)->UpdateInset(bv, the_locking_inset);
540 need_update = CURSOR_PAR;
541 return the_locking_inset->UpdateInsetInInset(bv, inset);
543 // UpdateLocal(bv, FULL, false);
544 if (TEXT(bv)->UpdateInset(bv, inset))
545 UpdateLocal(bv, CURSOR_PAR, false);
546 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
547 inset_x = cx(bv) - top_x + drawTextXOffset;
548 inset_y = cy(bv) + drawTextYOffset;
554 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
556 no_selection = false;
558 int tmp_x = x - drawTextXOffset;
559 int tmp_y = y + insetAscent;
560 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
563 if (the_locking_inset) {
564 if (the_locking_inset == inset) {
565 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
568 // otherwise unlock the_locking_inset and lock the new inset
569 the_locking_inset->InsetUnlock(bv);
570 inset_x = cx(bv) - top_x + drawTextXOffset;
571 inset_y = cy(bv) + drawTextYOffset;
572 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
573 inset->Edit(bv, x - inset_x, y - inset_y, button);
574 if (the_locking_inset) {
575 UpdateLocal(bv, CURSOR_PAR, false);
579 // otherwise only unlock the_locking_inset
580 the_locking_inset->InsetUnlock(bv);
581 the_locking_inset = 0;
583 if (bv->the_locking_inset) {
584 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
585 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
586 inset_x = cx(bv) - top_x + drawTextXOffset;
587 inset_y = cy(bv) + drawTextYOffset;
588 inset_pos = cpos(bv);
589 inset_par = cpar(bv);
590 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
591 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
592 if (the_locking_inset) {
593 UpdateLocal(bv, CURSOR_PAR, false);
599 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
600 y+TEXT(bv)->first+insetAscent);
601 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
602 UpdateLocal(bv, CURSOR, false);
603 bv->owner()->setLayout(cpar(bv)->GetLayout());
610 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
612 UpdatableInset * inset = 0;
614 if (the_locking_inset) {
615 the_locking_inset->InsetButtonRelease(bv,
616 x - inset_x, y - inset_y,
619 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
620 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
621 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
622 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
624 inset_x = cx(bv) - top_x + drawTextXOffset;
625 inset_y = cy(bv) + drawTextYOffset;
626 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
627 inset->Edit(bv, x - inset_x, y - inset_y, button);
629 UpdateLocal(bv, CURSOR_PAR, false);
632 no_selection = false;
636 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
638 if (the_locking_inset) {
639 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
645 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
646 y+TEXT(bv)->first+insetAscent);
647 TEXT(bv)->SetSelection();
648 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
649 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
650 UpdateLocal(bv, SELECTION, false);
653 no_selection = false;
657 void InsetText::InsetKeyPress(XKeyEvent * xke)
659 if (the_locking_inset) {
660 the_locking_inset->InsetKeyPress(xke);
666 UpdatableInset::RESULT
667 InsetText::LocalDispatch(BufferView * bv,
668 int action, string const & arg)
670 no_selection = false;
671 UpdatableInset::RESULT
672 result= UpdatableInset::LocalDispatch(bv, action, arg);
673 if (result != UNDISPATCHED) {
678 if ((action < 0) && arg.empty())
681 if (the_locking_inset) {
682 result = the_locking_inset->LocalDispatch(bv, action, arg);
683 if (result == DISPATCHED_NOUPDATE)
685 else if (result == DISPATCHED) {
686 UpdateLocal(bv, CURSOR_PAR, false);
688 } else if (result == FINISHED) {
692 moveRight(bv, false);
698 the_locking_inset = 0;
706 if (bv->buffer()->isReadonly()) {
708 // setErrorMessage(N_("Document is read only"));
712 /* Automatically delete the currently selected
713 * text and replace it with what is being
714 * typed in now. Depends on lyxrc settings
715 * "auto_region_delete", which defaults to
718 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
720 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
721 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
723 bv->text->cursor.par()->previous,
724 bv->text->cursor.par()->next
727 if (lyxrc.auto_region_delete) {
728 if (TEXT(bv)->selection){
729 TEXT(bv)->CutSelection(bv, false);
732 TEXT(bv)->ClearSelection();
733 for (string::size_type i = 0; i < arg.length(); ++i) {
734 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
737 UpdateLocal(bv, CURSOR_PAR, true);
739 // --- Cursor Movements ---------------------------------------------
741 bv->text->FinishUndo();
742 moveRight(bv, false);
743 TEXT(bv)->SetSelection();
744 UpdateLocal(bv, SELECTION, false);
747 result = moveRight(bv);
748 bv->text->FinishUndo();
749 UpdateLocal(bv, CURSOR, false);
752 bv->text->FinishUndo();
754 TEXT(bv)->SetSelection();
755 UpdateLocal(bv, SELECTION, false);
758 bv->text->FinishUndo();
759 result= moveLeft(bv);
760 UpdateLocal(bv, CURSOR, false);
763 bv->text->FinishUndo();
765 TEXT(bv)->SetSelection();
766 UpdateLocal(bv, SELECTION, false);
769 bv->text->FinishUndo();
770 result = moveDown(bv);
771 UpdateLocal(bv, CURSOR, false);
774 bv->text->FinishUndo();
776 TEXT(bv)->SetSelection();
777 UpdateLocal(bv, SELECTION, false);
780 bv->text->FinishUndo();
782 UpdateLocal(bv, CURSOR, false);
785 bv->text->FinishUndo();
786 TEXT(bv)->CursorHome(bv);
787 UpdateLocal(bv, CURSOR, false);
790 TEXT(bv)->CursorEnd(bv);
791 UpdateLocal(bv, CURSOR, false);
794 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
796 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
797 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
799 bv->text->cursor.par()->previous,
800 bv->text->cursor.par()->next
803 TEXT(bv)->Backspace(bv);
804 UpdateLocal(bv, CURSOR_PAR, true);
807 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
809 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
810 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
812 bv->text->cursor.par()->previous,
813 bv->text->cursor.par()->next
816 TEXT(bv)->Delete(bv);
817 UpdateLocal(bv, CURSOR_PAR, true);
820 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
822 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
823 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
825 bv->text->cursor.par()->previous,
826 bv->text->cursor.par()->next
829 TEXT(bv)->CutSelection(bv);
830 UpdateLocal(bv, CURSOR_PAR, true);
833 bv->text->FinishUndo();
834 TEXT(bv)->CopySelection(bv);
835 UpdateLocal(bv, CURSOR_PAR, false);
838 if (!autoBreakRows) {
841 if (cap.nrOfParagraphs() > 1) {
842 WriteAlert(_("Impossible operation"),
843 _("Cannot include more than one paragraph!"),
848 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
850 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
851 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
853 bv->text->cursor.par()->previous,
854 bv->text->cursor.par()->next
857 TEXT(bv)->PasteSelection(bv);
858 UpdateLocal(bv, CURSOR_PAR, true);
860 case LFUN_BREAKPARAGRAPH:
863 TEXT(bv)->BreakParagraph(bv, 0);
864 UpdateLocal(bv, FULL, true);
869 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
871 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
872 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
874 bv->text->cursor.par()->previous,
875 bv->text->cursor.par()->next
878 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
879 UpdateLocal(bv, CURSOR_PAR, true);
883 static LyXTextClass::size_type cur_layout = cpar(bv)->layout;
885 // Derive layout number from given argument (string)
886 // and current buffer's textclass (number). */
887 LyXTextClassList::ClassList::size_type tclass =
888 bv->buffer()->params.textclass;
889 std::pair <bool, LyXTextClass::size_type> layout =
890 textclasslist.NumberOfLayout(tclass, arg);
892 // If the entry is obsolete, use the new one instead.
894 string obs = textclasslist.Style(tclass,layout.second).
897 layout = textclasslist.NumberOfLayout(tclass, obs);
900 // see if we found the layout number:
902 string msg = string(N_("Layout ")) + arg + N_(" not known");
904 bv->owner()->getMiniBuffer()->Set(msg);
908 if (cur_layout != layout.second) {
909 cur_layout = layout.second;
910 TEXT(bv)->SetLayout(bv, layout.second);
911 bv->owner()->setLayout(cpar(bv)->GetLayout());
912 UpdateLocal(bv, CURSOR_PAR, true);
916 case LFUN_PARAGRAPH_SPACING:
917 // This one is absolutely not working. When fiddling with this
918 // it also seems to me that the paragraphs inside the insettext
919 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
921 LyXParagraph * par = TEXT(bv)->cursor.par();
922 Spacing::Space cur_spacing = par->spacing.getSpace();
923 float cur_value = 1.0;
924 if (cur_spacing == Spacing::Other) {
925 cur_value = par->spacing.getValue();
928 std::istringstream istr(arg.c_str());
931 Spacing::Space new_spacing = cur_spacing;
932 float new_value = cur_value;
934 lyxerr << "Missing argument to `paragraph-spacing'"
936 } else if (tmp == "single") {
937 new_spacing = Spacing::Single;
938 } else if (tmp == "onehalf") {
939 new_spacing = Spacing::Onehalf;
940 } else if (tmp == "double") {
941 new_spacing = Spacing::Double;
942 } else if (tmp == "other") {
943 new_spacing = Spacing::Other;
946 lyxerr << "new_value = " << tmpval << endl;
949 } else if (tmp == "default") {
950 new_spacing = Spacing::Default;
952 lyxerr << _("Unknown spacing argument: ")
955 if (cur_spacing != new_spacing || cur_value != new_value) {
956 par->spacing.set(new_spacing, new_value);
957 //TEXT(bv)->RedoParagraph(owner->view());
958 UpdateLocal(bv, CURSOR_PAR, true);
959 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
965 result = UNDISPATCHED;
968 if (result != FINISHED) {
971 bv->unlockInset(this);
976 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
979 buf->latexParagraphs(os, par, 0, texrow);
980 return texrow.rows();
984 void InsetText::Validate(LaTeXFeatures & features) const
986 LyXParagraph * p = par;
988 p->validate(features);
994 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
996 if (textclasslist.Style(buf->params.textclass,
997 p->GetLayout()).labeltype != LABEL_MANUAL)
1000 return p->BeginningOfMainBody();
1004 void InsetText::GetCursorPos(BufferView * bv, int & x, int & y) const
1011 int InsetText::InsetInInsetY()
1013 if (!the_locking_inset)
1016 return (inset_y + the_locking_inset->InsetInInsetY());
1020 void InsetText::ToggleInsetCursor(BufferView * bv)
1022 if (the_locking_inset) {
1023 the_locking_inset->ToggleInsetCursor(bv);
1027 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1029 int asc = lyxfont::maxAscent(font);
1030 int desc = lyxfont::maxDescent(font);
1033 bv->hideLockedInsetCursor();
1035 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1036 cursor_visible = !cursor_visible;
1040 void InsetText::ShowInsetCursor(BufferView * bv)
1042 if (the_locking_inset) {
1043 the_locking_inset->ShowInsetCursor(bv);
1046 if (!cursor_visible) {
1047 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1049 int asc = lyxfont::maxAscent(font);
1050 int desc = lyxfont::maxDescent(font);
1052 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1053 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1054 cursor_visible = true;
1059 void InsetText::HideInsetCursor(BufferView * bv)
1061 if (cursor_visible) {
1062 bv->hideLockedInsetCursor();
1063 cursor_visible = false;
1065 if (the_locking_inset)
1066 the_locking_inset->HideInsetCursor(bv);
1070 UpdatableInset::RESULT
1071 InsetText::moveRight(BufferView * bv, bool activate_inset)
1073 if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1075 if (activate_inset && checkAndActivateInset(bv, false))
1077 TEXT(bv)->CursorRight(bv);
1078 return DISPATCHED_NOUPDATE;
1082 UpdatableInset::RESULT
1083 InsetText::moveLeft(BufferView * bv, bool activate_inset)
1085 if (!cpar(bv)->previous && (cpos(bv) <= 0))
1087 TEXT(bv)->CursorLeft(bv);
1088 if (activate_inset && checkAndActivateInset(bv, true))
1090 return DISPATCHED_NOUPDATE;
1094 UpdatableInset::RESULT
1095 InsetText::moveUp(BufferView * bv)
1097 if (!crow(bv)->previous())
1099 TEXT(bv)->CursorUp(bv);
1100 return DISPATCHED_NOUPDATE;
1104 UpdatableInset::RESULT
1105 InsetText::moveDown(BufferView * bv)
1107 if (!crow(bv)->next())
1109 TEXT(bv)->CursorDown(bv);
1110 return DISPATCHED_NOUPDATE;
1114 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1116 if (the_locking_inset) {
1117 if (the_locking_inset->InsertInsetAllowed(inset))
1118 return the_locking_inset->InsertInset(bv, inset);
1121 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1123 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1124 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1126 bv->text->cursor.par()->previous,
1127 bv->text->cursor.par()->next
1130 if (inset->Editable() == Inset::IS_EDITABLE) {
1131 UpdatableInset * i = static_cast<UpdatableInset *>(inset);
1132 i->setOwner(static_cast<UpdatableInset *>(this));
1134 TEXT(bv)->InsertInset(bv, inset);
1135 TEXT(bv)->selection = 0;
1136 UpdateLocal(bv, CURSOR_PAR, true);
1137 static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1142 UpdatableInset * InsetText::GetLockingInset()
1144 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1148 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1152 if (the_locking_inset)
1153 return the_locking_inset->GetFirstLockingInsetOfType(c);
1158 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1160 TEXT(bv)->SetFont(bv, font, toggleall);
1164 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1166 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1169 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1170 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1172 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1174 x = inset->width(bv, font);
1175 y = inset->descent(bv, font);
1179 inset_x = cx(bv) - top_x + drawTextXOffset;
1180 inset_y = cy(bv) + drawTextYOffset;
1181 inset->Edit(bv, x-inset_x, y-inset_y, 0);
1182 if (!the_locking_inset)
1184 UpdateLocal(bv, CURSOR_PAR, false);
1191 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1194 int tmp_x = x - drawTextXOffset;
1195 int tmp_y = y + insetAscent;
1196 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
1203 inset_x = cx(bv) - top_x + drawTextXOffset;
1204 inset_y = cy(bv) + drawTextYOffset;
1205 inset->Edit(bv, x - inset_x, y - inset_y, button);
1206 if (!the_locking_inset)
1208 UpdateLocal(bv, CURSOR_PAR, false);
1215 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1217 int w = UpdatableInset::getMaxWidth(pain, inset);
1222 w = w - top_x + owner()->x();
1225 w -= (2 * TEXT_TO_INSET_OFFSET);
1227 // return w - (2*TEXT_TO_INSET_OFFSET);
1231 void InsetText::SetParagraphData(LyXParagraph *p)
1245 par->SetInsetOwner(this);
1249 np->next = p->Clone();
1250 np->next->previous = np;
1252 np->SetInsetOwner(this);
1258 void InsetText::SetAutoBreakRows(bool flag)
1260 if (flag != autoBreakRows) {
1261 autoBreakRows = flag;
1269 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1271 if (how != drawFrame) {
1274 UpdateLocal(bv, DRAW_FRAME, false);
1279 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1281 if (frame_color != col) {
1284 UpdateLocal(bv, DRAW_FRAME, false);
1289 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1291 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1295 int InsetText::cx(BufferView * bv) const
1297 return TEXT(bv)->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1301 int InsetText::cy(BufferView * bv) const
1304 return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1308 int InsetText::cpos(BufferView * bv) const
1310 return TEXT(bv)->cursor.pos();
1314 LyXParagraph * InsetText::cpar(BufferView * bv) const
1316 return TEXT(bv)->cursor.par();
1320 Row * InsetText::crow(BufferView * bv) const
1322 return TEXT(bv)->cursor.row();
1326 LyXText * InsetText::getLyXText(BufferView * bv) const
1328 if (cache.find(bv) != cache.end())
1330 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1333 if (the_locking_inset) {
1334 lt->SetCursor(bv, inset_par, inset_pos);
1340 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1344 /// then remove all LyXText in text-insets
1345 LyXParagraph * p = par;
1346 for(;p;p = p->next) {
1347 p->deleteInsetsLyXText(bv);
1353 void InsetText::resizeLyXText(BufferView * bv) const
1355 if (!par->next && !par->size()) // resize not neccessary!
1357 if (cache.find(bv) == cache.end())
1360 LyXParagraph * lpar = 0;
1361 LyXParagraph * selstartpar = 0;
1362 LyXParagraph * selendpar = 0;
1364 int selstartpos = 0;
1369 // ProhibitInput(bv);
1371 lpar = TEXT(bv)->cursor.par();
1372 pos = TEXT(bv)->cursor.pos();
1373 selstartpar = TEXT(bv)->sel_start_cursor.par();
1374 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1375 selendpar = TEXT(bv)->sel_end_cursor.par();
1376 selendpos = TEXT(bv)->sel_end_cursor.pos();
1377 selection = TEXT(bv)->selection;
1378 mark_set = TEXT(bv)->mark_set;
1379 deleteLyXText(bv, (the_locking_inset == 0));
1382 TEXT(bv)->selection = true;
1383 /* at this point just to avoid the Delete-Empty-Paragraph
1384 * Mechanism when setting the cursor */
1385 TEXT(bv)->mark_set = mark_set;
1387 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos);
1388 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1389 TEXT(bv)->SetCursor(bv, selendpar, selendpos);
1390 TEXT(bv)->SetSelection();
1391 TEXT(bv)->SetCursor(bv, lpar, pos);
1393 TEXT(bv)->SetCursor(bv, lpar, pos);
1394 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1395 TEXT(bv)->selection = false;
1399 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1400 // this will scroll the screen such that the cursor becomes visible
1401 bv->updateScrollbar();
1403 if (the_locking_inset) {
1404 /// then resize all LyXText in text-insets
1405 inset_x = cx(bv) - top_x + drawTextXOffset;
1406 inset_y = cy(bv) + drawTextYOffset;
1407 for(LyXParagraph * p = par; p; p = p->next) {
1408 p->resizeInsetsLyXText(bv);
1415 void InsetText::removeNewlines()
1417 for(LyXParagraph * p = par; p; p = p->next) {
1418 for(int i = 0; i < p->Last(); ++i) {
1419 if (p->GetChar(i) == LyXParagraph::META_NEWLINE)