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 "paragraph_funcs.h"
39 #include "insets/insetbibitem.h"
40 #include "insets/insetenv.h"
41 #include "insets/insetfloat.h"
42 #include "insets/insetwrap.h"
44 #include "support/LAssert.h"
45 #include "support/textutils.h"
46 #include "support/lstrings.h"
48 #include <boost/tuple/tuple.hpp>
50 using namespace lyx::support;
60 LyXText::LyXText(BufferView * bv)
61 : height(0), width(0), anchor_row_offset_(0),
62 inset_owner(0), the_locking_inset(0), bv_owner(bv)
64 anchor_row_ = rows().end();
65 need_break_row = rows().end();
71 LyXText::LyXText(BufferView * bv, InsetText * inset)
72 : height(0), width(0), anchor_row_offset_(0),
73 inset_owner(inset), the_locking_inset(0), bv_owner(bv)
75 anchor_row_ = rows().end();
76 need_break_row = rows().end();
82 void LyXText::init(BufferView * bview)
87 need_break_row = rows().end();
91 anchor_row_ = rows().end();
92 anchor_row_offset_ = 0;
94 ParagraphList::iterator pit = ownerParagraphs().begin();
95 ParagraphList::iterator end = ownerParagraphs().end();
97 current_font = getFont(bview->buffer(), pit, 0);
99 for (; pit != end; ++pit)
100 insertParagraph(pit, rowlist_.end());
102 setCursorIntern(rowlist_.begin()->par(), 0);
103 selection.cursor = cursor;
109 // Gets the fully instantiated font at a given position in a paragraph
110 // Basically the same routine as Paragraph::getFont() in paragraph.C.
111 // The difference is that this one is used for displaying, and thus we
112 // are allowed to make cosmetic improvements. For instance make footnotes
114 // If position is -1, we get the layout font of the paragraph.
115 // If position is -2, we get the font of the manual label of the paragraph.
116 LyXFont const LyXText::getFont(Buffer const * buf, ParagraphList::iterator pit,
121 LyXLayout_ptr const & layout = pit->layout();
123 // We specialize the 95% common case:
124 if (!pit->getDepth()) {
125 if (layout->labeltype == LABEL_MANUAL
126 && pos < pit->beginningOfBody()) {
128 LyXFont f = pit->getFontSettings(buf->params, pos);
130 pit->inInset()->getDrawFont(f);
131 return f.realize(layout->reslabelfont);
133 LyXFont f = pit->getFontSettings(buf->params, pos);
135 pit->inInset()->getDrawFont(f);
136 return f.realize(layout->resfont);
140 // The uncommon case need not be optimized as much
144 if (pos < pit->beginningOfBody()) {
146 layoutfont = layout->labelfont;
149 layoutfont = layout->font;
152 LyXFont tmpfont = pit->getFontSettings(buf->params, pos);
153 tmpfont.realize(layoutfont);
156 pit->inInset()->getDrawFont(tmpfont);
158 // Realize with the fonts of lesser depth.
159 tmpfont.realize(outerFont(pit, ownerParagraphs()));
161 return realizeFont(tmpfont, buf->params);
165 LyXFont const LyXText::getLayoutFont(Buffer const * buf,
166 ParagraphList::iterator pit) const
168 LyXLayout_ptr const & layout = pit->layout();
170 if (!pit->getDepth()) {
171 return layout->resfont;
174 LyXFont font(layout->font);
175 // Realize with the fonts of lesser depth.
176 font.realize(outerFont(pit, ownerParagraphs()));
178 return realizeFont(font, buf->params);
182 LyXFont const LyXText::getLabelFont(Buffer const * buf,
183 ParagraphList::iterator pit) const
185 LyXLayout_ptr const & layout = pit->layout();
187 if (!pit->getDepth()) {
188 return layout->reslabelfont;
191 LyXFont font(layout->labelfont);
192 // Realize with the fonts of lesser depth.
193 font.realize(outerFont(pit, ownerParagraphs()));
195 return realizeFont(layout->labelfont, buf->params);
199 void LyXText::setCharFont(ParagraphList::iterator pit,
200 pos_type pos, LyXFont const & fnt,
203 Buffer const * buf = bv()->buffer();
204 LyXFont font = getFont(buf, pit, pos);
205 font.update(fnt, buf->params.language, toggleall);
206 // Let the insets convert their font
207 if (pit->isInset(pos)) {
208 Inset * inset = pit->getInset(pos);
209 if (isEditableInset(inset)) {
210 UpdatableInset * uinset =
211 static_cast<UpdatableInset *>(inset);
212 uinset->setFont(bv(), fnt, toggleall, true);
216 // Plug thru to version below:
217 setCharFont(buf, pit, pos, font);
221 void LyXText::setCharFont(Buffer const * buf, ParagraphList::iterator pit,
222 pos_type pos, LyXFont const & fnt)
226 LyXTextClass const & tclass = buf->params.getLyXTextClass();
227 LyXLayout_ptr const & layout = pit->layout();
229 // Get concrete layout font to reduce against
232 if (pos < pit->beginningOfBody())
233 layoutfont = layout->labelfont;
235 layoutfont = layout->font;
237 // Realize against environment font information
238 if (pit->getDepth()) {
239 ParagraphList::iterator tp = pit;
240 while (!layoutfont.resolved() &&
241 tp != ownerParagraphs().end() &&
243 tp = outerHook(tp, ownerParagraphs());
244 if (tp != ownerParagraphs().end())
245 layoutfont.realize(tp->layout()->font);
249 layoutfont.realize(tclass.defaultfont());
251 // Now, reduce font against full layout font
252 font.reduce(layoutfont);
254 pit->setFont(pos, font);
258 // removes the row and reset the touched counters
259 void LyXText::removeRow(RowList::iterator rit)
261 if (anchor_row_ == rit) {
262 if (rit != rows().begin()) {
263 anchor_row_ = boost::prior(rit);
264 anchor_row_offset_ += anchor_row_->height();
266 anchor_row_ = boost::next(rit);
267 anchor_row_offset_ -= rit->height();
271 // the text becomes smaller
272 height -= rit->height();
278 // remove all following rows of the paragraph of the specified row.
279 void LyXText::removeParagraph(RowList::iterator rit)
281 ParagraphList::iterator tmppit = rit->par();
284 while (rit != rows().end() && rit->par() == tmppit) {
285 RowList::iterator tmprit = boost::next(rit);
292 void LyXText::insertParagraph(ParagraphList::iterator pit,
293 RowList::iterator rowit)
295 // insert a new row, starting at position 0
297 RowList::iterator rit = rowlist_.insert(rowit, newrow);
299 // and now append the whole paragraph before the new row
300 appendParagraph(rit);
304 Inset * LyXText::getInset() const
306 ParagraphList::iterator pit = cursor.par();
307 pos_type const pos = cursor.pos();
309 if (pos < pit->size() && pit->isInset(pos)) {
310 return pit->getInset(pos);
316 void LyXText::toggleInset()
318 Inset * inset = getInset();
319 // is there an editable inset at cursor position?
320 if (!isEditableInset(inset)) {
321 // No, try to see if we are inside a collapsable inset
322 if (inset_owner && inset_owner->owner()
323 && inset_owner->owner()->isOpen()) {
324 bv()->unlockInset(inset_owner->owner());
325 inset_owner->owner()->close(bv());
326 bv()->getLyXText()->cursorRight(bv());
330 //bv()->owner()->message(inset->editMessage());
332 // do we want to keep this?? (JMarc)
333 if (!isHighlyEditableInset(inset))
334 setCursorParUndo(bv());
336 if (inset->isOpen()) {
342 bv()->updateInset(inset);
346 /* used in setlayout */
347 // Asger is not sure we want to do this...
348 void LyXText::makeFontEntriesLayoutSpecific(Buffer const & buf,
351 LyXLayout_ptr const & layout = par.layout();
352 pos_type const psize = par.size();
355 for (pos_type pos = 0; pos < psize; ++pos) {
356 if (pos < par.beginningOfBody())
357 layoutfont = layout->labelfont;
359 layoutfont = layout->font;
361 LyXFont tmpfont = par.getFontSettings(buf.params, pos);
362 tmpfont.reduce(layoutfont);
363 par.setFont(pos, tmpfont);
368 ParagraphList::iterator
369 LyXText::setLayout(LyXCursor & cur, LyXCursor & sstart_cur,
370 LyXCursor & send_cur,
371 string const & layout)
373 ParagraphList::iterator endpit = boost::next(send_cur.par());
374 ParagraphList::iterator undoendpit = endpit;
375 ParagraphList::iterator pars_end = ownerParagraphs().end();
377 if (endpit != pars_end && endpit->getDepth()) {
378 while (endpit != pars_end && endpit->getDepth()) {
382 } else if (endpit != pars_end) {
383 // because of parindents etc.
387 setUndo(bv(), Undo::EDIT, sstart_cur.par(), boost::prior(undoendpit));
389 // ok we have a selection. This is always between sstart_cur
390 // and sel_end cursor
392 ParagraphList::iterator pit = sstart_cur.par();
393 ParagraphList::iterator epit = boost::next(send_cur.par());
395 LyXLayout_ptr const & lyxlayout =
396 bv()->buffer()->params.getLyXTextClass()[layout];
399 pit->applyLayout(lyxlayout);
400 makeFontEntriesLayoutSpecific(*bv()->buffer(), *pit);
401 ParagraphList::iterator fppit = pit;
402 fppit->params().spaceTop(lyxlayout->fill_top ?
403 VSpace(VSpace::VFILL)
404 : VSpace(VSpace::NONE));
405 fppit->params().spaceBottom(lyxlayout->fill_bottom ?
406 VSpace(VSpace::VFILL)
407 : VSpace(VSpace::NONE));
408 if (lyxlayout->margintype == MARGIN_MANUAL)
409 pit->setLabelWidthString(lyxlayout->labelstring());
412 } while (pit != epit);
418 // set layout over selection and make a total rebreak of those paragraphs
419 void LyXText::setLayout(string const & layout)
421 LyXCursor tmpcursor = cursor; // store the current cursor
423 // if there is no selection just set the layout
424 // of the current paragraph
425 if (!selection.set()) {
426 selection.start = cursor; // dummy selection
427 selection.end = cursor;
430 // special handling of new environment insets
431 BufferParams const & params = bv()->buffer()->params;
432 LyXLayout_ptr const & lyxlayout = params.getLyXTextClass()[layout];
433 if (lyxlayout->is_environment) {
434 // move everything in a new environment inset
435 lyxerr << "setting layout " << layout << endl;
436 bv()->owner()->dispatch(FuncRequest(LFUN_HOME));
437 bv()->owner()->dispatch(FuncRequest(LFUN_ENDSEL));
438 bv()->owner()->dispatch(FuncRequest(LFUN_CUT));
439 Inset * inset = new InsetEnvironment(params, layout);
440 if (bv()->insertInset(inset)) {
442 //bv()->owner()->dispatch(FuncRequest(LFUN_PASTE));
449 ParagraphList::iterator endpit = setLayout(cursor, selection.start,
450 selection.end, layout);
451 redoParagraphs(selection.start, endpit);
453 // we have to reset the selection, because the
454 // geometry could have changed
455 setCursor(selection.start.par(), selection.start.pos(), false);
456 selection.cursor = cursor;
457 setCursor(selection.end.par(), selection.end.pos(), false);
461 setCursor(tmpcursor.par(), tmpcursor.pos(), true);
465 bool LyXText::changeDepth(bv_funcs::DEPTH_CHANGE type, bool test_only)
467 ParagraphList::iterator pit(cursor.par());
468 ParagraphList::iterator end(cursor.par());
469 ParagraphList::iterator start = pit;
471 if (selection.set()) {
472 pit = selection.start.par();
473 end = selection.end.par();
477 ParagraphList::iterator pastend = boost::next(end);
480 setUndo(bv(), Undo::EDIT, start, end);
482 bool changed = false;
484 int prev_after_depth = 0;
485 #warning parlist ... could be nicer ?
486 if (start != ownerParagraphs().begin()) {
487 prev_after_depth = boost::prior(start)->getMaxDepthAfter();
491 int const depth = pit->params().depth();
492 if (type == bv_funcs::INC_DEPTH) {
493 if (depth < prev_after_depth
494 && pit->layout()->labeltype != LABEL_BIBLIO) {
497 pit->params().depth(depth + 1);
504 pit->params().depth(depth - 1);
507 prev_after_depth = pit->getMaxDepthAfter();
519 // Wow, redoParagraphs is stupid.
521 setCursor(tmpcursor, start, 0);
523 redoParagraphs(tmpcursor, pastend);
525 // We need to actually move the text->cursor. I don't
526 // understand why ...
529 // we have to reset the visual selection because the
530 // geometry could have changed
531 if (selection.set()) {
532 setCursor(selection.start.par(), selection.start.pos());
533 selection.cursor = cursor;
534 setCursor(selection.end.par(), selection.end.pos());
537 // this handles the counter labels, and also fixes up
538 // depth values for follow-on (child) paragraphs
542 setCursor(tmpcursor.par(), tmpcursor.pos());
548 // set font over selection and make a total rebreak of those paragraphs
549 void LyXText::setFont(LyXFont const & font, bool toggleall)
551 // if there is no selection just set the current_font
552 if (!selection.set()) {
553 // Determine basis font
555 if (cursor.pos() < cursor.par()->beginningOfBody()) {
556 layoutfont = getLabelFont(bv()->buffer(),
559 layoutfont = getLayoutFont(bv()->buffer(),
562 // Update current font
563 real_current_font.update(font,
564 bv()->buffer()->params.language,
567 // Reduce to implicit settings
568 current_font = real_current_font;
569 current_font.reduce(layoutfont);
570 // And resolve it completely
571 real_current_font.realize(layoutfont);
576 LyXCursor tmpcursor = cursor; // store the current cursor
578 // ok we have a selection. This is always between sel_start_cursor
579 // and sel_end cursor
581 setUndo(bv(), Undo::EDIT, selection.start.par(), selection.end.par());
583 cursor = selection.start;
584 while (cursor.par() != selection.end.par() ||
585 cursor.pos() < selection.end.pos())
587 if (cursor.pos() < cursor.par()->size()) {
588 // an open footnote should behave like a closed one
589 setCharFont(cursor.par(), cursor.pos(),
591 cursor.pos(cursor.pos() + 1);
594 cursor.par(boost::next(cursor.par()));
599 redoParagraphs(selection.start, boost::next(selection.end.par()));
601 // we have to reset the selection, because the
602 // geometry could have changed, but we keep
603 // it for user convenience
604 setCursor(selection.start.par(), selection.start.pos());
605 selection.cursor = cursor;
606 setCursor(selection.end.par(), selection.end.pos());
608 setCursor(tmpcursor.par(), tmpcursor.pos(), true,
609 tmpcursor.boundary());
613 void LyXText::redoHeightOfParagraph()
615 RowList::iterator tmprow = cursorRow();
616 int y = cursor.y() - tmprow->baseline();
618 setHeightOfRow(tmprow);
620 while (tmprow != rows().begin()
621 && boost::prior(tmprow)->par() == tmprow->par()) {
623 y -= tmprow->height();
624 setHeightOfRow(tmprow);
629 setCursor(cursor.par(), cursor.pos(), false, cursor.boundary());
633 void LyXText::redoDrawingOfParagraph(LyXCursor const & cur)
635 RowList::iterator tmprow = getRow(cur);
637 int y = cur.y() - tmprow->baseline();
638 setHeightOfRow(tmprow);
640 while (tmprow != rows().begin()
641 && boost::prior(tmprow)->par() == tmprow->par()) {
643 y -= tmprow->height();
647 setCursor(cur.par(), cur.pos());
651 // deletes and inserts again all paragraphs between the cursor
652 // and the specified par
653 // This function is needed after SetLayout and SetFont etc.
654 void LyXText::redoParagraphs(LyXCursor const & cur,
655 ParagraphList::iterator endpit)
657 RowList::iterator tmprit = getRow(cur);
658 int y = cur.y() - tmprit->baseline();
660 ParagraphList::iterator first_phys_pit;
661 RowList::iterator prevrit;
662 if (tmprit == rows().begin()) {
663 // A trick/hack for UNDO.
664 // This is needed because in an UNDO/REDO we could have
665 // changed the ownerParagraph() so the paragraph inside
666 // the row is NOT my really first par anymore.
667 // Got it Lars ;) (Jug 20011206)
668 first_phys_pit = ownerParagraphs().begin();
669 prevrit = rows().end();
671 first_phys_pit = tmprit->par();
672 while (tmprit != rows().begin()
673 && boost::prior(tmprit)->par() == first_phys_pit)
676 y -= tmprit->height();
678 prevrit = boost::prior(tmprit);
682 while (tmprit != rows().end() && tmprit->par() != endpit) {
683 RowList::iterator tmprit2 = tmprit++;
687 // Reinsert the paragraphs.
688 ParagraphList::iterator tmppit = first_phys_pit;
690 while (tmppit != ownerParagraphs().end()) {
691 insertParagraph(tmppit, tmprit);
692 while (tmprit != rows().end()
693 && tmprit->par() == tmppit) {
697 if (tmppit == endpit)
700 if (prevrit != rows().end()) {
701 setHeightOfRow(prevrit);
702 postPaint(y - prevrit->height());
704 setHeightOfRow(rows().begin());
707 if (tmprit != rows().end())
708 setHeightOfRow(tmprit);
714 void LyXText::fullRebreak()
717 setCursorIntern(cursor.par(), cursor.pos());
721 void LyXText::partialRebreak()
723 if (rows().empty()) {
728 RowList::iterator rows_end = rows().end();
730 if (need_break_row != rows_end) {
731 breakAgain(need_break_row);
732 need_break_row = rows_end;
738 // important for the screen
741 // the cursor set functions have a special mechanism. When they
742 // realize, that you left an empty paragraph, they will delete it.
743 // They also delete the corresponding row
745 // need the selection cursor:
746 void LyXText::setSelection()
748 bool const lsel = TextCursor::setSelection();
750 if (inset_owner && (selection.set() || lsel))
751 inset_owner->setUpdateStatus(InsetText::SELECTION);
756 void LyXText::clearSelection()
758 TextCursor::clearSelection();
760 // reset this in the bv_owner!
761 if (bv_owner && bv_owner->text)
762 bv_owner->text->xsel_cache.set(false);
766 void LyXText::cursorHome()
768 setCursor(cursor.par(), cursorRow()->pos());
772 void LyXText::cursorEnd()
774 if (cursor.par()->empty())
777 RowList::iterator rit = cursorRow();
778 RowList::iterator next_rit = boost::next(rit);
779 ParagraphList::iterator pit = rit->par();
780 pos_type last_pos = lastPos(*this, rit);
782 if (next_rit == rows().end() || next_rit->par() != pit) {
786 (pit->getChar(last_pos) != ' ' && !pit->isNewline(last_pos))) {
791 setCursor(pit, last_pos);
795 void LyXText::cursorTop()
797 setCursor(ownerParagraphs().begin(), 0);
801 void LyXText::cursorBottom()
803 ParagraphList::iterator lastpit =
804 boost::prior(ownerParagraphs().end());
805 setCursor(lastpit, lastpit->size());
809 void LyXText::toggleFree(LyXFont const & font, bool toggleall)
811 // If the mask is completely neutral, tell user
812 if (font == LyXFont(LyXFont::ALL_IGNORE)) {
813 // Could only happen with user style
814 bv()->owner()->message(_("No font change defined. Use Character under the Layout menu to define font change."));
818 // Try implicit word selection
819 // If there is a change in the language the implicit word selection
821 LyXCursor resetCursor = cursor;
822 bool implicitSelection = (font.language() == ignore_language
823 && font.number() == LyXFont::IGNORE)
824 ? selectWordWhenUnderCursor(lyx::WHOLE_WORD_STRICT) : false;
827 setFont(font, toggleall);
829 // Implicit selections are cleared afterwards
830 //and cursor is set to the original position.
831 if (implicitSelection) {
833 cursor = resetCursor;
834 setCursor(cursor.par(), cursor.pos());
835 selection.cursor = cursor;
838 inset_owner->setUpdateStatus(InsetText::CURSOR_PAR);
842 string LyXText::getStringToIndex()
844 // Try implicit word selection
845 // If there is a change in the language the implicit word selection
847 LyXCursor const reset_cursor = cursor;
848 bool const implicitSelection =
849 selectWordWhenUnderCursor(lyx::PREVIOUS_WORD);
852 if (!selection.set())
853 bv()->owner()->message(_("Nothing to index!"));
854 else if (selection.start.par() != selection.end.par())
855 bv()->owner()->message(_("Cannot index more than one paragraph!"));
857 idxstring = selectionAsString(bv()->buffer(), false);
859 // Reset cursors to their original position.
860 cursor = reset_cursor;
861 setCursor(cursor.par(), cursor.pos());
862 selection.cursor = cursor;
864 // Clear the implicit selection.
865 if (implicitSelection)
872 // the DTP switches for paragraphs. LyX will store them in the first
873 // physicla paragraph. When a paragraph is broken, the top settings rest,
874 // the bottom settings are given to the new one. So I can make shure,
875 // they do not duplicate themself and you cannnot make dirty things with
878 void LyXText::setParagraph(bool line_top, bool line_bottom,
879 bool pagebreak_top, bool pagebreak_bottom,
880 VSpace const & space_top,
881 VSpace const & space_bottom,
882 Spacing const & spacing,
884 string const & labelwidthstring,
887 LyXCursor tmpcursor = cursor;
888 if (!selection.set()) {
889 selection.start = cursor;
890 selection.end = cursor;
893 // make sure that the depth behind the selection are restored, too
894 ParagraphList::iterator endpit = boost::next(selection.end.par());
895 ParagraphList::iterator undoendpit = endpit;
896 ParagraphList::iterator pars_end = ownerParagraphs().end();
898 if (endpit != pars_end && endpit->getDepth()) {
899 while (endpit != pars_end && endpit->getDepth()) {
903 } else if (endpit != pars_end) {
904 // because of parindents etc.
908 setUndo(bv(), Undo::EDIT, selection.start.par(),
909 boost::prior(undoendpit));
912 ParagraphList::iterator tmppit = selection.end.par();
914 while (tmppit != boost::prior(selection.start.par())) {
915 setCursor(tmppit, 0);
916 postPaint(cursor.y() - cursorRow()->baseline());
918 ParagraphList::iterator pit = cursor.par();
919 ParagraphParameters & params = pit->params();
921 params.lineTop(line_top);
922 params.lineBottom(line_bottom);
923 params.pagebreakTop(pagebreak_top);
924 params.pagebreakBottom(pagebreak_bottom);
925 params.spaceTop(space_top);
926 params.spaceBottom(space_bottom);
927 params.spacing(spacing);
928 // does the layout allow the new alignment?
929 LyXLayout_ptr const & layout = pit->layout();
931 if (align == LYX_ALIGN_LAYOUT)
932 align = layout->align;
933 if (align & layout->alignpossible) {
934 if (align == layout->align)
935 params.align(LYX_ALIGN_LAYOUT);
939 pit->setLabelWidthString(labelwidthstring);
940 params.noindent(noindent);
941 tmppit = boost::prior(pit);
944 redoParagraphs(selection.start, endpit);
947 setCursor(selection.start.par(), selection.start.pos());
948 selection.cursor = cursor;
949 setCursor(selection.end.par(), selection.end.pos());
951 setCursor(tmpcursor.par(), tmpcursor.pos());
953 bv()->updateInset(inset_owner);
957 // set the counter of a paragraph. This includes the labels
958 void LyXText::setCounter(Buffer const * buf, ParagraphList::iterator pit)
960 LyXTextClass const & textclass = buf->params.getLyXTextClass();
961 LyXLayout_ptr const & layout = pit->layout();
963 if (pit != ownerParagraphs().begin()) {
965 pit->params().appendix(boost::prior(pit)->params().appendix());
966 if (!pit->params().appendix() &&
967 pit->params().startOfAppendix()) {
968 pit->params().appendix(true);
969 textclass.counters().reset();
971 pit->enumdepth = boost::prior(pit)->enumdepth;
972 pit->itemdepth = boost::prior(pit)->itemdepth;
974 pit->params().appendix(pit->params().startOfAppendix());
979 /* Maybe we have to increment the enumeration depth.
980 * BUT, enumeration in a footnote is considered in isolation from its
981 * surrounding paragraph so don't increment if this is the
982 * first line of the footnote
983 * AND, bibliographies can't have their depth changed ie. they
984 * are always of depth 0
986 if (pit != ownerParagraphs().begin()
987 && boost::prior(pit)->getDepth() < pit->getDepth()
988 && boost::prior(pit)->layout()->labeltype == LABEL_COUNTER_ENUMI
989 && pit->enumdepth < 3
990 && layout->labeltype != LABEL_BIBLIO) {
994 // Maybe we have to decrement the enumeration depth, see note above
995 if (pit != ownerParagraphs().begin()
996 && boost::prior(pit)->getDepth() > pit->getDepth()
997 && layout->labeltype != LABEL_BIBLIO) {
998 pit->enumdepth = depthHook(pit, ownerParagraphs(),
999 pit->getDepth())->enumdepth;
1002 if (!pit->params().labelString().empty()) {
1003 pit->params().labelString(string());
1006 if (layout->margintype == MARGIN_MANUAL) {
1007 if (pit->params().labelWidthString().empty()) {
1008 pit->setLabelWidthString(layout->labelstring());
1011 pit->setLabelWidthString(string());
1014 // is it a layout that has an automatic label?
1015 if (layout->labeltype >= LABEL_COUNTER_CHAPTER) {
1016 int const i = layout->labeltype - LABEL_COUNTER_CHAPTER;
1020 if (i >= 0 && i <= buf->params.secnumdepth) {
1024 textclass.counters().step(layout->latexname());
1026 // Is there a label? Useful for Chapter layout
1027 if (!pit->params().appendix()) {
1028 s << buf->B_(layout->labelstring());
1030 s << buf->B_(layout->labelstring_appendix());
1033 // Use of an integer is here less than elegant. For now.
1034 int head = textclass.maxcounter() - LABEL_COUNTER_CHAPTER;
1035 if (!pit->params().appendix()) {
1036 numbertype = "sectioning";
1038 numbertype = "appendix";
1039 if (pit->isRightToLeftPar(buf->params))
1040 langtype = "hebrew";
1046 << textclass.counters()
1047 .numberLabel(layout->latexname(),
1048 numbertype, langtype, head);
1050 pit->params().labelString(STRCONV(s.str()));
1052 // reset enum counters
1053 textclass.counters().reset("enum");
1054 } else if (layout->labeltype < LABEL_COUNTER_ENUMI) {
1055 textclass.counters().reset("enum");
1056 } else if (layout->labeltype == LABEL_COUNTER_ENUMI) {
1058 // Yes I know this is a really, really! bad solution
1060 string enumcounter("enum");
1062 switch (pit->enumdepth) {
1071 enumcounter += "iv";
1074 // not a valid enumdepth...
1078 textclass.counters().step(enumcounter);
1080 s << textclass.counters()
1081 .numberLabel(enumcounter, "enumeration");
1082 pit->params().labelString(STRCONV(s.str()));
1084 } else if (layout->labeltype == LABEL_BIBLIO) {// ale970302
1085 textclass.counters().step("bibitem");
1086 int number = textclass.counters().value("bibitem");
1087 if (pit->bibitem()) {
1088 pit->bibitem()->setCounter(number);
1089 pit->params().labelString(layout->labelstring());
1091 // In biblio should't be following counters but...
1093 string s = buf->B_(layout->labelstring());
1095 // the caption hack:
1096 if (layout->labeltype == LABEL_SENSITIVE) {
1097 ParagraphList::iterator tmppit = pit;
1100 while (tmppit != ownerParagraphs().end() &&
1102 // the single '=' is intended below
1103 && (in = tmppit->inInset()->owner())) {
1104 if (in->lyxCode() == Inset::FLOAT_CODE ||
1105 in->lyxCode() == Inset::WRAP_CODE) {
1109 tmppit = std::find(ownerParagraphs().begin(), ownerParagraphs().end(), *in->parOwner());
1116 if (in->lyxCode() == Inset::FLOAT_CODE)
1117 type = static_cast<InsetFloat*>(in)->params().type;
1118 else if (in->lyxCode() == Inset::WRAP_CODE)
1119 type = static_cast<InsetWrap*>(in)->params().type;
1123 Floating const & fl = textclass.floats().getType(type);
1125 textclass.counters().step(fl.type());
1127 // Doesn't work... yet.
1128 s = bformat(_("%1$s #:"), buf->B_(fl.name()));
1130 // par->SetLayout(0);
1131 // s = layout->labelstring;
1132 s = _("Senseless: ");
1135 pit->params().labelString(s);
1137 // reset the enumeration counter. They are always reset
1138 // when there is any other layout between
1139 // Just fall-through between the cases so that all
1140 // enum counters deeper than enumdepth is also reset.
1141 switch (pit->enumdepth) {
1143 textclass.counters().reset("enumi");
1145 textclass.counters().reset("enumii");
1147 textclass.counters().reset("enumiii");
1149 textclass.counters().reset("enumiv");
1155 // Updates all counters. Paragraphs with changed label string will be rebroken
1156 void LyXText::updateCounters()
1158 RowList::iterator rowit = rows().begin();
1159 ParagraphList::iterator pit = rowit->par();
1161 // CHECK if this is really needed. (Lgb)
1162 bv()->buffer()->params.getLyXTextClass().counters().reset();
1164 ParagraphList::iterator beg = ownerParagraphs().begin();
1165 ParagraphList::iterator end = ownerParagraphs().end();
1166 for (; pit != end; ++pit) {
1167 while (rowit->par() != pit)
1170 string const oldLabel = pit->params().labelString();
1172 size_t maxdepth = 0;
1174 maxdepth = boost::prior(pit)->getMaxDepthAfter();
1176 if (pit->params().depth() > maxdepth)
1177 pit->params().depth(maxdepth);
1179 // setCounter can potentially change the labelString.
1180 setCounter(bv()->buffer(), pit);
1182 string const & newLabel = pit->params().labelString();
1184 if (oldLabel.empty() && !newLabel.empty()) {
1185 removeParagraph(rowit);
1186 appendParagraph(rowit);
1192 void LyXText::insertInset(Inset * inset)
1194 if (!cursor.par()->insetAllowed(inset->lyxCode()))
1196 setUndo(bv(), Undo::FINISH, cursor.par());
1198 cursor.par()->insertInset(cursor.pos(), inset);
1199 // Just to rebreak and refresh correctly.
1200 // The character will not be inserted a second time
1201 insertChar(Paragraph::META_INSET);
1202 // If we enter a highly editable inset the cursor should be to before
1203 // the inset. This couldn't happen before as Undo was not handled inside
1204 // inset now after the Undo LyX tries to call inset->Edit(...) again
1205 // and cannot do this as the cursor is behind the inset and GetInset
1206 // does not return the inset!
1207 if (isHighlyEditableInset(inset)) {
1214 void LyXText::cutSelection(bool doclear, bool realcut)
1216 // Stuff what we got on the clipboard. Even if there is no selection.
1218 // There is a problem with having the stuffing here in that the
1219 // larger the selection the slower LyX will get. This can be
1220 // solved by running the line below only when the selection has
1221 // finished. The solution used currently just works, to make it
1222 // faster we need to be more clever and probably also have more
1223 // calls to stuffClipboard. (Lgb)
1224 bv()->stuffClipboard(selectionAsString(bv()->buffer(), true));
1226 // This doesn't make sense, if there is no selection
1227 if (!selection.set())
1230 // OK, we have a selection. This is always between selection.start
1231 // and selection.end
1233 // make sure that the depth behind the selection are restored, too
1234 ParagraphList::iterator endpit = boost::next(selection.end.par());
1235 ParagraphList::iterator undoendpit = endpit;
1236 ParagraphList::iterator pars_end = ownerParagraphs().end();
1238 if (endpit != pars_end && endpit->getDepth()) {
1239 while (endpit != pars_end && endpit->getDepth()) {
1241 undoendpit = endpit;
1243 } else if (endpit != pars_end) {
1244 // because of parindents etc.
1248 setUndo(bv(), Undo::DELETE, selection.start.par(),
1249 boost::prior(undoendpit));
1252 endpit = selection.end.par();
1253 int endpos = selection.end.pos();
1255 boost::tie(endpit, endpos) = realcut ?
1256 CutAndPaste::cutSelection(bv()->buffer()->params,
1258 selection.start.par(), endpit,
1259 selection.start.pos(), endpos,
1260 bv()->buffer()->params.textclass,
1262 : CutAndPaste::eraseSelection(bv()->buffer()->params,
1264 selection.start.par(), endpit,
1265 selection.start.pos(), endpos,
1267 // sometimes necessary
1269 selection.start.par()->stripLeadingSpaces();
1271 redoParagraphs(selection.start, boost::next(endpit));
1272 #warning FIXME latent bug
1273 // endpit will be invalidated on redoParagraphs once ParagraphList
1274 // becomes a std::list? There are maybe other places on which this
1275 // can happend? (Ab)
1276 // cutSelection can invalidate the cursor so we need to set
1278 // we prefer the end for when tracking changes
1282 // need a valid cursor. (Lgb)
1285 setCursor(cursor.par(), cursor.pos());
1286 selection.cursor = cursor;
1291 void LyXText::copySelection()
1293 // stuff the selection onto the X clipboard, from an explicit copy request
1294 bv()->stuffClipboard(selectionAsString(bv()->buffer(), true));
1296 // this doesnt make sense, if there is no selection
1297 if (!selection.set())
1300 // ok we have a selection. This is always between selection.start
1301 // and sel_end cursor
1303 // copy behind a space if there is one
1304 while (selection.start.par()->size() > selection.start.pos()
1305 && selection.start.par()->isLineSeparator(selection.start.pos())
1306 && (selection.start.par() != selection.end.par()
1307 || selection.start.pos() < selection.end.pos()))
1308 selection.start.pos(selection.start.pos() + 1);
1310 CutAndPaste::copySelection(selection.start.par(),
1311 selection.end.par(),
1312 selection.start.pos(), selection.end.pos(),
1313 bv()->buffer()->params.textclass);
1317 void LyXText::pasteSelection(size_t sel_index)
1319 // this does not make sense, if there is nothing to paste
1320 if (!CutAndPaste::checkPastePossible())
1323 setUndo(bv(), Undo::INSERT, cursor.par());
1325 ParagraphList::iterator endpit;
1330 boost::tie(ppp, endpit) =
1331 CutAndPaste::pasteSelection(*bv()->buffer(),
1333 cursor.par(), cursor.pos(),
1334 bv()->buffer()->params.textclass,
1336 bufferErrors(*bv()->buffer(), el);
1337 bv()->showErrorList(_("Paste"));
1339 redoParagraphs(cursor, endpit);
1341 setCursor(cursor.par(), cursor.pos());
1344 selection.cursor = cursor;
1345 setCursor(ppp.first, ppp.second);
1351 void LyXText::setSelectionRange(lyx::pos_type length)
1356 selection.cursor = cursor;
1363 // simple replacing. The font of the first selected character is used
1364 void LyXText::replaceSelectionWithString(string const & str)
1366 setCursorParUndo(bv());
1369 if (!selection.set()) { // create a dummy selection
1370 selection.end = cursor;
1371 selection.start = cursor;
1374 // Get font setting before we cut
1375 pos_type pos = selection.end.pos();
1376 LyXFont const font = selection.start.par()
1377 ->getFontSettings(bv()->buffer()->params,
1378 selection.start.pos());
1380 // Insert the new string
1381 string::const_iterator cit = str.begin();
1382 string::const_iterator end = str.end();
1383 for (; cit != end; ++cit) {
1384 selection.end.par()->insertChar(pos, (*cit), font);
1388 // Cut the selection
1389 cutSelection(true, false);
1395 // needed to insert the selection
1396 void LyXText::insertStringAsLines(string const & str)
1398 ParagraphList::iterator pit = cursor.par();
1399 pos_type pos = cursor.pos();
1400 ParagraphList::iterator endpit = boost::next(cursor.par());
1402 setCursorParUndo(bv());
1404 // only to be sure, should not be neccessary
1407 bv()->buffer()->insertStringAsLines(pit, pos, current_font, str);
1409 redoParagraphs(cursor, endpit);
1410 setCursor(cursor.par(), cursor.pos());
1411 selection.cursor = cursor;
1412 setCursor(pit, pos);
1417 // turns double-CR to single CR, others where converted into one
1418 // blank. Then InsertStringAsLines is called
1419 void LyXText::insertStringAsParagraphs(string const & str)
1421 string linestr(str);
1422 bool newline_inserted = false;
1423 string::size_type const siz = linestr.length();
1425 for (string::size_type i = 0; i < siz; ++i) {
1426 if (linestr[i] == '\n') {
1427 if (newline_inserted) {
1428 // we know that \r will be ignored by
1429 // InsertStringA. Of course, it is a dirty
1430 // trick, but it works...
1431 linestr[i - 1] = '\r';
1435 newline_inserted = true;
1437 } else if (IsPrintable(linestr[i])) {
1438 newline_inserted = false;
1441 insertStringAsLines(linestr);
1445 void LyXText::checkParagraph(ParagraphList::iterator pit, pos_type pos)
1447 LyXCursor tmpcursor;
1451 RowList::iterator row = getRow(pit, pos, y);
1452 RowList::iterator beg = rows().begin();
1454 // is there a break one row above
1456 && boost::prior(row)->par() == row->par()) {
1457 z = rowBreakPoint(*boost::prior(row));
1458 if (z >= row->pos()) {
1459 // set the dimensions of the row above
1460 y -= boost::prior(row)->height();
1463 breakAgain(boost::prior(row));
1465 // set the cursor again. Otherwise
1466 // dangling pointers are possible
1467 setCursor(cursor.par(), cursor.pos(),
1468 false, cursor.boundary());
1469 selection.cursor = cursor;
1474 int const tmpheight = row->height();
1475 pos_type const tmplast = lastPos(*this, row);
1478 if (row->height() == tmpheight && lastPos(*this, row) == tmplast) {
1479 postRowPaint(row, y);
1484 // check the special right address boxes
1485 if (pit->layout()->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1491 redoDrawingOfParagraph(tmpcursor);
1494 // set the cursor again. Otherwise dangling pointers are possible
1495 // also set the selection
1497 if (selection.set()) {
1499 setCursorIntern(selection.cursor.par(), selection.cursor.pos(),
1500 false, selection.cursor.boundary());
1501 selection.cursor = cursor;
1502 setCursorIntern(selection.start.par(),
1503 selection.start.pos(),
1504 false, selection.start.boundary());
1505 selection.start = cursor;
1506 setCursorIntern(selection.end.par(),
1507 selection.end.pos(),
1508 false, selection.end.boundary());
1509 selection.end = cursor;
1510 setCursorIntern(last_sel_cursor.par(),
1511 last_sel_cursor.pos(),
1512 false, last_sel_cursor.boundary());
1513 last_sel_cursor = cursor;
1516 setCursorIntern(cursor.par(), cursor.pos(),
1517 false, cursor.boundary());
1521 // returns false if inset wasn't found
1522 bool LyXText::updateInset(Inset * inset)
1524 // first check the current paragraph
1525 int pos = cursor.par()->getPositionOfInset(inset);
1527 checkParagraph(cursor.par(), pos);
1531 // check every paragraph
1533 ParagraphList::iterator par = ownerParagraphs().begin();
1534 ParagraphList::iterator end = ownerParagraphs().end();
1535 for (; par != end; ++par) {
1536 pos = par->getPositionOfInset(inset);
1538 checkParagraph(par, pos);
1547 bool LyXText::setCursor(ParagraphList::iterator pit,
1549 bool setfont, bool boundary)
1551 LyXCursor old_cursor = cursor;
1552 setCursorIntern(pit, pos, setfont, boundary);
1553 return deleteEmptyParagraphMechanism(old_cursor);
1557 void LyXText::setCursor(LyXCursor & cur, ParagraphList::iterator pit,
1558 pos_type pos, bool boundary)
1560 Assert(pit != ownerParagraphs().end());
1564 cur.boundary(boundary);
1566 // get the cursor y position in text
1568 RowList::iterator row = getRow(pit, pos, y);
1569 RowList::iterator beg = rows().begin();
1571 RowList::iterator old_row = row;
1573 // if we are before the first char of this row and are still in the
1574 // same paragraph and there is a previous row then put the cursor on
1575 // the end of the previous row
1576 cur.iy(y + row->baseline());
1579 boost::prior(row)->par() == row->par() &&
1580 pos < pit->size() &&
1581 pit->getChar(pos) == Paragraph::META_INSET) {
1582 Inset * ins = pit->getInset(pos);
1583 if (ins && (ins->needFullRow() || ins->display())) {
1589 // y is now the beginning of the cursor row
1590 y += row->baseline();
1591 // y is now the cursor baseline
1594 pos_type last = lastPrintablePos(*this, old_row);
1596 // None of these should happen, but we're scaredy-cats
1597 if (pos > pit->size()) {
1598 lyxerr << "dont like 1 please report" << endl;
1601 } else if (pos > last + 1) {
1602 lyxerr << "dont like 2 please report" << endl;
1603 // This shouldn't happen.
1606 } else if (pos < row->pos()) {
1607 lyxerr << "dont like 3 please report" << endl;
1612 // now get the cursors x position
1613 float x = getCursorX(row, pos, last, boundary);
1616 if (old_row != row) {
1617 x = getCursorX(old_row, pos, last, boundary);
1621 /* We take out this for the time being because 1) the redraw code is not
1622 prepared to this yet and 2) because some good policy has yet to be decided
1623 while editting: for instance how to act on rows being created/deleted
1627 //if the cursor is in a visible row, anchor to it
1629 if (topy < y && y < topy + bv()->workHeight())
1635 float LyXText::getCursorX(RowList::iterator rit,
1636 pos_type pos, pos_type last, bool boundary) const
1638 pos_type cursor_vpos = 0;
1640 float fill_separator;
1642 float fill_label_hfill;
1643 // This call HAS to be here because of the BidiTables!!!
1644 prepareToPrint(rit, x, fill_separator, fill_hfill,
1647 ParagraphList::iterator rit_par = rit->par();
1648 pos_type const rit_pos = rit->pos();
1651 cursor_vpos = rit_pos;
1652 else if (pos > last && !boundary)
1653 cursor_vpos = (rit_par->isRightToLeftPar(bv()->buffer()->params))
1654 ? rit_pos : last + 1;
1655 else if (pos > rit_pos && (pos > last || boundary))
1656 /// Place cursor after char at (logical) position pos - 1
1657 cursor_vpos = (bidi_level(pos - 1) % 2 == 0)
1658 ? log2vis(pos - 1) + 1 : log2vis(pos - 1);
1660 /// Place cursor before char at (logical) position pos
1661 cursor_vpos = (bidi_level(pos) % 2 == 0)
1662 ? log2vis(pos) : log2vis(pos) + 1;
1664 pos_type body_pos = rit_par->beginningOfBody();
1665 if ((body_pos > 0) &&
1666 ((body_pos - 1 > last) || !rit_par->isLineSeparator(body_pos - 1)))
1669 for (pos_type vpos = rit_pos; vpos < cursor_vpos; ++vpos) {
1670 pos_type pos = vis2log(vpos);
1671 if (body_pos > 0 && pos == body_pos - 1) {
1672 x += fill_label_hfill +
1673 font_metrics::width(
1674 rit_par->layout()->labelsep,
1675 getLabelFont(bv()->buffer(), rit_par));
1676 if (rit_par->isLineSeparator(body_pos - 1))
1677 x -= singleWidth(rit_par, body_pos - 1);
1680 if (hfillExpansion(*this, rit, pos)) {
1681 x += singleWidth(rit_par, pos);
1682 if (pos >= body_pos)
1685 x += fill_label_hfill;
1686 } else if (rit_par->isSeparator(pos)) {
1687 x += singleWidth(rit_par, pos);
1688 if (pos >= body_pos)
1689 x += fill_separator;
1691 x += singleWidth(rit_par, pos);
1697 void LyXText::setCursorIntern(ParagraphList::iterator pit,
1698 pos_type pos, bool setfont, bool boundary)
1700 UpdatableInset * it = pit->inInset();
1702 if (it != inset_owner) {
1703 lyxerr[Debug::INSETS] << "InsetText is " << it
1705 << "inset_owner is "
1706 << inset_owner << endl;
1707 #ifdef WITH_WARNINGS
1708 #warning I believe this code is wrong. (Lgb)
1709 #warning Jürgen, have a look at this. (Lgb)
1710 #warning Hmmm, I guess you are right but we
1711 #warning should verify when this is needed
1713 // Jürgen, would you like to have a look?
1714 // I guess we need to move the outer cursor
1715 // and open and lock the inset (bla bla bla)
1716 // stuff I don't know... so can you have a look?
1718 // I moved the lyxerr stuff in here so we can see if
1719 // this is actually really needed and where!
1721 // it->getLyXText(bv())->setCursorIntern(bv(), par, pos, setfont, boundary);
1726 setCursor(cursor, pit, pos, boundary);
1732 void LyXText::setCurrentFont()
1734 pos_type pos = cursor.pos();
1735 ParagraphList::iterator pit = cursor.par();
1737 if (cursor.boundary() && pos > 0)
1741 if (pos == pit->size())
1743 else // potentional bug... BUG (Lgb)
1744 if (pit->isSeparator(pos)) {
1745 if (pos > cursorRow()->pos() &&
1746 bidi_level(pos) % 2 ==
1747 bidi_level(pos - 1) % 2)
1749 else if (pos + 1 < pit->size())
1755 pit->getFontSettings(bv()->buffer()->params, pos);
1756 real_current_font = getFont(bv()->buffer(), pit, pos);
1758 if (cursor.pos() == pit->size() &&
1759 isBoundary(bv()->buffer(), *pit, cursor.pos()) &&
1760 !cursor.boundary()) {
1761 Language const * lang =
1762 pit->getParLanguage(bv()->buffer()->params);
1763 current_font.setLanguage(lang);
1764 current_font.setNumber(LyXFont::OFF);
1765 real_current_font.setLanguage(lang);
1766 real_current_font.setNumber(LyXFont::OFF);
1771 // returns the column near the specified x-coordinate of the row
1772 // x is set to the real beginning of this column
1774 LyXText::getColumnNearX(RowList::iterator rit, int & x, bool & boundary) const
1777 float fill_separator;
1779 float fill_label_hfill;
1781 prepareToPrint(rit, tmpx, fill_separator,
1782 fill_hfill, fill_label_hfill);
1784 pos_type vc = rit->pos();
1785 pos_type last = lastPrintablePos(*this, rit);
1788 ParagraphList::iterator rit_par = rit->par();
1789 LyXLayout_ptr const & layout = rit->par()->layout();
1791 bool left_side = false;
1793 pos_type body_pos = rit_par->beginningOfBody();
1794 float last_tmpx = tmpx;
1797 (body_pos - 1 > last ||
1798 !rit_par->isLineSeparator(body_pos - 1)))
1801 // check for empty row
1802 if (!rit_par->size()) {
1807 while (vc <= last && tmpx <= x) {
1810 if (body_pos > 0 && c == body_pos - 1) {
1811 tmpx += fill_label_hfill +
1812 font_metrics::width(layout->labelsep,
1813 getLabelFont(bv()->buffer(), rit_par));
1814 if (rit_par->isLineSeparator(body_pos - 1))
1815 tmpx -= singleWidth(rit_par, body_pos - 1);
1818 if (hfillExpansion(*this, rit, c)) {
1819 tmpx += singleWidth(rit_par, c);
1823 tmpx += fill_label_hfill;
1824 } else if (rit_par->isSeparator(c)) {
1825 tmpx += singleWidth(rit_par, c);
1827 tmpx+= fill_separator;
1829 tmpx += singleWidth(rit_par, c);
1834 if ((tmpx + last_tmpx) / 2 > x) {
1839 if (vc > last + 1) // This shouldn't happen.
1843 // This (rtl_support test) is not needed, but gives
1844 // some speedup if rtl_support=false
1845 RowList::iterator next_rit = boost::next(rit);
1847 bool const lastrow = lyxrc.rtl_support &&
1848 (next_rit == rowlist_.end() ||
1849 next_rit->par() != rit_par);
1851 // If lastrow is false, we don't need to compute
1852 // the value of rtl.
1853 bool const rtl = (lastrow)
1854 ? rit_par->isRightToLeftPar(bv()->buffer()->params)
1857 ((rtl && left_side && vc == rit->pos() && x < tmpx - 5) ||
1858 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5)))
1860 else if (vc == rit->pos()) {
1862 if (bidi_level(c) % 2 == 1)
1865 c = vis2log(vc - 1);
1866 bool const rtl = (bidi_level(c) % 2 == 1);
1867 if (left_side == rtl) {
1869 boundary = isBoundary(bv()->buffer(), *rit_par, c);
1873 if (rit->pos() <= last && c > last
1874 && rit_par->isNewline(last)) {
1875 if (bidi_level(last) % 2 == 0)
1876 tmpx -= singleWidth(rit_par, last);
1878 tmpx += singleWidth(rit_par, last);
1888 void LyXText::setCursorFromCoordinates(int x, int y)
1890 LyXCursor old_cursor = cursor;
1892 setCursorFromCoordinates(cursor, x, y);
1894 deleteEmptyParagraphMechanism(old_cursor);
1901 * return true if the cursor given is at the end of a row,
1902 * and the next row is filled by an inset that spans an entire
1905 bool beforeFullRowInset(LyXText & lt, LyXCursor const & cur)
1907 RowList::iterator row = lt.getRow(cur);
1908 if (boost::next(row) == lt.rows().end())
1911 Row const & next = *boost::next(row);
1913 if (next.pos() != cur.pos() || next.par() != cur.par())
1916 if (cur.pos() == cur.par()->size()
1917 || !cur.par()->isInset(cur.pos()))
1920 Inset const * inset = cur.par()->getInset(cur.pos());
1921 if (inset->needFullRow() || inset->display())
1929 void LyXText::setCursorFromCoordinates(LyXCursor & cur, int x, int y)
1931 // Get the row first.
1933 RowList::iterator row = getRowNearY(y);
1935 pos_type const column = getColumnNearX(row, x, bound);
1936 cur.par(row->par());
1937 cur.pos(row->pos() + column);
1939 cur.y(y + row->baseline());
1941 if (beforeFullRowInset(*this, cur)) {
1942 pos_type const last = lastPrintablePos(*this, row);
1943 RowList::iterator next_row = boost::next(row);
1945 float x = getCursorX(next_row, cur.pos(), last, bound);
1947 cur.iy(y + row->height() + next_row->baseline());
1954 cur.boundary(bound);
1958 void LyXText::cursorLeft(bool internal)
1960 if (cursor.pos() > 0) {
1961 bool boundary = cursor.boundary();
1962 setCursor(cursor.par(), cursor.pos() - 1, true, false);
1963 if (!internal && !boundary &&
1964 isBoundary(bv()->buffer(), *cursor.par(), cursor.pos() + 1))
1965 setCursor(cursor.par(), cursor.pos() + 1, true, true);
1966 } else if (cursor.par() != ownerParagraphs().begin()) { // steps into the above paragraph.
1967 ParagraphList::iterator pit = boost::prior(cursor.par());
1968 setCursor(pit, pit->size());
1973 void LyXText::cursorRight(bool internal)
1975 bool const at_end = (cursor.pos() == cursor.par()->size());
1976 bool const at_newline = !at_end &&
1977 cursor.par()->isNewline(cursor.pos());
1979 if (!internal && cursor.boundary() && !at_newline)
1980 setCursor(cursor.par(), cursor.pos(), true, false);
1982 setCursor(cursor.par(), cursor.pos() + 1, true, false);
1984 isBoundary(bv()->buffer(), *cursor.par(), cursor.pos()))
1985 setCursor(cursor.par(), cursor.pos(), true, true);
1986 } else if (boost::next(cursor.par()) != ownerParagraphs().end())
1987 setCursor(boost::next(cursor.par()), 0);
1991 void LyXText::cursorUp(bool selecting)
1994 int x = cursor.x_fix();
1995 int y = cursor.y() - cursorRow()->baseline() - 1;
1996 setCursorFromCoordinates(x, y);
1999 int y1 = cursor.iy() - topy;
2002 Inset * inset_hit = checkInsetHit(x, y1);
2003 if (inset_hit && isHighlyEditableInset(inset_hit)) {
2004 inset_hit->localDispatch(
2005 FuncRequest(bv(), LFUN_INSET_EDIT, x, y - (y2 - y1), mouse_button::none));
2009 setCursorFromCoordinates(bv(), cursor.x_fix(),
2010 cursor.y() - cursorRow()->baseline() - 1);
2015 void LyXText::cursorDown(bool selecting)
2018 int x = cursor.x_fix();
2019 int y = cursor.y() - cursorRow()->baseline() +
2020 cursorRow()->height() + 1;
2021 setCursorFromCoordinates(x, y);
2022 if (!selecting && cursorRow() == cursor.irow()) {
2024 int y1 = cursor.iy() - topy;
2027 Inset * inset_hit = checkInsetHit(x, y1);
2028 if (inset_hit && isHighlyEditableInset(inset_hit)) {
2029 FuncRequest cmd(bv(), LFUN_INSET_EDIT, x, y - (y2 - y1), mouse_button::none);
2030 inset_hit->localDispatch(cmd);
2034 setCursorFromCoordinates(bv(), cursor.x_fix(),
2035 cursor.y() - cursorRow()->baseline()
2036 + cursorRow()->height() + 1);
2041 void LyXText::cursorUpParagraph()
2043 if (cursor.pos() > 0) {
2044 setCursor(cursor.par(), 0);
2046 else if (cursor.par() != ownerParagraphs().begin()) {
2047 setCursor(boost::prior(cursor.par()), 0);
2052 void LyXText::cursorDownParagraph()
2054 ParagraphList::iterator par = cursor.par();
2055 ParagraphList::iterator next_par = boost::next(par);
2057 if (next_par != ownerParagraphs().end()) {
2058 setCursor(next_par, 0);
2060 setCursor(par, par->size());
2064 // fix the cursor `cur' after a characters has been deleted at `where'
2065 // position. Called by deleteEmptyParagraphMechanism
2066 void LyXText::fixCursorAfterDelete(LyXCursor & cur,
2067 LyXCursor const & where)
2069 // if cursor is not in the paragraph where the delete occured,
2071 if (cur.par() != where.par())
2074 // if cursor position is after the place where the delete occured,
2076 if (cur.pos() > where.pos())
2077 cur.pos(cur.pos()-1);
2079 // check also if we don't want to set the cursor on a spot behind the
2080 // pagragraph because we erased the last character.
2081 if (cur.pos() > cur.par()->size())
2082 cur.pos(cur.par()->size());
2084 // recompute row et al. for this cursor
2085 setCursor(cur, cur.par(), cur.pos(), cur.boundary());
2089 bool LyXText::deleteEmptyParagraphMechanism(LyXCursor const & old_cursor)
2091 // Would be wrong to delete anything if we have a selection.
2092 if (selection.set())
2095 // We allow all kinds of "mumbo-jumbo" when freespacing.
2096 if (old_cursor.par()->layout()->free_spacing
2097 || old_cursor.par()->isFreeSpacing()) {
2101 /* Ok I'll put some comments here about what is missing.
2102 I have fixed BackSpace (and thus Delete) to not delete
2103 double-spaces automagically. I have also changed Cut,
2104 Copy and Paste to hopefully do some sensible things.
2105 There are still some small problems that can lead to
2106 double spaces stored in the document file or space at
2107 the beginning of paragraphs. This happens if you have
2108 the cursor betwenn to spaces and then save. Or if you
2109 cut and paste and the selection have a space at the
2110 beginning and then save right after the paste. I am
2111 sure none of these are very hard to fix, but I will
2112 put out 1.1.4pre2 with FIX_DOUBLE_SPACE defined so
2113 that I can get some feedback. (Lgb)
2116 // If old_cursor.pos() == 0 and old_cursor.pos()(1) == LineSeparator
2117 // delete the LineSeparator.
2120 // If old_cursor.pos() == 1 and old_cursor.pos()(0) == LineSeparator
2121 // delete the LineSeparator.
2124 // If the pos around the old_cursor were spaces, delete one of them.
2125 if (old_cursor.par() != cursor.par()
2126 || old_cursor.pos() != cursor.pos()) {
2127 // Only if the cursor has really moved
2129 if (old_cursor.pos() > 0
2130 && old_cursor.pos() < old_cursor.par()->size()
2131 && old_cursor.par()->isLineSeparator(old_cursor.pos())
2132 && old_cursor.par()->isLineSeparator(old_cursor.pos() - 1)) {
2133 old_cursor.par()->erase(old_cursor.pos() - 1);
2134 redoParagraphs(old_cursor, boost::next(old_cursor.par()));
2136 #ifdef WITH_WARNINGS
2137 #warning This will not work anymore when we have multiple views of the same buffer
2138 // In this case, we will have to correct also the cursors held by
2139 // other bufferviews. It will probably be easier to do that in a more
2140 // automated way in LyXCursor code. (JMarc 26/09/2001)
2142 // correct all cursors held by the LyXText
2143 fixCursorAfterDelete(cursor, old_cursor);
2144 fixCursorAfterDelete(selection.cursor,
2146 fixCursorAfterDelete(selection.start,
2148 fixCursorAfterDelete(selection.end, old_cursor);
2149 fixCursorAfterDelete(last_sel_cursor,
2151 fixCursorAfterDelete(toggle_cursor, old_cursor);
2152 fixCursorAfterDelete(toggle_end_cursor,
2158 // don't delete anything if this is the ONLY paragraph!
2159 if (ownerParagraphs().size() == 1)
2162 // Do not delete empty paragraphs with keepempty set.
2163 if (old_cursor.par()->allowEmpty())
2166 // only do our magic if we changed paragraph
2167 if (old_cursor.par() == cursor.par())
2170 // record if we have deleted a paragraph
2171 // we can't possibly have deleted a paragraph before this point
2172 bool deleted = false;
2174 if (old_cursor.par()->empty() ||
2175 (old_cursor.par()->size() == 1 &&
2176 old_cursor.par()->isLineSeparator(0))) {
2177 // ok, we will delete anything
2178 LyXCursor tmpcursor;
2182 bool selection_position_was_oldcursor_position = (
2183 selection.cursor.par() == old_cursor.par()
2184 && selection.cursor.pos() == old_cursor.pos());
2186 if (getRow(old_cursor) != rows().begin()) {
2188 prevrow = boost::prior(getRow(old_cursor));
2189 postPaint(old_cursor.y() - getRow(old_cursor)->baseline() - prevrow->height());
2191 cursor = old_cursor; // that undo can restore the right cursor position
2192 #warning FIXME. --end() iterator is usable here
2193 ParagraphList::iterator endpit = boost::next(old_cursor.par());
2194 while (endpit != ownerParagraphs().end() &&
2195 endpit->getDepth()) {
2199 setUndo(bv(), Undo::DELETE, old_cursor.par(),
2200 boost::prior(endpit));
2204 removeRow(getRow(old_cursor));
2206 ownerParagraphs().erase(old_cursor.par());
2208 /* Breakagain the next par. Needed because of
2209 * the parindent that can occur or dissappear.
2210 * The next row can change its height, if
2211 * there is another layout before */
2212 RowList::iterator tmprit = boost::next(prevrow);
2213 if (tmprit != rows().end()) {
2217 setHeightOfRow(prevrow);
2219 RowList::iterator nextrow = boost::next(getRow(old_cursor));
2220 postPaint(old_cursor.y() - getRow(old_cursor)->baseline());
2223 cursor = old_cursor; // that undo can restore the right cursor position
2224 #warning FIXME. --end() iterator is usable here
2225 ParagraphList::iterator endpit = boost::next(old_cursor.par());
2226 while (endpit != ownerParagraphs().end() &&
2227 endpit->getDepth()) {
2231 setUndo(bv(), Undo::DELETE, old_cursor.par(), boost::prior(endpit));
2235 removeRow(getRow(old_cursor));
2237 ownerParagraphs().erase(old_cursor.par());
2239 /* Breakagain the next par. Needed because of
2240 the parindent that can occur or dissappear.
2241 The next row can change its height, if
2242 there is another layout before */
2243 if (nextrow != rows().end()) {
2244 breakAgain(nextrow);
2250 setCursorIntern(cursor.par(), cursor.pos());
2252 if (selection_position_was_oldcursor_position) {
2253 // correct selection
2254 selection.cursor = cursor;
2258 if (old_cursor.par()->stripLeadingSpaces()) {
2259 redoParagraphs(old_cursor, boost::next(old_cursor.par()));
2261 setCursorIntern(cursor.par(), cursor.pos());
2262 selection.cursor = cursor;
2269 ParagraphList & LyXText::ownerParagraphs() const
2272 return inset_owner->paragraphs;
2274 return bv_owner->buffer()->paragraphs;
2278 bool LyXText::needRefresh() const
2280 return need_refresh_;
2284 void LyXText::clearPaint()
2286 need_refresh_ = false;
2291 void LyXText::postPaint(int start_y)
2293 bool old = need_refresh_;
2295 need_refresh_ = true;
2297 if (old && refresh_y < start_y)
2300 refresh_y = start_y;
2305 // We are an inset's lyxtext. Tell the top-level lyxtext
2306 // it needs to update the row we're in.
2307 LyXText * t = bv()->text;
2308 t->postRowPaint(t->cursorRow(), t->cursor.y() - t->cursorRow()->baseline());
2312 // FIXME: we should probably remove this y parameter,
2313 // make refresh_y be 0, and use row->y etc.
2314 void LyXText::postRowPaint(RowList::iterator rit, int start_y)
2316 if (need_refresh_ && refresh_y < start_y) {
2317 need_refresh_ = true;
2321 refresh_y = start_y;
2326 need_refresh_ = true;
2331 // We are an inset's lyxtext. Tell the top-level lyxtext
2332 // it needs to update the row we're in.
2333 LyXText * t = bv()->text;
2334 t->postRowPaint(t->cursorRow(), t->cursor.y() - t->cursorRow()->baseline());
2338 bool LyXText::isInInset() const
2340 // Sub-level has non-null bv owner and
2341 // non-null inset owner.
2342 return inset_owner != 0 && bv_owner != 0;
2346 int defaultRowHeight()
2348 LyXFont const font(LyXFont::ALL_SANE);
2349 return int(font_metrics::maxAscent(font)
2350 + font_metrics::maxDescent(font) * 1.5);