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)
91 the_locking_inset = 0;
92 cursor_visible = false;
98 autoBreakRows = false;
102 SetParagraphData(ins->par);
103 autoBreakRows = ins->autoBreakRows;
104 drawFrame = ins->drawFrame;
106 par->SetInsetOwner(this);
107 frame_color = LColor::insetframe;
113 InsetText::~InsetText()
115 for(Cache::const_iterator cit=cache.begin(); cit != cache.end(); ++cit)
116 delete (*cit).second;
117 // deleteLyXText((*cit).first);
118 LyXParagraph * p = par->next;
128 void InsetText::clear()
130 LyXParagraph * p = par->next;
137 par = new LyXParagraph();
141 Inset * InsetText::Clone() const
143 InsetText * t = new InsetText(*this);
148 void InsetText::Write(Buffer const * buf, ostream & os) const
151 WriteParagraphData(buf, os);
155 void InsetText::WriteParagraphData(Buffer const * buf, ostream & os) const
157 par->writeFile(buf, os, buf->params, 0, 0);
161 void InsetText::Read(Buffer const * buf, LyXLex & lex)
165 LyXParagraph * return_par = 0;
166 char depth = 0; // signed or unsigned?
168 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
169 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
171 LyXFont font(LyXFont::ALL_INHERIT);
173 LyXParagraph * p = par->next;
180 par = new LyXParagraph;
183 token = lex.GetString();
186 if (token == "\\end_inset")
188 if (const_cast<Buffer*>(buf)->
189 parseSingleLyXformat2Token(lex, par, return_par,token, pos, depth,
192 , footnoteflag, footnotekind
196 // the_end read this should NEVER happen
197 lex.printError("\\the_end read in inset! Error in document!");
205 return_par->SetInsetOwner(this);
206 return_par = return_par->next;
209 if (token != "\\end_inset") {
210 lex.printError("Missing \\end_inset at this point. "
217 int InsetText::ascent(BufferView * bv, LyXFont const &) const
220 Row * row = TEXT(bv)->GetRowNearY(y_temp);
221 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
226 int InsetText::descent(BufferView * bv, LyXFont const &) const
229 Row * row = TEXT(bv)->GetRowNearY(y_temp);
230 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
231 TEXT_TO_INSET_OFFSET;
236 int InsetText::width(BufferView * bv, LyXFont const &) const
238 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
243 int InsetText::textWidth(Painter & pain) const
245 int w = getMaxWidth(pain, this);
250 void InsetText::draw(BufferView * bv, LyXFont const & f,
251 int baseline, float & x, bool cleared) const
253 Painter & pain = bv->painter();
255 // no draw is necessary !!!
256 if ((drawFrame == LOCKED) && !locked && !par->size()) {
257 if (!cleared && (need_update == CLEAR_FRAME)) {
258 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
260 insetAscent + insetDescent - 1,
264 top_baseline = baseline;
271 UpdatableInset::draw(bv, f, baseline, x, cleared);
273 if (!cleared && ((need_update==FULL) || (top_x!=int(x)) ||
274 (top_baseline!=baseline))) {
276 unsigned int h = insetAscent + insetDescent;
277 int ty = baseline - insetAscent;
283 if ((ty + h) > pain.paperHeight())
284 h = pain.paperHeight();
285 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
286 w = pain.paperWidth();
287 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
291 if (!cleared && (need_update == NONE))
294 if (top_x != int(x)) {
297 bv->text->status = LyXText::CHANGED_IN_DRAW;
301 top_baseline = baseline;
302 top_y = baseline - ascent(bv, f);
303 last_width = width(bv, f);
304 last_height = ascent(bv, f) + descent(bv, f);
306 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
307 inset_x = cx(bv) - top_x + drawTextXOffset;
308 inset_y = cy(bv) + drawTextYOffset;
310 if (!cleared && (need_update == CURSOR) && !TEXT(bv)->selection) {
315 x += TEXT_TO_INSET_OFFSET;
317 Row * row = TEXT(bv)->GetRowNearY(y);
318 y += baseline - row->ascent_of_text();
319 if (cleared || !locked || (need_update == FULL)) {
321 TEXT(bv)->GetVisibleRow(bv, int(y), int(x), row, y, cleared);
325 } else if (need_update == SELECTION) {
326 bv->screen()->ToggleToggle(TEXT(bv), int(y), int(x));
329 if (need_update == CURSOR) {
330 bv->screen()->ToggleSelection(TEXT(bv), true, int(y), int(x));
331 TEXT(bv)->ClearSelection();
332 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
334 bv->screen()->Update(TEXT(bv), int(y), int(x));
337 TEXT(bv)->refresh_y = 0;
338 TEXT(bv)->status = LyXText::UNCHANGED;
339 if ((drawFrame == ALWAYS) || ((drawFrame == LOCKED) && locked)) {
340 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
341 width(bv, f) - 1, insetAscent + insetDescent - 1,
343 } else if (need_update == CLEAR_FRAME) {
344 pain.rectangle(top_x + 1, baseline - insetAscent + 1,
345 width(bv, f) - 1, insetAscent + insetDescent - 1,
348 x += width(bv, f) - TEXT_TO_INSET_OFFSET;
349 if (bv->text->status==LyXText::CHANGED_IN_DRAW)
351 else if (need_update != INIT)
356 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
358 if (reinit) { // && (need_update != CURSOR)) {
362 owner()->update(bv, font, true);
365 if (the_locking_inset) {
366 inset_x = cx(bv) - top_x + drawTextXOffset;
367 inset_y = cy(bv) + drawTextYOffset;
368 the_locking_inset->update(bv, font, reinit);
370 if (need_update == INIT) {
373 // if (!owner() && bv->text)
374 // bv->text->UpdateInset(bv, this);
376 int oldw = insetWidth;
378 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
379 // max(textWidth(bv->painter()),
380 // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
381 // (2 * TEXT_TO_INSET_OFFSET);
383 insetWidth = textWidth(bv->painter());
385 insetWidth = static_cast<int>(TEXT(bv)->width);
387 if (oldw != insetWidth) {
388 // printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
389 // textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
394 owner()->update(bv, font, reinit);
397 update(bv, font, reinit);
401 update(bv, font, reinit);
403 UpdateLocal(bv, INIT, false);
408 if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
411 TEXT(bv)->UpdateInset(bv, the_locking_inset);
414 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
418 Row * row = TEXT(bv)->GetRowNearY(y_temp);
419 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
420 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
421 TEXT_TO_INSET_OFFSET;
425 void InsetText::UpdateLocal(BufferView * bv, UpdateCodes what, bool mark_dirty)
427 TEXT(bv)->FullRebreak(bv);
428 if (need_update != INIT) {
429 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
431 else if (!the_locking_inset || (what != CURSOR))
434 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
436 bv->updateInset(this, mark_dirty);
437 bv->owner()->showState();
438 if (old_par != cpar(bv)) {
439 bv->owner()->setLayout(cpar(bv)->GetLayout());
445 string const InsetText::EditMessage() const
447 return _("Opened Text Inset");
451 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
453 // par->SetInsetOwner(this);
454 UpdatableInset::Edit(bv, x, y, button);
456 if (!bv->lockInset(this)) {
457 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
461 the_locking_inset = 0;
462 inset_pos = inset_x = inset_y = 0;
465 if (!checkAndActivateInset(bv, x, y, button))
466 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
467 y+TEXT(bv)->first+insetAscent);
468 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
469 bv->text->FinishUndo();
470 UpdateLocal(bv, FULL, false);
474 void InsetText::InsetUnlock(BufferView * bv)
476 if (the_locking_inset) {
477 the_locking_inset->InsetUnlock(bv);
478 the_locking_inset = 0;
481 no_selection = false;
483 TEXT(bv)->selection = 0;
484 UpdateLocal(bv, CLEAR_FRAME, false);
486 bv->owner()->setLayout(owner()->getLyXText(bv)
487 ->cursor.par()->GetLayout());
489 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
493 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
495 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
498 if (inset == cpar(bv)->GetInset(cpos(bv))) {
499 lyxerr[Debug::INSETS] << "OK" << endl;
500 the_locking_inset = inset;
501 inset_x = cx(bv) - top_x + drawTextXOffset;
502 inset_y = cy(bv) + drawTextYOffset;
503 inset_pos = cpos(bv);
504 inset_par = cpar(bv);
505 TEXT(bv)->UpdateInset(bv, the_locking_inset);
507 } else if (the_locking_inset && (the_locking_inset == inset)) {
508 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
509 lyxerr[Debug::INSETS] << "OK" << endl;
510 inset_x = cx(bv) - top_x + drawTextXOffset;
511 inset_y = cy(bv) + drawTextYOffset;
513 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
515 } else if (the_locking_inset) {
516 lyxerr[Debug::INSETS] << "MAYBE" << endl;
517 return the_locking_inset->LockInsetInInset(bv, inset);
519 lyxerr[Debug::INSETS] << "NOT OK" << endl;
524 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
527 if (!the_locking_inset)
529 if (the_locking_inset == inset) {
530 the_locking_inset->InsetUnlock(bv);
531 TEXT(bv)->UpdateInset(bv, inset);
532 the_locking_inset = 0;
534 moveRight(bv, false);
535 old_par = 0; // force layout setting
536 UpdateLocal(bv, CURSOR_PAR, false);
539 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
543 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
545 if (!the_locking_inset)
547 if (the_locking_inset != inset) {
548 TEXT(bv)->UpdateInset(bv, the_locking_inset);
549 need_update = CURSOR_PAR;
550 return the_locking_inset->UpdateInsetInInset(bv, inset);
552 // UpdateLocal(bv, FULL, false);
553 if (TEXT(bv)->UpdateInset(bv, inset))
554 UpdateLocal(bv, CURSOR_PAR, false);
555 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
556 inset_x = cx(bv) - top_x + drawTextXOffset;
557 inset_y = cy(bv) + drawTextYOffset;
563 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
565 no_selection = false;
567 int tmp_x = x - drawTextXOffset;
568 int tmp_y = y + insetAscent;
569 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
572 if (the_locking_inset) {
573 if (the_locking_inset == inset) {
574 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
577 // otherwise unlock the_locking_inset and lock the new inset
578 the_locking_inset->InsetUnlock(bv);
579 inset_x = cx(bv) - top_x + drawTextXOffset;
580 inset_y = cy(bv) + drawTextYOffset;
581 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
582 inset->Edit(bv, x - inset_x, y - inset_y, button);
583 if (the_locking_inset) {
584 UpdateLocal(bv, CURSOR_PAR, false);
588 // otherwise only unlock the_locking_inset
589 the_locking_inset->InsetUnlock(bv);
590 the_locking_inset = 0;
592 if (bv->the_locking_inset) {
593 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
594 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
595 inset_x = cx(bv) - top_x + drawTextXOffset;
596 inset_y = cy(bv) + drawTextYOffset;
597 inset_pos = cpos(bv);
598 inset_par = cpar(bv);
599 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
600 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
601 if (the_locking_inset) {
602 UpdateLocal(bv, CURSOR_PAR, false);
608 bool paste_internally = false;
609 if ((button == 2) && TEXT(bv)->selection) {
610 LocalDispatch(bv, LFUN_COPY, "");
611 paste_internally = true;
613 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
614 y+TEXT(bv)->first+insetAscent);
615 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
616 UpdateLocal(bv, CURSOR, false);
617 bv->owner()->setLayout(cpar(bv)->GetLayout());
619 // Insert primary selection with middle mouse
620 // if there is a local selection in the current buffer,
623 if (paste_internally)
624 LocalDispatch(bv, LFUN_PASTE, "");
626 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
633 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
635 UpdatableInset * inset = 0;
637 if (the_locking_inset) {
638 the_locking_inset->InsetButtonRelease(bv,
639 x - inset_x, y - inset_y,
642 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
643 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
644 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
645 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
647 inset_x = cx(bv) - top_x + drawTextXOffset;
648 inset_y = cy(bv) + drawTextYOffset;
649 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
650 inset->Edit(bv, x - inset_x, y - inset_y, button);
652 UpdateLocal(bv, CURSOR_PAR, false);
655 no_selection = false;
659 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
661 if (the_locking_inset) {
662 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
668 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
669 y+TEXT(bv)->first+insetAscent);
670 TEXT(bv)->SetSelection();
671 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
672 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
673 UpdateLocal(bv, SELECTION, false);
676 no_selection = false;
680 void InsetText::InsetKeyPress(XKeyEvent * xke)
682 if (the_locking_inset) {
683 the_locking_inset->InsetKeyPress(xke);
689 UpdatableInset::RESULT
690 InsetText::LocalDispatch(BufferView * bv,
691 int action, string const & arg)
693 no_selection = false;
694 UpdatableInset::RESULT
695 result= UpdatableInset::LocalDispatch(bv, action, arg);
696 if (result != UNDISPATCHED) {
701 if ((action < 0) && arg.empty())
704 if (the_locking_inset) {
705 result = the_locking_inset->LocalDispatch(bv, action, arg);
706 if (result == DISPATCHED_NOUPDATE)
708 else if (result == DISPATCHED) {
709 UpdateLocal(bv, CURSOR_PAR, false);
711 } else if (result == FINISHED) {
715 moveRight(bv, false);
721 the_locking_inset = 0;
729 if (bv->buffer()->isReadonly()) {
731 // setErrorMessage(N_("Document is read only"));
735 /* Automatically delete the currently selected
736 * text and replace it with what is being
737 * typed in now. Depends on lyxrc settings
738 * "auto_region_delete", which defaults to
741 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
743 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
744 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
746 bv->text->cursor.par()->previous,
747 bv->text->cursor.par()->next
750 if (lyxrc.auto_region_delete) {
751 if (TEXT(bv)->selection){
752 TEXT(bv)->CutSelection(bv, false);
755 TEXT(bv)->ClearSelection();
756 for (string::size_type i = 0; i < arg.length(); ++i) {
757 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
760 UpdateLocal(bv, CURSOR_PAR, true);
762 // --- Cursor Movements ---------------------------------------------
764 bv->text->FinishUndo();
765 moveRight(bv, false, true);
766 TEXT(bv)->SetSelection();
767 UpdateLocal(bv, SELECTION, false);
770 result = moveRight(bv);
771 bv->text->FinishUndo();
772 UpdateLocal(bv, CURSOR, false);
775 bv->text->FinishUndo();
776 moveLeft(bv, false, true);
777 TEXT(bv)->SetSelection();
778 UpdateLocal(bv, SELECTION, false);
781 bv->text->FinishUndo();
782 result= moveLeft(bv);
783 UpdateLocal(bv, CURSOR, false);
786 bv->text->FinishUndo();
788 TEXT(bv)->SetSelection();
789 UpdateLocal(bv, SELECTION, false);
792 bv->text->FinishUndo();
793 result = moveDown(bv);
794 UpdateLocal(bv, CURSOR, false);
797 bv->text->FinishUndo();
799 TEXT(bv)->SetSelection();
800 UpdateLocal(bv, SELECTION, false);
803 bv->text->FinishUndo();
805 UpdateLocal(bv, CURSOR, false);
808 bv->text->FinishUndo();
809 TEXT(bv)->CursorHome(bv);
810 UpdateLocal(bv, CURSOR, false);
813 TEXT(bv)->CursorEnd(bv);
814 UpdateLocal(bv, CURSOR, false);
817 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
819 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
820 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
822 bv->text->cursor.par()->previous,
823 bv->text->cursor.par()->next
826 if (TEXT(bv)->selection)
827 TEXT(bv)->CutSelection(bv);
829 TEXT(bv)->Backspace(bv);
830 UpdateLocal(bv, CURSOR_PAR, true);
833 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
835 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
836 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
838 bv->text->cursor.par()->previous,
839 bv->text->cursor.par()->next
842 if (TEXT(bv)->selection)
843 TEXT(bv)->CutSelection(bv);
845 TEXT(bv)->Delete(bv);
846 UpdateLocal(bv, CURSOR_PAR, true);
849 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
851 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
852 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
854 bv->text->cursor.par()->previous,
855 bv->text->cursor.par()->next
858 TEXT(bv)->CutSelection(bv);
859 UpdateLocal(bv, CURSOR_PAR, true);
862 bv->text->FinishUndo();
863 TEXT(bv)->CopySelection(bv);
864 UpdateLocal(bv, CURSOR_PAR, false);
866 case LFUN_PASTESELECTION:
868 string clip(bv->workarea()->getClipboard());
872 if (arg == "paragraph") {
873 TEXT(bv)->InsertStringB(bv, clip);
875 TEXT(bv)->InsertStringA(bv, clip);
877 UpdateLocal(bv, CURSOR_PAR, true);
881 if (!autoBreakRows) {
884 if (cap.nrOfParagraphs() > 1) {
885 WriteAlert(_("Impossible operation"),
886 _("Cannot include more than one paragraph!"),
891 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
893 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
894 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
896 bv->text->cursor.par()->previous,
897 bv->text->cursor.par()->next
900 TEXT(bv)->PasteSelection(bv);
901 UpdateLocal(bv, CURSOR_PAR, true);
903 case LFUN_BREAKPARAGRAPH:
906 TEXT(bv)->BreakParagraph(bv, 0);
907 UpdateLocal(bv, FULL, true);
912 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
914 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
915 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
917 bv->text->cursor.par()->previous,
918 bv->text->cursor.par()->next
921 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
922 UpdateLocal(bv, CURSOR_PAR, true);
925 // do not set layouts on non breakable textinsets
927 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
929 // Derive layout number from given argument (string)
930 // and current buffer's textclass (number). */
931 LyXTextClassList::ClassList::size_type tclass =
932 bv->buffer()->params.textclass;
933 std::pair <bool, LyXTextClass::size_type> layout =
934 textclasslist.NumberOfLayout(tclass, arg);
936 // If the entry is obsolete, use the new one instead.
938 string obs = textclasslist.Style(tclass,layout.second).
941 layout = textclasslist.NumberOfLayout(tclass, obs);
944 // see if we found the layout number:
946 string msg = string(N_("Layout ")) + arg + N_(" not known");
948 bv->owner()->getMiniBuffer()->Set(msg);
952 if (cur_layout != layout.second) {
953 cur_layout = layout.second;
954 TEXT(bv)->SetLayout(bv, layout.second);
955 bv->owner()->setLayout(cpar(bv)->GetLayout());
956 UpdateLocal(bv, CURSOR_PAR, true);
959 // reset the layout box
960 bv->owner()->setLayout(cpar(bv)->GetLayout());
963 case LFUN_PARAGRAPH_SPACING:
964 // This one is absolutely not working. When fiddling with this
965 // it also seems to me that the paragraphs inside the insettext
966 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
968 LyXParagraph * par = TEXT(bv)->cursor.par();
969 Spacing::Space cur_spacing = par->spacing.getSpace();
970 float cur_value = 1.0;
971 if (cur_spacing == Spacing::Other) {
972 cur_value = par->spacing.getValue();
975 std::istringstream istr(arg.c_str());
978 Spacing::Space new_spacing = cur_spacing;
979 float new_value = cur_value;
981 lyxerr << "Missing argument to `paragraph-spacing'"
983 } else if (tmp == "single") {
984 new_spacing = Spacing::Single;
985 } else if (tmp == "onehalf") {
986 new_spacing = Spacing::Onehalf;
987 } else if (tmp == "double") {
988 new_spacing = Spacing::Double;
989 } else if (tmp == "other") {
990 new_spacing = Spacing::Other;
993 lyxerr << "new_value = " << tmpval << endl;
996 } else if (tmp == "default") {
997 new_spacing = Spacing::Default;
999 lyxerr << _("Unknown spacing argument: ")
1002 if (cur_spacing != new_spacing || cur_value != new_value) {
1003 par->spacing.set(new_spacing, new_value);
1004 //TEXT(bv)->RedoParagraph(owner->view());
1005 UpdateLocal(bv, CURSOR_PAR, true);
1006 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1012 result = UNDISPATCHED;
1015 if (result != FINISHED) {
1016 ShowInsetCursor(bv);
1018 bv->unlockInset(this);
1023 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1026 buf->latexParagraphs(os, par, 0, texrow);
1027 return texrow.rows();
1031 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1033 LyXParagraph * p = par;
1034 unsigned int lines = 0;
1037 string const tmp = buf->asciiParagraph(p, linelen);
1038 lines += countChar(tmp, '\n');
1048 void InsetText::Validate(LaTeXFeatures & features) const
1050 LyXParagraph * p = par;
1052 p->validate(features);
1058 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1060 if (textclasslist.Style(buf->params.textclass,
1061 p->GetLayout()).labeltype != LABEL_MANUAL)
1064 return p->BeginningOfMainBody();
1068 void InsetText::GetCursorPos(BufferView * bv,
1069 int & x, int & y) const
1076 unsigned int InsetText::InsetInInsetY()
1078 if (!the_locking_inset)
1081 return (inset_y + the_locking_inset->InsetInInsetY());
1085 void InsetText::ToggleInsetCursor(BufferView * bv)
1087 if (the_locking_inset) {
1088 the_locking_inset->ToggleInsetCursor(bv);
1092 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1094 int asc = lyxfont::maxAscent(font);
1095 int desc = lyxfont::maxDescent(font);
1098 bv->hideLockedInsetCursor();
1100 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1101 cursor_visible = !cursor_visible;
1105 void InsetText::ShowInsetCursor(BufferView * bv)
1107 if (the_locking_inset) {
1108 the_locking_inset->ShowInsetCursor(bv);
1111 if (!cursor_visible) {
1112 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1114 int asc = lyxfont::maxAscent(font);
1115 int desc = lyxfont::maxDescent(font);
1117 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1118 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1119 cursor_visible = true;
1124 void InsetText::HideInsetCursor(BufferView * bv)
1126 if (cursor_visible) {
1127 bv->hideLockedInsetCursor();
1128 cursor_visible = false;
1130 if (the_locking_inset)
1131 the_locking_inset->HideInsetCursor(bv);
1135 UpdatableInset::RESULT
1136 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1138 if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1140 if (activate_inset && checkAndActivateInset(bv, false))
1142 TEXT(bv)->CursorRight(bv, selecting);
1143 return DISPATCHED_NOUPDATE;
1147 UpdatableInset::RESULT
1148 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1150 if (!cpar(bv)->previous && (cpos(bv) <= 0))
1152 TEXT(bv)->CursorLeft(bv, selecting);
1153 if (activate_inset && checkAndActivateInset(bv, true))
1155 return DISPATCHED_NOUPDATE;
1159 UpdatableInset::RESULT
1160 InsetText::moveUp(BufferView * bv)
1162 if (!crow(bv)->previous())
1164 TEXT(bv)->CursorUp(bv);
1165 return DISPATCHED_NOUPDATE;
1169 UpdatableInset::RESULT
1170 InsetText::moveDown(BufferView * bv)
1172 if (!crow(bv)->next())
1174 TEXT(bv)->CursorDown(bv);
1175 return DISPATCHED_NOUPDATE;
1179 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1181 if (the_locking_inset) {
1182 if (the_locking_inset->InsertInsetAllowed(inset))
1183 return the_locking_inset->InsertInset(bv, inset);
1186 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1188 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1189 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1191 bv->text->cursor.par()->previous,
1192 bv->text->cursor.par()->next
1195 if (inset->Editable() == Inset::IS_EDITABLE) {
1196 UpdatableInset * i = static_cast<UpdatableInset *>(inset);
1197 i->setOwner(static_cast<UpdatableInset *>(this));
1199 HideInsetCursor(bv);
1200 TEXT(bv)->InsertInset(bv, inset);
1201 TEXT(bv)->selection = 0;
1202 bv->fitCursor(TEXT(bv));
1203 UpdateLocal(bv, CURSOR_PAR, true);
1204 static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1205 ShowInsetCursor(bv);
1210 UpdatableInset * InsetText::GetLockingInset()
1212 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1216 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1220 if (the_locking_inset)
1221 return the_locking_inset->GetFirstLockingInsetOfType(c);
1226 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1228 TEXT(bv)->SetFont(bv, font, toggleall);
1232 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1234 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1238 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1239 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1241 LyXFont const font =
1242 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1244 x = inset->width(bv, font);
1245 y = inset->descent(bv, font);
1249 inset_x = cx(bv) - top_x + drawTextXOffset;
1250 inset_y = cy(bv) + drawTextYOffset;
1251 inset->Edit(bv, x - inset_x, y - inset_y, 0);
1252 if (!the_locking_inset)
1254 UpdateLocal(bv, CURSOR_PAR, false);
1261 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1264 x = x - drawTextXOffset;
1265 y = y + insetAscent;
1266 Inset * inset = bv->checkInsetHit(TEXT(bv), x, y, button);
1273 inset_x = cx(bv) - top_x + drawTextXOffset;
1274 inset_y = cy(bv) + drawTextYOffset;
1275 inset->Edit(bv, x - inset_x, y - inset_y, button);
1276 if (!the_locking_inset)
1278 UpdateLocal(bv, CURSOR_PAR, false);
1285 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1287 int w = UpdatableInset::getMaxWidth(pain, inset);
1292 w = w - top_x + owner()->x();
1295 w -= (2 * TEXT_TO_INSET_OFFSET);
1297 // return w - (2*TEXT_TO_INSET_OFFSET);
1301 void InsetText::SetParagraphData(LyXParagraph *p)
1315 par->SetInsetOwner(this);
1319 np->next = p->Clone();
1320 np->next->previous = np;
1322 np->SetInsetOwner(this);
1328 void InsetText::SetAutoBreakRows(bool flag)
1330 if (flag != autoBreakRows) {
1331 autoBreakRows = flag;
1339 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1341 if (how != drawFrame) {
1344 UpdateLocal(bv, DRAW_FRAME, false);
1349 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1351 if (frame_color != col) {
1354 UpdateLocal(bv, DRAW_FRAME, false);
1359 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1361 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1366 int InsetText::cx(BufferView * bv) const
1368 return TEXT(bv)->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1372 int InsetText::cy(BufferView * bv) const
1375 return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1379 int InsetText::cpos(BufferView * bv) const
1381 return TEXT(bv)->cursor.pos();
1385 LyXParagraph * InsetText::cpar(BufferView * bv) const
1387 return TEXT(bv)->cursor.par();
1391 Row * InsetText::crow(BufferView * bv) const
1393 return TEXT(bv)->cursor.row();
1397 LyXText * InsetText::getLyXText(BufferView * bv) const
1399 if (cache.find(bv) != cache.end())
1401 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1404 if (the_locking_inset) {
1405 lt->SetCursor(bv, inset_par, inset_pos);
1411 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1413 if (cache.find(bv) == cache.end())
1418 /// then remove all LyXText in text-insets
1419 LyXParagraph * p = par;
1420 for(;p;p = p->next) {
1421 p->deleteInsetsLyXText(bv);
1427 void InsetText::resizeLyXText(BufferView * bv) const
1429 if (!par->next && !par->size()) // resize not neccessary!
1431 if (cache.find(bv) == cache.end())
1434 LyXParagraph * lpar = 0;
1435 LyXParagraph * selstartpar = 0;
1436 LyXParagraph * selendpar = 0;
1438 int selstartpos = 0;
1443 // ProhibitInput(bv);
1446 lpar = TEXT(bv)->cursor.par();
1447 pos = TEXT(bv)->cursor.pos();
1448 selstartpar = TEXT(bv)->sel_start_cursor.par();
1449 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1450 selendpar = TEXT(bv)->sel_end_cursor.par();
1451 selendpos = TEXT(bv)->sel_end_cursor.pos();
1452 selection = TEXT(bv)->selection;
1453 mark_set = TEXT(bv)->mark_set;
1455 deleteLyXText(bv, (the_locking_inset == 0));
1458 TEXT(bv)->selection = true;
1459 /* at this point just to avoid the Delete-Empty-Paragraph
1460 * Mechanism when setting the cursor */
1461 TEXT(bv)->mark_set = mark_set;
1463 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos);
1464 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1465 TEXT(bv)->SetCursor(bv, selendpar, selendpos);
1466 TEXT(bv)->SetSelection();
1467 TEXT(bv)->SetCursor(bv, lpar, pos);
1469 TEXT(bv)->SetCursor(bv, lpar, pos);
1470 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1471 TEXT(bv)->selection = false;
1475 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1476 // this will scroll the screen such that the cursor becomes visible
1477 bv->updateScrollbar();
1479 if (the_locking_inset) {
1480 /// then resize all LyXText in text-insets
1481 inset_x = cx(bv) - top_x + drawTextXOffset;
1482 inset_y = cy(bv) + drawTextYOffset;
1483 for(LyXParagraph * p = par; p; p = p->next) {
1484 p->resizeInsetsLyXText(bv);
1491 void InsetText::removeNewlines()
1493 for(LyXParagraph * p = par; p; p = p->next) {
1494 for(int i = 0; i < p->Last(); ++i) {
1495 if (p->GetChar(i) == LyXParagraph::META_NEWLINE)