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;
81 void InsetText::init(InsetText const * ins)
83 top_y = last_width = last_height = 0;
84 insetAscent = insetDescent = insetWidth = 0;
85 the_locking_inset = 0;
86 cursor_visible = false;
90 drawTextXOffset = drawTextYOffset = 0;
91 autoBreakRows = false;
95 SetParagraphData(ins->par);
96 autoBreakRows = ins->autoBreakRows;
97 drawFrame = ins->drawFrame;
99 par->SetInsetOwner(this);
100 frame_color = LColor::insetframe;
106 InsetText::~InsetText()
108 LyXParagraph * p = par->next;
118 void InsetText::clear()
120 LyXParagraph * p = par->next;
127 par = new LyXParagraph();
131 Inset * InsetText::Clone() const
133 InsetText * t = new InsetText(*this);
138 void InsetText::Write(Buffer const * buf, ostream & os) const
141 WriteParagraphData(buf, os);
145 void InsetText::WriteParagraphData(Buffer const * buf, ostream & os) const
147 par->writeFile(buf, os, buf->params, 0, 0);
151 void InsetText::Read(Buffer const * buf, LyXLex & lex)
155 LyXParagraph * return_par = 0;
156 char depth = 0; // signed or unsigned?
158 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
159 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
161 LyXFont font(LyXFont::ALL_INHERIT);
163 LyXParagraph * p = par->next;
170 par = new LyXParagraph;
173 token = lex.GetString();
176 if (token == "\\end_inset")
178 if (const_cast<Buffer*>(buf)->parseSingleLyXformat2Token(lex, par, return_par,
186 // the_end read this should NEVER happen
187 lex.printError("\\the_end read in inset! Error in document!");
195 return_par->SetInsetOwner(this);
196 return_par = return_par->next;
199 if (token != "\\end_inset") {
200 lex.printError("Missing \\end_inset at this point. "
207 int InsetText::ascent(BufferView * bv, LyXFont const &) const
210 Row * row = TEXT(bv)->GetRowNearY(y_temp);
211 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
216 int InsetText::descent(BufferView * bv, LyXFont const &) const
219 Row * row = TEXT(bv)->GetRowNearY(y_temp);
220 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
221 TEXT_TO_INSET_OFFSET;
226 int InsetText::width(BufferView * bv, LyXFont const &) const
228 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
233 int InsetText::textWidth(Painter & pain) const
235 int w = getMaxWidth(pain, this);
240 w = w - top_x + owner()->x();
243 w -= (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, int(x), row, y, cleared);
307 } else if (need_update == SELECTION) {
308 bv->screen()->ToggleToggle(TEXT(bv), y, int(x));
311 if (need_update == CURSOR) {
312 bv->screen()->ToggleSelection(TEXT(bv), true, y, int(x));
313 TEXT(bv)->ClearSelection();
314 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
316 bv->screen()->Update(TEXT(bv), y, int(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()->setLayout(cpar(bv)->GetLayout());
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()->setLayout(owner()->getLyXText(bv)
465 ->cursor.par()->GetLayout());
467 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
471 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
473 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
476 if (inset == cpar(bv)->GetInset(cpos(bv))) {
477 lyxerr[Debug::INSETS] << "OK" << endl;
478 the_locking_inset = inset;
479 inset_x = cx(bv) - top_x + drawTextXOffset;
480 inset_y = cy(bv) + drawTextYOffset;
481 inset_pos = cpos(bv);
482 inset_par = cpar(bv);
483 TEXT(bv)->UpdateInset(bv, the_locking_inset);
485 } else if (the_locking_inset && (the_locking_inset == inset)) {
486 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
487 lyxerr[Debug::INSETS] << "OK" << endl;
488 inset_x = cx(bv) - top_x + drawTextXOffset;
489 inset_y = cy(bv) + drawTextYOffset;
491 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
493 } else if (the_locking_inset) {
494 lyxerr[Debug::INSETS] << "MAYBE" << endl;
495 return the_locking_inset->LockInsetInInset(bv, inset);
497 lyxerr[Debug::INSETS] << "NOT OK" << endl;
502 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
505 if (!the_locking_inset)
507 if (the_locking_inset == inset) {
508 the_locking_inset->InsetUnlock(bv);
509 TEXT(bv)->UpdateInset(bv, inset);
510 the_locking_inset = 0;
512 moveRight(bv, false);
513 old_par = 0; // force layout setting
514 UpdateLocal(bv, CURSOR_PAR, false);
517 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
521 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
523 if (!the_locking_inset)
525 if (the_locking_inset != inset) {
526 TEXT(bv)->UpdateInset(bv, the_locking_inset);
527 need_update = CURSOR_PAR;
528 return the_locking_inset->UpdateInsetInInset(bv, inset);
530 // UpdateLocal(bv, FULL, false);
531 if (TEXT(bv)->UpdateInset(bv, inset))
532 UpdateLocal(bv, CURSOR_PAR, false);
533 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
534 inset_x = cx(bv) - top_x + drawTextXOffset;
535 inset_y = cy(bv) + drawTextYOffset;
541 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
543 no_selection = false;
545 int tmp_x = x - drawTextXOffset;
546 // int tmp_y = y + TEXT(bv)->first + insetAscent;
547 int tmp_y = y + insetAscent;
548 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
550 if (the_locking_inset) {
551 if (the_locking_inset == inset) {
552 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
555 // otherwise unlock the_locking_inset and lock the new inset
556 the_locking_inset->InsetUnlock(bv);
557 inset_x = cx(bv) - top_x + drawTextXOffset;
558 inset_y = cy(bv) + drawTextYOffset;
559 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
560 inset->Edit(bv, x - inset_x, y - inset_y, button);
561 if (the_locking_inset) {
562 UpdateLocal(bv, CURSOR_PAR, false);
566 // otherwise only unlock the_locking_inset
567 the_locking_inset->InsetUnlock(bv);
568 the_locking_inset = 0;
570 if (bv->the_locking_inset) {
571 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
572 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
573 inset_x = cx(bv) - top_x + drawTextXOffset;
574 inset_y = cy(bv) + drawTextYOffset;
575 inset_pos = cpos(bv);
576 inset_par = cpar(bv);
577 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
578 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
579 if (the_locking_inset) {
580 UpdateLocal(bv, CURSOR_PAR, false);
586 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
587 y+TEXT(bv)->first+insetAscent);
588 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
589 UpdateLocal(bv, CURSOR, false);
590 bv->owner()->setLayout(cpar(bv)->GetLayout());
596 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
598 UpdatableInset * inset = 0;
600 if (the_locking_inset) {
601 the_locking_inset->InsetButtonRelease(bv,
602 x - inset_x, y - inset_y,
605 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
606 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
607 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
608 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
610 inset_x = cx(bv) - top_x + drawTextXOffset;
611 inset_y = cy(bv) + drawTextYOffset;
612 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
613 inset->Edit(bv, x - inset_x, y - inset_y, button);
615 UpdateLocal(bv, CURSOR_PAR, false);
618 no_selection = false;
622 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
624 if (the_locking_inset) {
625 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
631 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
632 y+TEXT(bv)->first+insetAscent);
633 TEXT(bv)->SetSelection();
634 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
635 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
636 UpdateLocal(bv, SELECTION, false);
639 no_selection = false;
643 void InsetText::InsetKeyPress(XKeyEvent * xke)
645 if (the_locking_inset) {
646 the_locking_inset->InsetKeyPress(xke);
652 UpdatableInset::RESULT
653 InsetText::LocalDispatch(BufferView * bv,
654 int action, string const & arg)
656 no_selection = false;
657 UpdatableInset::RESULT
658 result= UpdatableInset::LocalDispatch(bv, action, arg);
659 if (result != UNDISPATCHED) {
660 // resetPos(bv->painter());
665 if ((action < 0) && arg.empty())
668 if (the_locking_inset) {
669 result = the_locking_inset->LocalDispatch(bv, action, arg);
670 if (result == DISPATCHED_NOUPDATE)
672 else if (result == DISPATCHED) {
673 UpdateLocal(bv, CURSOR_PAR, false);
675 } else if (result == FINISHED) {
679 moveRight(bv, false);
685 the_locking_inset = 0;
693 if (bv->buffer()->isReadonly()) {
695 // setErrorMessage(N_("Document is read only"));
699 /* Automatically delete the currently selected
700 * text and replace it with what is being
701 * typed in now. Depends on lyxrc settings
702 * "auto_region_delete", which defaults to
705 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
707 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
708 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
710 bv->text->cursor.par()->previous,
711 bv->text->cursor.par()->next
714 if (lyxrc.auto_region_delete) {
715 if (TEXT(bv)->selection){
716 TEXT(bv)->CutSelection(bv, false);
719 TEXT(bv)->ClearSelection();
720 for (string::size_type i = 0; i < arg.length(); ++i) {
721 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
724 UpdateLocal(bv, CURSOR_PAR, true);
726 // --- Cursor Movements ---------------------------------------------
728 bv->text->FinishUndo();
729 moveRight(bv, false);
730 TEXT(bv)->SetSelection();
731 UpdateLocal(bv, SELECTION, false);
734 result = moveRight(bv);
735 bv->text->FinishUndo();
736 UpdateLocal(bv, CURSOR, false);
739 bv->text->FinishUndo();
741 TEXT(bv)->SetSelection();
742 UpdateLocal(bv, SELECTION, false);
745 bv->text->FinishUndo();
746 result= moveLeft(bv);
747 UpdateLocal(bv, CURSOR, false);
750 bv->text->FinishUndo();
752 TEXT(bv)->SetSelection();
753 UpdateLocal(bv, SELECTION, false);
756 bv->text->FinishUndo();
757 result = moveDown(bv);
758 UpdateLocal(bv, CURSOR, false);
761 bv->text->FinishUndo();
763 TEXT(bv)->SetSelection();
764 UpdateLocal(bv, SELECTION, false);
767 bv->text->FinishUndo();
769 UpdateLocal(bv, CURSOR, false);
772 bv->text->FinishUndo();
773 TEXT(bv)->CursorHome(bv);
774 UpdateLocal(bv, CURSOR, false);
777 TEXT(bv)->CursorEnd(bv);
778 UpdateLocal(bv, CURSOR, false);
781 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
783 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
784 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
786 bv->text->cursor.par()->previous,
787 bv->text->cursor.par()->next
790 TEXT(bv)->Backspace(bv);
791 UpdateLocal(bv, CURSOR_PAR, true);
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)->Delete(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)->CutSelection(bv);
817 UpdateLocal(bv, CURSOR_PAR, true);
820 bv->text->FinishUndo();
821 TEXT(bv)->CopySelection(bv);
822 UpdateLocal(bv, CURSOR_PAR, false);
825 if (!autoBreakRows) {
828 if (cap.nrOfParagraphs() > 1) {
829 WriteAlert(_("Impossible operation"),
830 _("Cannot include more than one paragraph!"),
835 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
837 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
838 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
840 bv->text->cursor.par()->previous,
841 bv->text->cursor.par()->next
844 TEXT(bv)->PasteSelection(bv);
845 UpdateLocal(bv, CURSOR_PAR, true);
847 case LFUN_BREAKPARAGRAPH:
850 TEXT(bv)->BreakParagraph(bv, 0);
851 UpdateLocal(bv, FULL, true);
856 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
858 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
859 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
861 bv->text->cursor.par()->previous,
862 bv->text->cursor.par()->next
865 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
866 UpdateLocal(bv, CURSOR_PAR, true);
870 static LyXTextClass::size_type cur_layout = cpar(bv)->layout;
872 // Derive layout number from given argument (string)
873 // and current buffer's textclass (number). */
874 LyXTextClassList::ClassList::size_type tclass =
875 bv->buffer()->params.textclass;
876 std::pair <bool, LyXTextClass::size_type> layout =
877 textclasslist.NumberOfLayout(tclass, arg);
879 // If the entry is obsolete, use the new one instead.
881 string obs = textclasslist.Style(tclass,layout.second).
884 layout = textclasslist.NumberOfLayout(tclass, obs);
887 // see if we found the layout number:
889 string msg = string(N_("Layout ")) + arg + N_(" not known");
891 bv->owner()->getMiniBuffer()->Set(msg);
895 if (cur_layout != layout.second) {
896 cur_layout = layout.second;
897 TEXT(bv)->SetLayout(bv, layout.second);
898 bv->owner()->setLayout(cpar(bv)->GetLayout());
899 UpdateLocal(bv, CURSOR_PAR, true);
903 case LFUN_PARAGRAPH_SPACING:
904 // This one is absolutely not working. When fiddling with this
905 // it also seems to me that the paragraphs inside the insettext
906 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
908 LyXParagraph * par = TEXT(bv)->cursor.par();
909 Spacing::Space cur_spacing = par->spacing.getSpace();
910 float cur_value = 1.0;
911 if (cur_spacing == Spacing::Other) {
912 cur_value = par->spacing.getValue();
916 std::istringstream istr(arg.c_str());
918 istrstream istr(arg.c_str());
922 Spacing::Space new_spacing = cur_spacing;
923 float new_value = cur_value;
925 lyxerr << "Missing argument to `paragraph-spacing'"
927 } else if (tmp == "single") {
928 new_spacing = Spacing::Single;
929 } else if (tmp == "onehalf") {
930 new_spacing = Spacing::Onehalf;
931 } else if (tmp == "double") {
932 new_spacing = Spacing::Double;
933 } else if (tmp == "other") {
934 new_spacing = Spacing::Other;
937 lyxerr << "new_value = " << tmpval << endl;
940 } else if (tmp == "default") {
941 new_spacing = Spacing::Default;
943 lyxerr << _("Unknown spacing argument: ")
946 if (cur_spacing != new_spacing || cur_value != new_value) {
947 par->spacing.set(new_spacing, new_value);
948 //TEXT(bv)->RedoParagraph(owner->view());
949 UpdateLocal(bv, CURSOR_PAR, true);
950 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
956 result = UNDISPATCHED;
959 if (result != FINISHED) {
962 bv->unlockInset(this);
967 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
970 buf->latexParagraphs(os, par, 0, texrow);
971 return texrow.rows();
975 void InsetText::Validate(LaTeXFeatures & features) const
977 LyXParagraph * p = par;
979 p->validate(features);
985 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
987 if (textclasslist.Style(buf->params.textclass,
988 p->GetLayout()).labeltype != LABEL_MANUAL)
991 return p->BeginningOfMainBody();
995 void InsetText::GetCursorPos(BufferView * bv, int & x, int & y) const
1002 int InsetText::InsetInInsetY()
1004 if (!the_locking_inset)
1007 return (inset_y + the_locking_inset->InsetInInsetY());
1011 void InsetText::ToggleInsetCursor(BufferView * bv)
1013 if (the_locking_inset) {
1014 the_locking_inset->ToggleInsetCursor(bv);
1018 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1020 int asc = lyxfont::maxAscent(font);
1021 int desc = lyxfont::maxDescent(font);
1024 bv->hideLockedInsetCursor();
1026 bv->showLockedInsetCursor(cx(bv)-1, cy(bv),
1028 cursor_visible = !cursor_visible;
1032 void InsetText::ShowInsetCursor(BufferView * bv)
1034 if (the_locking_inset) {
1035 the_locking_inset->ShowInsetCursor(bv);
1038 if (!cursor_visible) {
1039 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1041 int asc = lyxfont::maxAscent(font);
1042 int desc = lyxfont::maxDescent(font);
1044 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1045 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1046 cursor_visible = true;
1051 void InsetText::HideInsetCursor(BufferView * bv)
1053 if (cursor_visible) {
1054 bv->hideLockedInsetCursor();
1055 cursor_visible = false;
1057 if (the_locking_inset)
1058 the_locking_inset->HideInsetCursor(bv);
1062 UpdatableInset::RESULT
1063 InsetText::moveRight(BufferView * bv, bool activate_inset)
1065 if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1067 if (activate_inset && checkAndActivateInset(bv, false))
1069 TEXT(bv)->CursorRight(bv);
1070 return DISPATCHED_NOUPDATE;
1074 UpdatableInset::RESULT
1075 InsetText::moveLeft(BufferView * bv, bool activate_inset)
1077 if (!cpar(bv)->previous && (cpos(bv) <= 0))
1079 TEXT(bv)->CursorLeft(bv);
1080 if (activate_inset && checkAndActivateInset(bv, true))
1082 return DISPATCHED_NOUPDATE;
1086 UpdatableInset::RESULT
1087 InsetText::moveUp(BufferView * bv)
1089 if (!crow(bv)->previous())
1091 TEXT(bv)->CursorUp(bv);
1092 return DISPATCHED_NOUPDATE;
1096 UpdatableInset::RESULT
1097 InsetText::moveDown(BufferView * bv)
1099 if (!crow(bv)->next())
1101 TEXT(bv)->CursorDown(bv);
1102 return DISPATCHED_NOUPDATE;
1106 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1108 if (the_locking_inset) {
1109 if (the_locking_inset->InsertInsetAllowed(inset))
1110 return the_locking_inset->InsertInset(bv, inset);
1113 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1115 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1116 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1118 bv->text->cursor.par()->previous,
1119 bv->text->cursor.par()->next
1122 if (inset->Editable() == Inset::IS_EDITABLE) {
1123 UpdatableInset * i = static_cast<UpdatableInset *>(inset);
1124 i->setOwner(static_cast<UpdatableInset *>(this));
1126 TEXT(bv)->InsertInset(bv, inset);
1127 TEXT(bv)->selection = 0;
1128 UpdateLocal(bv, CURSOR_PAR, true);
1129 static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1134 UpdatableInset * InsetText::GetLockingInset()
1136 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1140 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1144 if (the_locking_inset)
1145 return the_locking_inset->GetFirstLockingInsetOfType(c);
1150 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1152 TEXT(bv)->SetFont(bv, font, toggleall);
1156 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1158 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1161 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1162 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1164 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1166 x = inset->width(bv, font);
1167 y = inset->descent(bv, font);
1171 inset_x = cx(bv) - top_x + drawTextXOffset;
1172 inset_y = cy(bv) + drawTextYOffset;
1173 inset->Edit(bv, x-inset_x, y-inset_y, 0);
1174 if (!the_locking_inset)
1176 UpdateLocal(bv, CURSOR_PAR, false);
1183 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1186 int tmp_x = x - drawTextXOffset;
1187 int tmp_y = y + insetAscent;
1188 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
1195 inset_x = cx(bv) - top_x + drawTextXOffset;
1196 inset_y = cy(bv) + drawTextYOffset;
1197 inset->Edit(bv, x - inset_x, y - inset_y, button);
1198 if (!the_locking_inset)
1200 UpdateLocal(bv, CURSOR_PAR, false);
1207 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1209 int w = UpdatableInset::getMaxWidth(pain, inset);
1212 return w - (2*TEXT_TO_INSET_OFFSET);
1216 void InsetText::SetParagraphData(LyXParagraph *p)
1230 par->SetInsetOwner(this);
1234 np->next = p->Clone();
1235 np->next->previous = np;
1237 np->SetInsetOwner(this);
1243 void InsetText::SetAutoBreakRows(bool flag)
1245 if (flag != autoBreakRows) {
1246 autoBreakRows = flag;
1252 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1254 if (how != drawFrame) {
1257 UpdateLocal(bv, DRAW_FRAME, false);
1262 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1264 if (frame_color != col) {
1267 UpdateLocal(bv, DRAW_FRAME, false);
1272 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1274 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1278 int InsetText::cx(BufferView * bv) const
1280 return TEXT(bv)->cursor.x() + top_x + 1;
1284 int InsetText::cy(BufferView * bv) const
1286 long int y_dummy = 0;
1287 Row * tmprow = TEXT(bv)->GetRowNearY(y_dummy);
1288 return TEXT(bv)->cursor.y() - tmprow->baseline();
1292 int InsetText::cpos(BufferView * bv) const
1294 return TEXT(bv)->cursor.pos();
1298 LyXParagraph * InsetText::cpar(BufferView * bv) const
1300 return TEXT(bv)->cursor.par();
1304 Row * InsetText::crow(BufferView * bv) const
1306 return TEXT(bv)->cursor.row();
1310 LyXText * InsetText::getLyXText(BufferView * bv) const
1312 if (cache.find(bv) != cache.end())
1314 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1317 if (the_locking_inset) {
1318 lt->SetCursor(bv, inset_par, inset_pos);
1324 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1328 /// then remove all LyXText in text-insets
1329 LyXParagraph * p = par;
1330 for(;p;p = p->next) {
1331 p->deleteInsetsLyXText(bv);
1336 void InsetText::resizeLyXText(BufferView * bv) const
1338 if (!par->next && !par->size()) // not neccessary!
1341 LyXParagraph * lpar = 0;
1342 LyXParagraph * selstartpar = 0;
1343 LyXParagraph * selendpar = 0;
1345 int selstartpos = 0;
1350 // ProhibitInput(bv);
1352 lpar = TEXT(bv)->cursor.par();
1353 pos = TEXT(bv)->cursor.pos();
1354 selstartpar = TEXT(bv)->sel_start_cursor.par();
1355 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1356 selendpar = TEXT(bv)->sel_end_cursor.par();
1357 selendpos = TEXT(bv)->sel_end_cursor.pos();
1358 selection = TEXT(bv)->selection;
1359 mark_set = TEXT(bv)->mark_set;
1360 deleteLyXText(bv, (the_locking_inset == 0));
1363 TEXT(bv)->selection = true;
1364 /* at this point just to avoid the Delete-Empty-Paragraph
1365 * Mechanism when setting the cursor */
1366 TEXT(bv)->mark_set = mark_set;
1368 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos);
1369 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1370 TEXT(bv)->SetCursor(bv, selendpar, selendpos);
1371 TEXT(bv)->SetSelection();
1372 TEXT(bv)->SetCursor(bv, lpar, pos);
1374 TEXT(bv)->SetCursor(bv, lpar, pos);
1375 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1376 TEXT(bv)->selection = false;
1380 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1381 // this will scroll the screen such that the cursor becomes visible
1382 bv->updateScrollbar();
1384 if (the_locking_inset) {
1385 /// then resize all LyXText in text-insets
1386 inset_x = cx(bv) - top_x + drawTextXOffset;
1387 inset_y = cy(bv) + drawTextYOffset;
1388 LyXParagraph * p = par;
1389 for(;p;p = p->next) {
1390 p->resizeInsetsLyXText(bv);