1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998-2001 The LyX Team.
8 * ======================================================
20 #pragma implementation
23 #include "insettext.h"
24 #include "paragraph.h"
28 #include "commandtags.h"
31 #include "BufferView.h"
33 #include "LaTeXFeatures.h"
35 #include "lyx_gui_misc.h"
37 #include "lyxcursor.h"
38 #include "CutAndPaste.h"
41 #include "support/textutils.h"
42 #include "support/LAssert.h"
46 #include "trans_mgr.h"
47 #include "lyxscreen.h"
51 #include "ParagraphParameters.h"
52 #include "undo_funcs.h"
62 extern unsigned char getCurrentTextClass(Buffer *);
63 extern bool math_insert_greek(BufferView *, char);
64 extern int greek_kb_flag;
67 #warning this functions should probably go into bufferview_funcs somehow (Jug)
69 void InsetText::saveLyXTextState(LyXText * t) const
71 sstate.lpar = t->cursor.par();
72 sstate.pos = t->cursor.pos();
73 sstate.boundary = t->cursor.boundary();
74 sstate.selstartpar = t->selection.start.par();
75 sstate.selstartpos = t->selection.start.pos();
76 sstate.selstartboundary = t->selection.start.boundary();
77 sstate.selendpar = t->selection.end.par();
78 sstate.selendpos = t->selection.end.pos();
79 sstate.selendboundary = t->selection.end.boundary();
80 sstate.selection = t->selection.set();
81 sstate.mark_set = t->selection.mark();
84 void InsetText::restoreLyXTextState(BufferView * bv, LyXText * t) const
87 t->selection.set(true);
88 /* at this point just to avoid the Delete-Empty-Paragraph
89 * Mechanism when setting the cursor */
90 t->selection.mark(sstate.mark_set);
91 if (sstate.selection) {
92 t->setCursor(bv, sstate.selstartpar, sstate.selstartpos,
93 true, sstate.selstartboundary);
94 t->selection.cursor = t->cursor;
95 t->setCursor(bv, sstate.selendpar, sstate.selendpos,
96 true, sstate.selendboundary);
98 t->setCursor(bv, sstate.lpar, sstate.pos);
100 t->setCursor(bv, sstate.lpar, sstate.pos, true, sstate.boundary);
101 t->selection.cursor = t->cursor;
102 t->selection.set(false);
108 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
115 InsetText::InsetText()
122 InsetText::InsetText(InsetText const & ins, bool same_id)
127 autoBreakRows = ins.autoBreakRows;
131 InsetText & InsetText::operator=(InsetText const & it)
134 autoBreakRows = it.autoBreakRows;
139 void InsetText::init(InsetText const * ins, bool same_id)
147 the_locking_inset = 0;
149 no_selection = false;
153 autoBreakRows = false;
157 setParagraphData(ins->par);
158 autoBreakRows = ins->autoBreakRows;
159 drawFrame_ = ins->drawFrame_;
163 par->setInsetOwner(this);
164 frame_color = LColor::insetframe;
167 last_drawn_width = -1;
168 frame_is_visible = false;
175 InsetText::~InsetText()
182 Paragraph * tmp = par->next();
189 void InsetText::clear()
193 // now also delete all caches this should be safe, hopefully
197 Paragraph * tmp = par->next();
205 Inset * InsetText::clone(Buffer const &, bool same_id) const
207 InsetText * t = new InsetText(*this, same_id);
212 void InsetText::write(Buffer const * buf, ostream & os) const
215 writeParagraphData(buf, os);
219 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
221 par->writeFile(buf, os, buf->params, 0);
225 void InsetText::read(Buffer const * buf, LyXLex & lex)
229 Paragraph * return_par = 0;
230 Paragraph::depth_type depth = 0;
231 LyXFont font(LyXFont::ALL_INHERIT);
237 token = lex.GetString();
240 if (token == "\\end_inset")
242 if (const_cast<Buffer*>(buf)->
243 parseSingleLyXformat2Token(lex, par, return_par,
244 token, pos, depth, font)) {
245 // the_end read this should NEVER happen
246 lex.printError("\\the_end read in inset! Error in document!");
254 return_par->setInsetOwner(this);
255 return_par = return_par->next();
258 if (token != "\\end_inset") {
259 lex.printError("Missing \\end_inset at this point. "
266 int InsetText::ascent(BufferView * bv, LyXFont const &) const
269 Row * row = getLyXText(bv)->getRowNearY(y_temp);
270 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
275 int InsetText::descent(BufferView * bv, LyXFont const &) const
283 Row * row = lt->getRowNearY(y_temp);
284 insetDescent = lt->height - row->ascent_of_text() +
285 TEXT_TO_INSET_OFFSET;
292 int InsetText::width(BufferView * bv, LyXFont const &) const
294 insetWidth = max(textWidth(bv),
295 (int)getLyXText(bv)->width + (2 * TEXT_TO_INSET_OFFSET));
300 int InsetText::textWidth(BufferView * bv) const
302 int const w = getMaxWidth(bv, this);
307 void InsetText::draw(BufferView * bv, LyXFont const & f,
308 int baseline, float & x, bool cleared) const
313 Painter & pain = bv->painter();
315 // no draw is necessary !!!
316 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
318 top_baseline = baseline;
320 if (!cleared && (need_update & CLEAR_FRAME))
321 clearFrame(pain, cleared);
323 frame_is_visible = false;
330 UpdatableInset::draw(bv, f, baseline, x, cleared);
333 x += static_cast<float>(scroll());
336 // update insetWidth and insetHeight with dummy calls
338 (void)descent(bv, f);
342 // if top_x differs we have a rule down and we don't have to clear anything
343 if (!cleared && (top_x == int(x)) &&
344 ((need_update&(INIT|FULL)) || (top_baseline!=baseline) ||
345 (last_drawn_width!=insetWidth))) {
346 clearInset(pain, baseline, cleared);
349 frame_is_visible = false;
351 if (!cleared && (need_update == NONE))
354 if (top_x != int(x)) {
355 if ((getMaxWidth(bv, this) > 0) &&
356 (getLyXText(bv)->width != old_max_width)) {
359 old_max_width = getLyXText(bv)->width;
360 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
363 clearInset(pain, baseline, cleared);
366 // lyxerr << "InsetText::draw[" << this << "](" << need_update << ":" << int(x) << ":" << top_x << ")\n";
368 if (cleared || (last_drawn_width != insetWidth)) {
370 last_drawn_width = insetWidth;
373 top_baseline = baseline;
374 top_y = baseline - ascent(bv, f);
375 last_width = width(bv, f);
376 last_height = ascent(bv, f) + descent(bv, f);
378 if (the_locking_inset && (cpar(bv) == inset_par)
379 && (cpos(bv) == inset_pos)) {
380 inset_x = cx(bv) - top_x + drawTextXOffset;
381 inset_y = cy(bv) + drawTextYOffset;
383 if (!cleared && (need_update == CURSOR)
384 && !getLyXText(bv)->selection.set()) {
385 drawFrame(pain, cleared);
386 x += last_width; // was width(bv, f);
395 x += TEXT_TO_INSET_OFFSET;
398 Row * row = lt->getRowNearY(y);
399 int y_offset = baseline - row->ascent_of_text();
400 int ph = pain.paperHeight();
403 while ((row != 0) && ((y+row->height()) <= 0)) {
405 first += row->height();
414 while ((row != 0) && (yf < ph)) {
415 lt->getVisibleRow(bv, y+y_offset, int(x), row,
421 } else if (!locked) {
422 if (need_update & CURSOR) {
423 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
424 lt->clearSelection(bv);
425 lt->selection.cursor = lt->cursor;
427 bv->screen()->update(lt, bv, y_offset, int(x));
430 if (need_update & SELECTION)
431 bv->screen()->toggleToggle(lt, bv, y_offset, int(x));
432 else if (need_update & CURSOR) {
433 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
434 lt->clearSelection(bv);
435 lt->selection.cursor = lt->cursor;
437 bv->screen()->update(lt, bv, y_offset, int(x));
442 lt->status(bv, LyXText::UNCHANGED);
443 if ((need_update != CURSOR_PAR) &&
444 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked)))
445 drawFrame(pain, cleared);
446 else if (need_update & CLEAR_FRAME)
447 clearFrame(pain, cleared);
448 x += last_width /* was width(bv, f) */ - TEXT_TO_INSET_OFFSET;
449 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
451 } else if (need_update != INIT)
458 void InsetText::drawFrame(Painter & pain, bool cleared) const
460 if (!frame_is_visible || cleared) {
461 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
462 insetWidth - 1, insetAscent + insetDescent - 1,
464 frame_is_visible = true;
469 void InsetText::clearFrame(Painter & pain, bool cleared) const
471 if (frame_is_visible) {
473 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
474 insetWidth - 1, insetAscent + insetDescent - 1,
477 frame_is_visible = false;
482 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
488 owner()->update(bv, font, true);
491 if (the_locking_inset) {
492 inset_x = cx(bv) - top_x + drawTextXOffset;
493 inset_y = cy(bv) + drawTextYOffset;
494 the_locking_inset->update(bv, font, reinit);
502 int oldw = insetWidth;
503 insetWidth = lt->width + (2 * TEXT_TO_INSET_OFFSET);
504 if (oldw != insetWidth) {
511 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
514 lt->updateInset(bv, the_locking_inset);
516 if (lt->status() == LyXText::NEED_MORE_REFRESH)
523 void InsetText::setUpdateStatus(BufferView * bv, int what) const
531 if (lt->status() == LyXText::NEED_MORE_REFRESH)
533 else if (lt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)
534 need_update |= CURSOR_PAR;
536 // this to not draw a selection when we redraw all of it!
537 if ((need_update & (INIT|FULL)) && (need_update & CURSOR))
538 lt->clearSelection(bv);
544 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
552 setUpdateStatus(bv, what);
553 if ((need_update != CURSOR) || (lt->status() != LyXText::UNCHANGED) ||
556 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
558 bv->owner()->showState();
559 if (old_par != cpar(bv)) {
560 bv->owner()->setLayout(cpar(bv)->getLayout());
568 string const InsetText::editMessage() const
570 return _("Opened Text Inset");
574 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
576 UpdatableInset::edit(bv, x, y, button);
578 if (!bv->lockInset(this)) {
579 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
583 the_locking_inset = 0;
584 inset_pos = inset_x = inset_y = 0;
585 inset_boundary = false;
588 int tmp_y = (y < 0) ? 0 : y;
595 if (!checkAndActivateInset(bv, x, tmp_y, button))
596 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
598 lt->clearSelection(bv);
601 updateLocal(bv, CURSOR, false);
603 // If the inset is empty set the language of the current font to the
604 // language to the surronding text (if different).
605 if (par->size() == 0 && !par->next() &&
606 bv->getParentLanguage(this) != lt->current_font.language()) {
607 LyXFont font(LyXFont::ALL_IGNORE);
608 font.setLanguage(bv->getParentLanguage(this));
609 setFont(bv, font, false);
616 void InsetText::edit(BufferView * bv, bool front)
618 UpdatableInset::edit(bv, front);
620 if (!bv->lockInset(this)) {
621 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
625 the_locking_inset = 0;
626 inset_pos = inset_x = inset_y = 0;
627 inset_boundary = false;
636 lt->setCursor(bv, par, 0);
641 lt->setCursor(bv, p, p->size()-1);
643 lt->clearSelection(bv);
646 updateLocal(bv, CURSOR, false);
648 // If the inset is empty set the language of the current font to the
649 // language to the surronding text (if different).
650 if (par->size() == 0 && !par->next() &&
651 bv->getParentLanguage(this) != lt->current_font.language()) {
652 LyXFont font(LyXFont::ALL_IGNORE);
653 font.setLanguage(bv->getParentLanguage(this));
654 setFont(bv, font, false);
661 void InsetText::insetUnlock(BufferView * bv)
663 if (the_locking_inset) {
664 the_locking_inset->insetUnlock(bv);
665 the_locking_inset = 0;
668 no_selection = false;
670 int code = CURSOR|CLEAR_FRAME;
676 if (lt->selection.set()) {
677 lt->clearSelection(bv);
679 } else if (owner()) {
680 bv->owner()->setLayout(owner()->getLyXText(bv)
681 ->cursor.par()->getLayout());
683 bv->owner()->setLayout(bv->text->cursor.par()->getLayout());
684 updateLocal(bv, code, false);
690 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
692 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
696 if (inset == cpar(bv)->getInset(cpos(bv))) {
697 lyxerr[Debug::INSETS] << "OK" << endl;
698 the_locking_inset = inset;
699 inset_x = cx(bv) - top_x + drawTextXOffset;
700 inset_y = cy(bv) + drawTextYOffset;
701 inset_pos = cpos(bv);
702 inset_par = cpar(bv);
703 inset_boundary = cboundary(bv);
704 updateLocal(bv, CURSOR, false);
706 } else if (the_locking_inset && (the_locking_inset == inset)) {
707 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
708 lyxerr[Debug::INSETS] << "OK" << endl;
709 inset_x = cx(bv) - top_x + drawTextXOffset;
710 inset_y = cy(bv) + drawTextYOffset;
712 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
714 } else if (the_locking_inset) {
715 lyxerr[Debug::INSETS] << "MAYBE" << endl;
716 return the_locking_inset->lockInsetInInset(bv, inset);
718 lyxerr[Debug::INSETS] << "NOT OK" << endl;
723 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
726 if (!the_locking_inset)
728 if (the_locking_inset == inset) {
729 the_locking_inset->insetUnlock(bv);
730 getLyXText(bv)->updateInset(bv, inset);
731 the_locking_inset = 0;
733 moveRight(bv, false);
734 old_par = 0; // force layout setting
738 updateLocal(bv, CURSOR, false);
741 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
745 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
747 if (!the_locking_inset)
749 if (the_locking_inset != inset) {
750 getLyXText(bv)->updateInset(bv, the_locking_inset);
751 setUpdateStatus(bv, CURSOR_PAR);
752 return the_locking_inset->updateInsetInInset(bv, inset);
754 // updateLocal(bv, FULL, false);
755 if (getLyXText(bv)->updateInset(bv, inset))
756 updateLocal(bv, CURSOR_PAR, false);
757 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
758 inset_x = cx(bv) - top_x + drawTextXOffset;
759 inset_y = cy(bv) + drawTextYOffset;
765 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
769 int tmp_x = x - drawTextXOffset;
770 int tmp_y = y + insetAscent - getLyXText(bv)->first;
771 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y, button);
774 if (the_locking_inset) {
775 if (the_locking_inset == inset) {
776 the_locking_inset->insetButtonPress(bv,x-inset_x,y-inset_y,button);
777 no_selection = false;
780 // otherwise unlock the_locking_inset and lock the new inset
781 the_locking_inset->insetUnlock(bv);
782 inset_x = cx(bv) - top_x + drawTextXOffset;
783 inset_y = cy(bv) + drawTextYOffset;
784 the_locking_inset = static_cast<UpdatableInset*>(inset);
785 inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
786 inset->edit(bv, x - inset_x, y - inset_y, button);
787 if (the_locking_inset)
788 updateLocal(bv, CURSOR, false);
789 no_selection = false;
792 // otherwise only unlock the_locking_inset
793 the_locking_inset->insetUnlock(bv);
794 the_locking_inset = 0;
796 if (bv->theLockingInset()) {
797 if (inset && inset->editable() == Inset::HIGHLY_EDITABLE) {
798 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
799 inset_x = cx(bv) - top_x + drawTextXOffset;
800 inset_y = cy(bv) + drawTextYOffset;
801 inset_pos = cpos(bv);
802 inset_par = cpar(bv);
803 inset_boundary = cboundary(bv);
804 the_locking_inset = uinset;
805 uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
807 uinset->edit(bv, x - inset_x, y - inset_y, 0);
808 if (the_locking_inset)
809 updateLocal(bv, CURSOR, false);
810 no_selection = false;
814 if (!inset) { // && (button == 2)) {
815 bool paste_internally = false;
816 if ((button == 2) && getLyXText(bv)->selection.set()) {
817 localDispatch(bv, LFUN_COPY, "");
818 paste_internally = true;
820 getLyXText(bv)->setCursorFromCoordinates(bv, x-drawTextXOffset,
822 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
823 updateLocal(bv, CURSOR, false);
824 bv->owner()->setLayout(cpar(bv)->getLayout());
826 // Insert primary selection with middle mouse
827 // if there is a local selection in the current buffer,
830 if (paste_internally)
831 localDispatch(bv, LFUN_PASTE, "");
833 localDispatch(bv, LFUN_PASTESELECTION,
838 no_selection = false;
842 void InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
844 UpdatableInset * inset = 0;
846 if (the_locking_inset) {
847 the_locking_inset->insetButtonRelease(bv,
848 x - inset_x, y - inset_y,
851 if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
852 inset = static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
853 if (inset->editable() == Inset::HIGHLY_EDITABLE) {
854 inset->insetButtonRelease(bv,
856 y - inset_y, button);
858 inset_x = cx(bv) - top_x + drawTextXOffset;
859 inset_y = cy(bv) + drawTextYOffset;
860 inset->insetButtonRelease(bv,
862 y - inset_y, button);
864 x - inset_x, y - inset_y, button);
866 updateLocal(bv, CURSOR_PAR, false);
869 no_selection = false;
873 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
877 if (the_locking_inset) {
878 the_locking_inset->insetMotionNotify(bv, x - inset_x,
882 LyXText * t = getLyXText(bv);
884 t->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
886 if (t->toggle_cursor.par() != t->toggle_end_cursor.par() ||
887 t->toggle_cursor.pos() != t->toggle_end_cursor.pos())
888 updateLocal(bv, SELECTION, false);
893 void InsetText::insetKeyPress(XKeyEvent * xke)
895 if (the_locking_inset) {
896 the_locking_inset->insetKeyPress(xke);
902 UpdatableInset::RESULT
903 InsetText::localDispatch(BufferView * bv,
904 kb_action action, string const & arg)
906 no_selection = false;
907 UpdatableInset::RESULT
908 result= UpdatableInset::localDispatch(bv, action, arg);
909 if (result != UNDISPATCHED) {
914 if ((action < 0) && arg.empty())
917 if (the_locking_inset) {
918 result = the_locking_inset->localDispatch(bv, action, arg);
919 if (result == DISPATCHED_NOUPDATE)
921 else if (result == DISPATCHED) {
922 updateLocal(bv, CURSOR_PAR, false);
924 } else if (result == FINISHED) {
925 bool dispatched = false;
927 case LFUN_UNKNOWN_ACTION:
928 case LFUN_BREAKPARAGRAPH:
930 moveRightIntern(bv, false, false);
933 if (!getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
934 moveRightIntern(bv, false, false);
938 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
939 moveRightIntern(bv, false, false);
945 the_locking_inset = 0;
958 case LFUN_SELFINSERT:
959 if (bv->buffer()->isReadonly()) {
960 // setErrorMessage(N_("Document is read only"));
964 /* Automatically delete the currently selected
965 * text and replace it with what is being
966 * typed in now. Depends on lyxrc settings
967 * "auto_region_delete", which defaults to
970 setUndo(bv, Undo::INSERT,
971 lt->cursor.par(), lt->cursor.par()->next());
973 if (lyxrc.auto_region_delete) {
974 if (lt->selection.set()) {
975 lt->cutSelection(bv, false);
978 lt->clearSelection(bv);
979 for (string::size_type i = 0; i < arg.length(); ++i) {
981 if (!math_insert_greek(bv, arg[i])) {
982 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
983 } else if (!the_locking_inset) {
984 (void)moveRight(bv, false);
987 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
991 lt->selection.cursor = lt->cursor;
992 updateLocal(bv, CURSOR_PAR, true);
993 result = DISPATCHED_NOUPDATE;
995 // --- Cursor Movements -----------------------------------
998 moveRight(bv, false, true);
999 lt->setSelection(bv);
1000 updateLocal(bv, SELECTION, false);
1003 result = moveRight(bv);
1005 updateLocal(bv, CURSOR, false);
1009 moveLeft(bv, false, true);
1010 lt->setSelection(bv);
1011 updateLocal(bv, SELECTION, false);
1015 result = moveLeft(bv);
1016 updateLocal(bv, CURSOR, false);
1021 lt->setSelection(bv);
1022 updateLocal(bv, SELECTION, false);
1026 result = moveDown(bv);
1027 updateLocal(bv, CURSOR, false);
1032 lt->setSelection(bv);
1033 updateLocal(bv, SELECTION, false);
1037 result = moveUp(bv);
1038 updateLocal(bv, CURSOR, false);
1043 updateLocal(bv, CURSOR, false);
1047 updateLocal(bv, CURSOR, false);
1049 case LFUN_BACKSPACE: {
1050 setUndo(bv, Undo::DELETE,
1051 lt->cursor.par(), lt->cursor.par()->next());
1052 if (lt->selection.set())
1053 lt->cutSelection(bv);
1056 updateLocal(bv, CURSOR_PAR, true);
1061 setUndo(bv, Undo::DELETE,
1062 lt->cursor.par(), lt->cursor.par()->next());
1063 if (lt->selection.set()) {
1064 lt->cutSelection(bv);
1068 updateLocal(bv, CURSOR_PAR, true);
1073 setUndo(bv, Undo::DELETE,
1074 lt->cursor.par(), lt->cursor.par()->next());
1075 lt->cutSelection(bv);
1076 updateLocal(bv, CURSOR_PAR, true);
1082 lt->copySelection(bv);
1083 updateLocal(bv, CURSOR_PAR, false);
1085 case LFUN_PASTESELECTION:
1087 string const clip(bv->getClipboard());
1091 if (arg == "paragraph") {
1092 lt->insertStringAsParagraphs(bv, clip);
1094 lt->insertStringAsLines(bv, clip);
1096 updateLocal(bv, CURSOR_PAR, true);
1100 if (!autoBreakRows) {
1102 if (CutAndPaste::nrOfParagraphs() > 1) {
1103 WriteAlert(_("Impossible operation"),
1104 _("Cannot include more than one paragraph!"),
1109 setUndo(bv, Undo::INSERT,
1110 lt->cursor.par(), lt->cursor.par()->next());
1111 lt->pasteSelection(bv);
1112 updateLocal(bv, CURSOR_PAR, true);
1116 case LFUN_BREAKPARAGRAPH:
1117 if (!autoBreakRows) {
1118 result = DISPATCHED;
1121 lt->breakParagraph(bv, 0);
1122 updateLocal(bv, FULL, true);
1124 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1125 if (!autoBreakRows) {
1126 result = DISPATCHED;
1129 lt->breakParagraph(bv, 1);
1130 updateLocal(bv, FULL, true);
1133 case LFUN_BREAKLINE: {
1134 if (!autoBreakRows) {
1135 result = DISPATCHED;
1138 setUndo(bv, Undo::INSERT,
1139 lt->cursor.par(), lt->cursor.par()->next());
1140 lt->insertChar(bv, Paragraph::META_NEWLINE);
1141 updateLocal(bv, CURSOR_PAR, true);
1146 // do not set layouts on non breakable textinsets
1147 if (autoBreakRows) {
1148 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1150 // Derive layout number from given argument (string)
1151 // and current buffer's textclass (number). */
1152 LyXTextClassList::ClassList::size_type tclass =
1153 bv->buffer()->params.textclass;
1154 std::pair <bool, LyXTextClass::size_type> layout =
1155 textclasslist.NumberOfLayout(tclass, arg);
1157 // If the entry is obsolete, use the new one instead.
1159 string obs = textclasslist.Style(tclass,layout.second).
1162 layout = textclasslist.NumberOfLayout(tclass, obs);
1165 // see if we found the layout number:
1166 if (!layout.first) {
1167 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1168 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1172 if (cur_layout != layout.second) {
1173 cur_layout = layout.second;
1174 lt->setLayout(bv, layout.second);
1175 bv->owner()->setLayout(cpar(bv)->getLayout());
1176 updateLocal(bv, CURSOR_PAR, true);
1179 // reset the layout box
1180 bv->owner()->setLayout(cpar(bv)->getLayout());
1183 case LFUN_PARAGRAPH_SPACING:
1184 // This one is absolutely not working. When fiddling with this
1185 // it also seems to me that the paragraphs inside the insettext
1186 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1188 Paragraph * par = lt->cursor.par();
1189 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1190 float cur_value = 1.0;
1191 if (cur_spacing == Spacing::Other) {
1192 cur_value = par->params().spacing().getValue();
1195 istringstream istr(arg.c_str());
1198 Spacing::Space new_spacing = cur_spacing;
1199 float new_value = cur_value;
1201 lyxerr << "Missing argument to `paragraph-spacing'"
1203 } else if (tmp == "single") {
1204 new_spacing = Spacing::Single;
1205 } else if (tmp == "onehalf") {
1206 new_spacing = Spacing::Onehalf;
1207 } else if (tmp == "double") {
1208 new_spacing = Spacing::Double;
1209 } else if (tmp == "other") {
1210 new_spacing = Spacing::Other;
1213 lyxerr << "new_value = " << tmpval << endl;
1216 } else if (tmp == "default") {
1217 new_spacing = Spacing::Default;
1219 lyxerr << _("Unknown spacing argument: ")
1222 if (cur_spacing != new_spacing || cur_value != new_value) {
1223 par->params().spacing(Spacing(new_spacing, new_value));
1224 updateLocal(bv, CURSOR_PAR, true);
1230 if (!bv->Dispatch(action, arg))
1231 result = UNDISPATCHED;
1235 /// If the action has deleted all text in the inset, we need to change the
1236 // language to the language of the surronding text.
1237 if (par->size() == 0 && !par->next()) {
1238 LyXFont font(LyXFont::ALL_IGNORE);
1239 font.setLanguage(bv->getParentLanguage(this));
1240 setFont(bv, font, false);
1243 if (result != FINISHED) {
1244 showInsetCursor(bv);
1246 bv->unlockInset(this);
1253 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1256 buf->latexParagraphs(os, par, 0, texrow);
1257 return texrow.rows();
1261 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1263 Paragraph * p = par;
1264 unsigned int lines = 0;
1267 string const tmp = buf->asciiParagraph(p, linelen);
1268 lines += countChar(tmp, '\n');
1276 int InsetText::docBook(Buffer const * buf, ostream & os) const
1278 Paragraph * p = par;
1279 unsigned int lines = 0;
1284 buf->simpleDocBookOnePar(os, tmp, p, desc, 0);
1292 void InsetText::validate(LaTeXFeatures & features) const
1294 Paragraph * p = par;
1296 p->validate(features);
1302 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1304 if (textclasslist.Style(buf->params.textclass,
1305 p->getLayout()).labeltype != LABEL_MANUAL)
1308 return p->beginningOfMainBody();
1312 void InsetText::getCursorPos(BufferView * bv,
1313 int & x, int & y) const
1315 if (the_locking_inset) {
1316 the_locking_inset->getCursorPos(bv, x, y);
1324 unsigned int InsetText::insetInInsetY()
1326 if (!the_locking_inset)
1329 return (inset_y + the_locking_inset->insetInInsetY());
1333 void InsetText::toggleInsetCursor(BufferView * bv)
1335 if (the_locking_inset) {
1336 the_locking_inset->toggleInsetCursor(bv);
1340 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1342 int const asc = lyxfont::maxAscent(font);
1343 int const desc = lyxfont::maxDescent(font);
1345 if (isCursorVisible())
1346 bv->hideLockedInsetCursor();
1348 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1349 toggleCursorVisible();
1353 void InsetText::showInsetCursor(BufferView * bv, bool show)
1355 if (the_locking_inset) {
1356 the_locking_inset->showInsetCursor(bv, show);
1359 if (!isCursorVisible()) {
1360 LyXFont const font =
1361 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1363 int const asc = lyxfont::maxAscent(font);
1364 int const desc = lyxfont::maxDescent(font);
1366 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1368 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1369 setCursorVisible(true);
1374 void InsetText::hideInsetCursor(BufferView * bv)
1376 if (isCursorVisible()) {
1377 bv->hideLockedInsetCursor();
1378 setCursorVisible(false);
1380 if (the_locking_inset)
1381 the_locking_inset->hideInsetCursor(bv);
1385 void InsetText::fitInsetCursor(BufferView * bv) const
1387 if (the_locking_inset) {
1388 the_locking_inset->fitInsetCursor(bv);
1391 LyXFont const font =
1392 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1394 int const asc = lyxfont::maxAscent(font);
1395 int const desc = lyxfont::maxDescent(font);
1397 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1401 UpdatableInset::RESULT
1402 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1404 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1405 return moveLeftIntern(bv, false, activate_inset, selecting);
1407 return moveRightIntern(bv, false, activate_inset, selecting);
1411 UpdatableInset::RESULT
1412 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1414 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1415 return moveRightIntern(bv, true, activate_inset, selecting);
1417 return moveLeftIntern(bv, true, activate_inset, selecting);
1421 UpdatableInset::RESULT
1422 InsetText::moveRightIntern(BufferView * bv, bool behind,
1423 bool activate_inset, bool selecting)
1425 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1427 if (activate_inset && checkAndActivateInset(bv, behind))
1429 getLyXText(bv)->cursorRight(bv);
1431 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1432 return DISPATCHED_NOUPDATE;
1436 UpdatableInset::RESULT
1437 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1438 bool activate_inset, bool selecting)
1440 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1442 getLyXText(bv)->cursorLeft(bv);
1444 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1445 if (activate_inset && checkAndActivateInset(bv, behind))
1447 return DISPATCHED_NOUPDATE;
1451 UpdatableInset::RESULT
1452 InsetText::moveUp(BufferView * bv)
1454 if (!crow(bv)->previous())
1456 getLyXText(bv)->cursorUp(bv);
1457 return DISPATCHED_NOUPDATE;
1461 UpdatableInset::RESULT
1462 InsetText::moveDown(BufferView * bv)
1464 if (!crow(bv)->next())
1466 getLyXText(bv)->cursorDown(bv);
1467 return DISPATCHED_NOUPDATE;
1471 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1473 if (the_locking_inset) {
1474 if (the_locking_inset->insetAllowed(inset))
1475 return the_locking_inset->insertInset(bv, inset);
1478 inset->setOwner(this);
1479 hideInsetCursor(bv);
1483 lt = getLyXText(bv);
1486 lt->insertInset(bv, inset);
1487 if ((cpar(bv)->getChar(cpos(bv)) != Paragraph::META_INSET) ||
1488 (cpar(bv)->getInset(cpos(bv)) != inset))
1491 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1492 showInsetCursor(bv);
1499 bool InsetText::insetAllowed(Inset::Code code) const
1501 if (the_locking_inset)
1502 return the_locking_inset->insetAllowed(code);
1507 UpdatableInset * InsetText::getLockingInset() const
1509 return the_locking_inset ? the_locking_inset->getLockingInset() :
1510 const_cast<InsetText *>(this);
1514 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1518 if (the_locking_inset)
1519 return the_locking_inset->getFirstLockingInsetOfType(c);
1524 bool InsetText::showInsetDialog(BufferView * bv) const
1526 if (the_locking_inset)
1527 return the_locking_inset->showInsetDialog(bv);
1532 std::vector<string> const InsetText::getLabelList() const
1534 std::vector<string> label_list;
1536 Paragraph * tpar = par;
1538 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1539 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1540 for (; beg != end; ++beg) {
1541 std::vector<string> const l = (*beg)->getLabelList();
1542 label_list.insert(label_list.end(), l.begin(), l.end());
1544 tpar = tpar->next();
1550 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1553 if (the_locking_inset) {
1554 the_locking_inset->setFont(bv, font, toggleall, selectall);
1559 lt = getLyXText(bv);
1562 if (lt->selection.set()) {
1563 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1568 lt->toggleFree(bv, font, toggleall);
1570 lt->setFont(bv, font, toggleall);
1573 lt->clearSelection(bv);
1575 if (selectall || lt->selection.set())
1576 updateLocal(bv, FULL, true);
1578 updateLocal(bv, CURSOR_PAR, true);
1584 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1586 if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
1590 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1591 if (!inset || inset->editable() != Inset::HIGHLY_EDITABLE)
1593 LyXFont const font =
1594 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1596 x = inset->width(bv, font);
1597 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1600 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1602 //inset_x = cx(bv) - top_x + drawTextXOffset;
1603 //inset_y = cy(bv) + drawTextYOffset;
1604 inset->edit(bv, x, y, 0);
1605 if (!the_locking_inset)
1607 updateLocal(bv, CURSOR, false);
1614 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1617 x -= drawTextXOffset;
1619 int dummyy = y + insetAscent;
1620 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy, button);
1627 inset_x = cx(bv) - top_x + drawTextXOffset;
1628 inset_y = cy(bv) + drawTextYOffset;
1629 inset->edit(bv, x - inset_x, y - inset_y, button);
1630 if (!the_locking_inset)
1632 updateLocal(bv, CURSOR, false);
1639 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1641 int w = UpdatableInset::getMaxWidth(bv, inset);
1646 w = w - top_x + owner()->x();
1649 w -= (2 * TEXT_TO_INSET_OFFSET);
1651 // return w - (2*TEXT_TO_INSET_OFFSET);
1655 void InsetText::setParagraphData(Paragraph * p)
1659 // now also delete all caches this should be safe, hopefully
1663 Paragraph * tmp = par->next();
1668 par = new Paragraph(*p);
1669 par->setInsetOwner(this);
1670 Paragraph * np = par;
1673 np->next(new Paragraph(*p));
1674 np->next()->previous(np);
1676 np->setInsetOwner(this);
1683 void InsetText::setText(string const & data)
1686 LyXFont font(LyXFont::ALL_SANE);
1687 for (unsigned int i=0; i < data.length(); ++i)
1688 par->insertChar(i, data[i], font);
1692 void InsetText::setAutoBreakRows(bool flag)
1694 if (flag != autoBreakRows) {
1695 autoBreakRows = flag;
1703 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1705 if (how != drawFrame_) {
1708 updateLocal(bv, DRAW_FRAME, false);
1713 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1715 if (frame_color != col) {
1718 updateLocal(bv, DRAW_FRAME, false);
1723 int InsetText::cx(BufferView * bv) const
1727 lt = getLyXText(bv);
1730 int x = lt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1731 if (the_locking_inset) {
1732 LyXFont font = lt->getFont(bv->buffer(),
1735 if (font.isVisibleRightToLeft())
1736 x -= the_locking_inset->width(bv, font);
1744 int InsetText::cy(BufferView * bv) const
1747 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1751 Paragraph::size_type InsetText::cpos(BufferView * bv) const
1753 return getLyXText(bv)->cursor.pos();
1757 Paragraph * InsetText::cpar(BufferView * bv) const
1759 return getLyXText(bv)->cursor.par();
1763 bool InsetText::cboundary(BufferView * bv) const
1765 return getLyXText(bv)->cursor.boundary();
1769 Row * InsetText::crow(BufferView * bv) const
1771 return getLyXText(bv)->cursor.row();
1775 LyXText * InsetText::getLyXText(BufferView const * lbv,
1776 bool const recursive) const
1778 if (!recursive && (cached_bview == lbv))
1779 return cached_text.get();
1781 // Super UGLY! (Lgb)
1782 BufferView * bv = const_cast<BufferView *>(lbv);
1785 Cache::iterator it = cache.find(bv);
1787 if (it != cache.end() && (lt || !it->second.remove)) {
1788 lyx::Assert(it->second.text.get());
1790 cached_text = it->second.text;
1791 if (recursive && the_locking_inset) {
1792 return the_locking_inset->getLyXText(bv);
1794 return cached_text.get();
1795 } else if (it->second.remove) {
1797 saveLyXTextState(it->second.text.get());
1805 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
1806 cached_text->init(bv);
1807 restoreLyXTextState(bv, cached_text.get());
1809 cache.insert(make_pair(bv, cached_text));
1811 if (the_locking_inset && recursive) {
1812 return the_locking_inset->getLyXText(bv);
1814 return cached_text.get();
1818 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1822 Cache::iterator it = cache.find(bv);
1824 if (it == cache.end()) {
1828 lyx::Assert(it->second.text.get());
1830 it->second.remove = true;
1832 /// then remove all LyXText in text-insets
1833 Paragraph * p = par;
1834 for (; p; p = p->next()) {
1835 p->deleteInsetsLyXText(bv);
1841 void InsetText::resizeLyXText(BufferView * bv, bool force) const
1843 if (!par->next() && !par->size()) // no data, resize not neccessary!
1845 // one endless line, resize normally not necessary
1846 if (!force && getMaxWidth(bv, this) < 0)
1849 Cache::iterator it = cache.find(bv);
1850 if (it == cache.end()) {
1853 lyx::Assert(it->second.text.get());
1855 LyXText * t = it->second.text.get();
1856 saveLyXTextState(t);
1857 for (Paragraph * p = par; p; p = p->next()) {
1858 p->resizeInsetsLyXText(bv);
1861 restoreLyXTextState(bv, t);
1862 if (the_locking_inset) {
1863 inset_x = cx(bv) - top_x + drawTextXOffset;
1864 inset_y = cy(bv) + drawTextYOffset;
1868 t->first = bv->screen()->topCursorVisible(t);
1871 updateLocal(bv, FULL, false);
1874 // this will scroll the screen such that the cursor becomes visible
1875 bv->updateScrollbar();
1879 void InsetText::removeNewlines()
1881 for (Paragraph * p = par; p; p = p->next()) {
1882 for (int i = 0; i < p->size(); ++i) {
1883 if (p->getChar(i) == Paragraph::META_NEWLINE)
1890 bool InsetText::nodraw() const
1892 if (the_locking_inset)
1893 return the_locking_inset->nodraw();
1894 return UpdatableInset::nodraw();
1898 int InsetText::scroll(bool recursive) const
1900 int sx = UpdatableInset::scroll(false);
1902 if (recursive && the_locking_inset)
1903 sx += the_locking_inset->scroll(recursive);
1909 bool InsetText::doClearArea() const
1911 return !locked || (need_update & (FULL|INIT));
1915 void InsetText::selectAll(BufferView * bv)
1917 getLyXText(bv)->cursorTop(bv);
1918 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1919 getLyXText(bv)->cursorBottom(bv);
1920 getLyXText(bv)->setSelection(bv);
1924 void InsetText::clearSelection(BufferView * bv)
1926 getLyXText(bv)->clearSelection(bv);
1930 void InsetText::clearInset(Painter & pain, int baseline, bool & cleared) const
1933 int h = insetAscent + insetDescent;
1934 int ty = baseline - insetAscent;
1940 if ((ty + h) > pain.paperHeight())
1941 h = pain.paperHeight();
1942 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
1943 w = pain.paperWidth();
1944 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
1950 Paragraph * InsetText::getParFromID(int id) const
1952 Paragraph * result = par;
1953 Paragraph * ires = 0;
1954 while (result && result->id() != id) {
1955 if ((ires = result->getParFromID(id)))
1957 result = result->next();
1963 Paragraph * InsetText::firstParagraph() const
1966 if (the_locking_inset)
1967 if ((result = the_locking_inset->firstParagraph()))
1973 LyXCursor const & InsetText::cursor(BufferView * bv) const
1975 if (the_locking_inset)
1976 return the_locking_inset->cursor(bv);
1977 return getLyXText(bv)->cursor;
1981 Paragraph * InsetText::paragraph() const
1987 void InsetText::paragraph(Paragraph * p)
1991 // we now have to update/redraw all instances
1992 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
1997 // redraw myself when asked for
1998 need_update |= INIT;
2002 Inset * InsetText::getInsetFromID(int id_arg) const
2005 return const_cast<InsetText *>(this);
2007 Paragraph * lp = par;
2010 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2011 en = lp->inset_iterator_end();
2014 if ((*it)->id() == id_arg)
2016 Inset * in = (*it)->getInsetFromID(id_arg);
2026 string InsetText::selectNextWord(BufferView * bv, float & value) const
2032 lt = getLyXText(bv);
2035 if (the_locking_inset) {
2036 str = the_locking_inset->selectNextWord(bv, value);
2043 #warning Dekel please have a look on this one RTL? (Jug)
2044 // we have to go on checking so move cusor to the right
2045 lt->cursor.pos(lt->cursor.pos() + 1);
2047 str = lt->selectNextWord(bv, value);
2049 bv->unlockInset(const_cast<InsetText *>(this));
2058 void InsetText::selectSelectedWord(BufferView * bv)
2060 if (the_locking_inset) {
2061 the_locking_inset->selectSelectedWord(bv);
2064 getLyXText(bv)->selectSelectedWord(bv);
2065 updateLocal(bv, SELECTION, false);
2069 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2071 if (the_locking_inset) {
2072 the_locking_inset->toggleSelection(bv, kill_selection);
2076 lt = getLyXText(bv);
2080 int x = top_x + TEXT_TO_INSET_OFFSET;
2083 Row * row = lt->getRowNearY(y);
2084 int y_offset = top_baseline - row->ascent_of_text();
2086 while ((row != 0) && ((y+row->height()) <= 0)) {
2093 if (need_update & SELECTION)
2095 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2101 bool InsetText::searchForward(BufferView * bv, string const & str,
2102 bool const & cs, bool const & mw)
2104 if (the_locking_inset)
2105 if (the_locking_inset->searchForward(bv, str, cs, mw))
2107 if (LyXFind(bv, str, true, true, cs , mw)) {
2110 // we have to unlock ourself in this function by default!
2111 bv->unlockInset(const_cast<InsetText *>(this));
2115 bool InsetText::searchBackward(BufferView * bv, string const & str,
2116 bool const & cs, bool const & mw)
2118 if (the_locking_inset)
2119 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2121 if (LyXFind(bv, str, false, true, cs, mw)) {
2124 // we have to unlock ourself in this function by default!
2125 bv->unlockInset(const_cast<InsetText *>(this));