1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
16 #include "paragraph.h"
17 #include "funcrequest.h"
18 #include "frontends/LyXView.h"
19 #include "undo_funcs.h"
21 #include "buffer_funcs.h"
22 #include "bufferparams.h"
23 #include "errorlist.h"
25 #include "BufferView.h"
26 #include "CutAndPaste.h"
27 #include "frontends/Painter.h"
28 #include "frontends/font_metrics.h"
32 #include "FloatList.h"
34 #include "ParagraphParameters.h"
36 #include "lyxrow_funcs.h"
37 #include "paragraph_funcs.h"
39 #include "insets/insetbibitem.h"
40 #include "insets/insetenv.h"
41 #include "insets/insetfloat.h"
42 #include "insets/insetwrap.h"
44 #include "support/LAssert.h"
45 #include "support/textutils.h"
46 #include "support/lstrings.h"
48 #include <boost/tuple/tuple.hpp>
50 using namespace lyx::support;
60 LyXText::LyXText(BufferView * bv)
61 : height(0), width(0), anchor_row_offset_(0),
62 inset_owner(0), the_locking_inset(0), bv_owner(bv)
64 anchor_row_ = rows().end();
65 need_break_row = rows().end();
70 LyXText::LyXText(BufferView * bv, InsetText * inset)
71 : height(0), width(0), anchor_row_offset_(0),
72 inset_owner(inset), the_locking_inset(0), bv_owner(bv)
74 anchor_row_ = rows().end();
75 need_break_row = rows().end();
80 void LyXText::init(BufferView * bview)
85 need_break_row = rows().end();
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(bview->buffer(), 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 const LyXText::getFont(Buffer const * buf, ParagraphList::iterator pit,
119 LyXLayout_ptr const & layout = pit->layout();
121 // We specialize the 95% common case:
122 if (!pit->getDepth()) {
123 if (layout->labeltype == LABEL_MANUAL
124 && pos < pit->beginningOfBody()) {
126 LyXFont f = pit->getFontSettings(buf->params, pos);
128 pit->inInset()->getDrawFont(f);
129 return f.realize(layout->reslabelfont);
131 LyXFont f = pit->getFontSettings(buf->params, pos);
133 pit->inInset()->getDrawFont(f);
134 return f.realize(layout->resfont);
138 // The uncommon case need not be optimized as much
142 if (pos < pit->beginningOfBody()) {
144 layoutfont = layout->labelfont;
147 layoutfont = layout->font;
150 LyXFont tmpfont = pit->getFontSettings(buf->params, pos);
151 tmpfont.realize(layoutfont);
154 pit->inInset()->getDrawFont(tmpfont);
156 // Realize with the fonts of lesser depth.
157 tmpfont.realize(outerFont(pit, ownerParagraphs()));
159 return realizeFont(tmpfont, buf->params);
163 LyXFont const LyXText::getLayoutFont(Buffer const * buf,
164 ParagraphList::iterator pit) const
166 LyXLayout_ptr const & layout = pit->layout();
168 if (!pit->getDepth()) {
169 return layout->resfont;
172 LyXFont font(layout->font);
173 // Realize with the fonts of lesser depth.
174 font.realize(outerFont(pit, ownerParagraphs()));
176 return realizeFont(font, buf->params);
180 LyXFont const LyXText::getLabelFont(Buffer const * buf,
181 ParagraphList::iterator pit) const
183 LyXLayout_ptr const & layout = pit->layout();
185 if (!pit->getDepth()) {
186 return layout->reslabelfont;
189 LyXFont font(layout->labelfont);
190 // Realize with the fonts of lesser depth.
191 font.realize(outerFont(pit, ownerParagraphs()));
193 return realizeFont(layout->labelfont, buf->params);
197 void LyXText::setCharFont(ParagraphList::iterator pit,
198 pos_type pos, LyXFont const & fnt,
201 Buffer const * buf = bv()->buffer();
202 LyXFont font = getFont(buf, pit, pos);
203 font.update(fnt, buf->params.language, toggleall);
204 // Let the insets convert their font
205 if (pit->isInset(pos)) {
206 Inset * inset = pit->getInset(pos);
207 if (isEditableInset(inset)) {
208 UpdatableInset * uinset =
209 static_cast<UpdatableInset *>(inset);
210 uinset->setFont(bv(), fnt, toggleall, true);
214 // Plug thru to version below:
215 setCharFont(buf, pit, pos, font);
219 void LyXText::setCharFont(Buffer const * buf, ParagraphList::iterator pit,
220 pos_type pos, LyXFont const & fnt)
224 LyXTextClass const & tclass = buf->params.getLyXTextClass();
225 LyXLayout_ptr const & layout = pit->layout();
227 // Get concrete layout font to reduce against
230 if (pos < pit->beginningOfBody())
231 layoutfont = layout->labelfont;
233 layoutfont = layout->font;
235 // Realize against environment font information
236 if (pit->getDepth()) {
237 ParagraphList::iterator tp = pit;
238 while (!layoutfont.resolved() &&
239 tp != ownerParagraphs().end() &&
241 tp = outerHook(tp, ownerParagraphs());
242 if (tp != ownerParagraphs().end())
243 layoutfont.realize(tp->layout()->font);
247 layoutfont.realize(tclass.defaultfont());
249 // Now, reduce font against full layout font
250 font.reduce(layoutfont);
252 pit->setFont(pos, font);
256 // removes the row and reset the touched counters
257 void LyXText::removeRow(RowList::iterator rit)
259 if (anchor_row_ == rit) {
260 if (rit != rows().begin()) {
261 anchor_row_ = boost::prior(rit);
262 anchor_row_offset_ += anchor_row_->height();
264 anchor_row_ = boost::next(rit);
265 anchor_row_offset_ -= rit->height();
269 // the text becomes smaller
270 height -= rit->height();
276 // remove all following rows of the paragraph of the specified row.
277 void LyXText::removeParagraph(RowList::iterator rit)
279 ParagraphList::iterator tmppit = rit->par();
282 while (rit != rows().end() && rit->par() == tmppit) {
283 RowList::iterator tmprit = boost::next(rit);
290 void LyXText::insertParagraph(ParagraphList::iterator pit,
291 RowList::iterator rowit)
293 // insert a new row, starting at position 0
295 RowList::iterator rit = rowlist_.insert(rowit, newrow);
297 // and now append the whole paragraph before the new row
298 appendParagraph(rit);
302 Inset * LyXText::getInset() const
304 ParagraphList::iterator pit = cursor.par();
305 pos_type const pos = cursor.pos();
307 if (pos < pit->size() && pit->isInset(pos)) {
308 return pit->getInset(pos);
314 void LyXText::toggleInset()
316 Inset * inset = getInset();
317 // is there an editable inset at cursor position?
318 if (!isEditableInset(inset)) {
319 // No, try to see if we are inside a collapsable inset
320 if (inset_owner && inset_owner->owner()
321 && inset_owner->owner()->isOpen()) {
322 bv()->unlockInset(inset_owner->owner());
323 inset_owner->owner()->close(bv());
324 bv()->getLyXText()->cursorRight(bv());
328 //bv()->owner()->message(inset->editMessage());
330 // do we want to keep this?? (JMarc)
331 if (!isHighlyEditableInset(inset))
332 setCursorParUndo(bv());
334 if (inset->isOpen()) {
340 bv()->updateInset(inset);
344 /* used in setlayout */
345 // Asger is not sure we want to do this...
346 void LyXText::makeFontEntriesLayoutSpecific(Buffer const & buf,
349 LyXLayout_ptr const & layout = par.layout();
350 pos_type const psize = par.size();
353 for (pos_type pos = 0; pos < psize; ++pos) {
354 if (pos < par.beginningOfBody())
355 layoutfont = layout->labelfont;
357 layoutfont = layout->font;
359 LyXFont tmpfont = par.getFontSettings(buf.params, pos);
360 tmpfont.reduce(layoutfont);
361 par.setFont(pos, tmpfont);
366 ParagraphList::iterator
367 LyXText::setLayout(LyXCursor & cur, LyXCursor & sstart_cur,
368 LyXCursor & send_cur,
369 string const & layout)
371 ParagraphList::iterator endpit = boost::next(send_cur.par());
372 ParagraphList::iterator undoendpit = endpit;
373 ParagraphList::iterator pars_end = ownerParagraphs().end();
375 if (endpit != pars_end && endpit->getDepth()) {
376 while (endpit != pars_end && endpit->getDepth()) {
380 } else if (endpit != pars_end) {
381 // because of parindents etc.
385 setUndo(bv(), Undo::EDIT, sstart_cur.par(), boost::prior(undoendpit));
387 // ok we have a selection. This is always between sstart_cur
388 // and sel_end cursor
390 ParagraphList::iterator pit = sstart_cur.par();
391 ParagraphList::iterator epit = boost::next(send_cur.par());
393 LyXLayout_ptr const & lyxlayout =
394 bv()->buffer()->params.getLyXTextClass()[layout];
397 pit->applyLayout(lyxlayout);
398 makeFontEntriesLayoutSpecific(*bv()->buffer(), *pit);
399 ParagraphList::iterator fppit = pit;
400 fppit->params().spaceTop(lyxlayout->fill_top ?
401 VSpace(VSpace::VFILL)
402 : VSpace(VSpace::NONE));
403 fppit->params().spaceBottom(lyxlayout->fill_bottom ?
404 VSpace(VSpace::VFILL)
405 : VSpace(VSpace::NONE));
406 if (lyxlayout->margintype == MARGIN_MANUAL)
407 pit->setLabelWidthString(lyxlayout->labelstring());
410 } while (pit != epit);
416 // set layout over selection and make a total rebreak of those paragraphs
417 void LyXText::setLayout(string const & layout)
419 LyXCursor tmpcursor = cursor; // store the current cursor
421 // if there is no selection just set the layout
422 // of the current paragraph
423 if (!selection.set()) {
424 selection.start = cursor; // dummy selection
425 selection.end = cursor;
428 // special handling of new environment insets
429 BufferParams const & params = bv()->buffer()->params;
430 LyXLayout_ptr const & lyxlayout = params.getLyXTextClass()[layout];
431 if (lyxlayout->is_environment) {
432 // move everything in a new environment inset
433 lyxerr << "setting layout " << layout << endl;
434 bv()->owner()->dispatch(FuncRequest(LFUN_HOME));
435 bv()->owner()->dispatch(FuncRequest(LFUN_ENDSEL));
436 bv()->owner()->dispatch(FuncRequest(LFUN_CUT));
437 Inset * inset = new InsetEnvironment(params, layout);
438 if (bv()->insertInset(inset)) {
440 //bv()->owner()->dispatch(FuncRequest(LFUN_PASTE));
447 ParagraphList::iterator endpit = setLayout(cursor, selection.start,
448 selection.end, layout);
449 redoParagraphs(selection.start, endpit);
451 // we have to reset the selection, because the
452 // geometry could have changed
453 setCursor(selection.start.par(), selection.start.pos(), false);
454 selection.cursor = cursor;
455 setCursor(selection.end.par(), selection.end.pos(), false);
459 setCursor(tmpcursor.par(), tmpcursor.pos(), true);
463 bool LyXText::changeDepth(bv_funcs::DEPTH_CHANGE type, bool test_only)
465 ParagraphList::iterator pit(cursor.par());
466 ParagraphList::iterator end(cursor.par());
467 ParagraphList::iterator start = pit;
469 if (selection.set()) {
470 pit = selection.start.par();
471 end = selection.end.par();
475 ParagraphList::iterator pastend = boost::next(end);
478 setUndo(bv(), Undo::EDIT, start, end);
480 bool changed = false;
482 int prev_after_depth = 0;
483 #warning parlist ... could be nicer ?
484 if (start != ownerParagraphs().begin()) {
485 prev_after_depth = boost::prior(start)->getMaxDepthAfter();
489 int const depth = pit->params().depth();
490 if (type == bv_funcs::INC_DEPTH) {
491 if (depth < prev_after_depth
492 && pit->layout()->labeltype != LABEL_BIBLIO) {
495 pit->params().depth(depth + 1);
502 pit->params().depth(depth - 1);
505 prev_after_depth = pit->getMaxDepthAfter();
517 // Wow, redoParagraphs is stupid.
519 setCursor(tmpcursor, start, 0);
521 redoParagraphs(tmpcursor, pastend);
523 // We need to actually move the text->cursor. I don't
524 // understand why ...
527 // we have to reset the visual selection because the
528 // geometry could have changed
529 if (selection.set()) {
530 setCursor(selection.start.par(), selection.start.pos());
531 selection.cursor = cursor;
532 setCursor(selection.end.par(), selection.end.pos());
535 // this handles the counter labels, and also fixes up
536 // depth values for follow-on (child) paragraphs
540 setCursor(tmpcursor.par(), tmpcursor.pos());
546 // set font over selection and make a total rebreak of those paragraphs
547 void LyXText::setFont(LyXFont const & font, bool toggleall)
549 // if there is no selection just set the current_font
550 if (!selection.set()) {
551 // Determine basis font
553 if (cursor.pos() < cursor.par()->beginningOfBody()) {
554 layoutfont = getLabelFont(bv()->buffer(),
557 layoutfont = getLayoutFont(bv()->buffer(),
560 // Update current font
561 real_current_font.update(font,
562 bv()->buffer()->params.language,
565 // Reduce to implicit settings
566 current_font = real_current_font;
567 current_font.reduce(layoutfont);
568 // And resolve it completely
569 real_current_font.realize(layoutfont);
574 LyXCursor tmpcursor = cursor; // store the current cursor
576 // ok we have a selection. This is always between sel_start_cursor
577 // and sel_end cursor
579 setUndo(bv(), Undo::EDIT, selection.start.par(), selection.end.par());
581 cursor = selection.start;
582 while (cursor.par() != selection.end.par() ||
583 cursor.pos() < selection.end.pos())
585 if (cursor.pos() < cursor.par()->size()) {
586 // an open footnote should behave like a closed one
587 setCharFont(cursor.par(), cursor.pos(),
589 cursor.pos(cursor.pos() + 1);
592 cursor.par(boost::next(cursor.par()));
597 redoParagraphs(selection.start, boost::next(selection.end.par()));
599 // we have to reset the selection, because the
600 // geometry could have changed, but we keep
601 // it for user convenience
602 setCursor(selection.start.par(), selection.start.pos());
603 selection.cursor = cursor;
604 setCursor(selection.end.par(), selection.end.pos());
606 setCursor(tmpcursor.par(), tmpcursor.pos(), true,
607 tmpcursor.boundary());
611 void LyXText::redoHeightOfParagraph()
613 RowList::iterator tmprow = cursorRow();
615 setHeightOfRow(tmprow);
617 while (tmprow != rows().begin()
618 && boost::prior(tmprow)->par() == tmprow->par()) {
620 setHeightOfRow(tmprow);
625 setCursor(cursor.par(), cursor.pos(), false, cursor.boundary());
629 // deletes and inserts again all paragraphs between the cursor
630 // and the specified par
631 // This function is needed after SetLayout and SetFont etc.
632 void LyXText::redoParagraphs(LyXCursor const & cur,
633 ParagraphList::iterator endpit)
635 RowList::iterator tmprit = getRow(cur);
637 ParagraphList::iterator first_phys_pit;
638 RowList::iterator prevrit;
639 if (tmprit == rows().begin()) {
640 // A trick/hack for UNDO.
641 // This is needed because in an UNDO/REDO we could have
642 // changed the ownerParagraph() so the paragraph inside
643 // the row is NOT my really first par anymore.
644 // Got it Lars ;) (Jug 20011206)
645 first_phys_pit = ownerParagraphs().begin();
646 prevrit = rows().end();
648 first_phys_pit = tmprit->par();
649 while (tmprit != rows().begin()
650 && boost::prior(tmprit)->par() == first_phys_pit)
654 prevrit = boost::prior(tmprit);
658 while (tmprit != rows().end() && tmprit->par() != endpit) {
659 RowList::iterator tmprit2 = tmprit++;
663 // Reinsert the paragraphs.
664 ParagraphList::iterator tmppit = first_phys_pit;
666 while (tmppit != ownerParagraphs().end()) {
667 insertParagraph(tmppit, tmprit);
668 while (tmprit != rows().end()
669 && tmprit->par() == tmppit) {
673 if (tmppit == endpit)
676 if (prevrit != rows().end())
677 setHeightOfRow(prevrit);
679 setHeightOfRow(rows().begin());
681 if (tmprit != rows().end())
682 setHeightOfRow(tmprit);
688 void LyXText::fullRebreak()
691 setCursorIntern(cursor.par(), cursor.pos());
695 void LyXText::rebuild()
698 need_break_row = rows().end();
701 anchor_row_ = rows().end();
702 anchor_row_offset_ = 0;
704 ParagraphList::iterator pit = ownerParagraphs().begin();
705 ParagraphList::iterator end = ownerParagraphs().end();
707 //current_font = getFont(bview->buffer(), pit, 0);
709 for (; pit != end; ++pit)
710 insertParagraph(pit, rowlist_.end());
712 //setCursorIntern(rowlist_.begin()->par(), 0);
713 //selection.cursor = cursor;
717 //setCursorIntern(cursor.par(), cursor.pos());
721 void LyXText::partialRebreak()
723 if (rows().empty()) {
728 RowList::iterator rows_end = rows().end();
730 if (need_break_row != rows_end) {
731 breakAgain(need_break_row);
732 need_break_row = rows_end;
738 // important for the screen
741 // the cursor set functions have a special mechanism. When they
742 // realize, that you left an empty paragraph, they will delete it.
743 // They also delete the corresponding row
745 // need the selection cursor:
746 void LyXText::setSelection()
748 bool const lsel = TextCursor::setSelection();
750 if (inset_owner && (selection.set() || lsel))
751 inset_owner->setUpdateStatus(InsetText::SELECTION);
756 void LyXText::clearSelection()
758 TextCursor::clearSelection();
760 // reset this in the bv_owner!
761 if (bv_owner && bv_owner->text)
762 bv_owner->text->xsel_cache.set(false);
766 void LyXText::cursorHome()
768 setCursor(cursor.par(), cursorRow()->pos());
772 void LyXText::cursorEnd()
774 if (cursor.par()->empty())
777 RowList::iterator rit = cursorRow();
778 RowList::iterator next_rit = boost::next(rit);
779 ParagraphList::iterator pit = rit->par();
780 pos_type last_pos = lastPos(*this, rit);
782 if (next_rit == rows().end() || next_rit->par() != pit) {
786 (pit->getChar(last_pos) != ' ' && !pit->isNewline(last_pos))) {
791 setCursor(pit, last_pos);
795 void LyXText::cursorTop()
797 setCursor(ownerParagraphs().begin(), 0);
801 void LyXText::cursorBottom()
803 ParagraphList::iterator lastpit =
804 boost::prior(ownerParagraphs().end());
805 setCursor(lastpit, lastpit->size());
809 void LyXText::toggleFree(LyXFont const & font, bool toggleall)
811 // If the mask is completely neutral, tell user
812 if (font == LyXFont(LyXFont::ALL_IGNORE)) {
813 // Could only happen with user style
814 bv()->owner()->message(_("No font change defined. Use Character under the Layout menu to define font change."));
818 // Try implicit word selection
819 // If there is a change in the language the implicit word selection
821 LyXCursor resetCursor = cursor;
822 bool implicitSelection = (font.language() == ignore_language
823 && font.number() == LyXFont::IGNORE)
824 ? selectWordWhenUnderCursor(lyx::WHOLE_WORD_STRICT) : false;
827 setFont(font, toggleall);
829 // Implicit selections are cleared afterwards
830 //and cursor is set to the original position.
831 if (implicitSelection) {
833 cursor = resetCursor;
834 setCursor(cursor.par(), cursor.pos());
835 selection.cursor = cursor;
838 inset_owner->setUpdateStatus(InsetText::CURSOR_PAR);
842 string LyXText::getStringToIndex()
844 // Try implicit word selection
845 // If there is a change in the language the implicit word selection
847 LyXCursor const reset_cursor = cursor;
848 bool const implicitSelection =
849 selectWordWhenUnderCursor(lyx::PREVIOUS_WORD);
852 if (!selection.set())
853 bv()->owner()->message(_("Nothing to index!"));
854 else if (selection.start.par() != selection.end.par())
855 bv()->owner()->message(_("Cannot index more than one paragraph!"));
857 idxstring = selectionAsString(bv()->buffer(), false);
859 // Reset cursors to their original position.
860 cursor = reset_cursor;
861 setCursor(cursor.par(), cursor.pos());
862 selection.cursor = cursor;
864 // Clear the implicit selection.
865 if (implicitSelection)
872 // the DTP switches for paragraphs. LyX will store them in the first
873 // physicla paragraph. When a paragraph is broken, the top settings rest,
874 // the bottom settings are given to the new one. So I can make shure,
875 // they do not duplicate themself and you cannnot make dirty things with
878 void LyXText::setParagraph(bool line_top, bool line_bottom,
879 bool pagebreak_top, bool pagebreak_bottom,
880 VSpace const & space_top,
881 VSpace const & space_bottom,
882 Spacing const & spacing,
884 string const & labelwidthstring,
887 LyXCursor tmpcursor = cursor;
888 if (!selection.set()) {
889 selection.start = cursor;
890 selection.end = cursor;
893 // make sure that the depth behind the selection are restored, too
894 ParagraphList::iterator endpit = boost::next(selection.end.par());
895 ParagraphList::iterator undoendpit = endpit;
896 ParagraphList::iterator pars_end = ownerParagraphs().end();
898 if (endpit != pars_end && endpit->getDepth()) {
899 while (endpit != pars_end && endpit->getDepth()) {
903 } else if (endpit != pars_end) {
904 // because of parindents etc.
908 setUndo(bv(), Undo::EDIT, selection.start.par(),
909 boost::prior(undoendpit));
912 ParagraphList::iterator tmppit = selection.end.par();
914 while (tmppit != boost::prior(selection.start.par())) {
915 setCursor(tmppit, 0);
917 ParagraphList::iterator pit = cursor.par();
918 ParagraphParameters & params = pit->params();
920 params.lineTop(line_top);
921 params.lineBottom(line_bottom);
922 params.pagebreakTop(pagebreak_top);
923 params.pagebreakBottom(pagebreak_bottom);
924 params.spaceTop(space_top);
925 params.spaceBottom(space_bottom);
926 params.spacing(spacing);
927 // does the layout allow the new alignment?
928 LyXLayout_ptr const & layout = pit->layout();
930 if (align == LYX_ALIGN_LAYOUT)
931 align = layout->align;
932 if (align & layout->alignpossible) {
933 if (align == layout->align)
934 params.align(LYX_ALIGN_LAYOUT);
938 pit->setLabelWidthString(labelwidthstring);
939 params.noindent(noindent);
940 tmppit = boost::prior(pit);
944 redoParagraphs(selection.start, endpit);
947 setCursor(selection.start.par(), selection.start.pos());
948 selection.cursor = cursor;
949 setCursor(selection.end.par(), selection.end.pos());
951 setCursor(tmpcursor.par(), tmpcursor.pos());
953 bv()->updateInset(inset_owner);
957 // set the counter of a paragraph. This includes the labels
958 void LyXText::setCounter(Buffer const * buf, ParagraphList::iterator pit)
960 LyXTextClass const & textclass = buf->params.getLyXTextClass();
961 LyXLayout_ptr const & layout = pit->layout();
963 if (pit != ownerParagraphs().begin()) {
965 pit->params().appendix(boost::prior(pit)->params().appendix());
966 if (!pit->params().appendix() &&
967 pit->params().startOfAppendix()) {
968 pit->params().appendix(true);
969 textclass.counters().reset();
971 pit->enumdepth = boost::prior(pit)->enumdepth;
972 pit->itemdepth = boost::prior(pit)->itemdepth;
974 pit->params().appendix(pit->params().startOfAppendix());
979 /* Maybe we have to increment the enumeration depth.
980 * BUT, enumeration in a footnote is considered in isolation from its
981 * surrounding paragraph so don't increment if this is the
982 * first line of the footnote
983 * AND, bibliographies can't have their depth changed ie. they
984 * are always of depth 0
986 if (pit != ownerParagraphs().begin()
987 && boost::prior(pit)->getDepth() < pit->getDepth()
988 && boost::prior(pit)->layout()->labeltype == LABEL_COUNTER_ENUMI
989 && pit->enumdepth < 3
990 && layout->labeltype != LABEL_BIBLIO) {
994 // Maybe we have to decrement the enumeration depth, see note above
995 if (pit != ownerParagraphs().begin()
996 && boost::prior(pit)->getDepth() > pit->getDepth()
997 && layout->labeltype != LABEL_BIBLIO) {
998 pit->enumdepth = depthHook(pit, ownerParagraphs(),
999 pit->getDepth())->enumdepth;
1002 if (!pit->params().labelString().empty()) {
1003 pit->params().labelString(string());
1006 if (layout->margintype == MARGIN_MANUAL) {
1007 if (pit->params().labelWidthString().empty()) {
1008 pit->setLabelWidthString(layout->labelstring());
1011 pit->setLabelWidthString(string());
1014 // is it a layout that has an automatic label?
1015 if (layout->labeltype >= LABEL_COUNTER_CHAPTER) {
1016 int const i = layout->labeltype - LABEL_COUNTER_CHAPTER;
1020 if (i >= 0 && i <= buf->params.secnumdepth) {
1024 textclass.counters().step(layout->latexname());
1026 // Is there a label? Useful for Chapter layout
1027 if (!pit->params().appendix()) {
1028 s << buf->B_(layout->labelstring());
1030 s << buf->B_(layout->labelstring_appendix());
1033 // Use of an integer is here less than elegant. For now.
1034 int head = textclass.maxcounter() - LABEL_COUNTER_CHAPTER;
1035 if (!pit->params().appendix()) {
1036 numbertype = "sectioning";
1038 numbertype = "appendix";
1039 if (pit->isRightToLeftPar(buf->params))
1040 langtype = "hebrew";
1046 << textclass.counters()
1047 .numberLabel(layout->latexname(),
1048 numbertype, langtype, head);
1050 pit->params().labelString(STRCONV(s.str()));
1052 // reset enum counters
1053 textclass.counters().reset("enum");
1054 } else if (layout->labeltype < LABEL_COUNTER_ENUMI) {
1055 textclass.counters().reset("enum");
1056 } else if (layout->labeltype == LABEL_COUNTER_ENUMI) {
1058 // Yes I know this is a really, really! bad solution
1060 string enumcounter("enum");
1062 switch (pit->enumdepth) {
1071 enumcounter += "iv";
1074 // not a valid enumdepth...
1078 textclass.counters().step(enumcounter);
1080 s << textclass.counters()
1081 .numberLabel(enumcounter, "enumeration");
1082 pit->params().labelString(STRCONV(s.str()));
1084 } else if (layout->labeltype == LABEL_BIBLIO) {// ale970302
1085 textclass.counters().step("bibitem");
1086 int number = textclass.counters().value("bibitem");
1087 if (pit->bibitem()) {
1088 pit->bibitem()->setCounter(number);
1089 pit->params().labelString(layout->labelstring());
1091 // In biblio should't be following counters but...
1093 string s = buf->B_(layout->labelstring());
1095 // the caption hack:
1096 if (layout->labeltype == LABEL_SENSITIVE) {
1097 ParagraphList::iterator tmppit = pit;
1100 while (tmppit != ownerParagraphs().end() &&
1102 // the single '=' is intended below
1103 && (in = tmppit->inInset()->owner())) {
1104 if (in->lyxCode() == Inset::FLOAT_CODE ||
1105 in->lyxCode() == Inset::WRAP_CODE) {
1109 tmppit = std::find(ownerParagraphs().begin(), ownerParagraphs().end(), *in->parOwner());
1116 if (in->lyxCode() == Inset::FLOAT_CODE)
1117 type = static_cast<InsetFloat*>(in)->params().type;
1118 else if (in->lyxCode() == Inset::WRAP_CODE)
1119 type = static_cast<InsetWrap*>(in)->params().type;
1123 Floating const & fl = textclass.floats().getType(type);
1125 textclass.counters().step(fl.type());
1127 // Doesn't work... yet.
1128 s = bformat(_("%1$s #:"), buf->B_(fl.name()));
1130 // par->SetLayout(0);
1131 // s = layout->labelstring;
1132 s = _("Senseless: ");
1135 pit->params().labelString(s);
1137 // reset the enumeration counter. They are always reset
1138 // when there is any other layout between
1139 // Just fall-through between the cases so that all
1140 // enum counters deeper than enumdepth is also reset.
1141 switch (pit->enumdepth) {
1143 textclass.counters().reset("enumi");
1145 textclass.counters().reset("enumii");
1147 textclass.counters().reset("enumiii");
1149 textclass.counters().reset("enumiv");
1155 // Updates all counters. Paragraphs with changed label string will be rebroken
1156 void LyXText::updateCounters()
1158 RowList::iterator rowit = rows().begin();
1159 ParagraphList::iterator pit = rowit->par();
1161 // CHECK if this is really needed. (Lgb)
1162 bv()->buffer()->params.getLyXTextClass().counters().reset();
1164 ParagraphList::iterator beg = ownerParagraphs().begin();
1165 ParagraphList::iterator end = ownerParagraphs().end();
1166 for (; pit != end; ++pit) {
1167 while (rowit->par() != pit)
1170 string const oldLabel = pit->params().labelString();
1172 size_t maxdepth = 0;
1174 maxdepth = boost::prior(pit)->getMaxDepthAfter();
1176 if (pit->params().depth() > maxdepth)
1177 pit->params().depth(maxdepth);
1179 // setCounter can potentially change the labelString.
1180 setCounter(bv()->buffer(), pit);
1182 string const & newLabel = pit->params().labelString();
1184 if (oldLabel.empty() && !newLabel.empty()) {
1185 removeParagraph(rowit);
1186 appendParagraph(rowit);
1192 void LyXText::insertInset(Inset * inset)
1194 if (!cursor.par()->insetAllowed(inset->lyxCode()))
1196 setUndo(bv(), Undo::FINISH, cursor.par());
1198 cursor.par()->insertInset(cursor.pos(), inset);
1199 // Just to rebreak and refresh correctly.
1200 // The character will not be inserted a second time
1201 insertChar(Paragraph::META_INSET);
1202 // If we enter a highly editable inset the cursor should be to before
1203 // the inset. This couldn't happen before as Undo was not handled inside
1204 // inset now after the Undo LyX tries to call inset->Edit(...) again
1205 // and cannot do this as the cursor is behind the inset and GetInset
1206 // does not return the inset!
1207 if (isHighlyEditableInset(inset)) {
1214 void LyXText::cutSelection(bool doclear, bool realcut)
1216 // Stuff what we got on the clipboard. Even if there is no selection.
1218 // There is a problem with having the stuffing here in that the
1219 // larger the selection the slower LyX will get. This can be
1220 // solved by running the line below only when the selection has
1221 // finished. The solution used currently just works, to make it
1222 // faster we need to be more clever and probably also have more
1223 // calls to stuffClipboard. (Lgb)
1224 bv()->stuffClipboard(selectionAsString(bv()->buffer(), true));
1226 // This doesn't make sense, if there is no selection
1227 if (!selection.set())
1230 // OK, we have a selection. This is always between selection.start
1231 // and selection.end
1233 // make sure that the depth behind the selection are restored, too
1234 ParagraphList::iterator endpit = boost::next(selection.end.par());
1235 ParagraphList::iterator undoendpit = endpit;
1236 ParagraphList::iterator pars_end = ownerParagraphs().end();
1238 if (endpit != pars_end && endpit->getDepth()) {
1239 while (endpit != pars_end && endpit->getDepth()) {
1241 undoendpit = endpit;
1243 } else if (endpit != pars_end) {
1244 // because of parindents etc.
1248 setUndo(bv(), Undo::DELETE, selection.start.par(),
1249 boost::prior(undoendpit));
1252 endpit = selection.end.par();
1253 int endpos = selection.end.pos();
1255 boost::tie(endpit, endpos) = realcut ?
1256 CutAndPaste::cutSelection(bv()->buffer()->params,
1258 selection.start.par(), endpit,
1259 selection.start.pos(), endpos,
1260 bv()->buffer()->params.textclass,
1262 : CutAndPaste::eraseSelection(bv()->buffer()->params,
1264 selection.start.par(), endpit,
1265 selection.start.pos(), endpos,
1267 // sometimes necessary
1269 selection.start.par()->stripLeadingSpaces();
1271 redoParagraphs(selection.start, boost::next(endpit));
1272 #warning FIXME latent bug
1273 // endpit will be invalidated on redoParagraphs once ParagraphList
1274 // becomes a std::list? There are maybe other places on which this
1275 // can happend? (Ab)
1276 // cutSelection can invalidate the cursor so we need to set
1278 // we prefer the end for when tracking changes
1282 // need a valid cursor. (Lgb)
1285 setCursor(cursor.par(), cursor.pos());
1286 selection.cursor = cursor;
1291 void LyXText::copySelection()
1293 // stuff the selection onto the X clipboard, from an explicit copy request
1294 bv()->stuffClipboard(selectionAsString(bv()->buffer(), true));
1296 // this doesnt make sense, if there is no selection
1297 if (!selection.set())
1300 // ok we have a selection. This is always between selection.start
1301 // and sel_end cursor
1303 // copy behind a space if there is one
1304 while (selection.start.par()->size() > selection.start.pos()
1305 && selection.start.par()->isLineSeparator(selection.start.pos())
1306 && (selection.start.par() != selection.end.par()
1307 || selection.start.pos() < selection.end.pos()))
1308 selection.start.pos(selection.start.pos() + 1);
1310 CutAndPaste::copySelection(selection.start.par(),
1311 selection.end.par(),
1312 selection.start.pos(), selection.end.pos(),
1313 bv()->buffer()->params.textclass);
1317 void LyXText::pasteSelection(size_t sel_index)
1319 // this does not make sense, if there is nothing to paste
1320 if (!CutAndPaste::checkPastePossible())
1323 setUndo(bv(), Undo::INSERT, cursor.par());
1325 ParagraphList::iterator endpit;
1330 boost::tie(ppp, endpit) =
1331 CutAndPaste::pasteSelection(*bv()->buffer(),
1333 cursor.par(), cursor.pos(),
1334 bv()->buffer()->params.textclass,
1336 bufferErrors(*bv()->buffer(), el);
1337 bv()->showErrorList(_("Paste"));
1339 redoParagraphs(cursor, endpit);
1341 setCursor(cursor.par(), cursor.pos());
1344 selection.cursor = cursor;
1345 setCursor(ppp.first, ppp.second);
1351 void LyXText::setSelectionRange(lyx::pos_type length)
1356 selection.cursor = cursor;
1363 // simple replacing. The font of the first selected character is used
1364 void LyXText::replaceSelectionWithString(string const & str)
1366 setCursorParUndo(bv());
1369 if (!selection.set()) { // create a dummy selection
1370 selection.end = cursor;
1371 selection.start = cursor;
1374 // Get font setting before we cut
1375 pos_type pos = selection.end.pos();
1376 LyXFont const font = selection.start.par()
1377 ->getFontSettings(bv()->buffer()->params,
1378 selection.start.pos());
1380 // Insert the new string
1381 string::const_iterator cit = str.begin();
1382 string::const_iterator end = str.end();
1383 for (; cit != end; ++cit) {
1384 selection.end.par()->insertChar(pos, (*cit), font);
1388 // Cut the selection
1389 cutSelection(true, false);
1395 // needed to insert the selection
1396 void LyXText::insertStringAsLines(string const & str)
1398 ParagraphList::iterator pit = cursor.par();
1399 pos_type pos = cursor.pos();
1400 ParagraphList::iterator endpit = boost::next(cursor.par());
1402 setCursorParUndo(bv());
1404 // only to be sure, should not be neccessary
1407 bv()->buffer()->insertStringAsLines(pit, pos, current_font, str);
1409 redoParagraphs(cursor, endpit);
1410 setCursor(cursor.par(), cursor.pos());
1411 selection.cursor = cursor;
1412 setCursor(pit, pos);
1417 // turns double-CR to single CR, others where converted into one
1418 // blank. Then InsertStringAsLines is called
1419 void LyXText::insertStringAsParagraphs(string const & str)
1421 string linestr(str);
1422 bool newline_inserted = false;
1423 string::size_type const siz = linestr.length();
1425 for (string::size_type i = 0; i < siz; ++i) {
1426 if (linestr[i] == '\n') {
1427 if (newline_inserted) {
1428 // we know that \r will be ignored by
1429 // InsertStringA. Of course, it is a dirty
1430 // trick, but it works...
1431 linestr[i - 1] = '\r';
1435 newline_inserted = true;
1437 } else if (IsPrintable(linestr[i])) {
1438 newline_inserted = false;
1441 insertStringAsLines(linestr);
1445 void LyXText::checkParagraph(ParagraphList::iterator pit, pos_type pos)
1447 LyXCursor tmpcursor;
1450 RowList::iterator row = getRow(pit, pos);
1451 RowList::iterator beg = rows().begin();
1453 // is there a break one row above
1454 if (row != beg && boost::prior(row)->par() == row->par()) {
1455 z = rowBreakPoint(*boost::prior(row));
1456 if (z >= row->pos()) {
1457 // set the dimensions of the row above
1460 breakAgain(boost::prior(row));
1462 // set the cursor again. Otherwise
1463 // dangling pointers are possible
1464 setCursor(cursor.par(), cursor.pos(),
1465 false, cursor.boundary());
1466 selection.cursor = cursor;
1474 // set the cursor again. Otherwise dangling pointers are possible
1475 // also set the selection
1477 if (selection.set()) {
1479 setCursorIntern(selection.cursor.par(), selection.cursor.pos(),
1480 false, selection.cursor.boundary());
1481 selection.cursor = cursor;
1482 setCursorIntern(selection.start.par(),
1483 selection.start.pos(),
1484 false, selection.start.boundary());
1485 selection.start = cursor;
1486 setCursorIntern(selection.end.par(),
1487 selection.end.pos(),
1488 false, selection.end.boundary());
1489 selection.end = cursor;
1490 setCursorIntern(last_sel_cursor.par(),
1491 last_sel_cursor.pos(),
1492 false, last_sel_cursor.boundary());
1493 last_sel_cursor = cursor;
1496 setCursorIntern(cursor.par(), cursor.pos(),
1497 false, cursor.boundary());
1501 // returns false if inset wasn't found
1502 bool LyXText::updateInset(Inset * inset)
1504 // first check the current paragraph
1505 int pos = cursor.par()->getPositionOfInset(inset);
1507 checkParagraph(cursor.par(), pos);
1511 // check every paragraph
1513 ParagraphList::iterator par = ownerParagraphs().begin();
1514 ParagraphList::iterator end = ownerParagraphs().end();
1515 for (; par != end; ++par) {
1516 pos = par->getPositionOfInset(inset);
1518 checkParagraph(par, pos);
1527 bool LyXText::setCursor(ParagraphList::iterator pit,
1529 bool setfont, bool boundary)
1531 LyXCursor old_cursor = cursor;
1532 setCursorIntern(pit, pos, setfont, boundary);
1533 return deleteEmptyParagraphMechanism(old_cursor);
1537 void LyXText::setCursor(LyXCursor & cur, ParagraphList::iterator pit,
1538 pos_type pos, bool boundary)
1540 Assert(pit != ownerParagraphs().end());
1544 cur.boundary(boundary);
1548 // get the cursor y position in text
1550 RowList::iterator row = getRow(pit, pos, y);
1551 RowList::iterator beg = rows().begin();
1553 RowList::iterator old_row = row;
1554 // if we are before the first char of this row and are still in the
1555 // same paragraph and there is a previous row then put the cursor on
1556 // the end of the previous row
1557 cur.iy(y + row->baseline());
1560 boost::prior(row)->par() == row->par() &&
1561 pos < pit->size() &&
1562 pit->getChar(pos) == Paragraph::META_INSET) {
1563 Inset * ins = pit->getInset(pos);
1564 if (ins && (ins->needFullRow() || ins->display())) {
1570 // y is now the beginning of the cursor row
1571 y += row->baseline();
1572 // y is now the cursor baseline
1575 pos_type last = lastPrintablePos(*this, old_row);
1577 // None of these should happen, but we're scaredy-cats
1578 if (pos > pit->size()) {
1579 lyxerr << "dont like 1 please report" << endl;
1582 } else if (pos > last + 1) {
1583 lyxerr << "dont like 2 please report" << endl;
1584 // This shouldn't happen.
1587 } else if (pos < row->pos()) {
1588 lyxerr << "dont like 3 please report" << endl;
1593 // now get the cursors x position
1594 float x = getCursorX(row, pos, last, boundary);
1597 if (old_row != row) {
1598 x = getCursorX(old_row, pos, last, boundary);
1602 /* We take out this for the time being because 1) the redraw code is not
1603 prepared to this yet and 2) because some good policy has yet to be decided
1604 while editting: for instance how to act on rows being created/deleted
1608 //if the cursor is in a visible row, anchor to it
1610 if (topy < y && y < topy + bv()->workHeight())
1616 float LyXText::getCursorX(RowList::iterator rit,
1617 pos_type pos, pos_type last, bool boundary) const
1619 pos_type cursor_vpos = 0;
1621 float fill_separator;
1623 float fill_label_hfill;
1624 // This call HAS to be here because of the BidiTables!!!
1625 prepareToPrint(rit, x, fill_separator, fill_hfill,
1628 ParagraphList::iterator rit_par = rit->par();
1629 pos_type const rit_pos = rit->pos();
1632 cursor_vpos = rit_pos;
1633 else if (pos > last && !boundary)
1634 cursor_vpos = (rit_par->isRightToLeftPar(bv()->buffer()->params))
1635 ? rit_pos : last + 1;
1636 else if (pos > rit_pos && (pos > last || boundary))
1637 /// Place cursor after char at (logical) position pos - 1
1638 cursor_vpos = (bidi_level(pos - 1) % 2 == 0)
1639 ? log2vis(pos - 1) + 1 : log2vis(pos - 1);
1641 /// Place cursor before char at (logical) position pos
1642 cursor_vpos = (bidi_level(pos) % 2 == 0)
1643 ? log2vis(pos) : log2vis(pos) + 1;
1645 pos_type body_pos = rit_par->beginningOfBody();
1646 if ((body_pos > 0) &&
1647 ((body_pos - 1 > last) || !rit_par->isLineSeparator(body_pos - 1)))
1650 for (pos_type vpos = rit_pos; vpos < cursor_vpos; ++vpos) {
1651 pos_type pos = vis2log(vpos);
1652 if (body_pos > 0 && pos == body_pos - 1) {
1653 x += fill_label_hfill +
1654 font_metrics::width(
1655 rit_par->layout()->labelsep,
1656 getLabelFont(bv()->buffer(), rit_par));
1657 if (rit_par->isLineSeparator(body_pos - 1))
1658 x -= singleWidth(rit_par, body_pos - 1);
1661 if (hfillExpansion(*this, rit, pos)) {
1662 x += singleWidth(rit_par, pos);
1663 if (pos >= body_pos)
1666 x += fill_label_hfill;
1667 } else if (rit_par->isSeparator(pos)) {
1668 x += singleWidth(rit_par, pos);
1669 if (pos >= body_pos)
1670 x += fill_separator;
1672 x += singleWidth(rit_par, pos);
1678 void LyXText::setCursorIntern(ParagraphList::iterator pit,
1679 pos_type pos, bool setfont, bool boundary)
1681 UpdatableInset * it = pit->inInset();
1683 if (it != inset_owner) {
1684 lyxerr[Debug::INSETS] << "InsetText is " << it
1686 << "inset_owner is "
1687 << inset_owner << endl;
1688 #ifdef WITH_WARNINGS
1689 #warning I believe this code is wrong. (Lgb)
1690 #warning Jürgen, have a look at this. (Lgb)
1691 #warning Hmmm, I guess you are right but we
1692 #warning should verify when this is needed
1694 // Jürgen, would you like to have a look?
1695 // I guess we need to move the outer cursor
1696 // and open and lock the inset (bla bla bla)
1697 // stuff I don't know... so can you have a look?
1699 // I moved the lyxerr stuff in here so we can see if
1700 // this is actually really needed and where!
1702 // it->getLyXText(bv())->setCursorIntern(bv(), par, pos, setfont, boundary);
1707 setCursor(cursor, pit, pos, boundary);
1713 void LyXText::setCurrentFont()
1715 pos_type pos = cursor.pos();
1716 ParagraphList::iterator pit = cursor.par();
1718 if (cursor.boundary() && pos > 0)
1722 if (pos == pit->size())
1724 else // potentional bug... BUG (Lgb)
1725 if (pit->isSeparator(pos)) {
1726 if (pos > cursorRow()->pos() &&
1727 bidi_level(pos) % 2 ==
1728 bidi_level(pos - 1) % 2)
1730 else if (pos + 1 < pit->size())
1736 pit->getFontSettings(bv()->buffer()->params, pos);
1737 real_current_font = getFont(bv()->buffer(), pit, pos);
1739 if (cursor.pos() == pit->size() &&
1740 isBoundary(bv()->buffer(), *pit, cursor.pos()) &&
1741 !cursor.boundary()) {
1742 Language const * lang =
1743 pit->getParLanguage(bv()->buffer()->params);
1744 current_font.setLanguage(lang);
1745 current_font.setNumber(LyXFont::OFF);
1746 real_current_font.setLanguage(lang);
1747 real_current_font.setNumber(LyXFont::OFF);
1752 // returns the column near the specified x-coordinate of the row
1753 // x is set to the real beginning of this column
1755 LyXText::getColumnNearX(RowList::iterator rit, int & x, bool & boundary) const
1758 float fill_separator;
1760 float fill_label_hfill;
1762 prepareToPrint(rit, tmpx, fill_separator,
1763 fill_hfill, fill_label_hfill);
1765 pos_type vc = rit->pos();
1766 pos_type last = lastPrintablePos(*this, rit);
1769 ParagraphList::iterator rit_par = rit->par();
1770 LyXLayout_ptr const & layout = rit->par()->layout();
1772 bool left_side = false;
1774 pos_type body_pos = rit_par->beginningOfBody();
1775 float last_tmpx = tmpx;
1778 (body_pos - 1 > last ||
1779 !rit_par->isLineSeparator(body_pos - 1)))
1782 // check for empty row
1783 if (!rit_par->size()) {
1788 while (vc <= last && tmpx <= x) {
1791 if (body_pos > 0 && c == body_pos - 1) {
1792 tmpx += fill_label_hfill +
1793 font_metrics::width(layout->labelsep,
1794 getLabelFont(bv()->buffer(), rit_par));
1795 if (rit_par->isLineSeparator(body_pos - 1))
1796 tmpx -= singleWidth(rit_par, body_pos - 1);
1799 if (hfillExpansion(*this, rit, c)) {
1800 tmpx += singleWidth(rit_par, c);
1804 tmpx += fill_label_hfill;
1805 } else if (rit_par->isSeparator(c)) {
1806 tmpx += singleWidth(rit_par, c);
1808 tmpx+= fill_separator;
1810 tmpx += singleWidth(rit_par, c);
1815 if ((tmpx + last_tmpx) / 2 > x) {
1820 if (vc > last + 1) // This shouldn't happen.
1824 // This (rtl_support test) is not needed, but gives
1825 // some speedup if rtl_support=false
1826 RowList::iterator next_rit = boost::next(rit);
1828 bool const lastrow = lyxrc.rtl_support &&
1829 (next_rit == rowlist_.end() ||
1830 next_rit->par() != rit_par);
1832 // If lastrow is false, we don't need to compute
1833 // the value of rtl.
1834 bool const rtl = (lastrow)
1835 ? rit_par->isRightToLeftPar(bv()->buffer()->params)
1838 ((rtl && left_side && vc == rit->pos() && x < tmpx - 5) ||
1839 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5)))
1841 else if (vc == rit->pos()) {
1843 if (bidi_level(c) % 2 == 1)
1846 c = vis2log(vc - 1);
1847 bool const rtl = (bidi_level(c) % 2 == 1);
1848 if (left_side == rtl) {
1850 boundary = isBoundary(bv()->buffer(), *rit_par, c);
1854 if (rit->pos() <= last && c > last
1855 && rit_par->isNewline(last)) {
1856 if (bidi_level(last) % 2 == 0)
1857 tmpx -= singleWidth(rit_par, last);
1859 tmpx += singleWidth(rit_par, last);
1869 void LyXText::setCursorFromCoordinates(int x, int y)
1871 LyXCursor old_cursor = cursor;
1873 setCursorFromCoordinates(cursor, x, y);
1875 deleteEmptyParagraphMechanism(old_cursor);
1882 * return true if the cursor given is at the end of a row,
1883 * and the next row is filled by an inset that spans an entire
1886 bool beforeFullRowInset(LyXText & lt, LyXCursor const & cur)
1888 RowList::iterator row = lt.getRow(cur);
1889 if (boost::next(row) == lt.rows().end())
1892 Row const & next = *boost::next(row);
1894 if (next.pos() != cur.pos() || next.par() != cur.par())
1897 if (cur.pos() == cur.par()->size()
1898 || !cur.par()->isInset(cur.pos()))
1901 Inset const * inset = cur.par()->getInset(cur.pos());
1902 if (inset->needFullRow() || inset->display())
1910 void LyXText::setCursorFromCoordinates(LyXCursor & cur, int x, int y)
1912 // Get the row first.
1914 RowList::iterator row = getRowNearY(y);
1916 pos_type const column = getColumnNearX(row, x, bound);
1917 cur.par(row->par());
1918 cur.pos(row->pos() + column);
1920 cur.y(y + row->baseline());
1922 if (beforeFullRowInset(*this, cur)) {
1923 pos_type const last = lastPrintablePos(*this, row);
1924 RowList::iterator next_row = boost::next(row);
1926 float x = getCursorX(next_row, cur.pos(), last, bound);
1928 cur.iy(y + row->height() + next_row->baseline());
1933 cur.boundary(bound);
1937 void LyXText::cursorLeft(bool internal)
1939 if (cursor.pos() > 0) {
1940 bool boundary = cursor.boundary();
1941 setCursor(cursor.par(), cursor.pos() - 1, true, false);
1942 if (!internal && !boundary &&
1943 isBoundary(bv()->buffer(), *cursor.par(), cursor.pos() + 1))
1944 setCursor(cursor.par(), cursor.pos() + 1, true, true);
1945 } else if (cursor.par() != ownerParagraphs().begin()) { // steps into the above paragraph.
1946 ParagraphList::iterator pit = boost::prior(cursor.par());
1947 setCursor(pit, pit->size());
1952 void LyXText::cursorRight(bool internal)
1954 bool const at_end = (cursor.pos() == cursor.par()->size());
1955 bool const at_newline = !at_end &&
1956 cursor.par()->isNewline(cursor.pos());
1958 if (!internal && cursor.boundary() && !at_newline)
1959 setCursor(cursor.par(), cursor.pos(), true, false);
1961 setCursor(cursor.par(), cursor.pos() + 1, true, false);
1963 isBoundary(bv()->buffer(), *cursor.par(), cursor.pos()))
1964 setCursor(cursor.par(), cursor.pos(), true, true);
1965 } else if (boost::next(cursor.par()) != ownerParagraphs().end())
1966 setCursor(boost::next(cursor.par()), 0);
1970 void LyXText::cursorUp(bool selecting)
1973 int x = cursor.x_fix();
1974 int y = cursor.y() - cursorRow()->baseline() - 1;
1975 setCursorFromCoordinates(x, y);
1978 int y1 = cursor.iy() - topy;
1981 Inset * inset_hit = checkInsetHit(x, y1);
1982 if (inset_hit && isHighlyEditableInset(inset_hit)) {
1983 inset_hit->localDispatch(
1984 FuncRequest(bv(), LFUN_INSET_EDIT, x, y - (y2 - y1), mouse_button::none));
1988 setCursorFromCoordinates(bv(), cursor.x_fix(),
1989 cursor.y() - cursorRow()->baseline() - 1);
1994 void LyXText::cursorDown(bool selecting)
1997 int x = cursor.x_fix();
1998 int y = cursor.y() - cursorRow()->baseline() + cursorRow()->height() + 1;
1999 setCursorFromCoordinates(x, y);
2000 if (!selecting && cursorRow() == cursorIRow()) {
2002 int y1 = cursor.iy() - topy;
2005 Inset * inset_hit = checkInsetHit(x, y1);
2006 if (inset_hit && isHighlyEditableInset(inset_hit)) {
2007 FuncRequest cmd(bv(), LFUN_INSET_EDIT, x, y - (y2 - y1), mouse_button::none);
2008 inset_hit->localDispatch(cmd);
2012 setCursorFromCoordinates(bv(), cursor.x_fix(),
2013 cursor.y() - cursorRow()->baseline()
2014 + cursorRow()->height() + 1);
2019 void LyXText::cursorUpParagraph()
2021 if (cursor.pos() > 0) {
2022 setCursor(cursor.par(), 0);
2024 else if (cursor.par() != ownerParagraphs().begin()) {
2025 setCursor(boost::prior(cursor.par()), 0);
2030 void LyXText::cursorDownParagraph()
2032 ParagraphList::iterator par = cursor.par();
2033 ParagraphList::iterator next_par = boost::next(par);
2035 if (next_par != ownerParagraphs().end()) {
2036 setCursor(next_par, 0);
2038 setCursor(par, par->size());
2042 // fix the cursor `cur' after a characters has been deleted at `where'
2043 // position. Called by deleteEmptyParagraphMechanism
2044 void LyXText::fixCursorAfterDelete(LyXCursor & cur,
2045 LyXCursor const & where)
2047 // if cursor is not in the paragraph where the delete occured,
2049 if (cur.par() != where.par())
2052 // if cursor position is after the place where the delete occured,
2054 if (cur.pos() > where.pos())
2055 cur.pos(cur.pos()-1);
2057 // check also if we don't want to set the cursor on a spot behind the
2058 // pagragraph because we erased the last character.
2059 if (cur.pos() > cur.par()->size())
2060 cur.pos(cur.par()->size());
2062 // recompute row et al. for this cursor
2063 setCursor(cur, cur.par(), cur.pos(), cur.boundary());
2067 bool LyXText::deleteEmptyParagraphMechanism(LyXCursor const & old_cursor)
2069 // Would be wrong to delete anything if we have a selection.
2070 if (selection.set())
2073 // We allow all kinds of "mumbo-jumbo" when freespacing.
2074 if (old_cursor.par()->layout()->free_spacing
2075 || old_cursor.par()->isFreeSpacing()) {
2079 /* Ok I'll put some comments here about what is missing.
2080 I have fixed BackSpace (and thus Delete) to not delete
2081 double-spaces automagically. I have also changed Cut,
2082 Copy and Paste to hopefully do some sensible things.
2083 There are still some small problems that can lead to
2084 double spaces stored in the document file or space at
2085 the beginning of paragraphs. This happens if you have
2086 the cursor betwenn to spaces and then save. Or if you
2087 cut and paste and the selection have a space at the
2088 beginning and then save right after the paste. I am
2089 sure none of these are very hard to fix, but I will
2090 put out 1.1.4pre2 with FIX_DOUBLE_SPACE defined so
2091 that I can get some feedback. (Lgb)
2094 // If old_cursor.pos() == 0 and old_cursor.pos()(1) == LineSeparator
2095 // delete the LineSeparator.
2098 // If old_cursor.pos() == 1 and old_cursor.pos()(0) == LineSeparator
2099 // delete the LineSeparator.
2102 // If the pos around the old_cursor were spaces, delete one of them.
2103 if (old_cursor.par() != cursor.par()
2104 || old_cursor.pos() != cursor.pos()) {
2105 // Only if the cursor has really moved
2107 if (old_cursor.pos() > 0
2108 && old_cursor.pos() < old_cursor.par()->size()
2109 && old_cursor.par()->isLineSeparator(old_cursor.pos())
2110 && old_cursor.par()->isLineSeparator(old_cursor.pos() - 1)) {
2111 old_cursor.par()->erase(old_cursor.pos() - 1);
2112 redoParagraphs(old_cursor, boost::next(old_cursor.par()));
2114 #ifdef WITH_WARNINGS
2115 #warning This will not work anymore when we have multiple views of the same buffer
2116 // In this case, we will have to correct also the cursors held by
2117 // other bufferviews. It will probably be easier to do that in a more
2118 // automated way in LyXCursor code. (JMarc 26/09/2001)
2120 // correct all cursors held by the LyXText
2121 fixCursorAfterDelete(cursor, old_cursor);
2122 fixCursorAfterDelete(selection.cursor,
2124 fixCursorAfterDelete(selection.start,
2126 fixCursorAfterDelete(selection.end, old_cursor);
2127 fixCursorAfterDelete(last_sel_cursor,
2129 fixCursorAfterDelete(toggle_cursor, old_cursor);
2130 fixCursorAfterDelete(toggle_end_cursor,
2136 // don't delete anything if this is the ONLY paragraph!
2137 if (ownerParagraphs().size() == 1)
2140 // Do not delete empty paragraphs with keepempty set.
2141 if (old_cursor.par()->allowEmpty())
2144 // only do our magic if we changed paragraph
2145 if (old_cursor.par() == cursor.par())
2148 // record if we have deleted a paragraph
2149 // we can't possibly have deleted a paragraph before this point
2150 bool deleted = false;
2152 if (old_cursor.par()->empty() ||
2153 (old_cursor.par()->size() == 1 &&
2154 old_cursor.par()->isLineSeparator(0))) {
2155 // ok, we will delete anything
2156 LyXCursor tmpcursor;
2160 bool selection_position_was_oldcursor_position = (
2161 selection.cursor.par() == old_cursor.par()
2162 && selection.cursor.pos() == old_cursor.pos());
2164 if (getRow(old_cursor) != rows().begin()) {
2165 RowList::iterator prevrow = boost::prior(getRow(old_cursor));
2168 cursor = old_cursor; // that undo can restore the right cursor position
2169 #warning FIXME. --end() iterator is usable here
2170 ParagraphList::iterator endpit = boost::next(old_cursor.par());
2171 while (endpit != ownerParagraphs().end() &&
2172 endpit->getDepth()) {
2176 setUndo(bv(), Undo::DELETE, old_cursor.par(),
2177 boost::prior(endpit));
2181 removeRow(getRow(old_cursor));
2183 ownerParagraphs().erase(old_cursor.par());
2185 /* Breakagain the next par. Needed because of
2186 * the parindent that can occur or dissappear.
2187 * The next row can change its height, if
2188 * there is another layout before */
2189 RowList::iterator tmprit = boost::next(prevrow);
2190 if (tmprit != rows().end()) {
2194 setHeightOfRow(prevrow);
2196 RowList::iterator nextrow = boost::next(getRow(old_cursor));
2200 cursor = old_cursor; // that undo can restore the right cursor position
2201 #warning FIXME. --end() iterator is usable here
2202 ParagraphList::iterator endpit = boost::next(old_cursor.par());
2203 while (endpit != ownerParagraphs().end() &&
2204 endpit->getDepth()) {
2208 setUndo(bv(), Undo::DELETE, old_cursor.par(), boost::prior(endpit));
2212 removeRow(getRow(old_cursor));
2214 ownerParagraphs().erase(old_cursor.par());
2216 /* Breakagain the next par. Needed because of
2217 the parindent that can occur or dissappear.
2218 The next row can change its height, if
2219 there is another layout before */
2220 if (nextrow != rows().end()) {
2221 breakAgain(nextrow);
2227 setCursorIntern(cursor.par(), cursor.pos());
2229 if (selection_position_was_oldcursor_position) {
2230 // correct selection
2231 selection.cursor = cursor;
2235 if (old_cursor.par()->stripLeadingSpaces()) {
2236 redoParagraphs(old_cursor, boost::next(old_cursor.par()));
2238 setCursorIntern(cursor.par(), cursor.pos());
2239 selection.cursor = cursor;
2246 ParagraphList & LyXText::ownerParagraphs() const
2249 return inset_owner->paragraphs;
2251 return bv_owner->buffer()->paragraphs;
2255 bool LyXText::needRefresh() const
2257 return need_refresh_;
2261 void LyXText::clearPaint()
2263 need_refresh_ = false;
2267 void LyXText::postPaint()
2269 need_refresh_ = true;
2271 // We are an inset's lyxtext. Tell the top-level lyxtext
2272 // it needs to update the row we're in.
2274 bv()->text->postPaint();
2278 bool LyXText::isInInset() const
2280 // Sub-level has non-null bv owner and
2281 // non-null inset owner.
2282 return inset_owner != 0 && bv_owner != 0;
2286 int defaultRowHeight()
2288 LyXFont const font(LyXFont::ALL_SANE);
2289 return int(font_metrics::maxAscent(font)
2290 + font_metrics::maxDescent(font) * 1.5);