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);
268 top_baseline = baseline;
269 top_y = baseline - ascent(bv, f);
270 last_width = width(bv, f);
271 last_height = ascent(bv, f) + descent(bv, f);
273 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
274 inset_x = cx(bv) - top_x + drawTextXOffset;
275 inset_y = cy(bv) + drawTextYOffset;
277 if (!cleared && (need_update == CURSOR)) {
282 x += TEXT_TO_INSET_OFFSET; // place for border
284 Row * row = TEXT(bv)->GetRowNearY(y);
285 y += baseline - row->ascent_of_text() + 1;
286 if (cleared || !locked || (need_update == FULL)) {
288 TEXT(bv)->GetVisibleRow(bv, y, x, row, y, cleared);
292 } else if (need_update == SELECTION) {
293 bv->screen()->ToggleToggle(getLyXText(bv), y, x);
296 bv->screen()->Update(TEXT(bv), y, x);
299 TEXT(bv)->refresh_y = 0;
300 TEXT(bv)->status = LyXText::UNCHANGED;
301 if ((drawFrame == ALWAYS) || ((drawFrame == LOCKED) && locked)) {
302 pain.rectangle(top_x, baseline - insetAscent, insetWidth,
303 insetAscent + insetDescent, frame_color);
304 } else if (need_update == CLEAR_FRAME) {
305 pain.rectangle(top_x, baseline - insetAscent, insetWidth,
306 insetAscent + insetDescent,
309 x += insetWidth - TEXT_TO_INSET_OFFSET;
310 if (need_update != INIT)
315 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
317 if (reinit) { // && (need_update != CURSOR)) {
321 owner()->update(bv, font, true);
324 if (the_locking_inset)
325 the_locking_inset->update(bv, font, reinit);
326 if (need_update == INIT) {
329 // if (!owner() && bv->text)
330 // bv->text->UpdateInset(bv, this);
332 int oldw = insetWidth;
334 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
335 // max(textWidth(bv->painter()),
336 // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
337 // (2 * TEXT_TO_INSET_OFFSET);
339 insetWidth = textWidth(bv->painter());
341 insetWidth = static_cast<int>(TEXT(bv)->width);
343 if (oldw != insetWidth) {
344 // printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
345 // textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
350 owner()->update(bv, font, reinit);
353 update(bv, font, reinit);
357 update(bv, font, reinit);
359 UpdateLocal(bv, INIT, false);
364 if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
367 TEXT(bv)->UpdateInset(bv, the_locking_inset);
370 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
374 Row * row = TEXT(bv)->GetRowNearY(y_temp);
375 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
376 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
377 TEXT_TO_INSET_OFFSET;
381 void InsetText::UpdateLocal(BufferView * bv, UpdateCodes what, bool mark_dirty)
383 TEXT(bv)->FullRebreak(bv);
384 if (need_update != INIT) {
385 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
387 else if (!the_locking_inset || (what != CURSOR))
390 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED))
391 bv->updateInset(this, mark_dirty);
392 if (old_par != cpar(bv)) {
393 bv->owner()->getToolbar()->combox->select(cpar(bv)->GetLayout()+1);
399 char const * InsetText::EditMessage() const
401 return _("Opened Text Inset");
405 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
407 // par->SetInsetOwner(this);
408 UpdatableInset::Edit(bv, x, y, button);
410 if (!bv->lockInset(this)) {
411 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
415 the_locking_inset = 0;
416 inset_pos = inset_x = inset_y = 0;
418 if (!checkAndActivateInset(bv, x, y, button))
419 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
420 y+TEXT(bv)->first+insetAscent);
421 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
422 bv->text->FinishUndo();
423 UpdateLocal(bv, FULL, false);
427 void InsetText::InsetUnlock(BufferView * bv)
429 if (the_locking_inset) {
430 the_locking_inset->InsetUnlock(bv);
431 the_locking_inset = 0;
434 no_selection = false;
436 TEXT(bv)->selection = 0;
437 UpdateLocal(bv, CLEAR_FRAME, false);
438 bv->owner()->getToolbar()->combox->select(bv->text->cursor.par()->GetLayout()+1);
442 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
444 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
447 if (inset == cpar(bv)->GetInset(cpos(bv))) {
448 lyxerr[Debug::INSETS] << "OK" << endl;
449 the_locking_inset = inset;
450 inset_x = cx(bv) - top_x + drawTextXOffset;
451 inset_y = cy(bv) + drawTextYOffset;
452 inset_pos = cpos(bv);
453 inset_par = cpar(bv);
454 TEXT(bv)->UpdateInset(bv, the_locking_inset);
456 } else if (the_locking_inset && (the_locking_inset == inset)) {
457 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
458 lyxerr[Debug::INSETS] << "OK" << endl;
459 inset_x = cx(bv) - top_x + drawTextXOffset;
460 inset_y = cy(bv) + drawTextYOffset;
462 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
464 } else if (the_locking_inset) {
465 lyxerr[Debug::INSETS] << "MAYBE" << endl;
466 return the_locking_inset->LockInsetInInset(bv, inset);
468 lyxerr[Debug::INSETS] << "NOT OK" << endl;
473 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
476 if (!the_locking_inset)
478 if (the_locking_inset == inset) {
479 the_locking_inset->InsetUnlock(bv);
480 TEXT(bv)->UpdateInset(bv, inset);
481 the_locking_inset = 0;
483 moveRight(bv, false);
484 UpdateLocal(bv, CURSOR_PAR, false);
487 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
491 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
493 if (!the_locking_inset)
495 if (the_locking_inset != inset) {
496 TEXT(bv)->UpdateInset(bv, the_locking_inset);
497 need_update = CURSOR_PAR;
498 return the_locking_inset->UpdateInsetInInset(bv, inset);
500 // UpdateLocal(bv, FULL, false);
501 if (TEXT(bv)->UpdateInset(bv, inset))
502 UpdateLocal(bv, CURSOR_PAR, false);
503 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
504 inset_x = cx(bv) - top_x + drawTextXOffset;
505 inset_y = cy(bv) + drawTextYOffset;
511 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
513 if (TEXT(bv)->selection) {
514 TEXT(bv)->selection = 0;
515 UpdateLocal(bv, FULL, false);
517 no_selection = false;
519 int tmp_x = x - drawTextXOffset;
520 // int tmp_y = y + TEXT(bv)->first + insetAscent;
521 int tmp_y = y + insetAscent;
522 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
524 if (the_locking_inset) {
525 if (the_locking_inset == inset) {
526 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
529 // otherwise unlock the_locking_inset and lock the new inset
530 the_locking_inset->InsetUnlock(bv);
531 inset_x = cx(bv) - top_x + drawTextXOffset;
532 inset_y = cy(bv) + drawTextYOffset;
533 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
534 inset->Edit(bv, x - inset_x, y - inset_y, button);
535 if (the_locking_inset) {
536 UpdateLocal(bv, CURSOR_PAR, false);
540 // otherwise only unlock the_locking_inset
541 the_locking_inset->InsetUnlock(bv);
542 the_locking_inset = 0;
544 if (bv->the_locking_inset) {
545 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
546 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
547 inset_x = cx(bv) - top_x + drawTextXOffset;
548 inset_y = cy(bv) + drawTextYOffset;
549 inset_pos = cpos(bv);
550 inset_par = cpar(bv);
551 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
552 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
553 if (the_locking_inset) {
554 UpdateLocal(bv, CURSOR_PAR, false);
560 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
561 y+TEXT(bv)->first+insetAscent);
565 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
567 UpdatableInset * inset = 0;
569 if (the_locking_inset) {
570 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
572 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
573 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
574 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
575 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
577 inset_x = cx(bv) - top_x + drawTextXOffset;
578 inset_y = cy(bv) + drawTextYOffset;
579 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
580 inset->Edit(bv, x - inset_x, y - inset_y, button);
582 UpdateLocal(bv, CURSOR_PAR, false);
585 no_selection = false;
589 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
591 if (the_locking_inset) {
592 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
596 #warning REDO this (Jug)
598 // LyXCursor old = selection_end_cursor;
600 // setPos(bv->painter(), x, y);
601 // selection_end_cursor = cursor;
602 // if (old != selection_end_cursor)
603 // UpdateLocal(bv, false, false);
606 no_selection = false;
610 void InsetText::InsetKeyPress(XKeyEvent * xke)
612 if (the_locking_inset) {
613 the_locking_inset->InsetKeyPress(xke);
619 UpdatableInset::RESULT
620 InsetText::LocalDispatch(BufferView * bv,
621 int action, string const & arg)
623 no_selection = false;
624 UpdatableInset::RESULT
625 result= UpdatableInset::LocalDispatch(bv, action, arg);
626 if (result != UNDISPATCHED) {
627 // resetPos(bv->painter());
632 if ((action < 0) && arg.empty())
635 if (the_locking_inset) {
636 result = the_locking_inset->LocalDispatch(bv, action, arg);
637 if (result == DISPATCHED_NOUPDATE)
639 else if (result == DISPATCHED) {
640 UpdateLocal(bv, CURSOR_PAR, false);
642 } else if (result == FINISHED) {
646 moveRight(bv, false);
652 the_locking_inset = 0;
660 if (bv->buffer()->isReadonly()) {
662 // setErrorMessage(N_("Document is read only"));
666 /* Automatically delete the currently selected
667 * text and replace it with what is being
668 * typed in now. Depends on lyxrc settings
669 * "auto_region_delete", which defaults to
672 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
673 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
674 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
675 if (lyxrc.auto_region_delete) {
676 if (TEXT(bv)->selection){
677 TEXT(bv)->CutSelection(bv, false);
680 TEXT(bv)->ClearSelection();
681 for (string::size_type i = 0; i < arg.length(); ++i) {
682 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
685 UpdateLocal(bv, CURSOR_PAR, true);
687 // --- Cursor Movements ---------------------------------------------
689 bv->text->FinishUndo();
690 moveRight(bv, false);
691 TEXT(bv)->SetSelection();
692 UpdateLocal(bv, SELECTION, false);
695 bv->text->FinishUndo();
696 result = moveRight(bv);
697 TEXT(bv)->selection = 0;
698 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
699 UpdateLocal(bv, CURSOR, false);
702 bv->text->FinishUndo();
704 TEXT(bv)->SetSelection();
705 UpdateLocal(bv, SELECTION, false);
708 bv->text->FinishUndo();
709 result= moveLeft(bv);
710 TEXT(bv)->selection = 0;
711 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
712 UpdateLocal(bv, CURSOR, false);
715 bv->text->FinishUndo();
717 TEXT(bv)->SetSelection();
718 UpdateLocal(bv, SELECTION, false);
721 bv->text->FinishUndo();
722 result = moveDown(bv);
723 TEXT(bv)->selection = 0;
724 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
725 UpdateLocal(bv, CURSOR, false);
728 bv->text->FinishUndo();
730 TEXT(bv)->SetSelection();
731 UpdateLocal(bv, SELECTION, false);
734 bv->text->FinishUndo();
736 TEXT(bv)->selection = 0;
737 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
738 UpdateLocal(bv, CURSOR, false);
741 bv->text->FinishUndo();
742 TEXT(bv)->CursorHome(bv);
743 TEXT(bv)->selection = 0;
744 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
745 UpdateLocal(bv, CURSOR, false);
748 TEXT(bv)->CursorEnd(bv);
749 TEXT(bv)->selection = 0;
750 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
751 UpdateLocal(bv, CURSOR, false);
754 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
755 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
756 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
757 TEXT(bv)->Backspace(bv);
758 UpdateLocal(bv, CURSOR_PAR, true);
761 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
762 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
763 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
764 TEXT(bv)->Delete(bv);
765 UpdateLocal(bv, CURSOR_PAR, true);
768 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
769 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
770 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
771 TEXT(bv)->CutSelection(bv);
772 UpdateLocal(bv, CURSOR_PAR, true);
775 bv->text->FinishUndo();
776 TEXT(bv)->CopySelection(bv);
777 UpdateLocal(bv, CURSOR_PAR, false);
780 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
781 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
782 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
783 TEXT(bv)->PasteSelection(bv);
784 UpdateLocal(bv, CURSOR_PAR, true);
786 case LFUN_BREAKPARAGRAPH:
789 TEXT(bv)->BreakParagraph(bv, 0);
790 UpdateLocal(bv, FULL, true);
795 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
796 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
797 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
798 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
799 UpdateLocal(bv, CURSOR_PAR, true);
803 static LyXTextClass::size_type cur_layout = cpar(bv)->layout;
805 // Derive layout number from given argument (string)
806 // and current buffer's textclass (number). */
807 LyXTextClassList::ClassList::size_type tclass =
808 bv->buffer()->params.textclass;
809 std::pair <bool, LyXTextClass::size_type> layout =
810 textclasslist.NumberOfLayout(tclass, arg);
812 // If the entry is obsolete, use the new one instead.
814 string obs = textclasslist.Style(tclass,layout.second).
817 layout = textclasslist.NumberOfLayout(tclass, obs);
820 // see if we found the layout number:
822 string msg = string(N_("Layout ")) + arg + N_(" not known");
824 bv->owner()->getMiniBuffer()->Set(msg);
828 if (cur_layout != layout.second) {
829 cur_layout = layout.second;
830 TEXT(bv)->SetLayout(bv, layout.second);
831 bv->owner()->getToolbar()->combox->select(cpar(bv)->GetLayout()+1);
832 UpdateLocal(bv, CURSOR_PAR, true);
836 case LFUN_PARAGRAPH_SPACING:
837 // This one is absolutely not working. When fiddling with this
838 // it also seems to me that the paragraphs inside the insettext
839 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
841 LyXParagraph * par = TEXT(bv)->cursor.par();
842 Spacing::Space cur_spacing = par->spacing.getSpace();
843 float cur_value = 1.0;
844 if (cur_spacing == Spacing::Other) {
845 cur_value = par->spacing.getValue();
849 std::istringstream istr(arg);
851 istrstream istr(arg.c_str());
855 Spacing::Space new_spacing = cur_spacing;
856 float new_value = cur_value;
858 lyxerr << "Missing argument to `paragraph-spacing'"
860 } else if (tmp == "single") {
861 new_spacing = Spacing::Single;
862 } else if (tmp == "onehalf") {
863 new_spacing = Spacing::Onehalf;
864 } else if (tmp == "double") {
865 new_spacing = Spacing::Double;
866 } else if (tmp == "other") {
867 new_spacing = Spacing::Other;
870 lyxerr << "new_value = " << tmpval << endl;
873 } else if (tmp == "default") {
874 new_spacing = Spacing::Default;
876 lyxerr << _("Unknown spacing argument: ")
879 if (cur_spacing != new_spacing || cur_value != new_value) {
880 par->spacing.set(new_spacing, new_value);
881 //TEXT(bv)->RedoParagraph(owner->view());
882 UpdateLocal(bv, CURSOR_PAR, true);
883 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
889 result = UNDISPATCHED;
892 if (result != FINISHED) {
895 bv->unlockInset(this);
900 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
903 buf->latexParagraphs(os, par, 0, texrow);
904 return texrow.rows();
908 void InsetText::Validate(LaTeXFeatures & features) const
910 LyXParagraph * p = par;
912 p->validate(features);
918 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
920 if (textclasslist.Style(buf->params.textclass,
921 p->GetLayout()).labeltype != LABEL_MANUAL)
924 return p->BeginningOfMainBody();
928 void InsetText::GetCursorPos(BufferView * bv, int & x, int & y) const
935 int InsetText::InsetInInsetY()
937 if (!the_locking_inset)
940 return (inset_y + the_locking_inset->InsetInInsetY());
944 void InsetText::ToggleInsetCursor(BufferView * bv)
946 if (the_locking_inset) {
947 the_locking_inset->ToggleInsetCursor(bv);
951 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
953 int asc = lyxfont::maxAscent(font);
954 int desc = lyxfont::maxDescent(font);
957 bv->hideLockedInsetCursor();
959 bv->showLockedInsetCursor(cx(bv), cy(bv),
961 cursor_visible = !cursor_visible;
965 void InsetText::ShowInsetCursor(BufferView * bv)
967 if (the_locking_inset) {
968 the_locking_inset->ShowInsetCursor(bv);
971 if (!cursor_visible) {
972 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
974 int asc = lyxfont::maxAscent(font);
975 int desc = lyxfont::maxDescent(font);
977 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
978 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
979 cursor_visible = true;
984 void InsetText::HideInsetCursor(BufferView * bv)
986 if (cursor_visible) {
987 bv->hideLockedInsetCursor();
988 cursor_visible = false;
990 if (the_locking_inset)
991 the_locking_inset->HideInsetCursor(bv);
995 UpdatableInset::RESULT
996 InsetText::moveRight(BufferView * bv, bool activate_inset)
998 if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1000 if (activate_inset && checkAndActivateInset(bv, false))
1002 TEXT(bv)->CursorRight(bv);
1003 return DISPATCHED_NOUPDATE;
1007 UpdatableInset::RESULT
1008 InsetText::moveLeft(BufferView * bv, bool activate_inset)
1010 if (!cpar(bv)->previous && (cpos(bv) <= 0))
1012 TEXT(bv)->CursorLeft(bv);
1013 if (activate_inset && checkAndActivateInset(bv, true))
1015 return DISPATCHED_NOUPDATE;
1019 UpdatableInset::RESULT
1020 InsetText::moveUp(BufferView * bv)
1022 if (!crow(bv)->previous())
1024 TEXT(bv)->CursorUp(bv);
1025 return DISPATCHED_NOUPDATE;
1029 UpdatableInset::RESULT
1030 InsetText::moveDown(BufferView * bv)
1032 if (!crow(bv)->next())
1034 TEXT(bv)->CursorDown(bv);
1035 return DISPATCHED_NOUPDATE;
1039 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1041 if (the_locking_inset) {
1042 if (the_locking_inset->InsertInsetAllowed(inset))
1043 return the_locking_inset->InsertInset(bv, inset);
1046 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1047 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1048 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
1049 if (inset->Editable() == Inset::IS_EDITABLE) {
1050 UpdatableInset * i = static_cast<UpdatableInset *>(inset);
1051 i->setOwner(static_cast<UpdatableInset *>(this));
1053 TEXT(bv)->InsertInset(bv, inset);
1054 TEXT(bv)->selection = 0;
1055 UpdateLocal(bv, CURSOR_PAR, true);
1056 static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1061 UpdatableInset * InsetText::GetLockingInset()
1063 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1067 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1071 if (the_locking_inset)
1072 return the_locking_inset->GetFirstLockingInsetOfType(c);
1077 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1079 TEXT(bv)->SetFont(bv, font, toggleall);
1083 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1085 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1088 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1089 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1091 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1093 x = inset->width(bv, font);
1094 y = inset->descent(bv, font);
1098 inset_x = cx(bv) - top_x + drawTextXOffset;
1099 inset_y = cy(bv) + drawTextYOffset;
1100 inset->Edit(bv, x-inset_x, y-inset_y, 0);
1101 if (!the_locking_inset)
1103 UpdateLocal(bv, CURSOR_PAR, false);
1110 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1113 int tmp_x = x - drawTextXOffset;
1114 int tmp_y = y + insetAscent;
1115 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
1122 inset_x = cx(bv) - top_x + drawTextXOffset;
1123 inset_y = cy(bv) + drawTextYOffset;
1124 inset->Edit(bv, x - inset_x, y - inset_y, button);
1125 if (!the_locking_inset)
1127 UpdateLocal(bv, CURSOR_PAR, false);
1134 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1136 int w = UpdatableInset::getMaxWidth(pain, inset);
1139 return w - (2*TEXT_TO_INSET_OFFSET);
1143 void InsetText::SetParagraphData(LyXParagraph *p)
1157 par->SetInsetOwner(this);
1161 np->next = p->Clone();
1162 np->next->previous = np;
1164 np->SetInsetOwner(this);
1170 void InsetText::SetAutoBreakRows(bool flag)
1172 if (flag != autoBreakRows) {
1173 autoBreakRows = flag;
1179 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1181 if (how != drawFrame) {
1184 UpdateLocal(bv, DRAW_FRAME, false);
1189 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1191 if (frame_color != col) {
1194 UpdateLocal(bv, DRAW_FRAME, false);
1199 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1201 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1205 int InsetText::cx(BufferView * bv) const
1207 return TEXT(bv)->cursor.x() + top_x + 1;
1211 int InsetText::cy(BufferView * bv) const
1213 long int y_dummy = 0;
1214 Row * tmprow = TEXT(bv)->GetRowNearY(y_dummy);
1215 return TEXT(bv)->cursor.y() - tmprow->baseline();
1219 int InsetText::cpos(BufferView * bv) const
1221 return TEXT(bv)->cursor.pos();
1225 LyXParagraph * InsetText::cpar(BufferView * bv) const
1227 return TEXT(bv)->cursor.par();
1231 Row * InsetText::crow(BufferView * bv) const
1233 return TEXT(bv)->cursor.row();
1237 LyXText * InsetText::getLyXText(BufferView * bv) const
1239 if (cache.find(bv) != cache.end())
1241 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1244 if (the_locking_inset) {
1245 lt->SetCursor(bv, inset_par, inset_pos);
1251 void InsetText::deleteLyXText(BufferView * bv) const
1254 /// then remove all LyXText in text-insets
1255 LyXParagraph * p = par;
1256 for(;p;p = p->next) {
1257 p->deleteInsetsLyXText(bv);