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()
120 Inset * InsetText::Clone() const
122 InsetText * t = new InsetText(*this);
127 void InsetText::Write(Buffer const * buf, ostream & os) const
130 WriteParagraphData(buf, os);
134 void InsetText::WriteParagraphData(Buffer const * buf, ostream & os) const
136 par->writeFile(buf, os, buf->params, 0, 0);
140 void InsetText::Read(Buffer const * buf, LyXLex & lex)
144 LyXParagraph * return_par = 0;
145 char depth = 0; // signed or unsigned?
146 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
147 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
148 LyXFont font(LyXFont::ALL_INHERIT);
150 LyXParagraph * p = par->next;
157 par = new LyXParagraph;
160 token = lex.GetString();
163 if (token == "\\end_inset")
165 if (const_cast<Buffer*>(buf)->parseSingleLyXformat2Token(lex, par, return_par,
169 // the_end read this should NEVER happen
170 lex.printError("\\the_end read in inset! Error in document!");
178 return_par->SetInsetOwner(this);
179 return_par = return_par->next;
182 if (token != "\\end_inset") {
183 lex.printError("Missing \\end_inset at this point. "
190 int InsetText::ascent(Painter &, LyXFont const &) const
196 int InsetText::descent(Painter &, LyXFont const &) const
202 int InsetText::width(Painter &, LyXFont const &) const
208 int InsetText::textWidth(Painter & pain) const
210 int w = getMaxWidth(pain, this);
212 // printf("WW1: %d\n",w);
217 w = w - top_x; // + owner()->x();
218 // printf("WW2: %d\n",w);
219 return w; // - top_x + owner()->x();
222 w -= (2 * TEXT_TO_INSET_OFFSET);
223 // printf("WW2: %d\n",w);
224 return w - top_x; // - top_x - (2 * TEXT_TO_INSET_OFFSET);
228 void InsetText::draw(BufferView * bv, LyXFont const & f,
229 int baseline, float & x, bool cleared) const
231 Painter & pain = bv->painter();
233 if (!cleared && ((need_update==FULL) || (top_x!=int(x)) ||
234 (top_baseline!=baseline))) {
236 int h = insetAscent + insetDescent;
237 int ty = baseline - insetAscent;
241 if ((ty + h) > pain.paperHeight())
242 h = pain.paperHeight();
243 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
244 w = pain.paperWidth();
245 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
250 if (!cleared && (need_update == NONE))
254 UpdatableInset::draw(bv, f, baseline, x, cleared);
256 top_baseline = baseline;
258 top_y = baseline - ascent(pain, f);
259 last_width = width(pain, f);
260 last_height = ascent(pain, f) + descent(pain, f);
262 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
263 inset_x = cx(bv) - top_x + drawTextXOffset;
264 inset_y = cy(bv) + drawTextYOffset;
266 if (!cleared && (need_update == CURSOR)) {
271 x += TEXT_TO_INSET_OFFSET; // place for border
273 Row * row = TEXT(bv)->GetRowNearY(y);
274 y += baseline - row->ascent_of_text() + 1;
275 if (cleared || !locked || (need_update == FULL)) {
277 TEXT(bv)->GetVisibleRow(bv, y, x, row, y, cleared);
281 } else if (need_update == SELECTION) {
282 bv->screen()->ToggleToggle(getLyXText(bv), y, x);
285 bv->screen()->Update(TEXT(bv), y, x);
288 TEXT(bv)->refresh_y = 0;
289 TEXT(bv)->status = LyXText::UNCHANGED;
290 if ((drawFrame == ALWAYS) || ((drawFrame == LOCKED) && locked)) {
291 pain.rectangle(top_x, baseline - insetAscent, insetWidth,
292 insetAscent + insetDescent, frame_color);
293 } else if (need_update == CLEAR_FRAME) {
294 pain.rectangle(top_x, baseline - insetAscent, insetWidth,
295 insetAscent + insetDescent,
298 x += insetWidth - TEXT_TO_INSET_OFFSET;
303 void InsetText::update(BufferView * bv, LyXFont const & font, bool dodraw)
305 if (the_locking_inset)
306 the_locking_inset->update(bv, font, dodraw);
307 if (need_update == INIT) {
311 int oldw = insetWidth;
313 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
314 // max(textWidth(bv->painter()),
315 // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
316 // (2 * TEXT_TO_INSET_OFFSET);
318 insetWidth = textWidth(bv->painter());
320 insetWidth = static_cast<int>(TEXT(bv)->width);
322 if (oldw != insetWidth) {
323 printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
324 textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
329 owner()->update(bv, font, dodraw);
332 update(bv, font, dodraw);
336 update(bv, font, dodraw);
338 UpdateLocal(bv, INIT, false);
343 if (dodraw && (need_update != CURSOR))
346 TEXT(bv)->FullRebreak(bv);
348 if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
351 TEXT(bv)->UpdateInset(bv, the_locking_inset);
354 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
358 Row * row = TEXT(bv)->GetRowNearY(y_temp);
359 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
360 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
361 TEXT_TO_INSET_OFFSET;
365 void InsetText::UpdateLocal(BufferView * bv, UpdateCodes what, bool mark_dirty)
367 TEXT(bv)->FullRebreak(bv);
368 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
370 else if (!the_locking_inset || (what != CURSOR))
372 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED))
373 bv->updateInset(this, mark_dirty);
374 if (old_par != cpar(bv)) {
375 bv->owner()->getToolbar()->combox->select(cpar(bv)->GetLayout()+1);
381 char const * InsetText::EditMessage() const
383 return _("Opened Text Inset");
387 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
389 // par->SetInsetOwner(this);
390 UpdatableInset::Edit(bv, x, y, button);
392 if (!bv->lockInset(this)) {
393 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
397 the_locking_inset = 0;
398 inset_pos = inset_x = inset_y = 0;
400 if (!checkAndActivateInset(bv, x, y, button))
401 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset, y);
402 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
403 bv->text->FinishUndo();
404 UpdateLocal(bv, FULL, false);
408 void InsetText::InsetUnlock(BufferView * bv)
410 if (the_locking_inset) {
411 the_locking_inset->InsetUnlock(bv);
412 the_locking_inset = 0;
415 no_selection = false;
417 TEXT(bv)->selection = 0;
418 UpdateLocal(bv, CLEAR_FRAME, false);
419 bv->owner()->getToolbar()->combox->select(bv->text->cursor.par()->GetLayout()+1);
423 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
425 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
428 if (inset == cpar(bv)->GetInset(cpos(bv))) {
429 lyxerr[Debug::INSETS] << "OK" << endl;
430 the_locking_inset = inset;
431 inset_x = cx(bv) - top_x + drawTextXOffset;
432 inset_y = cy(bv) + drawTextYOffset;
433 inset_pos = cpos(bv);
434 inset_par = cpar(bv);
435 TEXT(bv)->UpdateInset(bv, the_locking_inset);
437 } else if (the_locking_inset && (the_locking_inset == inset)) {
438 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
439 lyxerr[Debug::INSETS] << "OK" << endl;
440 inset_x = cx(bv) - top_x + drawTextXOffset;
441 inset_y = cy(bv) + drawTextYOffset;
443 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
445 } else if (the_locking_inset) {
446 lyxerr[Debug::INSETS] << "MAYBE" << endl;
447 return the_locking_inset->LockInsetInInset(bv, inset);
449 lyxerr[Debug::INSETS] << "NOT OK" << endl;
454 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
457 if (!the_locking_inset)
459 if (the_locking_inset == inset) {
460 the_locking_inset->InsetUnlock(bv);
461 TEXT(bv)->UpdateInset(bv, inset);
462 the_locking_inset = 0;
464 moveRight(bv, false);
465 UpdateLocal(bv, CURSOR_PAR, false);
468 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
472 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
474 if (!the_locking_inset)
476 if (the_locking_inset != inset) {
477 TEXT(bv)->UpdateInset(bv, the_locking_inset);
478 need_update = CURSOR_PAR;
479 return the_locking_inset->UpdateInsetInInset(bv, inset);
481 // UpdateLocal(bv, FULL, false);
482 if (TEXT(bv)->UpdateInset(bv, inset))
483 UpdateLocal(bv, CURSOR_PAR, false);
484 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
485 inset_x = cx(bv) - top_x + drawTextXOffset;
486 inset_y = cy(bv) + drawTextYOffset;
492 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
494 if (TEXT(bv)->selection) {
495 TEXT(bv)->selection = 0;
496 UpdateLocal(bv, FULL, false);
498 no_selection = false;
500 int tmp_x = x - drawTextXOffset;
501 // int tmp_y = y + TEXT(bv)->first + insetAscent;
502 int tmp_y = y + insetAscent;
503 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
505 if (the_locking_inset) {
506 if (the_locking_inset == inset) {
507 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
510 // otherwise unlock the_locking_inset and lock the new inset
511 the_locking_inset->InsetUnlock(bv);
512 inset_x = cx(bv) - top_x + drawTextXOffset;
513 inset_y = cy(bv) + drawTextYOffset;
514 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
515 inset->Edit(bv, x - inset_x, y - inset_y, button);
516 if (the_locking_inset)
517 UpdateLocal(bv, CURSOR_PAR, false);
520 // otherwise only unlock the_locking_inset
521 the_locking_inset->InsetUnlock(bv);
522 the_locking_inset = 0;
524 if (bv->the_locking_inset) {
525 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
526 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
527 inset_x = cx(bv) - top_x + drawTextXOffset;
528 inset_y = cy(bv) + drawTextYOffset;
529 inset_pos = cpos(bv);
530 inset_par = cpar(bv);
531 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
532 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
533 if (the_locking_inset)
534 UpdateLocal(bv, CURSOR_PAR, false);
539 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
540 y+TEXT(bv)->first+insetAscent);
544 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
546 UpdatableInset * inset = 0;
548 if (the_locking_inset) {
549 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
551 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
552 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
553 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
554 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
556 inset_x = cx(bv) - top_x + drawTextXOffset;
557 inset_y = cy(bv) + drawTextYOffset;
558 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
559 inset->Edit(bv, x - inset_x, y - inset_y, button);
563 no_selection = false;
567 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
569 if (the_locking_inset) {
570 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
574 #warning REDO this (Jug)
576 // LyXCursor old = selection_end_cursor;
578 // setPos(bv->painter(), x, y);
579 // selection_end_cursor = cursor;
580 // if (old != selection_end_cursor)
581 // UpdateLocal(bv, false, false);
584 no_selection = false;
588 void InsetText::InsetKeyPress(XKeyEvent * xke)
590 if (the_locking_inset) {
591 the_locking_inset->InsetKeyPress(xke);
597 UpdatableInset::RESULT
598 InsetText::LocalDispatch(BufferView * bv,
599 int action, string const & arg)
601 no_selection = false;
602 UpdatableInset::RESULT
603 result= UpdatableInset::LocalDispatch(bv, action, arg);
604 if (result != UNDISPATCHED) {
605 // resetPos(bv->painter());
610 if ((action < 0) && arg.empty())
613 if (the_locking_inset) {
614 result = the_locking_inset->LocalDispatch(bv, action, arg);
615 if (result == DISPATCHED_NOUPDATE)
617 else if (result == DISPATCHED) {
618 UpdateLocal(bv, CURSOR_PAR, false);
620 } else if (result == FINISHED) {
624 moveRight(bv, false);
630 the_locking_inset = 0;
638 if (bv->buffer()->isReadonly()) {
640 // setErrorMessage(N_("Document is read only"));
644 /* Automatically delete the currently selected
645 * text and replace it with what is being
646 * typed in now. Depends on lyxrc settings
647 * "auto_region_delete", which defaults to
650 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
651 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
652 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
653 if (lyxrc.auto_region_delete) {
654 if (TEXT(bv)->selection){
655 TEXT(bv)->CutSelection(bv, false);
658 TEXT(bv)->ClearSelection();
659 for (string::size_type i = 0; i < arg.length(); ++i) {
660 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
663 UpdateLocal(bv, CURSOR_PAR, true);
665 // --- Cursor Movements ---------------------------------------------
667 bv->text->FinishUndo();
668 moveRight(bv, false);
669 TEXT(bv)->SetSelection();
670 UpdateLocal(bv, SELECTION, false);
673 bv->text->FinishUndo();
674 result = moveRight(bv);
675 TEXT(bv)->selection = 0;
676 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
677 UpdateLocal(bv, CURSOR, false);
680 bv->text->FinishUndo();
682 TEXT(bv)->SetSelection();
683 UpdateLocal(bv, SELECTION, false);
686 bv->text->FinishUndo();
687 result= moveLeft(bv);
688 TEXT(bv)->selection = 0;
689 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
690 UpdateLocal(bv, CURSOR, false);
693 bv->text->FinishUndo();
695 TEXT(bv)->SetSelection();
696 UpdateLocal(bv, SELECTION, false);
699 bv->text->FinishUndo();
700 result = moveDown(bv);
701 TEXT(bv)->selection = 0;
702 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
703 UpdateLocal(bv, CURSOR, false);
706 bv->text->FinishUndo();
708 TEXT(bv)->SetSelection();
709 UpdateLocal(bv, SELECTION, false);
712 bv->text->FinishUndo();
714 TEXT(bv)->selection = 0;
715 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
716 UpdateLocal(bv, CURSOR, false);
719 bv->text->FinishUndo();
720 TEXT(bv)->CursorHome(bv);
721 TEXT(bv)->selection = 0;
722 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
723 UpdateLocal(bv, CURSOR, false);
726 TEXT(bv)->CursorEnd(bv);
727 TEXT(bv)->selection = 0;
728 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
729 UpdateLocal(bv, CURSOR, false);
732 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
733 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
734 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
735 TEXT(bv)->Backspace(bv);
736 UpdateLocal(bv, CURSOR_PAR, true);
739 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
740 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
741 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
742 TEXT(bv)->Delete(bv);
743 UpdateLocal(bv, CURSOR_PAR, true);
746 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
747 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
748 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
749 TEXT(bv)->CutSelection(bv);
750 UpdateLocal(bv, CURSOR_PAR, true);
753 bv->text->FinishUndo();
754 TEXT(bv)->CopySelection(bv);
755 UpdateLocal(bv, CURSOR_PAR, false);
758 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
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)->PasteSelection(bv);
762 UpdateLocal(bv, CURSOR_PAR, true);
764 case LFUN_BREAKPARAGRAPH:
767 TEXT(bv)->BreakParagraph(bv, 0);
768 UpdateLocal(bv, FULL, true);
773 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
774 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
775 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
776 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
777 UpdateLocal(bv, CURSOR_PAR, true);
781 static LyXTextClass::size_type cur_layout = cpar(bv)->layout;
783 // Derive layout number from given argument (string)
784 // and current buffer's textclass (number). */
785 LyXTextClassList::ClassList::size_type tclass =
786 bv->buffer()->params.textclass;
787 std::pair <bool, LyXTextClass::size_type> layout =
788 textclasslist.NumberOfLayout(tclass, arg);
790 // If the entry is obsolete, use the new one instead.
792 string obs = textclasslist.Style(tclass,layout.second).
795 layout = textclasslist.NumberOfLayout(tclass, obs);
798 // see if we found the layout number:
800 string msg = string(N_("Layout ")) + arg + N_(" not known");
802 bv->owner()->getMiniBuffer()->Set(msg);
806 if (cur_layout != layout.second) {
807 cur_layout = layout.second;
808 TEXT(bv)->SetLayout(bv, layout.second);
809 bv->owner()->getToolbar()->combox->select(cpar(bv)->GetLayout()+1);
810 UpdateLocal(bv, CURSOR_PAR, true);
814 case LFUN_PARAGRAPH_SPACING:
815 // This one is absolutely not working. When fiddling with this
816 // it also seems to me that the paragraphs inside the insettext
817 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
819 LyXParagraph * par = TEXT(bv)->cursor.par();
820 Spacing::Space cur_spacing = par->spacing.getSpace();
821 float cur_value = 1.0;
822 if (cur_spacing == Spacing::Other) {
823 cur_value = par->spacing.getValue();
827 std::istringstream istr(arg);
829 istrstream istr(arg.c_str());
833 Spacing::Space new_spacing = cur_spacing;
834 float new_value = cur_value;
836 lyxerr << "Missing argument to `paragraph-spacing'"
838 } else if (tmp == "single") {
839 new_spacing = Spacing::Single;
840 } else if (tmp == "onehalf") {
841 new_spacing = Spacing::Onehalf;
842 } else if (tmp == "double") {
843 new_spacing = Spacing::Double;
844 } else if (tmp == "other") {
845 new_spacing = Spacing::Other;
848 lyxerr << "new_value = " << tmpval << endl;
851 } else if (tmp == "default") {
852 new_spacing = Spacing::Default;
854 lyxerr << _("Unknown spacing argument: ")
857 if (cur_spacing != new_spacing || cur_value != new_value) {
858 par->spacing.set(new_spacing, new_value);
859 //TEXT(bv)->RedoParagraph(owner->view());
860 UpdateLocal(bv, CURSOR_PAR, true);
861 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
867 result = UNDISPATCHED;
870 if (result != FINISHED) {
873 bv->unlockInset(this);
878 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
881 buf->latexParagraphs(os, par, 0, texrow);
882 return texrow.rows();
886 void InsetText::Validate(LaTeXFeatures & features) const
888 LyXParagraph * p = par;
890 p->validate(features);
896 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
898 if (textclasslist.Style(buf->params.textclass,
899 p->GetLayout()).labeltype != LABEL_MANUAL)
902 return p->BeginningOfMainBody();
906 void InsetText::GetCursorPos(BufferView * bv, int & x, int & y) const
913 int InsetText::InsetInInsetY()
915 if (!the_locking_inset)
918 return (inset_y + the_locking_inset->InsetInInsetY());
922 void InsetText::ToggleInsetCursor(BufferView * bv)
924 if (the_locking_inset) {
925 the_locking_inset->ToggleInsetCursor(bv);
929 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
931 int asc = lyxfont::maxAscent(font);
932 int desc = lyxfont::maxDescent(font);
935 bv->hideLockedInsetCursor();
937 bv->showLockedInsetCursor(cx(bv), cy(bv),
939 cursor_visible = !cursor_visible;
943 void InsetText::ShowInsetCursor(BufferView * bv)
945 if (the_locking_inset) {
946 the_locking_inset->ShowInsetCursor(bv);
949 if (!cursor_visible) {
950 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
952 int asc = lyxfont::maxAscent(font);
953 int desc = lyxfont::maxDescent(font);
955 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
956 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
957 cursor_visible = true;
962 void InsetText::HideInsetCursor(BufferView * bv)
964 if (cursor_visible) {
965 bv->hideLockedInsetCursor();
966 cursor_visible = false;
968 if (the_locking_inset)
969 the_locking_inset->HideInsetCursor(bv);
973 UpdatableInset::RESULT
974 InsetText::moveRight(BufferView * bv, bool activate_inset)
976 if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
978 if (activate_inset && checkAndActivateInset(bv, false))
980 TEXT(bv)->CursorRight(bv);
981 return DISPATCHED_NOUPDATE;
985 UpdatableInset::RESULT
986 InsetText::moveLeft(BufferView * bv, bool activate_inset)
988 if (!cpar(bv)->previous && (cpos(bv) <= 0))
990 TEXT(bv)->CursorLeft(bv);
991 if (activate_inset && checkAndActivateInset(bv, true))
993 return DISPATCHED_NOUPDATE;
997 UpdatableInset::RESULT
998 InsetText::moveUp(BufferView * bv)
1000 if (!crow(bv)->previous())
1002 TEXT(bv)->CursorUp(bv);
1003 return DISPATCHED_NOUPDATE;
1007 UpdatableInset::RESULT
1008 InsetText::moveDown(BufferView * bv)
1010 if (!crow(bv)->next())
1012 TEXT(bv)->CursorDown(bv);
1013 return DISPATCHED_NOUPDATE;
1017 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1019 if (the_locking_inset) {
1020 if (the_locking_inset->InsertInsetAllowed(inset))
1021 return the_locking_inset->InsertInset(bv, inset);
1024 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1025 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1026 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
1027 if (inset->Editable() == Inset::IS_EDITABLE) {
1028 UpdatableInset * i = static_cast<UpdatableInset *>(inset);
1029 i->setOwner(static_cast<UpdatableInset *>(this));
1031 cpar(bv)->InsertInset(cpos(bv), inset);
1032 TEXT(bv)->selection = 0;
1033 UpdateLocal(bv, CURSOR_PAR, true);
1034 static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1039 UpdatableInset * InsetText::GetLockingInset()
1041 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1045 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1049 if (the_locking_inset)
1050 return the_locking_inset->GetFirstLockingInsetOfType(c);
1055 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1057 TEXT(bv)->SetFont(bv, font, toggleall);
1061 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1063 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1066 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1067 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1069 x = inset->width(bv->painter(), font);
1070 y = inset->descent(bv->painter(), font);
1074 inset_x = cx(bv) - top_x + drawTextXOffset;
1075 inset_y = cy(bv) + drawTextYOffset;
1076 inset->Edit(bv, x-inset_x, y-inset_y, 0);
1077 if (!the_locking_inset)
1079 UpdateLocal(bv, CURSOR_PAR, false);
1086 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1089 int tmp_x = x - drawTextXOffset;
1090 int tmp_y = y + insetAscent;
1091 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
1098 inset_x = cx(bv) - top_x + drawTextXOffset;
1099 inset_y = cy(bv) + drawTextYOffset;
1100 inset->Edit(bv, x - inset_x, y - inset_y, button);
1101 if (!the_locking_inset)
1103 UpdateLocal(bv, CURSOR_PAR, false);
1110 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1112 return UpdatableInset::getMaxWidth(pain, inset) - (2*TEXT_TO_INSET_OFFSET);
1116 void InsetText::SetParagraphData(LyXParagraph *p)
1130 par->SetInsetOwner(this);
1134 np->next = p->Clone();
1135 np->next->previous = np;
1137 np->SetInsetOwner(this);
1143 void InsetText::SetAutoBreakRows(bool flag)
1145 if (flag != autoBreakRows) {
1146 autoBreakRows = flag;
1152 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1154 if (how != drawFrame) {
1157 UpdateLocal(bv, DRAW_FRAME, false);
1162 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1164 if (frame_color != col) {
1167 UpdateLocal(bv, DRAW_FRAME, false);
1172 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1174 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1178 int InsetText::cx(BufferView * bv) const
1180 return TEXT(bv)->cursor.x() + top_x + 1;
1184 int InsetText::cy(BufferView * bv) const
1186 long int y_dummy = 0;
1187 Row * tmprow = TEXT(bv)->GetRowNearY(y_dummy);
1188 return TEXT(bv)->cursor.y() - tmprow->baseline();
1192 int InsetText::cpos(BufferView * bv) const
1194 return TEXT(bv)->cursor.pos();
1198 LyXParagraph * InsetText::cpar(BufferView * bv) const
1200 return TEXT(bv)->cursor.par();
1204 Row * InsetText::crow(BufferView * bv) const
1206 return TEXT(bv)->cursor.row();
1210 LyXText * InsetText::getLyXText(BufferView * bv) const
1212 if (cache.find(bv) != cache.end())
1214 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1217 if (the_locking_inset) {
1218 lt->SetCursor(bv, inset_par, inset_pos);
1224 void InsetText::deleteLyXText(BufferView * bv)
1227 /// then remove all LyXText in text-insets
1228 LyXParagraph * p = par;
1229 for(;p;p = p->next) {
1230 p->deleteInsetsLyXText(bv);