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);
237 // printf("WW1: %d\n",w);
242 w = w - top_x + owner()->x();
243 // printf("WW2: %d\n",w);
244 return w; // - top_x + owner()->x();
247 w -= (2 * TEXT_TO_INSET_OFFSET);
248 // printf("WW2: %d\n",w);
249 return w - top_x; // - top_x - (2 * TEXT_TO_INSET_OFFSET);
253 void InsetText::draw(BufferView * bv, LyXFont const & f,
254 int baseline, float & x, bool cleared) const
256 Painter & pain = bv->painter();
259 UpdatableInset::draw(bv, f, baseline, x, cleared);
261 if (!cleared && ((need_update==FULL) || (top_x!=int(x)) ||
262 (top_baseline!=baseline))) {
264 int h = insetAscent + insetDescent;
265 int ty = baseline - insetAscent;
269 if ((ty + h) > pain.paperHeight())
270 h = pain.paperHeight();
271 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
272 w = pain.paperWidth();
273 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
277 if (!cleared && (need_update == NONE))
280 if (top_x != int(x)) {
283 owner()->update(bv, f, true);
284 bv->text->status = LyXText::CHANGED_IN_DRAW;
288 top_baseline = baseline;
289 top_y = baseline - ascent(bv, f);
290 last_width = width(bv, f);
291 last_height = ascent(bv, f) + descent(bv, f);
293 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
294 inset_x = cx(bv) - top_x + drawTextXOffset;
295 inset_y = cy(bv) + drawTextYOffset;
297 if (!cleared && (need_update == CURSOR) && !TEXT(bv)->selection) {
302 x += 1; // place for border
304 Row * row = TEXT(bv)->GetRowNearY(y);
305 y += baseline - row->ascent_of_text() + 1;
306 if (cleared || !locked || (need_update == FULL)) {
308 TEXT(bv)->GetVisibleRow(bv, y, int(x), row, y, cleared);
312 } else if (need_update == SELECTION) {
313 bv->screen()->ToggleToggle(TEXT(bv), y, int(x));
316 if (need_update == CURSOR) {
317 bv->screen()->ToggleSelection(TEXT(bv), true, y, int(x));
318 TEXT(bv)->ClearSelection();
319 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
321 bv->screen()->Update(TEXT(bv), y, int(x));
324 TEXT(bv)->refresh_y = 0;
325 TEXT(bv)->status = LyXText::UNCHANGED;
326 if ((drawFrame == ALWAYS) || ((drawFrame == LOCKED) && locked)) {
327 pain.rectangle(top_x, baseline - insetAscent, insetWidth,
328 insetAscent + insetDescent, frame_color);
329 } else if (need_update == CLEAR_FRAME) {
330 pain.rectangle(top_x, baseline - insetAscent, insetWidth,
331 insetAscent + insetDescent,
334 x += insetWidth - TEXT_TO_INSET_OFFSET;
335 if (need_update != INIT)
340 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
342 if (reinit) { // && (need_update != CURSOR)) {
346 owner()->update(bv, font, true);
349 if (the_locking_inset) {
350 inset_x = cx(bv) - top_x + drawTextXOffset;
351 inset_y = cy(bv) + drawTextYOffset;
352 the_locking_inset->update(bv, font, reinit);
354 if (need_update == INIT) {
357 // if (!owner() && bv->text)
358 // bv->text->UpdateInset(bv, this);
360 int oldw = insetWidth;
362 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
363 // max(textWidth(bv->painter()),
364 // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
365 // (2 * TEXT_TO_INSET_OFFSET);
367 insetWidth = textWidth(bv->painter());
369 insetWidth = static_cast<int>(TEXT(bv)->width);
371 if (oldw != insetWidth) {
372 // printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
373 // textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
378 owner()->update(bv, font, reinit);
381 update(bv, font, reinit);
385 update(bv, font, reinit);
387 UpdateLocal(bv, INIT, false);
392 if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
395 TEXT(bv)->UpdateInset(bv, the_locking_inset);
398 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
402 Row * row = TEXT(bv)->GetRowNearY(y_temp);
403 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
404 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
405 TEXT_TO_INSET_OFFSET;
409 void InsetText::UpdateLocal(BufferView * bv, UpdateCodes what, bool mark_dirty)
411 TEXT(bv)->FullRebreak(bv);
412 if (need_update != INIT) {
413 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
415 else if (!the_locking_inset || (what != CURSOR))
418 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
420 bv->updateInset(this, mark_dirty);
421 if (old_par != cpar(bv)) {
422 bv->owner()->setLayout(cpar(bv)->GetLayout());
428 char const * InsetText::EditMessage() const
430 return _("Opened Text Inset");
434 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
436 // par->SetInsetOwner(this);
437 UpdatableInset::Edit(bv, x, y, button);
439 if (!bv->lockInset(this)) {
440 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
444 the_locking_inset = 0;
445 inset_pos = inset_x = inset_y = 0;
448 if (!checkAndActivateInset(bv, x, y, button))
449 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
450 y+TEXT(bv)->first+insetAscent);
451 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
452 bv->text->FinishUndo();
453 UpdateLocal(bv, FULL, false);
457 void InsetText::InsetUnlock(BufferView * bv)
459 if (the_locking_inset) {
460 the_locking_inset->InsetUnlock(bv);
461 the_locking_inset = 0;
464 no_selection = false;
466 TEXT(bv)->selection = 0;
467 UpdateLocal(bv, CLEAR_FRAME, false);
469 bv->owner()->setLayout(owner()->getLyXText(bv)
470 ->cursor.par()->GetLayout());
472 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
476 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
478 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
481 if (inset == cpar(bv)->GetInset(cpos(bv))) {
482 lyxerr[Debug::INSETS] << "OK" << endl;
483 the_locking_inset = inset;
484 inset_x = cx(bv) - top_x + drawTextXOffset;
485 inset_y = cy(bv) + drawTextYOffset;
486 inset_pos = cpos(bv);
487 inset_par = cpar(bv);
488 TEXT(bv)->UpdateInset(bv, the_locking_inset);
490 } else if (the_locking_inset && (the_locking_inset == inset)) {
491 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
492 lyxerr[Debug::INSETS] << "OK" << endl;
493 inset_x = cx(bv) - top_x + drawTextXOffset;
494 inset_y = cy(bv) + drawTextYOffset;
496 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
498 } else if (the_locking_inset) {
499 lyxerr[Debug::INSETS] << "MAYBE" << endl;
500 return the_locking_inset->LockInsetInInset(bv, inset);
502 lyxerr[Debug::INSETS] << "NOT OK" << endl;
507 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
510 if (!the_locking_inset)
512 if (the_locking_inset == inset) {
513 the_locking_inset->InsetUnlock(bv);
514 TEXT(bv)->UpdateInset(bv, inset);
515 the_locking_inset = 0;
517 moveRight(bv, false);
518 old_par = 0; // force layout setting
519 UpdateLocal(bv, CURSOR_PAR, false);
522 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
526 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
528 if (!the_locking_inset)
530 if (the_locking_inset != inset) {
531 TEXT(bv)->UpdateInset(bv, the_locking_inset);
532 need_update = CURSOR_PAR;
533 return the_locking_inset->UpdateInsetInInset(bv, inset);
535 // UpdateLocal(bv, FULL, false);
536 if (TEXT(bv)->UpdateInset(bv, inset))
537 UpdateLocal(bv, CURSOR_PAR, false);
538 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
539 inset_x = cx(bv) - top_x + drawTextXOffset;
540 inset_y = cy(bv) + drawTextYOffset;
546 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
548 no_selection = false;
550 int tmp_x = x - drawTextXOffset;
551 // int tmp_y = y + TEXT(bv)->first + insetAscent;
552 int tmp_y = y + insetAscent;
553 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
555 if (the_locking_inset) {
556 if (the_locking_inset == inset) {
557 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
560 // otherwise unlock the_locking_inset and lock the new inset
561 the_locking_inset->InsetUnlock(bv);
562 inset_x = cx(bv) - top_x + drawTextXOffset;
563 inset_y = cy(bv) + drawTextYOffset;
564 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
565 inset->Edit(bv, x - inset_x, y - inset_y, button);
566 if (the_locking_inset) {
567 UpdateLocal(bv, CURSOR_PAR, false);
571 // otherwise only unlock the_locking_inset
572 the_locking_inset->InsetUnlock(bv);
573 the_locking_inset = 0;
575 if (bv->the_locking_inset) {
576 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
577 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
578 inset_x = cx(bv) - top_x + drawTextXOffset;
579 inset_y = cy(bv) + drawTextYOffset;
580 inset_pos = cpos(bv);
581 inset_par = cpar(bv);
582 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
583 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
584 if (the_locking_inset) {
585 UpdateLocal(bv, CURSOR_PAR, false);
591 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
592 y+TEXT(bv)->first+insetAscent);
593 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
594 UpdateLocal(bv, CURSOR, false);
595 bv->owner()->setLayout(cpar(bv)->GetLayout());
601 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
603 UpdatableInset * inset = 0;
605 if (the_locking_inset) {
606 the_locking_inset->InsetButtonRelease(bv,
607 x - inset_x, y - inset_y,
610 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
611 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
612 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
613 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
615 inset_x = cx(bv) - top_x + drawTextXOffset;
616 inset_y = cy(bv) + drawTextYOffset;
617 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
618 inset->Edit(bv, x - inset_x, y - inset_y, button);
620 UpdateLocal(bv, CURSOR_PAR, false);
623 no_selection = false;
627 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
629 if (the_locking_inset) {
630 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
636 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
637 y+TEXT(bv)->first+insetAscent);
638 TEXT(bv)->SetSelection();
639 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
640 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
641 UpdateLocal(bv, SELECTION, false);
644 no_selection = false;
648 void InsetText::InsetKeyPress(XKeyEvent * xke)
650 if (the_locking_inset) {
651 the_locking_inset->InsetKeyPress(xke);
657 UpdatableInset::RESULT
658 InsetText::LocalDispatch(BufferView * bv,
659 int action, string const & arg)
661 no_selection = false;
662 UpdatableInset::RESULT
663 result= UpdatableInset::LocalDispatch(bv, action, arg);
664 if (result != UNDISPATCHED) {
665 // resetPos(bv->painter());
670 if ((action < 0) && arg.empty())
673 if (the_locking_inset) {
674 result = the_locking_inset->LocalDispatch(bv, action, arg);
675 if (result == DISPATCHED_NOUPDATE)
677 else if (result == DISPATCHED) {
678 UpdateLocal(bv, CURSOR_PAR, false);
680 } else if (result == FINISHED) {
684 moveRight(bv, false);
690 the_locking_inset = 0;
698 if (bv->buffer()->isReadonly()) {
700 // setErrorMessage(N_("Document is read only"));
704 /* Automatically delete the currently selected
705 * text and replace it with what is being
706 * typed in now. Depends on lyxrc settings
707 * "auto_region_delete", which defaults to
710 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
712 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
713 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
715 bv->text->cursor.par()->previous,
716 bv->text->cursor.par()->next
719 if (lyxrc.auto_region_delete) {
720 if (TEXT(bv)->selection){
721 TEXT(bv)->CutSelection(bv, false);
724 TEXT(bv)->ClearSelection();
725 for (string::size_type i = 0; i < arg.length(); ++i) {
726 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
729 UpdateLocal(bv, CURSOR_PAR, true);
731 // --- Cursor Movements ---------------------------------------------
733 bv->text->FinishUndo();
734 moveRight(bv, false);
735 TEXT(bv)->SetSelection();
736 UpdateLocal(bv, SELECTION, false);
739 result = moveRight(bv);
740 bv->text->FinishUndo();
741 UpdateLocal(bv, CURSOR, false);
744 bv->text->FinishUndo();
746 TEXT(bv)->SetSelection();
747 UpdateLocal(bv, SELECTION, false);
750 bv->text->FinishUndo();
751 result= moveLeft(bv);
752 UpdateLocal(bv, CURSOR, false);
755 bv->text->FinishUndo();
757 TEXT(bv)->SetSelection();
758 UpdateLocal(bv, SELECTION, false);
761 bv->text->FinishUndo();
762 result = moveDown(bv);
763 UpdateLocal(bv, CURSOR, false);
766 bv->text->FinishUndo();
768 TEXT(bv)->SetSelection();
769 UpdateLocal(bv, SELECTION, false);
772 bv->text->FinishUndo();
774 UpdateLocal(bv, CURSOR, false);
777 bv->text->FinishUndo();
778 TEXT(bv)->CursorHome(bv);
779 UpdateLocal(bv, CURSOR, false);
782 TEXT(bv)->CursorEnd(bv);
783 UpdateLocal(bv, CURSOR, false);
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)->Backspace(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)->Delete(bv);
809 UpdateLocal(bv, CURSOR_PAR, true);
812 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
814 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
815 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
817 bv->text->cursor.par()->previous,
818 bv->text->cursor.par()->next
821 TEXT(bv)->CutSelection(bv);
822 UpdateLocal(bv, CURSOR_PAR, true);
825 bv->text->FinishUndo();
826 TEXT(bv)->CopySelection(bv);
827 UpdateLocal(bv, CURSOR_PAR, false);
830 if (!autoBreakRows) {
833 if (cap.nrOfParagraphs() > 1) {
834 WriteAlert(_("Impossible operation"),
835 _("Cannot include more than one paragraph!"),
840 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
842 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
843 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
845 bv->text->cursor.par()->previous,
846 bv->text->cursor.par()->next
849 TEXT(bv)->PasteSelection(bv);
850 UpdateLocal(bv, CURSOR_PAR, true);
852 case LFUN_BREAKPARAGRAPH:
855 TEXT(bv)->BreakParagraph(bv, 0);
856 UpdateLocal(bv, FULL, true);
861 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
863 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
864 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
866 bv->text->cursor.par()->previous,
867 bv->text->cursor.par()->next
870 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
871 UpdateLocal(bv, CURSOR_PAR, true);
875 static LyXTextClass::size_type cur_layout = cpar(bv)->layout;
877 // Derive layout number from given argument (string)
878 // and current buffer's textclass (number). */
879 LyXTextClassList::ClassList::size_type tclass =
880 bv->buffer()->params.textclass;
881 std::pair <bool, LyXTextClass::size_type> layout =
882 textclasslist.NumberOfLayout(tclass, arg);
884 // If the entry is obsolete, use the new one instead.
886 string obs = textclasslist.Style(tclass,layout.second).
889 layout = textclasslist.NumberOfLayout(tclass, obs);
892 // see if we found the layout number:
894 string msg = string(N_("Layout ")) + arg + N_(" not known");
896 bv->owner()->getMiniBuffer()->Set(msg);
900 if (cur_layout != layout.second) {
901 cur_layout = layout.second;
902 TEXT(bv)->SetLayout(bv, layout.second);
903 bv->owner()->setLayout(cpar(bv)->GetLayout());
904 UpdateLocal(bv, CURSOR_PAR, true);
908 case LFUN_PARAGRAPH_SPACING:
909 // This one is absolutely not working. When fiddling with this
910 // it also seems to me that the paragraphs inside the insettext
911 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
913 LyXParagraph * par = TEXT(bv)->cursor.par();
914 Spacing::Space cur_spacing = par->spacing.getSpace();
915 float cur_value = 1.0;
916 if (cur_spacing == Spacing::Other) {
917 cur_value = par->spacing.getValue();
921 std::istringstream istr(arg);
923 istrstream istr(arg.c_str());
927 Spacing::Space new_spacing = cur_spacing;
928 float new_value = cur_value;
930 lyxerr << "Missing argument to `paragraph-spacing'"
932 } else if (tmp == "single") {
933 new_spacing = Spacing::Single;
934 } else if (tmp == "onehalf") {
935 new_spacing = Spacing::Onehalf;
936 } else if (tmp == "double") {
937 new_spacing = Spacing::Double;
938 } else if (tmp == "other") {
939 new_spacing = Spacing::Other;
942 lyxerr << "new_value = " << tmpval << endl;
945 } else if (tmp == "default") {
946 new_spacing = Spacing::Default;
948 lyxerr << _("Unknown spacing argument: ")
951 if (cur_spacing != new_spacing || cur_value != new_value) {
952 par->spacing.set(new_spacing, new_value);
953 //TEXT(bv)->RedoParagraph(owner->view());
954 UpdateLocal(bv, CURSOR_PAR, true);
955 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
961 result = UNDISPATCHED;
964 if (result != FINISHED) {
967 bv->unlockInset(this);
972 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
975 buf->latexParagraphs(os, par, 0, texrow);
976 return texrow.rows();
980 void InsetText::Validate(LaTeXFeatures & features) const
982 LyXParagraph * p = par;
984 p->validate(features);
990 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
992 if (textclasslist.Style(buf->params.textclass,
993 p->GetLayout()).labeltype != LABEL_MANUAL)
996 return p->BeginningOfMainBody();
1000 void InsetText::GetCursorPos(BufferView * bv, int & x, int & y) const
1007 int InsetText::InsetInInsetY()
1009 if (!the_locking_inset)
1012 return (inset_y + the_locking_inset->InsetInInsetY());
1016 void InsetText::ToggleInsetCursor(BufferView * bv)
1018 if (the_locking_inset) {
1019 the_locking_inset->ToggleInsetCursor(bv);
1023 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1025 int asc = lyxfont::maxAscent(font);
1026 int desc = lyxfont::maxDescent(font);
1029 bv->hideLockedInsetCursor();
1031 bv->showLockedInsetCursor(cx(bv)-1, cy(bv),
1033 cursor_visible = !cursor_visible;
1037 void InsetText::ShowInsetCursor(BufferView * bv)
1039 if (the_locking_inset) {
1040 the_locking_inset->ShowInsetCursor(bv);
1043 if (!cursor_visible) {
1044 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1046 int asc = lyxfont::maxAscent(font);
1047 int desc = lyxfont::maxDescent(font);
1049 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1050 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1051 cursor_visible = true;
1056 void InsetText::HideInsetCursor(BufferView * bv)
1058 if (cursor_visible) {
1059 bv->hideLockedInsetCursor();
1060 cursor_visible = false;
1062 if (the_locking_inset)
1063 the_locking_inset->HideInsetCursor(bv);
1067 UpdatableInset::RESULT
1068 InsetText::moveRight(BufferView * bv, bool activate_inset)
1070 if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1072 if (activate_inset && checkAndActivateInset(bv, false))
1074 TEXT(bv)->CursorRight(bv);
1075 return DISPATCHED_NOUPDATE;
1079 UpdatableInset::RESULT
1080 InsetText::moveLeft(BufferView * bv, bool activate_inset)
1082 if (!cpar(bv)->previous && (cpos(bv) <= 0))
1084 TEXT(bv)->CursorLeft(bv);
1085 if (activate_inset && checkAndActivateInset(bv, true))
1087 return DISPATCHED_NOUPDATE;
1091 UpdatableInset::RESULT
1092 InsetText::moveUp(BufferView * bv)
1094 if (!crow(bv)->previous())
1096 TEXT(bv)->CursorUp(bv);
1097 return DISPATCHED_NOUPDATE;
1101 UpdatableInset::RESULT
1102 InsetText::moveDown(BufferView * bv)
1104 if (!crow(bv)->next())
1106 TEXT(bv)->CursorDown(bv);
1107 return DISPATCHED_NOUPDATE;
1111 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1113 if (the_locking_inset) {
1114 if (the_locking_inset->InsertInsetAllowed(inset))
1115 return the_locking_inset->InsertInset(bv, inset);
1118 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1120 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1121 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1123 bv->text->cursor.par()->previous,
1124 bv->text->cursor.par()->next
1127 if (inset->Editable() == Inset::IS_EDITABLE) {
1128 UpdatableInset * i = static_cast<UpdatableInset *>(inset);
1129 i->setOwner(static_cast<UpdatableInset *>(this));
1131 TEXT(bv)->InsertInset(bv, inset);
1132 TEXT(bv)->selection = 0;
1133 UpdateLocal(bv, CURSOR_PAR, true);
1134 static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1139 UpdatableInset * InsetText::GetLockingInset()
1141 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1145 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1149 if (the_locking_inset)
1150 return the_locking_inset->GetFirstLockingInsetOfType(c);
1155 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1157 TEXT(bv)->SetFont(bv, font, toggleall);
1161 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1163 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1166 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1167 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1169 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1171 x = inset->width(bv, font);
1172 y = inset->descent(bv, font);
1176 inset_x = cx(bv) - top_x + drawTextXOffset;
1177 inset_y = cy(bv) + drawTextYOffset;
1178 inset->Edit(bv, x-inset_x, y-inset_y, 0);
1179 if (!the_locking_inset)
1181 UpdateLocal(bv, CURSOR_PAR, false);
1188 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1191 int tmp_x = x - drawTextXOffset;
1192 int tmp_y = y + insetAscent;
1193 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
1200 inset_x = cx(bv) - top_x + drawTextXOffset;
1201 inset_y = cy(bv) + drawTextYOffset;
1202 inset->Edit(bv, x - inset_x, y - inset_y, button);
1203 if (!the_locking_inset)
1205 UpdateLocal(bv, CURSOR_PAR, false);
1212 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1214 int w = UpdatableInset::getMaxWidth(pain, inset);
1217 return w - (2*TEXT_TO_INSET_OFFSET);
1221 void InsetText::SetParagraphData(LyXParagraph *p)
1235 par->SetInsetOwner(this);
1239 np->next = p->Clone();
1240 np->next->previous = np;
1242 np->SetInsetOwner(this);
1248 void InsetText::SetAutoBreakRows(bool flag)
1250 if (flag != autoBreakRows) {
1251 autoBreakRows = flag;
1257 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1259 if (how != drawFrame) {
1262 UpdateLocal(bv, DRAW_FRAME, false);
1267 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1269 if (frame_color != col) {
1272 UpdateLocal(bv, DRAW_FRAME, false);
1277 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1279 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1283 int InsetText::cx(BufferView * bv) const
1285 return TEXT(bv)->cursor.x() + top_x + 1;
1289 int InsetText::cy(BufferView * bv) const
1291 long int y_dummy = 0;
1292 Row * tmprow = TEXT(bv)->GetRowNearY(y_dummy);
1293 return TEXT(bv)->cursor.y() - tmprow->baseline();
1297 int InsetText::cpos(BufferView * bv) const
1299 return TEXT(bv)->cursor.pos();
1303 LyXParagraph * InsetText::cpar(BufferView * bv) const
1305 return TEXT(bv)->cursor.par();
1309 Row * InsetText::crow(BufferView * bv) const
1311 return TEXT(bv)->cursor.row();
1315 LyXText * InsetText::getLyXText(BufferView * bv) const
1317 if (cache.find(bv) != cache.end())
1319 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1322 if (the_locking_inset) {
1323 lt->SetCursor(bv, inset_par, inset_pos);
1329 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1333 /// then remove all LyXText in text-insets
1334 LyXParagraph * p = par;
1335 for(;p;p = p->next) {
1336 p->deleteInsetsLyXText(bv);
1341 void InsetText::resizeLyXText(BufferView * bv) const
1343 if (!par->next && !par->size()) // not neccessary!
1346 LyXParagraph * lpar = 0;
1347 LyXParagraph * selstartpar = 0;
1348 LyXParagraph * selendpar = 0;
1350 int selstartpos = 0;
1355 // ProhibitInput(bv);
1357 lpar = TEXT(bv)->cursor.par();
1358 pos = TEXT(bv)->cursor.pos();
1359 selstartpar = TEXT(bv)->sel_start_cursor.par();
1360 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1361 selendpar = TEXT(bv)->sel_end_cursor.par();
1362 selendpos = TEXT(bv)->sel_end_cursor.pos();
1363 selection = TEXT(bv)->selection;
1364 mark_set = TEXT(bv)->mark_set;
1365 deleteLyXText(bv, (the_locking_inset == 0));
1368 TEXT(bv)->selection = true;
1369 /* at this point just to avoid the Delete-Empty-Paragraph
1370 * Mechanism when setting the cursor */
1371 TEXT(bv)->mark_set = mark_set;
1373 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos);
1374 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1375 TEXT(bv)->SetCursor(bv, selendpar, selendpos);
1376 TEXT(bv)->SetSelection();
1377 TEXT(bv)->SetCursor(bv, lpar, pos);
1379 TEXT(bv)->SetCursor(bv, lpar, pos);
1380 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1381 TEXT(bv)->selection = false;
1385 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1386 // this will scroll the screen such that the cursor becomes visible
1387 bv->updateScrollbar();
1389 if (the_locking_inset) {
1390 /// then resize all LyXText in text-insets
1391 inset_x = cx(bv) - top_x + drawTextXOffset;
1392 inset_y = cy(bv) + drawTextYOffset;
1393 LyXParagraph * p = par;
1394 for(;p;p = p->next) {
1395 p->resizeInsetsLyXText(bv);