1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
16 #include "paragraph.h"
17 #include "funcrequest.h"
18 #include "frontends/LyXView.h"
19 #include "undo_funcs.h"
21 #include "buffer_funcs.h"
22 #include "bufferparams.h"
23 #include "errorlist.h"
25 #include "BufferView.h"
26 #include "CutAndPaste.h"
27 #include "frontends/Painter.h"
28 #include "frontends/font_metrics.h"
32 #include "FloatList.h"
34 #include "ParagraphParameters.h"
36 #include "lyxrow_funcs.h"
37 #include "metricsinfo.h"
38 #include "paragraph_funcs.h"
40 #include "insets/insetbibitem.h"
41 #include "insets/insetenv.h"
42 #include "insets/insetfloat.h"
43 #include "insets/insetwrap.h"
45 #include "support/LAssert.h"
46 #include "support/textutils.h"
47 #include "support/lstrings.h"
49 #include <boost/tuple/tuple.hpp>
53 using namespace lyx::support;
63 LyXText::LyXText(BufferView * bv)
64 : height(0), width(0), anchor_row_offset_(0),
65 inset_owner(0), the_locking_inset(0), bv_owner(bv)
67 anchor_row_ = rows().end();
72 LyXText::LyXText(BufferView * bv, InsetText * inset)
73 : height(0), width(0), anchor_row_offset_(0),
74 inset_owner(inset), the_locking_inset(0), bv_owner(bv)
76 anchor_row_ = rows().end();
81 void LyXText::init(BufferView * bview)
89 anchor_row_ = rows().end();
90 anchor_row_offset_ = 0;
92 current_font = getFont(ownerParagraphs().begin(), 0);
94 redoParagraphs(ownerParagraphs().begin(), ownerParagraphs().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 LyXText::getFont(ParagraphList::iterator pit, pos_type pos) const
114 LyXLayout_ptr const & layout = pit->layout();
116 BufferParams const & params = bv()->buffer()->params;
118 // We specialize the 95% common case:
119 if (!pit->getDepth()) {
120 if (layout->labeltype == LABEL_MANUAL
121 && pos < pit->beginningOfBody()) {
123 LyXFont f = pit->getFontSettings(params, pos);
125 pit->inInset()->getDrawFont(f);
126 return f.realize(layout->reslabelfont);
128 LyXFont f = pit->getFontSettings(params, pos);
130 pit->inInset()->getDrawFont(f);
131 return f.realize(layout->resfont);
135 // The uncommon case need not be optimized as much
139 if (pos < pit->beginningOfBody()) {
141 layoutfont = layout->labelfont;
144 layoutfont = layout->font;
147 LyXFont tmpfont = pit->getFontSettings(params, pos);
148 tmpfont.realize(layoutfont);
151 pit->inInset()->getDrawFont(tmpfont);
153 // Realize with the fonts of lesser depth.
154 tmpfont.realize(outerFont(pit, ownerParagraphs()));
155 tmpfont.realize(defaultfont_);
161 LyXFont LyXText::getLayoutFont(ParagraphList::iterator pit) const
163 LyXLayout_ptr const & layout = pit->layout();
165 if (!pit->getDepth())
166 return layout->resfont;
168 LyXFont font = layout->font;
169 // Realize with the fonts of lesser depth.
170 font.realize(outerFont(pit, ownerParagraphs()));
171 font.realize(defaultfont_);
177 LyXFont LyXText::getLabelFont(ParagraphList::iterator pit) const
179 LyXLayout_ptr const & layout = pit->layout();
181 if (!pit->getDepth())
182 return layout->reslabelfont;
184 LyXFont font = layout->labelfont;
185 // Realize with the fonts of lesser depth.
186 font.realize(outerFont(pit, ownerParagraphs()));
187 font.realize(defaultfont_);
193 void LyXText::setCharFont(ParagraphList::iterator pit,
194 pos_type pos, LyXFont const & fnt,
197 BufferParams const & params = bv()->buffer()->params;
198 LyXFont font = getFont(pit, pos);
199 font.update(fnt, params.language, toggleall);
200 // Let the insets convert their font
201 if (pit->isInset(pos)) {
202 InsetOld * inset = pit->getInset(pos);
203 if (isEditableInset(inset)) {
204 static_cast<UpdatableInset *>(inset)
205 ->setFont(bv(), fnt, toggleall, true);
209 // Plug through to version below:
210 setCharFont(pit, pos, font);
214 void LyXText::setCharFont(
215 ParagraphList::iterator pit, pos_type pos, LyXFont const & fnt)
218 LyXLayout_ptr const & layout = pit->layout();
220 // Get concrete layout font to reduce against
223 if (pos < pit->beginningOfBody())
224 layoutfont = layout->labelfont;
226 layoutfont = layout->font;
228 // Realize against environment font information
229 if (pit->getDepth()) {
230 ParagraphList::iterator tp = pit;
231 while (!layoutfont.resolved() &&
232 tp != ownerParagraphs().end() &&
234 tp = outerHook(tp, ownerParagraphs());
235 if (tp != ownerParagraphs().end())
236 layoutfont.realize(tp->layout()->font);
240 layoutfont.realize(defaultfont_);
242 // Now, reduce font against full layout font
243 font.reduce(layoutfont);
245 pit->setFont(pos, font);
249 // removes the row and reset the touched counters
250 void LyXText::removeRow(RowList::iterator rit)
252 if (anchor_row_ == rit) {
253 if (rit != rows().begin()) {
254 anchor_row_ = boost::prior(rit);
255 anchor_row_offset_ += anchor_row_->height();
257 anchor_row_ = boost::next(rit);
258 anchor_row_offset_ -= rit->height();
262 // the text becomes smaller
263 height -= rit->height();
269 // remove all following rows of the paragraph of the specified row.
270 void LyXText::removeParagraph(RowList::iterator rit)
272 ParagraphList::iterator tmppit = rit->par();
275 while (rit != rows().end() && rit->par() == tmppit) {
276 RowList::iterator tmprit = boost::next(rit);
283 void LyXText::insertParagraph(ParagraphList::iterator pit,
284 RowList::iterator rowit)
286 // insert a new row, starting at position 0
288 RowList::iterator rit = rowlist_.insert(rowit, newrow);
290 // and now append the whole paragraph before the new row
291 appendParagraph(rit);
295 InsetOld * LyXText::getInset() const
297 ParagraphList::iterator pit = cursor.par();
298 pos_type const pos = cursor.pos();
300 if (pos < pit->size() && pit->isInset(pos)) {
301 return pit->getInset(pos);
307 void LyXText::toggleInset()
309 InsetOld * inset = getInset();
310 // is there an editable inset at cursor position?
311 if (!isEditableInset(inset)) {
312 // No, try to see if we are inside a collapsable inset
313 if (inset_owner && inset_owner->owner()
314 && inset_owner->owner()->isOpen()) {
315 bv()->unlockInset(inset_owner->owner());
316 inset_owner->owner()->close(bv());
317 bv()->getLyXText()->cursorRight(bv());
321 //bv()->owner()->message(inset->editMessage());
323 // do we want to keep this?? (JMarc)
324 if (!isHighlyEditableInset(inset))
325 recordUndo(bv(), Undo::ATOMIC);
327 if (inset->isOpen()) {
333 bv()->updateInset(inset);
337 /* used in setlayout */
338 // Asger is not sure we want to do this...
339 void LyXText::makeFontEntriesLayoutSpecific(BufferParams const & params,
342 LyXLayout_ptr const & layout = par.layout();
343 pos_type const psize = par.size();
346 for (pos_type pos = 0; pos < psize; ++pos) {
347 if (pos < par.beginningOfBody())
348 layoutfont = layout->labelfont;
350 layoutfont = layout->font;
352 LyXFont tmpfont = par.getFontSettings(params, pos);
353 tmpfont.reduce(layoutfont);
354 par.setFont(pos, tmpfont);
359 ParagraphList::iterator
360 LyXText::setLayout(LyXCursor & cur, LyXCursor & sstart_cur,
361 LyXCursor & send_cur,
362 string const & layout)
364 ParagraphList::iterator endpit = boost::next(send_cur.par());
365 ParagraphList::iterator undoendpit = endpit;
366 ParagraphList::iterator pars_end = ownerParagraphs().end();
368 if (endpit != pars_end && endpit->getDepth()) {
369 while (endpit != pars_end && endpit->getDepth()) {
373 } else if (endpit != pars_end) {
374 // because of parindents etc.
378 recordUndo(bv(), Undo::ATOMIC, sstart_cur.par(), boost::prior(undoendpit));
380 // ok we have a selection. This is always between sstart_cur
381 // and sel_end cursor
383 ParagraphList::iterator pit = sstart_cur.par();
384 ParagraphList::iterator epit = boost::next(send_cur.par());
386 LyXLayout_ptr const & lyxlayout =
387 bv()->buffer()->params.getLyXTextClass()[layout];
390 pit->applyLayout(lyxlayout);
391 makeFontEntriesLayoutSpecific(bv()->buffer()->params, *pit);
392 ParagraphList::iterator fppit = pit;
393 fppit->params().spaceTop(lyxlayout->fill_top ?
394 VSpace(VSpace::VFILL)
395 : VSpace(VSpace::NONE));
396 fppit->params().spaceBottom(lyxlayout->fill_bottom ?
397 VSpace(VSpace::VFILL)
398 : VSpace(VSpace::NONE));
399 if (lyxlayout->margintype == MARGIN_MANUAL)
400 pit->setLabelWidthString(lyxlayout->labelstring());
403 } while (pit != epit);
409 // set layout over selection and make a total rebreak of those paragraphs
410 void LyXText::setLayout(string const & layout)
412 LyXCursor tmpcursor = cursor; // store the current cursor
414 // if there is no selection just set the layout
415 // of the current paragraph
416 if (!selection.set()) {
417 selection.start = cursor; // dummy selection
418 selection.end = cursor;
421 // special handling of new environment insets
422 BufferParams const & params = bv()->buffer()->params;
423 LyXLayout_ptr const & lyxlayout = params.getLyXTextClass()[layout];
424 if (lyxlayout->is_environment) {
425 // move everything in a new environment inset
426 lyxerr << "setting layout " << layout << endl;
427 bv()->owner()->dispatch(FuncRequest(LFUN_HOME));
428 bv()->owner()->dispatch(FuncRequest(LFUN_ENDSEL));
429 bv()->owner()->dispatch(FuncRequest(LFUN_CUT));
430 InsetOld * inset = new InsetEnvironment(params, layout);
431 if (bv()->insertInset(inset)) {
433 //bv()->owner()->dispatch(FuncRequest(LFUN_PASTE));
440 ParagraphList::iterator endpit = setLayout(cursor, selection.start,
441 selection.end, layout);
442 redoParagraphs(selection.start.par(), endpit);
444 // we have to reset the selection, because the
445 // geometry could have changed
446 setCursor(selection.start.par(), selection.start.pos(), false);
447 selection.cursor = cursor;
448 setCursor(selection.end.par(), selection.end.pos(), false);
452 setCursor(tmpcursor.par(), tmpcursor.pos(), true);
456 bool LyXText::changeDepth(bv_funcs::DEPTH_CHANGE type, bool test_only)
458 ParagraphList::iterator pit(cursor.par());
459 ParagraphList::iterator end(cursor.par());
460 ParagraphList::iterator start = pit;
462 if (selection.set()) {
463 pit = selection.start.par();
464 end = selection.end.par();
468 ParagraphList::iterator pastend = boost::next(end);
471 recordUndo(bv(), Undo::ATOMIC, start, end);
473 bool changed = false;
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();
482 int const depth = pit->params().depth();
483 if (type == bv_funcs::INC_DEPTH) {
484 if (depth < prev_after_depth
485 && pit->layout()->labeltype != LABEL_BIBLIO) {
488 pit->params().depth(depth + 1);
495 pit->params().depth(depth - 1);
498 prev_after_depth = pit->getMaxDepthAfter();
511 redoParagraphs(start, pastend);
513 // We need to actually move the text->cursor. I don't
514 // understand why ...
515 LyXCursor tmpcursor = cursor;
517 // we have to reset the visual selection because the
518 // geometry could have changed
519 if (selection.set()) {
520 setCursor(selection.start.par(), selection.start.pos());
521 selection.cursor = cursor;
522 setCursor(selection.end.par(), selection.end.pos());
525 // this handles the counter labels, and also fixes up
526 // depth values for follow-on (child) paragraphs
530 setCursor(tmpcursor.par(), tmpcursor.pos());
536 // set font over selection and make a total rebreak of those paragraphs
537 void LyXText::setFont(LyXFont const & font, bool toggleall)
539 // if there is no selection just set the current_font
540 if (!selection.set()) {
541 // Determine basis font
543 if (cursor.pos() < cursor.par()->beginningOfBody()) {
544 layoutfont = getLabelFont(cursor.par());
546 layoutfont = getLayoutFont(cursor.par());
548 // Update current font
549 real_current_font.update(font,
550 bv()->buffer()->params.language,
553 // Reduce to implicit settings
554 current_font = real_current_font;
555 current_font.reduce(layoutfont);
556 // And resolve it completely
557 real_current_font.realize(layoutfont);
562 LyXCursor tmpcursor = cursor; // store the current cursor
564 // ok we have a selection. This is always between sel_start_cursor
565 // and sel_end cursor
567 recordUndo(bv(), Undo::ATOMIC, selection.start.par(), selection.end.par());
569 cursor = selection.start;
570 while (cursor.par() != selection.end.par() ||
571 cursor.pos() < selection.end.pos())
573 if (cursor.pos() < cursor.par()->size()) {
574 // an open footnote should behave like a closed one
575 setCharFont(cursor.par(), cursor.pos(),
577 cursor.pos(cursor.pos() + 1);
580 cursor.par(boost::next(cursor.par()));
585 redoParagraph(selection.start.par());
587 // we have to reset the selection, because the
588 // geometry could have changed, but we keep
589 // it for user convenience
590 setCursor(selection.start.par(), selection.start.pos());
591 selection.cursor = cursor;
592 setCursor(selection.end.par(), selection.end.pos());
594 setCursor(tmpcursor.par(), tmpcursor.pos(), true,
595 tmpcursor.boundary());
599 void LyXText::redoHeightOfParagraph()
601 RowList::iterator tmprow = cursorRow();
603 setHeightOfRow(tmprow);
605 while (tmprow != rows().begin()
606 && boost::prior(tmprow)->par() == tmprow->par()) {
608 setHeightOfRow(tmprow);
613 setCursor(cursor.par(), cursor.pos(), false, cursor.boundary());
617 RowList::iterator LyXText::firstRow(ParagraphList::iterator pit)
619 RowList::iterator rit;
620 for (rit = rows().begin(); rit != rows().end(); ++rit)
621 if (rit->par() == pit)
627 // rebreaks all paragraphs between the specified pars
628 // This function is needed after SetLayout and SetFont etc.
629 void LyXText::redoParagraphs(ParagraphList::iterator start,
630 ParagraphList::iterator end)
632 for ( ; start != end; ++start)
633 redoParagraph(start);
637 void LyXText::redoParagraph(ParagraphList::iterator pit)
639 RowList::iterator rit = firstRow(pit);
641 // remove paragraph from rowlist
642 while (rit != rows().end() && rit->par() == pit) {
643 RowList::iterator rit2 = rit++;
647 // reinsert the paragraph
648 insertParagraph(pit, rit);
649 setHeightOfRow(rows().begin());
653 void LyXText::fullRebreak()
655 lyxerr << "fullRebreak\n";
656 redoParagraphs(ownerParagraphs().begin(), ownerParagraphs().end());
657 setCursorIntern(cursor.par(), cursor.pos());
658 selection.cursor = cursor;
662 void LyXText::metrics(MetricsInfo & mi, Dimension & dim)
664 lyxerr << "LyXText::metrics: width: " << mi.base.textwidth << "\n";
665 //Assert(mi.base.textwidth);
671 anchor_row_ = rows().end();
672 anchor_row_offset_ = 0;
674 ParagraphList::iterator pit = ownerParagraphs().begin();
675 ParagraphList::iterator end = ownerParagraphs().end();
677 for (; pit != end; ++pit) {
678 InsetList::iterator ii = pit->insetlist.begin();
679 InsetList::iterator iend = pit->insetlist.end();
680 for (; ii != iend; ++ii) {
683 ii->inset->metrics(m, dim);
686 // insert a new row, starting at position 0
688 RowList::iterator rit = rowlist_.insert(rowlist_.end(), newrow);
690 // and now append the whole paragraph before the new row
691 appendParagraph(rit);
695 //lyxerr << "height 0: " << height << "\n";
696 //for (RowList::iterator rit = rows().begin(); rit != rows().end(); ++rit) {
697 // height += rit->height();
699 //lyxerr << "height 1: " << height << "\n";
702 dim.asc = rows().begin()->ascent_of_text();
703 dim.des = height - dim.asc;
704 dim.wid = std::max(mi.base.textwidth, int(width));
708 void LyXText::partialRebreak()
710 if (rows().empty()) {
714 breakAgain(rows().begin());
718 // important for the screen
721 // the cursor set functions have a special mechanism. When they
722 // realize, that you left an empty paragraph, they will delete it.
723 // They also delete the corresponding row
725 // need the selection cursor:
726 void LyXText::setSelection()
728 bool const lsel = TextCursor::setSelection();
730 if (inset_owner && (selection.set() || lsel))
731 inset_owner->setUpdateStatus(InsetText::SELECTION);
736 void LyXText::clearSelection()
738 TextCursor::clearSelection();
740 // reset this in the bv_owner!
741 if (bv_owner && bv_owner->text)
742 bv_owner->text->xsel_cache.set(false);
746 void LyXText::cursorHome()
748 setCursor(cursor.par(), cursorRow()->pos());
752 void LyXText::cursorEnd()
754 if (cursor.par()->empty())
757 RowList::iterator rit = cursorRow();
758 RowList::iterator next_rit = boost::next(rit);
759 ParagraphList::iterator pit = rit->par();
760 pos_type last_pos = lastPos(*this, rit);
762 if (next_rit == rows().end() || next_rit->par() != pit) {
766 (pit->getChar(last_pos) != ' ' && !pit->isNewline(last_pos))) {
771 setCursor(pit, last_pos);
775 void LyXText::cursorTop()
777 setCursor(ownerParagraphs().begin(), 0);
781 void LyXText::cursorBottom()
783 ParagraphList::iterator lastpit =
784 boost::prior(ownerParagraphs().end());
785 setCursor(lastpit, lastpit->size());
789 void LyXText::toggleFree(LyXFont const & font, bool toggleall)
791 // If the mask is completely neutral, tell user
792 if (font == LyXFont(LyXFont::ALL_IGNORE)) {
793 // Could only happen with user style
794 bv()->owner()->message(_("No font change defined. Use Character under the Layout menu to define font change."));
798 // Try implicit word selection
799 // If there is a change in the language the implicit word selection
801 LyXCursor resetCursor = cursor;
802 bool implicitSelection = (font.language() == ignore_language
803 && font.number() == LyXFont::IGNORE)
804 ? selectWordWhenUnderCursor(lyx::WHOLE_WORD_STRICT) : false;
807 setFont(font, toggleall);
809 // Implicit selections are cleared afterwards
810 //and cursor is set to the original position.
811 if (implicitSelection) {
813 cursor = resetCursor;
814 setCursor(cursor.par(), cursor.pos());
815 selection.cursor = cursor;
818 inset_owner->setUpdateStatus(InsetText::CURSOR_PAR);
822 string LyXText::getStringToIndex()
824 // Try implicit word selection
825 // If there is a change in the language the implicit word selection
827 LyXCursor const reset_cursor = cursor;
828 bool const implicitSelection =
829 selectWordWhenUnderCursor(lyx::PREVIOUS_WORD);
832 if (!selection.set())
833 bv()->owner()->message(_("Nothing to index!"));
834 else if (selection.start.par() != selection.end.par())
835 bv()->owner()->message(_("Cannot index more than one paragraph!"));
837 idxstring = selectionAsString(bv()->buffer(), false);
839 // Reset cursors to their original position.
840 cursor = reset_cursor;
841 setCursor(cursor.par(), cursor.pos());
842 selection.cursor = cursor;
844 // Clear the implicit selection.
845 if (implicitSelection)
852 // the DTP switches for paragraphs. LyX will store them in the first
853 // physicla paragraph. When a paragraph is broken, the top settings rest,
854 // the bottom settings are given to the new one. So I can make shure,
855 // they do not duplicate themself and you cannnot make dirty things with
858 void LyXText::setParagraph(bool line_top, bool line_bottom,
859 bool pagebreak_top, bool pagebreak_bottom,
860 VSpace const & space_top,
861 VSpace const & space_bottom,
862 Spacing const & spacing,
864 string const & labelwidthstring,
867 LyXCursor tmpcursor = cursor;
868 if (!selection.set()) {
869 selection.start = cursor;
870 selection.end = cursor;
873 // make sure that the depth behind the selection are restored, too
874 ParagraphList::iterator endpit = boost::next(selection.end.par());
875 ParagraphList::iterator undoendpit = endpit;
876 ParagraphList::iterator pars_end = ownerParagraphs().end();
878 if (endpit != pars_end && endpit->getDepth()) {
879 while (endpit != pars_end && endpit->getDepth()) {
883 } else if (endpit != pars_end) {
884 // because of parindents etc.
888 recordUndo(bv(), Undo::ATOMIC, selection.start.par(),
889 boost::prior(undoendpit));
892 ParagraphList::iterator tmppit = selection.end.par();
894 while (tmppit != boost::prior(selection.start.par())) {
895 setCursor(tmppit, 0);
897 ParagraphList::iterator pit = cursor.par();
898 ParagraphParameters & params = pit->params();
900 params.lineTop(line_top);
901 params.lineBottom(line_bottom);
902 params.pagebreakTop(pagebreak_top);
903 params.pagebreakBottom(pagebreak_bottom);
904 params.spaceTop(space_top);
905 params.spaceBottom(space_bottom);
906 params.spacing(spacing);
907 // does the layout allow the new alignment?
908 LyXLayout_ptr const & layout = pit->layout();
910 if (align == LYX_ALIGN_LAYOUT)
911 align = layout->align;
912 if (align & layout->alignpossible) {
913 if (align == layout->align)
914 params.align(LYX_ALIGN_LAYOUT);
918 pit->setLabelWidthString(labelwidthstring);
919 params.noindent(noindent);
920 tmppit = boost::prior(pit);
924 redoParagraphs(selection.start.par(), endpit);
927 setCursor(selection.start.par(), selection.start.pos());
928 selection.cursor = cursor;
929 setCursor(selection.end.par(), selection.end.pos());
931 setCursor(tmpcursor.par(), tmpcursor.pos());
933 bv()->updateInset(inset_owner);
937 // set the counter of a paragraph. This includes the labels
938 void LyXText::setCounter(Buffer const * buf, ParagraphList::iterator pit)
940 LyXTextClass const & textclass = buf->params.getLyXTextClass();
941 LyXLayout_ptr const & layout = pit->layout();
943 if (pit != ownerParagraphs().begin()) {
945 pit->params().appendix(boost::prior(pit)->params().appendix());
946 if (!pit->params().appendix() &&
947 pit->params().startOfAppendix()) {
948 pit->params().appendix(true);
949 textclass.counters().reset();
951 pit->enumdepth = boost::prior(pit)->enumdepth;
952 pit->itemdepth = boost::prior(pit)->itemdepth;
954 pit->params().appendix(pit->params().startOfAppendix());
959 /* Maybe we have to increment the enumeration depth.
960 * BUT, enumeration in a footnote is considered in isolation from its
961 * surrounding paragraph so don't increment if this is the
962 * first line of the footnote
963 * AND, bibliographies can't have their depth changed ie. they
964 * are always of depth 0
966 if (pit != ownerParagraphs().begin()
967 && boost::prior(pit)->getDepth() < pit->getDepth()
968 && boost::prior(pit)->layout()->labeltype == LABEL_COUNTER_ENUMI
969 && pit->enumdepth < 3
970 && layout->labeltype != LABEL_BIBLIO) {
974 // Maybe we have to decrement the enumeration depth, see note above
975 if (pit != ownerParagraphs().begin()
976 && boost::prior(pit)->getDepth() > pit->getDepth()
977 && layout->labeltype != LABEL_BIBLIO) {
978 pit->enumdepth = depthHook(pit, ownerParagraphs(),
979 pit->getDepth())->enumdepth;
982 if (!pit->params().labelString().empty()) {
983 pit->params().labelString(string());
986 if (layout->margintype == MARGIN_MANUAL) {
987 if (pit->params().labelWidthString().empty()) {
988 pit->setLabelWidthString(layout->labelstring());
991 pit->setLabelWidthString(string());
994 // is it a layout that has an automatic label?
995 if (layout->labeltype >= LABEL_COUNTER_CHAPTER) {
996 int const i = layout->labeltype - LABEL_COUNTER_CHAPTER;
1000 if (i >= 0 && i <= buf->params.secnumdepth) {
1004 textclass.counters().step(layout->latexname());
1006 // Is there a label? Useful for Chapter layout
1007 if (!pit->params().appendix()) {
1008 s << buf->B_(layout->labelstring());
1010 s << buf->B_(layout->labelstring_appendix());
1013 // Use of an integer is here less than elegant. For now.
1014 int head = textclass.maxcounter() - LABEL_COUNTER_CHAPTER;
1015 if (!pit->params().appendix()) {
1016 numbertype = "sectioning";
1018 numbertype = "appendix";
1019 if (pit->isRightToLeftPar(buf->params))
1020 langtype = "hebrew";
1026 << textclass.counters()
1027 .numberLabel(layout->latexname(),
1028 numbertype, langtype, head);
1030 pit->params().labelString(STRCONV(s.str()));
1032 // reset enum counters
1033 textclass.counters().reset("enum");
1034 } else if (layout->labeltype < LABEL_COUNTER_ENUMI) {
1035 textclass.counters().reset("enum");
1036 } else if (layout->labeltype == LABEL_COUNTER_ENUMI) {
1038 // Yes I know this is a really, really! bad solution
1040 string enumcounter("enum");
1042 switch (pit->enumdepth) {
1051 enumcounter += "iv";
1054 // not a valid enumdepth...
1058 textclass.counters().step(enumcounter);
1060 s << textclass.counters()
1061 .numberLabel(enumcounter, "enumeration");
1062 pit->params().labelString(STRCONV(s.str()));
1064 } else if (layout->labeltype == LABEL_BIBLIO) {// ale970302
1065 textclass.counters().step("bibitem");
1066 int number = textclass.counters().value("bibitem");
1067 if (pit->bibitem()) {
1068 pit->bibitem()->setCounter(number);
1069 pit->params().labelString(layout->labelstring());
1071 // In biblio should't be following counters but...
1073 string s = buf->B_(layout->labelstring());
1075 // the caption hack:
1076 if (layout->labeltype == LABEL_SENSITIVE) {
1077 ParagraphList::iterator end = ownerParagraphs().end();
1078 ParagraphList::iterator tmppit = pit;
1081 while (tmppit != end && tmppit->inInset()
1082 // the single '=' is intended below
1083 && (in = tmppit->inInset()->owner()))
1085 if (in->lyxCode() == InsetOld::FLOAT_CODE ||
1086 in->lyxCode() == InsetOld::WRAP_CODE) {
1090 tmppit = ownerParagraphs().begin();
1091 for ( ; tmppit != end; ++tmppit)
1092 if (&*tmppit == in->parOwner())
1100 if (in->lyxCode() == InsetOld::FLOAT_CODE)
1101 type = static_cast<InsetFloat*>(in)->params().type;
1102 else if (in->lyxCode() == InsetOld::WRAP_CODE)
1103 type = static_cast<InsetWrap*>(in)->params().type;
1107 Floating const & fl = textclass.floats().getType(type);
1109 textclass.counters().step(fl.type());
1111 // Doesn't work... yet.
1112 s = bformat(_("%1$s #:"), buf->B_(fl.name()));
1114 // par->SetLayout(0);
1115 // s = layout->labelstring;
1116 s = _("Senseless: ");
1119 pit->params().labelString(s);
1121 // reset the enumeration counter. They are always reset
1122 // when there is any other layout between
1123 // Just fall-through between the cases so that all
1124 // enum counters deeper than enumdepth is also reset.
1125 switch (pit->enumdepth) {
1127 textclass.counters().reset("enumi");
1129 textclass.counters().reset("enumii");
1131 textclass.counters().reset("enumiii");
1133 textclass.counters().reset("enumiv");
1139 // Updates all counters. Paragraphs with changed label string will be rebroken
1140 void LyXText::updateCounters()
1142 RowList::iterator rowit = rows().begin();
1143 ParagraphList::iterator pit = rowit->par();
1145 // CHECK if this is really needed. (Lgb)
1146 bv()->buffer()->params.getLyXTextClass().counters().reset();
1148 ParagraphList::iterator beg = ownerParagraphs().begin();
1149 ParagraphList::iterator end = ownerParagraphs().end();
1150 for (; pit != end; ++pit) {
1151 while (rowit->par() != pit)
1154 string const oldLabel = pit->params().labelString();
1156 size_t maxdepth = 0;
1158 maxdepth = boost::prior(pit)->getMaxDepthAfter();
1160 if (pit->params().depth() > maxdepth)
1161 pit->params().depth(maxdepth);
1163 // setCounter can potentially change the labelString.
1164 setCounter(bv()->buffer(), pit);
1166 string const & newLabel = pit->params().labelString();
1168 if (oldLabel.empty() && !newLabel.empty()) {
1169 removeParagraph(rowit);
1170 appendParagraph(rowit);
1176 void LyXText::insertInset(InsetOld * inset)
1178 if (!cursor.par()->insetAllowed(inset->lyxCode()))
1180 recordUndo(bv(), Undo::ATOMIC, cursor.par());
1182 cursor.par()->insertInset(cursor.pos(), inset);
1183 // Just to rebreak and refresh correctly.
1184 // The character will not be inserted a second time
1185 insertChar(Paragraph::META_INSET);
1186 // If we enter a highly editable inset the cursor should be to before
1187 // the inset. This couldn't happen before as Undo was not handled inside
1188 // inset now after the Undo LyX tries to call inset->Edit(...) again
1189 // and cannot do this as the cursor is behind the inset and GetInset
1190 // does not return the inset!
1191 if (isHighlyEditableInset(inset)) {
1198 void LyXText::cutSelection(bool doclear, bool realcut)
1200 // Stuff what we got on the clipboard. Even if there is no selection.
1202 // There is a problem with having the stuffing here in that the
1203 // larger the selection the slower LyX will get. This can be
1204 // solved by running the line below only when the selection has
1205 // finished. The solution used currently just works, to make it
1206 // faster we need to be more clever and probably also have more
1207 // calls to stuffClipboard. (Lgb)
1208 bv()->stuffClipboard(selectionAsString(bv()->buffer(), true));
1210 // This doesn't make sense, if there is no selection
1211 if (!selection.set())
1214 // OK, we have a selection. This is always between selection.start
1215 // and selection.end
1217 // make sure that the depth behind the selection are restored, too
1218 ParagraphList::iterator endpit = boost::next(selection.end.par());
1219 ParagraphList::iterator undoendpit = endpit;
1220 ParagraphList::iterator pars_end = ownerParagraphs().end();
1222 if (endpit != pars_end && endpit->getDepth()) {
1223 while (endpit != pars_end && endpit->getDepth()) {
1225 undoendpit = endpit;
1227 } else if (endpit != pars_end) {
1228 // because of parindents etc.
1232 recordUndo(bv(), Undo::DELETE, selection.start.par(),
1233 boost::prior(undoendpit));
1236 endpit = selection.end.par();
1237 int endpos = selection.end.pos();
1239 boost::tie(endpit, endpos) = realcut ?
1240 CutAndPaste::cutSelection(bv()->buffer()->params,
1242 selection.start.par(), endpit,
1243 selection.start.pos(), endpos,
1244 bv()->buffer()->params.textclass,
1246 : CutAndPaste::eraseSelection(bv()->buffer()->params,
1248 selection.start.par(), endpit,
1249 selection.start.pos(), endpos,
1251 // sometimes necessary
1253 selection.start.par()->stripLeadingSpaces();
1255 redoParagraphs(selection.start.par(), boost::next(endpit));
1256 #warning FIXME latent bug
1257 // endpit will be invalidated on redoParagraphs once ParagraphList
1258 // becomes a std::list? There are maybe other places on which this
1259 // can happend? (Ab)
1260 // cutSelection can invalidate the cursor so we need to set
1262 // we prefer the end for when tracking changes
1266 // need a valid cursor. (Lgb)
1269 setCursor(cursor.par(), cursor.pos());
1270 selection.cursor = cursor;
1275 void LyXText::copySelection()
1277 // stuff the selection onto the X clipboard, from an explicit copy request
1278 bv()->stuffClipboard(selectionAsString(bv()->buffer(), true));
1280 // this doesnt make sense, if there is no selection
1281 if (!selection.set())
1284 // ok we have a selection. This is always between selection.start
1285 // and sel_end cursor
1287 // copy behind a space if there is one
1288 while (selection.start.par()->size() > selection.start.pos()
1289 && selection.start.par()->isLineSeparator(selection.start.pos())
1290 && (selection.start.par() != selection.end.par()
1291 || selection.start.pos() < selection.end.pos()))
1292 selection.start.pos(selection.start.pos() + 1);
1294 CutAndPaste::copySelection(selection.start.par(),
1295 selection.end.par(),
1296 selection.start.pos(), selection.end.pos(),
1297 bv()->buffer()->params.textclass);
1301 void LyXText::pasteSelection(size_t sel_index)
1303 // this does not make sense, if there is nothing to paste
1304 if (!CutAndPaste::checkPastePossible())
1307 recordUndo(bv(), Undo::INSERT, cursor.par());
1309 ParagraphList::iterator endpit;
1314 boost::tie(ppp, endpit) =
1315 CutAndPaste::pasteSelection(*bv()->buffer(),
1317 cursor.par(), cursor.pos(),
1318 bv()->buffer()->params.textclass,
1320 bufferErrors(*bv()->buffer(), el);
1321 bv()->showErrorList(_("Paste"));
1323 redoParagraphs(cursor.par(), endpit);
1325 setCursor(cursor.par(), cursor.pos());
1328 selection.cursor = cursor;
1329 setCursor(ppp.first, ppp.second);
1335 void LyXText::setSelectionRange(lyx::pos_type length)
1340 selection.cursor = cursor;
1347 // simple replacing. The font of the first selected character is used
1348 void LyXText::replaceSelectionWithString(string const & str)
1350 recordUndo(bv(), Undo::ATOMIC);
1353 if (!selection.set()) { // create a dummy selection
1354 selection.end = cursor;
1355 selection.start = cursor;
1358 // Get font setting before we cut
1359 pos_type pos = selection.end.pos();
1360 LyXFont const font = selection.start.par()
1361 ->getFontSettings(bv()->buffer()->params,
1362 selection.start.pos());
1364 // Insert the new string
1365 string::const_iterator cit = str.begin();
1366 string::const_iterator end = str.end();
1367 for (; cit != end; ++cit) {
1368 selection.end.par()->insertChar(pos, (*cit), font);
1372 // Cut the selection
1373 cutSelection(true, false);
1379 // needed to insert the selection
1380 void LyXText::insertStringAsLines(string const & str)
1382 ParagraphList::iterator pit = cursor.par();
1383 pos_type pos = cursor.pos();
1384 ParagraphList::iterator endpit = boost::next(cursor.par());
1386 recordUndo(bv(), Undo::ATOMIC);
1388 // only to be sure, should not be neccessary
1391 bv()->buffer()->insertStringAsLines(pit, pos, current_font, str);
1393 redoParagraphs(cursor.par(), endpit);
1394 setCursor(cursor.par(), cursor.pos());
1395 selection.cursor = cursor;
1396 setCursor(pit, pos);
1401 // turns double-CR to single CR, others where converted into one
1402 // blank. Then InsertStringAsLines is called
1403 void LyXText::insertStringAsParagraphs(string const & str)
1405 string linestr(str);
1406 bool newline_inserted = false;
1407 string::size_type const siz = linestr.length();
1409 for (string::size_type i = 0; i < siz; ++i) {
1410 if (linestr[i] == '\n') {
1411 if (newline_inserted) {
1412 // we know that \r will be ignored by
1413 // InsertStringA. Of course, it is a dirty
1414 // trick, but it works...
1415 linestr[i - 1] = '\r';
1419 newline_inserted = true;
1421 } else if (IsPrintable(linestr[i])) {
1422 newline_inserted = false;
1425 insertStringAsLines(linestr);
1429 void LyXText::checkParagraph(ParagraphList::iterator pit, pos_type pos)
1431 breakAgain(getRow(pit, pos));
1433 setCursorIntern(cursor.par(), cursor.pos(), false, cursor.boundary());
1437 // returns false if inset wasn't found
1438 bool LyXText::updateInset(InsetOld * inset)
1440 // first check the current paragraph
1441 int pos = cursor.par()->getPositionOfInset(inset);
1443 checkParagraph(cursor.par(), pos);
1447 // check every paragraph
1448 ParagraphList::iterator par = ownerParagraphs().begin();
1449 ParagraphList::iterator end = ownerParagraphs().end();
1450 for (; par != end; ++par) {
1451 pos = par->getPositionOfInset(inset);
1453 checkParagraph(par, pos);
1462 bool LyXText::setCursor(ParagraphList::iterator pit,
1464 bool setfont, bool boundary)
1466 LyXCursor old_cursor = cursor;
1467 setCursorIntern(pit, pos, setfont, boundary);
1468 return deleteEmptyParagraphMechanism(old_cursor);
1472 void LyXText::setCursor(LyXCursor & cur, ParagraphList::iterator pit,
1473 pos_type pos, bool boundary)
1475 Assert(pit != ownerParagraphs().end());
1479 cur.boundary(boundary);
1483 // get the cursor y position in text
1485 RowList::iterator row = getRow(pit, pos, y);
1486 RowList::iterator beg = rows().begin();
1488 RowList::iterator old_row = row;
1489 // if we are before the first char of this row and are still in the
1490 // same paragraph and there is a previous row then put the cursor on
1491 // the end of the previous row
1492 cur.iy(y + row->baseline());
1495 boost::prior(row)->par() == row->par() &&
1496 pos < pit->size() &&
1497 pit->getChar(pos) == Paragraph::META_INSET) {
1498 InsetOld * ins = pit->getInset(pos);
1499 if (ins && (ins->needFullRow() || ins->display())) {
1505 // y is now the beginning of the cursor row
1506 y += row->baseline();
1507 // y is now the cursor baseline
1510 pos_type last = lastPrintablePos(*this, old_row);
1512 // None of these should happen, but we're scaredy-cats
1513 if (pos > pit->size()) {
1514 lyxerr << "dont like 1 please report" << endl;
1517 } else if (pos > last + 1) {
1518 lyxerr << "dont like 2 please report" << endl;
1519 // This shouldn't happen.
1522 } else if (pos < row->pos()) {
1523 lyxerr << "dont like 3 please report" << endl;
1528 // now get the cursors x position
1529 float x = getCursorX(row, pos, last, boundary);
1532 if (old_row != row) {
1533 x = getCursorX(old_row, pos, last, boundary);
1537 /* We take out this for the time being because 1) the redraw code is not
1538 prepared to this yet and 2) because some good policy has yet to be decided
1539 while editting: for instance how to act on rows being created/deleted
1543 //if the cursor is in a visible row, anchor to it
1545 if (topy < y && y < topy + bv()->workHeight())
1551 float LyXText::getCursorX(RowList::iterator rit,
1552 pos_type pos, pos_type last, bool boundary) const
1554 pos_type cursor_vpos = 0;
1556 double fill_separator;
1558 double fill_label_hfill;
1559 // This call HAS to be here because of the BidiTables!!!
1560 prepareToPrint(rit, x, fill_separator, fill_hfill,
1563 ParagraphList::iterator rit_par = rit->par();
1564 pos_type const rit_pos = rit->pos();
1567 cursor_vpos = rit_pos;
1568 else if (pos > last && !boundary)
1569 cursor_vpos = (rit_par->isRightToLeftPar(bv()->buffer()->params))
1570 ? rit_pos : last + 1;
1571 else if (pos > rit_pos && (pos > last || boundary))
1572 /// Place cursor after char at (logical) position pos - 1
1573 cursor_vpos = (bidi_level(pos - 1) % 2 == 0)
1574 ? log2vis(pos - 1) + 1 : log2vis(pos - 1);
1576 /// Place cursor before char at (logical) position pos
1577 cursor_vpos = (bidi_level(pos) % 2 == 0)
1578 ? log2vis(pos) : log2vis(pos) + 1;
1580 pos_type body_pos = rit_par->beginningOfBody();
1582 (body_pos - 1 > last || !rit_par->isLineSeparator(body_pos - 1)))
1585 for (pos_type vpos = rit_pos; vpos < cursor_vpos; ++vpos) {
1586 pos_type pos = vis2log(vpos);
1587 if (body_pos > 0 && pos == body_pos - 1) {
1588 x += fill_label_hfill +
1589 font_metrics::width(
1590 rit_par->layout()->labelsep, getLabelFont(rit_par));
1591 if (rit_par->isLineSeparator(body_pos - 1))
1592 x -= singleWidth(rit_par, body_pos - 1);
1595 if (hfillExpansion(*this, rit, pos)) {
1596 x += singleWidth(rit_par, pos);
1597 if (pos >= body_pos)
1600 x += fill_label_hfill;
1601 } else if (rit_par->isSeparator(pos)) {
1602 x += singleWidth(rit_par, pos);
1603 if (pos >= body_pos)
1604 x += fill_separator;
1606 x += singleWidth(rit_par, pos);
1612 void LyXText::setCursorIntern(ParagraphList::iterator pit,
1613 pos_type pos, bool setfont, bool boundary)
1615 UpdatableInset * it = pit->inInset();
1617 if (it != inset_owner) {
1618 lyxerr[Debug::INSETS] << "InsetText is " << it
1620 << "inset_owner is "
1621 << inset_owner << endl;
1622 #ifdef WITH_WARNINGS
1623 #warning I believe this code is wrong. (Lgb)
1624 #warning Jürgen, have a look at this. (Lgb)
1625 #warning Hmmm, I guess you are right but we
1626 #warning should verify when this is needed
1628 // Jürgen, would you like to have a look?
1629 // I guess we need to move the outer cursor
1630 // and open and lock the inset (bla bla bla)
1631 // stuff I don't know... so can you have a look?
1633 // I moved the lyxerr stuff in here so we can see if
1634 // this is actually really needed and where!
1636 // it->getLyXText(bv())->setCursorIntern(bv(), par, pos, setfont, boundary);
1641 setCursor(cursor, pit, pos, boundary);
1647 void LyXText::setCurrentFont()
1649 pos_type pos = cursor.pos();
1650 ParagraphList::iterator pit = cursor.par();
1652 if (cursor.boundary() && pos > 0)
1656 if (pos == pit->size())
1658 else // potentional bug... BUG (Lgb)
1659 if (pit->isSeparator(pos)) {
1660 if (pos > cursorRow()->pos() &&
1661 bidi_level(pos) % 2 ==
1662 bidi_level(pos - 1) % 2)
1664 else if (pos + 1 < pit->size())
1669 current_font = pit->getFontSettings(bv()->buffer()->params, pos);
1670 real_current_font = getFont(pit, pos);
1672 if (cursor.pos() == pit->size() &&
1673 isBoundary(bv()->buffer(), *pit, cursor.pos()) &&
1674 !cursor.boundary()) {
1675 Language const * lang =
1676 pit->getParLanguage(bv()->buffer()->params);
1677 current_font.setLanguage(lang);
1678 current_font.setNumber(LyXFont::OFF);
1679 real_current_font.setLanguage(lang);
1680 real_current_font.setNumber(LyXFont::OFF);
1685 // returns the column near the specified x-coordinate of the row
1686 // x is set to the real beginning of this column
1688 LyXText::getColumnNearX(RowList::iterator rit, int & x, bool & boundary) const
1691 double fill_separator;
1693 double fill_label_hfill;
1695 prepareToPrint(rit, tmpx, fill_separator, fill_hfill, fill_label_hfill);
1697 pos_type vc = rit->pos();
1698 pos_type last = lastPrintablePos(*this, rit);
1701 ParagraphList::iterator rit_par = rit->par();
1702 LyXLayout_ptr const & layout = rit->par()->layout();
1704 bool left_side = false;
1706 pos_type body_pos = rit_par->beginningOfBody();
1707 double last_tmpx = tmpx;
1710 (body_pos - 1 > last ||
1711 !rit_par->isLineSeparator(body_pos - 1)))
1714 // check for empty row
1715 if (!rit_par->size()) {
1720 while (vc <= last && tmpx <= x) {
1723 if (body_pos > 0 && c == body_pos - 1) {
1724 tmpx += fill_label_hfill +
1725 font_metrics::width(layout->labelsep, getLabelFont(rit_par));
1726 if (rit_par->isLineSeparator(body_pos - 1))
1727 tmpx -= singleWidth(rit_par, body_pos - 1);
1730 if (hfillExpansion(*this, rit, c)) {
1731 tmpx += singleWidth(rit_par, c);
1735 tmpx += fill_label_hfill;
1736 } else if (rit_par->isSeparator(c)) {
1737 tmpx += singleWidth(rit_par, c);
1739 tmpx += fill_separator;
1741 tmpx += singleWidth(rit_par, c);
1746 if ((tmpx + last_tmpx) / 2 > x) {
1751 if (vc > last + 1) // This shouldn't happen.
1755 // This (rtl_support test) is not needed, but gives
1756 // some speedup if rtl_support=false
1757 RowList::iterator next_rit = boost::next(rit);
1759 bool const lastrow = lyxrc.rtl_support &&
1760 (next_rit == rowlist_.end() ||
1761 next_rit->par() != rit_par);
1763 // If lastrow is false, we don't need to compute
1764 // the value of rtl.
1765 bool const rtl = (lastrow)
1766 ? rit_par->isRightToLeftPar(bv()->buffer()->params)
1769 ((rtl && left_side && vc == rit->pos() && x < tmpx - 5) ||
1770 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5)))
1772 else if (vc == rit->pos()) {
1774 if (bidi_level(c) % 2 == 1)
1777 c = vis2log(vc - 1);
1778 bool const rtl = (bidi_level(c) % 2 == 1);
1779 if (left_side == rtl) {
1781 boundary = isBoundary(bv()->buffer(), *rit_par, c);
1785 if (rit->pos() <= last && c > last
1786 && rit_par->isNewline(last)) {
1787 if (bidi_level(last) % 2 == 0)
1788 tmpx -= singleWidth(rit_par, last);
1790 tmpx += singleWidth(rit_par, last);
1800 void LyXText::setCursorFromCoordinates(int x, int y)
1802 //LyXCursor old_cursor = cursor;
1803 setCursorFromCoordinates(cursor, x, y);
1805 #warning DEPM disabled, otherwise crash when entering new table
1806 //deleteEmptyParagraphMechanism(old_cursor);
1813 * return true if the cursor given is at the end of a row,
1814 * and the next row is filled by an inset that spans an entire
1817 bool beforeFullRowInset(LyXText & lt, LyXCursor const & cur)
1819 RowList::iterator row = lt.getRow(cur);
1820 if (boost::next(row) == lt.rows().end())
1823 Row const & next = *boost::next(row);
1825 if (next.pos() != cur.pos() || next.par() != cur.par())
1828 if (cur.pos() == cur.par()->size()
1829 || !cur.par()->isInset(cur.pos()))
1832 InsetOld const * inset = cur.par()->getInset(cur.pos());
1833 if (inset->needFullRow() || inset->display())
1841 void LyXText::setCursorFromCoordinates(LyXCursor & cur, int x, int y)
1843 // Get the row first.
1845 RowList::iterator row = getRowNearY(y);
1847 pos_type const column = getColumnNearX(row, x, bound);
1848 cur.par(row->par());
1849 cur.pos(row->pos() + column);
1851 cur.y(y + row->baseline());
1853 if (beforeFullRowInset(*this, cur)) {
1854 pos_type const last = lastPrintablePos(*this, row);
1855 RowList::iterator next_row = boost::next(row);
1857 float x = getCursorX(next_row, cur.pos(), last, bound);
1859 cur.iy(y + row->height() + next_row->baseline());
1864 cur.boundary(bound);
1868 void LyXText::cursorLeft(bool internal)
1870 if (cursor.pos() > 0) {
1871 bool boundary = cursor.boundary();
1872 setCursor(cursor.par(), cursor.pos() - 1, true, false);
1873 if (!internal && !boundary &&
1874 isBoundary(bv()->buffer(), *cursor.par(), cursor.pos() + 1))
1875 setCursor(cursor.par(), cursor.pos() + 1, true, true);
1876 } else if (cursor.par() != ownerParagraphs().begin()) { // steps into the above paragraph.
1877 ParagraphList::iterator pit = boost::prior(cursor.par());
1878 setCursor(pit, pit->size());
1883 void LyXText::cursorRight(bool internal)
1885 bool const at_end = (cursor.pos() == cursor.par()->size());
1886 bool const at_newline = !at_end &&
1887 cursor.par()->isNewline(cursor.pos());
1889 if (!internal && cursor.boundary() && !at_newline)
1890 setCursor(cursor.par(), cursor.pos(), true, false);
1892 setCursor(cursor.par(), cursor.pos() + 1, true, false);
1894 isBoundary(bv()->buffer(), *cursor.par(), cursor.pos()))
1895 setCursor(cursor.par(), cursor.pos(), true, true);
1896 } else if (boost::next(cursor.par()) != ownerParagraphs().end())
1897 setCursor(boost::next(cursor.par()), 0);
1901 void LyXText::cursorUp(bool selecting)
1904 int x = cursor.x_fix();
1905 int y = cursor.y() - cursorRow()->baseline() - 1;
1906 setCursorFromCoordinates(x, y);
1909 int y1 = cursor.iy() - topy;
1912 InsetOld * inset_hit = checkInsetHit(x, y1);
1913 if (inset_hit && isHighlyEditableInset(inset_hit)) {
1914 inset_hit->localDispatch(
1915 FuncRequest(bv(), LFUN_INSET_EDIT, x, y - (y2 - y1), mouse_button::none));
1919 setCursorFromCoordinates(bv(), cursor.x_fix(),
1920 cursor.y() - cursorRow()->baseline() - 1);
1925 void LyXText::cursorDown(bool selecting)
1928 int x = cursor.x_fix();
1929 int y = cursor.y() - cursorRow()->baseline() + cursorRow()->height() + 1;
1930 setCursorFromCoordinates(x, y);
1931 if (!selecting && cursorRow() == cursorIRow()) {
1933 int y1 = cursor.iy() - topy;
1936 InsetOld * inset_hit = checkInsetHit(x, y1);
1937 if (inset_hit && isHighlyEditableInset(inset_hit)) {
1938 FuncRequest cmd(bv(), LFUN_INSET_EDIT, x, y - (y2 - y1), mouse_button::none);
1939 inset_hit->localDispatch(cmd);
1943 setCursorFromCoordinates(bv(), cursor.x_fix(),
1944 cursor.y() - cursorRow()->baseline()
1945 + cursorRow()->height() + 1);
1950 void LyXText::cursorUpParagraph()
1952 if (cursor.pos() > 0) {
1953 setCursor(cursor.par(), 0);
1955 else if (cursor.par() != ownerParagraphs().begin()) {
1956 setCursor(boost::prior(cursor.par()), 0);
1961 void LyXText::cursorDownParagraph()
1963 ParagraphList::iterator par = cursor.par();
1964 ParagraphList::iterator next_par = boost::next(par);
1966 if (next_par != ownerParagraphs().end()) {
1967 setCursor(next_par, 0);
1969 setCursor(par, par->size());
1973 // fix the cursor `cur' after a characters has been deleted at `where'
1974 // position. Called by deleteEmptyParagraphMechanism
1975 void LyXText::fixCursorAfterDelete(LyXCursor & cur,
1976 LyXCursor const & where)
1978 // if cursor is not in the paragraph where the delete occured,
1980 if (cur.par() != where.par())
1983 // if cursor position is after the place where the delete occured,
1985 if (cur.pos() > where.pos())
1986 cur.pos(cur.pos()-1);
1988 // check also if we don't want to set the cursor on a spot behind the
1989 // pagragraph because we erased the last character.
1990 if (cur.pos() > cur.par()->size())
1991 cur.pos(cur.par()->size());
1993 // recompute row et al. for this cursor
1994 setCursor(cur, cur.par(), cur.pos(), cur.boundary());
1998 bool LyXText::deleteEmptyParagraphMechanism(LyXCursor const & old_cursor)
2000 // Would be wrong to delete anything if we have a selection.
2001 if (selection.set())
2004 // We allow all kinds of "mumbo-jumbo" when freespacing.
2005 if (old_cursor.par()->layout()->free_spacing
2006 || old_cursor.par()->isFreeSpacing()) {
2010 /* Ok I'll put some comments here about what is missing.
2011 I have fixed BackSpace (and thus Delete) to not delete
2012 double-spaces automagically. I have also changed Cut,
2013 Copy and Paste to hopefully do some sensible things.
2014 There are still some small problems that can lead to
2015 double spaces stored in the document file or space at
2016 the beginning of paragraphs. This happens if you have
2017 the cursor betwenn to spaces and then save. Or if you
2018 cut and paste and the selection have a space at the
2019 beginning and then save right after the paste. I am
2020 sure none of these are very hard to fix, but I will
2021 put out 1.1.4pre2 with FIX_DOUBLE_SPACE defined so
2022 that I can get some feedback. (Lgb)
2025 // If old_cursor.pos() == 0 and old_cursor.pos()(1) == LineSeparator
2026 // delete the LineSeparator.
2029 // If old_cursor.pos() == 1 and old_cursor.pos()(0) == LineSeparator
2030 // delete the LineSeparator.
2033 // If the pos around the old_cursor were spaces, delete one of them.
2034 if (old_cursor.par() != cursor.par()
2035 || old_cursor.pos() != cursor.pos()) {
2036 // Only if the cursor has really moved
2038 if (old_cursor.pos() > 0
2039 && old_cursor.pos() < old_cursor.par()->size()
2040 && old_cursor.par()->isLineSeparator(old_cursor.pos())
2041 && old_cursor.par()->isLineSeparator(old_cursor.pos() - 1)) {
2042 old_cursor.par()->erase(old_cursor.pos() - 1);
2043 redoParagraph(old_cursor.par());
2045 #ifdef WITH_WARNINGS
2046 #warning This will not work anymore when we have multiple views of the same buffer
2047 // In this case, we will have to correct also the cursors held by
2048 // other bufferviews. It will probably be easier to do that in a more
2049 // automated way in LyXCursor code. (JMarc 26/09/2001)
2051 // correct all cursors held by the LyXText
2052 fixCursorAfterDelete(cursor, old_cursor);
2053 fixCursorAfterDelete(selection.cursor, old_cursor);
2054 fixCursorAfterDelete(selection.start, old_cursor);
2055 fixCursorAfterDelete(selection.end, old_cursor);
2056 fixCursorAfterDelete(last_sel_cursor, old_cursor);
2061 // don't delete anything if this is the ONLY paragraph!
2062 if (ownerParagraphs().size() == 1)
2065 // Do not delete empty paragraphs with keepempty set.
2066 if (old_cursor.par()->allowEmpty())
2069 // only do our magic if we changed paragraph
2070 if (old_cursor.par() == cursor.par())
2073 // record if we have deleted a paragraph
2074 // we can't possibly have deleted a paragraph before this point
2075 bool deleted = false;
2077 if (old_cursor.par()->empty() ||
2078 (old_cursor.par()->size() == 1 &&
2079 old_cursor.par()->isLineSeparator(0))) {
2080 // ok, we will delete anything
2081 LyXCursor tmpcursor;
2085 bool selection_position_was_oldcursor_position = (
2086 selection.cursor.par() == old_cursor.par()
2087 && selection.cursor.pos() == old_cursor.pos());
2089 if (getRow(old_cursor) != rows().begin()) {
2090 RowList::iterator prevrow = boost::prior(getRow(old_cursor));
2093 cursor = old_cursor; // that undo can restore the right cursor position
2094 #warning FIXME. --end() iterator is usable here
2095 ParagraphList::iterator endpit = boost::next(old_cursor.par());
2096 while (endpit != ownerParagraphs().end() &&
2097 endpit->getDepth()) {
2101 recordUndo(bv(), Undo::DELETE, old_cursor.par(),
2102 boost::prior(endpit));
2106 removeRow(getRow(old_cursor));
2108 ownerParagraphs().erase(old_cursor.par());
2110 /* Breakagain the next par. Needed because of
2111 * the parindent that can occur or dissappear.
2112 * The next row can change its height, if
2113 * there is another layout before */
2114 RowList::iterator tmprit = boost::next(prevrow);
2115 if (tmprit != rows().end()) {
2119 setHeightOfRow(prevrow);
2121 RowList::iterator nextrow = boost::next(getRow(old_cursor));
2125 cursor = old_cursor; // that undo can restore the right cursor position
2126 #warning FIXME. --end() iterator is usable here
2127 ParagraphList::iterator endpit = boost::next(old_cursor.par());
2128 while (endpit != ownerParagraphs().end() &&
2129 endpit->getDepth()) {
2133 recordUndo(bv(), Undo::DELETE, old_cursor.par(), boost::prior(endpit));
2137 removeRow(getRow(old_cursor));
2139 ownerParagraphs().erase(old_cursor.par());
2141 /* Breakagain the next par. Needed because of
2142 the parindent that can occur or dissappear.
2143 The next row can change its height, if
2144 there is another layout before */
2145 if (nextrow != rows().end()) {
2146 breakAgain(nextrow);
2152 setCursorIntern(cursor.par(), cursor.pos());
2154 if (selection_position_was_oldcursor_position) {
2155 // correct selection
2156 selection.cursor = cursor;
2160 if (old_cursor.par()->stripLeadingSpaces()) {
2161 redoParagraph(old_cursor.par());
2163 setCursorIntern(cursor.par(), cursor.pos());
2164 selection.cursor = cursor;
2171 ParagraphList & LyXText::ownerParagraphs() const
2174 return inset_owner->paragraphs;
2176 return bv_owner->buffer()->paragraphs;
2180 bool LyXText::needRefresh() const
2182 return need_refresh_;
2186 void LyXText::clearPaint()
2188 need_refresh_ = false;
2192 void LyXText::postPaint()
2194 need_refresh_ = true;
2196 // We are an inset's lyxtext. Tell the top-level lyxtext
2197 // it needs to update the row we're in.
2199 bv()->text->postPaint();
2203 bool LyXText::isInInset() const
2205 // Sub-level has non-null bv owner and
2206 // non-null inset owner.
2207 return inset_owner != 0 && bv_owner != 0;
2211 int defaultRowHeight()
2213 LyXFont const font(LyXFont::ALL_SANE);
2214 return int(font_metrics::maxAscent(font)
2215 + font_metrics::maxDescent(font) * 1.5);