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 * ====================================================== */
14 #pragma implementation "lyxtext.h"
19 #include "paragraph.h"
20 #include "lyxtextclasslist.h"
22 #include "undo_funcs.h"
24 #include "bufferparams.h"
26 #include "BufferView.h"
28 #include "CutAndPaste.h"
34 #include "FloatList.h"
36 #include "ParagraphParameters.h"
38 #include "insets/inseterror.h"
39 #include "insets/insetbib.h"
40 #include "insets/insetspecialchar.h"
41 #include "insets/insettext.h"
42 #include "insets/insetfloat.h"
44 #include "support/LAssert.h"
45 #include "support/textutils.h"
46 #include "support/lstrings.h"
57 LyXText::LyXText(BufferView * bv)
58 : number_of_rows(0), height(0), width(0), first_y(0),
59 bv_owner(bv), inset_owner(0), the_locking_inset(0),
60 need_break_row(0), refresh_y(0), refresh_row(0),
61 status_(LyXText::UNCHANGED), firstrow(0), lastrow(0)
65 LyXText::LyXText(InsetText * inset)
66 : number_of_rows(0), height(0), width(0), first_y(0),
67 bv_owner(0), inset_owner(inset), the_locking_inset(0),
68 need_break_row(0), refresh_y(0), refresh_row(0),
69 status_(LyXText::UNCHANGED), firstrow(0), lastrow(0)
73 void LyXText::init(BufferView * bview, bool reinit)
76 // Delete all rows, this does not touch the paragraphs!
77 Row * tmprow = firstrow;
79 tmprow = firstrow->next();
88 copylayouttype.erase();
89 number_of_rows = first_y = refresh_y = 0;
90 status_ = LyXText::UNCHANGED;
94 Paragraph * par = ownerParagraph();
95 current_font = getFont(bview->buffer(), par, 0);
98 insertParagraph(bview, par, lastrow);
101 setCursorIntern(bview, firstrow->par(), 0);
102 selection.cursor = cursor;
108 // Delete all rows, this does not touch the paragraphs!
109 Row * tmprow = firstrow;
111 tmprow = firstrow->next();
120 LyXFont const realizeFont(LyXFont const & font,
124 LyXFont tmpfont(font);
125 Paragraph::depth_type par_depth = par->getDepth();
127 // Resolve against environment font information
128 while (par && par_depth && !tmpfont.resolved()) {
129 par = par->outerHook();
131 #ifndef INHERIT_LANGUAGE
132 tmpfont.realize(textclasslist[buf->params.textclass][
133 par->layout()].font);
135 tmpfont.realize(textclasslist.
136 Style(buf->params.textclass,
138 buf->params.language);
140 par_depth = par->getDepth();
144 #ifndef INHERIT_LANGUAGE
145 tmpfont.realize(textclasslist[buf->params.textclass].defaultfont());
147 tmpfont.realize(textclasslist[buf->params.textclass].defaultfont(),
148 buf->params.language);
157 // Gets the fully instantiated font at a given position in a paragraph
158 // Basically the same routine as Paragraph::getFont() in paragraph.C.
159 // The difference is that this one is used for displaying, and thus we
160 // are allowed to make cosmetic improvements. For instance make footnotes
162 // If position is -1, we get the layout font of the paragraph.
163 // If position is -2, we get the font of the manual label of the paragraph.
164 LyXFont const LyXText::getFont(Buffer const * buf, Paragraph * par,
167 lyx::Assert(pos >= 0);
169 LyXLayout const & layout =
170 textclasslist[buf->params.textclass][par->layout()];
172 Paragraph::depth_type par_depth = par->getDepth();
173 // We specialize the 95% common case:
175 if (layout.labeltype == LABEL_MANUAL
176 && pos < beginningOfMainBody(buf, par)) {
178 LyXFont f = par->getFontSettings(buf->params, pos);
180 par->inInset()->getDrawFont(f);
181 #ifndef INHERIT_LANGUAGE
182 return f.realize(layout.reslabelfont);
184 return f.realize(layout.reslabelfont, buf->params.language);
187 LyXFont f = par->getFontSettings(buf->params, pos);
189 par->inInset()->getDrawFont(f);
190 #ifndef INHERIT_LANGUAGE
191 return f.realize(layout.resfont);
193 return f.realize(layout.resfont, buf->params.language);
198 // The uncommon case need not be optimized as much
202 if (pos < beginningOfMainBody(buf, par)) {
204 layoutfont = layout.labelfont;
207 layoutfont = layout.font;
210 LyXFont tmpfont = par->getFontSettings(buf->params, pos);
211 #ifndef INHERIT_LANGUAGE
212 tmpfont.realize(layoutfont);
214 tmpfont.realize(layoutfont, buf->params.language);
217 par->inInset()->getDrawFont(tmpfont);
219 return realizeFont(tmpfont, buf, par);
223 LyXFont const LyXText::getLayoutFont(Buffer const * buf, Paragraph * par) const
225 LyXLayout const & layout =
226 textclasslist[buf->params.textclass][par->layout()];
228 Paragraph::depth_type par_depth = par->getDepth();
231 return layout.resfont;
234 return realizeFont(layout.font, buf, par);
238 LyXFont const LyXText::getLabelFont(Buffer const * buf, Paragraph * par) const
240 LyXLayout const & layout =
241 textclasslist[buf->params.textclass][par->layout()];
243 Paragraph::depth_type par_depth = par->getDepth();
246 return layout.reslabelfont;
249 return realizeFont(layout.labelfont, buf, par);
253 void LyXText::setCharFont(BufferView * bv, Paragraph * par,
254 pos_type pos, LyXFont const & fnt,
257 Buffer const * buf = bv->buffer();
258 LyXFont font = getFont(buf, par, pos);
259 font.update(fnt, buf->params.language, toggleall);
260 // Let the insets convert their font
261 if (par->isInset(pos)) {
262 Inset * inset = par->getInset(pos);
263 if (isEditableInset(inset)) {
264 UpdatableInset * uinset =
265 static_cast<UpdatableInset *>(inset);
266 uinset->setFont(bv, fnt, toggleall, true);
270 // Plug thru to version below:
271 setCharFont(buf, par, pos, font);
275 void LyXText::setCharFont(Buffer const * buf, Paragraph * par,
276 pos_type pos, LyXFont const & fnt)
280 LyXTextClass const & tclass = textclasslist[buf->params.textclass];
281 LyXLayout const & layout = tclass[par->layout()];
283 // Get concrete layout font to reduce against
286 if (pos < beginningOfMainBody(buf, par))
287 layoutfont = layout.labelfont;
289 layoutfont = layout.font;
291 // Realize against environment font information
292 if (par->getDepth()) {
293 Paragraph * tp = par;
294 while (!layoutfont.resolved() && tp && tp->getDepth()) {
295 tp = tp->outerHook();
297 #ifndef INHERIT_LANGUAGE
298 layoutfont.realize(tclass[tp->layout()].font);
300 layoutfont.realize(textclasslist.
301 Style(buf->params.textclass,
303 buf->params.language);
308 #ifndef INHERIT_LANGUAGE
309 layoutfont.realize(tclass.defaultfont());
311 layoutfont.realize(tclass.defaultfont(), buf->params.language);
314 // Now, reduce font against full layout font
315 font.reduce(layoutfont);
317 par->setFont(pos, font);
321 // inserts a new row behind the specified row, increments
322 // the touched counters
323 void LyXText::insertRow(Row * row, Paragraph * par,
326 Row * tmprow = new Row;
329 tmprow->next(firstrow);
332 tmprow->previous(row);
333 tmprow->next(row->next());
338 tmprow->next()->previous(tmprow);
340 if (tmprow->previous())
341 tmprow->previous()->next(tmprow);
353 // removes the row and reset the touched counters
354 void LyXText::removeRow(Row * row) const
356 Row * row_prev = row->previous();
358 row->next()->previous(row_prev);
360 firstrow = row->next();
361 // lyx::Assert(firstrow);
363 row_prev->next(row->next());
365 if (row == lastrow) {
366 lyx::Assert(!row->next());
369 if (refresh_row == row) {
370 refresh_row = row_prev ? row_prev : row->next();
371 // what about refresh_y, refresh_height
374 height -= row->height(); // the text becomes smaller
377 --number_of_rows; // one row less
381 // remove all following rows of the paragraph of the specified row.
382 void LyXText::removeParagraph(Row * row) const
384 Paragraph * tmppar = row->par();
388 while (row && row->par() == tmppar) {
389 tmprow = row->next();
396 // insert the specified paragraph behind the specified row
397 void LyXText::insertParagraph(BufferView * bview, Paragraph * par,
400 insertRow(row, par, 0); /* insert a new row, starting
403 setCounter(bview->buffer(), par); // set the counters
405 // and now append the whole paragraph behind the new row
408 appendParagraph(bview, firstrow);
410 row->next()->height(0);
411 appendParagraph(bview, row->next());
416 Inset * LyXText::getInset() const
419 if (cursor.pos() == 0 && cursor.par()->bibkey) {
420 inset = cursor.par()->bibkey;
421 } else if (cursor.pos() < cursor.par()->size()
422 && cursor.par()->isInset(cursor.pos())) {
423 inset = cursor.par()->getInset(cursor.pos());
429 void LyXText::toggleInset(BufferView * bview)
431 Inset * inset = getInset();
432 // is there an editable inset at cursor position?
433 if (!isEditableInset(inset)) {
434 // No, try to see if we are inside a collapsable inset
435 if (inset_owner && inset_owner->owner()
436 && inset_owner->owner()->isOpen()) {
437 bview->unlockInset(static_cast<UpdatableInset *>(inset_owner->owner()));
438 inset_owner->owner()->close(bview);
442 //bview->owner()->message(inset->editMessage());
444 // do we want to keep this?? (JMarc)
445 if (!isHighlyEditableInset(inset))
446 setCursorParUndo(bview);
448 if (inset->isOpen()) {
454 inset->open(bview, !inset->isOpen());
459 /* used in setlayout */
460 // Asger is not sure we want to do this...
461 void LyXText::makeFontEntriesLayoutSpecific(Buffer const * buf,
464 LyXLayout const & layout =
465 textclasslist[buf->params.textclass][par->layout()];
468 for (pos_type pos = 0; pos < par->size(); ++pos) {
469 if (pos < beginningOfMainBody(buf, par))
470 layoutfont = layout.labelfont;
472 layoutfont = layout.font;
474 LyXFont tmpfont = par->getFontSettings(buf->params, pos);
475 tmpfont.reduce(layoutfont);
476 par->setFont(pos, tmpfont);
481 Paragraph * LyXText::setLayout(BufferView * bview,
482 LyXCursor & cur, LyXCursor & sstart_cur,
483 LyXCursor & send_cur,
484 string const & layout)
486 Paragraph * endpar = send_cur.par()->next();
487 Paragraph * undoendpar = endpar;
489 if (endpar && endpar->getDepth()) {
490 while (endpar && endpar->getDepth()) {
491 endpar = endpar->next();
495 endpar = endpar->next(); // because of parindents etc.
498 setUndo(bview, Undo::EDIT, sstart_cur.par(), undoendpar);
500 // ok we have a selection. This is always between sstart_cur
501 // and sel_end cursor
504 LyXLayout const & lyxlayout =
505 textclasslist[bview->buffer()->params.textclass][layout];
508 cur.par()->applyLayout(layout);
509 makeFontEntriesLayoutSpecific(bview->buffer(), cur.par());
510 Paragraph * fppar = cur.par();
511 fppar->params().spaceTop(lyxlayout.fill_top ?
512 VSpace(VSpace::VFILL)
513 : VSpace(VSpace::NONE));
514 fppar->params().spaceBottom(lyxlayout.fill_bottom ?
515 VSpace(VSpace::VFILL)
516 : VSpace(VSpace::NONE));
517 if (lyxlayout.margintype == MARGIN_MANUAL)
518 cur.par()->setLabelWidthString(lyxlayout.labelstring());
519 if (lyxlayout.labeltype != LABEL_BIBLIO
521 delete fppar->bibkey;
524 if (cur.par() != send_cur.par())
525 cur.par(cur.par()->next());
526 } while (cur.par() != send_cur.par());
532 // set layout over selection and make a total rebreak of those paragraphs
533 void LyXText::setLayout(BufferView * bview, string const & layout)
535 LyXCursor tmpcursor = cursor; /* store the current cursor */
537 // if there is no selection just set the layout
538 // of the current paragraph */
539 if (!selection.set()) {
540 selection.start = cursor; // dummy selection
541 selection.end = cursor;
543 Paragraph * endpar = setLayout(bview, cursor, selection.start,
544 selection.end, layout);
545 redoParagraphs(bview, selection.start, endpar);
547 // we have to reset the selection, because the
548 // geometry could have changed
549 setCursor(bview, selection.start.par(),
550 selection.start.pos(), false);
551 selection.cursor = cursor;
552 setCursor(bview, selection.end.par(), selection.end.pos(), false);
553 updateCounters(bview, cursor.row());
556 setCursor(bview, tmpcursor.par(), tmpcursor.pos(), true);
560 // increment depth over selection and
561 // make a total rebreak of those paragraphs
562 void LyXText::incDepth(BufferView * bview)
564 // If there is no selection, just use the current paragraph
565 if (!selection.set()) {
566 selection.start = cursor; // dummy selection
567 selection.end = cursor;
570 // We end at the next paragraph with depth 0
571 Paragraph * endpar = selection.end.par()->next();
573 Paragraph * undoendpar = endpar;
575 if (endpar && endpar->getDepth()) {
576 while (endpar && endpar->getDepth()) {
577 endpar = endpar->next();
581 endpar = endpar->next(); // because of parindents etc.
584 setUndo(bview, Undo::EDIT,
585 selection.start.par(), undoendpar);
587 LyXCursor tmpcursor = cursor; // store the current cursor
589 // ok we have a selection. This is always between sel_start_cursor
590 // and sel_end cursor
591 cursor = selection.start;
593 bool anything_changed = false;
595 LyXTextClass const & tclass =
596 textclasslist[bview->buffer()->params.textclass];
599 // NOTE: you can't change the depth of a bibliography entry
600 if (tclass[cursor.par()->layout()].labeltype != LABEL_BIBLIO) {
601 Paragraph * prev = cursor.par()->previous();
604 if (cursor.par()->getDepth()
605 < prev->getMaxDepthAfter(bview->buffer())){
606 cursor.par()->params().depth(cursor.par()->getDepth() + 1);
607 anything_changed = true;
611 if (cursor.par() == selection.end.par())
613 cursor.par(cursor.par()->next());
616 // if nothing changed set all depth to 0
617 if (!anything_changed) {
618 cursor = selection.start;
619 while (cursor.par() != selection.end.par()) {
620 cursor.par()->params().depth(0);
621 cursor.par(cursor.par()->next());
623 cursor.par()->params().depth(0);
626 redoParagraphs(bview, selection.start, endpar);
628 // we have to reset the selection, because the
629 // geometry could have changed
630 setCursor(bview, selection.start.par(), selection.start.pos());
631 selection.cursor = cursor;
632 setCursor(bview, selection.end.par(), selection.end.pos());
633 updateCounters(bview, cursor.row());
636 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
640 // decrement depth over selection and
641 // make a total rebreak of those paragraphs
642 void LyXText::decDepth(BufferView * bview)
644 // if there is no selection just set the layout
645 // of the current paragraph
646 if (!selection.set()) {
647 selection.start = cursor; // dummy selection
648 selection.end = cursor;
650 Paragraph * endpar = selection.end.par()->next();
651 Paragraph * undoendpar = endpar;
653 if (endpar && endpar->getDepth()) {
654 while (endpar && endpar->getDepth()) {
655 endpar = endpar->next();
659 endpar = endpar->next(); // because of parindents etc.
662 setUndo(bview, Undo::EDIT,
663 selection.start.par(), undoendpar);
665 LyXCursor tmpcursor = cursor; // store the current cursor
667 // ok we have a selection. This is always between sel_start_cursor
668 // and sel_end cursor
669 cursor = selection.start;
672 if (cursor.par()->params().depth()) {
673 cursor.par()->params()
674 .depth(cursor.par()->params().depth() - 1);
676 if (cursor.par() == selection.end.par()) {
679 cursor.par(cursor.par()->next());
682 redoParagraphs(bview, selection.start, endpar);
684 // we have to reset the selection, because the
685 // geometry could have changed
686 setCursor(bview, selection.start.par(),
687 selection.start.pos());
688 selection.cursor = cursor;
689 setCursor(bview, selection.end.par(), selection.end.pos());
690 updateCounters(bview, cursor.row());
693 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
697 // set font over selection and make a total rebreak of those paragraphs
698 void LyXText::setFont(BufferView * bview, LyXFont const & font, bool toggleall)
700 // if there is no selection just set the current_font
701 if (!selection.set()) {
702 // Determine basis font
704 if (cursor.pos() < beginningOfMainBody(bview->buffer(),
706 layoutfont = getLabelFont(bview->buffer(),
709 layoutfont = getLayoutFont(bview->buffer(),
712 // Update current font
713 real_current_font.update(font,
714 bview->buffer()->params.language,
717 // Reduce to implicit settings
718 current_font = real_current_font;
719 current_font.reduce(layoutfont);
720 // And resolve it completely
721 #ifndef INHERIT_LANGUAGE
722 real_current_font.realize(layoutfont);
724 real_current_font.realize(layoutfont,
725 bview->buffer()->params.language);
730 LyXCursor tmpcursor = cursor; // store the current cursor
732 // ok we have a selection. This is always between sel_start_cursor
733 // and sel_end cursor
735 setUndo(bview, Undo::EDIT,
736 selection.start.par(), selection.end.par()->next());
738 cursor = selection.start;
739 while (cursor.par() != selection.end.par() ||
740 (cursor.pos() < selection.end.pos()))
742 if (cursor.pos() < cursor.par()->size()) {
743 // an open footnote should behave
745 setCharFont(bview, cursor.par(), cursor.pos(),
747 cursor.pos(cursor.pos() + 1);
750 cursor.par(cursor.par()->next());
755 redoParagraphs(bview, selection.start, selection.end.par()->next());
757 // we have to reset the selection, because the
758 // geometry could have changed, but we keep
759 // it for user convenience
760 setCursor(bview, selection.start.par(), selection.start.pos());
761 selection.cursor = cursor;
762 setCursor(bview, selection.end.par(), selection.end.pos());
764 setCursor(bview, tmpcursor.par(), tmpcursor.pos(), true,
765 tmpcursor.boundary());
769 void LyXText::redoHeightOfParagraph(BufferView * bview, LyXCursor const & cur)
771 Row * tmprow = cur.row();
772 int y = cur.y() - tmprow->baseline();
774 setHeightOfRow(bview, tmprow);
776 while (tmprow->previous()
777 && tmprow->previous()->par() == tmprow->par()) {
778 tmprow = tmprow->previous();
779 y -= tmprow->height();
780 setHeightOfRow(bview, tmprow);
783 // we can set the refreshing parameters now
784 status(bview, LyXText::NEED_MORE_REFRESH);
786 refresh_row = tmprow;
787 setCursor(bview, cur.par(), cur.pos(), false, cursor.boundary());
791 void LyXText::redoDrawingOfParagraph(BufferView * bview, LyXCursor const & cur)
793 Row * tmprow = cur.row();
795 int y = cur.y() - tmprow->baseline();
796 setHeightOfRow(bview, tmprow);
798 while (tmprow->previous()
799 && tmprow->previous()->par() == tmprow->par()) {
800 tmprow = tmprow->previous();
801 y -= tmprow->height();
804 // we can set the refreshing parameters now
805 if (status_ == LyXText::UNCHANGED || y < refresh_y) {
807 refresh_row = tmprow;
809 status(bview, LyXText::NEED_MORE_REFRESH);
810 setCursor(bview, cur.par(), cur.pos());
814 // deletes and inserts again all paragaphs between the cursor
815 // and the specified par
816 // This function is needed after SetLayout and SetFont etc.
817 void LyXText::redoParagraphs(BufferView * bview, LyXCursor const & cur,
818 Paragraph const * endpar) const
821 Paragraph * tmppar = 0;
822 Paragraph * first_phys_par = 0;
824 Row * tmprow = cur.row();
826 int y = cur.y() - tmprow->baseline();
828 if (!tmprow->previous()) {
829 // a trick/hack for UNDO
830 // This is needed because in an UNDO/REDO we could have changed
831 // the ownerParagrah() so the paragraph inside the row is NOT
832 // my really first par anymore. Got it Lars ;) (Jug 20011206)
833 first_phys_par = ownerParagraph();
835 first_phys_par = tmprow->par();
836 while (tmprow->previous()
837 && tmprow->previous()->par() == first_phys_par)
839 tmprow = tmprow->previous();
840 y -= tmprow->height();
844 // we can set the refreshing parameters now
845 status(bview, LyXText::NEED_MORE_REFRESH);
847 refresh_row = tmprow->previous(); /* the real refresh row will
848 be deleted, so I store
852 tmppar = tmprow->next()->par();
855 while (tmprow->next() && tmppar != endpar) {
856 removeRow(tmprow->next());
857 if (tmprow->next()) {
858 tmppar = tmprow->next()->par();
864 // remove the first one
865 tmprow2 = tmprow; /* this is because tmprow->previous()
867 tmprow = tmprow->previous();
870 tmppar = first_phys_par;
874 insertParagraph(bview, tmppar, tmprow);
878 while (tmprow->next()
879 && tmprow->next()->par() == tmppar) {
880 tmprow = tmprow->next();
882 tmppar = tmppar->next();
884 } while (tmppar && tmppar != endpar);
886 // this is because of layout changes
888 refresh_y -= refresh_row->height();
889 setHeightOfRow(bview, refresh_row);
891 refresh_row = firstrow;
893 setHeightOfRow(bview, refresh_row);
896 if (tmprow && tmprow->next())
897 setHeightOfRow(bview, tmprow->next());
901 bool LyXText::fullRebreak(BufferView * bview)
907 if (need_break_row) {
908 breakAgain(bview, need_break_row);
916 // important for the screen
919 /* the cursor set functions have a special mechanism. When they
920 * realize, that you left an empty paragraph, they will delete it.
921 * They also delete the corresponding row */
923 // need the selection cursor:
924 void LyXText::setSelection(BufferView * bview)
926 bool const lsel = selection.set();
928 if (!selection.set()) {
929 last_sel_cursor = selection.cursor;
930 selection.start = selection.cursor;
931 selection.end = selection.cursor;
936 // first the toggling area
937 if (cursor.y() < last_sel_cursor.y()
938 || (cursor.y() == last_sel_cursor.y()
939 && cursor.x() < last_sel_cursor.x())) {
940 toggle_end_cursor = last_sel_cursor;
941 toggle_cursor = cursor;
943 toggle_end_cursor = cursor;
944 toggle_cursor = last_sel_cursor;
947 last_sel_cursor = cursor;
949 // and now the whole selection
951 if (selection.cursor.par() == cursor.par())
952 if (selection.cursor.pos() < cursor.pos()) {
953 selection.end = cursor;
954 selection.start = selection.cursor;
956 selection.end = selection.cursor;
957 selection.start = cursor;
959 else if (selection.cursor.y() < cursor.y() ||
960 (selection.cursor.y() == cursor.y()
961 && selection.cursor.x() < cursor.x())) {
962 selection.end = cursor;
963 selection.start = selection.cursor;
966 selection.end = selection.cursor;
967 selection.start = cursor;
970 // a selection with no contents is not a selection
971 if (selection.start.par() == selection.end.par() &&
972 selection.start.pos() == selection.end.pos())
973 selection.set(false);
975 if (inset_owner && (selection.set() || lsel))
976 inset_owner->setUpdateStatus(bview, InsetText::SELECTION);
980 string const LyXText::selectionAsString(Buffer const * buffer,
983 if (!selection.set()) return string();
986 // Special handling if the whole selection is within one paragraph
987 if (selection.start.par() == selection.end.par()) {
988 result += selection.start.par()->asString(buffer,
989 selection.start.pos(),
995 // The selection spans more than one paragraph
997 // First paragraph in selection
998 result += selection.start.par()->asString(buffer,
999 selection.start.pos(),
1000 selection.start.par()->size(),
1004 // The paragraphs in between (if any)
1005 LyXCursor tmpcur(selection.start);
1006 tmpcur.par(tmpcur.par()->next());
1007 while (tmpcur.par() != selection.end.par()) {
1008 result += tmpcur.par()->asString(buffer, 0,
1009 tmpcur.par()->size(),
1011 tmpcur.par(tmpcur.par()->next());
1014 // Last paragraph in selection
1015 result += selection.end.par()->asString(buffer, 0,
1016 selection.end.pos(), label);
1022 void LyXText::clearSelection() const
1024 selection.set(false);
1025 selection.mark(false);
1026 last_sel_cursor = selection.end = selection.start = selection.cursor = cursor;
1027 // reset this in the bv_owner!
1028 if (bv_owner && bv_owner->text)
1029 bv_owner->text->xsel_cache.set(false);
1033 void LyXText::cursorHome(BufferView * bview) const
1035 setCursor(bview, cursor.par(), cursor.row()->pos());
1039 void LyXText::cursorEnd(BufferView * bview) const
1041 if (!cursor.row()->next()
1042 || cursor.row()->next()->par() != cursor.row()->par()) {
1043 setCursor(bview, cursor.par(), rowLast(cursor.row()) + 1);
1045 if (cursor.par()->size() &&
1046 (cursor.par()->getChar(rowLast(cursor.row())) == ' '
1047 || cursor.par()->isNewline(rowLast(cursor.row())))) {
1048 setCursor(bview, cursor.par(), rowLast(cursor.row()));
1050 setCursor(bview,cursor.par(),
1051 rowLast(cursor.row()) + 1);
1057 void LyXText::cursorTop(BufferView * bview) const
1059 while (cursor.par()->previous())
1060 cursor.par(cursor.par()->previous());
1061 setCursor(bview, cursor.par(), 0);
1065 void LyXText::cursorBottom(BufferView * bview) const
1067 while (cursor.par()->next())
1068 cursor.par(cursor.par()->next());
1069 setCursor(bview, cursor.par(), cursor.par()->size());
1073 void LyXText::toggleFree(BufferView * bview,
1074 LyXFont const & font, bool toggleall)
1076 // If the mask is completely neutral, tell user
1077 if (font == LyXFont(LyXFont::ALL_IGNORE)) {
1078 // Could only happen with user style
1079 bview->owner()->message(_("No font change defined. Use Character under the Layout menu to define font change."));
1083 // Try implicit word selection
1084 // If there is a change in the language the implicit word selection
1086 LyXCursor resetCursor = cursor;
1087 bool implicitSelection = (font.language() == ignore_language
1088 && font.number() == LyXFont::IGNORE)
1089 ? selectWordWhenUnderCursor(bview, WHOLE_WORD_STRICT) : false;
1092 setFont(bview, font, toggleall);
1094 // Implicit selections are cleared afterwards
1095 //and cursor is set to the original position.
1096 if (implicitSelection) {
1098 cursor = resetCursor;
1099 setCursor(bview, cursor.par(), cursor.pos());
1100 selection.cursor = cursor;
1103 inset_owner->setUpdateStatus(bview, InsetText::CURSOR_PAR);
1107 string LyXText::getStringToIndex(BufferView * bview)
1111 // Try implicit word selection
1112 // If there is a change in the language the implicit word selection
1114 LyXCursor resetCursor = cursor;
1115 bool implicitSelection = selectWordWhenUnderCursor(bview, PREVIOUS_WORD);
1117 if (!selection.set()) {
1118 bview->owner()->message(_("Nothing to index!"));
1121 if (selection.start.par() != selection.end.par()) {
1122 bview->owner()->message(_("Cannot index more than one paragraph!"));
1126 idxstring = selectionAsString(bview->buffer(), false);
1128 // Implicit selections are cleared afterwards
1129 //and cursor is set to the original position.
1130 if (implicitSelection) {
1132 cursor = resetCursor;
1133 setCursor(bview, cursor.par(), cursor.pos());
1134 selection.cursor = cursor;
1140 pos_type LyXText::beginningOfMainBody(Buffer const * buf,
1141 Paragraph const * par) const
1143 if (textclasslist[buf->params.textclass][
1144 par->layout()].labeltype != LABEL_MANUAL)
1147 return par->beginningOfMainBody();
1151 /* the DTP switches for paragraphs. LyX will store them in the
1152 * first physicla paragraph. When a paragraph is broken, the top settings
1153 * rest, the bottom settings are given to the new one. So I can make shure,
1154 * they do not duplicate themself and you cannnot make dirty things with
1157 void LyXText::setParagraph(BufferView * bview,
1158 bool line_top, bool line_bottom,
1159 bool pagebreak_top, bool pagebreak_bottom,
1160 VSpace const & space_top,
1161 VSpace const & space_bottom,
1162 Spacing const & spacing,
1164 string labelwidthstring,
1167 LyXCursor tmpcursor = cursor;
1168 if (!selection.set()) {
1169 selection.start = cursor;
1170 selection.end = cursor;
1173 // make sure that the depth behind the selection are restored, too
1174 Paragraph * endpar = selection.end.par()->next();
1175 Paragraph * undoendpar = endpar;
1177 if (endpar && endpar->getDepth()) {
1178 while (endpar && endpar->getDepth()) {
1179 endpar = endpar->next();
1180 undoendpar = endpar;
1184 // because of parindents etc.
1185 endpar = endpar->next();
1188 setUndo(bview, Undo::EDIT, selection.start.par(), undoendpar);
1191 Paragraph * tmppar = selection.end.par();
1192 LyXTextClass const & tclass =
1193 textclasslist[bview->buffer()->params.textclass];
1195 while (tmppar != selection.start.par()->previous()) {
1196 setCursor(bview, tmppar, 0);
1197 status(bview, LyXText::NEED_MORE_REFRESH);
1198 refresh_row = cursor.row();
1199 refresh_y = cursor.y() - cursor.row()->baseline();
1200 cursor.par()->params().lineTop(line_top);
1201 cursor.par()->params().lineBottom(line_bottom);
1202 cursor.par()->params().pagebreakTop(pagebreak_top);
1203 cursor.par()->params().pagebreakBottom(pagebreak_bottom);
1204 cursor.par()->params().spaceTop(space_top);
1205 cursor.par()->params().spaceBottom(space_bottom);
1206 cursor.par()->params().spacing(spacing);
1207 // does the layout allow the new alignment?
1208 LyXLayout const & layout = tclass[cursor.par()->layout()];
1210 if (align == LYX_ALIGN_LAYOUT)
1211 align = layout.align;
1212 if (align & layout.alignpossible) {
1213 if (align == layout.align)
1214 cursor.par()->params().align(LYX_ALIGN_LAYOUT);
1216 cursor.par()->params().align(align);
1218 cursor.par()->setLabelWidthString(labelwidthstring);
1219 cursor.par()->params().noindent(noindent);
1220 tmppar = cursor.par()->previous();
1223 redoParagraphs(bview, selection.start, endpar);
1226 setCursor(bview, selection.start.par(), selection.start.pos());
1227 selection.cursor = cursor;
1228 setCursor(bview, selection.end.par(), selection.end.pos());
1229 setSelection(bview);
1230 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
1232 bview->updateInset(inset_owner, true);
1236 char loweralphaCounter(int n)
1238 if (n < 1 || n > 26)
1248 char alphaCounter(int n)
1250 if (n < 1 || n > 26)
1258 char hebrewCounter(int n)
1260 static const char hebrew[22] = {
1261 'à ', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è',
1262 'é', 'ë', 'ì', 'î', 'ð', 'ñ', 'ò', 'ô', 'ö',
1263 '÷', 'ø', 'ù', 'ú'
1265 if (n < 1 || n > 22)
1273 string const romanCounter(int n)
1275 static char const * roman[20] = {
1276 "i", "ii", "iii", "iv", "v",
1277 "vi", "vii", "viii", "ix", "x",
1278 "xi", "xii", "xiii", "xiv", "xv",
1279 "xvi", "xvii", "xviii", "xix", "xx"
1281 if (n < 1 || n > 20)
1290 // set the counter of a paragraph. This includes the labels
1291 void LyXText::setCounter(Buffer const * buf, Paragraph * par) const
1293 LyXTextClass const & textclass = textclasslist[buf->params.textclass];
1294 LyXLayout const & layout = textclass[par->layout()];
1297 // copy the prev-counters to this one,
1298 // unless this is the first paragraph
1299 if (par->previous()) {
1300 for (int i = 0; i < 10; ++i) {
1301 par->setCounter(i, par->previous()->getFirstCounter(i));
1303 par->params().appendix(par->previous()->params().appendix());
1304 if (!par->params().appendix() && par->params().startOfAppendix()) {
1305 par->params().appendix(true);
1306 for (int i = 0; i < 10; ++i) {
1307 par->setCounter(i, 0);
1310 par->enumdepth = par->previous()->enumdepth;
1311 par->itemdepth = par->previous()->itemdepth;
1313 for (int i = 0; i < 10; ++i) {
1314 par->setCounter(i, 0);
1316 par->params().appendix(par->params().startOfAppendix());
1321 /* Maybe we have to increment the enumeration depth.
1322 * BUT, enumeration in a footnote is considered in isolation from its
1323 * surrounding paragraph so don't increment if this is the
1324 * first line of the footnote
1325 * AND, bibliographies can't have their depth changed ie. they
1326 * are always of depth 0
1329 && par->previous()->getDepth() < par->getDepth()
1330 && textclass[par->previous()->layout()].labeltype == LABEL_COUNTER_ENUMI
1331 && par->enumdepth < 3
1332 && layout.labeltype != LABEL_BIBLIO) {
1336 // Maybe we have to decrement the enumeration depth, see note above
1338 && par->previous()->getDepth() > par->getDepth()
1339 && layout.labeltype != LABEL_BIBLIO) {
1340 par->enumdepth = par->depthHook(par->getDepth())->enumdepth;
1341 par->setCounter(6 + par->enumdepth,
1342 par->depthHook(par->getDepth())->getCounter(6 + par->enumdepth));
1343 /* reset the counters.
1344 * A depth change is like a breaking layout
1346 for (int i = 6 + par->enumdepth + 1; i < 10; ++i)
1347 par->setCounter(i, 0);
1350 if (!par->params().labelString().empty()) {
1351 par->params().labelString(string());
1354 if (layout.margintype == MARGIN_MANUAL) {
1355 if (par->params().labelWidthString().empty()) {
1356 par->setLabelWidthString(layout.labelstring());
1359 par->setLabelWidthString(string());
1362 // is it a layout that has an automatic label?
1363 if (layout.labeltype >= LABEL_COUNTER_CHAPTER) {
1365 int i = layout.labeltype - LABEL_COUNTER_CHAPTER;
1366 if (i >= 0 && i<= buf->params.secnumdepth) {
1367 par->incCounter(i); // increment the counter
1369 // Is there a label? Useful for Chapter layout
1370 if (!par->params().appendix()) {
1371 if (!layout.labelstring().empty())
1372 par->params().labelString(layout.labelstring());
1374 par->params().labelString(string());
1376 if (!layout.labelstring_appendix().empty())
1377 par->params().labelString(layout.labelstring_appendix());
1379 par->params().labelString(string());
1384 if (!par->params().appendix()) {
1385 switch (2 * LABEL_COUNTER_CHAPTER -
1386 textclass.maxcounter() + i) {
1387 case LABEL_COUNTER_CHAPTER:
1388 s << par->getCounter(i);
1390 case LABEL_COUNTER_SECTION:
1391 s << par->getCounter(i - 1) << '.'
1392 << par->getCounter(i);
1394 case LABEL_COUNTER_SUBSECTION:
1395 s << par->getCounter(i - 2) << '.'
1396 << par->getCounter(i - 1) << '.'
1397 << par->getCounter(i);
1399 case LABEL_COUNTER_SUBSUBSECTION:
1400 s << par->getCounter(i - 3) << '.'
1401 << par->getCounter(i - 2) << '.'
1402 << par->getCounter(i - 1) << '.'
1403 << par->getCounter(i);
1406 case LABEL_COUNTER_PARAGRAPH:
1407 s << par->getCounter(i - 4) << '.'
1408 << par->getCounter(i - 3) << '.'
1409 << par->getCounter(i - 2) << '.'
1410 << par->getCounter(i - 1) << '.'
1411 << par->getCounter(i);
1413 case LABEL_COUNTER_SUBPARAGRAPH:
1414 s << par->getCounter(i - 5) << '.'
1415 << par->getCounter(i - 4) << '.'
1416 << par->getCounter(i - 3) << '.'
1417 << par->getCounter(i - 2) << '.'
1418 << par->getCounter(i - 1) << '.'
1419 << par->getCounter(i);
1423 // Can this ever be reached? And in the
1424 // case it is, how can this be correct?
1426 s << par->getCounter(i) << '.';
1429 } else { // appendix
1430 switch (2 * LABEL_COUNTER_CHAPTER - textclass.maxcounter() + i) {
1431 case LABEL_COUNTER_CHAPTER:
1432 if (par->isRightToLeftPar(buf->params))
1433 s << hebrewCounter(par->getCounter(i));
1435 s << alphaCounter(par->getCounter(i));
1437 case LABEL_COUNTER_SECTION:
1438 if (par->isRightToLeftPar(buf->params))
1439 s << hebrewCounter(par->getCounter(i - 1));
1441 s << alphaCounter(par->getCounter(i - 1));
1444 << par->getCounter(i);
1447 case LABEL_COUNTER_SUBSECTION:
1448 if (par->isRightToLeftPar(buf->params))
1449 s << hebrewCounter(par->getCounter(i - 2));
1451 s << alphaCounter(par->getCounter(i - 2));
1454 << par->getCounter(i-1) << '.'
1455 << par->getCounter(i);
1458 case LABEL_COUNTER_SUBSUBSECTION:
1459 if (par->isRightToLeftPar(buf->params))
1460 s << hebrewCounter(par->getCounter(i-3));
1462 s << alphaCounter(par->getCounter(i-3));
1465 << par->getCounter(i-2) << '.'
1466 << par->getCounter(i-1) << '.'
1467 << par->getCounter(i);
1470 case LABEL_COUNTER_PARAGRAPH:
1471 if (par->isRightToLeftPar(buf->params))
1472 s << hebrewCounter(par->getCounter(i-4));
1474 s << alphaCounter(par->getCounter(i-4));
1477 << par->getCounter(i-3) << '.'
1478 << par->getCounter(i-2) << '.'
1479 << par->getCounter(i-1) << '.'
1480 << par->getCounter(i);
1483 case LABEL_COUNTER_SUBPARAGRAPH:
1484 if (par->isRightToLeftPar(buf->params))
1485 s << hebrewCounter(par->getCounter(i-5));
1487 s << alphaCounter(par->getCounter(i-5));
1490 << par->getCounter(i-4) << '.'
1491 << par->getCounter(i-3) << '.'
1492 << par->getCounter(i-2) << '.'
1493 << par->getCounter(i-1) << '.'
1494 << par->getCounter(i);
1498 // Can this ever be reached? And in the
1499 // case it is, how can this be correct?
1501 s << par->getCounter(i) << '.';
1507 par->params().labelString(par->params().labelString() +s.str().c_str());
1508 // We really want to remove the c_str as soon as
1511 for (i++; i < 10; ++i) {
1512 // reset the following counters
1513 par->setCounter(i, 0);
1515 } else if (layout.labeltype < LABEL_COUNTER_ENUMI) {
1516 for (i++; i < 10; ++i) {
1517 // reset the following counters
1518 par->setCounter(i, 0);
1520 } else if (layout.labeltype == LABEL_COUNTER_ENUMI) {
1521 par->incCounter(i + par->enumdepth);
1522 int number = par->getCounter(i + par->enumdepth);
1526 switch (par->enumdepth) {
1528 if (par->isRightToLeftPar(buf->params))
1530 << hebrewCounter(number)
1534 << loweralphaCounter(number)
1538 if (par->isRightToLeftPar(buf->params))
1539 s << '.' << romanCounter(number);
1541 s << romanCounter(number) << '.';
1544 if (par->isRightToLeftPar(buf->params))
1546 << alphaCounter(number);
1548 s << alphaCounter(number)
1552 if (par->isRightToLeftPar(buf->params))
1559 par->params().labelString(s.str().c_str());
1561 for (i += par->enumdepth + 1; i < 10; ++i) {
1562 // reset the following counters
1563 par->setCounter(i, 0);
1567 } else if (layout.labeltype == LABEL_BIBLIO) {// ale970302
1568 int i = LABEL_COUNTER_ENUMI - LABEL_COUNTER_CHAPTER + par->enumdepth;
1570 int number = par->getCounter(i);
1572 InsetCommandParams p("bibitem" );
1573 par->bibkey = new InsetBibKey(p);
1575 par->bibkey->setCounter(number);
1576 par->params().labelString(layout.labelstring());
1578 // In biblio should't be following counters but...
1580 string s = layout.labelstring();
1582 // the caption hack:
1583 if (layout.labeltype == LABEL_SENSITIVE) {
1584 bool isOK (par->inInset() && par->inInset()->owner() &&
1585 (par->inInset()->owner()->lyxCode() == Inset::FLOAT_CODE));
1588 InsetFloat * tmp = static_cast<InsetFloat*>(par->inInset()->owner());
1590 = floatList.getType(tmp->type());
1591 // We should get the correct number here too.
1592 s = fl.name() + " #:";
1594 /* par->SetLayout(0);
1595 s = layout->labelstring; */
1596 s = (par->getParLanguage(buf->params)->lang() == "hebrew")
1597 ? " :úåòîùî øñç" : "Senseless: ";
1600 par->params().labelString(s);
1602 /* reset the enumeration counter. They are always resetted
1603 * when there is any other layout between */
1604 for (int i = 6 + par->enumdepth; i < 10; ++i)
1605 par->setCounter(i, 0);
1610 // Updates all counters BEHIND the row. Changed paragraphs
1611 // with a dynamic left margin will be rebroken.
1612 void LyXText::updateCounters(BufferView * bview, Row * row) const
1620 par = row->par()->next();
1624 while (row->par() != par)
1627 setCounter(bview->buffer(), par);
1629 // now check for the headline layouts. remember that they
1630 // have a dynamic left margin
1631 LyXTextClass const & tclass =
1632 textclasslist[bview->buffer()->params.textclass];
1633 LyXLayout const & layout = tclass[par->layout()];
1635 if (layout.margintype == MARGIN_DYNAMIC
1636 || layout.labeltype == LABEL_SENSITIVE) {
1637 // Rebreak the paragraph
1638 removeParagraph(row);
1639 appendParagraph(bview, row);
1646 void LyXText::insertInset(BufferView * bview, Inset * inset)
1648 if (!cursor.par()->insetAllowed(inset->lyxCode()))
1650 // I don't know if this is necessary here (Jug 20020102)
1651 setUndo(bview, Undo::INSERT, cursor.par(), cursor.par()->next());
1652 cursor.par()->insertInset(cursor.pos(), inset);
1653 // Just to rebreak and refresh correctly.
1654 // The character will not be inserted a second time
1655 insertChar(bview, Paragraph::META_INSET);
1657 // If we enter a highly editable inset the cursor should be to before
1658 // the inset. This couldn't happen before as Undo was not handled inside
1659 // inset now after the Undo LyX tries to call inset->Edit(...) again
1660 // and cannot do this as the cursor is behind the inset and GetInset
1661 // does not return the inset!
1662 if (isHighlyEditableInset(inset)) {
1663 cursorLeft(bview, true);
1669 void LyXText::copyEnvironmentType()
1671 copylayouttype = cursor.par()->layout();
1675 void LyXText::pasteEnvironmentType(BufferView * bview)
1677 setLayout(bview, copylayouttype);
1681 void LyXText::cutSelection(BufferView * bview, bool doclear, bool realcut)
1683 // Stuff what we got on the clipboard. Even if there is no selection.
1685 // There is a problem with having the stuffing here in that the
1686 // larger the selection the slower LyX will get. This can be
1687 // solved by running the line below only when the selection has
1688 // finished. The solution used currently just works, to make it
1689 // faster we need to be more clever and probably also have more
1690 // calls to stuffClipboard. (Lgb)
1691 bview->stuffClipboard(selectionAsString(bview->buffer(), true));
1693 // This doesn't make sense, if there is no selection
1694 if (!selection.set())
1697 // OK, we have a selection. This is always between selection.start
1698 // and selection.end
1700 // make sure that the depth behind the selection are restored, too
1701 Paragraph * endpar = selection.end.par()->next();
1702 Paragraph * undoendpar = endpar;
1704 if (endpar && endpar->getDepth()) {
1705 while (endpar && endpar->getDepth()) {
1706 endpar = endpar->next();
1707 undoendpar = endpar;
1709 } else if (endpar) {
1710 endpar = endpar->next(); // because of parindents etc.
1713 setUndo(bview, Undo::DELETE,
1714 selection.start.par(), undoendpar);
1716 // there are two cases: cut only within one paragraph or
1717 // more than one paragraph
1718 if (selection.start.par() == selection.end.par()) {
1719 // only within one paragraph
1720 endpar = selection.end.par();
1721 int pos = selection.end.pos();
1722 CutAndPaste::cutSelection(selection.start.par(), &endpar,
1723 selection.start.pos(), pos,
1724 bview->buffer()->params.textclass,
1726 selection.end.pos(pos);
1728 endpar = selection.end.par();
1729 int pos = selection.end.pos();
1730 CutAndPaste::cutSelection(selection.start.par(), &endpar,
1731 selection.start.pos(), pos,
1732 bview->buffer()->params.textclass,
1735 selection.end.par(endpar);
1736 selection.end.pos(pos);
1737 cursor.pos(selection.end.pos());
1739 endpar = endpar->next();
1741 // sometimes necessary
1743 selection.start.par()->stripLeadingSpaces(bview->buffer()->params.textclass);
1745 redoParagraphs(bview, selection.start, endpar);
1747 // cutSelection can invalidate the cursor so we need to set
1749 cursor = selection.start;
1751 // need a valid cursor. (Lgb)
1754 setCursor(bview, cursor.par(), cursor.pos());
1755 selection.cursor = cursor;
1756 updateCounters(bview, cursor.row());
1760 void LyXText::copySelection(BufferView * bview)
1762 // stuff the selection onto the X clipboard, from an explicit copy request
1763 bview->stuffClipboard(selectionAsString(bview->buffer(), true));
1765 // this doesnt make sense, if there is no selection
1766 if (!selection.set())
1769 // ok we have a selection. This is always between selection.start
1770 // and sel_end cursor
1772 // copy behind a space if there is one
1773 while (selection.start.par()->size() > selection.start.pos()
1774 && selection.start.par()->isLineSeparator(selection.start.pos())
1775 && (selection.start.par() != selection.end.par()
1776 || selection.start.pos() < selection.end.pos()))
1777 selection.start.pos(selection.start.pos() + 1);
1779 CutAndPaste::copySelection(selection.start.par(), selection.end.par(),
1780 selection.start.pos(), selection.end.pos(),
1781 bview->buffer()->params.textclass);
1785 void LyXText::pasteSelection(BufferView * bview)
1787 // this does not make sense, if there is nothing to paste
1788 if (!CutAndPaste::checkPastePossible(cursor.par()))
1791 setUndo(bview, Undo::INSERT,
1792 cursor.par(), cursor.par()->next());
1795 Paragraph * actpar = cursor.par();
1796 int pos = cursor.pos();
1798 CutAndPaste::pasteSelection(&actpar, &endpar, pos,
1799 bview->buffer()->params.textclass);
1801 redoParagraphs(bview, cursor, endpar);
1803 setCursor(bview, cursor.par(), cursor.pos());
1806 selection.cursor = cursor;
1807 setCursor(bview, actpar, pos);
1808 setSelection(bview);
1809 updateCounters(bview, cursor.row());
1813 // sets the selection over the number of characters of string, no check!!
1814 void LyXText::setSelectionOverString(BufferView * bview, string const & str)
1819 selection.cursor = cursor;
1820 for (string::size_type i = 0; i < str.length(); ++i)
1822 setSelection(bview);
1826 // simple replacing. The font of the first selected character is used
1827 void LyXText::replaceSelectionWithString(BufferView * bview,
1830 setCursorParUndo(bview);
1833 if (!selection.set()) { // create a dummy selection
1834 selection.end = cursor;
1835 selection.start = cursor;
1838 // Get font setting before we cut
1839 pos_type pos = selection.end.pos();
1840 LyXFont const font = selection.start.par()
1841 ->getFontSettings(bview->buffer()->params,
1842 selection.start.pos());
1844 // Insert the new string
1845 for (string::const_iterator cit = str.begin(); cit != str.end(); ++cit) {
1846 selection.end.par()->insertChar(pos, (*cit), font);
1850 // Cut the selection
1851 cutSelection(bview, true, false);
1857 // needed to insert the selection
1858 void LyXText::insertStringAsLines(BufferView * bview, string const & str)
1860 Paragraph * par = cursor.par();
1861 pos_type pos = cursor.pos();
1862 Paragraph * endpar = cursor.par()->next();
1864 setCursorParUndo(bview);
1866 // only to be sure, should not be neccessary
1869 bview->buffer()->insertStringAsLines(par, pos, current_font, str);
1871 redoParagraphs(bview, cursor, endpar);
1872 setCursor(bview, cursor.par(), cursor.pos());
1873 selection.cursor = cursor;
1874 setCursor(bview, par, pos);
1875 setSelection(bview);
1879 // turns double-CR to single CR, others where converted into one
1880 // blank. Then InsertStringAsLines is called
1881 void LyXText::insertStringAsParagraphs(BufferView * bview, string const & str)
1883 string linestr(str);
1884 bool newline_inserted = false;
1885 for (string::size_type i = 0; i < linestr.length(); ++i) {
1886 if (linestr[i] == '\n') {
1887 if (newline_inserted) {
1888 // we know that \r will be ignored by
1889 // InsertStringA. Of course, it is a dirty
1890 // trick, but it works...
1891 linestr[i - 1] = '\r';
1895 newline_inserted = true;
1897 } else if (IsPrintable(linestr[i])) {
1898 newline_inserted = false;
1901 insertStringAsLines(bview, linestr);
1905 bool LyXText::gotoNextInset(BufferView * bview,
1906 vector<Inset::Code> const & codes,
1907 string const & contents) const
1909 LyXCursor res = cursor;
1912 if (res.pos() < res.par()->size() - 1) {
1913 res.pos(res.pos() + 1);
1915 res.par(res.par()->next());
1919 } while (res.par() &&
1920 !(res.par()->isInset(res.pos())
1921 && (inset = res.par()->getInset(res.pos())) != 0
1922 && find(codes.begin(), codes.end(), inset->lyxCode())
1924 && (contents.empty() ||
1925 static_cast<InsetCommand *>(res.par()->getInset(res.pos()))->getContents()
1929 setCursor(bview, res.par(), res.pos(), false);
1936 void LyXText::checkParagraph(BufferView * bview, Paragraph * par,
1939 LyXCursor tmpcursor;
1943 Row * row = getRow(par, pos, y);
1945 // is there a break one row above
1946 if (row->previous() && row->previous()->par() == row->par()) {
1947 z = nextBreakPoint(bview, row->previous(), workWidth(bview));
1948 if (z >= row->pos()) {
1949 // set the dimensions of the row above
1950 y -= row->previous()->height();
1952 refresh_row = row->previous();
1953 status(bview, LyXText::NEED_MORE_REFRESH);
1955 breakAgain(bview, row->previous());
1957 // set the cursor again. Otherwise
1958 // dangling pointers are possible
1959 setCursor(bview, cursor.par(), cursor.pos(),
1960 false, cursor.boundary());
1961 selection.cursor = cursor;
1966 int const tmpheight = row->height();
1967 pos_type const tmplast = rowLast(row);
1971 breakAgain(bview, row);
1972 if (row->height() == tmpheight && rowLast(row) == tmplast)
1973 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
1975 status(bview, LyXText::NEED_MORE_REFRESH);
1977 // check the special right address boxes
1978 if (textclasslist[bview->buffer()->params.textclass][
1979 par->layout()].margintype
1980 == MARGIN_RIGHT_ADDRESS_BOX)
1988 redoDrawingOfParagraph(bview, tmpcursor);
1991 // set the cursor again. Otherwise dangling pointers are possible
1992 // also set the selection
1994 if (selection.set()) {
1996 setCursorIntern(bview, selection.cursor.par(), selection.cursor.pos(),
1997 false, selection.cursor.boundary());
1998 selection.cursor = cursor;
1999 setCursorIntern(bview, selection.start.par(),
2000 selection.start.pos(),
2001 false, selection.start.boundary());
2002 selection.start = cursor;
2003 setCursorIntern(bview, selection.end.par(),
2004 selection.end.pos(),
2005 false, selection.end.boundary());
2006 selection.end = cursor;
2007 setCursorIntern(bview, last_sel_cursor.par(),
2008 last_sel_cursor.pos(),
2009 false, last_sel_cursor.boundary());
2010 last_sel_cursor = cursor;
2013 setCursorIntern(bview, cursor.par(), cursor.pos(),
2014 false, cursor.boundary());
2018 // returns false if inset wasn't found
2019 bool LyXText::updateInset(BufferView * bview, Inset * inset)
2021 // first check the current paragraph
2022 int pos = cursor.par()->getPositionOfInset(inset);
2024 checkParagraph(bview, cursor.par(), pos);
2028 // check every paragraph
2030 Paragraph * par = ownerParagraph();
2032 pos = par->getPositionOfInset(inset);
2034 checkParagraph(bview, par, pos);
2044 bool LyXText::setCursor(BufferView * bview, Paragraph * par,
2046 bool setfont, bool boundary) const
2048 LyXCursor old_cursor = cursor;
2049 setCursorIntern(bview, par, pos, setfont, boundary);
2050 return deleteEmptyParagraphMechanism(bview, old_cursor);
2054 void LyXText::setCursor(BufferView * bview, LyXCursor & cur, Paragraph * par,
2055 pos_type pos, bool boundary) const
2062 cur.boundary(boundary);
2065 if (pos && par->getChar(pos) == Paragraph::META_INSET &&
2066 par->getInset(pos)) {
2067 Inset * ins = par->getInset(pos);
2068 if (ins->needFullRow() || ins->display()) {
2075 // get the cursor y position in text
2077 Row * row = getRow(par, pos, y);
2078 // y is now the beginning of the cursor row
2079 y += row->baseline();
2080 // y is now the cursor baseline
2083 // now get the cursors x position
2085 float fill_separator;
2087 float fill_label_hfill;
2088 prepareToPrint(bview, row, x, fill_separator, fill_hfill,
2090 pos_type cursor_vpos = 0;
2091 pos_type last = rowLastPrintable(row);
2093 if (pos > last + 1) {
2094 // This shouldn't happen.
2097 } else if (pos < row->pos()) {
2102 if (last < row->pos())
2103 cursor_vpos = row->pos();
2104 else if (pos > last && !boundary)
2105 cursor_vpos = (row->par()->isRightToLeftPar(bview->buffer()->params))
2106 ? row->pos() : last + 1;
2107 else if (pos > row->pos() &&
2108 (pos > last || boundary))
2109 /// Place cursor after char at (logical) position pos - 1
2110 cursor_vpos = (bidi_level(pos - 1) % 2 == 0)
2111 ? log2vis(pos - 1) + 1 : log2vis(pos - 1);
2113 /// Place cursor before char at (logical) position pos
2114 cursor_vpos = (bidi_level(pos) % 2 == 0)
2115 ? log2vis(pos) : log2vis(pos) + 1;
2117 pos_type main_body =
2118 beginningOfMainBody(bview->buffer(), row->par());
2119 if ((main_body > 0) &&
2120 ((main_body-1 > last) ||
2121 !row->par()->isLineSeparator(main_body-1)))
2124 for (pos_type vpos = row->pos(); vpos < cursor_vpos; ++vpos) {
2125 pos = vis2log(vpos);
2126 if (main_body > 0 && pos == main_body - 1) {
2127 x += fill_label_hfill +
2128 lyxfont::width(textclasslist[
2129 bview->buffer()->params.textclass][
2130 row->par()->layout()]
2132 getLabelFont(bview->buffer(), row->par()));
2133 if (row->par()->isLineSeparator(main_body-1))
2134 x -= singleWidth(bview, row->par(),main_body-1);
2136 if (hfillExpansion(bview->buffer(), row, pos)) {
2137 x += singleWidth(bview, row->par(), pos);
2138 if (pos >= main_body)
2141 x += fill_label_hfill;
2142 } else if (row->par()->isSeparator(pos)) {
2143 x += singleWidth(bview, row->par(), pos);
2144 if (pos >= main_body)
2145 x += fill_separator;
2147 x += singleWidth(bview, row->par(), pos);
2156 void LyXText::setCursorIntern(BufferView * bview, Paragraph * par,
2157 pos_type pos, bool setfont, bool boundary) const
2159 InsetText * it = static_cast<InsetText *>(par->inInset());
2161 if (it != inset_owner) {
2162 lyxerr[Debug::INSETS] << "InsetText is " << it
2164 << "inset_owner is "
2165 << inset_owner << endl;
2166 #ifdef WITH_WARNINGS
2167 #warning I believe this code is wrong. (Lgb)
2168 #warning Jürgen, have a look at this. (Lgb)
2169 #warning Hmmm, I guess you are right but we
2170 #warning should verify when this is needed
2172 // Jürgen, would you like to have a look?
2173 // I guess we need to move the outer cursor
2174 // and open and lock the inset (bla bla bla)
2175 // stuff I don't know... so can you have a look?
2177 // I moved the lyxerr stuff in here so we can see if
2178 // this is actually really needed and where!
2180 // it->getLyXText(bview)->setCursorIntern(bview, par, pos, setfont, boundary);
2185 setCursor(bview, cursor, par, pos, boundary);
2187 setCurrentFont(bview);
2191 void LyXText::setCurrentFont(BufferView * bview) const
2193 pos_type pos = cursor.pos();
2194 if (cursor.boundary() && pos > 0)
2198 if (pos == cursor.par()->size())
2200 else // potentional bug... BUG (Lgb)
2201 if (cursor.par()->isSeparator(pos)) {
2202 if (pos > cursor.row()->pos() &&
2203 bidi_level(pos) % 2 ==
2204 bidi_level(pos - 1) % 2)
2206 else if (pos + 1 < cursor.par()->size())
2212 cursor.par()->getFontSettings(bview->buffer()->params, pos);
2213 real_current_font = getFont(bview->buffer(), cursor.par(), pos);
2215 if (cursor.pos() == cursor.par()->size() &&
2216 isBoundary(bview->buffer(), cursor.par(), cursor.pos()) &&
2217 !cursor.boundary()) {
2218 Language const * lang =
2219 cursor.par()->getParLanguage(bview->buffer()->params);
2220 current_font.setLanguage(lang);
2221 current_font.setNumber(LyXFont::OFF);
2222 real_current_font.setLanguage(lang);
2223 real_current_font.setNumber(LyXFont::OFF);
2228 void LyXText::setCursorFromCoordinates(BufferView * bview, int x, int y) const
2230 LyXCursor old_cursor = cursor;
2232 setCursorFromCoordinates(bview, cursor, x, y);
2233 setCurrentFont(bview);
2234 deleteEmptyParagraphMechanism(bview, old_cursor);
2238 void LyXText::setCursorFromCoordinates(BufferView * bview, LyXCursor & cur,
2241 // Get the row first.
2243 Row * row = getRowNearY(y);
2245 pos_type const column = getColumnNearX(bview, row, x, bound);
2246 cur.par(row->par());
2247 cur.pos(row->pos() + column);
2249 cur.y(y + row->baseline());
2251 cur.boundary(bound);
2255 void LyXText::cursorLeft(BufferView * bview, bool internal) const
2257 if (cursor.pos() > 0) {
2258 bool boundary = cursor.boundary();
2259 setCursor(bview, cursor.par(), cursor.pos() - 1, true, false);
2260 if (!internal && !boundary &&
2261 isBoundary(bview->buffer(), cursor.par(), cursor.pos() + 1))
2262 setCursor(bview, cursor.par(), cursor.pos() + 1, true, true);
2263 } else if (cursor.par()->previous()) { // steps into the above paragraph.
2264 Paragraph * par = cursor.par()->previous();
2265 setCursor(bview, par, par->size());
2270 void LyXText::cursorRight(BufferView * bview, bool internal) const
2272 if (!internal && cursor.boundary() &&
2273 !cursor.par()->isNewline(cursor.pos()))
2274 setCursor(bview, cursor.par(), cursor.pos(), true, false);
2275 else if (cursor.pos() < cursor.par()->size()) {
2276 setCursor(bview, cursor.par(), cursor.pos() + 1, true, false);
2278 isBoundary(bview->buffer(), cursor.par(), cursor.pos()))
2279 setCursor(bview, cursor.par(), cursor.pos(), true, true);
2280 } else if (cursor.par()->next())
2281 setCursor(bview, cursor.par()->next(), 0);
2285 void LyXText::cursorUp(BufferView * bview) const
2287 setCursorFromCoordinates(bview, cursor.x_fix(),
2288 cursor.y() - cursor.row()->baseline() - 1);
2292 void LyXText::cursorDown(BufferView * bview) const
2294 setCursorFromCoordinates(bview, cursor.x_fix(),
2295 cursor.y() - cursor.row()->baseline()
2296 + cursor.row()->height() + 1);
2300 void LyXText::cursorUpParagraph(BufferView * bview) const
2302 if (cursor.pos() > 0) {
2303 setCursor(bview, cursor.par(), 0);
2305 else if (cursor.par()->previous()) {
2306 setCursor(bview, cursor.par()->previous(), 0);
2311 void LyXText::cursorDownParagraph(BufferView * bview) const
2313 if (cursor.par()->next()) {
2314 setCursor(bview, cursor.par()->next(), 0);
2316 setCursor(bview, cursor.par(), cursor.par()->size());
2320 // fix the cursor `cur' after a characters has been deleted at `where'
2321 // position. Called by deleteEmptyParagraphMechanism
2322 void LyXText::fixCursorAfterDelete(BufferView * bview,
2324 LyXCursor const & where) const
2326 // if cursor is not in the paragraph where the delete occured,
2328 if (cur.par() != where.par())
2331 // if cursor position is after the place where the delete occured,
2333 if (cur.pos() > where.pos())
2334 cur.pos(cur.pos()-1);
2336 // recompute row et al. for this cursor
2337 setCursor(bview, cur, cur.par(), cur.pos(), cur.boundary());
2341 bool LyXText::deleteEmptyParagraphMechanism(BufferView * bview,
2342 LyXCursor const & old_cursor) const
2344 // Would be wrong to delete anything if we have a selection.
2345 if (selection.set())
2348 // We allow all kinds of "mumbo-jumbo" when freespacing.
2349 if (textclasslist[bview->buffer()->params.textclass][
2350 old_cursor.par()->layout()].free_spacing
2351 || old_cursor.par()->isFreeSpacing())
2356 /* Ok I'll put some comments here about what is missing.
2357 I have fixed BackSpace (and thus Delete) to not delete
2358 double-spaces automagically. I have also changed Cut,
2359 Copy and Paste to hopefully do some sensible things.
2360 There are still some small problems that can lead to
2361 double spaces stored in the document file or space at
2362 the beginning of paragraphs. This happens if you have
2363 the cursor betwenn to spaces and then save. Or if you
2364 cut and paste and the selection have a space at the
2365 beginning and then save right after the paste. I am
2366 sure none of these are very hard to fix, but I will
2367 put out 1.1.4pre2 with FIX_DOUBLE_SPACE defined so
2368 that I can get some feedback. (Lgb)
2371 // If old_cursor.pos() == 0 and old_cursor.pos()(1) == LineSeparator
2372 // delete the LineSeparator.
2375 // If old_cursor.pos() == 1 and old_cursor.pos()(0) == LineSeparator
2376 // delete the LineSeparator.
2379 // If the pos around the old_cursor were spaces, delete one of them.
2380 if (old_cursor.par() != cursor.par()
2381 || old_cursor.pos() != cursor.pos()) {
2382 // Only if the cursor has really moved
2384 if (old_cursor.pos() > 0
2385 && old_cursor.pos() < old_cursor.par()->size()
2386 && old_cursor.par()->isLineSeparator(old_cursor.pos())
2387 && old_cursor.par()->isLineSeparator(old_cursor.pos() - 1)) {
2388 old_cursor.par()->erase(old_cursor.pos() - 1);
2389 redoParagraphs(bview, old_cursor, old_cursor.par()->next());
2391 #ifdef WITH_WARNINGS
2392 #warning This will not work anymore when we have multiple views of the same buffer
2393 // In this case, we will have to correct also the cursors held by
2394 // other bufferviews. It will probably be easier to do that in a more
2395 // automated way in LyXCursor code. (JMarc 26/09/2001)
2397 // correct all cursors held by the LyXText
2398 fixCursorAfterDelete(bview, cursor, old_cursor);
2399 fixCursorAfterDelete(bview, selection.cursor,
2401 fixCursorAfterDelete(bview, selection.start,
2403 fixCursorAfterDelete(bview, selection.end, old_cursor);
2404 fixCursorAfterDelete(bview, last_sel_cursor,
2406 fixCursorAfterDelete(bview, toggle_cursor, old_cursor);
2407 fixCursorAfterDelete(bview, toggle_end_cursor,
2413 // don't delete anything if this is the ONLY paragraph!
2414 if (!old_cursor.par()->next() && !old_cursor.par()->previous())
2417 // Do not delete empty paragraphs with keepempty set.
2419 [bview->buffer()->params.textclass]
2420 [old_cursor.par()->layout()].keepempty)
2423 // only do our magic if we changed paragraph
2424 if (old_cursor.par() == cursor.par())
2427 // record if we have deleted a paragraph
2428 // we can't possibly have deleted a paragraph before this point
2429 bool deleted = false;
2431 if ((old_cursor.par()->size() == 0
2432 || (old_cursor.par()->size() == 1
2433 && old_cursor.par()->isLineSeparator(0)))) {
2434 // ok, we will delete anything
2435 LyXCursor tmpcursor;
2437 // make sure that you do not delete any environments
2438 status(bview, LyXText::NEED_MORE_REFRESH);
2441 if (old_cursor.row()->previous()) {
2442 refresh_row = old_cursor.row()->previous();
2443 refresh_y = old_cursor.y() - old_cursor.row()->baseline() - refresh_row->height();
2445 cursor = old_cursor; // that undo can restore the right cursor position
2446 Paragraph * endpar = old_cursor.par()->next();
2447 if (endpar && endpar->getDepth()) {
2448 while (endpar && endpar->getDepth()) {
2449 endpar = endpar->next();
2452 setUndo(bview, Undo::DELETE, old_cursor.par(), endpar);
2456 removeRow(old_cursor.row());
2457 if (ownerParagraph() == old_cursor.par()) {
2458 ownerParagraph(ownerParagraph()->next());
2461 delete old_cursor.par();
2463 /* Breakagain the next par. Needed because of
2464 * the parindent that can occur or dissappear.
2465 * The next row can change its height, if
2466 * there is another layout before */
2467 if (refresh_row->next()) {
2468 breakAgain(bview, refresh_row->next());
2469 updateCounters(bview, refresh_row);
2471 setHeightOfRow(bview, refresh_row);
2473 refresh_row = old_cursor.row()->next();
2474 refresh_y = old_cursor.y() - old_cursor.row()->baseline();
2477 cursor = old_cursor; // that undo can restore the right cursor position
2478 Paragraph * endpar = old_cursor.par()->next();
2479 if (endpar && endpar->getDepth()) {
2480 while (endpar && endpar->getDepth()) {
2481 endpar = endpar->next();
2484 setUndo(bview, Undo::DELETE, old_cursor.par(), endpar);
2488 removeRow(old_cursor.row());
2490 if (ownerParagraph() == old_cursor.par()) {
2491 ownerParagraph(ownerParagraph()->next());
2494 delete old_cursor.par();
2496 /* Breakagain the next par. Needed because of
2497 the parindent that can occur or dissappear.
2498 The next row can change its height, if
2499 there is another layout before */
2501 breakAgain(bview, refresh_row);
2502 updateCounters(bview, refresh_row->previous());
2507 setCursorIntern(bview, cursor.par(), cursor.pos());
2509 if (selection.cursor.par() == old_cursor.par()
2510 && selection.cursor.pos() == old_cursor.pos()) {
2511 // correct selection
2512 selection.cursor = cursor;
2516 if (old_cursor.par()->stripLeadingSpaces(bview->buffer()->params.textclass)) {
2517 redoParagraphs(bview, old_cursor,
2518 old_cursor.par()->next());
2520 setCursorIntern(bview, cursor.par(), cursor.pos());
2521 selection.cursor = cursor;
2528 void LyXText::toggleAppendix(BufferView * bview)
2530 Paragraph * par = cursor.par();
2531 bool start = !par->params().startOfAppendix();
2533 // ensure that we have only one start_of_appendix in this document
2534 Paragraph * tmp = ownerParagraph();
2535 for (; tmp; tmp = tmp->next()) {
2536 tmp->params().startOfAppendix(false);
2539 par->params().startOfAppendix(start);
2541 // we can set the refreshing parameters now
2542 status(bview, LyXText::NEED_MORE_REFRESH);
2544 refresh_row = 0; // not needed for full update
2545 updateCounters(bview, 0);
2546 setCursor(bview, cursor.par(), cursor.pos());
2550 Paragraph * LyXText::ownerParagraph() const
2553 return inset_owner->paragraph();
2555 return bv_owner->buffer()->paragraph;
2559 void LyXText::ownerParagraph(Paragraph * p) const
2562 inset_owner->paragraph(p);
2564 bv_owner->buffer()->paragraph = p;
2569 void LyXText::ownerParagraph(int id, Paragraph * p) const
2571 Paragraph * op = bv_owner->buffer()->getParFromID(id);
2572 if (op && op->inInset()) {
2573 static_cast<InsetText *>(op->inInset())->paragraph(p);
2580 LyXText::text_status LyXText::status() const
2586 void LyXText::status(BufferView * bview, LyXText::text_status st) const
2588 // well as much as I know && binds more then || so the above and the
2589 // below are identical (this for your known use of parentesis!)
2590 // Now some explanation:
2591 // We should only go up with refreshing code so this means that if
2592 // we have a MORE refresh we should never set it to LITTLE if we still
2593 // didn't handle it (and then it will be UNCHANGED. Now as long as
2594 // we stay inside one LyXText this may work but we need to tell the
2595 // outermost LyXText that it should REALLY draw us if there is some
2596 // change in a Inset::LyXText. So you see that when we are inside a
2597 // inset's LyXText we give the LITTLE to the outermost LyXText to
2598 // tell'em that it should redraw the actual row (where the inset
2599 // resides! Capito?!
2601 if ((status_ != NEED_MORE_REFRESH)
2602 || (status_ == NEED_MORE_REFRESH
2603 && st != NEED_VERY_LITTLE_REFRESH))
2606 if (inset_owner && st != UNCHANGED) {
2607 bview->text->status(bview, NEED_VERY_LITTLE_REFRESH);
2608 if (!bview->text->refresh_row) {
2609 bview->text->refresh_row = bview->text->cursor.row();
2610 bview->text->refresh_y = bview->text->cursor.y() -
2611 bview->text->cursor.row()->baseline();