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 void InsetText::draw(BufferView * bv, LyXFont const & f,
241 int baseline, float & x, bool cleared) const
243 Painter & pain = bv->painter();
246 UpdatableInset::draw(bv, f, baseline, x, cleared);
248 if (!cleared && ((need_update==FULL) || (top_x!=int(x)) ||
249 (top_baseline!=baseline))) {
251 int h = insetAscent + insetDescent;
252 int ty = baseline - insetAscent;
256 if ((ty + h) > pain.paperHeight())
257 h = pain.paperHeight();
258 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
259 w = pain.paperWidth();
260 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
264 if (!cleared && (need_update == NONE))
267 if (top_x != int(x)) {
270 owner()->update(bv, f, true);
271 bv->text->status = LyXText::CHANGED_IN_DRAW;
275 top_baseline = baseline;
276 top_y = baseline - ascent(bv, f);
277 last_width = width(bv, f);
278 last_height = ascent(bv, f) + descent(bv, f);
280 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
281 inset_x = cx(bv) - top_x + drawTextXOffset;
282 inset_y = cy(bv) + drawTextYOffset;
284 if (!cleared && (need_update == CURSOR) && !TEXT(bv)->selection) {
289 x += 1; // place for border
291 Row * row = TEXT(bv)->GetRowNearY(y);
292 y += baseline - row->ascent_of_text() + 1;
293 if (cleared || !locked || (need_update == FULL)) {
295 TEXT(bv)->GetVisibleRow(bv, y, int(x), row, y, cleared);
299 } else if (need_update == SELECTION) {
300 bv->screen()->ToggleToggle(TEXT(bv), y, int(x));
303 if (need_update == CURSOR) {
304 bv->screen()->ToggleSelection(TEXT(bv), true, y, int(x));
305 TEXT(bv)->ClearSelection();
306 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
308 bv->screen()->Update(TEXT(bv), y, int(x));
311 TEXT(bv)->refresh_y = 0;
312 TEXT(bv)->status = LyXText::UNCHANGED;
313 if ((drawFrame == ALWAYS) || ((drawFrame == LOCKED) && locked)) {
314 pain.rectangle(top_x, baseline - insetAscent, insetWidth,
315 insetAscent + insetDescent, frame_color);
316 } else if (need_update == CLEAR_FRAME) {
317 pain.rectangle(top_x, baseline - insetAscent, insetWidth,
318 insetAscent + insetDescent,
321 x += insetWidth - TEXT_TO_INSET_OFFSET;
322 if (need_update != INIT)
327 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
329 if (reinit) { // && (need_update != CURSOR)) {
333 owner()->update(bv, font, true);
336 if (the_locking_inset) {
337 inset_x = cx(bv) - top_x + drawTextXOffset;
338 inset_y = cy(bv) + drawTextYOffset;
339 the_locking_inset->update(bv, font, reinit);
341 if (need_update == INIT) {
344 // if (!owner() && bv->text)
345 // bv->text->UpdateInset(bv, this);
347 int oldw = insetWidth;
349 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
350 // max(textWidth(bv->painter()),
351 // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
352 // (2 * TEXT_TO_INSET_OFFSET);
354 insetWidth = textWidth(bv->painter());
356 insetWidth = static_cast<int>(TEXT(bv)->width);
358 if (oldw != insetWidth) {
359 // printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
360 // textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
365 owner()->update(bv, font, reinit);
368 update(bv, font, reinit);
372 update(bv, font, reinit);
374 UpdateLocal(bv, INIT, false);
379 if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
382 TEXT(bv)->UpdateInset(bv, the_locking_inset);
385 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
389 Row * row = TEXT(bv)->GetRowNearY(y_temp);
390 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
391 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
392 TEXT_TO_INSET_OFFSET;
396 void InsetText::UpdateLocal(BufferView * bv, UpdateCodes what, bool mark_dirty)
398 TEXT(bv)->FullRebreak(bv);
399 if (need_update != INIT) {
400 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
402 else if (!the_locking_inset || (what != CURSOR))
405 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
407 bv->updateInset(this, mark_dirty);
408 if (old_par != cpar(bv)) {
409 bv->owner()->setLayout(cpar(bv)->GetLayout());
415 char const * InsetText::EditMessage() const
417 return _("Opened Text Inset");
421 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
423 // par->SetInsetOwner(this);
424 UpdatableInset::Edit(bv, x, y, button);
426 if (!bv->lockInset(this)) {
427 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
431 the_locking_inset = 0;
432 inset_pos = inset_x = inset_y = 0;
435 if (!checkAndActivateInset(bv, x, y, button))
436 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
437 y+TEXT(bv)->first+insetAscent);
438 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
439 bv->text->FinishUndo();
440 UpdateLocal(bv, FULL, false);
444 void InsetText::InsetUnlock(BufferView * bv)
446 if (the_locking_inset) {
447 the_locking_inset->InsetUnlock(bv);
448 the_locking_inset = 0;
451 no_selection = false;
453 TEXT(bv)->selection = 0;
454 UpdateLocal(bv, CLEAR_FRAME, false);
456 bv->owner()->setLayout(owner()->getLyXText(bv)
457 ->cursor.par()->GetLayout());
459 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
463 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
465 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
468 if (inset == cpar(bv)->GetInset(cpos(bv))) {
469 lyxerr[Debug::INSETS] << "OK" << endl;
470 the_locking_inset = inset;
471 inset_x = cx(bv) - top_x + drawTextXOffset;
472 inset_y = cy(bv) + drawTextYOffset;
473 inset_pos = cpos(bv);
474 inset_par = cpar(bv);
475 TEXT(bv)->UpdateInset(bv, the_locking_inset);
477 } else if (the_locking_inset && (the_locking_inset == inset)) {
478 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
479 lyxerr[Debug::INSETS] << "OK" << endl;
480 inset_x = cx(bv) - top_x + drawTextXOffset;
481 inset_y = cy(bv) + drawTextYOffset;
483 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
485 } else if (the_locking_inset) {
486 lyxerr[Debug::INSETS] << "MAYBE" << endl;
487 return the_locking_inset->LockInsetInInset(bv, inset);
489 lyxerr[Debug::INSETS] << "NOT OK" << endl;
494 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
497 if (!the_locking_inset)
499 if (the_locking_inset == inset) {
500 the_locking_inset->InsetUnlock(bv);
501 TEXT(bv)->UpdateInset(bv, inset);
502 the_locking_inset = 0;
504 moveRight(bv, false);
505 old_par = 0; // force layout setting
506 UpdateLocal(bv, CURSOR_PAR, false);
509 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
513 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
515 if (!the_locking_inset)
517 if (the_locking_inset != inset) {
518 TEXT(bv)->UpdateInset(bv, the_locking_inset);
519 need_update = CURSOR_PAR;
520 return the_locking_inset->UpdateInsetInInset(bv, inset);
522 // UpdateLocal(bv, FULL, false);
523 if (TEXT(bv)->UpdateInset(bv, inset))
524 UpdateLocal(bv, CURSOR_PAR, false);
525 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
526 inset_x = cx(bv) - top_x + drawTextXOffset;
527 inset_y = cy(bv) + drawTextYOffset;
533 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
535 no_selection = false;
537 int tmp_x = x - drawTextXOffset;
538 // int tmp_y = y + TEXT(bv)->first + insetAscent;
539 int tmp_y = y + insetAscent;
540 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
542 if (the_locking_inset) {
543 if (the_locking_inset == inset) {
544 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
547 // otherwise unlock the_locking_inset and lock the new inset
548 the_locking_inset->InsetUnlock(bv);
549 inset_x = cx(bv) - top_x + drawTextXOffset;
550 inset_y = cy(bv) + drawTextYOffset;
551 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
552 inset->Edit(bv, x - inset_x, y - inset_y, button);
553 if (the_locking_inset) {
554 UpdateLocal(bv, CURSOR_PAR, false);
558 // otherwise only unlock the_locking_inset
559 the_locking_inset->InsetUnlock(bv);
560 the_locking_inset = 0;
562 if (bv->the_locking_inset) {
563 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
564 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
565 inset_x = cx(bv) - top_x + drawTextXOffset;
566 inset_y = cy(bv) + drawTextYOffset;
567 inset_pos = cpos(bv);
568 inset_par = cpar(bv);
569 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
570 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
571 if (the_locking_inset) {
572 UpdateLocal(bv, CURSOR_PAR, false);
578 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
579 y+TEXT(bv)->first+insetAscent);
580 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
581 UpdateLocal(bv, CURSOR, false);
582 bv->owner()->setLayout(cpar(bv)->GetLayout());
588 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
590 UpdatableInset * inset = 0;
592 if (the_locking_inset) {
593 the_locking_inset->InsetButtonRelease(bv,
594 x - inset_x, y - inset_y,
597 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
598 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
599 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
600 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
602 inset_x = cx(bv) - top_x + drawTextXOffset;
603 inset_y = cy(bv) + drawTextYOffset;
604 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
605 inset->Edit(bv, x - inset_x, y - inset_y, button);
607 UpdateLocal(bv, CURSOR_PAR, false);
610 no_selection = false;
614 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
616 if (the_locking_inset) {
617 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
623 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
624 y+TEXT(bv)->first+insetAscent);
625 TEXT(bv)->SetSelection();
626 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
627 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
628 UpdateLocal(bv, SELECTION, false);
631 no_selection = false;
635 void InsetText::InsetKeyPress(XKeyEvent * xke)
637 if (the_locking_inset) {
638 the_locking_inset->InsetKeyPress(xke);
644 UpdatableInset::RESULT
645 InsetText::LocalDispatch(BufferView * bv,
646 int action, string const & arg)
648 no_selection = false;
649 UpdatableInset::RESULT
650 result= UpdatableInset::LocalDispatch(bv, action, arg);
651 if (result != UNDISPATCHED) {
652 // resetPos(bv->painter());
657 if ((action < 0) && arg.empty())
660 if (the_locking_inset) {
661 result = the_locking_inset->LocalDispatch(bv, action, arg);
662 if (result == DISPATCHED_NOUPDATE)
664 else if (result == DISPATCHED) {
665 UpdateLocal(bv, CURSOR_PAR, false);
667 } else if (result == FINISHED) {
671 moveRight(bv, false);
677 the_locking_inset = 0;
685 if (bv->buffer()->isReadonly()) {
687 // setErrorMessage(N_("Document is read only"));
691 /* Automatically delete the currently selected
692 * text and replace it with what is being
693 * typed in now. Depends on lyxrc settings
694 * "auto_region_delete", which defaults to
697 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
699 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
700 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
702 bv->text->cursor.par()->previous,
703 bv->text->cursor.par()->next
706 if (lyxrc.auto_region_delete) {
707 if (TEXT(bv)->selection){
708 TEXT(bv)->CutSelection(bv, false);
711 TEXT(bv)->ClearSelection();
712 for (string::size_type i = 0; i < arg.length(); ++i) {
713 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
716 UpdateLocal(bv, CURSOR_PAR, true);
718 // --- Cursor Movements ---------------------------------------------
720 bv->text->FinishUndo();
721 moveRight(bv, false);
722 TEXT(bv)->SetSelection();
723 UpdateLocal(bv, SELECTION, false);
726 result = moveRight(bv);
727 bv->text->FinishUndo();
728 UpdateLocal(bv, CURSOR, false);
731 bv->text->FinishUndo();
733 TEXT(bv)->SetSelection();
734 UpdateLocal(bv, SELECTION, false);
737 bv->text->FinishUndo();
738 result= moveLeft(bv);
739 UpdateLocal(bv, CURSOR, false);
742 bv->text->FinishUndo();
744 TEXT(bv)->SetSelection();
745 UpdateLocal(bv, SELECTION, false);
748 bv->text->FinishUndo();
749 result = moveDown(bv);
750 UpdateLocal(bv, CURSOR, false);
753 bv->text->FinishUndo();
755 TEXT(bv)->SetSelection();
756 UpdateLocal(bv, SELECTION, false);
759 bv->text->FinishUndo();
761 UpdateLocal(bv, CURSOR, false);
764 bv->text->FinishUndo();
765 TEXT(bv)->CursorHome(bv);
766 UpdateLocal(bv, CURSOR, false);
769 TEXT(bv)->CursorEnd(bv);
770 UpdateLocal(bv, CURSOR, false);
773 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
778 bv->text->cursor.par()->previous,
779 bv->text->cursor.par()->next
782 TEXT(bv)->Backspace(bv);
783 UpdateLocal(bv, CURSOR_PAR, true);
786 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
788 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
789 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
791 bv->text->cursor.par()->previous,
792 bv->text->cursor.par()->next
795 TEXT(bv)->Delete(bv);
796 UpdateLocal(bv, CURSOR_PAR, true);
799 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
801 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
802 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
804 bv->text->cursor.par()->previous,
805 bv->text->cursor.par()->next
808 TEXT(bv)->CutSelection(bv);
809 UpdateLocal(bv, CURSOR_PAR, true);
812 bv->text->FinishUndo();
813 TEXT(bv)->CopySelection(bv);
814 UpdateLocal(bv, CURSOR_PAR, false);
817 if (!autoBreakRows) {
820 if (cap.nrOfParagraphs() > 1) {
821 WriteAlert(_("Impossible operation"),
822 _("Cannot include more than one paragraph!"),
827 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
829 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
830 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
832 bv->text->cursor.par()->previous,
833 bv->text->cursor.par()->next
836 TEXT(bv)->PasteSelection(bv);
837 UpdateLocal(bv, CURSOR_PAR, true);
839 case LFUN_BREAKPARAGRAPH:
842 TEXT(bv)->BreakParagraph(bv, 0);
843 UpdateLocal(bv, FULL, true);
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)->InsertChar(bv, LyXParagraph::META_NEWLINE);
858 UpdateLocal(bv, CURSOR_PAR, true);
862 static LyXTextClass::size_type cur_layout = cpar(bv)->layout;
864 // Derive layout number from given argument (string)
865 // and current buffer's textclass (number). */
866 LyXTextClassList::ClassList::size_type tclass =
867 bv->buffer()->params.textclass;
868 std::pair <bool, LyXTextClass::size_type> layout =
869 textclasslist.NumberOfLayout(tclass, arg);
871 // If the entry is obsolete, use the new one instead.
873 string obs = textclasslist.Style(tclass,layout.second).
876 layout = textclasslist.NumberOfLayout(tclass, obs);
879 // see if we found the layout number:
881 string msg = string(N_("Layout ")) + arg + N_(" not known");
883 bv->owner()->getMiniBuffer()->Set(msg);
887 if (cur_layout != layout.second) {
888 cur_layout = layout.second;
889 TEXT(bv)->SetLayout(bv, layout.second);
890 bv->owner()->setLayout(cpar(bv)->GetLayout());
891 UpdateLocal(bv, CURSOR_PAR, true);
895 case LFUN_PARAGRAPH_SPACING:
896 // This one is absolutely not working. When fiddling with this
897 // it also seems to me that the paragraphs inside the insettext
898 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
900 LyXParagraph * par = TEXT(bv)->cursor.par();
901 Spacing::Space cur_spacing = par->spacing.getSpace();
902 float cur_value = 1.0;
903 if (cur_spacing == Spacing::Other) {
904 cur_value = par->spacing.getValue();
908 std::istringstream istr(arg.c_str());
910 istrstream istr(arg.c_str());
914 Spacing::Space new_spacing = cur_spacing;
915 float new_value = cur_value;
917 lyxerr << "Missing argument to `paragraph-spacing'"
919 } else if (tmp == "single") {
920 new_spacing = Spacing::Single;
921 } else if (tmp == "onehalf") {
922 new_spacing = Spacing::Onehalf;
923 } else if (tmp == "double") {
924 new_spacing = Spacing::Double;
925 } else if (tmp == "other") {
926 new_spacing = Spacing::Other;
929 lyxerr << "new_value = " << tmpval << endl;
932 } else if (tmp == "default") {
933 new_spacing = Spacing::Default;
935 lyxerr << _("Unknown spacing argument: ")
938 if (cur_spacing != new_spacing || cur_value != new_value) {
939 par->spacing.set(new_spacing, new_value);
940 //TEXT(bv)->RedoParagraph(owner->view());
941 UpdateLocal(bv, CURSOR_PAR, true);
942 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
948 result = UNDISPATCHED;
951 if (result != FINISHED) {
954 bv->unlockInset(this);
959 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
962 buf->latexParagraphs(os, par, 0, texrow);
963 return texrow.rows();
967 void InsetText::Validate(LaTeXFeatures & features) const
969 LyXParagraph * p = par;
971 p->validate(features);
977 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
979 if (textclasslist.Style(buf->params.textclass,
980 p->GetLayout()).labeltype != LABEL_MANUAL)
983 return p->BeginningOfMainBody();
987 void InsetText::GetCursorPos(BufferView * bv, int & x, int & y) const
994 int InsetText::InsetInInsetY()
996 if (!the_locking_inset)
999 return (inset_y + the_locking_inset->InsetInInsetY());
1003 void InsetText::ToggleInsetCursor(BufferView * bv)
1005 if (the_locking_inset) {
1006 the_locking_inset->ToggleInsetCursor(bv);
1010 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1012 int asc = lyxfont::maxAscent(font);
1013 int desc = lyxfont::maxDescent(font);
1016 bv->hideLockedInsetCursor();
1018 bv->showLockedInsetCursor(cx(bv)-1, cy(bv),
1020 cursor_visible = !cursor_visible;
1024 void InsetText::ShowInsetCursor(BufferView * bv)
1026 if (the_locking_inset) {
1027 the_locking_inset->ShowInsetCursor(bv);
1030 if (!cursor_visible) {
1031 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1033 int asc = lyxfont::maxAscent(font);
1034 int desc = lyxfont::maxDescent(font);
1036 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1037 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1038 cursor_visible = true;
1043 void InsetText::HideInsetCursor(BufferView * bv)
1045 if (cursor_visible) {
1046 bv->hideLockedInsetCursor();
1047 cursor_visible = false;
1049 if (the_locking_inset)
1050 the_locking_inset->HideInsetCursor(bv);
1054 UpdatableInset::RESULT
1055 InsetText::moveRight(BufferView * bv, bool activate_inset)
1057 if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1059 if (activate_inset && checkAndActivateInset(bv, false))
1061 TEXT(bv)->CursorRight(bv);
1062 return DISPATCHED_NOUPDATE;
1066 UpdatableInset::RESULT
1067 InsetText::moveLeft(BufferView * bv, bool activate_inset)
1069 if (!cpar(bv)->previous && (cpos(bv) <= 0))
1071 TEXT(bv)->CursorLeft(bv);
1072 if (activate_inset && checkAndActivateInset(bv, true))
1074 return DISPATCHED_NOUPDATE;
1078 UpdatableInset::RESULT
1079 InsetText::moveUp(BufferView * bv)
1081 if (!crow(bv)->previous())
1083 TEXT(bv)->CursorUp(bv);
1084 return DISPATCHED_NOUPDATE;
1088 UpdatableInset::RESULT
1089 InsetText::moveDown(BufferView * bv)
1091 if (!crow(bv)->next())
1093 TEXT(bv)->CursorDown(bv);
1094 return DISPATCHED_NOUPDATE;
1098 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1100 if (the_locking_inset) {
1101 if (the_locking_inset->InsertInsetAllowed(inset))
1102 return the_locking_inset->InsertInset(bv, inset);
1105 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1107 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1108 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1110 bv->text->cursor.par()->previous,
1111 bv->text->cursor.par()->next
1114 if (inset->Editable() == Inset::IS_EDITABLE) {
1115 UpdatableInset * i = static_cast<UpdatableInset *>(inset);
1116 i->setOwner(static_cast<UpdatableInset *>(this));
1118 TEXT(bv)->InsertInset(bv, inset);
1119 TEXT(bv)->selection = 0;
1120 UpdateLocal(bv, CURSOR_PAR, true);
1121 static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1126 UpdatableInset * InsetText::GetLockingInset()
1128 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1132 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1136 if (the_locking_inset)
1137 return the_locking_inset->GetFirstLockingInsetOfType(c);
1142 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1144 TEXT(bv)->SetFont(bv, font, toggleall);
1148 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1150 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1153 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1154 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1156 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1158 x = inset->width(bv, font);
1159 y = inset->descent(bv, font);
1163 inset_x = cx(bv) - top_x + drawTextXOffset;
1164 inset_y = cy(bv) + drawTextYOffset;
1165 inset->Edit(bv, x-inset_x, y-inset_y, 0);
1166 if (!the_locking_inset)
1168 UpdateLocal(bv, CURSOR_PAR, false);
1175 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1178 int tmp_x = x - drawTextXOffset;
1179 int tmp_y = y + insetAscent;
1180 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
1187 inset_x = cx(bv) - top_x + drawTextXOffset;
1188 inset_y = cy(bv) + drawTextYOffset;
1189 inset->Edit(bv, x - inset_x, y - inset_y, button);
1190 if (!the_locking_inset)
1192 UpdateLocal(bv, CURSOR_PAR, false);
1199 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1201 int w = UpdatableInset::getMaxWidth(pain, inset);
1206 w = w - top_x + owner()->x();
1209 w -= (2 * TEXT_TO_INSET_OFFSET);
1211 // return w - (2*TEXT_TO_INSET_OFFSET);
1215 void InsetText::SetParagraphData(LyXParagraph *p)
1229 par->SetInsetOwner(this);
1233 np->next = p->Clone();
1234 np->next->previous = np;
1236 np->SetInsetOwner(this);
1242 void InsetText::SetAutoBreakRows(bool flag)
1244 if (flag != autoBreakRows) {
1245 autoBreakRows = flag;
1251 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1253 if (how != drawFrame) {
1256 UpdateLocal(bv, DRAW_FRAME, false);
1261 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1263 if (frame_color != col) {
1266 UpdateLocal(bv, DRAW_FRAME, false);
1271 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1273 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1277 int InsetText::cx(BufferView * bv) const
1279 return TEXT(bv)->cursor.x() + top_x + 1;
1283 int InsetText::cy(BufferView * bv) const
1285 long int y_dummy = 0;
1286 Row * tmprow = TEXT(bv)->GetRowNearY(y_dummy);
1287 return TEXT(bv)->cursor.y() - tmprow->baseline();
1291 int InsetText::cpos(BufferView * bv) const
1293 return TEXT(bv)->cursor.pos();
1297 LyXParagraph * InsetText::cpar(BufferView * bv) const
1299 return TEXT(bv)->cursor.par();
1303 Row * InsetText::crow(BufferView * bv) const
1305 return TEXT(bv)->cursor.row();
1309 LyXText * InsetText::getLyXText(BufferView * bv) const
1311 if (cache.find(bv) != cache.end())
1313 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1316 if (the_locking_inset) {
1317 lt->SetCursor(bv, inset_par, inset_pos);
1323 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1327 /// then remove all LyXText in text-insets
1328 LyXParagraph * p = par;
1329 for(;p;p = p->next) {
1330 p->deleteInsetsLyXText(bv);
1335 void InsetText::resizeLyXText(BufferView * bv) const
1337 if (!par->next && !par->size()) // not neccessary!
1340 LyXParagraph * lpar = 0;
1341 LyXParagraph * selstartpar = 0;
1342 LyXParagraph * selendpar = 0;
1344 int selstartpos = 0;
1349 // ProhibitInput(bv);
1351 lpar = TEXT(bv)->cursor.par();
1352 pos = TEXT(bv)->cursor.pos();
1353 selstartpar = TEXT(bv)->sel_start_cursor.par();
1354 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1355 selendpar = TEXT(bv)->sel_end_cursor.par();
1356 selendpos = TEXT(bv)->sel_end_cursor.pos();
1357 selection = TEXT(bv)->selection;
1358 mark_set = TEXT(bv)->mark_set;
1359 deleteLyXText(bv, (the_locking_inset == 0));
1362 TEXT(bv)->selection = true;
1363 /* at this point just to avoid the Delete-Empty-Paragraph
1364 * Mechanism when setting the cursor */
1365 TEXT(bv)->mark_set = mark_set;
1367 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos);
1368 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1369 TEXT(bv)->SetCursor(bv, selendpar, selendpos);
1370 TEXT(bv)->SetSelection();
1371 TEXT(bv)->SetCursor(bv, lpar, pos);
1373 TEXT(bv)->SetCursor(bv, lpar, pos);
1374 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1375 TEXT(bv)->selection = false;
1379 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1380 // this will scroll the screen such that the cursor becomes visible
1381 bv->updateScrollbar();
1383 if (the_locking_inset) {
1384 /// then resize all LyXText in text-insets
1385 inset_x = cx(bv) - top_x + drawTextXOffset;
1386 inset_y = cy(bv) + drawTextYOffset;
1387 LyXParagraph * p = par;
1388 for(;p;p = p->next) {
1389 p->resizeInsetsLyXText(bv);