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;
269 top_baseline = baseline;
270 top_y = baseline - ascent(bv, f);
271 last_width = width(bv, f);
272 last_height = ascent(bv, f) + descent(bv, f);
274 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
275 inset_x = cx(bv) - top_x + drawTextXOffset;
276 inset_y = cy(bv) + drawTextYOffset;
278 if (!cleared && (need_update == CURSOR)) {
283 x += TEXT_TO_INSET_OFFSET; // place for border
285 Row * row = TEXT(bv)->GetRowNearY(y);
286 y += baseline - row->ascent_of_text() + 1;
287 if (cleared || !locked || (need_update == FULL)) {
289 TEXT(bv)->GetVisibleRow(bv, y, x, row, y, cleared);
293 } else if (need_update == SELECTION) {
294 bv->screen()->ToggleToggle(getLyXText(bv), y, x);
297 bv->screen()->Update(TEXT(bv), y, x);
300 TEXT(bv)->refresh_y = 0;
301 TEXT(bv)->status = LyXText::UNCHANGED;
302 if ((drawFrame == ALWAYS) || ((drawFrame == LOCKED) && locked)) {
303 pain.rectangle(top_x, baseline - insetAscent, insetWidth,
304 insetAscent + insetDescent, frame_color);
305 } else if (need_update == CLEAR_FRAME) {
306 pain.rectangle(top_x, baseline - insetAscent, insetWidth,
307 insetAscent + insetDescent,
310 x += insetWidth - TEXT_TO_INSET_OFFSET;
311 if (need_update != INIT)
316 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
318 if (reinit) { // && (need_update != CURSOR)) {
322 owner()->update(bv, font, true);
325 if (the_locking_inset)
326 the_locking_inset->update(bv, font, reinit);
327 if (need_update == INIT) {
330 // if (!owner() && bv->text)
331 // bv->text->UpdateInset(bv, this);
333 int oldw = insetWidth;
335 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
336 // max(textWidth(bv->painter()),
337 // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
338 // (2 * TEXT_TO_INSET_OFFSET);
340 insetWidth = textWidth(bv->painter());
342 insetWidth = static_cast<int>(TEXT(bv)->width);
344 if (oldw != insetWidth) {
345 // printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
346 // textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
351 owner()->update(bv, font, reinit);
354 update(bv, font, reinit);
358 update(bv, font, reinit);
360 UpdateLocal(bv, INIT, false);
365 if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
368 TEXT(bv)->UpdateInset(bv, the_locking_inset);
371 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
375 Row * row = TEXT(bv)->GetRowNearY(y_temp);
376 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
377 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
378 TEXT_TO_INSET_OFFSET;
382 void InsetText::UpdateLocal(BufferView * bv, UpdateCodes what, bool mark_dirty)
384 TEXT(bv)->FullRebreak(bv);
385 if (need_update != INIT) {
386 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
388 else if (!the_locking_inset || (what != CURSOR))
391 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED))
392 bv->updateInset(this, mark_dirty);
393 if (old_par != cpar(bv)) {
394 bv->owner()->getToolbar()->combox->select(cpar(bv)->GetLayout()+1);
400 char const * InsetText::EditMessage() const
402 return _("Opened Text Inset");
406 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
408 // par->SetInsetOwner(this);
409 UpdatableInset::Edit(bv, x, y, button);
411 if (!bv->lockInset(this)) {
412 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
416 the_locking_inset = 0;
417 inset_pos = inset_x = inset_y = 0;
419 if (!checkAndActivateInset(bv, x, y, button))
420 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
421 y+TEXT(bv)->first+insetAscent);
422 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
423 bv->text->FinishUndo();
424 UpdateLocal(bv, FULL, false);
428 void InsetText::InsetUnlock(BufferView * bv)
430 if (the_locking_inset) {
431 the_locking_inset->InsetUnlock(bv);
432 the_locking_inset = 0;
435 no_selection = false;
437 TEXT(bv)->selection = 0;
438 UpdateLocal(bv, CLEAR_FRAME, false);
439 bv->owner()->getToolbar()->combox->select(bv->text->cursor.par()->GetLayout()+1);
443 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
445 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
448 if (inset == cpar(bv)->GetInset(cpos(bv))) {
449 lyxerr[Debug::INSETS] << "OK" << endl;
450 the_locking_inset = inset;
451 inset_x = cx(bv) - top_x + drawTextXOffset;
452 inset_y = cy(bv) + drawTextYOffset;
453 inset_pos = cpos(bv);
454 inset_par = cpar(bv);
455 TEXT(bv)->UpdateInset(bv, the_locking_inset);
457 } else if (the_locking_inset && (the_locking_inset == inset)) {
458 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
459 lyxerr[Debug::INSETS] << "OK" << endl;
460 inset_x = cx(bv) - top_x + drawTextXOffset;
461 inset_y = cy(bv) + drawTextYOffset;
463 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
465 } else if (the_locking_inset) {
466 lyxerr[Debug::INSETS] << "MAYBE" << endl;
467 return the_locking_inset->LockInsetInInset(bv, inset);
469 lyxerr[Debug::INSETS] << "NOT OK" << endl;
474 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
477 if (!the_locking_inset)
479 if (the_locking_inset == inset) {
480 the_locking_inset->InsetUnlock(bv);
481 TEXT(bv)->UpdateInset(bv, inset);
482 the_locking_inset = 0;
484 moveRight(bv, false);
485 UpdateLocal(bv, CURSOR_PAR, false);
488 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
492 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
494 if (!the_locking_inset)
496 if (the_locking_inset != inset) {
497 TEXT(bv)->UpdateInset(bv, the_locking_inset);
498 need_update = CURSOR_PAR;
499 return the_locking_inset->UpdateInsetInInset(bv, inset);
501 // UpdateLocal(bv, FULL, false);
502 if (TEXT(bv)->UpdateInset(bv, inset))
503 UpdateLocal(bv, CURSOR_PAR, false);
504 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
505 inset_x = cx(bv) - top_x + drawTextXOffset;
506 inset_y = cy(bv) + drawTextYOffset;
512 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
514 if (TEXT(bv)->selection) {
515 TEXT(bv)->selection = 0;
516 UpdateLocal(bv, FULL, false);
518 no_selection = false;
520 int tmp_x = x - drawTextXOffset;
521 // int tmp_y = y + TEXT(bv)->first + insetAscent;
522 int tmp_y = y + insetAscent;
523 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
525 if (the_locking_inset) {
526 if (the_locking_inset == inset) {
527 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
530 // otherwise unlock the_locking_inset and lock the new inset
531 the_locking_inset->InsetUnlock(bv);
532 inset_x = cx(bv) - top_x + drawTextXOffset;
533 inset_y = cy(bv) + drawTextYOffset;
534 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
535 inset->Edit(bv, x - inset_x, y - inset_y, button);
536 if (the_locking_inset) {
537 UpdateLocal(bv, CURSOR_PAR, false);
541 // otherwise only unlock the_locking_inset
542 the_locking_inset->InsetUnlock(bv);
543 the_locking_inset = 0;
545 if (bv->the_locking_inset) {
546 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
547 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
548 inset_x = cx(bv) - top_x + drawTextXOffset;
549 inset_y = cy(bv) + drawTextYOffset;
550 inset_pos = cpos(bv);
551 inset_par = cpar(bv);
552 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
553 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
554 if (the_locking_inset) {
555 UpdateLocal(bv, CURSOR_PAR, false);
561 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
562 y+TEXT(bv)->first+insetAscent);
566 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
568 UpdatableInset * inset = 0;
570 if (the_locking_inset) {
571 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
573 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
574 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
575 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
576 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
578 inset_x = cx(bv) - top_x + drawTextXOffset;
579 inset_y = cy(bv) + drawTextYOffset;
580 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
581 inset->Edit(bv, x - inset_x, y - inset_y, button);
583 UpdateLocal(bv, CURSOR_PAR, false);
586 no_selection = false;
590 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
592 if (the_locking_inset) {
593 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
597 #warning REDO this (Jug)
599 // LyXCursor old = selection_end_cursor;
601 // setPos(bv->painter(), x, y);
602 // selection_end_cursor = cursor;
603 // if (old != selection_end_cursor)
604 // UpdateLocal(bv, false, false);
607 no_selection = false;
611 void InsetText::InsetKeyPress(XKeyEvent * xke)
613 if (the_locking_inset) {
614 the_locking_inset->InsetKeyPress(xke);
620 UpdatableInset::RESULT
621 InsetText::LocalDispatch(BufferView * bv,
622 int action, string const & arg)
624 no_selection = false;
625 UpdatableInset::RESULT
626 result= UpdatableInset::LocalDispatch(bv, action, arg);
627 if (result != UNDISPATCHED) {
628 // resetPos(bv->painter());
633 if ((action < 0) && arg.empty())
636 if (the_locking_inset) {
637 result = the_locking_inset->LocalDispatch(bv, action, arg);
638 if (result == DISPATCHED_NOUPDATE)
640 else if (result == DISPATCHED) {
641 UpdateLocal(bv, CURSOR_PAR, false);
643 } else if (result == FINISHED) {
647 moveRight(bv, false);
653 the_locking_inset = 0;
661 if (bv->buffer()->isReadonly()) {
663 // setErrorMessage(N_("Document is read only"));
667 /* Automatically delete the currently selected
668 * text and replace it with what is being
669 * typed in now. Depends on lyxrc settings
670 * "auto_region_delete", which defaults to
673 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
674 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
675 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
676 if (lyxrc.auto_region_delete) {
677 if (TEXT(bv)->selection){
678 TEXT(bv)->CutSelection(bv, false);
681 TEXT(bv)->ClearSelection();
682 for (string::size_type i = 0; i < arg.length(); ++i) {
683 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
686 UpdateLocal(bv, CURSOR_PAR, true);
688 // --- Cursor Movements ---------------------------------------------
690 bv->text->FinishUndo();
691 moveRight(bv, false);
692 TEXT(bv)->SetSelection();
693 UpdateLocal(bv, SELECTION, false);
696 bv->text->FinishUndo();
697 result = moveRight(bv);
698 TEXT(bv)->selection = 0;
699 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
700 UpdateLocal(bv, CURSOR, false);
703 bv->text->FinishUndo();
705 TEXT(bv)->SetSelection();
706 UpdateLocal(bv, SELECTION, false);
709 bv->text->FinishUndo();
710 result= moveLeft(bv);
711 TEXT(bv)->selection = 0;
712 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
713 UpdateLocal(bv, CURSOR, false);
716 bv->text->FinishUndo();
718 TEXT(bv)->SetSelection();
719 UpdateLocal(bv, SELECTION, false);
722 bv->text->FinishUndo();
723 result = moveDown(bv);
724 TEXT(bv)->selection = 0;
725 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
726 UpdateLocal(bv, CURSOR, false);
729 bv->text->FinishUndo();
731 TEXT(bv)->SetSelection();
732 UpdateLocal(bv, SELECTION, false);
735 bv->text->FinishUndo();
737 TEXT(bv)->selection = 0;
738 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
739 UpdateLocal(bv, CURSOR, false);
742 bv->text->FinishUndo();
743 TEXT(bv)->CursorHome(bv);
744 TEXT(bv)->selection = 0;
745 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
746 UpdateLocal(bv, CURSOR, false);
749 TEXT(bv)->CursorEnd(bv);
750 TEXT(bv)->selection = 0;
751 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
752 UpdateLocal(bv, CURSOR, false);
755 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
756 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
757 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
758 TEXT(bv)->Backspace(bv);
759 UpdateLocal(bv, CURSOR_PAR, true);
762 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
763 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
764 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
765 TEXT(bv)->Delete(bv);
766 UpdateLocal(bv, CURSOR_PAR, true);
769 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
770 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
771 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
772 TEXT(bv)->CutSelection(bv);
773 UpdateLocal(bv, CURSOR_PAR, true);
776 bv->text->FinishUndo();
777 TEXT(bv)->CopySelection(bv);
778 UpdateLocal(bv, CURSOR_PAR, false);
781 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
782 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
783 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
784 TEXT(bv)->PasteSelection(bv);
785 UpdateLocal(bv, CURSOR_PAR, true);
787 case LFUN_BREAKPARAGRAPH:
790 TEXT(bv)->BreakParagraph(bv, 0);
791 UpdateLocal(bv, FULL, true);
796 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
797 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
798 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
799 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
800 UpdateLocal(bv, CURSOR_PAR, true);
804 static LyXTextClass::size_type cur_layout = cpar(bv)->layout;
806 // Derive layout number from given argument (string)
807 // and current buffer's textclass (number). */
808 LyXTextClassList::ClassList::size_type tclass =
809 bv->buffer()->params.textclass;
810 std::pair <bool, LyXTextClass::size_type> layout =
811 textclasslist.NumberOfLayout(tclass, arg);
813 // If the entry is obsolete, use the new one instead.
815 string obs = textclasslist.Style(tclass,layout.second).
818 layout = textclasslist.NumberOfLayout(tclass, obs);
821 // see if we found the layout number:
823 string msg = string(N_("Layout ")) + arg + N_(" not known");
825 bv->owner()->getMiniBuffer()->Set(msg);
829 if (cur_layout != layout.second) {
830 cur_layout = layout.second;
831 TEXT(bv)->SetLayout(bv, layout.second);
832 bv->owner()->getToolbar()->combox->select(cpar(bv)->GetLayout()+1);
833 UpdateLocal(bv, CURSOR_PAR, true);
837 case LFUN_PARAGRAPH_SPACING:
838 // This one is absolutely not working. When fiddling with this
839 // it also seems to me that the paragraphs inside the insettext
840 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
842 LyXParagraph * par = TEXT(bv)->cursor.par();
843 Spacing::Space cur_spacing = par->spacing.getSpace();
844 float cur_value = 1.0;
845 if (cur_spacing == Spacing::Other) {
846 cur_value = par->spacing.getValue();
850 std::istringstream istr(arg);
852 istrstream istr(arg.c_str());
856 Spacing::Space new_spacing = cur_spacing;
857 float new_value = cur_value;
859 lyxerr << "Missing argument to `paragraph-spacing'"
861 } else if (tmp == "single") {
862 new_spacing = Spacing::Single;
863 } else if (tmp == "onehalf") {
864 new_spacing = Spacing::Onehalf;
865 } else if (tmp == "double") {
866 new_spacing = Spacing::Double;
867 } else if (tmp == "other") {
868 new_spacing = Spacing::Other;
871 lyxerr << "new_value = " << tmpval << endl;
874 } else if (tmp == "default") {
875 new_spacing = Spacing::Default;
877 lyxerr << _("Unknown spacing argument: ")
880 if (cur_spacing != new_spacing || cur_value != new_value) {
881 par->spacing.set(new_spacing, new_value);
882 //TEXT(bv)->RedoParagraph(owner->view());
883 UpdateLocal(bv, CURSOR_PAR, true);
884 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
890 result = UNDISPATCHED;
893 if (result != FINISHED) {
896 bv->unlockInset(this);
901 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
904 buf->latexParagraphs(os, par, 0, texrow);
905 return texrow.rows();
909 void InsetText::Validate(LaTeXFeatures & features) const
911 LyXParagraph * p = par;
913 p->validate(features);
919 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
921 if (textclasslist.Style(buf->params.textclass,
922 p->GetLayout()).labeltype != LABEL_MANUAL)
925 return p->BeginningOfMainBody();
929 void InsetText::GetCursorPos(BufferView * bv, int & x, int & y) const
936 int InsetText::InsetInInsetY()
938 if (!the_locking_inset)
941 return (inset_y + the_locking_inset->InsetInInsetY());
945 void InsetText::ToggleInsetCursor(BufferView * bv)
947 if (the_locking_inset) {
948 the_locking_inset->ToggleInsetCursor(bv);
952 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
954 int asc = lyxfont::maxAscent(font);
955 int desc = lyxfont::maxDescent(font);
958 bv->hideLockedInsetCursor();
960 bv->showLockedInsetCursor(cx(bv), cy(bv),
962 cursor_visible = !cursor_visible;
966 void InsetText::ShowInsetCursor(BufferView * bv)
968 if (the_locking_inset) {
969 the_locking_inset->ShowInsetCursor(bv);
972 if (!cursor_visible) {
973 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
975 int asc = lyxfont::maxAscent(font);
976 int desc = lyxfont::maxDescent(font);
978 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
979 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
980 cursor_visible = true;
985 void InsetText::HideInsetCursor(BufferView * bv)
987 if (cursor_visible) {
988 bv->hideLockedInsetCursor();
989 cursor_visible = false;
991 if (the_locking_inset)
992 the_locking_inset->HideInsetCursor(bv);
996 UpdatableInset::RESULT
997 InsetText::moveRight(BufferView * bv, bool activate_inset)
999 if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1001 if (activate_inset && checkAndActivateInset(bv, false))
1003 TEXT(bv)->CursorRight(bv);
1004 return DISPATCHED_NOUPDATE;
1008 UpdatableInset::RESULT
1009 InsetText::moveLeft(BufferView * bv, bool activate_inset)
1011 if (!cpar(bv)->previous && (cpos(bv) <= 0))
1013 TEXT(bv)->CursorLeft(bv);
1014 if (activate_inset && checkAndActivateInset(bv, true))
1016 return DISPATCHED_NOUPDATE;
1020 UpdatableInset::RESULT
1021 InsetText::moveUp(BufferView * bv)
1023 if (!crow(bv)->previous())
1025 TEXT(bv)->CursorUp(bv);
1026 return DISPATCHED_NOUPDATE;
1030 UpdatableInset::RESULT
1031 InsetText::moveDown(BufferView * bv)
1033 if (!crow(bv)->next())
1035 TEXT(bv)->CursorDown(bv);
1036 return DISPATCHED_NOUPDATE;
1040 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1042 if (the_locking_inset) {
1043 if (the_locking_inset->InsertInsetAllowed(inset))
1044 return the_locking_inset->InsertInset(bv, inset);
1047 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1048 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1049 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
1050 if (inset->Editable() == Inset::IS_EDITABLE) {
1051 UpdatableInset * i = static_cast<UpdatableInset *>(inset);
1052 i->setOwner(static_cast<UpdatableInset *>(this));
1054 TEXT(bv)->InsertInset(bv, inset);
1055 TEXT(bv)->selection = 0;
1056 UpdateLocal(bv, CURSOR_PAR, true);
1057 static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1062 UpdatableInset * InsetText::GetLockingInset()
1064 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1068 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1072 if (the_locking_inset)
1073 return the_locking_inset->GetFirstLockingInsetOfType(c);
1078 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1080 TEXT(bv)->SetFont(bv, font, toggleall);
1084 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1086 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1089 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1090 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1092 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1094 x = inset->width(bv, font);
1095 y = inset->descent(bv, font);
1099 inset_x = cx(bv) - top_x + drawTextXOffset;
1100 inset_y = cy(bv) + drawTextYOffset;
1101 inset->Edit(bv, x-inset_x, y-inset_y, 0);
1102 if (!the_locking_inset)
1104 UpdateLocal(bv, CURSOR_PAR, false);
1111 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1114 int tmp_x = x - drawTextXOffset;
1115 int tmp_y = y + insetAscent;
1116 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
1123 inset_x = cx(bv) - top_x + drawTextXOffset;
1124 inset_y = cy(bv) + drawTextYOffset;
1125 inset->Edit(bv, x - inset_x, y - inset_y, button);
1126 if (!the_locking_inset)
1128 UpdateLocal(bv, CURSOR_PAR, false);
1135 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1137 int w = UpdatableInset::getMaxWidth(pain, inset);
1140 return w - (2*TEXT_TO_INSET_OFFSET);
1144 void InsetText::SetParagraphData(LyXParagraph *p)
1158 par->SetInsetOwner(this);
1162 np->next = p->Clone();
1163 np->next->previous = np;
1165 np->SetInsetOwner(this);
1171 void InsetText::SetAutoBreakRows(bool flag)
1173 if (flag != autoBreakRows) {
1174 autoBreakRows = flag;
1180 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1182 if (how != drawFrame) {
1185 UpdateLocal(bv, DRAW_FRAME, false);
1190 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1192 if (frame_color != col) {
1195 UpdateLocal(bv, DRAW_FRAME, false);
1200 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1202 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1206 int InsetText::cx(BufferView * bv) const
1208 return TEXT(bv)->cursor.x() + top_x + 1;
1212 int InsetText::cy(BufferView * bv) const
1214 long int y_dummy = 0;
1215 Row * tmprow = TEXT(bv)->GetRowNearY(y_dummy);
1216 return TEXT(bv)->cursor.y() - tmprow->baseline();
1220 int InsetText::cpos(BufferView * bv) const
1222 return TEXT(bv)->cursor.pos();
1226 LyXParagraph * InsetText::cpar(BufferView * bv) const
1228 return TEXT(bv)->cursor.par();
1232 Row * InsetText::crow(BufferView * bv) const
1234 return TEXT(bv)->cursor.row();
1238 LyXText * InsetText::getLyXText(BufferView * bv) const
1240 if (cache.find(bv) != cache.end())
1242 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1245 if (the_locking_inset) {
1246 lt->SetCursor(bv, inset_par, inset_pos);
1252 void InsetText::deleteLyXText(BufferView * bv) const
1255 /// then remove all LyXText in text-insets
1256 LyXParagraph * p = par;
1257 for(;p;p = p->next) {
1258 p->deleteInsetsLyXText(bv);