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();
71 LyXText::LyXText(BufferView * bv, InsetText * inset)
72 : height(0), width(0), anchor_row_offset_(0),
73 inset_owner(inset), the_locking_inset(0), bv_owner(bv)
75 anchor_row_ = rows().end();
79 void LyXText::init(BufferView * bview)
86 anchor_row_ = rows().end();
87 anchor_row_offset_ = 0;
89 current_font = getFont(ownerParagraphs().begin(), 0);
91 redoParagraphs(ownerParagraphs().begin(), ownerParagraphs().end());
93 setCursorIntern(rowlist_.begin()->par(), 0);
94 selection.cursor = cursor;
100 // Gets the fully instantiated font at a given position in a paragraph
101 // Basically the same routine as Paragraph::getFont() in paragraph.C.
102 // The difference is that this one is used for displaying, and thus we
103 // are allowed to make cosmetic improvements. For instance make footnotes
105 // If position is -1, we get the layout font of the paragraph.
106 // If position is -2, we get the font of the manual label of the paragraph.
107 LyXFont LyXText::getFont(ParagraphList::iterator pit, pos_type pos) const
111 LyXLayout_ptr const & layout = pit->layout();
113 BufferParams const & params = bv()->buffer()->params;
115 // We specialize the 95% common case:
116 if (!pit->getDepth()) {
117 if (layout->labeltype == LABEL_MANUAL
118 && pos < pit->beginningOfBody()) {
120 LyXFont f = pit->getFontSettings(params, pos);
122 pit->inInset()->getDrawFont(f);
123 return f.realize(layout->reslabelfont);
125 LyXFont f = pit->getFontSettings(params, pos);
127 pit->inInset()->getDrawFont(f);
128 return f.realize(layout->resfont);
132 // The uncommon case need not be optimized as much
136 if (pos < pit->beginningOfBody()) {
138 layoutfont = layout->labelfont;
141 layoutfont = layout->font;
144 LyXFont tmpfont = pit->getFontSettings(params, pos);
145 tmpfont.realize(layoutfont);
148 pit->inInset()->getDrawFont(tmpfont);
150 // Realize with the fonts of lesser depth.
151 tmpfont.realize(outerFont(pit, ownerParagraphs()));
152 tmpfont.realize(defaultfont_);
158 LyXFont LyXText::getLayoutFont(ParagraphList::iterator pit) const
160 LyXLayout_ptr const & layout = pit->layout();
162 if (!pit->getDepth())
163 return layout->resfont;
165 LyXFont font = layout->font;
166 // Realize with the fonts of lesser depth.
167 font.realize(outerFont(pit, ownerParagraphs()));
168 font.realize(defaultfont_);
174 LyXFont LyXText::getLabelFont(ParagraphList::iterator pit) const
176 LyXLayout_ptr const & layout = pit->layout();
178 if (!pit->getDepth())
179 return layout->reslabelfont;
181 LyXFont font = layout->labelfont;
182 // Realize with the fonts of lesser depth.
183 font.realize(outerFont(pit, ownerParagraphs()));
184 font.realize(defaultfont_);
190 void LyXText::setCharFont(ParagraphList::iterator pit,
191 pos_type pos, LyXFont const & fnt,
194 BufferParams const & params = bv()->buffer()->params;
195 LyXFont font = getFont(pit, pos);
196 font.update(fnt, params.language, toggleall);
197 // Let the insets convert their font
198 if (pit->isInset(pos)) {
199 InsetOld * inset = pit->getInset(pos);
200 if (isEditableInset(inset)) {
201 static_cast<UpdatableInset *>(inset)
202 ->setFont(bv(), fnt, toggleall, true);
206 // Plug through to version below:
207 setCharFont(pit, pos, font);
211 void LyXText::setCharFont(
212 ParagraphList::iterator pit, pos_type pos, LyXFont const & fnt)
215 LyXLayout_ptr const & layout = pit->layout();
217 // Get concrete layout font to reduce against
220 if (pos < pit->beginningOfBody())
221 layoutfont = layout->labelfont;
223 layoutfont = layout->font;
225 // Realize against environment font information
226 if (pit->getDepth()) {
227 ParagraphList::iterator tp = pit;
228 while (!layoutfont.resolved() &&
229 tp != ownerParagraphs().end() &&
231 tp = outerHook(tp, ownerParagraphs());
232 if (tp != ownerParagraphs().end())
233 layoutfont.realize(tp->layout()->font);
237 layoutfont.realize(defaultfont_);
239 // Now, reduce font against full layout font
240 font.reduce(layoutfont);
242 pit->setFont(pos, font);
246 // removes the row and reset the touched counters
247 void LyXText::removeRow(RowList::iterator rit)
249 if (anchor_row_ == rit) {
250 if (rit != rows().begin()) {
251 anchor_row_ = boost::prior(rit);
252 anchor_row_offset_ += anchor_row_->height();
254 anchor_row_ = boost::next(rit);
255 anchor_row_offset_ -= rit->height();
259 // the text becomes smaller
260 height -= rit->height();
266 // remove all following rows of the paragraph of the specified row.
267 void LyXText::removeParagraph(RowList::iterator rit)
269 ParagraphList::iterator tmppit = rit->par();
272 while (rit != rows().end() && rit->par() == tmppit) {
273 RowList::iterator tmprit = boost::next(rit);
280 void LyXText::insertParagraph(ParagraphList::iterator pit,
281 RowList::iterator rit)
283 // insert a new row, starting at position 0
284 rit = rowlist_.insert(rit, Row(pit, 0));
286 // and now append the whole paragraph before the new row
287 appendParagraph(rit);
291 InsetOld * LyXText::getInset() const
293 ParagraphList::iterator pit = cursor.par();
294 pos_type const pos = cursor.pos();
296 if (pos < pit->size() && pit->isInset(pos)) {
297 return pit->getInset(pos);
303 void LyXText::toggleInset()
305 InsetOld * inset = getInset();
306 // is there an editable inset at cursor position?
307 if (!isEditableInset(inset)) {
308 // No, try to see if we are inside a collapsable inset
309 if (inset_owner && inset_owner->owner()
310 && inset_owner->owner()->isOpen()) {
311 bv()->unlockInset(inset_owner->owner());
312 inset_owner->owner()->close(bv());
313 bv()->getLyXText()->cursorRight(bv());
317 //bv()->owner()->message(inset->editMessage());
319 // do we want to keep this?? (JMarc)
320 if (!isHighlyEditableInset(inset))
321 recordUndo(bv(), Undo::ATOMIC);
323 if (inset->isOpen()) {
329 bv()->updateInset(inset);
333 /* used in setlayout */
334 // Asger is not sure we want to do this...
335 void LyXText::makeFontEntriesLayoutSpecific(BufferParams const & params,
338 LyXLayout_ptr const & layout = par.layout();
339 pos_type const psize = par.size();
342 for (pos_type pos = 0; pos < psize; ++pos) {
343 if (pos < par.beginningOfBody())
344 layoutfont = layout->labelfont;
346 layoutfont = layout->font;
348 LyXFont tmpfont = par.getFontSettings(params, pos);
349 tmpfont.reduce(layoutfont);
350 par.setFont(pos, tmpfont);
355 ParagraphList::iterator
356 LyXText::setLayout(LyXCursor & cur, LyXCursor & sstart_cur,
357 LyXCursor & send_cur,
358 string const & layout)
360 ParagraphList::iterator endpit = boost::next(send_cur.par());
361 ParagraphList::iterator undoendpit = endpit;
362 ParagraphList::iterator pars_end = ownerParagraphs().end();
364 if (endpit != pars_end && endpit->getDepth()) {
365 while (endpit != pars_end && endpit->getDepth()) {
369 } else if (endpit != pars_end) {
370 // because of parindents etc.
374 recordUndo(bv(), Undo::ATOMIC, sstart_cur.par(), boost::prior(undoendpit));
376 // ok we have a selection. This is always between sstart_cur
377 // and sel_end cursor
379 ParagraphList::iterator pit = sstart_cur.par();
380 ParagraphList::iterator epit = boost::next(send_cur.par());
382 LyXLayout_ptr const & lyxlayout =
383 bv()->buffer()->params.getLyXTextClass()[layout];
386 pit->applyLayout(lyxlayout);
387 makeFontEntriesLayoutSpecific(bv()->buffer()->params, *pit);
388 ParagraphList::iterator fppit = pit;
389 fppit->params().spaceTop(lyxlayout->fill_top ?
390 VSpace(VSpace::VFILL)
391 : VSpace(VSpace::NONE));
392 fppit->params().spaceBottom(lyxlayout->fill_bottom ?
393 VSpace(VSpace::VFILL)
394 : VSpace(VSpace::NONE));
395 if (lyxlayout->margintype == MARGIN_MANUAL)
396 pit->setLabelWidthString(lyxlayout->labelstring());
399 } while (pit != epit);
405 // set layout over selection and make a total rebreak of those paragraphs
406 void LyXText::setLayout(string const & layout)
408 LyXCursor tmpcursor = cursor; // store the current cursor
410 // if there is no selection just set the layout
411 // of the current paragraph
412 if (!selection.set()) {
413 selection.start = cursor; // dummy selection
414 selection.end = cursor;
417 // special handling of new environment insets
418 BufferParams const & params = bv()->buffer()->params;
419 LyXLayout_ptr const & lyxlayout = params.getLyXTextClass()[layout];
420 if (lyxlayout->is_environment) {
421 // move everything in a new environment inset
422 lyxerr << "setting layout " << layout << endl;
423 bv()->owner()->dispatch(FuncRequest(LFUN_HOME));
424 bv()->owner()->dispatch(FuncRequest(LFUN_ENDSEL));
425 bv()->owner()->dispatch(FuncRequest(LFUN_CUT));
426 InsetOld * inset = new InsetEnvironment(params, layout);
427 if (bv()->insertInset(inset)) {
429 //bv()->owner()->dispatch(FuncRequest(LFUN_PASTE));
436 ParagraphList::iterator endpit = setLayout(cursor, selection.start,
437 selection.end, layout);
438 redoParagraphs(selection.start.par(), endpit);
440 // we have to reset the selection, because the
441 // geometry could have changed
442 setCursor(selection.start.par(), selection.start.pos(), false);
443 selection.cursor = cursor;
444 setCursor(selection.end.par(), selection.end.pos(), false);
448 setCursor(tmpcursor.par(), tmpcursor.pos(), true);
452 bool LyXText::changeDepth(bv_funcs::DEPTH_CHANGE type, bool test_only)
454 ParagraphList::iterator pit(cursor.par());
455 ParagraphList::iterator end(cursor.par());
456 ParagraphList::iterator start = pit;
458 if (selection.set()) {
459 pit = selection.start.par();
460 end = selection.end.par();
464 ParagraphList::iterator pastend = boost::next(end);
467 recordUndo(bv(), Undo::ATOMIC, start, end);
469 bool changed = false;
471 int prev_after_depth = 0;
472 #warning parlist ... could be nicer ?
473 if (start != ownerParagraphs().begin()) {
474 prev_after_depth = boost::prior(start)->getMaxDepthAfter();
478 int const depth = pit->params().depth();
479 if (type == bv_funcs::INC_DEPTH) {
480 if (depth < prev_after_depth
481 && pit->layout()->labeltype != LABEL_BIBLIO) {
484 pit->params().depth(depth + 1);
491 pit->params().depth(depth - 1);
494 prev_after_depth = pit->getMaxDepthAfter();
507 redoParagraphs(start, pastend);
509 // We need to actually move the text->cursor. I don't
510 // understand why ...
511 LyXCursor tmpcursor = cursor;
513 // we have to reset the visual selection because the
514 // geometry could have changed
515 if (selection.set()) {
516 setCursor(selection.start.par(), selection.start.pos());
517 selection.cursor = cursor;
518 setCursor(selection.end.par(), selection.end.pos());
521 // this handles the counter labels, and also fixes up
522 // depth values for follow-on (child) paragraphs
526 setCursor(tmpcursor.par(), tmpcursor.pos());
532 // set font over selection and make a total rebreak of those paragraphs
533 void LyXText::setFont(LyXFont const & font, bool toggleall)
535 // if there is no selection just set the current_font
536 if (!selection.set()) {
537 // Determine basis font
539 if (cursor.pos() < cursor.par()->beginningOfBody()) {
540 layoutfont = getLabelFont(cursor.par());
542 layoutfont = getLayoutFont(cursor.par());
544 // Update current font
545 real_current_font.update(font,
546 bv()->buffer()->params.language,
549 // Reduce to implicit settings
550 current_font = real_current_font;
551 current_font.reduce(layoutfont);
552 // And resolve it completely
553 real_current_font.realize(layoutfont);
558 LyXCursor tmpcursor = cursor; // store the current cursor
560 // ok we have a selection. This is always between sel_start_cursor
561 // and sel_end cursor
563 recordUndo(bv(), Undo::ATOMIC, selection.start.par(), selection.end.par());
565 cursor = selection.start;
566 while (cursor.par() != selection.end.par() ||
567 cursor.pos() < selection.end.pos())
569 if (cursor.pos() < cursor.par()->size()) {
570 // an open footnote should behave like a closed one
571 setCharFont(cursor.par(), cursor.pos(),
573 cursor.pos(cursor.pos() + 1);
576 cursor.par(boost::next(cursor.par()));
581 redoParagraph(selection.start.par());
583 // we have to reset the selection, because the
584 // geometry could have changed, but we keep
585 // it for user convenience
586 setCursor(selection.start.par(), selection.start.pos());
587 selection.cursor = cursor;
588 setCursor(selection.end.par(), selection.end.pos());
590 setCursor(tmpcursor.par(), tmpcursor.pos(), true,
591 tmpcursor.boundary());
595 void LyXText::redoHeightOfParagraph()
597 RowList::iterator tmprow = cursorRow();
599 setHeightOfRow(tmprow);
601 while (tmprow != rows().begin()
602 && boost::prior(tmprow)->par() == tmprow->par()) {
604 setHeightOfRow(tmprow);
607 setCursor(cursor.par(), cursor.pos(), false, cursor.boundary());
611 RowList::iterator LyXText::firstRow(ParagraphList::iterator pit)
613 RowList::iterator rit;
614 for (rit = rows().begin(); rit != rows().end(); ++rit)
615 if (rit->par() == pit)
621 // rebreaks all paragraphs between the specified pars
622 // This function is needed after SetLayout and SetFont etc.
623 void LyXText::redoParagraphs(ParagraphList::iterator start,
624 ParagraphList::iterator end)
626 for ( ; start != end; ++start)
627 redoParagraph(start);
631 void LyXText::redoParagraph(ParagraphList::iterator pit)
633 RowList::iterator rit = firstRow(pit);
635 // remove paragraph from rowlist
636 while (rit != rows().end() && rit->par() == pit) {
637 RowList::iterator rit2 = rit++;
641 // reinsert the paragraph
642 // insert a new row, starting at position 0
644 rit = rowlist_.insert(rit, newrow);
645 //newrow.dump("newrow: ");
647 // and now append the whole paragraph before the new row
648 pos_type const last = rit->par()->size();
652 pos_type z = rowBreakPoint(*rit);
654 RowList::iterator tmprow = rit;
655 //tmprow->dump("tmprow: ");
659 Row newrow(rit->par(), z);
660 //newrow.dump("newrow2: ");
661 rit = rowlist_.insert(boost::next(rit), newrow);
666 // Set the dimensions of the row
667 // fixed fill setting now by calling inset->update() in
668 // SingleWidth when needed!
669 //tmprow->dump("tmprow 1: ");
670 tmprow->fill(fill(tmprow, workWidth()));
671 //tmprow->dump("tmprow 2: ");
672 setHeightOfRow(tmprow);
673 //tmprow->dump("tmprow 3: ");
674 height += rit->height();
678 setHeightOfRow(rows().begin());
682 void LyXText::fullRebreak()
684 lyxerr << "fullRebreak" << endl;
685 redoParagraphs(ownerParagraphs().begin(), ownerParagraphs().end());
686 setCursorIntern(cursor.par(), cursor.pos());
687 selection.cursor = cursor;
691 void LyXText::metrics(MetricsInfo & mi, Dimension & dim)
693 //lyxerr << "LyXText::metrics: width: " << mi.base.textwidth << endl;
694 //Assert(mi.base.textwidth);
700 anchor_row_ = rows().end();
701 anchor_row_offset_ = 0;
703 ParagraphList::iterator pit = ownerParagraphs().begin();
704 ParagraphList::iterator end = ownerParagraphs().end();
706 for (; pit != end; ++pit) {
707 InsetList::iterator ii = pit->insetlist.begin();
708 InsetList::iterator iend = pit->insetlist.end();
709 for (; ii != iend; ++ii) {
712 #warning FIXME: pos != 0
713 m.base.font = getFont(pit, 0);
714 ii->inset->metrics(m, dim);
718 // insert a new row, starting at position 0
720 RowList::iterator rit = rowlist_.insert(rowlist_.end(), newrow);
722 // and now append the whole paragraph before the new row
723 appendParagraph(rit);
730 //lyxerr << "height 0: " << height << endl;
731 //for (RowList::iterator rit = rows().begin(); rit != rows().end(); ++rit) {
732 // height += rit->height();
734 //lyxerr << "height 1: " << height << endl;
737 dim.asc = rows().begin()->ascent_of_text();
738 dim.des = height - dim.asc;
739 dim.wid = std::max(mi.base.textwidth, int(width));
743 void LyXText::partialRebreak()
745 if (rows().empty()) {
749 breakAgain(rows().begin());
753 // important for the screen
756 // the cursor set functions have a special mechanism. When they
757 // realize, that you left an empty paragraph, they will delete it.
758 // They also delete the corresponding row
760 // need the selection cursor:
761 void LyXText::setSelection()
763 bool const lsel = TextCursor::setSelection();
765 if (inset_owner && (selection.set() || lsel))
766 inset_owner->setUpdateStatus(InsetText::SELECTION);
771 void LyXText::clearSelection()
773 TextCursor::clearSelection();
775 // reset this in the bv_owner!
776 if (bv_owner && bv_owner->text)
777 bv_owner->text->xsel_cache.set(false);
781 void LyXText::cursorHome()
783 setCursor(cursor.par(), cursorRow()->pos());
787 void LyXText::cursorEnd()
789 if (cursor.par()->empty())
792 RowList::iterator rit = cursorRow();
793 RowList::iterator next_rit = boost::next(rit);
794 ParagraphList::iterator pit = rit->par();
795 pos_type last_pos = lastPos(*this, rit);
797 if (next_rit == rows().end() || next_rit->par() != pit) {
801 (pit->getChar(last_pos) != ' ' && !pit->isNewline(last_pos))) {
806 setCursor(pit, last_pos);
810 void LyXText::cursorTop()
812 setCursor(ownerParagraphs().begin(), 0);
816 void LyXText::cursorBottom()
818 ParagraphList::iterator lastpit =
819 boost::prior(ownerParagraphs().end());
820 setCursor(lastpit, lastpit->size());
824 void LyXText::toggleFree(LyXFont const & font, bool toggleall)
826 // If the mask is completely neutral, tell user
827 if (font == LyXFont(LyXFont::ALL_IGNORE)) {
828 // Could only happen with user style
829 bv()->owner()->message(_("No font change defined. Use Character under the Layout menu to define font change."));
833 // Try implicit word selection
834 // If there is a change in the language the implicit word selection
836 LyXCursor resetCursor = cursor;
837 bool implicitSelection = (font.language() == ignore_language
838 && font.number() == LyXFont::IGNORE)
839 ? selectWordWhenUnderCursor(lyx::WHOLE_WORD_STRICT) : false;
842 setFont(font, toggleall);
844 // Implicit selections are cleared afterwards
845 //and cursor is set to the original position.
846 if (implicitSelection) {
848 cursor = resetCursor;
849 setCursor(cursor.par(), cursor.pos());
850 selection.cursor = cursor;
853 inset_owner->setUpdateStatus(InsetText::CURSOR_PAR);
857 string LyXText::getStringToIndex()
859 // Try implicit word selection
860 // If there is a change in the language the implicit word selection
862 LyXCursor const reset_cursor = cursor;
863 bool const implicitSelection =
864 selectWordWhenUnderCursor(lyx::PREVIOUS_WORD);
867 if (!selection.set())
868 bv()->owner()->message(_("Nothing to index!"));
869 else if (selection.start.par() != selection.end.par())
870 bv()->owner()->message(_("Cannot index more than one paragraph!"));
872 idxstring = selectionAsString(bv()->buffer(), false);
874 // Reset cursors to their original position.
875 cursor = reset_cursor;
876 setCursor(cursor.par(), cursor.pos());
877 selection.cursor = cursor;
879 // Clear the implicit selection.
880 if (implicitSelection)
887 // the DTP switches for paragraphs. LyX will store them in the first
888 // physicla paragraph. When a paragraph is broken, the top settings rest,
889 // the bottom settings are given to the new one. So I can make shure,
890 // they do not duplicate themself and you cannnot make dirty things with
893 void LyXText::setParagraph(bool line_top, bool line_bottom,
894 bool pagebreak_top, bool pagebreak_bottom,
895 VSpace const & space_top,
896 VSpace const & space_bottom,
897 Spacing const & spacing,
899 string const & labelwidthstring,
902 LyXCursor tmpcursor = cursor;
903 if (!selection.set()) {
904 selection.start = cursor;
905 selection.end = cursor;
908 // make sure that the depth behind the selection are restored, too
909 ParagraphList::iterator endpit = boost::next(selection.end.par());
910 ParagraphList::iterator undoendpit = endpit;
911 ParagraphList::iterator pars_end = ownerParagraphs().end();
913 if (endpit != pars_end && endpit->getDepth()) {
914 while (endpit != pars_end && endpit->getDepth()) {
918 } else if (endpit != pars_end) {
919 // because of parindents etc.
923 recordUndo(bv(), Undo::ATOMIC, selection.start.par(),
924 boost::prior(undoendpit));
927 ParagraphList::iterator tmppit = selection.end.par();
929 while (tmppit != boost::prior(selection.start.par())) {
930 setCursor(tmppit, 0);
932 ParagraphList::iterator pit = cursor.par();
933 ParagraphParameters & params = pit->params();
935 params.lineTop(line_top);
936 params.lineBottom(line_bottom);
937 params.pagebreakTop(pagebreak_top);
938 params.pagebreakBottom(pagebreak_bottom);
939 params.spaceTop(space_top);
940 params.spaceBottom(space_bottom);
941 params.spacing(spacing);
942 // does the layout allow the new alignment?
943 LyXLayout_ptr const & layout = pit->layout();
945 if (align == LYX_ALIGN_LAYOUT)
946 align = layout->align;
947 if (align & layout->alignpossible) {
948 if (align == layout->align)
949 params.align(LYX_ALIGN_LAYOUT);
953 pit->setLabelWidthString(labelwidthstring);
954 params.noindent(noindent);
955 tmppit = boost::prior(pit);
958 redoParagraphs(selection.start.par(), endpit);
961 setCursor(selection.start.par(), selection.start.pos());
962 selection.cursor = cursor;
963 setCursor(selection.end.par(), selection.end.pos());
965 setCursor(tmpcursor.par(), tmpcursor.pos());
967 bv()->updateInset(inset_owner);
971 // set the counter of a paragraph. This includes the labels
972 void LyXText::setCounter(Buffer const * buf, ParagraphList::iterator pit)
974 LyXTextClass const & textclass = buf->params.getLyXTextClass();
975 LyXLayout_ptr const & layout = pit->layout();
977 if (pit != ownerParagraphs().begin()) {
979 pit->params().appendix(boost::prior(pit)->params().appendix());
980 if (!pit->params().appendix() &&
981 pit->params().startOfAppendix()) {
982 pit->params().appendix(true);
983 textclass.counters().reset();
985 pit->enumdepth = boost::prior(pit)->enumdepth;
986 pit->itemdepth = boost::prior(pit)->itemdepth;
988 pit->params().appendix(pit->params().startOfAppendix());
993 /* Maybe we have to increment the enumeration depth.
994 * BUT, enumeration in a footnote is considered in isolation from its
995 * surrounding paragraph so don't increment if this is the
996 * first line of the footnote
997 * AND, bibliographies can't have their depth changed ie. they
998 * are always of depth 0
1000 if (pit != ownerParagraphs().begin()
1001 && boost::prior(pit)->getDepth() < pit->getDepth()
1002 && boost::prior(pit)->layout()->labeltype == LABEL_COUNTER_ENUMI
1003 && pit->enumdepth < 3
1004 && layout->labeltype != LABEL_BIBLIO) {
1008 // Maybe we have to decrement the enumeration depth, see note above
1009 if (pit != ownerParagraphs().begin()
1010 && boost::prior(pit)->getDepth() > pit->getDepth()
1011 && layout->labeltype != LABEL_BIBLIO) {
1012 pit->enumdepth = depthHook(pit, ownerParagraphs(),
1013 pit->getDepth())->enumdepth;
1016 if (!pit->params().labelString().empty()) {
1017 pit->params().labelString(string());
1020 if (layout->margintype == MARGIN_MANUAL) {
1021 if (pit->params().labelWidthString().empty()) {
1022 pit->setLabelWidthString(layout->labelstring());
1025 pit->setLabelWidthString(string());
1028 // is it a layout that has an automatic label?
1029 if (layout->labeltype >= LABEL_COUNTER_CHAPTER) {
1030 int const i = layout->labeltype - LABEL_COUNTER_CHAPTER;
1034 if (i >= 0 && i <= buf->params.secnumdepth) {
1038 textclass.counters().step(layout->latexname());
1040 // Is there a label? Useful for Chapter layout
1041 if (!pit->params().appendix()) {
1042 s << buf->B_(layout->labelstring());
1044 s << buf->B_(layout->labelstring_appendix());
1047 // Use of an integer is here less than elegant. For now.
1048 int head = textclass.maxcounter() - LABEL_COUNTER_CHAPTER;
1049 if (!pit->params().appendix()) {
1050 numbertype = "sectioning";
1052 numbertype = "appendix";
1053 if (pit->isRightToLeftPar(buf->params))
1054 langtype = "hebrew";
1060 << textclass.counters()
1061 .numberLabel(layout->latexname(),
1062 numbertype, langtype, head);
1064 pit->params().labelString(STRCONV(s.str()));
1066 // reset enum counters
1067 textclass.counters().reset("enum");
1068 } else if (layout->labeltype < LABEL_COUNTER_ENUMI) {
1069 textclass.counters().reset("enum");
1070 } else if (layout->labeltype == LABEL_COUNTER_ENUMI) {
1072 // Yes I know this is a really, really! bad solution
1074 string enumcounter("enum");
1076 switch (pit->enumdepth) {
1085 enumcounter += "iv";
1088 // not a valid enumdepth...
1092 textclass.counters().step(enumcounter);
1094 s << textclass.counters()
1095 .numberLabel(enumcounter, "enumeration");
1096 pit->params().labelString(STRCONV(s.str()));
1098 } else if (layout->labeltype == LABEL_BIBLIO) {// ale970302
1099 textclass.counters().step("bibitem");
1100 int number = textclass.counters().value("bibitem");
1101 if (pit->bibitem()) {
1102 pit->bibitem()->setCounter(number);
1103 pit->params().labelString(layout->labelstring());
1105 // In biblio should't be following counters but...
1107 string s = buf->B_(layout->labelstring());
1109 // the caption hack:
1110 if (layout->labeltype == LABEL_SENSITIVE) {
1111 ParagraphList::iterator end = ownerParagraphs().end();
1112 ParagraphList::iterator tmppit = pit;
1115 while (tmppit != end && tmppit->inInset()
1116 // the single '=' is intended below
1117 && (in = tmppit->inInset()->owner()))
1119 if (in->lyxCode() == InsetOld::FLOAT_CODE ||
1120 in->lyxCode() == InsetOld::WRAP_CODE) {
1124 tmppit = ownerParagraphs().begin();
1125 for ( ; tmppit != end; ++tmppit)
1126 if (&*tmppit == in->parOwner())
1134 if (in->lyxCode() == InsetOld::FLOAT_CODE)
1135 type = static_cast<InsetFloat*>(in)->params().type;
1136 else if (in->lyxCode() == InsetOld::WRAP_CODE)
1137 type = static_cast<InsetWrap*>(in)->params().type;
1141 Floating const & fl = textclass.floats().getType(type);
1143 textclass.counters().step(fl.type());
1145 // Doesn't work... yet.
1146 s = bformat(_("%1$s #:"), buf->B_(fl.name()));
1148 // par->SetLayout(0);
1149 // s = layout->labelstring;
1150 s = _("Senseless: ");
1153 pit->params().labelString(s);
1155 // reset the enumeration counter. They are always reset
1156 // when there is any other layout between
1157 // Just fall-through between the cases so that all
1158 // enum counters deeper than enumdepth is also reset.
1159 switch (pit->enumdepth) {
1161 textclass.counters().reset("enumi");
1163 textclass.counters().reset("enumii");
1165 textclass.counters().reset("enumiii");
1167 textclass.counters().reset("enumiv");
1173 // Updates all counters. Paragraphs with changed label string will be rebroken
1174 void LyXText::updateCounters()
1176 RowList::iterator rowit = rows().begin();
1177 ParagraphList::iterator pit = rowit->par();
1179 // CHECK if this is really needed. (Lgb)
1180 bv()->buffer()->params.getLyXTextClass().counters().reset();
1182 ParagraphList::iterator beg = ownerParagraphs().begin();
1183 ParagraphList::iterator end = ownerParagraphs().end();
1184 for (; pit != end; ++pit) {
1185 while (rowit->par() != pit)
1188 string const oldLabel = pit->params().labelString();
1190 size_t maxdepth = 0;
1192 maxdepth = boost::prior(pit)->getMaxDepthAfter();
1194 if (pit->params().depth() > maxdepth)
1195 pit->params().depth(maxdepth);
1197 // setCounter can potentially change the labelString.
1198 setCounter(bv()->buffer(), pit);
1200 string const & newLabel = pit->params().labelString();
1202 if (oldLabel != newLabel)
1208 void LyXText::insertInset(InsetOld * inset)
1210 if (!cursor.par()->insetAllowed(inset->lyxCode()))
1212 recordUndo(bv(), Undo::ATOMIC, cursor.par());
1214 cursor.par()->insertInset(cursor.pos(), inset);
1215 // Just to rebreak and refresh correctly.
1216 // The character will not be inserted a second time
1217 insertChar(Paragraph::META_INSET);
1218 // If we enter a highly editable inset the cursor should be to before
1219 // the inset. This couldn't happen before as Undo was not handled inside
1220 // inset now after the Undo LyX tries to call inset->Edit(...) again
1221 // and cannot do this as the cursor is behind the inset and GetInset
1222 // does not return the inset!
1223 if (isHighlyEditableInset(inset)) {
1230 void LyXText::cutSelection(bool doclear, bool realcut)
1232 // Stuff what we got on the clipboard. Even if there is no selection.
1234 // There is a problem with having the stuffing here in that the
1235 // larger the selection the slower LyX will get. This can be
1236 // solved by running the line below only when the selection has
1237 // finished. The solution used currently just works, to make it
1238 // faster we need to be more clever and probably also have more
1239 // calls to stuffClipboard. (Lgb)
1240 bv()->stuffClipboard(selectionAsString(bv()->buffer(), true));
1242 // This doesn't make sense, if there is no selection
1243 if (!selection.set())
1246 // OK, we have a selection. This is always between selection.start
1247 // and selection.end
1249 // make sure that the depth behind the selection are restored, too
1250 ParagraphList::iterator endpit = boost::next(selection.end.par());
1251 ParagraphList::iterator undoendpit = endpit;
1252 ParagraphList::iterator pars_end = ownerParagraphs().end();
1254 if (endpit != pars_end && endpit->getDepth()) {
1255 while (endpit != pars_end && endpit->getDepth()) {
1257 undoendpit = endpit;
1259 } else if (endpit != pars_end) {
1260 // because of parindents etc.
1264 recordUndo(bv(), Undo::DELETE, selection.start.par(),
1265 boost::prior(undoendpit));
1268 endpit = selection.end.par();
1269 int endpos = selection.end.pos();
1271 boost::tie(endpit, endpos) = realcut ?
1272 CutAndPaste::cutSelection(bv()->buffer()->params,
1274 selection.start.par(), endpit,
1275 selection.start.pos(), endpos,
1276 bv()->buffer()->params.textclass,
1278 : CutAndPaste::eraseSelection(bv()->buffer()->params,
1280 selection.start.par(), endpit,
1281 selection.start.pos(), endpos,
1283 // sometimes necessary
1285 selection.start.par()->stripLeadingSpaces();
1287 redoParagraphs(selection.start.par(), boost::next(endpit));
1288 #warning FIXME latent bug
1289 // endpit will be invalidated on redoParagraphs once ParagraphList
1290 // becomes a std::list? There are maybe other places on which this
1291 // can happend? (Ab)
1292 // cutSelection can invalidate the cursor so we need to set
1294 // we prefer the end for when tracking changes
1298 // need a valid cursor. (Lgb)
1301 setCursor(cursor.par(), cursor.pos());
1302 selection.cursor = cursor;
1307 void LyXText::copySelection()
1309 // stuff the selection onto the X clipboard, from an explicit copy request
1310 bv()->stuffClipboard(selectionAsString(bv()->buffer(), true));
1312 // this doesnt make sense, if there is no selection
1313 if (!selection.set())
1316 // ok we have a selection. This is always between selection.start
1317 // and sel_end cursor
1319 // copy behind a space if there is one
1320 while (selection.start.par()->size() > selection.start.pos()
1321 && selection.start.par()->isLineSeparator(selection.start.pos())
1322 && (selection.start.par() != selection.end.par()
1323 || selection.start.pos() < selection.end.pos()))
1324 selection.start.pos(selection.start.pos() + 1);
1326 CutAndPaste::copySelection(selection.start.par(),
1327 selection.end.par(),
1328 selection.start.pos(), selection.end.pos(),
1329 bv()->buffer()->params.textclass);
1333 void LyXText::pasteSelection(size_t sel_index)
1335 // this does not make sense, if there is nothing to paste
1336 if (!CutAndPaste::checkPastePossible())
1339 recordUndo(bv(), Undo::INSERT, cursor.par());
1341 ParagraphList::iterator endpit;
1346 boost::tie(ppp, endpit) =
1347 CutAndPaste::pasteSelection(*bv()->buffer(),
1349 cursor.par(), cursor.pos(),
1350 bv()->buffer()->params.textclass,
1352 bufferErrors(*bv()->buffer(), el);
1353 bv()->showErrorList(_("Paste"));
1355 redoParagraphs(cursor.par(), endpit);
1357 setCursor(cursor.par(), cursor.pos());
1360 selection.cursor = cursor;
1361 setCursor(ppp.first, ppp.second);
1367 void LyXText::setSelectionRange(lyx::pos_type length)
1372 selection.cursor = cursor;
1379 // simple replacing. The font of the first selected character is used
1380 void LyXText::replaceSelectionWithString(string const & str)
1382 recordUndo(bv(), Undo::ATOMIC);
1385 if (!selection.set()) { // create a dummy selection
1386 selection.end = cursor;
1387 selection.start = cursor;
1390 // Get font setting before we cut
1391 pos_type pos = selection.end.pos();
1392 LyXFont const font = selection.start.par()
1393 ->getFontSettings(bv()->buffer()->params,
1394 selection.start.pos());
1396 // Insert the new string
1397 string::const_iterator cit = str.begin();
1398 string::const_iterator end = str.end();
1399 for (; cit != end; ++cit) {
1400 selection.end.par()->insertChar(pos, (*cit), font);
1404 // Cut the selection
1405 cutSelection(true, false);
1411 // needed to insert the selection
1412 void LyXText::insertStringAsLines(string const & str)
1414 ParagraphList::iterator pit = cursor.par();
1415 pos_type pos = cursor.pos();
1416 ParagraphList::iterator endpit = boost::next(cursor.par());
1418 recordUndo(bv(), Undo::ATOMIC);
1420 // only to be sure, should not be neccessary
1423 bv()->buffer()->insertStringAsLines(pit, pos, current_font, str);
1425 redoParagraphs(cursor.par(), endpit);
1426 setCursor(cursor.par(), cursor.pos());
1427 selection.cursor = cursor;
1428 setCursor(pit, pos);
1433 // turns double-CR to single CR, others where converted into one
1434 // blank. Then InsertStringAsLines is called
1435 void LyXText::insertStringAsParagraphs(string const & str)
1437 string linestr(str);
1438 bool newline_inserted = false;
1439 string::size_type const siz = linestr.length();
1441 for (string::size_type i = 0; i < siz; ++i) {
1442 if (linestr[i] == '\n') {
1443 if (newline_inserted) {
1444 // we know that \r will be ignored by
1445 // InsertStringA. Of course, it is a dirty
1446 // trick, but it works...
1447 linestr[i - 1] = '\r';
1451 newline_inserted = true;
1453 } else if (IsPrintable(linestr[i])) {
1454 newline_inserted = false;
1457 insertStringAsLines(linestr);
1461 void LyXText::checkParagraph(ParagraphList::iterator pit, pos_type pos)
1463 breakAgain(getRow(pit, pos));
1464 setCursorIntern(cursor.par(), cursor.pos(), false, cursor.boundary());
1468 // returns false if inset wasn't found
1469 bool LyXText::updateInset(InsetOld * inset)
1471 // first check the current paragraph
1472 int pos = cursor.par()->getPositionOfInset(inset);
1474 checkParagraph(cursor.par(), pos);
1478 // check every paragraph
1479 ParagraphList::iterator par = ownerParagraphs().begin();
1480 ParagraphList::iterator end = ownerParagraphs().end();
1481 for (; par != end; ++par) {
1482 pos = par->getPositionOfInset(inset);
1484 checkParagraph(par, pos);
1493 bool LyXText::setCursor(ParagraphList::iterator pit,
1495 bool setfont, bool boundary)
1497 LyXCursor old_cursor = cursor;
1498 setCursorIntern(pit, pos, setfont, boundary);
1499 return deleteEmptyParagraphMechanism(old_cursor);
1503 void LyXText::setCursor(LyXCursor & cur, ParagraphList::iterator pit,
1504 pos_type pos, bool boundary)
1506 Assert(pit != ownerParagraphs().end());
1510 cur.boundary(boundary);
1514 // get the cursor y position in text
1516 RowList::iterator row = getRow(pit, pos, y);
1517 RowList::iterator beg = rows().begin();
1519 RowList::iterator old_row = row;
1520 // if we are before the first char of this row and are still in the
1521 // same paragraph and there is a previous row then put the cursor on
1522 // the end of the previous row
1523 cur.iy(y + row->baseline());
1526 boost::prior(row)->par() == row->par() &&
1527 pos < pit->size() &&
1528 pit->getChar(pos) == Paragraph::META_INSET) {
1529 InsetOld * ins = pit->getInset(pos);
1530 if (ins && (ins->needFullRow() || ins->display())) {
1536 // y is now the beginning of the cursor row
1537 y += row->baseline();
1538 // y is now the cursor baseline
1541 pos_type last = lastPrintablePos(*this, old_row);
1543 // None of these should happen, but we're scaredy-cats
1544 if (pos > pit->size()) {
1545 lyxerr << "dont like 1 please report" << endl;
1548 } else if (pos > last + 1) {
1549 lyxerr << "dont like 2 please report" << endl;
1550 // This shouldn't happen.
1553 } else if (pos < row->pos()) {
1554 lyxerr << "dont like 3 please report" << endl;
1559 // now get the cursors x position
1560 float x = getCursorX(row, pos, last, boundary);
1563 if (old_row != row) {
1564 x = getCursorX(old_row, pos, last, boundary);
1568 /* We take out this for the time being because 1) the redraw code is not
1569 prepared to this yet and 2) because some good policy has yet to be decided
1570 while editting: for instance how to act on rows being created/deleted
1574 //if the cursor is in a visible row, anchor to it
1576 if (topy < y && y < topy + bv()->workHeight())
1582 float LyXText::getCursorX(RowList::iterator rit,
1583 pos_type pos, pos_type last, bool boundary) const
1585 pos_type cursor_vpos = 0;
1587 double fill_separator;
1589 double fill_label_hfill;
1590 // This call HAS to be here because of the BidiTables!!!
1591 prepareToPrint(rit, x, fill_separator, fill_hfill,
1594 ParagraphList::iterator rit_par = rit->par();
1595 pos_type const rit_pos = rit->pos();
1598 cursor_vpos = rit_pos;
1599 else if (pos > last && !boundary)
1600 cursor_vpos = (rit_par->isRightToLeftPar(bv()->buffer()->params))
1601 ? rit_pos : last + 1;
1602 else if (pos > rit_pos && (pos > last || boundary))
1603 /// Place cursor after char at (logical) position pos - 1
1604 cursor_vpos = (bidi_level(pos - 1) % 2 == 0)
1605 ? log2vis(pos - 1) + 1 : log2vis(pos - 1);
1607 /// Place cursor before char at (logical) position pos
1608 cursor_vpos = (bidi_level(pos) % 2 == 0)
1609 ? log2vis(pos) : log2vis(pos) + 1;
1611 pos_type body_pos = rit_par->beginningOfBody();
1613 (body_pos - 1 > last || !rit_par->isLineSeparator(body_pos - 1)))
1616 for (pos_type vpos = rit_pos; vpos < cursor_vpos; ++vpos) {
1617 pos_type pos = vis2log(vpos);
1618 if (body_pos > 0 && pos == body_pos - 1) {
1619 x += fill_label_hfill +
1620 font_metrics::width(
1621 rit_par->layout()->labelsep, getLabelFont(rit_par));
1622 if (rit_par->isLineSeparator(body_pos - 1))
1623 x -= singleWidth(rit_par, body_pos - 1);
1626 if (hfillExpansion(*this, rit, pos)) {
1627 x += singleWidth(rit_par, pos);
1628 if (pos >= body_pos)
1631 x += fill_label_hfill;
1632 } else if (rit_par->isSeparator(pos)) {
1633 x += singleWidth(rit_par, pos);
1634 if (pos >= body_pos)
1635 x += fill_separator;
1637 x += singleWidth(rit_par, pos);
1643 void LyXText::setCursorIntern(ParagraphList::iterator pit,
1644 pos_type pos, bool setfont, bool boundary)
1646 UpdatableInset * it = pit->inInset();
1648 if (it != inset_owner) {
1649 lyxerr[Debug::INSETS] << "InsetText is " << it
1651 << "inset_owner is "
1652 << inset_owner << endl;
1653 #ifdef WITH_WARNINGS
1654 #warning I believe this code is wrong. (Lgb)
1655 #warning Jürgen, have a look at this. (Lgb)
1656 #warning Hmmm, I guess you are right but we
1657 #warning should verify when this is needed
1659 // Jürgen, would you like to have a look?
1660 // I guess we need to move the outer cursor
1661 // and open and lock the inset (bla bla bla)
1662 // stuff I don't know... so can you have a look?
1664 // I moved the lyxerr stuff in here so we can see if
1665 // this is actually really needed and where!
1667 // it->getLyXText(bv())->setCursorIntern(bv(), par, pos, setfont, boundary);
1672 setCursor(cursor, pit, pos, boundary);
1678 void LyXText::setCurrentFont()
1680 pos_type pos = cursor.pos();
1681 ParagraphList::iterator pit = cursor.par();
1683 if (cursor.boundary() && pos > 0)
1687 if (pos == pit->size())
1689 else // potentional bug... BUG (Lgb)
1690 if (pit->isSeparator(pos)) {
1691 if (pos > cursorRow()->pos() &&
1692 bidi_level(pos) % 2 ==
1693 bidi_level(pos - 1) % 2)
1695 else if (pos + 1 < pit->size())
1700 current_font = pit->getFontSettings(bv()->buffer()->params, pos);
1701 real_current_font = getFont(pit, pos);
1703 if (cursor.pos() == pit->size() &&
1704 isBoundary(bv()->buffer(), *pit, cursor.pos()) &&
1705 !cursor.boundary()) {
1706 Language const * lang =
1707 pit->getParLanguage(bv()->buffer()->params);
1708 current_font.setLanguage(lang);
1709 current_font.setNumber(LyXFont::OFF);
1710 real_current_font.setLanguage(lang);
1711 real_current_font.setNumber(LyXFont::OFF);
1716 // returns the column near the specified x-coordinate of the row
1717 // x is set to the real beginning of this column
1719 LyXText::getColumnNearX(RowList::iterator rit, int & x, bool & boundary) const
1722 double fill_separator;
1724 double fill_label_hfill;
1726 prepareToPrint(rit, tmpx, fill_separator, fill_hfill, fill_label_hfill);
1728 pos_type vc = rit->pos();
1729 pos_type last = lastPrintablePos(*this, rit);
1732 ParagraphList::iterator rit_par = rit->par();
1733 LyXLayout_ptr const & layout = rit->par()->layout();
1735 bool left_side = false;
1737 pos_type body_pos = rit_par->beginningOfBody();
1738 double last_tmpx = tmpx;
1741 (body_pos - 1 > last ||
1742 !rit_par->isLineSeparator(body_pos - 1)))
1745 // check for empty row
1746 if (!rit_par->size()) {
1751 while (vc <= last && tmpx <= x) {
1754 if (body_pos > 0 && c == body_pos - 1) {
1755 tmpx += fill_label_hfill +
1756 font_metrics::width(layout->labelsep, getLabelFont(rit_par));
1757 if (rit_par->isLineSeparator(body_pos - 1))
1758 tmpx -= singleWidth(rit_par, body_pos - 1);
1761 if (hfillExpansion(*this, rit, c)) {
1762 tmpx += singleWidth(rit_par, c);
1766 tmpx += fill_label_hfill;
1767 } else if (rit_par->isSeparator(c)) {
1768 tmpx += singleWidth(rit_par, c);
1770 tmpx += fill_separator;
1772 tmpx += singleWidth(rit_par, c);
1777 if ((tmpx + last_tmpx) / 2 > x) {
1782 if (vc > last + 1) // This shouldn't happen.
1786 // This (rtl_support test) is not needed, but gives
1787 // some speedup if rtl_support=false
1788 RowList::iterator next_rit = boost::next(rit);
1790 bool const lastrow = lyxrc.rtl_support &&
1791 (next_rit == rowlist_.end() ||
1792 next_rit->par() != rit_par);
1794 // If lastrow is false, we don't need to compute
1795 // the value of rtl.
1796 bool const rtl = (lastrow)
1797 ? rit_par->isRightToLeftPar(bv()->buffer()->params)
1800 ((rtl && left_side && vc == rit->pos() && x < tmpx - 5) ||
1801 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5)))
1803 else if (vc == rit->pos()) {
1805 if (bidi_level(c) % 2 == 1)
1808 c = vis2log(vc - 1);
1809 bool const rtl = (bidi_level(c) % 2 == 1);
1810 if (left_side == rtl) {
1812 boundary = isBoundary(bv()->buffer(), *rit_par, c);
1816 if (rit->pos() <= last && c > last
1817 && rit_par->isNewline(last)) {
1818 if (bidi_level(last) % 2 == 0)
1819 tmpx -= singleWidth(rit_par, last);
1821 tmpx += singleWidth(rit_par, last);
1831 void LyXText::setCursorFromCoordinates(int x, int y)
1833 //LyXCursor old_cursor = cursor;
1834 setCursorFromCoordinates(cursor, x, y);
1836 #warning DEPM disabled, otherwise crash when entering new table
1837 //deleteEmptyParagraphMechanism(old_cursor);
1844 * return true if the cursor given is at the end of a row,
1845 * and the next row is filled by an inset that spans an entire
1848 bool beforeFullRowInset(LyXText & lt, LyXCursor const & cur)
1850 RowList::iterator row = lt.getRow(cur);
1851 if (boost::next(row) == lt.rows().end())
1854 Row const & next = *boost::next(row);
1856 if (next.pos() != cur.pos() || next.par() != cur.par())
1859 if (cur.pos() == cur.par()->size()
1860 || !cur.par()->isInset(cur.pos()))
1863 InsetOld const * inset = cur.par()->getInset(cur.pos());
1864 if (inset->needFullRow() || inset->display())
1872 void LyXText::setCursorFromCoordinates(LyXCursor & cur, int x, int y)
1874 // Get the row first.
1876 RowList::iterator row = getRowNearY(y);
1878 pos_type const column = getColumnNearX(row, x, bound);
1879 cur.par(row->par());
1880 cur.pos(row->pos() + column);
1882 cur.y(y + row->baseline());
1884 if (beforeFullRowInset(*this, cur)) {
1885 pos_type const last = lastPrintablePos(*this, row);
1886 RowList::iterator next_row = boost::next(row);
1888 float x = getCursorX(next_row, cur.pos(), last, bound);
1890 cur.iy(y + row->height() + next_row->baseline());
1895 cur.boundary(bound);
1899 void LyXText::cursorLeft(bool internal)
1901 if (cursor.pos() > 0) {
1902 bool boundary = cursor.boundary();
1903 setCursor(cursor.par(), cursor.pos() - 1, true, false);
1904 if (!internal && !boundary &&
1905 isBoundary(bv()->buffer(), *cursor.par(), cursor.pos() + 1))
1906 setCursor(cursor.par(), cursor.pos() + 1, true, true);
1907 } else if (cursor.par() != ownerParagraphs().begin()) {
1908 // steps into the paragraph above
1909 ParagraphList::iterator pit = boost::prior(cursor.par());
1910 setCursor(pit, pit->size());
1915 void LyXText::cursorRight(bool internal)
1917 bool const at_end = (cursor.pos() == cursor.par()->size());
1918 bool const at_newline = !at_end &&
1919 cursor.par()->isNewline(cursor.pos());
1921 if (!internal && cursor.boundary() && !at_newline)
1922 setCursor(cursor.par(), cursor.pos(), true, false);
1924 setCursor(cursor.par(), cursor.pos() + 1, true, false);
1926 isBoundary(bv()->buffer(), *cursor.par(), cursor.pos()))
1927 setCursor(cursor.par(), cursor.pos(), true, true);
1928 } else if (boost::next(cursor.par()) != ownerParagraphs().end())
1929 setCursor(boost::next(cursor.par()), 0);
1933 void LyXText::cursorUp(bool selecting)
1936 int x = cursor.x_fix();
1937 int y = cursor.y() - cursorRow()->baseline() - 1;
1938 setCursorFromCoordinates(x, y);
1941 int y1 = cursor.iy() - topy;
1944 InsetOld * inset_hit = checkInsetHit(x, y1);
1945 if (inset_hit && isHighlyEditableInset(inset_hit)) {
1946 inset_hit->localDispatch(
1947 FuncRequest(bv(), LFUN_INSET_EDIT, x, y - (y2 - y1), mouse_button::none));
1951 setCursorFromCoordinates(bv(), cursor.x_fix(),
1952 cursor.y() - cursorRow()->baseline() - 1);
1957 void LyXText::cursorDown(bool selecting)
1960 int x = cursor.x_fix();
1961 int y = cursor.y() - cursorRow()->baseline() + cursorRow()->height() + 1;
1962 setCursorFromCoordinates(x, y);
1963 if (!selecting && cursorRow() == cursorIRow()) {
1965 int y1 = cursor.iy() - topy;
1968 InsetOld * inset_hit = checkInsetHit(x, y1);
1969 if (inset_hit && isHighlyEditableInset(inset_hit)) {
1970 FuncRequest cmd(bv(), LFUN_INSET_EDIT, x, y - (y2 - y1), mouse_button::none);
1971 inset_hit->localDispatch(cmd);
1975 setCursorFromCoordinates(bv(), cursor.x_fix(),
1976 cursor.y() - cursorRow()->baseline()
1977 + cursorRow()->height() + 1);
1982 void LyXText::cursorUpParagraph()
1984 if (cursor.pos() > 0)
1985 setCursor(cursor.par(), 0);
1986 else if (cursor.par() != ownerParagraphs().begin())
1987 setCursor(boost::prior(cursor.par()), 0);
1991 void LyXText::cursorDownParagraph()
1993 ParagraphList::iterator par = cursor.par();
1994 ParagraphList::iterator next_par = boost::next(par);
1996 if (next_par != ownerParagraphs().end())
1997 setCursor(next_par, 0);
1999 setCursor(par, par->size());
2003 // fix the cursor `cur' after a characters has been deleted at `where'
2004 // position. Called by deleteEmptyParagraphMechanism
2005 void LyXText::fixCursorAfterDelete(LyXCursor & cur,
2006 LyXCursor const & where)
2008 // if cursor is not in the paragraph where the delete occured,
2010 if (cur.par() != where.par())
2013 // if cursor position is after the place where the delete occured,
2015 if (cur.pos() > where.pos())
2016 cur.pos(cur.pos()-1);
2018 // check also if we don't want to set the cursor on a spot behind the
2019 // pagragraph because we erased the last character.
2020 if (cur.pos() > cur.par()->size())
2021 cur.pos(cur.par()->size());
2023 // recompute row et al. for this cursor
2024 setCursor(cur, cur.par(), cur.pos(), cur.boundary());
2028 bool LyXText::deleteEmptyParagraphMechanism(LyXCursor const & old_cursor)
2030 // Would be wrong to delete anything if we have a selection.
2031 if (selection.set())
2034 // We allow all kinds of "mumbo-jumbo" when freespacing.
2035 if (old_cursor.par()->layout()->free_spacing
2036 || old_cursor.par()->isFreeSpacing()) {
2040 /* Ok I'll put some comments here about what is missing.
2041 I have fixed BackSpace (and thus Delete) to not delete
2042 double-spaces automagically. I have also changed Cut,
2043 Copy and Paste to hopefully do some sensible things.
2044 There are still some small problems that can lead to
2045 double spaces stored in the document file or space at
2046 the beginning of paragraphs. This happens if you have
2047 the cursor betwenn to spaces and then save. Or if you
2048 cut and paste and the selection have a space at the
2049 beginning and then save right after the paste. I am
2050 sure none of these are very hard to fix, but I will
2051 put out 1.1.4pre2 with FIX_DOUBLE_SPACE defined so
2052 that I can get some feedback. (Lgb)
2055 // If old_cursor.pos() == 0 and old_cursor.pos()(1) == LineSeparator
2056 // delete the LineSeparator.
2059 // If old_cursor.pos() == 1 and old_cursor.pos()(0) == LineSeparator
2060 // delete the LineSeparator.
2063 // If the pos around the old_cursor were spaces, delete one of them.
2064 if (old_cursor.par() != cursor.par()
2065 || old_cursor.pos() != cursor.pos()) {
2066 // Only if the cursor has really moved
2068 if (old_cursor.pos() > 0
2069 && old_cursor.pos() < old_cursor.par()->size()
2070 && old_cursor.par()->isLineSeparator(old_cursor.pos())
2071 && old_cursor.par()->isLineSeparator(old_cursor.pos() - 1)) {
2072 old_cursor.par()->erase(old_cursor.pos() - 1);
2073 redoParagraph(old_cursor.par());
2075 #ifdef WITH_WARNINGS
2076 #warning This will not work anymore when we have multiple views of the same buffer
2077 // In this case, we will have to correct also the cursors held by
2078 // other bufferviews. It will probably be easier to do that in a more
2079 // automated way in LyXCursor code. (JMarc 26/09/2001)
2081 // correct all cursors held by the LyXText
2082 fixCursorAfterDelete(cursor, old_cursor);
2083 fixCursorAfterDelete(selection.cursor, old_cursor);
2084 fixCursorAfterDelete(selection.start, old_cursor);
2085 fixCursorAfterDelete(selection.end, old_cursor);
2086 fixCursorAfterDelete(last_sel_cursor, old_cursor);
2091 // don't delete anything if this is the ONLY paragraph!
2092 if (ownerParagraphs().size() == 1)
2095 // Do not delete empty paragraphs with keepempty set.
2096 if (old_cursor.par()->allowEmpty())
2099 // only do our magic if we changed paragraph
2100 if (old_cursor.par() == cursor.par())
2103 // record if we have deleted a paragraph
2104 // we can't possibly have deleted a paragraph before this point
2105 bool deleted = false;
2107 if (old_cursor.par()->empty() ||
2108 (old_cursor.par()->size() == 1 &&
2109 old_cursor.par()->isLineSeparator(0))) {
2110 // ok, we will delete anything
2111 LyXCursor tmpcursor;
2115 bool selection_position_was_oldcursor_position = (
2116 selection.cursor.par() == old_cursor.par()
2117 && selection.cursor.pos() == old_cursor.pos());
2119 if (getRow(old_cursor) != rows().begin()) {
2120 RowList::iterator prevrow = boost::prior(getRow(old_cursor));
2122 cursor = old_cursor; // that undo can restore the right cursor position
2123 #warning FIXME. --end() iterator is usable here
2124 ParagraphList::iterator endpit = boost::next(old_cursor.par());
2125 while (endpit != ownerParagraphs().end() &&
2126 endpit->getDepth()) {
2130 recordUndo(bv(), Undo::DELETE, old_cursor.par(),
2131 boost::prior(endpit));
2135 removeRow(getRow(old_cursor));
2137 ownerParagraphs().erase(old_cursor.par());
2139 /* Breakagain the next par. Needed because of
2140 * the parindent that can occur or dissappear.
2141 * The next row can change its height, if
2142 * there is another layout before */
2143 RowList::iterator tmprit = boost::next(prevrow);
2144 if (tmprit != rows().end()) {
2148 setHeightOfRow(prevrow);
2150 RowList::iterator nextrow = boost::next(getRow(old_cursor));
2153 cursor = old_cursor; // that undo can restore the right cursor position
2154 #warning FIXME. --end() iterator is usable here
2155 ParagraphList::iterator endpit = boost::next(old_cursor.par());
2156 while (endpit != ownerParagraphs().end() &&
2157 endpit->getDepth()) {
2161 recordUndo(bv(), Undo::DELETE, old_cursor.par(), boost::prior(endpit));
2165 removeRow(getRow(old_cursor));
2167 ownerParagraphs().erase(old_cursor.par());
2169 /* Breakagain the next par. Needed because of
2170 the parindent that can occur or dissappear.
2171 The next row can change its height, if
2172 there is another layout before */
2173 if (nextrow != rows().end()) {
2174 breakAgain(nextrow);
2180 setCursorIntern(cursor.par(), cursor.pos());
2182 if (selection_position_was_oldcursor_position) {
2183 // correct selection
2184 selection.cursor = cursor;
2188 if (old_cursor.par()->stripLeadingSpaces()) {
2189 redoParagraph(old_cursor.par());
2191 setCursorIntern(cursor.par(), cursor.pos());
2192 selection.cursor = cursor;
2199 ParagraphList & LyXText::ownerParagraphs() const
2202 return inset_owner->paragraphs;
2204 return bv_owner->buffer()->paragraphs;
2208 bool LyXText::isInInset() const
2210 // Sub-level has non-null bv owner and
2211 // non-null inset owner.
2212 return inset_owner != 0 && bv_owner != 0;
2216 int defaultRowHeight()
2218 LyXFont const font(LyXFont::ALL_SANE);
2219 return int(font_metrics::maxAscent(font)
2220 + font_metrics::maxDescent(font) * 1.5);