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;
148 Paragraph * tmp = par->next();
156 Inset * InsetText::clone(Buffer const &) const
158 InsetText * t = new InsetText(*this);
163 void InsetText::write(Buffer const * buf, ostream & os) const
166 writeParagraphData(buf, os);
170 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
172 par->writeFile(buf, os, buf->params, 0);
176 void InsetText::read(Buffer const * buf, LyXLex & lex)
180 Paragraph * return_par = 0;
181 Paragraph::depth_type depth = 0;
182 LyXFont font(LyXFont::ALL_INHERIT);
188 token = lex.GetString();
191 if (token == "\\end_inset")
193 if (const_cast<Buffer*>(buf)->
194 parseSingleLyXformat2Token(lex, par, return_par,
195 token, pos, depth, font)) {
196 // the_end read this should NEVER happen
197 lex.printError("\\the_end read in inset! Error in document!");
205 return_par->setInsetOwner(this);
206 return_par = return_par->next();
209 if (token != "\\end_inset") {
210 lex.printError("Missing \\end_inset at this point. "
217 int InsetText::ascent(BufferView * bv, LyXFont const &) const
220 Row * row = getLyXText(bv)->getRowNearY(y_temp);
221 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
226 int InsetText::descent(BufferView * bv, LyXFont const &) const
228 LyXText * t = getLyXText(bv);
230 Row * row = t->getRowNearY(y_temp);
231 insetDescent = t->height - row->ascent_of_text() +
232 TEXT_TO_INSET_OFFSET;
237 int InsetText::width(BufferView * bv, LyXFont const &) const
239 insetWidth = max(textWidth(bv),
240 (int)getLyXText(bv)->width + (2 * TEXT_TO_INSET_OFFSET));
245 int InsetText::textWidth(BufferView * bv) const
247 int const w = getMaxWidth(bv, this);
252 void InsetText::draw(BufferView * bv, LyXFont const & f,
253 int baseline, float & x, bool cleared) const
258 Painter & pain = bv->painter();
260 // no draw is necessary !!!
261 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
263 top_baseline = baseline;
265 if (!cleared && (need_update & CLEAR_FRAME))
266 clearFrame(pain, cleared);
268 frame_is_visible = false;
275 UpdatableInset::draw(bv, f, baseline, x, cleared);
278 x += static_cast<float>(scroll());
281 // update insetWidth and insetHeight with dummy calls
283 (void)descent(bv, f);
287 // if top_x differs we have a rule down and we don't have to clear anything
288 if (!cleared && (top_x == int(x)) &&
289 ((need_update&(INIT|FULL)) || (top_baseline!=baseline) ||
290 (last_drawn_width!=insetWidth))) {
291 clearInset(pain, baseline, cleared);
294 frame_is_visible = false;
296 if (!cleared && (need_update == NONE))
299 if (top_x != int(x)) {
300 if ((getMaxWidth(bv, this) > 0) &&
301 (getLyXText(bv)->width != old_max_width)) {
304 old_max_width = getLyXText(bv)->width;
305 bv->text->status = LyXText::CHANGED_IN_DRAW;
309 clearInset(pain, baseline, cleared);
315 // lyxerr << "InsetText::draw[" << this << "](" << need_update << ":" << int(x) << ":" << top_x << ")\n";
317 if (cleared || (last_drawn_width != insetWidth)) {
319 last_drawn_width = insetWidth;
322 top_baseline = baseline;
323 top_y = baseline - ascent(bv, f);
324 last_width = width(bv, f);
325 last_height = ascent(bv, f) + descent(bv, f);
327 if (the_locking_inset && (cpar(bv) == inset_par)
328 && (cpos(bv) == inset_pos)) {
329 inset_x = cx(bv) - top_x + drawTextXOffset;
330 inset_y = cy(bv) + drawTextYOffset;
332 if (!cleared && (need_update == CURSOR)
333 && !getLyXText(bv)->selection.set()) {
334 drawFrame(pain, cleared);
335 x += last_width; // was width(bv, f);
339 x += TEXT_TO_INSET_OFFSET;
342 Row * row = getLyXText(bv)->getRowNearY(y);
343 int y_offset = baseline - row->ascent_of_text();
344 int ph = pain.paperHeight();
347 while ((row != 0) && ((y+row->height()) <= 0)) {
349 first += row->height();
354 getLyXText(bv)->first = first;
358 while ((row != 0) && (yf < ph)) {
359 getLyXText(bv)->getVisibleRow(bv, y+y_offset, int(x), row,
365 } else if (!locked) {
366 if (need_update & CURSOR) {
367 bv->screen()->toggleSelection(getLyXText(bv), bv, true, y_offset,int(x));
368 getLyXText(bv)->clearSelection(bv);
369 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
371 bv->screen()->update(getLyXText(bv), bv, y_offset, int(x));
374 if (need_update & SELECTION)
375 bv->screen()->toggleToggle(getLyXText(bv), bv, y_offset, int(x));
376 else if (need_update & CURSOR) {
377 bv->screen()->toggleSelection(getLyXText(bv), bv, true, y_offset,int(x));
378 getLyXText(bv)->clearSelection(bv);
379 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
381 bv->screen()->update(getLyXText(bv), bv, y_offset, int(x));
385 getLyXText(bv)->refresh_y = 0;
386 getLyXText(bv)->status = LyXText::UNCHANGED;
387 if ((need_update != CURSOR_PAR) &&
388 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked)))
389 drawFrame(pain, cleared);
390 else if (need_update & CLEAR_FRAME)
391 clearFrame(pain, cleared);
392 x += last_width /* was width(bv, f) */ - TEXT_TO_INSET_OFFSET;
393 if (bv->text->status==LyXText::CHANGED_IN_DRAW) {
395 } else if (need_update != INIT)
400 void InsetText::drawFrame(Painter & pain, bool cleared) const
402 if (!frame_is_visible || cleared) {
403 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
404 insetWidth - 1, insetAscent + insetDescent - 1,
406 frame_is_visible = true;
411 void InsetText::clearFrame(Painter & pain, bool cleared) const
413 if (frame_is_visible) {
415 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
416 insetWidth - 1, insetAscent + insetDescent - 1,
419 frame_is_visible = false;
424 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
426 LyXText * t = getLyXText(bv);
430 t->BreakParagraph(bv);
438 owner()->update(bv, font, true);
441 if (the_locking_inset) {
442 inset_x = cx(bv) - top_x + drawTextXOffset;
443 inset_y = cy(bv) + drawTextYOffset;
444 the_locking_inset->update(bv, font, reinit);
447 if (need_update == INIT) {
452 int oldw = insetWidth;
453 insetWidth = t->width + (2 * TEXT_TO_INSET_OFFSET);
454 if (oldw != insetWidth) {
457 // update(bv, font, reinit);
460 if ((need_update & CURSOR_PAR) && t->status == LyXText::UNCHANGED &&
462 t->updateInset(bv, the_locking_inset);
465 if (t->status == LyXText::NEED_MORE_REFRESH)
469 Row * row = t->GetRowNearY(y_temp);
470 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
471 insetDescent = t->height - row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
476 void InsetText::setUpdateStatus(BufferView * bv, int what) const
478 LyXText * t = getLyXText(bv);
480 if (t->status == LyXText::NEED_MORE_REFRESH)
482 else if (t->status == LyXText::NEED_VERY_LITTLE_REFRESH)
483 need_update |= CURSOR_PAR;
485 // this to not draw a selection when we redraw all of it!
486 if ((need_update & (INIT|FULL)) && (need_update & CURSOR))
487 t->clearSelection(bv);
491 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
493 LyXText * t = getLyXText(bv);
495 setUpdateStatus(bv, what);
496 if (need_update != CURSOR || t->status != LyXText::UNCHANGED ||
498 bv->updateInset(this, mark_dirty);
499 bv->owner()->showState();
500 if (old_par != cpar(bv)) {
501 bv->owner()->setLayout(cpar(bv)->getLayout());
507 string const InsetText::editMessage() const
509 return _("Opened Text Inset");
513 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
515 // par->SetInsetOwner(this);
516 UpdatableInset::edit(bv, x, y, button);
518 if (!bv->lockInset(this)) {
519 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
523 the_locking_inset = 0;
524 inset_pos = inset_x = inset_y = 0;
525 inset_boundary = false;
528 int tmp_y = (y < 0) ? 0 : y;
529 LyXText * t = getLyXText(bv);
531 if (!checkAndActivateInset(bv, x, tmp_y, button)) {
532 t->setCursorFromCoordinates(bv, x - drawTextXOffset,
536 t->selection.cursor = t->cursor;
537 bv->text->finishUndo();
539 updateLocal(bv, CURSOR, false);
541 // If the inset is empty set the language of the current font to the
542 // language to the surronding text (if different).
543 if (par->size() == 0 && !par->next() &&
544 bv->getParentLanguage(this) != t->current_font.language()) {
545 LyXFont font(LyXFont::ALL_IGNORE);
546 font.setLanguage(bv->getParentLanguage(this));
547 setFont(bv, font, false);
552 void InsetText::insetUnlock(BufferView * bv)
554 if (the_locking_inset) {
555 the_locking_inset->insetUnlock(bv);
556 the_locking_inset = 0;
559 no_selection = false;
561 int code = CURSOR|CLEAR_FRAME;
562 LyXText * t = getLyXText(bv);
563 if (t->selection.set()) {
564 t->clearSelection(bv);
566 } else if (owner()) {
567 bv->owner()->setLayout(owner()->getLyXText(bv)
568 ->cursor.par()->getLayout());
570 bv->owner()->setLayout(bv->text->cursor.par()->getLayout());
571 updateLocal(bv, code, false);
575 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
577 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
581 if (inset == cpar(bv)->getInset(cpos(bv))) {
582 lyxerr[Debug::INSETS] << "OK" << endl;
583 the_locking_inset = inset;
584 inset_x = cx(bv) - top_x + drawTextXOffset;
585 inset_y = cy(bv) + drawTextYOffset;
586 inset_pos = cpos(bv);
587 inset_par = cpar(bv);
588 inset_boundary = cboundary(bv);
590 getLyXText(bv)->clearSelection(bv);
591 getLyXText(bv)->sel_cursor = getLyXText(bv)->cursor;
592 getLyXText(bv)->UpdateInset(bv, the_locking_inset);
594 updateLocal(bv, CURSOR, false);
597 } else if (the_locking_inset && (the_locking_inset == inset)) {
598 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
599 lyxerr[Debug::INSETS] << "OK" << endl;
600 inset_x = cx(bv) - top_x + drawTextXOffset;
601 inset_y = cy(bv) + drawTextYOffset;
603 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
605 } else if (the_locking_inset) {
606 lyxerr[Debug::INSETS] << "MAYBE" << endl;
607 return the_locking_inset->lockInsetInInset(bv, inset);
609 lyxerr[Debug::INSETS] << "NOT OK" << endl;
614 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
617 if (!the_locking_inset)
619 if (the_locking_inset == inset) {
620 the_locking_inset->insetUnlock(bv);
621 getLyXText(bv)->updateInset(bv, inset);
622 the_locking_inset = 0;
624 moveRight(bv, false);
625 old_par = 0; // force layout setting
629 updateLocal(bv, CURSOR, false);
632 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
636 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
638 if (!the_locking_inset)
640 if (the_locking_inset != inset) {
641 getLyXText(bv)->updateInset(bv, the_locking_inset);
642 setUpdateStatus(bv, CURSOR_PAR);
643 return the_locking_inset->updateInsetInInset(bv, inset);
645 // updateLocal(bv, FULL, false);
646 if (getLyXText(bv)->updateInset(bv, inset))
647 updateLocal(bv, CURSOR_PAR, false);
648 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
649 inset_x = cx(bv) - top_x + drawTextXOffset;
650 inset_y = cy(bv) + drawTextYOffset;
656 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
660 int tmp_x = x - drawTextXOffset;
661 int tmp_y = y + insetAscent - getLyXText(bv)->first;
662 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y, button);
665 if (the_locking_inset) {
666 if (the_locking_inset == inset) {
667 the_locking_inset->insetButtonPress(bv,x-inset_x,y-inset_y,button);
668 no_selection = false;
671 // otherwise unlock the_locking_inset and lock the new inset
672 the_locking_inset->insetUnlock(bv);
673 inset_x = cx(bv) - top_x + drawTextXOffset;
674 inset_y = cy(bv) + drawTextYOffset;
675 the_locking_inset = static_cast<UpdatableInset*>(inset);
676 inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
677 inset->edit(bv, x - inset_x, y - inset_y, button);
678 if (the_locking_inset)
679 updateLocal(bv, CURSOR, false);
680 no_selection = false;
683 // otherwise only unlock the_locking_inset
684 the_locking_inset->insetUnlock(bv);
685 the_locking_inset = 0;
687 if (bv->theLockingInset()) {
688 if (inset && inset->editable() == Inset::HIGHLY_EDITABLE) {
689 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
690 inset_x = cx(bv) - top_x + drawTextXOffset;
691 inset_y = cy(bv) + drawTextYOffset;
692 inset_pos = cpos(bv);
693 inset_par = cpar(bv);
694 inset_boundary = cboundary(bv);
695 the_locking_inset = uinset;
696 uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
698 uinset->edit(bv, x - inset_x, y - inset_y, 0);
699 if (the_locking_inset)
700 updateLocal(bv, CURSOR, false);
701 no_selection = false;
705 if (!inset) { // && (button == 2)) {
706 bool paste_internally = false;
707 if ((button == 2) && getLyXText(bv)->selection.set()) {
708 localDispatch(bv, LFUN_COPY, "");
709 paste_internally = true;
711 getLyXText(bv)->setCursorFromCoordinates(bv, x-drawTextXOffset,
713 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
714 updateLocal(bv, CURSOR, false);
715 bv->owner()->setLayout(cpar(bv)->getLayout());
717 // Insert primary selection with middle mouse
718 // if there is a local selection in the current buffer,
721 if (paste_internally)
722 localDispatch(bv, LFUN_PASTE, "");
724 localDispatch(bv, LFUN_PASTESELECTION,
729 no_selection = false;
733 void InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
735 UpdatableInset * inset = 0;
737 if (the_locking_inset) {
738 the_locking_inset->insetButtonRelease(bv,
739 x - inset_x, y - inset_y,
742 if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
743 inset = static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
744 if (inset->editable() == Inset::HIGHLY_EDITABLE) {
745 inset->insetButtonRelease(bv,
747 y - inset_y, button);
749 inset_x = cx(bv) - top_x + drawTextXOffset;
750 inset_y = cy(bv) + drawTextYOffset;
751 inset->insetButtonRelease(bv,
753 y - inset_y, button);
755 x - inset_x, y - inset_y, button);
757 updateLocal(bv, CURSOR_PAR, false);
760 no_selection = false;
764 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
768 if (the_locking_inset) {
769 the_locking_inset->insetMotionNotify(bv, x - inset_x,
773 LyXText * t = getLyXText(bv);
775 t->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
777 if (t->toggle_cursor.par() != t->toggle_end_cursor.par() ||
778 t->toggle_cursor.pos() != t->toggle_end_cursor.pos())
779 updateLocal(bv, SELECTION, false);
784 void InsetText::insetKeyPress(XKeyEvent * xke)
786 if (the_locking_inset) {
787 the_locking_inset->insetKeyPress(xke);
793 UpdatableInset::RESULT
794 InsetText::localDispatch(BufferView * bv,
795 kb_action action, string const & arg)
797 no_selection = false;
798 UpdatableInset::RESULT
799 result= UpdatableInset::localDispatch(bv, action, arg);
800 if (result != UNDISPATCHED) {
805 if ((action < 0) && arg.empty())
808 if (the_locking_inset) {
809 result = the_locking_inset->localDispatch(bv, action, arg);
810 if (result == DISPATCHED_NOUPDATE)
812 else if (result == DISPATCHED) {
813 updateLocal(bv, CURSOR_PAR, false);
815 } else if (result == FINISHED) {
816 bool dispatched = false;
818 case LFUN_UNKNOWN_ACTION:
819 case LFUN_BREAKPARAGRAPH:
821 moveRightIntern(bv, false, false);
824 if (!getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
825 moveRightIntern(bv, false, false);
829 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
830 moveRightIntern(bv, false, false);
836 the_locking_inset = 0;
844 case LFUN_SELFINSERT:
845 if (bv->buffer()->isReadonly()) {
846 // setErrorMessage(N_("Document is read only"));
850 /* Automatically delete the currently selected
851 * text and replace it with what is being
852 * typed in now. Depends on lyxrc settings
853 * "auto_region_delete", which defaults to
856 bv->text->setUndo(bv->buffer(), Undo::INSERT,
857 bv->text->cursor.par()->previous(),
858 bv->text->cursor.par()->next());
860 if (lyxrc.auto_region_delete) {
861 if (getLyXText(bv)->selection.set()) {
862 getLyXText(bv)->cutSelection(bv, false);
865 getLyXText(bv)->clearSelection(bv);
866 for (string::size_type i = 0; i < arg.length(); ++i) {
868 if (!math_insert_greek(bv, arg[i])) {
869 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], getLyXText(bv));
870 } else if (!the_locking_inset) {
871 (void)moveRight(bv, false);
874 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], getLyXText(bv));
878 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
879 updateLocal(bv, CURSOR_PAR, true);
880 result = DISPATCHED_NOUPDATE;
882 // --- Cursor Movements -----------------------------------
884 bv->text->finishUndo();
885 moveRight(bv, false, true);
886 getLyXText(bv)->setSelection(bv);
887 updateLocal(bv, SELECTION, false);
890 result = moveRight(bv);
891 bv->text->finishUndo();
892 updateLocal(bv, CURSOR, false);
895 bv->text->finishUndo();
896 moveLeft(bv, false, true);
897 getLyXText(bv)->setSelection(bv);
898 updateLocal(bv, SELECTION, false);
901 bv->text->finishUndo();
902 result = moveLeft(bv);
903 updateLocal(bv, CURSOR, false);
906 bv->text->finishUndo();
908 getLyXText(bv)->setSelection(bv);
909 updateLocal(bv, SELECTION, false);
912 bv->text->finishUndo();
913 result = moveDown(bv);
914 updateLocal(bv, CURSOR, false);
917 bv->text->finishUndo();
919 getLyXText(bv)->setSelection(bv);
920 updateLocal(bv, SELECTION, false);
923 bv->text->finishUndo();
925 updateLocal(bv, CURSOR, false);
928 bv->text->finishUndo();
929 getLyXText(bv)->cursorHome(bv);
930 updateLocal(bv, CURSOR, false);
933 getLyXText(bv)->cursorEnd(bv);
934 updateLocal(bv, CURSOR, false);
936 case LFUN_BACKSPACE: {
937 bv->text->setUndo(bv->buffer(), Undo::DELETE,
938 bv->text->cursor.par()->previous(),
939 bv->text->cursor.par()->next());
940 LyXText * t = getLyXText(bv);
942 if (t->selection.set()) {
948 updateLocal(bv, CURSOR_PAR, true);
954 bv->text->setUndo(bv->buffer(), Undo::DELETE,
955 bv->text->cursor.par()->previous(),
956 bv->text->cursor.par()->next());
957 LyXText * t = getLyXText(bv);
959 if (t->selection.set()) {
964 updateLocal(bv, CURSOR_PAR, true);
969 bv->text->setUndo(bv->buffer(), Undo::DELETE,
970 bv->text->cursor.par()->previous(),
971 bv->text->cursor.par()->next());
972 getLyXText(bv)->cutSelection(bv);
973 updateLocal(bv, CURSOR_PAR, true);
976 bv->text->finishUndo();
977 getLyXText(bv)->copySelection(bv);
978 updateLocal(bv, CURSOR_PAR, false);
980 case LFUN_PASTESELECTION:
982 string const clip(bv->getClipboard());
986 if (arg == "paragraph") {
987 getLyXText(bv)->insertStringAsParagraphs(bv, clip);
989 getLyXText(bv)->insertStringAsLines(bv, clip);
991 updateLocal(bv, CURSOR_PAR, true);
995 if (!autoBreakRows) {
997 if (CutAndPaste::nrOfParagraphs() > 1) {
998 WriteAlert(_("Impossible operation"),
999 _("Cannot include more than one paragraph!"),
1004 bv->text->setUndo(bv->buffer(), Undo::INSERT,
1005 bv->text->cursor.par()->previous(),
1006 bv->text->cursor.par()->next());
1007 getLyXText(bv)->pasteSelection(bv);
1008 updateLocal(bv, CURSOR_PAR, true);
1010 case LFUN_BREAKPARAGRAPH:
1013 getLyXText(bv)->breakParagraph(bv, 0);
1014 updateLocal(bv, FULL, true);
1016 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1019 getLyXText(bv)->breakParagraph(bv, 1);
1020 updateLocal(bv, FULL, true);
1022 case LFUN_BREAKLINE:
1025 bv->text->setUndo(bv->buffer(), Undo::INSERT,
1026 bv->text->cursor.par()->previous(),
1027 bv->text->cursor.par()->next());
1028 getLyXText(bv)->insertChar(bv, Paragraph::META_NEWLINE);
1029 updateLocal(bv, CURSOR_PAR, true);
1032 // do not set layouts on non breakable textinsets
1033 if (autoBreakRows) {
1034 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1036 // Derive layout number from given argument (string)
1037 // and current buffer's textclass (number). */
1038 LyXTextClassList::ClassList::size_type tclass =
1039 bv->buffer()->params.textclass;
1040 std::pair <bool, LyXTextClass::size_type> layout =
1041 textclasslist.NumberOfLayout(tclass, arg);
1043 // If the entry is obsolete, use the new one instead.
1045 string obs = textclasslist.Style(tclass,layout.second).
1048 layout = textclasslist.NumberOfLayout(tclass, obs);
1051 // see if we found the layout number:
1052 if (!layout.first) {
1053 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1054 bv->owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, msg);
1058 if (cur_layout != layout.second) {
1059 cur_layout = layout.second;
1060 getLyXText(bv)->setLayout(bv, layout.second);
1061 bv->owner()->setLayout(cpar(bv)->getLayout());
1062 updateLocal(bv, CURSOR_PAR, true);
1065 // reset the layout box
1066 bv->owner()->setLayout(cpar(bv)->getLayout());
1069 case LFUN_PARAGRAPH_SPACING:
1070 // This one is absolutely not working. When fiddling with this
1071 // it also seems to me that the paragraphs inside the insettext
1072 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1074 Paragraph * par = getLyXText(bv)->cursor.par();
1075 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1076 float cur_value = 1.0;
1077 if (cur_spacing == Spacing::Other) {
1078 cur_value = par->params().spacing().getValue();
1081 std::istringstream istr(arg.c_str());
1084 Spacing::Space new_spacing = cur_spacing;
1085 float new_value = cur_value;
1087 lyxerr << "Missing argument to `paragraph-spacing'"
1089 } else if (tmp == "single") {
1090 new_spacing = Spacing::Single;
1091 } else if (tmp == "onehalf") {
1092 new_spacing = Spacing::Onehalf;
1093 } else if (tmp == "double") {
1094 new_spacing = Spacing::Double;
1095 } else if (tmp == "other") {
1096 new_spacing = Spacing::Other;
1099 lyxerr << "new_value = " << tmpval << endl;
1102 } else if (tmp == "default") {
1103 new_spacing = Spacing::Default;
1105 lyxerr << _("Unknown spacing argument: ")
1108 if (cur_spacing != new_spacing || cur_value != new_value) {
1109 par->params().spacing(Spacing(new_spacing, new_value));
1110 //getLyXText(bv)->RedoParagraph(owner->view());
1111 updateLocal(bv, CURSOR_PAR, true);
1112 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1118 if (!bv->Dispatch(action, arg))
1119 result = UNDISPATCHED;
1123 /// If the action has deleted all text in the inset, we need to change the
1124 // language to the language to the surronding text.
1125 if (par->size() == 0 && !par->next()) {
1126 LyXFont font(LyXFont::ALL_IGNORE);
1127 font.setLanguage(bv->getParentLanguage(this));
1128 setFont(bv, font, false);
1131 if (result != FINISHED) {
1132 showInsetCursor(bv);
1134 bv->unlockInset(this);
1139 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1142 buf->latexParagraphs(os, par, 0, texrow);
1143 return texrow.rows();
1147 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1149 Paragraph * p = par;
1150 unsigned int lines = 0;
1153 string const tmp = buf->asciiParagraph(p, linelen);
1154 lines += countChar(tmp, '\n');
1162 int InsetText::docBook(Buffer const * buf, ostream & os) const
1164 Paragraph * p = par;
1165 unsigned int lines = 0;
1170 buf->simpleDocBookOnePar(os, tmp, p, desc, 0);
1178 void InsetText::validate(LaTeXFeatures & features) const
1180 Paragraph * p = par;
1182 p->validate(features);
1188 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1190 if (textclasslist.Style(buf->params.textclass,
1191 p->getLayout()).labeltype != LABEL_MANUAL)
1194 return p->beginningOfMainBody();
1198 void InsetText::getCursorPos(BufferView * bv,
1199 int & x, int & y) const
1206 unsigned int InsetText::insetInInsetY()
1208 if (!the_locking_inset)
1211 return (inset_y + the_locking_inset->insetInInsetY());
1215 void InsetText::toggleInsetCursor(BufferView * bv)
1217 if (the_locking_inset) {
1218 the_locking_inset->toggleInsetCursor(bv);
1222 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1224 int const asc = lyxfont::maxAscent(font);
1225 int const desc = lyxfont::maxDescent(font);
1227 if (isCursorVisible())
1228 bv->hideLockedInsetCursor();
1230 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1231 toggleCursorVisible();
1235 void InsetText::showInsetCursor(BufferView * bv, bool show)
1237 if (the_locking_inset) {
1238 the_locking_inset->showInsetCursor(bv);
1241 if (!isCursorVisible()) {
1242 LyXFont const font =
1243 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1245 int const asc = lyxfont::maxAscent(font);
1246 int const desc = lyxfont::maxDescent(font);
1248 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1250 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1251 setCursorVisible(true);
1256 void InsetText::hideInsetCursor(BufferView * bv)
1258 if (isCursorVisible()) {
1259 bv->hideLockedInsetCursor();
1260 setCursorVisible(false);
1262 if (the_locking_inset)
1263 the_locking_inset->hideInsetCursor(bv);
1267 UpdatableInset::RESULT
1268 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1270 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1271 return moveLeftIntern(bv, false, activate_inset, selecting);
1273 return moveRightIntern(bv, false, activate_inset, selecting);
1277 UpdatableInset::RESULT
1278 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1280 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1281 return moveRightIntern(bv, true, activate_inset, selecting);
1283 return moveLeftIntern(bv, true, activate_inset, selecting);
1287 UpdatableInset::RESULT
1288 InsetText::moveRightIntern(BufferView * bv, bool behind,
1289 bool activate_inset, bool selecting)
1291 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1293 if (activate_inset && checkAndActivateInset(bv, behind))
1295 getLyXText(bv)->cursorRight(bv);
1297 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1298 return DISPATCHED_NOUPDATE;
1302 UpdatableInset::RESULT
1303 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1304 bool activate_inset, bool selecting)
1306 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1308 getLyXText(bv)->cursorLeft(bv);
1310 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1311 if (activate_inset && checkAndActivateInset(bv, behind))
1313 return DISPATCHED_NOUPDATE;
1317 UpdatableInset::RESULT
1318 InsetText::moveUp(BufferView * bv)
1320 if (!crow(bv)->previous())
1322 getLyXText(bv)->cursorUp(bv);
1323 return DISPATCHED_NOUPDATE;
1327 UpdatableInset::RESULT
1328 InsetText::moveDown(BufferView * bv)
1330 if (!crow(bv)->next())
1332 getLyXText(bv)->cursorDown(bv);
1333 return DISPATCHED_NOUPDATE;
1337 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1339 if (the_locking_inset) {
1340 if (the_locking_inset->insertInsetAllowed(inset))
1341 return the_locking_inset->insertInset(bv, inset);
1344 bv->text->setUndo(bv->buffer(), Undo::INSERT,
1345 bv->text->cursor.par()->previous(),
1346 bv->text->cursor.par()->next());
1347 inset->setOwner(this);
1348 hideInsetCursor(bv);
1349 getLyXText(bv)->insertInset(bv, inset);
1351 if ((cpar(bv)->GetChar(cpos(bv)) != Paragraph::META_INSET) ||
1352 (cpar(bv)->GetInset(cpos(bv)) != inset))
1353 getLyXText(bv)->CursorLeft(bv);
1355 bv->fitCursor(getLyXText(bv));
1356 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1357 showInsetCursor(bv);
1362 UpdatableInset * InsetText::getLockingInset()
1364 return the_locking_inset ? the_locking_inset->getLockingInset() : this;
1368 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1372 if (the_locking_inset)
1373 return the_locking_inset->getFirstLockingInsetOfType(c);
1378 bool InsetText::showInsetDialog(BufferView * bv) const
1380 if (the_locking_inset)
1381 return the_locking_inset->showInsetDialog(bv);
1386 std::vector<string> const InsetText::getLabelList() const
1388 std::vector<string> label_list;
1390 Paragraph * tpar = par;
1392 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1393 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1394 for (; beg != end; ++beg) {
1395 std::vector<string> const l = (*beg)->getLabelList();
1396 label_list.insert(label_list.end(), l.begin(), l.end());
1398 tpar = tpar->next();
1404 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1407 if (the_locking_inset) {
1408 the_locking_inset->setFont(bv, font, toggleall, selectall);
1411 if (getLyXText(bv)->selection.set()) {
1412 bv->text->setUndo(bv->buffer(), Undo::EDIT,
1413 bv->text->cursor.par()->previous(),
1414 bv->text->cursor.par()->next());
1418 getLyXText(bv)->setFont(bv, font, toggleall);
1420 getLyXText(bv)->clearSelection(bv);
1421 bv->fitCursor(getLyXText(bv));
1422 if (selectall || getLyXText(bv)->selection.set())
1423 updateLocal(bv, FULL, true);
1425 updateLocal(bv, CURSOR_PAR, true);
1429 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1431 if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
1435 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1436 if (!inset || inset->editable() != Inset::HIGHLY_EDITABLE)
1438 LyXFont const font =
1439 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1441 x = inset->width(bv, font);
1442 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1445 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1447 //inset_x = cx(bv) - top_x + drawTextXOffset;
1448 //inset_y = cy(bv) + drawTextYOffset;
1449 inset->edit(bv, x, y, 0);
1450 if (!the_locking_inset)
1452 updateLocal(bv, CURSOR, false);
1459 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1462 x -= drawTextXOffset;
1464 int dummyy = y + insetAscent;
1465 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy, button);
1472 inset_x = cx(bv) - top_x + drawTextXOffset;
1473 inset_y = cy(bv) + drawTextYOffset;
1474 inset->edit(bv, x - inset_x, y - inset_y, button);
1475 if (!the_locking_inset)
1477 updateLocal(bv, CURSOR, false);
1484 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1486 int w = UpdatableInset::getMaxWidth(bv, inset);
1491 w = w - top_x + owner()->x();
1494 w -= (2 * TEXT_TO_INSET_OFFSET);
1496 // return w - (2*TEXT_TO_INSET_OFFSET);
1500 void InsetText::setParagraphData(Paragraph * p)
1502 // delete all instances of LyXText before deleting the paragraps used
1505 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
1506 delete (*cit).second;
1511 Paragraph * tmp = par->next();
1516 par = new Paragraph(*p);
1517 par->setInsetOwner(this);
1518 Paragraph * np = par;
1521 np->next(new Paragraph(*p));
1522 np->next()->previous(np);
1524 np->setInsetOwner(this);
1531 void InsetText::setText(string const & data)
1534 LyXFont font(LyXFont::ALL_SANE);
1535 for (unsigned int i=0; i < data.length(); ++i)
1536 par->insertChar(i, data[i], font);
1540 void InsetText::setAutoBreakRows(bool flag)
1542 if (flag != autoBreakRows) {
1543 autoBreakRows = flag;
1551 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1553 if (how != drawFrame_) {
1556 updateLocal(bv, DRAW_FRAME, false);
1561 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1563 if (frame_color != col) {
1566 updateLocal(bv, DRAW_FRAME, false);
1571 int InsetText::cx(BufferView * bv) const
1573 LyXText * text = getLyXText(bv);
1574 int x = text->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1575 if (the_locking_inset) {
1576 LyXFont font = text->getFont(bv->buffer(),
1578 text->cursor.pos());
1579 if (font.isVisibleRightToLeft())
1580 x -= the_locking_inset->width(bv, font);
1586 int InsetText::cy(BufferView * bv) const
1589 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1593 Paragraph::size_type InsetText::cpos(BufferView * bv) const
1595 return getLyXText(bv)->cursor.pos();
1599 Paragraph * InsetText::cpar(BufferView * bv) const
1601 return getLyXText(bv)->cursor.par();
1605 bool InsetText::cboundary(BufferView * bv) const
1607 return getLyXText(bv)->cursor.boundary();
1611 Row * InsetText::crow(BufferView * bv) const
1613 return getLyXText(bv)->cursor.row();
1617 LyXText * InsetText::getLyXText(BufferView const * lbv,
1618 bool const recursive) const
1620 if (!recursive && (cached_bview == lbv))
1623 // Super UGLY! (Lgb)
1624 BufferView * bv = const_cast<BufferView *>(lbv);
1627 Cache::iterator it = cache.find(bv);
1629 if (it != cache.end()) {
1630 lyx::Assert(it->second);
1632 cached_text = it->second;
1633 if (recursive && the_locking_inset) {
1634 return the_locking_inset->getLyXText(bv);
1638 cached_text = new LyXText(const_cast<InsetText *>(this));
1639 cached_text->init(bv);
1641 cache.insert(make_pair(bv, cached_text));
1643 if (the_locking_inset) {
1644 cached_text->setCursor(bv, inset_par, inset_pos,
1645 true, inset_boundary);
1647 return the_locking_inset->getLyXText(bv);
1654 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1658 Cache::iterator it = cache.find(bv);
1660 if (it == cache.end()) {
1664 lyx::Assert(it->second);
1669 /// then remove all LyXText in text-insets
1670 Paragraph * p = par;
1671 for (; p; p = p->next()) {
1672 p->deleteInsetsLyXText(bv);
1678 void InsetText::resizeLyXText(BufferView * bv, bool force) const
1680 if (!par->next() && !par->size()) // no data, resize not neccessary!
1682 // one endless line, resize normally not necessary
1683 if (!force && getMaxWidth(bv, this) < 0)
1686 Cache::iterator it = cache.find(bv);
1687 if (it == cache.end()) {
1690 lyx::Assert(it->second);
1692 Paragraph * lpar = 0;
1693 Paragraph * selstartpar = 0;
1694 Paragraph * selendpar = 0;
1695 Paragraph::size_type pos = 0;
1696 Paragraph::size_type selstartpos = 0;
1697 Paragraph::size_type selendpos = 0;
1698 bool boundary = false;
1699 bool selstartboundary = false;
1700 bool selendboundary = false;
1701 bool selection = false;
1702 bool mark_set = false;
1704 // bv->owner()->prohibitInput();
1707 LyXText * t = getLyXText(bv);
1708 lpar = t->cursor.par();
1709 pos = t->cursor.pos();
1710 boundary = t->cursor.boundary();
1711 selstartpar = t->selection.start.par();
1712 selstartpos = t->selection.start.pos();
1713 selstartboundary = t->selection.start.boundary();
1714 selendpar = t->selection.end.par();
1715 selendpos = t->selection.end.pos();
1716 selendboundary = t->selection.end.boundary();
1717 selection = t->selection.set();
1718 mark_set = t->selection.mark();
1720 deleteLyXText(bv, (the_locking_inset == 0) || force);
1723 LyXText * t = getLyXText(bv);
1725 t->selection.set(true);
1726 /* at this point just to avoid the Delete-Empty-Paragraph
1727 * Mechanism when setting the cursor */
1728 t->selection.mark(mark_set);
1730 t->setCursor(bv, selstartpar, selstartpos, true, selstartboundary);
1731 t->selection.cursor = t->cursor;
1732 t->setCursor(bv, selendpar, selendpos, true, selendboundary);
1733 t->setSelection(bv);
1734 t->setCursor(bv, lpar, pos);
1736 t->setCursor(bv, lpar, pos, true, boundary);
1737 t->selection.cursor = t->cursor;
1738 t->selection.set(false);
1742 LyXText * t = getLyXText(bv);
1743 t->first = bv->screen()->topCursorVisible(t);
1746 // this will scroll the screen such that the cursor becomes visible
1747 bv->updateScrollbar();
1748 // bv->owner()->allowInput();
1749 if (the_locking_inset) {
1750 /// then resize all LyXText in text-insets
1751 inset_x = cx(bv) - top_x + drawTextXOffset;
1752 inset_y = cy(bv) + drawTextYOffset;
1753 for (Paragraph * p = par; p; p = p->next()) {
1754 p->resizeInsetsLyXText(bv);
1761 void InsetText::removeNewlines()
1763 for (Paragraph * p = par; p; p = p->next()) {
1764 for (int i = 0; i < p->size(); ++i) {
1765 if (p->getChar(i) == Paragraph::META_NEWLINE)
1772 bool InsetText::nodraw() const
1774 if (the_locking_inset)
1775 return the_locking_inset->nodraw();
1776 return UpdatableInset::nodraw();
1780 int InsetText::scroll(bool recursive) const
1782 int sx = UpdatableInset::scroll(false);
1784 if (recursive && the_locking_inset)
1785 sx += the_locking_inset->scroll(recursive);
1791 bool InsetText::doClearArea() const
1793 return !locked || (need_update & (FULL|INIT));
1797 void InsetText::selectAll(BufferView * bv)
1799 getLyXText(bv)->cursorTop(bv);
1800 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1801 getLyXText(bv)->cursorBottom(bv);
1802 getLyXText(bv)->setSelection(bv);
1806 void InsetText::clearSelection(BufferView * bv)
1808 getLyXText(bv)->clearSelection(bv);
1812 void InsetText::clearInset(Painter & pain, int baseline, bool & cleared) const
1815 int h = insetAscent + insetDescent;
1816 int ty = baseline - insetAscent;
1822 if ((ty + h) > pain.paperHeight())
1823 h = pain.paperHeight();
1824 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
1825 w = pain.paperWidth();
1826 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);