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 "metricsinfo.h"
38 #include "paragraph_funcs.h"
40 #include "insets/insetbibitem.h"
41 #include "insets/insetenv.h"
42 #include "insets/insetfloat.h"
43 #include "insets/insetwrap.h"
45 #include "support/LAssert.h"
46 #include "support/textutils.h"
47 #include "support/lstrings.h"
49 #include <boost/tuple/tuple.hpp>
53 using namespace lyx::support;
63 LyXText::LyXText(BufferView * bv)
64 : height(0), width(0), anchor_row_offset_(0),
65 inset_owner(0), the_locking_inset(0), bv_owner(bv)
67 anchor_row_ = rows().end();
72 LyXText::LyXText(BufferView * bv, InsetText * inset)
73 : height(0), width(0), anchor_row_offset_(0),
74 inset_owner(inset), the_locking_inset(0), bv_owner(bv)
76 anchor_row_ = rows().end();
81 void LyXText::init(BufferView * bview)
89 anchor_row_ = rows().end();
90 anchor_row_offset_ = 0;
92 ParagraphList::iterator pit = ownerParagraphs().begin();
93 ParagraphList::iterator end = ownerParagraphs().end();
95 current_font = getFont(pit, 0);
97 for (; pit != end; ++pit)
98 insertParagraph(pit, rowlist_.end());
100 setCursorIntern(rowlist_.begin()->par(), 0);
101 selection.cursor = cursor;
107 // Gets the fully instantiated font at a given position in a paragraph
108 // Basically the same routine as Paragraph::getFont() in paragraph.C.
109 // The difference is that this one is used for displaying, and thus we
110 // are allowed to make cosmetic improvements. For instance make footnotes
112 // If position is -1, we get the layout font of the paragraph.
113 // If position is -2, we get the font of the manual label of the paragraph.
114 LyXFont LyXText::getFont(ParagraphList::iterator pit, pos_type pos) const
118 LyXLayout_ptr const & layout = pit->layout();
120 BufferParams const & params = bv()->buffer()->params;
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(params, pos);
129 pit->inInset()->getDrawFont(f);
130 return f.realize(layout->reslabelfont);
132 LyXFont f = pit->getFontSettings(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(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()));
159 tmpfont.realize(defaultfont_);
165 LyXFont LyXText::getLayoutFont(ParagraphList::iterator pit) const
167 LyXLayout_ptr const & layout = pit->layout();
169 if (!pit->getDepth())
170 return layout->resfont;
172 LyXFont font = layout->font;
173 // Realize with the fonts of lesser depth.
174 font.realize(outerFont(pit, ownerParagraphs()));
175 font.realize(defaultfont_);
181 LyXFont LyXText::getLabelFont(ParagraphList::iterator pit) const
183 LyXLayout_ptr const & layout = pit->layout();
185 if (!pit->getDepth())
186 return layout->reslabelfont;
188 LyXFont font = layout->labelfont;
189 // Realize with the fonts of lesser depth.
190 font.realize(outerFont(pit, ownerParagraphs()));
191 font.realize(defaultfont_);
197 void LyXText::setCharFont(ParagraphList::iterator pit,
198 pos_type pos, LyXFont const & fnt,
201 BufferParams const & params = bv()->buffer()->params;
202 LyXFont font = getFont(pit, pos);
203 font.update(fnt, params.language, toggleall);
204 // Let the insets convert their font
205 if (pit->isInset(pos)) {
206 InsetOld * inset = pit->getInset(pos);
207 if (isEditableInset(inset)) {
208 static_cast<UpdatableInset *>(inset)
209 ->setFont(bv(), fnt, toggleall, true);
213 // Plug through to version below:
214 setCharFont(pit, pos, font);
218 void LyXText::setCharFont(
219 ParagraphList::iterator pit, pos_type pos, LyXFont const & fnt)
222 LyXLayout_ptr const & layout = pit->layout();
224 // Get concrete layout font to reduce against
227 if (pos < pit->beginningOfBody())
228 layoutfont = layout->labelfont;
230 layoutfont = layout->font;
232 // Realize against environment font information
233 if (pit->getDepth()) {
234 ParagraphList::iterator tp = pit;
235 while (!layoutfont.resolved() &&
236 tp != ownerParagraphs().end() &&
238 tp = outerHook(tp, ownerParagraphs());
239 if (tp != ownerParagraphs().end())
240 layoutfont.realize(tp->layout()->font);
244 layoutfont.realize(defaultfont_);
246 // Now, reduce font against full layout font
247 font.reduce(layoutfont);
249 pit->setFont(pos, font);
253 // removes the row and reset the touched counters
254 void LyXText::removeRow(RowList::iterator rit)
256 if (anchor_row_ == rit) {
257 if (rit != rows().begin()) {
258 anchor_row_ = boost::prior(rit);
259 anchor_row_offset_ += anchor_row_->height();
261 anchor_row_ = boost::next(rit);
262 anchor_row_offset_ -= rit->height();
266 // the text becomes smaller
267 height -= rit->height();
273 // remove all following rows of the paragraph of the specified row.
274 void LyXText::removeParagraph(RowList::iterator rit)
276 ParagraphList::iterator tmppit = rit->par();
279 while (rit != rows().end() && rit->par() == tmppit) {
280 RowList::iterator tmprit = boost::next(rit);
287 void LyXText::insertParagraph(ParagraphList::iterator pit,
288 RowList::iterator rowit)
290 // insert a new row, starting at position 0
292 RowList::iterator rit = rowlist_.insert(rowit, newrow);
294 // and now append the whole paragraph before the new row
295 appendParagraph(rit);
299 InsetOld * LyXText::getInset() const
301 ParagraphList::iterator pit = cursor.par();
302 pos_type const pos = cursor.pos();
304 if (pos < pit->size() && pit->isInset(pos)) {
305 return pit->getInset(pos);
311 void LyXText::toggleInset()
313 InsetOld * inset = getInset();
314 // is there an editable inset at cursor position?
315 if (!isEditableInset(inset)) {
316 // No, try to see if we are inside a collapsable inset
317 if (inset_owner && inset_owner->owner()
318 && inset_owner->owner()->isOpen()) {
319 bv()->unlockInset(inset_owner->owner());
320 inset_owner->owner()->close(bv());
321 bv()->getLyXText()->cursorRight(bv());
325 //bv()->owner()->message(inset->editMessage());
327 // do we want to keep this?? (JMarc)
328 if (!isHighlyEditableInset(inset))
329 recordUndo(bv(), Undo::ATOMIC);
331 if (inset->isOpen()) {
337 bv()->updateInset(inset);
341 /* used in setlayout */
342 // Asger is not sure we want to do this...
343 void LyXText::makeFontEntriesLayoutSpecific(BufferParams const & params,
346 LyXLayout_ptr const & layout = par.layout();
347 pos_type const psize = par.size();
350 for (pos_type pos = 0; pos < psize; ++pos) {
351 if (pos < par.beginningOfBody())
352 layoutfont = layout->labelfont;
354 layoutfont = layout->font;
356 LyXFont tmpfont = par.getFontSettings(params, pos);
357 tmpfont.reduce(layoutfont);
358 par.setFont(pos, tmpfont);
363 ParagraphList::iterator
364 LyXText::setLayout(LyXCursor & cur, LyXCursor & sstart_cur,
365 LyXCursor & send_cur,
366 string const & layout)
368 ParagraphList::iterator endpit = boost::next(send_cur.par());
369 ParagraphList::iterator undoendpit = endpit;
370 ParagraphList::iterator pars_end = ownerParagraphs().end();
372 if (endpit != pars_end && endpit->getDepth()) {
373 while (endpit != pars_end && endpit->getDepth()) {
377 } else if (endpit != pars_end) {
378 // because of parindents etc.
382 recordUndo(bv(), Undo::ATOMIC, sstart_cur.par(), boost::prior(undoendpit));
384 // ok we have a selection. This is always between sstart_cur
385 // and sel_end cursor
387 ParagraphList::iterator pit = sstart_cur.par();
388 ParagraphList::iterator epit = boost::next(send_cur.par());
390 LyXLayout_ptr const & lyxlayout =
391 bv()->buffer()->params.getLyXTextClass()[layout];
394 pit->applyLayout(lyxlayout);
395 makeFontEntriesLayoutSpecific(bv()->buffer()->params, *pit);
396 ParagraphList::iterator fppit = pit;
397 fppit->params().spaceTop(lyxlayout->fill_top ?
398 VSpace(VSpace::VFILL)
399 : VSpace(VSpace::NONE));
400 fppit->params().spaceBottom(lyxlayout->fill_bottom ?
401 VSpace(VSpace::VFILL)
402 : VSpace(VSpace::NONE));
403 if (lyxlayout->margintype == MARGIN_MANUAL)
404 pit->setLabelWidthString(lyxlayout->labelstring());
407 } while (pit != epit);
413 // set layout over selection and make a total rebreak of those paragraphs
414 void LyXText::setLayout(string const & layout)
416 LyXCursor tmpcursor = cursor; // store the current cursor
418 // if there is no selection just set the layout
419 // of the current paragraph
420 if (!selection.set()) {
421 selection.start = cursor; // dummy selection
422 selection.end = cursor;
425 // special handling of new environment insets
426 BufferParams const & params = bv()->buffer()->params;
427 LyXLayout_ptr const & lyxlayout = params.getLyXTextClass()[layout];
428 if (lyxlayout->is_environment) {
429 // move everything in a new environment inset
430 lyxerr << "setting layout " << layout << endl;
431 bv()->owner()->dispatch(FuncRequest(LFUN_HOME));
432 bv()->owner()->dispatch(FuncRequest(LFUN_ENDSEL));
433 bv()->owner()->dispatch(FuncRequest(LFUN_CUT));
434 InsetOld * inset = new InsetEnvironment(params, layout);
435 if (bv()->insertInset(inset)) {
437 //bv()->owner()->dispatch(FuncRequest(LFUN_PASTE));
444 ParagraphList::iterator endpit = setLayout(cursor, selection.start,
445 selection.end, layout);
446 redoParagraphs(selection.start, endpit);
448 // we have to reset the selection, because the
449 // geometry could have changed
450 setCursor(selection.start.par(), selection.start.pos(), false);
451 selection.cursor = cursor;
452 setCursor(selection.end.par(), selection.end.pos(), false);
456 setCursor(tmpcursor.par(), tmpcursor.pos(), true);
460 bool LyXText::changeDepth(bv_funcs::DEPTH_CHANGE type, bool test_only)
462 ParagraphList::iterator pit(cursor.par());
463 ParagraphList::iterator end(cursor.par());
464 ParagraphList::iterator start = pit;
466 if (selection.set()) {
467 pit = selection.start.par();
468 end = selection.end.par();
472 ParagraphList::iterator pastend = boost::next(end);
475 recordUndo(bv(), Undo::ATOMIC, start, end);
477 bool changed = false;
479 int prev_after_depth = 0;
480 #warning parlist ... could be nicer ?
481 if (start != ownerParagraphs().begin()) {
482 prev_after_depth = boost::prior(start)->getMaxDepthAfter();
486 int const depth = pit->params().depth();
487 if (type == bv_funcs::INC_DEPTH) {
488 if (depth < prev_after_depth
489 && pit->layout()->labeltype != LABEL_BIBLIO) {
492 pit->params().depth(depth + 1);
499 pit->params().depth(depth - 1);
502 prev_after_depth = pit->getMaxDepthAfter();
514 // Wow, redoParagraphs is stupid.
516 setCursor(tmpcursor, start, 0);
518 redoParagraphs(tmpcursor, pastend);
520 // We need to actually move the text->cursor. I don't
521 // understand why ...
524 // we have to reset the visual selection because the
525 // geometry could have changed
526 if (selection.set()) {
527 setCursor(selection.start.par(), selection.start.pos());
528 selection.cursor = cursor;
529 setCursor(selection.end.par(), selection.end.pos());
532 // this handles the counter labels, and also fixes up
533 // depth values for follow-on (child) paragraphs
537 setCursor(tmpcursor.par(), tmpcursor.pos());
543 // set font over selection and make a total rebreak of those paragraphs
544 void LyXText::setFont(LyXFont const & font, bool toggleall)
546 // if there is no selection just set the current_font
547 if (!selection.set()) {
548 // Determine basis font
550 if (cursor.pos() < cursor.par()->beginningOfBody()) {
551 layoutfont = getLabelFont(cursor.par());
553 layoutfont = getLayoutFont(cursor.par());
555 // Update current font
556 real_current_font.update(font,
557 bv()->buffer()->params.language,
560 // Reduce to implicit settings
561 current_font = real_current_font;
562 current_font.reduce(layoutfont);
563 // And resolve it completely
564 real_current_font.realize(layoutfont);
569 LyXCursor tmpcursor = cursor; // store the current cursor
571 // ok we have a selection. This is always between sel_start_cursor
572 // and sel_end cursor
574 recordUndo(bv(), Undo::ATOMIC, selection.start.par(), selection.end.par());
576 cursor = selection.start;
577 while (cursor.par() != selection.end.par() ||
578 cursor.pos() < selection.end.pos())
580 if (cursor.pos() < cursor.par()->size()) {
581 // an open footnote should behave like a closed one
582 setCharFont(cursor.par(), cursor.pos(),
584 cursor.pos(cursor.pos() + 1);
587 cursor.par(boost::next(cursor.par()));
592 redoParagraphs(selection.start, boost::next(selection.end.par()));
594 // we have to reset the selection, because the
595 // geometry could have changed, but we keep
596 // it for user convenience
597 setCursor(selection.start.par(), selection.start.pos());
598 selection.cursor = cursor;
599 setCursor(selection.end.par(), selection.end.pos());
601 setCursor(tmpcursor.par(), tmpcursor.pos(), true,
602 tmpcursor.boundary());
606 void LyXText::redoHeightOfParagraph()
608 RowList::iterator tmprow = cursorRow();
610 setHeightOfRow(tmprow);
612 while (tmprow != rows().begin()
613 && boost::prior(tmprow)->par() == tmprow->par()) {
615 setHeightOfRow(tmprow);
620 setCursor(cursor.par(), cursor.pos(), false, cursor.boundary());
624 // deletes and inserts again all paragraphs between the cursor
625 // and the specified par
626 // This function is needed after SetLayout and SetFont etc.
627 void LyXText::redoParagraphs(LyXCursor const & cur,
628 ParagraphList::iterator endpit)
630 RowList::iterator tmprit = getRow(cur);
632 ParagraphList::iterator first_phys_pit;
633 RowList::iterator prevrit;
634 if (tmprit == rows().begin()) {
635 // A trick/hack for UNDO.
636 // This is needed because in an UNDO/REDO we could have
637 // changed the ownerParagraph() so the paragraph inside
638 // the row is NOT my really first par anymore.
639 // Got it Lars ;) (Jug 20011206)
640 first_phys_pit = ownerParagraphs().begin();
641 prevrit = rows().end();
643 first_phys_pit = tmprit->par();
644 while (tmprit != rows().begin()
645 && boost::prior(tmprit)->par() == first_phys_pit)
649 prevrit = boost::prior(tmprit);
653 while (tmprit != rows().end() && tmprit->par() != endpit) {
654 RowList::iterator tmprit2 = tmprit++;
658 // Reinsert the paragraphs.
659 ParagraphList::iterator tmppit = first_phys_pit;
661 while (tmppit != ownerParagraphs().end()) {
662 insertParagraph(tmppit, tmprit);
663 while (tmprit != rows().end()
664 && tmprit->par() == tmppit) {
668 if (tmppit == endpit)
671 if (prevrit != rows().end())
672 setHeightOfRow(prevrit);
674 setHeightOfRow(rows().begin());
676 if (tmprit != rows().end())
677 setHeightOfRow(tmprit);
683 void LyXText::fullRebreak()
686 setCursorIntern(cursor.par(), cursor.pos());
690 void LyXText::metrics(MetricsInfo & mi, Dimension & dim)
692 //lyxerr << "LyXText::metrics: width: " << mi.base.textwidth << "\n";
698 anchor_row_ = rows().end();
699 anchor_row_offset_ = 0;
701 ParagraphList::iterator pit = ownerParagraphs().begin();
702 ParagraphList::iterator end = ownerParagraphs().end();
704 for (; pit != end; ++pit) {
705 InsetList::iterator ii = pit->insetlist.begin();
706 InsetList::iterator iend = pit->insetlist.end();
707 for (; ii != iend; ++ii) {
710 ii->inset->metrics(m, dim);
713 // insert a new row, starting at position 0
715 RowList::iterator rit = rowlist_.insert(rowlist_.end(), newrow);
717 // and now append the whole paragraph before the new row
718 appendParagraph(rit);
722 //lyxerr << "height 0: " << height << "\n";
723 //for (RowList::iterator rit = rows().begin(); rit != rows().end(); ++rit) {
724 // height += rit->height();
726 //lyxerr << "height 1: " << height << "\n";
729 dim.asc = rows().begin()->ascent_of_text();
730 dim.des = height - dim.asc;
731 dim.wid = std::max(mi.base.textwidth, int(width));
735 void LyXText::partialRebreak()
737 if (rows().empty()) {
741 breakAgain(rows().begin());
745 // important for the screen
748 // the cursor set functions have a special mechanism. When they
749 // realize, that you left an empty paragraph, they will delete it.
750 // They also delete the corresponding row
752 // need the selection cursor:
753 void LyXText::setSelection()
755 bool const lsel = TextCursor::setSelection();
757 if (inset_owner && (selection.set() || lsel))
758 inset_owner->setUpdateStatus(InsetText::SELECTION);
763 void LyXText::clearSelection()
765 TextCursor::clearSelection();
767 // reset this in the bv_owner!
768 if (bv_owner && bv_owner->text)
769 bv_owner->text->xsel_cache.set(false);
773 void LyXText::cursorHome()
775 setCursor(cursor.par(), cursorRow()->pos());
779 void LyXText::cursorEnd()
781 if (cursor.par()->empty())
784 RowList::iterator rit = cursorRow();
785 RowList::iterator next_rit = boost::next(rit);
786 ParagraphList::iterator pit = rit->par();
787 pos_type last_pos = lastPos(*this, rit);
789 if (next_rit == rows().end() || next_rit->par() != pit) {
793 (pit->getChar(last_pos) != ' ' && !pit->isNewline(last_pos))) {
798 setCursor(pit, last_pos);
802 void LyXText::cursorTop()
804 setCursor(ownerParagraphs().begin(), 0);
808 void LyXText::cursorBottom()
810 ParagraphList::iterator lastpit =
811 boost::prior(ownerParagraphs().end());
812 setCursor(lastpit, lastpit->size());
816 void LyXText::toggleFree(LyXFont const & font, bool toggleall)
818 // If the mask is completely neutral, tell user
819 if (font == LyXFont(LyXFont::ALL_IGNORE)) {
820 // Could only happen with user style
821 bv()->owner()->message(_("No font change defined. Use Character under the Layout menu to define font change."));
825 // Try implicit word selection
826 // If there is a change in the language the implicit word selection
828 LyXCursor resetCursor = cursor;
829 bool implicitSelection = (font.language() == ignore_language
830 && font.number() == LyXFont::IGNORE)
831 ? selectWordWhenUnderCursor(lyx::WHOLE_WORD_STRICT) : false;
834 setFont(font, toggleall);
836 // Implicit selections are cleared afterwards
837 //and cursor is set to the original position.
838 if (implicitSelection) {
840 cursor = resetCursor;
841 setCursor(cursor.par(), cursor.pos());
842 selection.cursor = cursor;
845 inset_owner->setUpdateStatus(InsetText::CURSOR_PAR);
849 string LyXText::getStringToIndex()
851 // Try implicit word selection
852 // If there is a change in the language the implicit word selection
854 LyXCursor const reset_cursor = cursor;
855 bool const implicitSelection =
856 selectWordWhenUnderCursor(lyx::PREVIOUS_WORD);
859 if (!selection.set())
860 bv()->owner()->message(_("Nothing to index!"));
861 else if (selection.start.par() != selection.end.par())
862 bv()->owner()->message(_("Cannot index more than one paragraph!"));
864 idxstring = selectionAsString(bv()->buffer(), false);
866 // Reset cursors to their original position.
867 cursor = reset_cursor;
868 setCursor(cursor.par(), cursor.pos());
869 selection.cursor = cursor;
871 // Clear the implicit selection.
872 if (implicitSelection)
879 // the DTP switches for paragraphs. LyX will store them in the first
880 // physicla paragraph. When a paragraph is broken, the top settings rest,
881 // the bottom settings are given to the new one. So I can make shure,
882 // they do not duplicate themself and you cannnot make dirty things with
885 void LyXText::setParagraph(bool line_top, bool line_bottom,
886 bool pagebreak_top, bool pagebreak_bottom,
887 VSpace const & space_top,
888 VSpace const & space_bottom,
889 Spacing const & spacing,
891 string const & labelwidthstring,
894 LyXCursor tmpcursor = cursor;
895 if (!selection.set()) {
896 selection.start = cursor;
897 selection.end = cursor;
900 // make sure that the depth behind the selection are restored, too
901 ParagraphList::iterator endpit = boost::next(selection.end.par());
902 ParagraphList::iterator undoendpit = endpit;
903 ParagraphList::iterator pars_end = ownerParagraphs().end();
905 if (endpit != pars_end && endpit->getDepth()) {
906 while (endpit != pars_end && endpit->getDepth()) {
910 } else if (endpit != pars_end) {
911 // because of parindents etc.
915 recordUndo(bv(), Undo::ATOMIC, selection.start.par(),
916 boost::prior(undoendpit));
919 ParagraphList::iterator tmppit = selection.end.par();
921 while (tmppit != boost::prior(selection.start.par())) {
922 setCursor(tmppit, 0);
924 ParagraphList::iterator pit = cursor.par();
925 ParagraphParameters & params = pit->params();
927 params.lineTop(line_top);
928 params.lineBottom(line_bottom);
929 params.pagebreakTop(pagebreak_top);
930 params.pagebreakBottom(pagebreak_bottom);
931 params.spaceTop(space_top);
932 params.spaceBottom(space_bottom);
933 params.spacing(spacing);
934 // does the layout allow the new alignment?
935 LyXLayout_ptr const & layout = pit->layout();
937 if (align == LYX_ALIGN_LAYOUT)
938 align = layout->align;
939 if (align & layout->alignpossible) {
940 if (align == layout->align)
941 params.align(LYX_ALIGN_LAYOUT);
945 pit->setLabelWidthString(labelwidthstring);
946 params.noindent(noindent);
947 tmppit = boost::prior(pit);
951 redoParagraphs(selection.start, endpit);
954 setCursor(selection.start.par(), selection.start.pos());
955 selection.cursor = cursor;
956 setCursor(selection.end.par(), selection.end.pos());
958 setCursor(tmpcursor.par(), tmpcursor.pos());
960 bv()->updateInset(inset_owner);
964 // set the counter of a paragraph. This includes the labels
965 void LyXText::setCounter(Buffer const * buf, ParagraphList::iterator pit)
967 LyXTextClass const & textclass = buf->params.getLyXTextClass();
968 LyXLayout_ptr const & layout = pit->layout();
970 if (pit != ownerParagraphs().begin()) {
972 pit->params().appendix(boost::prior(pit)->params().appendix());
973 if (!pit->params().appendix() &&
974 pit->params().startOfAppendix()) {
975 pit->params().appendix(true);
976 textclass.counters().reset();
978 pit->enumdepth = boost::prior(pit)->enumdepth;
979 pit->itemdepth = boost::prior(pit)->itemdepth;
981 pit->params().appendix(pit->params().startOfAppendix());
986 /* Maybe we have to increment the enumeration depth.
987 * BUT, enumeration in a footnote is considered in isolation from its
988 * surrounding paragraph so don't increment if this is the
989 * first line of the footnote
990 * AND, bibliographies can't have their depth changed ie. they
991 * are always of depth 0
993 if (pit != ownerParagraphs().begin()
994 && boost::prior(pit)->getDepth() < pit->getDepth()
995 && boost::prior(pit)->layout()->labeltype == LABEL_COUNTER_ENUMI
996 && pit->enumdepth < 3
997 && layout->labeltype != LABEL_BIBLIO) {
1001 // Maybe we have to decrement the enumeration depth, see note above
1002 if (pit != ownerParagraphs().begin()
1003 && boost::prior(pit)->getDepth() > pit->getDepth()
1004 && layout->labeltype != LABEL_BIBLIO) {
1005 pit->enumdepth = depthHook(pit, ownerParagraphs(),
1006 pit->getDepth())->enumdepth;
1009 if (!pit->params().labelString().empty()) {
1010 pit->params().labelString(string());
1013 if (layout->margintype == MARGIN_MANUAL) {
1014 if (pit->params().labelWidthString().empty()) {
1015 pit->setLabelWidthString(layout->labelstring());
1018 pit->setLabelWidthString(string());
1021 // is it a layout that has an automatic label?
1022 if (layout->labeltype >= LABEL_COUNTER_CHAPTER) {
1023 int const i = layout->labeltype - LABEL_COUNTER_CHAPTER;
1027 if (i >= 0 && i <= buf->params.secnumdepth) {
1031 textclass.counters().step(layout->latexname());
1033 // Is there a label? Useful for Chapter layout
1034 if (!pit->params().appendix()) {
1035 s << buf->B_(layout->labelstring());
1037 s << buf->B_(layout->labelstring_appendix());
1040 // Use of an integer is here less than elegant. For now.
1041 int head = textclass.maxcounter() - LABEL_COUNTER_CHAPTER;
1042 if (!pit->params().appendix()) {
1043 numbertype = "sectioning";
1045 numbertype = "appendix";
1046 if (pit->isRightToLeftPar(buf->params))
1047 langtype = "hebrew";
1053 << textclass.counters()
1054 .numberLabel(layout->latexname(),
1055 numbertype, langtype, head);
1057 pit->params().labelString(STRCONV(s.str()));
1059 // reset enum counters
1060 textclass.counters().reset("enum");
1061 } else if (layout->labeltype < LABEL_COUNTER_ENUMI) {
1062 textclass.counters().reset("enum");
1063 } else if (layout->labeltype == LABEL_COUNTER_ENUMI) {
1065 // Yes I know this is a really, really! bad solution
1067 string enumcounter("enum");
1069 switch (pit->enumdepth) {
1078 enumcounter += "iv";
1081 // not a valid enumdepth...
1085 textclass.counters().step(enumcounter);
1087 s << textclass.counters()
1088 .numberLabel(enumcounter, "enumeration");
1089 pit->params().labelString(STRCONV(s.str()));
1091 } else if (layout->labeltype == LABEL_BIBLIO) {// ale970302
1092 textclass.counters().step("bibitem");
1093 int number = textclass.counters().value("bibitem");
1094 if (pit->bibitem()) {
1095 pit->bibitem()->setCounter(number);
1096 pit->params().labelString(layout->labelstring());
1098 // In biblio should't be following counters but...
1100 string s = buf->B_(layout->labelstring());
1102 // the caption hack:
1103 if (layout->labeltype == LABEL_SENSITIVE) {
1104 ParagraphList::iterator tmppit = pit;
1107 while (tmppit != ownerParagraphs().end() &&
1109 // the single '=' is intended below
1110 && (in = tmppit->inInset()->owner())) {
1111 if (in->lyxCode() == InsetOld::FLOAT_CODE ||
1112 in->lyxCode() == InsetOld::WRAP_CODE) {
1116 tmppit = std::find(ownerParagraphs().begin(), ownerParagraphs().end(), *in->parOwner());
1123 if (in->lyxCode() == InsetOld::FLOAT_CODE)
1124 type = static_cast<InsetFloat*>(in)->params().type;
1125 else if (in->lyxCode() == InsetOld::WRAP_CODE)
1126 type = static_cast<InsetWrap*>(in)->params().type;
1130 Floating const & fl = textclass.floats().getType(type);
1132 textclass.counters().step(fl.type());
1134 // Doesn't work... yet.
1135 s = bformat(_("%1$s #:"), buf->B_(fl.name()));
1137 // par->SetLayout(0);
1138 // s = layout->labelstring;
1139 s = _("Senseless: ");
1142 pit->params().labelString(s);
1144 // reset the enumeration counter. They are always reset
1145 // when there is any other layout between
1146 // Just fall-through between the cases so that all
1147 // enum counters deeper than enumdepth is also reset.
1148 switch (pit->enumdepth) {
1150 textclass.counters().reset("enumi");
1152 textclass.counters().reset("enumii");
1154 textclass.counters().reset("enumiii");
1156 textclass.counters().reset("enumiv");
1162 // Updates all counters. Paragraphs with changed label string will be rebroken
1163 void LyXText::updateCounters()
1165 RowList::iterator rowit = rows().begin();
1166 ParagraphList::iterator pit = rowit->par();
1168 // CHECK if this is really needed. (Lgb)
1169 bv()->buffer()->params.getLyXTextClass().counters().reset();
1171 ParagraphList::iterator beg = ownerParagraphs().begin();
1172 ParagraphList::iterator end = ownerParagraphs().end();
1173 for (; pit != end; ++pit) {
1174 while (rowit->par() != pit)
1177 string const oldLabel = pit->params().labelString();
1179 size_t maxdepth = 0;
1181 maxdepth = boost::prior(pit)->getMaxDepthAfter();
1183 if (pit->params().depth() > maxdepth)
1184 pit->params().depth(maxdepth);
1186 // setCounter can potentially change the labelString.
1187 setCounter(bv()->buffer(), pit);
1189 string const & newLabel = pit->params().labelString();
1191 if (oldLabel.empty() && !newLabel.empty()) {
1192 removeParagraph(rowit);
1193 appendParagraph(rowit);
1199 void LyXText::insertInset(InsetOld * inset)
1201 if (!cursor.par()->insetAllowed(inset->lyxCode()))
1203 recordUndo(bv(), Undo::ATOMIC, cursor.par());
1205 cursor.par()->insertInset(cursor.pos(), inset);
1206 // Just to rebreak and refresh correctly.
1207 // The character will not be inserted a second time
1208 insertChar(Paragraph::META_INSET);
1209 // If we enter a highly editable inset the cursor should be to before
1210 // the inset. This couldn't happen before as Undo was not handled inside
1211 // inset now after the Undo LyX tries to call inset->Edit(...) again
1212 // and cannot do this as the cursor is behind the inset and GetInset
1213 // does not return the inset!
1214 if (isHighlyEditableInset(inset)) {
1221 void LyXText::cutSelection(bool doclear, bool realcut)
1223 // Stuff what we got on the clipboard. Even if there is no selection.
1225 // There is a problem with having the stuffing here in that the
1226 // larger the selection the slower LyX will get. This can be
1227 // solved by running the line below only when the selection has
1228 // finished. The solution used currently just works, to make it
1229 // faster we need to be more clever and probably also have more
1230 // calls to stuffClipboard. (Lgb)
1231 bv()->stuffClipboard(selectionAsString(bv()->buffer(), true));
1233 // This doesn't make sense, if there is no selection
1234 if (!selection.set())
1237 // OK, we have a selection. This is always between selection.start
1238 // and selection.end
1240 // make sure that the depth behind the selection are restored, too
1241 ParagraphList::iterator endpit = boost::next(selection.end.par());
1242 ParagraphList::iterator undoendpit = endpit;
1243 ParagraphList::iterator pars_end = ownerParagraphs().end();
1245 if (endpit != pars_end && endpit->getDepth()) {
1246 while (endpit != pars_end && endpit->getDepth()) {
1248 undoendpit = endpit;
1250 } else if (endpit != pars_end) {
1251 // because of parindents etc.
1255 recordUndo(bv(), Undo::DELETE, selection.start.par(),
1256 boost::prior(undoendpit));
1259 endpit = selection.end.par();
1260 int endpos = selection.end.pos();
1262 boost::tie(endpit, endpos) = realcut ?
1263 CutAndPaste::cutSelection(bv()->buffer()->params,
1265 selection.start.par(), endpit,
1266 selection.start.pos(), endpos,
1267 bv()->buffer()->params.textclass,
1269 : CutAndPaste::eraseSelection(bv()->buffer()->params,
1271 selection.start.par(), endpit,
1272 selection.start.pos(), endpos,
1274 // sometimes necessary
1276 selection.start.par()->stripLeadingSpaces();
1278 redoParagraphs(selection.start, boost::next(endpit));
1279 #warning FIXME latent bug
1280 // endpit will be invalidated on redoParagraphs once ParagraphList
1281 // becomes a std::list? There are maybe other places on which this
1282 // can happend? (Ab)
1283 // cutSelection can invalidate the cursor so we need to set
1285 // we prefer the end for when tracking changes
1289 // need a valid cursor. (Lgb)
1292 setCursor(cursor.par(), cursor.pos());
1293 selection.cursor = cursor;
1298 void LyXText::copySelection()
1300 // stuff the selection onto the X clipboard, from an explicit copy request
1301 bv()->stuffClipboard(selectionAsString(bv()->buffer(), true));
1303 // this doesnt make sense, if there is no selection
1304 if (!selection.set())
1307 // ok we have a selection. This is always between selection.start
1308 // and sel_end cursor
1310 // copy behind a space if there is one
1311 while (selection.start.par()->size() > selection.start.pos()
1312 && selection.start.par()->isLineSeparator(selection.start.pos())
1313 && (selection.start.par() != selection.end.par()
1314 || selection.start.pos() < selection.end.pos()))
1315 selection.start.pos(selection.start.pos() + 1);
1317 CutAndPaste::copySelection(selection.start.par(),
1318 selection.end.par(),
1319 selection.start.pos(), selection.end.pos(),
1320 bv()->buffer()->params.textclass);
1324 void LyXText::pasteSelection(size_t sel_index)
1326 // this does not make sense, if there is nothing to paste
1327 if (!CutAndPaste::checkPastePossible())
1330 recordUndo(bv(), Undo::INSERT, cursor.par());
1332 ParagraphList::iterator endpit;
1337 boost::tie(ppp, endpit) =
1338 CutAndPaste::pasteSelection(*bv()->buffer(),
1340 cursor.par(), cursor.pos(),
1341 bv()->buffer()->params.textclass,
1343 bufferErrors(*bv()->buffer(), el);
1344 bv()->showErrorList(_("Paste"));
1346 redoParagraphs(cursor, endpit);
1348 setCursor(cursor.par(), cursor.pos());
1351 selection.cursor = cursor;
1352 setCursor(ppp.first, ppp.second);
1358 void LyXText::setSelectionRange(lyx::pos_type length)
1363 selection.cursor = cursor;
1370 // simple replacing. The font of the first selected character is used
1371 void LyXText::replaceSelectionWithString(string const & str)
1373 recordUndo(bv(), Undo::ATOMIC);
1376 if (!selection.set()) { // create a dummy selection
1377 selection.end = cursor;
1378 selection.start = cursor;
1381 // Get font setting before we cut
1382 pos_type pos = selection.end.pos();
1383 LyXFont const font = selection.start.par()
1384 ->getFontSettings(bv()->buffer()->params,
1385 selection.start.pos());
1387 // Insert the new string
1388 string::const_iterator cit = str.begin();
1389 string::const_iterator end = str.end();
1390 for (; cit != end; ++cit) {
1391 selection.end.par()->insertChar(pos, (*cit), font);
1395 // Cut the selection
1396 cutSelection(true, false);
1402 // needed to insert the selection
1403 void LyXText::insertStringAsLines(string const & str)
1405 ParagraphList::iterator pit = cursor.par();
1406 pos_type pos = cursor.pos();
1407 ParagraphList::iterator endpit = boost::next(cursor.par());
1409 recordUndo(bv(), Undo::ATOMIC);
1411 // only to be sure, should not be neccessary
1414 bv()->buffer()->insertStringAsLines(pit, pos, current_font, str);
1416 redoParagraphs(cursor, endpit);
1417 setCursor(cursor.par(), cursor.pos());
1418 selection.cursor = cursor;
1419 setCursor(pit, pos);
1424 // turns double-CR to single CR, others where converted into one
1425 // blank. Then InsertStringAsLines is called
1426 void LyXText::insertStringAsParagraphs(string const & str)
1428 string linestr(str);
1429 bool newline_inserted = false;
1430 string::size_type const siz = linestr.length();
1432 for (string::size_type i = 0; i < siz; ++i) {
1433 if (linestr[i] == '\n') {
1434 if (newline_inserted) {
1435 // we know that \r will be ignored by
1436 // InsertStringA. Of course, it is a dirty
1437 // trick, but it works...
1438 linestr[i - 1] = '\r';
1442 newline_inserted = true;
1444 } else if (IsPrintable(linestr[i])) {
1445 newline_inserted = false;
1448 insertStringAsLines(linestr);
1452 void LyXText::checkParagraph(ParagraphList::iterator pit, pos_type pos)
1454 breakAgain(getRow(pit, pos));
1456 setCursorIntern(cursor.par(), cursor.pos(), false, cursor.boundary());
1460 // returns false if inset wasn't found
1461 bool LyXText::updateInset(InsetOld * inset)
1463 // first check the current paragraph
1464 int pos = cursor.par()->getPositionOfInset(inset);
1466 checkParagraph(cursor.par(), pos);
1470 // check every paragraph
1471 ParagraphList::iterator par = ownerParagraphs().begin();
1472 ParagraphList::iterator end = ownerParagraphs().end();
1473 for (; par != end; ++par) {
1474 pos = par->getPositionOfInset(inset);
1476 checkParagraph(par, pos);
1485 bool LyXText::setCursor(ParagraphList::iterator pit,
1487 bool setfont, bool boundary)
1489 LyXCursor old_cursor = cursor;
1490 setCursorIntern(pit, pos, setfont, boundary);
1491 return deleteEmptyParagraphMechanism(old_cursor);
1495 void LyXText::setCursor(LyXCursor & cur, ParagraphList::iterator pit,
1496 pos_type pos, bool boundary)
1498 Assert(pit != ownerParagraphs().end());
1502 cur.boundary(boundary);
1506 // get the cursor y position in text
1508 RowList::iterator row = getRow(pit, pos, y);
1509 RowList::iterator beg = rows().begin();
1511 RowList::iterator old_row = row;
1512 // if we are before the first char of this row and are still in the
1513 // same paragraph and there is a previous row then put the cursor on
1514 // the end of the previous row
1515 cur.iy(y + row->baseline());
1518 boost::prior(row)->par() == row->par() &&
1519 pos < pit->size() &&
1520 pit->getChar(pos) == Paragraph::META_INSET) {
1521 InsetOld * ins = pit->getInset(pos);
1522 if (ins && (ins->needFullRow() || ins->display())) {
1528 // y is now the beginning of the cursor row
1529 y += row->baseline();
1530 // y is now the cursor baseline
1533 pos_type last = lastPrintablePos(*this, old_row);
1535 // None of these should happen, but we're scaredy-cats
1536 if (pos > pit->size()) {
1537 lyxerr << "dont like 1 please report" << endl;
1540 } else if (pos > last + 1) {
1541 lyxerr << "dont like 2 please report" << endl;
1542 // This shouldn't happen.
1545 } else if (pos < row->pos()) {
1546 lyxerr << "dont like 3 please report" << endl;
1551 // now get the cursors x position
1552 float x = getCursorX(row, pos, last, boundary);
1555 if (old_row != row) {
1556 x = getCursorX(old_row, pos, last, boundary);
1560 /* We take out this for the time being because 1) the redraw code is not
1561 prepared to this yet and 2) because some good policy has yet to be decided
1562 while editting: for instance how to act on rows being created/deleted
1566 //if the cursor is in a visible row, anchor to it
1568 if (topy < y && y < topy + bv()->workHeight())
1574 float LyXText::getCursorX(RowList::iterator rit,
1575 pos_type pos, pos_type last, bool boundary) const
1577 pos_type cursor_vpos = 0;
1579 double fill_separator;
1581 double fill_label_hfill;
1582 // This call HAS to be here because of the BidiTables!!!
1583 prepareToPrint(rit, x, fill_separator, fill_hfill,
1586 ParagraphList::iterator rit_par = rit->par();
1587 pos_type const rit_pos = rit->pos();
1590 cursor_vpos = rit_pos;
1591 else if (pos > last && !boundary)
1592 cursor_vpos = (rit_par->isRightToLeftPar(bv()->buffer()->params))
1593 ? rit_pos : last + 1;
1594 else if (pos > rit_pos && (pos > last || boundary))
1595 /// Place cursor after char at (logical) position pos - 1
1596 cursor_vpos = (bidi_level(pos - 1) % 2 == 0)
1597 ? log2vis(pos - 1) + 1 : log2vis(pos - 1);
1599 /// Place cursor before char at (logical) position pos
1600 cursor_vpos = (bidi_level(pos) % 2 == 0)
1601 ? log2vis(pos) : log2vis(pos) + 1;
1603 pos_type body_pos = rit_par->beginningOfBody();
1605 (body_pos - 1 > last || !rit_par->isLineSeparator(body_pos - 1)))
1608 for (pos_type vpos = rit_pos; vpos < cursor_vpos; ++vpos) {
1609 pos_type pos = vis2log(vpos);
1610 if (body_pos > 0 && pos == body_pos - 1) {
1611 x += fill_label_hfill +
1612 font_metrics::width(
1613 rit_par->layout()->labelsep, getLabelFont(rit_par));
1614 if (rit_par->isLineSeparator(body_pos - 1))
1615 x -= singleWidth(rit_par, body_pos - 1);
1618 if (hfillExpansion(*this, rit, pos)) {
1619 x += singleWidth(rit_par, pos);
1620 if (pos >= body_pos)
1623 x += fill_label_hfill;
1624 } else if (rit_par->isSeparator(pos)) {
1625 x += singleWidth(rit_par, pos);
1626 if (pos >= body_pos)
1627 x += fill_separator;
1629 x += singleWidth(rit_par, pos);
1635 void LyXText::setCursorIntern(ParagraphList::iterator pit,
1636 pos_type pos, bool setfont, bool boundary)
1638 UpdatableInset * it = pit->inInset();
1640 if (it != inset_owner) {
1641 lyxerr[Debug::INSETS] << "InsetText is " << it
1643 << "inset_owner is "
1644 << inset_owner << endl;
1645 #ifdef WITH_WARNINGS
1646 #warning I believe this code is wrong. (Lgb)
1647 #warning Jürgen, have a look at this. (Lgb)
1648 #warning Hmmm, I guess you are right but we
1649 #warning should verify when this is needed
1651 // Jürgen, would you like to have a look?
1652 // I guess we need to move the outer cursor
1653 // and open and lock the inset (bla bla bla)
1654 // stuff I don't know... so can you have a look?
1656 // I moved the lyxerr stuff in here so we can see if
1657 // this is actually really needed and where!
1659 // it->getLyXText(bv())->setCursorIntern(bv(), par, pos, setfont, boundary);
1664 setCursor(cursor, pit, pos, boundary);
1670 void LyXText::setCurrentFont()
1672 pos_type pos = cursor.pos();
1673 ParagraphList::iterator pit = cursor.par();
1675 if (cursor.boundary() && pos > 0)
1679 if (pos == pit->size())
1681 else // potentional bug... BUG (Lgb)
1682 if (pit->isSeparator(pos)) {
1683 if (pos > cursorRow()->pos() &&
1684 bidi_level(pos) % 2 ==
1685 bidi_level(pos - 1) % 2)
1687 else if (pos + 1 < pit->size())
1692 current_font = pit->getFontSettings(bv()->buffer()->params, pos);
1693 real_current_font = getFont(pit, pos);
1695 if (cursor.pos() == pit->size() &&
1696 isBoundary(bv()->buffer(), *pit, cursor.pos()) &&
1697 !cursor.boundary()) {
1698 Language const * lang =
1699 pit->getParLanguage(bv()->buffer()->params);
1700 current_font.setLanguage(lang);
1701 current_font.setNumber(LyXFont::OFF);
1702 real_current_font.setLanguage(lang);
1703 real_current_font.setNumber(LyXFont::OFF);
1708 // returns the column near the specified x-coordinate of the row
1709 // x is set to the real beginning of this column
1711 LyXText::getColumnNearX(RowList::iterator rit, int & x, bool & boundary) const
1714 double fill_separator;
1716 double fill_label_hfill;
1718 prepareToPrint(rit, tmpx, fill_separator, fill_hfill, fill_label_hfill);
1720 pos_type vc = rit->pos();
1721 pos_type last = lastPrintablePos(*this, rit);
1724 ParagraphList::iterator rit_par = rit->par();
1725 LyXLayout_ptr const & layout = rit->par()->layout();
1727 bool left_side = false;
1729 pos_type body_pos = rit_par->beginningOfBody();
1730 double last_tmpx = tmpx;
1733 (body_pos - 1 > last ||
1734 !rit_par->isLineSeparator(body_pos - 1)))
1737 // check for empty row
1738 if (!rit_par->size()) {
1743 while (vc <= last && tmpx <= x) {
1746 if (body_pos > 0 && c == body_pos - 1) {
1747 tmpx += fill_label_hfill +
1748 font_metrics::width(layout->labelsep, getLabelFont(rit_par));
1749 if (rit_par->isLineSeparator(body_pos - 1))
1750 tmpx -= singleWidth(rit_par, body_pos - 1);
1753 if (hfillExpansion(*this, rit, c)) {
1754 tmpx += singleWidth(rit_par, c);
1758 tmpx += fill_label_hfill;
1759 } else if (rit_par->isSeparator(c)) {
1760 tmpx += singleWidth(rit_par, c);
1762 tmpx += fill_separator;
1764 tmpx += singleWidth(rit_par, c);
1769 if ((tmpx + last_tmpx) / 2 > x) {
1774 if (vc > last + 1) // This shouldn't happen.
1778 // This (rtl_support test) is not needed, but gives
1779 // some speedup if rtl_support=false
1780 RowList::iterator next_rit = boost::next(rit);
1782 bool const lastrow = lyxrc.rtl_support &&
1783 (next_rit == rowlist_.end() ||
1784 next_rit->par() != rit_par);
1786 // If lastrow is false, we don't need to compute
1787 // the value of rtl.
1788 bool const rtl = (lastrow)
1789 ? rit_par->isRightToLeftPar(bv()->buffer()->params)
1792 ((rtl && left_side && vc == rit->pos() && x < tmpx - 5) ||
1793 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5)))
1795 else if (vc == rit->pos()) {
1797 if (bidi_level(c) % 2 == 1)
1800 c = vis2log(vc - 1);
1801 bool const rtl = (bidi_level(c) % 2 == 1);
1802 if (left_side == rtl) {
1804 boundary = isBoundary(bv()->buffer(), *rit_par, c);
1808 if (rit->pos() <= last && c > last
1809 && rit_par->isNewline(last)) {
1810 if (bidi_level(last) % 2 == 0)
1811 tmpx -= singleWidth(rit_par, last);
1813 tmpx += singleWidth(rit_par, last);
1823 void LyXText::setCursorFromCoordinates(int x, int y)
1825 //LyXCursor old_cursor = cursor;
1826 setCursorFromCoordinates(cursor, x, y);
1828 #warning DEPM disabled, otherwise crash when entering new table
1829 //deleteEmptyParagraphMechanism(old_cursor);
1836 * return true if the cursor given is at the end of a row,
1837 * and the next row is filled by an inset that spans an entire
1840 bool beforeFullRowInset(LyXText & lt, LyXCursor const & cur)
1842 RowList::iterator row = lt.getRow(cur);
1843 if (boost::next(row) == lt.rows().end())
1846 Row const & next = *boost::next(row);
1848 if (next.pos() != cur.pos() || next.par() != cur.par())
1851 if (cur.pos() == cur.par()->size()
1852 || !cur.par()->isInset(cur.pos()))
1855 InsetOld const * inset = cur.par()->getInset(cur.pos());
1856 if (inset->needFullRow() || inset->display())
1864 void LyXText::setCursorFromCoordinates(LyXCursor & cur, int x, int y)
1866 // Get the row first.
1868 RowList::iterator row = getRowNearY(y);
1870 pos_type const column = getColumnNearX(row, x, bound);
1871 cur.par(row->par());
1872 cur.pos(row->pos() + column);
1874 cur.y(y + row->baseline());
1876 if (beforeFullRowInset(*this, cur)) {
1877 pos_type const last = lastPrintablePos(*this, row);
1878 RowList::iterator next_row = boost::next(row);
1880 float x = getCursorX(next_row, cur.pos(), last, bound);
1882 cur.iy(y + row->height() + next_row->baseline());
1887 cur.boundary(bound);
1891 void LyXText::cursorLeft(bool internal)
1893 if (cursor.pos() > 0) {
1894 bool boundary = cursor.boundary();
1895 setCursor(cursor.par(), cursor.pos() - 1, true, false);
1896 if (!internal && !boundary &&
1897 isBoundary(bv()->buffer(), *cursor.par(), cursor.pos() + 1))
1898 setCursor(cursor.par(), cursor.pos() + 1, true, true);
1899 } else if (cursor.par() != ownerParagraphs().begin()) { // steps into the above paragraph.
1900 ParagraphList::iterator pit = boost::prior(cursor.par());
1901 setCursor(pit, pit->size());
1906 void LyXText::cursorRight(bool internal)
1908 bool const at_end = (cursor.pos() == cursor.par()->size());
1909 bool const at_newline = !at_end &&
1910 cursor.par()->isNewline(cursor.pos());
1912 if (!internal && cursor.boundary() && !at_newline)
1913 setCursor(cursor.par(), cursor.pos(), true, false);
1915 setCursor(cursor.par(), cursor.pos() + 1, true, false);
1917 isBoundary(bv()->buffer(), *cursor.par(), cursor.pos()))
1918 setCursor(cursor.par(), cursor.pos(), true, true);
1919 } else if (boost::next(cursor.par()) != ownerParagraphs().end())
1920 setCursor(boost::next(cursor.par()), 0);
1924 void LyXText::cursorUp(bool selecting)
1927 int x = cursor.x_fix();
1928 int y = cursor.y() - cursorRow()->baseline() - 1;
1929 setCursorFromCoordinates(x, y);
1932 int y1 = cursor.iy() - topy;
1935 InsetOld * inset_hit = checkInsetHit(x, y1);
1936 if (inset_hit && isHighlyEditableInset(inset_hit)) {
1937 inset_hit->localDispatch(
1938 FuncRequest(bv(), LFUN_INSET_EDIT, x, y - (y2 - y1), mouse_button::none));
1942 setCursorFromCoordinates(bv(), cursor.x_fix(),
1943 cursor.y() - cursorRow()->baseline() - 1);
1948 void LyXText::cursorDown(bool selecting)
1951 int x = cursor.x_fix();
1952 int y = cursor.y() - cursorRow()->baseline() + cursorRow()->height() + 1;
1953 setCursorFromCoordinates(x, y);
1954 if (!selecting && cursorRow() == cursorIRow()) {
1956 int y1 = cursor.iy() - topy;
1959 InsetOld * inset_hit = checkInsetHit(x, y1);
1960 if (inset_hit && isHighlyEditableInset(inset_hit)) {
1961 FuncRequest cmd(bv(), LFUN_INSET_EDIT, x, y - (y2 - y1), mouse_button::none);
1962 inset_hit->localDispatch(cmd);
1966 setCursorFromCoordinates(bv(), cursor.x_fix(),
1967 cursor.y() - cursorRow()->baseline()
1968 + cursorRow()->height() + 1);
1973 void LyXText::cursorUpParagraph()
1975 if (cursor.pos() > 0) {
1976 setCursor(cursor.par(), 0);
1978 else if (cursor.par() != ownerParagraphs().begin()) {
1979 setCursor(boost::prior(cursor.par()), 0);
1984 void LyXText::cursorDownParagraph()
1986 ParagraphList::iterator par = cursor.par();
1987 ParagraphList::iterator next_par = boost::next(par);
1989 if (next_par != ownerParagraphs().end()) {
1990 setCursor(next_par, 0);
1992 setCursor(par, par->size());
1996 // fix the cursor `cur' after a characters has been deleted at `where'
1997 // position. Called by deleteEmptyParagraphMechanism
1998 void LyXText::fixCursorAfterDelete(LyXCursor & cur,
1999 LyXCursor const & where)
2001 // if cursor is not in the paragraph where the delete occured,
2003 if (cur.par() != where.par())
2006 // if cursor position is after the place where the delete occured,
2008 if (cur.pos() > where.pos())
2009 cur.pos(cur.pos()-1);
2011 // check also if we don't want to set the cursor on a spot behind the
2012 // pagragraph because we erased the last character.
2013 if (cur.pos() > cur.par()->size())
2014 cur.pos(cur.par()->size());
2016 // recompute row et al. for this cursor
2017 setCursor(cur, cur.par(), cur.pos(), cur.boundary());
2021 bool LyXText::deleteEmptyParagraphMechanism(LyXCursor const & old_cursor)
2023 // Would be wrong to delete anything if we have a selection.
2024 if (selection.set())
2027 // We allow all kinds of "mumbo-jumbo" when freespacing.
2028 if (old_cursor.par()->layout()->free_spacing
2029 || old_cursor.par()->isFreeSpacing()) {
2033 /* Ok I'll put some comments here about what is missing.
2034 I have fixed BackSpace (and thus Delete) to not delete
2035 double-spaces automagically. I have also changed Cut,
2036 Copy and Paste to hopefully do some sensible things.
2037 There are still some small problems that can lead to
2038 double spaces stored in the document file or space at
2039 the beginning of paragraphs. This happens if you have
2040 the cursor betwenn to spaces and then save. Or if you
2041 cut and paste and the selection have a space at the
2042 beginning and then save right after the paste. I am
2043 sure none of these are very hard to fix, but I will
2044 put out 1.1.4pre2 with FIX_DOUBLE_SPACE defined so
2045 that I can get some feedback. (Lgb)
2048 // If old_cursor.pos() == 0 and old_cursor.pos()(1) == LineSeparator
2049 // delete the LineSeparator.
2052 // If old_cursor.pos() == 1 and old_cursor.pos()(0) == LineSeparator
2053 // delete the LineSeparator.
2056 // If the pos around the old_cursor were spaces, delete one of them.
2057 if (old_cursor.par() != cursor.par()
2058 || old_cursor.pos() != cursor.pos()) {
2059 // Only if the cursor has really moved
2061 if (old_cursor.pos() > 0
2062 && old_cursor.pos() < old_cursor.par()->size()
2063 && old_cursor.par()->isLineSeparator(old_cursor.pos())
2064 && old_cursor.par()->isLineSeparator(old_cursor.pos() - 1)) {
2065 old_cursor.par()->erase(old_cursor.pos() - 1);
2066 redoParagraphs(old_cursor, boost::next(old_cursor.par()));
2068 #ifdef WITH_WARNINGS
2069 #warning This will not work anymore when we have multiple views of the same buffer
2070 // In this case, we will have to correct also the cursors held by
2071 // other bufferviews. It will probably be easier to do that in a more
2072 // automated way in LyXCursor code. (JMarc 26/09/2001)
2074 // correct all cursors held by the LyXText
2075 fixCursorAfterDelete(cursor, old_cursor);
2076 fixCursorAfterDelete(selection.cursor, old_cursor);
2077 fixCursorAfterDelete(selection.start, old_cursor);
2078 fixCursorAfterDelete(selection.end, old_cursor);
2079 fixCursorAfterDelete(last_sel_cursor, old_cursor);
2084 // don't delete anything if this is the ONLY paragraph!
2085 if (ownerParagraphs().size() == 1)
2088 // Do not delete empty paragraphs with keepempty set.
2089 if (old_cursor.par()->allowEmpty())
2092 // only do our magic if we changed paragraph
2093 if (old_cursor.par() == cursor.par())
2096 // record if we have deleted a paragraph
2097 // we can't possibly have deleted a paragraph before this point
2098 bool deleted = false;
2100 if (old_cursor.par()->empty() ||
2101 (old_cursor.par()->size() == 1 &&
2102 old_cursor.par()->isLineSeparator(0))) {
2103 // ok, we will delete anything
2104 LyXCursor tmpcursor;
2108 bool selection_position_was_oldcursor_position = (
2109 selection.cursor.par() == old_cursor.par()
2110 && selection.cursor.pos() == old_cursor.pos());
2112 if (getRow(old_cursor) != rows().begin()) {
2113 RowList::iterator prevrow = boost::prior(getRow(old_cursor));
2116 cursor = old_cursor; // that undo can restore the right cursor position
2117 #warning FIXME. --end() iterator is usable here
2118 ParagraphList::iterator endpit = boost::next(old_cursor.par());
2119 while (endpit != ownerParagraphs().end() &&
2120 endpit->getDepth()) {
2124 recordUndo(bv(), Undo::DELETE, old_cursor.par(),
2125 boost::prior(endpit));
2129 removeRow(getRow(old_cursor));
2131 ownerParagraphs().erase(old_cursor.par());
2133 /* Breakagain the next par. Needed because of
2134 * the parindent that can occur or dissappear.
2135 * The next row can change its height, if
2136 * there is another layout before */
2137 RowList::iterator tmprit = boost::next(prevrow);
2138 if (tmprit != rows().end()) {
2142 setHeightOfRow(prevrow);
2144 RowList::iterator nextrow = boost::next(getRow(old_cursor));
2148 cursor = old_cursor; // that undo can restore the right cursor position
2149 #warning FIXME. --end() iterator is usable here
2150 ParagraphList::iterator endpit = boost::next(old_cursor.par());
2151 while (endpit != ownerParagraphs().end() &&
2152 endpit->getDepth()) {
2156 recordUndo(bv(), Undo::DELETE, old_cursor.par(), boost::prior(endpit));
2160 removeRow(getRow(old_cursor));
2162 ownerParagraphs().erase(old_cursor.par());
2164 /* Breakagain the next par. Needed because of
2165 the parindent that can occur or dissappear.
2166 The next row can change its height, if
2167 there is another layout before */
2168 if (nextrow != rows().end()) {
2169 breakAgain(nextrow);
2175 setCursorIntern(cursor.par(), cursor.pos());
2177 if (selection_position_was_oldcursor_position) {
2178 // correct selection
2179 selection.cursor = cursor;
2183 if (old_cursor.par()->stripLeadingSpaces()) {
2184 redoParagraphs(old_cursor, boost::next(old_cursor.par()));
2186 setCursorIntern(cursor.par(), cursor.pos());
2187 selection.cursor = cursor;
2194 ParagraphList & LyXText::ownerParagraphs() const
2197 return inset_owner->paragraphs;
2199 return bv_owner->buffer()->paragraphs;
2203 bool LyXText::needRefresh() const
2205 return need_refresh_;
2209 void LyXText::clearPaint()
2211 need_refresh_ = false;
2215 void LyXText::postPaint()
2217 need_refresh_ = true;
2219 // We are an inset's lyxtext. Tell the top-level lyxtext
2220 // it needs to update the row we're in.
2222 bv()->text->postPaint();
2226 bool LyXText::isInInset() const
2228 // Sub-level has non-null bv owner and
2229 // non-null inset owner.
2230 return inset_owner != 0 && bv_owner != 0;
2234 int defaultRowHeight()
2236 LyXFont const font(LyXFont::ALL_SANE);
2237 return int(font_metrics::maxAscent(font)
2238 + font_metrics::maxDescent(font) * 1.5);