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 void InsetText::clear()
121 LyXParagraph * p = par->next;
128 par = new LyXParagraph();
132 Inset * InsetText::Clone() const
134 InsetText * t = new InsetText(*this);
139 void InsetText::Write(Buffer const * buf, ostream & os) const
142 WriteParagraphData(buf, os);
146 void InsetText::WriteParagraphData(Buffer const * buf, ostream & os) const
148 par->writeFile(buf, os, buf->params, 0, 0);
152 void InsetText::Read(Buffer const * buf, LyXLex & lex)
156 LyXParagraph * return_par = 0;
157 char depth = 0; // signed or unsigned?
158 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
159 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
160 LyXFont font(LyXFont::ALL_INHERIT);
162 LyXParagraph * p = par->next;
169 par = new LyXParagraph;
172 token = lex.GetString();
175 if (token == "\\end_inset")
177 if (const_cast<Buffer*>(buf)->parseSingleLyXformat2Token(lex, par, return_par,
181 // the_end read this should NEVER happen
182 lex.printError("\\the_end read in inset! Error in document!");
190 return_par->SetInsetOwner(this);
191 return_par = return_par->next;
194 if (token != "\\end_inset") {
195 lex.printError("Missing \\end_inset at this point. "
202 int InsetText::ascent(BufferView * bv, LyXFont const &) const
205 Row * row = TEXT(bv)->GetRowNearY(y_temp);
206 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
211 int InsetText::descent(BufferView * bv, LyXFont const &) const
214 Row * row = TEXT(bv)->GetRowNearY(y_temp);
215 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
216 TEXT_TO_INSET_OFFSET;
221 int InsetText::width(BufferView * bv, LyXFont const &) const
223 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
228 int InsetText::textWidth(Painter & pain) const
230 int w = getMaxWidth(pain, this);
232 // printf("WW1: %d\n",w);
237 w = w - top_x + owner()->x();
238 // printf("WW2: %d\n",w);
239 return w; // - top_x + owner()->x();
242 w -= (2 * TEXT_TO_INSET_OFFSET);
243 // printf("WW2: %d\n",w);
244 return w - top_x; // - top_x - (2 * TEXT_TO_INSET_OFFSET);
248 void InsetText::draw(BufferView * bv, LyXFont const & f,
249 int baseline, float & x, bool cleared) const
251 Painter & pain = bv->painter();
254 UpdatableInset::draw(bv, f, baseline, x, cleared);
256 if (!cleared && ((need_update==FULL) || (top_x!=int(x)) ||
257 (top_baseline!=baseline))) {
259 int h = insetAscent + insetDescent;
260 int ty = baseline - insetAscent;
264 if ((ty + h) > pain.paperHeight())
265 h = pain.paperHeight();
266 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
267 w = pain.paperWidth();
268 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
272 if (!cleared && (need_update == NONE))
275 if (top_x != int(x)) {
278 owner()->update(bv, f, true);
279 bv->text->status = LyXText::CHANGED_IN_DRAW;
283 top_baseline = baseline;
284 top_y = baseline - ascent(bv, f);
285 last_width = width(bv, f);
286 last_height = ascent(bv, f) + descent(bv, f);
288 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
289 inset_x = cx(bv) - top_x + drawTextXOffset;
290 inset_y = cy(bv) + drawTextYOffset;
292 if (!cleared && (need_update == CURSOR) && !TEXT(bv)->selection) {
297 x += 1; // place for border
299 Row * row = TEXT(bv)->GetRowNearY(y);
300 y += baseline - row->ascent_of_text() + 1;
301 if (cleared || !locked || (need_update == FULL)) {
303 TEXT(bv)->GetVisibleRow(bv, y, x, row, y, cleared);
307 } else if (need_update == SELECTION) {
308 bv->screen()->ToggleToggle(TEXT(bv), y, x);
311 if (need_update == CURSOR) {
312 bv->screen()->ToggleSelection(TEXT(bv), true, y, x);
313 TEXT(bv)->ClearSelection();
314 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
316 bv->screen()->Update(TEXT(bv), y, x);
319 TEXT(bv)->refresh_y = 0;
320 TEXT(bv)->status = LyXText::UNCHANGED;
321 if ((drawFrame == ALWAYS) || ((drawFrame == LOCKED) && locked)) {
322 pain.rectangle(top_x, baseline - insetAscent, insetWidth,
323 insetAscent + insetDescent, frame_color);
324 } else if (need_update == CLEAR_FRAME) {
325 pain.rectangle(top_x, baseline - insetAscent, insetWidth,
326 insetAscent + insetDescent,
329 x += insetWidth - TEXT_TO_INSET_OFFSET;
330 if (need_update != INIT)
335 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
337 if (reinit) { // && (need_update != CURSOR)) {
341 owner()->update(bv, font, true);
344 if (the_locking_inset) {
345 inset_x = cx(bv) - top_x + drawTextXOffset;
346 inset_y = cy(bv) + drawTextYOffset;
347 the_locking_inset->update(bv, font, reinit);
349 if (need_update == INIT) {
352 // if (!owner() && bv->text)
353 // bv->text->UpdateInset(bv, this);
355 int oldw = insetWidth;
357 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
358 // max(textWidth(bv->painter()),
359 // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
360 // (2 * TEXT_TO_INSET_OFFSET);
362 insetWidth = textWidth(bv->painter());
364 insetWidth = static_cast<int>(TEXT(bv)->width);
366 if (oldw != insetWidth) {
367 // printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
368 // textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
373 owner()->update(bv, font, reinit);
376 update(bv, font, reinit);
380 update(bv, font, reinit);
382 UpdateLocal(bv, INIT, false);
387 if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
390 TEXT(bv)->UpdateInset(bv, the_locking_inset);
393 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
397 Row * row = TEXT(bv)->GetRowNearY(y_temp);
398 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
399 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
400 TEXT_TO_INSET_OFFSET;
404 void InsetText::UpdateLocal(BufferView * bv, UpdateCodes what, bool mark_dirty)
406 TEXT(bv)->FullRebreak(bv);
407 if (need_update != INIT) {
408 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
410 else if (!the_locking_inset || (what != CURSOR))
413 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
415 bv->updateInset(this, mark_dirty);
416 if (old_par != cpar(bv)) {
417 bv->owner()->getToolbar()->combox->select(cpar(bv)->GetLayout()+1);
423 char const * InsetText::EditMessage() const
425 return _("Opened Text Inset");
429 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
431 // par->SetInsetOwner(this);
432 UpdatableInset::Edit(bv, x, y, button);
434 if (!bv->lockInset(this)) {
435 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
439 the_locking_inset = 0;
440 inset_pos = inset_x = inset_y = 0;
443 if (!checkAndActivateInset(bv, x, y, button))
444 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
445 y+TEXT(bv)->first+insetAscent);
446 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
447 bv->text->FinishUndo();
448 UpdateLocal(bv, FULL, false);
452 void InsetText::InsetUnlock(BufferView * bv)
454 if (the_locking_inset) {
455 the_locking_inset->InsetUnlock(bv);
456 the_locking_inset = 0;
459 no_selection = false;
461 TEXT(bv)->selection = 0;
462 UpdateLocal(bv, CLEAR_FRAME, false);
464 bv->owner()->getToolbar()->combox->
465 select(owner()->getLyXText(bv)->cursor.par()->GetLayout()+1);
467 bv->owner()->getToolbar()->combox->select(bv->text->cursor.par()->
472 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
474 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
477 if (inset == cpar(bv)->GetInset(cpos(bv))) {
478 lyxerr[Debug::INSETS] << "OK" << endl;
479 the_locking_inset = inset;
480 inset_x = cx(bv) - top_x + drawTextXOffset;
481 inset_y = cy(bv) + drawTextYOffset;
482 inset_pos = cpos(bv);
483 inset_par = cpar(bv);
484 TEXT(bv)->UpdateInset(bv, the_locking_inset);
486 } else if (the_locking_inset && (the_locking_inset == inset)) {
487 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
488 lyxerr[Debug::INSETS] << "OK" << endl;
489 inset_x = cx(bv) - top_x + drawTextXOffset;
490 inset_y = cy(bv) + drawTextYOffset;
492 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
494 } else if (the_locking_inset) {
495 lyxerr[Debug::INSETS] << "MAYBE" << endl;
496 return the_locking_inset->LockInsetInInset(bv, inset);
498 lyxerr[Debug::INSETS] << "NOT OK" << endl;
503 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
506 if (!the_locking_inset)
508 if (the_locking_inset == inset) {
509 the_locking_inset->InsetUnlock(bv);
510 TEXT(bv)->UpdateInset(bv, inset);
511 the_locking_inset = 0;
513 moveRight(bv, false);
514 old_par = 0; // force layout setting
515 UpdateLocal(bv, CURSOR_PAR, false);
518 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
522 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
524 if (!the_locking_inset)
526 if (the_locking_inset != inset) {
527 TEXT(bv)->UpdateInset(bv, the_locking_inset);
528 need_update = CURSOR_PAR;
529 return the_locking_inset->UpdateInsetInInset(bv, inset);
531 // UpdateLocal(bv, FULL, false);
532 if (TEXT(bv)->UpdateInset(bv, inset))
533 UpdateLocal(bv, CURSOR_PAR, false);
534 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
535 inset_x = cx(bv) - top_x + drawTextXOffset;
536 inset_y = cy(bv) + drawTextYOffset;
542 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
544 no_selection = false;
546 int tmp_x = x - drawTextXOffset;
547 // int tmp_y = y + TEXT(bv)->first + insetAscent;
548 int tmp_y = y + insetAscent;
549 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
551 if (the_locking_inset) {
552 if (the_locking_inset == inset) {
553 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
556 // otherwise unlock the_locking_inset and lock the new inset
557 the_locking_inset->InsetUnlock(bv);
558 inset_x = cx(bv) - top_x + drawTextXOffset;
559 inset_y = cy(bv) + drawTextYOffset;
560 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
561 inset->Edit(bv, x - inset_x, y - inset_y, button);
562 if (the_locking_inset) {
563 UpdateLocal(bv, CURSOR_PAR, false);
567 // otherwise only unlock the_locking_inset
568 the_locking_inset->InsetUnlock(bv);
569 the_locking_inset = 0;
571 if (bv->the_locking_inset) {
572 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
573 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
574 inset_x = cx(bv) - top_x + drawTextXOffset;
575 inset_y = cy(bv) + drawTextYOffset;
576 inset_pos = cpos(bv);
577 inset_par = cpar(bv);
578 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
579 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
580 if (the_locking_inset) {
581 UpdateLocal(bv, CURSOR_PAR, false);
587 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
588 y+TEXT(bv)->first+insetAscent);
589 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
590 UpdateLocal(bv, CURSOR, false);
591 bv->owner()->getToolbar()->combox->select(cpar(bv)->GetLayout()+1);
597 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
599 UpdatableInset * inset = 0;
601 if (the_locking_inset) {
602 the_locking_inset->InsetButtonRelease(bv,
603 x - inset_x, y - inset_y,
606 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
607 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
608 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
609 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
611 inset_x = cx(bv) - top_x + drawTextXOffset;
612 inset_y = cy(bv) + drawTextYOffset;
613 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
614 inset->Edit(bv, x - inset_x, y - inset_y, button);
616 UpdateLocal(bv, CURSOR_PAR, false);
619 no_selection = false;
623 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
625 if (the_locking_inset) {
626 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
632 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
633 y+TEXT(bv)->first+insetAscent);
634 TEXT(bv)->SetSelection();
635 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
636 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
637 UpdateLocal(bv, SELECTION, false);
640 no_selection = false;
644 void InsetText::InsetKeyPress(XKeyEvent * xke)
646 if (the_locking_inset) {
647 the_locking_inset->InsetKeyPress(xke);
653 UpdatableInset::RESULT
654 InsetText::LocalDispatch(BufferView * bv,
655 int action, string const & arg)
657 no_selection = false;
658 UpdatableInset::RESULT
659 result= UpdatableInset::LocalDispatch(bv, action, arg);
660 if (result != UNDISPATCHED) {
661 // resetPos(bv->painter());
666 if ((action < 0) && arg.empty())
669 if (the_locking_inset) {
670 result = the_locking_inset->LocalDispatch(bv, action, arg);
671 if (result == DISPATCHED_NOUPDATE)
673 else if (result == DISPATCHED) {
674 UpdateLocal(bv, CURSOR_PAR, false);
676 } else if (result == FINISHED) {
680 moveRight(bv, false);
686 the_locking_inset = 0;
694 if (bv->buffer()->isReadonly()) {
696 // setErrorMessage(N_("Document is read only"));
700 /* Automatically delete the currently selected
701 * text and replace it with what is being
702 * typed in now. Depends on lyxrc settings
703 * "auto_region_delete", which defaults to
706 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
707 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
708 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
709 if (lyxrc.auto_region_delete) {
710 if (TEXT(bv)->selection){
711 TEXT(bv)->CutSelection(bv, false);
714 TEXT(bv)->ClearSelection();
715 for (string::size_type i = 0; i < arg.length(); ++i) {
716 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
719 UpdateLocal(bv, CURSOR_PAR, true);
721 // --- Cursor Movements ---------------------------------------------
723 bv->text->FinishUndo();
724 moveRight(bv, false);
725 TEXT(bv)->SetSelection();
726 UpdateLocal(bv, SELECTION, false);
729 result = moveRight(bv);
730 bv->text->FinishUndo();
731 UpdateLocal(bv, CURSOR, false);
734 bv->text->FinishUndo();
736 TEXT(bv)->SetSelection();
737 UpdateLocal(bv, SELECTION, false);
740 bv->text->FinishUndo();
741 result= moveLeft(bv);
742 UpdateLocal(bv, CURSOR, false);
745 bv->text->FinishUndo();
747 TEXT(bv)->SetSelection();
748 UpdateLocal(bv, SELECTION, false);
751 bv->text->FinishUndo();
752 result = moveDown(bv);
753 UpdateLocal(bv, CURSOR, false);
756 bv->text->FinishUndo();
758 TEXT(bv)->SetSelection();
759 UpdateLocal(bv, SELECTION, false);
762 bv->text->FinishUndo();
764 UpdateLocal(bv, CURSOR, false);
767 bv->text->FinishUndo();
768 TEXT(bv)->CursorHome(bv);
769 UpdateLocal(bv, CURSOR, false);
772 TEXT(bv)->CursorEnd(bv);
773 UpdateLocal(bv, CURSOR, false);
776 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
777 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
778 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
779 TEXT(bv)->Backspace(bv);
780 UpdateLocal(bv, CURSOR_PAR, true);
783 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
784 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
785 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
786 TEXT(bv)->Delete(bv);
787 UpdateLocal(bv, CURSOR_PAR, true);
790 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
791 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
792 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
793 TEXT(bv)->CutSelection(bv);
794 UpdateLocal(bv, CURSOR_PAR, true);
797 bv->text->FinishUndo();
798 TEXT(bv)->CopySelection(bv);
799 UpdateLocal(bv, CURSOR_PAR, false);
802 if (!autoBreakRows) {
805 if (cap.nrOfParagraphs() > 1) {
806 WriteAlert(_("Impossible operation"),
807 _("Cannot include more than one paragraph!"),
812 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
813 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
814 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
815 TEXT(bv)->PasteSelection(bv);
816 UpdateLocal(bv, CURSOR_PAR, true);
818 case LFUN_BREAKPARAGRAPH:
821 TEXT(bv)->BreakParagraph(bv, 0);
822 UpdateLocal(bv, FULL, true);
827 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
828 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
829 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
830 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
831 UpdateLocal(bv, CURSOR_PAR, true);
835 static LyXTextClass::size_type cur_layout = cpar(bv)->layout;
837 // Derive layout number from given argument (string)
838 // and current buffer's textclass (number). */
839 LyXTextClassList::ClassList::size_type tclass =
840 bv->buffer()->params.textclass;
841 std::pair <bool, LyXTextClass::size_type> layout =
842 textclasslist.NumberOfLayout(tclass, arg);
844 // If the entry is obsolete, use the new one instead.
846 string obs = textclasslist.Style(tclass,layout.second).
849 layout = textclasslist.NumberOfLayout(tclass, obs);
852 // see if we found the layout number:
854 string msg = string(N_("Layout ")) + arg + N_(" not known");
856 bv->owner()->getMiniBuffer()->Set(msg);
860 if (cur_layout != layout.second) {
861 cur_layout = layout.second;
862 TEXT(bv)->SetLayout(bv, layout.second);
863 bv->owner()->getToolbar()->combox->select(cpar(bv)->GetLayout()+1);
864 UpdateLocal(bv, CURSOR_PAR, true);
868 case LFUN_PARAGRAPH_SPACING:
869 // This one is absolutely not working. When fiddling with this
870 // it also seems to me that the paragraphs inside the insettext
871 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
873 LyXParagraph * par = TEXT(bv)->cursor.par();
874 Spacing::Space cur_spacing = par->spacing.getSpace();
875 float cur_value = 1.0;
876 if (cur_spacing == Spacing::Other) {
877 cur_value = par->spacing.getValue();
881 std::istringstream istr(arg);
883 istrstream istr(arg.c_str());
887 Spacing::Space new_spacing = cur_spacing;
888 float new_value = cur_value;
890 lyxerr << "Missing argument to `paragraph-spacing'"
892 } else if (tmp == "single") {
893 new_spacing = Spacing::Single;
894 } else if (tmp == "onehalf") {
895 new_spacing = Spacing::Onehalf;
896 } else if (tmp == "double") {
897 new_spacing = Spacing::Double;
898 } else if (tmp == "other") {
899 new_spacing = Spacing::Other;
902 lyxerr << "new_value = " << tmpval << endl;
905 } else if (tmp == "default") {
906 new_spacing = Spacing::Default;
908 lyxerr << _("Unknown spacing argument: ")
911 if (cur_spacing != new_spacing || cur_value != new_value) {
912 par->spacing.set(new_spacing, new_value);
913 //TEXT(bv)->RedoParagraph(owner->view());
914 UpdateLocal(bv, CURSOR_PAR, true);
915 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
921 result = UNDISPATCHED;
924 if (result != FINISHED) {
927 bv->unlockInset(this);
932 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
935 buf->latexParagraphs(os, par, 0, texrow);
936 return texrow.rows();
940 void InsetText::Validate(LaTeXFeatures & features) const
942 LyXParagraph * p = par;
944 p->validate(features);
950 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
952 if (textclasslist.Style(buf->params.textclass,
953 p->GetLayout()).labeltype != LABEL_MANUAL)
956 return p->BeginningOfMainBody();
960 void InsetText::GetCursorPos(BufferView * bv, int & x, int & y) const
967 int InsetText::InsetInInsetY()
969 if (!the_locking_inset)
972 return (inset_y + the_locking_inset->InsetInInsetY());
976 void InsetText::ToggleInsetCursor(BufferView * bv)
978 if (the_locking_inset) {
979 the_locking_inset->ToggleInsetCursor(bv);
983 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
985 int asc = lyxfont::maxAscent(font);
986 int desc = lyxfont::maxDescent(font);
989 bv->hideLockedInsetCursor();
991 bv->showLockedInsetCursor(cx(bv)-1, cy(bv),
993 cursor_visible = !cursor_visible;
997 void InsetText::ShowInsetCursor(BufferView * bv)
999 if (the_locking_inset) {
1000 the_locking_inset->ShowInsetCursor(bv);
1003 if (!cursor_visible) {
1004 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1006 int asc = lyxfont::maxAscent(font);
1007 int desc = lyxfont::maxDescent(font);
1009 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1010 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1011 cursor_visible = true;
1016 void InsetText::HideInsetCursor(BufferView * bv)
1018 if (cursor_visible) {
1019 bv->hideLockedInsetCursor();
1020 cursor_visible = false;
1022 if (the_locking_inset)
1023 the_locking_inset->HideInsetCursor(bv);
1027 UpdatableInset::RESULT
1028 InsetText::moveRight(BufferView * bv, bool activate_inset)
1030 if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1032 if (activate_inset && checkAndActivateInset(bv, false))
1034 TEXT(bv)->CursorRight(bv);
1035 return DISPATCHED_NOUPDATE;
1039 UpdatableInset::RESULT
1040 InsetText::moveLeft(BufferView * bv, bool activate_inset)
1042 if (!cpar(bv)->previous && (cpos(bv) <= 0))
1044 TEXT(bv)->CursorLeft(bv);
1045 if (activate_inset && checkAndActivateInset(bv, true))
1047 return DISPATCHED_NOUPDATE;
1051 UpdatableInset::RESULT
1052 InsetText::moveUp(BufferView * bv)
1054 if (!crow(bv)->previous())
1056 TEXT(bv)->CursorUp(bv);
1057 return DISPATCHED_NOUPDATE;
1061 UpdatableInset::RESULT
1062 InsetText::moveDown(BufferView * bv)
1064 if (!crow(bv)->next())
1066 TEXT(bv)->CursorDown(bv);
1067 return DISPATCHED_NOUPDATE;
1071 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1073 if (the_locking_inset) {
1074 if (the_locking_inset->InsertInsetAllowed(inset))
1075 return the_locking_inset->InsertInset(bv, inset);
1078 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1079 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1080 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
1081 if (inset->Editable() == Inset::IS_EDITABLE) {
1082 UpdatableInset * i = static_cast<UpdatableInset *>(inset);
1083 i->setOwner(static_cast<UpdatableInset *>(this));
1085 TEXT(bv)->InsertInset(bv, inset);
1086 TEXT(bv)->selection = 0;
1087 UpdateLocal(bv, CURSOR_PAR, true);
1088 static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1093 UpdatableInset * InsetText::GetLockingInset()
1095 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1099 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1103 if (the_locking_inset)
1104 return the_locking_inset->GetFirstLockingInsetOfType(c);
1109 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1111 TEXT(bv)->SetFont(bv, font, toggleall);
1115 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1117 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1120 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1121 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1123 LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1125 x = inset->width(bv, font);
1126 y = inset->descent(bv, font);
1130 inset_x = cx(bv) - top_x + drawTextXOffset;
1131 inset_y = cy(bv) + drawTextYOffset;
1132 inset->Edit(bv, x-inset_x, y-inset_y, 0);
1133 if (!the_locking_inset)
1135 UpdateLocal(bv, CURSOR_PAR, false);
1142 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1145 int tmp_x = x - drawTextXOffset;
1146 int tmp_y = y + insetAscent;
1147 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
1154 inset_x = cx(bv) - top_x + drawTextXOffset;
1155 inset_y = cy(bv) + drawTextYOffset;
1156 inset->Edit(bv, x - inset_x, y - inset_y, button);
1157 if (!the_locking_inset)
1159 UpdateLocal(bv, CURSOR_PAR, false);
1166 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1168 int w = UpdatableInset::getMaxWidth(pain, inset);
1171 return w - (2*TEXT_TO_INSET_OFFSET);
1175 void InsetText::SetParagraphData(LyXParagraph *p)
1189 par->SetInsetOwner(this);
1193 np->next = p->Clone();
1194 np->next->previous = np;
1196 np->SetInsetOwner(this);
1202 void InsetText::SetAutoBreakRows(bool flag)
1204 if (flag != autoBreakRows) {
1205 autoBreakRows = flag;
1211 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1213 if (how != drawFrame) {
1216 UpdateLocal(bv, DRAW_FRAME, false);
1221 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1223 if (frame_color != col) {
1226 UpdateLocal(bv, DRAW_FRAME, false);
1231 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1233 return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1237 int InsetText::cx(BufferView * bv) const
1239 return TEXT(bv)->cursor.x() + top_x + 1;
1243 int InsetText::cy(BufferView * bv) const
1245 long int y_dummy = 0;
1246 Row * tmprow = TEXT(bv)->GetRowNearY(y_dummy);
1247 return TEXT(bv)->cursor.y() - tmprow->baseline();
1251 int InsetText::cpos(BufferView * bv) const
1253 return TEXT(bv)->cursor.pos();
1257 LyXParagraph * InsetText::cpar(BufferView * bv) const
1259 return TEXT(bv)->cursor.par();
1263 Row * InsetText::crow(BufferView * bv) const
1265 return TEXT(bv)->cursor.row();
1269 LyXText * InsetText::getLyXText(BufferView * bv) const
1271 if (cache.find(bv) != cache.end())
1273 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1276 if (the_locking_inset) {
1277 lt->SetCursor(bv, inset_par, inset_pos);
1283 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1287 /// then remove all LyXText in text-insets
1288 LyXParagraph * p = par;
1289 for(;p;p = p->next) {
1290 p->deleteInsetsLyXText(bv);
1295 void InsetText::resizeLyXText(BufferView * bv) const
1297 if (!par->next && !par->size()) // not neccessary!
1300 LyXParagraph * lpar = 0;
1301 LyXParagraph * selstartpar = 0;
1302 LyXParagraph * selendpar = 0;
1304 int selstartpos = 0;
1309 // ProhibitInput(bv);
1311 lpar = TEXT(bv)->cursor.par();
1312 pos = TEXT(bv)->cursor.pos();
1313 selstartpar = TEXT(bv)->sel_start_cursor.par();
1314 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1315 selendpar = TEXT(bv)->sel_end_cursor.par();
1316 selendpos = TEXT(bv)->sel_end_cursor.pos();
1317 selection = TEXT(bv)->selection;
1318 mark_set = TEXT(bv)->mark_set;
1319 deleteLyXText(bv, (the_locking_inset == 0));
1322 TEXT(bv)->selection = true;
1323 /* at this point just to avoid the Delete-Empty-Paragraph
1324 * Mechanism when setting the cursor */
1325 TEXT(bv)->mark_set = mark_set;
1327 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos);
1328 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1329 TEXT(bv)->SetCursor(bv, selendpar, selendpos);
1330 TEXT(bv)->SetSelection();
1331 TEXT(bv)->SetCursor(bv, lpar, pos);
1333 TEXT(bv)->SetCursor(bv, lpar, pos);
1334 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1335 TEXT(bv)->selection = false;
1339 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1340 // this will scroll the screen such that the cursor becomes visible
1341 bv->updateScrollbar();
1343 if (the_locking_inset) {
1344 /// then resize all LyXText in text-insets
1345 inset_x = cx(bv) - top_x + drawTextXOffset;
1346 inset_y = cy(bv) + drawTextYOffset;
1347 LyXParagraph * p = par;
1348 for(;p;p = p->next) {
1349 p->resizeInsetsLyXText(bv);