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)) {
266 top_baseline = baseline;
268 top_y = baseline - ascent(bv, f);
269 last_width = width(bv, f);
270 last_height = ascent(bv, f) + descent(bv, f);
272 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
273 inset_x = cx(bv) - top_x + drawTextXOffset;
274 inset_y = cy(bv) + drawTextYOffset;
276 if (!cleared && (need_update == CURSOR)) {
281 x += TEXT_TO_INSET_OFFSET; // place for border
283 Row * row = TEXT(bv)->GetRowNearY(y);
284 y += baseline - row->ascent_of_text() + 1;
285 if (cleared || !locked || (need_update == FULL)) {
287 TEXT(bv)->GetVisibleRow(bv, y, x, row, y, cleared);
291 } else if (need_update == SELECTION) {
292 bv->screen()->ToggleToggle(getLyXText(bv), y, x);
295 bv->screen()->Update(TEXT(bv), y, x);
298 TEXT(bv)->refresh_y = 0;
299 TEXT(bv)->status = LyXText::UNCHANGED;
300 if ((drawFrame == ALWAYS) || ((drawFrame == LOCKED) && locked)) {
301 pain.rectangle(top_x, baseline - insetAscent, insetWidth,
302 insetAscent + insetDescent, frame_color);
303 } else if (need_update == CLEAR_FRAME) {
304 pain.rectangle(top_x, baseline - insetAscent, insetWidth,
305 insetAscent + insetDescent,
308 x += insetWidth - TEXT_TO_INSET_OFFSET;
309 if (need_update != INIT)
314 void InsetText::update(BufferView * bv, LyXFont const & font, bool dodraw)
316 if (the_locking_inset)
317 the_locking_inset->update(bv, font, dodraw);
318 if (need_update == INIT) {
322 int oldw = insetWidth;
324 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
325 // max(textWidth(bv->painter()),
326 // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
327 // (2 * TEXT_TO_INSET_OFFSET);
329 insetWidth = textWidth(bv->painter());
331 insetWidth = static_cast<int>(TEXT(bv)->width);
333 if (oldw != insetWidth) {
334 printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
335 textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
340 owner()->update(bv, font, dodraw);
343 update(bv, font, dodraw);
347 update(bv, font, dodraw);
349 UpdateLocal(bv, INIT, false);
354 if (dodraw && (need_update != CURSOR))
357 TEXT(bv)->FullRebreak(bv);
359 if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
362 TEXT(bv)->UpdateInset(bv, the_locking_inset);
365 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
369 Row * row = TEXT(bv)->GetRowNearY(y_temp);
370 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
371 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
372 TEXT_TO_INSET_OFFSET;
376 void InsetText::UpdateLocal(BufferView * bv, UpdateCodes what, bool mark_dirty)
378 TEXT(bv)->FullRebreak(bv);
379 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
381 else if (!the_locking_inset || (what != CURSOR))
383 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED))
384 bv->updateInset(this, mark_dirty);
385 if (old_par != cpar(bv)) {
386 bv->owner()->getToolbar()->combox->select(cpar(bv)->GetLayout()+1);
392 char const * InsetText::EditMessage() const
394 return _("Opened Text Inset");
398 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
400 // par->SetInsetOwner(this);
401 UpdatableInset::Edit(bv, x, y, button);
403 if (!bv->lockInset(this)) {
404 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
408 the_locking_inset = 0;
409 inset_pos = inset_x = inset_y = 0;
411 if (!checkAndActivateInset(bv, x, y, button))
412 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset, y);
413 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
414 bv->text->FinishUndo();
415 UpdateLocal(bv, FULL, false);
419 void InsetText::InsetUnlock(BufferView * bv)
421 if (the_locking_inset) {
422 the_locking_inset->InsetUnlock(bv);
423 the_locking_inset = 0;
426 no_selection = false;
428 TEXT(bv)->selection = 0;
429 UpdateLocal(bv, CLEAR_FRAME, false);
430 bv->owner()->getToolbar()->combox->select(bv->text->cursor.par()->GetLayout()+1);
434 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
436 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
439 if (inset == cpar(bv)->GetInset(cpos(bv))) {
440 lyxerr[Debug::INSETS] << "OK" << endl;
441 the_locking_inset = inset;
442 inset_x = cx(bv) - top_x + drawTextXOffset;
443 inset_y = cy(bv) + drawTextYOffset;
444 inset_pos = cpos(bv);
445 inset_par = cpar(bv);
446 TEXT(bv)->UpdateInset(bv, the_locking_inset);
448 } else if (the_locking_inset && (the_locking_inset == inset)) {
449 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
450 lyxerr[Debug::INSETS] << "OK" << endl;
451 inset_x = cx(bv) - top_x + drawTextXOffset;
452 inset_y = cy(bv) + drawTextYOffset;
454 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
456 } else if (the_locking_inset) {
457 lyxerr[Debug::INSETS] << "MAYBE" << endl;
458 return the_locking_inset->LockInsetInInset(bv, inset);
460 lyxerr[Debug::INSETS] << "NOT OK" << endl;
465 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
468 if (!the_locking_inset)
470 if (the_locking_inset == inset) {
471 the_locking_inset->InsetUnlock(bv);
472 TEXT(bv)->UpdateInset(bv, inset);
473 the_locking_inset = 0;
475 moveRight(bv, false);
476 UpdateLocal(bv, CURSOR_PAR, false);
479 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
483 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
485 if (!the_locking_inset)
487 if (the_locking_inset != inset) {
488 TEXT(bv)->UpdateInset(bv, the_locking_inset);
489 need_update = CURSOR_PAR;
490 return the_locking_inset->UpdateInsetInInset(bv, inset);
492 // UpdateLocal(bv, FULL, false);
493 if (TEXT(bv)->UpdateInset(bv, inset))
494 UpdateLocal(bv, CURSOR_PAR, false);
495 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
496 inset_x = cx(bv) - top_x + drawTextXOffset;
497 inset_y = cy(bv) + drawTextYOffset;
503 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
505 if (TEXT(bv)->selection) {
506 TEXT(bv)->selection = 0;
507 UpdateLocal(bv, FULL, false);
509 no_selection = false;
511 int tmp_x = x - drawTextXOffset;
512 // int tmp_y = y + TEXT(bv)->first + insetAscent;
513 int tmp_y = y + insetAscent;
514 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
516 if (the_locking_inset) {
517 if (the_locking_inset == inset) {
518 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
521 // otherwise unlock the_locking_inset and lock the new inset
522 the_locking_inset->InsetUnlock(bv);
523 inset_x = cx(bv) - top_x + drawTextXOffset;
524 inset_y = cy(bv) + drawTextYOffset;
525 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
526 inset->Edit(bv, x - inset_x, y - inset_y, button);
527 if (the_locking_inset)
528 UpdateLocal(bv, CURSOR_PAR, false);
531 // otherwise only unlock the_locking_inset
532 the_locking_inset->InsetUnlock(bv);
533 the_locking_inset = 0;
535 if (bv->the_locking_inset) {
536 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
537 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
538 inset_x = cx(bv) - top_x + drawTextXOffset;
539 inset_y = cy(bv) + drawTextYOffset;
540 inset_pos = cpos(bv);
541 inset_par = cpar(bv);
542 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
543 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
544 if (the_locking_inset)
545 UpdateLocal(bv, CURSOR_PAR, false);
550 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
551 y+TEXT(bv)->first+insetAscent);
555 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
557 UpdatableInset * inset = 0;
559 if (the_locking_inset) {
560 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
562 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
563 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
564 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
565 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
567 inset_x = cx(bv) - top_x + drawTextXOffset;
568 inset_y = cy(bv) + drawTextYOffset;
569 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
570 inset->Edit(bv, x - inset_x, y - inset_y, button);
574 no_selection = false;
578 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
580 if (the_locking_inset) {
581 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
585 #warning REDO this (Jug)
587 // LyXCursor old = selection_end_cursor;
589 // setPos(bv->painter(), x, y);
590 // selection_end_cursor = cursor;
591 // if (old != selection_end_cursor)
592 // UpdateLocal(bv, false, false);
595 no_selection = false;
599 void InsetText::InsetKeyPress(XKeyEvent * xke)
601 if (the_locking_inset) {
602 the_locking_inset->InsetKeyPress(xke);
608 UpdatableInset::RESULT
609 InsetText::LocalDispatch(BufferView * bv,
610 int action, string const & arg)
612 no_selection = false;
613 UpdatableInset::RESULT
614 result= UpdatableInset::LocalDispatch(bv, action, arg);
615 if (result != UNDISPATCHED) {
616 // resetPos(bv->painter());
621 if ((action < 0) && arg.empty())
624 if (the_locking_inset) {
625 result = the_locking_inset->LocalDispatch(bv, action, arg);
626 if (result == DISPATCHED_NOUPDATE)
628 else if (result == DISPATCHED) {
629 UpdateLocal(bv, CURSOR_PAR, false);
631 } else if (result == FINISHED) {
635 moveRight(bv, false);
641 the_locking_inset = 0;
649 if (bv->buffer()->isReadonly()) {
651 // setErrorMessage(N_("Document is read only"));
655 /* Automatically delete the currently selected
656 * text and replace it with what is being
657 * typed in now. Depends on lyxrc settings
658 * "auto_region_delete", which defaults to
661 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
662 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
663 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
664 if (lyxrc.auto_region_delete) {
665 if (TEXT(bv)->selection){
666 TEXT(bv)->CutSelection(bv, false);
669 TEXT(bv)->ClearSelection();
670 for (string::size_type i = 0; i < arg.length(); ++i) {
671 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
674 UpdateLocal(bv, CURSOR_PAR, true);
676 // --- Cursor Movements ---------------------------------------------
678 bv->text->FinishUndo();
679 moveRight(bv, false);
680 TEXT(bv)->SetSelection();
681 UpdateLocal(bv, SELECTION, false);
684 bv->text->FinishUndo();
685 result = moveRight(bv);
686 TEXT(bv)->selection = 0;
687 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
688 UpdateLocal(bv, CURSOR, false);
691 bv->text->FinishUndo();
693 TEXT(bv)->SetSelection();
694 UpdateLocal(bv, SELECTION, false);
697 bv->text->FinishUndo();
698 result= moveLeft(bv);
699 TEXT(bv)->selection = 0;
700 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
701 UpdateLocal(bv, CURSOR, false);
704 bv->text->FinishUndo();
706 TEXT(bv)->SetSelection();
707 UpdateLocal(bv, SELECTION, false);
710 bv->text->FinishUndo();
711 result = moveDown(bv);
712 TEXT(bv)->selection = 0;
713 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
714 UpdateLocal(bv, CURSOR, false);
717 bv->text->FinishUndo();
719 TEXT(bv)->SetSelection();
720 UpdateLocal(bv, SELECTION, false);
723 bv->text->FinishUndo();
725 TEXT(bv)->selection = 0;
726 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
727 UpdateLocal(bv, CURSOR, false);
730 bv->text->FinishUndo();
731 TEXT(bv)->CursorHome(bv);
732 TEXT(bv)->selection = 0;
733 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
734 UpdateLocal(bv, CURSOR, false);
737 TEXT(bv)->CursorEnd(bv);
738 TEXT(bv)->selection = 0;
739 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
740 UpdateLocal(bv, CURSOR, false);
743 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
744 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
745 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
746 TEXT(bv)->Backspace(bv);
747 UpdateLocal(bv, CURSOR_PAR, true);
750 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
751 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
752 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
753 TEXT(bv)->Delete(bv);
754 UpdateLocal(bv, CURSOR_PAR, true);
757 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
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)->CutSelection(bv);
761 UpdateLocal(bv, CURSOR_PAR, true);
764 bv->text->FinishUndo();
765 TEXT(bv)->CopySelection(bv);
766 UpdateLocal(bv, CURSOR_PAR, false);
769 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
770 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
771 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
772 TEXT(bv)->PasteSelection(bv);
773 UpdateLocal(bv, CURSOR_PAR, true);
775 case LFUN_BREAKPARAGRAPH:
778 TEXT(bv)->BreakParagraph(bv, 0);
779 UpdateLocal(bv, FULL, true);
784 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
785 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
786 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
787 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
788 UpdateLocal(bv, CURSOR_PAR, true);
792 static LyXTextClass::size_type cur_layout = cpar(bv)->layout;
794 // Derive layout number from given argument (string)
795 // and current buffer's textclass (number). */
796 LyXTextClassList::ClassList::size_type tclass =
797 bv->buffer()->params.textclass;
798 std::pair <bool, LyXTextClass::size_type> layout =
799 textclasslist.NumberOfLayout(tclass, arg);
801 // If the entry is obsolete, use the new one instead.
803 string obs = textclasslist.Style(tclass,layout.second).
806 layout = textclasslist.NumberOfLayout(tclass, obs);
809 // see if we found the layout number:
811 string msg = string(N_("Layout ")) + arg + N_(" not known");
813 bv->owner()->getMiniBuffer()->Set(msg);
817 if (cur_layout != layout.second) {
818 cur_layout = layout.second;
819 TEXT(bv)->SetLayout(bv, layout.second);
820 bv->owner()->getToolbar()->combox->select(cpar(bv)->GetLayout()+1);
821 UpdateLocal(bv, CURSOR_PAR, true);
825 case LFUN_PARAGRAPH_SPACING:
826 // This one is absolutely not working. When fiddling with this
827 // it also seems to me that the paragraphs inside the insettext
828 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
830 LyXParagraph * par = TEXT(bv)->cursor.par();
831 Spacing::Space cur_spacing = par->spacing.getSpace();
832 float cur_value = 1.0;
833 if (cur_spacing == Spacing::Other) {
834 cur_value = par->spacing.getValue();
838 std::istringstream istr(arg);
840 istrstream istr(arg.c_str());
844 Spacing::Space new_spacing = cur_spacing;
845 float new_value = cur_value;
847 lyxerr << "Missing argument to `paragraph-spacing'"
849 } else if (tmp == "single") {
850 new_spacing = Spacing::Single;
851 } else if (tmp == "onehalf") {
852 new_spacing = Spacing::Onehalf;
853 } else if (tmp == "double") {
854 new_spacing = Spacing::Double;
855 } else if (tmp == "other") {
856 new_spacing = Spacing::Other;
859 lyxerr << "new_value = " << tmpval << endl;
862 } else if (tmp == "default") {
863 new_spacing = Spacing::Default;
865 lyxerr << _("Unknown spacing argument: ")
868 if (cur_spacing != new_spacing || cur_value != new_value) {
869 par->spacing.set(new_spacing, new_value);
870 //TEXT(bv)->RedoParagraph(owner->view());
871 UpdateLocal(bv, CURSOR_PAR, true);
872 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
878 result = UNDISPATCHED;
881 if (result != FINISHED) {
884 bv->unlockInset(this);
889 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
892 buf->latexParagraphs(os, par, 0, texrow);
893 return texrow.rows();
897 void InsetText::Validate(LaTeXFeatures & features) const
899 LyXParagraph * p = par;
901 p->validate(features);
907 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
909 if (textclasslist.Style(buf->params.textclass,
910 p->GetLayout()).labeltype != LABEL_MANUAL)
913 return p->BeginningOfMainBody();
917 void InsetText::GetCursorPos(BufferView * bv, int & x, int & y) const
924 int InsetText::InsetInInsetY()
926 if (!the_locking_inset)
929 return (inset_y + the_locking_inset->InsetInInsetY());
933 void InsetText::ToggleInsetCursor(BufferView * bv)
935 if (the_locking_inset) {
936 the_locking_inset->ToggleInsetCursor(bv);
940 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
942 int asc = lyxfont::maxAscent(font);
943 int desc = lyxfont::maxDescent(font);
946 bv->hideLockedInsetCursor();
948 bv->showLockedInsetCursor(cx(bv), cy(bv),
950 cursor_visible = !cursor_visible;
954 void InsetText::ShowInsetCursor(BufferView * bv)
956 if (the_locking_inset) {
957 the_locking_inset->ShowInsetCursor(bv);
960 if (!cursor_visible) {
961 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
963 int asc = lyxfont::maxAscent(font);
964 int desc = lyxfont::maxDescent(font);
966 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
967 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
968 cursor_visible = true;
973 void InsetText::HideInsetCursor(BufferView * bv)
975 if (cursor_visible) {
976 bv->hideLockedInsetCursor();
977 cursor_visible = false;
979 if (the_locking_inset)
980 the_locking_inset->HideInsetCursor(bv);
984 UpdatableInset::RESULT
985 InsetText::moveRight(BufferView * bv, bool activate_inset)
987 if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
989 if (activate_inset && checkAndActivateInset(bv, false))
991 TEXT(bv)->CursorRight(bv);
992 return DISPATCHED_NOUPDATE;
996 UpdatableInset::RESULT
997 InsetText::moveLeft(BufferView * bv, bool activate_inset)
999 if (!cpar(bv)->previous && (cpos(bv) <= 0))
1001 TEXT(bv)->CursorLeft(bv);
1002 if (activate_inset && checkAndActivateInset(bv, true))
1004 return DISPATCHED_NOUPDATE;
1008 UpdatableInset::RESULT
1009 InsetText::moveUp(BufferView * bv)
1011 if (!crow(bv)->previous())
1013 TEXT(bv)->CursorUp(bv);
1014 return DISPATCHED_NOUPDATE;
1018 UpdatableInset::RESULT
1019 InsetText::moveDown(BufferView * bv)
1021 if (!crow(bv)->next())
1023 TEXT(bv)->CursorDown(bv);
1024 return DISPATCHED_NOUPDATE;
1028 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1030 if (the_locking_inset) {
1031 if (the_locking_inset->InsertInsetAllowed(inset))
1032 return the_locking_inset->InsertInset(bv, inset);
1035 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1036 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1037 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
1038 if (inset->Editable() == Inset::IS_EDITABLE) {
1039 UpdatableInset * i = static_cast<UpdatableInset *>(inset);
1040 i->setOwner(static_cast<UpdatableInset *>(this));
1042 cpar(bv)->InsertInset(cpos(bv), inset);
1043 TEXT(bv)->selection = 0;
1044 UpdateLocal(bv, CURSOR_PAR, true);
1045 static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1050 UpdatableInset * InsetText::GetLockingInset()
1052 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1056 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1060 if (the_locking_inset)
1061 return the_locking_inset->GetFirstLockingInsetOfType(c);
1066 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1068 TEXT(bv)->SetFont(bv, font, toggleall);
1072 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1074 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1077 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1078 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1080 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1082 x = inset->width(bv, font);
1083 y = inset->descent(bv, font);
1087 inset_x = cx(bv) - top_x + drawTextXOffset;
1088 inset_y = cy(bv) + drawTextYOffset;
1089 inset->Edit(bv, x-inset_x, y-inset_y, 0);
1090 if (!the_locking_inset)
1092 UpdateLocal(bv, CURSOR_PAR, false);
1099 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1102 int tmp_x = x - drawTextXOffset;
1103 int tmp_y = y + insetAscent;
1104 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
1111 inset_x = cx(bv) - top_x + drawTextXOffset;
1112 inset_y = cy(bv) + drawTextYOffset;
1113 inset->Edit(bv, x - inset_x, y - inset_y, button);
1114 if (!the_locking_inset)
1116 UpdateLocal(bv, CURSOR_PAR, false);
1123 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1125 int w = UpdatableInset::getMaxWidth(pain, inset);
1128 return w - (2*TEXT_TO_INSET_OFFSET);
1132 void InsetText::SetParagraphData(LyXParagraph *p)
1146 par->SetInsetOwner(this);
1150 np->next = p->Clone();
1151 np->next->previous = np;
1153 np->SetInsetOwner(this);
1159 void InsetText::SetAutoBreakRows(bool flag)
1161 if (flag != autoBreakRows) {
1162 autoBreakRows = flag;
1168 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1170 if (how != drawFrame) {
1173 UpdateLocal(bv, DRAW_FRAME, false);
1178 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1180 if (frame_color != col) {
1183 UpdateLocal(bv, DRAW_FRAME, false);
1188 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1190 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1194 int InsetText::cx(BufferView * bv) const
1196 return TEXT(bv)->cursor.x() + top_x + 1;
1200 int InsetText::cy(BufferView * bv) const
1202 long int y_dummy = 0;
1203 Row * tmprow = TEXT(bv)->GetRowNearY(y_dummy);
1204 return TEXT(bv)->cursor.y() - tmprow->baseline();
1208 int InsetText::cpos(BufferView * bv) const
1210 return TEXT(bv)->cursor.pos();
1214 LyXParagraph * InsetText::cpar(BufferView * bv) const
1216 return TEXT(bv)->cursor.par();
1220 Row * InsetText::crow(BufferView * bv) const
1222 return TEXT(bv)->cursor.row();
1226 LyXText * InsetText::getLyXText(BufferView * bv) const
1228 if (cache.find(bv) != cache.end())
1230 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1233 if (the_locking_inset) {
1234 lt->SetCursor(bv, inset_par, inset_pos);
1240 void InsetText::deleteLyXText(BufferView * bv) const
1243 /// then remove all LyXText in text-insets
1244 LyXParagraph * p = par;
1245 for(;p;p = p->next) {
1246 p->deleteInsetsLyXText(bv);