1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
16 #include "paragraph.h"
17 #include "funcrequest.h"
18 #include "frontends/LyXView.h"
19 #include "undo_funcs.h"
21 #include "buffer_funcs.h"
22 #include "bufferparams.h"
23 #include "errorlist.h"
25 #include "BufferView.h"
26 #include "CutAndPaste.h"
27 #include "frontends/Painter.h"
28 #include "frontends/font_metrics.h"
32 #include "FloatList.h"
34 #include "ParagraphParameters.h"
36 #include "lyxrow_funcs.h"
37 #include "paragraph_funcs.h"
39 #include "insets/insetbibitem.h"
40 #include "insets/insetenv.h"
41 #include "insets/insetfloat.h"
42 #include "insets/insetwrap.h"
44 #include "support/LAssert.h"
45 #include "support/textutils.h"
46 #include "support/lstrings.h"
48 #include <boost/tuple/tuple.hpp>
58 LyXText::LyXText(BufferView * bv)
59 : height(0), width(0), anchor_row_offset_(0),
60 inset_owner(0), the_locking_inset(0), bv_owner(bv)
62 anchor_row_ = rows().end();
63 need_break_row = rows().end();
64 refresh_row = rows().end();
70 LyXText::LyXText(BufferView * bv, InsetText * inset)
71 : height(0), width(0), anchor_row_offset_(0),
72 inset_owner(inset), the_locking_inset(0), bv_owner(bv)
74 anchor_row_ = rows().end();
75 need_break_row = rows().end();
76 refresh_row = rows().end();
82 void LyXText::init(BufferView * bview, bool reinit)
86 need_break_row = rows().end();
90 } else if (!rowlist_.empty())
93 ParagraphList::iterator pit = ownerParagraphs().begin();
94 ParagraphList::iterator end = ownerParagraphs().end();
96 current_font = getFont(bview->buffer(), pit, 0);
98 for (; pit != end; ++pit) {
99 insertParagraph(pit, rowlist_.end());
101 setCursorIntern(rowlist_.begin()->par(), 0);
102 selection.cursor = cursor;
108 // Gets the fully instantiated font at a given position in a paragraph
109 // Basically the same routine as Paragraph::getFont() in paragraph.C.
110 // The difference is that this one is used for displaying, and thus we
111 // are allowed to make cosmetic improvements. For instance make footnotes
113 // If position is -1, we get the layout font of the paragraph.
114 // If position is -2, we get the font of the manual label of the paragraph.
115 LyXFont const LyXText::getFont(Buffer const * buf, ParagraphList::iterator pit,
118 lyx::Assert(pos >= 0);
120 LyXLayout_ptr const & layout = pit->layout();
122 // We specialize the 95% common case:
123 if (!pit->getDepth()) {
124 if (layout->labeltype == LABEL_MANUAL
125 && pos < pit->beginningOfBody()) {
127 LyXFont f = pit->getFontSettings(buf->params, pos);
129 pit->inInset()->getDrawFont(f);
130 return f.realize(layout->reslabelfont);
132 LyXFont f = pit->getFontSettings(buf->params, pos);
134 pit->inInset()->getDrawFont(f);
135 return f.realize(layout->resfont);
139 // The uncommon case need not be optimized as much
143 if (pos < pit->beginningOfBody()) {
145 layoutfont = layout->labelfont;
148 layoutfont = layout->font;
151 LyXFont tmpfont = pit->getFontSettings(buf->params, pos);
152 tmpfont.realize(layoutfont);
155 pit->inInset()->getDrawFont(tmpfont);
157 // Realize with the fonts of lesser depth.
158 tmpfont.realize(outerFont(pit, ownerParagraphs()));
160 return realizeFont(tmpfont, buf->params);
164 LyXFont const LyXText::getLayoutFont(Buffer const * buf,
165 ParagraphList::iterator pit) const
167 LyXLayout_ptr const & layout = pit->layout();
169 if (!pit->getDepth()) {
170 return layout->resfont;
173 LyXFont font(layout->font);
174 // Realize with the fonts of lesser depth.
175 font.realize(outerFont(pit, ownerParagraphs()));
177 return realizeFont(font, buf->params);
181 LyXFont const LyXText::getLabelFont(Buffer const * buf,
182 ParagraphList::iterator pit) const
184 LyXLayout_ptr const & layout = pit->layout();
186 if (!pit->getDepth()) {
187 return layout->reslabelfont;
190 LyXFont font(layout->labelfont);
191 // Realize with the fonts of lesser depth.
192 font.realize(outerFont(pit, ownerParagraphs()));
194 return realizeFont(layout->labelfont, buf->params);
198 void LyXText::setCharFont(ParagraphList::iterator pit,
199 pos_type pos, LyXFont const & fnt,
202 Buffer const * buf = bv()->buffer();
203 LyXFont font = getFont(buf, pit, pos);
204 font.update(fnt, buf->params.language, toggleall);
205 // Let the insets convert their font
206 if (pit->isInset(pos)) {
207 Inset * inset = pit->getInset(pos);
208 if (isEditableInset(inset)) {
209 UpdatableInset * uinset =
210 static_cast<UpdatableInset *>(inset);
211 uinset->setFont(bv(), fnt, toggleall, true);
215 // Plug thru to version below:
216 setCharFont(buf, pit, pos, font);
220 void LyXText::setCharFont(Buffer const * buf, ParagraphList::iterator pit,
221 pos_type pos, LyXFont const & fnt)
225 LyXTextClass const & tclass = buf->params.getLyXTextClass();
226 LyXLayout_ptr const & layout = pit->layout();
228 // Get concrete layout font to reduce against
231 if (pos < pit->beginningOfBody())
232 layoutfont = layout->labelfont;
234 layoutfont = layout->font;
236 // Realize against environment font information
237 if (pit->getDepth()) {
238 ParagraphList::iterator tp = pit;
239 while (!layoutfont.resolved() &&
240 tp != ownerParagraphs().end() &&
242 tp = outerHook(tp, ownerParagraphs());
243 if (tp != ownerParagraphs().end())
244 layoutfont.realize(tp->layout()->font);
248 layoutfont.realize(tclass.defaultfont());
250 // Now, reduce font against full layout font
251 font.reduce(layoutfont);
253 pit->setFont(pos, font);
257 // removes the row and reset the touched counters
258 void LyXText::removeRow(RowList::iterator rit)
260 /* FIXME: when we cache the bview, this should just
261 * become a postPaint(), I think */
262 if (refresh_row == rit) {
263 if (rit == rows().begin())
264 refresh_row = boost::next(rit);
266 refresh_row = boost::prior(rit);
268 // what about refresh_y
271 if (anchor_row_ == rit) {
272 if (rit != rows().begin()) {
273 anchor_row_ = boost::prior(rit);
274 anchor_row_offset_ += anchor_row_->height();
276 anchor_row_ = boost::next(rit);
277 anchor_row_offset_ -= rit->height();
281 // the text becomes smaller
282 height -= rit->height();
288 // remove all following rows of the paragraph of the specified row.
289 void LyXText::removeParagraph(RowList::iterator rit)
291 ParagraphList::iterator tmppit = rit->par();
294 while (rit != rows().end() && rit->par() == tmppit) {
295 RowList::iterator tmprit = boost::next(rit);
302 void LyXText::insertParagraph(ParagraphList::iterator pit,
303 RowList::iterator rowit)
305 // insert a new row, starting at position 0
307 RowList::iterator rit = rowlist_.insert(rowit, newrow);
309 // and now append the whole paragraph before the new row
310 appendParagraph(rit);
314 Inset * LyXText::getInset() const
316 ParagraphList::iterator pit = cursor.par();
317 pos_type const pos = cursor.pos();
319 if (pos < pit->size() && pit->isInset(pos)) {
320 return pit->getInset(pos);
326 void LyXText::toggleInset()
328 Inset * inset = getInset();
329 // is there an editable inset at cursor position?
330 if (!isEditableInset(inset)) {
331 // No, try to see if we are inside a collapsable inset
332 if (inset_owner && inset_owner->owner()
333 && inset_owner->owner()->isOpen()) {
334 bv()->unlockInset(inset_owner->owner());
335 inset_owner->owner()->close(bv());
336 bv()->getLyXText()->cursorRight(bv());
340 //bv()->owner()->message(inset->editMessage());
342 // do we want to keep this?? (JMarc)
343 if (!isHighlyEditableInset(inset))
344 setCursorParUndo(bv());
346 if (inset->isOpen()) {
352 bv()->updateInset(inset);
356 /* used in setlayout */
357 // Asger is not sure we want to do this...
358 void LyXText::makeFontEntriesLayoutSpecific(Buffer const & buf,
361 LyXLayout_ptr const & layout = par.layout();
362 pos_type const psize = par.size();
365 for (pos_type pos = 0; pos < psize; ++pos) {
366 if (pos < par.beginningOfBody())
367 layoutfont = layout->labelfont;
369 layoutfont = layout->font;
371 LyXFont tmpfont = par.getFontSettings(buf.params, pos);
372 tmpfont.reduce(layoutfont);
373 par.setFont(pos, tmpfont);
378 ParagraphList::iterator
379 LyXText::setLayout(LyXCursor & cur, LyXCursor & sstart_cur,
380 LyXCursor & send_cur,
381 string const & layout)
383 ParagraphList::iterator endpit = boost::next(send_cur.par());
384 ParagraphList::iterator undoendpit = endpit;
385 ParagraphList::iterator pars_end = ownerParagraphs().end();
387 if (endpit != pars_end && endpit->getDepth()) {
388 while (endpit != pars_end && endpit->getDepth()) {
392 } else if (endpit != pars_end) {
393 // because of parindents etc.
397 setUndo(bv(), Undo::EDIT, sstart_cur.par(), boost::prior(undoendpit));
399 // ok we have a selection. This is always between sstart_cur
400 // and sel_end cursor
402 ParagraphList::iterator pit = sstart_cur.par();
403 ParagraphList::iterator epit = boost::next(send_cur.par());
405 LyXLayout_ptr const & lyxlayout =
406 bv()->buffer()->params.getLyXTextClass()[layout];
409 pit->applyLayout(lyxlayout);
410 makeFontEntriesLayoutSpecific(*bv()->buffer(), *pit);
411 ParagraphList::iterator fppit = pit;
412 fppit->params().spaceTop(lyxlayout->fill_top ?
413 VSpace(VSpace::VFILL)
414 : VSpace(VSpace::NONE));
415 fppit->params().spaceBottom(lyxlayout->fill_bottom ?
416 VSpace(VSpace::VFILL)
417 : VSpace(VSpace::NONE));
418 if (lyxlayout->margintype == MARGIN_MANUAL)
419 pit->setLabelWidthString(lyxlayout->labelstring());
422 } while (pit != epit);
428 // set layout over selection and make a total rebreak of those paragraphs
429 void LyXText::setLayout(string const & layout)
431 LyXCursor tmpcursor = cursor; // store the current cursor
433 // if there is no selection just set the layout
434 // of the current paragraph
435 if (!selection.set()) {
436 selection.start = cursor; // dummy selection
437 selection.end = cursor;
440 // special handling of new environment insets
441 BufferParams const & params = bv()->buffer()->params;
442 LyXLayout_ptr const & lyxlayout = params.getLyXTextClass()[layout];
443 if (lyxlayout->is_environment) {
444 // move everything in a new environment inset
445 lyxerr << "setting layout " << layout << endl;
446 bv()->owner()->dispatch(FuncRequest(LFUN_HOME));
447 bv()->owner()->dispatch(FuncRequest(LFUN_ENDSEL));
448 bv()->owner()->dispatch(FuncRequest(LFUN_CUT));
449 Inset * inset = new InsetEnvironment(params, layout);
450 if (bv()->insertInset(inset)) {
452 //bv()->owner()->dispatch(FuncRequest(LFUN_PASTE));
459 ParagraphList::iterator endpit = setLayout(cursor, selection.start,
460 selection.end, layout);
461 redoParagraphs(selection.start, endpit);
463 // we have to reset the selection, because the
464 // geometry could have changed
465 setCursor(selection.start.par(), selection.start.pos(), false);
466 selection.cursor = cursor;
467 setCursor(selection.end.par(), selection.end.pos(), false);
471 setCursor(tmpcursor.par(), tmpcursor.pos(), true);
475 bool LyXText::changeDepth(bv_funcs::DEPTH_CHANGE type, bool test_only)
477 ParagraphList::iterator pit(cursor.par());
478 ParagraphList::iterator end(cursor.par());
479 ParagraphList::iterator start = pit;
481 if (selection.set()) {
482 pit = selection.start.par();
483 end = selection.end.par();
487 ParagraphList::iterator pastend = boost::next(end);
490 setUndo(bv(), Undo::EDIT, start, end);
492 bool changed = false;
494 int prev_after_depth = 0;
495 #warning parlist ... could be nicer ?
496 if (start != ownerParagraphs().begin()) {
497 prev_after_depth = boost::prior(start)->getMaxDepthAfter();
501 int const depth = pit->params().depth();
502 if (type == bv_funcs::INC_DEPTH) {
503 if (depth < prev_after_depth
504 && pit->layout()->labeltype != LABEL_BIBLIO) {
507 pit->params().depth(depth + 1);
514 pit->params().depth(depth - 1);
517 prev_after_depth = pit->getMaxDepthAfter();
529 // Wow, redoParagraphs is stupid.
531 setCursor(tmpcursor, start, 0);
533 redoParagraphs(tmpcursor, pastend);
535 // We need to actually move the text->cursor. I don't
536 // understand why ...
539 // we have to reset the visual selection because the
540 // geometry could have changed
541 if (selection.set()) {
542 setCursor(selection.start.par(), selection.start.pos());
543 selection.cursor = cursor;
544 setCursor(selection.end.par(), selection.end.pos());
547 // this handles the counter labels, and also fixes up
548 // depth values for follow-on (child) paragraphs
552 setCursor(tmpcursor.par(), tmpcursor.pos());
558 // set font over selection and make a total rebreak of those paragraphs
559 void LyXText::setFont(LyXFont const & font, bool toggleall)
561 // if there is no selection just set the current_font
562 if (!selection.set()) {
563 // Determine basis font
565 if (cursor.pos() < cursor.par()->beginningOfBody()) {
566 layoutfont = getLabelFont(bv()->buffer(),
569 layoutfont = getLayoutFont(bv()->buffer(),
572 // Update current font
573 real_current_font.update(font,
574 bv()->buffer()->params.language,
577 // Reduce to implicit settings
578 current_font = real_current_font;
579 current_font.reduce(layoutfont);
580 // And resolve it completely
581 real_current_font.realize(layoutfont);
586 LyXCursor tmpcursor = cursor; // store the current cursor
588 // ok we have a selection. This is always between sel_start_cursor
589 // and sel_end cursor
591 setUndo(bv(), Undo::EDIT, selection.start.par(), selection.end.par());
593 cursor = selection.start;
594 while (cursor.par() != selection.end.par() ||
595 cursor.pos() < selection.end.pos())
597 if (cursor.pos() < cursor.par()->size()) {
598 // an open footnote should behave like a closed one
599 setCharFont(cursor.par(), cursor.pos(),
601 cursor.pos(cursor.pos() + 1);
604 cursor.par(boost::next(cursor.par()));
609 redoParagraphs(selection.start, boost::next(selection.end.par()));
611 // we have to reset the selection, because the
612 // geometry could have changed, but we keep
613 // it for user convenience
614 setCursor(selection.start.par(), selection.start.pos());
615 selection.cursor = cursor;
616 setCursor(selection.end.par(), selection.end.pos());
618 setCursor(tmpcursor.par(), tmpcursor.pos(), true,
619 tmpcursor.boundary());
623 void LyXText::redoHeightOfParagraph()
625 RowList::iterator tmprow = cursorRow();
626 int y = cursor.y() - tmprow->baseline();
628 setHeightOfRow(tmprow);
630 while (tmprow != rows().begin()
631 && boost::prior(tmprow)->par() == tmprow->par()) {
633 y -= tmprow->height();
634 setHeightOfRow(tmprow);
639 setCursor(cursor.par(), cursor.pos(), false, cursor.boundary());
643 void LyXText::redoDrawingOfParagraph(LyXCursor const & cur)
645 RowList::iterator tmprow = getRow(cur);
647 int y = cur.y() - tmprow->baseline();
648 setHeightOfRow(tmprow);
650 while (tmprow != rows().begin()
651 && boost::prior(tmprow)->par() == tmprow->par()) {
653 y -= tmprow->height();
657 setCursor(cur.par(), cur.pos());
661 // deletes and inserts again all paragraphs between the cursor
662 // and the specified par
663 // This function is needed after SetLayout and SetFont etc.
664 void LyXText::redoParagraphs(LyXCursor const & cur,
665 ParagraphList::iterator endpit)
667 RowList::iterator tmprit = getRow(cur);
668 int y = cur.y() - tmprit->baseline();
670 ParagraphList::iterator first_phys_pit;
671 RowList::iterator prevrit;
672 if (tmprit == rows().begin()) {
673 // A trick/hack for UNDO.
674 // This is needed because in an UNDO/REDO we could have
675 // changed the ownerParagraph() so the paragraph inside
676 // the row is NOT my really first par anymore.
677 // Got it Lars ;) (Jug 20011206)
678 first_phys_pit = ownerParagraphs().begin();
679 prevrit = rows().end();
681 first_phys_pit = tmprit->par();
682 while (tmprit != rows().begin()
683 && boost::prior(tmprit)->par() == first_phys_pit)
686 y -= tmprit->height();
688 prevrit = boost::prior(tmprit);
692 while (tmprit != rows().end() && tmprit->par() != endpit) {
693 RowList::iterator tmprit2 = tmprit++;
697 // Reinsert the paragraphs.
698 ParagraphList::iterator tmppit = first_phys_pit;
700 while (tmppit != ownerParagraphs().end()) {
701 insertParagraph(tmppit, tmprit);
702 while (tmprit != rows().end()
703 && tmprit->par() == tmppit) {
707 if (tmppit == endpit)
710 if (prevrit != rows().end()) {
711 setHeightOfRow(prevrit);
712 postPaint(y - prevrit->height());
714 setHeightOfRow(rows().begin());
717 if (tmprit != rows().end())
718 setHeightOfRow(tmprit);
724 void LyXText::fullRebreak()
726 need_break_row = rows().begin();
728 setCursorIntern(cursor.par(), cursor.pos());
732 void LyXText::partialRebreak()
734 if (rows().empty()) {
739 RowList::iterator rows_end = rows().end();
741 if (need_break_row != rows_end) {
742 breakAgain(need_break_row);
743 need_break_row = rows_end;
749 // important for the screen
752 // the cursor set functions have a special mechanism. When they
753 // realize, that you left an empty paragraph, they will delete it.
754 // They also delete the corresponding row
756 // need the selection cursor:
757 void LyXText::setSelection()
759 bool const lsel = TextCursor::setSelection();
761 if (inset_owner && (selection.set() || lsel))
762 inset_owner->setUpdateStatus(bv(), InsetText::SELECTION);
767 void LyXText::clearSelection()
769 TextCursor::clearSelection();
771 // reset this in the bv_owner!
772 if (bv_owner && bv_owner->text)
773 bv_owner->text->xsel_cache.set(false);
777 void LyXText::cursorHome()
779 setCursor(cursor.par(), cursorRow()->pos());
783 void LyXText::cursorEnd()
785 if (cursor.par()->empty())
788 RowList::iterator rit = cursorRow();
789 RowList::iterator next_rit = boost::next(rit);
790 ParagraphList::iterator pit = rit->par();
791 pos_type last_pos = lastPos(*this, rit);
793 if (next_rit == rows().end() || next_rit->par() != pit) {
797 (pit->getChar(last_pos) != ' ' && !pit->isNewline(last_pos))) {
802 setCursor(pit, last_pos);
806 void LyXText::cursorTop()
808 setCursor(ownerParagraphs().begin(), 0);
812 void LyXText::cursorBottom()
814 ParagraphList::iterator lastpit =
815 boost::prior(ownerParagraphs().end());
816 setCursor(lastpit, lastpit->size());
820 void LyXText::toggleFree(LyXFont const & font, bool toggleall)
822 // If the mask is completely neutral, tell user
823 if (font == LyXFont(LyXFont::ALL_IGNORE)) {
824 // Could only happen with user style
825 bv()->owner()->message(_("No font change defined. Use Character under the Layout menu to define font change."));
829 // Try implicit word selection
830 // If there is a change in the language the implicit word selection
832 LyXCursor resetCursor = cursor;
833 bool implicitSelection = (font.language() == ignore_language
834 && font.number() == LyXFont::IGNORE)
835 ? selectWordWhenUnderCursor(WHOLE_WORD_STRICT) : false;
838 setFont(font, toggleall);
840 // Implicit selections are cleared afterwards
841 //and cursor is set to the original position.
842 if (implicitSelection) {
844 cursor = resetCursor;
845 setCursor(cursor.par(), cursor.pos());
846 selection.cursor = cursor;
849 inset_owner->setUpdateStatus(bv(), InsetText::CURSOR_PAR);
853 string LyXText::getStringToIndex()
855 // Try implicit word selection
856 // If there is a change in the language the implicit word selection
858 LyXCursor const reset_cursor = cursor;
859 bool const implicitSelection = selectWordWhenUnderCursor(PREVIOUS_WORD);
862 if (!selection.set())
863 bv()->owner()->message(_("Nothing to index!"));
864 else if (selection.start.par() != selection.end.par())
865 bv()->owner()->message(_("Cannot index more than one paragraph!"));
867 idxstring = selectionAsString(bv()->buffer(), false);
869 // Reset cursors to their original position.
870 cursor = reset_cursor;
871 setCursor(cursor.par(), cursor.pos());
872 selection.cursor = cursor;
874 // Clear the implicit selection.
875 if (implicitSelection)
882 // the DTP switches for paragraphs. LyX will store them in the first
883 // physicla paragraph. When a paragraph is broken, the top settings rest,
884 // the bottom settings are given to the new one. So I can make shure,
885 // they do not duplicate themself and you cannnot make dirty things with
888 void LyXText::setParagraph(bool line_top, bool line_bottom,
889 bool pagebreak_top, bool pagebreak_bottom,
890 VSpace const & space_top,
891 VSpace const & space_bottom,
892 Spacing const & spacing,
894 string const & labelwidthstring,
897 LyXCursor tmpcursor = cursor;
898 if (!selection.set()) {
899 selection.start = cursor;
900 selection.end = cursor;
903 // make sure that the depth behind the selection are restored, too
904 ParagraphList::iterator endpit = boost::next(selection.end.par());
905 ParagraphList::iterator undoendpit = endpit;
906 ParagraphList::iterator pars_end = ownerParagraphs().end();
908 if (endpit != pars_end && endpit->getDepth()) {
909 while (endpit != pars_end && endpit->getDepth()) {
913 } else if (endpit != pars_end) {
914 // because of parindents etc.
918 setUndo(bv(), Undo::EDIT, selection.start.par(),
919 boost::prior(undoendpit));
922 ParagraphList::iterator tmppit = selection.end.par();
924 while (tmppit != boost::prior(selection.start.par())) {
925 setCursor(tmppit, 0);
926 postPaint(cursor.y() - cursorRow()->baseline());
928 ParagraphList::iterator pit = cursor.par();
929 ParagraphParameters & params = pit->params();
931 params.lineTop(line_top);
932 params.lineBottom(line_bottom);
933 params.pagebreakTop(pagebreak_top);
934 params.pagebreakBottom(pagebreak_bottom);
935 params.spaceTop(space_top);
936 params.spaceBottom(space_bottom);
937 params.spacing(spacing);
938 // does the layout allow the new alignment?
939 LyXLayout_ptr const & layout = pit->layout();
941 if (align == LYX_ALIGN_LAYOUT)
942 align = layout->align;
943 if (align & layout->alignpossible) {
944 if (align == layout->align)
945 params.align(LYX_ALIGN_LAYOUT);
949 pit->setLabelWidthString(labelwidthstring);
950 params.noindent(noindent);
951 tmppit = boost::prior(pit);
954 redoParagraphs(selection.start, endpit);
957 setCursor(selection.start.par(), selection.start.pos());
958 selection.cursor = cursor;
959 setCursor(selection.end.par(), selection.end.pos());
961 setCursor(tmpcursor.par(), tmpcursor.pos());
963 bv()->updateInset(inset_owner);
967 // set the counter of a paragraph. This includes the labels
968 void LyXText::setCounter(Buffer const * buf, ParagraphList::iterator pit)
970 LyXTextClass const & textclass = buf->params.getLyXTextClass();
971 LyXLayout_ptr const & layout = pit->layout();
973 if (pit != ownerParagraphs().begin()) {
975 pit->params().appendix(boost::prior(pit)->params().appendix());
976 if (!pit->params().appendix() &&
977 pit->params().startOfAppendix()) {
978 pit->params().appendix(true);
979 textclass.counters().reset();
981 pit->enumdepth = boost::prior(pit)->enumdepth;
982 pit->itemdepth = boost::prior(pit)->itemdepth;
984 pit->params().appendix(pit->params().startOfAppendix());
989 /* Maybe we have to increment the enumeration depth.
990 * BUT, enumeration in a footnote is considered in isolation from its
991 * surrounding paragraph so don't increment if this is the
992 * first line of the footnote
993 * AND, bibliographies can't have their depth changed ie. they
994 * are always of depth 0
996 if (pit != ownerParagraphs().begin()
997 && boost::prior(pit)->getDepth() < pit->getDepth()
998 && boost::prior(pit)->layout()->labeltype == LABEL_COUNTER_ENUMI
999 && pit->enumdepth < 3
1000 && layout->labeltype != LABEL_BIBLIO) {
1004 // Maybe we have to decrement the enumeration depth, see note above
1005 if (pit != ownerParagraphs().begin()
1006 && boost::prior(pit)->getDepth() > pit->getDepth()
1007 && layout->labeltype != LABEL_BIBLIO) {
1008 pit->enumdepth = depthHook(pit, ownerParagraphs(),
1009 pit->getDepth())->enumdepth;
1012 if (!pit->params().labelString().empty()) {
1013 pit->params().labelString(string());
1016 if (layout->margintype == MARGIN_MANUAL) {
1017 if (pit->params().labelWidthString().empty()) {
1018 pit->setLabelWidthString(layout->labelstring());
1021 pit->setLabelWidthString(string());
1024 // is it a layout that has an automatic label?
1025 if (layout->labeltype >= LABEL_COUNTER_CHAPTER) {
1026 int const i = layout->labeltype - LABEL_COUNTER_CHAPTER;
1030 if (i >= 0 && i <= buf->params.secnumdepth) {
1034 textclass.counters().step(layout->latexname());
1036 // Is there a label? Useful for Chapter layout
1037 if (!pit->params().appendix()) {
1038 s << buf->B_(layout->labelstring());
1040 s << buf->B_(layout->labelstring_appendix());
1043 // Use of an integer is here less than elegant. For now.
1044 int head = textclass.maxcounter() - LABEL_COUNTER_CHAPTER;
1045 if (!pit->params().appendix()) {
1046 numbertype = "sectioning";
1048 numbertype = "appendix";
1049 if (pit->isRightToLeftPar(buf->params))
1050 langtype = "hebrew";
1056 << textclass.counters()
1057 .numberLabel(layout->latexname(),
1058 numbertype, langtype, head);
1060 pit->params().labelString(STRCONV(s.str()));
1062 // reset enum counters
1063 textclass.counters().reset("enum");
1064 } else if (layout->labeltype < LABEL_COUNTER_ENUMI) {
1065 textclass.counters().reset("enum");
1066 } else if (layout->labeltype == LABEL_COUNTER_ENUMI) {
1068 // Yes I know this is a really, really! bad solution
1070 string enumcounter("enum");
1072 switch (pit->enumdepth) {
1081 enumcounter += "iv";
1084 // not a valid enumdepth...
1088 textclass.counters().step(enumcounter);
1090 s << textclass.counters()
1091 .numberLabel(enumcounter, "enumeration");
1092 pit->params().labelString(STRCONV(s.str()));
1094 } else if (layout->labeltype == LABEL_BIBLIO) {// ale970302
1095 textclass.counters().step("bibitem");
1096 int number = textclass.counters().value("bibitem");
1097 if (pit->bibitem()) {
1098 pit->bibitem()->setCounter(number);
1099 pit->params().labelString(layout->labelstring());
1101 // In biblio should't be following counters but...
1103 string s = buf->B_(layout->labelstring());
1105 // the caption hack:
1106 if (layout->labeltype == LABEL_SENSITIVE) {
1107 ParagraphList::iterator tmppit = pit;
1110 while (tmppit != ownerParagraphs().end() &&
1112 // the single '=' is intended below
1113 && (in = tmppit->inInset()->owner())) {
1114 if (in->lyxCode() == Inset::FLOAT_CODE ||
1115 in->lyxCode() == Inset::WRAP_CODE) {
1119 tmppit = std::find(ownerParagraphs().begin(), ownerParagraphs().end(), *in->parOwner());
1126 if (in->lyxCode() == Inset::FLOAT_CODE)
1127 type = static_cast<InsetFloat*>(in)->params().type;
1128 else if (in->lyxCode() == Inset::WRAP_CODE)
1129 type = static_cast<InsetWrap*>(in)->params().type;
1133 Floating const & fl = textclass.floats().getType(type);
1135 textclass.counters().step(fl.type());
1137 // Doesn't work... yet.
1138 s = bformat(_("%1$s #:"), buf->B_(fl.name()));
1140 // par->SetLayout(0);
1141 // s = layout->labelstring;
1142 s = _("Senseless: ");
1145 pit->params().labelString(s);
1147 // reset the enumeration counter. They are always reset
1148 // when there is any other layout between
1149 // Just fall-through between the cases so that all
1150 // enum counters deeper than enumdepth is also reset.
1151 switch (pit->enumdepth) {
1153 textclass.counters().reset("enumi");
1155 textclass.counters().reset("enumii");
1157 textclass.counters().reset("enumiii");
1159 textclass.counters().reset("enumiv");
1165 // Updates all counters. Paragraphs with changed label string will be rebroken
1166 void LyXText::updateCounters()
1168 RowList::iterator rowit = rows().begin();
1169 ParagraphList::iterator pit = rowit->par();
1171 // CHECK if this is really needed. (Lgb)
1172 bv()->buffer()->params.getLyXTextClass().counters().reset();
1174 ParagraphList::iterator beg = ownerParagraphs().begin();
1175 ParagraphList::iterator end = ownerParagraphs().end();
1176 for (; pit != end; ++pit) {
1177 while (rowit->par() != pit)
1180 string const oldLabel = pit->params().labelString();
1182 size_t maxdepth = 0;
1184 maxdepth = boost::prior(pit)->getMaxDepthAfter();
1186 if (pit->params().depth() > maxdepth)
1187 pit->params().depth(maxdepth);
1189 // setCounter can potentially change the labelString.
1190 setCounter(bv()->buffer(), pit);
1192 string const & newLabel = pit->params().labelString();
1194 if (oldLabel.empty() && !newLabel.empty()) {
1195 removeParagraph(rowit);
1196 appendParagraph(rowit);
1202 void LyXText::insertInset(Inset * inset)
1204 if (!cursor.par()->insetAllowed(inset->lyxCode()))
1206 setUndo(bv(), Undo::FINISH, cursor.par());
1208 cursor.par()->insertInset(cursor.pos(), inset);
1209 // Just to rebreak and refresh correctly.
1210 // The character will not be inserted a second time
1211 insertChar(Paragraph::META_INSET);
1212 // If we enter a highly editable inset the cursor should be to before
1213 // the inset. This couldn't happen before as Undo was not handled inside
1214 // inset now after the Undo LyX tries to call inset->Edit(...) again
1215 // and cannot do this as the cursor is behind the inset and GetInset
1216 // does not return the inset!
1217 if (isHighlyEditableInset(inset)) {
1224 void LyXText::cutSelection(bool doclear, bool realcut)
1226 // Stuff what we got on the clipboard. Even if there is no selection.
1228 // There is a problem with having the stuffing here in that the
1229 // larger the selection the slower LyX will get. This can be
1230 // solved by running the line below only when the selection has
1231 // finished. The solution used currently just works, to make it
1232 // faster we need to be more clever and probably also have more
1233 // calls to stuffClipboard. (Lgb)
1234 bv()->stuffClipboard(selectionAsString(bv()->buffer(), true));
1236 // This doesn't make sense, if there is no selection
1237 if (!selection.set())
1240 // OK, we have a selection. This is always between selection.start
1241 // and selection.end
1243 // make sure that the depth behind the selection are restored, too
1244 ParagraphList::iterator endpit = boost::next(selection.end.par());
1245 ParagraphList::iterator undoendpit = endpit;
1246 ParagraphList::iterator pars_end = ownerParagraphs().end();
1248 if (endpit != pars_end && endpit->getDepth()) {
1249 while (endpit != pars_end && endpit->getDepth()) {
1251 undoendpit = endpit;
1253 } else if (endpit != pars_end) {
1254 // because of parindents etc.
1258 setUndo(bv(), Undo::DELETE, selection.start.par(),
1259 boost::prior(undoendpit));
1262 endpit = selection.end.par();
1263 int endpos = selection.end.pos();
1265 boost::tie(endpit, endpos) = realcut ?
1266 CutAndPaste::cutSelection(bv()->buffer()->params,
1268 selection.start.par(), endpit,
1269 selection.start.pos(), endpos,
1270 bv()->buffer()->params.textclass,
1272 : CutAndPaste::eraseSelection(bv()->buffer()->params,
1274 selection.start.par(), endpit,
1275 selection.start.pos(), endpos,
1277 // sometimes necessary
1279 selection.start.par()->stripLeadingSpaces();
1281 redoParagraphs(selection.start, boost::next(endpit));
1282 #warning FIXME latent bug
1283 // endpit will be invalidated on redoParagraphs once ParagraphList
1284 // becomes a std::list? There are maybe other places on which this
1285 // can happend? (Ab)
1286 // cutSelection can invalidate the cursor so we need to set
1288 // we prefer the end for when tracking changes
1292 // need a valid cursor. (Lgb)
1295 setCursor(cursor.par(), cursor.pos());
1296 selection.cursor = cursor;
1301 void LyXText::copySelection()
1303 // stuff the selection onto the X clipboard, from an explicit copy request
1304 bv()->stuffClipboard(selectionAsString(bv()->buffer(), true));
1306 // this doesnt make sense, if there is no selection
1307 if (!selection.set())
1310 // ok we have a selection. This is always between selection.start
1311 // and sel_end cursor
1313 // copy behind a space if there is one
1314 while (selection.start.par()->size() > selection.start.pos()
1315 && selection.start.par()->isLineSeparator(selection.start.pos())
1316 && (selection.start.par() != selection.end.par()
1317 || selection.start.pos() < selection.end.pos()))
1318 selection.start.pos(selection.start.pos() + 1);
1320 CutAndPaste::copySelection(selection.start.par(),
1321 selection.end.par(),
1322 selection.start.pos(), selection.end.pos(),
1323 bv()->buffer()->params.textclass);
1327 void LyXText::pasteSelection(size_t sel_index)
1329 // this does not make sense, if there is nothing to paste
1330 if (!CutAndPaste::checkPastePossible())
1333 setUndo(bv(), Undo::INSERT, cursor.par());
1335 ParagraphList::iterator endpit;
1340 boost::tie(ppp, endpit) =
1341 CutAndPaste::pasteSelection(*bv()->buffer(),
1343 cursor.par(), cursor.pos(),
1344 bv()->buffer()->params.textclass,
1346 parseErrors(*bv()->buffer(), el);
1347 bv()->showErrorList(_("Paste"));
1349 redoParagraphs(cursor, endpit);
1351 setCursor(cursor.par(), cursor.pos());
1354 selection.cursor = cursor;
1355 setCursor(ppp.first, ppp.second);
1361 void LyXText::setSelectionRange(lyx::pos_type length)
1366 selection.cursor = cursor;
1373 // simple replacing. The font of the first selected character is used
1374 void LyXText::replaceSelectionWithString(string const & str)
1376 setCursorParUndo(bv());
1379 if (!selection.set()) { // create a dummy selection
1380 selection.end = cursor;
1381 selection.start = cursor;
1384 // Get font setting before we cut
1385 pos_type pos = selection.end.pos();
1386 LyXFont const font = selection.start.par()
1387 ->getFontSettings(bv()->buffer()->params,
1388 selection.start.pos());
1390 // Insert the new string
1391 string::const_iterator cit = str.begin();
1392 string::const_iterator end = str.end();
1393 for (; cit != end; ++cit) {
1394 selection.end.par()->insertChar(pos, (*cit), font);
1398 // Cut the selection
1399 cutSelection(true, false);
1405 // needed to insert the selection
1406 void LyXText::insertStringAsLines(string const & str)
1408 ParagraphList::iterator pit = cursor.par();
1409 pos_type pos = cursor.pos();
1410 ParagraphList::iterator endpit = boost::next(cursor.par());
1412 setCursorParUndo(bv());
1414 // only to be sure, should not be neccessary
1417 bv()->buffer()->insertStringAsLines(pit, pos, current_font, str);
1419 redoParagraphs(cursor, endpit);
1420 setCursor(cursor.par(), cursor.pos());
1421 selection.cursor = cursor;
1422 setCursor(pit, pos);
1427 // turns double-CR to single CR, others where converted into one
1428 // blank. Then InsertStringAsLines is called
1429 void LyXText::insertStringAsParagraphs(string const & str)
1431 string linestr(str);
1432 bool newline_inserted = false;
1433 string::size_type const siz = linestr.length();
1435 for (string::size_type i = 0; i < siz; ++i) {
1436 if (linestr[i] == '\n') {
1437 if (newline_inserted) {
1438 // we know that \r will be ignored by
1439 // InsertStringA. Of course, it is a dirty
1440 // trick, but it works...
1441 linestr[i - 1] = '\r';
1445 newline_inserted = true;
1447 } else if (IsPrintable(linestr[i])) {
1448 newline_inserted = false;
1451 insertStringAsLines(linestr);
1455 void LyXText::checkParagraph(ParagraphList::iterator pit, pos_type pos)
1457 LyXCursor tmpcursor;
1461 RowList::iterator row = getRow(pit, pos, y);
1462 RowList::iterator beg = rows().begin();
1464 // is there a break one row above
1466 && boost::prior(row)->par() == row->par()) {
1467 z = rowBreakPoint(*boost::prior(row));
1468 if (z >= row->pos()) {
1469 // set the dimensions of the row above
1470 y -= boost::prior(row)->height();
1473 breakAgain(boost::prior(row));
1475 // set the cursor again. Otherwise
1476 // dangling pointers are possible
1477 setCursor(cursor.par(), cursor.pos(),
1478 false, cursor.boundary());
1479 selection.cursor = cursor;
1484 int const tmpheight = row->height();
1485 pos_type const tmplast = lastPos(*this, row);
1488 if (row->height() == tmpheight && lastPos(*this, row) == tmplast) {
1489 postRowPaint(row, y);
1494 // check the special right address boxes
1495 if (pit->layout()->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1501 redoDrawingOfParagraph(tmpcursor);
1504 // set the cursor again. Otherwise dangling pointers are possible
1505 // also set the selection
1507 if (selection.set()) {
1509 setCursorIntern(selection.cursor.par(), selection.cursor.pos(),
1510 false, selection.cursor.boundary());
1511 selection.cursor = cursor;
1512 setCursorIntern(selection.start.par(),
1513 selection.start.pos(),
1514 false, selection.start.boundary());
1515 selection.start = cursor;
1516 setCursorIntern(selection.end.par(),
1517 selection.end.pos(),
1518 false, selection.end.boundary());
1519 selection.end = cursor;
1520 setCursorIntern(last_sel_cursor.par(),
1521 last_sel_cursor.pos(),
1522 false, last_sel_cursor.boundary());
1523 last_sel_cursor = cursor;
1526 setCursorIntern(cursor.par(), cursor.pos(),
1527 false, cursor.boundary());
1531 // returns false if inset wasn't found
1532 bool LyXText::updateInset(Inset * inset)
1534 // first check the current paragraph
1535 int pos = cursor.par()->getPositionOfInset(inset);
1537 checkParagraph(cursor.par(), pos);
1541 // check every paragraph
1543 ParagraphList::iterator par = ownerParagraphs().begin();
1544 ParagraphList::iterator end = ownerParagraphs().end();
1545 for (; par != end; ++par) {
1546 pos = par->getPositionOfInset(inset);
1548 checkParagraph(par, pos);
1557 bool LyXText::setCursor(ParagraphList::iterator pit,
1559 bool setfont, bool boundary)
1561 LyXCursor old_cursor = cursor;
1562 setCursorIntern(pit, pos, setfont, boundary);
1563 return deleteEmptyParagraphMechanism(old_cursor);
1567 void LyXText::setCursor(LyXCursor & cur, ParagraphList::iterator pit,
1568 pos_type pos, bool boundary)
1570 lyx::Assert(pit != ownerParagraphs().end());
1574 cur.boundary(boundary);
1576 // get the cursor y position in text
1578 RowList::iterator row = getRow(pit, pos, y);
1579 RowList::iterator beg = rows().begin();
1581 RowList::iterator old_row = row;
1583 // if we are before the first char of this row and are still in the
1584 // same paragraph and there is a previous row then put the cursor on
1585 // the end of the previous row
1586 cur.iy(y + row->baseline());
1589 boost::prior(row)->par() == row->par() &&
1590 pos < pit->size() &&
1591 pit->getChar(pos) == Paragraph::META_INSET) {
1592 Inset * ins = pit->getInset(pos);
1593 if (ins && (ins->needFullRow() || ins->display())) {
1599 // y is now the beginning of the cursor row
1600 y += row->baseline();
1601 // y is now the cursor baseline
1604 pos_type last = lastPrintablePos(*this, old_row);
1606 // None of these should happen, but we're scaredy-cats
1607 if (pos > pit->size()) {
1608 lyxerr << "dont like 1 please report" << endl;
1611 } else if (pos > last + 1) {
1612 lyxerr << "dont like 2 please report" << endl;
1613 // This shouldn't happen.
1616 } else if (pos < row->pos()) {
1617 lyxerr << "dont like 3 please report" << endl;
1622 // now get the cursors x position
1623 float x = getCursorX(row, pos, last, boundary);
1626 if (old_row != row) {
1627 x = getCursorX(old_row, pos, last, boundary);
1631 /* We take out this for the time being because 1) the redraw code is not
1632 prepared to this yet and 2) because some good policy has yet to be decided
1633 while editting: for instance how to act on rows being created/deleted
1637 //if the cursor is in a visible row, anchor to it
1639 if (topy < y && y < topy + bv()->workHeight())
1645 float LyXText::getCursorX(RowList::iterator rit,
1646 pos_type pos, pos_type last, bool boundary) const
1648 pos_type cursor_vpos = 0;
1650 float fill_separator;
1652 float fill_label_hfill;
1653 // This call HAS to be here because of the BidiTables!!!
1654 prepareToPrint(rit, x, fill_separator, fill_hfill,
1657 ParagraphList::iterator rit_par = rit->par();
1658 pos_type const rit_pos = rit->pos();
1661 cursor_vpos = rit_pos;
1662 else if (pos > last && !boundary)
1663 cursor_vpos = (rit_par->isRightToLeftPar(bv()->buffer()->params))
1664 ? rit_pos : last + 1;
1665 else if (pos > rit_pos && (pos > last || boundary))
1666 /// Place cursor after char at (logical) position pos - 1
1667 cursor_vpos = (bidi_level(pos - 1) % 2 == 0)
1668 ? log2vis(pos - 1) + 1 : log2vis(pos - 1);
1670 /// Place cursor before char at (logical) position pos
1671 cursor_vpos = (bidi_level(pos) % 2 == 0)
1672 ? log2vis(pos) : log2vis(pos) + 1;
1674 pos_type body_pos = rit_par->beginningOfBody();
1675 if ((body_pos > 0) &&
1676 ((body_pos - 1 > last) || !rit_par->isLineSeparator(body_pos - 1)))
1679 for (pos_type vpos = rit_pos; vpos < cursor_vpos; ++vpos) {
1680 pos_type pos = vis2log(vpos);
1681 if (body_pos > 0 && pos == body_pos - 1) {
1682 x += fill_label_hfill +
1683 font_metrics::width(
1684 rit_par->layout()->labelsep,
1685 getLabelFont(bv()->buffer(), rit_par));
1686 if (rit_par->isLineSeparator(body_pos - 1))
1687 x -= singleWidth(rit_par, body_pos - 1);
1690 if (hfillExpansion(*this, rit, pos)) {
1691 x += singleWidth(rit_par, pos);
1692 if (pos >= body_pos)
1695 x += fill_label_hfill;
1696 } else if (rit_par->isSeparator(pos)) {
1697 x += singleWidth(rit_par, pos);
1698 if (pos >= body_pos)
1699 x += fill_separator;
1701 x += singleWidth(rit_par, pos);
1707 void LyXText::setCursorIntern(ParagraphList::iterator pit,
1708 pos_type pos, bool setfont, bool boundary)
1710 UpdatableInset * it = pit->inInset();
1712 if (it != inset_owner) {
1713 lyxerr[Debug::INSETS] << "InsetText is " << it
1715 << "inset_owner is "
1716 << inset_owner << endl;
1717 #ifdef WITH_WARNINGS
1718 #warning I believe this code is wrong. (Lgb)
1719 #warning Jürgen, have a look at this. (Lgb)
1720 #warning Hmmm, I guess you are right but we
1721 #warning should verify when this is needed
1723 // Jürgen, would you like to have a look?
1724 // I guess we need to move the outer cursor
1725 // and open and lock the inset (bla bla bla)
1726 // stuff I don't know... so can you have a look?
1728 // I moved the lyxerr stuff in here so we can see if
1729 // this is actually really needed and where!
1731 // it->getLyXText(bv())->setCursorIntern(bv(), par, pos, setfont, boundary);
1736 setCursor(cursor, pit, pos, boundary);
1742 void LyXText::setCurrentFont()
1744 pos_type pos = cursor.pos();
1745 ParagraphList::iterator pit = cursor.par();
1747 if (cursor.boundary() && pos > 0)
1751 if (pos == pit->size())
1753 else // potentional bug... BUG (Lgb)
1754 if (pit->isSeparator(pos)) {
1755 if (pos > cursorRow()->pos() &&
1756 bidi_level(pos) % 2 ==
1757 bidi_level(pos - 1) % 2)
1759 else if (pos + 1 < pit->size())
1765 pit->getFontSettings(bv()->buffer()->params, pos);
1766 real_current_font = getFont(bv()->buffer(), pit, pos);
1768 if (cursor.pos() == pit->size() &&
1769 isBoundary(bv()->buffer(), *pit, cursor.pos()) &&
1770 !cursor.boundary()) {
1771 Language const * lang =
1772 pit->getParLanguage(bv()->buffer()->params);
1773 current_font.setLanguage(lang);
1774 current_font.setNumber(LyXFont::OFF);
1775 real_current_font.setLanguage(lang);
1776 real_current_font.setNumber(LyXFont::OFF);
1781 // returns the column near the specified x-coordinate of the row
1782 // x is set to the real beginning of this column
1784 LyXText::getColumnNearX(RowList::iterator rit, int & x, bool & boundary) const
1787 float fill_separator;
1789 float fill_label_hfill;
1791 prepareToPrint(rit, tmpx, fill_separator,
1792 fill_hfill, fill_label_hfill);
1794 pos_type vc = rit->pos();
1795 pos_type last = lastPrintablePos(*this, rit);
1798 ParagraphList::iterator rit_par = rit->par();
1799 LyXLayout_ptr const & layout = rit->par()->layout();
1801 bool left_side = false;
1803 pos_type body_pos = rit_par->beginningOfBody();
1804 float last_tmpx = tmpx;
1807 (body_pos - 1 > last ||
1808 !rit_par->isLineSeparator(body_pos - 1)))
1811 // check for empty row
1812 if (!rit_par->size()) {
1817 while (vc <= last && tmpx <= x) {
1820 if (body_pos > 0 && c == body_pos - 1) {
1821 tmpx += fill_label_hfill +
1822 font_metrics::width(layout->labelsep,
1823 getLabelFont(bv()->buffer(), rit_par));
1824 if (rit_par->isLineSeparator(body_pos - 1))
1825 tmpx -= singleWidth(rit_par, body_pos - 1);
1828 if (hfillExpansion(*this, rit, c)) {
1829 tmpx += singleWidth(rit_par, c);
1833 tmpx += fill_label_hfill;
1834 } else if (rit_par->isSeparator(c)) {
1835 tmpx += singleWidth(rit_par, c);
1837 tmpx+= fill_separator;
1839 tmpx += singleWidth(rit_par, c);
1844 if ((tmpx + last_tmpx) / 2 > x) {
1849 if (vc > last + 1) // This shouldn't happen.
1853 // This (rtl_support test) is not needed, but gives
1854 // some speedup if rtl_support=false
1855 RowList::iterator next_rit = boost::next(rit);
1857 bool const lastrow = lyxrc.rtl_support &&
1858 (next_rit == rowlist_.end() ||
1859 next_rit->par() != rit_par);
1861 // If lastrow is false, we don't need to compute
1862 // the value of rtl.
1863 bool const rtl = (lastrow)
1864 ? rit_par->isRightToLeftPar(bv()->buffer()->params)
1867 ((rtl && left_side && vc == rit->pos() && x < tmpx - 5) ||
1868 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5)))
1870 else if (vc == rit->pos()) {
1872 if (bidi_level(c) % 2 == 1)
1875 c = vis2log(vc - 1);
1876 bool const rtl = (bidi_level(c) % 2 == 1);
1877 if (left_side == rtl) {
1879 boundary = isBoundary(bv()->buffer(), *rit_par, c);
1883 if (rit->pos() <= last && c > last
1884 && rit_par->isNewline(last)) {
1885 if (bidi_level(last) % 2 == 0)
1886 tmpx -= singleWidth(rit_par, last);
1888 tmpx += singleWidth(rit_par, last);
1898 void LyXText::setCursorFromCoordinates(int x, int y)
1900 LyXCursor old_cursor = cursor;
1902 setCursorFromCoordinates(cursor, x, y);
1904 deleteEmptyParagraphMechanism(old_cursor);
1911 * return true if the cursor given is at the end of a row,
1912 * and the next row is filled by an inset that spans an entire
1915 bool beforeFullRowInset(LyXText & lt, LyXCursor const & cur)
1917 RowList::iterator row = lt.getRow(cur);
1918 if (boost::next(row) == lt.rows().end())
1921 Row const & next = *boost::next(row);
1923 if (next.pos() != cur.pos() || next.par() != cur.par())
1926 if (cur.pos() == cur.par()->size()
1927 || !cur.par()->isInset(cur.pos()))
1930 Inset const * inset = cur.par()->getInset(cur.pos());
1931 if (inset->needFullRow() || inset->display())
1939 void LyXText::setCursorFromCoordinates(LyXCursor & cur, int x, int y)
1941 // Get the row first.
1943 RowList::iterator row = getRowNearY(y);
1945 pos_type const column = getColumnNearX(row, x, bound);
1946 cur.par(row->par());
1947 cur.pos(row->pos() + column);
1949 cur.y(y + row->baseline());
1951 if (beforeFullRowInset(*this, cur)) {
1952 pos_type const last = lastPrintablePos(*this, row);
1953 RowList::iterator next_row = boost::next(row);
1955 float x = getCursorX(next_row, cur.pos(), last, bound);
1957 cur.iy(y + row->height() + next_row->baseline());
1964 cur.boundary(bound);
1968 void LyXText::cursorLeft(bool internal)
1970 if (cursor.pos() > 0) {
1971 bool boundary = cursor.boundary();
1972 setCursor(cursor.par(), cursor.pos() - 1, true, false);
1973 if (!internal && !boundary &&
1974 isBoundary(bv()->buffer(), *cursor.par(), cursor.pos() + 1))
1975 setCursor(cursor.par(), cursor.pos() + 1, true, true);
1976 } else if (cursor.par() != ownerParagraphs().begin()) { // steps into the above paragraph.
1977 ParagraphList::iterator pit = boost::prior(cursor.par());
1978 setCursor(pit, pit->size());
1983 void LyXText::cursorRight(bool internal)
1985 bool const at_end = (cursor.pos() == cursor.par()->size());
1986 bool const at_newline = !at_end &&
1987 cursor.par()->isNewline(cursor.pos());
1989 if (!internal && cursor.boundary() && !at_newline)
1990 setCursor(cursor.par(), cursor.pos(), true, false);
1992 setCursor(cursor.par(), cursor.pos() + 1, true, false);
1994 isBoundary(bv()->buffer(), *cursor.par(), cursor.pos()))
1995 setCursor(cursor.par(), cursor.pos(), true, true);
1996 } else if (boost::next(cursor.par()) != ownerParagraphs().end())
1997 setCursor(boost::next(cursor.par()), 0);
2001 void LyXText::cursorUp(bool selecting)
2004 int x = cursor.x_fix();
2005 int y = cursor.y() - cursorRow()->baseline() - 1;
2006 setCursorFromCoordinates(x, y);
2009 int y1 = cursor.iy() - topy;
2012 Inset * inset_hit = checkInsetHit(x, y1);
2013 if (inset_hit && isHighlyEditableInset(inset_hit)) {
2014 inset_hit->localDispatch(
2015 FuncRequest(bv(), LFUN_INSET_EDIT, x, y - (y2 - y1), mouse_button::none));
2019 setCursorFromCoordinates(bv(), cursor.x_fix(),
2020 cursor.y() - cursorRow()->baseline() - 1);
2025 void LyXText::cursorDown(bool selecting)
2028 int x = cursor.x_fix();
2029 int y = cursor.y() - cursorRow()->baseline() +
2030 cursorRow()->height() + 1;
2031 setCursorFromCoordinates(x, y);
2032 if (!selecting && cursorRow() == cursor.irow()) {
2034 int y1 = cursor.iy() - topy;
2037 Inset * inset_hit = checkInsetHit(x, y1);
2038 if (inset_hit && isHighlyEditableInset(inset_hit)) {
2039 FuncRequest cmd(bv(), LFUN_INSET_EDIT, x, y - (y2 - y1), mouse_button::none);
2040 inset_hit->localDispatch(cmd);
2044 setCursorFromCoordinates(bv(), cursor.x_fix(),
2045 cursor.y() - cursorRow()->baseline()
2046 + cursorRow()->height() + 1);
2051 void LyXText::cursorUpParagraph()
2053 if (cursor.pos() > 0) {
2054 setCursor(cursor.par(), 0);
2056 else if (cursor.par() != ownerParagraphs().begin()) {
2057 setCursor(boost::prior(cursor.par()), 0);
2062 void LyXText::cursorDownParagraph()
2064 ParagraphList::iterator par = cursor.par();
2065 ParagraphList::iterator next_par = boost::next(par);
2067 if (next_par != ownerParagraphs().end()) {
2068 setCursor(next_par, 0);
2070 setCursor(par, par->size());
2074 // fix the cursor `cur' after a characters has been deleted at `where'
2075 // position. Called by deleteEmptyParagraphMechanism
2076 void LyXText::fixCursorAfterDelete(LyXCursor & cur,
2077 LyXCursor const & where)
2079 // if cursor is not in the paragraph where the delete occured,
2081 if (cur.par() != where.par())
2084 // if cursor position is after the place where the delete occured,
2086 if (cur.pos() > where.pos())
2087 cur.pos(cur.pos()-1);
2089 // check also if we don't want to set the cursor on a spot behind the
2090 // pagragraph because we erased the last character.
2091 if (cur.pos() > cur.par()->size())
2092 cur.pos(cur.par()->size());
2094 // recompute row et al. for this cursor
2095 setCursor(cur, cur.par(), cur.pos(), cur.boundary());
2099 bool LyXText::deleteEmptyParagraphMechanism(LyXCursor const & old_cursor)
2101 // Would be wrong to delete anything if we have a selection.
2102 if (selection.set())
2105 // We allow all kinds of "mumbo-jumbo" when freespacing.
2106 if (old_cursor.par()->layout()->free_spacing
2107 || old_cursor.par()->isFreeSpacing()) {
2111 /* Ok I'll put some comments here about what is missing.
2112 I have fixed BackSpace (and thus Delete) to not delete
2113 double-spaces automagically. I have also changed Cut,
2114 Copy and Paste to hopefully do some sensible things.
2115 There are still some small problems that can lead to
2116 double spaces stored in the document file or space at
2117 the beginning of paragraphs. This happens if you have
2118 the cursor betwenn to spaces and then save. Or if you
2119 cut and paste and the selection have a space at the
2120 beginning and then save right after the paste. I am
2121 sure none of these are very hard to fix, but I will
2122 put out 1.1.4pre2 with FIX_DOUBLE_SPACE defined so
2123 that I can get some feedback. (Lgb)
2126 // If old_cursor.pos() == 0 and old_cursor.pos()(1) == LineSeparator
2127 // delete the LineSeparator.
2130 // If old_cursor.pos() == 1 and old_cursor.pos()(0) == LineSeparator
2131 // delete the LineSeparator.
2134 // If the pos around the old_cursor were spaces, delete one of them.
2135 if (old_cursor.par() != cursor.par()
2136 || old_cursor.pos() != cursor.pos()) {
2137 // Only if the cursor has really moved
2139 if (old_cursor.pos() > 0
2140 && old_cursor.pos() < old_cursor.par()->size()
2141 && old_cursor.par()->isLineSeparator(old_cursor.pos())
2142 && old_cursor.par()->isLineSeparator(old_cursor.pos() - 1)) {
2143 old_cursor.par()->erase(old_cursor.pos() - 1);
2144 redoParagraphs(old_cursor, boost::next(old_cursor.par()));
2146 #ifdef WITH_WARNINGS
2147 #warning This will not work anymore when we have multiple views of the same buffer
2148 // In this case, we will have to correct also the cursors held by
2149 // other bufferviews. It will probably be easier to do that in a more
2150 // automated way in LyXCursor code. (JMarc 26/09/2001)
2152 // correct all cursors held by the LyXText
2153 fixCursorAfterDelete(cursor, old_cursor);
2154 fixCursorAfterDelete(selection.cursor,
2156 fixCursorAfterDelete(selection.start,
2158 fixCursorAfterDelete(selection.end, old_cursor);
2159 fixCursorAfterDelete(last_sel_cursor,
2161 fixCursorAfterDelete(toggle_cursor, old_cursor);
2162 fixCursorAfterDelete(toggle_end_cursor,
2168 // don't delete anything if this is the ONLY paragraph!
2169 if (ownerParagraphs().size() == 1)
2172 // Do not delete empty paragraphs with keepempty set.
2173 if (old_cursor.par()->allowEmpty())
2176 // only do our magic if we changed paragraph
2177 if (old_cursor.par() == cursor.par())
2180 // record if we have deleted a paragraph
2181 // we can't possibly have deleted a paragraph before this point
2182 bool deleted = false;
2184 if (old_cursor.par()->empty() ||
2185 (old_cursor.par()->size() == 1 &&
2186 old_cursor.par()->isLineSeparator(0))) {
2187 // ok, we will delete anything
2188 LyXCursor tmpcursor;
2192 bool selection_position_was_oldcursor_position = (
2193 selection.cursor.par() == old_cursor.par()
2194 && selection.cursor.pos() == old_cursor.pos());
2196 if (getRow(old_cursor) != rows().begin()) {
2198 prevrow = boost::prior(getRow(old_cursor));
2199 postPaint(old_cursor.y() - getRow(old_cursor)->baseline() - prevrow->height());
2201 cursor = old_cursor; // that undo can restore the right cursor position
2202 #warning FIXME. --end() iterator is usable here
2203 ParagraphList::iterator endpit = boost::next(old_cursor.par());
2204 while (endpit != ownerParagraphs().end() &&
2205 endpit->getDepth()) {
2209 setUndo(bv(), Undo::DELETE, old_cursor.par(),
2210 boost::prior(endpit));
2214 removeRow(getRow(old_cursor));
2216 ownerParagraphs().erase(old_cursor.par());
2218 /* Breakagain the next par. Needed because of
2219 * the parindent that can occur or dissappear.
2220 * The next row can change its height, if
2221 * there is another layout before */
2222 RowList::iterator tmprit = boost::next(prevrow);
2223 if (tmprit != rows().end()) {
2227 setHeightOfRow(prevrow);
2229 RowList::iterator nextrow = boost::next(getRow(old_cursor));
2230 postPaint(old_cursor.y() - getRow(old_cursor)->baseline());
2233 cursor = old_cursor; // that undo can restore the right cursor position
2234 #warning FIXME. --end() iterator is usable here
2235 ParagraphList::iterator endpit = boost::next(old_cursor.par());
2236 while (endpit != ownerParagraphs().end() &&
2237 endpit->getDepth()) {
2241 setUndo(bv(), Undo::DELETE, old_cursor.par(), boost::prior(endpit));
2245 removeRow(getRow(old_cursor));
2247 ownerParagraphs().erase(old_cursor.par());
2249 /* Breakagain the next par. Needed because of
2250 the parindent that can occur or dissappear.
2251 The next row can change its height, if
2252 there is another layout before */
2253 if (nextrow != rows().end()) {
2254 breakAgain(nextrow);
2260 setCursorIntern(cursor.par(), cursor.pos());
2262 if (selection_position_was_oldcursor_position) {
2263 // correct selection
2264 selection.cursor = cursor;
2268 if (old_cursor.par()->stripLeadingSpaces()) {
2269 redoParagraphs(old_cursor, boost::next(old_cursor.par()));
2271 setCursorIntern(cursor.par(), cursor.pos());
2272 selection.cursor = cursor;
2279 ParagraphList & LyXText::ownerParagraphs() const
2282 return inset_owner->paragraphs;
2284 return bv_owner->buffer()->paragraphs;
2288 LyXText::refresh_status LyXText::refreshStatus() const
2290 return refresh_status_;
2294 void LyXText::clearPaint()
2296 refresh_status_ = REFRESH_NONE;
2297 refresh_row = rows().end();
2302 void LyXText::postPaint(int start_y)
2304 refresh_status old = refresh_status_;
2306 refresh_status_ = REFRESH_AREA;
2307 refresh_row = rows().end();
2309 if (old != REFRESH_NONE && refresh_y < start_y)
2312 refresh_y = start_y;
2317 // We are an inset's lyxtext. Tell the top-level lyxtext
2318 // it needs to update the row we're in.
2319 LyXText * t = bv()->text;
2320 t->postRowPaint(t->cursorRow(), t->cursor.y() - t->cursorRow()->baseline());
2324 // FIXME: we should probably remove this y parameter,
2325 // make refresh_y be 0, and use row->y etc.
2326 void LyXText::postRowPaint(RowList::iterator rit, int start_y)
2328 if (refresh_status_ != REFRESH_NONE && refresh_y < start_y) {
2329 refresh_status_ = REFRESH_AREA;
2332 refresh_y = start_y;
2335 if (refresh_status_ == REFRESH_AREA)
2338 refresh_status_ = REFRESH_ROW;
2344 // We are an inset's lyxtext. Tell the top-level lyxtext
2345 // it needs to update the row we're in.
2346 LyXText * t = bv()->text;
2347 t->postRowPaint(t->cursorRow(), t->cursor.y() - t->cursorRow()->baseline());
2351 bool LyXText::isInInset() const
2353 // Sub-level has non-null bv owner and
2354 // non-null inset owner.
2355 return inset_owner != 0 && bv_owner != 0;
2359 int defaultRowHeight()
2361 LyXFont const font(LyXFont::ALL_SANE);
2362 return int(font_metrics::maxAscent(font)
2363 + font_metrics::maxDescent(font) * 1.5);