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 * ====================================================== */
15 #include "paragraph.h"
16 #include "frontends/LyXView.h"
17 #include "undo_funcs.h"
19 #include "bufferparams.h"
21 #include "BufferView.h"
22 #include "CutAndPaste.h"
23 #include "frontends/Painter.h"
24 #include "frontends/font_metrics.h"
28 #include "FloatList.h"
30 #include "ParagraphParameters.h"
32 #include "lyxrow_funcs.h"
33 #include "paragraph_funcs.h"
35 #include "insets/insetbibitem.h"
36 #include "insets/insetfloat.h"
38 #include "support/LAssert.h"
39 #include "support/textutils.h"
40 #include "support/lstrings.h"
42 #include "support/BoostFormat.h"
43 #include <boost/tuple/tuple.hpp>
53 LyXText::LyXText(BufferView * bv)
54 : height(0), width(0), anchor_row_offset_(0),
55 inset_owner(0), the_locking_inset(0), bv_owner(bv)
57 anchor_row_ = rows().end();
58 need_break_row = rows().end();
59 refresh_row = rows().end();
65 LyXText::LyXText(BufferView * bv, InsetText * inset)
66 : height(0), width(0), anchor_row_offset_(0),
67 inset_owner(inset), the_locking_inset(0), bv_owner(bv)
69 anchor_row_ = rows().end();
70 need_break_row = rows().end();
71 refresh_row = rows().end();
77 void LyXText::init(BufferView * bview, bool reinit)
81 need_break_row = rows().end();
85 } else if (!rowlist_.empty())
88 ParagraphList::iterator pit = ownerParagraphs().begin();
89 ParagraphList::iterator end = ownerParagraphs().end();
91 current_font = getFont(bview->buffer(), pit, 0);
93 for (; pit != end; ++pit) {
94 insertParagraph(pit, rowlist_.end());
96 setCursorIntern(rowlist_.begin()->par(), 0);
97 selection.cursor = cursor;
103 // Gets the fully instantiated font at a given position in a paragraph
104 // Basically the same routine as Paragraph::getFont() in paragraph.C.
105 // The difference is that this one is used for displaying, and thus we
106 // are allowed to make cosmetic improvements. For instance make footnotes
108 // If position is -1, we get the layout font of the paragraph.
109 // If position is -2, we get the font of the manual label of the paragraph.
110 LyXFont const LyXText::getFont(Buffer const * buf, ParagraphList::iterator pit,
113 lyx::Assert(pos >= 0);
115 LyXLayout_ptr const & layout = pit->layout();
117 // We specialize the 95% common case:
118 if (!pit->getDepth()) {
119 if (layout->labeltype == LABEL_MANUAL
120 && pos < pit->beginningOfBody()) {
122 LyXFont f = pit->getFontSettings(buf->params, pos);
124 pit->inInset()->getDrawFont(f);
125 return f.realize(layout->reslabelfont);
127 LyXFont f = pit->getFontSettings(buf->params, pos);
129 pit->inInset()->getDrawFont(f);
130 return f.realize(layout->resfont);
134 // The uncommon case need not be optimized as much
138 if (pos < pit->beginningOfBody()) {
140 layoutfont = layout->labelfont;
143 layoutfont = layout->font;
146 LyXFont tmpfont = pit->getFontSettings(buf->params, pos);
147 tmpfont.realize(layoutfont);
150 pit->inInset()->getDrawFont(tmpfont);
152 // Realize with the fonts of lesser depth.
153 tmpfont.realize(outerFont(pit, ownerParagraphs()));
155 return realizeFont(tmpfont, buf->params);
159 LyXFont const LyXText::getLayoutFont(Buffer const * buf,
160 ParagraphList::iterator pit) const
162 LyXLayout_ptr const & layout = pit->layout();
164 if (!pit->getDepth()) {
165 return layout->resfont;
168 LyXFont font(layout->font);
169 // Realize with the fonts of lesser depth.
170 font.realize(outerFont(pit, ownerParagraphs()));
172 return realizeFont(font, buf->params);
176 LyXFont const LyXText::getLabelFont(Buffer const * buf,
177 ParagraphList::iterator pit) const
179 LyXLayout_ptr const & layout = pit->layout();
181 if (!pit->getDepth()) {
182 return layout->reslabelfont;
185 LyXFont font(layout->labelfont);
186 // Realize with the fonts of lesser depth.
187 font.realize(outerFont(pit, ownerParagraphs()));
189 return realizeFont(layout->labelfont, buf->params);
193 void LyXText::setCharFont(ParagraphList::iterator pit,
194 pos_type pos, LyXFont const & fnt,
197 Buffer const * buf = bv()->buffer();
198 LyXFont font = getFont(buf, pit, pos);
199 font.update(fnt, buf->params.language, toggleall);
200 // Let the insets convert their font
201 if (pit->isInset(pos)) {
202 Inset * inset = pit->getInset(pos);
203 if (isEditableInset(inset)) {
204 UpdatableInset * uinset =
205 static_cast<UpdatableInset *>(inset);
206 uinset->setFont(bv(), fnt, toggleall, true);
210 // Plug thru to version below:
211 setCharFont(buf, pit, pos, font);
215 void LyXText::setCharFont(Buffer const * buf, ParagraphList::iterator pit,
216 pos_type pos, LyXFont const & fnt)
220 LyXTextClass const & tclass = buf->params.getLyXTextClass();
221 LyXLayout_ptr const & layout = pit->layout();
223 // Get concrete layout font to reduce against
226 if (pos < pit->beginningOfBody())
227 layoutfont = layout->labelfont;
229 layoutfont = layout->font;
231 // Realize against environment font information
232 if (pit->getDepth()) {
233 ParagraphList::iterator tp = pit;
234 while (!layoutfont.resolved() &&
235 tp != ownerParagraphs().end() &&
237 tp = outerHook(tp, ownerParagraphs());
238 if (tp != ownerParagraphs().end())
239 layoutfont.realize(tp->layout()->font);
243 layoutfont.realize(tclass.defaultfont());
245 // Now, reduce font against full layout font
246 font.reduce(layoutfont);
248 pit->setFont(pos, font);
252 // removes the row and reset the touched counters
253 void LyXText::removeRow(RowList::iterator rit)
255 /* FIXME: when we cache the bview, this should just
256 * become a postPaint(), I think */
257 if (refresh_row == rit) {
258 if (rit == rows().begin())
259 refresh_row = boost::next(rit);
261 refresh_row = boost::prior(rit);
263 // what about refresh_y
266 if (anchor_row_ == rit) {
267 if (rit != rows().begin()) {
268 anchor_row_ = boost::prior(rit);
269 anchor_row_offset_ += anchor_row_->height();
271 anchor_row_ = boost::next(rit);
272 anchor_row_offset_ -= rit->height();
276 // the text becomes smaller
277 height -= rit->height();
283 // remove all following rows of the paragraph of the specified row.
284 void LyXText::removeParagraph(RowList::iterator rit)
286 ParagraphList::iterator tmppit = rit->par();
289 while (rit != rows().end() && rit->par() == tmppit) {
290 RowList::iterator tmprit = boost::next(rit);
297 void LyXText::insertParagraph(ParagraphList::iterator pit,
298 RowList::iterator rowit)
300 // insert a new row, starting at position 0
302 RowList::iterator rit = rowlist_.insert(rowit, newrow);
304 // and now append the whole paragraph before the new row
305 appendParagraph(rit);
309 Inset * LyXText::getInset() const
311 ParagraphList::iterator pit = cursor.par();
312 pos_type const pos = cursor.pos();
314 if (pos < pit->size() && pit->isInset(pos)) {
315 return pit->getInset(pos);
321 void LyXText::toggleInset()
323 Inset * inset = getInset();
324 // is there an editable inset at cursor position?
325 if (!isEditableInset(inset)) {
326 // No, try to see if we are inside a collapsable inset
327 if (inset_owner && inset_owner->owner()
328 && inset_owner->owner()->isOpen()) {
329 bv()->unlockInset(static_cast<UpdatableInset *>(inset_owner->owner()));
330 inset_owner->owner()->close(bv());
331 bv()->getLyXText()->cursorRight(bv());
335 //bv()->owner()->message(inset->editMessage());
337 // do we want to keep this?? (JMarc)
338 if (!isHighlyEditableInset(inset))
339 setCursorParUndo(bv());
341 if (inset->isOpen()) {
347 bv()->updateInset(inset);
351 /* used in setlayout */
352 // Asger is not sure we want to do this...
353 void LyXText::makeFontEntriesLayoutSpecific(Buffer const & buf,
356 LyXLayout_ptr const & layout = par.layout();
357 pos_type const psize = par.size();
360 for (pos_type pos = 0; pos < psize; ++pos) {
361 if (pos < par.beginningOfBody())
362 layoutfont = layout->labelfont;
364 layoutfont = layout->font;
366 LyXFont tmpfont = par.getFontSettings(buf.params, pos);
367 tmpfont.reduce(layoutfont);
368 par.setFont(pos, tmpfont);
373 ParagraphList::iterator
374 LyXText::setLayout(LyXCursor & cur, LyXCursor & sstart_cur,
375 LyXCursor & send_cur,
376 string const & layout)
378 ParagraphList::iterator endpit = boost::next(send_cur.par());
379 ParagraphList::iterator undoendpit = endpit;
380 ParagraphList::iterator pars_end = ownerParagraphs().end();
382 if (endpit != pars_end && endpit->getDepth()) {
383 while (endpit != pars_end && endpit->getDepth()) {
387 } else if (endpit != pars_end) {
388 // because of parindents etc.
392 setUndo(bv(), Undo::EDIT, sstart_cur.par(), undoendpit);
394 // ok we have a selection. This is always between sstart_cur
395 // and sel_end cursor
397 ParagraphList::iterator pit = sstart_cur.par();
398 ParagraphList::iterator epit = boost::next(send_cur.par());
400 LyXLayout_ptr const & lyxlayout =
401 bv()->buffer()->params.getLyXTextClass()[layout];
404 pit->applyLayout(lyxlayout);
405 makeFontEntriesLayoutSpecific(*bv()->buffer(), *pit);
406 ParagraphList::iterator fppit = pit;
407 fppit->params().spaceTop(lyxlayout->fill_top ?
408 VSpace(VSpace::VFILL)
409 : VSpace(VSpace::NONE));
410 fppit->params().spaceBottom(lyxlayout->fill_bottom ?
411 VSpace(VSpace::VFILL)
412 : VSpace(VSpace::NONE));
413 if (lyxlayout->margintype == MARGIN_MANUAL)
414 pit->setLabelWidthString(lyxlayout->labelstring());
417 } while (pit != epit);
423 // set layout over selection and make a total rebreak of those paragraphs
424 void LyXText::setLayout(string const & layout)
426 LyXCursor tmpcursor = cursor; /* store the current cursor */
428 // if there is no selection just set the layout
429 // of the current paragraph */
430 if (!selection.set()) {
431 selection.start = cursor; // dummy selection
432 selection.end = cursor;
434 ParagraphList::iterator endpit = setLayout(cursor, selection.start,
435 selection.end, layout);
436 redoParagraphs(selection.start, endpit);
438 // we have to reset the selection, because the
439 // geometry could have changed
440 setCursor(selection.start.par(),
441 selection.start.pos(), false);
442 selection.cursor = cursor;
443 setCursor(selection.end.par(), selection.end.pos(), false);
447 setCursor(tmpcursor.par(), tmpcursor.pos(), true);
451 bool LyXText::changeDepth(bv_funcs::DEPTH_CHANGE type, bool test_only)
453 ParagraphList::iterator pit(cursor.par());
454 ParagraphList::iterator end(cursor.par());
455 ParagraphList::iterator start = pit;
457 if (selection.set()) {
458 pit = selection.start.par();
459 end = selection.end.par();
463 ParagraphList::iterator pastend = boost::next(end);
466 setUndo(bv(), Undo::EDIT, start, pastend);
468 bool changed = false;
470 int prev_after_depth = 0;
471 #warning parlist ... could be nicer ?
472 if (start != ownerParagraphs().begin()) {
473 prev_after_depth = boost::prior(start)->getMaxDepthAfter();
477 int const depth = pit->params().depth();
478 if (type == bv_funcs::INC_DEPTH) {
479 if (depth < prev_after_depth
480 && pit->layout()->labeltype != LABEL_BIBLIO) {
483 pit->params().depth(depth + 1);
490 pit->params().depth(depth - 1);
493 prev_after_depth = pit->getMaxDepthAfter();
505 // Wow, redoParagraphs is stupid.
507 setCursor(tmpcursor, &(*start), 0);
509 //redoParagraphs(tmpcursor, &(*pastend));
510 redoParagraphs(tmpcursor, &(*pastend));
512 // We need to actually move the text->cursor. I don't
513 // understand why ...
516 // we have to reset the visual selection because the
517 // geometry could have changed
518 if (selection.set()) {
519 setCursor(selection.start.par(), selection.start.pos());
520 selection.cursor = cursor;
521 setCursor(selection.end.par(), selection.end.pos());
524 // this handles the counter labels, and also fixes up
525 // depth values for follow-on (child) paragraphs
529 setCursor(tmpcursor.par(), tmpcursor.pos());
535 // set font over selection and make a total rebreak of those paragraphs
536 void LyXText::setFont(LyXFont const & font, bool toggleall)
538 // if there is no selection just set the current_font
539 if (!selection.set()) {
540 // Determine basis font
542 if (cursor.pos() < cursor.par()->beginningOfBody()) {
543 layoutfont = getLabelFont(bv()->buffer(),
546 layoutfont = getLayoutFont(bv()->buffer(),
549 // Update current font
550 real_current_font.update(font,
551 bv()->buffer()->params.language,
554 // Reduce to implicit settings
555 current_font = real_current_font;
556 current_font.reduce(layoutfont);
557 // And resolve it completely
558 real_current_font.realize(layoutfont);
563 LyXCursor tmpcursor = cursor; // store the current cursor
565 // ok we have a selection. This is always between sel_start_cursor
566 // and sel_end cursor
568 setUndo(bv(), Undo::EDIT,
569 selection.start.par(), boost::next(selection.end.par()));
571 cursor = selection.start;
572 while (cursor.par() != selection.end.par() ||
573 cursor.pos() < selection.end.pos())
575 if (cursor.pos() < cursor.par()->size()) {
576 // an open footnote should behave like a closed one
577 setCharFont(cursor.par(), cursor.pos(),
579 cursor.pos(cursor.pos() + 1);
582 cursor.par(boost::next(cursor.par()));
587 redoParagraphs(selection.start, boost::next(selection.end.par()));
589 // we have to reset the selection, because the
590 // geometry could have changed, but we keep
591 // it for user convenience
592 setCursor(selection.start.par(), selection.start.pos());
593 selection.cursor = cursor;
594 setCursor(selection.end.par(), selection.end.pos());
596 setCursor(tmpcursor.par(), tmpcursor.pos(), true,
597 tmpcursor.boundary());
601 void LyXText::redoHeightOfParagraph()
603 RowList::iterator tmprow = cursor.row();
604 int y = cursor.y() - tmprow->baseline();
606 setHeightOfRow(tmprow);
608 while (tmprow != rows().begin()
609 && boost::prior(tmprow)->par() == tmprow->par()) {
611 y -= tmprow->height();
612 setHeightOfRow(tmprow);
617 setCursor(cursor.par(), cursor.pos(), false, cursor.boundary());
621 void LyXText::redoDrawingOfParagraph(LyXCursor const & cur)
623 RowList::iterator tmprow = cur.row();
625 int y = cur.y() - tmprow->baseline();
626 setHeightOfRow(tmprow);
628 while (tmprow != rows().begin()
629 && boost::prior(tmprow)->par() == tmprow->par()) {
631 y -= tmprow->height();
635 setCursor(cur.par(), cur.pos());
639 // deletes and inserts again all paragaphs between the cursor
640 // and the specified par
641 // This function is needed after SetLayout and SetFont etc.
642 void LyXText::redoParagraphs(LyXCursor const & cur,
643 ParagraphList::iterator endpit)
645 RowList::iterator tmprit = cur.row();
646 int y = cur.y() - tmprit->baseline();
648 ParagraphList::iterator first_phys_pit;
649 RowList::iterator prevrit;
650 if (tmprit == rows().begin()) {
651 // A trick/hack for UNDO.
652 // This is needed because in an UNDO/REDO we could have
653 // changed the ownerParagrah() so the paragraph inside
654 // the row is NOT my really first par anymore.
655 // Got it Lars ;) (Jug 20011206)
656 first_phys_pit = ownerParagraphs().begin();
657 prevrit = rows().end();
659 first_phys_pit = tmprit->par();
660 while (tmprit != rows().begin()
661 && boost::prior(tmprit)->par() == first_phys_pit)
664 y -= tmprit->height();
666 prevrit = boost::prior(tmprit);
670 while (tmprit != rows().end() && tmprit->par() != endpit) {
671 RowList::iterator tmprit2 = tmprit++;
675 // Reinsert the paragraphs.
676 ParagraphList::iterator tmppit = first_phys_pit;
678 while (tmppit != ownerParagraphs().end()) {
679 insertParagraph(tmppit, tmprit);
680 while (tmprit != rows().end()
681 && tmprit->par() == tmppit) {
685 if (tmppit == endpit)
688 if (prevrit != rows().end()) {
689 setHeightOfRow(prevrit);
690 const_cast<LyXText *>(this)->postPaint(y - prevrit->height());
692 setHeightOfRow(rows().begin());
693 const_cast<LyXText *>(this)->postPaint(0);
695 if (tmprit != rows().end())
696 setHeightOfRow(tmprit);
702 void LyXText::fullRebreak()
704 if (rows().empty()) {
709 RowList::iterator rows_end = rows().end();
711 if (need_break_row != rows_end) {
712 breakAgain(need_break_row);
713 need_break_row = rows_end;
719 // important for the screen
722 // the cursor set functions have a special mechanism. When they
723 // realize, that you left an empty paragraph, they will delete it.
724 // They also delete the corresponding row
726 // need the selection cursor:
727 void LyXText::setSelection()
729 bool const lsel = selection.set();
731 if (!selection.set()) {
732 last_sel_cursor = selection.cursor;
733 selection.start = selection.cursor;
734 selection.end = selection.cursor;
739 // first the toggling area
740 if (cursor.y() < last_sel_cursor.y()
741 || (cursor.y() == last_sel_cursor.y()
742 && cursor.x() < last_sel_cursor.x())) {
743 toggle_end_cursor = last_sel_cursor;
744 toggle_cursor = cursor;
746 toggle_end_cursor = cursor;
747 toggle_cursor = last_sel_cursor;
750 last_sel_cursor = cursor;
752 // and now the whole selection
754 if (selection.cursor.par() == cursor.par())
755 if (selection.cursor.pos() < cursor.pos()) {
756 selection.end = cursor;
757 selection.start = selection.cursor;
759 selection.end = selection.cursor;
760 selection.start = cursor;
762 else if (selection.cursor.y() < cursor.y() ||
763 (selection.cursor.y() == cursor.y()
764 && selection.cursor.x() < cursor.x())) {
765 selection.end = cursor;
766 selection.start = selection.cursor;
769 selection.end = selection.cursor;
770 selection.start = cursor;
773 // a selection with no contents is not a selection
774 if (selection.start.par() == selection.end.par() &&
775 selection.start.pos() == selection.end.pos())
776 selection.set(false);
778 if (inset_owner && (selection.set() || lsel))
779 inset_owner->setUpdateStatus(bv(), InsetText::SELECTION);
783 string const LyXText::selectionAsString(Buffer const * buffer,
786 if (!selection.set()) return string();
788 // should be const ...
789 ParagraphList::iterator startpit = selection.start.par();
790 ParagraphList::iterator endpit = selection.end.par();
791 pos_type const startpos(selection.start.pos());
792 pos_type const endpos(selection.end.pos());
794 if (startpit == endpit) {
795 return startpit->asString(buffer, startpos, endpos, label);
800 // First paragraph in selection
801 result += startpit->asString(buffer, startpos, startpit->size(), label) + "\n\n";
803 // The paragraphs in between (if any)
804 ParagraphList::iterator pit = boost::next(startpit);
805 for (; pit != endpit; ++pit) {
806 result += pit->asString(buffer, 0, pit->size(), label) + "\n\n";
809 // Last paragraph in selection
810 result += endpit->asString(buffer, 0, endpos, label);
816 void LyXText::clearSelection()
818 selection.set(false);
819 selection.mark(false);
820 last_sel_cursor = selection.end = selection.start = selection.cursor = cursor;
821 // reset this in the bv_owner!
822 if (bv_owner && bv_owner->text)
823 bv_owner->text->xsel_cache.set(false);
827 void LyXText::cursorHome()
829 setCursor(cursor.par(), cursor.row()->pos());
833 void LyXText::cursorEnd()
835 if (cursor.par()->empty())
838 RowList::iterator rit = cursor.row();
839 RowList::iterator next_rit = boost::next(rit);
840 ParagraphList::iterator pit = rit->par();
841 pos_type last_pos = lastPos(*this, rit);
843 if (next_rit == rows().end() || next_rit->par() != pit) {
847 (pit->getChar(last_pos) != ' ' && !pit->isNewline(last_pos))) {
852 setCursor(pit, last_pos);
856 void LyXText::cursorTop()
858 setCursor(ownerParagraphs().begin(), 0);
862 void LyXText::cursorBottom()
865 // This is how it should be:
866 // ParagraphList::iterator lastpit = boost::prior(ownerParagraphs().end());
867 ParagraphList::iterator lastpit = &ownerParagraphs().back();
868 int pos = lastpit->size();
869 setCursor(lastpit, pos);
873 void LyXText::toggleFree(LyXFont const & font, bool toggleall)
875 // If the mask is completely neutral, tell user
876 if (font == LyXFont(LyXFont::ALL_IGNORE)) {
877 // Could only happen with user style
878 bv()->owner()->message(_("No font change defined. Use Character under the Layout menu to define font change."));
882 // Try implicit word selection
883 // If there is a change in the language the implicit word selection
885 LyXCursor resetCursor = cursor;
886 bool implicitSelection = (font.language() == ignore_language
887 && font.number() == LyXFont::IGNORE)
888 ? selectWordWhenUnderCursor(WHOLE_WORD_STRICT) : false;
891 setFont(font, toggleall);
893 // Implicit selections are cleared afterwards
894 //and cursor is set to the original position.
895 if (implicitSelection) {
897 cursor = resetCursor;
898 setCursor(cursor.par(), cursor.pos());
899 selection.cursor = cursor;
902 inset_owner->setUpdateStatus(bv(), InsetText::CURSOR_PAR);
906 string LyXText::getStringToIndex()
908 // Try implicit word selection
909 // If there is a change in the language the implicit word selection
911 LyXCursor const reset_cursor = cursor;
912 bool const implicitSelection = selectWordWhenUnderCursor(PREVIOUS_WORD);
915 if (!selection.set())
916 bv()->owner()->message(_("Nothing to index!"));
917 else if (selection.start.par() != selection.end.par())
918 bv()->owner()->message(_("Cannot index more than one paragraph!"));
920 idxstring = selectionAsString(bv()->buffer(), false);
922 // Reset cursors to their original position.
923 cursor = reset_cursor;
924 setCursor(cursor.par(), cursor.pos());
925 selection.cursor = cursor;
927 // Clear the implicit selection.
928 if (implicitSelection)
935 // the DTP switches for paragraphs. LyX will store them in the first
936 // physicla paragraph. When a paragraph is broken, the top settings rest,
937 // the bottom settings are given to the new one. So I can make shure,
938 // they do not duplicate themself and you cannnot make dirty things with
941 void LyXText::setParagraph(bool line_top, bool line_bottom,
942 bool pagebreak_top, bool pagebreak_bottom,
943 VSpace const & space_top,
944 VSpace const & space_bottom,
945 Spacing const & spacing,
947 string const & labelwidthstring,
950 LyXCursor tmpcursor = cursor;
951 if (!selection.set()) {
952 selection.start = cursor;
953 selection.end = cursor;
956 // make sure that the depth behind the selection are restored, too
957 ParagraphList::iterator endpit = boost::next(selection.end.par());
958 ParagraphList::iterator undoendpit = endpit;
959 ParagraphList::iterator pars_end = ownerParagraphs().end();
961 if (endpit != pars_end && endpit->getDepth()) {
962 while (endpit != pars_end && endpit->getDepth()) {
966 } else if (endpit!= pars_end) {
967 // because of parindents etc.
971 setUndo(bv(), Undo::EDIT, selection.start.par(), undoendpit);
974 ParagraphList::iterator tmppit = selection.end.par();
976 while (tmppit != boost::prior(selection.start.par())) {
977 setCursor(tmppit, 0);
978 postPaint(cursor.y() - cursor.row()->baseline());
980 ParagraphList::iterator pit = cursor.par();
981 ParagraphParameters & params = pit->params();
983 params.lineTop(line_top);
984 params.lineBottom(line_bottom);
985 params.pagebreakTop(pagebreak_top);
986 params.pagebreakBottom(pagebreak_bottom);
987 params.spaceTop(space_top);
988 params.spaceBottom(space_bottom);
989 params.spacing(spacing);
990 // does the layout allow the new alignment?
991 LyXLayout_ptr const & layout = pit->layout();
993 if (align == LYX_ALIGN_LAYOUT)
994 align = layout->align;
995 if (align & layout->alignpossible) {
996 if (align == layout->align)
997 params.align(LYX_ALIGN_LAYOUT);
1001 pit->setLabelWidthString(labelwidthstring);
1002 params.noindent(noindent);
1003 tmppit = boost::prior(pit);
1006 redoParagraphs(selection.start, endpit);
1009 setCursor(selection.start.par(), selection.start.pos());
1010 selection.cursor = cursor;
1011 setCursor(selection.end.par(), selection.end.pos());
1013 setCursor(tmpcursor.par(), tmpcursor.pos());
1015 bv()->updateInset(inset_owner);
1019 // set the counter of a paragraph. This includes the labels
1020 void LyXText::setCounter(Buffer const * buf, ParagraphList::iterator pit)
1022 LyXTextClass const & textclass = buf->params.getLyXTextClass();
1023 LyXLayout_ptr const & layout = pit->layout();
1025 if (pit != ownerParagraphs().begin()) {
1027 pit->params().appendix(boost::prior(pit)->params().appendix());
1028 if (!pit->params().appendix() &&
1029 pit->params().startOfAppendix()) {
1030 pit->params().appendix(true);
1031 textclass.counters().reset();
1033 pit->enumdepth = boost::prior(pit)->enumdepth;
1034 pit->itemdepth = boost::prior(pit)->itemdepth;
1036 pit->params().appendix(pit->params().startOfAppendix());
1041 /* Maybe we have to increment the enumeration depth.
1042 * BUT, enumeration in a footnote is considered in isolation from its
1043 * surrounding paragraph so don't increment if this is the
1044 * first line of the footnote
1045 * AND, bibliographies can't have their depth changed ie. they
1046 * are always of depth 0
1048 if (pit != ownerParagraphs().begin()
1049 && boost::prior(pit)->getDepth() < pit->getDepth()
1050 && boost::prior(pit)->layout()->labeltype == LABEL_COUNTER_ENUMI
1051 && pit->enumdepth < 3
1052 && layout->labeltype != LABEL_BIBLIO) {
1056 // Maybe we have to decrement the enumeration depth, see note above
1057 if (pit != ownerParagraphs().begin()
1058 && boost::prior(pit)->getDepth() > pit->getDepth()
1059 && layout->labeltype != LABEL_BIBLIO) {
1060 pit->enumdepth = depthHook(pit, ownerParagraphs(),
1061 pit->getDepth())->enumdepth;
1064 if (!pit->params().labelString().empty()) {
1065 pit->params().labelString(string());
1068 if (layout->margintype == MARGIN_MANUAL) {
1069 if (pit->params().labelWidthString().empty()) {
1070 pit->setLabelWidthString(layout->labelstring());
1073 pit->setLabelWidthString(string());
1076 // is it a layout that has an automatic label?
1077 if (layout->labeltype >= LABEL_COUNTER_CHAPTER) {
1078 int const i = layout->labeltype - LABEL_COUNTER_CHAPTER;
1082 if (i >= 0 && i <= buf->params.secnumdepth) {
1086 textclass.counters().step(layout->latexname());
1088 // Is there a label? Useful for Chapter layout
1089 if (!pit->params().appendix()) {
1090 s << buf->B_(layout->labelstring());
1092 s << buf->B_(layout->labelstring_appendix());
1095 // Use of an integer is here less than elegant. For now.
1096 int head = textclass.maxcounter() - LABEL_COUNTER_CHAPTER;
1097 if (!pit->params().appendix()) {
1098 numbertype = "sectioning";
1100 numbertype = "appendix";
1101 if (pit->isRightToLeftPar(buf->params))
1102 langtype = "hebrew";
1108 << textclass.counters()
1109 .numberLabel(layout->latexname(),
1110 numbertype, langtype, head);
1112 pit->params().labelString(STRCONV(s.str()));
1114 // reset enum counters
1115 textclass.counters().reset("enum");
1116 } else if (layout->labeltype < LABEL_COUNTER_ENUMI) {
1117 textclass.counters().reset("enum");
1118 } else if (layout->labeltype == LABEL_COUNTER_ENUMI) {
1120 // Yes I know this is a really, really! bad solution
1122 string enumcounter("enum");
1124 switch (pit->enumdepth) {
1133 enumcounter += "iv";
1136 // not a valid enumdepth...
1140 textclass.counters().step(enumcounter);
1142 s << textclass.counters()
1143 .numberLabel(enumcounter, "enumeration");
1144 pit->params().labelString(STRCONV(s.str()));
1146 } else if (layout->labeltype == LABEL_BIBLIO) {// ale970302
1147 textclass.counters().step("bibitem");
1148 int number = textclass.counters().value("bibitem");
1149 if (pit->bibitem()) {
1150 pit->bibitem()->setCounter(number);
1151 pit->params().labelString(layout->labelstring());
1153 // In biblio should't be following counters but...
1155 string s = buf->B_(layout->labelstring());
1157 // the caption hack:
1158 if (layout->labeltype == LABEL_SENSITIVE) {
1159 ParagraphList::iterator tmppit = pit;
1162 while (tmppit != ownerParagraphs().end() &&
1164 // the single '=' is intended below
1165 && (in = tmppit->inInset()->owner())) {
1166 if (in->lyxCode() == Inset::FLOAT_CODE ||
1167 in->lyxCode() == Inset::WRAP_CODE) {
1171 tmppit = in->parOwner();
1177 = textclass.floats().getType(static_cast<InsetFloat*>(in)->type());
1179 textclass.counters().step(fl.type());
1181 // Doesn't work... yet.
1182 #if USE_BOOST_FORMAT
1183 s = boost::io::str(boost::format(_("%1$s #:")) % buf->B_(fl.name()));
1184 // s << boost::format(_("%1$s %1$d:")
1186 // % buf->counters().value(fl.name());
1189 //o << fl.name() << ' ' << buf->counters().value(fl.name()) << ":";
1190 o << buf->B_(fl.name()) << " #:";
1191 s = STRCONV(o.str());
1194 // par->SetLayout(0);
1195 // s = layout->labelstring;
1196 s = _("Senseless: ");
1199 pit->params().labelString(s);
1201 // reset the enumeration counter. They are always reset
1202 // when there is any other layout between
1203 // Just fall-through between the cases so that all
1204 // enum counters deeper than enumdepth is also reset.
1205 switch (pit->enumdepth) {
1207 textclass.counters().reset("enumi");
1209 textclass.counters().reset("enumii");
1211 textclass.counters().reset("enumiii");
1213 textclass.counters().reset("enumiv");
1219 // Updates all counters. Paragraphs with changed label string will be rebroken
1220 void LyXText::updateCounters()
1222 RowList::iterator rowit = rows().begin();
1223 ParagraphList::iterator pit = rowit->par();
1225 // CHECK if this is really needed. (Lgb)
1226 bv()->buffer()->params.getLyXTextClass().counters().reset();
1228 for (; pit != ownerParagraphs().end(); ++pit) {
1229 while (rowit->par() != pit)
1232 string const oldLabel = pit->params().labelString();
1235 if (pit != ownerParagraphs().begin())
1236 maxdepth = boost::prior(pit)->getMaxDepthAfter();
1238 if (pit->params().depth() > maxdepth)
1239 pit->params().depth(maxdepth);
1241 // setCounter can potentially change the labelString.
1242 setCounter(bv()->buffer(), pit);
1244 string const & newLabel = pit->params().labelString();
1246 if (oldLabel.empty() && !newLabel.empty()) {
1247 removeParagraph(rowit);
1248 appendParagraph(rowit);
1254 void LyXText::insertInset(Inset * inset)
1256 if (!cursor.par()->insetAllowed(inset->lyxCode()))
1258 setUndo(bv(), Undo::FINISH, cursor.par(),
1259 boost::next(cursor.par()));
1261 cursor.par()->insertInset(cursor.pos(), inset);
1262 // Just to rebreak and refresh correctly.
1263 // The character will not be inserted a second time
1264 insertChar(Paragraph::META_INSET);
1265 // If we enter a highly editable inset the cursor should be to before
1266 // the inset. This couldn't happen before as Undo was not handled inside
1267 // inset now after the Undo LyX tries to call inset->Edit(...) again
1268 // and cannot do this as the cursor is behind the inset and GetInset
1269 // does not return the inset!
1270 if (isHighlyEditableInset(inset)) {
1277 void LyXText::cutSelection(bool doclear, bool realcut)
1279 // Stuff what we got on the clipboard. Even if there is no selection.
1281 // There is a problem with having the stuffing here in that the
1282 // larger the selection the slower LyX will get. This can be
1283 // solved by running the line below only when the selection has
1284 // finished. The solution used currently just works, to make it
1285 // faster we need to be more clever and probably also have more
1286 // calls to stuffClipboard. (Lgb)
1287 bv()->stuffClipboard(selectionAsString(bv()->buffer(), true));
1289 // This doesn't make sense, if there is no selection
1290 if (!selection.set())
1293 // OK, we have a selection. This is always between selection.start
1294 // and selection.end
1296 // make sure that the depth behind the selection are restored, too
1297 ParagraphList::iterator endpit = boost::next(selection.end.par());
1298 ParagraphList::iterator undoendpit = endpit;
1299 ParagraphList::iterator pars_end = ownerParagraphs().end();
1301 if (endpit != pars_end && endpit->getDepth()) {
1302 while (endpit != pars_end && endpit->getDepth()) {
1304 undoendpit = endpit;
1306 } else if (endpit != pars_end) {
1307 // because of parindents etc.
1311 setUndo(bv(), Undo::DELETE, selection.start.par(), undoendpit);
1314 endpit = selection.end.par();
1315 int endpos = selection.end.pos();
1317 boost::tie(endpit, endpos) = realcut ?
1318 CutAndPaste::cutSelection(ownerParagraphs(),
1319 selection.start.par(), endpit,
1320 selection.start.pos(), endpos,
1321 bv()->buffer()->params.textclass,
1323 : CutAndPaste::eraseSelection(ownerParagraphs(),
1324 selection.start.par(), endpit,
1325 selection.start.pos(), endpos,
1327 // sometimes necessary
1329 selection.start.par()->stripLeadingSpaces();
1331 redoParagraphs(selection.start, boost::next(endpit));
1332 #warning FIXME latent bug
1333 // endpit will be invalidated on redoParagraphs once ParagraphList
1334 // becomes a std::list? There are maybe other places on which this
1335 // can happend? (Ab)
1336 // cutSelection can invalidate the cursor so we need to set
1338 // we prefer the end for when tracking changes
1342 // need a valid cursor. (Lgb)
1345 setCursor(cursor.par(), cursor.pos());
1346 selection.cursor = cursor;
1351 void LyXText::copySelection()
1353 // stuff the selection onto the X clipboard, from an explicit copy request
1354 bv()->stuffClipboard(selectionAsString(bv()->buffer(), true));
1356 // this doesnt make sense, if there is no selection
1357 if (!selection.set())
1360 // ok we have a selection. This is always between selection.start
1361 // and sel_end cursor
1363 // copy behind a space if there is one
1364 while (selection.start.par()->size() > selection.start.pos()
1365 && selection.start.par()->isLineSeparator(selection.start.pos())
1366 && (selection.start.par() != selection.end.par()
1367 || selection.start.pos() < selection.end.pos()))
1368 selection.start.pos(selection.start.pos() + 1);
1370 CutAndPaste::copySelection(&*selection.start.par(),
1371 &*selection.end.par(),
1372 selection.start.pos(), selection.end.pos(),
1373 bv()->buffer()->params.textclass);
1377 void LyXText::pasteSelection()
1379 // this does not make sense, if there is nothing to paste
1380 if (!CutAndPaste::checkPastePossible())
1383 setUndo(bv(), Undo::INSERT,
1384 cursor.par(), boost::next(cursor.par()));
1387 ParagraphList::iterator actpit = cursor.par();
1388 int pos = cursor.pos();
1390 Paragraph * actpar = &*actpit;
1391 CutAndPaste::pasteSelection(&actpar, &endpar, pos,
1392 bv()->buffer()->params.textclass);
1394 redoParagraphs(cursor, endpar);
1396 setCursor(cursor.par(), cursor.pos());
1399 selection.cursor = cursor;
1400 setCursor(actpit, pos);
1406 void LyXText::setSelectionRange(lyx::pos_type length)
1411 selection.cursor = cursor;
1418 // simple replacing. The font of the first selected character is used
1419 void LyXText::replaceSelectionWithString(string const & str)
1421 setCursorParUndo(bv());
1424 if (!selection.set()) { // create a dummy selection
1425 selection.end = cursor;
1426 selection.start = cursor;
1429 // Get font setting before we cut
1430 pos_type pos = selection.end.pos();
1431 LyXFont const font = selection.start.par()
1432 ->getFontSettings(bv()->buffer()->params,
1433 selection.start.pos());
1435 // Insert the new string
1436 for (string::const_iterator cit = str.begin(); cit != str.end(); ++cit) {
1437 selection.end.par()->insertChar(pos, (*cit), font);
1441 // Cut the selection
1442 cutSelection(true, false);
1448 // needed to insert the selection
1449 void LyXText::insertStringAsLines(string const & str)
1451 ParagraphList::iterator pit = cursor.par();
1452 pos_type pos = cursor.pos();
1453 ParagraphList::iterator endpit = boost::next(cursor.par());
1455 setCursorParUndo(bv());
1457 // only to be sure, should not be neccessary
1460 bv()->buffer()->insertStringAsLines(pit, pos, current_font, str);
1462 redoParagraphs(cursor, endpit);
1463 setCursor(cursor.par(), cursor.pos());
1464 selection.cursor = cursor;
1465 setCursor(pit, pos);
1470 // turns double-CR to single CR, others where converted into one
1471 // blank. Then InsertStringAsLines is called
1472 void LyXText::insertStringAsParagraphs(string const & str)
1474 string linestr(str);
1475 bool newline_inserted = false;
1476 for (string::size_type i = 0; i < linestr.length(); ++i) {
1477 if (linestr[i] == '\n') {
1478 if (newline_inserted) {
1479 // we know that \r will be ignored by
1480 // InsertStringA. Of course, it is a dirty
1481 // trick, but it works...
1482 linestr[i - 1] = '\r';
1486 newline_inserted = true;
1488 } else if (IsPrintable(linestr[i])) {
1489 newline_inserted = false;
1492 insertStringAsLines(linestr);
1496 void LyXText::checkParagraph(ParagraphList::iterator pit, pos_type pos)
1498 LyXCursor tmpcursor;
1502 RowList::iterator row = getRow(pit, pos, y);
1503 RowList::iterator beg = rows().begin();
1505 // is there a break one row above
1507 && boost::prior(row)->par() == row->par()) {
1508 z = rowBreakPoint(*boost::prior(row));
1509 if (z >= row->pos()) {
1510 // set the dimensions of the row above
1511 y -= boost::prior(row)->height();
1514 breakAgain(boost::prior(row));
1516 // set the cursor again. Otherwise
1517 // dangling pointers are possible
1518 setCursor(cursor.par(), cursor.pos(),
1519 false, cursor.boundary());
1520 selection.cursor = cursor;
1525 int const tmpheight = row->height();
1526 pos_type const tmplast = lastPos(*this, row);
1529 if (row->height() == tmpheight && lastPos(*this, row) == tmplast) {
1530 postRowPaint(row, y);
1535 // check the special right address boxes
1536 if (pit->layout()->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1543 redoDrawingOfParagraph(tmpcursor);
1546 // set the cursor again. Otherwise dangling pointers are possible
1547 // also set the selection
1549 if (selection.set()) {
1551 setCursorIntern(selection.cursor.par(), selection.cursor.pos(),
1552 false, selection.cursor.boundary());
1553 selection.cursor = cursor;
1554 setCursorIntern(selection.start.par(),
1555 selection.start.pos(),
1556 false, selection.start.boundary());
1557 selection.start = cursor;
1558 setCursorIntern(selection.end.par(),
1559 selection.end.pos(),
1560 false, selection.end.boundary());
1561 selection.end = cursor;
1562 setCursorIntern(last_sel_cursor.par(),
1563 last_sel_cursor.pos(),
1564 false, last_sel_cursor.boundary());
1565 last_sel_cursor = cursor;
1568 setCursorIntern(cursor.par(), cursor.pos(),
1569 false, cursor.boundary());
1573 // returns false if inset wasn't found
1574 bool LyXText::updateInset(Inset * inset)
1576 // first check the current paragraph
1577 int pos = cursor.par()->getPositionOfInset(inset);
1579 checkParagraph(cursor.par(), pos);
1583 // check every paragraph
1585 ParagraphList::iterator par = ownerParagraphs().begin();
1586 ParagraphList::iterator end = ownerParagraphs().end();
1587 for (; par != end; ++par) {
1588 pos = par->getPositionOfInset(inset);
1590 checkParagraph(par, pos);
1599 bool LyXText::setCursor(ParagraphList::iterator pit,
1601 bool setfont, bool boundary)
1603 LyXCursor old_cursor = cursor;
1604 setCursorIntern(pit, pos, setfont, boundary);
1605 return deleteEmptyParagraphMechanism(old_cursor);
1609 void LyXText::setCursor(LyXCursor & cur, ParagraphList::iterator pit,
1610 pos_type pos, bool boundary)
1612 lyx::Assert(pit != ownerParagraphs().end());
1616 cur.boundary(boundary);
1618 // get the cursor y position in text
1620 RowList::iterator row = getRow(pit, pos, y);
1621 RowList::iterator beg = rows().begin();
1623 RowList::iterator old_row = row;
1625 // if we are before the first char of this row and are still in the
1626 // same paragraph and there is a previous row then put the cursor on
1627 // the end of the previous row
1628 cur.iy(y + row->baseline());
1631 boost::prior(row)->par() == row->par() &&
1632 pos < pit->size() &&
1633 pit->getChar(pos) == Paragraph::META_INSET) {
1634 Inset * ins = pit->getInset(pos);
1635 if (ins && (ins->needFullRow() || ins->display())) {
1642 // y is now the beginning of the cursor row
1643 y += row->baseline();
1644 // y is now the cursor baseline
1647 pos_type last = lastPrintablePos(*this, old_row);
1649 // None of these should happen, but we're scaredy-cats
1650 if (pos > pit->size()) {
1651 lyxerr << "dont like 1 please report" << endl;
1654 } else if (pos > last + 1) {
1655 lyxerr << "dont like 2 please report" << endl;
1656 // This shouldn't happen.
1659 } else if (pos < row->pos()) {
1660 lyxerr << "dont like 3 please report" << endl;
1665 // now get the cursors x position
1666 float x = getCursorX(row, pos, last, boundary);
1669 if (old_row != row) {
1670 x = getCursorX(old_row, pos, last, boundary);
1674 /* We take out this for the time being because 1) the redraw code is not
1675 prepared to this yet and 2) because some good policy has yet to be decided
1676 while editting: for instance how to act on rows being created/deleted
1680 //if the cursor is in a visible row, anchor to it
1682 if (topy < y && y < topy + bv()->workHeight())
1688 float LyXText::getCursorX(RowList::iterator rit,
1689 pos_type pos, pos_type last, bool boundary) const
1691 pos_type cursor_vpos = 0;
1693 float fill_separator;
1695 float fill_label_hfill;
1696 // This call HAS to be here because of the BidiTables!!!
1697 prepareToPrint(rit, x, fill_separator, fill_hfill,
1700 ParagraphList::iterator rit_par = rit->par();
1701 pos_type const rit_pos = rit->pos();
1704 cursor_vpos = rit_pos;
1705 else if (pos > last && !boundary)
1706 cursor_vpos = (rit_par->isRightToLeftPar(bv()->buffer()->params))
1707 ? rit_pos : last + 1;
1708 else if (pos > rit_pos && (pos > last || boundary))
1709 /// Place cursor after char at (logical) position pos - 1
1710 cursor_vpos = (bidi_level(pos - 1) % 2 == 0)
1711 ? log2vis(pos - 1) + 1 : log2vis(pos - 1);
1713 /// Place cursor before char at (logical) position pos
1714 cursor_vpos = (bidi_level(pos) % 2 == 0)
1715 ? log2vis(pos) : log2vis(pos) + 1;
1717 pos_type body_pos = rit_par->beginningOfBody();
1718 if ((body_pos > 0) &&
1719 ((body_pos - 1 > last) || !rit_par->isLineSeparator(body_pos - 1)))
1722 for (pos_type vpos = rit_pos; vpos < cursor_vpos; ++vpos) {
1723 pos_type pos = vis2log(vpos);
1724 if (body_pos > 0 && pos == body_pos - 1) {
1725 x += fill_label_hfill +
1726 font_metrics::width(
1727 rit_par->layout()->labelsep,
1728 getLabelFont(bv()->buffer(), rit_par));
1729 if (rit_par->isLineSeparator(body_pos - 1))
1730 x -= singleWidth(rit_par, body_pos - 1);
1733 if (hfillExpansion(*this, rit, pos)) {
1734 x += singleWidth(rit_par, pos);
1735 if (pos >= body_pos)
1738 x += fill_label_hfill;
1739 } else if (rit_par->isSeparator(pos)) {
1740 x += singleWidth(rit_par, pos);
1741 if (pos >= body_pos)
1742 x += fill_separator;
1744 x += singleWidth(rit_par, pos);
1750 void LyXText::setCursorIntern(ParagraphList::iterator pit,
1751 pos_type pos, bool setfont, bool boundary)
1753 InsetText * it = static_cast<InsetText *>(pit->inInset());
1755 if (it != inset_owner) {
1756 lyxerr[Debug::INSETS] << "InsetText is " << it
1758 << "inset_owner is "
1759 << inset_owner << endl;
1760 #ifdef WITH_WARNINGS
1761 #warning I believe this code is wrong. (Lgb)
1762 #warning Jürgen, have a look at this. (Lgb)
1763 #warning Hmmm, I guess you are right but we
1764 #warning should verify when this is needed
1766 // Jürgen, would you like to have a look?
1767 // I guess we need to move the outer cursor
1768 // and open and lock the inset (bla bla bla)
1769 // stuff I don't know... so can you have a look?
1771 // I moved the lyxerr stuff in here so we can see if
1772 // this is actually really needed and where!
1774 // it->getLyXText(bv())->setCursorIntern(bv(), par, pos, setfont, boundary);
1779 setCursor(cursor, pit, pos, boundary);
1785 void LyXText::setCurrentFont()
1787 pos_type pos = cursor.pos();
1788 ParagraphList::iterator pit = cursor.par();
1790 if (cursor.boundary() && pos > 0)
1794 if (pos == pit->size())
1796 else // potentional bug... BUG (Lgb)
1797 if (pit->isSeparator(pos)) {
1798 if (pos > cursor.row()->pos() &&
1799 bidi_level(pos) % 2 ==
1800 bidi_level(pos - 1) % 2)
1802 else if (pos + 1 < pit->size())
1808 pit->getFontSettings(bv()->buffer()->params, pos);
1809 real_current_font = getFont(bv()->buffer(), pit, pos);
1811 if (cursor.pos() == pit->size() &&
1812 isBoundary(bv()->buffer(), *pit, cursor.pos()) &&
1813 !cursor.boundary()) {
1814 Language const * lang =
1815 pit->getParLanguage(bv()->buffer()->params);
1816 current_font.setLanguage(lang);
1817 current_font.setNumber(LyXFont::OFF);
1818 real_current_font.setLanguage(lang);
1819 real_current_font.setNumber(LyXFont::OFF);
1824 // returns the column near the specified x-coordinate of the row
1825 // x is set to the real beginning of this column
1827 LyXText::getColumnNearX(RowList::iterator rit, int & x, bool & boundary) const
1830 float fill_separator;
1832 float fill_label_hfill;
1834 prepareToPrint(rit, tmpx, fill_separator,
1835 fill_hfill, fill_label_hfill);
1837 pos_type vc = rit->pos();
1838 pos_type last = lastPrintablePos(*this, rit);
1841 ParagraphList::iterator rit_par = rit->par();
1842 LyXLayout_ptr const & layout = rit->par()->layout();
1844 bool left_side = false;
1846 pos_type body_pos = rit_par->beginningOfBody();
1847 float last_tmpx = tmpx;
1850 (body_pos - 1 > last ||
1851 !rit_par->isLineSeparator(body_pos - 1)))
1854 // check for empty row
1855 if (!rit_par->size()) {
1860 while (vc <= last && tmpx <= x) {
1863 if (body_pos > 0 && c == body_pos - 1) {
1864 tmpx += fill_label_hfill +
1865 font_metrics::width(layout->labelsep,
1866 getLabelFont(bv()->buffer(), &*rit_par));
1867 if (rit_par->isLineSeparator(body_pos - 1))
1868 tmpx -= singleWidth(rit_par, body_pos - 1);
1871 if (hfillExpansion(*this, rit, c)) {
1872 tmpx += singleWidth(rit_par, c);
1876 tmpx += fill_label_hfill;
1877 } else if (rit_par->isSeparator(c)) {
1878 tmpx += singleWidth(rit_par, c);
1880 tmpx+= fill_separator;
1882 tmpx += singleWidth(rit_par, c);
1887 if ((tmpx + last_tmpx) / 2 > x) {
1892 if (vc > last + 1) // This shouldn't happen.
1896 // This (rtl_support test) is not needed, but gives
1897 // some speedup if rtl_support=false
1898 bool const lastrow = lyxrc.rtl_support &&
1899 (boost::next(rit) == rowlist_.end() ||
1900 boost::next(rit)->par() != rit_par);
1901 // If lastrow is false, we don't need to compute
1902 // the value of rtl.
1903 bool const rtl = (lastrow)
1904 ? rit_par->isRightToLeftPar(bv()->buffer()->params)
1907 ((rtl && left_side && vc == rit->pos() && x < tmpx - 5) ||
1908 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5)))
1910 else if (vc == rit->pos()) {
1912 if (bidi_level(c) % 2 == 1)
1915 c = vis2log(vc - 1);
1916 bool const rtl = (bidi_level(c) % 2 == 1);
1917 if (left_side == rtl) {
1919 boundary = isBoundary(bv()->buffer(), *rit_par, c);
1923 if (rit->pos() <= last && c > last
1924 && rit_par->isNewline(last)) {
1925 if (bidi_level(last) % 2 == 0)
1926 tmpx -= singleWidth(rit_par, last);
1928 tmpx += singleWidth(rit_par, last);
1938 void LyXText::setCursorFromCoordinates(int x, int y)
1940 LyXCursor old_cursor = cursor;
1942 setCursorFromCoordinates(cursor, x, y);
1944 deleteEmptyParagraphMechanism(old_cursor);
1951 * return true if the cursor given is at the end of a row,
1952 * and the next row is filled by an inset that spans an entire
1955 bool beforeFullRowInset(LyXText & lt, LyXCursor const & cur) {
1956 RowList::iterator row = cur.row();
1957 if (boost::next(row) == lt.rows().end())
1959 Row const & next = *boost::next(row);
1961 if (next.pos() != cur.pos() || next.par() != cur.par())
1964 if (cur.pos() == cur.par()->size()
1965 || !cur.par()->isInset(cur.pos()))
1967 Inset const * inset = cur.par()->getInset(cur.pos());
1968 if (inset->needFullRow() || inset->display())
1975 void LyXText::setCursorFromCoordinates(LyXCursor & cur, int x, int y)
1977 // Get the row first.
1979 RowList::iterator row = getRowNearY(y);
1981 pos_type const column = getColumnNearX(row, x, bound);
1982 cur.par(row->par());
1983 cur.pos(row->pos() + column);
1985 cur.y(y + row->baseline());
1988 if (beforeFullRowInset(*this, cur)) {
1989 pos_type last = lastPrintablePos(*this, row);
1990 float x = getCursorX(boost::next(row), cur.pos(), last, bound);
1992 cur.iy(y + row->height() + boost::next(row)->baseline());
1993 cur.irow(boost::next(row));
1999 cur.boundary(bound);
2003 void LyXText::cursorLeft(bool internal)
2005 if (cursor.pos() > 0) {
2006 bool boundary = cursor.boundary();
2007 setCursor(cursor.par(), cursor.pos() - 1, true, false);
2008 if (!internal && !boundary &&
2009 isBoundary(bv()->buffer(), *cursor.par(), cursor.pos() + 1))
2010 setCursor(cursor.par(), cursor.pos() + 1, true, true);
2011 } else if (cursor.par() != ownerParagraphs().begin()) { // steps into the above paragraph.
2012 ParagraphList::iterator pit = boost::prior(cursor.par());
2013 setCursor(pit, pit->size());
2018 void LyXText::cursorRight(bool internal)
2020 bool const at_end = (cursor.pos() == cursor.par()->size());
2021 bool const at_newline = !at_end &&
2022 cursor.par()->isNewline(cursor.pos());
2024 if (!internal && cursor.boundary() && !at_newline)
2025 setCursor(cursor.par(), cursor.pos(), true, false);
2027 setCursor(cursor.par(), cursor.pos() + 1, true, false);
2029 isBoundary(bv()->buffer(), *cursor.par(), cursor.pos()))
2030 setCursor(cursor.par(), cursor.pos(), true, true);
2031 } else if (boost::next(cursor.par()) != ownerParagraphs().end())
2032 setCursor(boost::next(cursor.par()), 0);
2036 void LyXText::cursorUp(bool selecting)
2039 int x = cursor.x_fix();
2040 int y = cursor.y() - cursor.row()->baseline() - 1;
2041 setCursorFromCoordinates(x, y);
2044 int y1 = cursor.iy() - topy;
2047 Inset * inset_hit = checkInsetHit(x, y1);
2048 if (inset_hit && isHighlyEditableInset(inset_hit)) {
2049 inset_hit->edit(bv(), x, y - (y2 - y1), mouse_button::none);
2053 setCursorFromCoordinates(bv(), cursor.x_fix(),
2054 cursor.y() - cursor.row()->baseline() - 1);
2059 void LyXText::cursorDown(bool selecting)
2062 int x = cursor.x_fix();
2063 int y = cursor.y() - cursor.row()->baseline() +
2064 cursor.row()->height() + 1;
2065 setCursorFromCoordinates(x, y);
2066 if (!selecting && cursor.row() == cursor.irow()) {
2068 int y1 = cursor.iy() - topy;
2071 Inset * inset_hit = checkInsetHit(x, y1);
2072 if (inset_hit && isHighlyEditableInset(inset_hit)) {
2073 inset_hit->edit(bv(), x, y - (y2 - y1), mouse_button::none);
2077 setCursorFromCoordinates(bv(), cursor.x_fix(),
2078 cursor.y() - cursor.row()->baseline()
2079 + cursor.row()->height() + 1);
2084 void LyXText::cursorUpParagraph()
2086 if (cursor.pos() > 0) {
2087 setCursor(cursor.par(), 0);
2089 else if (cursor.par() != ownerParagraphs().begin()) {
2090 setCursor(boost::prior(cursor.par()), 0);
2095 void LyXText::cursorDownParagraph()
2097 if (boost::next(cursor.par()) != ownerParagraphs().end()) {
2098 setCursor(boost::next(cursor.par()), 0);
2100 setCursor(cursor.par(), cursor.par()->size());
2104 // fix the cursor `cur' after a characters has been deleted at `where'
2105 // position. Called by deleteEmptyParagraphMechanism
2106 void LyXText::fixCursorAfterDelete(LyXCursor & cur,
2107 LyXCursor const & where)
2109 // if cursor is not in the paragraph where the delete occured,
2111 if (cur.par() != where.par())
2114 // if cursor position is after the place where the delete occured,
2116 if (cur.pos() > where.pos())
2117 cur.pos(cur.pos()-1);
2119 // check also if we don't want to set the cursor on a spot behind the
2120 // pagragraph because we erased the last character.
2121 if (cur.pos() > cur.par()->size())
2122 cur.pos(cur.par()->size());
2124 // recompute row et al. for this cursor
2125 setCursor(cur, cur.par(), cur.pos(), cur.boundary());
2129 bool LyXText::deleteEmptyParagraphMechanism(LyXCursor const & old_cursor)
2131 // Would be wrong to delete anything if we have a selection.
2132 if (selection.set())
2135 // We allow all kinds of "mumbo-jumbo" when freespacing.
2136 if (old_cursor.par()->layout()->free_spacing
2137 || old_cursor.par()->isFreeSpacing()) {
2141 /* Ok I'll put some comments here about what is missing.
2142 I have fixed BackSpace (and thus Delete) to not delete
2143 double-spaces automagically. I have also changed Cut,
2144 Copy and Paste to hopefully do some sensible things.
2145 There are still some small problems that can lead to
2146 double spaces stored in the document file or space at
2147 the beginning of paragraphs. This happens if you have
2148 the cursor betwenn to spaces and then save. Or if you
2149 cut and paste and the selection have a space at the
2150 beginning and then save right after the paste. I am
2151 sure none of these are very hard to fix, but I will
2152 put out 1.1.4pre2 with FIX_DOUBLE_SPACE defined so
2153 that I can get some feedback. (Lgb)
2156 // If old_cursor.pos() == 0 and old_cursor.pos()(1) == LineSeparator
2157 // delete the LineSeparator.
2160 // If old_cursor.pos() == 1 and old_cursor.pos()(0) == LineSeparator
2161 // delete the LineSeparator.
2164 // If the pos around the old_cursor were spaces, delete one of them.
2165 if (old_cursor.par() != cursor.par()
2166 || old_cursor.pos() != cursor.pos()) {
2167 // Only if the cursor has really moved
2169 if (old_cursor.pos() > 0
2170 && old_cursor.pos() < old_cursor.par()->size()
2171 && old_cursor.par()->isLineSeparator(old_cursor.pos())
2172 && old_cursor.par()->isLineSeparator(old_cursor.pos() - 1)) {
2173 old_cursor.par()->erase(old_cursor.pos() - 1);
2174 redoParagraphs(old_cursor, boost::next(old_cursor.par()));
2176 #ifdef WITH_WARNINGS
2177 #warning This will not work anymore when we have multiple views of the same buffer
2178 // In this case, we will have to correct also the cursors held by
2179 // other bufferviews. It will probably be easier to do that in a more
2180 // automated way in LyXCursor code. (JMarc 26/09/2001)
2182 // correct all cursors held by the LyXText
2183 fixCursorAfterDelete(cursor, old_cursor);
2184 fixCursorAfterDelete(selection.cursor,
2186 fixCursorAfterDelete(selection.start,
2188 fixCursorAfterDelete(selection.end, old_cursor);
2189 fixCursorAfterDelete(last_sel_cursor,
2191 fixCursorAfterDelete(toggle_cursor, old_cursor);
2192 fixCursorAfterDelete(toggle_end_cursor,
2198 // don't delete anything if this is the ONLY paragraph!
2199 if (ownerParagraphs().size() == 1)
2202 // Do not delete empty paragraphs with keepempty set.
2203 if (old_cursor.par()->layout()->keepempty)
2206 // only do our magic if we changed paragraph
2207 if (old_cursor.par() == cursor.par())
2210 // record if we have deleted a paragraph
2211 // we can't possibly have deleted a paragraph before this point
2212 bool deleted = false;
2214 if (old_cursor.par()->empty() ||
2215 (old_cursor.par()->size() == 1 &&
2216 old_cursor.par()->isLineSeparator(0))) {
2217 // ok, we will delete anything
2218 LyXCursor tmpcursor;
2222 bool selection_position_was_oldcursor_position = (
2223 selection.cursor.par() == old_cursor.par()
2224 && selection.cursor.pos() == old_cursor.pos());
2226 if (old_cursor.row() != rows().begin()) {
2228 prevrow = boost::prior(old_cursor.row());
2229 const_cast<LyXText *>(this)->postPaint(old_cursor.y() - old_cursor.row()->baseline() - prevrow->height());
2231 cursor = old_cursor; // that undo can restore the right cursor position
2232 #warning FIXME. --end() iterator is usable here
2233 ParagraphList::iterator endpit = boost::next(old_cursor.par());
2234 while (endpit != ownerParagraphs().end() &&
2235 endpit->getDepth()) {
2239 setUndo(bv(), Undo::DELETE, old_cursor.par(), endpit);
2243 removeRow(old_cursor.row());
2245 ownerParagraphs().erase(old_cursor.par());
2247 /* Breakagain the next par. Needed because of
2248 * the parindent that can occur or dissappear.
2249 * The next row can change its height, if
2250 * there is another layout before */
2251 if (boost::next(prevrow) != rows().end()) {
2252 breakAgain(boost::next(prevrow));
2255 setHeightOfRow(prevrow);
2257 RowList::iterator nextrow = boost::next(old_cursor.row());
2258 const_cast<LyXText *>(this)->postPaint(
2259 old_cursor.y() - old_cursor.row()->baseline());
2262 cursor = old_cursor; // that undo can restore the right cursor position
2263 #warning FIXME. --end() iterator is usable here
2264 ParagraphList::iterator endpit = boost::next(old_cursor.par());
2265 while (endpit != ownerParagraphs().end() &&
2266 endpit->getDepth()) {
2270 setUndo(bv(), Undo::DELETE, old_cursor.par(), endpit);
2274 removeRow(old_cursor.row());
2276 ownerParagraphs().erase(old_cursor.par());
2278 /* Breakagain the next par. Needed because of
2279 the parindent that can occur or dissappear.
2280 The next row can change its height, if
2281 there is another layout before */
2282 if (nextrow != rows().end()) {
2283 breakAgain(nextrow);
2289 setCursorIntern(cursor.par(), cursor.pos());
2291 if (selection_position_was_oldcursor_position) {
2292 // correct selection
2293 selection.cursor = cursor;
2297 if (old_cursor.par()->stripLeadingSpaces()) {
2298 redoParagraphs(old_cursor, boost::next(old_cursor.par()));
2300 setCursorIntern(cursor.par(), cursor.pos());
2301 selection.cursor = cursor;
2308 ParagraphList & LyXText::ownerParagraphs() const
2311 return inset_owner->paragraphs;
2313 return bv_owner->buffer()->paragraphs;
2317 LyXText::refresh_status LyXText::refreshStatus() const
2319 return refresh_status_;
2323 void LyXText::clearPaint()
2325 refresh_status_ = REFRESH_NONE;
2326 refresh_row = rows().end();
2331 void LyXText::postPaint(int start_y)
2333 refresh_status old = refresh_status_;
2335 refresh_status_ = REFRESH_AREA;
2336 refresh_row = rows().end();
2338 if (old != REFRESH_NONE && refresh_y < start_y)
2341 refresh_y = start_y;
2346 // We are an inset's lyxtext. Tell the top-level lyxtext
2347 // it needs to update the row we're in.
2348 LyXText * t = bv()->text;
2349 t->postRowPaint(t->cursor.row(), t->cursor.y() - t->cursor.row()->baseline());
2353 // FIXME: we should probably remove this y parameter,
2354 // make refresh_y be 0, and use row->y etc.
2355 void LyXText::postRowPaint(RowList::iterator rit, int start_y)
2357 if (refresh_status_ != REFRESH_NONE && refresh_y < start_y) {
2358 refresh_status_ = REFRESH_AREA;
2361 refresh_y = start_y;
2364 if (refresh_status_ == REFRESH_AREA)
2367 refresh_status_ = REFRESH_ROW;
2373 // We are an inset's lyxtext. Tell the top-level lyxtext
2374 // it needs to update the row we're in.
2375 LyXText * t = bv()->text;
2376 t->postRowPaint(t->cursor.row(), t->cursor.y() - t->cursor.row()->baseline());
2380 bool LyXText::isInInset() const
2382 // Sub-level has non-null bv owner and
2383 // non-null inset owner.
2384 return inset_owner != 0 && bv_owner != 0;
2388 int defaultRowHeight()
2390 LyXFont const font(LyXFont::ALL_SANE);
2391 return int(font_metrics::maxAscent(font)
2392 + font_metrics::maxDescent(font) * 1.5);