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) {
508 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
511 lt->updateInset(bv, the_locking_inset);
513 if (lt->status() == LyXText::NEED_MORE_REFRESH)
520 void InsetText::setUpdateStatus(BufferView * bv, int what) const
528 if (lt->status() == LyXText::NEED_MORE_REFRESH)
530 else if (lt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)
531 need_update |= CURSOR_PAR;
533 // this to not draw a selection when we redraw all of it!
534 if ((need_update & (INIT|FULL)) && (need_update & CURSOR))
535 lt->clearSelection(bv);
541 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
549 setUpdateStatus(bv, what);
550 if ((need_update != CURSOR) || (lt->status() != LyXText::UNCHANGED) ||
553 bv->updateInset(this, mark_dirty);
555 bv->owner()->showState();
556 if (old_par != cpar(bv)) {
557 bv->owner()->setLayout(cpar(bv)->getLayout());
565 string const InsetText::editMessage() const
567 return _("Opened Text Inset");
571 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
573 // par->SetInsetOwner(this);
574 UpdatableInset::edit(bv, x, y, button);
576 if (!bv->lockInset(this)) {
577 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
581 the_locking_inset = 0;
582 inset_pos = inset_x = inset_y = 0;
583 inset_boundary = false;
586 int tmp_y = (y < 0) ? 0 : y;
593 if (!checkAndActivateInset(bv, x, tmp_y, button))
594 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
596 lt->selection.cursor = lt->cursor;
599 updateLocal(bv, CURSOR, false);
601 // If the inset is empty set the language of the current font to the
602 // language to the surronding text (if different).
603 if (par->size() == 0 && !par->next() &&
604 bv->getParentLanguage(this) != lt->current_font.language()) {
605 LyXFont font(LyXFont::ALL_IGNORE);
606 font.setLanguage(bv->getParentLanguage(this));
607 setFont(bv, font, false);
614 void InsetText::insetUnlock(BufferView * bv)
616 if (the_locking_inset) {
617 the_locking_inset->insetUnlock(bv);
618 the_locking_inset = 0;
621 no_selection = false;
623 int code = CURSOR|CLEAR_FRAME;
629 if (lt->selection.set()) {
630 lt->clearSelection(bv);
632 } else if (owner()) {
633 bv->owner()->setLayout(owner()->getLyXText(bv)
634 ->cursor.par()->getLayout());
636 bv->owner()->setLayout(bv->text->cursor.par()->getLayout());
637 updateLocal(bv, code, false);
643 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
645 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
649 if (inset == cpar(bv)->getInset(cpos(bv))) {
650 lyxerr[Debug::INSETS] << "OK" << endl;
651 the_locking_inset = inset;
652 inset_x = cx(bv) - top_x + drawTextXOffset;
653 inset_y = cy(bv) + drawTextYOffset;
654 inset_pos = cpos(bv);
655 inset_par = cpar(bv);
656 inset_boundary = cboundary(bv);
657 updateLocal(bv, CURSOR, false);
659 } else if (the_locking_inset && (the_locking_inset == inset)) {
660 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
661 lyxerr[Debug::INSETS] << "OK" << endl;
662 inset_x = cx(bv) - top_x + drawTextXOffset;
663 inset_y = cy(bv) + drawTextYOffset;
665 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
667 } else if (the_locking_inset) {
668 lyxerr[Debug::INSETS] << "MAYBE" << endl;
669 return the_locking_inset->lockInsetInInset(bv, inset);
671 lyxerr[Debug::INSETS] << "NOT OK" << endl;
676 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
679 if (!the_locking_inset)
681 if (the_locking_inset == inset) {
682 the_locking_inset->insetUnlock(bv);
683 getLyXText(bv)->updateInset(bv, inset);
684 the_locking_inset = 0;
686 moveRight(bv, false);
687 old_par = 0; // force layout setting
691 updateLocal(bv, CURSOR, false);
694 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
698 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
700 if (!the_locking_inset)
702 if (the_locking_inset != inset) {
703 getLyXText(bv)->updateInset(bv, the_locking_inset);
704 setUpdateStatus(bv, CURSOR_PAR);
705 return the_locking_inset->updateInsetInInset(bv, inset);
707 // updateLocal(bv, FULL, false);
708 if (getLyXText(bv)->updateInset(bv, inset))
709 updateLocal(bv, CURSOR_PAR, false);
710 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
711 inset_x = cx(bv) - top_x + drawTextXOffset;
712 inset_y = cy(bv) + drawTextYOffset;
718 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
722 int tmp_x = x - drawTextXOffset;
723 int tmp_y = y + insetAscent - getLyXText(bv)->first;
724 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y, button);
727 if (the_locking_inset) {
728 if (the_locking_inset == inset) {
729 the_locking_inset->insetButtonPress(bv,x-inset_x,y-inset_y,button);
730 no_selection = false;
733 // otherwise unlock the_locking_inset and lock the new inset
734 the_locking_inset->insetUnlock(bv);
735 inset_x = cx(bv) - top_x + drawTextXOffset;
736 inset_y = cy(bv) + drawTextYOffset;
737 the_locking_inset = static_cast<UpdatableInset*>(inset);
738 inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
739 inset->edit(bv, x - inset_x, y - inset_y, button);
740 if (the_locking_inset)
741 updateLocal(bv, CURSOR, false);
742 no_selection = false;
745 // otherwise only unlock the_locking_inset
746 the_locking_inset->insetUnlock(bv);
747 the_locking_inset = 0;
749 if (bv->theLockingInset()) {
750 if (inset && inset->editable() == Inset::HIGHLY_EDITABLE) {
751 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
752 inset_x = cx(bv) - top_x + drawTextXOffset;
753 inset_y = cy(bv) + drawTextYOffset;
754 inset_pos = cpos(bv);
755 inset_par = cpar(bv);
756 inset_boundary = cboundary(bv);
757 the_locking_inset = uinset;
758 uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
760 uinset->edit(bv, x - inset_x, y - inset_y, 0);
761 if (the_locking_inset)
762 updateLocal(bv, CURSOR, false);
763 no_selection = false;
767 if (!inset) { // && (button == 2)) {
768 bool paste_internally = false;
769 if ((button == 2) && getLyXText(bv)->selection.set()) {
770 localDispatch(bv, LFUN_COPY, "");
771 paste_internally = true;
773 getLyXText(bv)->setCursorFromCoordinates(bv, x-drawTextXOffset,
775 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
776 updateLocal(bv, CURSOR, false);
777 bv->owner()->setLayout(cpar(bv)->getLayout());
779 // Insert primary selection with middle mouse
780 // if there is a local selection in the current buffer,
783 if (paste_internally)
784 localDispatch(bv, LFUN_PASTE, "");
786 localDispatch(bv, LFUN_PASTESELECTION,
791 no_selection = false;
795 void InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
797 UpdatableInset * inset = 0;
799 if (the_locking_inset) {
800 the_locking_inset->insetButtonRelease(bv,
801 x - inset_x, y - inset_y,
804 if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
805 inset = static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
806 if (inset->editable() == Inset::HIGHLY_EDITABLE) {
807 inset->insetButtonRelease(bv,
809 y - inset_y, button);
811 inset_x = cx(bv) - top_x + drawTextXOffset;
812 inset_y = cy(bv) + drawTextYOffset;
813 inset->insetButtonRelease(bv,
815 y - inset_y, button);
817 x - inset_x, y - inset_y, button);
819 updateLocal(bv, CURSOR_PAR, false);
822 no_selection = false;
826 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
830 if (the_locking_inset) {
831 the_locking_inset->insetMotionNotify(bv, x - inset_x,
835 LyXText * t = getLyXText(bv);
837 t->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
839 if (t->toggle_cursor.par() != t->toggle_end_cursor.par() ||
840 t->toggle_cursor.pos() != t->toggle_end_cursor.pos())
841 updateLocal(bv, SELECTION, false);
846 void InsetText::insetKeyPress(XKeyEvent * xke)
848 if (the_locking_inset) {
849 the_locking_inset->insetKeyPress(xke);
855 UpdatableInset::RESULT
856 InsetText::localDispatch(BufferView * bv,
857 kb_action action, string const & arg)
859 no_selection = false;
860 UpdatableInset::RESULT
861 result= UpdatableInset::localDispatch(bv, action, arg);
862 if (result != UNDISPATCHED) {
867 if ((action < 0) && arg.empty())
870 if (the_locking_inset) {
871 result = the_locking_inset->localDispatch(bv, action, arg);
872 if (result == DISPATCHED_NOUPDATE)
874 else if (result == DISPATCHED) {
875 updateLocal(bv, CURSOR_PAR, false);
877 } else if (result == FINISHED) {
878 bool dispatched = false;
880 case LFUN_UNKNOWN_ACTION:
881 case LFUN_BREAKPARAGRAPH:
883 moveRightIntern(bv, false, false);
886 if (!getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
887 moveRightIntern(bv, false, false);
891 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
892 moveRightIntern(bv, false, false);
898 the_locking_inset = 0;
911 case LFUN_SELFINSERT:
912 if (bv->buffer()->isReadonly()) {
913 // setErrorMessage(N_("Document is read only"));
917 /* Automatically delete the currently selected
918 * text and replace it with what is being
919 * typed in now. Depends on lyxrc settings
920 * "auto_region_delete", which defaults to
923 setUndo(bv, Undo::INSERT,
924 lt->cursor.par(), lt->cursor.par()->next());
926 if (lyxrc.auto_region_delete) {
927 if (lt->selection.set()) {
928 lt->cutSelection(bv, false);
931 lt->clearSelection(bv);
932 for (string::size_type i = 0; i < arg.length(); ++i) {
934 if (!math_insert_greek(bv, arg[i])) {
935 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
936 } else if (!the_locking_inset) {
937 (void)moveRight(bv, false);
940 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
944 lt->selection.cursor = lt->cursor;
945 updateLocal(bv, CURSOR_PAR, true);
946 result = DISPATCHED_NOUPDATE;
948 // --- Cursor Movements -----------------------------------
951 moveRight(bv, false, true);
952 lt->setSelection(bv);
953 updateLocal(bv, SELECTION, false);
956 result = moveRight(bv);
958 updateLocal(bv, CURSOR, false);
962 moveLeft(bv, false, true);
963 lt->setSelection(bv);
964 updateLocal(bv, SELECTION, false);
968 result = moveLeft(bv);
969 updateLocal(bv, CURSOR, false);
974 lt->setSelection(bv);
975 updateLocal(bv, SELECTION, false);
979 result = moveDown(bv);
980 updateLocal(bv, CURSOR, false);
985 lt->setSelection(bv);
986 updateLocal(bv, SELECTION, false);
991 updateLocal(bv, CURSOR, false);
996 updateLocal(bv, CURSOR, false);
1000 updateLocal(bv, CURSOR, false);
1002 case LFUN_BACKSPACE: {
1003 setUndo(bv, Undo::DELETE,
1004 lt->cursor.par(), lt->cursor.par()->next());
1005 if (lt->selection.set())
1006 lt->cutSelection(bv);
1009 updateLocal(bv, CURSOR_PAR, true);
1014 setUndo(bv, Undo::DELETE,
1015 lt->cursor.par(), lt->cursor.par()->next());
1016 if (lt->selection.set()) {
1017 lt->cutSelection(bv);
1021 updateLocal(bv, CURSOR_PAR, true);
1026 setUndo(bv, Undo::DELETE,
1027 lt->cursor.par(), lt->cursor.par()->next());
1028 lt->cutSelection(bv);
1029 updateLocal(bv, CURSOR_PAR, true);
1035 lt->copySelection(bv);
1036 updateLocal(bv, CURSOR_PAR, false);
1038 case LFUN_PASTESELECTION:
1040 string const clip(bv->getClipboard());
1044 if (arg == "paragraph") {
1045 lt->insertStringAsParagraphs(bv, clip);
1047 lt->insertStringAsLines(bv, clip);
1049 updateLocal(bv, CURSOR_PAR, true);
1053 if (!autoBreakRows) {
1055 if (CutAndPaste::nrOfParagraphs() > 1) {
1056 WriteAlert(_("Impossible operation"),
1057 _("Cannot include more than one paragraph!"),
1062 setUndo(bv, Undo::INSERT,
1063 lt->cursor.par(), lt->cursor.par()->next());
1064 lt->pasteSelection(bv);
1065 updateLocal(bv, CURSOR_PAR, true);
1069 case LFUN_BREAKPARAGRAPH:
1070 if (!autoBreakRows) {
1071 result = DISPATCHED;
1074 lt->breakParagraph(bv, 0);
1075 updateLocal(bv, FULL, true);
1077 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1078 if (!autoBreakRows) {
1079 result = DISPATCHED;
1082 lt->breakParagraph(bv, 1);
1083 updateLocal(bv, FULL, true);
1086 case LFUN_BREAKLINE: {
1087 if (!autoBreakRows) {
1088 result = DISPATCHED;
1091 setUndo(bv, Undo::INSERT,
1092 lt->cursor.par(), lt->cursor.par()->next());
1093 lt->insertChar(bv, Paragraph::META_NEWLINE);
1094 updateLocal(bv, CURSOR_PAR, true);
1099 // do not set layouts on non breakable textinsets
1100 if (autoBreakRows) {
1101 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1103 // Derive layout number from given argument (string)
1104 // and current buffer's textclass (number). */
1105 LyXTextClassList::ClassList::size_type tclass =
1106 bv->buffer()->params.textclass;
1107 std::pair <bool, LyXTextClass::size_type> layout =
1108 textclasslist.NumberOfLayout(tclass, arg);
1110 // If the entry is obsolete, use the new one instead.
1112 string obs = textclasslist.Style(tclass,layout.second).
1115 layout = textclasslist.NumberOfLayout(tclass, obs);
1118 // see if we found the layout number:
1119 if (!layout.first) {
1120 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1121 bv->owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, msg);
1125 if (cur_layout != layout.second) {
1126 cur_layout = layout.second;
1127 lt->setLayout(bv, layout.second);
1128 bv->owner()->setLayout(cpar(bv)->getLayout());
1129 updateLocal(bv, CURSOR_PAR, true);
1132 // reset the layout box
1133 bv->owner()->setLayout(cpar(bv)->getLayout());
1136 case LFUN_PARAGRAPH_SPACING:
1137 // This one is absolutely not working. When fiddling with this
1138 // it also seems to me that the paragraphs inside the insettext
1139 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1141 Paragraph * par = lt->cursor.par();
1142 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1143 float cur_value = 1.0;
1144 if (cur_spacing == Spacing::Other) {
1145 cur_value = par->params().spacing().getValue();
1148 std::istringstream istr(arg.c_str());
1151 Spacing::Space new_spacing = cur_spacing;
1152 float new_value = cur_value;
1154 lyxerr << "Missing argument to `paragraph-spacing'"
1156 } else if (tmp == "single") {
1157 new_spacing = Spacing::Single;
1158 } else if (tmp == "onehalf") {
1159 new_spacing = Spacing::Onehalf;
1160 } else if (tmp == "double") {
1161 new_spacing = Spacing::Double;
1162 } else if (tmp == "other") {
1163 new_spacing = Spacing::Other;
1166 lyxerr << "new_value = " << tmpval << endl;
1169 } else if (tmp == "default") {
1170 new_spacing = Spacing::Default;
1172 lyxerr << _("Unknown spacing argument: ")
1175 if (cur_spacing != new_spacing || cur_value != new_value) {
1176 par->params().spacing(Spacing(new_spacing, new_value));
1177 updateLocal(bv, CURSOR_PAR, true);
1183 if (!bv->Dispatch(action, arg))
1184 result = UNDISPATCHED;
1188 /// If the action has deleted all text in the inset, we need to change the
1189 // language to the language of the surronding text.
1190 if (par->size() == 0 && !par->next()) {
1191 LyXFont font(LyXFont::ALL_IGNORE);
1192 font.setLanguage(bv->getParentLanguage(this));
1193 setFont(bv, font, false);
1196 if (result != FINISHED) {
1197 showInsetCursor(bv);
1199 bv->unlockInset(this);
1206 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1209 buf->latexParagraphs(os, par, 0, texrow);
1210 return texrow.rows();
1214 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1216 Paragraph * p = par;
1217 unsigned int lines = 0;
1220 string const tmp = buf->asciiParagraph(p, linelen);
1221 lines += countChar(tmp, '\n');
1229 int InsetText::docBook(Buffer const * buf, ostream & os) const
1231 Paragraph * p = par;
1232 unsigned int lines = 0;
1237 buf->simpleDocBookOnePar(os, tmp, p, desc, 0);
1245 void InsetText::validate(LaTeXFeatures & features) const
1247 Paragraph * p = par;
1249 p->validate(features);
1255 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1257 if (textclasslist.Style(buf->params.textclass,
1258 p->getLayout()).labeltype != LABEL_MANUAL)
1261 return p->beginningOfMainBody();
1265 void InsetText::getCursorPos(BufferView * bv,
1266 int & x, int & y) const
1273 unsigned int InsetText::insetInInsetY()
1275 if (!the_locking_inset)
1278 return (inset_y + the_locking_inset->insetInInsetY());
1282 void InsetText::toggleInsetCursor(BufferView * bv)
1284 if (the_locking_inset) {
1285 the_locking_inset->toggleInsetCursor(bv);
1289 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1291 int const asc = lyxfont::maxAscent(font);
1292 int const desc = lyxfont::maxDescent(font);
1294 if (isCursorVisible())
1295 bv->hideLockedInsetCursor();
1297 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1298 toggleCursorVisible();
1302 void InsetText::showInsetCursor(BufferView * bv, bool show)
1304 if (the_locking_inset) {
1305 the_locking_inset->showInsetCursor(bv);
1308 if (!isCursorVisible()) {
1309 LyXFont const font =
1310 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1312 int const asc = lyxfont::maxAscent(font);
1313 int const desc = lyxfont::maxDescent(font);
1315 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1317 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1318 setCursorVisible(true);
1323 void InsetText::hideInsetCursor(BufferView * bv)
1325 if (isCursorVisible()) {
1326 bv->hideLockedInsetCursor();
1327 setCursorVisible(false);
1329 if (the_locking_inset)
1330 the_locking_inset->hideInsetCursor(bv);
1334 UpdatableInset::RESULT
1335 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1337 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1338 return moveLeftIntern(bv, false, activate_inset, selecting);
1340 return moveRightIntern(bv, false, activate_inset, selecting);
1344 UpdatableInset::RESULT
1345 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1347 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1348 return moveRightIntern(bv, true, activate_inset, selecting);
1350 return moveLeftIntern(bv, true, activate_inset, selecting);
1354 UpdatableInset::RESULT
1355 InsetText::moveRightIntern(BufferView * bv, bool behind,
1356 bool activate_inset, bool selecting)
1358 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1360 if (activate_inset && checkAndActivateInset(bv, behind))
1362 getLyXText(bv)->cursorRight(bv);
1364 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1365 return DISPATCHED_NOUPDATE;
1369 UpdatableInset::RESULT
1370 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1371 bool activate_inset, bool selecting)
1373 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1375 getLyXText(bv)->cursorLeft(bv);
1377 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1378 if (activate_inset && checkAndActivateInset(bv, behind))
1380 return DISPATCHED_NOUPDATE;
1384 UpdatableInset::RESULT
1385 InsetText::moveUp(BufferView * bv)
1387 if (!crow(bv)->previous())
1389 getLyXText(bv)->cursorUp(bv);
1390 return DISPATCHED_NOUPDATE;
1394 UpdatableInset::RESULT
1395 InsetText::moveDown(BufferView * bv)
1397 if (!crow(bv)->next())
1399 getLyXText(bv)->cursorDown(bv);
1400 return DISPATCHED_NOUPDATE;
1404 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1406 if (the_locking_inset) {
1407 if (the_locking_inset->insertInsetAllowed(inset))
1408 return the_locking_inset->insertInset(bv, inset);
1411 inset->setOwner(this);
1412 hideInsetCursor(bv);
1416 lt = getLyXText(bv);
1419 lt->insertInset(bv, inset);
1420 if ((cpar(bv)->getChar(cpos(bv)) != Paragraph::META_INSET) ||
1421 (cpar(bv)->getInset(cpos(bv)) != inset))
1424 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1425 showInsetCursor(bv);
1432 bool InsetText::insertInsetAllowed(Inset * in) const
1434 if (the_locking_inset)
1435 return the_locking_inset->insertInsetAllowed(in);
1440 UpdatableInset * InsetText::getLockingInset() const
1442 return the_locking_inset ? the_locking_inset->getLockingInset() :
1443 const_cast<InsetText *>(this);
1447 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1451 if (the_locking_inset)
1452 return the_locking_inset->getFirstLockingInsetOfType(c);
1457 bool InsetText::showInsetDialog(BufferView * bv) const
1459 if (the_locking_inset)
1460 return the_locking_inset->showInsetDialog(bv);
1465 std::vector<string> const InsetText::getLabelList() const
1467 std::vector<string> label_list;
1469 Paragraph * tpar = par;
1471 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1472 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1473 for (; beg != end; ++beg) {
1474 std::vector<string> const l = (*beg)->getLabelList();
1475 label_list.insert(label_list.end(), l.begin(), l.end());
1477 tpar = tpar->next();
1483 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1486 if (the_locking_inset) {
1487 the_locking_inset->setFont(bv, font, toggleall, selectall);
1492 lt = getLyXText(bv);
1495 if (lt->selection.set()) {
1496 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1501 lt->toggleFree(bv, font, toggleall);
1503 lt->setFont(bv, font, toggleall);
1506 lt->clearSelection(bv);
1508 if (selectall || lt->selection.set())
1509 updateLocal(bv, FULL, true);
1511 updateLocal(bv, CURSOR_PAR, true);
1517 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1519 if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
1523 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1524 if (!inset || inset->editable() != Inset::HIGHLY_EDITABLE)
1526 LyXFont const font =
1527 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1529 x = inset->width(bv, font);
1530 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1533 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1535 //inset_x = cx(bv) - top_x + drawTextXOffset;
1536 //inset_y = cy(bv) + drawTextYOffset;
1537 inset->edit(bv, x, y, 0);
1538 if (!the_locking_inset)
1540 updateLocal(bv, CURSOR, false);
1547 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1550 x -= drawTextXOffset;
1552 int dummyy = y + insetAscent;
1553 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy, button);
1560 inset_x = cx(bv) - top_x + drawTextXOffset;
1561 inset_y = cy(bv) + drawTextYOffset;
1562 inset->edit(bv, x - inset_x, y - inset_y, button);
1563 if (!the_locking_inset)
1565 updateLocal(bv, CURSOR, false);
1572 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1574 int w = UpdatableInset::getMaxWidth(bv, inset);
1579 w = w - top_x + owner()->x();
1582 w -= (2 * TEXT_TO_INSET_OFFSET);
1584 // return w - (2*TEXT_TO_INSET_OFFSET);
1588 void InsetText::setParagraphData(Paragraph * p)
1592 // now also delete all caches this should be safe, hopefully
1596 Paragraph * tmp = par->next();
1601 par = new Paragraph(*p);
1602 par->setInsetOwner(this);
1603 Paragraph * np = par;
1606 np->next(new Paragraph(*p));
1607 np->next()->previous(np);
1609 np->setInsetOwner(this);
1616 void InsetText::setText(string const & data)
1619 LyXFont font(LyXFont::ALL_SANE);
1620 for (unsigned int i=0; i < data.length(); ++i)
1621 par->insertChar(i, data[i], font);
1625 void InsetText::setAutoBreakRows(bool flag)
1627 if (flag != autoBreakRows) {
1628 autoBreakRows = flag;
1636 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1638 if (how != drawFrame_) {
1641 updateLocal(bv, DRAW_FRAME, false);
1646 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1648 if (frame_color != col) {
1651 updateLocal(bv, DRAW_FRAME, false);
1656 int InsetText::cx(BufferView * bv) const
1660 lt = getLyXText(bv);
1663 int x = lt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1664 if (the_locking_inset) {
1665 LyXFont font = lt->getFont(bv->buffer(),
1668 if (font.isVisibleRightToLeft())
1669 x -= the_locking_inset->width(bv, font);
1677 int InsetText::cy(BufferView * bv) const
1680 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1684 Paragraph::size_type InsetText::cpos(BufferView * bv) const
1686 return getLyXText(bv)->cursor.pos();
1690 Paragraph * InsetText::cpar(BufferView * bv) const
1692 return getLyXText(bv)->cursor.par();
1696 bool InsetText::cboundary(BufferView * bv) const
1698 return getLyXText(bv)->cursor.boundary();
1702 Row * InsetText::crow(BufferView * bv) const
1704 return getLyXText(bv)->cursor.row();
1708 LyXText * InsetText::getLyXText(BufferView const * lbv,
1709 bool const recursive) const
1711 if (!recursive && (cached_bview == lbv))
1712 return cached_text.get();
1714 // Super UGLY! (Lgb)
1715 BufferView * bv = const_cast<BufferView *>(lbv);
1718 Cache::iterator it = cache.find(bv);
1720 if (it != cache.end() && (lt || !it->second.remove)) {
1721 lyx::Assert(it->second.text.get());
1723 cached_text = it->second.text;
1724 if (recursive && the_locking_inset) {
1725 return the_locking_inset->getLyXText(bv);
1727 return cached_text.get();
1728 } else if (it->second.remove) {
1730 saveLyXTextState(it->second.text.get());
1738 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
1739 cached_text->init(bv);
1740 restoreLyXTextState(bv, cached_text.get());
1742 cache.insert(make_pair(bv, cached_text));
1744 if (the_locking_inset && recursive) {
1745 return the_locking_inset->getLyXText(bv);
1747 return cached_text.get();
1751 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1755 Cache::iterator it = cache.find(bv);
1757 if (it == cache.end()) {
1761 lyx::Assert(it->second.text.get());
1763 it->second.remove = true;
1765 /// then remove all LyXText in text-insets
1766 Paragraph * p = par;
1767 for (; p; p = p->next()) {
1768 p->deleteInsetsLyXText(bv);
1774 void InsetText::resizeLyXText(BufferView * bv, bool force) const
1776 if (!par->next() && !par->size()) // no data, resize not neccessary!
1778 // one endless line, resize normally not necessary
1779 if (!force && getMaxWidth(bv, this) < 0)
1782 Cache::iterator it = cache.find(bv);
1783 if (it == cache.end()) {
1786 lyx::Assert(it->second.text.get());
1788 deleteLyXText(bv, (the_locking_inset == 0) || force);
1791 LyXText * t = getLyXText(bv);
1792 t->first = bv->screen()->topCursorVisible(t);
1795 // this will scroll the screen such that the cursor becomes visible
1796 bv->updateScrollbar();
1797 if (the_locking_inset) {
1798 /// then resize all LyXText in text-insets
1799 inset_x = cx(bv) - top_x + drawTextXOffset;
1800 inset_y = cy(bv) + drawTextYOffset;
1801 for (Paragraph * p = par; p; p = p->next()) {
1802 p->resizeInsetsLyXText(bv);
1809 void InsetText::removeNewlines()
1811 for (Paragraph * p = par; p; p = p->next()) {
1812 for (int i = 0; i < p->size(); ++i) {
1813 if (p->getChar(i) == Paragraph::META_NEWLINE)
1820 bool InsetText::nodraw() const
1822 if (the_locking_inset)
1823 return the_locking_inset->nodraw();
1824 return UpdatableInset::nodraw();
1828 int InsetText::scroll(bool recursive) const
1830 int sx = UpdatableInset::scroll(false);
1832 if (recursive && the_locking_inset)
1833 sx += the_locking_inset->scroll(recursive);
1839 bool InsetText::doClearArea() const
1841 return !locked || (need_update & (FULL|INIT));
1845 void InsetText::selectAll(BufferView * bv)
1847 getLyXText(bv)->cursorTop(bv);
1848 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1849 getLyXText(bv)->cursorBottom(bv);
1850 getLyXText(bv)->setSelection(bv);
1854 void InsetText::clearSelection(BufferView * bv)
1856 getLyXText(bv)->clearSelection(bv);
1860 void InsetText::clearInset(Painter & pain, int baseline, bool & cleared) const
1863 int h = insetAscent + insetDescent;
1864 int ty = baseline - insetAscent;
1870 if ((ty + h) > pain.paperHeight())
1871 h = pain.paperHeight();
1872 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
1873 w = pain.paperWidth();
1874 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
1880 Paragraph * InsetText::getParFromID(int id) const
1882 Paragraph * result = par;
1883 Paragraph * ires = 0;
1884 while (result && result->id() != id) {
1885 if ((ires = result->getParFromID(id)))
1887 result = result->next();
1893 Paragraph * InsetText::firstParagraph() const
1896 if (the_locking_inset)
1897 if ((result = the_locking_inset->firstParagraph()))
1903 LyXCursor const & InsetText::cursor(BufferView * bv) const
1905 if (the_locking_inset)
1906 return the_locking_inset->cursor(bv);
1907 return getLyXText(bv)->cursor;
1911 Paragraph * InsetText::paragraph() const
1917 void InsetText::paragraph(Paragraph * p)
1921 // we now have to update/redraw all instances
1922 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
1927 // redraw myself when asked for
1928 need_update |= INIT;
1932 Inset * InsetText::getInsetFromID(int id_arg) const
1935 return const_cast<InsetText *>(this);
1937 Paragraph * lp = par;
1940 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
1941 en = lp->inset_iterator_end();
1944 if ((*it)->id() == id_arg)
1946 Inset * in = (*it)->getInsetFromID(id_arg);