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"
52 LyXText::LyXText(BufferView * bv)
53 : height(0), width(0), anchor_row_offset_(0),
54 inset_owner(0), the_locking_inset(0), bv_owner(bv)
56 anchor_row_ = rows().end();
57 need_break_row = rows().end();
58 refresh_row = rows().end();
64 LyXText::LyXText(BufferView * bv, InsetText * inset)
65 : height(0), width(0), anchor_row_offset_(0),
66 inset_owner(inset), the_locking_inset(0), bv_owner(bv)
68 anchor_row_ = rows().end();
69 need_break_row = rows().end();
70 refresh_row = rows().end();
76 void LyXText::init(BufferView * bview, bool reinit)
80 need_break_row = rows().end();
84 } else if (!rowlist_.empty())
87 ParagraphList::iterator pit = ownerParagraphs().begin();
88 ParagraphList::iterator end = ownerParagraphs().end();
90 current_font = getFont(bview->buffer(), pit, 0);
92 for (; pit != end; ++pit) {
93 insertParagraph(pit, rowlist_.end());
95 setCursorIntern(rowlist_.begin()->par(), 0);
96 selection.cursor = cursor;
102 // Gets the fully instantiated font at a given position in a paragraph
103 // Basically the same routine as Paragraph::getFont() in paragraph.C.
104 // The difference is that this one is used for displaying, and thus we
105 // are allowed to make cosmetic improvements. For instance make footnotes
107 // If position is -1, we get the layout font of the paragraph.
108 // If position is -2, we get the font of the manual label of the paragraph.
109 LyXFont const LyXText::getFont(Buffer const * buf, ParagraphList::iterator pit,
112 lyx::Assert(pos >= 0);
114 LyXLayout_ptr const & layout = pit->layout();
116 // We specialize the 95% common case:
117 if (!pit->getDepth()) {
118 if (layout->labeltype == LABEL_MANUAL
119 && pos < pit->beginningOfBody()) {
121 LyXFont f = pit->getFontSettings(buf->params, pos);
123 pit->inInset()->getDrawFont(f);
124 return f.realize(layout->reslabelfont);
126 LyXFont f = pit->getFontSettings(buf->params, pos);
128 pit->inInset()->getDrawFont(f);
129 return f.realize(layout->resfont);
133 // The uncommon case need not be optimized as much
137 if (pos < pit->beginningOfBody()) {
139 layoutfont = layout->labelfont;
142 layoutfont = layout->font;
145 LyXFont tmpfont = pit->getFontSettings(buf->params, pos);
146 tmpfont.realize(layoutfont);
149 pit->inInset()->getDrawFont(tmpfont);
151 // Realize with the fonts of lesser depth.
152 tmpfont.realize(outerFont(pit, ownerParagraphs()));
154 return realizeFont(tmpfont, buf->params);
158 LyXFont const LyXText::getLayoutFont(Buffer const * buf,
159 ParagraphList::iterator pit) const
161 LyXLayout_ptr const & layout = pit->layout();
163 if (!pit->getDepth()) {
164 return layout->resfont;
167 LyXFont font(layout->font);
168 // Realize with the fonts of lesser depth.
169 font.realize(outerFont(pit, ownerParagraphs()));
171 return realizeFont(font, buf->params);
175 LyXFont const LyXText::getLabelFont(Buffer const * buf,
176 ParagraphList::iterator pit) const
178 LyXLayout_ptr const & layout = pit->layout();
180 if (!pit->getDepth()) {
181 return layout->reslabelfont;
184 LyXFont font(layout->labelfont);
185 // Realize with the fonts of lesser depth.
186 font.realize(outerFont(pit, ownerParagraphs()));
188 return realizeFont(layout->labelfont, buf->params);
192 void LyXText::setCharFont(ParagraphList::iterator pit,
193 pos_type pos, LyXFont const & fnt,
196 Buffer const * buf = bv()->buffer();
197 LyXFont font = getFont(buf, pit, pos);
198 font.update(fnt, buf->params.language, toggleall);
199 // Let the insets convert their font
200 if (pit->isInset(pos)) {
201 Inset * inset = pit->getInset(pos);
202 if (isEditableInset(inset)) {
203 UpdatableInset * uinset =
204 static_cast<UpdatableInset *>(inset);
205 uinset->setFont(bv(), fnt, toggleall, true);
209 // Plug thru to version below:
210 setCharFont(buf, pit, pos, font);
214 void LyXText::setCharFont(Buffer const * buf, ParagraphList::iterator pit,
215 pos_type pos, LyXFont const & fnt)
219 LyXTextClass const & tclass = buf->params.getLyXTextClass();
220 LyXLayout_ptr const & layout = pit->layout();
222 // Get concrete layout font to reduce against
225 if (pos < pit->beginningOfBody())
226 layoutfont = layout->labelfont;
228 layoutfont = layout->font;
230 // Realize against environment font information
231 if (pit->getDepth()) {
232 ParagraphList::iterator tp = pit;
233 while (!layoutfont.resolved() &&
234 tp != ownerParagraphs().end() &&
236 tp = outerHook(tp, ownerParagraphs());
237 if (tp != ownerParagraphs().end())
238 layoutfont.realize(tp->layout()->font);
242 layoutfont.realize(tclass.defaultfont());
244 // Now, reduce font against full layout font
245 font.reduce(layoutfont);
247 pit->setFont(pos, font);
251 // removes the row and reset the touched counters
252 void LyXText::removeRow(RowList::iterator rit)
254 /* FIXME: when we cache the bview, this should just
255 * become a postPaint(), I think */
256 if (refresh_row == rit) {
257 if (rit == rows().begin())
258 refresh_row = boost::next(rit);
260 refresh_row = boost::prior(rit);
262 // what about refresh_y
265 if (anchor_row_ == rit) {
266 if (rit != rows().begin()) {
267 anchor_row_ = boost::prior(rit);
268 anchor_row_offset_ += anchor_row_->height();
270 anchor_row_ = boost::next(rit);
271 anchor_row_offset_ -= rit->height();
275 // the text becomes smaller
276 height -= rit->height();
282 // remove all following rows of the paragraph of the specified row.
283 void LyXText::removeParagraph(RowList::iterator rit)
285 ParagraphList::iterator tmppit = rit->par();
288 while (rit != rows().end() && rit->par() == tmppit) {
289 RowList::iterator tmprit = boost::next(rit);
296 void LyXText::insertParagraph(ParagraphList::iterator pit,
297 RowList::iterator rowit)
299 // insert a new row, starting at position 0
301 RowList::iterator rit = rowlist_.insert(rowit, newrow);
303 // and now append the whole paragraph before the new row
304 appendParagraph(rit);
308 Inset * LyXText::getInset() const
310 ParagraphList::iterator pit = cursor.par();
311 pos_type const pos = cursor.pos();
313 if (pos < pit->size() && pit->isInset(pos)) {
314 return pit->getInset(pos);
320 void LyXText::toggleInset()
322 Inset * inset = getInset();
323 // is there an editable inset at cursor position?
324 if (!isEditableInset(inset)) {
325 // No, try to see if we are inside a collapsable inset
326 if (inset_owner && inset_owner->owner()
327 && inset_owner->owner()->isOpen()) {
328 bv()->unlockInset(static_cast<UpdatableInset *>(inset_owner->owner()));
329 inset_owner->owner()->close(bv());
330 bv()->getLyXText()->cursorRight(bv());
334 //bv()->owner()->message(inset->editMessage());
336 // do we want to keep this?? (JMarc)
337 if (!isHighlyEditableInset(inset))
338 setCursorParUndo(bv());
340 if (inset->isOpen()) {
346 bv()->updateInset(inset);
350 /* used in setlayout */
351 // Asger is not sure we want to do this...
352 void LyXText::makeFontEntriesLayoutSpecific(Buffer const & buf,
355 LyXLayout_ptr const & layout = par.layout();
356 pos_type const psize = par.size();
359 for (pos_type pos = 0; pos < psize; ++pos) {
360 if (pos < par.beginningOfBody())
361 layoutfont = layout->labelfont;
363 layoutfont = layout->font;
365 LyXFont tmpfont = par.getFontSettings(buf.params, pos);
366 tmpfont.reduce(layoutfont);
367 par.setFont(pos, tmpfont);
372 ParagraphList::iterator
373 LyXText::setLayout(LyXCursor & cur, LyXCursor & sstart_cur,
374 LyXCursor & send_cur,
375 string const & layout)
377 ParagraphList::iterator endpit = boost::next(send_cur.par());
378 ParagraphList::iterator undoendpit = endpit;
379 ParagraphList::iterator pars_end = ownerParagraphs().end();
381 if (endpit != pars_end && endpit->getDepth()) {
382 while (endpit != pars_end && endpit->getDepth()) {
386 } else if (endpit != pars_end) {
387 // because of parindents etc.
391 setUndo(bv(), Undo::EDIT, &*sstart_cur.par(), &*undoendpit);
393 // ok we have a selection. This is always between sstart_cur
394 // and sel_end cursor
396 ParagraphList::iterator pit = sstart_cur.par();
397 ParagraphList::iterator epit = boost::next(send_cur.par());
399 LyXLayout_ptr const & lyxlayout =
400 bv()->buffer()->params.getLyXTextClass()[layout];
403 pit->applyLayout(lyxlayout);
404 makeFontEntriesLayoutSpecific(*bv()->buffer(), *pit);
405 ParagraphList::iterator fppit = pit;
406 fppit->params().spaceTop(lyxlayout->fill_top ?
407 VSpace(VSpace::VFILL)
408 : VSpace(VSpace::NONE));
409 fppit->params().spaceBottom(lyxlayout->fill_bottom ?
410 VSpace(VSpace::VFILL)
411 : VSpace(VSpace::NONE));
412 if (lyxlayout->margintype == MARGIN_MANUAL)
413 pit->setLabelWidthString(lyxlayout->labelstring());
416 } while (pit != epit);
422 // set layout over selection and make a total rebreak of those paragraphs
423 void LyXText::setLayout(string const & layout)
425 LyXCursor tmpcursor = cursor; /* store the current cursor */
427 // if there is no selection just set the layout
428 // of the current paragraph */
429 if (!selection.set()) {
430 selection.start = cursor; // dummy selection
431 selection.end = cursor;
433 ParagraphList::iterator endpit = setLayout(cursor, selection.start,
434 selection.end, layout);
435 redoParagraphs(selection.start, endpit);
437 // we have to reset the selection, because the
438 // geometry could have changed
439 setCursor(selection.start.par(),
440 selection.start.pos(), false);
441 selection.cursor = cursor;
442 setCursor(selection.end.par(), selection.end.pos(), false);
446 setCursor(tmpcursor.par(), tmpcursor.pos(), true);
450 bool LyXText::changeDepth(bv_funcs::DEPTH_CHANGE type, bool test_only)
452 ParagraphList::iterator pit(cursor.par());
453 ParagraphList::iterator end(cursor.par());
454 ParagraphList::iterator start = pit;
456 if (selection.set()) {
457 pit = selection.start.par();
458 end = selection.end.par();
462 ParagraphList::iterator pastend = boost::next(end);
465 setUndo(bv(), Undo::EDIT, &(*start), &(*pastend));
467 bool changed = false;
469 int prev_after_depth = 0;
470 #warning parlist ... could be nicer ?
471 if (start != ownerParagraphs().begin()) {
472 prev_after_depth = boost::prior(start)->getMaxDepthAfter();
476 int const depth = pit->params().depth();
477 if (type == bv_funcs::INC_DEPTH) {
478 if (depth < prev_after_depth
479 && pit->layout()->labeltype != LABEL_BIBLIO) {
482 pit->params().depth(depth + 1);
489 pit->params().depth(depth - 1);
492 prev_after_depth = pit->getMaxDepthAfter();
504 // Wow, redoParagraphs is stupid.
506 setCursor(tmpcursor, &(*start), 0);
508 //redoParagraphs(tmpcursor, &(*pastend));
509 redoParagraphs(tmpcursor, &(*pastend));
511 // We need to actually move the text->cursor. I don't
512 // understand why ...
515 // we have to reset the visual selection because the
516 // geometry could have changed
517 if (selection.set()) {
518 setCursor(selection.start.par(), selection.start.pos());
519 selection.cursor = cursor;
520 setCursor(selection.end.par(), selection.end.pos());
523 // this handles the counter labels, and also fixes up
524 // depth values for follow-on (child) paragraphs
528 setCursor(tmpcursor.par(), tmpcursor.pos());
534 // set font over selection and make a total rebreak of those paragraphs
535 void LyXText::setFont(LyXFont const & font, bool toggleall)
537 // if there is no selection just set the current_font
538 if (!selection.set()) {
539 // Determine basis font
541 if (cursor.pos() < cursor.par()->beginningOfBody()) {
542 layoutfont = getLabelFont(bv()->buffer(),
545 layoutfont = getLayoutFont(bv()->buffer(),
548 // Update current font
549 real_current_font.update(font,
550 bv()->buffer()->params.language,
553 // Reduce to implicit settings
554 current_font = real_current_font;
555 current_font.reduce(layoutfont);
556 // And resolve it completely
557 real_current_font.realize(layoutfont);
562 LyXCursor tmpcursor = cursor; // store the current cursor
564 // ok we have a selection. This is always between sel_start_cursor
565 // and sel_end cursor
567 setUndo(bv(), Undo::EDIT,
568 &*selection.start.par(), &*boost::next(selection.end.par()));
570 cursor = selection.start;
571 while (cursor.par() != selection.end.par() ||
572 cursor.pos() < selection.end.pos())
574 if (cursor.pos() < cursor.par()->size()) {
575 // an open footnote should behave like a closed one
576 setCharFont(cursor.par(), cursor.pos(),
578 cursor.pos(cursor.pos() + 1);
581 cursor.par(boost::next(cursor.par()));
586 redoParagraphs(selection.start, boost::next(selection.end.par()));
588 // we have to reset the selection, because the
589 // geometry could have changed, but we keep
590 // it for user convenience
591 setCursor(selection.start.par(), selection.start.pos());
592 selection.cursor = cursor;
593 setCursor(selection.end.par(), selection.end.pos());
595 setCursor(tmpcursor.par(), tmpcursor.pos(), true,
596 tmpcursor.boundary());
600 void LyXText::redoHeightOfParagraph()
602 RowList::iterator tmprow = cursor.row();
603 int y = cursor.y() - tmprow->baseline();
605 setHeightOfRow(tmprow);
607 while (tmprow != rows().begin()
608 && boost::prior(tmprow)->par() == tmprow->par()) {
610 y -= tmprow->height();
611 setHeightOfRow(tmprow);
616 setCursor(cursor.par(), cursor.pos(), false, cursor.boundary());
620 void LyXText::redoDrawingOfParagraph(LyXCursor const & cur)
622 RowList::iterator tmprow = cur.row();
624 int y = cur.y() - tmprow->baseline();
625 setHeightOfRow(tmprow);
627 while (tmprow != rows().begin()
628 && boost::prior(tmprow)->par() == tmprow->par()) {
630 y -= tmprow->height();
634 setCursor(cur.par(), cur.pos());
638 // deletes and inserts again all paragaphs between the cursor
639 // and the specified par
640 // This function is needed after SetLayout and SetFont etc.
641 void LyXText::redoParagraphs(LyXCursor const & cur,
642 ParagraphList::iterator endpit)
644 RowList::iterator tmprit = cur.row();
645 int y = cur.y() - tmprit->baseline();
647 ParagraphList::iterator first_phys_pit;
648 RowList::iterator prevrit;
649 if (tmprit == rows().begin()) {
650 // A trick/hack for UNDO.
651 // This is needed because in an UNDO/REDO we could have
652 // changed the ownerParagrah() so the paragraph inside
653 // the row is NOT my really first par anymore.
654 // Got it Lars ;) (Jug 20011206)
655 first_phys_pit = ownerParagraphs().begin();
656 prevrit = rows().end();
658 first_phys_pit = tmprit->par();
659 while (tmprit != rows().begin()
660 && boost::prior(tmprit)->par() == first_phys_pit)
663 y -= tmprit->height();
665 prevrit = boost::prior(tmprit);
669 while (tmprit != rows().end() && tmprit->par() != endpit) {
670 RowList::iterator tmprit2 = tmprit++;
674 // Reinsert the paragraphs.
675 ParagraphList::iterator tmppit = first_phys_pit;
677 while (tmppit != ownerParagraphs().end()) {
678 insertParagraph(tmppit, tmprit);
679 while (tmprit != rows().end()
680 && tmprit->par() == tmppit) {
684 if (tmppit == endpit)
687 if (prevrit != rows().end()) {
688 setHeightOfRow(prevrit);
689 const_cast<LyXText *>(this)->postPaint(y - prevrit->height());
691 setHeightOfRow(rows().begin());
692 const_cast<LyXText *>(this)->postPaint(0);
694 if (tmprit != rows().end())
695 setHeightOfRow(tmprit);
701 void LyXText::fullRebreak()
703 if (rows().empty()) {
708 RowList::iterator rows_end = rows().end();
710 if (need_break_row != rows_end) {
711 breakAgain(need_break_row);
712 need_break_row = rows_end;
718 // important for the screen
721 // the cursor set functions have a special mechanism. When they
722 // realize, that you left an empty paragraph, they will delete it.
723 // They also delete the corresponding row
725 // need the selection cursor:
726 void LyXText::setSelection()
728 bool const lsel = selection.set();
730 if (!selection.set()) {
731 last_sel_cursor = selection.cursor;
732 selection.start = selection.cursor;
733 selection.end = selection.cursor;
738 // first the toggling area
739 if (cursor.y() < last_sel_cursor.y()
740 || (cursor.y() == last_sel_cursor.y()
741 && cursor.x() < last_sel_cursor.x())) {
742 toggle_end_cursor = last_sel_cursor;
743 toggle_cursor = cursor;
745 toggle_end_cursor = cursor;
746 toggle_cursor = last_sel_cursor;
749 last_sel_cursor = cursor;
751 // and now the whole selection
753 if (selection.cursor.par() == cursor.par())
754 if (selection.cursor.pos() < cursor.pos()) {
755 selection.end = cursor;
756 selection.start = selection.cursor;
758 selection.end = selection.cursor;
759 selection.start = cursor;
761 else if (selection.cursor.y() < cursor.y() ||
762 (selection.cursor.y() == cursor.y()
763 && selection.cursor.x() < cursor.x())) {
764 selection.end = cursor;
765 selection.start = selection.cursor;
768 selection.end = selection.cursor;
769 selection.start = cursor;
772 // a selection with no contents is not a selection
773 if (selection.start.par() == selection.end.par() &&
774 selection.start.pos() == selection.end.pos())
775 selection.set(false);
777 if (inset_owner && (selection.set() || lsel))
778 inset_owner->setUpdateStatus(bv(), InsetText::SELECTION);
782 string const LyXText::selectionAsString(Buffer const * buffer,
785 if (!selection.set()) return string();
787 // should be const ...
788 ParagraphList::iterator startpit = selection.start.par();
789 ParagraphList::iterator endpit = selection.end.par();
790 pos_type const startpos(selection.start.pos());
791 pos_type const endpos(selection.end.pos());
793 if (startpit == endpit) {
794 return startpit->asString(buffer, startpos, endpos, label);
799 // First paragraph in selection
800 result += startpit->asString(buffer, startpos, startpit->size(), label) + "\n\n";
802 // The paragraphs in between (if any)
803 ParagraphList::iterator pit = boost::next(startpit);
804 for (; pit != endpit; ++pit) {
805 result += pit->asString(buffer, 0, pit->size(), label) + "\n\n";
808 // Last paragraph in selection
809 result += endpit->asString(buffer, 0, endpos, label);
815 void LyXText::clearSelection()
817 selection.set(false);
818 selection.mark(false);
819 last_sel_cursor = selection.end = selection.start = selection.cursor = cursor;
820 // reset this in the bv_owner!
821 if (bv_owner && bv_owner->text)
822 bv_owner->text->xsel_cache.set(false);
826 void LyXText::cursorHome()
828 setCursor(cursor.par(), cursor.row()->pos());
832 void LyXText::cursorEnd()
834 if (cursor.par()->empty())
837 RowList::iterator rit = cursor.row();
838 RowList::iterator next_rit = boost::next(rit);
839 ParagraphList::iterator pit = rit->par();
840 pos_type last_pos = lastPos(*this, rit);
842 if (next_rit == rows().end() || next_rit->par() != pit) {
846 (pit->getChar(last_pos) != ' ' && !pit->isNewline(last_pos))) {
851 setCursor(pit, last_pos);
855 void LyXText::cursorTop()
857 setCursor(ownerParagraphs().begin(), 0);
861 void LyXText::cursorBottom()
864 // This is how it should be:
865 // ParagraphList::iterator lastpit = boost::prior(ownerParagraphs().end());
866 ParagraphList::iterator lastpit = &ownerParagraphs().back();
867 int pos = lastpit->size();
868 setCursor(lastpit, pos);
872 void LyXText::toggleFree(LyXFont const & font, bool toggleall)
874 // If the mask is completely neutral, tell user
875 if (font == LyXFont(LyXFont::ALL_IGNORE)) {
876 // Could only happen with user style
877 bv()->owner()->message(_("No font change defined. Use Character under the Layout menu to define font change."));
881 // Try implicit word selection
882 // If there is a change in the language the implicit word selection
884 LyXCursor resetCursor = cursor;
885 bool implicitSelection = (font.language() == ignore_language
886 && font.number() == LyXFont::IGNORE)
887 ? selectWordWhenUnderCursor(WHOLE_WORD_STRICT) : false;
890 setFont(font, toggleall);
892 // Implicit selections are cleared afterwards
893 //and cursor is set to the original position.
894 if (implicitSelection) {
896 cursor = resetCursor;
897 setCursor(cursor.par(), cursor.pos());
898 selection.cursor = cursor;
901 inset_owner->setUpdateStatus(bv(), InsetText::CURSOR_PAR);
905 string LyXText::getStringToIndex()
907 // Try implicit word selection
908 // If there is a change in the language the implicit word selection
910 LyXCursor const reset_cursor = cursor;
911 bool const implicitSelection = selectWordWhenUnderCursor(PREVIOUS_WORD);
914 if (!selection.set())
915 bv()->owner()->message(_("Nothing to index!"));
916 else if (selection.start.par() != selection.end.par())
917 bv()->owner()->message(_("Cannot index more than one paragraph!"));
919 idxstring = selectionAsString(bv()->buffer(), false);
921 // Reset cursors to their original position.
922 cursor = reset_cursor;
923 setCursor(cursor.par(), cursor.pos());
924 selection.cursor = cursor;
926 // Clear the implicit selection.
927 if (implicitSelection)
934 // the DTP switches for paragraphs. LyX will store them in the first
935 // physicla paragraph. When a paragraph is broken, the top settings rest,
936 // the bottom settings are given to the new one. So I can make shure,
937 // they do not duplicate themself and you cannnot make dirty things with
940 void LyXText::setParagraph(bool line_top, bool line_bottom,
941 bool pagebreak_top, bool pagebreak_bottom,
942 VSpace const & space_top,
943 VSpace const & space_bottom,
944 Spacing const & spacing,
946 string const & labelwidthstring,
949 LyXCursor tmpcursor = cursor;
950 if (!selection.set()) {
951 selection.start = cursor;
952 selection.end = cursor;
955 // make sure that the depth behind the selection are restored, too
956 ParagraphList::iterator endpit = boost::next(selection.end.par());
957 ParagraphList::iterator undoendpit = endpit;
958 ParagraphList::iterator pars_end = ownerParagraphs().end();
960 if (endpit != pars_end && endpit->getDepth()) {
961 while (endpit != pars_end && endpit->getDepth()) {
965 } else if (endpit!= pars_end) {
966 // because of parindents etc.
970 setUndo(bv(), Undo::EDIT, &*selection.start.par(), &*undoendpit);
973 ParagraphList::iterator tmppit = selection.end.par();
975 while (tmppit != boost::prior(selection.start.par())) {
976 setCursor(tmppit, 0);
977 postPaint(cursor.y() - cursor.row()->baseline());
979 ParagraphList::iterator pit = cursor.par();
980 ParagraphParameters & params = pit->params();
982 params.lineTop(line_top);
983 params.lineBottom(line_bottom);
984 params.pagebreakTop(pagebreak_top);
985 params.pagebreakBottom(pagebreak_bottom);
986 params.spaceTop(space_top);
987 params.spaceBottom(space_bottom);
988 params.spacing(spacing);
989 // does the layout allow the new alignment?
990 LyXLayout_ptr const & layout = pit->layout();
992 if (align == LYX_ALIGN_LAYOUT)
993 align = layout->align;
994 if (align & layout->alignpossible) {
995 if (align == layout->align)
996 params.align(LYX_ALIGN_LAYOUT);
1000 pit->setLabelWidthString(labelwidthstring);
1001 params.noindent(noindent);
1002 tmppit = boost::prior(pit);
1005 redoParagraphs(selection.start, endpit);
1008 setCursor(selection.start.par(), selection.start.pos());
1009 selection.cursor = cursor;
1010 setCursor(selection.end.par(), selection.end.pos());
1012 setCursor(tmpcursor.par(), tmpcursor.pos());
1014 bv()->updateInset(inset_owner);
1018 // set the counter of a paragraph. This includes the labels
1019 void LyXText::setCounter(Buffer const * buf, ParagraphList::iterator pit)
1021 LyXTextClass const & textclass = buf->params.getLyXTextClass();
1022 LyXLayout_ptr const & layout = pit->layout();
1024 if (pit != ownerParagraphs().begin()) {
1026 pit->params().appendix(boost::prior(pit)->params().appendix());
1027 if (!pit->params().appendix() &&
1028 pit->params().startOfAppendix()) {
1029 pit->params().appendix(true);
1030 textclass.counters().reset();
1032 pit->enumdepth = boost::prior(pit)->enumdepth;
1033 pit->itemdepth = boost::prior(pit)->itemdepth;
1035 pit->params().appendix(pit->params().startOfAppendix());
1040 /* Maybe we have to increment the enumeration depth.
1041 * BUT, enumeration in a footnote is considered in isolation from its
1042 * surrounding paragraph so don't increment if this is the
1043 * first line of the footnote
1044 * AND, bibliographies can't have their depth changed ie. they
1045 * are always of depth 0
1047 if (pit != ownerParagraphs().begin()
1048 && boost::prior(pit)->getDepth() < pit->getDepth()
1049 && boost::prior(pit)->layout()->labeltype == LABEL_COUNTER_ENUMI
1050 && pit->enumdepth < 3
1051 && layout->labeltype != LABEL_BIBLIO) {
1055 // Maybe we have to decrement the enumeration depth, see note above
1056 if (pit != ownerParagraphs().begin()
1057 && boost::prior(pit)->getDepth() > pit->getDepth()
1058 && layout->labeltype != LABEL_BIBLIO) {
1059 pit->enumdepth = depthHook(pit, ownerParagraphs(),
1060 pit->getDepth())->enumdepth;
1063 if (!pit->params().labelString().empty()) {
1064 pit->params().labelString(string());
1067 if (layout->margintype == MARGIN_MANUAL) {
1068 if (pit->params().labelWidthString().empty()) {
1069 pit->setLabelWidthString(layout->labelstring());
1072 pit->setLabelWidthString(string());
1075 // is it a layout that has an automatic label?
1076 if (layout->labeltype >= LABEL_COUNTER_CHAPTER) {
1077 int const i = layout->labeltype - LABEL_COUNTER_CHAPTER;
1081 if (i >= 0 && i <= buf->params.secnumdepth) {
1085 textclass.counters().step(layout->latexname());
1087 // Is there a label? Useful for Chapter layout
1088 if (!pit->params().appendix()) {
1089 s << layout->labelstring();
1091 s << layout->labelstring_appendix();
1094 // Use of an integer is here less than elegant. For now.
1095 int head = textclass.maxcounter() - LABEL_COUNTER_CHAPTER;
1096 if (!pit->params().appendix()) {
1097 numbertype = "sectioning";
1099 numbertype = "appendix";
1100 if (pit->isRightToLeftPar(buf->params))
1101 langtype = "hebrew";
1106 s << textclass.counters()
1107 .numberLabel(layout->latexname(),
1108 numbertype, langtype, head);
1110 pit->params().labelString(STRCONV(s.str()));
1112 // reset enum counters
1113 textclass.counters().reset("enum");
1114 } else if (layout->labeltype < LABEL_COUNTER_ENUMI) {
1115 textclass.counters().reset("enum");
1116 } else if (layout->labeltype == LABEL_COUNTER_ENUMI) {
1118 // Yes I know this is a really, really! bad solution
1120 string enumcounter("enum");
1122 switch (pit->enumdepth) {
1131 enumcounter += "iv";
1134 // not a valid enumdepth...
1138 textclass.counters().step(enumcounter);
1140 s << textclass.counters()
1141 .numberLabel(enumcounter, "enumeration");
1142 pit->params().labelString(STRCONV(s.str()));
1144 } else if (layout->labeltype == LABEL_BIBLIO) {// ale970302
1145 textclass.counters().step("bibitem");
1146 int number = textclass.counters().value("bibitem");
1147 if (pit->bibitem()) {
1148 pit->bibitem()->setCounter(number);
1149 pit->params().labelString(layout->labelstring());
1151 // In biblio should't be following counters but...
1153 string s = layout->labelstring();
1155 // the caption hack:
1156 if (layout->labeltype == LABEL_SENSITIVE) {
1157 ParagraphList::iterator tmppit = pit;
1160 while (tmppit != ownerParagraphs().end() &&
1162 // the single '=' is intended below
1163 && (in = tmppit->inInset()->owner())) {
1164 if (in->lyxCode() == Inset::FLOAT_CODE ||
1165 in->lyxCode() == Inset::WRAP_CODE) {
1169 tmppit = in->parOwner();
1175 = textclass.floats().getType(static_cast<InsetFloat*>(in)->type());
1177 textclass.counters().step(fl.type());
1179 // Doesn't work... yet.
1180 #if USE_BOOST_FORMAT
1181 s = boost::io::str(boost::format(_("%1$s #:")) % fl.name());
1182 // s << boost::format(_("%1$s %1$d:")
1184 // % buf->counters().value(fl.name());
1187 //o << fl.name() << ' ' << buf->counters().value(fl.name()) << ":";
1188 o << fl.name() << " #:";
1189 s = STRCONV(o.str());
1192 // par->SetLayout(0);
1193 // s = layout->labelstring;
1194 s = _("Senseless: ");
1197 pit->params().labelString(s);
1199 // reset the enumeration counter. They are always reset
1200 // when there is any other layout between
1201 // Just fall-through between the cases so that all
1202 // enum counters deeper than enumdepth is also reset.
1203 switch (pit->enumdepth) {
1205 textclass.counters().reset("enumi");
1207 textclass.counters().reset("enumii");
1209 textclass.counters().reset("enumiii");
1211 textclass.counters().reset("enumiv");
1217 // Updates all counters. Paragraphs with changed label string will be rebroken
1218 void LyXText::updateCounters()
1220 RowList::iterator rowit = rows().begin();
1221 ParagraphList::iterator pit = rowit->par();
1223 // CHECK if this is really needed. (Lgb)
1224 bv()->buffer()->params.getLyXTextClass().counters().reset();
1226 for (; pit != ownerParagraphs().end(); ++pit) {
1227 while (rowit->par() != pit)
1230 string const oldLabel = pit->params().labelString();
1233 if (pit != ownerParagraphs().begin())
1234 maxdepth = boost::prior(pit)->getMaxDepthAfter();
1236 if (pit->params().depth() > maxdepth)
1237 pit->params().depth(maxdepth);
1239 // setCounter can potentially change the labelString.
1240 setCounter(bv()->buffer(), pit);
1242 string const & newLabel = pit->params().labelString();
1244 if (oldLabel.empty() && !newLabel.empty()) {
1245 removeParagraph(rowit);
1246 appendParagraph(rowit);
1252 void LyXText::insertInset(Inset * inset)
1254 if (!cursor.par()->insetAllowed(inset->lyxCode()))
1256 setUndo(bv(), Undo::FINISH, &*cursor.par(),
1257 &*boost::next(cursor.par()));
1259 cursor.par()->insertInset(cursor.pos(), inset);
1260 // Just to rebreak and refresh correctly.
1261 // The character will not be inserted a second time
1262 insertChar(Paragraph::META_INSET);
1263 // If we enter a highly editable inset the cursor should be to before
1264 // the inset. This couldn't happen before as Undo was not handled inside
1265 // inset now after the Undo LyX tries to call inset->Edit(...) again
1266 // and cannot do this as the cursor is behind the inset and GetInset
1267 // does not return the inset!
1268 if (isHighlyEditableInset(inset)) {
1275 void LyXText::cutSelection(bool doclear, bool realcut)
1277 // Stuff what we got on the clipboard. Even if there is no selection.
1279 // There is a problem with having the stuffing here in that the
1280 // larger the selection the slower LyX will get. This can be
1281 // solved by running the line below only when the selection has
1282 // finished. The solution used currently just works, to make it
1283 // faster we need to be more clever and probably also have more
1284 // calls to stuffClipboard. (Lgb)
1285 bv()->stuffClipboard(selectionAsString(bv()->buffer(), true));
1287 // This doesn't make sense, if there is no selection
1288 if (!selection.set())
1291 // OK, we have a selection. This is always between selection.start
1292 // and selection.end
1294 // make sure that the depth behind the selection are restored, too
1295 ParagraphList::iterator endpit = boost::next(selection.end.par());
1296 ParagraphList::iterator undoendpit = endpit;
1297 ParagraphList::iterator pars_end = ownerParagraphs().end();
1299 if (endpit != pars_end && endpit->getDepth()) {
1300 while (endpit != pars_end && endpit->getDepth()) {
1302 undoendpit = endpit;
1304 } else if (endpit != pars_end) {
1305 // because of parindents etc.
1309 setUndo(bv(), Undo::DELETE, &*selection.start.par(), &*undoendpit);
1311 // there are two cases: cut only within one paragraph or
1312 // more than one paragraph
1313 if (selection.start.par() == selection.end.par()) {
1314 // only within one paragraph
1315 endpit = selection.end.par();
1316 int pos = selection.end.pos();
1317 CutAndPaste::cutSelection(&*selection.start.par(), &*endpit,
1318 selection.start.pos(), pos,
1319 bv()->buffer()->params.textclass,
1321 selection.end.pos(pos);
1323 endpit = selection.end.par();
1324 int pos = selection.end.pos();
1325 CutAndPaste::cutSelection(&*selection.start.par(), &*endpit,
1326 selection.start.pos(), pos,
1327 bv()->buffer()->params.textclass,
1330 selection.end.par(endpit);
1331 selection.end.pos(pos);
1332 cursor.pos(selection.end.pos());
1336 // sometimes necessary
1338 selection.start.par()->stripLeadingSpaces();
1340 redoParagraphs(selection.start, endpit);
1342 // cutSelection can invalidate the cursor so we need to set
1344 // we prefer the end for when tracking changes
1345 cursor = selection.end;
1347 // need a valid cursor. (Lgb)
1350 setCursor(cursor.par(), cursor.pos());
1351 selection.cursor = cursor;
1356 void LyXText::copySelection()
1358 // stuff the selection onto the X clipboard, from an explicit copy request
1359 bv()->stuffClipboard(selectionAsString(bv()->buffer(), true));
1361 // this doesnt make sense, if there is no selection
1362 if (!selection.set())
1365 // ok we have a selection. This is always between selection.start
1366 // and sel_end cursor
1368 // copy behind a space if there is one
1369 while (selection.start.par()->size() > selection.start.pos()
1370 && selection.start.par()->isLineSeparator(selection.start.pos())
1371 && (selection.start.par() != selection.end.par()
1372 || selection.start.pos() < selection.end.pos()))
1373 selection.start.pos(selection.start.pos() + 1);
1375 CutAndPaste::copySelection(&*selection.start.par(),
1376 &*selection.end.par(),
1377 selection.start.pos(), selection.end.pos(),
1378 bv()->buffer()->params.textclass);
1382 void LyXText::pasteSelection()
1384 // this does not make sense, if there is nothing to paste
1385 if (!CutAndPaste::checkPastePossible())
1388 setUndo(bv(), Undo::INSERT,
1389 &*cursor.par(), &*boost::next(cursor.par()));
1392 ParagraphList::iterator actpit = cursor.par();
1393 int pos = cursor.pos();
1395 Paragraph * actpar = &*actpit;
1396 CutAndPaste::pasteSelection(&actpar, &endpar, pos,
1397 bv()->buffer()->params.textclass);
1399 redoParagraphs(cursor, endpar);
1401 setCursor(cursor.par(), cursor.pos());
1404 selection.cursor = cursor;
1405 setCursor(actpit, pos);
1411 void LyXText::setSelectionRange(lyx::pos_type length)
1416 selection.cursor = cursor;
1423 // simple replacing. The font of the first selected character is used
1424 void LyXText::replaceSelectionWithString(string const & str)
1426 setCursorParUndo(bv());
1429 if (!selection.set()) { // create a dummy selection
1430 selection.end = cursor;
1431 selection.start = cursor;
1434 // Get font setting before we cut
1435 pos_type pos = selection.end.pos();
1436 LyXFont const font = selection.start.par()
1437 ->getFontSettings(bv()->buffer()->params,
1438 selection.start.pos());
1440 // Insert the new string
1441 for (string::const_iterator cit = str.begin(); cit != str.end(); ++cit) {
1442 selection.end.par()->insertChar(pos, (*cit), font);
1446 // Cut the selection
1447 cutSelection(true, false);
1453 // needed to insert the selection
1454 void LyXText::insertStringAsLines(string const & str)
1456 ParagraphList::iterator pit = cursor.par();
1457 pos_type pos = cursor.pos();
1458 ParagraphList::iterator endpit = boost::next(cursor.par());
1460 setCursorParUndo(bv());
1462 // only to be sure, should not be neccessary
1465 bv()->buffer()->insertStringAsLines(pit, pos, current_font, str);
1467 redoParagraphs(cursor, endpit);
1468 setCursor(cursor.par(), cursor.pos());
1469 selection.cursor = cursor;
1470 setCursor(pit, pos);
1475 // turns double-CR to single CR, others where converted into one
1476 // blank. Then InsertStringAsLines is called
1477 void LyXText::insertStringAsParagraphs(string const & str)
1479 string linestr(str);
1480 bool newline_inserted = false;
1481 for (string::size_type i = 0; i < linestr.length(); ++i) {
1482 if (linestr[i] == '\n') {
1483 if (newline_inserted) {
1484 // we know that \r will be ignored by
1485 // InsertStringA. Of course, it is a dirty
1486 // trick, but it works...
1487 linestr[i - 1] = '\r';
1491 newline_inserted = true;
1493 } else if (IsPrintable(linestr[i])) {
1494 newline_inserted = false;
1497 insertStringAsLines(linestr);
1501 void LyXText::checkParagraph(ParagraphList::iterator pit, pos_type pos)
1503 LyXCursor tmpcursor;
1507 RowList::iterator row = getRow(pit, pos, y);
1508 RowList::iterator beg = rows().begin();
1510 // is there a break one row above
1512 && boost::prior(row)->par() == row->par()) {
1513 z = rowBreakPoint(*boost::prior(row));
1514 if (z >= row->pos()) {
1515 // set the dimensions of the row above
1516 y -= boost::prior(row)->height();
1519 breakAgain(boost::prior(row));
1521 // set the cursor again. Otherwise
1522 // dangling pointers are possible
1523 setCursor(cursor.par(), cursor.pos(),
1524 false, cursor.boundary());
1525 selection.cursor = cursor;
1530 int const tmpheight = row->height();
1531 pos_type const tmplast = lastPos(*this, row);
1534 if (row->height() == tmpheight && lastPos(*this, row) == tmplast) {
1535 postRowPaint(row, y);
1540 // check the special right address boxes
1541 if (pit->layout()->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1548 redoDrawingOfParagraph(tmpcursor);
1551 // set the cursor again. Otherwise dangling pointers are possible
1552 // also set the selection
1554 if (selection.set()) {
1556 setCursorIntern(selection.cursor.par(), selection.cursor.pos(),
1557 false, selection.cursor.boundary());
1558 selection.cursor = cursor;
1559 setCursorIntern(selection.start.par(),
1560 selection.start.pos(),
1561 false, selection.start.boundary());
1562 selection.start = cursor;
1563 setCursorIntern(selection.end.par(),
1564 selection.end.pos(),
1565 false, selection.end.boundary());
1566 selection.end = cursor;
1567 setCursorIntern(last_sel_cursor.par(),
1568 last_sel_cursor.pos(),
1569 false, last_sel_cursor.boundary());
1570 last_sel_cursor = cursor;
1573 setCursorIntern(cursor.par(), cursor.pos(),
1574 false, cursor.boundary());
1578 // returns false if inset wasn't found
1579 bool LyXText::updateInset(Inset * inset)
1581 // first check the current paragraph
1582 int pos = cursor.par()->getPositionOfInset(inset);
1584 checkParagraph(cursor.par(), pos);
1588 // check every paragraph
1590 ParagraphList::iterator par = ownerParagraphs().begin();
1591 ParagraphList::iterator end = ownerParagraphs().end();
1592 for (; par != end; ++par) {
1593 pos = par->getPositionOfInset(inset);
1595 checkParagraph(par, pos);
1604 bool LyXText::setCursor(ParagraphList::iterator pit,
1606 bool setfont, bool boundary)
1608 LyXCursor old_cursor = cursor;
1609 setCursorIntern(pit, pos, setfont, boundary);
1610 return deleteEmptyParagraphMechanism(old_cursor);
1614 void LyXText::setCursor(LyXCursor & cur, ParagraphList::iterator pit,
1615 pos_type pos, bool boundary)
1617 lyx::Assert(pit != ownerParagraphs().end());
1621 cur.boundary(boundary);
1623 // get the cursor y position in text
1625 RowList::iterator row = getRow(pit, pos, y);
1626 RowList::iterator beg = rows().begin();
1628 RowList::iterator old_row = row;
1630 // if we are before the first char of this row and are still in the
1631 // same paragraph and there is a previous row then put the cursor on
1632 // the end of the previous row
1633 cur.iy(y + row->baseline());
1635 if (row != beg && pos &&
1636 boost::prior(row)->par() == row->par() &&
1637 pos < pit->size() &&
1638 pit->getChar(pos) == Paragraph::META_INSET &&
1639 (ins = pit->getInset(pos)) && (ins->needFullRow() || ins->display()))
1646 // y is now the beginning of the cursor row
1647 y += row->baseline();
1648 // y is now the cursor baseline
1651 pos_type last = lastPrintablePos(*this, old_row);
1653 // None of these should happen, but we're scaredy-cats
1654 if (pos > pit->size()) {
1655 lyxerr << "dont like 1 please report" << endl;
1658 } else if (pos > last + 1) {
1659 lyxerr << "dont like 2 please report" << endl;
1660 // This shouldn't happen.
1663 } else if (pos < row->pos()) {
1664 lyxerr << "dont like 3 please report" << endl;
1669 // now get the cursors x position
1670 float x = getCursorX(row, pos, last, boundary);
1673 if (old_row != row) {
1674 x = getCursorX(old_row, pos, last, boundary);
1678 /* We take out this for the time being because 1) the redraw code is not
1679 prepared to this yet and 2) because some good policy has yet to be decided
1680 while editting: for instance how to act on rows being created/deleted
1684 //if the cursor is in a visible row, anchor to it
1686 if (topy < y && y < topy + bv()->workHeight())
1692 float LyXText::getCursorX(RowList::iterator rit,
1693 pos_type pos, pos_type last, bool boundary) const
1695 pos_type cursor_vpos = 0;
1697 float fill_separator;
1699 float fill_label_hfill;
1700 // This call HAS to be here because of the BidiTables!!!
1701 prepareToPrint(rit, x, fill_separator, fill_hfill,
1704 ParagraphList::iterator rit_par = rit->par();
1705 pos_type const rit_pos = rit->pos();
1708 cursor_vpos = rit_pos;
1709 else if (pos > last && !boundary)
1710 cursor_vpos = (rit_par->isRightToLeftPar(bv()->buffer()->params))
1711 ? rit_pos : last + 1;
1712 else if (pos > rit_pos && (pos > last || boundary))
1713 /// Place cursor after char at (logical) position pos - 1
1714 cursor_vpos = (bidi_level(pos - 1) % 2 == 0)
1715 ? log2vis(pos - 1) + 1 : log2vis(pos - 1);
1717 /// Place cursor before char at (logical) position pos
1718 cursor_vpos = (bidi_level(pos) % 2 == 0)
1719 ? log2vis(pos) : log2vis(pos) + 1;
1721 pos_type body_pos = rit_par->beginningOfBody();
1722 if ((body_pos > 0) &&
1723 ((body_pos - 1 > last) || !rit_par->isLineSeparator(body_pos - 1)))
1726 for (pos_type vpos = rit_pos; vpos < cursor_vpos; ++vpos) {
1727 pos_type pos = vis2log(vpos);
1728 if (body_pos > 0 && pos == body_pos - 1) {
1729 x += fill_label_hfill +
1730 font_metrics::width(
1731 rit_par->layout()->labelsep,
1732 getLabelFont(bv()->buffer(), rit_par));
1733 if (rit_par->isLineSeparator(body_pos - 1))
1734 x -= singleWidth(rit_par, body_pos - 1);
1737 if (hfillExpansion(*this, rit, pos)) {
1738 x += singleWidth(rit_par, pos);
1739 if (pos >= body_pos)
1742 x += fill_label_hfill;
1743 } else if (rit_par->isSeparator(pos)) {
1744 x += singleWidth(rit_par, pos);
1745 if (pos >= body_pos)
1746 x += fill_separator;
1748 x += singleWidth(rit_par, pos);
1754 void LyXText::setCursorIntern(ParagraphList::iterator pit,
1755 pos_type pos, bool setfont, bool boundary)
1757 InsetText * it = static_cast<InsetText *>(pit->inInset());
1759 if (it != inset_owner) {
1760 lyxerr[Debug::INSETS] << "InsetText is " << it
1762 << "inset_owner is "
1763 << inset_owner << endl;
1764 #ifdef WITH_WARNINGS
1765 #warning I believe this code is wrong. (Lgb)
1766 #warning Jürgen, have a look at this. (Lgb)
1767 #warning Hmmm, I guess you are right but we
1768 #warning should verify when this is needed
1770 // Jürgen, would you like to have a look?
1771 // I guess we need to move the outer cursor
1772 // and open and lock the inset (bla bla bla)
1773 // stuff I don't know... so can you have a look?
1775 // I moved the lyxerr stuff in here so we can see if
1776 // this is actually really needed and where!
1778 // it->getLyXText(bv())->setCursorIntern(bv(), par, pos, setfont, boundary);
1783 setCursor(cursor, pit, pos, boundary);
1789 void LyXText::setCurrentFont()
1791 pos_type pos = cursor.pos();
1792 ParagraphList::iterator pit = cursor.par();
1794 if (cursor.boundary() && pos > 0)
1798 if (pos == pit->size())
1800 else // potentional bug... BUG (Lgb)
1801 if (pit->isSeparator(pos)) {
1802 if (pos > cursor.row()->pos() &&
1803 bidi_level(pos) % 2 ==
1804 bidi_level(pos - 1) % 2)
1806 else if (pos + 1 < pit->size())
1812 pit->getFontSettings(bv()->buffer()->params, pos);
1813 real_current_font = getFont(bv()->buffer(), pit, pos);
1815 if (cursor.pos() == pit->size() &&
1816 isBoundary(bv()->buffer(), *pit, cursor.pos()) &&
1817 !cursor.boundary()) {
1818 Language const * lang =
1819 pit->getParLanguage(bv()->buffer()->params);
1820 current_font.setLanguage(lang);
1821 current_font.setNumber(LyXFont::OFF);
1822 real_current_font.setLanguage(lang);
1823 real_current_font.setNumber(LyXFont::OFF);
1828 // returns the column near the specified x-coordinate of the row
1829 // x is set to the real beginning of this column
1831 LyXText::getColumnNearX(RowList::iterator rit, int & x, bool & boundary) const
1834 float fill_separator;
1836 float fill_label_hfill;
1838 prepareToPrint(rit, tmpx, fill_separator,
1839 fill_hfill, fill_label_hfill);
1841 pos_type vc = rit->pos();
1842 pos_type last = lastPrintablePos(*this, rit);
1845 ParagraphList::iterator rit_par = rit->par();
1846 LyXLayout_ptr const & layout = rit->par()->layout();
1848 bool left_side = false;
1850 pos_type body_pos = rit_par->beginningOfBody();
1851 float last_tmpx = tmpx;
1854 (body_pos - 1 > last ||
1855 !rit_par->isLineSeparator(body_pos - 1)))
1858 // check for empty row
1859 if (!rit_par->size()) {
1864 while (vc <= last && tmpx <= x) {
1867 if (body_pos > 0 && c == body_pos - 1) {
1868 tmpx += fill_label_hfill +
1869 font_metrics::width(layout->labelsep,
1870 getLabelFont(bv()->buffer(), &*rit_par));
1871 if (rit_par->isLineSeparator(body_pos - 1))
1872 tmpx -= singleWidth(rit_par, body_pos - 1);
1875 if (hfillExpansion(*this, rit, c)) {
1876 tmpx += singleWidth(rit_par, c);
1880 tmpx += fill_label_hfill;
1881 } else if (rit_par->isSeparator(c)) {
1882 tmpx += singleWidth(rit_par, c);
1884 tmpx+= fill_separator;
1886 tmpx += singleWidth(rit_par, c);
1891 if ((tmpx + last_tmpx) / 2 > x) {
1896 if (vc > last + 1) // This shouldn't happen.
1900 // This (rtl_support test) is not needed, but gives
1901 // some speedup if rtl_support=false
1902 bool const lastrow = lyxrc.rtl_support &&
1903 (boost::next(rit) == rowlist_.end() ||
1904 boost::next(rit)->par() != rit_par);
1905 // If lastrow is false, we don't need to compute
1906 // the value of rtl.
1907 bool const rtl = (lastrow)
1908 ? rit_par->isRightToLeftPar(bv()->buffer()->params)
1911 ((rtl && left_side && vc == rit->pos() && x < tmpx - 5) ||
1912 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5)))
1914 else if (vc == rit->pos()) {
1916 if (bidi_level(c) % 2 == 1)
1919 c = vis2log(vc - 1);
1920 bool const rtl = (bidi_level(c) % 2 == 1);
1921 if (left_side == rtl) {
1923 boundary = isBoundary(bv()->buffer(), *rit_par, c);
1927 if (rit->pos() <= last && c > last
1928 && rit_par->isNewline(last)) {
1929 if (bidi_level(last) % 2 == 0)
1930 tmpx -= singleWidth(rit_par, last);
1932 tmpx += singleWidth(rit_par, last);
1942 void LyXText::setCursorFromCoordinates(int x, int y)
1944 LyXCursor old_cursor = cursor;
1946 setCursorFromCoordinates(cursor, x, y);
1948 deleteEmptyParagraphMechanism(old_cursor);
1955 * return true if the cursor given is at the end of a row,
1956 * and the next row is filled by an inset that spans an entire
1959 bool beforeFullRowInset(LyXText & lt, LyXCursor const & cur) {
1960 RowList::iterator row = cur.row();
1961 if (boost::next(row) == lt.rows().end())
1963 Row const & next = *boost::next(row);
1965 if (next.pos() != cur.pos() || next.par() != cur.par())
1968 if (cur.pos() == cur.par()->size()
1969 || !cur.par()->isInset(cur.pos()))
1971 Inset const * inset = cur.par()->getInset(cur.pos());
1972 if (inset->needFullRow() || inset->display())
1979 void LyXText::setCursorFromCoordinates(LyXCursor & cur, int x, int y)
1981 // Get the row first.
1983 RowList::iterator row = getRowNearY(y);
1985 pos_type const column = getColumnNearX(row, x, bound);
1986 cur.par(row->par());
1987 cur.pos(row->pos() + column);
1989 cur.y(y + row->baseline());
1992 if (beforeFullRowInset(*this, cur)) {
1993 pos_type last = lastPrintablePos(*this, row);
1994 float x = getCursorX(boost::next(row), cur.pos(), last, bound);
1996 cur.iy(y + row->height() + boost::next(row)->baseline());
1997 cur.irow(boost::next(row));
2003 cur.boundary(bound);
2007 void LyXText::cursorLeft(bool internal)
2009 if (cursor.pos() > 0) {
2010 bool boundary = cursor.boundary();
2011 setCursor(cursor.par(), cursor.pos() - 1, true, false);
2012 if (!internal && !boundary &&
2013 isBoundary(bv()->buffer(), *cursor.par(), cursor.pos() + 1))
2014 setCursor(cursor.par(), cursor.pos() + 1, true, true);
2015 } else if (cursor.par() != ownerParagraphs().begin()) { // steps into the above paragraph.
2016 ParagraphList::iterator pit = boost::prior(cursor.par());
2017 setCursor(pit, pit->size());
2022 void LyXText::cursorRight(bool internal)
2024 bool const at_end = (cursor.pos() == cursor.par()->size());
2025 bool const at_newline = !at_end &&
2026 cursor.par()->isNewline(cursor.pos());
2028 if (!internal && cursor.boundary() && !at_newline)
2029 setCursor(cursor.par(), cursor.pos(), true, false);
2031 setCursor(cursor.par(), cursor.pos() + 1, true, false);
2033 isBoundary(bv()->buffer(), *cursor.par(), cursor.pos()))
2034 setCursor(cursor.par(), cursor.pos(), true, true);
2035 } else if (boost::next(cursor.par()) != ownerParagraphs().end())
2036 setCursor(boost::next(cursor.par()), 0);
2040 void LyXText::cursorUp(bool selecting)
2043 int x = cursor.x_fix();
2044 int y = cursor.y() - cursor.row()->baseline() - 1;
2045 setCursorFromCoordinates(x, y);
2048 int y1 = cursor.iy() - topy;
2051 Inset * inset_hit = checkInsetHit(x, y1);
2052 if (inset_hit && isHighlyEditableInset(inset_hit)) {
2053 inset_hit->edit(bv(), x, y - (y2 - y1), mouse_button::none);
2057 setCursorFromCoordinates(bv(), cursor.x_fix(),
2058 cursor.y() - cursor.row()->baseline() - 1);
2063 void LyXText::cursorDown(bool selecting)
2066 int x = cursor.x_fix();
2067 int y = cursor.y() - cursor.row()->baseline() +
2068 cursor.row()->height() + 1;
2069 setCursorFromCoordinates(x, y);
2070 if (!selecting && cursor.row() == cursor.irow()) {
2072 int y1 = cursor.iy() - topy;
2075 Inset * inset_hit = checkInsetHit(x, y1);
2076 if (inset_hit && isHighlyEditableInset(inset_hit)) {
2077 inset_hit->edit(bv(), x, y - (y2 - y1), mouse_button::none);
2081 setCursorFromCoordinates(bv(), cursor.x_fix(),
2082 cursor.y() - cursor.row()->baseline()
2083 + cursor.row()->height() + 1);
2088 void LyXText::cursorUpParagraph()
2090 if (cursor.pos() > 0) {
2091 setCursor(cursor.par(), 0);
2093 else if (cursor.par() != ownerParagraphs().begin()) {
2094 setCursor(boost::prior(cursor.par()), 0);
2099 void LyXText::cursorDownParagraph()
2101 if (boost::next(cursor.par()) != ownerParagraphs().end()) {
2102 setCursor(boost::next(cursor.par()), 0);
2104 setCursor(cursor.par(), cursor.par()->size());
2108 // fix the cursor `cur' after a characters has been deleted at `where'
2109 // position. Called by deleteEmptyParagraphMechanism
2110 void LyXText::fixCursorAfterDelete(LyXCursor & cur,
2111 LyXCursor const & where)
2113 // if cursor is not in the paragraph where the delete occured,
2115 if (cur.par() != where.par())
2118 // if cursor position is after the place where the delete occured,
2120 if (cur.pos() > where.pos())
2121 cur.pos(cur.pos()-1);
2123 // check also if we don't want to set the cursor on a spot behind the
2124 // pagragraph because we erased the last character.
2125 if (cur.pos() > cur.par()->size())
2126 cur.pos(cur.par()->size());
2128 // recompute row et al. for this cursor
2129 setCursor(cur, cur.par(), cur.pos(), cur.boundary());
2133 bool LyXText::deleteEmptyParagraphMechanism(LyXCursor const & old_cursor)
2135 // Would be wrong to delete anything if we have a selection.
2136 if (selection.set())
2139 // We allow all kinds of "mumbo-jumbo" when freespacing.
2140 if (old_cursor.par()->layout()->free_spacing
2141 || old_cursor.par()->isFreeSpacing()) {
2145 /* Ok I'll put some comments here about what is missing.
2146 I have fixed BackSpace (and thus Delete) to not delete
2147 double-spaces automagically. I have also changed Cut,
2148 Copy and Paste to hopefully do some sensible things.
2149 There are still some small problems that can lead to
2150 double spaces stored in the document file or space at
2151 the beginning of paragraphs. This happens if you have
2152 the cursor betwenn to spaces and then save. Or if you
2153 cut and paste and the selection have a space at the
2154 beginning and then save right after the paste. I am
2155 sure none of these are very hard to fix, but I will
2156 put out 1.1.4pre2 with FIX_DOUBLE_SPACE defined so
2157 that I can get some feedback. (Lgb)
2160 // If old_cursor.pos() == 0 and old_cursor.pos()(1) == LineSeparator
2161 // delete the LineSeparator.
2164 // If old_cursor.pos() == 1 and old_cursor.pos()(0) == LineSeparator
2165 // delete the LineSeparator.
2168 // If the pos around the old_cursor were spaces, delete one of them.
2169 if (old_cursor.par() != cursor.par()
2170 || old_cursor.pos() != cursor.pos()) {
2171 // Only if the cursor has really moved
2173 if (old_cursor.pos() > 0
2174 && old_cursor.pos() < old_cursor.par()->size()
2175 && old_cursor.par()->isLineSeparator(old_cursor.pos())
2176 && old_cursor.par()->isLineSeparator(old_cursor.pos() - 1)) {
2177 old_cursor.par()->erase(old_cursor.pos() - 1);
2178 redoParagraphs(old_cursor, boost::next(old_cursor.par()));
2180 #ifdef WITH_WARNINGS
2181 #warning This will not work anymore when we have multiple views of the same buffer
2182 // In this case, we will have to correct also the cursors held by
2183 // other bufferviews. It will probably be easier to do that in a more
2184 // automated way in LyXCursor code. (JMarc 26/09/2001)
2186 // correct all cursors held by the LyXText
2187 fixCursorAfterDelete(cursor, old_cursor);
2188 fixCursorAfterDelete(selection.cursor,
2190 fixCursorAfterDelete(selection.start,
2192 fixCursorAfterDelete(selection.end, old_cursor);
2193 fixCursorAfterDelete(last_sel_cursor,
2195 fixCursorAfterDelete(toggle_cursor, old_cursor);
2196 fixCursorAfterDelete(toggle_end_cursor,
2202 // don't delete anything if this is the ONLY paragraph!
2203 if (ownerParagraphs().size() == 1)
2206 // Do not delete empty paragraphs with keepempty set.
2207 if (old_cursor.par()->layout()->keepempty)
2210 // only do our magic if we changed paragraph
2211 if (old_cursor.par() == cursor.par())
2214 // record if we have deleted a paragraph
2215 // we can't possibly have deleted a paragraph before this point
2216 bool deleted = false;
2218 if (old_cursor.par()->empty() ||
2219 (old_cursor.par()->size() == 1 &&
2220 old_cursor.par()->isLineSeparator(0))) {
2221 // ok, we will delete anything
2222 LyXCursor tmpcursor;
2226 bool selection_position_was_oldcursor_position = (
2227 selection.cursor.par() == old_cursor.par()
2228 && selection.cursor.pos() == old_cursor.pos());
2230 if (old_cursor.row() != rows().begin()) {
2232 prevrow = boost::prior(old_cursor.row());
2233 const_cast<LyXText *>(this)->postPaint(old_cursor.y() - old_cursor.row()->baseline() - prevrow->height());
2235 cursor = old_cursor; // that undo can restore the right cursor position
2236 #warning FIXME. --end() iterator is usable here
2237 ParagraphList::iterator endpit = boost::next(old_cursor.par());
2238 while (endpit != ownerParagraphs().end() &&
2239 endpit->getDepth()) {
2243 setUndo(bv(), Undo::DELETE, &*old_cursor.par(), &*endpit);
2247 removeRow(old_cursor.row());
2249 ownerParagraphs().erase(old_cursor.par());
2251 /* Breakagain the next par. Needed because of
2252 * the parindent that can occur or dissappear.
2253 * The next row can change its height, if
2254 * there is another layout before */
2255 if (boost::next(prevrow) != rows().end()) {
2256 breakAgain(boost::next(prevrow));
2259 setHeightOfRow(prevrow);
2261 RowList::iterator nextrow = boost::next(old_cursor.row());
2262 const_cast<LyXText *>(this)->postPaint(
2263 old_cursor.y() - old_cursor.row()->baseline());
2266 cursor = old_cursor; // that undo can restore the right cursor position
2267 #warning FIXME. --end() iterator is usable here
2268 ParagraphList::iterator endpit = boost::next(old_cursor.par());
2269 while (endpit != ownerParagraphs().end() &&
2270 endpit->getDepth()) {
2274 setUndo(bv(), Undo::DELETE, &*old_cursor.par(), &*endpit);
2278 removeRow(old_cursor.row());
2280 ownerParagraphs().erase(old_cursor.par());
2282 /* Breakagain the next par. Needed because of
2283 the parindent that can occur or dissappear.
2284 The next row can change its height, if
2285 there is another layout before */
2286 if (nextrow != rows().end()) {
2287 breakAgain(nextrow);
2293 setCursorIntern(cursor.par(), cursor.pos());
2295 if (selection_position_was_oldcursor_position) {
2296 // correct selection
2297 selection.cursor = cursor;
2301 if (old_cursor.par()->stripLeadingSpaces()) {
2302 redoParagraphs(old_cursor, boost::next(old_cursor.par()));
2304 setCursorIntern(cursor.par(), cursor.pos());
2305 selection.cursor = cursor;
2312 ParagraphList & LyXText::ownerParagraphs() const
2315 return inset_owner->paragraphs;
2317 return bv_owner->buffer()->paragraphs;
2321 LyXText::refresh_status LyXText::refreshStatus() const
2323 return refresh_status_;
2327 void LyXText::clearPaint()
2329 refresh_status_ = REFRESH_NONE;
2330 refresh_row = rows().end();
2335 void LyXText::postPaint(int start_y)
2337 refresh_status old = refresh_status_;
2339 refresh_status_ = REFRESH_AREA;
2340 refresh_row = rows().end();
2342 if (old != REFRESH_NONE && refresh_y < start_y)
2345 refresh_y = start_y;
2350 // We are an inset's lyxtext. Tell the top-level lyxtext
2351 // it needs to update the row we're in.
2352 LyXText * t = bv()->text;
2353 t->postRowPaint(t->cursor.row(), t->cursor.y() - t->cursor.row()->baseline());
2357 // FIXME: we should probably remove this y parameter,
2358 // make refresh_y be 0, and use row->y etc.
2359 void LyXText::postRowPaint(RowList::iterator rit, int start_y)
2361 if (refresh_status_ != REFRESH_NONE && refresh_y < start_y) {
2362 refresh_status_ = REFRESH_AREA;
2365 refresh_y = start_y;
2368 if (refresh_status_ == REFRESH_AREA)
2371 refresh_status_ = REFRESH_ROW;
2377 // We are an inset's lyxtext. Tell the top-level lyxtext
2378 // it needs to update the row we're in.
2379 LyXText * t = bv()->text;
2380 t->postRowPaint(t->cursor.row(), t->cursor.y() - t->cursor.row()->baseline());
2384 bool LyXText::isInInset() const
2386 // Sub-level has non-null bv owner and
2387 // non-null inset owner.
2388 return inset_owner != 0 && bv_owner != 0;
2392 int defaultRowHeight()
2394 LyXFont const font(LyXFont::ALL_SANE);
2395 return int(font_metrics::maxAscent(font)
2396 + font_metrics::maxDescent(font) * 1.5);