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();
82 copylayouttype.erase();
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 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();
657 // In here prevrit could be set to rows().end(). (Lgb)
659 first_phys_pit = tmprit->par();
660 while (tmprit != rows().begin()
661 && boost::prior(tmprit)->par() == first_phys_pit)
664 y -= tmprit->height();
667 // Is it possible to put the prevrit setting in here? (Lgb)
670 RowList::iterator prevrit;
671 bool good_prevrit = false;
673 // It seems to mee that good_prevrit is not needed if we let
674 // a bad prevrit have the value rows().end() (Lgb)
675 if (tmprit != rows().begin()) {
676 prevrit = boost::prior(tmprit);
681 while (tmprit != rows().end() && tmprit->par() != endpit) {
682 RowList::iterator tmprit2 = tmprit++;
686 // Reinsert the paragraphs.
687 ParagraphList::iterator tmppit = first_phys_pit;
689 // See if this loop can be rewritten as a while loop instead.
690 // That should also make the code a bit easier to read. (Lgb)
692 if (tmppit != ownerParagraphs().end()) {
693 insertParagraph(tmppit, tmprit);
694 while (tmprit != rows().end()
695 && tmprit->par() == tmppit) {
700 } while (tmppit != ownerParagraphs().end() && tmppit != endpit);
703 // If the above changes are done, then we can compare prevrit
704 // with rows().end() here. (Lgb)
706 setHeightOfRow(prevrit);
707 const_cast<LyXText *>(this)->postPaint(y - prevrit->height());
709 setHeightOfRow(rows().begin());
710 const_cast<LyXText *>(this)->postPaint(0);
712 if (tmprit != rows().end())
713 setHeightOfRow(tmprit);
719 void LyXText::fullRebreak()
721 if (rows().empty()) {
726 RowList::iterator rows_end = rows().end();
728 if (need_break_row != rows_end) {
729 breakAgain(need_break_row);
730 need_break_row = rows_end;
736 // important for the screen
739 // the cursor set functions have a special mechanism. When they
740 // realize, that you left an empty paragraph, they will delete it.
741 // They also delete the corresponding row
743 // need the selection cursor:
744 void LyXText::setSelection()
746 bool const lsel = selection.set();
748 if (!selection.set()) {
749 last_sel_cursor = selection.cursor;
750 selection.start = selection.cursor;
751 selection.end = selection.cursor;
756 // first the toggling area
757 if (cursor.y() < last_sel_cursor.y()
758 || (cursor.y() == last_sel_cursor.y()
759 && cursor.x() < last_sel_cursor.x())) {
760 toggle_end_cursor = last_sel_cursor;
761 toggle_cursor = cursor;
763 toggle_end_cursor = cursor;
764 toggle_cursor = last_sel_cursor;
767 last_sel_cursor = cursor;
769 // and now the whole selection
771 if (selection.cursor.par() == cursor.par())
772 if (selection.cursor.pos() < cursor.pos()) {
773 selection.end = cursor;
774 selection.start = selection.cursor;
776 selection.end = selection.cursor;
777 selection.start = cursor;
779 else if (selection.cursor.y() < cursor.y() ||
780 (selection.cursor.y() == cursor.y()
781 && selection.cursor.x() < cursor.x())) {
782 selection.end = cursor;
783 selection.start = selection.cursor;
786 selection.end = selection.cursor;
787 selection.start = cursor;
790 // a selection with no contents is not a selection
791 if (selection.start.par() == selection.end.par() &&
792 selection.start.pos() == selection.end.pos())
793 selection.set(false);
795 if (inset_owner && (selection.set() || lsel))
796 inset_owner->setUpdateStatus(bv(), InsetText::SELECTION);
800 string const LyXText::selectionAsString(Buffer const * buffer,
803 if (!selection.set()) return string();
805 // should be const ...
806 ParagraphList::iterator startpit = selection.start.par();
807 ParagraphList::iterator endpit = selection.end.par();
808 pos_type const startpos(selection.start.pos());
809 pos_type const endpos(selection.end.pos());
811 if (startpit == endpit) {
812 return startpit->asString(buffer, startpos, endpos, label);
817 // First paragraph in selection
818 result += startpit->asString(buffer, startpos, startpit->size(), label) + "\n\n";
820 // The paragraphs in between (if any)
821 ParagraphList::iterator pit = boost::next(startpit);
822 for (; pit != endpit; ++pit) {
823 result += pit->asString(buffer, 0, pit->size(), label) + "\n\n";
826 // Last paragraph in selection
827 result += endpit->asString(buffer, 0, endpos, label);
833 void LyXText::clearSelection()
835 selection.set(false);
836 selection.mark(false);
837 last_sel_cursor = selection.end = selection.start = selection.cursor = cursor;
838 // reset this in the bv_owner!
839 if (bv_owner && bv_owner->text)
840 bv_owner->text->xsel_cache.set(false);
844 void LyXText::cursorHome()
846 setCursor(cursor.par(), cursor.row()->pos());
850 void LyXText::cursorEnd()
852 if (cursor.par()->empty())
855 RowList::iterator rit = cursor.row();
856 RowList::iterator next_rit = boost::next(rit);
857 ParagraphList::iterator pit = rit->par();
858 pos_type last_pos = lastPos(*this, rit);
860 if (next_rit == rows().end() || next_rit->par() != pit) {
864 (pit->getChar(last_pos) != ' ' && !pit->isNewline(last_pos))) {
869 setCursor(pit, last_pos);
873 void LyXText::cursorTop()
875 setCursor(ownerParagraphs().begin(), 0);
879 void LyXText::cursorBottom()
882 // This is how it should be:
883 // ParagraphList::iterator lastpit = boost::prior(ownerParagraphs().end());
884 ParagraphList::iterator lastpit = &ownerParagraphs().back();
885 int pos = lastpit->size();
886 setCursor(lastpit, pos);
890 void LyXText::toggleFree(LyXFont const & font, bool toggleall)
892 // If the mask is completely neutral, tell user
893 if (font == LyXFont(LyXFont::ALL_IGNORE)) {
894 // Could only happen with user style
895 bv()->owner()->message(_("No font change defined. Use Character under the Layout menu to define font change."));
899 // Try implicit word selection
900 // If there is a change in the language the implicit word selection
902 LyXCursor resetCursor = cursor;
903 bool implicitSelection = (font.language() == ignore_language
904 && font.number() == LyXFont::IGNORE)
905 ? selectWordWhenUnderCursor(WHOLE_WORD_STRICT) : false;
908 setFont(font, toggleall);
910 // Implicit selections are cleared afterwards
911 //and cursor is set to the original position.
912 if (implicitSelection) {
914 cursor = resetCursor;
915 setCursor(cursor.par(), cursor.pos());
916 selection.cursor = cursor;
919 inset_owner->setUpdateStatus(bv(), InsetText::CURSOR_PAR);
923 string LyXText::getStringToIndex()
925 // Try implicit word selection
926 // If there is a change in the language the implicit word selection
928 LyXCursor const reset_cursor = cursor;
929 bool const implicitSelection = selectWordWhenUnderCursor(PREVIOUS_WORD);
932 if (!selection.set())
933 bv()->owner()->message(_("Nothing to index!"));
934 else if (selection.start.par() != selection.end.par())
935 bv()->owner()->message(_("Cannot index more than one paragraph!"));
937 idxstring = selectionAsString(bv()->buffer(), false);
939 // Reset cursors to their original position.
940 cursor = reset_cursor;
941 setCursor(cursor.par(), cursor.pos());
942 selection.cursor = cursor;
944 // Clear the implicit selection.
945 if (implicitSelection)
952 // the DTP switches for paragraphs. LyX will store them in the first
953 // physicla paragraph. When a paragraph is broken, the top settings rest,
954 // the bottom settings are given to the new one. So I can make shure,
955 // they do not duplicate themself and you cannnot make dirty things with
958 void LyXText::setParagraph(bool line_top, bool line_bottom,
959 bool pagebreak_top, bool pagebreak_bottom,
960 VSpace const & space_top,
961 VSpace const & space_bottom,
962 Spacing const & spacing,
964 string const & labelwidthstring,
967 LyXCursor tmpcursor = cursor;
968 if (!selection.set()) {
969 selection.start = cursor;
970 selection.end = cursor;
973 // make sure that the depth behind the selection are restored, too
974 ParagraphList::iterator endpit = boost::next(selection.end.par());
975 ParagraphList::iterator undoendpit = endpit;
976 ParagraphList::iterator pars_end = ownerParagraphs().end();
978 if (endpit != pars_end && endpit->getDepth()) {
979 while (endpit != pars_end && endpit->getDepth()) {
983 } else if (endpit!= pars_end) {
984 // because of parindents etc.
988 setUndo(bv(), Undo::EDIT, &*selection.start.par(), &*undoendpit);
991 ParagraphList::iterator tmppit = selection.end.par();
993 while (tmppit != boost::prior(selection.start.par())) {
994 setCursor(tmppit, 0);
995 postPaint(cursor.y() - cursor.row()->baseline());
997 ParagraphList::iterator pit = cursor.par();
998 ParagraphParameters & params = pit->params();
1000 params.lineTop(line_top);
1001 params.lineBottom(line_bottom);
1002 params.pagebreakTop(pagebreak_top);
1003 params.pagebreakBottom(pagebreak_bottom);
1004 params.spaceTop(space_top);
1005 params.spaceBottom(space_bottom);
1006 params.spacing(spacing);
1007 // does the layout allow the new alignment?
1008 LyXLayout_ptr const & layout = pit->layout();
1010 if (align == LYX_ALIGN_LAYOUT)
1011 align = layout->align;
1012 if (align & layout->alignpossible) {
1013 if (align == layout->align)
1014 params.align(LYX_ALIGN_LAYOUT);
1016 params.align(align);
1018 pit->setLabelWidthString(labelwidthstring);
1019 params.noindent(noindent);
1020 tmppit = boost::prior(pit);
1023 redoParagraphs(selection.start, endpit);
1026 setCursor(selection.start.par(), selection.start.pos());
1027 selection.cursor = cursor;
1028 setCursor(selection.end.par(), selection.end.pos());
1030 setCursor(tmpcursor.par(), tmpcursor.pos());
1032 bv()->updateInset(inset_owner);
1036 // set the counter of a paragraph. This includes the labels
1037 void LyXText::setCounter(Buffer const * buf, ParagraphList::iterator pit)
1039 LyXTextClass const & textclass = buf->params.getLyXTextClass();
1040 LyXLayout_ptr const & layout = pit->layout();
1042 if (pit != ownerParagraphs().begin()) {
1044 pit->params().appendix(boost::prior(pit)->params().appendix());
1045 if (!pit->params().appendix() &&
1046 pit->params().startOfAppendix()) {
1047 pit->params().appendix(true);
1048 textclass.counters().reset();
1050 pit->enumdepth = boost::prior(pit)->enumdepth;
1051 pit->itemdepth = boost::prior(pit)->itemdepth;
1053 pit->params().appendix(pit->params().startOfAppendix());
1058 /* Maybe we have to increment the enumeration depth.
1059 * BUT, enumeration in a footnote is considered in isolation from its
1060 * surrounding paragraph so don't increment if this is the
1061 * first line of the footnote
1062 * AND, bibliographies can't have their depth changed ie. they
1063 * are always of depth 0
1065 if (pit != ownerParagraphs().begin()
1066 && boost::prior(pit)->getDepth() < pit->getDepth()
1067 && boost::prior(pit)->layout()->labeltype == LABEL_COUNTER_ENUMI
1068 && pit->enumdepth < 3
1069 && layout->labeltype != LABEL_BIBLIO) {
1073 // Maybe we have to decrement the enumeration depth, see note above
1074 if (pit != ownerParagraphs().begin()
1075 && boost::prior(pit)->getDepth() > pit->getDepth()
1076 && layout->labeltype != LABEL_BIBLIO) {
1077 pit->enumdepth = depthHook(pit, ownerParagraphs(),
1078 pit->getDepth())->enumdepth;
1081 if (!pit->params().labelString().empty()) {
1082 pit->params().labelString(string());
1085 if (layout->margintype == MARGIN_MANUAL) {
1086 if (pit->params().labelWidthString().empty()) {
1087 pit->setLabelWidthString(layout->labelstring());
1090 pit->setLabelWidthString(string());
1093 // is it a layout that has an automatic label?
1094 if (layout->labeltype >= LABEL_COUNTER_CHAPTER) {
1095 int const i = layout->labeltype - LABEL_COUNTER_CHAPTER;
1099 if (i >= 0 && i <= buf->params.secnumdepth) {
1103 textclass.counters().step(layout->latexname());
1105 // Is there a label? Useful for Chapter layout
1106 if (!pit->params().appendix()) {
1107 s << layout->labelstring();
1109 s << layout->labelstring_appendix();
1112 // Use of an integer is here less than elegant. For now.
1113 int head = textclass.maxcounter() - LABEL_COUNTER_CHAPTER;
1114 if (!pit->params().appendix()) {
1115 numbertype = "sectioning";
1117 numbertype = "appendix";
1118 if (pit->isRightToLeftPar(buf->params))
1119 langtype = "hebrew";
1124 s << textclass.counters()
1125 .numberLabel(layout->latexname(),
1126 numbertype, langtype, head);
1128 pit->params().labelString(STRCONV(s.str()));
1130 // reset enum counters
1131 textclass.counters().reset("enum");
1132 } else if (layout->labeltype < LABEL_COUNTER_ENUMI) {
1133 textclass.counters().reset("enum");
1134 } else if (layout->labeltype == LABEL_COUNTER_ENUMI) {
1136 // Yes I know this is a really, really! bad solution
1138 string enumcounter("enum");
1140 switch (pit->enumdepth) {
1149 enumcounter += "iv";
1152 // not a valid enumdepth...
1156 textclass.counters().step(enumcounter);
1158 s << textclass.counters()
1159 .numberLabel(enumcounter, "enumeration");
1160 pit->params().labelString(STRCONV(s.str()));
1162 } else if (layout->labeltype == LABEL_BIBLIO) {// ale970302
1163 textclass.counters().step("bibitem");
1164 int number = textclass.counters().value("bibitem");
1165 if (pit->bibitem()) {
1166 pit->bibitem()->setCounter(number);
1167 pit->params().labelString(layout->labelstring());
1169 // In biblio should't be following counters but...
1171 string s = layout->labelstring();
1173 // the caption hack:
1174 if (layout->labeltype == LABEL_SENSITIVE) {
1175 ParagraphList::iterator tmppit = pit;
1178 while (tmppit != ownerParagraphs().end() &&
1180 // the single '=' is intended below
1181 && (in = tmppit->inInset()->owner())) {
1182 if (in->lyxCode() == Inset::FLOAT_CODE ||
1183 in->lyxCode() == Inset::WRAP_CODE) {
1187 tmppit = in->parOwner();
1193 = textclass.floats().getType(static_cast<InsetFloat*>(in)->type());
1195 textclass.counters().step(fl.type());
1197 // Doesn't work... yet.
1198 #if USE_BOOST_FORMAT
1199 s = boost::io::str(boost::format(_("%1$s #:")) % fl.name());
1200 // s << boost::format(_("%1$s %1$d:")
1202 // % buf->counters().value(fl.name());
1205 //o << fl.name() << ' ' << buf->counters().value(fl.name()) << ":";
1206 o << fl.name() << " #:";
1207 s = STRCONV(o.str());
1210 // par->SetLayout(0);
1211 // s = layout->labelstring;
1212 s = _("Senseless: ");
1215 pit->params().labelString(s);
1217 // reset the enumeration counter. They are always reset
1218 // when there is any other layout between
1219 // Just fall-through between the cases so that all
1220 // enum counters deeper than enumdepth is also reset.
1221 switch (pit->enumdepth) {
1223 textclass.counters().reset("enumi");
1225 textclass.counters().reset("enumii");
1227 textclass.counters().reset("enumiii");
1229 textclass.counters().reset("enumiv");
1235 // Updates all counters. Paragraphs with changed label string will be rebroken
1236 void LyXText::updateCounters()
1238 RowList::iterator rowit = rows().begin();
1239 ParagraphList::iterator pit = rowit->par();
1241 // CHECK if this is really needed. (Lgb)
1242 bv()->buffer()->params.getLyXTextClass().counters().reset();
1244 for (; pit != ownerParagraphs().end(); ++pit) {
1245 while (rowit->par() != pit)
1248 string const oldLabel = pit->params().labelString();
1251 if (pit != ownerParagraphs().begin())
1252 maxdepth = boost::prior(pit)->getMaxDepthAfter();
1254 if (pit->params().depth() > maxdepth)
1255 pit->params().depth(maxdepth);
1257 // setCounter can potentially change the labelString.
1258 setCounter(bv()->buffer(), pit);
1260 string const & newLabel = pit->params().labelString();
1262 if (oldLabel.empty() && !newLabel.empty()) {
1263 removeParagraph(rowit);
1264 appendParagraph(rowit);
1270 void LyXText::insertInset(Inset * inset)
1272 if (!cursor.par()->insetAllowed(inset->lyxCode()))
1274 setUndo(bv(), Undo::FINISH, &*cursor.par(),
1275 &*boost::next(cursor.par()));
1277 cursor.par()->insertInset(cursor.pos(), inset);
1278 // Just to rebreak and refresh correctly.
1279 // The character will not be inserted a second time
1280 insertChar(Paragraph::META_INSET);
1281 // If we enter a highly editable inset the cursor should be to before
1282 // the inset. This couldn't happen before as Undo was not handled inside
1283 // inset now after the Undo LyX tries to call inset->Edit(...) again
1284 // and cannot do this as the cursor is behind the inset and GetInset
1285 // does not return the inset!
1286 if (isHighlyEditableInset(inset)) {
1293 void LyXText::copyEnvironmentType()
1295 copylayouttype = cursor.par()->layout()->name();
1299 void LyXText::pasteEnvironmentType()
1301 // do nothing if there has been no previous copyEnvironmentType()
1302 if (!copylayouttype.empty())
1303 setLayout(copylayouttype);
1307 void LyXText::cutSelection(bool doclear, bool realcut)
1309 // Stuff what we got on the clipboard. Even if there is no selection.
1311 // There is a problem with having the stuffing here in that the
1312 // larger the selection the slower LyX will get. This can be
1313 // solved by running the line below only when the selection has
1314 // finished. The solution used currently just works, to make it
1315 // faster we need to be more clever and probably also have more
1316 // calls to stuffClipboard. (Lgb)
1317 bv()->stuffClipboard(selectionAsString(bv()->buffer(), true));
1319 // This doesn't make sense, if there is no selection
1320 if (!selection.set())
1323 // OK, we have a selection. This is always between selection.start
1324 // and selection.end
1326 // make sure that the depth behind the selection are restored, too
1327 ParagraphList::iterator endpit = boost::next(selection.end.par());
1328 ParagraphList::iterator undoendpit = endpit;
1329 ParagraphList::iterator pars_end = ownerParagraphs().end();
1331 if (endpit != pars_end && endpit->getDepth()) {
1332 while (endpit != pars_end && endpit->getDepth()) {
1334 undoendpit = endpit;
1336 } else if (endpit != pars_end) {
1337 // because of parindents etc.
1341 setUndo(bv(), Undo::DELETE, &*selection.start.par(), &*undoendpit);
1343 // there are two cases: cut only within one paragraph or
1344 // more than one paragraph
1345 if (selection.start.par() == selection.end.par()) {
1346 // only within one paragraph
1347 endpit = selection.end.par();
1348 int pos = selection.end.pos();
1349 CutAndPaste::cutSelection(&*selection.start.par(), &*endpit,
1350 selection.start.pos(), pos,
1351 bv()->buffer()->params.textclass,
1353 selection.end.pos(pos);
1355 endpit = selection.end.par();
1356 int pos = selection.end.pos();
1357 CutAndPaste::cutSelection(&*selection.start.par(), &*endpit,
1358 selection.start.pos(), pos,
1359 bv()->buffer()->params.textclass,
1362 selection.end.par(endpit);
1363 selection.end.pos(pos);
1364 cursor.pos(selection.end.pos());
1368 // sometimes necessary
1370 selection.start.par()->stripLeadingSpaces();
1372 redoParagraphs(selection.start, endpit);
1374 // cutSelection can invalidate the cursor so we need to set
1376 // we prefer the end for when tracking changes
1377 cursor = selection.end;
1379 // need a valid cursor. (Lgb)
1382 setCursor(cursor.par(), cursor.pos());
1383 selection.cursor = cursor;
1388 void LyXText::copySelection()
1390 // stuff the selection onto the X clipboard, from an explicit copy request
1391 bv()->stuffClipboard(selectionAsString(bv()->buffer(), true));
1393 // this doesnt make sense, if there is no selection
1394 if (!selection.set())
1397 // ok we have a selection. This is always between selection.start
1398 // and sel_end cursor
1400 // copy behind a space if there is one
1401 while (selection.start.par()->size() > selection.start.pos()
1402 && selection.start.par()->isLineSeparator(selection.start.pos())
1403 && (selection.start.par() != selection.end.par()
1404 || selection.start.pos() < selection.end.pos()))
1405 selection.start.pos(selection.start.pos() + 1);
1407 CutAndPaste::copySelection(&*selection.start.par(),
1408 &*selection.end.par(),
1409 selection.start.pos(), selection.end.pos(),
1410 bv()->buffer()->params.textclass);
1414 void LyXText::pasteSelection()
1416 // this does not make sense, if there is nothing to paste
1417 if (!CutAndPaste::checkPastePossible())
1420 setUndo(bv(), Undo::INSERT,
1421 &*cursor.par(), &*boost::next(cursor.par()));
1424 ParagraphList::iterator actpit = cursor.par();
1425 int pos = cursor.pos();
1427 Paragraph * actpar = &*actpit;
1428 CutAndPaste::pasteSelection(&actpar, &endpar, pos,
1429 bv()->buffer()->params.textclass);
1431 redoParagraphs(cursor, endpar);
1433 setCursor(cursor.par(), cursor.pos());
1436 selection.cursor = cursor;
1437 setCursor(actpit, pos);
1443 void LyXText::setSelectionRange(lyx::pos_type length)
1448 selection.cursor = cursor;
1455 // simple replacing. The font of the first selected character is used
1456 void LyXText::replaceSelectionWithString(string const & str)
1458 setCursorParUndo(bv());
1461 if (!selection.set()) { // create a dummy selection
1462 selection.end = cursor;
1463 selection.start = cursor;
1466 // Get font setting before we cut
1467 pos_type pos = selection.end.pos();
1468 LyXFont const font = selection.start.par()
1469 ->getFontSettings(bv()->buffer()->params,
1470 selection.start.pos());
1472 // Insert the new string
1473 for (string::const_iterator cit = str.begin(); cit != str.end(); ++cit) {
1474 selection.end.par()->insertChar(pos, (*cit), font);
1478 // Cut the selection
1479 cutSelection(true, false);
1485 // needed to insert the selection
1486 void LyXText::insertStringAsLines(string const & str)
1488 ParagraphList::iterator pit = cursor.par();
1489 pos_type pos = cursor.pos();
1490 ParagraphList::iterator endpit = boost::next(cursor.par());
1492 setCursorParUndo(bv());
1494 // only to be sure, should not be neccessary
1497 bv()->buffer()->insertStringAsLines(pit, pos, current_font, str);
1499 redoParagraphs(cursor, endpit);
1500 setCursor(cursor.par(), cursor.pos());
1501 selection.cursor = cursor;
1502 setCursor(pit, pos);
1507 // turns double-CR to single CR, others where converted into one
1508 // blank. Then InsertStringAsLines is called
1509 void LyXText::insertStringAsParagraphs(string const & str)
1511 string linestr(str);
1512 bool newline_inserted = false;
1513 for (string::size_type i = 0; i < linestr.length(); ++i) {
1514 if (linestr[i] == '\n') {
1515 if (newline_inserted) {
1516 // we know that \r will be ignored by
1517 // InsertStringA. Of course, it is a dirty
1518 // trick, but it works...
1519 linestr[i - 1] = '\r';
1523 newline_inserted = true;
1525 } else if (IsPrintable(linestr[i])) {
1526 newline_inserted = false;
1529 insertStringAsLines(linestr);
1533 void LyXText::checkParagraph(ParagraphList::iterator pit, pos_type pos)
1535 LyXCursor tmpcursor;
1539 RowList::iterator row = getRow(pit, pos, y);
1540 RowList::iterator beg = rows().begin();
1542 // is there a break one row above
1544 && boost::prior(row)->par() == row->par()) {
1545 z = rowBreakPoint(*boost::prior(row));
1546 if (z >= row->pos()) {
1547 // set the dimensions of the row above
1548 y -= boost::prior(row)->height();
1551 breakAgain(boost::prior(row));
1553 // set the cursor again. Otherwise
1554 // dangling pointers are possible
1555 setCursor(cursor.par(), cursor.pos(),
1556 false, cursor.boundary());
1557 selection.cursor = cursor;
1562 int const tmpheight = row->height();
1563 pos_type const tmplast = lastPos(*this, row);
1566 if (row->height() == tmpheight && lastPos(*this, row) == tmplast) {
1567 postRowPaint(row, y);
1572 // check the special right address boxes
1573 if (pit->layout()->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1580 redoDrawingOfParagraph(tmpcursor);
1583 // set the cursor again. Otherwise dangling pointers are possible
1584 // also set the selection
1586 if (selection.set()) {
1588 setCursorIntern(selection.cursor.par(), selection.cursor.pos(),
1589 false, selection.cursor.boundary());
1590 selection.cursor = cursor;
1591 setCursorIntern(selection.start.par(),
1592 selection.start.pos(),
1593 false, selection.start.boundary());
1594 selection.start = cursor;
1595 setCursorIntern(selection.end.par(),
1596 selection.end.pos(),
1597 false, selection.end.boundary());
1598 selection.end = cursor;
1599 setCursorIntern(last_sel_cursor.par(),
1600 last_sel_cursor.pos(),
1601 false, last_sel_cursor.boundary());
1602 last_sel_cursor = cursor;
1605 setCursorIntern(cursor.par(), cursor.pos(),
1606 false, cursor.boundary());
1610 // returns false if inset wasn't found
1611 bool LyXText::updateInset(Inset * inset)
1613 // first check the current paragraph
1614 int pos = cursor.par()->getPositionOfInset(inset);
1616 checkParagraph(cursor.par(), pos);
1620 // check every paragraph
1622 ParagraphList::iterator par = ownerParagraphs().begin();
1623 ParagraphList::iterator end = ownerParagraphs().end();
1624 for (; par != end; ++par) {
1625 pos = par->getPositionOfInset(inset);
1627 checkParagraph(par, pos);
1636 bool LyXText::setCursor(ParagraphList::iterator pit,
1638 bool setfont, bool boundary)
1640 LyXCursor old_cursor = cursor;
1641 setCursorIntern(pit, pos, setfont, boundary);
1642 return deleteEmptyParagraphMechanism(old_cursor);
1646 void LyXText::setCursor(LyXCursor & cur, ParagraphList::iterator pit,
1647 pos_type pos, bool boundary)
1649 lyx::Assert(pit != ownerParagraphs().end());
1653 cur.boundary(boundary);
1655 // get the cursor y position in text
1657 RowList::iterator row = getRow(pit, pos, y);
1658 RowList::iterator beg = rows().begin();
1660 RowList::iterator old_row = row;
1662 // if we are before the first char of this row and are still in the
1663 // same paragraph and there is a previous row then put the cursor on
1664 // the end of the previous row
1665 cur.iy(y + row->baseline());
1667 if (row != beg && pos &&
1668 boost::prior(row)->par() == row->par() &&
1669 pos < pit->size() &&
1670 pit->getChar(pos) == Paragraph::META_INSET &&
1671 (ins = pit->getInset(pos)) && (ins->needFullRow() || ins->display()))
1678 // y is now the beginning of the cursor row
1679 y += row->baseline();
1680 // y is now the cursor baseline
1683 pos_type last = lastPrintablePos(*this, old_row);
1685 // None of these should happen, but we're scaredy-cats
1686 if (pos > pit->size()) {
1687 lyxerr << "dont like 1 please report" << endl;
1690 } else if (pos > last + 1) {
1691 lyxerr << "dont like 2 please report" << endl;
1692 // This shouldn't happen.
1695 } else if (pos < row->pos()) {
1696 lyxerr << "dont like 3 please report" << endl;
1701 // now get the cursors x position
1702 float x = getCursorX(row, pos, last, boundary);
1705 if (old_row != row) {
1706 x = getCursorX(old_row, pos, last, boundary);
1710 /* We take out this for the time being because 1) the redraw code is not
1711 prepared to this yet and 2) because some good policy has yet to be decided
1712 while editting: for instance how to act on rows being created/deleted
1716 //if the cursor is in a visible row, anchor to it
1718 if (topy < y && y < topy + bv()->workHeight())
1724 float LyXText::getCursorX(RowList::iterator rit,
1725 pos_type pos, pos_type last, bool boundary) const
1727 pos_type cursor_vpos = 0;
1729 float fill_separator;
1731 float fill_label_hfill;
1732 // This call HAS to be here because of the BidiTables!!!
1733 prepareToPrint(rit, x, fill_separator, fill_hfill,
1736 ParagraphList::iterator rit_par = rit->par();
1737 pos_type const rit_pos = rit->pos();
1740 cursor_vpos = rit_pos;
1741 else if (pos > last && !boundary)
1742 cursor_vpos = (rit_par->isRightToLeftPar(bv()->buffer()->params))
1743 ? rit_pos : last + 1;
1744 else if (pos > rit_pos && (pos > last || boundary))
1745 /// Place cursor after char at (logical) position pos - 1
1746 cursor_vpos = (bidi_level(pos - 1) % 2 == 0)
1747 ? log2vis(pos - 1) + 1 : log2vis(pos - 1);
1749 /// Place cursor before char at (logical) position pos
1750 cursor_vpos = (bidi_level(pos) % 2 == 0)
1751 ? log2vis(pos) : log2vis(pos) + 1;
1753 pos_type body_pos = rit_par->beginningOfBody();
1754 if ((body_pos > 0) &&
1755 ((body_pos - 1 > last) || !rit_par->isLineSeparator(body_pos - 1)))
1758 for (pos_type vpos = rit_pos; vpos < cursor_vpos; ++vpos) {
1759 pos_type pos = vis2log(vpos);
1760 if (body_pos > 0 && pos == body_pos - 1) {
1761 x += fill_label_hfill +
1762 font_metrics::width(
1763 rit_par->layout()->labelsep,
1764 getLabelFont(bv()->buffer(), rit_par));
1765 if (rit_par->isLineSeparator(body_pos - 1))
1766 x -= singleWidth(rit_par, body_pos - 1);
1769 if (hfillExpansion(*this, rit, pos)) {
1770 x += singleWidth(rit_par, pos);
1771 if (pos >= body_pos)
1774 x += fill_label_hfill;
1775 } else if (rit_par->isSeparator(pos)) {
1776 x += singleWidth(rit_par, pos);
1777 if (pos >= body_pos)
1778 x += fill_separator;
1780 x += singleWidth(rit_par, pos);
1786 void LyXText::setCursorIntern(ParagraphList::iterator pit,
1787 pos_type pos, bool setfont, bool boundary)
1789 InsetText * it = static_cast<InsetText *>(pit->inInset());
1791 if (it != inset_owner) {
1792 lyxerr[Debug::INSETS] << "InsetText is " << it
1794 << "inset_owner is "
1795 << inset_owner << endl;
1796 #ifdef WITH_WARNINGS
1797 #warning I believe this code is wrong. (Lgb)
1798 #warning Jürgen, have a look at this. (Lgb)
1799 #warning Hmmm, I guess you are right but we
1800 #warning should verify when this is needed
1802 // Jürgen, would you like to have a look?
1803 // I guess we need to move the outer cursor
1804 // and open and lock the inset (bla bla bla)
1805 // stuff I don't know... so can you have a look?
1807 // I moved the lyxerr stuff in here so we can see if
1808 // this is actually really needed and where!
1810 // it->getLyXText(bv())->setCursorIntern(bv(), par, pos, setfont, boundary);
1815 setCursor(cursor, pit, pos, boundary);
1821 void LyXText::setCurrentFont()
1823 pos_type pos = cursor.pos();
1824 ParagraphList::iterator pit = cursor.par();
1826 if (cursor.boundary() && pos > 0)
1830 if (pos == pit->size())
1832 else // potentional bug... BUG (Lgb)
1833 if (pit->isSeparator(pos)) {
1834 if (pos > cursor.row()->pos() &&
1835 bidi_level(pos) % 2 ==
1836 bidi_level(pos - 1) % 2)
1838 else if (pos + 1 < pit->size())
1844 pit->getFontSettings(bv()->buffer()->params, pos);
1845 real_current_font = getFont(bv()->buffer(), pit, pos);
1847 if (cursor.pos() == pit->size() &&
1848 isBoundary(bv()->buffer(), *pit, cursor.pos()) &&
1849 !cursor.boundary()) {
1850 Language const * lang =
1851 pit->getParLanguage(bv()->buffer()->params);
1852 current_font.setLanguage(lang);
1853 current_font.setNumber(LyXFont::OFF);
1854 real_current_font.setLanguage(lang);
1855 real_current_font.setNumber(LyXFont::OFF);
1860 // returns the column near the specified x-coordinate of the row
1861 // x is set to the real beginning of this column
1863 LyXText::getColumnNearX(RowList::iterator rit, int & x, bool & boundary) const
1866 float fill_separator;
1868 float fill_label_hfill;
1870 prepareToPrint(rit, tmpx, fill_separator,
1871 fill_hfill, fill_label_hfill);
1873 pos_type vc = rit->pos();
1874 pos_type last = lastPrintablePos(*this, rit);
1877 ParagraphList::iterator rit_par = rit->par();
1878 LyXLayout_ptr const & layout = rit->par()->layout();
1880 bool left_side = false;
1882 pos_type body_pos = rit_par->beginningOfBody();
1883 float last_tmpx = tmpx;
1886 (body_pos - 1 > last ||
1887 !rit_par->isLineSeparator(body_pos - 1)))
1890 // check for empty row
1891 if (!rit_par->size()) {
1896 while (vc <= last && tmpx <= x) {
1899 if (body_pos > 0 && c == body_pos - 1) {
1900 tmpx += fill_label_hfill +
1901 font_metrics::width(layout->labelsep,
1902 getLabelFont(bv()->buffer(), &*rit_par));
1903 if (rit_par->isLineSeparator(body_pos - 1))
1904 tmpx -= singleWidth(rit_par, body_pos - 1);
1907 if (hfillExpansion(*this, rit, c)) {
1908 tmpx += singleWidth(rit_par, c);
1912 tmpx += fill_label_hfill;
1913 } else if (rit_par->isSeparator(c)) {
1914 tmpx += singleWidth(rit_par, c);
1916 tmpx+= fill_separator;
1918 tmpx += singleWidth(rit_par, c);
1923 if ((tmpx + last_tmpx) / 2 > x) {
1928 if (vc > last + 1) // This shouldn't happen.
1932 // This (rtl_support test) is not needed, but gives
1933 // some speedup if rtl_support=false
1934 bool const lastrow = lyxrc.rtl_support &&
1935 (boost::next(rit) == rowlist_.end() ||
1936 boost::next(rit)->par() != rit_par);
1937 // If lastrow is false, we don't need to compute
1938 // the value of rtl.
1939 bool const rtl = (lastrow)
1940 ? rit_par->isRightToLeftPar(bv()->buffer()->params)
1943 ((rtl && left_side && vc == rit->pos() && x < tmpx - 5) ||
1944 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5)))
1946 else if (vc == rit->pos()) {
1948 if (bidi_level(c) % 2 == 1)
1951 c = vis2log(vc - 1);
1952 bool const rtl = (bidi_level(c) % 2 == 1);
1953 if (left_side == rtl) {
1955 boundary = isBoundary(bv()->buffer(), *rit_par, c);
1959 if (rit->pos() <= last && c > last
1960 && rit_par->isNewline(last)) {
1961 if (bidi_level(last) % 2 == 0)
1962 tmpx -= singleWidth(rit_par, last);
1964 tmpx += singleWidth(rit_par, last);
1974 void LyXText::setCursorFromCoordinates(int x, int y)
1976 LyXCursor old_cursor = cursor;
1978 setCursorFromCoordinates(cursor, x, y);
1980 deleteEmptyParagraphMechanism(old_cursor);
1987 * return true if the cursor given is at the end of a row,
1988 * and the next row is filled by an inset that spans an entire
1991 bool beforeFullRowInset(LyXText & lt, LyXCursor const & cur) {
1992 RowList::iterator row = cur.row();
1993 if (boost::next(row) == lt.rows().end())
1995 Row const & next = *boost::next(row);
1997 if (next.pos() != cur.pos() || next.par() != cur.par())
2000 if (cur.pos() == cur.par()->size()
2001 || !cur.par()->isInset(cur.pos()))
2003 Inset const * inset = cur.par()->getInset(cur.pos());
2004 if (inset->needFullRow() || inset->display())
2011 void LyXText::setCursorFromCoordinates(LyXCursor & cur, int x, int y)
2013 // Get the row first.
2015 RowList::iterator row = getRowNearY(y);
2017 pos_type const column = getColumnNearX(row, x, bound);
2018 cur.par(row->par());
2019 cur.pos(row->pos() + column);
2021 cur.y(y + row->baseline());
2024 if (beforeFullRowInset(*this, cur)) {
2025 pos_type last = lastPrintablePos(*this, row);
2026 float x = getCursorX(boost::next(row), cur.pos(), last, bound);
2028 cur.iy(y + row->height() + boost::next(row)->baseline());
2029 cur.irow(boost::next(row));
2035 cur.boundary(bound);
2039 void LyXText::cursorLeft(bool internal)
2041 if (cursor.pos() > 0) {
2042 bool boundary = cursor.boundary();
2043 setCursor(cursor.par(), cursor.pos() - 1, true, false);
2044 if (!internal && !boundary &&
2045 isBoundary(bv()->buffer(), *cursor.par(), cursor.pos() + 1))
2046 setCursor(cursor.par(), cursor.pos() + 1, true, true);
2047 } else if (cursor.par() != ownerParagraphs().begin()) { // steps into the above paragraph.
2048 ParagraphList::iterator pit = boost::prior(cursor.par());
2049 setCursor(pit, pit->size());
2054 void LyXText::cursorRight(bool internal)
2056 bool const at_end = (cursor.pos() == cursor.par()->size());
2057 bool const at_newline = !at_end &&
2058 cursor.par()->isNewline(cursor.pos());
2060 if (!internal && cursor.boundary() && !at_newline)
2061 setCursor(cursor.par(), cursor.pos(), true, false);
2063 setCursor(cursor.par(), cursor.pos() + 1, true, false);
2065 isBoundary(bv()->buffer(), *cursor.par(), cursor.pos()))
2066 setCursor(cursor.par(), cursor.pos(), true, true);
2067 } else if (boost::next(cursor.par()) != ownerParagraphs().end())
2068 setCursor(boost::next(cursor.par()), 0);
2072 void LyXText::cursorUp(bool selecting)
2075 int x = cursor.x_fix();
2076 int y = cursor.y() - cursor.row()->baseline() - 1;
2077 setCursorFromCoordinates(x, y);
2080 int y1 = cursor.iy() - topy;
2083 Inset * inset_hit = checkInsetHit(x, y1);
2084 if (inset_hit && isHighlyEditableInset(inset_hit)) {
2085 inset_hit->edit(bv(), x, y - (y2 - y1), mouse_button::none);
2089 setCursorFromCoordinates(bv(), cursor.x_fix(),
2090 cursor.y() - cursor.row()->baseline() - 1);
2095 void LyXText::cursorDown(bool selecting)
2098 int x = cursor.x_fix();
2099 int y = cursor.y() - cursor.row()->baseline() +
2100 cursor.row()->height() + 1;
2101 setCursorFromCoordinates(x, y);
2102 if (!selecting && cursor.row() == cursor.irow()) {
2104 int y1 = cursor.iy() - topy;
2107 Inset * inset_hit = checkInsetHit(x, y1);
2108 if (inset_hit && isHighlyEditableInset(inset_hit)) {
2109 inset_hit->edit(bv(), x, y - (y2 - y1), mouse_button::none);
2113 setCursorFromCoordinates(bv(), cursor.x_fix(),
2114 cursor.y() - cursor.row()->baseline()
2115 + cursor.row()->height() + 1);
2120 void LyXText::cursorUpParagraph()
2122 if (cursor.pos() > 0) {
2123 setCursor(cursor.par(), 0);
2125 else if (cursor.par() != ownerParagraphs().begin()) {
2126 setCursor(boost::prior(cursor.par()), 0);
2131 void LyXText::cursorDownParagraph()
2133 if (boost::next(cursor.par()) != ownerParagraphs().end()) {
2134 setCursor(boost::next(cursor.par()), 0);
2136 setCursor(cursor.par(), cursor.par()->size());
2140 // fix the cursor `cur' after a characters has been deleted at `where'
2141 // position. Called by deleteEmptyParagraphMechanism
2142 void LyXText::fixCursorAfterDelete(LyXCursor & cur,
2143 LyXCursor const & where)
2145 // if cursor is not in the paragraph where the delete occured,
2147 if (cur.par() != where.par())
2150 // if cursor position is after the place where the delete occured,
2152 if (cur.pos() > where.pos())
2153 cur.pos(cur.pos()-1);
2155 // check also if we don't want to set the cursor on a spot behind the
2156 // pagragraph because we erased the last character.
2157 if (cur.pos() > cur.par()->size())
2158 cur.pos(cur.par()->size());
2160 // recompute row et al. for this cursor
2161 setCursor(cur, cur.par(), cur.pos(), cur.boundary());
2165 bool LyXText::deleteEmptyParagraphMechanism(LyXCursor const & old_cursor)
2167 // Would be wrong to delete anything if we have a selection.
2168 if (selection.set())
2171 // We allow all kinds of "mumbo-jumbo" when freespacing.
2172 if (old_cursor.par()->layout()->free_spacing
2173 || old_cursor.par()->isFreeSpacing()) {
2177 /* Ok I'll put some comments here about what is missing.
2178 I have fixed BackSpace (and thus Delete) to not delete
2179 double-spaces automagically. I have also changed Cut,
2180 Copy and Paste to hopefully do some sensible things.
2181 There are still some small problems that can lead to
2182 double spaces stored in the document file or space at
2183 the beginning of paragraphs. This happens if you have
2184 the cursor betwenn to spaces and then save. Or if you
2185 cut and paste and the selection have a space at the
2186 beginning and then save right after the paste. I am
2187 sure none of these are very hard to fix, but I will
2188 put out 1.1.4pre2 with FIX_DOUBLE_SPACE defined so
2189 that I can get some feedback. (Lgb)
2192 // If old_cursor.pos() == 0 and old_cursor.pos()(1) == LineSeparator
2193 // delete the LineSeparator.
2196 // If old_cursor.pos() == 1 and old_cursor.pos()(0) == LineSeparator
2197 // delete the LineSeparator.
2200 // If the pos around the old_cursor were spaces, delete one of them.
2201 if (old_cursor.par() != cursor.par()
2202 || old_cursor.pos() != cursor.pos()) {
2203 // Only if the cursor has really moved
2205 if (old_cursor.pos() > 0
2206 && old_cursor.pos() < old_cursor.par()->size()
2207 && old_cursor.par()->isLineSeparator(old_cursor.pos())
2208 && old_cursor.par()->isLineSeparator(old_cursor.pos() - 1)) {
2209 old_cursor.par()->erase(old_cursor.pos() - 1);
2210 redoParagraphs(old_cursor, boost::next(old_cursor.par()));
2212 #ifdef WITH_WARNINGS
2213 #warning This will not work anymore when we have multiple views of the same buffer
2214 // In this case, we will have to correct also the cursors held by
2215 // other bufferviews. It will probably be easier to do that in a more
2216 // automated way in LyXCursor code. (JMarc 26/09/2001)
2218 // correct all cursors held by the LyXText
2219 fixCursorAfterDelete(cursor, old_cursor);
2220 fixCursorAfterDelete(selection.cursor,
2222 fixCursorAfterDelete(selection.start,
2224 fixCursorAfterDelete(selection.end, old_cursor);
2225 fixCursorAfterDelete(last_sel_cursor,
2227 fixCursorAfterDelete(toggle_cursor, old_cursor);
2228 fixCursorAfterDelete(toggle_end_cursor,
2234 // don't delete anything if this is the ONLY paragraph!
2235 if (ownerParagraphs().size() == 1)
2238 // Do not delete empty paragraphs with keepempty set.
2239 if (old_cursor.par()->layout()->keepempty)
2242 // only do our magic if we changed paragraph
2243 if (old_cursor.par() == cursor.par())
2246 // record if we have deleted a paragraph
2247 // we can't possibly have deleted a paragraph before this point
2248 bool deleted = false;
2250 if (old_cursor.par()->empty() ||
2251 (old_cursor.par()->size() == 1 &&
2252 old_cursor.par()->isLineSeparator(0))) {
2253 // ok, we will delete anything
2254 LyXCursor tmpcursor;
2258 bool selection_position_was_oldcursor_position = (
2259 selection.cursor.par() == old_cursor.par()
2260 && selection.cursor.pos() == old_cursor.pos());
2262 if (old_cursor.row() != rows().begin()) {
2264 prevrow = boost::prior(old_cursor.row());
2265 const_cast<LyXText *>(this)->postPaint(old_cursor.y() - old_cursor.row()->baseline() - prevrow->height());
2267 cursor = old_cursor; // that undo can restore the right cursor position
2268 #warning FIXME. --end() iterator is usable here
2269 ParagraphList::iterator endpit = boost::next(old_cursor.par());
2270 while (endpit != ownerParagraphs().end() &&
2271 endpit->getDepth()) {
2275 setUndo(bv(), Undo::DELETE, &*old_cursor.par(), &*endpit);
2279 removeRow(old_cursor.row());
2281 ownerParagraphs().erase(old_cursor.par());
2283 /* Breakagain the next par. Needed because of
2284 * the parindent that can occur or dissappear.
2285 * The next row can change its height, if
2286 * there is another layout before */
2287 if (boost::next(prevrow) != rows().end()) {
2288 breakAgain(boost::next(prevrow));
2291 setHeightOfRow(prevrow);
2293 RowList::iterator nextrow = boost::next(old_cursor.row());
2294 const_cast<LyXText *>(this)->postPaint(
2295 old_cursor.y() - old_cursor.row()->baseline());
2298 cursor = old_cursor; // that undo can restore the right cursor position
2299 #warning FIXME. --end() iterator is usable here
2300 ParagraphList::iterator endpit = boost::next(old_cursor.par());
2301 while (endpit != ownerParagraphs().end() &&
2302 endpit->getDepth()) {
2306 setUndo(bv(), Undo::DELETE, &*old_cursor.par(), &*endpit);
2310 removeRow(old_cursor.row());
2312 ownerParagraphs().erase(old_cursor.par());
2314 /* Breakagain the next par. Needed because of
2315 the parindent that can occur or dissappear.
2316 The next row can change its height, if
2317 there is another layout before */
2318 if (nextrow != rows().end()) {
2319 breakAgain(nextrow);
2325 setCursorIntern(cursor.par(), cursor.pos());
2327 if (selection_position_was_oldcursor_position) {
2328 // correct selection
2329 selection.cursor = cursor;
2333 if (old_cursor.par()->stripLeadingSpaces()) {
2334 redoParagraphs(old_cursor, boost::next(old_cursor.par()));
2336 setCursorIntern(cursor.par(), cursor.pos());
2337 selection.cursor = cursor;
2344 ParagraphList & LyXText::ownerParagraphs() const
2347 return inset_owner->paragraphs;
2349 return bv_owner->buffer()->paragraphs;
2353 LyXText::refresh_status LyXText::refreshStatus() const
2355 return refresh_status_;
2359 void LyXText::clearPaint()
2361 refresh_status_ = REFRESH_NONE;
2362 refresh_row = rows().end();
2367 void LyXText::postPaint(int start_y)
2369 refresh_status old = refresh_status_;
2371 refresh_status_ = REFRESH_AREA;
2372 refresh_row = rows().end();
2374 if (old != REFRESH_NONE && refresh_y < start_y)
2377 refresh_y = start_y;
2382 // We are an inset's lyxtext. Tell the top-level lyxtext
2383 // it needs to update the row we're in.
2384 LyXText * t = bv()->text;
2385 t->postRowPaint(t->cursor.row(), t->cursor.y() - t->cursor.row()->baseline());
2389 // FIXME: we should probably remove this y parameter,
2390 // make refresh_y be 0, and use row->y etc.
2391 void LyXText::postRowPaint(RowList::iterator rit, int start_y)
2393 if (refresh_status_ != REFRESH_NONE && refresh_y < start_y) {
2394 refresh_status_ = REFRESH_AREA;
2397 refresh_y = start_y;
2400 if (refresh_status_ == REFRESH_AREA)
2403 refresh_status_ = REFRESH_ROW;
2409 // We are an inset's lyxtext. Tell the top-level lyxtext
2410 // it needs to update the row we're in.
2411 LyXText * t = bv()->text;
2412 t->postRowPaint(t->cursor.row(), t->cursor.y() - t->cursor.row()->baseline());
2416 bool LyXText::isInInset() const
2418 // Sub-level has non-null bv owner and
2419 // non-null inset owner.
2420 return inset_owner != 0 && bv_owner != 0;
2424 int defaultRowHeight()
2426 LyXFont const font(LyXFont::ALL_SANE);
2427 return int(font_metrics::maxAscent(font)
2428 + font_metrics::maxDescent(font) * 1.5);