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
1270 if (the_locking_inset) {
1271 the_locking_inset->getCursorPos(bv, x, y);
1279 unsigned int InsetText::insetInInsetY()
1281 if (!the_locking_inset)
1284 return (inset_y + the_locking_inset->insetInInsetY());
1288 void InsetText::toggleInsetCursor(BufferView * bv)
1290 if (the_locking_inset) {
1291 the_locking_inset->toggleInsetCursor(bv);
1295 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1297 int const asc = lyxfont::maxAscent(font);
1298 int const desc = lyxfont::maxDescent(font);
1300 if (isCursorVisible())
1301 bv->hideLockedInsetCursor();
1303 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1304 toggleCursorVisible();
1308 void InsetText::showInsetCursor(BufferView * bv, bool show)
1310 if (the_locking_inset) {
1311 the_locking_inset->showInsetCursor(bv);
1314 if (!isCursorVisible()) {
1315 LyXFont const font =
1316 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1318 int const asc = lyxfont::maxAscent(font);
1319 int const desc = lyxfont::maxDescent(font);
1321 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1323 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1324 setCursorVisible(true);
1329 void InsetText::hideInsetCursor(BufferView * bv)
1331 if (isCursorVisible()) {
1332 bv->hideLockedInsetCursor();
1333 setCursorVisible(false);
1335 if (the_locking_inset)
1336 the_locking_inset->hideInsetCursor(bv);
1340 UpdatableInset::RESULT
1341 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1343 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1344 return moveLeftIntern(bv, false, activate_inset, selecting);
1346 return moveRightIntern(bv, false, activate_inset, selecting);
1350 UpdatableInset::RESULT
1351 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1353 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1354 return moveRightIntern(bv, true, activate_inset, selecting);
1356 return moveLeftIntern(bv, true, activate_inset, selecting);
1360 UpdatableInset::RESULT
1361 InsetText::moveRightIntern(BufferView * bv, bool behind,
1362 bool activate_inset, bool selecting)
1364 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1366 if (activate_inset && checkAndActivateInset(bv, behind))
1368 getLyXText(bv)->cursorRight(bv);
1370 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1371 return DISPATCHED_NOUPDATE;
1375 UpdatableInset::RESULT
1376 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1377 bool activate_inset, bool selecting)
1379 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1381 getLyXText(bv)->cursorLeft(bv);
1383 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1384 if (activate_inset && checkAndActivateInset(bv, behind))
1386 return DISPATCHED_NOUPDATE;
1390 UpdatableInset::RESULT
1391 InsetText::moveUp(BufferView * bv)
1393 if (!crow(bv)->previous())
1395 getLyXText(bv)->cursorUp(bv);
1396 return DISPATCHED_NOUPDATE;
1400 UpdatableInset::RESULT
1401 InsetText::moveDown(BufferView * bv)
1403 if (!crow(bv)->next())
1405 getLyXText(bv)->cursorDown(bv);
1406 return DISPATCHED_NOUPDATE;
1410 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1412 if (the_locking_inset) {
1413 if (the_locking_inset->insertInsetAllowed(inset))
1414 return the_locking_inset->insertInset(bv, inset);
1417 inset->setOwner(this);
1418 hideInsetCursor(bv);
1422 lt = getLyXText(bv);
1425 lt->insertInset(bv, inset);
1426 if ((cpar(bv)->getChar(cpos(bv)) != Paragraph::META_INSET) ||
1427 (cpar(bv)->getInset(cpos(bv)) != inset))
1430 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1431 showInsetCursor(bv);
1438 bool InsetText::insertInsetAllowed(Inset * in) const
1440 if (the_locking_inset)
1441 return the_locking_inset->insertInsetAllowed(in);
1445 bool InsetText::insertInsetAllowed(Inset::Code code) const
1447 if (the_locking_inset)
1448 return the_locking_inset->insertInsetAllowed(code);
1453 UpdatableInset * InsetText::getLockingInset() const
1455 return the_locking_inset ? the_locking_inset->getLockingInset() :
1456 const_cast<InsetText *>(this);
1460 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1464 if (the_locking_inset)
1465 return the_locking_inset->getFirstLockingInsetOfType(c);
1470 bool InsetText::showInsetDialog(BufferView * bv) const
1472 if (the_locking_inset)
1473 return the_locking_inset->showInsetDialog(bv);
1478 std::vector<string> const InsetText::getLabelList() const
1480 std::vector<string> label_list;
1482 Paragraph * tpar = par;
1484 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1485 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1486 for (; beg != end; ++beg) {
1487 std::vector<string> const l = (*beg)->getLabelList();
1488 label_list.insert(label_list.end(), l.begin(), l.end());
1490 tpar = tpar->next();
1496 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1499 if (the_locking_inset) {
1500 the_locking_inset->setFont(bv, font, toggleall, selectall);
1505 lt = getLyXText(bv);
1508 if (lt->selection.set()) {
1509 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1514 lt->toggleFree(bv, font, toggleall);
1516 lt->setFont(bv, font, toggleall);
1519 lt->clearSelection(bv);
1521 if (selectall || lt->selection.set())
1522 updateLocal(bv, FULL, true);
1524 updateLocal(bv, CURSOR_PAR, true);
1530 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1532 if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
1536 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1537 if (!inset || inset->editable() != Inset::HIGHLY_EDITABLE)
1539 LyXFont const font =
1540 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1542 x = inset->width(bv, font);
1543 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1546 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1548 //inset_x = cx(bv) - top_x + drawTextXOffset;
1549 //inset_y = cy(bv) + drawTextYOffset;
1550 inset->edit(bv, x, y, 0);
1551 if (!the_locking_inset)
1553 updateLocal(bv, CURSOR, false);
1560 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1563 x -= drawTextXOffset;
1565 int dummyy = y + insetAscent;
1566 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy, button);
1573 inset_x = cx(bv) - top_x + drawTextXOffset;
1574 inset_y = cy(bv) + drawTextYOffset;
1575 inset->edit(bv, x - inset_x, y - inset_y, button);
1576 if (!the_locking_inset)
1578 updateLocal(bv, CURSOR, false);
1585 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1587 int w = UpdatableInset::getMaxWidth(bv, inset);
1592 w = w - top_x + owner()->x();
1595 w -= (2 * TEXT_TO_INSET_OFFSET);
1597 // return w - (2*TEXT_TO_INSET_OFFSET);
1601 void InsetText::setParagraphData(Paragraph * p)
1605 // now also delete all caches this should be safe, hopefully
1609 Paragraph * tmp = par->next();
1614 par = new Paragraph(*p);
1615 par->setInsetOwner(this);
1616 Paragraph * np = par;
1619 np->next(new Paragraph(*p));
1620 np->next()->previous(np);
1622 np->setInsetOwner(this);
1629 void InsetText::setText(string const & data)
1632 LyXFont font(LyXFont::ALL_SANE);
1633 for (unsigned int i=0; i < data.length(); ++i)
1634 par->insertChar(i, data[i], font);
1638 void InsetText::setAutoBreakRows(bool flag)
1640 if (flag != autoBreakRows) {
1641 autoBreakRows = flag;
1649 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1651 if (how != drawFrame_) {
1654 updateLocal(bv, DRAW_FRAME, false);
1659 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1661 if (frame_color != col) {
1664 updateLocal(bv, DRAW_FRAME, false);
1669 int InsetText::cx(BufferView * bv) const
1673 lt = getLyXText(bv);
1676 int x = lt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1677 if (the_locking_inset) {
1678 LyXFont font = lt->getFont(bv->buffer(),
1681 if (font.isVisibleRightToLeft())
1682 x -= the_locking_inset->width(bv, font);
1690 int InsetText::cy(BufferView * bv) const
1693 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1697 Paragraph::size_type InsetText::cpos(BufferView * bv) const
1699 return getLyXText(bv)->cursor.pos();
1703 Paragraph * InsetText::cpar(BufferView * bv) const
1705 return getLyXText(bv)->cursor.par();
1709 bool InsetText::cboundary(BufferView * bv) const
1711 return getLyXText(bv)->cursor.boundary();
1715 Row * InsetText::crow(BufferView * bv) const
1717 return getLyXText(bv)->cursor.row();
1721 LyXText * InsetText::getLyXText(BufferView const * lbv,
1722 bool const recursive) const
1724 if (!recursive && (cached_bview == lbv))
1725 return cached_text.get();
1727 // Super UGLY! (Lgb)
1728 BufferView * bv = const_cast<BufferView *>(lbv);
1731 Cache::iterator it = cache.find(bv);
1733 if (it != cache.end() && (lt || !it->second.remove)) {
1734 lyx::Assert(it->second.text.get());
1736 cached_text = it->second.text;
1737 if (recursive && the_locking_inset) {
1738 return the_locking_inset->getLyXText(bv);
1740 return cached_text.get();
1741 } else if (it->second.remove) {
1743 saveLyXTextState(it->second.text.get());
1751 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
1752 cached_text->init(bv);
1753 restoreLyXTextState(bv, cached_text.get());
1755 cache.insert(make_pair(bv, cached_text));
1757 if (the_locking_inset && recursive) {
1758 return the_locking_inset->getLyXText(bv);
1760 return cached_text.get();
1764 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1768 Cache::iterator it = cache.find(bv);
1770 if (it == cache.end()) {
1774 lyx::Assert(it->second.text.get());
1776 it->second.remove = true;
1778 /// then remove all LyXText in text-insets
1779 Paragraph * p = par;
1780 for (; p; p = p->next()) {
1781 p->deleteInsetsLyXText(bv);
1787 void InsetText::resizeLyXText(BufferView * bv, bool force) const
1789 if (!par->next() && !par->size()) // no data, resize not neccessary!
1791 // one endless line, resize normally not necessary
1792 if (!force && getMaxWidth(bv, this) < 0)
1795 Cache::iterator it = cache.find(bv);
1796 if (it == cache.end()) {
1799 lyx::Assert(it->second.text.get());
1801 deleteLyXText(bv, (the_locking_inset == 0) || force);
1804 LyXText * t = getLyXText(bv);
1805 t->first = bv->screen()->topCursorVisible(t);
1808 // this will scroll the screen such that the cursor becomes visible
1809 bv->updateScrollbar();
1810 if (the_locking_inset) {
1811 /// then resize all LyXText in text-insets
1812 inset_x = cx(bv) - top_x + drawTextXOffset;
1813 inset_y = cy(bv) + drawTextYOffset;
1814 for (Paragraph * p = par; p; p = p->next()) {
1815 p->resizeInsetsLyXText(bv);
1822 void InsetText::removeNewlines()
1824 for (Paragraph * p = par; p; p = p->next()) {
1825 for (int i = 0; i < p->size(); ++i) {
1826 if (p->getChar(i) == Paragraph::META_NEWLINE)
1833 bool InsetText::nodraw() const
1835 if (the_locking_inset)
1836 return the_locking_inset->nodraw();
1837 return UpdatableInset::nodraw();
1841 int InsetText::scroll(bool recursive) const
1843 int sx = UpdatableInset::scroll(false);
1845 if (recursive && the_locking_inset)
1846 sx += the_locking_inset->scroll(recursive);
1852 bool InsetText::doClearArea() const
1854 return !locked || (need_update & (FULL|INIT));
1858 void InsetText::selectAll(BufferView * bv)
1860 getLyXText(bv)->cursorTop(bv);
1861 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1862 getLyXText(bv)->cursorBottom(bv);
1863 getLyXText(bv)->setSelection(bv);
1867 void InsetText::clearSelection(BufferView * bv)
1869 getLyXText(bv)->clearSelection(bv);
1873 void InsetText::clearInset(Painter & pain, int baseline, bool & cleared) const
1876 int h = insetAscent + insetDescent;
1877 int ty = baseline - insetAscent;
1883 if ((ty + h) > pain.paperHeight())
1884 h = pain.paperHeight();
1885 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
1886 w = pain.paperWidth();
1887 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
1893 Paragraph * InsetText::getParFromID(int id) const
1895 Paragraph * result = par;
1896 Paragraph * ires = 0;
1897 while (result && result->id() != id) {
1898 if ((ires = result->getParFromID(id)))
1900 result = result->next();
1906 Paragraph * InsetText::firstParagraph() const
1909 if (the_locking_inset)
1910 if ((result = the_locking_inset->firstParagraph()))
1916 LyXCursor const & InsetText::cursor(BufferView * bv) const
1918 if (the_locking_inset)
1919 return the_locking_inset->cursor(bv);
1920 return getLyXText(bv)->cursor;
1924 Paragraph * InsetText::paragraph() const
1930 void InsetText::paragraph(Paragraph * p)
1934 // we now have to update/redraw all instances
1935 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
1940 // redraw myself when asked for
1941 need_update |= INIT;
1945 Inset * InsetText::getInsetFromID(int id_arg) const
1948 return const_cast<InsetText *>(this);
1950 Paragraph * lp = par;
1953 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
1954 en = lp->inset_iterator_end();
1957 if ((*it)->id() == id_arg)
1959 Inset * in = (*it)->getInsetFromID(id_arg);
1969 string InsetText::selectNextWord(BufferView * bv, float & value) const
1975 lt = getLyXText(bv);
1978 if (the_locking_inset) {
1979 str = the_locking_inset->selectNextWord(bv, value);
1986 #warning Dekel please have a look on this one RTL? (Jug)
1987 // we have to go on checking so move cusor to the right
1988 lt->cursor.pos(lt->cursor.pos() + 1);
1990 str = lt->selectNextWord(bv, value);
1992 bv->unlockInset(const_cast<InsetText *>(this));
2001 void InsetText::selectSelectedWord(BufferView * bv)
2003 if (the_locking_inset) {
2004 the_locking_inset->selectSelectedWord(bv);
2007 getLyXText(bv)->selectSelectedWord(bv);
2008 updateLocal(bv, SELECTION, false);
2012 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2014 if (the_locking_inset) {
2015 the_locking_inset->toggleSelection(bv, kill_selection);
2019 lt = getLyXText(bv);
2023 int x = top_x + TEXT_TO_INSET_OFFSET;
2026 Row * row = lt->getRowNearY(y);
2027 int y_offset = top_baseline - row->ascent_of_text();
2029 while ((row != 0) && ((y+row->height()) <= 0)) {
2036 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);