1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998-2001 The LyX Team.
8 * ======================================================
20 #pragma implementation
23 #include "insettext.h"
24 #include "paragraph.h"
28 #include "commandtags.h"
31 #include "BufferView.h"
33 #include "LaTeXFeatures.h"
35 #include "lyx_gui_misc.h"
37 #include "lyxcursor.h"
38 #include "CutAndPaste.h"
41 #include "support/textutils.h"
42 #include "support/LAssert.h"
46 #include "trans_mgr.h"
47 #include "lyxscreen.h"
51 #include "ParagraphParameters.h"
52 #include "undo_funcs.h"
62 extern unsigned char getCurrentTextClass(Buffer *);
63 extern bool math_insert_greek(BufferView *, char);
64 extern int greek_kb_flag;
67 #warning this functions should probably go into bufferview_funcs somehow (Jug)
69 void InsetText::saveLyXTextState(LyXText * t) const
71 sstate.lpar = t->cursor.par();
72 sstate.pos = t->cursor.pos();
73 sstate.boundary = t->cursor.boundary();
74 sstate.selstartpar = t->selection.start.par();
75 sstate.selstartpos = t->selection.start.pos();
76 sstate.selstartboundary = t->selection.start.boundary();
77 sstate.selendpar = t->selection.end.par();
78 sstate.selendpos = t->selection.end.pos();
79 sstate.selendboundary = t->selection.end.boundary();
80 sstate.selection = t->selection.set();
81 sstate.mark_set = t->selection.mark();
84 void InsetText::restoreLyXTextState(BufferView * bv, LyXText * t) const
87 t->selection.set(true);
88 /* at this point just to avoid the Delete-Empty-Paragraph
89 * Mechanism when setting the cursor */
90 t->selection.mark(sstate.mark_set);
91 if (sstate.selection) {
92 t->setCursor(bv, sstate.selstartpar, sstate.selstartpos,
93 true, sstate.selstartboundary);
94 t->selection.cursor = t->cursor;
95 t->setCursor(bv, sstate.selendpar, sstate.selendpos,
96 true, sstate.selendboundary);
98 t->setCursor(bv, sstate.lpar, sstate.pos);
100 t->setCursor(bv, sstate.lpar, sstate.pos, true, sstate.boundary);
101 t->selection.cursor = t->cursor;
102 t->selection.set(false);
108 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
115 InsetText::InsetText()
122 InsetText::InsetText(InsetText const & ins, bool same_id)
127 autoBreakRows = ins.autoBreakRows;
131 InsetText & InsetText::operator=(InsetText const & it)
134 autoBreakRows = it.autoBreakRows;
139 void InsetText::init(InsetText const * ins, bool same_id)
147 the_locking_inset = 0;
149 no_selection = false;
153 autoBreakRows = false;
156 frame_color = LColor::insetframe;
158 setParagraphData(ins->par);
159 autoBreakRows = ins->autoBreakRows;
160 drawFrame_ = ins->drawFrame_;
161 frame_color = ins->frame_color;
165 par->setInsetOwner(this);
168 last_drawn_width = -1;
169 frame_is_visible = false;
176 InsetText::~InsetText()
183 Paragraph * tmp = par->next();
190 void InsetText::clear()
194 // now also delete all caches this should be safe, hopefully
198 Paragraph * tmp = par->next();
206 Inset * InsetText::clone(Buffer const &, bool same_id) const
208 return new InsetText(*this, same_id);
212 void InsetText::write(Buffer const * buf, ostream & os) const
215 writeParagraphData(buf, os);
219 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
221 par->writeFile(buf, os, buf->params, 0);
225 void InsetText::read(Buffer const * buf, LyXLex & lex)
229 Paragraph * return_par = 0;
230 Paragraph::depth_type depth = 0;
231 LyXFont font(LyXFont::ALL_INHERIT);
237 token = lex.GetString();
240 if (token == "\\end_inset")
242 if (const_cast<Buffer*>(buf)->
243 parseSingleLyXformat2Token(lex, par, return_par,
244 token, pos, depth, font)) {
245 // the_end read this should NEVER happen
246 lex.printError("\\the_end read in inset! Error in document!");
254 return_par->setInsetOwner(this);
255 return_par = return_par->next();
258 if (token != "\\end_inset") {
259 lex.printError("Missing \\end_inset at this point. "
266 int InsetText::ascent(BufferView * bv, LyXFont const &) const
269 Row * row = getLyXText(bv)->getRowNearY(y_temp);
270 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
275 int InsetText::descent(BufferView * bv, LyXFont const &) const
283 Row * row = lt->getRowNearY(y_temp);
284 insetDescent = lt->height - row->ascent_of_text() +
285 TEXT_TO_INSET_OFFSET;
292 int InsetText::width(BufferView * bv, LyXFont const &) const
294 insetWidth = max(textWidth(bv),
295 (int)getLyXText(bv)->width + (2 * TEXT_TO_INSET_OFFSET));
300 int InsetText::textWidth(BufferView * bv) const
302 int const w = getMaxWidth(bv, this);
307 void InsetText::draw(BufferView * bv, LyXFont const & f,
308 int baseline, float & x, bool cleared) const
313 Painter & pain = bv->painter();
315 // no draw is necessary !!!
316 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
318 top_baseline = baseline;
320 if (!cleared && (need_update & CLEAR_FRAME))
321 clearFrame(pain, cleared);
323 frame_is_visible = false;
330 UpdatableInset::draw(bv, f, baseline, x, cleared);
333 x += static_cast<float>(scroll());
336 // update insetWidth and insetHeight with dummy calls
338 (void)descent(bv, f);
342 // if top_x differs we have a rule down and we don't have to clear anything
343 if (!cleared && (top_x == int(x)) &&
344 ((need_update&(INIT|FULL)) || (top_baseline!=baseline) ||
345 (last_drawn_width!=insetWidth))) {
346 clearInset(pain, baseline, cleared);
349 frame_is_visible = false;
351 if (!cleared && (need_update == NONE))
354 if (top_x != int(x)) {
355 if ((getMaxWidth(bv, this) > 0) &&
356 (getLyXText(bv)->width != old_max_width)) {
359 old_max_width = getLyXText(bv)->width;
360 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
363 clearInset(pain, baseline, cleared);
366 // lyxerr << "InsetText::draw[" << this << "](" << need_update << ":" << int(x) << ":" << top_x << ")\n";
368 if (cleared || (last_drawn_width != insetWidth)) {
370 last_drawn_width = insetWidth;
373 top_baseline = baseline;
374 top_y = baseline - ascent(bv, f);
375 last_width = width(bv, f);
376 last_height = ascent(bv, f) + descent(bv, f);
378 if (the_locking_inset && (cpar(bv) == inset_par)
379 && (cpos(bv) == inset_pos)) {
380 inset_x = cx(bv) - top_x + drawTextXOffset;
381 inset_y = cy(bv) + drawTextYOffset;
383 if (!cleared && (need_update == CURSOR)
384 && !getLyXText(bv)->selection.set()) {
385 drawFrame(pain, cleared);
386 x += last_width; // was width(bv, f);
395 x += TEXT_TO_INSET_OFFSET;
398 Row * row = lt->getRowNearY(y);
399 int y_offset = baseline - row->ascent_of_text();
400 int ph = pain.paperHeight();
403 while ((row != 0) && ((y+row->height()) <= 0)) {
405 first += row->height();
414 while ((row != 0) && (yf < ph)) {
415 lt->getVisibleRow(bv, y+y_offset, int(x), row,
421 } else if (!locked) {
422 if (need_update & CURSOR) {
423 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
424 lt->clearSelection(bv);
425 lt->selection.cursor = lt->cursor;
427 bv->screen()->update(lt, bv, y_offset, int(x));
430 if (need_update & SELECTION)
431 bv->screen()->toggleToggle(lt, bv, y_offset, int(x));
432 else if (need_update & CURSOR) {
433 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
434 lt->clearSelection(bv);
435 lt->selection.cursor = lt->cursor;
437 bv->screen()->update(lt, bv, y_offset, int(x));
442 lt->status(bv, LyXText::UNCHANGED);
443 if ((need_update != CURSOR_PAR) &&
444 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked)))
445 drawFrame(pain, cleared);
446 else if (need_update & CLEAR_FRAME)
447 clearFrame(pain, cleared);
448 x += last_width /* was width(bv, f) */ - TEXT_TO_INSET_OFFSET;
449 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
451 } else if (need_update != INIT)
458 void InsetText::drawFrame(Painter & pain, bool cleared) const
460 if (!frame_is_visible || cleared) {
461 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
462 insetWidth - 1, insetAscent + insetDescent - 1,
464 frame_is_visible = true;
469 void InsetText::clearFrame(Painter & pain, bool cleared) const
471 if (frame_is_visible) {
473 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
474 insetWidth - 1, insetAscent + insetDescent - 1,
477 frame_is_visible = false;
482 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
488 owner()->update(bv, font, true);
491 if (the_locking_inset) {
492 inset_x = cx(bv) - top_x + drawTextXOffset;
493 inset_y = cy(bv) + drawTextYOffset;
494 the_locking_inset->update(bv, font, reinit);
502 int oldw = insetWidth;
503 insetWidth = lt->width + (2 * TEXT_TO_INSET_OFFSET);
504 if (oldw != insetWidth) {
511 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
514 lt->updateInset(bv, the_locking_inset);
516 if (lt->status() == LyXText::NEED_MORE_REFRESH)
523 void InsetText::setUpdateStatus(BufferView * bv, int what) const
531 if (lt->status() == LyXText::NEED_MORE_REFRESH)
533 else if (lt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)
534 need_update |= CURSOR_PAR;
536 // this to not draw a selection when we redraw all of it!
537 if ((need_update & (INIT|FULL)) && (need_update & CURSOR))
538 lt->clearSelection(bv);
544 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
552 setUpdateStatus(bv, what);
553 if ((need_update != CURSOR) || (lt->status() != LyXText::UNCHANGED) ||
556 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
558 bv->owner()->showState();
559 if (old_par != cpar(bv)) {
560 bv->owner()->setLayout(cpar(bv)->getLayout());
568 string const InsetText::editMessage() const
570 return _("Opened Text Inset");
574 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
576 UpdatableInset::edit(bv, x, y, button);
578 if (!bv->lockInset(this)) {
579 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
583 the_locking_inset = 0;
584 inset_pos = inset_x = inset_y = 0;
585 inset_boundary = false;
588 int tmp_y = (y < 0) ? 0 : y;
595 if (!checkAndActivateInset(bv, x, tmp_y, button))
596 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
598 lt->clearSelection(bv);
601 updateLocal(bv, CURSOR, false);
603 // If the inset is empty set the language of the current font to the
604 // language to the surronding text (if different).
605 if (par->size() == 0 && !par->next() &&
606 bv->getParentLanguage(this) != lt->current_font.language()) {
607 LyXFont font(LyXFont::ALL_IGNORE);
608 font.setLanguage(bv->getParentLanguage(this));
609 setFont(bv, font, false);
616 void InsetText::edit(BufferView * bv, bool front)
618 UpdatableInset::edit(bv, front);
620 if (!bv->lockInset(this)) {
621 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
625 the_locking_inset = 0;
626 inset_pos = inset_x = inset_y = 0;
627 inset_boundary = false;
636 lt->setCursor(bv, par, 0);
641 lt->setCursor(bv, p, p->size()-1);
643 lt->clearSelection(bv);
646 updateLocal(bv, CURSOR, false);
648 // If the inset is empty set the language of the current font to the
649 // language to the surronding text (if different).
650 if (par->size() == 0 && !par->next() &&
651 bv->getParentLanguage(this) != lt->current_font.language()) {
652 LyXFont font(LyXFont::ALL_IGNORE);
653 font.setLanguage(bv->getParentLanguage(this));
654 setFont(bv, font, false);
661 void InsetText::insetUnlock(BufferView * bv)
663 if (the_locking_inset) {
664 the_locking_inset->insetUnlock(bv);
665 the_locking_inset = 0;
668 no_selection = false;
671 if (drawFrame_ == LOCKED)
672 code = CURSOR|CLEAR_FRAME;
680 if (lt->selection.set()) {
681 lt->clearSelection(bv);
683 } else if (owner()) {
684 bv->owner()->setLayout(owner()->getLyXText(bv)
685 ->cursor.par()->getLayout());
687 bv->owner()->setLayout(bv->text->cursor.par()->getLayout());
688 updateLocal(bv, code, false);
694 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
696 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
700 if (inset == cpar(bv)->getInset(cpos(bv))) {
701 lyxerr[Debug::INSETS] << "OK" << endl;
702 the_locking_inset = inset;
703 inset_x = cx(bv) - top_x + drawTextXOffset;
704 inset_y = cy(bv) + drawTextYOffset;
705 inset_pos = cpos(bv);
706 inset_par = cpar(bv);
707 inset_boundary = cboundary(bv);
708 updateLocal(bv, CURSOR, false);
710 } else if (the_locking_inset && (the_locking_inset == inset)) {
711 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
712 lyxerr[Debug::INSETS] << "OK" << endl;
713 inset_x = cx(bv) - top_x + drawTextXOffset;
714 inset_y = cy(bv) + drawTextYOffset;
716 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
718 } else if (the_locking_inset) {
719 lyxerr[Debug::INSETS] << "MAYBE" << endl;
720 return the_locking_inset->lockInsetInInset(bv, inset);
722 lyxerr[Debug::INSETS] << "NOT OK" << endl;
727 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
730 if (!the_locking_inset)
732 if (the_locking_inset == inset) {
733 the_locking_inset->insetUnlock(bv);
734 getLyXText(bv)->updateInset(bv, inset);
735 the_locking_inset = 0;
737 moveRight(bv, false);
738 old_par = 0; // force layout setting
742 updateLocal(bv, CURSOR, false);
745 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
749 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
751 if (!the_locking_inset)
753 if (the_locking_inset != inset) {
754 getLyXText(bv)->updateInset(bv, the_locking_inset);
755 setUpdateStatus(bv, CURSOR_PAR);
756 return the_locking_inset->updateInsetInInset(bv, inset);
758 // updateLocal(bv, FULL, false);
759 if (getLyXText(bv)->updateInset(bv, inset))
760 updateLocal(bv, CURSOR_PAR, false);
761 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
762 inset_x = cx(bv) - top_x + drawTextXOffset;
763 inset_y = cy(bv) + drawTextYOffset;
769 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
773 int tmp_x = x - drawTextXOffset;
774 int tmp_y = y + insetAscent - getLyXText(bv)->first;
775 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y, button);
778 if (the_locking_inset) {
779 if (the_locking_inset == inset) {
780 the_locking_inset->insetButtonPress(bv,x-inset_x,y-inset_y,button);
781 no_selection = false;
784 // otherwise unlock the_locking_inset and lock the new inset
785 the_locking_inset->insetUnlock(bv);
786 inset_x = cx(bv) - top_x + drawTextXOffset;
787 inset_y = cy(bv) + drawTextYOffset;
788 the_locking_inset = static_cast<UpdatableInset*>(inset);
789 inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
790 inset->edit(bv, x - inset_x, y - inset_y, button);
791 if (the_locking_inset)
792 updateLocal(bv, CURSOR, false);
793 no_selection = false;
796 // otherwise only unlock the_locking_inset
797 the_locking_inset->insetUnlock(bv);
798 the_locking_inset = 0;
800 if (bv->theLockingInset()) {
801 if (inset && inset->editable() == Inset::HIGHLY_EDITABLE) {
802 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
803 inset_x = cx(bv) - top_x + drawTextXOffset;
804 inset_y = cy(bv) + drawTextYOffset;
805 inset_pos = cpos(bv);
806 inset_par = cpar(bv);
807 inset_boundary = cboundary(bv);
808 the_locking_inset = uinset;
809 uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
811 uinset->edit(bv, x - inset_x, y - inset_y, 0);
812 if (the_locking_inset)
813 updateLocal(bv, CURSOR, false);
814 no_selection = false;
818 if (!inset) { // && (button == 2)) {
819 bool paste_internally = false;
820 if ((button == 2) && getLyXText(bv)->selection.set()) {
821 localDispatch(bv, LFUN_COPY, "");
822 paste_internally = true;
824 getLyXText(bv)->setCursorFromCoordinates(bv, x-drawTextXOffset,
826 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
827 updateLocal(bv, CURSOR, false);
828 bv->owner()->setLayout(cpar(bv)->getLayout());
830 // Insert primary selection with middle mouse
831 // if there is a local selection in the current buffer,
834 if (paste_internally)
835 localDispatch(bv, LFUN_PASTE, "");
837 localDispatch(bv, LFUN_PASTESELECTION,
842 no_selection = false;
846 void InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
848 UpdatableInset * inset = 0;
850 if (the_locking_inset) {
851 the_locking_inset->insetButtonRelease(bv,
852 x - inset_x, y - inset_y,
855 if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
856 inset = static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
857 if (inset->editable() == Inset::HIGHLY_EDITABLE) {
858 inset->insetButtonRelease(bv,
860 y - inset_y, button);
862 inset_x = cx(bv) - top_x + drawTextXOffset;
863 inset_y = cy(bv) + drawTextYOffset;
864 inset->insetButtonRelease(bv,
866 y - inset_y, button);
868 x - inset_x, y - inset_y, button);
870 updateLocal(bv, CURSOR_PAR, false);
873 no_selection = false;
877 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
881 if (the_locking_inset) {
882 the_locking_inset->insetMotionNotify(bv, x - inset_x,
886 LyXText * t = getLyXText(bv);
888 t->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
890 if (t->toggle_cursor.par() != t->toggle_end_cursor.par() ||
891 t->toggle_cursor.pos() != t->toggle_end_cursor.pos())
892 updateLocal(bv, SELECTION, false);
897 void InsetText::insetKeyPress(XKeyEvent * xke)
899 if (the_locking_inset) {
900 the_locking_inset->insetKeyPress(xke);
906 UpdatableInset::RESULT
907 InsetText::localDispatch(BufferView * bv,
908 kb_action action, string const & arg)
910 no_selection = false;
911 UpdatableInset::RESULT
912 result= UpdatableInset::localDispatch(bv, action, arg);
913 if (result != UNDISPATCHED) {
918 if ((action < 0) && arg.empty())
921 if (the_locking_inset) {
922 result = the_locking_inset->localDispatch(bv, action, arg);
923 if (result == DISPATCHED_NOUPDATE)
925 else if (result == DISPATCHED) {
926 updateLocal(bv, CURSOR_PAR, false);
928 } else if (result == FINISHED) {
929 bool dispatched = false;
931 case LFUN_UNKNOWN_ACTION:
932 case LFUN_BREAKPARAGRAPH:
934 moveRightIntern(bv, false, false);
937 if (!getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
938 moveRightIntern(bv, false, false);
942 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
943 moveRightIntern(bv, false, false);
949 the_locking_inset = 0;
962 case LFUN_SELFINSERT:
963 if (bv->buffer()->isReadonly()) {
964 // setErrorMessage(N_("Document is read only"));
968 /* Automatically delete the currently selected
969 * text and replace it with what is being
970 * typed in now. Depends on lyxrc settings
971 * "auto_region_delete", which defaults to
974 setUndo(bv, Undo::INSERT,
975 lt->cursor.par(), lt->cursor.par()->next());
977 if (lyxrc.auto_region_delete) {
978 if (lt->selection.set()) {
979 lt->cutSelection(bv, false);
982 lt->clearSelection(bv);
983 for (string::size_type i = 0; i < arg.length(); ++i) {
985 if (!math_insert_greek(bv, arg[i])) {
986 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
987 } else if (!the_locking_inset) {
988 (void)moveRight(bv, false);
991 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
995 lt->selection.cursor = lt->cursor;
996 updateLocal(bv, CURSOR_PAR, true);
997 result = DISPATCHED_NOUPDATE;
999 // --- Cursor Movements -----------------------------------
1002 moveRight(bv, false, true);
1003 lt->setSelection(bv);
1004 updateLocal(bv, SELECTION, false);
1007 result = moveRight(bv);
1009 updateLocal(bv, CURSOR, false);
1013 moveLeft(bv, false, true);
1014 lt->setSelection(bv);
1015 updateLocal(bv, SELECTION, false);
1019 result = moveLeft(bv);
1020 updateLocal(bv, CURSOR, false);
1025 lt->setSelection(bv);
1026 updateLocal(bv, SELECTION, false);
1030 result = moveDown(bv);
1031 updateLocal(bv, CURSOR, false);
1036 lt->setSelection(bv);
1037 updateLocal(bv, SELECTION, false);
1041 result = moveUp(bv);
1042 updateLocal(bv, CURSOR, false);
1047 updateLocal(bv, CURSOR, false);
1051 updateLocal(bv, CURSOR, false);
1053 case LFUN_BACKSPACE: {
1054 setUndo(bv, Undo::DELETE,
1055 lt->cursor.par(), lt->cursor.par()->next());
1056 if (lt->selection.set())
1057 lt->cutSelection(bv);
1060 updateLocal(bv, CURSOR_PAR, true);
1065 setUndo(bv, Undo::DELETE,
1066 lt->cursor.par(), lt->cursor.par()->next());
1067 if (lt->selection.set()) {
1068 lt->cutSelection(bv);
1072 updateLocal(bv, CURSOR_PAR, true);
1077 setUndo(bv, Undo::DELETE,
1078 lt->cursor.par(), lt->cursor.par()->next());
1079 lt->cutSelection(bv);
1080 updateLocal(bv, CURSOR_PAR, true);
1086 lt->copySelection(bv);
1087 updateLocal(bv, CURSOR_PAR, false);
1089 case LFUN_PASTESELECTION:
1091 string const clip(bv->getClipboard());
1095 if (arg == "paragraph") {
1096 lt->insertStringAsParagraphs(bv, clip);
1098 lt->insertStringAsLines(bv, clip);
1100 updateLocal(bv, CURSOR_PAR, true);
1104 if (!autoBreakRows) {
1106 if (CutAndPaste::nrOfParagraphs() > 1) {
1107 WriteAlert(_("Impossible operation"),
1108 _("Cannot include more than one paragraph!"),
1113 setUndo(bv, Undo::INSERT,
1114 lt->cursor.par(), lt->cursor.par()->next());
1115 lt->pasteSelection(bv);
1116 updateLocal(bv, CURSOR_PAR, true);
1120 case LFUN_BREAKPARAGRAPH:
1121 if (!autoBreakRows) {
1122 result = DISPATCHED;
1125 lt->breakParagraph(bv, 0);
1126 updateLocal(bv, FULL, true);
1128 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1129 if (!autoBreakRows) {
1130 result = DISPATCHED;
1133 lt->breakParagraph(bv, 1);
1134 updateLocal(bv, FULL, true);
1137 case LFUN_BREAKLINE: {
1138 if (!autoBreakRows) {
1139 result = DISPATCHED;
1142 setUndo(bv, Undo::INSERT,
1143 lt->cursor.par(), lt->cursor.par()->next());
1144 lt->insertChar(bv, Paragraph::META_NEWLINE);
1145 updateLocal(bv, CURSOR_PAR, true);
1150 // do not set layouts on non breakable textinsets
1151 if (autoBreakRows) {
1152 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1154 // Derive layout number from given argument (string)
1155 // and current buffer's textclass (number). */
1156 LyXTextClassList::ClassList::size_type tclass =
1157 bv->buffer()->params.textclass;
1158 std::pair <bool, LyXTextClass::size_type> layout =
1159 textclasslist.NumberOfLayout(tclass, arg);
1161 // If the entry is obsolete, use the new one instead.
1163 string obs = textclasslist.Style(tclass,layout.second).
1166 layout = textclasslist.NumberOfLayout(tclass, obs);
1169 // see if we found the layout number:
1170 if (!layout.first) {
1171 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1172 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1176 if (cur_layout != layout.second) {
1177 cur_layout = layout.second;
1178 lt->setLayout(bv, layout.second);
1179 bv->owner()->setLayout(cpar(bv)->getLayout());
1180 updateLocal(bv, CURSOR_PAR, true);
1183 // reset the layout box
1184 bv->owner()->setLayout(cpar(bv)->getLayout());
1187 case LFUN_PARAGRAPH_SPACING:
1188 // This one is absolutely not working. When fiddling with this
1189 // it also seems to me that the paragraphs inside the insettext
1190 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1192 Paragraph * par = lt->cursor.par();
1193 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1194 float cur_value = 1.0;
1195 if (cur_spacing == Spacing::Other) {
1196 cur_value = par->params().spacing().getValue();
1199 istringstream istr(arg.c_str());
1202 Spacing::Space new_spacing = cur_spacing;
1203 float new_value = cur_value;
1205 lyxerr << "Missing argument to `paragraph-spacing'"
1207 } else if (tmp == "single") {
1208 new_spacing = Spacing::Single;
1209 } else if (tmp == "onehalf") {
1210 new_spacing = Spacing::Onehalf;
1211 } else if (tmp == "double") {
1212 new_spacing = Spacing::Double;
1213 } else if (tmp == "other") {
1214 new_spacing = Spacing::Other;
1217 lyxerr << "new_value = " << tmpval << endl;
1220 } else if (tmp == "default") {
1221 new_spacing = Spacing::Default;
1223 lyxerr << _("Unknown spacing argument: ")
1226 if (cur_spacing != new_spacing || cur_value != new_value) {
1227 par->params().spacing(Spacing(new_spacing, new_value));
1228 updateLocal(bv, CURSOR_PAR, true);
1234 if (!bv->Dispatch(action, arg))
1235 result = UNDISPATCHED;
1239 /// If the action has deleted all text in the inset, we need to change the
1240 // language to the language of the surronding text.
1241 if (par->size() == 0 && !par->next()) {
1242 LyXFont font(LyXFont::ALL_IGNORE);
1243 font.setLanguage(bv->getParentLanguage(this));
1244 setFont(bv, font, false);
1247 if (result != FINISHED) {
1248 showInsetCursor(bv);
1250 bv->unlockInset(this);
1257 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1260 buf->latexParagraphs(os, par, 0, texrow);
1261 return texrow.rows();
1265 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1267 Paragraph * p = par;
1268 unsigned int lines = 0;
1271 string const tmp = buf->asciiParagraph(p, linelen);
1272 lines += countChar(tmp, '\n');
1280 int InsetText::docBook(Buffer const * buf, ostream & os) const
1282 Paragraph * p = par;
1283 unsigned int lines = 0;
1288 buf->simpleDocBookOnePar(os, tmp, p, desc, 0);
1296 void InsetText::validate(LaTeXFeatures & features) const
1298 Paragraph * p = par;
1300 p->validate(features);
1306 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1308 if (textclasslist.Style(buf->params.textclass,
1309 p->getLayout()).labeltype != LABEL_MANUAL)
1312 return p->beginningOfMainBody();
1316 void InsetText::getCursorPos(BufferView * bv,
1317 int & x, int & y) const
1319 if (the_locking_inset) {
1320 the_locking_inset->getCursorPos(bv, x, y);
1328 unsigned int InsetText::insetInInsetY()
1330 if (!the_locking_inset)
1333 return (inset_y + the_locking_inset->insetInInsetY());
1337 void InsetText::toggleInsetCursor(BufferView * bv)
1339 if (the_locking_inset) {
1340 the_locking_inset->toggleInsetCursor(bv);
1344 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1346 int const asc = lyxfont::maxAscent(font);
1347 int const desc = lyxfont::maxDescent(font);
1349 if (isCursorVisible())
1350 bv->hideLockedInsetCursor();
1352 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1353 toggleCursorVisible();
1357 void InsetText::showInsetCursor(BufferView * bv, bool show)
1359 if (the_locking_inset) {
1360 the_locking_inset->showInsetCursor(bv, show);
1363 if (!isCursorVisible()) {
1364 LyXFont const font =
1365 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1367 int const asc = lyxfont::maxAscent(font);
1368 int const desc = lyxfont::maxDescent(font);
1370 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1372 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1373 setCursorVisible(true);
1378 void InsetText::hideInsetCursor(BufferView * bv)
1380 if (isCursorVisible()) {
1381 bv->hideLockedInsetCursor();
1382 setCursorVisible(false);
1384 if (the_locking_inset)
1385 the_locking_inset->hideInsetCursor(bv);
1389 void InsetText::fitInsetCursor(BufferView * bv) const
1391 if (the_locking_inset) {
1392 the_locking_inset->fitInsetCursor(bv);
1395 LyXFont const font =
1396 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1398 int const asc = lyxfont::maxAscent(font);
1399 int const desc = lyxfont::maxDescent(font);
1401 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1405 UpdatableInset::RESULT
1406 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1408 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1409 return moveLeftIntern(bv, false, activate_inset, selecting);
1411 return moveRightIntern(bv, false, activate_inset, selecting);
1415 UpdatableInset::RESULT
1416 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1418 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1419 return moveRightIntern(bv, true, activate_inset, selecting);
1421 return moveLeftIntern(bv, true, activate_inset, selecting);
1425 UpdatableInset::RESULT
1426 InsetText::moveRightIntern(BufferView * bv, bool behind,
1427 bool activate_inset, bool selecting)
1429 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1431 if (activate_inset && checkAndActivateInset(bv, behind))
1433 getLyXText(bv)->cursorRight(bv);
1435 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1436 return DISPATCHED_NOUPDATE;
1440 UpdatableInset::RESULT
1441 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1442 bool activate_inset, bool selecting)
1444 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1446 getLyXText(bv)->cursorLeft(bv);
1448 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1449 if (activate_inset && checkAndActivateInset(bv, behind))
1451 return DISPATCHED_NOUPDATE;
1455 UpdatableInset::RESULT
1456 InsetText::moveUp(BufferView * bv)
1458 if (!crow(bv)->previous())
1460 getLyXText(bv)->cursorUp(bv);
1461 return DISPATCHED_NOUPDATE;
1465 UpdatableInset::RESULT
1466 InsetText::moveDown(BufferView * bv)
1468 if (!crow(bv)->next())
1470 getLyXText(bv)->cursorDown(bv);
1471 return DISPATCHED_NOUPDATE;
1475 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1477 if (the_locking_inset) {
1478 if (the_locking_inset->insetAllowed(inset))
1479 return the_locking_inset->insertInset(bv, inset);
1482 inset->setOwner(this);
1483 hideInsetCursor(bv);
1487 lt = getLyXText(bv);
1490 lt->insertInset(bv, inset);
1491 if ((cpar(bv)->getChar(cpos(bv)) != Paragraph::META_INSET) ||
1492 (cpar(bv)->getInset(cpos(bv)) != inset))
1495 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1496 showInsetCursor(bv);
1503 bool InsetText::insetAllowed(Inset::Code code) const
1505 if (the_locking_inset)
1506 return the_locking_inset->insetAllowed(code);
1511 UpdatableInset * InsetText::getLockingInset() const
1513 return the_locking_inset ? the_locking_inset->getLockingInset() :
1514 const_cast<InsetText *>(this);
1518 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1522 if (the_locking_inset)
1523 return the_locking_inset->getFirstLockingInsetOfType(c);
1528 bool InsetText::showInsetDialog(BufferView * bv) const
1530 if (the_locking_inset)
1531 return the_locking_inset->showInsetDialog(bv);
1536 std::vector<string> const InsetText::getLabelList() const
1538 std::vector<string> label_list;
1540 Paragraph * tpar = par;
1542 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1543 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1544 for (; beg != end; ++beg) {
1545 std::vector<string> const l = (*beg)->getLabelList();
1546 label_list.insert(label_list.end(), l.begin(), l.end());
1548 tpar = tpar->next();
1554 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1557 if (the_locking_inset) {
1558 the_locking_inset->setFont(bv, font, toggleall, selectall);
1563 lt = getLyXText(bv);
1566 if (lt->selection.set()) {
1567 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1572 lt->toggleFree(bv, font, toggleall);
1574 lt->setFont(bv, font, toggleall);
1577 lt->clearSelection(bv);
1579 if (selectall || lt->selection.set())
1580 updateLocal(bv, FULL, true);
1582 updateLocal(bv, CURSOR_PAR, true);
1588 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1590 if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
1594 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1595 if (!inset || inset->editable() != Inset::HIGHLY_EDITABLE)
1597 LyXFont const font =
1598 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1600 x = inset->width(bv, font);
1601 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1604 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1606 //inset_x = cx(bv) - top_x + drawTextXOffset;
1607 //inset_y = cy(bv) + drawTextYOffset;
1608 inset->edit(bv, x, y, 0);
1609 if (!the_locking_inset)
1611 updateLocal(bv, CURSOR, false);
1618 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1621 x -= drawTextXOffset;
1623 int dummyy = y + insetAscent;
1624 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy, button);
1631 inset_x = cx(bv) - top_x + drawTextXOffset;
1632 inset_y = cy(bv) + drawTextYOffset;
1633 inset->edit(bv, x - inset_x, y - inset_y, button);
1634 if (!the_locking_inset)
1636 updateLocal(bv, CURSOR, false);
1643 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1645 int w = UpdatableInset::getMaxWidth(bv, inset);
1650 w = w - top_x + owner()->x();
1653 w -= (2 * TEXT_TO_INSET_OFFSET);
1655 // return w - (2*TEXT_TO_INSET_OFFSET);
1659 void InsetText::setParagraphData(Paragraph * p)
1663 // now also delete all caches this should be safe, hopefully
1667 Paragraph * tmp = par->next();
1672 par = new Paragraph(*p);
1673 par->setInsetOwner(this);
1674 Paragraph * np = par;
1677 np->next(new Paragraph(*p));
1678 np->next()->previous(np);
1680 np->setInsetOwner(this);
1687 void InsetText::setText(string const & data)
1690 LyXFont font(LyXFont::ALL_SANE);
1691 for (unsigned int i=0; i < data.length(); ++i)
1692 par->insertChar(i, data[i], font);
1696 void InsetText::setAutoBreakRows(bool flag)
1698 if (flag != autoBreakRows) {
1699 autoBreakRows = flag;
1707 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1709 if (how != drawFrame_) {
1712 updateLocal(bv, DRAW_FRAME, false);
1717 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1719 if (frame_color != col) {
1722 updateLocal(bv, DRAW_FRAME, false);
1727 int InsetText::cx(BufferView * bv) const
1731 lt = getLyXText(bv);
1734 int x = lt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1735 if (the_locking_inset) {
1736 LyXFont font = lt->getFont(bv->buffer(),
1739 if (font.isVisibleRightToLeft())
1740 x -= the_locking_inset->width(bv, font);
1748 int InsetText::cy(BufferView * bv) const
1751 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1755 Paragraph::size_type InsetText::cpos(BufferView * bv) const
1757 return getLyXText(bv)->cursor.pos();
1761 Paragraph * InsetText::cpar(BufferView * bv) const
1763 return getLyXText(bv)->cursor.par();
1767 bool InsetText::cboundary(BufferView * bv) const
1769 return getLyXText(bv)->cursor.boundary();
1773 Row * InsetText::crow(BufferView * bv) const
1775 return getLyXText(bv)->cursor.row();
1779 LyXText * InsetText::getLyXText(BufferView const * lbv,
1780 bool const recursive) const
1782 if (!recursive && (cached_bview == lbv))
1783 return cached_text.get();
1785 // Super UGLY! (Lgb)
1786 BufferView * bv = const_cast<BufferView *>(lbv);
1789 Cache::iterator it = cache.find(bv);
1791 if (it != cache.end() && (lt || !it->second.remove)) {
1792 lyx::Assert(it->second.text.get());
1794 cached_text = it->second.text;
1795 if (recursive && the_locking_inset) {
1796 return the_locking_inset->getLyXText(bv);
1798 return cached_text.get();
1799 } else if (it->second.remove) {
1801 saveLyXTextState(it->second.text.get());
1809 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
1810 cached_text->init(bv);
1811 restoreLyXTextState(bv, cached_text.get());
1813 cache.insert(make_pair(bv, cached_text));
1815 if (the_locking_inset && recursive) {
1816 return the_locking_inset->getLyXText(bv);
1818 return cached_text.get();
1822 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1826 Cache::iterator it = cache.find(bv);
1828 if (it == cache.end()) {
1832 lyx::Assert(it->second.text.get());
1834 it->second.remove = true;
1836 /// then remove all LyXText in text-insets
1837 Paragraph * p = par;
1838 for (; p; p = p->next()) {
1839 p->deleteInsetsLyXText(bv);
1845 void InsetText::resizeLyXText(BufferView * bv, bool force) const
1847 if (!par->next() && !par->size()) // no data, resize not neccessary!
1849 // one endless line, resize normally not necessary
1850 if (!force && getMaxWidth(bv, this) < 0)
1853 Cache::iterator it = cache.find(bv);
1854 if (it == cache.end()) {
1857 lyx::Assert(it->second.text.get());
1859 LyXText * t = it->second.text.get();
1860 saveLyXTextState(t);
1861 for (Paragraph * p = par; p; p = p->next()) {
1862 p->resizeInsetsLyXText(bv);
1865 restoreLyXTextState(bv, t);
1866 if (the_locking_inset) {
1867 inset_x = cx(bv) - top_x + drawTextXOffset;
1868 inset_y = cy(bv) + drawTextYOffset;
1872 t->first = bv->screen()->topCursorVisible(t);
1875 updateLocal(bv, FULL, false);
1878 // this will scroll the screen such that the cursor becomes visible
1879 bv->updateScrollbar();
1883 void InsetText::removeNewlines()
1885 for (Paragraph * p = par; p; p = p->next()) {
1886 for (int i = 0; i < p->size(); ++i) {
1887 if (p->getChar(i) == Paragraph::META_NEWLINE)
1894 bool InsetText::nodraw() const
1896 if (the_locking_inset)
1897 return the_locking_inset->nodraw();
1898 return UpdatableInset::nodraw();
1902 int InsetText::scroll(bool recursive) const
1904 int sx = UpdatableInset::scroll(false);
1906 if (recursive && the_locking_inset)
1907 sx += the_locking_inset->scroll(recursive);
1913 bool InsetText::doClearArea() const
1915 return !locked || (need_update & (FULL|INIT));
1919 void InsetText::selectAll(BufferView * bv)
1921 getLyXText(bv)->cursorTop(bv);
1922 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1923 getLyXText(bv)->cursorBottom(bv);
1924 getLyXText(bv)->setSelection(bv);
1928 void InsetText::clearSelection(BufferView * bv)
1930 getLyXText(bv)->clearSelection(bv);
1934 void InsetText::clearInset(Painter & pain, int baseline, bool & cleared) const
1937 int h = insetAscent + insetDescent;
1938 int ty = baseline - insetAscent;
1944 if ((ty + h) > pain.paperHeight())
1945 h = pain.paperHeight();
1946 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
1947 w = pain.paperWidth();
1948 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h,
1955 Paragraph * InsetText::getParFromID(int id) const
1957 Paragraph * result = par;
1958 Paragraph * ires = 0;
1959 while (result && result->id() != id) {
1960 if ((ires = result->getParFromID(id)))
1962 result = result->next();
1968 Paragraph * InsetText::firstParagraph() const
1971 if (the_locking_inset)
1972 if ((result = the_locking_inset->firstParagraph()))
1978 LyXCursor const & InsetText::cursor(BufferView * bv) const
1980 if (the_locking_inset)
1981 return the_locking_inset->cursor(bv);
1982 return getLyXText(bv)->cursor;
1986 Paragraph * InsetText::paragraph() const
1992 void InsetText::paragraph(Paragraph * p)
1996 // we now have to update/redraw all instances
1997 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
2002 // redraw myself when asked for
2003 need_update |= INIT;
2007 Inset * InsetText::getInsetFromID(int id_arg) const
2010 return const_cast<InsetText *>(this);
2012 Paragraph * lp = par;
2015 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2016 en = lp->inset_iterator_end();
2019 if ((*it)->id() == id_arg)
2021 Inset * in = (*it)->getInsetFromID(id_arg);
2031 string InsetText::selectNextWord(BufferView * bv, float & value) const
2037 lt = getLyXText(bv);
2040 if (the_locking_inset) {
2041 str = the_locking_inset->selectNextWord(bv, value);
2048 #warning Dekel please have a look on this one RTL? (Jug)
2049 // we have to go on checking so move cusor to the right
2050 lt->cursor.pos(lt->cursor.pos() + 1);
2052 str = lt->selectNextWord(bv, value);
2054 bv->unlockInset(const_cast<InsetText *>(this));
2063 void InsetText::selectSelectedWord(BufferView * bv)
2065 if (the_locking_inset) {
2066 the_locking_inset->selectSelectedWord(bv);
2069 getLyXText(bv)->selectSelectedWord(bv);
2070 updateLocal(bv, SELECTION, false);
2074 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2076 if (the_locking_inset) {
2077 the_locking_inset->toggleSelection(bv, kill_selection);
2081 lt = getLyXText(bv);
2085 int x = top_x + TEXT_TO_INSET_OFFSET;
2088 Row * row = lt->getRowNearY(y);
2089 int y_offset = top_baseline - row->ascent_of_text();
2091 while ((row != 0) && ((y+row->height()) <= 0)) {
2098 if (need_update & SELECTION)
2100 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2106 bool InsetText::searchForward(BufferView * bv, string const & str,
2107 bool const & cs, bool const & mw)
2109 if (the_locking_inset)
2110 if (the_locking_inset->searchForward(bv, str, cs, mw))
2112 if (LyXFind(bv, str, true, true, cs , mw)) {
2115 // we have to unlock ourself in this function by default!
2116 bv->unlockInset(const_cast<InsetText *>(this));
2120 bool InsetText::searchBackward(BufferView * bv, string const & str,
2121 bool const & cs, bool const & mw)
2123 if (the_locking_inset)
2124 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2126 if (LyXFind(bv, str, false, true, cs, mw)) {
2129 // we have to unlock ourself in this function by default!
2130 bv->unlockInset(const_cast<InsetText *>(this));