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"
61 extern unsigned char getCurrentTextClass(Buffer *);
62 extern bool math_insert_greek(BufferView *, char);
63 extern int greek_kb_flag;
66 #warning this functions should probably go into bufferview_funcs somehow (Jug)
68 void InsetText::saveLyXTextState(LyXText * t) const
70 sstate.lpar = t->cursor.par();
71 sstate.pos = t->cursor.pos();
72 sstate.boundary = t->cursor.boundary();
73 sstate.selstartpar = t->selection.start.par();
74 sstate.selstartpos = t->selection.start.pos();
75 sstate.selstartboundary = t->selection.start.boundary();
76 sstate.selendpar = t->selection.end.par();
77 sstate.selendpos = t->selection.end.pos();
78 sstate.selendboundary = t->selection.end.boundary();
79 sstate.selection = t->selection.set();
80 sstate.mark_set = t->selection.mark();
83 void InsetText::restoreLyXTextState(BufferView * bv, LyXText * t) const
86 t->selection.set(true);
87 /* at this point just to avoid the Delete-Empty-Paragraph
88 * Mechanism when setting the cursor */
89 t->selection.mark(sstate.mark_set);
90 if (sstate.selection) {
91 t->setCursor(bv, sstate.selstartpar, sstate.selstartpos,
92 true, sstate.selstartboundary);
93 t->selection.cursor = t->cursor;
94 t->setCursor(bv, sstate.selendpar, sstate.selendpos,
95 true, sstate.selendboundary);
97 t->setCursor(bv, sstate.lpar, sstate.pos);
99 t->setCursor(bv, sstate.lpar, sstate.pos, true, sstate.boundary);
100 t->selection.cursor = t->cursor;
101 t->selection.set(false);
107 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
114 InsetText::InsetText()
121 InsetText::InsetText(InsetText const & ins, bool same_id)
126 autoBreakRows = ins.autoBreakRows;
130 InsetText & InsetText::operator=(InsetText const & it)
133 autoBreakRows = it.autoBreakRows;
138 void InsetText::init(InsetText const * ins, bool same_id)
146 the_locking_inset = 0;
148 no_selection = false;
152 autoBreakRows = false;
156 setParagraphData(ins->par);
157 autoBreakRows = ins->autoBreakRows;
158 drawFrame_ = ins->drawFrame_;
162 par->setInsetOwner(this);
163 frame_color = LColor::insetframe;
166 last_drawn_width = -1;
167 frame_is_visible = false;
174 InsetText::~InsetText()
181 Paragraph * tmp = par->next();
188 void InsetText::clear()
192 // now also delete all caches this should be safe, hopefully
196 Paragraph * tmp = par->next();
204 Inset * InsetText::clone(Buffer const &, bool same_id) const
206 InsetText * t = new InsetText(*this, same_id);
211 void InsetText::write(Buffer const * buf, ostream & os) const
214 writeParagraphData(buf, os);
218 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
220 par->writeFile(buf, os, buf->params, 0);
224 void InsetText::read(Buffer const * buf, LyXLex & lex)
228 Paragraph * return_par = 0;
229 Paragraph::depth_type depth = 0;
230 LyXFont font(LyXFont::ALL_INHERIT);
236 token = lex.GetString();
239 if (token == "\\end_inset")
241 if (const_cast<Buffer*>(buf)->
242 parseSingleLyXformat2Token(lex, par, return_par,
243 token, pos, depth, font)) {
244 // the_end read this should NEVER happen
245 lex.printError("\\the_end read in inset! Error in document!");
253 return_par->setInsetOwner(this);
254 return_par = return_par->next();
257 if (token != "\\end_inset") {
258 lex.printError("Missing \\end_inset at this point. "
265 int InsetText::ascent(BufferView * bv, LyXFont const &) const
268 Row * row = getLyXText(bv)->getRowNearY(y_temp);
269 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
274 int InsetText::descent(BufferView * bv, LyXFont const &) const
282 Row * row = lt->getRowNearY(y_temp);
283 insetDescent = lt->height - row->ascent_of_text() +
284 TEXT_TO_INSET_OFFSET;
291 int InsetText::width(BufferView * bv, LyXFont const &) const
293 insetWidth = max(textWidth(bv),
294 (int)getLyXText(bv)->width + (2 * TEXT_TO_INSET_OFFSET));
299 int InsetText::textWidth(BufferView * bv) const
301 int const w = getMaxWidth(bv, this);
306 void InsetText::draw(BufferView * bv, LyXFont const & f,
307 int baseline, float & x, bool cleared) const
312 Painter & pain = bv->painter();
314 // no draw is necessary !!!
315 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
317 top_baseline = baseline;
319 if (!cleared && (need_update & CLEAR_FRAME))
320 clearFrame(pain, cleared);
322 frame_is_visible = false;
329 UpdatableInset::draw(bv, f, baseline, x, cleared);
332 x += static_cast<float>(scroll());
335 // update insetWidth and insetHeight with dummy calls
337 (void)descent(bv, f);
341 // if top_x differs we have a rule down and we don't have to clear anything
342 if (!cleared && (top_x == int(x)) &&
343 ((need_update&(INIT|FULL)) || (top_baseline!=baseline) ||
344 (last_drawn_width!=insetWidth))) {
345 clearInset(pain, baseline, cleared);
348 frame_is_visible = false;
350 if (!cleared && (need_update == NONE))
353 if (top_x != int(x)) {
354 if ((getMaxWidth(bv, this) > 0) &&
355 (getLyXText(bv)->width != old_max_width)) {
358 old_max_width = getLyXText(bv)->width;
359 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
362 clearInset(pain, baseline, cleared);
365 // lyxerr << "InsetText::draw[" << this << "](" << need_update << ":" << int(x) << ":" << top_x << ")\n";
367 if (cleared || (last_drawn_width != insetWidth)) {
369 last_drawn_width = insetWidth;
372 top_baseline = baseline;
373 top_y = baseline - ascent(bv, f);
374 last_width = width(bv, f);
375 last_height = ascent(bv, f) + descent(bv, f);
377 if (the_locking_inset && (cpar(bv) == inset_par)
378 && (cpos(bv) == inset_pos)) {
379 inset_x = cx(bv) - top_x + drawTextXOffset;
380 inset_y = cy(bv) + drawTextYOffset;
382 if (!cleared && (need_update == CURSOR)
383 && !getLyXText(bv)->selection.set()) {
384 drawFrame(pain, cleared);
385 x += last_width; // was width(bv, f);
394 x += TEXT_TO_INSET_OFFSET;
397 Row * row = lt->getRowNearY(y);
398 int y_offset = baseline - row->ascent_of_text();
399 int ph = pain.paperHeight();
402 while ((row != 0) && ((y+row->height()) <= 0)) {
404 first += row->height();
413 while ((row != 0) && (yf < ph)) {
414 lt->getVisibleRow(bv, y+y_offset, int(x), row,
420 } else if (!locked) {
421 if (need_update & CURSOR) {
422 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
423 lt->clearSelection(bv);
424 lt->selection.cursor = lt->cursor;
426 bv->screen()->update(lt, bv, y_offset, int(x));
429 if (need_update & SELECTION)
430 bv->screen()->toggleToggle(lt, bv, y_offset, int(x));
431 else if (need_update & CURSOR) {
432 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
433 lt->clearSelection(bv);
434 lt->selection.cursor = lt->cursor;
436 bv->screen()->update(lt, bv, y_offset, int(x));
441 lt->status(bv, LyXText::UNCHANGED);
442 if ((need_update != CURSOR_PAR) &&
443 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked)))
444 drawFrame(pain, cleared);
445 else if (need_update & CLEAR_FRAME)
446 clearFrame(pain, cleared);
447 x += last_width /* was width(bv, f) */ - TEXT_TO_INSET_OFFSET;
448 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
450 } else if (need_update != INIT)
457 void InsetText::drawFrame(Painter & pain, bool cleared) const
459 if (!frame_is_visible || cleared) {
460 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
461 insetWidth - 1, insetAscent + insetDescent - 1,
463 frame_is_visible = true;
468 void InsetText::clearFrame(Painter & pain, bool cleared) const
470 if (frame_is_visible) {
472 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
473 insetWidth - 1, insetAscent + insetDescent - 1,
476 frame_is_visible = false;
481 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
487 owner()->update(bv, font, true);
490 if (the_locking_inset) {
491 inset_x = cx(bv) - top_x + drawTextXOffset;
492 inset_y = cy(bv) + drawTextYOffset;
493 the_locking_inset->update(bv, font, reinit);
501 int oldw = insetWidth;
502 insetWidth = lt->width + (2 * TEXT_TO_INSET_OFFSET);
503 if (oldw != insetWidth) {
510 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
513 lt->updateInset(bv, the_locking_inset);
515 if (lt->status() == LyXText::NEED_MORE_REFRESH)
522 void InsetText::setUpdateStatus(BufferView * bv, int what) const
530 if (lt->status() == LyXText::NEED_MORE_REFRESH)
532 else if (lt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)
533 need_update |= CURSOR_PAR;
535 // this to not draw a selection when we redraw all of it!
536 if ((need_update & (INIT|FULL)) && (need_update & CURSOR))
537 lt->clearSelection(bv);
543 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
551 setUpdateStatus(bv, what);
552 if ((need_update != CURSOR) || (lt->status() != LyXText::UNCHANGED) ||
555 bv->updateInset(this, mark_dirty);
557 bv->owner()->showState();
558 if (old_par != cpar(bv)) {
559 bv->owner()->setLayout(cpar(bv)->getLayout());
567 string const InsetText::editMessage() const
569 return _("Opened Text Inset");
573 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
575 // par->SetInsetOwner(this);
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->selection.cursor = lt->cursor;
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::insetUnlock(BufferView * bv)
618 if (the_locking_inset) {
619 the_locking_inset->insetUnlock(bv);
620 the_locking_inset = 0;
623 no_selection = false;
625 int code = CURSOR|CLEAR_FRAME;
631 if (lt->selection.set()) {
632 lt->clearSelection(bv);
634 } else if (owner()) {
635 bv->owner()->setLayout(owner()->getLyXText(bv)
636 ->cursor.par()->getLayout());
638 bv->owner()->setLayout(bv->text->cursor.par()->getLayout());
639 updateLocal(bv, code, false);
645 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
647 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
651 if (inset == cpar(bv)->getInset(cpos(bv))) {
652 lyxerr[Debug::INSETS] << "OK" << endl;
653 the_locking_inset = inset;
654 inset_x = cx(bv) - top_x + drawTextXOffset;
655 inset_y = cy(bv) + drawTextYOffset;
656 inset_pos = cpos(bv);
657 inset_par = cpar(bv);
658 inset_boundary = cboundary(bv);
659 updateLocal(bv, CURSOR, false);
661 } else if (the_locking_inset && (the_locking_inset == inset)) {
662 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
663 lyxerr[Debug::INSETS] << "OK" << endl;
664 inset_x = cx(bv) - top_x + drawTextXOffset;
665 inset_y = cy(bv) + drawTextYOffset;
667 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
669 } else if (the_locking_inset) {
670 lyxerr[Debug::INSETS] << "MAYBE" << endl;
671 return the_locking_inset->lockInsetInInset(bv, inset);
673 lyxerr[Debug::INSETS] << "NOT OK" << endl;
678 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
681 if (!the_locking_inset)
683 if (the_locking_inset == inset) {
684 the_locking_inset->insetUnlock(bv);
685 getLyXText(bv)->updateInset(bv, inset);
686 the_locking_inset = 0;
688 moveRight(bv, false);
689 old_par = 0; // force layout setting
693 updateLocal(bv, CURSOR, false);
696 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
700 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
702 if (!the_locking_inset)
704 if (the_locking_inset != inset) {
705 getLyXText(bv)->updateInset(bv, the_locking_inset);
706 setUpdateStatus(bv, CURSOR_PAR);
707 return the_locking_inset->updateInsetInInset(bv, inset);
709 // updateLocal(bv, FULL, false);
710 if (getLyXText(bv)->updateInset(bv, inset))
711 updateLocal(bv, CURSOR_PAR, false);
712 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
713 inset_x = cx(bv) - top_x + drawTextXOffset;
714 inset_y = cy(bv) + drawTextYOffset;
720 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
724 int tmp_x = x - drawTextXOffset;
725 int tmp_y = y + insetAscent - getLyXText(bv)->first;
726 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y, button);
729 if (the_locking_inset) {
730 if (the_locking_inset == inset) {
731 the_locking_inset->insetButtonPress(bv,x-inset_x,y-inset_y,button);
732 no_selection = false;
735 // otherwise unlock the_locking_inset and lock the new inset
736 the_locking_inset->insetUnlock(bv);
737 inset_x = cx(bv) - top_x + drawTextXOffset;
738 inset_y = cy(bv) + drawTextYOffset;
739 the_locking_inset = static_cast<UpdatableInset*>(inset);
740 inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
741 inset->edit(bv, x - inset_x, y - inset_y, button);
742 if (the_locking_inset)
743 updateLocal(bv, CURSOR, false);
744 no_selection = false;
747 // otherwise only unlock the_locking_inset
748 the_locking_inset->insetUnlock(bv);
749 the_locking_inset = 0;
751 if (bv->theLockingInset()) {
752 if (inset && inset->editable() == Inset::HIGHLY_EDITABLE) {
753 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
754 inset_x = cx(bv) - top_x + drawTextXOffset;
755 inset_y = cy(bv) + drawTextYOffset;
756 inset_pos = cpos(bv);
757 inset_par = cpar(bv);
758 inset_boundary = cboundary(bv);
759 the_locking_inset = uinset;
760 uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
762 uinset->edit(bv, x - inset_x, y - inset_y, 0);
763 if (the_locking_inset)
764 updateLocal(bv, CURSOR, false);
765 no_selection = false;
769 if (!inset) { // && (button == 2)) {
770 bool paste_internally = false;
771 if ((button == 2) && getLyXText(bv)->selection.set()) {
772 localDispatch(bv, LFUN_COPY, "");
773 paste_internally = true;
775 getLyXText(bv)->setCursorFromCoordinates(bv, x-drawTextXOffset,
777 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
778 updateLocal(bv, CURSOR, false);
779 bv->owner()->setLayout(cpar(bv)->getLayout());
781 // Insert primary selection with middle mouse
782 // if there is a local selection in the current buffer,
785 if (paste_internally)
786 localDispatch(bv, LFUN_PASTE, "");
788 localDispatch(bv, LFUN_PASTESELECTION,
793 no_selection = false;
797 void InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
799 UpdatableInset * inset = 0;
801 if (the_locking_inset) {
802 the_locking_inset->insetButtonRelease(bv,
803 x - inset_x, y - inset_y,
806 if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
807 inset = static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
808 if (inset->editable() == Inset::HIGHLY_EDITABLE) {
809 inset->insetButtonRelease(bv,
811 y - inset_y, button);
813 inset_x = cx(bv) - top_x + drawTextXOffset;
814 inset_y = cy(bv) + drawTextYOffset;
815 inset->insetButtonRelease(bv,
817 y - inset_y, button);
819 x - inset_x, y - inset_y, button);
821 updateLocal(bv, CURSOR_PAR, false);
824 no_selection = false;
828 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
832 if (the_locking_inset) {
833 the_locking_inset->insetMotionNotify(bv, x - inset_x,
837 LyXText * t = getLyXText(bv);
839 t->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
841 if (t->toggle_cursor.par() != t->toggle_end_cursor.par() ||
842 t->toggle_cursor.pos() != t->toggle_end_cursor.pos())
843 updateLocal(bv, SELECTION, false);
848 void InsetText::insetKeyPress(XKeyEvent * xke)
850 if (the_locking_inset) {
851 the_locking_inset->insetKeyPress(xke);
857 UpdatableInset::RESULT
858 InsetText::localDispatch(BufferView * bv,
859 kb_action action, string const & arg)
861 no_selection = false;
862 UpdatableInset::RESULT
863 result= UpdatableInset::localDispatch(bv, action, arg);
864 if (result != UNDISPATCHED) {
869 if ((action < 0) && arg.empty())
872 if (the_locking_inset) {
873 result = the_locking_inset->localDispatch(bv, action, arg);
874 if (result == DISPATCHED_NOUPDATE)
876 else if (result == DISPATCHED) {
877 updateLocal(bv, CURSOR_PAR, false);
879 } else if (result == FINISHED) {
880 bool dispatched = false;
882 case LFUN_UNKNOWN_ACTION:
883 case LFUN_BREAKPARAGRAPH:
885 moveRightIntern(bv, false, false);
888 if (!getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
889 moveRightIntern(bv, false, false);
893 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
894 moveRightIntern(bv, false, false);
900 the_locking_inset = 0;
913 case LFUN_SELFINSERT:
914 if (bv->buffer()->isReadonly()) {
915 // setErrorMessage(N_("Document is read only"));
919 /* Automatically delete the currently selected
920 * text and replace it with what is being
921 * typed in now. Depends on lyxrc settings
922 * "auto_region_delete", which defaults to
925 setUndo(bv, Undo::INSERT,
926 lt->cursor.par(), lt->cursor.par()->next());
928 if (lyxrc.auto_region_delete) {
929 if (lt->selection.set()) {
930 lt->cutSelection(bv, false);
933 lt->clearSelection(bv);
934 for (string::size_type i = 0; i < arg.length(); ++i) {
936 if (!math_insert_greek(bv, arg[i])) {
937 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
938 } else if (!the_locking_inset) {
939 (void)moveRight(bv, false);
942 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
946 lt->selection.cursor = lt->cursor;
947 updateLocal(bv, CURSOR_PAR, true);
948 result = DISPATCHED_NOUPDATE;
950 // --- Cursor Movements -----------------------------------
953 moveRight(bv, false, true);
954 lt->setSelection(bv);
955 updateLocal(bv, SELECTION, false);
958 result = moveRight(bv);
960 updateLocal(bv, CURSOR, false);
964 moveLeft(bv, false, true);
965 lt->setSelection(bv);
966 updateLocal(bv, SELECTION, false);
970 result = moveLeft(bv);
971 updateLocal(bv, CURSOR, false);
976 lt->setSelection(bv);
977 updateLocal(bv, SELECTION, false);
981 result = moveDown(bv);
982 updateLocal(bv, CURSOR, false);
987 lt->setSelection(bv);
988 updateLocal(bv, SELECTION, false);
993 updateLocal(bv, CURSOR, false);
998 updateLocal(bv, CURSOR, false);
1002 updateLocal(bv, CURSOR, false);
1004 case LFUN_BACKSPACE: {
1005 setUndo(bv, Undo::DELETE,
1006 lt->cursor.par(), lt->cursor.par()->next());
1007 if (lt->selection.set())
1008 lt->cutSelection(bv);
1011 updateLocal(bv, CURSOR_PAR, true);
1016 setUndo(bv, Undo::DELETE,
1017 lt->cursor.par(), lt->cursor.par()->next());
1018 if (lt->selection.set()) {
1019 lt->cutSelection(bv);
1023 updateLocal(bv, CURSOR_PAR, true);
1028 setUndo(bv, Undo::DELETE,
1029 lt->cursor.par(), lt->cursor.par()->next());
1030 lt->cutSelection(bv);
1031 updateLocal(bv, CURSOR_PAR, true);
1037 lt->copySelection(bv);
1038 updateLocal(bv, CURSOR_PAR, false);
1040 case LFUN_PASTESELECTION:
1042 string const clip(bv->getClipboard());
1046 if (arg == "paragraph") {
1047 lt->insertStringAsParagraphs(bv, clip);
1049 lt->insertStringAsLines(bv, clip);
1051 updateLocal(bv, CURSOR_PAR, true);
1055 if (!autoBreakRows) {
1057 if (CutAndPaste::nrOfParagraphs() > 1) {
1058 WriteAlert(_("Impossible operation"),
1059 _("Cannot include more than one paragraph!"),
1064 setUndo(bv, Undo::INSERT,
1065 lt->cursor.par(), lt->cursor.par()->next());
1066 lt->pasteSelection(bv);
1067 updateLocal(bv, CURSOR_PAR, true);
1071 case LFUN_BREAKPARAGRAPH:
1072 if (!autoBreakRows) {
1073 result = DISPATCHED;
1076 lt->breakParagraph(bv, 0);
1077 updateLocal(bv, FULL, true);
1079 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1080 if (!autoBreakRows) {
1081 result = DISPATCHED;
1084 lt->breakParagraph(bv, 1);
1085 updateLocal(bv, FULL, true);
1088 case LFUN_BREAKLINE: {
1089 if (!autoBreakRows) {
1090 result = DISPATCHED;
1093 setUndo(bv, Undo::INSERT,
1094 lt->cursor.par(), lt->cursor.par()->next());
1095 lt->insertChar(bv, Paragraph::META_NEWLINE);
1096 updateLocal(bv, CURSOR_PAR, true);
1101 // do not set layouts on non breakable textinsets
1102 if (autoBreakRows) {
1103 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1105 // Derive layout number from given argument (string)
1106 // and current buffer's textclass (number). */
1107 LyXTextClassList::ClassList::size_type tclass =
1108 bv->buffer()->params.textclass;
1109 std::pair <bool, LyXTextClass::size_type> layout =
1110 textclasslist.NumberOfLayout(tclass, arg);
1112 // If the entry is obsolete, use the new one instead.
1114 string obs = textclasslist.Style(tclass,layout.second).
1117 layout = textclasslist.NumberOfLayout(tclass, obs);
1120 // see if we found the layout number:
1121 if (!layout.first) {
1122 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1123 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1127 if (cur_layout != layout.second) {
1128 cur_layout = layout.second;
1129 lt->setLayout(bv, layout.second);
1130 bv->owner()->setLayout(cpar(bv)->getLayout());
1131 updateLocal(bv, CURSOR_PAR, true);
1134 // reset the layout box
1135 bv->owner()->setLayout(cpar(bv)->getLayout());
1138 case LFUN_PARAGRAPH_SPACING:
1139 // This one is absolutely not working. When fiddling with this
1140 // it also seems to me that the paragraphs inside the insettext
1141 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1143 Paragraph * par = lt->cursor.par();
1144 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1145 float cur_value = 1.0;
1146 if (cur_spacing == Spacing::Other) {
1147 cur_value = par->params().spacing().getValue();
1150 istringstream istr(arg.c_str());
1153 Spacing::Space new_spacing = cur_spacing;
1154 float new_value = cur_value;
1156 lyxerr << "Missing argument to `paragraph-spacing'"
1158 } else if (tmp == "single") {
1159 new_spacing = Spacing::Single;
1160 } else if (tmp == "onehalf") {
1161 new_spacing = Spacing::Onehalf;
1162 } else if (tmp == "double") {
1163 new_spacing = Spacing::Double;
1164 } else if (tmp == "other") {
1165 new_spacing = Spacing::Other;
1168 lyxerr << "new_value = " << tmpval << endl;
1171 } else if (tmp == "default") {
1172 new_spacing = Spacing::Default;
1174 lyxerr << _("Unknown spacing argument: ")
1177 if (cur_spacing != new_spacing || cur_value != new_value) {
1178 par->params().spacing(Spacing(new_spacing, new_value));
1179 updateLocal(bv, CURSOR_PAR, true);
1185 if (!bv->Dispatch(action, arg))
1186 result = UNDISPATCHED;
1190 /// If the action has deleted all text in the inset, we need to change the
1191 // language to the language of the surronding text.
1192 if (par->size() == 0 && !par->next()) {
1193 LyXFont font(LyXFont::ALL_IGNORE);
1194 font.setLanguage(bv->getParentLanguage(this));
1195 setFont(bv, font, false);
1198 if (result != FINISHED) {
1199 showInsetCursor(bv);
1201 bv->unlockInset(this);
1208 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1211 buf->latexParagraphs(os, par, 0, texrow);
1212 return texrow.rows();
1216 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1218 Paragraph * p = par;
1219 unsigned int lines = 0;
1222 string const tmp = buf->asciiParagraph(p, linelen);
1223 lines += countChar(tmp, '\n');
1231 int InsetText::docBook(Buffer const * buf, ostream & os) const
1233 Paragraph * p = par;
1234 unsigned int lines = 0;
1239 buf->simpleDocBookOnePar(os, tmp, p, desc, 0);
1247 void InsetText::validate(LaTeXFeatures & features) const
1249 Paragraph * p = par;
1251 p->validate(features);
1257 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1259 if (textclasslist.Style(buf->params.textclass,
1260 p->getLayout()).labeltype != LABEL_MANUAL)
1263 return p->beginningOfMainBody();
1267 void InsetText::getCursorPos(BufferView * bv,
1268 int & x, int & y) const
1275 unsigned int InsetText::insetInInsetY()
1277 if (!the_locking_inset)
1280 return (inset_y + the_locking_inset->insetInInsetY());
1284 void InsetText::toggleInsetCursor(BufferView * bv)
1286 if (the_locking_inset) {
1287 the_locking_inset->toggleInsetCursor(bv);
1291 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1293 int const asc = lyxfont::maxAscent(font);
1294 int const desc = lyxfont::maxDescent(font);
1296 if (isCursorVisible())
1297 bv->hideLockedInsetCursor();
1299 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1300 toggleCursorVisible();
1304 void InsetText::showInsetCursor(BufferView * bv, bool show)
1306 if (the_locking_inset) {
1307 the_locking_inset->showInsetCursor(bv);
1310 if (!isCursorVisible()) {
1311 LyXFont const font =
1312 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1314 int const asc = lyxfont::maxAscent(font);
1315 int const desc = lyxfont::maxDescent(font);
1317 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1319 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1320 setCursorVisible(true);
1325 void InsetText::hideInsetCursor(BufferView * bv)
1327 if (isCursorVisible()) {
1328 bv->hideLockedInsetCursor();
1329 setCursorVisible(false);
1331 if (the_locking_inset)
1332 the_locking_inset->hideInsetCursor(bv);
1336 UpdatableInset::RESULT
1337 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1339 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1340 return moveLeftIntern(bv, false, activate_inset, selecting);
1342 return moveRightIntern(bv, false, activate_inset, selecting);
1346 UpdatableInset::RESULT
1347 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1349 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1350 return moveRightIntern(bv, true, activate_inset, selecting);
1352 return moveLeftIntern(bv, true, activate_inset, selecting);
1356 UpdatableInset::RESULT
1357 InsetText::moveRightIntern(BufferView * bv, bool behind,
1358 bool activate_inset, bool selecting)
1360 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1362 if (activate_inset && checkAndActivateInset(bv, behind))
1364 getLyXText(bv)->cursorRight(bv);
1366 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1367 return DISPATCHED_NOUPDATE;
1371 UpdatableInset::RESULT
1372 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1373 bool activate_inset, bool selecting)
1375 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1377 getLyXText(bv)->cursorLeft(bv);
1379 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1380 if (activate_inset && checkAndActivateInset(bv, behind))
1382 return DISPATCHED_NOUPDATE;
1386 UpdatableInset::RESULT
1387 InsetText::moveUp(BufferView * bv)
1389 if (!crow(bv)->previous())
1391 getLyXText(bv)->cursorUp(bv);
1392 return DISPATCHED_NOUPDATE;
1396 UpdatableInset::RESULT
1397 InsetText::moveDown(BufferView * bv)
1399 if (!crow(bv)->next())
1401 getLyXText(bv)->cursorDown(bv);
1402 return DISPATCHED_NOUPDATE;
1406 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1408 if (the_locking_inset) {
1409 if (the_locking_inset->insertInsetAllowed(inset))
1410 return the_locking_inset->insertInset(bv, inset);
1413 inset->setOwner(this);
1414 hideInsetCursor(bv);
1418 lt = getLyXText(bv);
1421 lt->insertInset(bv, inset);
1422 if ((cpar(bv)->getChar(cpos(bv)) != Paragraph::META_INSET) ||
1423 (cpar(bv)->getInset(cpos(bv)) != inset))
1426 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1427 showInsetCursor(bv);
1434 bool InsetText::insertInsetAllowed(Inset * in) const
1436 if (the_locking_inset)
1437 return the_locking_inset->insertInsetAllowed(in);
1441 bool InsetText::insertInsetAllowed(Inset::Code code) const
1443 if (the_locking_inset)
1444 return the_locking_inset->insertInsetAllowed(code);
1449 UpdatableInset * InsetText::getLockingInset() const
1451 return the_locking_inset ? the_locking_inset->getLockingInset() :
1452 const_cast<InsetText *>(this);
1456 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1460 if (the_locking_inset)
1461 return the_locking_inset->getFirstLockingInsetOfType(c);
1466 bool InsetText::showInsetDialog(BufferView * bv) const
1468 if (the_locking_inset)
1469 return the_locking_inset->showInsetDialog(bv);
1474 std::vector<string> const InsetText::getLabelList() const
1476 std::vector<string> label_list;
1478 Paragraph * tpar = par;
1480 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1481 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1482 for (; beg != end; ++beg) {
1483 std::vector<string> const l = (*beg)->getLabelList();
1484 label_list.insert(label_list.end(), l.begin(), l.end());
1486 tpar = tpar->next();
1492 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1495 if (the_locking_inset) {
1496 the_locking_inset->setFont(bv, font, toggleall, selectall);
1501 lt = getLyXText(bv);
1504 if (lt->selection.set()) {
1505 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1510 lt->toggleFree(bv, font, toggleall);
1512 lt->setFont(bv, font, toggleall);
1515 lt->clearSelection(bv);
1517 if (selectall || lt->selection.set())
1518 updateLocal(bv, FULL, true);
1520 updateLocal(bv, CURSOR_PAR, true);
1526 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1528 if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
1532 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1533 if (!inset || inset->editable() != Inset::HIGHLY_EDITABLE)
1535 LyXFont const font =
1536 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1538 x = inset->width(bv, font);
1539 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1542 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1544 //inset_x = cx(bv) - top_x + drawTextXOffset;
1545 //inset_y = cy(bv) + drawTextYOffset;
1546 inset->edit(bv, x, y, 0);
1547 if (!the_locking_inset)
1549 updateLocal(bv, CURSOR, false);
1556 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1559 x -= drawTextXOffset;
1561 int dummyy = y + insetAscent;
1562 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy, button);
1569 inset_x = cx(bv) - top_x + drawTextXOffset;
1570 inset_y = cy(bv) + drawTextYOffset;
1571 inset->edit(bv, x - inset_x, y - inset_y, button);
1572 if (!the_locking_inset)
1574 updateLocal(bv, CURSOR, false);
1581 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1583 int w = UpdatableInset::getMaxWidth(bv, inset);
1588 w = w - top_x + owner()->x();
1591 w -= (2 * TEXT_TO_INSET_OFFSET);
1593 // return w - (2*TEXT_TO_INSET_OFFSET);
1597 void InsetText::setParagraphData(Paragraph * p)
1601 // now also delete all caches this should be safe, hopefully
1605 Paragraph * tmp = par->next();
1610 par = new Paragraph(*p);
1611 par->setInsetOwner(this);
1612 Paragraph * np = par;
1615 np->next(new Paragraph(*p));
1616 np->next()->previous(np);
1618 np->setInsetOwner(this);
1625 void InsetText::setText(string const & data)
1628 LyXFont font(LyXFont::ALL_SANE);
1629 for (unsigned int i=0; i < data.length(); ++i)
1630 par->insertChar(i, data[i], font);
1634 void InsetText::setAutoBreakRows(bool flag)
1636 if (flag != autoBreakRows) {
1637 autoBreakRows = flag;
1645 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1647 if (how != drawFrame_) {
1650 updateLocal(bv, DRAW_FRAME, false);
1655 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1657 if (frame_color != col) {
1660 updateLocal(bv, DRAW_FRAME, false);
1665 int InsetText::cx(BufferView * bv) const
1669 lt = getLyXText(bv);
1672 int x = lt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1673 if (the_locking_inset) {
1674 LyXFont font = lt->getFont(bv->buffer(),
1677 if (font.isVisibleRightToLeft())
1678 x -= the_locking_inset->width(bv, font);
1686 int InsetText::cy(BufferView * bv) const
1689 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1693 Paragraph::size_type InsetText::cpos(BufferView * bv) const
1695 return getLyXText(bv)->cursor.pos();
1699 Paragraph * InsetText::cpar(BufferView * bv) const
1701 return getLyXText(bv)->cursor.par();
1705 bool InsetText::cboundary(BufferView * bv) const
1707 return getLyXText(bv)->cursor.boundary();
1711 Row * InsetText::crow(BufferView * bv) const
1713 return getLyXText(bv)->cursor.row();
1717 LyXText * InsetText::getLyXText(BufferView const * lbv,
1718 bool const recursive) const
1720 if (!recursive && (cached_bview == lbv))
1721 return cached_text.get();
1723 // Super UGLY! (Lgb)
1724 BufferView * bv = const_cast<BufferView *>(lbv);
1727 Cache::iterator it = cache.find(bv);
1729 if (it != cache.end() && (lt || !it->second.remove)) {
1730 lyx::Assert(it->second.text.get());
1732 cached_text = it->second.text;
1733 if (recursive && the_locking_inset) {
1734 return the_locking_inset->getLyXText(bv);
1736 return cached_text.get();
1737 } else if (it->second.remove) {
1739 saveLyXTextState(it->second.text.get());
1747 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
1748 cached_text->init(bv);
1749 restoreLyXTextState(bv, cached_text.get());
1751 cache.insert(make_pair(bv, cached_text));
1753 if (the_locking_inset && recursive) {
1754 return the_locking_inset->getLyXText(bv);
1756 return cached_text.get();
1760 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1764 Cache::iterator it = cache.find(bv);
1766 if (it == cache.end()) {
1770 lyx::Assert(it->second.text.get());
1772 it->second.remove = true;
1774 /// then remove all LyXText in text-insets
1775 Paragraph * p = par;
1776 for (; p; p = p->next()) {
1777 p->deleteInsetsLyXText(bv);
1783 void InsetText::resizeLyXText(BufferView * bv, bool force) const
1785 if (!par->next() && !par->size()) // no data, resize not neccessary!
1787 // one endless line, resize normally not necessary
1788 if (!force && getMaxWidth(bv, this) < 0)
1791 Cache::iterator it = cache.find(bv);
1792 if (it == cache.end()) {
1795 lyx::Assert(it->second.text.get());
1797 deleteLyXText(bv, (the_locking_inset == 0) || force);
1800 LyXText * t = getLyXText(bv);
1801 t->first = bv->screen()->topCursorVisible(t);
1804 // this will scroll the screen such that the cursor becomes visible
1805 bv->updateScrollbar();
1806 if (the_locking_inset) {
1807 /// then resize all LyXText in text-insets
1808 inset_x = cx(bv) - top_x + drawTextXOffset;
1809 inset_y = cy(bv) + drawTextYOffset;
1810 for (Paragraph * p = par; p; p = p->next()) {
1811 p->resizeInsetsLyXText(bv);
1818 void InsetText::removeNewlines()
1820 for (Paragraph * p = par; p; p = p->next()) {
1821 for (int i = 0; i < p->size(); ++i) {
1822 if (p->getChar(i) == Paragraph::META_NEWLINE)
1829 bool InsetText::nodraw() const
1831 if (the_locking_inset)
1832 return the_locking_inset->nodraw();
1833 return UpdatableInset::nodraw();
1837 int InsetText::scroll(bool recursive) const
1839 int sx = UpdatableInset::scroll(false);
1841 if (recursive && the_locking_inset)
1842 sx += the_locking_inset->scroll(recursive);
1848 bool InsetText::doClearArea() const
1850 return !locked || (need_update & (FULL|INIT));
1854 void InsetText::selectAll(BufferView * bv)
1856 getLyXText(bv)->cursorTop(bv);
1857 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1858 getLyXText(bv)->cursorBottom(bv);
1859 getLyXText(bv)->setSelection(bv);
1863 void InsetText::clearSelection(BufferView * bv)
1865 getLyXText(bv)->clearSelection(bv);
1869 void InsetText::clearInset(Painter & pain, int baseline, bool & cleared) const
1872 int h = insetAscent + insetDescent;
1873 int ty = baseline - insetAscent;
1879 if ((ty + h) > pain.paperHeight())
1880 h = pain.paperHeight();
1881 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
1882 w = pain.paperWidth();
1883 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
1889 Paragraph * InsetText::getParFromID(int id) const
1891 Paragraph * result = par;
1892 Paragraph * ires = 0;
1893 while (result && result->id() != id) {
1894 if ((ires = result->getParFromID(id)))
1896 result = result->next();
1902 Paragraph * InsetText::firstParagraph() const
1905 if (the_locking_inset)
1906 if ((result = the_locking_inset->firstParagraph()))
1912 LyXCursor const & InsetText::cursor(BufferView * bv) const
1914 if (the_locking_inset)
1915 return the_locking_inset->cursor(bv);
1916 return getLyXText(bv)->cursor;
1920 Paragraph * InsetText::paragraph() const
1926 void InsetText::paragraph(Paragraph * p)
1930 // we now have to update/redraw all instances
1931 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
1936 // redraw myself when asked for
1937 need_update |= INIT;
1941 Inset * InsetText::getInsetFromID(int id_arg) const
1944 return const_cast<InsetText *>(this);
1946 Paragraph * lp = par;
1949 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
1950 en = lp->inset_iterator_end();
1953 if ((*it)->id() == id_arg)
1955 Inset * in = (*it)->getInsetFromID(id_arg);