1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
16 #include "paragraph.h"
17 #include "funcrequest.h"
18 #include "frontends/LyXView.h"
19 #include "undo_funcs.h"
21 #include "buffer_funcs.h"
22 #include "bufferparams.h"
23 #include "errorlist.h"
25 #include "BufferView.h"
26 #include "CutAndPaste.h"
27 #include "frontends/Painter.h"
28 #include "frontends/font_metrics.h"
32 #include "FloatList.h"
34 #include "ParagraphParameters.h"
36 #include "lyxrow_funcs.h"
37 #include "paragraph_funcs.h"
39 #include "insets/insetbibitem.h"
40 #include "insets/insetenv.h"
41 #include "insets/insetfloat.h"
42 #include "insets/insetwrap.h"
44 #include "support/LAssert.h"
45 #include "support/textutils.h"
46 #include "support/lstrings.h"
48 #include <boost/tuple/tuple.hpp>
58 LyXText::LyXText(BufferView * bv)
59 : height(0), width(0), anchor_row_offset_(0),
60 inset_owner(0), the_locking_inset(0), bv_owner(bv)
62 anchor_row_ = rows().end();
63 need_break_row = rows().end();
64 refresh_row = rows().end();
70 LyXText::LyXText(BufferView * bv, InsetText * inset)
71 : height(0), width(0), anchor_row_offset_(0),
72 inset_owner(inset), the_locking_inset(0), bv_owner(bv)
74 anchor_row_ = rows().end();
75 need_break_row = rows().end();
76 refresh_row = rows().end();
82 void LyXText::init(BufferView * bview, bool reinit)
86 need_break_row = rows().end();
90 } else if (!rowlist_.empty())
93 ParagraphList::iterator pit = ownerParagraphs().begin();
94 ParagraphList::iterator end = ownerParagraphs().end();
96 current_font = getFont(bview->buffer(), pit, 0);
98 for (; pit != end; ++pit) {
99 insertParagraph(pit, rowlist_.end());
101 setCursorIntern(rowlist_.begin()->par(), 0);
102 selection.cursor = cursor;
108 // Gets the fully instantiated font at a given position in a paragraph
109 // Basically the same routine as Paragraph::getFont() in paragraph.C.
110 // The difference is that this one is used for displaying, and thus we
111 // are allowed to make cosmetic improvements. For instance make footnotes
113 // If position is -1, we get the layout font of the paragraph.
114 // If position is -2, we get the font of the manual label of the paragraph.
115 LyXFont const LyXText::getFont(Buffer const * buf, ParagraphList::iterator pit,
118 lyx::Assert(pos >= 0);
120 LyXLayout_ptr const & layout = pit->layout();
122 // We specialize the 95% common case:
123 if (!pit->getDepth()) {
124 if (layout->labeltype == LABEL_MANUAL
125 && pos < pit->beginningOfBody()) {
127 LyXFont f = pit->getFontSettings(buf->params, pos);
129 pit->inInset()->getDrawFont(f);
130 return f.realize(layout->reslabelfont);
132 LyXFont f = pit->getFontSettings(buf->params, pos);
134 pit->inInset()->getDrawFont(f);
135 return f.realize(layout->resfont);
139 // The uncommon case need not be optimized as much
143 if (pos < pit->beginningOfBody()) {
145 layoutfont = layout->labelfont;
148 layoutfont = layout->font;
151 LyXFont tmpfont = pit->getFontSettings(buf->params, pos);
152 tmpfont.realize(layoutfont);
155 pit->inInset()->getDrawFont(tmpfont);
157 // Realize with the fonts of lesser depth.
158 tmpfont.realize(outerFont(pit, ownerParagraphs()));
160 return realizeFont(tmpfont, buf->params);
164 LyXFont const LyXText::getLayoutFont(Buffer const * buf,
165 ParagraphList::iterator pit) const
167 LyXLayout_ptr const & layout = pit->layout();
169 if (!pit->getDepth()) {
170 return layout->resfont;
173 LyXFont font(layout->font);
174 // Realize with the fonts of lesser depth.
175 font.realize(outerFont(pit, ownerParagraphs()));
177 return realizeFont(font, buf->params);
181 LyXFont const LyXText::getLabelFont(Buffer const * buf,
182 ParagraphList::iterator pit) const
184 LyXLayout_ptr const & layout = pit->layout();
186 if (!pit->getDepth()) {
187 return layout->reslabelfont;
190 LyXFont font(layout->labelfont);
191 // Realize with the fonts of lesser depth.
192 font.realize(outerFont(pit, ownerParagraphs()));
194 return realizeFont(layout->labelfont, buf->params);
198 void LyXText::setCharFont(ParagraphList::iterator pit,
199 pos_type pos, LyXFont const & fnt,
202 Buffer const * buf = bv()->buffer();
203 LyXFont font = getFont(buf, pit, pos);
204 font.update(fnt, buf->params.language, toggleall);
205 // Let the insets convert their font
206 if (pit->isInset(pos)) {
207 Inset * inset = pit->getInset(pos);
208 if (isEditableInset(inset)) {
209 UpdatableInset * uinset =
210 static_cast<UpdatableInset *>(inset);
211 uinset->setFont(bv(), fnt, toggleall, true);
215 // Plug thru to version below:
216 setCharFont(buf, pit, pos, font);
220 void LyXText::setCharFont(Buffer const * buf, ParagraphList::iterator pit,
221 pos_type pos, LyXFont const & fnt)
225 LyXTextClass const & tclass = buf->params.getLyXTextClass();
226 LyXLayout_ptr const & layout = pit->layout();
228 // Get concrete layout font to reduce against
231 if (pos < pit->beginningOfBody())
232 layoutfont = layout->labelfont;
234 layoutfont = layout->font;
236 // Realize against environment font information
237 if (pit->getDepth()) {
238 ParagraphList::iterator tp = pit;
239 while (!layoutfont.resolved() &&
240 tp != ownerParagraphs().end() &&
242 tp = outerHook(tp, ownerParagraphs());
243 if (tp != ownerParagraphs().end())
244 layoutfont.realize(tp->layout()->font);
248 layoutfont.realize(tclass.defaultfont());
250 // Now, reduce font against full layout font
251 font.reduce(layoutfont);
253 pit->setFont(pos, font);
257 // removes the row and reset the touched counters
258 void LyXText::removeRow(RowList::iterator rit)
260 /* FIXME: when we cache the bview, this should just
261 * become a postPaint(), I think */
262 if (refresh_row == rit) {
263 if (rit == rows().begin())
264 refresh_row = boost::next(rit);
266 refresh_row = boost::prior(rit);
268 // what about refresh_y
271 if (anchor_row_ == rit) {
272 if (rit != rows().begin()) {
273 anchor_row_ = boost::prior(rit);
274 anchor_row_offset_ += anchor_row_->height();
276 anchor_row_ = boost::next(rit);
277 anchor_row_offset_ -= rit->height();
281 // the text becomes smaller
282 height -= rit->height();
288 // remove all following rows of the paragraph of the specified row.
289 void LyXText::removeParagraph(RowList::iterator rit)
291 ParagraphList::iterator tmppit = rit->par();
294 while (rit != rows().end() && rit->par() == tmppit) {
295 RowList::iterator tmprit = boost::next(rit);
302 void LyXText::insertParagraph(ParagraphList::iterator pit,
303 RowList::iterator rowit)
305 // insert a new row, starting at position 0
307 RowList::iterator rit = rowlist_.insert(rowit, newrow);
309 // and now append the whole paragraph before the new row
310 appendParagraph(rit);
314 Inset * LyXText::getInset() const
316 ParagraphList::iterator pit = cursor.par();
317 pos_type const pos = cursor.pos();
319 if (pos < pit->size() && pit->isInset(pos)) {
320 return pit->getInset(pos);
326 void LyXText::toggleInset()
328 Inset * inset = getInset();
329 // is there an editable inset at cursor position?
330 if (!isEditableInset(inset)) {
331 // No, try to see if we are inside a collapsable inset
332 if (inset_owner && inset_owner->owner()
333 && inset_owner->owner()->isOpen()) {
334 bv()->unlockInset(inset_owner->owner());
335 inset_owner->owner()->close(bv());
336 bv()->getLyXText()->cursorRight(bv());
340 //bv()->owner()->message(inset->editMessage());
342 // do we want to keep this?? (JMarc)
343 if (!isHighlyEditableInset(inset))
344 setCursorParUndo(bv());
346 if (inset->isOpen()) {
352 bv()->updateInset(inset);
356 /* used in setlayout */
357 // Asger is not sure we want to do this...
358 void LyXText::makeFontEntriesLayoutSpecific(Buffer const & buf,
361 LyXLayout_ptr const & layout = par.layout();
362 pos_type const psize = par.size();
365 for (pos_type pos = 0; pos < psize; ++pos) {
366 if (pos < par.beginningOfBody())
367 layoutfont = layout->labelfont;
369 layoutfont = layout->font;
371 LyXFont tmpfont = par.getFontSettings(buf.params, pos);
372 tmpfont.reduce(layoutfont);
373 par.setFont(pos, tmpfont);
378 ParagraphList::iterator
379 LyXText::setLayout(LyXCursor & cur, LyXCursor & sstart_cur,
380 LyXCursor & send_cur,
381 string const & layout)
383 ParagraphList::iterator endpit = boost::next(send_cur.par());
384 ParagraphList::iterator undoendpit = endpit;
385 ParagraphList::iterator pars_end = ownerParagraphs().end();
387 if (endpit != pars_end && endpit->getDepth()) {
388 while (endpit != pars_end && endpit->getDepth()) {
392 } else if (endpit != pars_end) {
393 // because of parindents etc.
397 setUndo(bv(), Undo::EDIT, sstart_cur.par(), boost::prior(undoendpit));
399 // ok we have a selection. This is always between sstart_cur
400 // and sel_end cursor
402 ParagraphList::iterator pit = sstart_cur.par();
403 ParagraphList::iterator epit = boost::next(send_cur.par());
405 LyXLayout_ptr const & lyxlayout =
406 bv()->buffer()->params.getLyXTextClass()[layout];
409 pit->applyLayout(lyxlayout);
410 makeFontEntriesLayoutSpecific(*bv()->buffer(), *pit);
411 ParagraphList::iterator fppit = pit;
412 fppit->params().spaceTop(lyxlayout->fill_top ?
413 VSpace(VSpace::VFILL)
414 : VSpace(VSpace::NONE));
415 fppit->params().spaceBottom(lyxlayout->fill_bottom ?
416 VSpace(VSpace::VFILL)
417 : VSpace(VSpace::NONE));
418 if (lyxlayout->margintype == MARGIN_MANUAL)
419 pit->setLabelWidthString(lyxlayout->labelstring());
422 } while (pit != epit);
428 // set layout over selection and make a total rebreak of those paragraphs
429 void LyXText::setLayout(string const & layout)
431 LyXCursor tmpcursor = cursor; // store the current cursor
433 // if there is no selection just set the layout
434 // of the current paragraph
435 if (!selection.set()) {
436 selection.start = cursor; // dummy selection
437 selection.end = cursor;
440 // special handling of new environment insets
441 BufferParams const & params = bv()->buffer()->params;
442 LyXLayout_ptr const & lyxlayout = params.getLyXTextClass()[layout];
443 if (lyxlayout->is_environment) {
444 // move everything in a new environment inset
445 lyxerr << "setting layout " << layout << endl;
446 bv()->owner()->dispatch(FuncRequest(LFUN_HOME));
447 bv()->owner()->dispatch(FuncRequest(LFUN_ENDSEL));
448 bv()->owner()->dispatch(FuncRequest(LFUN_CUT));
449 Inset * inset = new InsetEnvironment(params, layout);
450 if (bv()->insertInset(inset)) {
452 //bv()->owner()->dispatch(FuncRequest(LFUN_PASTE));
459 ParagraphList::iterator endpit = setLayout(cursor, selection.start,
460 selection.end, layout);
461 redoParagraphs(selection.start, endpit);
463 // we have to reset the selection, because the
464 // geometry could have changed
465 setCursor(selection.start.par(), selection.start.pos(), false);
466 selection.cursor = cursor;
467 setCursor(selection.end.par(), selection.end.pos(), false);
471 setCursor(tmpcursor.par(), tmpcursor.pos(), true);
475 bool LyXText::changeDepth(bv_funcs::DEPTH_CHANGE type, bool test_only)
477 ParagraphList::iterator pit(cursor.par());
478 ParagraphList::iterator end(cursor.par());
479 ParagraphList::iterator start = pit;
481 if (selection.set()) {
482 pit = selection.start.par();
483 end = selection.end.par();
487 ParagraphList::iterator pastend = boost::next(end);
490 setUndo(bv(), Undo::EDIT, start, end);
492 bool changed = false;
494 int prev_after_depth = 0;
495 #warning parlist ... could be nicer ?
496 if (start != ownerParagraphs().begin()) {
497 prev_after_depth = boost::prior(start)->getMaxDepthAfter();
501 int const depth = pit->params().depth();
502 if (type == bv_funcs::INC_DEPTH) {
503 if (depth < prev_after_depth
504 && pit->layout()->labeltype != LABEL_BIBLIO) {
507 pit->params().depth(depth + 1);
514 pit->params().depth(depth - 1);
517 prev_after_depth = pit->getMaxDepthAfter();
529 // Wow, redoParagraphs is stupid.
531 setCursor(tmpcursor, start, 0);
533 //redoParagraphs(tmpcursor, &(*pastend));
534 redoParagraphs(tmpcursor, pastend);
536 // We need to actually move the text->cursor. I don't
537 // understand why ...
540 // we have to reset the visual selection because the
541 // geometry could have changed
542 if (selection.set()) {
543 setCursor(selection.start.par(), selection.start.pos());
544 selection.cursor = cursor;
545 setCursor(selection.end.par(), selection.end.pos());
548 // this handles the counter labels, and also fixes up
549 // depth values for follow-on (child) paragraphs
553 setCursor(tmpcursor.par(), tmpcursor.pos());
559 // set font over selection and make a total rebreak of those paragraphs
560 void LyXText::setFont(LyXFont const & font, bool toggleall)
562 // if there is no selection just set the current_font
563 if (!selection.set()) {
564 // Determine basis font
566 if (cursor.pos() < cursor.par()->beginningOfBody()) {
567 layoutfont = getLabelFont(bv()->buffer(),
570 layoutfont = getLayoutFont(bv()->buffer(),
573 // Update current font
574 real_current_font.update(font,
575 bv()->buffer()->params.language,
578 // Reduce to implicit settings
579 current_font = real_current_font;
580 current_font.reduce(layoutfont);
581 // And resolve it completely
582 real_current_font.realize(layoutfont);
587 LyXCursor tmpcursor = cursor; // store the current cursor
589 // ok we have a selection. This is always between sel_start_cursor
590 // and sel_end cursor
592 setUndo(bv(), Undo::EDIT, selection.start.par(), selection.end.par());
594 cursor = selection.start;
595 while (cursor.par() != selection.end.par() ||
596 cursor.pos() < selection.end.pos())
598 if (cursor.pos() < cursor.par()->size()) {
599 // an open footnote should behave like a closed one
600 setCharFont(cursor.par(), cursor.pos(),
602 cursor.pos(cursor.pos() + 1);
605 cursor.par(boost::next(cursor.par()));
610 redoParagraphs(selection.start, boost::next(selection.end.par()));
612 // we have to reset the selection, because the
613 // geometry could have changed, but we keep
614 // it for user convenience
615 setCursor(selection.start.par(), selection.start.pos());
616 selection.cursor = cursor;
617 setCursor(selection.end.par(), selection.end.pos());
619 setCursor(tmpcursor.par(), tmpcursor.pos(), true,
620 tmpcursor.boundary());
624 void LyXText::redoHeightOfParagraph()
626 RowList::iterator tmprow = cursor.row();
627 int y = cursor.y() - tmprow->baseline();
629 setHeightOfRow(tmprow);
631 while (tmprow != rows().begin()
632 && boost::prior(tmprow)->par() == tmprow->par()) {
634 y -= tmprow->height();
635 setHeightOfRow(tmprow);
640 setCursor(cursor.par(), cursor.pos(), false, cursor.boundary());
644 void LyXText::redoDrawingOfParagraph(LyXCursor const & cur)
646 RowList::iterator tmprow = cur.row();
648 int y = cur.y() - tmprow->baseline();
649 setHeightOfRow(tmprow);
651 while (tmprow != rows().begin()
652 && boost::prior(tmprow)->par() == tmprow->par()) {
654 y -= tmprow->height();
658 setCursor(cur.par(), cur.pos());
662 // deletes and inserts again all paragraphs between the cursor
663 // and the specified par
664 // This function is needed after SetLayout and SetFont etc.
665 void LyXText::redoParagraphs(LyXCursor const & cur,
666 ParagraphList::iterator endpit)
668 RowList::iterator tmprit = cur.row();
669 int y = cur.y() - tmprit->baseline();
671 ParagraphList::iterator first_phys_pit;
672 RowList::iterator prevrit;
673 if (tmprit == rows().begin()) {
674 // A trick/hack for UNDO.
675 // This is needed because in an UNDO/REDO we could have
676 // changed the ownerParagraph() so the paragraph inside
677 // the row is NOT my really first par anymore.
678 // Got it Lars ;) (Jug 20011206)
679 first_phys_pit = ownerParagraphs().begin();
680 prevrit = rows().end();
682 first_phys_pit = tmprit->par();
683 while (tmprit != rows().begin()
684 && boost::prior(tmprit)->par() == first_phys_pit)
687 y -= tmprit->height();
689 prevrit = boost::prior(tmprit);
693 while (tmprit != rows().end() && tmprit->par() != endpit) {
694 RowList::iterator tmprit2 = tmprit++;
698 // Reinsert the paragraphs.
699 ParagraphList::iterator tmppit = first_phys_pit;
701 while (tmppit != ownerParagraphs().end()) {
702 insertParagraph(tmppit, tmprit);
703 while (tmprit != rows().end()
704 && tmprit->par() == tmppit) {
708 if (tmppit == endpit)
711 if (prevrit != rows().end()) {
712 setHeightOfRow(prevrit);
713 postPaint(y - prevrit->height());
715 setHeightOfRow(rows().begin());
718 if (tmprit != rows().end())
719 setHeightOfRow(tmprit);
725 void LyXText::fullRebreak()
727 if (rows().empty()) {
732 RowList::iterator rows_end = rows().end();
734 if (need_break_row != rows_end) {
735 breakAgain(need_break_row);
736 need_break_row = rows_end;
742 // important for the screen
745 // the cursor set functions have a special mechanism. When they
746 // realize, that you left an empty paragraph, they will delete it.
747 // They also delete the corresponding row
749 // need the selection cursor:
750 void LyXText::setSelection()
752 bool const lsel = selection.set();
754 if (!selection.set()) {
755 last_sel_cursor = selection.cursor;
756 selection.start = selection.cursor;
757 selection.end = selection.cursor;
762 // first the toggling area
763 if (cursor.y() < last_sel_cursor.y()
764 || (cursor.y() == last_sel_cursor.y()
765 && cursor.x() < last_sel_cursor.x())) {
766 toggle_end_cursor = last_sel_cursor;
767 toggle_cursor = cursor;
769 toggle_end_cursor = cursor;
770 toggle_cursor = last_sel_cursor;
773 last_sel_cursor = cursor;
775 // and now the whole selection
777 if (selection.cursor.par() == cursor.par())
778 if (selection.cursor.pos() < cursor.pos()) {
779 selection.end = cursor;
780 selection.start = selection.cursor;
782 selection.end = selection.cursor;
783 selection.start = cursor;
785 else if (selection.cursor.y() < cursor.y() ||
786 (selection.cursor.y() == cursor.y()
787 && selection.cursor.x() < cursor.x())) {
788 selection.end = cursor;
789 selection.start = selection.cursor;
792 selection.end = selection.cursor;
793 selection.start = cursor;
796 // a selection with no contents is not a selection
797 if (selection.start.par() == selection.end.par() &&
798 selection.start.pos() == selection.end.pos())
799 selection.set(false);
801 if (inset_owner && (selection.set() || lsel))
802 inset_owner->setUpdateStatus(bv(), InsetText::SELECTION);
806 string const LyXText::selectionAsString(Buffer const * buffer,
809 if (!selection.set()) return string();
811 // should be const ...
812 ParagraphList::iterator startpit = selection.start.par();
813 ParagraphList::iterator endpit = selection.end.par();
814 pos_type const startpos(selection.start.pos());
815 pos_type const endpos(selection.end.pos());
817 if (startpit == endpit) {
818 return startpit->asString(buffer, startpos, endpos, label);
823 // First paragraph in selection
824 result += startpit->asString(buffer, startpos, startpit->size(), label) + "\n\n";
826 // The paragraphs in between (if any)
827 ParagraphList::iterator pit = boost::next(startpit);
828 for (; pit != endpit; ++pit) {
829 result += pit->asString(buffer, 0, pit->size(), label) + "\n\n";
832 // Last paragraph in selection
833 result += endpit->asString(buffer, 0, endpos, label);
839 void LyXText::clearSelection()
841 selection.set(false);
842 selection.mark(false);
843 last_sel_cursor = selection.end = selection.start = selection.cursor = cursor;
844 // reset this in the bv_owner!
845 if (bv_owner && bv_owner->text)
846 bv_owner->text->xsel_cache.set(false);
850 void LyXText::cursorHome()
852 setCursor(cursor.par(), cursor.row()->pos());
856 void LyXText::cursorEnd()
858 if (cursor.par()->empty())
861 RowList::iterator rit = cursor.row();
862 RowList::iterator next_rit = boost::next(rit);
863 ParagraphList::iterator pit = rit->par();
864 pos_type last_pos = lastPos(*this, rit);
866 if (next_rit == rows().end() || next_rit->par() != pit) {
870 (pit->getChar(last_pos) != ' ' && !pit->isNewline(last_pos))) {
875 setCursor(pit, last_pos);
879 void LyXText::cursorTop()
881 setCursor(ownerParagraphs().begin(), 0);
885 void LyXText::cursorBottom()
887 ParagraphList::iterator lastpit =
888 boost::prior(ownerParagraphs().end());
889 setCursor(lastpit, lastpit->size());
893 void LyXText::toggleFree(LyXFont const & font, bool toggleall)
895 // If the mask is completely neutral, tell user
896 if (font == LyXFont(LyXFont::ALL_IGNORE)) {
897 // Could only happen with user style
898 bv()->owner()->message(_("No font change defined. Use Character under the Layout menu to define font change."));
902 // Try implicit word selection
903 // If there is a change in the language the implicit word selection
905 LyXCursor resetCursor = cursor;
906 bool implicitSelection = (font.language() == ignore_language
907 && font.number() == LyXFont::IGNORE)
908 ? selectWordWhenUnderCursor(WHOLE_WORD_STRICT) : false;
911 setFont(font, toggleall);
913 // Implicit selections are cleared afterwards
914 //and cursor is set to the original position.
915 if (implicitSelection) {
917 cursor = resetCursor;
918 setCursor(cursor.par(), cursor.pos());
919 selection.cursor = cursor;
922 inset_owner->setUpdateStatus(bv(), InsetText::CURSOR_PAR);
926 string LyXText::getStringToIndex()
928 // Try implicit word selection
929 // If there is a change in the language the implicit word selection
931 LyXCursor const reset_cursor = cursor;
932 bool const implicitSelection = selectWordWhenUnderCursor(PREVIOUS_WORD);
935 if (!selection.set())
936 bv()->owner()->message(_("Nothing to index!"));
937 else if (selection.start.par() != selection.end.par())
938 bv()->owner()->message(_("Cannot index more than one paragraph!"));
940 idxstring = selectionAsString(bv()->buffer(), false);
942 // Reset cursors to their original position.
943 cursor = reset_cursor;
944 setCursor(cursor.par(), cursor.pos());
945 selection.cursor = cursor;
947 // Clear the implicit selection.
948 if (implicitSelection)
955 // the DTP switches for paragraphs. LyX will store them in the first
956 // physicla paragraph. When a paragraph is broken, the top settings rest,
957 // the bottom settings are given to the new one. So I can make shure,
958 // they do not duplicate themself and you cannnot make dirty things with
961 void LyXText::setParagraph(bool line_top, bool line_bottom,
962 bool pagebreak_top, bool pagebreak_bottom,
963 VSpace const & space_top,
964 VSpace const & space_bottom,
965 Spacing const & spacing,
967 string const & labelwidthstring,
970 LyXCursor tmpcursor = cursor;
971 if (!selection.set()) {
972 selection.start = cursor;
973 selection.end = cursor;
976 // make sure that the depth behind the selection are restored, too
977 ParagraphList::iterator endpit = boost::next(selection.end.par());
978 ParagraphList::iterator undoendpit = endpit;
979 ParagraphList::iterator pars_end = ownerParagraphs().end();
981 if (endpit != pars_end && endpit->getDepth()) {
982 while (endpit != pars_end && endpit->getDepth()) {
986 } else if (endpit != pars_end) {
987 // because of parindents etc.
991 setUndo(bv(), Undo::EDIT, selection.start.par(),
992 boost::prior(undoendpit));
995 ParagraphList::iterator tmppit = selection.end.par();
997 while (tmppit != boost::prior(selection.start.par())) {
998 setCursor(tmppit, 0);
999 postPaint(cursor.y() - cursor.row()->baseline());
1001 ParagraphList::iterator pit = cursor.par();
1002 ParagraphParameters & params = pit->params();
1004 params.lineTop(line_top);
1005 params.lineBottom(line_bottom);
1006 params.pagebreakTop(pagebreak_top);
1007 params.pagebreakBottom(pagebreak_bottom);
1008 params.spaceTop(space_top);
1009 params.spaceBottom(space_bottom);
1010 params.spacing(spacing);
1011 // does the layout allow the new alignment?
1012 LyXLayout_ptr const & layout = pit->layout();
1014 if (align == LYX_ALIGN_LAYOUT)
1015 align = layout->align;
1016 if (align & layout->alignpossible) {
1017 if (align == layout->align)
1018 params.align(LYX_ALIGN_LAYOUT);
1020 params.align(align);
1022 pit->setLabelWidthString(labelwidthstring);
1023 params.noindent(noindent);
1024 tmppit = boost::prior(pit);
1027 redoParagraphs(selection.start, endpit);
1030 setCursor(selection.start.par(), selection.start.pos());
1031 selection.cursor = cursor;
1032 setCursor(selection.end.par(), selection.end.pos());
1034 setCursor(tmpcursor.par(), tmpcursor.pos());
1036 bv()->updateInset(inset_owner);
1040 // set the counter of a paragraph. This includes the labels
1041 void LyXText::setCounter(Buffer const * buf, ParagraphList::iterator pit)
1043 LyXTextClass const & textclass = buf->params.getLyXTextClass();
1044 LyXLayout_ptr const & layout = pit->layout();
1046 if (pit != ownerParagraphs().begin()) {
1048 pit->params().appendix(boost::prior(pit)->params().appendix());
1049 if (!pit->params().appendix() &&
1050 pit->params().startOfAppendix()) {
1051 pit->params().appendix(true);
1052 textclass.counters().reset();
1054 pit->enumdepth = boost::prior(pit)->enumdepth;
1055 pit->itemdepth = boost::prior(pit)->itemdepth;
1057 pit->params().appendix(pit->params().startOfAppendix());
1062 /* Maybe we have to increment the enumeration depth.
1063 * BUT, enumeration in a footnote is considered in isolation from its
1064 * surrounding paragraph so don't increment if this is the
1065 * first line of the footnote
1066 * AND, bibliographies can't have their depth changed ie. they
1067 * are always of depth 0
1069 if (pit != ownerParagraphs().begin()
1070 && boost::prior(pit)->getDepth() < pit->getDepth()
1071 && boost::prior(pit)->layout()->labeltype == LABEL_COUNTER_ENUMI
1072 && pit->enumdepth < 3
1073 && layout->labeltype != LABEL_BIBLIO) {
1077 // Maybe we have to decrement the enumeration depth, see note above
1078 if (pit != ownerParagraphs().begin()
1079 && boost::prior(pit)->getDepth() > pit->getDepth()
1080 && layout->labeltype != LABEL_BIBLIO) {
1081 pit->enumdepth = depthHook(pit, ownerParagraphs(),
1082 pit->getDepth())->enumdepth;
1085 if (!pit->params().labelString().empty()) {
1086 pit->params().labelString(string());
1089 if (layout->margintype == MARGIN_MANUAL) {
1090 if (pit->params().labelWidthString().empty()) {
1091 pit->setLabelWidthString(layout->labelstring());
1094 pit->setLabelWidthString(string());
1097 // is it a layout that has an automatic label?
1098 if (layout->labeltype >= LABEL_COUNTER_CHAPTER) {
1099 int const i = layout->labeltype - LABEL_COUNTER_CHAPTER;
1103 if (i >= 0 && i <= buf->params.secnumdepth) {
1107 textclass.counters().step(layout->latexname());
1109 // Is there a label? Useful for Chapter layout
1110 if (!pit->params().appendix()) {
1111 s << buf->B_(layout->labelstring());
1113 s << buf->B_(layout->labelstring_appendix());
1116 // Use of an integer is here less than elegant. For now.
1117 int head = textclass.maxcounter() - LABEL_COUNTER_CHAPTER;
1118 if (!pit->params().appendix()) {
1119 numbertype = "sectioning";
1121 numbertype = "appendix";
1122 if (pit->isRightToLeftPar(buf->params))
1123 langtype = "hebrew";
1129 << textclass.counters()
1130 .numberLabel(layout->latexname(),
1131 numbertype, langtype, head);
1133 pit->params().labelString(STRCONV(s.str()));
1135 // reset enum counters
1136 textclass.counters().reset("enum");
1137 } else if (layout->labeltype < LABEL_COUNTER_ENUMI) {
1138 textclass.counters().reset("enum");
1139 } else if (layout->labeltype == LABEL_COUNTER_ENUMI) {
1141 // Yes I know this is a really, really! bad solution
1143 string enumcounter("enum");
1145 switch (pit->enumdepth) {
1154 enumcounter += "iv";
1157 // not a valid enumdepth...
1161 textclass.counters().step(enumcounter);
1163 s << textclass.counters()
1164 .numberLabel(enumcounter, "enumeration");
1165 pit->params().labelString(STRCONV(s.str()));
1167 } else if (layout->labeltype == LABEL_BIBLIO) {// ale970302
1168 textclass.counters().step("bibitem");
1169 int number = textclass.counters().value("bibitem");
1170 if (pit->bibitem()) {
1171 pit->bibitem()->setCounter(number);
1172 pit->params().labelString(layout->labelstring());
1174 // In biblio should't be following counters but...
1176 string s = buf->B_(layout->labelstring());
1178 // the caption hack:
1179 if (layout->labeltype == LABEL_SENSITIVE) {
1180 ParagraphList::iterator tmppit = pit;
1183 while (tmppit != ownerParagraphs().end() &&
1185 // the single '=' is intended below
1186 && (in = tmppit->inInset()->owner())) {
1187 if (in->lyxCode() == Inset::FLOAT_CODE ||
1188 in->lyxCode() == Inset::WRAP_CODE) {
1192 tmppit = std::find(ownerParagraphs().begin(), ownerParagraphs().end(), *in->parOwner());
1199 if (in->lyxCode() == Inset::FLOAT_CODE)
1200 type = static_cast<InsetFloat*>(in)->params().type;
1201 else if (in->lyxCode() == Inset::WRAP_CODE)
1202 type = static_cast<InsetWrap*>(in)->params().type;
1206 Floating const & fl = textclass.floats().getType(type);
1208 textclass.counters().step(fl.type());
1210 // Doesn't work... yet.
1211 s = bformat(_("%1$s #:"), buf->B_(fl.name()));
1213 // par->SetLayout(0);
1214 // s = layout->labelstring;
1215 s = _("Senseless: ");
1218 pit->params().labelString(s);
1220 // reset the enumeration counter. They are always reset
1221 // when there is any other layout between
1222 // Just fall-through between the cases so that all
1223 // enum counters deeper than enumdepth is also reset.
1224 switch (pit->enumdepth) {
1226 textclass.counters().reset("enumi");
1228 textclass.counters().reset("enumii");
1230 textclass.counters().reset("enumiii");
1232 textclass.counters().reset("enumiv");
1238 // Updates all counters. Paragraphs with changed label string will be rebroken
1239 void LyXText::updateCounters()
1241 RowList::iterator rowit = rows().begin();
1242 ParagraphList::iterator pit = rowit->par();
1244 // CHECK if this is really needed. (Lgb)
1245 bv()->buffer()->params.getLyXTextClass().counters().reset();
1247 ParagraphList::iterator beg = ownerParagraphs().begin();
1248 ParagraphList::iterator end = ownerParagraphs().end();
1249 for (; pit != end; ++pit) {
1250 while (rowit->par() != pit)
1253 string const oldLabel = pit->params().labelString();
1255 size_t maxdepth = 0;
1257 maxdepth = boost::prior(pit)->getMaxDepthAfter();
1259 if (pit->params().depth() > maxdepth)
1260 pit->params().depth(maxdepth);
1262 // setCounter can potentially change the labelString.
1263 setCounter(bv()->buffer(), pit);
1265 string const & newLabel = pit->params().labelString();
1267 if (oldLabel.empty() && !newLabel.empty()) {
1268 removeParagraph(rowit);
1269 appendParagraph(rowit);
1275 void LyXText::insertInset(Inset * inset)
1277 if (!cursor.par()->insetAllowed(inset->lyxCode()))
1279 setUndo(bv(), Undo::FINISH, cursor.par());
1281 cursor.par()->insertInset(cursor.pos(), inset);
1282 // Just to rebreak and refresh correctly.
1283 // The character will not be inserted a second time
1284 insertChar(Paragraph::META_INSET);
1285 // If we enter a highly editable inset the cursor should be to before
1286 // the inset. This couldn't happen before as Undo was not handled inside
1287 // inset now after the Undo LyX tries to call inset->Edit(...) again
1288 // and cannot do this as the cursor is behind the inset and GetInset
1289 // does not return the inset!
1290 if (isHighlyEditableInset(inset)) {
1297 void LyXText::cutSelection(bool doclear, bool realcut)
1299 // Stuff what we got on the clipboard. Even if there is no selection.
1301 // There is a problem with having the stuffing here in that the
1302 // larger the selection the slower LyX will get. This can be
1303 // solved by running the line below only when the selection has
1304 // finished. The solution used currently just works, to make it
1305 // faster we need to be more clever and probably also have more
1306 // calls to stuffClipboard. (Lgb)
1307 bv()->stuffClipboard(selectionAsString(bv()->buffer(), true));
1309 // This doesn't make sense, if there is no selection
1310 if (!selection.set())
1313 // OK, we have a selection. This is always between selection.start
1314 // and selection.end
1316 // make sure that the depth behind the selection are restored, too
1317 ParagraphList::iterator endpit = boost::next(selection.end.par());
1318 ParagraphList::iterator undoendpit = endpit;
1319 ParagraphList::iterator pars_end = ownerParagraphs().end();
1321 if (endpit != pars_end && endpit->getDepth()) {
1322 while (endpit != pars_end && endpit->getDepth()) {
1324 undoendpit = endpit;
1326 } else if (endpit != pars_end) {
1327 // because of parindents etc.
1331 setUndo(bv(), Undo::DELETE, selection.start.par(),
1332 boost::prior(undoendpit));
1335 endpit = selection.end.par();
1336 int endpos = selection.end.pos();
1338 boost::tie(endpit, endpos) = realcut ?
1339 CutAndPaste::cutSelection(bv()->buffer()->params,
1341 selection.start.par(), endpit,
1342 selection.start.pos(), endpos,
1343 bv()->buffer()->params.textclass,
1345 : CutAndPaste::eraseSelection(bv()->buffer()->params,
1347 selection.start.par(), endpit,
1348 selection.start.pos(), endpos,
1350 // sometimes necessary
1352 selection.start.par()->stripLeadingSpaces();
1354 redoParagraphs(selection.start, boost::next(endpit));
1355 #warning FIXME latent bug
1356 // endpit will be invalidated on redoParagraphs once ParagraphList
1357 // becomes a std::list? There are maybe other places on which this
1358 // can happend? (Ab)
1359 // cutSelection can invalidate the cursor so we need to set
1361 // we prefer the end for when tracking changes
1365 // need a valid cursor. (Lgb)
1368 setCursor(cursor.par(), cursor.pos());
1369 selection.cursor = cursor;
1374 void LyXText::copySelection()
1376 // stuff the selection onto the X clipboard, from an explicit copy request
1377 bv()->stuffClipboard(selectionAsString(bv()->buffer(), true));
1379 // this doesnt make sense, if there is no selection
1380 if (!selection.set())
1383 // ok we have a selection. This is always between selection.start
1384 // and sel_end cursor
1386 // copy behind a space if there is one
1387 while (selection.start.par()->size() > selection.start.pos()
1388 && selection.start.par()->isLineSeparator(selection.start.pos())
1389 && (selection.start.par() != selection.end.par()
1390 || selection.start.pos() < selection.end.pos()))
1391 selection.start.pos(selection.start.pos() + 1);
1393 CutAndPaste::copySelection(selection.start.par(),
1394 selection.end.par(),
1395 selection.start.pos(), selection.end.pos(),
1396 bv()->buffer()->params.textclass);
1400 void LyXText::pasteSelection(size_t sel_index)
1402 // this does not make sense, if there is nothing to paste
1403 if (!CutAndPaste::checkPastePossible())
1406 setUndo(bv(), Undo::INSERT, cursor.par());
1408 ParagraphList::iterator endpit;
1413 boost::tie(ppp, endpit) =
1414 CutAndPaste::pasteSelection(*bv()->buffer(),
1416 cursor.par(), cursor.pos(),
1417 bv()->buffer()->params.textclass,
1419 parseErrors(*bv()->buffer(), el);
1420 bv()->showErrorList(_("Paste"));
1422 redoParagraphs(cursor, endpit);
1424 setCursor(cursor.par(), cursor.pos());
1427 selection.cursor = cursor;
1428 setCursor(ppp.first, ppp.second);
1434 void LyXText::setSelectionRange(lyx::pos_type length)
1439 selection.cursor = cursor;
1446 // simple replacing. The font of the first selected character is used
1447 void LyXText::replaceSelectionWithString(string const & str)
1449 setCursorParUndo(bv());
1452 if (!selection.set()) { // create a dummy selection
1453 selection.end = cursor;
1454 selection.start = cursor;
1457 // Get font setting before we cut
1458 pos_type pos = selection.end.pos();
1459 LyXFont const font = selection.start.par()
1460 ->getFontSettings(bv()->buffer()->params,
1461 selection.start.pos());
1463 // Insert the new string
1464 string::const_iterator cit = str.begin();
1465 string::const_iterator end = str.end();
1466 for (; cit != end; ++cit) {
1467 selection.end.par()->insertChar(pos, (*cit), font);
1471 // Cut the selection
1472 cutSelection(true, false);
1478 // needed to insert the selection
1479 void LyXText::insertStringAsLines(string const & str)
1481 ParagraphList::iterator pit = cursor.par();
1482 pos_type pos = cursor.pos();
1483 ParagraphList::iterator endpit = boost::next(cursor.par());
1485 setCursorParUndo(bv());
1487 // only to be sure, should not be neccessary
1490 bv()->buffer()->insertStringAsLines(pit, pos, current_font, str);
1492 redoParagraphs(cursor, endpit);
1493 setCursor(cursor.par(), cursor.pos());
1494 selection.cursor = cursor;
1495 setCursor(pit, pos);
1500 // turns double-CR to single CR, others where converted into one
1501 // blank. Then InsertStringAsLines is called
1502 void LyXText::insertStringAsParagraphs(string const & str)
1504 string linestr(str);
1505 bool newline_inserted = false;
1506 string::size_type const siz = linestr.length();
1508 for (string::size_type i = 0; i < siz; ++i) {
1509 if (linestr[i] == '\n') {
1510 if (newline_inserted) {
1511 // we know that \r will be ignored by
1512 // InsertStringA. Of course, it is a dirty
1513 // trick, but it works...
1514 linestr[i - 1] = '\r';
1518 newline_inserted = true;
1520 } else if (IsPrintable(linestr[i])) {
1521 newline_inserted = false;
1524 insertStringAsLines(linestr);
1528 void LyXText::checkParagraph(ParagraphList::iterator pit, pos_type pos)
1530 LyXCursor tmpcursor;
1534 RowList::iterator row = getRow(pit, pos, y);
1535 RowList::iterator beg = rows().begin();
1537 // is there a break one row above
1539 && boost::prior(row)->par() == row->par()) {
1540 z = rowBreakPoint(*boost::prior(row));
1541 if (z >= row->pos()) {
1542 // set the dimensions of the row above
1543 y -= boost::prior(row)->height();
1546 breakAgain(boost::prior(row));
1548 // set the cursor again. Otherwise
1549 // dangling pointers are possible
1550 setCursor(cursor.par(), cursor.pos(),
1551 false, cursor.boundary());
1552 selection.cursor = cursor;
1557 int const tmpheight = row->height();
1558 pos_type const tmplast = lastPos(*this, row);
1561 if (row->height() == tmpheight && lastPos(*this, row) == tmplast) {
1562 postRowPaint(row, y);
1567 // check the special right address boxes
1568 if (pit->layout()->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1575 redoDrawingOfParagraph(tmpcursor);
1578 // set the cursor again. Otherwise dangling pointers are possible
1579 // also set the selection
1581 if (selection.set()) {
1583 setCursorIntern(selection.cursor.par(), selection.cursor.pos(),
1584 false, selection.cursor.boundary());
1585 selection.cursor = cursor;
1586 setCursorIntern(selection.start.par(),
1587 selection.start.pos(),
1588 false, selection.start.boundary());
1589 selection.start = cursor;
1590 setCursorIntern(selection.end.par(),
1591 selection.end.pos(),
1592 false, selection.end.boundary());
1593 selection.end = cursor;
1594 setCursorIntern(last_sel_cursor.par(),
1595 last_sel_cursor.pos(),
1596 false, last_sel_cursor.boundary());
1597 last_sel_cursor = cursor;
1600 setCursorIntern(cursor.par(), cursor.pos(),
1601 false, cursor.boundary());
1605 // returns false if inset wasn't found
1606 bool LyXText::updateInset(Inset * inset)
1608 // first check the current paragraph
1609 int pos = cursor.par()->getPositionOfInset(inset);
1611 checkParagraph(cursor.par(), pos);
1615 // check every paragraph
1617 ParagraphList::iterator par = ownerParagraphs().begin();
1618 ParagraphList::iterator end = ownerParagraphs().end();
1619 for (; par != end; ++par) {
1620 pos = par->getPositionOfInset(inset);
1622 checkParagraph(par, pos);
1631 bool LyXText::setCursor(ParagraphList::iterator pit,
1633 bool setfont, bool boundary)
1635 LyXCursor old_cursor = cursor;
1636 setCursorIntern(pit, pos, setfont, boundary);
1637 return deleteEmptyParagraphMechanism(old_cursor);
1641 void LyXText::setCursor(LyXCursor & cur, ParagraphList::iterator pit,
1642 pos_type pos, bool boundary)
1644 lyx::Assert(pit != ownerParagraphs().end());
1648 cur.boundary(boundary);
1650 // get the cursor y position in text
1652 RowList::iterator row = getRow(pit, pos, y);
1653 RowList::iterator beg = rows().begin();
1655 RowList::iterator old_row = row;
1657 // if we are before the first char of this row and are still in the
1658 // same paragraph and there is a previous row then put the cursor on
1659 // the end of the previous row
1660 cur.iy(y + row->baseline());
1663 boost::prior(row)->par() == row->par() &&
1664 pos < pit->size() &&
1665 pit->getChar(pos) == Paragraph::META_INSET) {
1666 Inset * ins = pit->getInset(pos);
1667 if (ins && (ins->needFullRow() || ins->display())) {
1674 // y is now the beginning of the cursor row
1675 y += row->baseline();
1676 // y is now the cursor baseline
1679 pos_type last = lastPrintablePos(*this, old_row);
1681 // None of these should happen, but we're scaredy-cats
1682 if (pos > pit->size()) {
1683 lyxerr << "dont like 1 please report" << endl;
1686 } else if (pos > last + 1) {
1687 lyxerr << "dont like 2 please report" << endl;
1688 // This shouldn't happen.
1691 } else if (pos < row->pos()) {
1692 lyxerr << "dont like 3 please report" << endl;
1697 // now get the cursors x position
1698 float x = getCursorX(row, pos, last, boundary);
1701 if (old_row != row) {
1702 x = getCursorX(old_row, pos, last, boundary);
1706 /* We take out this for the time being because 1) the redraw code is not
1707 prepared to this yet and 2) because some good policy has yet to be decided
1708 while editting: for instance how to act on rows being created/deleted
1712 //if the cursor is in a visible row, anchor to it
1714 if (topy < y && y < topy + bv()->workHeight())
1720 float LyXText::getCursorX(RowList::iterator rit,
1721 pos_type pos, pos_type last, bool boundary) const
1723 pos_type cursor_vpos = 0;
1725 float fill_separator;
1727 float fill_label_hfill;
1728 // This call HAS to be here because of the BidiTables!!!
1729 prepareToPrint(rit, x, fill_separator, fill_hfill,
1732 ParagraphList::iterator rit_par = rit->par();
1733 pos_type const rit_pos = rit->pos();
1736 cursor_vpos = rit_pos;
1737 else if (pos > last && !boundary)
1738 cursor_vpos = (rit_par->isRightToLeftPar(bv()->buffer()->params))
1739 ? rit_pos : last + 1;
1740 else if (pos > rit_pos && (pos > last || boundary))
1741 /// Place cursor after char at (logical) position pos - 1
1742 cursor_vpos = (bidi_level(pos - 1) % 2 == 0)
1743 ? log2vis(pos - 1) + 1 : log2vis(pos - 1);
1745 /// Place cursor before char at (logical) position pos
1746 cursor_vpos = (bidi_level(pos) % 2 == 0)
1747 ? log2vis(pos) : log2vis(pos) + 1;
1749 pos_type body_pos = rit_par->beginningOfBody();
1750 if ((body_pos > 0) &&
1751 ((body_pos - 1 > last) || !rit_par->isLineSeparator(body_pos - 1)))
1754 for (pos_type vpos = rit_pos; vpos < cursor_vpos; ++vpos) {
1755 pos_type pos = vis2log(vpos);
1756 if (body_pos > 0 && pos == body_pos - 1) {
1757 x += fill_label_hfill +
1758 font_metrics::width(
1759 rit_par->layout()->labelsep,
1760 getLabelFont(bv()->buffer(), rit_par));
1761 if (rit_par->isLineSeparator(body_pos - 1))
1762 x -= singleWidth(rit_par, body_pos - 1);
1765 if (hfillExpansion(*this, rit, pos)) {
1766 x += singleWidth(rit_par, pos);
1767 if (pos >= body_pos)
1770 x += fill_label_hfill;
1771 } else if (rit_par->isSeparator(pos)) {
1772 x += singleWidth(rit_par, pos);
1773 if (pos >= body_pos)
1774 x += fill_separator;
1776 x += singleWidth(rit_par, pos);
1782 void LyXText::setCursorIntern(ParagraphList::iterator pit,
1783 pos_type pos, bool setfont, bool boundary)
1785 UpdatableInset * it = pit->inInset();
1787 if (it != inset_owner) {
1788 lyxerr[Debug::INSETS] << "InsetText is " << it
1790 << "inset_owner is "
1791 << inset_owner << endl;
1792 #ifdef WITH_WARNINGS
1793 #warning I believe this code is wrong. (Lgb)
1794 #warning Jürgen, have a look at this. (Lgb)
1795 #warning Hmmm, I guess you are right but we
1796 #warning should verify when this is needed
1798 // Jürgen, would you like to have a look?
1799 // I guess we need to move the outer cursor
1800 // and open and lock the inset (bla bla bla)
1801 // stuff I don't know... so can you have a look?
1803 // I moved the lyxerr stuff in here so we can see if
1804 // this is actually really needed and where!
1806 // it->getLyXText(bv())->setCursorIntern(bv(), par, pos, setfont, boundary);
1811 setCursor(cursor, pit, pos, boundary);
1817 void LyXText::setCurrentFont()
1819 pos_type pos = cursor.pos();
1820 ParagraphList::iterator pit = cursor.par();
1822 if (cursor.boundary() && pos > 0)
1826 if (pos == pit->size())
1828 else // potentional bug... BUG (Lgb)
1829 if (pit->isSeparator(pos)) {
1830 if (pos > cursor.row()->pos() &&
1831 bidi_level(pos) % 2 ==
1832 bidi_level(pos - 1) % 2)
1834 else if (pos + 1 < pit->size())
1840 pit->getFontSettings(bv()->buffer()->params, pos);
1841 real_current_font = getFont(bv()->buffer(), pit, pos);
1843 if (cursor.pos() == pit->size() &&
1844 isBoundary(bv()->buffer(), *pit, cursor.pos()) &&
1845 !cursor.boundary()) {
1846 Language const * lang =
1847 pit->getParLanguage(bv()->buffer()->params);
1848 current_font.setLanguage(lang);
1849 current_font.setNumber(LyXFont::OFF);
1850 real_current_font.setLanguage(lang);
1851 real_current_font.setNumber(LyXFont::OFF);
1856 // returns the column near the specified x-coordinate of the row
1857 // x is set to the real beginning of this column
1859 LyXText::getColumnNearX(RowList::iterator rit, int & x, bool & boundary) const
1862 float fill_separator;
1864 float fill_label_hfill;
1866 prepareToPrint(rit, tmpx, fill_separator,
1867 fill_hfill, fill_label_hfill);
1869 pos_type vc = rit->pos();
1870 pos_type last = lastPrintablePos(*this, rit);
1873 ParagraphList::iterator rit_par = rit->par();
1874 LyXLayout_ptr const & layout = rit->par()->layout();
1876 bool left_side = false;
1878 pos_type body_pos = rit_par->beginningOfBody();
1879 float last_tmpx = tmpx;
1882 (body_pos - 1 > last ||
1883 !rit_par->isLineSeparator(body_pos - 1)))
1886 // check for empty row
1887 if (!rit_par->size()) {
1892 while (vc <= last && tmpx <= x) {
1895 if (body_pos > 0 && c == body_pos - 1) {
1896 tmpx += fill_label_hfill +
1897 font_metrics::width(layout->labelsep,
1898 getLabelFont(bv()->buffer(), rit_par));
1899 if (rit_par->isLineSeparator(body_pos - 1))
1900 tmpx -= singleWidth(rit_par, body_pos - 1);
1903 if (hfillExpansion(*this, rit, c)) {
1904 tmpx += singleWidth(rit_par, c);
1908 tmpx += fill_label_hfill;
1909 } else if (rit_par->isSeparator(c)) {
1910 tmpx += singleWidth(rit_par, c);
1912 tmpx+= fill_separator;
1914 tmpx += singleWidth(rit_par, c);
1919 if ((tmpx + last_tmpx) / 2 > x) {
1924 if (vc > last + 1) // This shouldn't happen.
1928 // This (rtl_support test) is not needed, but gives
1929 // some speedup if rtl_support=false
1930 RowList::iterator next_rit = boost::next(rit);
1932 bool const lastrow = lyxrc.rtl_support &&
1933 (next_rit == rowlist_.end() ||
1934 next_rit->par() != rit_par);
1936 // If lastrow is false, we don't need to compute
1937 // the value of rtl.
1938 bool const rtl = (lastrow)
1939 ? rit_par->isRightToLeftPar(bv()->buffer()->params)
1942 ((rtl && left_side && vc == rit->pos() && x < tmpx - 5) ||
1943 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5)))
1945 else if (vc == rit->pos()) {
1947 if (bidi_level(c) % 2 == 1)
1950 c = vis2log(vc - 1);
1951 bool const rtl = (bidi_level(c) % 2 == 1);
1952 if (left_side == rtl) {
1954 boundary = isBoundary(bv()->buffer(), *rit_par, c);
1958 if (rit->pos() <= last && c > last
1959 && rit_par->isNewline(last)) {
1960 if (bidi_level(last) % 2 == 0)
1961 tmpx -= singleWidth(rit_par, last);
1963 tmpx += singleWidth(rit_par, last);
1973 void LyXText::setCursorFromCoordinates(int x, int y)
1975 LyXCursor old_cursor = cursor;
1977 setCursorFromCoordinates(cursor, x, y);
1979 deleteEmptyParagraphMechanism(old_cursor);
1986 * return true if the cursor given is at the end of a row,
1987 * and the next row is filled by an inset that spans an entire
1990 bool beforeFullRowInset(LyXText & lt, LyXCursor const & cur)
1992 RowList::iterator row = cur.row();
1993 if (boost::next(row) == lt.rows().end())
1996 Row const & next = *boost::next(row);
1998 if (next.pos() != cur.pos() || next.par() != cur.par())
2001 if (cur.pos() == cur.par()->size()
2002 || !cur.par()->isInset(cur.pos()))
2005 Inset const * inset = cur.par()->getInset(cur.pos());
2006 if (inset->needFullRow() || inset->display())
2014 void LyXText::setCursorFromCoordinates(LyXCursor & cur, int x, int y)
2016 // Get the row first.
2018 RowList::iterator row = getRowNearY(y);
2020 pos_type const column = getColumnNearX(row, x, bound);
2021 cur.par(row->par());
2022 cur.pos(row->pos() + column);
2024 cur.y(y + row->baseline());
2027 if (beforeFullRowInset(*this, cur)) {
2028 pos_type const last = lastPrintablePos(*this, row);
2029 RowList::iterator next_row = boost::next(row);
2031 float x = getCursorX(next_row, cur.pos(), last, bound);
2033 cur.iy(y + row->height() + next_row->baseline());
2040 cur.boundary(bound);
2044 void LyXText::cursorLeft(bool internal)
2046 if (cursor.pos() > 0) {
2047 bool boundary = cursor.boundary();
2048 setCursor(cursor.par(), cursor.pos() - 1, true, false);
2049 if (!internal && !boundary &&
2050 isBoundary(bv()->buffer(), *cursor.par(), cursor.pos() + 1))
2051 setCursor(cursor.par(), cursor.pos() + 1, true, true);
2052 } else if (cursor.par() != ownerParagraphs().begin()) { // steps into the above paragraph.
2053 ParagraphList::iterator pit = boost::prior(cursor.par());
2054 setCursor(pit, pit->size());
2059 void LyXText::cursorRight(bool internal)
2061 bool const at_end = (cursor.pos() == cursor.par()->size());
2062 bool const at_newline = !at_end &&
2063 cursor.par()->isNewline(cursor.pos());
2065 if (!internal && cursor.boundary() && !at_newline)
2066 setCursor(cursor.par(), cursor.pos(), true, false);
2068 setCursor(cursor.par(), cursor.pos() + 1, true, false);
2070 isBoundary(bv()->buffer(), *cursor.par(), cursor.pos()))
2071 setCursor(cursor.par(), cursor.pos(), true, true);
2072 } else if (boost::next(cursor.par()) != ownerParagraphs().end())
2073 setCursor(boost::next(cursor.par()), 0);
2077 void LyXText::cursorUp(bool selecting)
2080 int x = cursor.x_fix();
2081 int y = cursor.y() - cursor.row()->baseline() - 1;
2082 setCursorFromCoordinates(x, y);
2085 int y1 = cursor.iy() - topy;
2088 Inset * inset_hit = checkInsetHit(x, y1);
2089 if (inset_hit && isHighlyEditableInset(inset_hit)) {
2090 inset_hit->localDispatch(
2091 FuncRequest(bv(), LFUN_INSET_EDIT, x, y - (y2 - y1), mouse_button::none));
2095 setCursorFromCoordinates(bv(), cursor.x_fix(),
2096 cursor.y() - cursor.row()->baseline() - 1);
2101 void LyXText::cursorDown(bool selecting)
2104 int x = cursor.x_fix();
2105 int y = cursor.y() - cursor.row()->baseline() +
2106 cursor.row()->height() + 1;
2107 setCursorFromCoordinates(x, y);
2108 if (!selecting && cursor.row() == cursor.irow()) {
2110 int y1 = cursor.iy() - topy;
2113 Inset * inset_hit = checkInsetHit(x, y1);
2114 if (inset_hit && isHighlyEditableInset(inset_hit)) {
2115 FuncRequest cmd(bv(), LFUN_INSET_EDIT, x, y - (y2 - y1), mouse_button::none);
2116 inset_hit->localDispatch(cmd);
2120 setCursorFromCoordinates(bv(), cursor.x_fix(),
2121 cursor.y() - cursor.row()->baseline()
2122 + cursor.row()->height() + 1);
2127 void LyXText::cursorUpParagraph()
2129 if (cursor.pos() > 0) {
2130 setCursor(cursor.par(), 0);
2132 else if (cursor.par() != ownerParagraphs().begin()) {
2133 setCursor(boost::prior(cursor.par()), 0);
2138 void LyXText::cursorDownParagraph()
2140 ParagraphList::iterator par = cursor.par();
2141 ParagraphList::iterator next_par = boost::next(par);
2143 if (next_par != ownerParagraphs().end()) {
2144 setCursor(next_par, 0);
2146 setCursor(par, par->size());
2150 // fix the cursor `cur' after a characters has been deleted at `where'
2151 // position. Called by deleteEmptyParagraphMechanism
2152 void LyXText::fixCursorAfterDelete(LyXCursor & cur,
2153 LyXCursor const & where)
2155 // if cursor is not in the paragraph where the delete occured,
2157 if (cur.par() != where.par())
2160 // if cursor position is after the place where the delete occured,
2162 if (cur.pos() > where.pos())
2163 cur.pos(cur.pos()-1);
2165 // check also if we don't want to set the cursor on a spot behind the
2166 // pagragraph because we erased the last character.
2167 if (cur.pos() > cur.par()->size())
2168 cur.pos(cur.par()->size());
2170 // recompute row et al. for this cursor
2171 setCursor(cur, cur.par(), cur.pos(), cur.boundary());
2175 bool LyXText::deleteEmptyParagraphMechanism(LyXCursor const & old_cursor)
2177 // Would be wrong to delete anything if we have a selection.
2178 if (selection.set())
2181 // We allow all kinds of "mumbo-jumbo" when freespacing.
2182 if (old_cursor.par()->layout()->free_spacing
2183 || old_cursor.par()->isFreeSpacing()) {
2187 /* Ok I'll put some comments here about what is missing.
2188 I have fixed BackSpace (and thus Delete) to not delete
2189 double-spaces automagically. I have also changed Cut,
2190 Copy and Paste to hopefully do some sensible things.
2191 There are still some small problems that can lead to
2192 double spaces stored in the document file or space at
2193 the beginning of paragraphs. This happens if you have
2194 the cursor betwenn to spaces and then save. Or if you
2195 cut and paste and the selection have a space at the
2196 beginning and then save right after the paste. I am
2197 sure none of these are very hard to fix, but I will
2198 put out 1.1.4pre2 with FIX_DOUBLE_SPACE defined so
2199 that I can get some feedback. (Lgb)
2202 // If old_cursor.pos() == 0 and old_cursor.pos()(1) == LineSeparator
2203 // delete the LineSeparator.
2206 // If old_cursor.pos() == 1 and old_cursor.pos()(0) == LineSeparator
2207 // delete the LineSeparator.
2210 // If the pos around the old_cursor were spaces, delete one of them.
2211 if (old_cursor.par() != cursor.par()
2212 || old_cursor.pos() != cursor.pos()) {
2213 // Only if the cursor has really moved
2215 if (old_cursor.pos() > 0
2216 && old_cursor.pos() < old_cursor.par()->size()
2217 && old_cursor.par()->isLineSeparator(old_cursor.pos())
2218 && old_cursor.par()->isLineSeparator(old_cursor.pos() - 1)) {
2219 old_cursor.par()->erase(old_cursor.pos() - 1);
2220 redoParagraphs(old_cursor, boost::next(old_cursor.par()));
2222 #ifdef WITH_WARNINGS
2223 #warning This will not work anymore when we have multiple views of the same buffer
2224 // In this case, we will have to correct also the cursors held by
2225 // other bufferviews. It will probably be easier to do that in a more
2226 // automated way in LyXCursor code. (JMarc 26/09/2001)
2228 // correct all cursors held by the LyXText
2229 fixCursorAfterDelete(cursor, old_cursor);
2230 fixCursorAfterDelete(selection.cursor,
2232 fixCursorAfterDelete(selection.start,
2234 fixCursorAfterDelete(selection.end, old_cursor);
2235 fixCursorAfterDelete(last_sel_cursor,
2237 fixCursorAfterDelete(toggle_cursor, old_cursor);
2238 fixCursorAfterDelete(toggle_end_cursor,
2244 // don't delete anything if this is the ONLY paragraph!
2245 if (ownerParagraphs().size() == 1)
2248 // Do not delete empty paragraphs with keepempty set.
2249 if (old_cursor.par()->allowEmpty())
2252 // only do our magic if we changed paragraph
2253 if (old_cursor.par() == cursor.par())
2256 // record if we have deleted a paragraph
2257 // we can't possibly have deleted a paragraph before this point
2258 bool deleted = false;
2260 if (old_cursor.par()->empty() ||
2261 (old_cursor.par()->size() == 1 &&
2262 old_cursor.par()->isLineSeparator(0))) {
2263 // ok, we will delete anything
2264 LyXCursor tmpcursor;
2268 bool selection_position_was_oldcursor_position = (
2269 selection.cursor.par() == old_cursor.par()
2270 && selection.cursor.pos() == old_cursor.pos());
2272 if (old_cursor.row() != rows().begin()) {
2274 prevrow = boost::prior(old_cursor.row());
2275 const_cast<LyXText *>(this)->postPaint(old_cursor.y() - old_cursor.row()->baseline() - prevrow->height());
2277 cursor = old_cursor; // that undo can restore the right cursor position
2278 #warning FIXME. --end() iterator is usable here
2279 ParagraphList::iterator endpit = boost::next(old_cursor.par());
2280 while (endpit != ownerParagraphs().end() &&
2281 endpit->getDepth()) {
2285 setUndo(bv(), Undo::DELETE, old_cursor.par(),
2286 boost::prior(endpit));
2290 removeRow(old_cursor.row());
2292 ownerParagraphs().erase(old_cursor.par());
2294 /* Breakagain the next par. Needed because of
2295 * the parindent that can occur or dissappear.
2296 * The next row can change its height, if
2297 * there is another layout before */
2298 RowList::iterator tmprit = boost::next(prevrow);
2299 if (tmprit != rows().end()) {
2303 setHeightOfRow(prevrow);
2305 RowList::iterator nextrow = boost::next(old_cursor.row());
2306 const_cast<LyXText *>(this)->postPaint(
2307 old_cursor.y() - old_cursor.row()->baseline());
2310 cursor = old_cursor; // that undo can restore the right cursor position
2311 #warning FIXME. --end() iterator is usable here
2312 ParagraphList::iterator endpit = boost::next(old_cursor.par());
2313 while (endpit != ownerParagraphs().end() &&
2314 endpit->getDepth()) {
2318 setUndo(bv(), Undo::DELETE, old_cursor.par(), boost::prior(endpit));
2322 removeRow(old_cursor.row());
2324 ownerParagraphs().erase(old_cursor.par());
2326 /* Breakagain the next par. Needed because of
2327 the parindent that can occur or dissappear.
2328 The next row can change its height, if
2329 there is another layout before */
2330 if (nextrow != rows().end()) {
2331 breakAgain(nextrow);
2337 setCursorIntern(cursor.par(), cursor.pos());
2339 if (selection_position_was_oldcursor_position) {
2340 // correct selection
2341 selection.cursor = cursor;
2345 if (old_cursor.par()->stripLeadingSpaces()) {
2346 redoParagraphs(old_cursor, boost::next(old_cursor.par()));
2348 setCursorIntern(cursor.par(), cursor.pos());
2349 selection.cursor = cursor;
2356 ParagraphList & LyXText::ownerParagraphs() const
2359 return inset_owner->paragraphs;
2361 return bv_owner->buffer()->paragraphs;
2365 LyXText::refresh_status LyXText::refreshStatus() const
2367 return refresh_status_;
2371 void LyXText::clearPaint()
2373 refresh_status_ = REFRESH_NONE;
2374 refresh_row = rows().end();
2379 void LyXText::postPaint(int start_y)
2381 refresh_status old = refresh_status_;
2383 refresh_status_ = REFRESH_AREA;
2384 refresh_row = rows().end();
2386 if (old != REFRESH_NONE && refresh_y < start_y)
2389 refresh_y = start_y;
2394 // We are an inset's lyxtext. Tell the top-level lyxtext
2395 // it needs to update the row we're in.
2396 LyXText * t = bv()->text;
2397 t->postRowPaint(t->cursor.row(), t->cursor.y() - t->cursor.row()->baseline());
2401 // FIXME: we should probably remove this y parameter,
2402 // make refresh_y be 0, and use row->y etc.
2403 void LyXText::postRowPaint(RowList::iterator rit, int start_y)
2405 if (refresh_status_ != REFRESH_NONE && refresh_y < start_y) {
2406 refresh_status_ = REFRESH_AREA;
2409 refresh_y = start_y;
2412 if (refresh_status_ == REFRESH_AREA)
2415 refresh_status_ = REFRESH_ROW;
2421 // We are an inset's lyxtext. Tell the top-level lyxtext
2422 // it needs to update the row we're in.
2423 LyXText * t = bv()->text;
2424 t->postRowPaint(t->cursor.row(), t->cursor.y() - t->cursor.row()->baseline());
2428 bool LyXText::isInInset() const
2430 // Sub-level has non-null bv owner and
2431 // non-null inset owner.
2432 return inset_owner != 0 && bv_owner != 0;
2436 int defaultRowHeight()
2438 LyXFont const font(LyXFont::ALL_SANE);
2439 return int(font_metrics::maxAscent(font)
2440 + font_metrics::maxDescent(font) * 1.5);