1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998-2001 The LyX Team.
8 * ======================================================
19 #pragma implementation
22 #include "insettext.h"
23 #include "paragraph.h"
27 #include "commandtags.h"
30 #include "BufferView.h"
32 #include "LaTeXFeatures.h"
34 #include "lyx_gui_misc.h"
36 #include "lyxcursor.h"
37 #include "CutAndPaste.h"
40 #include "support/textutils.h"
41 #include "support/LAssert.h"
45 #include "trans_mgr.h"
46 #include "lyxscreen.h"
50 #include "ParagraphParameters.h"
59 extern unsigned char getCurrentTextClass(Buffer *);
60 extern bool math_insert_greek(BufferView *, char);
61 extern int greek_kb_flag;
63 InsetText::InsetText()
70 InsetText::InsetText(InsetText const & ins)
75 autoBreakRows = ins.autoBreakRows;
79 InsetText & InsetText::operator=(InsetText const & it)
82 autoBreakRows = it.autoBreakRows;
87 void InsetText::init(InsetText const * ins)
95 the_locking_inset = 0;
101 autoBreakRows = false;
105 setParagraphData(ins->par);
106 autoBreakRows = ins->autoBreakRows;
107 drawFrame_ = ins->drawFrame_;
109 par->setInsetOwner(this);
110 frame_color = LColor::insetframe;
113 last_drawn_width = -1;
114 frame_is_visible = false;
119 InsetText::~InsetText()
121 // delete all instances of LyXText before deleting the paragraps used
124 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
125 delete (*cit).second;
130 Paragraph * tmp = par->next();
137 void InsetText::clear()
139 // delete all instances of LyXText before deleting the paragraps used
142 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
143 delete (*cit).second;
146 // now also delete all caches this should be safe, hopefully
147 for (Cache::iterator cit = cache.begin(); cit != cache.end();
150 cache.erase((*cit).first);
154 Paragraph * tmp = par->next();
162 Inset * InsetText::clone(Buffer const &) const
164 InsetText * t = new InsetText(*this);
169 void InsetText::write(Buffer const * buf, ostream & os) const
172 writeParagraphData(buf, os);
176 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
178 par->writeFile(buf, os, buf->params, 0);
182 void InsetText::read(Buffer const * buf, LyXLex & lex)
186 Paragraph * return_par = 0;
187 Paragraph::depth_type depth = 0;
188 LyXFont font(LyXFont::ALL_INHERIT);
194 token = lex.GetString();
197 if (token == "\\end_inset")
199 if (const_cast<Buffer*>(buf)->
200 parseSingleLyXformat2Token(lex, par, return_par,
201 token, pos, depth, font)) {
202 // the_end read this should NEVER happen
203 lex.printError("\\the_end read in inset! Error in document!");
211 return_par->setInsetOwner(this);
212 return_par = return_par->next();
215 if (token != "\\end_inset") {
216 lex.printError("Missing \\end_inset at this point. "
223 int InsetText::ascent(BufferView * bv, LyXFont const &) const
226 Row * row = getLyXText(bv)->getRowNearY(y_temp);
227 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
232 int InsetText::descent(BufferView * bv, LyXFont const &) const
234 LyXText * t = getLyXText(bv);
236 Row * row = t->getRowNearY(y_temp);
237 insetDescent = t->height - row->ascent_of_text() +
238 TEXT_TO_INSET_OFFSET;
243 int InsetText::width(BufferView * bv, LyXFont const &) const
245 insetWidth = max(textWidth(bv),
246 (int)getLyXText(bv)->width + (2 * TEXT_TO_INSET_OFFSET));
251 int InsetText::textWidth(BufferView * bv) const
253 int const w = getMaxWidth(bv, this);
258 void InsetText::draw(BufferView * bv, LyXFont const & f,
259 int baseline, float & x, bool cleared) const
264 Painter & pain = bv->painter();
266 // no draw is necessary !!!
267 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
269 top_baseline = baseline;
271 if (!cleared && (need_update & CLEAR_FRAME))
272 clearFrame(pain, cleared);
274 frame_is_visible = false;
281 UpdatableInset::draw(bv, f, baseline, x, cleared);
284 x += static_cast<float>(scroll());
287 // update insetWidth and insetHeight with dummy calls
289 (void)descent(bv, f);
293 // if top_x differs we have a rule down and we don't have to clear anything
294 if (!cleared && (top_x == int(x)) &&
295 ((need_update&(INIT|FULL)) || (top_baseline!=baseline) ||
296 (last_drawn_width!=insetWidth))) {
297 clearInset(pain, baseline, cleared);
300 frame_is_visible = false;
302 if (!cleared && (need_update == NONE))
305 if (top_x != int(x)) {
306 if ((getMaxWidth(bv, this) > 0) &&
307 (getLyXText(bv)->width != old_max_width)) {
310 old_max_width = getLyXText(bv)->width;
311 bv->text->status = LyXText::CHANGED_IN_DRAW;
315 clearInset(pain, baseline, cleared);
321 // lyxerr << "InsetText::draw[" << this << "](" << need_update << ":" << int(x) << ":" << top_x << ")\n";
323 if (cleared || (last_drawn_width != insetWidth)) {
325 last_drawn_width = insetWidth;
328 top_baseline = baseline;
329 top_y = baseline - ascent(bv, f);
330 last_width = width(bv, f);
331 last_height = ascent(bv, f) + descent(bv, f);
333 if (the_locking_inset && (cpar(bv) == inset_par)
334 && (cpos(bv) == inset_pos)) {
335 inset_x = cx(bv) - top_x + drawTextXOffset;
336 inset_y = cy(bv) + drawTextYOffset;
338 if (!cleared && (need_update == CURSOR)
339 && !getLyXText(bv)->selection.set()) {
340 drawFrame(pain, cleared);
341 x += last_width; // was width(bv, f);
345 x += TEXT_TO_INSET_OFFSET;
348 Row * row = getLyXText(bv)->getRowNearY(y);
349 int y_offset = baseline - row->ascent_of_text();
350 int ph = pain.paperHeight();
353 while ((row != 0) && ((y+row->height()) <= 0)) {
355 first += row->height();
360 getLyXText(bv)->first = first;
364 while ((row != 0) && (yf < ph)) {
365 getLyXText(bv)->getVisibleRow(bv, y+y_offset, int(x), row,
371 } else if (!locked) {
372 if (need_update & CURSOR) {
373 bv->screen()->toggleSelection(getLyXText(bv), bv, true, y_offset,int(x));
374 getLyXText(bv)->clearSelection(bv);
375 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
377 bv->screen()->update(getLyXText(bv), bv, y_offset, int(x));
380 if (need_update & SELECTION)
381 bv->screen()->toggleToggle(getLyXText(bv), bv, y_offset, int(x));
382 else if (need_update & CURSOR) {
383 bv->screen()->toggleSelection(getLyXText(bv), bv, true, y_offset,int(x));
384 getLyXText(bv)->clearSelection(bv);
385 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
387 bv->screen()->update(getLyXText(bv), bv, y_offset, int(x));
391 getLyXText(bv)->refresh_y = 0;
392 getLyXText(bv)->status = LyXText::UNCHANGED;
393 if ((need_update != CURSOR_PAR) &&
394 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked)))
395 drawFrame(pain, cleared);
396 else if (need_update & CLEAR_FRAME)
397 clearFrame(pain, cleared);
398 x += last_width /* was width(bv, f) */ - TEXT_TO_INSET_OFFSET;
399 if (bv->text->status==LyXText::CHANGED_IN_DRAW) {
401 } else if (need_update != INIT)
406 void InsetText::drawFrame(Painter & pain, bool cleared) const
408 if (!frame_is_visible || cleared) {
409 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
410 insetWidth - 1, insetAscent + insetDescent - 1,
412 frame_is_visible = true;
417 void InsetText::clearFrame(Painter & pain, bool cleared) const
419 if (frame_is_visible) {
421 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
422 insetWidth - 1, insetAscent + insetDescent - 1,
425 frame_is_visible = false;
430 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
432 LyXText * t = getLyXText(bv);
436 t->BreakParagraph(bv);
444 owner()->update(bv, font, true);
447 if (the_locking_inset) {
448 inset_x = cx(bv) - top_x + drawTextXOffset;
449 inset_y = cy(bv) + drawTextYOffset;
450 the_locking_inset->update(bv, font, reinit);
453 if (need_update == INIT) {
458 int oldw = insetWidth;
459 insetWidth = t->width + (2 * TEXT_TO_INSET_OFFSET);
460 if (oldw != insetWidth) {
463 // update(bv, font, reinit);
466 if ((need_update & CURSOR_PAR) && t->status == LyXText::UNCHANGED &&
468 t->updateInset(bv, the_locking_inset);
471 if (t->status == LyXText::NEED_MORE_REFRESH)
475 Row * row = t->GetRowNearY(y_temp);
476 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
477 insetDescent = t->height - row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
482 void InsetText::setUpdateStatus(BufferView * bv, int what) const
484 LyXText * t = getLyXText(bv);
486 if (t->status == LyXText::NEED_MORE_REFRESH)
488 else if (t->status == LyXText::NEED_VERY_LITTLE_REFRESH)
489 need_update |= CURSOR_PAR;
491 // this to not draw a selection when we redraw all of it!
492 if ((need_update & (INIT|FULL)) && (need_update & CURSOR))
493 t->clearSelection(bv);
497 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
499 LyXText * t = getLyXText(bv);
501 setUpdateStatus(bv, what);
502 if (need_update != CURSOR || t->status != LyXText::UNCHANGED ||
504 bv->updateInset(this, mark_dirty);
505 bv->owner()->showState();
506 if (old_par != cpar(bv)) {
507 bv->owner()->setLayout(cpar(bv)->getLayout());
513 string const InsetText::editMessage() const
515 return _("Opened Text Inset");
519 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
521 // par->SetInsetOwner(this);
522 UpdatableInset::edit(bv, x, y, button);
524 if (!bv->lockInset(this)) {
525 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
529 the_locking_inset = 0;
530 inset_pos = inset_x = inset_y = 0;
531 inset_boundary = false;
534 int tmp_y = (y < 0) ? 0 : y;
535 LyXText * t = getLyXText(bv);
537 if (!checkAndActivateInset(bv, x, tmp_y, button)) {
538 t->setCursorFromCoordinates(bv, x - drawTextXOffset,
542 t->selection.cursor = t->cursor;
543 bv->text->finishUndo();
545 updateLocal(bv, CURSOR, false);
547 // If the inset is empty set the language of the current font to the
548 // language to the surronding text (if different).
549 if (par->size() == 0 && !par->next() &&
550 bv->getParentLanguage(this) != t->current_font.language()) {
551 LyXFont font(LyXFont::ALL_IGNORE);
552 font.setLanguage(bv->getParentLanguage(this));
553 setFont(bv, font, false);
558 void InsetText::insetUnlock(BufferView * bv)
560 if (the_locking_inset) {
561 the_locking_inset->insetUnlock(bv);
562 the_locking_inset = 0;
565 no_selection = false;
567 int code = CURSOR|CLEAR_FRAME;
568 LyXText * t = getLyXText(bv);
569 if (t->selection.set()) {
570 t->clearSelection(bv);
572 } else if (owner()) {
573 bv->owner()->setLayout(owner()->getLyXText(bv)
574 ->cursor.par()->getLayout());
576 bv->owner()->setLayout(bv->text->cursor.par()->getLayout());
577 updateLocal(bv, code, false);
581 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
583 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
587 if (inset == cpar(bv)->getInset(cpos(bv))) {
588 lyxerr[Debug::INSETS] << "OK" << endl;
589 the_locking_inset = inset;
590 inset_x = cx(bv) - top_x + drawTextXOffset;
591 inset_y = cy(bv) + drawTextYOffset;
592 inset_pos = cpos(bv);
593 inset_par = cpar(bv);
594 inset_boundary = cboundary(bv);
596 getLyXText(bv)->clearSelection(bv);
597 getLyXText(bv)->sel_cursor = getLyXText(bv)->cursor;
598 getLyXText(bv)->UpdateInset(bv, the_locking_inset);
600 updateLocal(bv, CURSOR, false);
603 } else if (the_locking_inset && (the_locking_inset == inset)) {
604 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
605 lyxerr[Debug::INSETS] << "OK" << endl;
606 inset_x = cx(bv) - top_x + drawTextXOffset;
607 inset_y = cy(bv) + drawTextYOffset;
609 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
611 } else if (the_locking_inset) {
612 lyxerr[Debug::INSETS] << "MAYBE" << endl;
613 return the_locking_inset->lockInsetInInset(bv, inset);
615 lyxerr[Debug::INSETS] << "NOT OK" << endl;
620 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
623 if (!the_locking_inset)
625 if (the_locking_inset == inset) {
626 the_locking_inset->insetUnlock(bv);
627 getLyXText(bv)->updateInset(bv, inset);
628 the_locking_inset = 0;
630 moveRight(bv, false);
631 old_par = 0; // force layout setting
635 updateLocal(bv, CURSOR, false);
638 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
642 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
644 if (!the_locking_inset)
646 if (the_locking_inset != inset) {
647 getLyXText(bv)->updateInset(bv, the_locking_inset);
648 setUpdateStatus(bv, CURSOR_PAR);
649 return the_locking_inset->updateInsetInInset(bv, inset);
651 // updateLocal(bv, FULL, false);
652 if (getLyXText(bv)->updateInset(bv, inset))
653 updateLocal(bv, CURSOR_PAR, false);
654 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
655 inset_x = cx(bv) - top_x + drawTextXOffset;
656 inset_y = cy(bv) + drawTextYOffset;
662 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
666 int tmp_x = x - drawTextXOffset;
667 int tmp_y = y + insetAscent - getLyXText(bv)->first;
668 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y, button);
671 if (the_locking_inset) {
672 if (the_locking_inset == inset) {
673 the_locking_inset->insetButtonPress(bv,x-inset_x,y-inset_y,button);
674 no_selection = false;
677 // otherwise unlock the_locking_inset and lock the new inset
678 the_locking_inset->insetUnlock(bv);
679 inset_x = cx(bv) - top_x + drawTextXOffset;
680 inset_y = cy(bv) + drawTextYOffset;
681 the_locking_inset = static_cast<UpdatableInset*>(inset);
682 inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
683 inset->edit(bv, x - inset_x, y - inset_y, button);
684 if (the_locking_inset)
685 updateLocal(bv, CURSOR, false);
686 no_selection = false;
689 // otherwise only unlock the_locking_inset
690 the_locking_inset->insetUnlock(bv);
691 the_locking_inset = 0;
693 if (bv->theLockingInset()) {
694 if (inset && inset->editable() == Inset::HIGHLY_EDITABLE) {
695 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
696 inset_x = cx(bv) - top_x + drawTextXOffset;
697 inset_y = cy(bv) + drawTextYOffset;
698 inset_pos = cpos(bv);
699 inset_par = cpar(bv);
700 inset_boundary = cboundary(bv);
701 the_locking_inset = uinset;
702 uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
704 uinset->edit(bv, x - inset_x, y - inset_y, 0);
705 if (the_locking_inset)
706 updateLocal(bv, CURSOR, false);
707 no_selection = false;
711 if (!inset) { // && (button == 2)) {
712 bool paste_internally = false;
713 if ((button == 2) && getLyXText(bv)->selection.set()) {
714 localDispatch(bv, LFUN_COPY, "");
715 paste_internally = true;
717 getLyXText(bv)->setCursorFromCoordinates(bv, x-drawTextXOffset,
719 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
720 updateLocal(bv, CURSOR, false);
721 bv->owner()->setLayout(cpar(bv)->getLayout());
723 // Insert primary selection with middle mouse
724 // if there is a local selection in the current buffer,
727 if (paste_internally)
728 localDispatch(bv, LFUN_PASTE, "");
730 localDispatch(bv, LFUN_PASTESELECTION,
735 no_selection = false;
739 void InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
741 UpdatableInset * inset = 0;
743 if (the_locking_inset) {
744 the_locking_inset->insetButtonRelease(bv,
745 x - inset_x, y - inset_y,
748 if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
749 inset = static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
750 if (inset->editable() == Inset::HIGHLY_EDITABLE) {
751 inset->insetButtonRelease(bv,
753 y - inset_y, button);
755 inset_x = cx(bv) - top_x + drawTextXOffset;
756 inset_y = cy(bv) + drawTextYOffset;
757 inset->insetButtonRelease(bv,
759 y - inset_y, button);
761 x - inset_x, y - inset_y, button);
763 updateLocal(bv, CURSOR_PAR, false);
766 no_selection = false;
770 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
774 if (the_locking_inset) {
775 the_locking_inset->insetMotionNotify(bv, x - inset_x,
779 LyXText * t = getLyXText(bv);
781 t->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
783 if (t->toggle_cursor.par() != t->toggle_end_cursor.par() ||
784 t->toggle_cursor.pos() != t->toggle_end_cursor.pos())
785 updateLocal(bv, SELECTION, false);
790 void InsetText::insetKeyPress(XKeyEvent * xke)
792 if (the_locking_inset) {
793 the_locking_inset->insetKeyPress(xke);
799 UpdatableInset::RESULT
800 InsetText::localDispatch(BufferView * bv,
801 kb_action action, string const & arg)
803 no_selection = false;
804 UpdatableInset::RESULT
805 result= UpdatableInset::localDispatch(bv, action, arg);
806 if (result != UNDISPATCHED) {
811 if ((action < 0) && arg.empty())
814 if (the_locking_inset) {
815 result = the_locking_inset->localDispatch(bv, action, arg);
816 if (result == DISPATCHED_NOUPDATE)
818 else if (result == DISPATCHED) {
819 updateLocal(bv, CURSOR_PAR, false);
821 } else if (result == FINISHED) {
822 bool dispatched = false;
824 case LFUN_UNKNOWN_ACTION:
825 case LFUN_BREAKPARAGRAPH:
827 moveRightIntern(bv, false, false);
830 if (!getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
831 moveRightIntern(bv, false, false);
835 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
836 moveRightIntern(bv, false, false);
842 the_locking_inset = 0;
850 case LFUN_SELFINSERT:
851 if (bv->buffer()->isReadonly()) {
852 // setErrorMessage(N_("Document is read only"));
856 /* Automatically delete the currently selected
857 * text and replace it with what is being
858 * typed in now. Depends on lyxrc settings
859 * "auto_region_delete", which defaults to
862 bv->text->setUndo(bv->buffer(), Undo::INSERT,
863 bv->text->cursor.par()->previous(),
864 bv->text->cursor.par()->next());
866 if (lyxrc.auto_region_delete) {
867 if (getLyXText(bv)->selection.set()) {
868 getLyXText(bv)->cutSelection(bv, false);
871 getLyXText(bv)->clearSelection(bv);
872 for (string::size_type i = 0; i < arg.length(); ++i) {
874 if (!math_insert_greek(bv, arg[i])) {
875 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], getLyXText(bv));
876 } else if (!the_locking_inset) {
877 (void)moveRight(bv, false);
880 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], getLyXText(bv));
884 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
885 updateLocal(bv, CURSOR_PAR, true);
886 result = DISPATCHED_NOUPDATE;
888 // --- Cursor Movements -----------------------------------
890 bv->text->finishUndo();
891 moveRight(bv, false, true);
892 getLyXText(bv)->setSelection(bv);
893 updateLocal(bv, SELECTION, false);
896 result = moveRight(bv);
897 bv->text->finishUndo();
898 updateLocal(bv, CURSOR, false);
901 bv->text->finishUndo();
902 moveLeft(bv, false, true);
903 getLyXText(bv)->setSelection(bv);
904 updateLocal(bv, SELECTION, false);
907 bv->text->finishUndo();
908 result = moveLeft(bv);
909 updateLocal(bv, CURSOR, false);
912 bv->text->finishUndo();
914 getLyXText(bv)->setSelection(bv);
915 updateLocal(bv, SELECTION, false);
918 bv->text->finishUndo();
919 result = moveDown(bv);
920 updateLocal(bv, CURSOR, false);
923 bv->text->finishUndo();
925 getLyXText(bv)->setSelection(bv);
926 updateLocal(bv, SELECTION, false);
929 bv->text->finishUndo();
931 updateLocal(bv, CURSOR, false);
934 bv->text->finishUndo();
935 getLyXText(bv)->cursorHome(bv);
936 updateLocal(bv, CURSOR, false);
939 getLyXText(bv)->cursorEnd(bv);
940 updateLocal(bv, CURSOR, false);
942 case LFUN_BACKSPACE: {
943 bv->text->setUndo(bv->buffer(), Undo::DELETE,
944 bv->text->cursor.par()->previous(),
945 bv->text->cursor.par()->next());
946 LyXText * t = getLyXText(bv);
948 if (t->selection.set()) {
954 updateLocal(bv, CURSOR_PAR, true);
960 bv->text->setUndo(bv->buffer(), Undo::DELETE,
961 bv->text->cursor.par()->previous(),
962 bv->text->cursor.par()->next());
963 LyXText * t = getLyXText(bv);
965 if (t->selection.set()) {
970 updateLocal(bv, CURSOR_PAR, true);
975 bv->text->setUndo(bv->buffer(), Undo::DELETE,
976 bv->text->cursor.par()->previous(),
977 bv->text->cursor.par()->next());
978 getLyXText(bv)->cutSelection(bv);
979 updateLocal(bv, CURSOR_PAR, true);
982 bv->text->finishUndo();
983 getLyXText(bv)->copySelection(bv);
984 updateLocal(bv, CURSOR_PAR, false);
986 case LFUN_PASTESELECTION:
988 string const clip(bv->getClipboard());
992 if (arg == "paragraph") {
993 getLyXText(bv)->insertStringAsParagraphs(bv, clip);
995 getLyXText(bv)->insertStringAsLines(bv, clip);
997 updateLocal(bv, CURSOR_PAR, true);
1001 if (!autoBreakRows) {
1003 if (CutAndPaste::nrOfParagraphs() > 1) {
1004 WriteAlert(_("Impossible operation"),
1005 _("Cannot include more than one paragraph!"),
1010 bv->text->setUndo(bv->buffer(), Undo::INSERT,
1011 bv->text->cursor.par()->previous(),
1012 bv->text->cursor.par()->next());
1013 getLyXText(bv)->pasteSelection(bv);
1014 updateLocal(bv, CURSOR_PAR, true);
1016 case LFUN_BREAKPARAGRAPH:
1019 getLyXText(bv)->breakParagraph(bv, 0);
1020 updateLocal(bv, FULL, true);
1022 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1025 getLyXText(bv)->breakParagraph(bv, 1);
1026 updateLocal(bv, FULL, true);
1028 case LFUN_BREAKLINE:
1031 bv->text->setUndo(bv->buffer(), Undo::INSERT,
1032 bv->text->cursor.par()->previous(),
1033 bv->text->cursor.par()->next());
1034 getLyXText(bv)->insertChar(bv, Paragraph::META_NEWLINE);
1035 updateLocal(bv, CURSOR_PAR, true);
1038 // do not set layouts on non breakable textinsets
1039 if (autoBreakRows) {
1040 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1042 // Derive layout number from given argument (string)
1043 // and current buffer's textclass (number). */
1044 LyXTextClassList::ClassList::size_type tclass =
1045 bv->buffer()->params.textclass;
1046 std::pair <bool, LyXTextClass::size_type> layout =
1047 textclasslist.NumberOfLayout(tclass, arg);
1049 // If the entry is obsolete, use the new one instead.
1051 string obs = textclasslist.Style(tclass,layout.second).
1054 layout = textclasslist.NumberOfLayout(tclass, obs);
1057 // see if we found the layout number:
1058 if (!layout.first) {
1059 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1060 bv->owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, msg);
1064 if (cur_layout != layout.second) {
1065 cur_layout = layout.second;
1066 getLyXText(bv)->setLayout(bv, layout.second);
1067 bv->owner()->setLayout(cpar(bv)->getLayout());
1068 updateLocal(bv, CURSOR_PAR, true);
1071 // reset the layout box
1072 bv->owner()->setLayout(cpar(bv)->getLayout());
1075 case LFUN_PARAGRAPH_SPACING:
1076 // This one is absolutely not working. When fiddling with this
1077 // it also seems to me that the paragraphs inside the insettext
1078 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1080 Paragraph * par = getLyXText(bv)->cursor.par();
1081 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1082 float cur_value = 1.0;
1083 if (cur_spacing == Spacing::Other) {
1084 cur_value = par->params().spacing().getValue();
1087 std::istringstream istr(arg.c_str());
1090 Spacing::Space new_spacing = cur_spacing;
1091 float new_value = cur_value;
1093 lyxerr << "Missing argument to `paragraph-spacing'"
1095 } else if (tmp == "single") {
1096 new_spacing = Spacing::Single;
1097 } else if (tmp == "onehalf") {
1098 new_spacing = Spacing::Onehalf;
1099 } else if (tmp == "double") {
1100 new_spacing = Spacing::Double;
1101 } else if (tmp == "other") {
1102 new_spacing = Spacing::Other;
1105 lyxerr << "new_value = " << tmpval << endl;
1108 } else if (tmp == "default") {
1109 new_spacing = Spacing::Default;
1111 lyxerr << _("Unknown spacing argument: ")
1114 if (cur_spacing != new_spacing || cur_value != new_value) {
1115 par->params().spacing(Spacing(new_spacing, new_value));
1116 //getLyXText(bv)->RedoParagraph(owner->view());
1117 updateLocal(bv, CURSOR_PAR, true);
1118 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1124 if (!bv->Dispatch(action, arg))
1125 result = UNDISPATCHED;
1129 /// If the action has deleted all text in the inset, we need to change the
1130 // language to the language to the surronding text.
1131 if (par->size() == 0 && !par->next()) {
1132 LyXFont font(LyXFont::ALL_IGNORE);
1133 font.setLanguage(bv->getParentLanguage(this));
1134 setFont(bv, font, false);
1137 if (result != FINISHED) {
1138 showInsetCursor(bv);
1140 bv->unlockInset(this);
1145 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1148 buf->latexParagraphs(os, par, 0, texrow);
1149 return texrow.rows();
1153 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1155 Paragraph * p = par;
1156 unsigned int lines = 0;
1159 string const tmp = buf->asciiParagraph(p, linelen);
1160 lines += countChar(tmp, '\n');
1168 int InsetText::docBook(Buffer const * buf, ostream & os) const
1170 Paragraph * p = par;
1171 unsigned int lines = 0;
1176 buf->simpleDocBookOnePar(os, tmp, p, desc, 0);
1184 void InsetText::validate(LaTeXFeatures & features) const
1186 Paragraph * p = par;
1188 p->validate(features);
1194 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1196 if (textclasslist.Style(buf->params.textclass,
1197 p->getLayout()).labeltype != LABEL_MANUAL)
1200 return p->beginningOfMainBody();
1204 void InsetText::getCursorPos(BufferView * bv,
1205 int & x, int & y) const
1212 unsigned int InsetText::insetInInsetY()
1214 if (!the_locking_inset)
1217 return (inset_y + the_locking_inset->insetInInsetY());
1221 void InsetText::toggleInsetCursor(BufferView * bv)
1223 if (the_locking_inset) {
1224 the_locking_inset->toggleInsetCursor(bv);
1228 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1230 int const asc = lyxfont::maxAscent(font);
1231 int const desc = lyxfont::maxDescent(font);
1233 if (isCursorVisible())
1234 bv->hideLockedInsetCursor();
1236 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1237 toggleCursorVisible();
1241 void InsetText::showInsetCursor(BufferView * bv, bool show)
1243 if (the_locking_inset) {
1244 the_locking_inset->showInsetCursor(bv);
1247 if (!isCursorVisible()) {
1248 LyXFont const font =
1249 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1251 int const asc = lyxfont::maxAscent(font);
1252 int const desc = lyxfont::maxDescent(font);
1254 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1256 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1257 setCursorVisible(true);
1262 void InsetText::hideInsetCursor(BufferView * bv)
1264 if (isCursorVisible()) {
1265 bv->hideLockedInsetCursor();
1266 setCursorVisible(false);
1268 if (the_locking_inset)
1269 the_locking_inset->hideInsetCursor(bv);
1273 UpdatableInset::RESULT
1274 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1276 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1277 return moveLeftIntern(bv, false, activate_inset, selecting);
1279 return moveRightIntern(bv, false, activate_inset, selecting);
1283 UpdatableInset::RESULT
1284 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1286 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1287 return moveRightIntern(bv, true, activate_inset, selecting);
1289 return moveLeftIntern(bv, true, activate_inset, selecting);
1293 UpdatableInset::RESULT
1294 InsetText::moveRightIntern(BufferView * bv, bool behind,
1295 bool activate_inset, bool selecting)
1297 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1299 if (activate_inset && checkAndActivateInset(bv, behind))
1301 getLyXText(bv)->cursorRight(bv);
1303 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1304 return DISPATCHED_NOUPDATE;
1308 UpdatableInset::RESULT
1309 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1310 bool activate_inset, bool selecting)
1312 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1314 getLyXText(bv)->cursorLeft(bv);
1316 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1317 if (activate_inset && checkAndActivateInset(bv, behind))
1319 return DISPATCHED_NOUPDATE;
1323 UpdatableInset::RESULT
1324 InsetText::moveUp(BufferView * bv)
1326 if (!crow(bv)->previous())
1328 getLyXText(bv)->cursorUp(bv);
1329 return DISPATCHED_NOUPDATE;
1333 UpdatableInset::RESULT
1334 InsetText::moveDown(BufferView * bv)
1336 if (!crow(bv)->next())
1338 getLyXText(bv)->cursorDown(bv);
1339 return DISPATCHED_NOUPDATE;
1343 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1345 if (the_locking_inset) {
1346 if (the_locking_inset->insertInsetAllowed(inset))
1347 return the_locking_inset->insertInset(bv, inset);
1350 bv->text->setUndo(bv->buffer(), Undo::INSERT,
1351 bv->text->cursor.par()->previous(),
1352 bv->text->cursor.par()->next());
1353 inset->setOwner(this);
1354 hideInsetCursor(bv);
1355 getLyXText(bv)->insertInset(bv, inset);
1357 if ((cpar(bv)->GetChar(cpos(bv)) != Paragraph::META_INSET) ||
1358 (cpar(bv)->GetInset(cpos(bv)) != inset))
1359 getLyXText(bv)->CursorLeft(bv);
1361 bv->fitCursor(getLyXText(bv));
1362 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1363 showInsetCursor(bv);
1368 UpdatableInset * InsetText::getLockingInset()
1370 return the_locking_inset ? the_locking_inset->getLockingInset() : this;
1374 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1378 if (the_locking_inset)
1379 return the_locking_inset->getFirstLockingInsetOfType(c);
1384 bool InsetText::showInsetDialog(BufferView * bv) const
1386 if (the_locking_inset)
1387 return the_locking_inset->showInsetDialog(bv);
1392 std::vector<string> const InsetText::getLabelList() const
1394 std::vector<string> label_list;
1396 Paragraph * tpar = par;
1398 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1399 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1400 for (; beg != end; ++beg) {
1401 std::vector<string> const l = (*beg)->getLabelList();
1402 label_list.insert(label_list.end(), l.begin(), l.end());
1404 tpar = tpar->next();
1410 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1413 if (the_locking_inset) {
1414 the_locking_inset->setFont(bv, font, toggleall, selectall);
1417 if (getLyXText(bv)->selection.set()) {
1418 bv->text->setUndo(bv->buffer(), Undo::EDIT,
1419 bv->text->cursor.par()->previous(),
1420 bv->text->cursor.par()->next());
1424 getLyXText(bv)->setFont(bv, font, toggleall);
1426 getLyXText(bv)->clearSelection(bv);
1427 bv->fitCursor(getLyXText(bv));
1428 if (selectall || getLyXText(bv)->selection.set())
1429 updateLocal(bv, FULL, true);
1431 updateLocal(bv, CURSOR_PAR, true);
1435 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1437 if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
1441 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1442 if (!inset || inset->editable() != Inset::HIGHLY_EDITABLE)
1444 LyXFont const font =
1445 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1447 x = inset->width(bv, font);
1448 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1451 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1453 //inset_x = cx(bv) - top_x + drawTextXOffset;
1454 //inset_y = cy(bv) + drawTextYOffset;
1455 inset->edit(bv, x, y, 0);
1456 if (!the_locking_inset)
1458 updateLocal(bv, CURSOR, false);
1465 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1468 x -= drawTextXOffset;
1470 int dummyy = y + insetAscent;
1471 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy, button);
1478 inset_x = cx(bv) - top_x + drawTextXOffset;
1479 inset_y = cy(bv) + drawTextYOffset;
1480 inset->edit(bv, x - inset_x, y - inset_y, button);
1481 if (!the_locking_inset)
1483 updateLocal(bv, CURSOR, false);
1490 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1492 int w = UpdatableInset::getMaxWidth(bv, inset);
1497 w = w - top_x + owner()->x();
1500 w -= (2 * TEXT_TO_INSET_OFFSET);
1502 // return w - (2*TEXT_TO_INSET_OFFSET);
1506 void InsetText::setParagraphData(Paragraph * p)
1508 // delete all instances of LyXText before deleting the paragraps used
1511 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
1512 delete (*cit).second;
1515 // now also delete all caches this should be safe, hopefully
1516 for (Cache::iterator cit = cache.begin(); cit != cache.end();
1517 cit = cache.begin())
1519 cache.erase((*cit).first);
1523 Paragraph * tmp = par->next();
1528 par = new Paragraph(*p);
1529 par->setInsetOwner(this);
1530 Paragraph * np = par;
1533 np->next(new Paragraph(*p));
1534 np->next()->previous(np);
1536 np->setInsetOwner(this);
1543 void InsetText::setText(string const & data)
1546 LyXFont font(LyXFont::ALL_SANE);
1547 for (unsigned int i=0; i < data.length(); ++i)
1548 par->insertChar(i, data[i], font);
1552 void InsetText::setAutoBreakRows(bool flag)
1554 if (flag != autoBreakRows) {
1555 autoBreakRows = flag;
1563 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1565 if (how != drawFrame_) {
1568 updateLocal(bv, DRAW_FRAME, false);
1573 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1575 if (frame_color != col) {
1578 updateLocal(bv, DRAW_FRAME, false);
1583 int InsetText::cx(BufferView * bv) const
1585 LyXText * text = getLyXText(bv);
1586 int x = text->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1587 if (the_locking_inset) {
1588 LyXFont font = text->getFont(bv->buffer(),
1590 text->cursor.pos());
1591 if (font.isVisibleRightToLeft())
1592 x -= the_locking_inset->width(bv, font);
1598 int InsetText::cy(BufferView * bv) const
1601 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1605 Paragraph::size_type InsetText::cpos(BufferView * bv) const
1607 return getLyXText(bv)->cursor.pos();
1611 Paragraph * InsetText::cpar(BufferView * bv) const
1613 return getLyXText(bv)->cursor.par();
1617 bool InsetText::cboundary(BufferView * bv) const
1619 return getLyXText(bv)->cursor.boundary();
1623 Row * InsetText::crow(BufferView * bv) const
1625 return getLyXText(bv)->cursor.row();
1629 LyXText * InsetText::getLyXText(BufferView const * lbv,
1630 bool const recursive) const
1632 if (!recursive && (cached_bview == lbv))
1635 // Super UGLY! (Lgb)
1636 BufferView * bv = const_cast<BufferView *>(lbv);
1639 Cache::iterator it = cache.find(bv);
1641 if (it != cache.end()) {
1642 lyx::Assert(it->second);
1644 cached_text = it->second;
1645 if (recursive && the_locking_inset) {
1646 return the_locking_inset->getLyXText(bv);
1650 cached_text = new LyXText(const_cast<InsetText *>(this));
1651 cached_text->init(bv);
1653 cache.insert(make_pair(bv, cached_text));
1655 if (the_locking_inset) {
1656 cached_text->setCursor(bv, inset_par, inset_pos,
1657 true, inset_boundary);
1659 return the_locking_inset->getLyXText(bv);
1666 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1670 Cache::iterator it = cache.find(bv);
1672 if (it == cache.end()) {
1676 lyx::Assert(it->second);
1681 /// then remove all LyXText in text-insets
1682 Paragraph * p = par;
1683 for (; p; p = p->next()) {
1684 p->deleteInsetsLyXText(bv);
1690 void InsetText::resizeLyXText(BufferView * bv, bool force) const
1692 if (!par->next() && !par->size()) // no data, resize not neccessary!
1694 // one endless line, resize normally not necessary
1695 if (!force && getMaxWidth(bv, this) < 0)
1698 Cache::iterator it = cache.find(bv);
1699 if (it == cache.end()) {
1702 lyx::Assert(it->second);
1704 Paragraph * lpar = 0;
1705 Paragraph * selstartpar = 0;
1706 Paragraph * selendpar = 0;
1707 Paragraph::size_type pos = 0;
1708 Paragraph::size_type selstartpos = 0;
1709 Paragraph::size_type selendpos = 0;
1710 bool boundary = false;
1711 bool selstartboundary = false;
1712 bool selendboundary = false;
1713 bool selection = false;
1714 bool mark_set = false;
1716 // bv->owner()->prohibitInput();
1719 LyXText * t = getLyXText(bv);
1720 lpar = t->cursor.par();
1721 pos = t->cursor.pos();
1722 boundary = t->cursor.boundary();
1723 selstartpar = t->selection.start.par();
1724 selstartpos = t->selection.start.pos();
1725 selstartboundary = t->selection.start.boundary();
1726 selendpar = t->selection.end.par();
1727 selendpos = t->selection.end.pos();
1728 selendboundary = t->selection.end.boundary();
1729 selection = t->selection.set();
1730 mark_set = t->selection.mark();
1732 deleteLyXText(bv, (the_locking_inset == 0) || force);
1735 LyXText * t = getLyXText(bv);
1737 t->selection.set(true);
1738 /* at this point just to avoid the Delete-Empty-Paragraph
1739 * Mechanism when setting the cursor */
1740 t->selection.mark(mark_set);
1742 t->setCursor(bv, selstartpar, selstartpos, true, selstartboundary);
1743 t->selection.cursor = t->cursor;
1744 t->setCursor(bv, selendpar, selendpos, true, selendboundary);
1745 t->setSelection(bv);
1746 t->setCursor(bv, lpar, pos);
1748 t->setCursor(bv, lpar, pos, true, boundary);
1749 t->selection.cursor = t->cursor;
1750 t->selection.set(false);
1754 LyXText * t = getLyXText(bv);
1755 t->first = bv->screen()->topCursorVisible(t);
1758 // this will scroll the screen such that the cursor becomes visible
1759 bv->updateScrollbar();
1760 // bv->owner()->allowInput();
1761 if (the_locking_inset) {
1762 /// then resize all LyXText in text-insets
1763 inset_x = cx(bv) - top_x + drawTextXOffset;
1764 inset_y = cy(bv) + drawTextYOffset;
1765 for (Paragraph * p = par; p; p = p->next()) {
1766 p->resizeInsetsLyXText(bv);
1773 void InsetText::removeNewlines()
1775 for (Paragraph * p = par; p; p = p->next()) {
1776 for (int i = 0; i < p->size(); ++i) {
1777 if (p->getChar(i) == Paragraph::META_NEWLINE)
1784 bool InsetText::nodraw() const
1786 if (the_locking_inset)
1787 return the_locking_inset->nodraw();
1788 return UpdatableInset::nodraw();
1792 int InsetText::scroll(bool recursive) const
1794 int sx = UpdatableInset::scroll(false);
1796 if (recursive && the_locking_inset)
1797 sx += the_locking_inset->scroll(recursive);
1803 bool InsetText::doClearArea() const
1805 return !locked || (need_update & (FULL|INIT));
1809 void InsetText::selectAll(BufferView * bv)
1811 getLyXText(bv)->cursorTop(bv);
1812 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1813 getLyXText(bv)->cursorBottom(bv);
1814 getLyXText(bv)->setSelection(bv);
1818 void InsetText::clearSelection(BufferView * bv)
1820 getLyXText(bv)->clearSelection(bv);
1824 void InsetText::clearInset(Painter & pain, int baseline, bool & cleared) const
1827 int h = insetAscent + insetDescent;
1828 int ty = baseline - insetAscent;
1834 if ((ty + h) > pain.paperHeight())
1835 h = pain.paperHeight();
1836 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
1837 w = pain.paperWidth();
1838 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);