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(Painter &, LyXFont const &) const
195 int InsetText::descent(Painter &, LyXFont const &) const
201 int InsetText::width(Painter &, LyXFont const &) const
207 int InsetText::textWidth(Painter & pain) const
209 int w = getMaxWidth(pain, this);
211 // printf("WW1: %d\n",w);
216 w = w - top_x; // + owner()->x();
217 // printf("WW2: %d\n",w);
218 return w; // - top_x + owner()->x();
221 w -= (2 * TEXT_TO_INSET_OFFSET);
222 // printf("WW2: %d\n",w);
223 return w - top_x; // - top_x - (2 * TEXT_TO_INSET_OFFSET);
227 void InsetText::draw(BufferView * bv, LyXFont const & f,
228 int baseline, float & x, bool cleared) const
230 Painter & pain = bv->painter();
232 if (!cleared && ((need_update==FULL) || (top_x!=int(x)) ||
233 (top_baseline!=baseline))) {
235 int h = insetAscent + insetDescent;
236 int ty = baseline - insetAscent;
240 if ((ty + h) > pain.paperHeight())
241 h = pain.paperHeight();
242 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
243 w = pain.paperWidth();
244 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
249 if (!cleared && (need_update == NONE))
253 UpdatableInset::draw(bv, f, baseline, x, cleared);
255 top_baseline = baseline;
257 top_y = baseline - ascent(pain, f);
258 last_width = width(pain, f);
259 last_height = ascent(pain, f) + descent(pain, f);
261 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
262 inset_x = cx(bv) - top_x + drawTextXOffset;
263 inset_y = cy(bv) + drawTextYOffset;
265 if (!cleared && (need_update == CURSOR)) {
270 x += TEXT_TO_INSET_OFFSET; // place for border
272 Row * row = TEXT(bv)->GetRowNearY(y);
273 y += baseline - row->ascent_of_text() + 1;
274 if (cleared || !locked || (need_update == FULL)) {
276 TEXT(bv)->GetVisibleRow(bv, y, x, row, y, cleared);
280 } else if (need_update == SELECTION) {
281 bv->screen()->ToggleToggle(getLyXText(bv), y, x);
284 bv->screen()->Update(TEXT(bv), y, x);
287 TEXT(bv)->refresh_y = 0;
288 TEXT(bv)->status = LyXText::UNCHANGED;
289 if ((drawFrame == ALWAYS) || ((drawFrame == LOCKED) && locked)) {
290 pain.rectangle(top_x, baseline - insetAscent, insetWidth,
291 insetAscent + insetDescent, frame_color);
292 } else if (need_update == CLEAR_FRAME) {
293 pain.rectangle(top_x, baseline - insetAscent, insetWidth,
294 insetAscent + insetDescent,
297 x += insetWidth - TEXT_TO_INSET_OFFSET;
302 void InsetText::update(BufferView * bv, LyXFont const & font, bool dodraw)
304 if (the_locking_inset)
305 the_locking_inset->update(bv, font, dodraw);
306 if (need_update == INIT) {
310 int oldw = insetWidth;
312 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
313 // max(textWidth(bv->painter()),
314 // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
315 // (2 * TEXT_TO_INSET_OFFSET);
317 insetWidth = textWidth(bv->painter());
319 insetWidth = static_cast<int>(TEXT(bv)->width);
321 if (oldw != insetWidth) {
322 printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
323 textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
328 owner()->update(bv, font, dodraw);
331 update(bv, font, dodraw);
335 update(bv, font, dodraw);
337 UpdateLocal(bv, INIT, false);
342 if (dodraw && (need_update != CURSOR))
345 TEXT(bv)->FullRebreak(bv);
347 if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
350 TEXT(bv)->UpdateInset(bv, the_locking_inset);
353 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
357 Row * row = TEXT(bv)->GetRowNearY(y_temp);
358 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
359 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
360 TEXT_TO_INSET_OFFSET;
364 void InsetText::UpdateLocal(BufferView * bv, UpdateCodes what, bool mark_dirty)
366 TEXT(bv)->FullRebreak(bv);
367 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
369 else if (!the_locking_inset || (what != CURSOR))
371 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED))
372 bv->updateInset(this, mark_dirty);
373 if (old_par != cpar(bv)) {
374 bv->owner()->getToolbar()->combox->select(cpar(bv)->GetLayout()+1);
380 char const * InsetText::EditMessage() const
382 return _("Opened Text Inset");
386 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
388 // par->SetInsetOwner(this);
389 UpdatableInset::Edit(bv, x, y, button);
391 if (!bv->lockInset(this)) {
392 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
396 the_locking_inset = 0;
397 inset_pos = inset_x = inset_y = 0;
399 if (!checkAndActivateInset(bv, x, y, button))
400 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset, y);
401 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
402 bv->text->FinishUndo();
403 UpdateLocal(bv, FULL, false);
407 void InsetText::InsetUnlock(BufferView * bv)
409 if (the_locking_inset) {
410 the_locking_inset->InsetUnlock(bv);
411 the_locking_inset = 0;
414 no_selection = false;
416 TEXT(bv)->selection = 0;
417 UpdateLocal(bv, CLEAR_FRAME, false);
418 bv->owner()->getToolbar()->combox->select(bv->text->cursor.par()->GetLayout()+1);
422 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
424 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
427 if (inset == cpar(bv)->GetInset(cpos(bv))) {
428 lyxerr[Debug::INSETS] << "OK" << endl;
429 the_locking_inset = inset;
430 inset_x = cx(bv) - top_x + drawTextXOffset;
431 inset_y = cy(bv) + drawTextYOffset;
432 inset_pos = cpos(bv);
433 inset_par = cpar(bv);
434 TEXT(bv)->UpdateInset(bv, the_locking_inset);
436 } else if (the_locking_inset && (the_locking_inset == inset)) {
437 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
438 lyxerr[Debug::INSETS] << "OK" << endl;
439 inset_x = cx(bv) - top_x + drawTextXOffset;
440 inset_y = cy(bv) + drawTextYOffset;
442 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
444 } else if (the_locking_inset) {
445 lyxerr[Debug::INSETS] << "MAYBE" << endl;
446 return the_locking_inset->LockInsetInInset(bv, inset);
448 lyxerr[Debug::INSETS] << "NOT OK" << endl;
453 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
456 if (!the_locking_inset)
458 if (the_locking_inset == inset) {
459 the_locking_inset->InsetUnlock(bv);
460 TEXT(bv)->UpdateInset(bv, inset);
461 the_locking_inset = 0;
463 moveRight(bv, false);
464 UpdateLocal(bv, CURSOR_PAR, false);
467 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
471 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
473 if (!the_locking_inset)
475 if (the_locking_inset != inset) {
476 TEXT(bv)->UpdateInset(bv, the_locking_inset);
477 need_update = CURSOR_PAR;
478 return the_locking_inset->UpdateInsetInInset(bv, inset);
480 // UpdateLocal(bv, FULL, false);
481 if (TEXT(bv)->UpdateInset(bv, inset))
482 UpdateLocal(bv, CURSOR_PAR, false);
483 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
484 inset_x = cx(bv) - top_x + drawTextXOffset;
485 inset_y = cy(bv) + drawTextYOffset;
491 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
493 if (TEXT(bv)->selection) {
494 TEXT(bv)->selection = 0;
495 UpdateLocal(bv, FULL, false);
497 no_selection = false;
499 int tmp_x = x - drawTextXOffset;
500 // int tmp_y = y + TEXT(bv)->first + insetAscent;
501 int tmp_y = y + insetAscent;
502 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
504 if (the_locking_inset) {
505 if (the_locking_inset == inset) {
506 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
509 // otherwise unlock the_locking_inset and lock the new inset
510 the_locking_inset->InsetUnlock(bv);
511 inset_x = cx(bv) - top_x + drawTextXOffset;
512 inset_y = cy(bv) + drawTextYOffset;
513 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
514 inset->Edit(bv, x - inset_x, y - inset_y, button);
515 if (the_locking_inset)
516 UpdateLocal(bv, CURSOR_PAR, false);
519 // otherwise only unlock the_locking_inset
520 the_locking_inset->InsetUnlock(bv);
521 the_locking_inset = 0;
523 if (bv->the_locking_inset) {
524 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
525 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
526 inset_x = cx(bv) - top_x + drawTextXOffset;
527 inset_y = cy(bv) + drawTextYOffset;
528 inset_pos = cpos(bv);
529 inset_par = cpar(bv);
530 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
531 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
532 if (the_locking_inset)
533 UpdateLocal(bv, CURSOR_PAR, false);
538 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
539 y+TEXT(bv)->first+insetAscent);
543 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
545 UpdatableInset * inset = 0;
547 if (the_locking_inset) {
548 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
550 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
551 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
552 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
553 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
555 inset_x = cx(bv) - top_x + drawTextXOffset;
556 inset_y = cy(bv) + drawTextYOffset;
557 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
558 inset->Edit(bv, x - inset_x, y - inset_y, button);
562 no_selection = false;
566 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
568 if (the_locking_inset) {
569 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
573 #warning REDO this (Jug)
575 // LyXCursor old = selection_end_cursor;
577 // setPos(bv->painter(), x, y);
578 // selection_end_cursor = cursor;
579 // if (old != selection_end_cursor)
580 // UpdateLocal(bv, false, false);
583 no_selection = false;
587 void InsetText::InsetKeyPress(XKeyEvent * xke)
589 if (the_locking_inset) {
590 the_locking_inset->InsetKeyPress(xke);
596 UpdatableInset::RESULT
597 InsetText::LocalDispatch(BufferView * bv,
598 int action, string const & arg)
600 no_selection = false;
601 UpdatableInset::RESULT
602 result= UpdatableInset::LocalDispatch(bv, action, arg);
603 if (result != UNDISPATCHED) {
604 // resetPos(bv->painter());
609 if ((action < 0) && arg.empty())
612 if (the_locking_inset) {
613 result = the_locking_inset->LocalDispatch(bv, action, arg);
614 if (result == DISPATCHED_NOUPDATE)
616 else if (result == DISPATCHED) {
617 UpdateLocal(bv, CURSOR_PAR, false);
619 } else if (result == FINISHED) {
623 moveRight(bv, false);
629 the_locking_inset = 0;
637 if (bv->buffer()->isReadonly()) {
639 // setErrorMessage(N_("Document is read only"));
643 /* Automatically delete the currently selected
644 * text and replace it with what is being
645 * typed in now. Depends on lyxrc settings
646 * "auto_region_delete", which defaults to
649 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
650 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
651 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
652 if (lyxrc.auto_region_delete) {
653 if (TEXT(bv)->selection){
654 TEXT(bv)->CutSelection(bv, false);
657 TEXT(bv)->ClearSelection();
658 for (string::size_type i = 0; i < arg.length(); ++i) {
659 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
662 UpdateLocal(bv, CURSOR_PAR, true);
664 // --- Cursor Movements ---------------------------------------------
666 bv->text->FinishUndo();
667 moveRight(bv, false);
668 TEXT(bv)->SetSelection();
669 UpdateLocal(bv, SELECTION, false);
672 bv->text->FinishUndo();
673 result = moveRight(bv);
674 TEXT(bv)->selection = 0;
675 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
676 UpdateLocal(bv, CURSOR, false);
679 bv->text->FinishUndo();
681 TEXT(bv)->SetSelection();
682 UpdateLocal(bv, SELECTION, false);
685 bv->text->FinishUndo();
686 result= moveLeft(bv);
687 TEXT(bv)->selection = 0;
688 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
689 UpdateLocal(bv, CURSOR, false);
692 bv->text->FinishUndo();
694 TEXT(bv)->SetSelection();
695 UpdateLocal(bv, SELECTION, false);
698 bv->text->FinishUndo();
699 result = moveDown(bv);
700 TEXT(bv)->selection = 0;
701 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
702 UpdateLocal(bv, CURSOR, false);
705 bv->text->FinishUndo();
707 TEXT(bv)->SetSelection();
708 UpdateLocal(bv, SELECTION, false);
711 bv->text->FinishUndo();
713 TEXT(bv)->selection = 0;
714 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
715 UpdateLocal(bv, CURSOR, false);
718 bv->text->FinishUndo();
719 TEXT(bv)->CursorHome(bv);
720 TEXT(bv)->selection = 0;
721 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
722 UpdateLocal(bv, CURSOR, false);
725 TEXT(bv)->CursorEnd(bv);
726 TEXT(bv)->selection = 0;
727 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
728 UpdateLocal(bv, CURSOR, false);
731 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
732 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
733 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
734 TEXT(bv)->Backspace(bv);
735 UpdateLocal(bv, CURSOR_PAR, true);
738 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
739 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
740 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
741 TEXT(bv)->Delete(bv);
742 UpdateLocal(bv, CURSOR_PAR, true);
745 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
746 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
747 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
748 TEXT(bv)->CutSelection(bv);
749 UpdateLocal(bv, CURSOR_PAR, true);
752 bv->text->FinishUndo();
753 TEXT(bv)->CopySelection(bv);
754 UpdateLocal(bv, CURSOR_PAR, false);
757 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
758 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
759 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
760 TEXT(bv)->PasteSelection(bv);
761 UpdateLocal(bv, CURSOR_PAR, true);
763 case LFUN_BREAKPARAGRAPH:
766 TEXT(bv)->BreakParagraph(bv, 0);
767 UpdateLocal(bv, FULL, true);
772 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
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)->InsertChar(bv, LyXParagraph::META_NEWLINE);
776 UpdateLocal(bv, CURSOR_PAR, true);
780 static LyXTextClass::size_type cur_layout = cpar(bv)->layout;
782 // Derive layout number from given argument (string)
783 // and current buffer's textclass (number). */
784 LyXTextClassList::ClassList::size_type tclass =
785 bv->buffer()->params.textclass;
786 std::pair <bool, LyXTextClass::size_type> layout =
787 textclasslist.NumberOfLayout(tclass, arg);
789 // If the entry is obsolete, use the new one instead.
791 string obs = textclasslist.Style(tclass,layout.second).
794 layout = textclasslist.NumberOfLayout(tclass, obs);
797 // see if we found the layout number:
799 string msg = string(N_("Layout ")) + arg + N_(" not known");
801 bv->owner()->getMiniBuffer()->Set(msg);
805 if (cur_layout != layout.second) {
806 cur_layout = layout.second;
807 TEXT(bv)->SetLayout(bv, layout.second);
808 bv->owner()->getToolbar()->combox->select(cpar(bv)->GetLayout()+1);
809 UpdateLocal(bv, CURSOR_PAR, true);
813 case LFUN_PARAGRAPH_SPACING:
814 // This one is absolutely not working. When fiddling with this
815 // it also seems to me that the paragraphs inside the insettext
816 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
818 LyXParagraph * par = TEXT(bv)->cursor.par();
819 Spacing::Space cur_spacing = par->spacing.getSpace();
820 float cur_value = 1.0;
821 if (cur_spacing == Spacing::Other) {
822 cur_value = par->spacing.getValue();
826 std::istringstream istr(arg);
828 istrstream istr(arg.c_str());
832 Spacing::Space new_spacing = cur_spacing;
833 float new_value = cur_value;
835 lyxerr << "Missing argument to `paragraph-spacing'"
837 } else if (tmp == "single") {
838 new_spacing = Spacing::Single;
839 } else if (tmp == "onehalf") {
840 new_spacing = Spacing::Onehalf;
841 } else if (tmp == "double") {
842 new_spacing = Spacing::Double;
843 } else if (tmp == "other") {
844 new_spacing = Spacing::Other;
847 lyxerr << "new_value = " << tmpval << endl;
850 } else if (tmp == "default") {
851 new_spacing = Spacing::Default;
853 lyxerr << _("Unknown spacing argument: ")
856 if (cur_spacing != new_spacing || cur_value != new_value) {
857 par->spacing.set(new_spacing, new_value);
858 //TEXT(bv)->RedoParagraph(owner->view());
859 UpdateLocal(bv, CURSOR_PAR, true);
860 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
866 result = UNDISPATCHED;
869 if (result != FINISHED) {
872 bv->unlockInset(this);
877 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
880 buf->latexParagraphs(os, par, 0, texrow);
881 return texrow.rows();
885 void InsetText::Validate(LaTeXFeatures & features) const
887 LyXParagraph * p = par;
889 p->validate(features);
895 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
897 if (textclasslist.Style(buf->params.textclass,
898 p->GetLayout()).labeltype != LABEL_MANUAL)
901 return p->BeginningOfMainBody();
905 void InsetText::GetCursorPos(BufferView * bv, int & x, int & y) const
912 int InsetText::InsetInInsetY()
914 if (!the_locking_inset)
917 return (inset_y + the_locking_inset->InsetInInsetY());
921 void InsetText::ToggleInsetCursor(BufferView * bv)
923 if (the_locking_inset) {
924 the_locking_inset->ToggleInsetCursor(bv);
928 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
930 int asc = lyxfont::maxAscent(font);
931 int desc = lyxfont::maxDescent(font);
934 bv->hideLockedInsetCursor();
936 bv->showLockedInsetCursor(cx(bv), cy(bv),
938 cursor_visible = !cursor_visible;
942 void InsetText::ShowInsetCursor(BufferView * bv)
944 if (the_locking_inset) {
945 the_locking_inset->ShowInsetCursor(bv);
948 if (!cursor_visible) {
949 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
951 int asc = lyxfont::maxAscent(font);
952 int desc = lyxfont::maxDescent(font);
954 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
955 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
956 cursor_visible = true;
961 void InsetText::HideInsetCursor(BufferView * bv)
963 if (cursor_visible) {
964 bv->hideLockedInsetCursor();
965 cursor_visible = false;
967 if (the_locking_inset)
968 the_locking_inset->HideInsetCursor(bv);
972 UpdatableInset::RESULT
973 InsetText::moveRight(BufferView * bv, bool activate_inset)
975 if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
977 if (activate_inset && checkAndActivateInset(bv, false))
979 TEXT(bv)->CursorRight(bv);
980 return DISPATCHED_NOUPDATE;
984 UpdatableInset::RESULT
985 InsetText::moveLeft(BufferView * bv, bool activate_inset)
987 if (!cpar(bv)->previous && (cpos(bv) <= 0))
989 TEXT(bv)->CursorLeft(bv);
990 if (activate_inset && checkAndActivateInset(bv, true))
992 return DISPATCHED_NOUPDATE;
996 UpdatableInset::RESULT
997 InsetText::moveUp(BufferView * bv)
999 if (!crow(bv)->previous())
1001 TEXT(bv)->CursorUp(bv);
1002 return DISPATCHED_NOUPDATE;
1006 UpdatableInset::RESULT
1007 InsetText::moveDown(BufferView * bv)
1009 if (!crow(bv)->next())
1011 TEXT(bv)->CursorDown(bv);
1012 return DISPATCHED_NOUPDATE;
1016 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1018 if (the_locking_inset) {
1019 if (the_locking_inset->InsertInsetAllowed(inset))
1020 return the_locking_inset->InsertInset(bv, inset);
1023 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1024 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1025 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
1026 if (inset->Editable() == Inset::IS_EDITABLE) {
1027 UpdatableInset * i = static_cast<UpdatableInset *>(inset);
1028 i->setOwner(static_cast<UpdatableInset *>(this));
1030 cpar(bv)->InsertInset(cpos(bv), inset);
1031 TEXT(bv)->selection = 0;
1032 UpdateLocal(bv, CURSOR_PAR, true);
1033 static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1038 UpdatableInset * InsetText::GetLockingInset()
1040 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1044 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1048 if (the_locking_inset)
1049 return the_locking_inset->GetFirstLockingInsetOfType(c);
1054 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1056 TEXT(bv)->SetFont(bv, font, toggleall);
1060 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1062 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1065 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1066 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1068 x = inset->width(bv->painter(), font);
1069 y = inset->descent(bv->painter(), font);
1073 inset_x = cx(bv) - top_x + drawTextXOffset;
1074 inset_y = cy(bv) + drawTextYOffset;
1075 inset->Edit(bv, x-inset_x, y-inset_y, 0);
1076 if (!the_locking_inset)
1078 UpdateLocal(bv, CURSOR_PAR, false);
1085 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1088 int tmp_x = x - drawTextXOffset;
1089 int tmp_y = y + insetAscent;
1090 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
1097 inset_x = cx(bv) - top_x + drawTextXOffset;
1098 inset_y = cy(bv) + drawTextYOffset;
1099 inset->Edit(bv, x - inset_x, y - inset_y, button);
1100 if (!the_locking_inset)
1102 UpdateLocal(bv, CURSOR_PAR, false);
1109 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1111 return UpdatableInset::getMaxWidth(pain, inset) - (2*TEXT_TO_INSET_OFFSET);
1115 void InsetText::SetParagraphData(LyXParagraph *p)
1129 par->SetInsetOwner(this);
1133 np->next = p->Clone();
1134 np->next->previous = np;
1136 np->SetInsetOwner(this);
1142 void InsetText::SetAutoBreakRows(bool flag)
1144 if (flag != autoBreakRows) {
1145 autoBreakRows = flag;
1151 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1153 if (how != drawFrame) {
1156 UpdateLocal(bv, DRAW_FRAME, false);
1161 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1163 if (frame_color != col) {
1166 UpdateLocal(bv, DRAW_FRAME, false);
1171 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1173 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1177 int InsetText::cx(BufferView * bv) const
1179 return TEXT(bv)->cursor.x() + top_x + 1;
1183 int InsetText::cy(BufferView * bv) const
1185 long int y_dummy = 0;
1186 Row * tmprow = TEXT(bv)->GetRowNearY(y_dummy);
1187 return TEXT(bv)->cursor.y() - tmprow->baseline();
1191 int InsetText::cpos(BufferView * bv) const
1193 return TEXT(bv)->cursor.pos();
1197 LyXParagraph * InsetText::cpar(BufferView * bv) const
1199 return TEXT(bv)->cursor.par();
1203 Row * InsetText::crow(BufferView * bv) const
1205 return TEXT(bv)->cursor.row();
1209 LyXText * InsetText::getLyXText(BufferView * bv) const
1211 if (cache.find(bv) != cache.end())
1213 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1216 if (the_locking_inset) {
1217 lt->SetCursor(bv, inset_par, inset_pos);
1223 void InsetText::deleteLyXText(BufferView * bv)
1226 /// then remove all LyXText in text-insets
1227 LyXParagraph * p = par;
1228 for(;p;p = p->next) {
1229 p->deleteInsetsLyXText(bv);