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"
34 #include "insets/inseterror.h"
35 #include "insets/insetbibitem.h"
36 #include "insets/insetspecialchar.h"
37 #include "insets/insettext.h"
38 #include "insets/insetfloat.h"
39 #include "insets/insetwrap.h"
41 #include "support/LAssert.h"
42 #include "support/textutils.h"
43 #include "support/lstrings.h"
45 #include "support/BoostFormat.h"
55 LyXText::LyXText(BufferView * bv)
56 : height(0), width(0), anchor_row_offset_(0),
57 inset_owner(0), the_locking_inset(0), bv_owner(bv)
59 anchor_row_ = rows().end();
60 need_break_row = rows().end();
61 refresh_row = rows().end();
67 LyXText::LyXText(BufferView * bv, InsetText * inset)
68 : height(0), width(0), anchor_row_offset_(0),
69 inset_owner(inset), the_locking_inset(0), bv_owner(bv)
71 anchor_row_ = rows().end();
72 need_break_row = rows().end();
73 refresh_row = rows().end();
79 void LyXText::init(BufferView * bview, bool reinit)
83 need_break_row = rows().end();
85 copylayouttype.erase();
88 } else if (!rowlist_.empty())
91 ParagraphList::iterator par = ownerParagraphs().begin();
92 ParagraphList::iterator end = ownerParagraphs().end();
94 current_font = getFont(bview->buffer(), &*par, 0);
96 for (; par != end; ++par) {
97 insertParagraph(&*par, rowlist_.end());
99 setCursorIntern(rowlist_.begin()->par(), 0);
100 selection.cursor = cursor;
108 LyXFont const realizeFont(LyXFont const & font,
112 LyXTextClass const & tclass = buf->params.getLyXTextClass();
113 LyXFont tmpfont(font);
114 Paragraph::depth_type par_depth = par->getDepth();
116 // Resolve against environment font information
117 while (par && par_depth && !tmpfont.resolved()) {
118 par = par->outerHook();
120 tmpfont.realize(par->layout()->font);
121 par_depth = par->getDepth();
125 tmpfont.realize(tclass.defaultfont());
133 // Gets the fully instantiated font at a given position in a paragraph
134 // Basically the same routine as Paragraph::getFont() in paragraph.C.
135 // The difference is that this one is used for displaying, and thus we
136 // are allowed to make cosmetic improvements. For instance make footnotes
138 // If position is -1, we get the layout font of the paragraph.
139 // If position is -2, we get the font of the manual label of the paragraph.
140 LyXFont const LyXText::getFont(Buffer const * buf, Paragraph * par,
143 lyx::Assert(pos >= 0);
145 LyXLayout_ptr const & layout = par->layout();
147 // We specialize the 95% common case:
148 if (!par->getDepth()) {
149 if (layout->labeltype == LABEL_MANUAL
150 && pos < par->beginningOfBody()) {
152 LyXFont f = par->getFontSettings(buf->params, pos);
154 par->inInset()->getDrawFont(f);
155 return f.realize(layout->reslabelfont);
157 LyXFont f = par->getFontSettings(buf->params, pos);
159 par->inInset()->getDrawFont(f);
160 return f.realize(layout->resfont);
164 // The uncommon case need not be optimized as much
168 if (pos < par->beginningOfBody()) {
170 layoutfont = layout->labelfont;
173 layoutfont = layout->font;
176 LyXFont tmpfont = par->getFontSettings(buf->params, pos);
177 tmpfont.realize(layoutfont);
180 par->inInset()->getDrawFont(tmpfont);
182 return realizeFont(tmpfont, buf, par);
186 LyXFont const LyXText::getLayoutFont(Buffer const * buf, Paragraph * par) const
188 LyXLayout_ptr const & layout = par->layout();
190 if (!par->getDepth()) {
191 return layout->resfont;
194 return realizeFont(layout->font, buf, par);
198 LyXFont const LyXText::getLabelFont(Buffer const * buf, Paragraph * par) const
200 LyXLayout_ptr const & layout = par->layout();
202 if (!par->getDepth()) {
203 return layout->reslabelfont;
206 return realizeFont(layout->labelfont, buf, par);
210 void LyXText::setCharFont(Paragraph * par,
211 pos_type pos, LyXFont const & fnt,
214 Buffer const * buf = bv()->buffer();
215 LyXFont font = getFont(buf, par, pos);
216 font.update(fnt, buf->params.language, toggleall);
217 // Let the insets convert their font
218 if (par->isInset(pos)) {
219 Inset * inset = par->getInset(pos);
220 if (isEditableInset(inset)) {
221 UpdatableInset * uinset =
222 static_cast<UpdatableInset *>(inset);
223 uinset->setFont(bv(), fnt, toggleall, true);
227 // Plug thru to version below:
228 setCharFont(buf, par, pos, font);
232 void LyXText::setCharFont(Buffer const * buf, Paragraph * par,
233 pos_type pos, LyXFont const & fnt)
237 LyXTextClass const & tclass = buf->params.getLyXTextClass();
238 LyXLayout_ptr const & layout = par->layout();
240 // Get concrete layout font to reduce against
243 if (pos < par->beginningOfBody())
244 layoutfont = layout->labelfont;
246 layoutfont = layout->font;
248 // Realize against environment font information
249 if (par->getDepth()) {
250 Paragraph * tp = par;
251 while (!layoutfont.resolved() && tp && tp->getDepth()) {
252 tp = tp->outerHook();
254 layoutfont.realize(tp->layout()->font);
258 layoutfont.realize(tclass.defaultfont());
260 // Now, reduce font against full layout font
261 font.reduce(layoutfont);
263 par->setFont(pos, font);
267 // removes the row and reset the touched counters
268 void LyXText::removeRow(RowList::iterator rit)
270 /* FIXME: when we cache the bview, this should just
271 * become a postPaint(), I think */
272 if (refresh_row == rit) {
273 if (rit == rows().begin())
274 refresh_row = boost::next(rit);
276 refresh_row = boost::prior(rit);
278 // what about refresh_y
281 if (anchor_row_ == rit) {
282 if (rit != rows().begin()) {
283 anchor_row_ = boost::prior(rit);
284 anchor_row_offset_ += boost::prior(rit)->height();
286 anchor_row_ = boost::next(rit);
287 anchor_row_offset_ -= rit->height();
291 // the text becomes smaller
292 height -= rit->height();
298 // remove all following rows of the paragraph of the specified row.
299 void LyXText::removeParagraph(RowList::iterator rit)
301 Paragraph * tmppar = rit->par();
304 while (rit != rows().end() && rit->par() == tmppar) {
305 RowList::iterator tmprit = boost::next(rit);
312 void LyXText::insertParagraph(Paragraph * par, RowList::iterator rowit)
314 // insert a new row, starting at position 0
316 RowList::iterator rit = rowlist_.insert(rowit, newrow);
318 // and now append the whole paragraph before the new row
319 appendParagraph(rit);
323 Inset * LyXText::getInset() const
325 if (cursor.pos() < cursor.par()->size()
326 && cursor.par()->isInset(cursor.pos())) {
327 return cursor.par()->getInset(cursor.pos());
333 void LyXText::toggleInset()
335 Inset * inset = getInset();
336 // is there an editable inset at cursor position?
337 if (!isEditableInset(inset)) {
338 // No, try to see if we are inside a collapsable inset
339 if (inset_owner && inset_owner->owner()
340 && inset_owner->owner()->isOpen()) {
341 bv()->unlockInset(static_cast<UpdatableInset *>(inset_owner->owner()));
342 inset_owner->owner()->close(bv());
343 bv()->getLyXText()->cursorRight(bv());
347 //bv()->owner()->message(inset->editMessage());
349 // do we want to keep this?? (JMarc)
350 if (!isHighlyEditableInset(inset))
351 setCursorParUndo(bv());
353 if (inset->isOpen()) {
359 bv()->updateInset(inset);
363 /* used in setlayout */
364 // Asger is not sure we want to do this...
365 void LyXText::makeFontEntriesLayoutSpecific(Buffer const & buf,
368 LyXLayout_ptr const & layout = par.layout();
371 for (pos_type pos = 0; pos < par.size(); ++pos) {
372 if (pos < par.beginningOfBody())
373 layoutfont = layout->labelfont;
375 layoutfont = layout->font;
377 LyXFont tmpfont = par.getFontSettings(buf.params, pos);
378 tmpfont.reduce(layoutfont);
379 par.setFont(pos, tmpfont);
384 Paragraph * LyXText::setLayout(LyXCursor & cur, LyXCursor & sstart_cur,
385 LyXCursor & send_cur,
386 string const & layout)
388 Paragraph * endpar = send_cur.par()->next();
389 Paragraph * undoendpar = endpar;
391 if (endpar && endpar->getDepth()) {
392 while (endpar && endpar->getDepth()) {
393 endpar = endpar->next();
397 endpar = endpar->next(); // because of parindents etc.
400 setUndo(bv(), Undo::EDIT, sstart_cur.par(), undoendpar);
402 // ok we have a selection. This is always between sstart_cur
403 // and sel_end cursor
405 Paragraph * par = sstart_cur.par();
406 Paragraph * epar = send_cur.par()->next();
408 LyXLayout_ptr const & lyxlayout =
409 bv()->buffer()->params.getLyXTextClass()[layout];
412 par->applyLayout(lyxlayout);
413 makeFontEntriesLayoutSpecific(*bv()->buffer(), *par);
414 Paragraph * fppar = par;
415 fppar->params().spaceTop(lyxlayout->fill_top ?
416 VSpace(VSpace::VFILL)
417 : VSpace(VSpace::NONE));
418 fppar->params().spaceBottom(lyxlayout->fill_bottom ?
419 VSpace(VSpace::VFILL)
420 : VSpace(VSpace::NONE));
421 if (lyxlayout->margintype == MARGIN_MANUAL)
422 par->setLabelWidthString(lyxlayout->labelstring());
425 } while (par != epar);
431 // set layout over selection and make a total rebreak of those paragraphs
432 void LyXText::setLayout(string const & layout)
434 LyXCursor tmpcursor = cursor; /* store the current cursor */
436 // if there is no selection just set the layout
437 // of the current paragraph */
438 if (!selection.set()) {
439 selection.start = cursor; // dummy selection
440 selection.end = cursor;
442 Paragraph * endpar = setLayout(cursor, selection.start,
443 selection.end, layout);
444 redoParagraphs(selection.start, endpar);
446 // we have to reset the selection, because the
447 // geometry could have changed
448 setCursor(selection.start.par(),
449 selection.start.pos(), false);
450 selection.cursor = cursor;
451 setCursor(selection.end.par(), selection.end.pos(), false);
455 setCursor(tmpcursor.par(), tmpcursor.pos(), true);
459 void LyXText::incDepth()
461 ParagraphList::iterator pit(cursor.par());
462 ParagraphList::iterator end(cursor.par());
463 ParagraphList::iterator start = pit;
465 if (selection.set()) {
466 pit = selection.start.par();
467 end = selection.end.par();
471 ParagraphList::iterator pastend = end;
473 setUndo(bv(), Undo::EDIT, &(*start), &(*pastend));
475 int prev_after_depth = 0;
476 #warning parlist ... could be nicer ?
477 if (start != ownerParagraphs().begin())
478 prev_after_depth = boost::prior(start)->getMaxDepthAfter();
481 int const depth = pit->params().depth();
483 if (depth < prev_after_depth
484 && pit->layout()->labeltype != LABEL_BIBLIO) {
485 pit->params().depth(depth + 1);
488 prev_after_depth = pit->getMaxDepthAfter();
496 // Wow, redoParagraphs is stupid.
498 setCursor(tmpcursor, &(*start), 0);
499 redoParagraphs(tmpcursor, &(*pastend));
501 // We need to actually move the text->cursor. I don't
502 // understand why ...
505 // we have to reset the visual selection because the
506 // geometry could have changed
507 if (selection.set()) {
508 setCursor(selection.start.par(), selection.start.pos());
509 selection.cursor = cursor;
510 setCursor(selection.end.par(), selection.end.pos());
515 setCursor(tmpcursor.par(), tmpcursor.pos());
519 // decrement depth over selection and
520 // make a total rebreak of those paragraphs
521 void LyXText::decDepth()
523 // if there is no selection just set the layout
524 // of the current paragraph
525 if (!selection.set()) {
526 selection.start = cursor; // dummy selection
527 selection.end = cursor;
529 Paragraph * endpar = selection.end.par()->next();
530 Paragraph * undoendpar = endpar;
532 if (endpar && endpar->getDepth()) {
533 while (endpar && endpar->getDepth()) {
534 endpar = endpar->next();
538 endpar = endpar->next(); // because of parindents etc.
541 setUndo(bv(), Undo::EDIT,
542 selection.start.par(), undoendpar);
544 LyXCursor tmpcursor = cursor; // store the current cursor
546 // ok we have a selection. This is always between sel_start_cursor
547 // and sel_end cursor
548 cursor = selection.start;
551 if (cursor.par()->params().depth()) {
552 cursor.par()->params()
553 .depth(cursor.par()->params().depth() - 1);
555 if (cursor.par() == selection.end.par()) {
558 cursor.par(cursor.par()->next());
561 redoParagraphs(selection.start, endpar);
563 // we have to reset the visual selection because the
564 // geometry could have changed
565 setCursor(selection.start.par(), selection.start.pos());
566 selection.cursor = cursor;
567 setCursor(selection.end.par(), selection.end.pos());
570 setCursor(tmpcursor.par(), tmpcursor.pos());
574 // set font over selection and make a total rebreak of those paragraphs
575 void LyXText::setFont(LyXFont const & font, bool toggleall)
577 // if there is no selection just set the current_font
578 if (!selection.set()) {
579 // Determine basis font
581 if (cursor.pos() < cursor.par()->beginningOfBody()) {
582 layoutfont = getLabelFont(bv()->buffer(),
585 layoutfont = getLayoutFont(bv()->buffer(),
588 // Update current font
589 real_current_font.update(font,
590 bv()->buffer()->params.language,
593 // Reduce to implicit settings
594 current_font = real_current_font;
595 current_font.reduce(layoutfont);
596 // And resolve it completely
597 real_current_font.realize(layoutfont);
602 LyXCursor tmpcursor = cursor; // store the current cursor
604 // ok we have a selection. This is always between sel_start_cursor
605 // and sel_end cursor
607 setUndo(bv(), Undo::EDIT,
608 selection.start.par(), selection.end.par()->next());
610 cursor = selection.start;
611 while (cursor.par() != selection.end.par() ||
612 cursor.pos() < selection.end.pos())
614 if (cursor.pos() < cursor.par()->size()) {
615 // an open footnote should behave like a closed one
616 setCharFont(cursor.par(), cursor.pos(),
618 cursor.pos(cursor.pos() + 1);
621 cursor.par(cursor.par()->next());
626 redoParagraphs(selection.start, selection.end.par()->next());
628 // we have to reset the selection, because the
629 // geometry could have changed, but we keep
630 // it for user convenience
631 setCursor(selection.start.par(), selection.start.pos());
632 selection.cursor = cursor;
633 setCursor(selection.end.par(), selection.end.pos());
635 setCursor(tmpcursor.par(), tmpcursor.pos(), true,
636 tmpcursor.boundary());
640 void LyXText::redoHeightOfParagraph()
642 RowList::iterator tmprow = cursor.row();
643 int y = cursor.y() - tmprow->baseline();
645 setHeightOfRow(tmprow);
647 while (tmprow != rows().begin()
648 && boost::prior(tmprow)->par() == tmprow->par()) {
650 y -= tmprow->height();
651 setHeightOfRow(tmprow);
656 setCursor(cursor.par(), cursor.pos(), false, cursor.boundary());
660 void LyXText::redoDrawingOfParagraph(LyXCursor const & cur)
662 RowList::iterator tmprow = cur.row();
664 int y = cur.y() - tmprow->baseline();
665 setHeightOfRow(tmprow);
667 while (tmprow != rows().begin()
668 && boost::prior(tmprow)->par() == tmprow->par()) {
670 y -= tmprow->height();
674 setCursor(cur.par(), cur.pos());
678 // deletes and inserts again all paragaphs between the cursor
679 // and the specified par
680 // This function is needed after SetLayout and SetFont etc.
681 void LyXText::redoParagraphs(LyXCursor const & cur,
682 Paragraph const * endpar)
684 RowList::iterator tmprit = cur.row();
686 int y = cur.y() - tmprit->baseline();
688 Paragraph * first_phys_par;
689 if (tmprit == rows().begin()) {
690 // A trick/hack for UNDO.
691 // This is needed because in an UNDO/REDO we could have
692 // changed the ownerParagrah() so the paragraph inside
693 // the row is NOT my really first par anymore.
694 // Got it Lars ;) (Jug 20011206)
695 first_phys_par = &*ownerParagraphs().begin();
697 // In here prevrit could be set to rows().end(). (Lgb)
699 first_phys_par = tmprit->par();
700 while (tmprit != rows().begin()
701 && boost::prior(tmprit)->par() == first_phys_par)
704 y -= tmprit->height();
707 // Is it possible to put the prevrit setting in here? (Lgb)
710 RowList::iterator prevrit;
711 bool good_prevrit = false;
713 // It seems to mee that good_prevrit is not needed if we let
714 // a bad prevrit have the value rows().end() (Lgb)
715 if (tmprit != rows().begin()) {
716 prevrit = boost::prior(tmprit);
721 while (tmprit != rows().end() && tmprit->par() != endpar) {
722 RowList::iterator tmprit2 = tmprit++;
726 // Reinsert the paragraphs.
727 Paragraph * tmppar = first_phys_par;
729 // See if this loop can be rewritten as a while loop instead.
730 // That should also make the code a bit easier to read. (Lgb)
733 insertParagraph(tmppar, tmprit);
734 while (tmprit != rows().end()
735 && tmprit->par() == tmppar) {
738 tmppar = tmppar->next();
740 } while (tmppar && tmppar != endpar);
743 // If the above changes are done, then we can compare prevrit
744 // with rows().end() here. (Lgb)
746 setHeightOfRow(prevrit);
747 const_cast<LyXText *>(this)->postPaint(y - prevrit->height());
749 setHeightOfRow(rows().begin());
750 const_cast<LyXText *>(this)->postPaint(0);
752 if (tmprit != rows().end())
753 setHeightOfRow(tmprit);
758 void LyXText::fullRebreak()
760 if (rows().empty()) {
764 if (need_break_row != rows().end()) {
765 breakAgain(need_break_row);
766 need_break_row = rows().end();
772 // important for the screen
775 // the cursor set functions have a special mechanism. When they
776 // realize, that you left an empty paragraph, they will delete it.
777 // They also delete the corresponding row
779 // need the selection cursor:
780 void LyXText::setSelection()
782 bool const lsel = selection.set();
784 if (!selection.set()) {
785 last_sel_cursor = selection.cursor;
786 selection.start = selection.cursor;
787 selection.end = selection.cursor;
792 // first the toggling area
793 if (cursor.y() < last_sel_cursor.y()
794 || (cursor.y() == last_sel_cursor.y()
795 && cursor.x() < last_sel_cursor.x())) {
796 toggle_end_cursor = last_sel_cursor;
797 toggle_cursor = cursor;
799 toggle_end_cursor = cursor;
800 toggle_cursor = last_sel_cursor;
803 last_sel_cursor = cursor;
805 // and now the whole selection
807 if (selection.cursor.par() == cursor.par())
808 if (selection.cursor.pos() < cursor.pos()) {
809 selection.end = cursor;
810 selection.start = selection.cursor;
812 selection.end = selection.cursor;
813 selection.start = cursor;
815 else if (selection.cursor.y() < cursor.y() ||
816 (selection.cursor.y() == cursor.y()
817 && selection.cursor.x() < cursor.x())) {
818 selection.end = cursor;
819 selection.start = selection.cursor;
822 selection.end = selection.cursor;
823 selection.start = cursor;
826 // a selection with no contents is not a selection
827 if (selection.start.par() == selection.end.par() &&
828 selection.start.pos() == selection.end.pos())
829 selection.set(false);
831 if (inset_owner && (selection.set() || lsel))
832 inset_owner->setUpdateStatus(bv(), InsetText::SELECTION);
836 string const LyXText::selectionAsString(Buffer const * buffer,
839 if (!selection.set()) return string();
841 // should be const ...
842 Paragraph * startpar(selection.start.par());
843 Paragraph * endpar(selection.end.par());
844 pos_type const startpos(selection.start.pos());
845 pos_type const endpos(selection.end.pos());
847 if (startpar == endpar) {
848 return startpar->asString(buffer, startpos, endpos, label);
853 // First paragraph in selection
854 result += startpar->asString(buffer, startpos, startpar->size(), label) + "\n\n";
856 // The paragraphs in between (if any)
857 LyXCursor tmpcur(selection.start);
858 tmpcur.par(tmpcur.par()->next());
859 while (tmpcur.par() != endpar) {
860 result += tmpcur.par()->asString(buffer, 0,
861 tmpcur.par()->size(),
863 tmpcur.par(tmpcur.par()->next());
866 // Last paragraph in selection
867 result += endpar->asString(buffer, 0, endpos, label);
873 void LyXText::clearSelection()
875 selection.set(false);
876 selection.mark(false);
877 last_sel_cursor = selection.end = selection.start = selection.cursor = cursor;
878 // reset this in the bv_owner!
879 if (bv_owner && bv_owner->text)
880 bv_owner->text->xsel_cache.set(false);
884 void LyXText::cursorHome()
886 setCursor(cursor.par(), cursor.row()->pos());
890 void LyXText::cursorEnd()
892 if (cursor.par()->empty())
895 // There is a lot of unneeded recalculation going on here:
896 // - boost::next(curosr.row())
897 // - lastPost(*this, cursor.row())
899 if (boost::next(cursor.row()) == rows().end()
900 || boost::next(cursor.row())->par() != cursor.row()->par()) {
901 setCursor(cursor.par(), lastPos(*this, cursor.row()) + 1);
903 if (!cursor.par()->empty() &&
904 (cursor.par()->getChar(lastPos(*this, cursor.row())) == ' '
905 || cursor.par()->isNewline(lastPos(*this, cursor.row())))) {
906 setCursor(cursor.par(), lastPos(*this, cursor.row()));
908 setCursor(cursor.par(),
909 lastPos(*this, cursor.row()) + 1);
915 void LyXText::cursorTop()
917 while (cursor.par()->previous())
918 cursor.par(cursor.par()->previous());
919 setCursor(cursor.par(), 0);
923 void LyXText::cursorBottom()
925 while (cursor.par()->next())
926 cursor.par(cursor.par()->next());
927 setCursor(cursor.par(), cursor.par()->size());
931 void LyXText::toggleFree(LyXFont const & font, bool toggleall)
933 // If the mask is completely neutral, tell user
934 if (font == LyXFont(LyXFont::ALL_IGNORE)) {
935 // Could only happen with user style
936 bv()->owner()->message(_("No font change defined. Use Character under the Layout menu to define font change."));
940 // Try implicit word selection
941 // If there is a change in the language the implicit word selection
943 LyXCursor resetCursor = cursor;
944 bool implicitSelection = (font.language() == ignore_language
945 && font.number() == LyXFont::IGNORE)
946 ? selectWordWhenUnderCursor(WHOLE_WORD_STRICT) : false;
949 setFont(font, toggleall);
951 // Implicit selections are cleared afterwards
952 //and cursor is set to the original position.
953 if (implicitSelection) {
955 cursor = resetCursor;
956 setCursor(cursor.par(), cursor.pos());
957 selection.cursor = cursor;
960 inset_owner->setUpdateStatus(bv(), InsetText::CURSOR_PAR);
964 string LyXText::getStringToIndex()
966 // Try implicit word selection
967 // If there is a change in the language the implicit word selection
969 LyXCursor const reset_cursor = cursor;
970 bool const implicitSelection = selectWordWhenUnderCursor(PREVIOUS_WORD);
973 if (!selection.set())
974 bv()->owner()->message(_("Nothing to index!"));
975 else if (selection.start.par() != selection.end.par())
976 bv()->owner()->message(_("Cannot index more than one paragraph!"));
978 idxstring = selectionAsString(bv()->buffer(), false);
980 // Reset cursors to their original position.
981 cursor = reset_cursor;
982 setCursor(cursor.par(), cursor.pos());
983 selection.cursor = cursor;
985 // Clear the implicit selection.
986 if (implicitSelection)
993 // the DTP switches for paragraphs. LyX will store them in the first
994 // physicla paragraph. When a paragraph is broken, the top settings rest,
995 // the bottom settings are given to the new one. So I can make shure,
996 // they do not duplicate themself and you cannnot make dirty things with
999 void LyXText::setParagraph(bool line_top, bool line_bottom,
1000 bool pagebreak_top, bool pagebreak_bottom,
1001 VSpace const & space_top,
1002 VSpace const & space_bottom,
1003 Spacing const & spacing,
1005 string const & labelwidthstring,
1008 LyXCursor tmpcursor = cursor;
1009 if (!selection.set()) {
1010 selection.start = cursor;
1011 selection.end = cursor;
1014 // make sure that the depth behind the selection are restored, too
1015 Paragraph * endpar = selection.end.par()->next();
1016 Paragraph * undoendpar = endpar;
1018 if (endpar && endpar->getDepth()) {
1019 while (endpar && endpar->getDepth()) {
1020 endpar = endpar->next();
1021 undoendpar = endpar;
1025 // because of parindents etc.
1026 endpar = endpar->next();
1029 setUndo(bv(), Undo::EDIT, selection.start.par(), undoendpar);
1032 Paragraph * tmppar = selection.end.par();
1034 while (tmppar != selection.start.par()->previous()) {
1035 setCursor(tmppar, 0);
1036 postPaint(cursor.y() - cursor.row()->baseline());
1037 cursor.par()->params().lineTop(line_top);
1038 cursor.par()->params().lineBottom(line_bottom);
1039 cursor.par()->params().pagebreakTop(pagebreak_top);
1040 cursor.par()->params().pagebreakBottom(pagebreak_bottom);
1041 cursor.par()->params().spaceTop(space_top);
1042 cursor.par()->params().spaceBottom(space_bottom);
1043 cursor.par()->params().spacing(spacing);
1044 // does the layout allow the new alignment?
1045 LyXLayout_ptr const & layout = cursor.par()->layout();
1047 if (align == LYX_ALIGN_LAYOUT)
1048 align = layout->align;
1049 if (align & layout->alignpossible) {
1050 if (align == layout->align)
1051 cursor.par()->params().align(LYX_ALIGN_LAYOUT);
1053 cursor.par()->params().align(align);
1055 cursor.par()->setLabelWidthString(labelwidthstring);
1056 cursor.par()->params().noindent(noindent);
1057 tmppar = cursor.par()->previous();
1060 redoParagraphs(selection.start, endpar);
1063 setCursor(selection.start.par(), selection.start.pos());
1064 selection.cursor = cursor;
1065 setCursor(selection.end.par(), selection.end.pos());
1067 setCursor(tmpcursor.par(), tmpcursor.pos());
1069 bv()->updateInset(inset_owner);
1073 // set the counter of a paragraph. This includes the labels
1074 void LyXText::setCounter(Buffer const * buf, Paragraph * par)
1076 LyXTextClass const & textclass = buf->params.getLyXTextClass();
1077 LyXLayout_ptr const & layout = par->layout();
1079 if (par->previous()) {
1081 par->params().appendix(par->previous()->params().appendix());
1082 if (!par->params().appendix() && par->params().startOfAppendix()) {
1083 par->params().appendix(true);
1084 textclass.counters().reset();
1086 par->enumdepth = par->previous()->enumdepth;
1087 par->itemdepth = par->previous()->itemdepth;
1089 par->params().appendix(par->params().startOfAppendix());
1094 /* Maybe we have to increment the enumeration depth.
1095 * BUT, enumeration in a footnote is considered in isolation from its
1096 * surrounding paragraph so don't increment if this is the
1097 * first line of the footnote
1098 * AND, bibliographies can't have their depth changed ie. they
1099 * are always of depth 0
1102 && par->previous()->getDepth() < par->getDepth()
1103 && par->previous()->layout()->labeltype == LABEL_COUNTER_ENUMI
1104 && par->enumdepth < 3
1105 && layout->labeltype != LABEL_BIBLIO) {
1109 // Maybe we have to decrement the enumeration depth, see note above
1111 && par->previous()->getDepth() > par->getDepth()
1112 && layout->labeltype != LABEL_BIBLIO) {
1113 par->enumdepth = par->depthHook(par->getDepth())->enumdepth;
1116 if (!par->params().labelString().empty()) {
1117 par->params().labelString(string());
1120 if (layout->margintype == MARGIN_MANUAL) {
1121 if (par->params().labelWidthString().empty()) {
1122 par->setLabelWidthString(layout->labelstring());
1125 par->setLabelWidthString(string());
1128 // is it a layout that has an automatic label?
1129 if (layout->labeltype >= LABEL_COUNTER_CHAPTER) {
1130 int const i = layout->labeltype - LABEL_COUNTER_CHAPTER;
1134 if (i >= 0 && i <= buf->params.secnumdepth) {
1138 textclass.counters().step(layout->latexname());
1140 // Is there a label? Useful for Chapter layout
1141 if (!par->params().appendix()) {
1142 s << layout->labelstring();
1144 s << layout->labelstring_appendix();
1147 // Use of an integer is here less than elegant. For now.
1148 int head = textclass.maxcounter() - LABEL_COUNTER_CHAPTER;
1149 if (!par->params().appendix()) {
1150 numbertype = "sectioning";
1152 numbertype = "appendix";
1153 if (par->isRightToLeftPar(buf->params))
1154 langtype = "hebrew";
1159 s << textclass.counters()
1160 .numberLabel(layout->latexname(),
1161 numbertype, langtype, head);
1163 par->params().labelString(STRCONV(s.str()));
1165 // reset enum counters
1166 textclass.counters().reset("enum");
1167 } else if (layout->labeltype < LABEL_COUNTER_ENUMI) {
1168 textclass.counters().reset("enum");
1169 } else if (layout->labeltype == LABEL_COUNTER_ENUMI) {
1171 // Yes I know this is a really, really! bad solution
1173 string enumcounter("enum");
1175 switch (par->enumdepth) {
1184 enumcounter += "iv";
1187 // not a valid enumdepth...
1191 textclass.counters().step(enumcounter);
1193 s << textclass.counters()
1194 .numberLabel(enumcounter, "enumeration");
1195 par->params().labelString(STRCONV(s.str()));
1197 } else if (layout->labeltype == LABEL_BIBLIO) {// ale970302
1198 textclass.counters().step("bibitem");
1199 int number = textclass.counters().value("bibitem");
1200 if (par->bibitem()) {
1201 par->bibitem()->setCounter(number);
1202 par->params().labelString(layout->labelstring());
1204 // In biblio should't be following counters but...
1206 string s = layout->labelstring();
1208 // the caption hack:
1209 if (layout->labeltype == LABEL_SENSITIVE) {
1210 Paragraph * tmppar = par;
1213 while (tmppar && tmppar->inInset()
1214 // the single '=' is intended below
1215 && (in = tmppar->inInset()->owner())) {
1216 if (in->lyxCode() == Inset::FLOAT_CODE ||
1217 in->lyxCode() == Inset::WRAP_CODE) {
1221 tmppar = in->parOwner();
1227 = textclass.floats().getType(static_cast<InsetFloat*>(in)->type());
1229 textclass.counters().step(fl.type());
1231 // Doesn't work... yet.
1232 #if USE_BOOST_FORMAT
1233 s = boost::io::str(boost::format(_("%1$s #:")) % fl.name());
1234 // s << boost::format(_("%1$s %1$d:")
1236 // % buf->counters().value(fl.name());
1239 //o << fl.name() << ' ' << buf->counters().value(fl.name()) << ":";
1240 o << fl.name() << " #:";
1241 s = STRCONV(o.str());
1244 // par->SetLayout(0);
1245 // s = layout->labelstring;
1246 s = _("Senseless: ");
1249 par->params().labelString(s);
1251 // reset the enumeration counter. They are always reset
1252 // when there is any other layout between
1253 // Just fall-through between the cases so that all
1254 // enum counters deeper than enumdepth is also reset.
1255 switch (par->enumdepth) {
1257 textclass.counters().reset("enumi");
1259 textclass.counters().reset("enumii");
1261 textclass.counters().reset("enumiii");
1263 textclass.counters().reset("enumiv");
1269 // Updates all counters. Paragraphs with changed label string will be rebroken
1270 void LyXText::updateCounters()
1272 RowList::iterator rowit = rows().begin();
1273 Paragraph * par = rowit->par();
1275 // CHECK if this is really needed. (Lgb)
1276 bv()->buffer()->params.getLyXTextClass().counters().reset();
1279 while (rowit->par() != par)
1282 string const oldLabel = par->params().labelString();
1284 // setCounter can potentially change the labelString.
1285 setCounter(bv()->buffer(), par);
1287 string const & newLabel = par->params().labelString();
1289 if (oldLabel.empty() && !newLabel.empty()) {
1290 removeParagraph(rowit);
1291 appendParagraph(rowit);
1299 void LyXText::insertInset(Inset * inset)
1301 if (!cursor.par()->insetAllowed(inset->lyxCode()))
1303 setUndo(bv(), Undo::FINISH, cursor.par(), cursor.par()->next());
1305 cursor.par()->insertInset(cursor.pos(), inset);
1306 // Just to rebreak and refresh correctly.
1307 // The character will not be inserted a second time
1308 insertChar(Paragraph::META_INSET);
1309 // If we enter a highly editable inset the cursor should be to before
1310 // the inset. This couldn't happen before as Undo was not handled inside
1311 // inset now after the Undo LyX tries to call inset->Edit(...) again
1312 // and cannot do this as the cursor is behind the inset and GetInset
1313 // does not return the inset!
1314 if (isHighlyEditableInset(inset)) {
1321 void LyXText::copyEnvironmentType()
1323 copylayouttype = cursor.par()->layout()->name();
1327 void LyXText::pasteEnvironmentType()
1329 // do nothing if there has been no previous copyEnvironmentType()
1330 if (!copylayouttype.empty())
1331 setLayout(copylayouttype);
1335 void LyXText::cutSelection(bool doclear, bool realcut)
1337 // Stuff what we got on the clipboard. Even if there is no selection.
1339 // There is a problem with having the stuffing here in that the
1340 // larger the selection the slower LyX will get. This can be
1341 // solved by running the line below only when the selection has
1342 // finished. The solution used currently just works, to make it
1343 // faster we need to be more clever and probably also have more
1344 // calls to stuffClipboard. (Lgb)
1345 bv()->stuffClipboard(selectionAsString(bv()->buffer(), true));
1347 // This doesn't make sense, if there is no selection
1348 if (!selection.set())
1351 // OK, we have a selection. This is always between selection.start
1352 // and selection.end
1354 // make sure that the depth behind the selection are restored, too
1355 Paragraph * endpar = selection.end.par()->next();
1356 Paragraph * undoendpar = endpar;
1358 if (endpar && endpar->getDepth()) {
1359 while (endpar && endpar->getDepth()) {
1360 endpar = endpar->next();
1361 undoendpar = endpar;
1363 } else if (endpar) {
1364 endpar = endpar->next(); // because of parindents etc.
1367 setUndo(bv(), Undo::DELETE,
1368 selection.start.par(), undoendpar);
1370 // there are two cases: cut only within one paragraph or
1371 // more than one paragraph
1372 if (selection.start.par() == selection.end.par()) {
1373 // only within one paragraph
1374 endpar = selection.end.par();
1375 int pos = selection.end.pos();
1376 CutAndPaste::cutSelection(selection.start.par(), &endpar,
1377 selection.start.pos(), pos,
1378 bv()->buffer()->params.textclass,
1380 selection.end.pos(pos);
1382 endpar = selection.end.par();
1383 int pos = selection.end.pos();
1384 CutAndPaste::cutSelection(selection.start.par(), &endpar,
1385 selection.start.pos(), pos,
1386 bv()->buffer()->params.textclass,
1389 selection.end.par(endpar);
1390 selection.end.pos(pos);
1391 cursor.pos(selection.end.pos());
1393 endpar = endpar->next();
1395 // sometimes necessary
1397 selection.start.par()->stripLeadingSpaces();
1399 redoParagraphs(selection.start, endpar);
1401 // cutSelection can invalidate the cursor so we need to set
1403 // we prefer the end for when tracking changes
1404 cursor = selection.end;
1406 // need a valid cursor. (Lgb)
1409 setCursor(cursor.par(), cursor.pos());
1410 selection.cursor = cursor;
1415 void LyXText::copySelection()
1417 // stuff the selection onto the X clipboard, from an explicit copy request
1418 bv()->stuffClipboard(selectionAsString(bv()->buffer(), true));
1420 // this doesnt make sense, if there is no selection
1421 if (!selection.set())
1424 // ok we have a selection. This is always between selection.start
1425 // and sel_end cursor
1427 // copy behind a space if there is one
1428 while (selection.start.par()->size() > selection.start.pos()
1429 && selection.start.par()->isLineSeparator(selection.start.pos())
1430 && (selection.start.par() != selection.end.par()
1431 || selection.start.pos() < selection.end.pos()))
1432 selection.start.pos(selection.start.pos() + 1);
1434 CutAndPaste::copySelection(selection.start.par(), selection.end.par(),
1435 selection.start.pos(), selection.end.pos(),
1436 bv()->buffer()->params.textclass);
1440 void LyXText::pasteSelection()
1442 // this does not make sense, if there is nothing to paste
1443 if (!CutAndPaste::checkPastePossible())
1446 setUndo(bv(), Undo::INSERT,
1447 cursor.par(), cursor.par()->next());
1450 Paragraph * actpar = cursor.par();
1451 int pos = cursor.pos();
1453 CutAndPaste::pasteSelection(&actpar, &endpar, pos,
1454 bv()->buffer()->params.textclass);
1456 redoParagraphs(cursor, endpar);
1458 setCursor(cursor.par(), cursor.pos());
1461 selection.cursor = cursor;
1462 setCursor(actpar, pos);
1468 void LyXText::setSelectionRange(lyx::pos_type length)
1473 selection.cursor = cursor;
1480 // simple replacing. The font of the first selected character is used
1481 void LyXText::replaceSelectionWithString(string const & str)
1483 setCursorParUndo(bv());
1486 if (!selection.set()) { // create a dummy selection
1487 selection.end = cursor;
1488 selection.start = cursor;
1491 // Get font setting before we cut
1492 pos_type pos = selection.end.pos();
1493 LyXFont const font = selection.start.par()
1494 ->getFontSettings(bv()->buffer()->params,
1495 selection.start.pos());
1497 // Insert the new string
1498 for (string::const_iterator cit = str.begin(); cit != str.end(); ++cit) {
1499 selection.end.par()->insertChar(pos, (*cit), font);
1503 // Cut the selection
1504 cutSelection(true, false);
1510 // needed to insert the selection
1511 void LyXText::insertStringAsLines(string const & str)
1513 Paragraph * par = cursor.par();
1514 pos_type pos = cursor.pos();
1515 Paragraph * endpar = cursor.par()->next();
1517 setCursorParUndo(bv());
1519 // only to be sure, should not be neccessary
1522 bv()->buffer()->insertStringAsLines(par, pos, current_font, str);
1524 redoParagraphs(cursor, endpar);
1525 setCursor(cursor.par(), cursor.pos());
1526 selection.cursor = cursor;
1527 setCursor(par, pos);
1532 // turns double-CR to single CR, others where converted into one
1533 // blank. Then InsertStringAsLines is called
1534 void LyXText::insertStringAsParagraphs(string const & str)
1536 string linestr(str);
1537 bool newline_inserted = false;
1538 for (string::size_type i = 0; i < linestr.length(); ++i) {
1539 if (linestr[i] == '\n') {
1540 if (newline_inserted) {
1541 // we know that \r will be ignored by
1542 // InsertStringA. Of course, it is a dirty
1543 // trick, but it works...
1544 linestr[i - 1] = '\r';
1548 newline_inserted = true;
1550 } else if (IsPrintable(linestr[i])) {
1551 newline_inserted = false;
1554 insertStringAsLines(linestr);
1558 void LyXText::checkParagraph(Paragraph * par, pos_type pos)
1560 LyXCursor tmpcursor;
1564 RowList::iterator row = getRow(par, pos, y);
1565 RowList::iterator beg = rows().begin();
1567 // is there a break one row above
1569 && boost::prior(row)->par() == row->par()) {
1570 z = rowBreakPoint(*boost::prior(row));
1571 if (z >= row->pos()) {
1572 // set the dimensions of the row above
1573 y -= boost::prior(row)->height();
1576 breakAgain(boost::prior(row));
1578 // set the cursor again. Otherwise
1579 // dangling pointers are possible
1580 setCursor(cursor.par(), cursor.pos(),
1581 false, cursor.boundary());
1582 selection.cursor = cursor;
1587 int const tmpheight = row->height();
1588 pos_type const tmplast = lastPos(*this, row);
1591 if (row->height() == tmpheight && lastPos(*this, row) == tmplast) {
1592 postRowPaint(row, y);
1597 // check the special right address boxes
1598 if (par->layout()->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1605 redoDrawingOfParagraph(tmpcursor);
1608 // set the cursor again. Otherwise dangling pointers are possible
1609 // also set the selection
1611 if (selection.set()) {
1613 setCursorIntern(selection.cursor.par(), selection.cursor.pos(),
1614 false, selection.cursor.boundary());
1615 selection.cursor = cursor;
1616 setCursorIntern(selection.start.par(),
1617 selection.start.pos(),
1618 false, selection.start.boundary());
1619 selection.start = cursor;
1620 setCursorIntern(selection.end.par(),
1621 selection.end.pos(),
1622 false, selection.end.boundary());
1623 selection.end = cursor;
1624 setCursorIntern(last_sel_cursor.par(),
1625 last_sel_cursor.pos(),
1626 false, last_sel_cursor.boundary());
1627 last_sel_cursor = cursor;
1630 setCursorIntern(cursor.par(), cursor.pos(),
1631 false, cursor.boundary());
1635 // returns false if inset wasn't found
1636 bool LyXText::updateInset(Inset * inset)
1638 // first check the current paragraph
1639 int pos = cursor.par()->getPositionOfInset(inset);
1641 checkParagraph(cursor.par(), pos);
1645 // check every paragraph
1647 ParagraphList::iterator par = ownerParagraphs().begin();
1648 ParagraphList::iterator end = ownerParagraphs().end();
1651 pos = par->getPositionOfInset(inset);
1653 checkParagraph(&*par, pos);
1657 } while (par != end);
1663 bool LyXText::setCursor(Paragraph * par,
1665 bool setfont, bool boundary)
1667 LyXCursor old_cursor = cursor;
1668 setCursorIntern(par, pos, setfont, boundary);
1669 return deleteEmptyParagraphMechanism(old_cursor);
1673 void LyXText::setCursor(LyXCursor & cur, Paragraph * par,
1674 pos_type pos, bool boundary)
1680 cur.boundary(boundary);
1682 // get the cursor y position in text
1684 RowList::iterator row = getRow(par, pos, y);
1685 RowList::iterator beg = rows().begin();
1687 RowList::iterator old_row = row;
1689 // if we are before the first char of this row and are still in the
1690 // same paragraph and there is a previous row then put the cursor on
1691 // the end of the previous row
1692 cur.iy(y + row->baseline());
1694 if (row != beg && pos &&
1695 boost::prior(row)->par() == row->par() &&
1696 pos < par->size() &&
1697 par->getChar(pos) == Paragraph::META_INSET &&
1698 (ins = par->getInset(pos)) && (ins->needFullRow() || ins->display()))
1705 // y is now the beginning of the cursor row
1706 y += row->baseline();
1707 // y is now the cursor baseline
1710 pos_type last = lastPrintablePos(*this, old_row);
1712 // None of these should happen, but we're scaredy-cats
1713 if (pos > par->size()) {
1714 lyxerr << "dont like 1 please report" << endl;
1717 } else if (pos > last + 1) {
1718 lyxerr << "dont like 2 please report" << endl;
1719 // This shouldn't happen.
1722 } else if (pos < row->pos()) {
1723 lyxerr << "dont like 3 please report" << endl;
1728 // now get the cursors x position
1729 float x = getCursorX(row, pos, last, boundary);
1732 if (old_row != row) {
1733 x = getCursorX(old_row, pos, last, boundary);
1737 /* We take out this for the time being because 1) the redraw code is not
1738 prepared to this yet and 2) because some good policy has yet to be decided
1739 while editting: for instance how to act on rows being created/deleted
1743 //if the cursor is in a visible row, anchor to it
1745 if (topy < y && y < topy + bv()->workHeight())
1751 float LyXText::getCursorX(RowList::iterator rit,
1752 pos_type pos, pos_type last, bool boundary) const
1754 pos_type cursor_vpos = 0;
1756 float fill_separator;
1758 float fill_label_hfill;
1759 // This call HAS to be here because of the BidiTables!!!
1760 prepareToPrint(rit, x, fill_separator, fill_hfill,
1763 if (last < rit->pos())
1764 cursor_vpos = rit->pos();
1765 else if (pos > last && !boundary)
1766 cursor_vpos = (rit->par()->isRightToLeftPar(bv()->buffer()->params))
1767 ? rit->pos() : last + 1;
1768 else if (pos > rit->pos() &&
1769 (pos > last || boundary))
1770 /// Place cursor after char at (logical) position pos - 1
1771 cursor_vpos = (bidi_level(pos - 1) % 2 == 0)
1772 ? log2vis(pos - 1) + 1 : log2vis(pos - 1);
1774 /// Place cursor before char at (logical) position pos
1775 cursor_vpos = (bidi_level(pos) % 2 == 0)
1776 ? log2vis(pos) : log2vis(pos) + 1;
1778 pos_type body_pos = rit->par()->beginningOfBody();
1779 if ((body_pos > 0) &&
1780 ((body_pos - 1 > last) ||
1781 !rit->par()->isLineSeparator(body_pos - 1)))
1784 for (pos_type vpos = rit->pos(); vpos < cursor_vpos; ++vpos) {
1785 pos_type pos = vis2log(vpos);
1786 if (body_pos > 0 && pos == body_pos - 1) {
1787 x += fill_label_hfill +
1788 font_metrics::width(
1789 rit->par()->layout()->labelsep,
1790 getLabelFont(bv()->buffer(),
1792 if (rit->par()->isLineSeparator(body_pos - 1))
1793 x -= singleWidth(rit->par(), body_pos - 1);
1796 if (hfillExpansion(*this, rit, pos)) {
1797 x += singleWidth(rit->par(), pos);
1798 if (pos >= body_pos)
1801 x += fill_label_hfill;
1802 } else if (rit->par()->isSeparator(pos)) {
1803 x += singleWidth(rit->par(), pos);
1804 if (pos >= body_pos)
1805 x += fill_separator;
1807 x += singleWidth(rit->par(), pos);
1813 void LyXText::setCursorIntern(Paragraph * par,
1814 pos_type pos, bool setfont, bool boundary)
1816 InsetText * it = static_cast<InsetText *>(par->inInset());
1818 if (it != inset_owner) {
1819 lyxerr[Debug::INSETS] << "InsetText is " << it
1821 << "inset_owner is "
1822 << inset_owner << endl;
1823 #ifdef WITH_WARNINGS
1824 #warning I believe this code is wrong. (Lgb)
1825 #warning Jürgen, have a look at this. (Lgb)
1826 #warning Hmmm, I guess you are right but we
1827 #warning should verify when this is needed
1829 // Jürgen, would you like to have a look?
1830 // I guess we need to move the outer cursor
1831 // and open and lock the inset (bla bla bla)
1832 // stuff I don't know... so can you have a look?
1834 // I moved the lyxerr stuff in here so we can see if
1835 // this is actually really needed and where!
1837 // it->getLyXText(bv())->setCursorIntern(bv(), par, pos, setfont, boundary);
1842 setCursor(cursor, par, pos, boundary);
1848 void LyXText::setCurrentFont()
1850 pos_type pos = cursor.pos();
1851 if (cursor.boundary() && pos > 0)
1855 if (pos == cursor.par()->size())
1857 else // potentional bug... BUG (Lgb)
1858 if (cursor.par()->isSeparator(pos)) {
1859 if (pos > cursor.row()->pos() &&
1860 bidi_level(pos) % 2 ==
1861 bidi_level(pos - 1) % 2)
1863 else if (pos + 1 < cursor.par()->size())
1869 cursor.par()->getFontSettings(bv()->buffer()->params, pos);
1870 real_current_font = getFont(bv()->buffer(), cursor.par(), pos);
1872 if (cursor.pos() == cursor.par()->size() &&
1873 isBoundary(bv()->buffer(), cursor.par(), cursor.pos()) &&
1874 !cursor.boundary()) {
1875 Language const * lang =
1876 cursor.par()->getParLanguage(bv()->buffer()->params);
1877 current_font.setLanguage(lang);
1878 current_font.setNumber(LyXFont::OFF);
1879 real_current_font.setLanguage(lang);
1880 real_current_font.setNumber(LyXFont::OFF);
1885 // returns the column near the specified x-coordinate of the row
1886 // x is set to the real beginning of this column
1888 LyXText::getColumnNearX(RowList::iterator rit, int & x, bool & boundary) const
1891 float fill_separator;
1893 float fill_label_hfill;
1895 prepareToPrint(rit, tmpx, fill_separator,
1896 fill_hfill, fill_label_hfill);
1898 pos_type vc = rit->pos();
1899 pos_type last = lastPrintablePos(*this, rit);
1902 LyXLayout_ptr const & layout = rit->par()->layout();
1904 bool left_side = false;
1906 pos_type body_pos = rit->par()->beginningOfBody();
1907 float last_tmpx = tmpx;
1910 (body_pos - 1 > last ||
1911 !rit->par()->isLineSeparator(body_pos - 1)))
1914 // check for empty row
1915 if (!rit->par()->size()) {
1920 while (vc <= last && tmpx <= x) {
1923 if (body_pos > 0 && c == body_pos - 1) {
1924 tmpx += fill_label_hfill +
1925 font_metrics::width(layout->labelsep,
1926 getLabelFont(bv()->buffer(), rit->par()));
1927 if (rit->par()->isLineSeparator(body_pos - 1))
1928 tmpx -= singleWidth(rit->par(), body_pos - 1);
1931 if (hfillExpansion(*this, rit, c)) {
1932 tmpx += singleWidth(rit->par(), c);
1936 tmpx += fill_label_hfill;
1937 } else if (rit->par()->isSeparator(c)) {
1938 tmpx += singleWidth(rit->par(), c);
1940 tmpx+= fill_separator;
1942 tmpx += singleWidth(rit->par(), c);
1947 if ((tmpx + last_tmpx) / 2 > x) {
1952 if (vc > last + 1) // This shouldn't happen.
1956 // This (rtl_support test) is not needed, but gives
1957 // some speedup if rtl_support=false
1958 bool const lastrow = lyxrc.rtl_support &&
1959 (boost::next(rit) == rowlist_.end() ||
1960 boost::next(rit)->par() != rit->par());
1961 // If lastrow is false, we don't need to compute
1962 // the value of rtl.
1963 bool const rtl = (lastrow)
1964 ? rit->par()->isRightToLeftPar(bv()->buffer()->params)
1967 ((rtl && left_side && vc == rit->pos() && x < tmpx - 5) ||
1968 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5)))
1970 else if (vc == rit->pos()) {
1972 if (bidi_level(c) % 2 == 1)
1975 c = vis2log(vc - 1);
1976 bool const rtl = (bidi_level(c) % 2 == 1);
1977 if (left_side == rtl) {
1979 boundary = isBoundary(bv()->buffer(), rit->par(), c);
1983 if (rit->pos() <= last && c > last
1984 && rit->par()->isNewline(last)) {
1985 if (bidi_level(last) % 2 == 0)
1986 tmpx -= singleWidth(rit->par(), last);
1988 tmpx += singleWidth(rit->par(), last);
1998 void LyXText::setCursorFromCoordinates(int x, int y)
2000 LyXCursor old_cursor = cursor;
2002 setCursorFromCoordinates(cursor, x, y);
2004 deleteEmptyParagraphMechanism(old_cursor);
2011 * return true if the cursor given is at the end of a row,
2012 * and the next row is filled by an inset that spans an entire
2015 bool beforeFullRowInset(LyXText & lt, RowList::iterator row,
2017 if (boost::next(row) == lt.rows().end())
2019 Row const & next = *boost::next(row);
2021 if (next.pos() != cur.pos() || next.par() != cur.par())
2023 if (!cur.par()->isInset(cur.pos()))
2025 Inset const * inset = cur.par()->getInset(cur.pos());
2026 if (inset->needFullRow() || inset->display())
2033 void LyXText::setCursorFromCoordinates(LyXCursor & cur, int x, int y)
2035 // Get the row first.
2037 RowList::iterator row = getRowNearY(y);
2039 pos_type const column = getColumnNearX(row, x, bound);
2040 cur.par(row->par());
2041 cur.pos(row->pos() + column);
2043 cur.y(y + row->baseline());
2046 if (beforeFullRowInset(*this, row, cur)) {
2047 pos_type last = lastPrintablePos(*this, row);
2048 float x = getCursorX(boost::next(row), cur.pos(), last, bound);
2050 cur.iy(y + row->height() + boost::next(row)->baseline());
2051 cur.irow(boost::next(row));
2057 cur.boundary(bound);
2061 void LyXText::cursorLeft(bool internal)
2063 if (cursor.pos() > 0) {
2064 bool boundary = cursor.boundary();
2065 setCursor(cursor.par(), cursor.pos() - 1, true, false);
2066 if (!internal && !boundary &&
2067 isBoundary(bv()->buffer(), cursor.par(), cursor.pos() + 1))
2068 setCursor(cursor.par(), cursor.pos() + 1, true, true);
2069 } else if (cursor.par()->previous()) { // steps into the above paragraph.
2070 Paragraph * par = cursor.par()->previous();
2071 setCursor(par, par->size());
2076 void LyXText::cursorRight(bool internal)
2078 if (!internal && cursor.boundary() &&
2079 !cursor.par()->isNewline(cursor.pos()))
2080 setCursor(cursor.par(), cursor.pos(), true, false);
2081 else if (cursor.pos() < cursor.par()->size()) {
2082 setCursor(cursor.par(), cursor.pos() + 1, true, false);
2084 isBoundary(bv()->buffer(), cursor.par(), cursor.pos()))
2085 setCursor(cursor.par(), cursor.pos(), true, true);
2086 } else if (cursor.par()->next())
2087 setCursor(cursor.par()->next(), 0);
2091 void LyXText::cursorUp(bool selecting)
2094 int x = cursor.x_fix();
2095 int y = cursor.y() - cursor.row()->baseline() - 1;
2096 setCursorFromCoordinates(x, y);
2099 int y1 = cursor.iy() - topy;
2102 Inset * inset_hit = checkInsetHit(x, y1);
2103 if (inset_hit && isHighlyEditableInset(inset_hit)) {
2104 inset_hit->edit(bv(), x, y - (y2 - y1), mouse_button::none);
2108 setCursorFromCoordinates(bv(), cursor.x_fix(),
2109 cursor.y() - cursor.row()->baseline() - 1);
2114 void LyXText::cursorDown(bool selecting)
2117 int x = cursor.x_fix();
2118 int y = cursor.y() - cursor.row()->baseline() +
2119 cursor.row()->height() + 1;
2120 setCursorFromCoordinates(x, y);
2121 if (!selecting && cursor.row() == cursor.irow()) {
2123 int y1 = cursor.iy() - topy;
2126 Inset * inset_hit = checkInsetHit(x, y1);
2127 if (inset_hit && isHighlyEditableInset(inset_hit)) {
2128 inset_hit->edit(bv(), x, y - (y2 - y1), mouse_button::none);
2132 setCursorFromCoordinates(bv(), cursor.x_fix(),
2133 cursor.y() - cursor.row()->baseline()
2134 + cursor.row()->height() + 1);
2139 void LyXText::cursorUpParagraph()
2141 if (cursor.pos() > 0) {
2142 setCursor(cursor.par(), 0);
2144 else if (cursor.par()->previous()) {
2145 setCursor(cursor.par()->previous(), 0);
2150 void LyXText::cursorDownParagraph()
2152 if (cursor.par()->next()) {
2153 setCursor(cursor.par()->next(), 0);
2155 setCursor(cursor.par(), cursor.par()->size());
2159 // fix the cursor `cur' after a characters has been deleted at `where'
2160 // position. Called by deleteEmptyParagraphMechanism
2161 void LyXText::fixCursorAfterDelete(LyXCursor & cur,
2162 LyXCursor const & where)
2164 // if cursor is not in the paragraph where the delete occured,
2166 if (cur.par() != where.par())
2169 // if cursor position is after the place where the delete occured,
2171 if (cur.pos() > where.pos())
2172 cur.pos(cur.pos()-1);
2174 // check also if we don't want to set the cursor on a spot behind the
2175 // pagragraph because we erased the last character.
2176 if (cur.pos() > cur.par()->size())
2177 cur.pos(cur.par()->size());
2179 // recompute row et al. for this cursor
2180 setCursor(cur, cur.par(), cur.pos(), cur.boundary());
2184 bool LyXText::deleteEmptyParagraphMechanism(LyXCursor const & old_cursor)
2186 // Would be wrong to delete anything if we have a selection.
2187 if (selection.set())
2190 // We allow all kinds of "mumbo-jumbo" when freespacing.
2191 if (old_cursor.par()->layout()->free_spacing
2192 || old_cursor.par()->isFreeSpacing()) {
2196 /* Ok I'll put some comments here about what is missing.
2197 I have fixed BackSpace (and thus Delete) to not delete
2198 double-spaces automagically. I have also changed Cut,
2199 Copy and Paste to hopefully do some sensible things.
2200 There are still some small problems that can lead to
2201 double spaces stored in the document file or space at
2202 the beginning of paragraphs. This happens if you have
2203 the cursor betwenn to spaces and then save. Or if you
2204 cut and paste and the selection have a space at the
2205 beginning and then save right after the paste. I am
2206 sure none of these are very hard to fix, but I will
2207 put out 1.1.4pre2 with FIX_DOUBLE_SPACE defined so
2208 that I can get some feedback. (Lgb)
2211 // If old_cursor.pos() == 0 and old_cursor.pos()(1) == LineSeparator
2212 // delete the LineSeparator.
2215 // If old_cursor.pos() == 1 and old_cursor.pos()(0) == LineSeparator
2216 // delete the LineSeparator.
2219 // If the pos around the old_cursor were spaces, delete one of them.
2220 if (old_cursor.par() != cursor.par()
2221 || old_cursor.pos() != cursor.pos()) {
2222 // Only if the cursor has really moved
2224 if (old_cursor.pos() > 0
2225 && old_cursor.pos() < old_cursor.par()->size()
2226 && old_cursor.par()->isLineSeparator(old_cursor.pos())
2227 && old_cursor.par()->isLineSeparator(old_cursor.pos() - 1)) {
2228 old_cursor.par()->erase(old_cursor.pos() - 1);
2229 redoParagraphs(old_cursor, old_cursor.par()->next());
2231 #ifdef WITH_WARNINGS
2232 #warning This will not work anymore when we have multiple views of the same buffer
2233 // In this case, we will have to correct also the cursors held by
2234 // other bufferviews. It will probably be easier to do that in a more
2235 // automated way in LyXCursor code. (JMarc 26/09/2001)
2237 // correct all cursors held by the LyXText
2238 fixCursorAfterDelete(cursor, old_cursor);
2239 fixCursorAfterDelete(selection.cursor,
2241 fixCursorAfterDelete(selection.start,
2243 fixCursorAfterDelete(selection.end, old_cursor);
2244 fixCursorAfterDelete(last_sel_cursor,
2246 fixCursorAfterDelete(toggle_cursor, old_cursor);
2247 fixCursorAfterDelete(toggle_end_cursor,
2253 // don't delete anything if this is the ONLY paragraph!
2254 if (!old_cursor.par()->next() && !old_cursor.par()->previous())
2257 // Do not delete empty paragraphs with keepempty set.
2258 if (old_cursor.par()->layout()->keepempty)
2261 // only do our magic if we changed paragraph
2262 if (old_cursor.par() == cursor.par())
2265 // record if we have deleted a paragraph
2266 // we can't possibly have deleted a paragraph before this point
2267 bool deleted = false;
2269 if ((old_cursor.par()->empty()
2270 || (old_cursor.par()->size() == 1
2271 && old_cursor.par()->isLineSeparator(0)))) {
2272 // ok, we will delete anything
2273 LyXCursor tmpcursor;
2277 if (old_cursor.row() != rows().begin()) {
2278 const_cast<LyXText *>(this)->postPaint(old_cursor.y() - old_cursor.row()->baseline()
2279 - boost::prior(old_cursor.row())->height());
2281 cursor = old_cursor; // that undo can restore the right cursor position
2282 Paragraph * endpar = old_cursor.par()->next();
2283 if (endpar && endpar->getDepth()) {
2284 while (endpar && endpar->getDepth()) {
2285 endpar = endpar->next();
2288 setUndo(bv(), Undo::DELETE, old_cursor.par(), endpar);
2292 removeRow(old_cursor.row());
2293 if (ownerParagraphs().begin() == old_cursor.par()) {
2294 ownerParagraph(&*boost::next(ownerParagraphs().begin()));
2297 delete old_cursor.par();
2299 /* Breakagain the next par. Needed because of
2300 * the parindent that can occur or dissappear.
2301 * The next row can change its height, if
2302 * there is another layout before */
2303 if (refresh_row != rows().end()) {
2304 if (boost::next(refresh_row) != rows().end()) {
2305 breakAgain(boost::next(refresh_row));
2308 setHeightOfRow(refresh_row);
2311 RowList::iterator nextrow = boost::next(old_cursor.row());
2312 const_cast<LyXText *>(this)->postPaint(
2313 old_cursor.y() - old_cursor.row()->baseline());
2316 cursor = old_cursor; // that undo can restore the right cursor position
2317 Paragraph * endpar = old_cursor.par()->next();
2318 if (endpar && endpar->getDepth()) {
2319 while (endpar && endpar->getDepth()) {
2320 endpar = endpar->next();
2323 setUndo(bv(), Undo::DELETE, old_cursor.par(), endpar);
2327 removeRow(old_cursor.row());
2329 if (ownerParagraphs().begin() == old_cursor.par()) {
2330 ownerParagraph(&*boost::next(ownerParagraphs().begin()));
2333 delete old_cursor.par();
2335 /* Breakagain the next par. Needed because of
2336 the parindent that can occur or dissappear.
2337 The next row can change its height, if
2338 there is another layout before */
2339 if (nextrow != rows().end()) {
2340 breakAgain(nextrow);
2346 setCursorIntern(cursor.par(), cursor.pos());
2348 if (selection.cursor.par() == old_cursor.par()
2349 && selection.cursor.pos() == old_cursor.pos()) {
2350 // correct selection
2351 selection.cursor = cursor;
2355 if (old_cursor.par()->stripLeadingSpaces()) {
2356 redoParagraphs(old_cursor,
2357 old_cursor.par()->next());
2359 setCursorIntern(cursor.par(), cursor.pos());
2360 selection.cursor = cursor;
2367 ParagraphList & LyXText::ownerParagraphs() const
2370 return inset_owner->paragraphs;
2372 return bv_owner->buffer()->paragraphs;
2376 void LyXText::ownerParagraph(Paragraph * p) const
2379 inset_owner->paragraph(p);
2381 bv_owner->buffer()->paragraphs.set(p);
2386 void LyXText::ownerParagraph(int id, Paragraph * p) const
2388 Paragraph * op = bv_owner->buffer()->getParFromID(id);
2389 if (op && op->inInset()) {
2390 static_cast<InsetText *>(op->inInset())->paragraph(p);
2397 LyXText::refresh_status LyXText::refreshStatus() const
2399 return refresh_status_;
2403 void LyXText::clearPaint()
2405 refresh_status_ = REFRESH_NONE;
2406 refresh_row = rows().end();
2411 void LyXText::postPaint(int start_y)
2413 refresh_status old = refresh_status_;
2415 refresh_status_ = REFRESH_AREA;
2416 refresh_row = rows().end();
2418 if (old != REFRESH_NONE && refresh_y < start_y)
2421 refresh_y = start_y;
2426 // We are an inset's lyxtext. Tell the top-level lyxtext
2427 // it needs to update the row we're in.
2428 LyXText * t = bv()->text;
2429 t->postRowPaint(t->cursor.row(), t->cursor.y() - t->cursor.row()->baseline());
2433 // FIXME: we should probably remove this y parameter,
2434 // make refresh_y be 0, and use row->y etc.
2435 void LyXText::postRowPaint(RowList::iterator rit, int start_y)
2437 if (refresh_status_ != REFRESH_NONE && refresh_y < start_y) {
2438 refresh_status_ = REFRESH_AREA;
2441 refresh_y = start_y;
2444 if (refresh_status_ == REFRESH_AREA)
2447 refresh_status_ = REFRESH_ROW;
2453 // We are an inset's lyxtext. Tell the top-level lyxtext
2454 // it needs to update the row we're in.
2455 LyXText * t = bv()->text;
2456 t->postRowPaint(t->cursor.row(), t->cursor.y() - t->cursor.row()->baseline());
2460 bool LyXText::isInInset() const
2462 // Sub-level has non-null bv owner and
2463 // non-null inset owner.
2464 return inset_owner != 0 && bv_owner != 0;
2468 int defaultRowHeight()
2470 LyXFont const font(LyXFont::ALL_SANE);
2471 return int(font_metrics::maxAscent(font)
2472 + font_metrics::maxDescent(font) * 1.5);