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 - (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);
294 pain.rectangle(top_x, baseline - insetAscent, insetWidth,
295 insetAscent + insetDescent,
298 x += width(pain, f) - 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 = 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));
327 owner()->update(bv, font, dodraw);
332 update(bv, font, dodraw);
334 UpdateLocal(bv, INIT, false);
339 if (dodraw && (need_update != CURSOR))
342 TEXT(bv)->FullRebreak(bv);
344 if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
347 TEXT(bv)->UpdateInset(bv, the_locking_inset);
351 Row * row = TEXT(bv)->GetRowNearY(y_temp);
352 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
353 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
354 TEXT_TO_INSET_OFFSET;
358 void InsetText::UpdateLocal(BufferView * bv, UpdateCodes what, bool mark_dirty)
360 TEXT(bv)->FullRebreak(bv);
361 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
363 else if (!the_locking_inset || (what != CURSOR))
365 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED))
366 bv->updateInset(this, mark_dirty);
367 if (old_par != cpar(bv)) {
368 bv->owner()->getToolbar()->combox->select(cpar(bv)->GetLayout()+1);
374 char const * InsetText::EditMessage() const
376 return _("Opened Text Inset");
380 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
382 // par->SetInsetOwner(this);
383 UpdatableInset::Edit(bv, x, y, button);
385 if (!bv->lockInset(this)) {
386 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
390 the_locking_inset = 0;
391 inset_pos = inset_x = inset_y = 0;
393 if (!checkAndActivateInset(bv, x, y, button))
394 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset, y);
395 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
396 bv->text->FinishUndo();
397 UpdateLocal(bv, FULL, false);
401 void InsetText::InsetUnlock(BufferView * bv)
403 if (the_locking_inset) {
404 the_locking_inset->InsetUnlock(bv);
405 the_locking_inset = 0;
408 no_selection = false;
410 TEXT(bv)->selection = 0;
411 UpdateLocal(bv, CURSOR_PAR, false);
412 bv->owner()->getToolbar()->combox->select(bv->text->cursor.par()->GetLayout()+1);
416 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
418 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
421 if (inset == cpar(bv)->GetInset(cpos(bv))) {
422 lyxerr[Debug::INSETS] << "OK" << endl;
423 the_locking_inset = inset;
424 inset_x = cx(bv) - top_x + drawTextXOffset;
425 inset_y = cy(bv) + drawTextYOffset;
426 inset_pos = cpos(bv);
427 inset_par = cpar(bv);
428 TEXT(bv)->UpdateInset(bv, the_locking_inset);
430 } else if (the_locking_inset && (the_locking_inset == inset)) {
431 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
432 lyxerr[Debug::INSETS] << "OK" << endl;
433 inset_x = cx(bv) - top_x + drawTextXOffset;
434 inset_y = cy(bv) + drawTextYOffset;
436 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
438 } else if (the_locking_inset) {
439 lyxerr[Debug::INSETS] << "MAYBE" << endl;
440 return the_locking_inset->LockInsetInInset(bv, inset);
442 lyxerr[Debug::INSETS] << "NOT OK" << endl;
447 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
450 if (!the_locking_inset)
452 if (the_locking_inset == inset) {
453 the_locking_inset->InsetUnlock(bv);
454 TEXT(bv)->UpdateInset(bv, inset);
455 the_locking_inset = 0;
457 moveRight(bv, false);
458 UpdateLocal(bv, CURSOR_PAR, false);
461 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
465 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
467 if (!the_locking_inset)
469 if (the_locking_inset != inset) {
470 TEXT(bv)->UpdateInset(bv, the_locking_inset);
471 need_update = CURSOR_PAR;
472 return the_locking_inset->UpdateInsetInInset(bv, inset);
474 // UpdateLocal(bv, FULL, false);
475 if (TEXT(bv)->UpdateInset(bv, inset))
476 UpdateLocal(bv, CURSOR_PAR, false);
477 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
478 inset_x = cx(bv) - top_x + drawTextXOffset;
479 inset_y = cy(bv) + drawTextYOffset;
485 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
487 if (TEXT(bv)->selection) {
488 TEXT(bv)->selection = 0;
489 UpdateLocal(bv, FULL, false);
491 no_selection = false;
493 int tmp_x = x - drawTextXOffset;
494 // int tmp_y = y + TEXT(bv)->first + insetAscent;
495 int tmp_y = y + insetAscent;
496 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
498 if (the_locking_inset) {
499 if (the_locking_inset == inset) {
500 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
503 // otherwise unlock the_locking_inset and lock the new inset
504 the_locking_inset->InsetUnlock(bv);
505 inset_x = cx(bv) - top_x + drawTextXOffset;
506 inset_y = cy(bv) + drawTextYOffset;
507 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
508 inset->Edit(bv, x - inset_x, y - inset_y, button);
509 if (the_locking_inset)
510 UpdateLocal(bv, CURSOR_PAR, false);
513 // otherwise only unlock the_locking_inset
514 the_locking_inset->InsetUnlock(bv);
515 the_locking_inset = 0;
517 if (bv->the_locking_inset) {
518 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
519 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
520 inset_x = cx(bv) - top_x + drawTextXOffset;
521 inset_y = cy(bv) + drawTextYOffset;
522 inset_pos = cpos(bv);
523 inset_par = cpar(bv);
524 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
525 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
526 if (the_locking_inset)
527 UpdateLocal(bv, CURSOR_PAR, false);
532 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
533 y+TEXT(bv)->first+insetAscent);
537 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
539 UpdatableInset * inset = 0;
541 if (the_locking_inset) {
542 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
544 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
545 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
546 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
547 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
549 inset_x = cx(bv) - top_x + drawTextXOffset;
550 inset_y = cy(bv) + drawTextYOffset;
551 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
552 inset->Edit(bv, x - inset_x, y - inset_y, button);
556 no_selection = false;
560 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
562 if (the_locking_inset) {
563 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
567 #warning REDO this (Jug)
569 // LyXCursor old = selection_end_cursor;
571 // setPos(bv->painter(), x, y);
572 // selection_end_cursor = cursor;
573 // if (old != selection_end_cursor)
574 // UpdateLocal(bv, false, false);
577 no_selection = false;
581 void InsetText::InsetKeyPress(XKeyEvent * xke)
583 if (the_locking_inset) {
584 the_locking_inset->InsetKeyPress(xke);
590 UpdatableInset::RESULT
591 InsetText::LocalDispatch(BufferView * bv,
592 int action, string const & arg)
594 no_selection = false;
595 UpdatableInset::RESULT
596 result= UpdatableInset::LocalDispatch(bv, action, arg);
597 if (result != UNDISPATCHED) {
598 // resetPos(bv->painter());
603 if ((action < 0) && arg.empty())
606 if (the_locking_inset) {
607 result = the_locking_inset->LocalDispatch(bv, action, arg);
608 if (result == DISPATCHED_NOUPDATE)
610 else if (result == DISPATCHED) {
611 UpdateLocal(bv, CURSOR_PAR, false);
613 } else if (result == FINISHED) {
617 moveRight(bv, false);
623 the_locking_inset = 0;
631 if (bv->buffer()->isReadonly()) {
633 // setErrorMessage(N_("Document is read only"));
637 /* Automatically delete the currently selected
638 * text and replace it with what is being
639 * typed in now. Depends on lyxrc settings
640 * "auto_region_delete", which defaults to
643 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
644 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
645 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
646 if (lyxrc.auto_region_delete) {
647 if (TEXT(bv)->selection){
648 TEXT(bv)->CutSelection(bv, false);
651 TEXT(bv)->ClearSelection();
652 for (string::size_type i = 0; i < arg.length(); ++i) {
653 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
656 UpdateLocal(bv, CURSOR_PAR, true);
658 // --- Cursor Movements ---------------------------------------------
660 bv->text->FinishUndo();
661 moveRight(bv, false);
662 TEXT(bv)->SetSelection();
663 UpdateLocal(bv, SELECTION, false);
666 bv->text->FinishUndo();
667 result = moveRight(bv);
668 TEXT(bv)->selection = 0;
669 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
670 UpdateLocal(bv, CURSOR, false);
673 bv->text->FinishUndo();
675 TEXT(bv)->SetSelection();
676 UpdateLocal(bv, SELECTION, false);
679 bv->text->FinishUndo();
680 result= moveLeft(bv);
681 TEXT(bv)->selection = 0;
682 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
683 UpdateLocal(bv, CURSOR, false);
686 bv->text->FinishUndo();
688 TEXT(bv)->SetSelection();
689 UpdateLocal(bv, SELECTION, false);
692 bv->text->FinishUndo();
693 result = moveDown(bv);
694 TEXT(bv)->selection = 0;
695 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
696 UpdateLocal(bv, CURSOR, false);
699 bv->text->FinishUndo();
701 TEXT(bv)->SetSelection();
702 UpdateLocal(bv, SELECTION, false);
705 bv->text->FinishUndo();
707 TEXT(bv)->selection = 0;
708 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
709 UpdateLocal(bv, CURSOR, false);
712 bv->text->FinishUndo();
713 TEXT(bv)->CursorHome(bv);
714 TEXT(bv)->selection = 0;
715 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
716 UpdateLocal(bv, CURSOR, false);
719 TEXT(bv)->CursorEnd(bv);
720 TEXT(bv)->selection = 0;
721 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
722 UpdateLocal(bv, CURSOR, false);
725 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
726 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
727 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
728 TEXT(bv)->Backspace(bv);
729 UpdateLocal(bv, CURSOR_PAR, true);
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)->Delete(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)->CutSelection(bv);
743 UpdateLocal(bv, CURSOR_PAR, true);
746 bv->text->FinishUndo();
747 TEXT(bv)->CopySelection(bv);
748 UpdateLocal(bv, CURSOR_PAR, false);
751 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
752 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
753 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
754 TEXT(bv)->PasteSelection(bv);
755 UpdateLocal(bv, CURSOR_PAR, true);
757 case LFUN_BREAKPARAGRAPH:
760 TEXT(bv)->BreakParagraph(bv, 0);
761 UpdateLocal(bv, FULL, true);
766 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
767 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
768 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
769 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
770 UpdateLocal(bv, CURSOR_PAR, true);
774 static LyXTextClass::size_type cur_layout = cpar(bv)->layout;
776 // Derive layout number from given argument (string)
777 // and current buffer's textclass (number). */
778 LyXTextClassList::ClassList::size_type tclass =
779 bv->buffer()->params.textclass;
780 std::pair <bool, LyXTextClass::size_type> layout =
781 textclasslist.NumberOfLayout(tclass, arg);
783 // If the entry is obsolete, use the new one instead.
785 string obs = textclasslist.Style(tclass,layout.second).
788 layout = textclasslist.NumberOfLayout(tclass, obs);
791 // see if we found the layout number:
793 string msg = string(N_("Layout ")) + arg + N_(" not known");
795 bv->owner()->getMiniBuffer()->Set(msg);
799 if (cur_layout != layout.second) {
800 cur_layout = layout.second;
801 TEXT(bv)->SetLayout(bv, layout.second);
802 bv->owner()->getToolbar()->combox->select(cpar(bv)->GetLayout()+1);
803 UpdateLocal(bv, CURSOR_PAR, true);
807 case LFUN_PARAGRAPH_SPACING:
808 // This one is absolutely not working. When fiddling with this
809 // it also seems to me that the paragraphs inside the insettext
810 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
812 LyXParagraph * par = TEXT(bv)->cursor.par();
813 Spacing::Space cur_spacing = par->spacing.getSpace();
814 float cur_value = 1.0;
815 if (cur_spacing == Spacing::Other) {
816 cur_value = par->spacing.getValue();
820 std::istringstream istr(arg);
822 istrstream istr(arg.c_str());
826 Spacing::Space new_spacing = cur_spacing;
827 float new_value = cur_value;
829 lyxerr << "Missing argument to `paragraph-spacing'"
831 } else if (tmp == "single") {
832 new_spacing = Spacing::Single;
833 } else if (tmp == "onehalf") {
834 new_spacing = Spacing::Onehalf;
835 } else if (tmp == "double") {
836 new_spacing = Spacing::Double;
837 } else if (tmp == "other") {
838 new_spacing = Spacing::Other;
841 lyxerr << "new_value = " << tmpval << endl;
844 } else if (tmp == "default") {
845 new_spacing = Spacing::Default;
847 lyxerr << _("Unknown spacing argument: ")
850 if (cur_spacing != new_spacing || cur_value != new_value) {
851 par->spacing.set(new_spacing, new_value);
852 //TEXT(bv)->RedoParagraph(owner->view());
853 UpdateLocal(bv, CURSOR_PAR, true);
854 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
860 result = UNDISPATCHED;
863 if (result != FINISHED) {
866 bv->unlockInset(this);
871 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
874 buf->latexParagraphs(os, par, 0, texrow);
875 return texrow.rows();
879 void InsetText::Validate(LaTeXFeatures & features) const
881 LyXParagraph * p = par;
883 p->validate(features);
889 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
891 if (textclasslist.Style(buf->params.textclass,
892 p->GetLayout()).labeltype != LABEL_MANUAL)
895 return p->BeginningOfMainBody();
899 void InsetText::GetCursorPos(BufferView * bv, int & x, int & y) const
906 int InsetText::InsetInInsetY()
908 if (!the_locking_inset)
911 return (inset_y + the_locking_inset->InsetInInsetY());
915 void InsetText::ToggleInsetCursor(BufferView * bv)
917 if (the_locking_inset) {
918 the_locking_inset->ToggleInsetCursor(bv);
922 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
924 int asc = lyxfont::maxAscent(font);
925 int desc = lyxfont::maxDescent(font);
928 bv->hideLockedInsetCursor();
930 bv->showLockedInsetCursor(cx(bv), cy(bv),
932 cursor_visible = !cursor_visible;
936 void InsetText::ShowInsetCursor(BufferView * bv)
938 if (the_locking_inset) {
939 the_locking_inset->ShowInsetCursor(bv);
942 if (!cursor_visible) {
943 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
945 int asc = lyxfont::maxAscent(font);
946 int desc = lyxfont::maxDescent(font);
948 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
949 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
950 cursor_visible = true;
955 void InsetText::HideInsetCursor(BufferView * bv)
957 if (cursor_visible) {
958 bv->hideLockedInsetCursor();
959 cursor_visible = false;
961 if (the_locking_inset)
962 the_locking_inset->HideInsetCursor(bv);
966 UpdatableInset::RESULT
967 InsetText::moveRight(BufferView * bv, bool activate_inset)
969 if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
971 if (activate_inset && checkAndActivateInset(bv, false))
973 TEXT(bv)->CursorRight(bv);
974 return DISPATCHED_NOUPDATE;
978 UpdatableInset::RESULT
979 InsetText::moveLeft(BufferView * bv, bool activate_inset)
981 if (!cpar(bv)->previous && (cpos(bv) <= 0))
983 TEXT(bv)->CursorLeft(bv);
984 if (activate_inset && checkAndActivateInset(bv, true))
986 return DISPATCHED_NOUPDATE;
990 UpdatableInset::RESULT
991 InsetText::moveUp(BufferView * bv)
993 if (!crow(bv)->previous())
995 TEXT(bv)->CursorUp(bv);
996 return DISPATCHED_NOUPDATE;
1000 UpdatableInset::RESULT
1001 InsetText::moveDown(BufferView * bv)
1003 if (!crow(bv)->next())
1005 TEXT(bv)->CursorDown(bv);
1006 return DISPATCHED_NOUPDATE;
1010 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1012 if (the_locking_inset) {
1013 if (the_locking_inset->InsertInsetAllowed(inset))
1014 return the_locking_inset->InsertInset(bv, inset);
1017 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1018 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1019 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
1020 if (inset->Editable() == Inset::IS_EDITABLE) {
1021 UpdatableInset * i = static_cast<UpdatableInset *>(inset);
1022 i->setOwner(static_cast<UpdatableInset *>(this));
1024 cpar(bv)->InsertInset(cpos(bv), inset);
1025 TEXT(bv)->selection = 0;
1026 UpdateLocal(bv, CURSOR_PAR, true);
1027 static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1032 UpdatableInset * InsetText::GetLockingInset()
1034 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1038 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1042 if (the_locking_inset)
1043 return the_locking_inset->GetFirstLockingInsetOfType(c);
1048 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1050 TEXT(bv)->SetFont(bv, font, toggleall);
1054 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1056 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1059 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1060 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1062 x = inset->width(bv->painter(), font);
1063 y = inset->descent(bv->painter(), font);
1067 inset_x = cx(bv) - top_x + drawTextXOffset;
1068 inset_y = cy(bv) + drawTextYOffset;
1069 inset->Edit(bv, x-inset_x, y-inset_y, 0);
1070 if (!the_locking_inset)
1072 UpdateLocal(bv, CURSOR_PAR, false);
1079 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1082 int tmp_x = x - drawTextXOffset;
1083 int tmp_y = y + insetAscent;
1084 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
1091 inset_x = cx(bv) - top_x + drawTextXOffset;
1092 inset_y = cy(bv) + drawTextYOffset;
1093 inset->Edit(bv, x - inset_x, y - inset_y, button);
1094 if (!the_locking_inset)
1096 UpdateLocal(bv, CURSOR_PAR, false);
1103 int InsetText::getMaxTextWidth(Painter & pain,
1104 UpdatableInset const * inset) const
1106 return getMaxWidth(pain, inset) - (2 * TEXT_TO_INSET_OFFSET);
1110 void InsetText::SetParagraphData(LyXParagraph *p)
1124 par->SetInsetOwner(this);
1128 np->next = p->Clone();
1129 np->next->previous = np;
1131 np->SetInsetOwner(this);
1137 void InsetText::SetAutoBreakRows(bool flag)
1139 if (flag != autoBreakRows) {
1140 autoBreakRows = flag;
1146 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1148 if (how != drawFrame) {
1151 UpdateLocal(bv, FRAME, false);
1156 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1158 if (frame_color != col) {
1161 UpdateLocal(bv, FRAME, false);
1166 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1168 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1172 int InsetText::cx(BufferView * bv) const
1174 return TEXT(bv)->cursor.x() + top_x + 1;
1178 int InsetText::cy(BufferView * bv) const
1180 long int y_dummy = 0;
1181 Row * tmprow = TEXT(bv)->GetRowNearY(y_dummy);
1182 return TEXT(bv)->cursor.y() - tmprow->baseline();
1186 int InsetText::cpos(BufferView * bv) const
1188 return TEXT(bv)->cursor.pos();
1192 LyXParagraph * InsetText::cpar(BufferView * bv) const
1194 return TEXT(bv)->cursor.par();
1198 Row * InsetText::crow(BufferView * bv) const
1200 return TEXT(bv)->cursor.row();
1204 LyXText * InsetText::getLyXText(BufferView * bv) const
1206 if (cache.find(bv) != cache.end())
1208 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1211 if (the_locking_inset) {
1212 lt->SetCursor(bv, inset_par, inset_pos);
1218 void InsetText::deleteLyXText(BufferView * bv)
1221 /// then remove all LyXText in text-insets
1222 LyXParagraph * p = par;
1223 for(;p;p = p->next) {
1224 p->deleteInsetsLyXText(bv);