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"
41 #include "support/textutils.h"
42 #include "support/LAssert.h"
46 #include "trans_mgr.h"
47 #include "lyxscreen.h"
58 extern unsigned char getCurrentTextClass(Buffer *);
59 extern bool math_insert_greek(BufferView *, char);
60 extern int greek_kb_flag;
62 InsetText::InsetText()
64 par = new LyXParagraph;
69 InsetText::InsetText(InsetText const & ins)
74 autoBreakRows = ins.autoBreakRows;
78 InsetText & InsetText::operator=(InsetText const & it)
81 autoBreakRows = it.autoBreakRows;
86 void InsetText::init(InsetText const * ins)
94 the_locking_inset = 0;
100 autoBreakRows = false;
104 SetParagraphData(ins->par);
105 autoBreakRows = ins->autoBreakRows;
106 drawFrame_ = ins->drawFrame_;
108 par->SetInsetOwner(this);
109 frame_color = LColor::insetframe;
112 last_drawn_width = -1;
113 frame_is_visible = false;
117 InsetText::~InsetText()
119 // delete all instances of LyXText before deleting the paragraps used
121 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
122 delete (*cit).second;
127 LyXParagraph * tmp = par->next();
134 void InsetText::clear()
136 // delete all instances of LyXText before deleting the paragraps used
138 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
139 delete (*cit).second;
144 LyXParagraph * tmp = par->next();
148 par = new LyXParagraph;
152 Inset * InsetText::Clone(Buffer const &) const
154 InsetText * t = new InsetText(*this);
159 void InsetText::Write(Buffer const * buf, ostream & os) const
162 WriteParagraphData(buf, os);
166 void InsetText::WriteParagraphData(Buffer const * buf, ostream & os) const
168 par->writeFile(buf, os, buf->params, 0, 0);
172 void InsetText::Read(Buffer const * buf, LyXLex & lex)
176 LyXParagraph * return_par = 0;
177 char depth = 0; // signed or unsigned?
178 LyXFont font(LyXFont::ALL_INHERIT);
184 token = lex.GetString();
187 if (token == "\\end_inset")
189 if (const_cast<Buffer*>(buf)->
190 parseSingleLyXformat2Token(lex, par, return_par,
191 token, pos, depth, font)) {
192 // the_end read this should NEVER happen
193 lex.printError("\\the_end read in inset! Error in document!");
201 return_par->SetInsetOwner(this);
202 return_par = return_par->next();
205 if (token != "\\end_inset") {
206 lex.printError("Missing \\end_inset at this point. "
213 int InsetText::ascent(BufferView * bv, LyXFont const &) const
216 Row * row = TEXT(bv)->GetRowNearY(y_temp);
217 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
222 int InsetText::descent(BufferView * bv, LyXFont const &) const
225 Row * row = TEXT(bv)->GetRowNearY(y_temp);
226 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
227 TEXT_TO_INSET_OFFSET;
232 int InsetText::width(BufferView * bv, LyXFont const &) const
234 insetWidth = max(textWidth(bv),
235 (int)TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET));
240 int InsetText::textWidth(BufferView * bv) const
242 int const w = getMaxWidth(bv, this);
247 void InsetText::draw(BufferView * bv, LyXFont const & f,
248 int baseline, float & x, bool cleared) const
253 Painter & pain = bv->painter();
255 // no draw is necessary !!!
256 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
258 top_baseline = baseline;
260 if (!cleared && (need_update & CLEAR_FRAME))
261 clearFrame(pain, cleared);
268 UpdatableInset::draw(bv, f, baseline, x, cleared);
271 x += static_cast<float>(scroll());
273 // update insetWidth and insetHeight with dummy calls
275 (void)descent(bv, f);
278 // if top_x differs we have a rule down and we don't have to clear anything
279 if (!cleared && (top_x == int(x)) &&
280 ((need_update&(INIT|FULL)) || (top_baseline!=baseline) ||
281 (last_drawn_width!=insetWidth)))
284 int h = insetAscent + insetDescent;
285 int ty = baseline - insetAscent;
291 if ((ty + h) > pain.paperHeight())
292 h = pain.paperHeight();
293 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
294 w = pain.paperWidth();
295 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
299 if (!cleared && (need_update == NONE))
302 if (top_x != int(x)) {
305 bv->text->status = LyXText::CHANGED_IN_DRAW;
309 // lyxerr << "InsetText::draw[" << this << "](" << need_update << ":" << int(x) << ":" << top_x << ")\n";
311 if (cleared || (last_drawn_width != insetWidth)) {
313 last_drawn_width = insetWidth;
316 top_baseline = baseline;
317 top_y = baseline - ascent(bv, f);
318 last_width = width(bv, f);
319 last_height = ascent(bv, f) + descent(bv, f);
321 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
322 inset_x = cx(bv) - top_x + drawTextXOffset;
323 inset_y = cy(bv) + drawTextYOffset;
325 if (!cleared && (need_update == CURSOR) && !TEXT(bv)->selection) {
326 drawFrame(pain, cleared);
331 x += TEXT_TO_INSET_OFFSET;
334 #warning Jürgen, why is this a block of its own? (Lgb)
335 #warning because you told me to define variables only in local context (Jug)!
336 #warning then make it a function/method of its own. (Lgb)
340 Row * row = TEXT(bv)->GetRowNearY(y);
341 int y_offset = baseline - row->ascent_of_text();
342 int ph = pain.paperHeight();
345 while ((row != 0) && ((y+row->height()) <= 0)) {
347 first += row->height();
352 TEXT(bv)->first = first;
356 while ((row != 0) && (yf < ph)) {
357 TEXT(bv)->GetVisibleRow(bv, y+y_offset, int(x), row,
363 } else if (!locked) {
364 if (need_update & CURSOR) {
365 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
366 TEXT(bv)->ClearSelection(bv);
367 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
369 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
372 if (need_update & SELECTION)
373 bv->screen()->ToggleToggle(TEXT(bv), bv, y_offset, int(x));
374 else if (need_update & CURSOR) {
375 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
376 TEXT(bv)->ClearSelection(bv);
377 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
379 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
383 TEXT(bv)->refresh_y = 0;
384 TEXT(bv)->status = LyXText::UNCHANGED;
385 if ((need_update != CURSOR_PAR) &&
386 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked)))
387 drawFrame(pain, cleared);
388 else if (need_update & CLEAR_FRAME)
389 clearFrame(pain, cleared);
390 x += width(bv, f) - TEXT_TO_INSET_OFFSET;
391 if (bv->text->status==LyXText::CHANGED_IN_DRAW) {
393 } else if (need_update != INIT)
398 void InsetText::drawFrame(Painter & pain, bool cleared) const
400 if (!frame_is_visible || cleared) {
401 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
402 insetWidth - 1, insetAscent + insetDescent - 1,
404 frame_is_visible = true;
409 void InsetText::clearFrame(Painter & pain, bool cleared) const
411 if (frame_is_visible) {
413 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
414 insetWidth - 1, insetAscent + insetDescent - 1,
417 frame_is_visible = false;
422 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
428 owner()->update(bv, font, true);
431 if (the_locking_inset) {
432 inset_x = cx(bv) - top_x + drawTextXOffset;
433 inset_y = cy(bv) + drawTextYOffset;
434 the_locking_inset->update(bv, font, reinit);
437 if (need_update == INIT) {
442 int oldw = insetWidth;
443 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
444 if (oldw != insetWidth) {
447 // update(bv, font, reinit);
450 if ((need_update&CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
453 TEXT(bv)->UpdateInset(bv, the_locking_inset);
456 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
460 Row * row = TEXT(bv)->GetRowNearY(y_temp);
461 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
462 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
463 TEXT_TO_INSET_OFFSET;
468 void InsetText::SetUpdateStatus(BufferView * bv, int what) const
471 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
473 else if (TEXT(bv)->status == LyXText::NEED_VERY_LITTLE_REFRESH)
474 need_update |= CURSOR_PAR;
476 // this to not draw a selection when we redraw all of it!
477 if ((need_update & (INIT|FULL)) && (need_update & CURSOR))
478 TEXT(bv)->ClearSelection(bv);
482 void InsetText::UpdateLocal(BufferView * bv, int what, bool mark_dirty)
484 TEXT(bv)->FullRebreak(bv);
485 SetUpdateStatus(bv, what);
486 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
488 bv->updateInset(this, mark_dirty);
489 bv->owner()->showState();
490 if (old_par != cpar(bv)) {
491 bv->owner()->setLayout(cpar(bv)->GetLayout());
497 string const InsetText::EditMessage() const
499 return _("Opened Text Inset");
503 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
505 // par->SetInsetOwner(this);
506 UpdatableInset::Edit(bv, x, y, button);
508 if (!bv->lockInset(this)) {
509 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
513 the_locking_inset = 0;
514 inset_pos = inset_x = inset_y = 0;
515 inset_boundary = false;
518 int tmp_y = (y < 0)?0:y;
519 if (!checkAndActivateInset(bv, x, tmp_y, button))
520 TEXT(bv)->SetCursorFromCoordinates(bv, x - drawTextXOffset,
522 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
523 bv->text->FinishUndo();
525 UpdateLocal(bv, CURSOR, false);
527 // If the inset is empty set the language of the current font to the
528 // language to the surronding text.
529 if (par->size() == 0 && !par->next()) {
530 LyXFont font(LyXFont::ALL_IGNORE);
531 font.setLanguage(bv->getParentLanguage(this));
532 SetFont(bv, font, false);
537 void InsetText::InsetUnlock(BufferView * bv)
539 if (the_locking_inset) {
540 the_locking_inset->InsetUnlock(bv);
541 the_locking_inset = 0;
544 no_selection = false;
546 UpdateLocal(bv, CLEAR_FRAME|CURSOR, false);
548 bv->owner()->setLayout(owner()->getLyXText(bv)
549 ->cursor.par()->GetLayout());
551 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
555 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
557 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
561 if (inset == cpar(bv)->GetInset(cpos(bv))) {
562 lyxerr[Debug::INSETS] << "OK" << endl;
563 the_locking_inset = inset;
564 inset_x = cx(bv) - top_x + drawTextXOffset;
565 inset_y = cy(bv) + drawTextYOffset;
566 inset_pos = cpos(bv);
567 inset_par = cpar(bv);
568 inset_boundary = cboundary(bv);
570 TEXT(bv)->ClearSelection(bv);
571 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
572 TEXT(bv)->UpdateInset(bv, the_locking_inset);
574 UpdateLocal(bv, CURSOR, false);
577 } else if (the_locking_inset && (the_locking_inset == inset)) {
578 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
579 lyxerr[Debug::INSETS] << "OK" << endl;
580 inset_x = cx(bv) - top_x + drawTextXOffset;
581 inset_y = cy(bv) + drawTextYOffset;
583 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
585 } else if (the_locking_inset) {
586 lyxerr[Debug::INSETS] << "MAYBE" << endl;
587 return the_locking_inset->LockInsetInInset(bv, inset);
589 lyxerr[Debug::INSETS] << "NOT OK" << endl;
594 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
597 if (!the_locking_inset)
599 if (the_locking_inset == inset) {
600 the_locking_inset->InsetUnlock(bv);
601 TEXT(bv)->UpdateInset(bv, inset);
602 the_locking_inset = 0;
604 moveRight(bv, false);
605 old_par = 0; // force layout setting
609 UpdateLocal(bv, CURSOR, false);
612 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
616 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
618 if (!the_locking_inset)
620 if (the_locking_inset != inset) {
621 TEXT(bv)->UpdateInset(bv, the_locking_inset);
622 SetUpdateStatus(bv, CURSOR_PAR);
623 return the_locking_inset->UpdateInsetInInset(bv, inset);
625 // UpdateLocal(bv, FULL, false);
626 if (TEXT(bv)->UpdateInset(bv, inset))
627 UpdateLocal(bv, CURSOR_PAR, false);
628 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
629 inset_x = cx(bv) - top_x + drawTextXOffset;
630 inset_y = cy(bv) + drawTextYOffset;
636 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
640 int tmp_x = x - drawTextXOffset;
641 int tmp_y = y + insetAscent - TEXT(bv)->first;
642 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
645 if (the_locking_inset) {
646 if (the_locking_inset == inset) {
647 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
648 no_selection = false;
651 // otherwise unlock the_locking_inset and lock the new inset
652 the_locking_inset->InsetUnlock(bv);
653 inset_x = cx(bv) - top_x + drawTextXOffset;
654 inset_y = cy(bv) + drawTextYOffset;
655 the_locking_inset = static_cast<UpdatableInset*>(inset);
656 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
657 inset->Edit(bv, x - inset_x, y - inset_y, button);
658 if (the_locking_inset)
659 UpdateLocal(bv, CURSOR, false);
660 no_selection = false;
663 // otherwise only unlock the_locking_inset
664 the_locking_inset->InsetUnlock(bv);
665 the_locking_inset = 0;
667 if (bv->theLockingInset()) {
668 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
669 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
670 inset_x = cx(bv) - top_x + drawTextXOffset;
671 inset_y = cy(bv) + drawTextYOffset;
672 inset_pos = cpos(bv);
673 inset_par = cpar(bv);
674 inset_boundary = cboundary(bv);
675 the_locking_inset = uinset;
676 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y,
678 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
679 if (the_locking_inset)
680 UpdateLocal(bv, CURSOR, false);
681 no_selection = false;
685 if (!inset) { // && (button == 2)) {
686 bool paste_internally = false;
687 if ((button == 2) && TEXT(bv)->selection) {
688 LocalDispatch(bv, LFUN_COPY, "");
689 paste_internally = true;
691 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
693 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
694 UpdateLocal(bv, CURSOR, false);
695 bv->owner()->setLayout(cpar(bv)->GetLayout());
697 // Insert primary selection with middle mouse
698 // if there is a local selection in the current buffer,
701 if (paste_internally)
702 LocalDispatch(bv, LFUN_PASTE, "");
704 LocalDispatch(bv, LFUN_PASTESELECTION,
709 no_selection = false;
713 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
715 UpdatableInset * inset = 0;
717 if (the_locking_inset) {
718 the_locking_inset->InsetButtonRelease(bv,
719 x - inset_x, y - inset_y,
722 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
723 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
724 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
725 inset->InsetButtonRelease(bv,
727 y - inset_y, button);
729 inset_x = cx(bv) - top_x + drawTextXOffset;
730 inset_y = cy(bv) + drawTextYOffset;
731 inset->InsetButtonRelease(bv,
733 y - inset_y, button);
735 x - inset_x, y - inset_y, button);
737 UpdateLocal(bv, CURSOR_PAR, false);
740 no_selection = false;
744 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
748 if (the_locking_inset) {
749 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
754 TEXT(bv)->SetCursorFromCoordinates(bv, x - drawTextXOffset,
756 TEXT(bv)->SetSelection(bv);
757 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
758 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
759 UpdateLocal(bv, SELECTION, false);
764 void InsetText::InsetKeyPress(XKeyEvent * xke)
766 if (the_locking_inset) {
767 the_locking_inset->InsetKeyPress(xke);
773 UpdatableInset::RESULT
774 InsetText::LocalDispatch(BufferView * bv,
775 kb_action action, string const & arg)
777 no_selection = false;
778 UpdatableInset::RESULT
779 result= UpdatableInset::LocalDispatch(bv, action, arg);
780 if (result != UNDISPATCHED) {
785 if ((action < 0) && arg.empty())
788 if (the_locking_inset) {
789 result = the_locking_inset->LocalDispatch(bv, action, arg);
790 if (result == DISPATCHED_NOUPDATE)
792 else if (result == DISPATCHED) {
793 UpdateLocal(bv, CURSOR_PAR, false);
795 } else if (result == FINISHED) {
796 bool dispatched = false;
798 case LFUN_UNKNOWN_ACTION:
799 case LFUN_BREAKPARAGRAPH:
801 moveRightIntern(bv, false, false);
804 if (!TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
805 moveRightIntern(bv, false, false);
809 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
810 moveRightIntern(bv, false, false);
816 the_locking_inset = 0;
824 case LFUN_UNKNOWN_ACTION:
825 if (bv->buffer()->isReadonly()) {
826 // setErrorMessage(N_("Document is read only"));
830 /* Automatically delete the currently selected
831 * text and replace it with what is being
832 * typed in now. Depends on lyxrc settings
833 * "auto_region_delete", which defaults to
836 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
837 bv->text->cursor.par()->previous(),
838 bv->text->cursor.par()->next());
840 if (lyxrc.auto_region_delete) {
841 if (TEXT(bv)->selection){
842 TEXT(bv)->CutSelection(bv, false);
845 TEXT(bv)->ClearSelection(bv);
846 for (string::size_type i = 0; i < arg.length(); ++i) {
848 if (!math_insert_greek(bv, arg[i])) {
849 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
850 } else if (!the_locking_inset) {
851 (void)moveRight(bv, false);
854 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
858 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
859 UpdateLocal(bv, CURSOR_PAR, true);
860 result=DISPATCHED_NOUPDATE;
862 // --- Cursor Movements -----------------------------------
864 bv->text->FinishUndo();
865 moveRight(bv, false, true);
866 TEXT(bv)->SetSelection(bv);
867 UpdateLocal(bv, SELECTION, false);
870 result = moveRight(bv);
871 bv->text->FinishUndo();
872 UpdateLocal(bv, CURSOR, false);
875 bv->text->FinishUndo();
876 moveLeft(bv, false, true);
877 TEXT(bv)->SetSelection(bv);
878 UpdateLocal(bv, SELECTION, false);
881 bv->text->FinishUndo();
882 result = moveLeft(bv);
883 UpdateLocal(bv, CURSOR, false);
886 bv->text->FinishUndo();
888 TEXT(bv)->SetSelection(bv);
889 UpdateLocal(bv, SELECTION, false);
892 bv->text->FinishUndo();
893 result = moveDown(bv);
894 UpdateLocal(bv, CURSOR, false);
897 bv->text->FinishUndo();
899 TEXT(bv)->SetSelection(bv);
900 UpdateLocal(bv, SELECTION, false);
903 bv->text->FinishUndo();
905 UpdateLocal(bv, CURSOR, false);
908 bv->text->FinishUndo();
909 TEXT(bv)->CursorHome(bv);
910 UpdateLocal(bv, CURSOR, false);
913 TEXT(bv)->CursorEnd(bv);
914 UpdateLocal(bv, CURSOR, false);
917 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
918 bv->text->cursor.par()->previous(),
919 bv->text->cursor.par()->next());
920 if (TEXT(bv)->selection)
921 TEXT(bv)->CutSelection(bv);
923 TEXT(bv)->Backspace(bv);
924 UpdateLocal(bv, CURSOR_PAR, true);
927 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
928 bv->text->cursor.par()->previous(),
929 bv->text->cursor.par()->next());
930 if (TEXT(bv)->selection)
931 TEXT(bv)->CutSelection(bv);
933 TEXT(bv)->Delete(bv);
934 UpdateLocal(bv, CURSOR_PAR, true);
937 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
938 bv->text->cursor.par()->previous(),
939 bv->text->cursor.par()->next());
940 TEXT(bv)->CutSelection(bv);
941 UpdateLocal(bv, CURSOR_PAR, true);
944 bv->text->FinishUndo();
945 TEXT(bv)->CopySelection(bv);
946 UpdateLocal(bv, CURSOR_PAR, false);
948 case LFUN_PASTESELECTION:
950 string clip(bv->getClipboard());
954 if (arg == "paragraph") {
955 TEXT(bv)->InsertStringB(bv, clip);
957 TEXT(bv)->InsertStringA(bv, clip);
959 UpdateLocal(bv, CURSOR_PAR, true);
963 if (!autoBreakRows) {
966 if (cap.nrOfParagraphs() > 1) {
967 WriteAlert(_("Impossible operation"),
968 _("Cannot include more than one paragraph!"),
973 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
974 bv->text->cursor.par()->previous(),
975 bv->text->cursor.par()->next());
976 TEXT(bv)->PasteSelection(bv);
977 UpdateLocal(bv, CURSOR_PAR, true);
979 case LFUN_BREAKPARAGRAPH:
982 TEXT(bv)->BreakParagraph(bv, 0);
983 UpdateLocal(bv, FULL, true);
985 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
988 TEXT(bv)->BreakParagraph(bv, 1);
989 UpdateLocal(bv, FULL, true);
994 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
995 bv->text->cursor.par()->previous(),
996 bv->text->cursor.par()->next());
997 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
998 UpdateLocal(bv, CURSOR_PAR, true);
1001 // do not set layouts on non breakable textinsets
1002 if (autoBreakRows) {
1003 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1005 // Derive layout number from given argument (string)
1006 // and current buffer's textclass (number). */
1007 LyXTextClassList::ClassList::size_type tclass =
1008 bv->buffer()->params.textclass;
1009 std::pair <bool, LyXTextClass::size_type> layout =
1010 textclasslist.NumberOfLayout(tclass, arg);
1012 // If the entry is obsolete, use the new one instead.
1014 string obs = textclasslist.Style(tclass,layout.second).
1017 layout = textclasslist.NumberOfLayout(tclass, obs);
1020 // see if we found the layout number:
1021 if (!layout.first) {
1022 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1023 bv->owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, msg);
1027 if (cur_layout != layout.second) {
1028 cur_layout = layout.second;
1029 TEXT(bv)->SetLayout(bv, layout.second);
1030 bv->owner()->setLayout(cpar(bv)->GetLayout());
1031 UpdateLocal(bv, CURSOR_PAR, true);
1034 // reset the layout box
1035 bv->owner()->setLayout(cpar(bv)->GetLayout());
1038 case LFUN_PARAGRAPH_SPACING:
1039 // This one is absolutely not working. When fiddling with this
1040 // it also seems to me that the paragraphs inside the insettext
1041 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1043 LyXParagraph * par = TEXT(bv)->cursor.par();
1044 Spacing::Space cur_spacing = par->params.spacing().getSpace();
1045 float cur_value = 1.0;
1046 if (cur_spacing == Spacing::Other) {
1047 cur_value = par->params.spacing().getValue();
1050 std::istringstream istr(arg.c_str());
1053 Spacing::Space new_spacing = cur_spacing;
1054 float new_value = cur_value;
1056 lyxerr << "Missing argument to `paragraph-spacing'"
1058 } else if (tmp == "single") {
1059 new_spacing = Spacing::Single;
1060 } else if (tmp == "onehalf") {
1061 new_spacing = Spacing::Onehalf;
1062 } else if (tmp == "double") {
1063 new_spacing = Spacing::Double;
1064 } else if (tmp == "other") {
1065 new_spacing = Spacing::Other;
1068 lyxerr << "new_value = " << tmpval << endl;
1071 } else if (tmp == "default") {
1072 new_spacing = Spacing::Default;
1074 lyxerr << _("Unknown spacing argument: ")
1077 if (cur_spacing != new_spacing || cur_value != new_value) {
1078 par->params.spacing(Spacing(new_spacing, new_value));
1079 //TEXT(bv)->RedoParagraph(owner->view());
1080 UpdateLocal(bv, CURSOR_PAR, true);
1081 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1087 if (!bv->Dispatch(action, arg))
1088 result = UNDISPATCHED;
1092 /// If the action has deleted all text in the inset, we need to change the
1093 // language to the language to the surronding text.
1094 if (par->size() == 0 && !par->next()) {
1095 LyXFont font(LyXFont::ALL_IGNORE);
1096 font.setLanguage(bv->getParentLanguage(this));
1097 SetFont(bv, font, false);
1100 if (result != FINISHED) {
1101 ShowInsetCursor(bv);
1103 bv->unlockInset(this);
1108 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1111 buf->latexParagraphs(os, par, 0, texrow);
1112 return texrow.rows();
1116 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1118 LyXParagraph * p = par;
1119 unsigned int lines = 0;
1123 tmp = buf->asciiParagraph(p, linelen);
1124 lines += countChar(tmp, '\n');
1132 int InsetText::DocBook(Buffer const * buf, ostream & os) const
1134 LyXParagraph * p = par;
1135 unsigned int lines = 0;
1140 buf->SimpleDocBookOnePar(os,tmp,p,desc,0);
1148 void InsetText::Validate(LaTeXFeatures & features) const
1150 LyXParagraph * p = par;
1152 p->validate(features);
1158 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1160 if (textclasslist.Style(buf->params.textclass,
1161 p->GetLayout()).labeltype != LABEL_MANUAL)
1164 return p->BeginningOfMainBody();
1168 void InsetText::GetCursorPos(BufferView * bv,
1169 int & x, int & y) const
1176 unsigned int InsetText::InsetInInsetY()
1178 if (!the_locking_inset)
1181 return (inset_y + the_locking_inset->InsetInInsetY());
1185 void InsetText::ToggleInsetCursor(BufferView * bv)
1187 if (the_locking_inset) {
1188 the_locking_inset->ToggleInsetCursor(bv);
1192 LyXFont const font(TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv)));
1194 int const asc = lyxfont::maxAscent(font);
1195 int const desc = lyxfont::maxDescent(font);
1197 if (isCursorVisible())
1198 bv->hideLockedInsetCursor();
1200 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1201 toggleCursorVisible();
1205 void InsetText::ShowInsetCursor(BufferView * bv, bool show)
1207 if (the_locking_inset) {
1208 the_locking_inset->ShowInsetCursor(bv);
1211 if (!isCursorVisible()) {
1212 LyXFont const font =
1213 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1215 int const asc = lyxfont::maxAscent(font);
1216 int const desc = lyxfont::maxDescent(font);
1218 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1220 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1221 setCursorVisible(true);
1226 void InsetText::HideInsetCursor(BufferView * bv)
1228 if (isCursorVisible()) {
1229 bv->hideLockedInsetCursor();
1230 setCursorVisible(false);
1232 if (the_locking_inset)
1233 the_locking_inset->HideInsetCursor(bv);
1237 UpdatableInset::RESULT
1238 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1240 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1241 return moveLeftIntern(bv, false, activate_inset, selecting);
1243 return moveRightIntern(bv, false, activate_inset, selecting);
1246 UpdatableInset::RESULT
1247 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1249 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1250 return moveRightIntern(bv, true, activate_inset, selecting);
1252 return moveLeftIntern(bv, true, activate_inset, selecting);
1256 UpdatableInset::RESULT
1257 InsetText::moveRightIntern(BufferView * bv, bool behind,
1258 bool activate_inset, bool selecting)
1260 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1262 if (activate_inset && checkAndActivateInset(bv, behind))
1264 TEXT(bv)->CursorRight(bv);
1266 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1267 return DISPATCHED_NOUPDATE;
1271 UpdatableInset::RESULT
1272 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1273 bool activate_inset, bool selecting)
1275 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1277 TEXT(bv)->CursorLeft(bv);
1279 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1280 if (activate_inset && checkAndActivateInset(bv, behind))
1282 return DISPATCHED_NOUPDATE;
1286 UpdatableInset::RESULT
1287 InsetText::moveUp(BufferView * bv)
1289 if (!crow(bv)->previous())
1291 TEXT(bv)->CursorUp(bv);
1292 return DISPATCHED_NOUPDATE;
1296 UpdatableInset::RESULT
1297 InsetText::moveDown(BufferView * bv)
1299 if (!crow(bv)->next())
1301 TEXT(bv)->CursorDown(bv);
1302 return DISPATCHED_NOUPDATE;
1306 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1308 if (the_locking_inset) {
1309 if (the_locking_inset->InsertInsetAllowed(inset))
1310 return the_locking_inset->InsertInset(bv, inset);
1313 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1314 bv->text->cursor.par()->previous(),
1315 bv->text->cursor.par()->next());
1316 inset->setOwner(this);
1317 HideInsetCursor(bv);
1318 TEXT(bv)->InsertInset(bv, inset);
1320 if ((cpar(bv)->GetChar(cpos(bv)) != LyXParagraph::META_INSET) ||
1321 (cpar(bv)->GetInset(cpos(bv)) != inset))
1322 TEXT(bv)->CursorLeft(bv);
1324 bv->fitCursor(TEXT(bv));
1325 UpdateLocal(bv, CURSOR_PAR|CURSOR, true);
1326 ShowInsetCursor(bv);
1331 UpdatableInset * InsetText::GetLockingInset()
1333 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1337 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1341 if (the_locking_inset)
1342 return the_locking_inset->GetFirstLockingInsetOfType(c);
1347 bool InsetText::ShowInsetDialog(BufferView * bv) const
1349 if (the_locking_inset)
1350 return the_locking_inset->ShowInsetDialog(bv);
1355 std::vector<string> const InsetText::getLabelList() const
1357 std::vector<string> label_list;
1359 LyXParagraph * tpar = par;
1361 LyXParagraph::inset_iterator beg = tpar->inset_iterator_begin();
1362 LyXParagraph::inset_iterator end = tpar->inset_iterator_end();
1363 for (; beg != end; ++beg) {
1364 std::vector<string> const l = (*beg)->getLabelList();
1365 label_list.insert(label_list.end(), l.begin(), l.end());
1367 tpar = tpar->next();
1373 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1375 if (TEXT(bv)->selection) {
1376 bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1377 bv->text->cursor.par()->previous(),
1378 bv->text->cursor.par()->next());
1380 TEXT(bv)->SetFont(bv, font, toggleall);
1381 bv->fitCursor(TEXT(bv));
1382 UpdateLocal(bv, CURSOR_PAR, true);
1386 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1388 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1392 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1393 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1395 LyXFont const font =
1396 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1398 x = inset->width(bv, font);
1399 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1402 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1404 //inset_x = cx(bv) - top_x + drawTextXOffset;
1405 //inset_y = cy(bv) + drawTextYOffset;
1406 inset->Edit(bv, x, y, 0);
1407 if (!the_locking_inset)
1409 UpdateLocal(bv, CURSOR, false);
1416 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1421 dummyx = x = x - drawTextXOffset;
1422 dummyy = y + insetAscent;
1423 Inset * inset = bv->checkInsetHit(TEXT(bv), dummyx, dummyy, button);
1430 inset_x = cx(bv) - top_x + drawTextXOffset;
1431 inset_y = cy(bv) + drawTextYOffset;
1432 inset->Edit(bv, x - inset_x, y - inset_y, button);
1433 if (!the_locking_inset)
1435 UpdateLocal(bv, CURSOR, false);
1442 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1444 int w = UpdatableInset::getMaxWidth(bv, inset);
1449 w = w - top_x + owner()->x();
1452 w -= (2 * TEXT_TO_INSET_OFFSET);
1454 // return w - (2*TEXT_TO_INSET_OFFSET);
1458 void InsetText::SetParagraphData(LyXParagraph * p)
1460 // delete all instances of LyXText before deleting the paragraps used
1462 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
1463 delete (*cit).second;
1468 LyXParagraph * tmp = par->next();
1473 par = new LyXParagraph(*p);
1474 par->SetInsetOwner(this);
1475 LyXParagraph * np = par;
1478 np->next(new LyXParagraph(*p));
1479 np->next()->previous(np);
1481 np->SetInsetOwner(this);
1488 void InsetText::SetText(string const & data)
1491 LyXFont font(LyXFont::ALL_SANE);
1492 for (unsigned int i=0; i < data.length(); ++i)
1493 par->InsertChar(i, data[i], font);
1497 void InsetText::SetAutoBreakRows(bool flag)
1499 if (flag != autoBreakRows) {
1500 autoBreakRows = flag;
1508 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1510 if (how != drawFrame_) {
1513 UpdateLocal(bv, DRAW_FRAME, false);
1518 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1520 if (frame_color != col) {
1523 UpdateLocal(bv, DRAW_FRAME, false);
1528 int InsetText::cx(BufferView * bv) const
1530 LyXText * text = TEXT(bv);
1531 int x = text->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1532 if (the_locking_inset) {
1533 LyXFont font = text->GetFont(bv->buffer(),
1535 text->cursor.pos());
1536 if (font.isVisibleRightToLeft())
1537 x -= the_locking_inset->width(bv, font);
1543 int InsetText::cy(BufferView * bv) const
1546 return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1550 LyXParagraph::size_type InsetText::cpos(BufferView * bv) const
1552 return TEXT(bv)->cursor.pos();
1556 LyXParagraph * InsetText::cpar(BufferView * bv) const
1558 return TEXT(bv)->cursor.par();
1561 bool InsetText::cboundary(BufferView * bv) const
1563 return TEXT(bv)->cursor.boundary();
1567 Row * InsetText::crow(BufferView * bv) const
1569 return TEXT(bv)->cursor.row();
1573 LyXText * InsetText::getLyXText(BufferView const * lbv,
1574 bool const recursive) const
1576 // Super UGLY! (Lgb)
1577 BufferView * bv = const_cast<BufferView *>(lbv);
1579 if ((cache.find(bv) != cache.end()) && cache[bv]) {
1580 if (recursive && the_locking_inset)
1581 return the_locking_inset->getLyXText(bv);
1584 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1587 if (the_locking_inset) {
1588 lt->SetCursor(bv, inset_par, inset_pos, true, inset_boundary);
1590 return the_locking_inset->getLyXText(bv);
1596 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1598 if ((cache.find(bv) == cache.end()) || !cache[bv])
1603 /// then remove all LyXText in text-insets
1604 LyXParagraph * p = par;
1605 for (; p; p = p->next()) {
1606 p->deleteInsetsLyXText(bv);
1612 void InsetText::resizeLyXText(BufferView * bv) const
1614 if (!par->next() && !par->size()) // resize not neccessary!
1616 if ((cache.find(bv) == cache.end()) || !cache[bv])
1619 LyXParagraph * lpar = 0;
1620 LyXParagraph * selstartpar = 0;
1621 LyXParagraph * selendpar = 0;
1622 LyXParagraph::size_type pos = 0;
1623 LyXParagraph::size_type selstartpos = 0;
1624 LyXParagraph::size_type selendpos = 0;
1625 bool boundary = false;
1626 bool selstartboundary = false;
1627 bool selendboundary = false;
1631 // ProhibitInput(bv);
1634 lpar = TEXT(bv)->cursor.par();
1635 pos = TEXT(bv)->cursor.pos();
1636 boundary = TEXT(bv)->cursor.boundary();
1637 selstartpar = TEXT(bv)->sel_start_cursor.par();
1638 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1639 selstartboundary = TEXT(bv)->sel_start_cursor.boundary();
1640 selendpar = TEXT(bv)->sel_end_cursor.par();
1641 selendpos = TEXT(bv)->sel_end_cursor.pos();
1642 selendboundary = TEXT(bv)->sel_end_cursor.boundary();
1643 selection = TEXT(bv)->selection;
1644 mark_set = TEXT(bv)->mark_set;
1646 deleteLyXText(bv, (the_locking_inset == 0));
1649 TEXT(bv)->selection = true;
1650 /* at this point just to avoid the Delete-Empty-Paragraph
1651 * Mechanism when setting the cursor */
1652 TEXT(bv)->mark_set = mark_set;
1654 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos,
1655 true, selstartboundary);
1656 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1657 TEXT(bv)->SetCursor(bv, selendpar, selendpos,
1658 true, selendboundary);
1659 TEXT(bv)->SetSelection(bv);
1660 TEXT(bv)->SetCursor(bv, lpar, pos);
1662 TEXT(bv)->SetCursor(bv, lpar, pos, true, boundary);
1663 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1664 TEXT(bv)->selection = false;
1668 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1669 // this will scroll the screen such that the cursor becomes visible
1670 bv->updateScrollbar();
1672 if (the_locking_inset) {
1673 /// then resize all LyXText in text-insets
1674 inset_x = cx(bv) - top_x + drawTextXOffset;
1675 inset_y = cy(bv) + drawTextYOffset;
1676 for (LyXParagraph * p = par; p; p = p->next()) {
1677 p->resizeInsetsLyXText(bv);
1684 void InsetText::removeNewlines()
1686 for (LyXParagraph * p = par; p; p = p->next()) {
1687 for (int i = 0; i < p->size(); ++i) {
1688 if (p->GetChar(i) == LyXParagraph::META_NEWLINE)
1694 bool InsetText::nodraw() const
1696 if (the_locking_inset)
1697 return the_locking_inset->nodraw();
1698 return UpdatableInset::nodraw();
1701 int InsetText::scroll(bool recursive) const
1703 int sx = UpdatableInset::scroll(false);
1705 if (recursive && the_locking_inset)
1706 sx += the_locking_inset->scroll(recursive);
1711 bool InsetText::doClearArea() const
1713 return !locked || (need_update & (FULL|INIT));