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"
58 extern unsigned char getCurrentTextClass(Buffer *);
59 extern bool math_insert_greek(BufferView *, char);
60 extern int greek_kb_flag;
62 InsetText::InsetText()
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;
118 InsetText::~InsetText()
120 // delete all instances of LyXText before deleting the paragraps used
123 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
124 delete (*cit).second;
129 Paragraph * tmp = par->next();
136 void InsetText::clear()
138 // delete all instances of LyXText before deleting the paragraps used
141 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
142 delete (*cit).second;
147 Paragraph * tmp = par->next();
155 Inset * InsetText::clone(Buffer const &) const
157 InsetText * t = new InsetText(*this);
162 void InsetText::write(Buffer const * buf, ostream & os) const
165 writeParagraphData(buf, os);
169 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
171 par->writeFile(buf, os, buf->params, 0);
175 void InsetText::read(Buffer const * buf, LyXLex & lex)
179 Paragraph * return_par = 0;
180 Paragraph::depth_type depth = 0;
181 LyXFont font(LyXFont::ALL_INHERIT);
187 token = lex.GetString();
190 if (token == "\\end_inset")
192 if (const_cast<Buffer*>(buf)->
193 parseSingleLyXformat2Token(lex, par, return_par,
194 token, pos, depth, font)) {
195 // the_end read this should NEVER happen
196 lex.printError("\\the_end read in inset! Error in document!");
204 return_par->setInsetOwner(this);
205 return_par = return_par->next();
208 if (token != "\\end_inset") {
209 lex.printError("Missing \\end_inset at this point. "
216 int InsetText::ascent(BufferView * bv, LyXFont const &) const
219 Row * row = getLyXText(bv)->getRowNearY(y_temp);
220 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
225 int InsetText::descent(BufferView * bv, LyXFont const &) const
227 LyXText * t = getLyXText(bv);
229 Row * row = t->getRowNearY(y_temp);
230 insetDescent = t->height - row->ascent_of_text() +
231 TEXT_TO_INSET_OFFSET;
236 int InsetText::width(BufferView * bv, LyXFont const &) const
238 insetWidth = max(textWidth(bv),
239 (int)getLyXText(bv)->width + (2 * TEXT_TO_INSET_OFFSET));
244 int InsetText::textWidth(BufferView * bv) const
246 int const w = getMaxWidth(bv, this);
251 void InsetText::draw(BufferView * bv, LyXFont const & f,
252 int baseline, float & x, bool cleared) const
257 Painter & pain = bv->painter();
259 // no draw is necessary !!!
260 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
262 top_baseline = baseline;
264 if (!cleared && (need_update & CLEAR_FRAME))
265 clearFrame(pain, cleared);
267 frame_is_visible = false;
274 UpdatableInset::draw(bv, f, baseline, x, cleared);
277 x += static_cast<float>(scroll());
280 // update insetWidth and insetHeight with dummy calls
282 (void)descent(bv, f);
286 // if top_x differs we have a rule down and we don't have to clear anything
287 if (!cleared && (top_x == int(x)) &&
288 ((need_update&(INIT|FULL)) || (top_baseline!=baseline) ||
289 (last_drawn_width!=insetWidth))) {
290 clearInset(pain, baseline, cleared);
293 frame_is_visible = false;
295 if (!cleared && (need_update == NONE))
298 if (top_x != int(x)) {
299 if ((getMaxWidth(bv, this) > 0) &&
300 (getLyXText(bv)->width != old_max_width)) {
303 old_max_width = getLyXText(bv)->width;
304 bv->text->status = LyXText::CHANGED_IN_DRAW;
308 clearInset(pain, baseline, cleared);
314 // lyxerr << "InsetText::draw[" << this << "](" << need_update << ":" << int(x) << ":" << top_x << ")\n";
316 if (cleared || (last_drawn_width != insetWidth)) {
318 last_drawn_width = insetWidth;
321 top_baseline = baseline;
322 top_y = baseline - ascent(bv, f);
323 last_width = width(bv, f);
324 last_height = ascent(bv, f) + descent(bv, f);
326 if (the_locking_inset && (cpar(bv) == inset_par)
327 && (cpos(bv) == inset_pos)) {
328 inset_x = cx(bv) - top_x + drawTextXOffset;
329 inset_y = cy(bv) + drawTextYOffset;
331 if (!cleared && (need_update == CURSOR)
332 && !getLyXText(bv)->selection.set()) {
333 drawFrame(pain, cleared);
334 x += last_width; // was width(bv, f);
338 x += TEXT_TO_INSET_OFFSET;
341 Row * row = getLyXText(bv)->getRowNearY(y);
342 int y_offset = baseline - row->ascent_of_text();
343 int ph = pain.paperHeight();
346 while ((row != 0) && ((y+row->height()) <= 0)) {
348 first += row->height();
353 getLyXText(bv)->first = first;
357 while ((row != 0) && (yf < ph)) {
358 getLyXText(bv)->getVisibleRow(bv, y+y_offset, int(x), row,
364 } else if (!locked) {
365 if (need_update & CURSOR) {
366 bv->screen()->ToggleSelection(getLyXText(bv), bv, true, y_offset,int(x));
367 getLyXText(bv)->clearSelection(bv);
368 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
370 bv->screen()->Update(getLyXText(bv), bv, y_offset, int(x));
373 if (need_update & SELECTION)
374 bv->screen()->ToggleToggle(getLyXText(bv), bv, y_offset, int(x));
375 else if (need_update & CURSOR) {
376 bv->screen()->ToggleSelection(getLyXText(bv), bv, true, y_offset,int(x));
377 getLyXText(bv)->clearSelection(bv);
378 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
380 bv->screen()->Update(getLyXText(bv), bv, y_offset, int(x));
384 getLyXText(bv)->refresh_y = 0;
385 getLyXText(bv)->status = LyXText::UNCHANGED;
386 if ((need_update != CURSOR_PAR) &&
387 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked)))
388 drawFrame(pain, cleared);
389 else if (need_update & CLEAR_FRAME)
390 clearFrame(pain, cleared);
391 x += last_width /* was width(bv, f) */ - TEXT_TO_INSET_OFFSET;
392 if (bv->text->status==LyXText::CHANGED_IN_DRAW) {
394 } else if (need_update != INIT)
399 void InsetText::drawFrame(Painter & pain, bool cleared) const
401 if (!frame_is_visible || cleared) {
402 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
403 insetWidth - 1, insetAscent + insetDescent - 1,
405 frame_is_visible = true;
410 void InsetText::clearFrame(Painter & pain, bool cleared) const
412 if (frame_is_visible) {
414 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
415 insetWidth - 1, insetAscent + insetDescent - 1,
418 frame_is_visible = false;
423 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
425 LyXText * t = getLyXText(bv);
429 t->BreakParagraph(bv);
437 owner()->update(bv, font, true);
440 if (the_locking_inset) {
441 inset_x = cx(bv) - top_x + drawTextXOffset;
442 inset_y = cy(bv) + drawTextYOffset;
443 the_locking_inset->update(bv, font, reinit);
446 if (need_update == INIT) {
451 int oldw = insetWidth;
452 insetWidth = t->width + (2 * TEXT_TO_INSET_OFFSET);
453 if (oldw != insetWidth) {
456 // update(bv, font, reinit);
459 if ((need_update & CURSOR_PAR) && t->status == LyXText::UNCHANGED &&
461 t->updateInset(bv, the_locking_inset);
464 if (t->status == LyXText::NEED_MORE_REFRESH)
468 Row * row = t->GetRowNearY(y_temp);
469 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
470 insetDescent = t->height - row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
475 void InsetText::setUpdateStatus(BufferView * bv, int what) const
477 LyXText * t = getLyXText(bv);
479 if (t->status == LyXText::NEED_MORE_REFRESH)
481 else if (t->status == LyXText::NEED_VERY_LITTLE_REFRESH)
482 need_update |= CURSOR_PAR;
484 // this to not draw a selection when we redraw all of it!
485 if ((need_update & (INIT|FULL)) && (need_update & CURSOR))
486 t->clearSelection(bv);
490 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
492 LyXText * t = getLyXText(bv);
494 setUpdateStatus(bv, what);
495 if (need_update != CURSOR || t->status != LyXText::UNCHANGED ||
497 bv->updateInset(this, mark_dirty);
498 bv->owner()->showState();
499 if (old_par != cpar(bv)) {
500 bv->owner()->setLayout(cpar(bv)->getLayout());
506 string const InsetText::editMessage() const
508 return _("Opened Text Inset");
512 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
514 // par->SetInsetOwner(this);
515 UpdatableInset::edit(bv, x, y, button);
517 if (!bv->lockInset(this)) {
518 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
522 the_locking_inset = 0;
523 inset_pos = inset_x = inset_y = 0;
524 inset_boundary = false;
527 int tmp_y = (y < 0) ? 0 : y;
528 LyXText * t = getLyXText(bv);
530 if (!checkAndActivateInset(bv, x, tmp_y, button)) {
531 t->setCursorFromCoordinates(bv, x - drawTextXOffset,
535 t->selection.cursor = t->cursor;
536 bv->text->finishUndo();
538 updateLocal(bv, CURSOR, false);
540 // If the inset is empty set the language of the current font to the
541 // language to the surronding text (if different).
542 if (par->size() == 0 && !par->next() &&
543 bv->getParentLanguage(this) != t->current_font.language()) {
544 LyXFont font(LyXFont::ALL_IGNORE);
545 font.setLanguage(bv->getParentLanguage(this));
546 setFont(bv, font, false);
551 void InsetText::insetUnlock(BufferView * bv)
553 if (the_locking_inset) {
554 the_locking_inset->insetUnlock(bv);
555 the_locking_inset = 0;
558 no_selection = false;
560 int code = CURSOR|CLEAR_FRAME;
561 LyXText * t = getLyXText(bv);
562 if (t->selection.set()) {
563 t->clearSelection(bv);
565 } else if (owner()) {
566 bv->owner()->setLayout(owner()->getLyXText(bv)
567 ->cursor.par()->getLayout());
569 bv->owner()->setLayout(bv->text->cursor.par()->getLayout());
570 updateLocal(bv, code, false);
574 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
576 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
580 if (inset == cpar(bv)->getInset(cpos(bv))) {
581 lyxerr[Debug::INSETS] << "OK" << endl;
582 the_locking_inset = inset;
583 inset_x = cx(bv) - top_x + drawTextXOffset;
584 inset_y = cy(bv) + drawTextYOffset;
585 inset_pos = cpos(bv);
586 inset_par = cpar(bv);
587 inset_boundary = cboundary(bv);
589 getLyXText(bv)->clearSelection(bv);
590 getLyXText(bv)->sel_cursor = getLyXText(bv)->cursor;
591 getLyXText(bv)->UpdateInset(bv, the_locking_inset);
593 updateLocal(bv, CURSOR, false);
596 } else if (the_locking_inset && (the_locking_inset == inset)) {
597 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
598 lyxerr[Debug::INSETS] << "OK" << endl;
599 inset_x = cx(bv) - top_x + drawTextXOffset;
600 inset_y = cy(bv) + drawTextYOffset;
602 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
604 } else if (the_locking_inset) {
605 lyxerr[Debug::INSETS] << "MAYBE" << endl;
606 return the_locking_inset->lockInsetInInset(bv, inset);
608 lyxerr[Debug::INSETS] << "NOT OK" << endl;
613 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
616 if (!the_locking_inset)
618 if (the_locking_inset == inset) {
619 the_locking_inset->insetUnlock(bv);
620 getLyXText(bv)->updateInset(bv, inset);
621 the_locking_inset = 0;
623 moveRight(bv, false);
624 old_par = 0; // force layout setting
628 updateLocal(bv, CURSOR, false);
631 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
635 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
637 if (!the_locking_inset)
639 if (the_locking_inset != inset) {
640 getLyXText(bv)->updateInset(bv, the_locking_inset);
641 setUpdateStatus(bv, CURSOR_PAR);
642 return the_locking_inset->updateInsetInInset(bv, inset);
644 // updateLocal(bv, FULL, false);
645 if (getLyXText(bv)->updateInset(bv, inset))
646 updateLocal(bv, CURSOR_PAR, false);
647 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
648 inset_x = cx(bv) - top_x + drawTextXOffset;
649 inset_y = cy(bv) + drawTextYOffset;
655 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
659 int tmp_x = x - drawTextXOffset;
660 int tmp_y = y + insetAscent - getLyXText(bv)->first;
661 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y, button);
664 if (the_locking_inset) {
665 if (the_locking_inset == inset) {
666 the_locking_inset->insetButtonPress(bv,x-inset_x,y-inset_y,button);
667 no_selection = false;
670 // otherwise unlock the_locking_inset and lock the new inset
671 the_locking_inset->insetUnlock(bv);
672 inset_x = cx(bv) - top_x + drawTextXOffset;
673 inset_y = cy(bv) + drawTextYOffset;
674 the_locking_inset = static_cast<UpdatableInset*>(inset);
675 inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
676 inset->edit(bv, x - inset_x, y - inset_y, button);
677 if (the_locking_inset)
678 updateLocal(bv, CURSOR, false);
679 no_selection = false;
682 // otherwise only unlock the_locking_inset
683 the_locking_inset->insetUnlock(bv);
684 the_locking_inset = 0;
686 if (bv->theLockingInset()) {
687 if (inset && inset->editable() == Inset::HIGHLY_EDITABLE) {
688 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
689 inset_x = cx(bv) - top_x + drawTextXOffset;
690 inset_y = cy(bv) + drawTextYOffset;
691 inset_pos = cpos(bv);
692 inset_par = cpar(bv);
693 inset_boundary = cboundary(bv);
694 the_locking_inset = uinset;
695 uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
697 uinset->edit(bv, x - inset_x, y - inset_y, 0);
698 if (the_locking_inset)
699 updateLocal(bv, CURSOR, false);
700 no_selection = false;
704 if (!inset) { // && (button == 2)) {
705 bool paste_internally = false;
706 if ((button == 2) && getLyXText(bv)->selection.set()) {
707 localDispatch(bv, LFUN_COPY, "");
708 paste_internally = true;
710 getLyXText(bv)->setCursorFromCoordinates(bv, x-drawTextXOffset,
712 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
713 updateLocal(bv, CURSOR, false);
714 bv->owner()->setLayout(cpar(bv)->getLayout());
716 // Insert primary selection with middle mouse
717 // if there is a local selection in the current buffer,
720 if (paste_internally)
721 localDispatch(bv, LFUN_PASTE, "");
723 localDispatch(bv, LFUN_PASTESELECTION,
728 no_selection = false;
732 void InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
734 UpdatableInset * inset = 0;
736 if (the_locking_inset) {
737 the_locking_inset->insetButtonRelease(bv,
738 x - inset_x, y - inset_y,
741 if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
742 inset = static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
743 if (inset->editable() == Inset::HIGHLY_EDITABLE) {
744 inset->insetButtonRelease(bv,
746 y - inset_y, button);
748 inset_x = cx(bv) - top_x + drawTextXOffset;
749 inset_y = cy(bv) + drawTextYOffset;
750 inset->insetButtonRelease(bv,
752 y - inset_y, button);
754 x - inset_x, y - inset_y, button);
756 updateLocal(bv, CURSOR_PAR, false);
759 no_selection = false;
763 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
767 if (the_locking_inset) {
768 the_locking_inset->insetMotionNotify(bv, x - inset_x,
772 LyXText * t = getLyXText(bv);
774 t->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
776 if (t->toggle_cursor.par() != t->toggle_end_cursor.par() ||
777 t->toggle_cursor.pos() != t->toggle_end_cursor.pos())
778 updateLocal(bv, SELECTION, false);
783 void InsetText::insetKeyPress(XKeyEvent * xke)
785 if (the_locking_inset) {
786 the_locking_inset->insetKeyPress(xke);
792 UpdatableInset::RESULT
793 InsetText::localDispatch(BufferView * bv,
794 kb_action action, string const & arg)
796 no_selection = false;
797 UpdatableInset::RESULT
798 result= UpdatableInset::localDispatch(bv, action, arg);
799 if (result != UNDISPATCHED) {
804 if ((action < 0) && arg.empty())
807 if (the_locking_inset) {
808 result = the_locking_inset->localDispatch(bv, action, arg);
809 if (result == DISPATCHED_NOUPDATE)
811 else if (result == DISPATCHED) {
812 updateLocal(bv, CURSOR_PAR, false);
814 } else if (result == FINISHED) {
815 bool dispatched = false;
817 case LFUN_UNKNOWN_ACTION:
818 case LFUN_BREAKPARAGRAPH:
820 moveRightIntern(bv, false, false);
823 if (!getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
824 moveRightIntern(bv, false, false);
828 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
829 moveRightIntern(bv, false, false);
835 the_locking_inset = 0;
843 case LFUN_SELFINSERT:
844 if (bv->buffer()->isReadonly()) {
845 // setErrorMessage(N_("Document is read only"));
849 /* Automatically delete the currently selected
850 * text and replace it with what is being
851 * typed in now. Depends on lyxrc settings
852 * "auto_region_delete", which defaults to
855 bv->text->setUndo(bv->buffer(), Undo::INSERT,
856 bv->text->cursor.par()->previous(),
857 bv->text->cursor.par()->next());
859 if (lyxrc.auto_region_delete) {
860 if (getLyXText(bv)->selection.set()) {
861 getLyXText(bv)->cutSelection(bv, false);
864 getLyXText(bv)->clearSelection(bv);
865 for (string::size_type i = 0; i < arg.length(); ++i) {
867 if (!math_insert_greek(bv, arg[i])) {
868 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], getLyXText(bv));
869 } else if (!the_locking_inset) {
870 (void)moveRight(bv, false);
873 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], getLyXText(bv));
877 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
878 updateLocal(bv, CURSOR_PAR, true);
879 result = DISPATCHED_NOUPDATE;
881 // --- Cursor Movements -----------------------------------
883 bv->text->finishUndo();
884 moveRight(bv, false, true);
885 getLyXText(bv)->setSelection(bv);
886 updateLocal(bv, SELECTION, false);
889 result = moveRight(bv);
890 bv->text->finishUndo();
891 updateLocal(bv, CURSOR, false);
894 bv->text->finishUndo();
895 moveLeft(bv, false, true);
896 getLyXText(bv)->setSelection(bv);
897 updateLocal(bv, SELECTION, false);
900 bv->text->finishUndo();
901 result = moveLeft(bv);
902 updateLocal(bv, CURSOR, false);
905 bv->text->finishUndo();
907 getLyXText(bv)->setSelection(bv);
908 updateLocal(bv, SELECTION, false);
911 bv->text->finishUndo();
912 result = moveDown(bv);
913 updateLocal(bv, CURSOR, false);
916 bv->text->finishUndo();
918 getLyXText(bv)->setSelection(bv);
919 updateLocal(bv, SELECTION, false);
922 bv->text->finishUndo();
924 updateLocal(bv, CURSOR, false);
927 bv->text->finishUndo();
928 getLyXText(bv)->cursorHome(bv);
929 updateLocal(bv, CURSOR, false);
932 getLyXText(bv)->cursorEnd(bv);
933 updateLocal(bv, CURSOR, false);
935 case LFUN_BACKSPACE: {
936 bv->text->setUndo(bv->buffer(), Undo::DELETE,
937 bv->text->cursor.par()->previous(),
938 bv->text->cursor.par()->next());
939 LyXText * t = getLyXText(bv);
941 if (t->selection.set()) {
947 updateLocal(bv, CURSOR_PAR, true);
953 bv->text->setUndo(bv->buffer(), Undo::DELETE,
954 bv->text->cursor.par()->previous(),
955 bv->text->cursor.par()->next());
956 LyXText * t = getLyXText(bv);
958 if (t->selection.set()) {
963 updateLocal(bv, CURSOR_PAR, true);
968 bv->text->setUndo(bv->buffer(), Undo::DELETE,
969 bv->text->cursor.par()->previous(),
970 bv->text->cursor.par()->next());
971 getLyXText(bv)->cutSelection(bv);
972 updateLocal(bv, CURSOR_PAR, true);
975 bv->text->finishUndo();
976 getLyXText(bv)->copySelection(bv);
977 updateLocal(bv, CURSOR_PAR, false);
979 case LFUN_PASTESELECTION:
981 string const clip(bv->getClipboard());
985 if (arg == "paragraph") {
986 getLyXText(bv)->insertStringAsParagraphs(bv, clip);
988 getLyXText(bv)->insertStringAsLines(bv, clip);
990 updateLocal(bv, CURSOR_PAR, true);
994 if (!autoBreakRows) {
997 if (cap.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 && (cached_bview == lbv))
1623 // Super UGLY! (Lgb)
1624 BufferView * bv = const_cast<BufferView *>(lbv);
1627 if ((cache.find(bv) != cache.end()) && cache[bv]) {
1628 cached_text = cache[bv];
1629 if (recursive && the_locking_inset)
1630 return the_locking_inset->getLyXText(bv);
1633 cached_text = new LyXText(const_cast<InsetText *>(this));
1634 cached_text->init(bv);
1635 cache[bv] = cached_text;
1636 if (the_locking_inset) {
1637 cached_text->setCursor(bv, inset_par, inset_pos, true, inset_boundary);
1639 return the_locking_inset->getLyXText(bv);
1645 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1648 if ((cache.find(bv) == cache.end()) || !cache[bv])
1653 /// then remove all LyXText in text-insets
1654 Paragraph * p = par;
1655 for (; p; p = p->next()) {
1656 p->deleteInsetsLyXText(bv);
1662 void InsetText::resizeLyXText(BufferView * bv, bool force) const
1664 if (!par->next() && !par->size()) // no data, resize not neccessary!
1666 // one endless line, resize normally not necessary
1667 if (!force && getMaxWidth(bv, this) < 0)
1669 if ((cache.find(bv) == cache.end()) || !cache[bv])
1672 Paragraph * lpar = 0;
1673 Paragraph * selstartpar = 0;
1674 Paragraph * selendpar = 0;
1675 Paragraph::size_type pos = 0;
1676 Paragraph::size_type selstartpos = 0;
1677 Paragraph::size_type selendpos = 0;
1678 bool boundary = false;
1679 bool selstartboundary = false;
1680 bool selendboundary = false;
1681 bool selection = false;
1682 bool mark_set = false;
1684 // ProhibitInput(bv);
1687 LyXText * t = getLyXText(bv);
1688 lpar = t->cursor.par();
1689 pos = t->cursor.pos();
1690 boundary = t->cursor.boundary();
1691 selstartpar = t->selection.start.par();
1692 selstartpos = t->selection.start.pos();
1693 selstartboundary = t->selection.start.boundary();
1694 selendpar = t->selection.end.par();
1695 selendpos = t->selection.end.pos();
1696 selendboundary = t->selection.end.boundary();
1697 selection = t->selection.set();
1698 mark_set = t->selection.mark();
1700 deleteLyXText(bv, (the_locking_inset == 0) || force);
1703 LyXText * t = getLyXText(bv);
1705 t->selection.set(true);
1706 /* at this point just to avoid the Delete-Empty-Paragraph
1707 * Mechanism when setting the cursor */
1708 t->selection.mark(mark_set);
1710 t->setCursor(bv, selstartpar, selstartpos, true, selstartboundary);
1711 t->selection.cursor = t->cursor;
1712 t->setCursor(bv, selendpar, selendpos, true, selendboundary);
1713 t->setSelection(bv);
1714 t->setCursor(bv, lpar, pos);
1716 t->setCursor(bv, lpar, pos, true, boundary);
1717 t->selection.cursor = t->cursor;
1718 t->selection.set(false);
1722 LyXText * t = getLyXText(bv);
1723 t->first = bv->screen()->TopCursorVisible(t);
1726 // this will scroll the screen such that the cursor becomes visible
1727 bv->updateScrollbar();
1729 if (the_locking_inset) {
1730 /// then resize all LyXText in text-insets
1731 inset_x = cx(bv) - top_x + drawTextXOffset;
1732 inset_y = cy(bv) + drawTextYOffset;
1733 for (Paragraph * p = par; p; p = p->next()) {
1734 p->resizeInsetsLyXText(bv);
1741 void InsetText::removeNewlines()
1743 for (Paragraph * p = par; p; p = p->next()) {
1744 for (int i = 0; i < p->size(); ++i) {
1745 if (p->getChar(i) == Paragraph::META_NEWLINE)
1752 bool InsetText::nodraw() const
1754 if (the_locking_inset)
1755 return the_locking_inset->nodraw();
1756 return UpdatableInset::nodraw();
1760 int InsetText::scroll(bool recursive) const
1762 int sx = UpdatableInset::scroll(false);
1764 if (recursive && the_locking_inset)
1765 sx += the_locking_inset->scroll(recursive);
1771 bool InsetText::doClearArea() const
1773 return !locked || (need_update & (FULL|INIT));
1777 void InsetText::selectAll(BufferView * bv)
1779 getLyXText(bv)->cursorTop(bv);
1780 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1781 getLyXText(bv)->cursorBottom(bv);
1782 getLyXText(bv)->setSelection(bv);
1786 void InsetText::clearSelection(BufferView * bv)
1788 getLyXText(bv)->clearSelection(bv);
1792 void InsetText::clearInset(Painter & pain, int baseline, bool & cleared) const
1795 int h = insetAscent + insetDescent;
1796 int ty = baseline - insetAscent;
1802 if ((ty + h) > pain.paperHeight())
1803 h = pain.paperHeight();
1804 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
1805 w = pain.paperWidth();
1806 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);