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"
42 #include "support/textutils.h"
43 #include "support/LAssert.h"
47 #include "trans_mgr.h"
48 #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;
83 void InsetText::init(InsetText const * ins)
85 top_y = last_width = last_height = 0;
86 insetAscent = insetDescent = insetWidth = 0;
87 the_locking_inset = 0;
88 cursor_visible = false;
92 drawTextXOffset = drawTextYOffset = 0;
93 autoBreakRows = false;
97 SetParagraphData(ins->par);
98 autoBreakRows = ins->autoBreakRows;
99 drawFrame = ins->drawFrame;
101 par->SetInsetOwner(this);
102 frame_color = LColor::insetframe;
108 InsetText::~InsetText()
110 LyXParagraph * p = par->next;
120 void InsetText::clear()
122 LyXParagraph * p = par->next;
129 par = new LyXParagraph();
133 Inset * InsetText::Clone() const
135 InsetText * t = new InsetText(*this);
140 void InsetText::Write(Buffer const * buf, ostream & os) const
143 WriteParagraphData(buf, os);
147 void InsetText::WriteParagraphData(Buffer const * buf, ostream & os) const
149 par->writeFile(buf, os, buf->params, 0, 0);
153 void InsetText::Read(Buffer const * buf, LyXLex & lex)
157 LyXParagraph * return_par = 0;
158 char depth = 0; // signed or unsigned?
160 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
161 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
163 LyXFont font(LyXFont::ALL_INHERIT);
165 LyXParagraph * p = par->next;
172 par = new LyXParagraph;
175 token = lex.GetString();
178 if (token == "\\end_inset")
180 if (const_cast<Buffer*>(buf)->
181 parseSingleLyXformat2Token(lex, par, return_par,token, pos, depth,
184 , footnoteflag, footnotekind
188 // the_end read this should NEVER happen
189 lex.printError("\\the_end read in inset! Error in document!");
197 return_par->SetInsetOwner(this);
198 return_par = return_par->next;
201 if (token != "\\end_inset") {
202 lex.printError("Missing \\end_inset at this point. "
209 int InsetText::ascent(BufferView * bv, LyXFont const &) const
212 Row * row = TEXT(bv)->GetRowNearY(y_temp);
213 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
218 int InsetText::descent(BufferView * bv, LyXFont const &) const
221 Row * row = TEXT(bv)->GetRowNearY(y_temp);
222 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
223 TEXT_TO_INSET_OFFSET;
228 int InsetText::width(BufferView * bv, LyXFont const &) const
230 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
235 int InsetText::textWidth(Painter & pain) const
237 int w = getMaxWidth(pain, this);
242 void InsetText::draw(BufferView * bv, LyXFont const & f,
243 int baseline, float & x, bool cleared) const
245 Painter & pain = bv->painter();
247 // no draw is necessary !!!
248 if ((drawFrame == LOCKED) && !locked && !par->size()) {
249 if (!cleared && (need_update == CLEAR_FRAME)) {
250 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
252 insetAscent + insetDescent - 1,
256 top_baseline = baseline;
263 UpdatableInset::draw(bv, f, baseline, x, cleared);
265 if (!cleared && ((need_update==FULL) || (top_x!=int(x)) ||
266 (top_baseline!=baseline))) {
268 int h = insetAscent + insetDescent;
269 int ty = baseline - insetAscent;
275 if ((ty + h) > pain.paperHeight())
276 h = pain.paperHeight();
277 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
278 w = pain.paperWidth();
279 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
283 if (!cleared && (need_update == NONE))
286 if (top_x != int(x)) {
289 bv->text->status = LyXText::CHANGED_IN_DRAW;
293 top_baseline = baseline;
294 top_y = baseline - ascent(bv, f);
295 last_width = width(bv, f);
296 last_height = ascent(bv, f) + descent(bv, f);
298 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
299 inset_x = cx(bv) - top_x + drawTextXOffset;
300 inset_y = cy(bv) + drawTextYOffset;
302 if (!cleared && (need_update == CURSOR) && !TEXT(bv)->selection) {
307 x += TEXT_TO_INSET_OFFSET;
309 Row * row = TEXT(bv)->GetRowNearY(y);
310 y += baseline - row->ascent_of_text();
311 if (cleared || !locked || (need_update == FULL)) {
313 TEXT(bv)->GetVisibleRow(bv, y, int(x), row, y, cleared);
317 } else if (need_update == SELECTION) {
318 bv->screen()->ToggleToggle(TEXT(bv), y, int(x));
321 if (need_update == CURSOR) {
322 bv->screen()->ToggleSelection(TEXT(bv), true, y, int(x));
323 TEXT(bv)->ClearSelection();
324 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
326 bv->screen()->Update(TEXT(bv), y, int(x));
329 TEXT(bv)->refresh_y = 0;
330 TEXT(bv)->status = LyXText::UNCHANGED;
331 if ((drawFrame == ALWAYS) || ((drawFrame == LOCKED) && locked)) {
332 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
333 width(bv, f) - 1, insetAscent + insetDescent - 1,
335 } else if (need_update == CLEAR_FRAME) {
336 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
337 width(bv, f) - 1, insetAscent + insetDescent - 1,
340 x += width(bv, f) - TEXT_TO_INSET_OFFSET;
341 if (bv->text->status==LyXText::CHANGED_IN_DRAW)
343 else if (need_update != INIT)
348 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
350 if (reinit) { // && (need_update != CURSOR)) {
354 owner()->update(bv, font, true);
357 if (the_locking_inset) {
358 inset_x = cx(bv) - top_x + drawTextXOffset;
359 inset_y = cy(bv) + drawTextYOffset;
360 the_locking_inset->update(bv, font, reinit);
362 if (need_update == INIT) {
365 // if (!owner() && bv->text)
366 // bv->text->UpdateInset(bv, this);
368 int oldw = insetWidth;
370 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
371 // max(textWidth(bv->painter()),
372 // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
373 // (2 * TEXT_TO_INSET_OFFSET);
375 insetWidth = textWidth(bv->painter());
377 insetWidth = static_cast<int>(TEXT(bv)->width);
379 if (oldw != insetWidth) {
380 // printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
381 // textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
386 owner()->update(bv, font, reinit);
389 update(bv, font, reinit);
393 update(bv, font, reinit);
395 UpdateLocal(bv, INIT, false);
400 if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
403 TEXT(bv)->UpdateInset(bv, the_locking_inset);
406 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
410 Row * row = TEXT(bv)->GetRowNearY(y_temp);
411 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
412 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
413 TEXT_TO_INSET_OFFSET;
417 void InsetText::UpdateLocal(BufferView * bv, UpdateCodes what, bool mark_dirty)
419 TEXT(bv)->FullRebreak(bv);
420 if (need_update != INIT) {
421 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
423 else if (!the_locking_inset || (what != CURSOR))
426 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
428 bv->updateInset(this, mark_dirty);
429 bv->owner()->showState();
430 if (old_par != cpar(bv)) {
431 bv->owner()->setLayout(cpar(bv)->GetLayout());
437 string const InsetText::EditMessage() const
439 return _("Opened Text Inset");
443 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
445 // par->SetInsetOwner(this);
446 UpdatableInset::Edit(bv, x, y, button);
448 if (!bv->lockInset(this)) {
449 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
453 the_locking_inset = 0;
454 inset_pos = inset_x = inset_y = 0;
457 if (!checkAndActivateInset(bv, x, y, button))
458 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
459 y+TEXT(bv)->first+insetAscent);
460 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
461 bv->text->FinishUndo();
462 UpdateLocal(bv, FULL, false);
466 void InsetText::InsetUnlock(BufferView * bv)
468 if (the_locking_inset) {
469 the_locking_inset->InsetUnlock(bv);
470 the_locking_inset = 0;
473 no_selection = false;
475 TEXT(bv)->selection = 0;
476 UpdateLocal(bv, CLEAR_FRAME, false);
478 bv->owner()->setLayout(owner()->getLyXText(bv)
479 ->cursor.par()->GetLayout());
481 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
485 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
487 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
490 if (inset == cpar(bv)->GetInset(cpos(bv))) {
491 lyxerr[Debug::INSETS] << "OK" << endl;
492 the_locking_inset = inset;
493 inset_x = cx(bv) - top_x + drawTextXOffset;
494 inset_y = cy(bv) + drawTextYOffset;
495 inset_pos = cpos(bv);
496 inset_par = cpar(bv);
497 TEXT(bv)->UpdateInset(bv, the_locking_inset);
499 } else if (the_locking_inset && (the_locking_inset == inset)) {
500 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
501 lyxerr[Debug::INSETS] << "OK" << endl;
502 inset_x = cx(bv) - top_x + drawTextXOffset;
503 inset_y = cy(bv) + drawTextYOffset;
505 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
507 } else if (the_locking_inset) {
508 lyxerr[Debug::INSETS] << "MAYBE" << endl;
509 return the_locking_inset->LockInsetInInset(bv, inset);
511 lyxerr[Debug::INSETS] << "NOT OK" << endl;
516 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
519 if (!the_locking_inset)
521 if (the_locking_inset == inset) {
522 the_locking_inset->InsetUnlock(bv);
523 TEXT(bv)->UpdateInset(bv, inset);
524 the_locking_inset = 0;
526 moveRight(bv, false);
527 old_par = 0; // force layout setting
528 UpdateLocal(bv, CURSOR_PAR, false);
531 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
535 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
537 if (!the_locking_inset)
539 if (the_locking_inset != inset) {
540 TEXT(bv)->UpdateInset(bv, the_locking_inset);
541 need_update = CURSOR_PAR;
542 return the_locking_inset->UpdateInsetInInset(bv, inset);
544 // UpdateLocal(bv, FULL, false);
545 if (TEXT(bv)->UpdateInset(bv, inset))
546 UpdateLocal(bv, CURSOR_PAR, false);
547 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
548 inset_x = cx(bv) - top_x + drawTextXOffset;
549 inset_y = cy(bv) + drawTextYOffset;
555 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
557 no_selection = false;
559 int tmp_x = x - drawTextXOffset;
560 int tmp_y = y + insetAscent;
561 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
564 if (the_locking_inset) {
565 if (the_locking_inset == inset) {
566 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
569 // otherwise unlock the_locking_inset and lock the new inset
570 the_locking_inset->InsetUnlock(bv);
571 inset_x = cx(bv) - top_x + drawTextXOffset;
572 inset_y = cy(bv) + drawTextYOffset;
573 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
574 inset->Edit(bv, x - inset_x, y - inset_y, button);
575 if (the_locking_inset) {
576 UpdateLocal(bv, CURSOR_PAR, false);
580 // otherwise only unlock the_locking_inset
581 the_locking_inset->InsetUnlock(bv);
582 the_locking_inset = 0;
584 if (bv->the_locking_inset) {
585 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
586 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
587 inset_x = cx(bv) - top_x + drawTextXOffset;
588 inset_y = cy(bv) + drawTextYOffset;
589 inset_pos = cpos(bv);
590 inset_par = cpar(bv);
591 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
592 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
593 if (the_locking_inset) {
594 UpdateLocal(bv, CURSOR_PAR, false);
600 bool paste_internally = false;
601 if ((button == 2) && TEXT(bv)->selection) {
602 LocalDispatch(bv, LFUN_COPY, "");
603 paste_internally = true;
605 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
606 y+TEXT(bv)->first+insetAscent);
607 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
608 UpdateLocal(bv, CURSOR, false);
609 bv->owner()->setLayout(cpar(bv)->GetLayout());
611 // Insert primary selection with middle mouse
612 // if there is a local selection in the current buffer,
615 if (paste_internally)
616 LocalDispatch(bv, LFUN_PASTE, "");
618 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
625 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
627 UpdatableInset * inset = 0;
629 if (the_locking_inset) {
630 the_locking_inset->InsetButtonRelease(bv,
631 x - inset_x, y - inset_y,
634 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
635 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
636 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
637 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
639 inset_x = cx(bv) - top_x + drawTextXOffset;
640 inset_y = cy(bv) + drawTextYOffset;
641 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
642 inset->Edit(bv, x - inset_x, y - inset_y, button);
644 UpdateLocal(bv, CURSOR_PAR, false);
647 no_selection = false;
651 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
653 if (the_locking_inset) {
654 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
660 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
661 y+TEXT(bv)->first+insetAscent);
662 TEXT(bv)->SetSelection();
663 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
664 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
665 UpdateLocal(bv, SELECTION, false);
668 no_selection = false;
672 void InsetText::InsetKeyPress(XKeyEvent * xke)
674 if (the_locking_inset) {
675 the_locking_inset->InsetKeyPress(xke);
681 UpdatableInset::RESULT
682 InsetText::LocalDispatch(BufferView * bv,
683 int action, string const & arg)
685 no_selection = false;
686 UpdatableInset::RESULT
687 result= UpdatableInset::LocalDispatch(bv, action, arg);
688 if (result != UNDISPATCHED) {
693 if ((action < 0) && arg.empty())
696 if (the_locking_inset) {
697 result = the_locking_inset->LocalDispatch(bv, action, arg);
698 if (result == DISPATCHED_NOUPDATE)
700 else if (result == DISPATCHED) {
701 UpdateLocal(bv, CURSOR_PAR, false);
703 } else if (result == FINISHED) {
707 moveRight(bv, false);
713 the_locking_inset = 0;
721 if (bv->buffer()->isReadonly()) {
723 // setErrorMessage(N_("Document is read only"));
727 /* Automatically delete the currently selected
728 * text and replace it with what is being
729 * typed in now. Depends on lyxrc settings
730 * "auto_region_delete", which defaults to
733 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
735 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
736 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
738 bv->text->cursor.par()->previous,
739 bv->text->cursor.par()->next
742 if (lyxrc.auto_region_delete) {
743 if (TEXT(bv)->selection){
744 TEXT(bv)->CutSelection(bv, false);
747 TEXT(bv)->ClearSelection();
748 for (string::size_type i = 0; i < arg.length(); ++i) {
749 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
752 UpdateLocal(bv, CURSOR_PAR, true);
754 // --- Cursor Movements ---------------------------------------------
756 bv->text->FinishUndo();
757 moveRight(bv, false);
758 TEXT(bv)->SetSelection();
759 UpdateLocal(bv, SELECTION, false);
762 result = moveRight(bv);
763 bv->text->FinishUndo();
764 UpdateLocal(bv, CURSOR, false);
767 bv->text->FinishUndo();
769 TEXT(bv)->SetSelection();
770 UpdateLocal(bv, SELECTION, false);
773 bv->text->FinishUndo();
774 result= moveLeft(bv);
775 UpdateLocal(bv, CURSOR, false);
778 bv->text->FinishUndo();
780 TEXT(bv)->SetSelection();
781 UpdateLocal(bv, SELECTION, false);
784 bv->text->FinishUndo();
785 result = moveDown(bv);
786 UpdateLocal(bv, CURSOR, false);
789 bv->text->FinishUndo();
791 TEXT(bv)->SetSelection();
792 UpdateLocal(bv, SELECTION, false);
795 bv->text->FinishUndo();
797 UpdateLocal(bv, CURSOR, false);
800 bv->text->FinishUndo();
801 TEXT(bv)->CursorHome(bv);
802 UpdateLocal(bv, CURSOR, false);
805 TEXT(bv)->CursorEnd(bv);
806 UpdateLocal(bv, CURSOR, false);
809 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
811 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
812 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
814 bv->text->cursor.par()->previous,
815 bv->text->cursor.par()->next
818 if (TEXT(bv)->selection)
819 TEXT(bv)->CutSelection(bv);
821 TEXT(bv)->Backspace(bv);
822 UpdateLocal(bv, CURSOR_PAR, true);
825 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
827 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
828 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
830 bv->text->cursor.par()->previous,
831 bv->text->cursor.par()->next
834 if (TEXT(bv)->selection)
835 TEXT(bv)->CutSelection(bv);
837 TEXT(bv)->Delete(bv);
838 UpdateLocal(bv, CURSOR_PAR, true);
841 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
843 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
844 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
846 bv->text->cursor.par()->previous,
847 bv->text->cursor.par()->next
850 TEXT(bv)->CutSelection(bv);
851 UpdateLocal(bv, CURSOR_PAR, true);
854 bv->text->FinishUndo();
855 TEXT(bv)->CopySelection(bv);
856 UpdateLocal(bv, CURSOR_PAR, false);
858 case LFUN_PASTESELECTION:
860 string clip(bv->workarea()->getClipboard());
864 if (arg == "paragraph") {
865 TEXT(bv)->InsertStringB(bv, clip);
867 TEXT(bv)->InsertStringA(bv, clip);
869 UpdateLocal(bv, CURSOR_PAR, true);
873 if (!autoBreakRows) {
876 if (cap.nrOfParagraphs() > 1) {
877 WriteAlert(_("Impossible operation"),
878 _("Cannot include more than one paragraph!"),
883 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
885 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
886 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
888 bv->text->cursor.par()->previous,
889 bv->text->cursor.par()->next
892 TEXT(bv)->PasteSelection(bv);
893 UpdateLocal(bv, CURSOR_PAR, true);
895 case LFUN_BREAKPARAGRAPH:
898 TEXT(bv)->BreakParagraph(bv, 0);
899 UpdateLocal(bv, FULL, true);
904 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
906 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
907 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
909 bv->text->cursor.par()->previous,
910 bv->text->cursor.par()->next
913 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
914 UpdateLocal(bv, CURSOR_PAR, true);
918 static LyXTextClass::size_type cur_layout = cpar(bv)->layout;
920 // Derive layout number from given argument (string)
921 // and current buffer's textclass (number). */
922 LyXTextClassList::ClassList::size_type tclass =
923 bv->buffer()->params.textclass;
924 std::pair <bool, LyXTextClass::size_type> layout =
925 textclasslist.NumberOfLayout(tclass, arg);
927 // If the entry is obsolete, use the new one instead.
929 string obs = textclasslist.Style(tclass,layout.second).
932 layout = textclasslist.NumberOfLayout(tclass, obs);
935 // see if we found the layout number:
937 string msg = string(N_("Layout ")) + arg + N_(" not known");
939 bv->owner()->getMiniBuffer()->Set(msg);
943 if (cur_layout != layout.second) {
944 cur_layout = layout.second;
945 TEXT(bv)->SetLayout(bv, layout.second);
946 bv->owner()->setLayout(cpar(bv)->GetLayout());
947 UpdateLocal(bv, CURSOR_PAR, true);
951 case LFUN_PARAGRAPH_SPACING:
952 // This one is absolutely not working. When fiddling with this
953 // it also seems to me that the paragraphs inside the insettext
954 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
956 LyXParagraph * par = TEXT(bv)->cursor.par();
957 Spacing::Space cur_spacing = par->spacing.getSpace();
958 float cur_value = 1.0;
959 if (cur_spacing == Spacing::Other) {
960 cur_value = par->spacing.getValue();
963 std::istringstream istr(arg.c_str());
966 Spacing::Space new_spacing = cur_spacing;
967 float new_value = cur_value;
969 lyxerr << "Missing argument to `paragraph-spacing'"
971 } else if (tmp == "single") {
972 new_spacing = Spacing::Single;
973 } else if (tmp == "onehalf") {
974 new_spacing = Spacing::Onehalf;
975 } else if (tmp == "double") {
976 new_spacing = Spacing::Double;
977 } else if (tmp == "other") {
978 new_spacing = Spacing::Other;
981 lyxerr << "new_value = " << tmpval << endl;
984 } else if (tmp == "default") {
985 new_spacing = Spacing::Default;
987 lyxerr << _("Unknown spacing argument: ")
990 if (cur_spacing != new_spacing || cur_value != new_value) {
991 par->spacing.set(new_spacing, new_value);
992 //TEXT(bv)->RedoParagraph(owner->view());
993 UpdateLocal(bv, CURSOR_PAR, true);
994 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1000 result = UNDISPATCHED;
1003 if (result != FINISHED) {
1004 ShowInsetCursor(bv);
1006 bv->unlockInset(this);
1011 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1014 buf->latexParagraphs(os, par, 0, texrow);
1015 return texrow.rows();
1019 void InsetText::Validate(LaTeXFeatures & features) const
1021 LyXParagraph * p = par;
1023 p->validate(features);
1029 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1031 if (textclasslist.Style(buf->params.textclass,
1032 p->GetLayout()).labeltype != LABEL_MANUAL)
1035 return p->BeginningOfMainBody();
1039 void InsetText::GetCursorPos(BufferView * bv, int & x, int & y) const
1046 int InsetText::InsetInInsetY()
1048 if (!the_locking_inset)
1051 return (inset_y + the_locking_inset->InsetInInsetY());
1055 void InsetText::ToggleInsetCursor(BufferView * bv)
1057 if (the_locking_inset) {
1058 the_locking_inset->ToggleInsetCursor(bv);
1062 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1064 int asc = lyxfont::maxAscent(font);
1065 int desc = lyxfont::maxDescent(font);
1068 bv->hideLockedInsetCursor();
1070 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1071 cursor_visible = !cursor_visible;
1075 void InsetText::ShowInsetCursor(BufferView * bv)
1077 if (the_locking_inset) {
1078 the_locking_inset->ShowInsetCursor(bv);
1081 if (!cursor_visible) {
1082 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1084 int asc = lyxfont::maxAscent(font);
1085 int desc = lyxfont::maxDescent(font);
1087 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1088 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1089 cursor_visible = true;
1094 void InsetText::HideInsetCursor(BufferView * bv)
1096 if (cursor_visible) {
1097 bv->hideLockedInsetCursor();
1098 cursor_visible = false;
1100 if (the_locking_inset)
1101 the_locking_inset->HideInsetCursor(bv);
1105 UpdatableInset::RESULT
1106 InsetText::moveRight(BufferView * bv, bool activate_inset)
1108 if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1110 if (activate_inset && checkAndActivateInset(bv, false))
1112 TEXT(bv)->CursorRight(bv);
1113 return DISPATCHED_NOUPDATE;
1117 UpdatableInset::RESULT
1118 InsetText::moveLeft(BufferView * bv, bool activate_inset)
1120 if (!cpar(bv)->previous && (cpos(bv) <= 0))
1122 TEXT(bv)->CursorLeft(bv);
1123 if (activate_inset && checkAndActivateInset(bv, true))
1125 return DISPATCHED_NOUPDATE;
1129 UpdatableInset::RESULT
1130 InsetText::moveUp(BufferView * bv)
1132 if (!crow(bv)->previous())
1134 TEXT(bv)->CursorUp(bv);
1135 return DISPATCHED_NOUPDATE;
1139 UpdatableInset::RESULT
1140 InsetText::moveDown(BufferView * bv)
1142 if (!crow(bv)->next())
1144 TEXT(bv)->CursorDown(bv);
1145 return DISPATCHED_NOUPDATE;
1149 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1151 if (the_locking_inset) {
1152 if (the_locking_inset->InsertInsetAllowed(inset))
1153 return the_locking_inset->InsertInset(bv, inset);
1156 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1158 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1159 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1161 bv->text->cursor.par()->previous,
1162 bv->text->cursor.par()->next
1165 if (inset->Editable() == Inset::IS_EDITABLE) {
1166 UpdatableInset * i = static_cast<UpdatableInset *>(inset);
1167 i->setOwner(static_cast<UpdatableInset *>(this));
1169 TEXT(bv)->InsertInset(bv, inset);
1170 TEXT(bv)->selection = 0;
1171 UpdateLocal(bv, CURSOR_PAR, true);
1172 static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1177 UpdatableInset * InsetText::GetLockingInset()
1179 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1183 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1187 if (the_locking_inset)
1188 return the_locking_inset->GetFirstLockingInsetOfType(c);
1193 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1195 TEXT(bv)->SetFont(bv, font, toggleall);
1199 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1201 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1204 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1205 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1207 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1209 x = inset->width(bv, font);
1210 y = inset->descent(bv, font);
1214 inset_x = cx(bv) - top_x + drawTextXOffset;
1215 inset_y = cy(bv) + drawTextYOffset;
1216 inset->Edit(bv, x-inset_x, y-inset_y, 0);
1217 if (!the_locking_inset)
1219 UpdateLocal(bv, CURSOR_PAR, false);
1226 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1229 int tmp_x = x - drawTextXOffset;
1230 int tmp_y = y + insetAscent;
1231 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
1238 inset_x = cx(bv) - top_x + drawTextXOffset;
1239 inset_y = cy(bv) + drawTextYOffset;
1240 inset->Edit(bv, x - inset_x, y - inset_y, button);
1241 if (!the_locking_inset)
1243 UpdateLocal(bv, CURSOR_PAR, false);
1250 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1252 int w = UpdatableInset::getMaxWidth(pain, inset);
1257 w = w - top_x + owner()->x();
1260 w -= (2 * TEXT_TO_INSET_OFFSET);
1262 // return w - (2*TEXT_TO_INSET_OFFSET);
1266 void InsetText::SetParagraphData(LyXParagraph *p)
1280 par->SetInsetOwner(this);
1284 np->next = p->Clone();
1285 np->next->previous = np;
1287 np->SetInsetOwner(this);
1293 void InsetText::SetAutoBreakRows(bool flag)
1295 if (flag != autoBreakRows) {
1296 autoBreakRows = flag;
1304 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1306 if (how != drawFrame) {
1309 UpdateLocal(bv, DRAW_FRAME, false);
1314 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1316 if (frame_color != col) {
1319 UpdateLocal(bv, DRAW_FRAME, false);
1324 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1326 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1330 int InsetText::cx(BufferView * bv) const
1332 return TEXT(bv)->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1336 int InsetText::cy(BufferView * bv) const
1339 return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1343 int InsetText::cpos(BufferView * bv) const
1345 return TEXT(bv)->cursor.pos();
1349 LyXParagraph * InsetText::cpar(BufferView * bv) const
1351 return TEXT(bv)->cursor.par();
1355 Row * InsetText::crow(BufferView * bv) const
1357 return TEXT(bv)->cursor.row();
1361 LyXText * InsetText::getLyXText(BufferView * bv) const
1363 if (cache.find(bv) != cache.end())
1365 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1368 if (the_locking_inset) {
1369 lt->SetCursor(bv, inset_par, inset_pos);
1375 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1379 /// then remove all LyXText in text-insets
1380 LyXParagraph * p = par;
1381 for(;p;p = p->next) {
1382 p->deleteInsetsLyXText(bv);
1388 void InsetText::resizeLyXText(BufferView * bv) const
1390 if (!par->next && !par->size()) // resize not neccessary!
1392 if (cache.find(bv) == cache.end())
1395 LyXParagraph * lpar = 0;
1396 LyXParagraph * selstartpar = 0;
1397 LyXParagraph * selendpar = 0;
1399 int selstartpos = 0;
1404 // ProhibitInput(bv);
1407 lpar = TEXT(bv)->cursor.par();
1408 pos = TEXT(bv)->cursor.pos();
1409 selstartpar = TEXT(bv)->sel_start_cursor.par();
1410 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1411 selendpar = TEXT(bv)->sel_end_cursor.par();
1412 selendpos = TEXT(bv)->sel_end_cursor.pos();
1413 selection = TEXT(bv)->selection;
1414 mark_set = TEXT(bv)->mark_set;
1416 deleteLyXText(bv, (the_locking_inset == 0));
1419 TEXT(bv)->selection = true;
1420 /* at this point just to avoid the Delete-Empty-Paragraph
1421 * Mechanism when setting the cursor */
1422 TEXT(bv)->mark_set = mark_set;
1424 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos);
1425 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1426 TEXT(bv)->SetCursor(bv, selendpar, selendpos);
1427 TEXT(bv)->SetSelection();
1428 TEXT(bv)->SetCursor(bv, lpar, pos);
1430 TEXT(bv)->SetCursor(bv, lpar, pos);
1431 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1432 TEXT(bv)->selection = false;
1436 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1437 // this will scroll the screen such that the cursor becomes visible
1438 bv->updateScrollbar();
1440 if (the_locking_inset) {
1441 /// then resize all LyXText in text-insets
1442 inset_x = cx(bv) - top_x + drawTextXOffset;
1443 inset_y = cy(bv) + drawTextYOffset;
1444 for(LyXParagraph * p = par; p; p = p->next) {
1445 p->resizeInsetsLyXText(bv);
1452 void InsetText::removeNewlines()
1454 for(LyXParagraph * p = par; p; p = p->next) {
1455 for(int i = 0; i < p->Last(); ++i) {
1456 if (p->GetChar(i) == LyXParagraph::META_NEWLINE)