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)->
179 parseSingleLyXformat2Token(lex, par, return_par,token, pos, depth,
182 , footnoteflag, footnotekind
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 (bv->text->status==LyXText::CHANGED_IN_DRAW)
324 else if (need_update != INIT)
329 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
331 if (reinit) { // && (need_update != CURSOR)) {
335 owner()->update(bv, font, true);
338 if (the_locking_inset) {
339 inset_x = cx(bv) - top_x + drawTextXOffset;
340 inset_y = cy(bv) + drawTextYOffset;
341 the_locking_inset->update(bv, font, reinit);
343 if (need_update == INIT) {
346 // if (!owner() && bv->text)
347 // bv->text->UpdateInset(bv, this);
349 int oldw = insetWidth;
351 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
352 // max(textWidth(bv->painter()),
353 // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
354 // (2 * TEXT_TO_INSET_OFFSET);
356 insetWidth = textWidth(bv->painter());
358 insetWidth = static_cast<int>(TEXT(bv)->width);
360 if (oldw != insetWidth) {
361 // printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
362 // textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
367 owner()->update(bv, font, reinit);
370 update(bv, font, reinit);
374 update(bv, font, reinit);
376 UpdateLocal(bv, INIT, false);
381 if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
384 TEXT(bv)->UpdateInset(bv, the_locking_inset);
387 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
391 Row * row = TEXT(bv)->GetRowNearY(y_temp);
392 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
393 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
394 TEXT_TO_INSET_OFFSET;
398 void InsetText::UpdateLocal(BufferView * bv, UpdateCodes what, bool mark_dirty)
400 TEXT(bv)->FullRebreak(bv);
401 if (need_update != INIT) {
402 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
404 else if (!the_locking_inset || (what != CURSOR))
407 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
409 bv->updateInset(this, mark_dirty);
410 if (old_par != cpar(bv)) {
411 bv->owner()->setLayout(cpar(bv)->GetLayout());
417 char const * InsetText::EditMessage() const
419 return _("Opened Text Inset");
423 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
425 // par->SetInsetOwner(this);
426 UpdatableInset::Edit(bv, x, y, button);
428 if (!bv->lockInset(this)) {
429 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
433 the_locking_inset = 0;
434 inset_pos = inset_x = inset_y = 0;
437 if (!checkAndActivateInset(bv, x, y, button))
438 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
439 y+TEXT(bv)->first+insetAscent);
440 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
441 bv->text->FinishUndo();
442 UpdateLocal(bv, FULL, false);
446 void InsetText::InsetUnlock(BufferView * bv)
448 if (the_locking_inset) {
449 the_locking_inset->InsetUnlock(bv);
450 the_locking_inset = 0;
453 no_selection = false;
455 TEXT(bv)->selection = 0;
456 UpdateLocal(bv, CLEAR_FRAME, false);
458 bv->owner()->setLayout(owner()->getLyXText(bv)
459 ->cursor.par()->GetLayout());
461 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
465 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
467 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
470 if (inset == cpar(bv)->GetInset(cpos(bv))) {
471 lyxerr[Debug::INSETS] << "OK" << endl;
472 the_locking_inset = inset;
473 inset_x = cx(bv) - top_x + drawTextXOffset;
474 inset_y = cy(bv) + drawTextYOffset;
475 inset_pos = cpos(bv);
476 inset_par = cpar(bv);
477 TEXT(bv)->UpdateInset(bv, the_locking_inset);
479 } else if (the_locking_inset && (the_locking_inset == inset)) {
480 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
481 lyxerr[Debug::INSETS] << "OK" << endl;
482 inset_x = cx(bv) - top_x + drawTextXOffset;
483 inset_y = cy(bv) + drawTextYOffset;
485 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
487 } else if (the_locking_inset) {
488 lyxerr[Debug::INSETS] << "MAYBE" << endl;
489 return the_locking_inset->LockInsetInInset(bv, inset);
491 lyxerr[Debug::INSETS] << "NOT OK" << endl;
496 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
499 if (!the_locking_inset)
501 if (the_locking_inset == inset) {
502 the_locking_inset->InsetUnlock(bv);
503 TEXT(bv)->UpdateInset(bv, inset);
504 the_locking_inset = 0;
506 moveRight(bv, false);
507 old_par = 0; // force layout setting
508 UpdateLocal(bv, CURSOR_PAR, false);
511 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
515 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
517 if (!the_locking_inset)
519 if (the_locking_inset != inset) {
520 TEXT(bv)->UpdateInset(bv, the_locking_inset);
521 need_update = CURSOR_PAR;
522 return the_locking_inset->UpdateInsetInInset(bv, inset);
524 // UpdateLocal(bv, FULL, false);
525 if (TEXT(bv)->UpdateInset(bv, inset))
526 UpdateLocal(bv, CURSOR_PAR, false);
527 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
528 inset_x = cx(bv) - top_x + drawTextXOffset;
529 inset_y = cy(bv) + drawTextYOffset;
535 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
537 no_selection = false;
539 int tmp_x = x - drawTextXOffset;
540 // int tmp_y = y + TEXT(bv)->first + insetAscent;
541 int tmp_y = y + insetAscent;
542 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
544 if (the_locking_inset) {
545 if (the_locking_inset == inset) {
546 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
549 // otherwise unlock the_locking_inset and lock the new inset
550 the_locking_inset->InsetUnlock(bv);
551 inset_x = cx(bv) - top_x + drawTextXOffset;
552 inset_y = cy(bv) + drawTextYOffset;
553 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
554 inset->Edit(bv, x - inset_x, y - inset_y, button);
555 if (the_locking_inset) {
556 UpdateLocal(bv, CURSOR_PAR, false);
560 // otherwise only unlock the_locking_inset
561 the_locking_inset->InsetUnlock(bv);
562 the_locking_inset = 0;
564 if (bv->the_locking_inset) {
565 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
566 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
567 inset_x = cx(bv) - top_x + drawTextXOffset;
568 inset_y = cy(bv) + drawTextYOffset;
569 inset_pos = cpos(bv);
570 inset_par = cpar(bv);
571 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
572 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
573 if (the_locking_inset) {
574 UpdateLocal(bv, CURSOR_PAR, false);
580 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
581 y+TEXT(bv)->first+insetAscent);
582 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
583 UpdateLocal(bv, CURSOR, false);
584 bv->owner()->setLayout(cpar(bv)->GetLayout());
590 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
592 UpdatableInset * inset = 0;
594 if (the_locking_inset) {
595 the_locking_inset->InsetButtonRelease(bv,
596 x - inset_x, y - inset_y,
599 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
600 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
601 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
602 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
604 inset_x = cx(bv) - top_x + drawTextXOffset;
605 inset_y = cy(bv) + drawTextYOffset;
606 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
607 inset->Edit(bv, x - inset_x, y - inset_y, button);
609 UpdateLocal(bv, CURSOR_PAR, false);
612 no_selection = false;
616 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
618 if (the_locking_inset) {
619 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
625 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
626 y+TEXT(bv)->first+insetAscent);
627 TEXT(bv)->SetSelection();
628 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
629 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
630 UpdateLocal(bv, SELECTION, false);
633 no_selection = false;
637 void InsetText::InsetKeyPress(XKeyEvent * xke)
639 if (the_locking_inset) {
640 the_locking_inset->InsetKeyPress(xke);
646 UpdatableInset::RESULT
647 InsetText::LocalDispatch(BufferView * bv,
648 int action, string const & arg)
650 no_selection = false;
651 UpdatableInset::RESULT
652 result= UpdatableInset::LocalDispatch(bv, action, arg);
653 if (result != UNDISPATCHED) {
654 // resetPos(bv->painter());
659 if ((action < 0) && arg.empty())
662 if (the_locking_inset) {
663 result = the_locking_inset->LocalDispatch(bv, action, arg);
664 if (result == DISPATCHED_NOUPDATE)
666 else if (result == DISPATCHED) {
667 UpdateLocal(bv, CURSOR_PAR, false);
669 } else if (result == FINISHED) {
673 moveRight(bv, false);
679 the_locking_inset = 0;
687 if (bv->buffer()->isReadonly()) {
689 // setErrorMessage(N_("Document is read only"));
693 /* Automatically delete the currently selected
694 * text and replace it with what is being
695 * typed in now. Depends on lyxrc settings
696 * "auto_region_delete", which defaults to
699 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
701 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
702 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
704 bv->text->cursor.par()->previous,
705 bv->text->cursor.par()->next
708 if (lyxrc.auto_region_delete) {
709 if (TEXT(bv)->selection){
710 TEXT(bv)->CutSelection(bv, false);
713 TEXT(bv)->ClearSelection();
714 for (string::size_type i = 0; i < arg.length(); ++i) {
715 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
718 UpdateLocal(bv, CURSOR_PAR, true);
720 // --- Cursor Movements ---------------------------------------------
722 bv->text->FinishUndo();
723 moveRight(bv, false);
724 TEXT(bv)->SetSelection();
725 UpdateLocal(bv, SELECTION, false);
728 result = moveRight(bv);
729 bv->text->FinishUndo();
730 UpdateLocal(bv, CURSOR, false);
733 bv->text->FinishUndo();
735 TEXT(bv)->SetSelection();
736 UpdateLocal(bv, SELECTION, false);
739 bv->text->FinishUndo();
740 result= moveLeft(bv);
741 UpdateLocal(bv, CURSOR, false);
744 bv->text->FinishUndo();
746 TEXT(bv)->SetSelection();
747 UpdateLocal(bv, SELECTION, false);
750 bv->text->FinishUndo();
751 result = moveDown(bv);
752 UpdateLocal(bv, CURSOR, false);
755 bv->text->FinishUndo();
757 TEXT(bv)->SetSelection();
758 UpdateLocal(bv, SELECTION, false);
761 bv->text->FinishUndo();
763 UpdateLocal(bv, CURSOR, false);
766 bv->text->FinishUndo();
767 TEXT(bv)->CursorHome(bv);
768 UpdateLocal(bv, CURSOR, false);
771 TEXT(bv)->CursorEnd(bv);
772 UpdateLocal(bv, CURSOR, false);
775 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
777 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
778 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
780 bv->text->cursor.par()->previous,
781 bv->text->cursor.par()->next
784 TEXT(bv)->Backspace(bv);
785 UpdateLocal(bv, CURSOR_PAR, true);
788 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
790 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
791 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
793 bv->text->cursor.par()->previous,
794 bv->text->cursor.par()->next
797 TEXT(bv)->Delete(bv);
798 UpdateLocal(bv, CURSOR_PAR, true);
801 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
803 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
804 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
806 bv->text->cursor.par()->previous,
807 bv->text->cursor.par()->next
810 TEXT(bv)->CutSelection(bv);
811 UpdateLocal(bv, CURSOR_PAR, true);
814 bv->text->FinishUndo();
815 TEXT(bv)->CopySelection(bv);
816 UpdateLocal(bv, CURSOR_PAR, false);
819 if (!autoBreakRows) {
822 if (cap.nrOfParagraphs() > 1) {
823 WriteAlert(_("Impossible operation"),
824 _("Cannot include more than one paragraph!"),
829 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
831 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
832 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
834 bv->text->cursor.par()->previous,
835 bv->text->cursor.par()->next
838 TEXT(bv)->PasteSelection(bv);
839 UpdateLocal(bv, CURSOR_PAR, true);
841 case LFUN_BREAKPARAGRAPH:
844 TEXT(bv)->BreakParagraph(bv, 0);
845 UpdateLocal(bv, FULL, true);
850 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
852 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
853 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
855 bv->text->cursor.par()->previous,
856 bv->text->cursor.par()->next
859 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
860 UpdateLocal(bv, CURSOR_PAR, true);
864 static LyXTextClass::size_type cur_layout = cpar(bv)->layout;
866 // Derive layout number from given argument (string)
867 // and current buffer's textclass (number). */
868 LyXTextClassList::ClassList::size_type tclass =
869 bv->buffer()->params.textclass;
870 std::pair <bool, LyXTextClass::size_type> layout =
871 textclasslist.NumberOfLayout(tclass, arg);
873 // If the entry is obsolete, use the new one instead.
875 string obs = textclasslist.Style(tclass,layout.second).
878 layout = textclasslist.NumberOfLayout(tclass, obs);
881 // see if we found the layout number:
883 string msg = string(N_("Layout ")) + arg + N_(" not known");
885 bv->owner()->getMiniBuffer()->Set(msg);
889 if (cur_layout != layout.second) {
890 cur_layout = layout.second;
891 TEXT(bv)->SetLayout(bv, layout.second);
892 bv->owner()->setLayout(cpar(bv)->GetLayout());
893 UpdateLocal(bv, CURSOR_PAR, true);
897 case LFUN_PARAGRAPH_SPACING:
898 // This one is absolutely not working. When fiddling with this
899 // it also seems to me that the paragraphs inside the insettext
900 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
902 LyXParagraph * par = TEXT(bv)->cursor.par();
903 Spacing::Space cur_spacing = par->spacing.getSpace();
904 float cur_value = 1.0;
905 if (cur_spacing == Spacing::Other) {
906 cur_value = par->spacing.getValue();
910 std::istringstream istr(arg.c_str());
912 istrstream istr(arg.c_str());
916 Spacing::Space new_spacing = cur_spacing;
917 float new_value = cur_value;
919 lyxerr << "Missing argument to `paragraph-spacing'"
921 } else if (tmp == "single") {
922 new_spacing = Spacing::Single;
923 } else if (tmp == "onehalf") {
924 new_spacing = Spacing::Onehalf;
925 } else if (tmp == "double") {
926 new_spacing = Spacing::Double;
927 } else if (tmp == "other") {
928 new_spacing = Spacing::Other;
931 lyxerr << "new_value = " << tmpval << endl;
934 } else if (tmp == "default") {
935 new_spacing = Spacing::Default;
937 lyxerr << _("Unknown spacing argument: ")
940 if (cur_spacing != new_spacing || cur_value != new_value) {
941 par->spacing.set(new_spacing, new_value);
942 //TEXT(bv)->RedoParagraph(owner->view());
943 UpdateLocal(bv, CURSOR_PAR, true);
944 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
950 result = UNDISPATCHED;
953 if (result != FINISHED) {
956 bv->unlockInset(this);
961 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
964 buf->latexParagraphs(os, par, 0, texrow);
965 return texrow.rows();
969 void InsetText::Validate(LaTeXFeatures & features) const
971 LyXParagraph * p = par;
973 p->validate(features);
979 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
981 if (textclasslist.Style(buf->params.textclass,
982 p->GetLayout()).labeltype != LABEL_MANUAL)
985 return p->BeginningOfMainBody();
989 void InsetText::GetCursorPos(BufferView * bv, int & x, int & y) const
996 int InsetText::InsetInInsetY()
998 if (!the_locking_inset)
1001 return (inset_y + the_locking_inset->InsetInInsetY());
1005 void InsetText::ToggleInsetCursor(BufferView * bv)
1007 if (the_locking_inset) {
1008 the_locking_inset->ToggleInsetCursor(bv);
1012 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1014 int asc = lyxfont::maxAscent(font);
1015 int desc = lyxfont::maxDescent(font);
1018 bv->hideLockedInsetCursor();
1020 bv->showLockedInsetCursor(cx(bv)-1, cy(bv),
1022 cursor_visible = !cursor_visible;
1026 void InsetText::ShowInsetCursor(BufferView * bv)
1028 if (the_locking_inset) {
1029 the_locking_inset->ShowInsetCursor(bv);
1032 if (!cursor_visible) {
1033 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1035 int asc = lyxfont::maxAscent(font);
1036 int desc = lyxfont::maxDescent(font);
1038 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1039 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1040 cursor_visible = true;
1045 void InsetText::HideInsetCursor(BufferView * bv)
1047 if (cursor_visible) {
1048 bv->hideLockedInsetCursor();
1049 cursor_visible = false;
1051 if (the_locking_inset)
1052 the_locking_inset->HideInsetCursor(bv);
1056 UpdatableInset::RESULT
1057 InsetText::moveRight(BufferView * bv, bool activate_inset)
1059 if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1061 if (activate_inset && checkAndActivateInset(bv, false))
1063 TEXT(bv)->CursorRight(bv);
1064 return DISPATCHED_NOUPDATE;
1068 UpdatableInset::RESULT
1069 InsetText::moveLeft(BufferView * bv, bool activate_inset)
1071 if (!cpar(bv)->previous && (cpos(bv) <= 0))
1073 TEXT(bv)->CursorLeft(bv);
1074 if (activate_inset && checkAndActivateInset(bv, true))
1076 return DISPATCHED_NOUPDATE;
1080 UpdatableInset::RESULT
1081 InsetText::moveUp(BufferView * bv)
1083 if (!crow(bv)->previous())
1085 TEXT(bv)->CursorUp(bv);
1086 return DISPATCHED_NOUPDATE;
1090 UpdatableInset::RESULT
1091 InsetText::moveDown(BufferView * bv)
1093 if (!crow(bv)->next())
1095 TEXT(bv)->CursorDown(bv);
1096 return DISPATCHED_NOUPDATE;
1100 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1102 if (the_locking_inset) {
1103 if (the_locking_inset->InsertInsetAllowed(inset))
1104 return the_locking_inset->InsertInset(bv, inset);
1107 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1109 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1110 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1112 bv->text->cursor.par()->previous,
1113 bv->text->cursor.par()->next
1116 if (inset->Editable() == Inset::IS_EDITABLE) {
1117 UpdatableInset * i = static_cast<UpdatableInset *>(inset);
1118 i->setOwner(static_cast<UpdatableInset *>(this));
1120 TEXT(bv)->InsertInset(bv, inset);
1121 TEXT(bv)->selection = 0;
1122 UpdateLocal(bv, CURSOR_PAR, true);
1123 static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1128 UpdatableInset * InsetText::GetLockingInset()
1130 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1134 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1138 if (the_locking_inset)
1139 return the_locking_inset->GetFirstLockingInsetOfType(c);
1144 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1146 TEXT(bv)->SetFont(bv, font, toggleall);
1150 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1152 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1155 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1156 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1158 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1160 x = inset->width(bv, font);
1161 y = inset->descent(bv, font);
1165 inset_x = cx(bv) - top_x + drawTextXOffset;
1166 inset_y = cy(bv) + drawTextYOffset;
1167 inset->Edit(bv, x-inset_x, y-inset_y, 0);
1168 if (!the_locking_inset)
1170 UpdateLocal(bv, CURSOR_PAR, false);
1177 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1180 int tmp_x = x - drawTextXOffset;
1181 int tmp_y = y + insetAscent;
1182 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
1189 inset_x = cx(bv) - top_x + drawTextXOffset;
1190 inset_y = cy(bv) + drawTextYOffset;
1191 inset->Edit(bv, x - inset_x, y - inset_y, button);
1192 if (!the_locking_inset)
1194 UpdateLocal(bv, CURSOR_PAR, false);
1201 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1203 int w = UpdatableInset::getMaxWidth(pain, inset);
1208 w = w - top_x + owner()->x();
1211 w -= (2 * TEXT_TO_INSET_OFFSET);
1213 // return w - (2*TEXT_TO_INSET_OFFSET);
1217 void InsetText::SetParagraphData(LyXParagraph *p)
1231 par->SetInsetOwner(this);
1235 np->next = p->Clone();
1236 np->next->previous = np;
1238 np->SetInsetOwner(this);
1244 void InsetText::SetAutoBreakRows(bool flag)
1246 if (flag != autoBreakRows) {
1247 autoBreakRows = flag;
1255 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1257 if (how != drawFrame) {
1260 UpdateLocal(bv, DRAW_FRAME, false);
1265 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1267 if (frame_color != col) {
1270 UpdateLocal(bv, DRAW_FRAME, false);
1275 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1277 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1281 int InsetText::cx(BufferView * bv) const
1283 return TEXT(bv)->cursor.x() + top_x + 1;
1287 int InsetText::cy(BufferView * bv) const
1290 return TEXT(bv)->cursor.y() - ascent(bv, font);
1294 int InsetText::cpos(BufferView * bv) const
1296 return TEXT(bv)->cursor.pos();
1300 LyXParagraph * InsetText::cpar(BufferView * bv) const
1302 return TEXT(bv)->cursor.par();
1306 Row * InsetText::crow(BufferView * bv) const
1308 return TEXT(bv)->cursor.row();
1312 LyXText * InsetText::getLyXText(BufferView * bv) const
1314 if (cache.find(bv) != cache.end())
1316 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1319 if (the_locking_inset) {
1320 lt->SetCursor(bv, inset_par, inset_pos);
1326 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1330 /// then remove all LyXText in text-insets
1331 LyXParagraph * p = par;
1332 for(;p;p = p->next) {
1333 p->deleteInsetsLyXText(bv);
1338 void InsetText::resizeLyXText(BufferView * bv) const
1340 if (!par->next && !par->size()) // not neccessary!
1343 LyXParagraph * lpar = 0;
1344 LyXParagraph * selstartpar = 0;
1345 LyXParagraph * selendpar = 0;
1347 int selstartpos = 0;
1352 // ProhibitInput(bv);
1354 lpar = TEXT(bv)->cursor.par();
1355 pos = TEXT(bv)->cursor.pos();
1356 selstartpar = TEXT(bv)->sel_start_cursor.par();
1357 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1358 selendpar = TEXT(bv)->sel_end_cursor.par();
1359 selendpos = TEXT(bv)->sel_end_cursor.pos();
1360 selection = TEXT(bv)->selection;
1361 mark_set = TEXT(bv)->mark_set;
1362 deleteLyXText(bv, (the_locking_inset == 0));
1365 TEXT(bv)->selection = true;
1366 /* at this point just to avoid the Delete-Empty-Paragraph
1367 * Mechanism when setting the cursor */
1368 TEXT(bv)->mark_set = mark_set;
1370 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos);
1371 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1372 TEXT(bv)->SetCursor(bv, selendpar, selendpos);
1373 TEXT(bv)->SetSelection();
1374 TEXT(bv)->SetCursor(bv, lpar, pos);
1376 TEXT(bv)->SetCursor(bv, lpar, pos);
1377 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1378 TEXT(bv)->selection = false;
1382 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1383 // this will scroll the screen such that the cursor becomes visible
1384 bv->updateScrollbar();
1386 if (the_locking_inset) {
1387 /// then resize all LyXText in text-insets
1388 inset_x = cx(bv) - top_x + drawTextXOffset;
1389 inset_y = cy(bv) + drawTextYOffset;
1390 LyXParagraph * p = par;
1391 for(;p;p = p->next) {
1392 p->resizeInsetsLyXText(bv);
1399 void InsetText::removeNewlines()
1401 LyXParagraph * p = par;
1403 for(;p; p = p->next) {
1404 for(int i = 0; i < p->Last(); ++i) {
1405 if (p->GetChar(i) == LyXParagraph::META_NEWLINE)