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 = cursorRow();
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 = getRow(cur);
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 = getRow(cur);
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(), cursorRow()->pos());
856 void LyXText::cursorEnd()
858 if (cursor.par()->empty())
861 RowList::iterator rit = cursorRow();
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() - cursorRow()->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) {
1574 redoDrawingOfParagraph(tmpcursor);
1577 // set the cursor again. Otherwise dangling pointers are possible
1578 // also set the selection
1580 if (selection.set()) {
1582 setCursorIntern(selection.cursor.par(), selection.cursor.pos(),
1583 false, selection.cursor.boundary());
1584 selection.cursor = cursor;
1585 setCursorIntern(selection.start.par(),
1586 selection.start.pos(),
1587 false, selection.start.boundary());
1588 selection.start = cursor;
1589 setCursorIntern(selection.end.par(),
1590 selection.end.pos(),
1591 false, selection.end.boundary());
1592 selection.end = cursor;
1593 setCursorIntern(last_sel_cursor.par(),
1594 last_sel_cursor.pos(),
1595 false, last_sel_cursor.boundary());
1596 last_sel_cursor = cursor;
1599 setCursorIntern(cursor.par(), cursor.pos(),
1600 false, cursor.boundary());
1604 // returns false if inset wasn't found
1605 bool LyXText::updateInset(Inset * inset)
1607 // first check the current paragraph
1608 int pos = cursor.par()->getPositionOfInset(inset);
1610 checkParagraph(cursor.par(), pos);
1614 // check every paragraph
1616 ParagraphList::iterator par = ownerParagraphs().begin();
1617 ParagraphList::iterator end = ownerParagraphs().end();
1618 for (; par != end; ++par) {
1619 pos = par->getPositionOfInset(inset);
1621 checkParagraph(par, pos);
1630 bool LyXText::setCursor(ParagraphList::iterator pit,
1632 bool setfont, bool boundary)
1634 LyXCursor old_cursor = cursor;
1635 setCursorIntern(pit, pos, setfont, boundary);
1636 return deleteEmptyParagraphMechanism(old_cursor);
1640 void LyXText::setCursor(LyXCursor & cur, ParagraphList::iterator pit,
1641 pos_type pos, bool boundary)
1643 lyx::Assert(pit != ownerParagraphs().end());
1647 cur.boundary(boundary);
1649 // get the cursor y position in text
1651 RowList::iterator row = getRow(pit, pos, y);
1652 RowList::iterator beg = rows().begin();
1654 RowList::iterator old_row = row;
1656 // if we are before the first char of this row and are still in the
1657 // same paragraph and there is a previous row then put the cursor on
1658 // the end of the previous row
1659 cur.iy(y + row->baseline());
1662 boost::prior(row)->par() == row->par() &&
1663 pos < pit->size() &&
1664 pit->getChar(pos) == Paragraph::META_INSET) {
1665 Inset * ins = pit->getInset(pos);
1666 if (ins && (ins->needFullRow() || ins->display())) {
1672 // y is now the beginning of the cursor row
1673 y += row->baseline();
1674 // y is now the cursor baseline
1677 pos_type last = lastPrintablePos(*this, old_row);
1679 // None of these should happen, but we're scaredy-cats
1680 if (pos > pit->size()) {
1681 lyxerr << "dont like 1 please report" << endl;
1684 } else if (pos > last + 1) {
1685 lyxerr << "dont like 2 please report" << endl;
1686 // This shouldn't happen.
1689 } else if (pos < row->pos()) {
1690 lyxerr << "dont like 3 please report" << endl;
1695 // now get the cursors x position
1696 float x = getCursorX(row, pos, last, boundary);
1699 if (old_row != row) {
1700 x = getCursorX(old_row, pos, last, boundary);
1704 /* We take out this for the time being because 1) the redraw code is not
1705 prepared to this yet and 2) because some good policy has yet to be decided
1706 while editting: for instance how to act on rows being created/deleted
1710 //if the cursor is in a visible row, anchor to it
1712 if (topy < y && y < topy + bv()->workHeight())
1718 float LyXText::getCursorX(RowList::iterator rit,
1719 pos_type pos, pos_type last, bool boundary) const
1721 pos_type cursor_vpos = 0;
1723 float fill_separator;
1725 float fill_label_hfill;
1726 // This call HAS to be here because of the BidiTables!!!
1727 prepareToPrint(rit, x, fill_separator, fill_hfill,
1730 ParagraphList::iterator rit_par = rit->par();
1731 pos_type const rit_pos = rit->pos();
1734 cursor_vpos = rit_pos;
1735 else if (pos > last && !boundary)
1736 cursor_vpos = (rit_par->isRightToLeftPar(bv()->buffer()->params))
1737 ? rit_pos : last + 1;
1738 else if (pos > rit_pos && (pos > last || boundary))
1739 /// Place cursor after char at (logical) position pos - 1
1740 cursor_vpos = (bidi_level(pos - 1) % 2 == 0)
1741 ? log2vis(pos - 1) + 1 : log2vis(pos - 1);
1743 /// Place cursor before char at (logical) position pos
1744 cursor_vpos = (bidi_level(pos) % 2 == 0)
1745 ? log2vis(pos) : log2vis(pos) + 1;
1747 pos_type body_pos = rit_par->beginningOfBody();
1748 if ((body_pos > 0) &&
1749 ((body_pos - 1 > last) || !rit_par->isLineSeparator(body_pos - 1)))
1752 for (pos_type vpos = rit_pos; vpos < cursor_vpos; ++vpos) {
1753 pos_type pos = vis2log(vpos);
1754 if (body_pos > 0 && pos == body_pos - 1) {
1755 x += fill_label_hfill +
1756 font_metrics::width(
1757 rit_par->layout()->labelsep,
1758 getLabelFont(bv()->buffer(), rit_par));
1759 if (rit_par->isLineSeparator(body_pos - 1))
1760 x -= singleWidth(rit_par, body_pos - 1);
1763 if (hfillExpansion(*this, rit, pos)) {
1764 x += singleWidth(rit_par, pos);
1765 if (pos >= body_pos)
1768 x += fill_label_hfill;
1769 } else if (rit_par->isSeparator(pos)) {
1770 x += singleWidth(rit_par, pos);
1771 if (pos >= body_pos)
1772 x += fill_separator;
1774 x += singleWidth(rit_par, pos);
1780 void LyXText::setCursorIntern(ParagraphList::iterator pit,
1781 pos_type pos, bool setfont, bool boundary)
1783 UpdatableInset * it = pit->inInset();
1785 if (it != inset_owner) {
1786 lyxerr[Debug::INSETS] << "InsetText is " << it
1788 << "inset_owner is "
1789 << inset_owner << endl;
1790 #ifdef WITH_WARNINGS
1791 #warning I believe this code is wrong. (Lgb)
1792 #warning Jürgen, have a look at this. (Lgb)
1793 #warning Hmmm, I guess you are right but we
1794 #warning should verify when this is needed
1796 // Jürgen, would you like to have a look?
1797 // I guess we need to move the outer cursor
1798 // and open and lock the inset (bla bla bla)
1799 // stuff I don't know... so can you have a look?
1801 // I moved the lyxerr stuff in here so we can see if
1802 // this is actually really needed and where!
1804 // it->getLyXText(bv())->setCursorIntern(bv(), par, pos, setfont, boundary);
1809 setCursor(cursor, pit, pos, boundary);
1815 void LyXText::setCurrentFont()
1817 pos_type pos = cursor.pos();
1818 ParagraphList::iterator pit = cursor.par();
1820 if (cursor.boundary() && pos > 0)
1824 if (pos == pit->size())
1826 else // potentional bug... BUG (Lgb)
1827 if (pit->isSeparator(pos)) {
1828 if (pos > cursorRow()->pos() &&
1829 bidi_level(pos) % 2 ==
1830 bidi_level(pos - 1) % 2)
1832 else if (pos + 1 < pit->size())
1838 pit->getFontSettings(bv()->buffer()->params, pos);
1839 real_current_font = getFont(bv()->buffer(), pit, pos);
1841 if (cursor.pos() == pit->size() &&
1842 isBoundary(bv()->buffer(), *pit, cursor.pos()) &&
1843 !cursor.boundary()) {
1844 Language const * lang =
1845 pit->getParLanguage(bv()->buffer()->params);
1846 current_font.setLanguage(lang);
1847 current_font.setNumber(LyXFont::OFF);
1848 real_current_font.setLanguage(lang);
1849 real_current_font.setNumber(LyXFont::OFF);
1854 // returns the column near the specified x-coordinate of the row
1855 // x is set to the real beginning of this column
1857 LyXText::getColumnNearX(RowList::iterator rit, int & x, bool & boundary) const
1860 float fill_separator;
1862 float fill_label_hfill;
1864 prepareToPrint(rit, tmpx, fill_separator,
1865 fill_hfill, fill_label_hfill);
1867 pos_type vc = rit->pos();
1868 pos_type last = lastPrintablePos(*this, rit);
1871 ParagraphList::iterator rit_par = rit->par();
1872 LyXLayout_ptr const & layout = rit->par()->layout();
1874 bool left_side = false;
1876 pos_type body_pos = rit_par->beginningOfBody();
1877 float last_tmpx = tmpx;
1880 (body_pos - 1 > last ||
1881 !rit_par->isLineSeparator(body_pos - 1)))
1884 // check for empty row
1885 if (!rit_par->size()) {
1890 while (vc <= last && tmpx <= x) {
1893 if (body_pos > 0 && c == body_pos - 1) {
1894 tmpx += fill_label_hfill +
1895 font_metrics::width(layout->labelsep,
1896 getLabelFont(bv()->buffer(), rit_par));
1897 if (rit_par->isLineSeparator(body_pos - 1))
1898 tmpx -= singleWidth(rit_par, body_pos - 1);
1901 if (hfillExpansion(*this, rit, c)) {
1902 tmpx += singleWidth(rit_par, c);
1906 tmpx += fill_label_hfill;
1907 } else if (rit_par->isSeparator(c)) {
1908 tmpx += singleWidth(rit_par, c);
1910 tmpx+= fill_separator;
1912 tmpx += singleWidth(rit_par, c);
1917 if ((tmpx + last_tmpx) / 2 > x) {
1922 if (vc > last + 1) // This shouldn't happen.
1926 // This (rtl_support test) is not needed, but gives
1927 // some speedup if rtl_support=false
1928 RowList::iterator next_rit = boost::next(rit);
1930 bool const lastrow = lyxrc.rtl_support &&
1931 (next_rit == rowlist_.end() ||
1932 next_rit->par() != rit_par);
1934 // If lastrow is false, we don't need to compute
1935 // the value of rtl.
1936 bool const rtl = (lastrow)
1937 ? rit_par->isRightToLeftPar(bv()->buffer()->params)
1940 ((rtl && left_side && vc == rit->pos() && x < tmpx - 5) ||
1941 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5)))
1943 else if (vc == rit->pos()) {
1945 if (bidi_level(c) % 2 == 1)
1948 c = vis2log(vc - 1);
1949 bool const rtl = (bidi_level(c) % 2 == 1);
1950 if (left_side == rtl) {
1952 boundary = isBoundary(bv()->buffer(), *rit_par, c);
1956 if (rit->pos() <= last && c > last
1957 && rit_par->isNewline(last)) {
1958 if (bidi_level(last) % 2 == 0)
1959 tmpx -= singleWidth(rit_par, last);
1961 tmpx += singleWidth(rit_par, last);
1971 void LyXText::setCursorFromCoordinates(int x, int y)
1973 LyXCursor old_cursor = cursor;
1975 setCursorFromCoordinates(cursor, x, y);
1977 deleteEmptyParagraphMechanism(old_cursor);
1984 * return true if the cursor given is at the end of a row,
1985 * and the next row is filled by an inset that spans an entire
1988 bool beforeFullRowInset(LyXText & lt, LyXCursor const & cur)
1990 RowList::iterator row = lt.getRow(cur);
1991 if (boost::next(row) == lt.rows().end())
1994 Row const & next = *boost::next(row);
1996 if (next.pos() != cur.pos() || next.par() != cur.par())
1999 if (cur.pos() == cur.par()->size()
2000 || !cur.par()->isInset(cur.pos()))
2003 Inset const * inset = cur.par()->getInset(cur.pos());
2004 if (inset->needFullRow() || inset->display())
2012 void LyXText::setCursorFromCoordinates(LyXCursor & cur, int x, int y)
2014 // Get the row first.
2016 RowList::iterator row = getRowNearY(y);
2018 pos_type const column = getColumnNearX(row, x, bound);
2019 cur.par(row->par());
2020 cur.pos(row->pos() + column);
2022 cur.y(y + row->baseline());
2024 if (beforeFullRowInset(*this, cur)) {
2025 pos_type const last = lastPrintablePos(*this, row);
2026 RowList::iterator next_row = boost::next(row);
2028 float x = getCursorX(next_row, cur.pos(), last, bound);
2030 cur.iy(y + row->height() + next_row->baseline());
2037 cur.boundary(bound);
2041 void LyXText::cursorLeft(bool internal)
2043 if (cursor.pos() > 0) {
2044 bool boundary = cursor.boundary();
2045 setCursor(cursor.par(), cursor.pos() - 1, true, false);
2046 if (!internal && !boundary &&
2047 isBoundary(bv()->buffer(), *cursor.par(), cursor.pos() + 1))
2048 setCursor(cursor.par(), cursor.pos() + 1, true, true);
2049 } else if (cursor.par() != ownerParagraphs().begin()) { // steps into the above paragraph.
2050 ParagraphList::iterator pit = boost::prior(cursor.par());
2051 setCursor(pit, pit->size());
2056 void LyXText::cursorRight(bool internal)
2058 bool const at_end = (cursor.pos() == cursor.par()->size());
2059 bool const at_newline = !at_end &&
2060 cursor.par()->isNewline(cursor.pos());
2062 if (!internal && cursor.boundary() && !at_newline)
2063 setCursor(cursor.par(), cursor.pos(), true, false);
2065 setCursor(cursor.par(), cursor.pos() + 1, true, false);
2067 isBoundary(bv()->buffer(), *cursor.par(), cursor.pos()))
2068 setCursor(cursor.par(), cursor.pos(), true, true);
2069 } else if (boost::next(cursor.par()) != ownerParagraphs().end())
2070 setCursor(boost::next(cursor.par()), 0);
2074 void LyXText::cursorUp(bool selecting)
2077 int x = cursor.x_fix();
2078 int y = cursor.y() - cursorRow()->baseline() - 1;
2079 setCursorFromCoordinates(x, y);
2082 int y1 = cursor.iy() - topy;
2085 Inset * inset_hit = checkInsetHit(x, y1);
2086 if (inset_hit && isHighlyEditableInset(inset_hit)) {
2087 inset_hit->localDispatch(
2088 FuncRequest(bv(), LFUN_INSET_EDIT, x, y - (y2 - y1), mouse_button::none));
2092 setCursorFromCoordinates(bv(), cursor.x_fix(),
2093 cursor.y() - cursorRow()->baseline() - 1);
2098 void LyXText::cursorDown(bool selecting)
2101 int x = cursor.x_fix();
2102 int y = cursor.y() - cursorRow()->baseline() +
2103 cursorRow()->height() + 1;
2104 setCursorFromCoordinates(x, y);
2105 if (!selecting && cursorRow() == cursor.irow()) {
2107 int y1 = cursor.iy() - topy;
2110 Inset * inset_hit = checkInsetHit(x, y1);
2111 if (inset_hit && isHighlyEditableInset(inset_hit)) {
2112 FuncRequest cmd(bv(), LFUN_INSET_EDIT, x, y - (y2 - y1), mouse_button::none);
2113 inset_hit->localDispatch(cmd);
2117 setCursorFromCoordinates(bv(), cursor.x_fix(),
2118 cursor.y() - cursorRow()->baseline()
2119 + cursorRow()->height() + 1);
2124 void LyXText::cursorUpParagraph()
2126 if (cursor.pos() > 0) {
2127 setCursor(cursor.par(), 0);
2129 else if (cursor.par() != ownerParagraphs().begin()) {
2130 setCursor(boost::prior(cursor.par()), 0);
2135 void LyXText::cursorDownParagraph()
2137 ParagraphList::iterator par = cursor.par();
2138 ParagraphList::iterator next_par = boost::next(par);
2140 if (next_par != ownerParagraphs().end()) {
2141 setCursor(next_par, 0);
2143 setCursor(par, par->size());
2147 // fix the cursor `cur' after a characters has been deleted at `where'
2148 // position. Called by deleteEmptyParagraphMechanism
2149 void LyXText::fixCursorAfterDelete(LyXCursor & cur,
2150 LyXCursor const & where)
2152 // if cursor is not in the paragraph where the delete occured,
2154 if (cur.par() != where.par())
2157 // if cursor position is after the place where the delete occured,
2159 if (cur.pos() > where.pos())
2160 cur.pos(cur.pos()-1);
2162 // check also if we don't want to set the cursor on a spot behind the
2163 // pagragraph because we erased the last character.
2164 if (cur.pos() > cur.par()->size())
2165 cur.pos(cur.par()->size());
2167 // recompute row et al. for this cursor
2168 setCursor(cur, cur.par(), cur.pos(), cur.boundary());
2172 bool LyXText::deleteEmptyParagraphMechanism(LyXCursor const & old_cursor)
2174 // Would be wrong to delete anything if we have a selection.
2175 if (selection.set())
2178 // We allow all kinds of "mumbo-jumbo" when freespacing.
2179 if (old_cursor.par()->layout()->free_spacing
2180 || old_cursor.par()->isFreeSpacing()) {
2184 /* Ok I'll put some comments here about what is missing.
2185 I have fixed BackSpace (and thus Delete) to not delete
2186 double-spaces automagically. I have also changed Cut,
2187 Copy and Paste to hopefully do some sensible things.
2188 There are still some small problems that can lead to
2189 double spaces stored in the document file or space at
2190 the beginning of paragraphs. This happens if you have
2191 the cursor betwenn to spaces and then save. Or if you
2192 cut and paste and the selection have a space at the
2193 beginning and then save right after the paste. I am
2194 sure none of these are very hard to fix, but I will
2195 put out 1.1.4pre2 with FIX_DOUBLE_SPACE defined so
2196 that I can get some feedback. (Lgb)
2199 // If old_cursor.pos() == 0 and old_cursor.pos()(1) == LineSeparator
2200 // delete the LineSeparator.
2203 // If old_cursor.pos() == 1 and old_cursor.pos()(0) == LineSeparator
2204 // delete the LineSeparator.
2207 // If the pos around the old_cursor were spaces, delete one of them.
2208 if (old_cursor.par() != cursor.par()
2209 || old_cursor.pos() != cursor.pos()) {
2210 // Only if the cursor has really moved
2212 if (old_cursor.pos() > 0
2213 && old_cursor.pos() < old_cursor.par()->size()
2214 && old_cursor.par()->isLineSeparator(old_cursor.pos())
2215 && old_cursor.par()->isLineSeparator(old_cursor.pos() - 1)) {
2216 old_cursor.par()->erase(old_cursor.pos() - 1);
2217 redoParagraphs(old_cursor, boost::next(old_cursor.par()));
2219 #ifdef WITH_WARNINGS
2220 #warning This will not work anymore when we have multiple views of the same buffer
2221 // In this case, we will have to correct also the cursors held by
2222 // other bufferviews. It will probably be easier to do that in a more
2223 // automated way in LyXCursor code. (JMarc 26/09/2001)
2225 // correct all cursors held by the LyXText
2226 fixCursorAfterDelete(cursor, old_cursor);
2227 fixCursorAfterDelete(selection.cursor,
2229 fixCursorAfterDelete(selection.start,
2231 fixCursorAfterDelete(selection.end, old_cursor);
2232 fixCursorAfterDelete(last_sel_cursor,
2234 fixCursorAfterDelete(toggle_cursor, old_cursor);
2235 fixCursorAfterDelete(toggle_end_cursor,
2241 // don't delete anything if this is the ONLY paragraph!
2242 if (ownerParagraphs().size() == 1)
2245 // Do not delete empty paragraphs with keepempty set.
2246 if (old_cursor.par()->allowEmpty())
2249 // only do our magic if we changed paragraph
2250 if (old_cursor.par() == cursor.par())
2253 // record if we have deleted a paragraph
2254 // we can't possibly have deleted a paragraph before this point
2255 bool deleted = false;
2257 if (old_cursor.par()->empty() ||
2258 (old_cursor.par()->size() == 1 &&
2259 old_cursor.par()->isLineSeparator(0))) {
2260 // ok, we will delete anything
2261 LyXCursor tmpcursor;
2265 bool selection_position_was_oldcursor_position = (
2266 selection.cursor.par() == old_cursor.par()
2267 && selection.cursor.pos() == old_cursor.pos());
2269 if (getRow(old_cursor) != rows().begin()) {
2271 prevrow = boost::prior(getRow(old_cursor));
2272 postPaint(old_cursor.y() - getRow(old_cursor)->baseline() - prevrow->height());
2274 cursor = old_cursor; // that undo can restore the right cursor position
2275 #warning FIXME. --end() iterator is usable here
2276 ParagraphList::iterator endpit = boost::next(old_cursor.par());
2277 while (endpit != ownerParagraphs().end() &&
2278 endpit->getDepth()) {
2282 setUndo(bv(), Undo::DELETE, old_cursor.par(),
2283 boost::prior(endpit));
2287 removeRow(getRow(old_cursor));
2289 ownerParagraphs().erase(old_cursor.par());
2291 /* Breakagain the next par. Needed because of
2292 * the parindent that can occur or dissappear.
2293 * The next row can change its height, if
2294 * there is another layout before */
2295 RowList::iterator tmprit = boost::next(prevrow);
2296 if (tmprit != rows().end()) {
2300 setHeightOfRow(prevrow);
2302 RowList::iterator nextrow = boost::next(getRow(old_cursor));
2303 postPaint(old_cursor.y() - getRow(old_cursor)->baseline());
2306 cursor = old_cursor; // that undo can restore the right cursor position
2307 #warning FIXME. --end() iterator is usable here
2308 ParagraphList::iterator endpit = boost::next(old_cursor.par());
2309 while (endpit != ownerParagraphs().end() &&
2310 endpit->getDepth()) {
2314 setUndo(bv(), Undo::DELETE, old_cursor.par(), boost::prior(endpit));
2318 removeRow(getRow(old_cursor));
2320 ownerParagraphs().erase(old_cursor.par());
2322 /* Breakagain the next par. Needed because of
2323 the parindent that can occur or dissappear.
2324 The next row can change its height, if
2325 there is another layout before */
2326 if (nextrow != rows().end()) {
2327 breakAgain(nextrow);
2333 setCursorIntern(cursor.par(), cursor.pos());
2335 if (selection_position_was_oldcursor_position) {
2336 // correct selection
2337 selection.cursor = cursor;
2341 if (old_cursor.par()->stripLeadingSpaces()) {
2342 redoParagraphs(old_cursor, boost::next(old_cursor.par()));
2344 setCursorIntern(cursor.par(), cursor.pos());
2345 selection.cursor = cursor;
2352 ParagraphList & LyXText::ownerParagraphs() const
2355 return inset_owner->paragraphs;
2357 return bv_owner->buffer()->paragraphs;
2361 LyXText::refresh_status LyXText::refreshStatus() const
2363 return refresh_status_;
2367 void LyXText::clearPaint()
2369 refresh_status_ = REFRESH_NONE;
2370 refresh_row = rows().end();
2375 void LyXText::postPaint(int start_y)
2377 refresh_status old = refresh_status_;
2379 refresh_status_ = REFRESH_AREA;
2380 refresh_row = rows().end();
2382 if (old != REFRESH_NONE && refresh_y < start_y)
2385 refresh_y = start_y;
2390 // We are an inset's lyxtext. Tell the top-level lyxtext
2391 // it needs to update the row we're in.
2392 LyXText * t = bv()->text;
2393 t->postRowPaint(t->cursorRow(), t->cursor.y() - t->cursorRow()->baseline());
2397 // FIXME: we should probably remove this y parameter,
2398 // make refresh_y be 0, and use row->y etc.
2399 void LyXText::postRowPaint(RowList::iterator rit, int start_y)
2401 if (refresh_status_ != REFRESH_NONE && refresh_y < start_y) {
2402 refresh_status_ = REFRESH_AREA;
2405 refresh_y = start_y;
2408 if (refresh_status_ == REFRESH_AREA)
2411 refresh_status_ = REFRESH_ROW;
2417 // We are an inset's lyxtext. Tell the top-level lyxtext
2418 // it needs to update the row we're in.
2419 LyXText * t = bv()->text;
2420 t->postRowPaint(t->cursorRow(), t->cursor.y() - t->cursorRow()->baseline());
2424 bool LyXText::isInInset() const
2426 // Sub-level has non-null bv owner and
2427 // non-null inset owner.
2428 return inset_owner != 0 && bv_owner != 0;
2432 int defaultRowHeight()
2434 LyXFont const font(LyXFont::ALL_SANE);
2435 return int(font_metrics::maxAscent(font)
2436 + font_metrics::maxDescent(font) * 1.5);