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"
43 #include "support/textutils.h"
44 #include "support/LAssert.h"
48 #include "trans_mgr.h"
49 #include "lyxscreen.h"
57 extern unsigned char getCurrentTextClass(Buffer *);
59 InsetText::InsetText()
61 par = new LyXParagraph();
66 InsetText::InsetText(InsetText const & ins)
71 autoBreakRows = ins.autoBreakRows;
75 InsetText & InsetText::operator=(InsetText const & it)
78 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?
158 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
159 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
160 LyXFont font(LyXFont::ALL_INHERIT);
162 LyXParagraph * p = par->next;
169 par = new LyXParagraph;
172 token = lex.GetString();
175 if (token == "\\end_inset")
177 if (const_cast<Buffer*>(buf)->parseSingleLyXformat2Token(lex, par, return_par,
181 // the_end read this should NEVER happen
182 lex.printError("\\the_end read in inset! Error in document!");
190 return_par->SetInsetOwner(this);
191 return_par = return_par->next;
194 if (token != "\\end_inset") {
195 lex.printError("Missing \\end_inset at this point. "
202 int InsetText::ascent(BufferView * bv, LyXFont const &) const
205 Row * row = TEXT(bv)->GetRowNearY(y_temp);
206 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
211 int InsetText::descent(BufferView * bv, LyXFont const &) const
214 Row * row = TEXT(bv)->GetRowNearY(y_temp);
215 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
216 TEXT_TO_INSET_OFFSET;
221 int InsetText::width(BufferView * bv, LyXFont const &) const
223 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
228 int InsetText::textWidth(Painter & pain) const
230 int w = getMaxWidth(pain, this);
232 // printf("WW1: %d\n",w);
237 w = w - top_x + owner()->x();
238 // printf("WW2: %d\n",w);
239 return w; // - top_x + owner()->x();
242 w -= (2 * TEXT_TO_INSET_OFFSET);
243 // printf("WW2: %d\n",w);
244 return w - top_x; // - top_x - (2 * TEXT_TO_INSET_OFFSET);
248 void InsetText::draw(BufferView * bv, LyXFont const & f,
249 int baseline, float & x, bool cleared) const
251 Painter & pain = bv->painter();
254 UpdatableInset::draw(bv, f, baseline, x, cleared);
256 if (!cleared && ((need_update==FULL) || (top_x!=int(x)) ||
257 (top_baseline!=baseline))) {
259 int h = insetAscent + insetDescent;
260 int ty = baseline - insetAscent;
264 if ((ty + h) > pain.paperHeight())
265 h = pain.paperHeight();
266 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
267 w = pain.paperWidth();
268 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
272 if (!cleared && (need_update == NONE))
275 if (top_x != int(x)) {
278 owner()->update(bv, f, true);
279 bv->text->status = LyXText::CHANGED_IN_DRAW;
283 top_baseline = baseline;
284 top_y = baseline - ascent(bv, f);
285 last_width = width(bv, f);
286 last_height = ascent(bv, f) + descent(bv, f);
288 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
289 inset_x = cx(bv) - top_x + drawTextXOffset;
290 inset_y = cy(bv) + drawTextYOffset;
292 if (!cleared && (need_update == CURSOR) && !TEXT(bv)->selection) {
297 x += 1; // place for border
299 Row * row = TEXT(bv)->GetRowNearY(y);
300 y += baseline - row->ascent_of_text() + 1;
301 if (cleared || !locked || (need_update == FULL)) {
303 TEXT(bv)->GetVisibleRow(bv, y, x, row, y, cleared);
307 } else if (need_update == SELECTION) {
308 bv->screen()->ToggleToggle(TEXT(bv), y, x);
311 if (need_update == CURSOR) {
312 bv->screen()->ToggleSelection(TEXT(bv), true, y, x);
313 TEXT(bv)->ClearSelection();
314 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
316 bv->screen()->Update(TEXT(bv), y, x);
319 TEXT(bv)->refresh_y = 0;
320 TEXT(bv)->status = LyXText::UNCHANGED;
321 if ((drawFrame == ALWAYS) || ((drawFrame == LOCKED) && locked)) {
322 pain.rectangle(top_x, baseline - insetAscent, insetWidth,
323 insetAscent + insetDescent, frame_color);
324 } else if (need_update == CLEAR_FRAME) {
325 pain.rectangle(top_x, baseline - insetAscent, insetWidth,
326 insetAscent + insetDescent,
329 x += insetWidth - TEXT_TO_INSET_OFFSET;
330 if (need_update != INIT)
335 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
337 if (reinit) { // && (need_update != CURSOR)) {
341 owner()->update(bv, font, true);
344 if (the_locking_inset) {
345 inset_x = cx(bv) - top_x + drawTextXOffset;
346 inset_y = cy(bv) + drawTextYOffset;
347 the_locking_inset->update(bv, font, reinit);
349 if (need_update == INIT) {
352 // if (!owner() && bv->text)
353 // bv->text->UpdateInset(bv, this);
355 int oldw = insetWidth;
357 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
358 // max(textWidth(bv->painter()),
359 // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
360 // (2 * TEXT_TO_INSET_OFFSET);
362 insetWidth = textWidth(bv->painter());
364 insetWidth = static_cast<int>(TEXT(bv)->width);
366 if (oldw != insetWidth) {
367 // printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
368 // textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
373 owner()->update(bv, font, reinit);
376 update(bv, font, reinit);
380 update(bv, font, reinit);
382 UpdateLocal(bv, INIT, false);
387 if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
390 TEXT(bv)->UpdateInset(bv, the_locking_inset);
393 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
397 Row * row = TEXT(bv)->GetRowNearY(y_temp);
398 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
399 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
400 TEXT_TO_INSET_OFFSET;
404 void InsetText::UpdateLocal(BufferView * bv, UpdateCodes what, bool mark_dirty)
406 TEXT(bv)->FullRebreak(bv);
407 if (need_update != INIT) {
408 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
410 else if (!the_locking_inset || (what != CURSOR))
413 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
415 bv->updateInset(this, mark_dirty);
416 if (old_par != cpar(bv)) {
417 bv->owner()->getToolbar()->combox->select(cpar(bv)->GetLayout()+1);
423 char const * InsetText::EditMessage() const
425 return _("Opened Text Inset");
429 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
431 // par->SetInsetOwner(this);
432 UpdatableInset::Edit(bv, x, y, button);
434 if (!bv->lockInset(this)) {
435 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
439 the_locking_inset = 0;
440 inset_pos = inset_x = inset_y = 0;
443 if (!checkAndActivateInset(bv, x, y, button))
444 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
445 y+TEXT(bv)->first+insetAscent);
446 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
447 bv->text->FinishUndo();
448 UpdateLocal(bv, FULL, false);
452 void InsetText::InsetUnlock(BufferView * bv)
454 if (the_locking_inset) {
455 the_locking_inset->InsetUnlock(bv);
456 the_locking_inset = 0;
459 no_selection = false;
461 TEXT(bv)->selection = 0;
462 UpdateLocal(bv, CLEAR_FRAME, false);
464 bv->owner()->getToolbar()->combox->
465 select(owner()->getLyXText(bv)->cursor.par()->GetLayout()+1);
467 bv->owner()->getToolbar()->combox->select(bv->text->cursor.par()->
472 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
474 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
477 if (inset == cpar(bv)->GetInset(cpos(bv))) {
478 lyxerr[Debug::INSETS] << "OK" << endl;
479 the_locking_inset = inset;
480 inset_x = cx(bv) - top_x + drawTextXOffset;
481 inset_y = cy(bv) + drawTextYOffset;
482 inset_pos = cpos(bv);
483 inset_par = cpar(bv);
484 TEXT(bv)->UpdateInset(bv, the_locking_inset);
486 } else if (the_locking_inset && (the_locking_inset == inset)) {
487 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
488 lyxerr[Debug::INSETS] << "OK" << endl;
489 inset_x = cx(bv) - top_x + drawTextXOffset;
490 inset_y = cy(bv) + drawTextYOffset;
492 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
494 } else if (the_locking_inset) {
495 lyxerr[Debug::INSETS] << "MAYBE" << endl;
496 return the_locking_inset->LockInsetInInset(bv, inset);
498 lyxerr[Debug::INSETS] << "NOT OK" << endl;
503 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
506 if (!the_locking_inset)
508 if (the_locking_inset == inset) {
509 the_locking_inset->InsetUnlock(bv);
510 TEXT(bv)->UpdateInset(bv, inset);
511 the_locking_inset = 0;
513 moveRight(bv, false);
514 old_par = 0; // force layout setting
515 UpdateLocal(bv, CURSOR_PAR, false);
518 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
522 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
524 if (!the_locking_inset)
526 if (the_locking_inset != inset) {
527 TEXT(bv)->UpdateInset(bv, the_locking_inset);
528 need_update = CURSOR_PAR;
529 return the_locking_inset->UpdateInsetInInset(bv, inset);
531 // UpdateLocal(bv, FULL, false);
532 if (TEXT(bv)->UpdateInset(bv, inset))
533 UpdateLocal(bv, CURSOR_PAR, false);
534 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
535 inset_x = cx(bv) - top_x + drawTextXOffset;
536 inset_y = cy(bv) + drawTextYOffset;
542 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
544 no_selection = false;
546 int tmp_x = x - drawTextXOffset;
547 // int tmp_y = y + TEXT(bv)->first + insetAscent;
548 int tmp_y = y + insetAscent;
549 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
551 if (the_locking_inset) {
552 if (the_locking_inset == inset) {
553 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
556 // otherwise unlock the_locking_inset and lock the new inset
557 the_locking_inset->InsetUnlock(bv);
558 inset_x = cx(bv) - top_x + drawTextXOffset;
559 inset_y = cy(bv) + drawTextYOffset;
560 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
561 inset->Edit(bv, x - inset_x, y - inset_y, button);
562 if (the_locking_inset) {
563 UpdateLocal(bv, CURSOR_PAR, false);
567 // otherwise only unlock the_locking_inset
568 the_locking_inset->InsetUnlock(bv);
569 the_locking_inset = 0;
571 if (bv->the_locking_inset) {
572 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
573 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
574 inset_x = cx(bv) - top_x + drawTextXOffset;
575 inset_y = cy(bv) + drawTextYOffset;
576 inset_pos = cpos(bv);
577 inset_par = cpar(bv);
578 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
579 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
580 if (the_locking_inset) {
581 UpdateLocal(bv, CURSOR_PAR, false);
587 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
588 y+TEXT(bv)->first+insetAscent);
589 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
590 UpdateLocal(bv, CURSOR, false);
591 bv->owner()->getToolbar()->combox->select(cpar(bv)->GetLayout()+1);
597 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
599 UpdatableInset * inset = 0;
601 if (the_locking_inset) {
602 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
604 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
605 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
606 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
607 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
609 inset_x = cx(bv) - top_x + drawTextXOffset;
610 inset_y = cy(bv) + drawTextYOffset;
611 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
612 inset->Edit(bv, x - inset_x, y - inset_y, button);
614 UpdateLocal(bv, CURSOR_PAR, false);
617 no_selection = false;
621 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
623 if (the_locking_inset) {
624 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
630 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
631 y+TEXT(bv)->first+insetAscent);
632 TEXT(bv)->SetSelection();
633 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
634 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
635 UpdateLocal(bv, SELECTION, false);
638 no_selection = false;
642 void InsetText::InsetKeyPress(XKeyEvent * xke)
644 if (the_locking_inset) {
645 the_locking_inset->InsetKeyPress(xke);
651 UpdatableInset::RESULT
652 InsetText::LocalDispatch(BufferView * bv,
653 int action, string const & arg)
655 no_selection = false;
656 UpdatableInset::RESULT
657 result= UpdatableInset::LocalDispatch(bv, action, arg);
658 if (result != UNDISPATCHED) {
659 // resetPos(bv->painter());
664 if ((action < 0) && arg.empty())
667 if (the_locking_inset) {
668 result = the_locking_inset->LocalDispatch(bv, action, arg);
669 if (result == DISPATCHED_NOUPDATE)
671 else if (result == DISPATCHED) {
672 UpdateLocal(bv, CURSOR_PAR, false);
674 } else if (result == FINISHED) {
678 moveRight(bv, false);
684 the_locking_inset = 0;
692 if (bv->buffer()->isReadonly()) {
694 // setErrorMessage(N_("Document is read only"));
698 /* Automatically delete the currently selected
699 * text and replace it with what is being
700 * typed in now. Depends on lyxrc settings
701 * "auto_region_delete", which defaults to
704 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
705 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
706 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
707 if (lyxrc.auto_region_delete) {
708 if (TEXT(bv)->selection){
709 TEXT(bv)->CutSelection(bv, false);
712 TEXT(bv)->ClearSelection();
713 for (string::size_type i = 0; i < arg.length(); ++i) {
714 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
717 UpdateLocal(bv, CURSOR_PAR, true);
719 // --- Cursor Movements ---------------------------------------------
721 bv->text->FinishUndo();
722 moveRight(bv, false);
723 TEXT(bv)->SetSelection();
724 UpdateLocal(bv, SELECTION, false);
727 result = moveRight(bv);
728 bv->text->FinishUndo();
729 UpdateLocal(bv, CURSOR, false);
732 bv->text->FinishUndo();
734 TEXT(bv)->SetSelection();
735 UpdateLocal(bv, SELECTION, false);
738 bv->text->FinishUndo();
739 result= moveLeft(bv);
740 UpdateLocal(bv, CURSOR, false);
743 bv->text->FinishUndo();
745 TEXT(bv)->SetSelection();
746 UpdateLocal(bv, SELECTION, false);
749 bv->text->FinishUndo();
750 result = moveDown(bv);
751 UpdateLocal(bv, CURSOR, false);
754 bv->text->FinishUndo();
756 TEXT(bv)->SetSelection();
757 UpdateLocal(bv, SELECTION, false);
760 bv->text->FinishUndo();
762 UpdateLocal(bv, CURSOR, false);
765 bv->text->FinishUndo();
766 TEXT(bv)->CursorHome(bv);
767 UpdateLocal(bv, CURSOR, false);
770 TEXT(bv)->CursorEnd(bv);
771 UpdateLocal(bv, CURSOR, false);
774 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
775 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
776 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
777 TEXT(bv)->Backspace(bv);
778 UpdateLocal(bv, CURSOR_PAR, true);
781 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
782 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
783 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
784 TEXT(bv)->Delete(bv);
785 UpdateLocal(bv, CURSOR_PAR, true);
788 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
789 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
790 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
791 TEXT(bv)->CutSelection(bv);
792 UpdateLocal(bv, CURSOR_PAR, true);
795 bv->text->FinishUndo();
796 TEXT(bv)->CopySelection(bv);
797 UpdateLocal(bv, CURSOR_PAR, false);
800 if (!autoBreakRows) {
803 if (cap.nrOfParagraphs() > 1) {
804 WriteAlert(_("Impossible operation"),
805 _("Cannot include more than one paragraph!"),
810 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
811 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
812 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
813 TEXT(bv)->PasteSelection(bv);
814 UpdateLocal(bv, CURSOR_PAR, true);
816 case LFUN_BREAKPARAGRAPH:
819 TEXT(bv)->BreakParagraph(bv, 0);
820 UpdateLocal(bv, FULL, true);
825 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
826 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
827 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
828 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
829 UpdateLocal(bv, CURSOR_PAR, true);
833 static LyXTextClass::size_type cur_layout = cpar(bv)->layout;
835 // Derive layout number from given argument (string)
836 // and current buffer's textclass (number). */
837 LyXTextClassList::ClassList::size_type tclass =
838 bv->buffer()->params.textclass;
839 std::pair <bool, LyXTextClass::size_type> layout =
840 textclasslist.NumberOfLayout(tclass, arg);
842 // If the entry is obsolete, use the new one instead.
844 string obs = textclasslist.Style(tclass,layout.second).
847 layout = textclasslist.NumberOfLayout(tclass, obs);
850 // see if we found the layout number:
852 string msg = string(N_("Layout ")) + arg + N_(" not known");
854 bv->owner()->getMiniBuffer()->Set(msg);
858 if (cur_layout != layout.second) {
859 cur_layout = layout.second;
860 TEXT(bv)->SetLayout(bv, layout.second);
861 bv->owner()->getToolbar()->combox->select(cpar(bv)->GetLayout()+1);
862 UpdateLocal(bv, CURSOR_PAR, true);
866 case LFUN_PARAGRAPH_SPACING:
867 // This one is absolutely not working. When fiddling with this
868 // it also seems to me that the paragraphs inside the insettext
869 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
871 LyXParagraph * par = TEXT(bv)->cursor.par();
872 Spacing::Space cur_spacing = par->spacing.getSpace();
873 float cur_value = 1.0;
874 if (cur_spacing == Spacing::Other) {
875 cur_value = par->spacing.getValue();
879 std::istringstream istr(arg);
881 istrstream istr(arg.c_str());
885 Spacing::Space new_spacing = cur_spacing;
886 float new_value = cur_value;
888 lyxerr << "Missing argument to `paragraph-spacing'"
890 } else if (tmp == "single") {
891 new_spacing = Spacing::Single;
892 } else if (tmp == "onehalf") {
893 new_spacing = Spacing::Onehalf;
894 } else if (tmp == "double") {
895 new_spacing = Spacing::Double;
896 } else if (tmp == "other") {
897 new_spacing = Spacing::Other;
900 lyxerr << "new_value = " << tmpval << endl;
903 } else if (tmp == "default") {
904 new_spacing = Spacing::Default;
906 lyxerr << _("Unknown spacing argument: ")
909 if (cur_spacing != new_spacing || cur_value != new_value) {
910 par->spacing.set(new_spacing, new_value);
911 //TEXT(bv)->RedoParagraph(owner->view());
912 UpdateLocal(bv, CURSOR_PAR, true);
913 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
919 result = UNDISPATCHED;
922 if (result != FINISHED) {
925 bv->unlockInset(this);
930 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
933 buf->latexParagraphs(os, par, 0, texrow);
934 return texrow.rows();
938 void InsetText::Validate(LaTeXFeatures & features) const
940 LyXParagraph * p = par;
942 p->validate(features);
948 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
950 if (textclasslist.Style(buf->params.textclass,
951 p->GetLayout()).labeltype != LABEL_MANUAL)
954 return p->BeginningOfMainBody();
958 void InsetText::GetCursorPos(BufferView * bv, int & x, int & y) const
965 int InsetText::InsetInInsetY()
967 if (!the_locking_inset)
970 return (inset_y + the_locking_inset->InsetInInsetY());
974 void InsetText::ToggleInsetCursor(BufferView * bv)
976 if (the_locking_inset) {
977 the_locking_inset->ToggleInsetCursor(bv);
981 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
983 int asc = lyxfont::maxAscent(font);
984 int desc = lyxfont::maxDescent(font);
987 bv->hideLockedInsetCursor();
989 bv->showLockedInsetCursor(cx(bv)-1, cy(bv),
991 cursor_visible = !cursor_visible;
995 void InsetText::ShowInsetCursor(BufferView * bv)
997 if (the_locking_inset) {
998 the_locking_inset->ShowInsetCursor(bv);
1001 if (!cursor_visible) {
1002 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1004 int asc = lyxfont::maxAscent(font);
1005 int desc = lyxfont::maxDescent(font);
1007 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1008 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1009 cursor_visible = true;
1014 void InsetText::HideInsetCursor(BufferView * bv)
1016 if (cursor_visible) {
1017 bv->hideLockedInsetCursor();
1018 cursor_visible = false;
1020 if (the_locking_inset)
1021 the_locking_inset->HideInsetCursor(bv);
1025 UpdatableInset::RESULT
1026 InsetText::moveRight(BufferView * bv, bool activate_inset)
1028 if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1030 if (activate_inset && checkAndActivateInset(bv, false))
1032 TEXT(bv)->CursorRight(bv);
1033 return DISPATCHED_NOUPDATE;
1037 UpdatableInset::RESULT
1038 InsetText::moveLeft(BufferView * bv, bool activate_inset)
1040 if (!cpar(bv)->previous && (cpos(bv) <= 0))
1042 TEXT(bv)->CursorLeft(bv);
1043 if (activate_inset && checkAndActivateInset(bv, true))
1045 return DISPATCHED_NOUPDATE;
1049 UpdatableInset::RESULT
1050 InsetText::moveUp(BufferView * bv)
1052 if (!crow(bv)->previous())
1054 TEXT(bv)->CursorUp(bv);
1055 return DISPATCHED_NOUPDATE;
1059 UpdatableInset::RESULT
1060 InsetText::moveDown(BufferView * bv)
1062 if (!crow(bv)->next())
1064 TEXT(bv)->CursorDown(bv);
1065 return DISPATCHED_NOUPDATE;
1069 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1071 if (the_locking_inset) {
1072 if (the_locking_inset->InsertInsetAllowed(inset))
1073 return the_locking_inset->InsertInset(bv, inset);
1076 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1077 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1078 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
1079 if (inset->Editable() == Inset::IS_EDITABLE) {
1080 UpdatableInset * i = static_cast<UpdatableInset *>(inset);
1081 i->setOwner(static_cast<UpdatableInset *>(this));
1083 TEXT(bv)->InsertInset(bv, inset);
1084 TEXT(bv)->selection = 0;
1085 UpdateLocal(bv, CURSOR_PAR, true);
1086 static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1091 UpdatableInset * InsetText::GetLockingInset()
1093 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1097 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1101 if (the_locking_inset)
1102 return the_locking_inset->GetFirstLockingInsetOfType(c);
1107 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1109 TEXT(bv)->SetFont(bv, font, toggleall);
1113 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1115 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1118 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1119 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1121 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1123 x = inset->width(bv, font);
1124 y = inset->descent(bv, font);
1128 inset_x = cx(bv) - top_x + drawTextXOffset;
1129 inset_y = cy(bv) + drawTextYOffset;
1130 inset->Edit(bv, x-inset_x, y-inset_y, 0);
1131 if (!the_locking_inset)
1133 UpdateLocal(bv, CURSOR_PAR, false);
1140 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1143 int tmp_x = x - drawTextXOffset;
1144 int tmp_y = y + insetAscent;
1145 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
1152 inset_x = cx(bv) - top_x + drawTextXOffset;
1153 inset_y = cy(bv) + drawTextYOffset;
1154 inset->Edit(bv, x - inset_x, y - inset_y, button);
1155 if (!the_locking_inset)
1157 UpdateLocal(bv, CURSOR_PAR, false);
1164 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1166 int w = UpdatableInset::getMaxWidth(pain, inset);
1169 return w - (2*TEXT_TO_INSET_OFFSET);
1173 void InsetText::SetParagraphData(LyXParagraph *p)
1187 par->SetInsetOwner(this);
1191 np->next = p->Clone();
1192 np->next->previous = np;
1194 np->SetInsetOwner(this);
1200 void InsetText::SetAutoBreakRows(bool flag)
1202 if (flag != autoBreakRows) {
1203 autoBreakRows = flag;
1209 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1211 if (how != drawFrame) {
1214 UpdateLocal(bv, DRAW_FRAME, false);
1219 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1221 if (frame_color != col) {
1224 UpdateLocal(bv, DRAW_FRAME, false);
1229 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1231 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1235 int InsetText::cx(BufferView * bv) const
1237 return TEXT(bv)->cursor.x() + top_x + 1;
1241 int InsetText::cy(BufferView * bv) const
1243 long int y_dummy = 0;
1244 Row * tmprow = TEXT(bv)->GetRowNearY(y_dummy);
1245 return TEXT(bv)->cursor.y() - tmprow->baseline();
1249 int InsetText::cpos(BufferView * bv) const
1251 return TEXT(bv)->cursor.pos();
1255 LyXParagraph * InsetText::cpar(BufferView * bv) const
1257 return TEXT(bv)->cursor.par();
1261 Row * InsetText::crow(BufferView * bv) const
1263 return TEXT(bv)->cursor.row();
1267 LyXText * InsetText::getLyXText(BufferView * bv) const
1269 if (cache.find(bv) != cache.end())
1271 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1274 if (the_locking_inset) {
1275 lt->SetCursor(bv, inset_par, inset_pos);
1281 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1285 /// then remove all LyXText in text-insets
1286 LyXParagraph * p = par;
1287 for(;p;p = p->next) {
1288 p->deleteInsetsLyXText(bv);
1293 void InsetText::resizeLyXText(BufferView * bv) const
1295 if (!par->next && !par->size()) // not neccessary!
1298 LyXParagraph * lpar = 0;
1299 LyXParagraph * selstartpar = 0;
1300 LyXParagraph * selendpar = 0;
1302 int selstartpos = 0;
1307 // ProhibitInput(bv);
1309 lpar = TEXT(bv)->cursor.par();
1310 pos = TEXT(bv)->cursor.pos();
1311 selstartpar = TEXT(bv)->sel_start_cursor.par();
1312 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1313 selendpar = TEXT(bv)->sel_end_cursor.par();
1314 selendpos = TEXT(bv)->sel_end_cursor.pos();
1315 selection = TEXT(bv)->selection;
1316 mark_set = TEXT(bv)->mark_set;
1317 deleteLyXText(bv, (the_locking_inset == 0));
1320 TEXT(bv)->selection = true;
1321 /* at this point just to avoid the Delete-Empty-Paragraph
1322 * Mechanism when setting the cursor */
1323 TEXT(bv)->mark_set = mark_set;
1325 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos);
1326 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1327 TEXT(bv)->SetCursor(bv, selendpar, selendpos);
1328 TEXT(bv)->SetSelection();
1329 TEXT(bv)->SetCursor(bv, lpar, pos);
1331 TEXT(bv)->SetCursor(bv, lpar, pos);
1332 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1333 TEXT(bv)->selection = false;
1337 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1338 // this will scroll the screen such that the cursor becomes visible
1339 bv->updateScrollbar();
1341 if (the_locking_inset) {
1342 /// then resize all LyXText in text-insets
1343 inset_x = cx(bv) - top_x + drawTextXOffset;
1344 inset_y = cy(bv) + drawTextYOffset;
1345 LyXParagraph * p = par;
1346 for(;p;p = p->next) {
1347 p->resizeInsetsLyXText(bv);