2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1998-2000 The LyX Team.
9 * ======================================================
20 #pragma implementation
23 #include "insettext.h"
24 #include "lyxparagraph.h"
28 #include "commandtags.h"
31 #include "BufferView.h"
33 #include "LaTeXFeatures.h"
35 #include "lyx_gui_misc.h"
37 #include "lyxcursor.h"
38 #include "CutAndPaste.h"
40 #include "minibuffer.h"
43 #include "support/textutils.h"
44 #include "support/LAssert.h"
48 #include "trans_mgr.h"
49 #include "lyxscreen.h"
57 extern unsigned char getCurrentTextClass(Buffer *);
59 InsetText::InsetText()
61 par = new LyXParagraph();
66 InsetText::InsetText(InsetText const & ins)
71 autoBreakRows = ins.autoBreakRows;
75 InsetText & InsetText::operator=(InsetText const & it)
78 autoBreakRows = it.autoBreakRows;
82 void InsetText::init(InsetText const * ins)
84 top_y = last_width = last_height = 0;
85 insetAscent = insetDescent = insetWidth = 0;
86 the_locking_inset = 0;
87 cursor_visible = false;
91 drawTextXOffset = drawTextYOffset = 0;
92 autoBreakRows = false;
96 SetParagraphData(ins->par);
97 autoBreakRows = ins->autoBreakRows;
98 drawFrame = ins->drawFrame;
100 par->SetInsetOwner(this);
101 frame_color = LColor::insetframe;
107 InsetText::~InsetText()
109 LyXParagraph * p = par->next;
119 Inset * InsetText::Clone() const
121 InsetText * t = new InsetText(*this);
126 void InsetText::Write(Buffer const * buf, ostream & os) const
129 WriteParagraphData(buf, os);
133 void InsetText::WriteParagraphData(Buffer const * buf, ostream & os) const
135 par->writeFile(buf, os, buf->params, 0, 0);
139 void InsetText::Read(Buffer const * buf, LyXLex & lex)
143 LyXParagraph * return_par = 0;
144 char depth = 0; // signed or unsigned?
145 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
146 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
147 LyXFont font(LyXFont::ALL_INHERIT);
149 LyXParagraph * p = par->next;
156 par = new LyXParagraph;
159 token = lex.GetString();
162 if (token == "\\end_inset")
164 if (const_cast<Buffer*>(buf)->parseSingleLyXformat2Token(lex, par, return_par,
168 // the_end read this should NEVER happen
169 lex.printError("\\the_end read in inset! Error in document!");
177 return_par->SetInsetOwner(this);
178 return_par = return_par->next;
181 if (token != "\\end_inset") {
182 lex.printError("Missing \\end_inset at this point. "
189 int InsetText::ascent(BufferView * bv, LyXFont const &) const
192 Row * row = TEXT(bv)->GetRowNearY(y_temp);
193 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
198 int InsetText::descent(BufferView * bv, LyXFont const &) const
201 Row * row = TEXT(bv)->GetRowNearY(y_temp);
202 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
203 TEXT_TO_INSET_OFFSET;
208 int InsetText::width(BufferView * bv, LyXFont const &) const
210 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
215 int InsetText::textWidth(Painter & pain) const
217 int w = getMaxWidth(pain, this);
219 // printf("WW1: %d\n",w);
224 w = w - top_x + owner()->x();
225 // printf("WW2: %d\n",w);
226 return w; // - top_x + owner()->x();
229 w -= (2 * TEXT_TO_INSET_OFFSET);
230 // printf("WW2: %d\n",w);
231 return w - top_x; // - top_x - (2 * TEXT_TO_INSET_OFFSET);
235 void InsetText::draw(BufferView * bv, LyXFont const & f,
236 int baseline, float & x, bool cleared) const
238 Painter & pain = bv->painter();
241 UpdatableInset::draw(bv, f, baseline, x, cleared);
243 if (!cleared && ((need_update==FULL) || (top_x!=int(x)) ||
244 (top_baseline!=baseline))) {
246 int h = insetAscent + insetDescent;
247 int ty = baseline - insetAscent;
251 if ((ty + h) > pain.paperHeight())
252 h = pain.paperHeight();
253 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
254 w = pain.paperWidth();
255 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
259 if (!cleared && (need_update == NONE))
262 if (top_x != int(x)) {
265 owner()->update(bv, f, true);
266 bv->text->status = LyXText::CHANGED_IN_DRAW;
270 top_baseline = baseline;
271 top_y = baseline - ascent(bv, f);
272 last_width = width(bv, f);
273 last_height = ascent(bv, f) + descent(bv, f);
275 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
276 inset_x = cx(bv) - top_x + drawTextXOffset;
277 inset_y = cy(bv) + drawTextYOffset;
279 if (!cleared && (need_update == CURSOR) && !TEXT(bv)->selection) {
284 x += 1; // place for border
286 Row * row = TEXT(bv)->GetRowNearY(y);
287 y += baseline - row->ascent_of_text() + 1;
288 if (cleared || !locked || (need_update == FULL)) {
290 TEXT(bv)->GetVisibleRow(bv, y, x, row, y, cleared);
294 } else if (need_update == SELECTION) {
295 bv->screen()->ToggleToggle(TEXT(bv), y, x);
298 if (need_update == CURSOR) {
299 bv->screen()->ToggleSelection(TEXT(bv), true, y, x);
300 TEXT(bv)->ClearSelection();
301 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
303 bv->screen()->Update(TEXT(bv), y, x);
306 TEXT(bv)->refresh_y = 0;
307 TEXT(bv)->status = LyXText::UNCHANGED;
308 if ((drawFrame == ALWAYS) || ((drawFrame == LOCKED) && locked)) {
309 pain.rectangle(top_x, baseline - insetAscent, insetWidth,
310 insetAscent + insetDescent, frame_color);
311 } else if (need_update == CLEAR_FRAME) {
312 pain.rectangle(top_x, baseline - insetAscent, insetWidth,
313 insetAscent + insetDescent,
316 x += insetWidth - TEXT_TO_INSET_OFFSET;
317 if (need_update != INIT)
322 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
324 if (reinit) { // && (need_update != CURSOR)) {
328 owner()->update(bv, font, true);
331 if (the_locking_inset)
332 the_locking_inset->update(bv, font, reinit);
333 if (need_update == INIT) {
336 // if (!owner() && bv->text)
337 // bv->text->UpdateInset(bv, this);
339 int oldw = insetWidth;
341 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
342 // max(textWidth(bv->painter()),
343 // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
344 // (2 * TEXT_TO_INSET_OFFSET);
346 insetWidth = textWidth(bv->painter());
348 insetWidth = static_cast<int>(TEXT(bv)->width);
350 if (oldw != insetWidth) {
351 // printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
352 // textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
357 owner()->update(bv, font, reinit);
360 update(bv, font, reinit);
364 update(bv, font, reinit);
366 UpdateLocal(bv, INIT, false);
371 if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
374 TEXT(bv)->UpdateInset(bv, the_locking_inset);
377 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
381 Row * row = TEXT(bv)->GetRowNearY(y_temp);
382 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
383 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
384 TEXT_TO_INSET_OFFSET;
388 void InsetText::UpdateLocal(BufferView * bv, UpdateCodes what, bool mark_dirty)
390 TEXT(bv)->FullRebreak(bv);
391 if (need_update != INIT) {
392 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
394 else if (!the_locking_inset || (what != CURSOR))
397 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
399 bv->updateInset(this, mark_dirty);
400 if (old_par != cpar(bv)) {
401 bv->owner()->getToolbar()->combox->select(cpar(bv)->GetLayout()+1);
407 char const * InsetText::EditMessage() const
409 return _("Opened Text Inset");
413 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
415 // par->SetInsetOwner(this);
416 UpdatableInset::Edit(bv, x, y, button);
418 if (!bv->lockInset(this)) {
419 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
423 the_locking_inset = 0;
424 inset_pos = inset_x = inset_y = 0;
427 if (!checkAndActivateInset(bv, x, y, button))
428 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
429 y+TEXT(bv)->first+insetAscent);
430 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
431 bv->text->FinishUndo();
432 UpdateLocal(bv, FULL, false);
436 void InsetText::InsetUnlock(BufferView * bv)
438 if (the_locking_inset) {
439 the_locking_inset->InsetUnlock(bv);
440 the_locking_inset = 0;
443 no_selection = false;
445 TEXT(bv)->selection = 0;
446 UpdateLocal(bv, CLEAR_FRAME, false);
448 bv->owner()->getToolbar()->combox->
449 select(owner()->getLyXText(bv)->cursor.par()->GetLayout()+1);
451 bv->owner()->getToolbar()->combox->select(bv->text->cursor.par()->
456 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
458 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
461 if (inset == cpar(bv)->GetInset(cpos(bv))) {
462 lyxerr[Debug::INSETS] << "OK" << endl;
463 the_locking_inset = inset;
464 inset_x = cx(bv) - top_x + drawTextXOffset;
465 inset_y = cy(bv) + drawTextYOffset;
466 inset_pos = cpos(bv);
467 inset_par = cpar(bv);
468 TEXT(bv)->UpdateInset(bv, the_locking_inset);
470 } else if (the_locking_inset && (the_locking_inset == inset)) {
471 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
472 lyxerr[Debug::INSETS] << "OK" << endl;
473 inset_x = cx(bv) - top_x + drawTextXOffset;
474 inset_y = cy(bv) + drawTextYOffset;
476 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
478 } else if (the_locking_inset) {
479 lyxerr[Debug::INSETS] << "MAYBE" << endl;
480 return the_locking_inset->LockInsetInInset(bv, inset);
482 lyxerr[Debug::INSETS] << "NOT OK" << endl;
487 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
490 if (!the_locking_inset)
492 if (the_locking_inset == inset) {
493 the_locking_inset->InsetUnlock(bv);
494 TEXT(bv)->UpdateInset(bv, inset);
495 the_locking_inset = 0;
497 moveRight(bv, false);
498 old_par = 0; // force layout setting
499 UpdateLocal(bv, CURSOR_PAR, false);
502 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
506 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
508 if (!the_locking_inset)
510 if (the_locking_inset != inset) {
511 TEXT(bv)->UpdateInset(bv, the_locking_inset);
512 need_update = CURSOR_PAR;
513 return the_locking_inset->UpdateInsetInInset(bv, inset);
515 // UpdateLocal(bv, FULL, false);
516 if (TEXT(bv)->UpdateInset(bv, inset))
517 UpdateLocal(bv, CURSOR_PAR, false);
518 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
519 inset_x = cx(bv) - top_x + drawTextXOffset;
520 inset_y = cy(bv) + drawTextYOffset;
526 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
528 no_selection = false;
530 int tmp_x = x - drawTextXOffset;
531 // int tmp_y = y + TEXT(bv)->first + insetAscent;
532 int tmp_y = y + insetAscent;
533 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
535 if (the_locking_inset) {
536 if (the_locking_inset == inset) {
537 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
540 // otherwise unlock the_locking_inset and lock the new inset
541 the_locking_inset->InsetUnlock(bv);
542 inset_x = cx(bv) - top_x + drawTextXOffset;
543 inset_y = cy(bv) + drawTextYOffset;
544 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
545 inset->Edit(bv, x - inset_x, y - inset_y, button);
546 if (the_locking_inset) {
547 UpdateLocal(bv, CURSOR_PAR, false);
551 // otherwise only unlock the_locking_inset
552 the_locking_inset->InsetUnlock(bv);
553 the_locking_inset = 0;
555 if (bv->the_locking_inset) {
556 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
557 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
558 inset_x = cx(bv) - top_x + drawTextXOffset;
559 inset_y = cy(bv) + drawTextYOffset;
560 inset_pos = cpos(bv);
561 inset_par = cpar(bv);
562 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
563 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
564 if (the_locking_inset) {
565 UpdateLocal(bv, CURSOR_PAR, false);
571 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
572 y+TEXT(bv)->first+insetAscent);
573 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
574 UpdateLocal(bv, CURSOR, false);
575 bv->owner()->getToolbar()->combox->select(cpar(bv)->GetLayout()+1);
581 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
583 UpdatableInset * inset = 0;
585 if (the_locking_inset) {
586 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
588 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
589 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
590 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
591 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
593 inset_x = cx(bv) - top_x + drawTextXOffset;
594 inset_y = cy(bv) + drawTextYOffset;
595 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
596 inset->Edit(bv, x - inset_x, y - inset_y, button);
598 UpdateLocal(bv, CURSOR_PAR, false);
601 no_selection = false;
605 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
607 if (the_locking_inset) {
608 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
614 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
615 y+TEXT(bv)->first+insetAscent);
616 TEXT(bv)->SetSelection();
617 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
618 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
619 UpdateLocal(bv, SELECTION, false);
622 no_selection = false;
626 void InsetText::InsetKeyPress(XKeyEvent * xke)
628 if (the_locking_inset) {
629 the_locking_inset->InsetKeyPress(xke);
635 UpdatableInset::RESULT
636 InsetText::LocalDispatch(BufferView * bv,
637 int action, string const & arg)
639 no_selection = false;
640 UpdatableInset::RESULT
641 result= UpdatableInset::LocalDispatch(bv, action, arg);
642 if (result != UNDISPATCHED) {
643 // resetPos(bv->painter());
648 if ((action < 0) && arg.empty())
651 if (the_locking_inset) {
652 result = the_locking_inset->LocalDispatch(bv, action, arg);
653 if (result == DISPATCHED_NOUPDATE)
655 else if (result == DISPATCHED) {
656 UpdateLocal(bv, CURSOR_PAR, false);
658 } else if (result == FINISHED) {
662 moveRight(bv, false);
668 the_locking_inset = 0;
676 if (bv->buffer()->isReadonly()) {
678 // setErrorMessage(N_("Document is read only"));
682 /* Automatically delete the currently selected
683 * text and replace it with what is being
684 * typed in now. Depends on lyxrc settings
685 * "auto_region_delete", which defaults to
688 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
689 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
690 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
691 if (lyxrc.auto_region_delete) {
692 if (TEXT(bv)->selection){
693 TEXT(bv)->CutSelection(bv, false);
696 TEXT(bv)->ClearSelection();
697 for (string::size_type i = 0; i < arg.length(); ++i) {
698 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
701 UpdateLocal(bv, CURSOR_PAR, true);
703 // --- Cursor Movements ---------------------------------------------
705 bv->text->FinishUndo();
706 moveRight(bv, false);
707 TEXT(bv)->SetSelection();
708 UpdateLocal(bv, SELECTION, false);
711 result = moveRight(bv);
712 bv->text->FinishUndo();
713 UpdateLocal(bv, CURSOR, false);
716 bv->text->FinishUndo();
718 TEXT(bv)->SetSelection();
719 UpdateLocal(bv, SELECTION, false);
722 bv->text->FinishUndo();
723 result= moveLeft(bv);
724 UpdateLocal(bv, CURSOR, false);
727 bv->text->FinishUndo();
729 TEXT(bv)->SetSelection();
730 UpdateLocal(bv, SELECTION, false);
733 bv->text->FinishUndo();
734 result = moveDown(bv);
735 UpdateLocal(bv, CURSOR, false);
738 bv->text->FinishUndo();
740 TEXT(bv)->SetSelection();
741 UpdateLocal(bv, SELECTION, false);
744 bv->text->FinishUndo();
746 UpdateLocal(bv, CURSOR, false);
749 bv->text->FinishUndo();
750 TEXT(bv)->CursorHome(bv);
751 UpdateLocal(bv, CURSOR, false);
754 TEXT(bv)->CursorEnd(bv);
755 UpdateLocal(bv, CURSOR, false);
758 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
759 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
760 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
761 TEXT(bv)->Backspace(bv);
762 UpdateLocal(bv, CURSOR_PAR, true);
765 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
766 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
767 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
768 TEXT(bv)->Delete(bv);
769 UpdateLocal(bv, CURSOR_PAR, true);
772 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
773 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
774 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
775 TEXT(bv)->CutSelection(bv);
776 UpdateLocal(bv, CURSOR_PAR, true);
779 bv->text->FinishUndo();
780 TEXT(bv)->CopySelection(bv);
781 UpdateLocal(bv, CURSOR_PAR, false);
784 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
785 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
786 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
787 TEXT(bv)->PasteSelection(bv);
788 UpdateLocal(bv, CURSOR_PAR, true);
790 case LFUN_BREAKPARAGRAPH:
793 TEXT(bv)->BreakParagraph(bv, 0);
794 UpdateLocal(bv, FULL, true);
799 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
800 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
801 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
802 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
803 UpdateLocal(bv, CURSOR_PAR, true);
807 static LyXTextClass::size_type cur_layout = cpar(bv)->layout;
809 // Derive layout number from given argument (string)
810 // and current buffer's textclass (number). */
811 LyXTextClassList::ClassList::size_type tclass =
812 bv->buffer()->params.textclass;
813 std::pair <bool, LyXTextClass::size_type> layout =
814 textclasslist.NumberOfLayout(tclass, arg);
816 // If the entry is obsolete, use the new one instead.
818 string obs = textclasslist.Style(tclass,layout.second).
821 layout = textclasslist.NumberOfLayout(tclass, obs);
824 // see if we found the layout number:
826 string msg = string(N_("Layout ")) + arg + N_(" not known");
828 bv->owner()->getMiniBuffer()->Set(msg);
832 if (cur_layout != layout.second) {
833 cur_layout = layout.second;
834 TEXT(bv)->SetLayout(bv, layout.second);
835 bv->owner()->getToolbar()->combox->select(cpar(bv)->GetLayout()+1);
836 UpdateLocal(bv, CURSOR_PAR, true);
840 case LFUN_PARAGRAPH_SPACING:
841 // This one is absolutely not working. When fiddling with this
842 // it also seems to me that the paragraphs inside the insettext
843 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
845 LyXParagraph * par = TEXT(bv)->cursor.par();
846 Spacing::Space cur_spacing = par->spacing.getSpace();
847 float cur_value = 1.0;
848 if (cur_spacing == Spacing::Other) {
849 cur_value = par->spacing.getValue();
853 std::istringstream istr(arg);
855 istrstream istr(arg.c_str());
859 Spacing::Space new_spacing = cur_spacing;
860 float new_value = cur_value;
862 lyxerr << "Missing argument to `paragraph-spacing'"
864 } else if (tmp == "single") {
865 new_spacing = Spacing::Single;
866 } else if (tmp == "onehalf") {
867 new_spacing = Spacing::Onehalf;
868 } else if (tmp == "double") {
869 new_spacing = Spacing::Double;
870 } else if (tmp == "other") {
871 new_spacing = Spacing::Other;
874 lyxerr << "new_value = " << tmpval << endl;
877 } else if (tmp == "default") {
878 new_spacing = Spacing::Default;
880 lyxerr << _("Unknown spacing argument: ")
883 if (cur_spacing != new_spacing || cur_value != new_value) {
884 par->spacing.set(new_spacing, new_value);
885 //TEXT(bv)->RedoParagraph(owner->view());
886 UpdateLocal(bv, CURSOR_PAR, true);
887 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
893 result = UNDISPATCHED;
896 if (result != FINISHED) {
899 bv->unlockInset(this);
904 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
907 buf->latexParagraphs(os, par, 0, texrow);
908 return texrow.rows();
912 void InsetText::Validate(LaTeXFeatures & features) const
914 LyXParagraph * p = par;
916 p->validate(features);
922 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
924 if (textclasslist.Style(buf->params.textclass,
925 p->GetLayout()).labeltype != LABEL_MANUAL)
928 return p->BeginningOfMainBody();
932 void InsetText::GetCursorPos(BufferView * bv, int & x, int & y) const
939 int InsetText::InsetInInsetY()
941 if (!the_locking_inset)
944 return (inset_y + the_locking_inset->InsetInInsetY());
948 void InsetText::ToggleInsetCursor(BufferView * bv)
950 if (the_locking_inset) {
951 the_locking_inset->ToggleInsetCursor(bv);
955 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
957 int asc = lyxfont::maxAscent(font);
958 int desc = lyxfont::maxDescent(font);
961 bv->hideLockedInsetCursor();
963 bv->showLockedInsetCursor(cx(bv), cy(bv),
965 cursor_visible = !cursor_visible;
969 void InsetText::ShowInsetCursor(BufferView * bv)
971 if (the_locking_inset) {
972 the_locking_inset->ShowInsetCursor(bv);
975 if (!cursor_visible) {
976 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
978 int asc = lyxfont::maxAscent(font);
979 int desc = lyxfont::maxDescent(font);
981 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
982 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
983 cursor_visible = true;
988 void InsetText::HideInsetCursor(BufferView * bv)
990 if (cursor_visible) {
991 bv->hideLockedInsetCursor();
992 cursor_visible = false;
994 if (the_locking_inset)
995 the_locking_inset->HideInsetCursor(bv);
999 UpdatableInset::RESULT
1000 InsetText::moveRight(BufferView * bv, bool activate_inset)
1002 if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1004 if (activate_inset && checkAndActivateInset(bv, false))
1006 TEXT(bv)->CursorRight(bv);
1007 return DISPATCHED_NOUPDATE;
1011 UpdatableInset::RESULT
1012 InsetText::moveLeft(BufferView * bv, bool activate_inset)
1014 if (!cpar(bv)->previous && (cpos(bv) <= 0))
1016 TEXT(bv)->CursorLeft(bv);
1017 if (activate_inset && checkAndActivateInset(bv, true))
1019 return DISPATCHED_NOUPDATE;
1023 UpdatableInset::RESULT
1024 InsetText::moveUp(BufferView * bv)
1026 if (!crow(bv)->previous())
1028 TEXT(bv)->CursorUp(bv);
1029 return DISPATCHED_NOUPDATE;
1033 UpdatableInset::RESULT
1034 InsetText::moveDown(BufferView * bv)
1036 if (!crow(bv)->next())
1038 TEXT(bv)->CursorDown(bv);
1039 return DISPATCHED_NOUPDATE;
1043 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1045 if (the_locking_inset) {
1046 if (the_locking_inset->InsertInsetAllowed(inset))
1047 return the_locking_inset->InsertInset(bv, inset);
1050 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1051 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1052 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
1053 if (inset->Editable() == Inset::IS_EDITABLE) {
1054 UpdatableInset * i = static_cast<UpdatableInset *>(inset);
1055 i->setOwner(static_cast<UpdatableInset *>(this));
1057 TEXT(bv)->InsertInset(bv, inset);
1058 TEXT(bv)->selection = 0;
1059 UpdateLocal(bv, CURSOR_PAR, true);
1060 static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1065 UpdatableInset * InsetText::GetLockingInset()
1067 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1071 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1075 if (the_locking_inset)
1076 return the_locking_inset->GetFirstLockingInsetOfType(c);
1081 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1083 TEXT(bv)->SetFont(bv, font, toggleall);
1087 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1089 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1092 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1093 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1095 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1097 x = inset->width(bv, font);
1098 y = inset->descent(bv, font);
1102 inset_x = cx(bv) - top_x + drawTextXOffset;
1103 inset_y = cy(bv) + drawTextYOffset;
1104 inset->Edit(bv, x-inset_x, y-inset_y, 0);
1105 if (!the_locking_inset)
1107 UpdateLocal(bv, CURSOR_PAR, false);
1114 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1117 int tmp_x = x - drawTextXOffset;
1118 int tmp_y = y + insetAscent;
1119 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
1126 inset_x = cx(bv) - top_x + drawTextXOffset;
1127 inset_y = cy(bv) + drawTextYOffset;
1128 inset->Edit(bv, x - inset_x, y - inset_y, button);
1129 if (!the_locking_inset)
1131 UpdateLocal(bv, CURSOR_PAR, false);
1138 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1140 int w = UpdatableInset::getMaxWidth(pain, inset);
1143 return w - (2*TEXT_TO_INSET_OFFSET);
1147 void InsetText::SetParagraphData(LyXParagraph *p)
1161 par->SetInsetOwner(this);
1165 np->next = p->Clone();
1166 np->next->previous = np;
1168 np->SetInsetOwner(this);
1174 void InsetText::SetAutoBreakRows(bool flag)
1176 if (flag != autoBreakRows) {
1177 autoBreakRows = flag;
1183 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1185 if (how != drawFrame) {
1188 UpdateLocal(bv, DRAW_FRAME, false);
1193 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1195 if (frame_color != col) {
1198 UpdateLocal(bv, DRAW_FRAME, false);
1203 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1205 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1209 int InsetText::cx(BufferView * bv) const
1211 return TEXT(bv)->cursor.x() + top_x + 1;
1215 int InsetText::cy(BufferView * bv) const
1217 long int y_dummy = 0;
1218 Row * tmprow = TEXT(bv)->GetRowNearY(y_dummy);
1219 return TEXT(bv)->cursor.y() - tmprow->baseline();
1223 int InsetText::cpos(BufferView * bv) const
1225 return TEXT(bv)->cursor.pos();
1229 LyXParagraph * InsetText::cpar(BufferView * bv) const
1231 return TEXT(bv)->cursor.par();
1235 Row * InsetText::crow(BufferView * bv) const
1237 return TEXT(bv)->cursor.row();
1241 LyXText * InsetText::getLyXText(BufferView * bv) const
1243 if (cache.find(bv) != cache.end())
1245 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1248 if (the_locking_inset) {
1249 lt->SetCursor(bv, inset_par, inset_pos);
1255 void InsetText::deleteLyXText(BufferView * bv) const
1258 /// then remove all LyXText in text-insets
1259 LyXParagraph * p = par;
1260 for(;p;p = p->next) {
1261 p->deleteInsetsLyXText(bv);