1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 The LyX Team.
9 * ====================================================== */
16 #pragma implementation "table.h"
20 #include "lyxparagraph.h"
22 #include "support/textutils.h"
23 #include "insets/insetbib.h"
24 #include "lyx_gui_misc.h"
26 #include "bufferparams.h"
28 #include "minibuffer.h"
42 static const int LYX_PAPER_MARGIN = 20;
46 extern int bibitemMaxWidth(Painter &, LyXFont const &);
48 static int iso885968x[] = {
49 0xbc, // 0xa8 = fathatan
50 0xbd, // 0xa9 = dammatan
51 0xbe, // 0xaa = kasratan
55 0xde, // 0xae = shadda
58 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 0xb0-0xbf
62 0xc2, // 0xc2 = ligature madda
63 0xc3, // 0xc3 = ligature hamza on alef
64 0xc4, // 0xc4 = ligature hamza on waw
65 0xc5, // 0xc5 = ligature hamza under alef
66 0xc0, // 0xc6 = ligature hamza on ya
69 0xc9, // 0xc9 = taa marbuta
88 0,0,0,0,0, // 0xdb- 0xdf
99 0xe9, // 0xe9 = alef maksura
105 bool is_arabic(unsigned char c)
107 return 0xa8 <= c && c <= 0xea && iso885968x[c-0xa8];
112 bool is_nikud(unsigned char c)
114 return 192 <= c && c <= 210;
118 unsigned char LyXText::TransformChar(unsigned char c, Letter_Form form) const
121 (form == FORM_INITIAL || form == FORM_MEDIAL) )
122 return iso885968x[c-0xa8];
128 unsigned char LyXText::TransformChar(unsigned char c, LyXParagraph * par,
129 LyXParagraph::size_type pos) const
133 return c + (0xb0 - '0');
137 bool not_first = (pos > 0 && is_arabic(par->GetChar(pos-1)));
138 if (pos < par->Last()-1 && is_arabic(par->GetChar(pos+1)))
140 return TransformChar(c,FORM_MEDIAL);
142 return TransformChar(c,FORM_INITIAL);
145 return TransformChar(c,FORM_FINAL);
147 return TransformChar(c,FORM_ISOLATED);
150 // This is the comments that some of the warnings below refers to.
151 // There are some issues in this file and I don't think they are
152 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
153 // this is a problem that has been here almost from day one and that a
154 // larger userbase with differenct access patters triggers the bad
155 // behaviour. (segfaults.) What I think happen is: In several places
156 // we store the paragraph in the current cursor and then moves the
157 // cursor. This movement of the cursor will delete paragraph at the
158 // old position if it is now empty. This will make the temporary
159 // pointer to the old cursor paragraph invalid and dangerous to use.
160 // And is some cases this will trigger a segfault. I have marked some
161 // of the cases where this happens with a warning, but I am sure there
162 // are others in this file and in text2.C. There is also a note in
163 // Delete() that you should read. In Delete I store the paragraph->id
164 // instead of a pointer to the paragraph. I am pretty sure this faulty
165 // use of temporary pointers to paragraphs that might have gotten
166 // invalidated (through a cursor movement) before they are used, are
167 // the cause of the strange crashes we get reported often.
169 // It is very tiresom to change this code, especially when it is as
170 // hard to read as it is. Help to fix all the cases where this is done
171 // would be greately appreciated.
175 int LyXText::SingleWidth(LyXParagraph * par,
176 LyXParagraph::size_type pos) const
178 char c = par->GetChar(pos);
179 return SingleWidth(par, pos, c);
183 int LyXText::SingleWidth(LyXParagraph * par,
184 LyXParagraph::size_type pos, char c) const
186 LyXFont font = GetFont(par, pos);
188 // The most common case is handled first (Asger)
189 if (IsPrintable(c)) {
190 if (font.language()->RightToLeft) {
191 if (font.language()->lang == "arabic" &&
192 lyxrc.font_norm == "iso8859-6.8x")
193 c = TransformChar(c, par, pos);
194 else if (font.language()->lang == "hebrew" &&
198 return lyxfont::width(c, font);
200 } else if (IsHfillChar(c)) {
201 return 3; /* Because of the representation
202 * as vertical lines */
203 } else if (c == LyXParagraph::META_FOOTNOTE ||
204 c == LyXParagraph::META_MARGIN ||
205 c == LyXParagraph::META_FIG ||
206 c == LyXParagraph::META_TAB ||
207 c == LyXParagraph::META_WIDE_FIG ||
208 c == LyXParagraph::META_WIDE_TAB ||
209 c == LyXParagraph::META_ALGORITHM) {
212 case LyXParagraph::META_MARGIN:
215 case LyXParagraph::META_FIG:
218 case LyXParagraph::META_TAB:
221 case LyXParagraph::META_ALGORITHM:
224 case LyXParagraph::META_WIDE_FIG:
227 case LyXParagraph::META_WIDE_TAB:
230 case LyXParagraph::META_FOOTNOTE:
236 return lyxfont::width(fs, font);
237 } else if (c == LyXParagraph::META_INSET) {
238 Inset * tmpinset= par->GetInset(pos);
240 return par->GetInset(pos)->width(owner_->painter(),
245 } else if (IsSeparatorChar(c))
247 else if (IsNewlineChar(c))
249 return lyxfont::width(c, font);
253 // Returns the paragraph position of the last character in the specified row
254 LyXParagraph::size_type LyXText::RowLast(Row const * row) const
257 return row->par->Last() - 1;
258 else if (row->next->par != row->par)
259 return row->par->Last() - 1;
261 return row->next->pos - 1;
265 LyXParagraph::size_type LyXText::RowLastPrintable(Row const * row) const
267 LyXParagraph::size_type last = RowLast(row);
268 if (last >= row->pos && row->next && row->next->par == row->par &&
269 row->par->IsSeparator(last))
276 void LyXText::ComputeBidiTables(Row * row) const
278 bidi_same_direction = true;
279 if (!lyxrc.rtl_support) {
283 LyXParagraph::size_type last = RowLastPrintable(row);
284 bidi_start = row->pos;
286 if (bidi_start > last) {
291 if (last + 2 - bidi_start >
292 static_cast<LyXParagraph::size_type>(log2vis_list.size())) {
293 LyXParagraph::size_type new_size =
294 (last + 2 - bidi_start < 500) ?
295 500 : 2 * (last + 2 - bidi_start);
296 log2vis_list.resize(new_size);
297 vis2log_list.resize(new_size);
298 bidi_levels.resize(new_size);
301 vis2log_list[last + 1 - bidi_start] = -1;
302 log2vis_list[last + 1 - bidi_start] = -1;
304 LyXParagraph::size_type stack[2];
305 bool rtl_par = row->par->getParLanguage()->RightToLeft;
309 LyXParagraph::size_type main_body = BeginningOfMainBody(row->par);
311 for (LyXParagraph::size_type lpos = bidi_start; lpos <= last; ++lpos) {
312 bool is_space = row->par->IsLineSeparator(lpos);
313 LyXParagraph::size_type pos =
314 (is_space && lpos+1 <= last &&
315 (!row->par->table || !row->par->IsNewline(lpos+1)) )
317 LyXFont font = row->par->GetFontSettings(pos);
318 bool new_rtl = font.isVisibleRightToLeft();
319 bool new_rtl0 = font.isRightToLeft();
322 if (row->par->table && row->par->IsNewline(lpos)) {
324 new_rtl = new_rtl0 = false;
325 } else if (lpos == main_body - 1 && row->pos < main_body - 1 &&
326 row->par->IsLineSeparator(lpos)) {
327 new_level = (rtl_par) ? 1 : 0;
328 new_rtl = new_rtl0 = rtl_par;
330 new_level = (new_rtl) ? 1 : 2;
332 new_level = (rtl_par) ? 2 : 0;
334 if (is_space && new_level >= level) {
340 int new_level2 = new_level;
342 if (level == new_level && rtl0 != new_rtl0) {
344 log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
345 } else if (level < new_level) {
346 log2vis_list[lpos - bidi_start] = (rtl) ? -1 : 1;
347 if (new_level > rtl_par)
348 bidi_same_direction = false;
350 log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
353 bidi_levels[lpos - bidi_start] = new_level;
355 while (level > new_level2) {
356 LyXParagraph::size_type old_lpos =
358 int delta = lpos - old_lpos - 1;
361 log2vis_list[lpos - bidi_start] += delta;
362 log2vis_list[old_lpos - bidi_start] += delta;
364 while (level < new_level)
365 stack[level++] = lpos;
369 LyXParagraph::size_type old_lpos = stack[--level];
370 int delta = last - old_lpos;
373 log2vis_list[old_lpos - bidi_start] += delta;
376 LyXParagraph::size_type vpos = bidi_start - 1;
377 for (LyXParagraph::size_type lpos = bidi_start; lpos <= last; ++lpos) {
378 vpos += log2vis_list[lpos - bidi_start];
379 vis2log_list[vpos - bidi_start] = lpos;
380 log2vis_list[lpos - bidi_start] = vpos;
385 void LyXText::draw(Row const * row,
386 LyXParagraph::size_type & vpos,
387 int offset, float & x)
389 Painter & pain = owner_->painter();
391 LyXParagraph::size_type pos = vis2log(vpos);
392 char c = row->par->GetChar(pos);
394 if (IsNewlineChar(c)) {
396 // Draw end-of-line marker
397 LyXFont font = GetFont(row->par, pos);
398 int wid = lyxfont::width('n', font);
399 int asc = lyxfont::maxAscent(font);
400 int y = offset + row->baseline;
403 if (bidi_level(pos) % 2 == 0) {
404 xp[0] = int(x + wid * 0.375);
405 yp[0] = int(y - 0.875 * asc * 0.75);
408 yp[1] = int(y - 0.500 * asc * 0.75);
410 xp[2] = int(x + wid * 0.375);
411 yp[2] = int(y - 0.125 * asc * 0.75);
413 pain.lines(xp, yp, 3, LColor::eolmarker);
416 yp[0] = int(y - 0.500 * asc * 0.75);
418 xp[1] = int(x + wid);
419 yp[1] = int(y - 0.500 * asc * 0.75);
421 xp[2] = int(x + wid);
422 yp[2] = int(y - asc * 0.75);
424 pain.lines(xp, yp, 3, LColor::eolmarker);
426 xp[0] = int(x + wid * 0.625);
427 yp[0] = int(y - 0.875 * asc * 0.75);
429 xp[1] = int(x + wid);
430 yp[1] = int(y - 0.500 * asc * 0.75);
432 xp[2] = int(x + wid * 0.625);
433 yp[2] = int(y - 0.125 * asc * 0.75);
435 pain.lines(xp, yp, 3, LColor::eolmarker);
437 xp[0] = int(x + wid);
438 yp[0] = int(y - 0.500 * asc * 0.75);
441 yp[1] = int(y - 0.500 * asc * 0.75);
444 yp[2] = int(y - asc * 0.75);
446 pain.lines(xp, yp, 3, LColor::eolmarker);
452 LyXFont font = GetFont(row->par, pos);
453 LyXFont font2 = font;
455 if (c == LyXParagraph::META_FOOTNOTE
456 || c == LyXParagraph::META_MARGIN
457 || c == LyXParagraph::META_FIG
458 || c == LyXParagraph::META_TAB
459 || c == LyXParagraph::META_WIDE_FIG
460 || c == LyXParagraph::META_WIDE_TAB
461 || c == LyXParagraph::META_ALGORITHM) {
464 case LyXParagraph::META_MARGIN:
467 case LyXParagraph::META_FIG:
470 case LyXParagraph::META_TAB:
473 case LyXParagraph::META_ALGORITHM:
476 case LyXParagraph::META_WIDE_FIG:
479 case LyXParagraph::META_WIDE_TAB:
482 case LyXParagraph::META_FOOTNOTE:
489 // calculate the position of the footnotemark
490 int y = (row->baseline - lyxfont::maxAscent(font2)
491 + lyxfont::maxAscent(font));
493 font.setColor(LColor::footnote);
497 // draw it and set new x position
499 pain.text(int(x), offset + y, fs, font);
500 x += lyxfont::width(fs, font);
501 pain.line(int(tmpx), offset + row->baseline,
502 int(x), offset + row->baseline,
507 } else if (c == LyXParagraph::META_INSET) {
508 Inset * tmpinset = row->par->GetInset(pos);
510 tmpinset->draw(owner_->painter(), font,
511 offset + row->baseline, x);
517 /* usual characters, no insets */
519 // Collect character that we can draw in one command
521 // This is dirty, but fast. Notice that it will never be too small.
522 // For the record, I'll note that Microsoft Word has a limit
523 // of 768 here. We have none :-) (Asger)
524 // Ok. I am the first to admit that the use of std::string will be
525 // a tiny bit slower than using a POD char array. However, I claim
526 // that this slowdown is so small that it is close to inperceptive.
527 // So IMHO we should go with the easier and clearer implementation.
528 // And even if 1024 is a large number here it might overflow, string
529 // will only overflow if the machine is out of memory...
530 static string textstring;
534 LyXParagraph::size_type last = RowLastPrintable(row);
537 if (font.language()->lang == "hebrew") {
539 LyXParagraph::size_type vpos2 = vpos;
540 int width = lyxfont::width(c, font2);
542 while (vpos2 <= last &&
543 (pos = vis2log(vpos2)) >= 0
544 && static_cast<unsigned char>(c = row->par->GetChar(pos)) > ' '
547 if (static_cast<unsigned char>(c = row->par->GetChar(pos)) > ' '
549 int width2 = SingleWidth(row->par, pos, c);
550 dx = (c == 'ø' || c == 'ã')
551 ? width2-width : (width2-width)/2;
554 pain.text(int(x)+dx, offset + row->baseline, textstring, font);
556 while (vpos <= last &&
557 (pos = vis2log(vpos)) >= 0
558 && static_cast<unsigned char>(c = row->par->GetChar(pos)) > ' '
560 && font2 == GetFont(row->par, pos)) {
564 // Draw text and set the new x position
565 pain.text(int(x), offset + row->baseline, textstring, font);
566 x += lyxfont::width(textstring, font);
568 } else if (font.language()->lang == "arabic" &&
569 lyxrc.font_norm == "iso8859-6.8x") {
570 textstring = TransformChar(c, row->par, pos);
571 while (vpos <= last &&
572 (pos = vis2log(vpos)) >= 0
573 && static_cast<unsigned char>(c = row->par->GetChar(pos)) > ' '
574 && font2 == GetFont(row->par, pos)) {
575 c = TransformChar(c, row->par, pos);
579 // Draw text and set the new x position
580 pain.text(int(x), offset + row->baseline, textstring, font);
581 x += lyxfont::width(textstring, font);
583 while (vpos <= last &&
584 (pos = vis2log(vpos)) >= 0
585 && static_cast<unsigned char>(c = row->par->GetChar(pos)) > ' '
586 && font2 == GetFont(row->par, pos)) {
590 // Draw text and set the new x position
591 pain.text(int(x), offset + row->baseline, textstring, font);
592 x += lyxfont::width(textstring, font);
595 // what about underbars?
596 if (font.underbar() == LyXFont::ON && font.latex() != LyXFont::ON) {
597 pain.line(int(tmpx), offset + row->baseline + 2,
598 int(x), offset + row->baseline + 2);
602 // If we want ulem.sty support, drawing
603 // routines should go here. (Asger)
604 // Why shouldn't LyXFont::drawText handle it internally?
608 // Returns the left beginning of the text.
609 // This information cannot be taken from the layouts-objekt, because in
610 // LaTeX the beginning of the text fits in some cases (for example sections)
611 // exactly the label-width.
612 int LyXText::LeftMargin(Row const * row) const
614 LyXLayout const & layout =
615 textclasslist.Style(buffer->params.textclass,
616 row->par->GetLayout());
618 string parindent = layout.parindent;
620 /* table stuff -- begin */
623 /* table stuff -- end */
625 int x = LYX_PAPER_MARGIN;
627 x += lyxfont::signedWidth(textclasslist
628 .TextClass(buffer->params.textclass)
631 .TextClass(buffer->params.textclass)
634 if (row->par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
635 LyXFont font(LyXFont::ALL_SANE);
636 font.setSize(LyXFont::SIZE_SMALL);
637 x += lyxfont::width("Mwide-figM", font)
638 + LYX_PAPER_MARGIN/2;
641 // this is the way, LyX handles the LaTeX-Environments.
642 // I have had this idea very late, so it seems to be a
643 // later added hack and this is true
644 if (!row->par->GetDepth()) {
645 if (!row->par->GetLayout()) {
646 // find the previous same level paragraph
647 if (row->par->FirstPhysicalPar()->Previous()) {
648 LyXParagraph * newpar = row->par
649 ->DepthHook(row->par->GetDepth());
651 textclasslist.Style(buffer->params.textclass,
658 // find the next level paragraph
660 LyXParagraph * newpar = row->par->DepthHook(row->par->GetDepth()-1);
662 // make a corresponding row. Needed to call LeftMargin()
664 // check wether it is a sufficent paragraph
665 if (newpar && newpar->footnoteflag == row->par->footnoteflag
667 .Style(buffer->params.textclass,
668 newpar->GetLayout()).isEnvironment()) {
670 dummyrow.par = newpar;
671 dummyrow.pos = newpar->Last();
672 x = LeftMargin(&dummyrow);
674 // this is no longer an error, because this function
675 // is used to clear impossible depths after changing
676 // a layout. Since there is always a redo,
677 // LeftMargin() is always called
678 row->par->FirstPhysicalPar()->depth = 0;
681 if (newpar && !row->par->GetLayout()) {
682 if (newpar->FirstPhysicalPar()->noindent)
685 parindent = textclasslist
686 .Style(buffer->params.textclass,
687 newpar->GetLayout()).parindent;
692 LyXFont labelfont = GetFont(row->par, -2);
693 switch (layout.margintype) {
695 if (!layout.leftmargin.empty()) {
696 x += lyxfont::signedWidth(layout.leftmargin,
698 .TextClass(buffer->params.
702 if (!row->par->GetLabelstring().empty()) {
703 x += lyxfont::signedWidth(layout.labelindent,
705 x += lyxfont::width(row->par->GetLabelstring(), labelfont);
706 x += lyxfont::width(layout.labelsep, labelfont);
710 x += lyxfont::signedWidth(layout.labelindent, labelfont);
711 if (row->pos >= BeginningOfMainBody(row->par)) {
712 if (!row->par->GetLabelWidthString().empty()) {
713 x += lyxfont::width(row->par->GetLabelWidthString(),
715 x += lyxfont::width(layout.labelsep, labelfont);
720 x += lyxfont::signedWidth(layout.leftmargin, textclasslist.TextClass(buffer->params.textclass).defaultfont()) * 4
721 / (row->par->GetDepth() + 4);
723 case MARGIN_FIRST_DYNAMIC:
724 if (layout.labeltype == LABEL_MANUAL) {
725 if (row->pos >= BeginningOfMainBody(row->par)) {
726 x += lyxfont::signedWidth(layout.leftmargin, labelfont);
728 x += lyxfont::signedWidth(layout.labelindent, labelfont);
731 // Special case to fix problems with
733 || (layout.labeltype == LABEL_STATIC
734 && layout.latextype == LATEX_ENVIRONMENT
735 && ! row->par->IsFirstInSequence())) {
736 x += lyxfont::signedWidth(layout.leftmargin, labelfont);
737 } else if (layout.labeltype != LABEL_TOP_ENVIRONMENT
738 && layout.labeltype != LABEL_BIBLIO
739 && layout.labeltype !=
740 LABEL_CENTERED_TOP_ENVIRONMENT) {
741 x += lyxfont::signedWidth(layout.labelindent,
743 x += lyxfont::width(layout.labelsep, labelfont);
744 x += lyxfont::width(row->par->GetLabelstring(), labelfont);
748 case MARGIN_RIGHT_ADDRESS_BOX:
750 // ok, a terrible hack. The left margin depends on the widest
751 // row in this paragraph. Do not care about footnotes, they
752 // are *NOT* allowed in the LaTeX realisation of this layout.
754 // find the first row of this paragraph
755 Row const * tmprow = row;
756 while (tmprow->previous && tmprow->previous->par == row->par)
757 tmprow = tmprow->previous;
759 int minfill = tmprow->fill;
760 while (tmprow->next && tmprow->next->par == row->par) {
761 tmprow = tmprow->next;
762 if (tmprow->fill < minfill)
763 minfill = tmprow->fill;
766 x += lyxfont::signedWidth(layout.leftmargin,
768 .TextClass(buffer->params.textclass)
774 if (row->par->pextra_type == LyXParagraph::PEXTRA_INDENT) {
775 if (!row->par->pextra_widthp.empty()) {
777 atoi(row->par->pextra_widthp.c_str()) / 100;
778 } else if (!row->par->pextra_width.empty()) {
779 int xx = VSpace(row->par->pextra_width).inPixels(owner_);
781 xx = paperwidth * 80 / 100;
783 } else { // should not happen
784 LyXFont font(LyXFont::ALL_SANE);
785 x += lyxfont::width("XXXXXX", font);
789 int align; // wrong type
790 if (row->par->FirstPhysicalPar()->align == LYX_ALIGN_LAYOUT)
791 align = layout.align;
793 align = row->par->FirstPhysicalPar()->align;
795 // set the correct parindent
797 if ((layout.labeltype == LABEL_NO_LABEL
798 || layout.labeltype == LABEL_TOP_ENVIRONMENT
799 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
800 || (layout.labeltype == LABEL_STATIC
801 && layout.latextype == LATEX_ENVIRONMENT
802 && ! row->par->IsFirstInSequence()))
803 && row->par == row->par->FirstPhysicalPar()
804 && align == LYX_ALIGN_BLOCK
805 && !row->par->noindent
806 && (row->par->layout ||
807 buffer->params.paragraph_separation ==
808 BufferParams::PARSEP_INDENT))
809 x += lyxfont::signedWidth(parindent,
811 .TextClass(buffer->params
814 else if (layout.labeltype == LABEL_BIBLIO) {
815 // ale970405 Right width for bibitems
816 x += bibitemMaxWidth(owner_->painter(),
818 .TextClass(buffer->params
827 int LyXText::RightMargin(Row const * row) const
829 LyXLayout const & layout =
830 textclasslist.Style(buffer->params.textclass,
831 row->par->GetLayout());
833 int x = LYX_PAPER_MARGIN
834 + lyxfont::signedWidth(textclasslist
835 .TextClass(buffer->params.textclass)
838 .TextClass(buffer->params.textclass)
841 if (row->par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
842 x += LYX_PAPER_MARGIN / 2;
845 // this is the way, LyX handles the LaTeX-Environments.
846 // I have had this idea very late, so it seems to be a
847 // later added hack and this is true
848 if (row->par->GetDepth()) {
849 // find the next level paragraph
851 LyXParagraph * newpar = row->par;
854 newpar = newpar->FirstPhysicalPar()->Previous();
856 newpar = newpar->FirstPhysicalPar();
857 } while (newpar && newpar->GetDepth() >= row->par->GetDepth()
858 && newpar->footnoteflag == row->par->footnoteflag);
860 // make a corresponding row. Needed to call LeftMargin()
862 // check wether it is a sufficent paragraph
863 if (newpar && newpar->footnoteflag == row->par->footnoteflag
864 && textclasslist.Style(buffer->params.textclass,
868 dummyrow.par = newpar;
870 x = RightMargin(&dummyrow);
872 // this is no longer an error, because this function
873 // is used to clear impossible depths after changing
874 // a layout. Since there is always a redo,
875 // LeftMargin() is always called
876 row->par->FirstPhysicalPar()->depth = 0;
880 //lyxerr << "rightmargin: " << layout->rightmargin << endl;
881 x += lyxfont::signedWidth(layout.rightmargin, textclasslist.TextClass(buffer->params.textclass).defaultfont()) * 4
882 / (row->par->GetDepth() + 4);
887 int LyXText::LabelEnd (Row const * row) const
889 if (textclasslist.Style(buffer->params.textclass,
890 row->par->GetLayout()).margintype
894 tmprow.pos = row->par->Last();
895 return LeftMargin(&tmprow); /* just the beginning
898 return 0; /* LabelEnd is only needed, if the
899 layout fills a flushleft
904 /* table stuff -- begin*/
905 int LyXText::NumberOfCell(LyXParagraph * par,
906 LyXParagraph::size_type pos) const
909 LyXParagraph::size_type tmp_pos = 0;
910 while (tmp_pos < pos) {
911 if (par->IsNewline(tmp_pos))
919 int LyXText::WidthOfCell(LyXParagraph * par,
920 LyXParagraph::size_type & pos) const
923 while (pos < par->Last() && !par->IsNewline(pos)) {
924 w += SingleWidth(par, pos);
927 if (par->IsNewline(pos))
933 bool LyXText::HitInTable(Row * row, int x) const
936 float fill_separator, fill_hfill, fill_label_hfill;
937 if (!row->par->table)
939 PrepareToPrint(row, tmpx, fill_separator,
940 fill_hfill, fill_label_hfill, false);
941 return (x > tmpx && x < tmpx + row->par->table->WidthOfTable());
945 bool LyXText::MouseHitInTable(int x, long y) const
947 Row * row = GetRowNearY(y);
948 return HitInTable(row, x);
952 /* table stuff -- end*/
955 // get the next breakpoint in a given paragraph
956 LyXParagraph::size_type
957 LyXText::NextBreakPoint(Row const * row, int width) const
959 LyXParagraph * par = row->par;
960 LyXParagraph::size_type pos = row->pos;
962 /* table stuff -- begin*/
964 while (pos < par->size()
965 && (!par->IsNewline(pos)
966 || !par->table->IsFirstCell(NumberOfCell(par, pos+1)))) {
967 if (par->GetChar(pos) == LyXParagraph::META_INSET &&
968 par->GetInset(pos) && par->GetInset(pos)->display()){
969 par->GetInset(pos)->display(false);
975 /* table stuff -- end*/
977 // position of the last possible breakpoint
978 // -1 isn't a suitable value, but a flag
979 LyXParagraph::size_type last_separator = -1;
980 width -= RightMargin(row);
982 LyXParagraph::size_type main_body = BeginningOfMainBody(par);
983 LyXLayout const & layout =
984 textclasslist.Style(buffer->params.textclass, par->GetLayout());
985 LyXParagraph::size_type i = pos;
987 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
988 /* special code for right address boxes, only newlines count */
989 while (i < par->Last()) {
990 if (par->IsNewline(i)) {
992 i = par->Last() - 1; // this means break
994 } else if (par->GetChar(i) == LyXParagraph::META_INSET &&
995 par->GetInset(i) && par->GetInset(i)->display()){
996 par->GetInset(i)->display(false);
1001 // Last position is an invariant
1002 LyXParagraph::size_type const last =
1004 // this is the usual handling
1005 int x = LeftMargin(row);
1006 while (x < width && i < last) {
1007 char c = par->GetChar(i);
1008 if (IsNewlineChar(c)) {
1010 x = width; // this means break
1011 } else if (c == LyXParagraph::META_INSET &&
1012 par->GetInset(i) && par->GetInset(i)->display()){
1013 // check wether a Display() inset is
1014 // valid here. if not, change it to
1016 if (layout.isCommand()
1017 || (layout.labeltype == LABEL_MANUAL
1018 && i < BeginningOfMainBody(par))){
1019 // display istn't allowd
1020 par->GetInset(i)->display(false);
1021 x += SingleWidth(par, i, c);
1023 // inset is display. So break the line here
1027 if (IsLineSeparatorChar(par->GetChar(i+1)))
1030 last_separator = last; // to avoid extra rows
1032 last_separator = i - 1;
1033 x = width; // this means break
1036 if (IsLineSeparatorChar(c))
1038 x += SingleWidth(par, i, c);
1041 if (i == main_body) {
1042 x += lyxfont::width(layout.labelsep,
1044 if (par->IsLineSeparator(i - 1))
1045 x-= SingleWidth(par, i - 1);
1046 int left_margin = LabelEnd(row);
1047 if (x < left_margin)
1051 // end of paragraph is always a suitable separator
1052 if (i == last && x < width)
1056 // well, if last_separator is still 0, the line isn't breakable.
1057 // don't care and cut simply at the end
1058 if (last_separator < 0) {
1062 // manual labels cannot be broken in LaTeX, do not care
1063 if (main_body && last_separator < main_body)
1064 last_separator = main_body - 1;
1066 return last_separator;
1070 // returns the minimum space a row needs on the screen in pixel
1071 int LyXText::Fill(Row const * row, int paper_width) const
1074 // get the pure distance
1075 LyXParagraph::size_type last = RowLastPrintable(row);
1076 /* table stuff -- begin */
1077 if (row->par->table) {
1078 // for tables FILL does calculate the widthes of each cell in
1080 LyXParagraph::size_type pos = row->pos;
1081 int cell = NumberOfCell(row->par, pos);
1084 row->par->table->SetWidthOfCell(cell,
1085 WidthOfCell(row->par,
1088 } while (pos <= last && !row->par->table->IsFirstCell(cell));
1089 // don't forget the very last table cell without characters
1090 if (cell == row->par->table->GetNumberOfCells() - 1)
1091 row->par->table->SetWidthOfCell(cell,
1092 WidthOfCell(row->par,
1095 return 0; /* width of table cannot be returned since
1096 * we cannot guarantee its correct value at
1099 /* table stuff -- end*/
1101 // special handling of the right address boxes
1102 if (textclasslist.Style(buffer->params.textclass,
1103 row->par->GetLayout()).margintype
1104 == MARGIN_RIGHT_ADDRESS_BOX) {
1105 int tmpfill = row->fill;
1106 row->fill = 0; // the minfill in MarginLeft()
1107 w = LeftMargin(row);
1108 row->fill = tmpfill;
1110 w = LeftMargin(row);
1112 LyXLayout const & layout = textclasslist.Style(buffer->params.textclass,
1113 row->par->GetLayout());
1114 LyXParagraph::size_type main_body =
1115 BeginningOfMainBody(row->par);
1116 LyXParagraph::size_type i = row->pos;
1119 if (main_body > 0 && i == main_body) {
1120 w += lyxfont::width(layout.labelsep, GetFont(row->par, -2));
1121 if (row->par->IsLineSeparator(i - 1))
1122 w -= SingleWidth(row->par, i - 1);
1123 int left_margin = LabelEnd(row);
1124 if (w < left_margin)
1127 w += SingleWidth(row->par, i);
1130 if (main_body > 0 && main_body > last) {
1131 w += lyxfont::width(layout.labelsep, GetFont(row->par, -2));
1132 if (last >= 0 && row->par->IsLineSeparator(last))
1133 w -= SingleWidth(row->par, last);
1134 int left_margin = LabelEnd(row);
1135 if (w < left_margin)
1139 fill = paper_width - w - RightMargin(row);
1144 // returns the minimum space a manual label needs on the screen in pixel
1145 int LyXText::LabelFill(Row const * row) const
1147 LyXParagraph::size_type last = BeginningOfMainBody(row->par) - 1;
1148 // -1 because a label ends either with a space that is in the label,
1149 // or with the beginning of a footnote that is outside the label.
1151 // I don't understand this code in depth, but sometimes "last" is
1152 // less than 0 and this causes a crash. This fix seems to work
1153 // correctly, but I bet the real error is elsewhere. The bug is
1154 // triggered when you have an open footnote in a paragraph
1155 // environment with a manual label. (Asger)
1156 if (last < 0) last = 0;
1158 if (row->par->IsLineSeparator(last)) /* a sepearator at this end
1165 w += SingleWidth(row->par, i);
1170 if (!row->par->labelwidthstring.empty()) {
1171 fill = max(lyxfont::width(row->par->labelwidthstring,
1172 GetFont(row->par, -2)) - w,
1180 // returns the number of separators in the specified row. The separator
1181 // on the very last column doesnt count
1182 int LyXText::NumberOfSeparators(Row const * row) const
1184 int last = RowLast(row);
1185 int p = max(row->pos, BeginningOfMainBody(row->par));
1187 for (; p < last; ++p) {
1188 if (row->par->IsSeparator(p)) {
1196 // returns the number of hfills in the specified row. The LyX-Hfill is
1197 // a LaTeX \hfill so that the hfills at the beginning and at the end were
1198 // ignored. This is *MUCH* more usefull than not to ignore!
1199 int LyXText::NumberOfHfills(Row const * row) const
1201 int last = RowLast(row);
1202 int first = row->pos;
1203 if (first) { /* hfill *DO* count at the beginning
1205 while(first <= last && row->par->IsHfill(first))
1209 first = max(first, BeginningOfMainBody(row->par));
1211 for (int p = first; p <= last; ++p) { // last, because the end is ignored!
1212 if (row->par->IsHfill(p)) {
1220 // like NumberOfHfills, but only those in the manual label!
1221 int LyXText::NumberOfLabelHfills(Row const * row) const
1223 LyXParagraph::size_type last = RowLast(row);
1224 LyXParagraph::size_type first = row->pos;
1225 if (first) { /* hfill *DO* count at the beginning
1227 while(first < last && row->par->IsHfill(first))
1231 last = min(last, BeginningOfMainBody(row->par));
1233 for (LyXParagraph::size_type p = first;
1234 p < last; ++p) { // last, because the end is ignored!
1235 if (row->par->IsHfill(p)) {
1243 // returns true, if a expansion is needed.
1244 // Rules are given by LaTeX
1245 bool LyXText::HfillExpansion(Row const * row_ptr,
1246 LyXParagraph::size_type pos) const
1248 // by the way, is it a hfill?
1249 if (!row_ptr->par->IsHfill(pos))
1252 // at the end of a row it does not count
1253 if (pos >= RowLast(row_ptr))
1256 // at the beginning of a row it does not count, if it is not
1257 // the first row of a paragaph
1261 // in some labels it does not count
1262 if (textclasslist.Style(buffer->params.textclass,
1263 row_ptr->par->GetLayout()).margintype
1265 && pos < BeginningOfMainBody(row_ptr->par))
1268 // if there is anything between the first char of the row and
1269 // the sepcified position that is not a newline and not a hfill,
1270 // the hfill will count, otherwise not
1271 LyXParagraph::size_type i = row_ptr->pos;
1272 while (i < pos && (row_ptr->par->IsNewline(i)
1273 || row_ptr->par->IsHfill(i)))
1280 void LyXText::SetHeightOfRow(Row * row_ptr) const
1282 /* get the maximum ascent and the maximum descent */
1284 float layoutasc = 0;
1285 float layoutdesc = 0;
1290 /* this must not happen before the currentrow for clear reasons.
1291 so the trick is just to set the current row onto this row */
1293 GetRow(row_ptr->par, row_ptr->pos, unused_y);
1295 /* ok , let us initialize the maxasc and maxdesc value.
1296 * This depends in LaTeX of the font of the last character
1297 * in the paragraph. The hack below is necessary because
1298 * of the possibility of open footnotes */
1300 /* Correction: only the fontsize count. The other properties
1301 are taken from the layoutfont. Nicer on the screen :) */
1303 LyXParagraph * par = row_ptr->par->LastPhysicalPar();
1304 LyXParagraph * firstpar = row_ptr->par->FirstPhysicalPar();
1306 LyXLayout const & layout = textclasslist.Style(buffer->params.textclass,
1307 firstpar->GetLayout());
1309 LyXFont font = GetFont(par, par->Last() - 1);
1310 LyXFont::FONT_SIZE size = font.size();
1311 font = GetFont(par, -1);
1314 LyXFont labelfont = GetFont(par, -2);
1316 float spacing_val = 1.0;
1317 if (!row_ptr->par->spacing.isDefault()) {
1318 spacing_val = row_ptr->par->spacing.getValue();
1320 spacing_val = buffer->params.spacing.getValue();
1322 //lyxerr << "spacing_val = " << spacing_val << endl;
1324 int maxasc = int(lyxfont::maxAscent(font) *
1325 layout.spacing.getValue() *
1327 int maxdesc = int(lyxfont::maxDescent(font) *
1328 layout.spacing.getValue() *
1331 int pos_end = RowLast(row_ptr);
1335 // Check if any insets are larger
1336 for (pos = row_ptr->pos; pos <= pos_end; ++pos) {
1337 if (row_ptr->par->GetChar(pos) == LyXParagraph::META_INSET) {
1338 tmpfont = GetFont(row_ptr->par, pos);
1339 tmpinset = row_ptr->par->GetInset(pos);
1341 asc = tmpinset->ascent(owner_->painter(), tmpfont);
1342 desc = tmpinset->descent(owner_->painter(), tmpfont);
1351 // Check if any custom fonts are larger (Asger)
1352 // This is not completely correct, but we can live with the small,
1353 // cosmetic error for now.
1354 LyXFont::FONT_SIZE maxsize = row_ptr->par->HighestFontInRange(row_ptr->pos,
1356 if (maxsize > font.size()) {
1357 font.setSize(maxsize);
1359 asc = lyxfont::maxAscent(font);
1360 desc = lyxfont::maxDescent(font);
1367 /* table stuff -- begin*/
1368 if (row_ptr->par->table){
1369 // stretch the rows a bit
1373 /* table stuff -- end*/
1375 // This is nicer with box insets:
1379 row_ptr->ascent_of_text = maxasc;
1381 /* is it a top line? */
1382 if (row_ptr->pos == 0
1383 && row_ptr->par == firstpar) {
1385 /* some parksips VERY EASY IMPLEMENTATION */
1386 if (buffer->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
1387 if (layout.isParagraph()
1388 && firstpar->GetDepth() == 0
1389 && firstpar->Previous())
1390 maxasc += buffer->params.getDefSkip().inPixels(owner_);
1391 else if (firstpar->Previous()
1392 && textclasslist.Style(buffer->params.textclass,
1393 firstpar->Previous()->GetLayout()).isParagraph()
1394 && firstpar->Previous()->GetDepth() == 0)
1395 // is it right to use defskip here too? (AS)
1396 maxasc += buffer->params.getDefSkip().inPixels(owner_);
1399 /* the paper margins */
1400 if (!row_ptr->par->previous)
1401 maxasc += LYX_PAPER_MARGIN;
1403 /* add the vertical spaces, that the user added */
1404 if (firstpar->added_space_top.kind() != VSpace::NONE)
1405 maxasc += int(firstpar->added_space_top.inPixels(owner_));
1407 /* do not forget the DTP-lines!
1408 * there height depends on the font of the nearest character */
1409 if (firstpar->line_top)
1410 maxasc += 2 * lyxfont::ascent('x', GetFont(firstpar, 0));
1412 /* and now the pagebreaks */
1413 if (firstpar->pagebreak_top)
1414 maxasc += 3 * DefaultHeight();
1416 /* this is special code for the chapter, since the label of this
1417 * layout is printed in an extra row */
1418 if (layout.labeltype == LABEL_COUNTER_CHAPTER
1419 && buffer->params.secnumdepth >= 0) {
1420 float spacing_val = 1.0;
1421 if (!row_ptr->par->spacing.isDefault()) {
1422 spacing_val = row_ptr->par->spacing.getValue();
1424 spacing_val = buffer->params.spacing.getValue();
1427 labeladdon = int(lyxfont::maxDescent(labelfont) *
1428 layout.spacing.getValue() *
1430 + int(lyxfont::maxAscent(labelfont) *
1431 layout.spacing.getValue() *
1435 /* special code for the top label */
1436 if ((layout.labeltype == LABEL_TOP_ENVIRONMENT
1437 || layout.labeltype == LABEL_BIBLIO
1438 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1439 && row_ptr->par->IsFirstInSequence()
1440 && !row_ptr->par->GetLabelstring().empty()) {
1441 float spacing_val = 1.0;
1442 if (!row_ptr->par->spacing.isDefault()) {
1443 spacing_val = row_ptr->par->spacing.getValue();
1445 spacing_val = buffer->params.spacing.getValue();
1449 (lyxfont::maxAscent(labelfont) *
1450 layout.spacing.getValue() *
1452 +(lyxfont::maxDescent(labelfont) *
1453 layout.spacing.getValue() *
1455 + layout.topsep * DefaultHeight()
1456 + layout.labelbottomsep * DefaultHeight());
1459 /* and now the layout spaces, for example before and after a section,
1460 * or between the items of a itemize or enumerate environment */
1462 if (!firstpar->pagebreak_top) {
1463 LyXParagraph * prev = row_ptr->par->Previous();
1465 prev = row_ptr->par->DepthHook(row_ptr->par->GetDepth());
1466 if (prev && prev->GetLayout() == firstpar->GetLayout()
1467 && prev->GetDepth() == firstpar->GetDepth()
1468 && prev->GetLabelWidthString() == firstpar->GetLabelWidthString())
1470 layoutasc = (layout.itemsep * DefaultHeight());
1472 else if (row_ptr->previous) {
1473 tmptop = layout.topsep;
1475 if (row_ptr->previous->par->GetDepth() >= row_ptr->par->GetDepth())
1476 tmptop-= textclasslist.Style(buffer->params.textclass, row_ptr->previous->par->GetLayout()).bottomsep;
1479 layoutasc = (tmptop * DefaultHeight());
1481 else if (row_ptr->par->line_top){
1482 tmptop = layout.topsep;
1485 layoutasc = (tmptop * DefaultHeight());
1488 prev = row_ptr->par->DepthHook(row_ptr->par->GetDepth()-1);
1490 maxasc += int(textclasslist.Style(buffer->params.textclass,
1491 prev->GetLayout()).parsep * DefaultHeight());
1494 if (firstpar->Previous()
1495 && firstpar->Previous()->GetDepth() == 0
1496 && firstpar->Previous()->GetLayout() != firstpar->GetLayout()) {
1499 else if (firstpar->Previous()){
1500 maxasc += int(layout.parsep * DefaultHeight());
1506 /* is it a bottom line? */
1507 if (row_ptr->par->ParFromPos(RowLast(row_ptr) + 1) == par
1508 && (!row_ptr->next || row_ptr->next->par != row_ptr->par)) {
1510 /* the paper margins */
1512 maxdesc += LYX_PAPER_MARGIN;
1514 /* add the vertical spaces, that the user added */
1515 if (firstpar->added_space_bottom.kind() != VSpace::NONE)
1516 maxdesc += int(firstpar->added_space_bottom.inPixels(owner_));
1518 /* do not forget the DTP-lines!
1519 * there height depends on the font of the nearest character */
1520 if (firstpar->line_bottom)
1521 maxdesc += 2 * lyxfont::ascent('x', GetFont(par, par->Last() - 1));
1523 /* and now the pagebreaks */
1524 if (firstpar->pagebreak_bottom)
1525 maxdesc += 3 * DefaultHeight();
1527 /* and now the layout spaces, for example before and after a section,
1528 * or between the items of a itemize or enumerate environment */
1529 if (!firstpar->pagebreak_bottom && row_ptr->par->Next()) {
1530 LyXParagraph * nextpar = row_ptr->par->Next();
1531 LyXParagraph * comparepar = row_ptr->par;
1535 if (comparepar->GetDepth() > nextpar->GetDepth()) {
1536 usual = (textclasslist.Style(buffer->params.textclass, comparepar->GetLayout()).bottomsep * DefaultHeight());
1537 comparepar = comparepar->DepthHook(nextpar->GetDepth());
1538 if (comparepar->GetLayout()!= nextpar->GetLayout()
1539 || nextpar->GetLabelWidthString() !=
1540 comparepar->GetLabelWidthString())
1541 unusual = (textclasslist.Style(buffer->params.textclass, comparepar->GetLayout()).bottomsep * DefaultHeight());
1543 if (unusual > usual)
1544 layoutdesc = unusual;
1548 else if (comparepar->GetDepth() == nextpar->GetDepth()) {
1550 if (comparepar->GetLayout()!= nextpar->GetLayout()
1551 || nextpar->GetLabelWidthString() !=
1552 comparepar->GetLabelWidthString())
1553 layoutdesc = int(textclasslist.Style(buffer->params.textclass, comparepar->GetLayout()).bottomsep * DefaultHeight());
1558 /* incalculate the layout spaces */
1559 maxasc += int(layoutasc * 2 / (2 + firstpar->GetDepth()));
1560 maxdesc += int(layoutdesc * 2 / (2 + firstpar->GetDepth()));
1562 /* table stuff -- begin*/
1563 if (row_ptr->par->table){
1564 maxasc += row_ptr->par->table->
1565 AdditionalHeight(NumberOfCell(row_ptr->par, row_ptr->pos));
1567 /* table stuff -- end*/
1569 /* calculate the new height of the text */
1570 height -= row_ptr->height;
1572 row_ptr->height = maxasc + maxdesc + labeladdon;
1573 row_ptr->baseline = maxasc + labeladdon;
1575 height += row_ptr->height;
1579 /* Appends the implicit specified paragraph behind the specified row,
1580 * start at the implicit given position */
1581 void LyXText::AppendParagraph(Row * row) const
1583 bool not_ready = true;
1585 // The last character position of a paragraph is an invariant so we can
1586 // safely get it here. (Asger)
1587 int lastposition = row->par->Last();
1590 // Get the next breakpoint
1591 int z = NextBreakPoint(row, paperwidth);
1595 // Insert the new row
1596 if (z < lastposition) {
1598 InsertRow(row, row->par, z);
1605 // Set the dimensions of the row
1606 tmprow->fill = Fill(tmprow, paperwidth);
1607 SetHeightOfRow(tmprow);
1609 } while (not_ready);
1613 void LyXText::BreakAgain(Row * row) const
1615 bool not_ready = true;
1618 /* get the next breakpoint */
1619 LyXParagraph::size_type z =
1620 NextBreakPoint(row, paperwidth);
1623 if (z < row->par->Last() ) {
1624 if (!row->next || (row->next && row->next->par != row->par)) {
1627 InsertRow(row, row->par, z);
1634 not_ready = false; // the rest will not change
1640 /* if there are some rows too much, delete them */
1641 /* only if you broke the whole paragraph! */
1642 Row * tmprow2 = row;
1643 while (tmprow2->next && tmprow2->next->par == row->par) {
1644 tmprow2 = tmprow2->next;
1646 while (tmprow2 != row) {
1647 tmprow2 = tmprow2->previous;
1648 RemoveRow(tmprow2->next);
1653 /* set the dimensions of the row */
1654 tmprow->fill = Fill(tmprow, paperwidth);
1655 SetHeightOfRow(tmprow);
1656 } while (not_ready);
1660 /* this is just a little changed version of break again */
1661 void LyXText::BreakAgainOneRow(Row * row)
1663 /* get the next breakpoint */
1664 LyXParagraph::size_type z = NextBreakPoint(row, paperwidth);
1667 if (z < row->par->Last() ) {
1668 if (!row->next || (row->next && row->next->par != row->par)) {
1669 /* insert a new row */
1671 InsertRow(row, row->par, z);
1683 /* if there are some rows too much, delete them */
1684 /* only if you broke the whole paragraph! */
1685 Row * tmprow2 = row;
1686 while (tmprow2->next && tmprow2->next->par == row->par) {
1687 tmprow2 = tmprow2->next;
1689 while (tmprow2 != row) {
1690 tmprow2 = tmprow2->previous;
1691 RemoveRow(tmprow2->next);
1695 /* set the dimensions of the row */
1696 tmprow->fill = Fill(tmprow, paperwidth);
1697 SetHeightOfRow(tmprow);
1701 void LyXText::BreakParagraph(char keep_layout)
1703 LyXLayout const & layout = textclasslist.Style(buffer->params.textclass,
1704 cursor.par->GetLayout());
1706 /* table stuff -- begin */
1707 if (cursor.par->table) {
1708 // breaking of tables is only allowed at the beginning or the end */
1709 if (cursor.pos && cursor.pos < cursor.par->size() &&
1710 !cursor.par->table->ShouldBeVeryLastCell(NumberOfCell(cursor.par, cursor.pos)))
1711 return; // no breaking of tables allowed
1713 /* table stuff -- end */
1715 // this is only allowed, if the current paragraph is not empty or caption
1716 if ((cursor.par->Last() <= 0 && !cursor.par->IsDummy())
1718 layout.labeltype!= LABEL_SENSITIVE)
1721 SetUndo(Undo::INSERT,
1722 cursor.par->ParFromPos(cursor.pos)->previous,
1723 cursor.par->ParFromPos(cursor.pos)->next);
1725 /* table stuff -- begin */
1726 if (cursor.par->table) {
1727 int cell = NumberOfCell(cursor.par, cursor.pos);
1728 if (cursor.par->table->ShouldBeVeryLastCell(cell))
1729 SetCursor(cursor.par, cursor.par->size());
1731 /* table stuff -- end */
1733 // please break always behind a space
1734 if (cursor.pos < cursor.par->Last()
1735 && cursor.par->IsLineSeparator(cursor.pos))
1738 // break the paragraph
1742 keep_layout = layout.isEnvironment();
1743 cursor.par->BreakParagraph(cursor.pos, keep_layout);
1745 /* table stuff -- begin */
1746 if (cursor.par->table){
1747 // the table should stay with the contents
1749 cursor.par->Next()->table = cursor.par->table;
1750 cursor.par->table = 0;
1753 /* table stuff -- end */
1755 // well this is the caption hack since one caption is really enough
1756 if (layout.labeltype == LABEL_SENSITIVE) {
1758 cursor.par->SetLayout(0); // set to standard-layout
1760 cursor.par->Next()->SetLayout(0); // set to standard-layout
1763 /* if the cursor is at the beginning of a row without prior newline,
1765 * This touches only the screen-update. Otherwise we would may have
1766 * an empty row on the screen */
1767 if (cursor.pos && !cursor.row->par->IsNewline(cursor.row->pos -1) &&
1768 cursor.row->pos == cursor.pos) {
1772 status = LyXText::NEED_MORE_REFRESH;
1773 refresh_row = cursor.row;
1774 refresh_y = cursor.y - cursor.row->baseline;
1776 // Do not forget the special right address boxes
1777 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1778 while (refresh_row->previous &&
1779 refresh_row->previous->par == refresh_row->par) {
1780 refresh_row = refresh_row->previous;
1781 refresh_y -= refresh_row->height;
1784 RemoveParagraph(cursor.row);
1786 // set the dimensions of the cursor row
1787 cursor.row->fill = Fill(cursor.row, paperwidth);
1789 SetHeightOfRow(cursor.row);
1791 while (!cursor.par->Next()->table && cursor.par->Next()->Last()
1792 && cursor.par->Next()->IsNewline(0))
1793 cursor.par->Next()->Erase(0);
1795 InsertParagraph(cursor.par->Next(), cursor.row);
1797 UpdateCounters(cursor.row->previous);
1799 /* This check is necessary. Otherwise the new empty paragraph will
1800 * be deleted automatically. And it is more friendly for the user! */
1802 SetCursor(cursor.par->Next(), 0);
1804 SetCursor(cursor.par, 0);
1806 if (cursor.row->next)
1807 BreakAgain(cursor.row->next);
1813 void LyXText::OpenFootnote()
1815 LyXParagraph * endpar,* tmppar;
1818 LyXParagraph * par = cursor.par->ParFromPos(cursor.pos);
1820 /* if there is no footnote in this paragraph, just return. */
1822 || par->next->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1825 /* ok, move the cursor right before the footnote */
1827 /* just a little faster than using CursorRight() */
1828 for (cursor.pos = 0;
1829 cursor.par->ParFromPos(cursor.pos) != par; cursor.pos++);
1830 /* now the cursor is at the beginning of the physical par */
1831 SetCursor(cursor.par,
1832 cursor.pos + cursor.par->ParFromPos(cursor.pos)->size());
1834 /* the cursor must be exactly before the footnote */
1835 par = cursor.par->ParFromPos(cursor.pos);
1837 status = LyXText::NEED_MORE_REFRESH;
1838 refresh_row = cursor.row;
1839 refresh_y = cursor.y - cursor.row->baseline;
1841 tmppar = cursor.par;
1842 endpar = cursor.par->Next();
1845 tmppar->OpenFootnote(cursor.pos);
1846 RemoveParagraph(row);
1847 /* set the dimensions of the cursor row */
1848 row->fill = Fill(row, paperwidth);
1849 SetHeightOfRow(row);
1850 // CHECK See comment on top of text.C
1851 tmppar = tmppar->Next();
1853 while (tmppar != endpar) {
1855 InsertParagraph(tmppar, row);
1856 while (row->next && row->next->par == tmppar)
1858 tmppar = tmppar->Next();
1861 SetCursor(par->next, 0);
1862 sel_cursor = cursor;
1866 /* table stuff -- begin*/
1868 void LyXText::TableFeatures(int feature, string const & val) const
1870 if (!cursor.par->table)
1871 return; /* this should never happen */
1873 int actCell = NumberOfCell(cursor.par, cursor.pos);
1874 SetUndo(Undo::FINISH,
1875 cursor.par->ParFromPos(cursor.pos)->previous,
1876 cursor.par->ParFromPos(cursor.pos)->next);
1879 case LyXTable::SET_PWIDTH:
1880 cursor.par->table->SetPWidth(actCell, val);
1882 case LyXTable::SET_SPECIAL_COLUMN:
1883 case LyXTable::SET_SPECIAL_MULTI:
1884 cursor.par->table->SetAlignSpecial(actCell, val, feature);
1893 void LyXText::TableFeatures(int feature) const
1896 int setAlign = LYX_ALIGN_LEFT;
1900 if (!cursor.par->table)
1901 return; /* this should never happen */
1903 int actCell = NumberOfCell(cursor.par, cursor.pos);
1904 SetUndo(Undo::FINISH,
1905 cursor.par->ParFromPos(cursor.pos)->previous,
1906 cursor.par->ParFromPos(cursor.pos)->next);
1909 case LyXTable::ALIGN_LEFT:
1910 setAlign= LYX_ALIGN_LEFT;
1912 case LyXTable::ALIGN_RIGHT:
1913 setAlign= LYX_ALIGN_RIGHT;
1915 case LyXTable::ALIGN_CENTER:
1916 setAlign= LYX_ALIGN_CENTER;
1922 case LyXTable::APPEND_ROW: {
1923 LyXParagraph::size_type pos = cursor.pos;
1925 /* move to the next row */
1926 int cell_org = actCell;
1927 int cell = cell_org;
1929 // if there is a ContRow following this row I have to add
1930 // the row after the ContRow's
1931 if ((pos < cursor.par->Last()) &&
1932 cursor.par->table->RowHasContRow(cell_org)) {
1933 while((pos < cursor.par->Last()) &&
1934 !cursor.par->table->IsContRow(cell)) {
1935 while (pos < cursor.par->Last() &&
1936 !cursor.par->IsNewline(pos))
1938 if (pos < cursor.par->Last())
1942 while((pos < cursor.par->Last()) &&
1943 cursor.par->table->IsContRow(cell)) {
1944 while (pos < cursor.par->Last() &&
1945 !cursor.par->IsNewline(pos))
1947 if (pos < cursor.par->Last())
1952 if (pos < cursor.par->Last())
1955 while (pos < cursor.par->Last() &&
1956 (cell == cell_org || !cursor.par->table->IsFirstCell(cell))){
1957 while (pos < cursor.par->Last() && !cursor.par->IsNewline(pos))
1959 if (pos < cursor.par->Last())
1964 /* insert the new cells */
1965 int number = cursor.par->table->NumberOfCellsInRow(cell_org);
1966 Language const * lang = cursor.par->getParLanguage();
1967 LyXFont font(LyXFont::ALL_INHERIT,lang);
1968 for (int i = 0; i < number; ++i) {
1969 cursor.par->InsertChar(pos, LyXParagraph::META_NEWLINE);
1970 cursor.par->SetFont(pos, font);
1973 /* append the row into the table */
1974 cursor.par->table->AppendRow(cell_org);
1978 case LyXTable::APPEND_CONT_ROW: {
1979 LyXParagraph::size_type pos = cursor.pos;
1980 /* move to the next row */
1981 int cell_org = actCell;
1982 int cell = cell_org;
1984 // if there is already a controw but not for this cell
1985 // the AppendContRow sets only the right values but does
1986 // not actually add a row
1987 if (cursor.par->table->RowHasContRow(cell_org) &&
1988 (cursor.par->table->CellHasContRow(cell_org)<0)) {
1989 cursor.par->table->AppendContRow(cell_org);
1993 while (pos < cursor.par->Last() &&
1995 || !cursor.par->table->IsFirstCell(cell))){
1996 while (pos < cursor.par->Last() && !cursor.par->IsNewline(pos))
1998 if (pos < cursor.par->Last())
2003 /* insert the new cells */
2004 int number = cursor.par->table->NumberOfCellsInRow(cell_org);
2005 Language const * lang = cursor.par->getParLanguage();
2006 LyXFont font(LyXFont::ALL_INHERIT,lang);
2007 for (int i = 0; i < number; ++i) {
2008 cursor.par->InsertChar(pos, LyXParagraph::META_NEWLINE);
2009 cursor.par->SetFont(pos, font);
2012 /* append the row into the table */
2013 cursor.par->table->AppendContRow(cell_org);
2017 case LyXTable::APPEND_COLUMN: {
2018 LyXParagraph::size_type pos = 0;
2019 int cell_org = actCell;
2021 Language const * lang = cursor.par->getParLanguage();
2022 LyXFont font(LyXFont::ALL_INHERIT,lang);
2024 if (pos && (cursor.par->IsNewline(pos-1))){
2025 if (cursor.par->table->AppendCellAfterCell(cell_org, cell)) {
2026 cursor.par->InsertChar(pos, LyXParagraph::META_NEWLINE);
2027 cursor.par->SetFont(pos, font);
2028 if (pos <= cursor.pos)
2035 } while (pos <= cursor.par->Last());
2036 /* remember that the very last cell doesn't end with a newline.
2037 This saves one byte memory per table ;-) */
2038 if (cursor.par->table->AppendCellAfterCell(cell_org, cell)) {
2039 LyXParagraph::size_type last = cursor.par->Last();
2040 cursor.par->InsertChar(last, LyXParagraph::META_NEWLINE);
2041 cursor.par->SetFont(last, font);
2044 /* append the column into the table */
2045 cursor.par->table->AppendColumn(cell_org);
2050 case LyXTable::DELETE_ROW:
2051 if (owner_->the_locking_inset)
2052 owner_->unlockInset(owner_->the_locking_inset);
2053 RemoveTableRow(&cursor);
2057 case LyXTable::DELETE_COLUMN: {
2058 LyXParagraph::size_type pos = 0;
2059 int cell_org = actCell;
2061 if (owner_->the_locking_inset)
2062 owner_->unlockInset(owner_->the_locking_inset);
2064 if (!pos || (cursor.par->IsNewline(pos-1))){
2065 if (cursor.par->table->DeleteCellIfColumnIsDeleted(cell, cell_org)){
2067 while (pos < cursor.par->Last() && !cursor.par->IsNewline(pos))
2068 cursor.par->Erase(pos);
2069 if (pos < cursor.par->Last())
2070 cursor.par->Erase(pos);
2072 cursor.par->Erase(pos - 1); // the missing newline at the end of a table
2073 --pos; // because of pos++ below
2078 } while (pos <= cursor.par->Last());
2080 /* delete the column from the table */
2081 cursor.par->table->DeleteColumn(cell_org);
2083 /* set the cursor to the beginning of the table, where else? */
2088 case LyXTable::TOGGLE_LINE_TOP:
2089 lineSet = !cursor.par->table->TopLine(actCell);
2091 cursor.par->table->SetTopLine(actCell, lineSet);
2093 LyXParagraph::size_type i;
2095 for (i = sel_start_cursor.pos; i <= sel_end_cursor.pos; ++i){
2096 if ((n = NumberOfCell(sel_start_cursor.par, i)) != m) {
2097 cursor.par->table->SetTopLine(n, lineSet);
2105 case LyXTable::TOGGLE_LINE_BOTTOM:
2106 lineSet = !cursor.par->table->BottomLine(actCell);
2108 cursor.par->table->SetBottomLine(actCell, lineSet);
2110 LyXParagraph::size_type i;
2112 for (i = sel_start_cursor.pos; i <= sel_end_cursor.pos; ++i) {
2113 if ((n = NumberOfCell(sel_start_cursor.par, i)) != m) {
2114 cursor.par->table->SetBottomLine(n, lineSet);
2122 case LyXTable::TOGGLE_LINE_LEFT:
2123 lineSet = !cursor.par->table->LeftLine(actCell);
2125 cursor.par->table->SetLeftLine(actCell, lineSet);
2127 LyXParagraph::size_type i;
2129 for (i = sel_start_cursor.pos; i <= sel_end_cursor.pos; ++i){
2130 if ((n= NumberOfCell(sel_start_cursor.par, i)) != m) {
2131 cursor.par->table->SetLeftLine(n, lineSet);
2139 case LyXTable::TOGGLE_LINE_RIGHT:
2140 lineSet = !cursor.par->table->RightLine(actCell);
2142 cursor.par->table->SetRightLine(actCell, lineSet);
2145 LyXParagraph::size_type i = sel_start_cursor.pos;
2146 for (; i <= sel_end_cursor.pos; ++i) {
2147 if ((n= NumberOfCell(sel_start_cursor.par, i)) != m) {
2148 cursor.par->table->SetRightLine(n, lineSet);
2156 case LyXTable::ALIGN_LEFT:
2157 case LyXTable::ALIGN_RIGHT:
2158 case LyXTable::ALIGN_CENTER:
2160 cursor.par->table->SetAlignment(actCell, setAlign);
2163 LyXParagraph::size_type i = sel_start_cursor.pos;
2164 for (; i <= sel_end_cursor.pos; ++i) {
2165 if ((n= NumberOfCell(sel_start_cursor.par, i)) != m) {
2166 cursor.par->table->SetAlignment(n, setAlign);
2174 case LyXTable::DELETE_TABLE:
2175 SetCursorIntern(cursor.par, 0);
2176 delete cursor.par->table;
2177 cursor.par->table = 0;
2178 // temporary: Should put table in simple_cut_buffer (with before and after
2179 // dummy-paragraph !!
2180 // not necessar anymore with UNDO :)
2181 for (LyXParagraph::size_type i =
2182 cursor.par->size() - 1; i >= 0; --i)
2183 cursor.par->Erase(i);
2187 case LyXTable::MULTICOLUMN: {
2189 // check wether we are completly in a multicol
2190 int multicol = cursor.par->table->IsMultiColumn(actCell);
2191 if (multicol && selection && sel_start_cursor.row == sel_end_cursor.row){
2192 multicol = NumberOfCell(sel_start_cursor.par, sel_start_cursor.pos)
2193 == NumberOfCell(sel_end_cursor.par, sel_end_cursor.pos);
2197 int newlines = cursor.par->table->UnsetMultiColumn(actCell);
2198 LyXParagraph::size_type pos = cursor.pos;
2199 while (pos < cursor.par->Last() && !cursor.par->IsNewline(pos))
2201 for (; newlines; --newlines)
2202 cursor.par->InsertChar(pos, LyXParagraph::META_NEWLINE);
2207 // selection must be in one row (or no selection)
2209 cursor.par->table->SetMultiColumn(NumberOfCell(cursor.par,
2216 if (sel_start_cursor.row == sel_end_cursor.row){
2217 LyXParagraph::size_type i;
2219 for (i = sel_start_cursor.pos;
2220 i < sel_end_cursor.pos; ++i){
2221 if (sel_start_cursor.par->IsNewline(i)){
2222 sel_start_cursor.par->Erase(i);
2223 // check for double-blanks
2224 if ((i && !sel_start_cursor.par->IsLineSeparator(i-1))
2226 (i < sel_start_cursor.par->Last()
2227 && !sel_start_cursor.par->IsLineSeparator(i)))
2228 sel_start_cursor.par->InsertChar(i, ' ');
2230 sel_end_cursor.pos--;
2237 SetMultiColumn(NumberOfCell(sel_start_cursor.par,
2238 sel_start_cursor.pos),
2240 cursor.pos = sel_start_cursor.pos;
2245 WriteAlert(_("Impossible Operation!"),
2246 _("Multicolumns can only be horizontally."),
2253 case LyXTable::SET_ALL_LINES:
2255 case LyXTable::UNSET_ALL_LINES:
2257 cursor.par->table->SetAllLines(NumberOfCell(cursor.par,
2261 LyXParagraph::size_type i;
2263 for (i = sel_start_cursor.pos; i <= sel_end_cursor.pos; ++i) {
2264 if ((n= NumberOfCell(sel_start_cursor.par, i)) != m) {
2265 cursor.par->table->SetAllLines(n, setLines);
2272 case LyXTable::SET_LONGTABLE:
2273 cursor.par->table->SetLongTable(true);
2275 case LyXTable::UNSET_LONGTABLE:
2276 cursor.par->table->SetLongTable(false);
2278 case LyXTable::SET_ROTATE_TABLE:
2279 cursor.par->table->SetRotateTable(true);
2281 case LyXTable::UNSET_ROTATE_TABLE:
2282 cursor.par->table->SetRotateTable(false);
2284 case LyXTable::SET_ROTATE_CELL:
2286 cursor.par->table->SetRotateCell(actCell, true);
2288 LyXParagraph::size_type i;
2290 for (i = sel_start_cursor.pos; i <= sel_end_cursor.pos; ++i){
2291 if ((n = NumberOfCell(sel_start_cursor.par, i)) != m) {
2292 cursor.par->table->SetRotateCell(n, true);
2298 case LyXTable::UNSET_ROTATE_CELL:
2300 cursor.par->table->SetRotateCell(actCell, false);
2303 LyXParagraph::size_type i = sel_start_cursor.pos;
2304 for (; i <= sel_end_cursor.pos; ++i) {
2305 if ((n= NumberOfCell(sel_start_cursor.par, i)) != m) {
2306 cursor.par->table->SetRotateCell(n, false);
2312 case LyXTable::SET_LINEBREAKS:
2313 what = !cursor.par->table->Linebreaks(cursor.par->table->FirstVirtualCell(actCell));
2315 cursor.par->table->SetLinebreaks(actCell, what);
2317 LyXParagraph::size_type i;
2319 for (i = sel_start_cursor.pos; i <= sel_end_cursor.pos; ++i) {
2320 if ((n = NumberOfCell(sel_start_cursor.par, i)) != m) {
2321 cursor.par->table->SetLinebreaks(n, what);
2327 case LyXTable::SET_LTFIRSTHEAD:
2328 cursor.par->table->SetLTHead(actCell, true);
2330 case LyXTable::SET_LTHEAD:
2331 cursor.par->table->SetLTHead(actCell, false);
2333 case LyXTable::SET_LTFOOT:
2334 cursor.par->table->SetLTFoot(actCell, false);
2336 case LyXTable::SET_LTLASTFOOT:
2337 cursor.par->table->SetLTFoot(actCell, true);
2339 case LyXTable::SET_LTNEWPAGE:
2340 what = !cursor.par->table->LTNewPage(actCell);
2341 cursor.par->table->SetLTNewPage(actCell, what);
2347 void LyXText::InsertCharInTable(char c)
2352 bool jumped_over_space;
2354 /* first check, if there will be two blanks together or a blank at
2355 * the beginning of a paragraph.
2356 * I decided to handle blanks like normal characters, the main
2357 * difference are the special checks when calculating the row.fill
2358 * (blank does not count at the end of a row) and the check here */
2360 LyXFont realtmpfont = real_current_font;
2361 LyXFont rawtmpfont = current_font; /* store the current font.
2362 * This is because of the use
2363 * of cursor movements. The moving
2364 * cursor would refresh the
2367 // Get the font that is used to calculate the baselineskip
2368 LyXParagraph::size_type const lastpos =
2370 LyXFont rawparfont = cursor.par->GetFontSettings(lastpos - 1);
2372 jumped_over_space = false;
2373 if (IsLineSeparatorChar(c)) {
2374 if ((cursor.pos > 0 &&
2375 cursor.par->IsLineSeparator(cursor.pos - 1))
2376 || (cursor.pos > 0 && cursor.par->IsNewline(cursor.pos - 1))
2377 || (cursor.pos == 0 &&
2378 !(cursor.par->Previous()
2379 && cursor.par->Previous()->footnoteflag
2380 == LyXParagraph::OPEN_FOOTNOTE)))
2382 } else if (IsNewlineChar(c)) {
2383 if (!IsEmptyTableCell()) {
2384 TableFeatures(LyXTable::APPEND_CONT_ROW);
2391 y = cursor.y - row->baseline;
2392 if (c != LyXParagraph::META_INSET) /* in this case LyXText::InsertInset
2393 * already inserted the character */
2394 cursor.par->InsertChar(cursor.pos, c);
2395 SetCharFont(cursor.par, cursor.pos, rawtmpfont);
2397 if (!jumped_over_space) {
2398 /* refresh the positions */
2400 while (tmprow->next && tmprow->next->par == row->par) {
2401 tmprow = tmprow->next;
2408 CheckParagraphInTable(cursor.par, cursor.pos);
2410 current_font = rawtmpfont;
2411 real_current_font = realtmpfont;
2413 /* check, whether the last character's font has changed. */
2414 if (cursor.pos && cursor.pos == cursor.par->Last()
2415 && rawparfont != rawtmpfont)
2416 RedoHeightOfParagraph(cursor);
2420 void LyXText::CheckParagraphInTable(LyXParagraph * par,
2421 LyXParagraph::size_type pos)
2424 if (par->GetChar(pos) == LyXParagraph::META_INSET &&
2425 par->GetInset(pos) && par->GetInset(pos)->display()){
2426 par->GetInset(pos)->display(false);
2430 Row * row = GetRow(par, pos, y);
2432 int tmpheight = row->height;
2433 SetHeightOfRow(row);
2435 LyXParagraph::size_type tmp_pos = pos;
2436 /* update the table information */
2437 while (tmp_pos && !par->IsNewline(tmp_pos - 1))
2439 if (par->table->SetWidthOfCell(NumberOfCell(par, pos),
2440 WidthOfCell(par, tmp_pos))) {
2441 LyXCursor tmpcursor = cursor;
2442 SetCursorIntern(par, pos);
2443 /* make a complete redraw */
2444 RedoDrawingOfParagraph(cursor);
2448 /* redraw only the row */
2449 LyXCursor tmpcursor = cursor;
2450 SetCursorIntern(par, pos);
2451 //CHECK See comment on top of text.C
2453 refresh_x = cursor.x;
2455 refresh_pos = cursor.pos;
2458 if (tmpheight == row->height)
2459 status = LyXText::NEED_VERY_LITTLE_REFRESH;
2461 status = LyXText::NEED_MORE_REFRESH;
2463 SetCursorIntern(cursor.par, cursor.pos);
2467 void LyXText::BackspaceInTable()
2469 Row * tmprow, * row;
2472 LyXFont rawtmpfont = current_font;
2473 LyXFont realtmpfont = real_current_font;
2475 // Get the font that is used to calculate the baselineskip
2476 int const lastpos = cursor.par->Last();
2477 LyXFont rawparfont = cursor.par->GetFontSettings(lastpos - 1);
2479 if (cursor.pos == 0) {
2480 /* no pasting of table paragraphs */
2484 /* this is the code for a normal backspace, not pasting
2486 SetUndo(Undo::DELETE,
2487 cursor.par->ParFromPos(cursor.pos)->previous,
2488 cursor.par->ParFromPos(cursor.pos)->next);
2492 /* some insets are undeletable here */
2493 if (cursor.par->GetChar(cursor.pos) == LyXParagraph::META_INSET) {
2494 if (!cursor.par->GetInset(cursor.pos)->Deletable())
2499 y = cursor.y - row->baseline;
2501 /* some special code when deleting a newline. */
2502 if (cursor.par->IsNewline(cursor.pos)) {
2506 cursor.par->Erase(cursor.pos);
2508 /* refresh the positions */
2510 while (tmprow->next && tmprow->next->par == row->par) {
2511 tmprow = tmprow->next;
2516 CheckParagraphInTable(cursor.par, cursor.pos);
2518 /* check, wether the last characters font has changed. */
2519 if (cursor.pos && cursor.pos == cursor.par->Last()
2520 && rawparfont != rawtmpfont)
2521 RedoHeightOfParagraph(cursor);
2523 /* restore the current font
2524 * That is what a user expects! */
2525 current_font = rawtmpfont;
2526 real_current_font = realtmpfont;
2528 SetCursorIntern(cursor.par, cursor.pos);
2531 /* table stuff -- end*/
2534 /* just a macro to make some thing easier. */
2535 void LyXText::RedoParagraph() const
2538 RedoParagraphs(cursor, cursor.par->Next());
2539 SetCursorIntern(cursor.par, cursor.pos);
2543 /* insert a character, moves all the following breaks in the
2544 * same Paragraph one to the right and make a rebreak */
2545 void LyXText::InsertChar(char c)
2547 SetUndo(Undo::INSERT,
2548 cursor.par->ParFromPos(cursor.pos)->previous,
2549 cursor.par->ParFromPos(cursor.pos)->next);
2551 /* When the free-spacing option is set for the current layout,
2552 * disable the double-space checking */
2555 textclasslist.Style(buffer->params.textclass,
2556 cursor.row->par->GetLayout()).free_spacing;
2558 /* table stuff -- begin*/
2559 if (cursor.par->table) {
2560 InsertCharInTable(c);
2564 /* table stuff -- end*/
2566 /* First check, if there will be two blanks together or a blank at
2567 the beginning of a paragraph.
2568 I decided to handle blanks like normal characters, the main
2569 difference are the special checks when calculating the row.fill
2570 (blank does not count at the end of a row) and the check here */
2572 // The bug is triggered when we type in a description environment:
2573 // The current_font is not changed when we go from label to main text
2574 // and it should (along with realtmpfont) when we type the space.
2575 // CHECK There is a bug here! (Asger)
2577 LyXFont realtmpfont = real_current_font;
2578 LyXFont rawtmpfont = current_font; /* store the current font.
2579 * This is because of the use
2580 * of cursor movements. The moving
2581 * cursor would refresh the
2584 // Get the font that is used to calculate the baselineskip
2585 LyXParagraph::size_type lastpos = cursor.par->Last();
2586 LyXFont rawparfont = cursor.par->GetFontSettings(lastpos - 1);
2588 bool jumped_over_space = false;
2590 if (!freeSpacing && IsLineSeparatorChar(c)) {
2592 && cursor.par->IsLineSeparator(cursor.pos - 1))
2594 && cursor.par->IsNewline(cursor.pos - 1))
2596 && !(cursor.par->Previous()
2597 && cursor.par->Previous()->footnoteflag
2598 == LyXParagraph::OPEN_FOOTNOTE))) {
2599 if (cursor.pos == 0 )
2600 owner_->owner()->getMiniBuffer()->Set(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
2602 owner_->owner()->getMiniBuffer()->Set(_("You cannot type two spaces this way. Please read the Tutorial."));
2606 } else if (IsNewlineChar(c)) {
2607 if (cursor.par->FirstPhysicalPar() == cursor.par
2608 && cursor.pos <= BeginningOfMainBody(cursor.par)) {
2612 /* No newline at first position
2613 * of a paragraph or behind labels.
2614 * TeX does not allow that. */
2616 if (cursor.pos < cursor.par->Last() &&
2617 cursor.par->IsLineSeparator(cursor.pos))
2618 CursorRightIntern(); // newline always after a blank!
2619 cursor.row->fill = -1; // to force a new break
2622 // the display inset stuff
2623 if (cursor.row->par->GetChar(cursor.row->pos) == LyXParagraph::META_INSET
2624 && cursor.row->par->GetInset(cursor.row->pos)
2625 && cursor.row->par->GetInset(cursor.row->pos)->display())
2626 cursor.row->fill = -1; // to force a new break
2628 // get the cursor row fist
2629 Row * row = cursor.row;
2630 long y = cursor.y - row->baseline;
2631 if (c != LyXParagraph::META_INSET) /* Here case LyXText::InsertInset
2632 * already insertet the character */
2633 cursor.par->InsertChar(cursor.pos, c);
2634 SetCharFont(cursor.par, cursor.pos, rawtmpfont);
2636 if (!jumped_over_space) {
2637 // refresh the positions
2639 while (tmprow->next && tmprow->next->par == row->par) {
2640 tmprow = tmprow->next;
2645 // Is there a break one row above
2646 if ((cursor.par->IsLineSeparator(cursor.pos)
2647 || cursor.par->IsNewline(cursor.pos)
2648 || cursor.row->fill == -1)
2649 && row->previous && row->previous->par == row->par) {
2650 LyXParagraph::size_type z = NextBreakPoint(row->previous,
2652 if ( z >= row->pos) {
2655 // set the dimensions of the row above
2656 row->previous->fill = Fill(row->previous, paperwidth);
2658 SetHeightOfRow(row->previous);
2660 y -= row->previous->height;
2662 refresh_row = row->previous;
2663 status = LyXText::NEED_MORE_REFRESH;
2665 BreakAgainOneRow(row);
2667 current_font = rawtmpfont;
2668 real_current_font = realtmpfont;
2669 SetCursor(cursor.par, cursor.pos + 1, false);
2670 /* cursor MUST be in row now */
2672 if (row->next && row->next->par == row->par)
2673 need_break_row = row->next;
2677 // check, wether the last characters font has changed.
2678 if (cursor.pos && cursor.pos == cursor.par->Last()
2679 && rawparfont != rawtmpfont)
2680 RedoHeightOfParagraph(cursor);
2687 /* recalculate the fill of the row */
2688 if (row->fill >= 0) /* needed because a newline
2689 * will set fill to -1. Otherwise
2690 * we would not get a rebreak! */
2691 row->fill = Fill(row, paperwidth);
2692 if (row->fill < 0 ) {
2695 refresh_x = cursor.x;
2696 refresh_pos = cursor.pos;
2697 status = LyXText::NEED_MORE_REFRESH;
2698 BreakAgainOneRow(row);
2699 /* will the cursor be in another row now? */
2700 if (RowLast(row) <= cursor.pos + 1 && row->next) {
2701 if (row->next && row->next->par == row->par)
2705 BreakAgainOneRow(row);
2707 current_font = rawtmpfont;
2708 real_current_font = realtmpfont;
2709 SetCursor(cursor.par, cursor.pos + 1, false);
2710 if (row->next && row->next->par == row->par)
2711 need_break_row = row->next;
2716 refresh_x = cursor.x;
2718 refresh_pos = cursor.pos;
2720 int tmpheight = row->height;
2721 SetHeightOfRow(row);
2722 if (tmpheight == row->height)
2723 status = LyXText::NEED_VERY_LITTLE_REFRESH;
2725 status = LyXText::NEED_MORE_REFRESH;
2727 current_font = rawtmpfont;
2728 real_current_font = realtmpfont;
2729 SetCursor(cursor.par, cursor.pos + 1, false);
2732 /* check, wether the last characters font has changed. */
2733 if (cursor.pos && cursor.pos == cursor.par->Last()
2734 && rawparfont != rawtmpfont) {
2735 RedoHeightOfParagraph(cursor);
2737 /* now the special right address boxes */
2738 if (textclasslist.Style(buffer->params.textclass,
2739 cursor.par->GetLayout()).margintype
2740 == MARGIN_RIGHT_ADDRESS_BOX) {
2741 RedoDrawingOfParagraph(cursor);
2749 void LyXText::charInserted()
2751 // Here we could call FinishUndo for every 20 characters inserted.
2752 // This is from my experience how emacs does it.
2753 static unsigned int counter = 0;
2762 void LyXText::PrepareToPrint(Row * row, float & x,
2763 float & fill_separator,
2765 float & fill_label_hfill,
2770 float w = row->fill;
2772 fill_label_hfill = 0;
2774 fill_label_hfill = 0;
2776 bool is_rtl = row->par->isRightToLeftPar();
2779 x = RightMargin(row);
2780 if (row->par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
2781 LyXFont font(LyXFont::ALL_SANE);
2782 font.setSize(LyXFont::SIZE_SMALL);
2783 x += lyxfont::width("Mwide-figM", font);
2786 x = LeftMargin(row);
2788 /* is there a manual margin with a manual label */
2789 if (textclasslist.Style(buffer->params.textclass,
2790 row->par->GetLayout()).margintype == MARGIN_MANUAL
2791 && textclasslist.Style(buffer->params.textclass,
2792 row->par->GetLayout()).labeltype == LABEL_MANUAL) {
2794 nlh = NumberOfLabelHfills(row) + 1; /* one more since labels
2795 * are left aligned*/
2796 if (nlh && !row->par->GetLabelWidthString().empty()) {
2797 fill_label_hfill = LabelFill(row) / nlh;
2801 /* are there any hfills in the row? */
2802 nh = NumberOfHfills(row);
2804 /* table stuff -- begin*/
2805 if (row->par->table) {
2806 w = paperwidth - row->par->table->WidthOfTable()
2807 - x - RightMargin(row);
2808 nh = 0; /* ignore hfills in tables */
2810 /* table stuff -- end*/
2815 /* is it block, flushleft or flushright?
2816 * set x how you need it */
2818 if (row->par->FirstPhysicalPar()->align == LYX_ALIGN_LAYOUT)
2819 align = textclasslist.Style(buffer->params.textclass, row->par->GetLayout()).align;
2821 align = row->par->FirstPhysicalPar()->align;
2823 /* center displayed insets */
2824 if (row->par->GetChar(row->pos) == LyXParagraph::META_INSET
2825 && row->par->GetInset(row->pos)
2826 && row->par->GetInset(row->pos)->display())
2827 align = LYX_ALIGN_CENTER;
2830 case LYX_ALIGN_BLOCK:
2831 ns = NumberOfSeparators(row);
2832 if (ns && row->next && row->next->par == row->par &&
2833 !(row->next->par->IsNewline(row->next->pos-1))
2834 && !(row->next->par->GetChar(row->next->pos) == LyXParagraph::META_INSET
2835 && row->next->par->GetInset(row->next->pos)
2836 && row->next->par->GetInset(row->next->pos)->display())
2838 fill_separator = w / ns;
2842 case LYX_ALIGN_RIGHT:
2845 case LYX_ALIGN_CENTER:
2853 ComputeBidiTables(row);
2855 LyXParagraph::size_type main_body =
2856 BeginningOfMainBody(row->par);
2857 LyXParagraph::size_type last = RowLast(row);
2859 if (main_body > 0 &&
2860 (main_body-1 > last ||
2861 !row->par->IsLineSeparator(main_body-1))) {
2862 LyXLayout const & layout = textclasslist.Style(buffer->params.textclass,
2863 row->par->GetLayout());
2864 x += lyxfont::width(layout.labelsep,
2865 GetFont(row->par, -2));
2866 if (main_body-1 <= last)
2867 x += fill_label_hfill;
2872 /* important for the screen */
2875 /* the cursor set functions have a special mechanism. When they
2876 * realize, that you left an empty paragraph, they will delete it.
2877 * They also delete the corresponding row */
2879 void LyXText::CursorRightOneWord() const
2881 // treat floats, HFills and Insets as words
2882 LyXCursor tmpcursor = cursor;
2883 // CHECK See comment on top of text.C
2885 if (tmpcursor.pos == tmpcursor.par->Last()
2886 && tmpcursor.par->Next())
2888 tmpcursor.par = tmpcursor.par->Next();
2893 // Skip through initial nonword stuff.
2894 while ( tmpcursor.pos < tmpcursor.par->Last() &&
2895 ! tmpcursor.par->IsWord( tmpcursor.pos ) )
2897 // printf("Current pos1 %d", tmpcursor.pos) ;
2901 // Advance through word.
2902 while ( tmpcursor.pos < tmpcursor.par->Last() &&
2903 tmpcursor.par->IsWord( tmpcursor.pos ) )
2905 // printf("Current pos2 %d", tmpcursor.pos) ;
2910 SetCursor(tmpcursor.par, tmpcursor.pos);
2914 void LyXText::CursorTab() const
2916 if (cursor.par->table) {
2917 int cell = NumberOfCell(cursor.par, cursor.pos);
2918 while(cursor.par->table->IsContRow(cell)) {
2920 cell = NumberOfCell(cursor.par, cursor.pos);
2922 if (cursor.par->table->ShouldBeVeryLastCell(cell))
2923 TableFeatures(LyXTable::APPEND_ROW);
2925 LyXCursor tmpcursor = cursor;
2926 while (tmpcursor.pos < tmpcursor.par->Last()
2927 && !tmpcursor.par->IsNewline(tmpcursor.pos))
2930 if (tmpcursor.pos == tmpcursor.par->Last()){
2931 if (tmpcursor.par->Next()) {
2932 tmpcursor.par = tmpcursor.par->Next();
2938 SetCursor(tmpcursor.par, tmpcursor.pos);
2939 if (cursor.par->table) {
2940 int cell = NumberOfCell(cursor.par, cursor.pos);
2941 while (cursor.par->table->IsContRow(cell) &&
2942 !cursor.par->table->ShouldBeVeryLastCell(cell)) {
2944 while (tmpcursor.pos < tmpcursor.par->Last()
2945 && !tmpcursor.par->IsNewline(tmpcursor.pos))
2948 if (tmpcursor.pos == tmpcursor.par->Last()){
2949 if (tmpcursor.par->Next()) {
2950 tmpcursor.par = tmpcursor.par->Next();
2956 SetCursor(tmpcursor.par, tmpcursor.pos);
2957 cell = NumberOfCell(cursor.par, cursor.pos);
2963 /* -------> Skip initial whitespace at end of word and move cursor to *start*
2964 of prior word, not to end of next prior word. */
2966 void LyXText::CursorLeftOneWord() const
2968 // treat HFills, floats and Insets as words
2969 LyXCursor tmpcursor = cursor;
2970 while (tmpcursor.pos
2971 && (tmpcursor.par->IsSeparator(tmpcursor.pos - 1)
2972 || tmpcursor.par->IsKomma(tmpcursor.pos - 1))
2973 && !(tmpcursor.par->IsHfill(tmpcursor.pos - 1)
2974 || tmpcursor.par->IsFloat(tmpcursor.pos - 1)
2975 || tmpcursor.par->IsInset(tmpcursor.pos - 1)))
2979 && (tmpcursor.par->IsInset(tmpcursor.pos - 1)
2980 || tmpcursor.par->IsFloat(tmpcursor.pos - 1)
2981 || tmpcursor.par->IsHfill(tmpcursor.pos - 1))) {
2983 } else if (!tmpcursor.pos) {
2984 if (tmpcursor.par->Previous()){
2985 tmpcursor.par = tmpcursor.par->Previous();
2986 tmpcursor.pos = tmpcursor.par->Last();
2988 } else { // Here, tmpcursor != 0
2989 while (tmpcursor.pos > 0 &&
2990 tmpcursor.par->IsWord(tmpcursor.pos-1) )
2993 SetCursor(tmpcursor.par, tmpcursor.pos);
2996 /* -------> Select current word. This depends on behaviour of CursorLeftOneWord(), so it is
2999 void LyXText::SelectWord()
3001 // Move cursor to the beginning, when not already there.
3003 && !cursor.par->IsSeparator(cursor.pos-1)
3004 && !cursor.par->IsKomma(cursor.pos-1) )
3005 CursorLeftOneWord();
3007 // set the sel cursor
3008 sel_cursor = cursor;
3010 while ( cursor.pos < cursor.par->Last()
3011 && !cursor.par->IsSeparator(cursor.pos)
3012 && !cursor.par->IsKomma(cursor.pos) )
3014 SetCursor( cursor.par, cursor.pos );
3016 // finally set the selection
3021 /* -------> Select the word currently under the cursor when:
3022 1: no selection is currently set,
3023 2: the cursor is not at the borders of the word. */
3025 bool LyXText::SelectWordWhenUnderCursor()
3028 cursor.pos > 0 && cursor.pos < cursor.par->Last()
3029 && !cursor.par->IsSeparator(cursor.pos)
3030 && !cursor.par->IsKomma(cursor.pos)
3031 && !cursor.par->IsSeparator(cursor.pos -1)
3032 && !cursor.par->IsKomma(cursor.pos -1) ) {
3040 // This function is only used by the spellchecker for NextWord().
3041 // It doesn't handle LYX_ACCENTs and probably never will.
3042 char * LyXText::SelectNextWord(float & value)
3044 LyXParagraph * tmppar = cursor.par;
3046 // If this is not the very first word, skip rest of
3047 // current word because we are probably in the middle
3048 // of a word if there is text here.
3049 if (cursor.pos || cursor.par->previous) {
3050 while (cursor.pos < cursor.par->Last()
3051 && cursor.par->IsLetter(cursor.pos))
3054 // Now, skip until we have real text (will jump paragraphs)
3055 while ((cursor.par->Last() > cursor.pos
3056 && (!cursor.par->IsLetter(cursor.pos)
3057 || cursor.par->getFont(cursor.pos).latex() == LyXFont::ON))
3058 || (cursor.par->Last() == cursor.pos
3059 && cursor.par->Next())){
3060 if (cursor.pos == cursor.par->Last()) {
3061 cursor.par = cursor.par->Next();
3068 // Update the value if we changed paragraphs
3069 if (cursor.par != tmppar){
3070 SetCursor(cursor.par, cursor.pos);
3071 value = float(cursor.y)/float(height);
3074 /* Start the selection from here */
3075 sel_cursor = cursor;
3078 std::ostringstream latex;
3082 /* and find the end of the word
3083 (optional hyphens are part of a word) */
3084 while (cursor.pos < cursor.par->Last()
3085 && (cursor.par->IsLetter(cursor.pos))
3086 || (cursor.par->GetChar(cursor.pos) == LyXParagraph::META_INSET
3087 && cursor.par->GetInset(cursor.pos) != 0
3088 && cursor.par->GetInset(cursor.pos)->Latex(latex, false, false) == 0
3090 && latex.str() == "\\-"
3092 && string(latex.str(), 3) == "\\-" // this is not nice at all
3097 #ifndef HAVE_SSTREAM
3098 delete [] latex.str();
3100 // Finally, we copy the word to a string and return it
3103 if (sel_cursor.pos < cursor.pos) {
3104 str = new char [cursor.pos - sel_cursor.pos + 2];
3105 LyXParagraph::size_type i, j;
3106 for (i = sel_cursor.pos, j = 0; i < cursor.pos; ++i) {
3107 if (cursor.par->GetChar(i) != LyXParagraph::META_INSET)
3108 str[j++] = cursor.par->GetChar(i);
3116 // This one is also only for the spellchecker
3117 void LyXText::SelectSelectedWord()
3119 /* move cursor to the beginning */
3120 SetCursor(sel_cursor.par, sel_cursor.pos);
3122 /* set the sel cursor */
3123 sel_cursor = cursor;
3126 std::ostringstream latex;
3131 /* now find the end of the word */
3132 while (cursor.pos < cursor.par->Last()
3133 && (cursor.par->IsLetter(cursor.pos)
3134 || (cursor.par->GetChar(cursor.pos) == LyXParagraph::META_INSET
3135 && cursor.par->GetInset(cursor.pos) != 0
3136 && cursor.par->GetInset(cursor.pos)->Latex(latex, false, false) == 0
3138 && latex.str() == "\\-"
3140 && string(latex.str(), 3) == "\\-"
3145 #ifndef HAVE_SSTREAM
3146 delete [] latex.str();
3148 SetCursor(cursor.par, cursor.pos);
3150 /* finally set the selection */
3155 /* -------> Delete from cursor up to the end of the current or next word. */
3156 void LyXText::DeleteWordForward()
3158 LyXCursor tmpcursor = cursor;
3160 if (!cursor.par->Last())
3162 // CHECK See comment on top of text.C
3164 /* -------> Skip initial non-word stuff. */
3165 while ( cursor.pos < cursor.par->Last()
3166 && (cursor.par->IsSeparator(cursor.pos)
3167 || cursor.par->IsKomma(cursor.pos)) )
3170 SetCursorIntern(cursor.par, cursor.pos);
3171 selection = True; // to avoid deletion
3172 CursorRightOneWord();
3173 sel_cursor = cursor;
3177 /* -----> Great, CutSelection() gets rid of multiple spaces. */
3183 /* -------> Delete from cursor to start of current or prior word. */
3184 void LyXText::DeleteWordBackward()
3186 LyXCursor tmpcursor = cursor;
3187 if (!cursor.par->Last())
3189 // CHECK See comment on top of text.C
3191 selection = true; // to avoid deletion
3192 CursorLeftOneWord();
3193 sel_cursor = cursor;
3201 /* -------> Kill to end of line. */
3202 void LyXText::DeleteLineForward()
3204 LyXCursor tmpcursor = cursor;
3205 if (!cursor.par->Last())
3207 // CHECK See comment on top of text.C
3210 sel_cursor = cursor;
3213 if (selection == false) {
3214 DeleteWordForward();
3222 // Change the case of a word at cursor position. The meaning of action
3224 // 0 change to lowercase
3225 // 1 capitalize word
3226 // 2 change to uppercase
3227 // This function directly manipulates LyXParagraph::text because there
3228 // is no LyXParagraph::SetChar currently. I did what I could to ensure
3229 // that it is correct. I guess part of it should be moved to
3230 // LyXParagraph, but it will have to change for 1.1 anyway. At least
3231 // it does not access outside of the allocated array as the older
3232 // version did. (JMarc)
3233 void LyXText::ChangeWordCase(LyXText::TextCase action)
3235 LyXParagraph * tmppar = cursor.par->ParFromPos(cursor.pos);
3237 SetUndo(Undo::FINISH, tmppar->previous, tmppar->next);
3239 LyXParagraph::size_type tmppos =
3240 cursor.par->PositionInParFromPos(cursor.pos);
3241 while (tmppos < tmppar->size()) {
3242 unsigned char c = tmppar->GetChar(tmppos);
3243 if (IsKommaChar(c) || IsLineSeparatorChar(c))
3245 if (c != LyXParagraph::META_INSET) {
3247 case text_lowercase:
3250 case text_capitalization:
3252 action = text_lowercase;
3254 case text_uppercase:
3260 //tmppar->text[tmppos] = c;
3261 tmppar->SetChar(tmppos, c);
3264 CheckParagraph(tmppar, tmppos);
3265 CursorRightOneWord();
3269 void LyXText::Delete()
3271 // this is a very easy implementation
3273 LyXCursor old_cursor = cursor;
3274 int old_cur_par_id = old_cursor.par->id();
3275 int old_cur_par_prev_id = old_cursor.par->previous ?
3276 old_cursor.par->previous->id() : 0;
3278 // just move to the right
3279 CursorRightIntern();
3281 // CHECK Look at the comment here.
3282 // This check is not very good...
3283 // The CursorRightIntern calls DeleteEmptyParagrapgMechanism
3284 // and that can very well delete the par or par->previous in
3285 // old_cursor. Will a solution where we compare paragraph id's
3287 if ((cursor.par->previous ? cursor.par->previous->id() : 0)
3288 == old_cur_par_prev_id
3289 && cursor.par->id() != old_cur_par_id)
3290 return; // delete-empty-paragraph-mechanism has done it
3292 // if you had success make a backspace
3293 if (old_cursor.par != cursor.par || old_cursor.pos != cursor.pos) {
3294 LyXCursor tmpcursor = cursor;
3295 cursor = old_cursor; // to make sure undo gets the right cursor position
3296 SetUndo(Undo::DELETE,
3297 cursor.par->ParFromPos(cursor.pos)->previous,
3298 cursor.par->ParFromPos(cursor.pos)->next);
3305 void LyXText::Backspace()
3307 /* table stuff -- begin */
3308 if (cursor.par->table) {
3312 /* table stuff -- end */
3314 LyXFont rawtmpfont = current_font;
3315 LyXFont realtmpfont = real_current_font;
3317 // Get the font that is used to calculate the baselineskip
3318 int const lastpos = cursor.par->Last();
3319 LyXFont rawparfont = cursor.par->GetFontSettings(lastpos - 1);
3321 if (cursor.pos == 0) {
3322 // The cursor is at the beginning of a paragraph, so the the backspace
3323 // will collapse two paragraphs into one.
3325 // we may paste some paragraphs
3327 // is it an empty paragraph?
3330 || (lastpos == 1 && cursor.par->IsSeparator(0)))
3331 && !(cursor.par->Next()
3332 && cursor.par->footnoteflag == LyXParagraph::NO_FOOTNOTE
3333 && cursor.par->Next()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE)) {
3334 // This is an empty paragraph and we delete it just by moving the cursor one step
3335 // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
3336 // of the paragraph.
3338 if (cursor.par->previous) {
3339 LyXParagraph * tmppar = cursor.par->previous->FirstPhysicalPar();
3340 if (cursor.par->GetLayout() == tmppar->GetLayout()
3341 && cursor.par->footnoteflag == tmppar->footnoteflag
3342 && cursor.par->GetAlign() == tmppar->GetAlign()) {
3343 // Inherit botom DTD from the paragraph below.
3344 // (the one we are deleting)
3345 tmppar->line_bottom = cursor.par->line_bottom;
3346 tmppar->added_space_bottom = cursor.par->added_space_bottom;
3347 tmppar->pagebreak_bottom = cursor.par->pagebreak_bottom;
3352 // the layout things can change the height of a row !
3353 int tmpheight = cursor.row->height;
3354 SetHeightOfRow(cursor.row);
3355 if (cursor.row->height != tmpheight) {
3356 refresh_y = cursor.y - cursor.row->baseline;
3357 refresh_row = cursor.row;
3358 status = LyXText::NEED_MORE_REFRESH;
3364 if (cursor.par->ParFromPos(cursor.pos)->previous){
3365 SetUndo(Undo::DELETE,
3366 cursor.par->ParFromPos(cursor.pos)->previous->previous,
3367 cursor.par->ParFromPos(cursor.pos)->next);
3370 LyXParagraph * tmppar = cursor.par;
3371 Row * tmprow = cursor.row;
3373 // We used to do CursorLeftIntern() here, but it is
3374 // not a good idea since it triggers the auto-delete
3375 // mechanism. So we do a CursorLeftIntern()-lite,
3376 // without the dreaded mechanism. (JMarc)
3377 if (cursor.par->Previous()) {
3378 // steps into the above paragraph.
3379 SetCursorIntern(cursor.par->Previous(),
3380 cursor.par->Previous()->Last());
3383 /* Pasting is not allowed, if the paragraphs have different
3384 layout. I think it is a real bug of all other
3385 word processors to allow it. It confuses the user.
3386 Even so with a footnote paragraph and a non-footnote
3387 paragraph. I will not allow pasting in this case,
3388 because the user would be confused if the footnote behaves
3389 different wether it is open or closed.
3391 Correction: Pasting is always allowed with standard-layout
3393 if (cursor.par != tmppar
3394 && (cursor.par->GetLayout() == tmppar->GetLayout()
3395 || tmppar->GetLayout() == 0 /*standard*/)
3396 && cursor.par->footnoteflag == tmppar->footnoteflag
3397 /* table stuff -- begin*/
3398 && !cursor.par->table /* no pasting of tables */
3399 /* table stuff -- end*/
3400 && cursor.par->GetAlign() == tmppar->GetAlign()) {
3402 RemoveParagraph(tmprow);
3404 cursor.par->PasteParagraph();
3406 if (!cursor.pos || !cursor.par->IsSeparator(cursor.pos - 1))
3407 ; //cursor.par->InsertChar(cursor.pos, ' ');
3408 // strangely enough it seems that commenting out the line above removes
3409 // most or all of the segfaults. I will however also try to move the
3410 // two Remove... lines in front of the PasteParagraph too.
3415 status = LyXText::NEED_MORE_REFRESH;
3416 refresh_row = cursor.row;
3417 refresh_y = cursor.y - cursor.row->baseline;
3419 // remove the lost paragraph
3420 // This one is not safe, since the paragraph that the tmprow and the
3421 // following rows belong to has been deleted by the PasteParagraph
3422 // above. The question is... could this be moved in front of the
3424 //RemoveParagraph(tmprow);
3425 //RemoveRow(tmprow);
3427 AppendParagraph(cursor.row); // This rebuilds the rows.
3428 UpdateCounters(cursor.row);
3430 // the row may have changed, block, hfills etc.
3431 SetCursor(cursor.par, cursor.pos);
3434 /* this is the code for a normal backspace, not pasting
3436 SetUndo(Undo::DELETE,
3437 cursor.par->ParFromPos(cursor.pos)->previous,
3438 cursor.par->ParFromPos(cursor.pos)->next);
3439 // We used to do CursorLeftIntern() here, but it is
3440 // not a good idea since it triggers the auto-delete
3441 // mechanism. So we do a CursorLeftIntern()-lite,
3442 // without the dreaded mechanism. (JMarc)
3443 SetCursorIntern(cursor.par, cursor.pos - 1);
3445 // some insets are undeletable here
3446 if (cursor.par->GetChar(cursor.pos) == LyXParagraph::META_INSET) {
3447 if (!cursor.par->GetInset(cursor.pos)->Deletable())
3449 // force complete redo when erasing display insets
3450 // this is a cruel method but safe..... Matthias
3451 if (cursor.par->GetInset(cursor.pos)->display()){
3452 cursor.par->Erase(cursor.pos);
3458 Row * row = cursor.row;
3459 long y = cursor.y - row->baseline;
3460 LyXParagraph::size_type z;
3461 /* remember that a space at the end of a row doesnt count
3462 * when calculating the fill */
3463 if (cursor.pos < RowLast(row) ||
3464 !cursor.par->IsLineSeparator(cursor.pos)) {
3465 row->fill += SingleWidth(cursor.par, cursor.pos);
3468 /* some special code when deleting a newline. This is similar
3469 * to the behavior when pasting paragraphs */
3470 if (cursor.pos && cursor.par->IsNewline(cursor.pos)) {
3471 cursor.par->Erase(cursor.pos);
3472 // refresh the positions
3474 while (tmprow->next && tmprow->next->par == row->par) {
3475 tmprow = tmprow->next;
3478 if (cursor.par->IsLineSeparator(cursor.pos - 1))
3481 if (cursor.pos < cursor.par->Last() && !cursor.par->IsSeparator(cursor.pos)) {
3482 cursor.par->InsertChar(cursor.pos, ' ');
3483 // refresh the positions
3485 while (tmprow->next && tmprow->next->par == row->par) {
3486 tmprow = tmprow->next;
3491 cursor.par->Erase(cursor.pos);
3493 // refresh the positions
3495 while (tmprow->next && tmprow->next->par == row->par) {
3496 tmprow = tmprow->next;
3500 // delete newlines at the beginning of paragraphs
3501 while (cursor.par->Last() &&
3502 cursor.par->IsNewline(cursor.pos) &&
3503 cursor.pos == BeginningOfMainBody(cursor.par)) {
3504 cursor.par->Erase(cursor.pos);
3505 // refresh the positions
3507 while (tmprow->next &&
3508 tmprow->next->par == row->par) {
3509 tmprow = tmprow->next;
3515 // is there a break one row above
3516 if (row->previous && row->previous->par == row->par) {
3517 z = NextBreakPoint(row->previous, paperwidth);
3518 if ( z >= row->pos) {
3521 Row * tmprow = row->previous;
3523 // maybe the current row is now empty
3524 if (row->pos >= row->par->Last()) {
3529 BreakAgainOneRow(row);
3530 if (row->next && row->next->par == row->par)
3531 need_break_row = row->next;
3536 // set the dimensions of the row above
3537 y -= tmprow->height;
3538 tmprow->fill = Fill(tmprow, paperwidth);
3539 SetHeightOfRow(tmprow);
3542 refresh_row = tmprow;
3543 status = LyXText::NEED_MORE_REFRESH;
3544 current_font = rawtmpfont;
3545 real_current_font = realtmpfont;
3546 SetCursor(cursor.par, cursor.pos, false);
3547 // check, whether the last character's font has changed.
3548 rawtmpfont = cursor.par->GetFontSettings(cursor.par->Last() - 1);
3549 if (rawparfont != rawtmpfont)
3550 RedoHeightOfParagraph(cursor);
3555 // break the cursor row again
3556 z = NextBreakPoint(row, paperwidth);
3558 if (z != RowLast(row) ||
3559 (row->next && row->next->par == row->par &&
3560 RowLast(row) == row->par->Last() - 1)){
3562 /* it can happen that a paragraph loses one row
3563 * without a real breakup. This is when a word
3564 * is to long to be broken. Well, I don t care this
3566 if (row->next && row->next->par == row->par &&
3567 RowLast(row) == row->par->Last() - 1)
3568 RemoveRow(row->next);
3572 status = LyXText::NEED_MORE_REFRESH;
3574 BreakAgainOneRow(row);
3575 current_font = rawtmpfont;
3576 real_current_font = realtmpfont;
3577 SetCursor(cursor.par, cursor.pos, false);
3578 // cursor MUST be in row now
3580 if (row->next && row->next->par == row->par)
3581 need_break_row = row->next;
3585 // set the dimensions of the row
3586 row->fill = Fill(row, paperwidth);
3587 int tmpheight = row->height;
3588 SetHeightOfRow(row);
3589 if (tmpheight == row->height)
3590 status = LyXText::NEED_VERY_LITTLE_REFRESH;
3592 status = LyXText::NEED_MORE_REFRESH;
3595 current_font = rawtmpfont;
3596 real_current_font = realtmpfont;
3597 SetCursor(cursor.par, cursor.pos, false);
3600 // restore the current font
3601 // That is what a user expects!
3602 current_font = rawtmpfont;
3603 real_current_font = realtmpfont;
3605 // check, wether the last characters font has changed.
3606 rawtmpfont = cursor.par->GetFontSettings(cursor.par->Last() - 1);
3607 if (rawparfont != rawtmpfont) {
3608 RedoHeightOfParagraph(cursor);
3610 // now the special right address boxes
3611 if (textclasslist.Style(buffer->params.textclass,
3612 cursor.par->GetLayout()).margintype == MARGIN_RIGHT_ADDRESS_BOX) {
3613 RedoDrawingOfParagraph(cursor);
3619 void LyXText::GetVisibleRow(int offset, Row * row_ptr, long y)
3621 /* returns a printed row */
3622 Painter & pain = owner_->painter();
3624 bool is_rtl = row_ptr->par->isRightToLeftPar();
3625 LyXParagraph::size_type last = RowLastPrintable(row_ptr);
3627 LyXParagraph::size_type vpos, pos;
3629 int y_top, y_bottom;
3630 float fill_separator, fill_hfill, fill_label_hfill;
3634 if (row_ptr->height <= 0) {
3635 lyxerr << "LYX_ERROR: row.height: " << row_ptr->height << endl;
3638 PrepareToPrint(row_ptr, x, fill_separator,
3639 fill_hfill, fill_label_hfill);
3641 // clear the area where we want to paint/print
3642 pain.fillRectangle(0, offset, paperwidth, row_ptr->height);
3645 /* selection code */
3646 if (bidi_same_direction) {
3647 if (sel_start_cursor.row == row_ptr &&
3648 sel_end_cursor.row == row_ptr) {
3649 if (sel_start_cursor.x < sel_end_cursor.x)
3650 pain.fillRectangle(sel_start_cursor.x, offset,
3651 sel_end_cursor.x - sel_start_cursor.x,
3655 pain.fillRectangle(sel_end_cursor.x, offset,
3656 sel_start_cursor.x - sel_end_cursor.x,
3659 } else if (sel_start_cursor.row == row_ptr) {
3661 pain.fillRectangle(0, offset,
3666 pain.fillRectangle(sel_start_cursor.x, offset,
3667 paperwidth - sel_start_cursor.x,
3670 } else if (sel_end_cursor.row == row_ptr) {
3672 pain.fillRectangle(sel_end_cursor.x, offset,
3673 paperwidth - sel_end_cursor.x,
3677 pain.fillRectangle(0, offset,
3681 } else if (y > long(sel_start_cursor.y)
3682 && y < long(sel_end_cursor.y)) {
3683 pain.fillRectangle(0, offset,
3684 paperwidth, row_ptr->height,
3687 } else if ( sel_start_cursor.row != row_ptr &&
3688 sel_end_cursor.row != row_ptr &&
3689 y > long(sel_start_cursor.y)
3690 && y < long(sel_end_cursor.y)) {
3691 pain.fillRectangle(0, offset,
3692 paperwidth, row_ptr->height,
3694 } else if (sel_start_cursor.row == row_ptr ||
3695 sel_end_cursor.row == row_ptr) {
3698 if (row_ptr->par->table) {
3699 cell = NumberOfCell(row_ptr->par, row_ptr->pos);
3700 tmpx += row_ptr->par->table->GetBeginningOfTextInCell(cell);
3702 if ( (sel_start_cursor.row != row_ptr && !is_rtl) ||
3703 (sel_end_cursor.row != row_ptr && is_rtl))
3704 pain.fillRectangle(0, offset,
3705 int(tmpx), row_ptr->height,
3707 if (row_ptr->par->table) {
3709 for (vpos = row_ptr->pos; vpos <= last; ++vpos) {
3710 pos = vis2log(vpos);
3711 float old_tmpx = tmpx;
3712 if (row_ptr->par->IsNewline(pos)) {
3713 tmpx = x_old + row_ptr->par->table->WidthOfColumn(cell);
3716 tmpx += row_ptr->par->table->GetBeginningOfTextInCell(cell);
3718 tmpx += SingleWidth(row_ptr->par, pos);
3720 if ( (sel_start_cursor.row != row_ptr ||
3721 sel_start_cursor.pos <= pos) &&
3722 (sel_end_cursor.row != row_ptr ||
3723 pos < sel_end_cursor.pos) )
3724 pain.fillRectangle(int(old_tmpx), offset,
3725 int(tmpx - old_tmpx + 1),
3730 LyXParagraph::size_type main_body =
3731 BeginningOfMainBody(row_ptr->par);
3733 for (vpos = row_ptr->pos; vpos <= last; ++vpos) {
3734 pos = vis2log(vpos);
3735 float old_tmpx = tmpx;
3736 if (main_body > 0 && pos == main_body-1) {
3737 tmpx += fill_label_hfill +
3738 lyxfont::width(textclasslist.Style(buffer->params.textclass,
3739 row_ptr->par->GetLayout()).labelsep,
3740 GetFont(row_ptr->par, -2));
3741 if (row_ptr->par->IsLineSeparator(main_body-1))
3742 tmpx -= SingleWidth(row_ptr->par, main_body-1);
3744 if (HfillExpansion(row_ptr, pos)) {
3745 tmpx += SingleWidth(row_ptr->par, pos);
3746 if (pos >= main_body)
3749 tmpx += fill_label_hfill;
3751 else if (row_ptr->par->IsSeparator(pos)) {
3752 tmpx += SingleWidth(row_ptr->par, pos);
3753 if (pos >= main_body)
3754 tmpx += fill_separator;
3756 tmpx += SingleWidth(row_ptr->par, pos);
3758 if ( (sel_start_cursor.row != row_ptr ||
3759 sel_start_cursor.pos <= pos) &&
3760 (sel_end_cursor.row != row_ptr ||
3761 pos < sel_end_cursor.pos) )
3762 pain.fillRectangle(int(old_tmpx), offset,
3763 int(tmpx - old_tmpx + 1),
3768 if ( (sel_start_cursor.row != row_ptr && is_rtl) ||
3769 (sel_end_cursor.row != row_ptr && !is_rtl) )
3770 pain.fillRectangle(int(tmpx), offset,
3771 int(paperwidth - tmpx),
3778 if (row_ptr->par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
3779 LyXFont font(LyXFont::ALL_SANE);
3780 font.setSize(LyXFont::SIZE_FOOTNOTE);
3781 font.setColor(LColor::footnote);
3783 box_x = LYX_PAPER_MARGIN + lyxfont::width(" wide-tab ", font);
3784 if (row_ptr->previous &&
3785 row_ptr->previous->par->footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
3787 switch (row_ptr->par->footnotekind) {
3788 case LyXParagraph::MARGIN:
3791 case LyXParagraph::FIG:
3794 case LyXParagraph::TAB:
3797 case LyXParagraph::WIDE_FIG:
3800 case LyXParagraph::WIDE_TAB:
3803 case LyXParagraph::ALGORITHM:
3806 case LyXParagraph::FOOTNOTE:
3811 pain.fillRectangle(LYX_PAPER_MARGIN,
3813 box_x - LYX_PAPER_MARGIN,
3814 int(lyxfont::maxAscent(font)
3815 + lyxfont::maxDescent(font)),
3816 LColor::footnotebg);
3818 pain.line(LYX_PAPER_MARGIN, offset,
3819 paperwidth - LYX_PAPER_MARGIN, offset,
3820 LColor::footnoteframe);
3822 pain.text(LYX_PAPER_MARGIN,
3823 offset + int(lyxfont::maxAscent(font)) + 1,
3826 pain.line(LYX_PAPER_MARGIN, offset,
3828 offset + int(lyxfont::maxAscent(font)
3829 + lyxfont::maxDescent(font)),
3830 LColor::footnoteframe);
3832 pain.line(LYX_PAPER_MARGIN,
3833 offset + int(lyxfont::maxAscent(font)
3834 + lyxfont::maxDescent(font)) + 1,
3836 offset + int(lyxfont::maxAscent(font)
3837 + lyxfont::maxDescent(font)) + 1,
3838 LColor::footnoteframe);
3842 /* draw the open floats in a red box */
3843 pain.line(box_x, offset,
3844 box_x, offset + row_ptr->height,
3845 LColor::footnoteframe);
3847 pain.line(paperwidth - LYX_PAPER_MARGIN,
3849 paperwidth - LYX_PAPER_MARGIN,
3850 offset + row_ptr->height,
3851 LColor::footnoteframe);
3854 // Draw appendix lines
3855 LyXParagraph * p = row_ptr->par->PreviousBeforeFootnote()->FirstPhysicalPar();
3857 pain.line(1, offset,
3858 1, offset + row_ptr->height,
3859 LColor::appendixline);
3860 pain.line(paperwidth - 2, offset,
3861 paperwidth - 2, offset + row_ptr->height,
3862 LColor::appendixline);
3865 // Draw minipage line
3866 bool minipage = p->pextra_type == LyXParagraph::PEXTRA_MINIPAGE;
3868 pain.line(LYX_PAPER_MARGIN/5, offset,
3870 offset + row_ptr->height - 1,
3871 LColor::minipageline);
3874 int depth = p->GetDepth();
3875 for (int i = 1; i <= depth; ++i) {
3876 int line_x = (LYX_PAPER_MARGIN/5)*(i+minipage);
3877 pain.line(line_x, offset, line_x,
3878 offset + row_ptr->height - 1,
3881 } else if (row_ptr->previous &&
3882 row_ptr->previous->par->footnoteflag
3883 == LyXParagraph::OPEN_FOOTNOTE) {
3884 LyXFont font(LyXFont::ALL_SANE);
3885 font.setSize(LyXFont::SIZE_FOOTNOTE);
3887 int box_x = LYX_PAPER_MARGIN;
3888 box_x += lyxfont::width(" wide-tab ", font);
3890 pain.line(box_x, offset,
3891 paperwidth - LYX_PAPER_MARGIN,
3892 offset, LColor::footnote);
3895 // Draw appendix lines
3896 LyXParagraph * firstpar = row_ptr->par->FirstPhysicalPar();
3897 if (firstpar->appendix){
3898 pain.line(1, offset,
3899 1, offset + row_ptr->height,
3900 LColor::appendixline);
3901 pain.line(paperwidth - 2, offset,
3902 paperwidth - 2, offset + row_ptr->height,
3903 LColor::appendixline);
3906 // Draw minipage line
3907 bool minipage = firstpar->pextra_type == LyXParagraph::PEXTRA_MINIPAGE;
3909 pain.line(LYX_PAPER_MARGIN/5 + box_x, offset,
3910 LYX_PAPER_MARGIN/5 + box_x,
3911 offset + row_ptr->height - 1,
3912 LColor::minipageline);
3915 int depth = firstpar->GetDepth();
3920 if (row_ptr->par->footnoteflag ==
3921 row_ptr->next->par->footnoteflag)
3922 next_depth = row_ptr->next->par->GetDepth();
3923 else if (row_ptr->par->footnoteflag != LyXParagraph::OPEN_FOOTNOTE)
3926 if (row_ptr->previous)
3927 if (row_ptr->par->footnoteflag ==
3928 row_ptr->previous->par->footnoteflag)
3929 prev_depth = row_ptr->previous->par->GetDepth();
3930 else if (row_ptr->par->footnoteflag != LyXParagraph::OPEN_FOOTNOTE)
3933 for (int i = 1; i <= depth; ++i) {
3934 int line_x = (LYX_PAPER_MARGIN/5)*(i+minipage)+box_x;
3935 pain.line(line_x, offset, line_x,
3936 offset + row_ptr->height - 1 - (i-next_depth-1)*3,
3940 pain.fillRectangle(line_x, offset, LYX_PAPER_MARGIN/5, 2,
3943 pain.fillRectangle(line_x,
3944 offset + row_ptr->height - 2 - (i-next_depth-1)*3,
3945 LYX_PAPER_MARGIN/5, 2,
3951 LyXLayout const & layout =
3952 textclasslist.Style(buffer->params.textclass,
3953 row_ptr->par->GetLayout());
3956 y_bottom = row_ptr->height;
3958 /* is it a first row? */
3959 if (row_ptr->pos == 0
3960 && row_ptr->par == firstpar) {
3962 /* start of appendix? */
3963 if (row_ptr->par->start_of_appendix){
3964 pain.line(1, offset,
3965 paperwidth - 2, offset,
3966 LColor::appendixline);
3969 /* think about the margins */
3970 if (!row_ptr->previous)
3971 y_top += LYX_PAPER_MARGIN;
3973 if (row_ptr->par->pagebreak_top){ /* draw a top pagebreak */
3975 pb_font.setColor(LColor::pagebreak).decSize();
3976 int w = 0, a = 0, d = 0;
3977 pain.line(0, offset + y_top + 2*DefaultHeight(),
3979 offset + y_top + 2*DefaultHeight(),
3981 Painter::line_onoffdash)
3984 _("Page Break (top)"),
3987 LColor::background, false, w, a, d);
3988 pain.rectText((paperwidth - w)/2,
3989 offset +y_top + 2*DefaultHeight() +d,
3990 _("Page Break (top)"),
3993 LColor::background);
3994 y_top += 3 * DefaultHeight();
3997 if (row_ptr->par->added_space_top.kind() == VSpace::VFILL) {
3998 /* draw a vfill top */
3999 pain.line(0, offset + 2 + y_top,
4000 LYX_PAPER_MARGIN, offset + 2 + y_top,
4003 pain.line(0, offset + y_top + 3 * DefaultHeight(),
4005 offset + y_top + 3 * DefaultHeight(),
4008 pain.line(LYX_PAPER_MARGIN / 2, offset + 2 + y_top,
4009 LYX_PAPER_MARGIN / 2,
4010 offset + y_top + 3 * DefaultHeight(),
4013 y_top += 3 * DefaultHeight();
4016 /* think about user added space */
4017 y_top += int(row_ptr->par->added_space_top.inPixels(owner_));
4019 /* think about the parskip */
4020 /* some parskips VERY EASY IMPLEMENTATION */
4021 if (buffer->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
4022 if (layout.latextype == LATEX_PARAGRAPH
4023 && firstpar->GetDepth() == 0
4024 && firstpar->Previous())
4025 y_top += buffer->params.getDefSkip().inPixels(owner_);
4026 else if (firstpar->Previous()
4027 && textclasslist.Style(buffer->params.textclass,
4028 firstpar->Previous()->GetLayout()).latextype == LATEX_PARAGRAPH
4029 && firstpar->Previous()->GetDepth() == 0)
4030 // is it right to use defskip here, too? (AS)
4031 y_top += buffer->params.getDefSkip().inPixels(owner_);
4034 if (row_ptr->par->line_top) { /* draw a top line */
4035 y_top += lyxfont::ascent('x', GetFont(row_ptr->par, 0));
4037 pain.line(0, offset + y_top,
4038 paperwidth, offset + y_top,
4040 Painter::line_solid,
4041 Painter::line_thick);
4043 y_top += lyxfont::ascent('x', GetFont(row_ptr->par, 0));
4046 /* should we print a label? */
4047 if (layout.labeltype >= LABEL_STATIC
4048 && (layout.labeltype != LABEL_STATIC
4049 || layout.latextype != LATEX_ENVIRONMENT
4050 || row_ptr->par->IsFirstInSequence())) {
4051 font = GetFont(row_ptr->par, -2);
4052 if (!row_ptr->par->GetLabelstring().empty()) {
4054 string tmpstring = row_ptr->par->GetLabelstring();
4056 if (layout.labeltype == LABEL_COUNTER_CHAPTER) {
4057 if (buffer->params.secnumdepth >= 0) {
4058 /* this is special code for the chapter layout. This is printed in
4059 * an extra row and has a pagebreak at the top. */
4060 float spacing_val = 1.0;
4061 if (!row_ptr->par->spacing.isDefault()) {
4062 spacing_val = row_ptr->par->spacing.getValue();
4064 spacing_val = buffer->params.spacing.getValue();
4067 maxdesc = int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val)
4068 + int(layout.parsep) * DefaultHeight();
4070 tmpx = paperwidth - LeftMargin(row_ptr) -
4071 lyxfont::width(tmpstring, font);
4072 pain.text(int(tmpx),
4073 offset + row_ptr->baseline - row_ptr->ascent_of_text - maxdesc,
4078 tmpx = paperwidth - LeftMargin(row_ptr)
4079 + lyxfont::width(layout.labelsep, font);
4080 if (row_ptr->par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
4081 LyXFont font(LyXFont::ALL_SANE);
4082 font.setSize(LyXFont::SIZE_SMALL);
4083 tmpx += lyxfont::width("Mwide-fixM", font);
4086 tmpx = x - lyxfont::width(layout.labelsep, font)
4087 - lyxfont::width(tmpstring, font);
4090 pain.text(int(tmpx),
4091 offset + row_ptr->baseline,
4095 /* the labels at the top of an environment. More or less for bibliography */
4096 } else if (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
4097 layout.labeltype == LABEL_BIBLIO ||
4098 layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
4099 if (row_ptr->par->IsFirstInSequence()) {
4100 font = GetFont(row_ptr->par, -2);
4101 if (!row_ptr->par->GetLabelstring().empty()) {
4102 string tmpstring = row_ptr->par->GetLabelstring();
4103 float spacing_val = 1.0;
4104 if (!row_ptr->par->spacing.isDefault()) {
4105 spacing_val = row_ptr->par->spacing.getValue();
4107 spacing_val = buffer->params.spacing.getValue();
4110 maxdesc = int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val
4111 + (layout.labelbottomsep * DefaultHeight()));
4114 if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT){
4115 tmpx = ( (is_rtl ? LeftMargin(row_ptr) : x)
4116 + paperwidth - RightMargin(row_ptr) ) / 2;
4117 tmpx -= lyxfont::width(tmpstring, font) / 2;
4119 tmpx = paperwidth - LeftMargin(row_ptr) -
4120 lyxfont::width(tmpstring, font);
4121 pain.text(int(tmpx),
4122 offset + row_ptr->baseline
4123 - row_ptr->ascent_of_text
4129 if (layout.labeltype == LABEL_BIBLIO && row_ptr->par->bibkey) {
4130 font = GetFont(row_ptr->par, -1);
4132 tmpx = paperwidth - LeftMargin(row_ptr)
4133 + lyxfont::width(layout.labelsep, font);
4135 tmpx = x - lyxfont::width(layout.labelsep, font)
4136 - row_ptr->par->bibkey->width(owner_->painter(), font);
4137 row_ptr->par->bibkey->draw(pain,
4139 offset + row_ptr->baseline,
4144 /* is it a last row? */
4145 LyXParagraph * par = row_ptr->par->LastPhysicalPar();
4146 if (row_ptr->par->ParFromPos(last + 1) == par
4147 && (!row_ptr->next || row_ptr->next->par != row_ptr->par)) {
4149 /* think about the margins */
4151 y_bottom -= LYX_PAPER_MARGIN;
4153 /* draw a bottom pagebreak */
4154 if (firstpar->pagebreak_bottom) {
4156 pb_font.setColor(LColor::pagebreak).decSize();
4157 int w = 0, a = 0, d = 0;
4159 offset + y_bottom - 2 * DefaultHeight(),
4161 offset + y_bottom - 2 * DefaultHeight(),
4163 Painter::line_onoffdash)
4166 _("Page Break (bottom)"),
4169 LColor::background, false, w, a, d);
4170 pain.rectText((paperwidth - w)/2,
4171 offset +y_top + 2*DefaultHeight() +d,
4172 _("Page Break (bottom)"),
4175 LColor::background);
4176 y_bottom -= 3 * DefaultHeight();
4179 if (firstpar->added_space_bottom.kind() == VSpace::VFILL) {
4180 /* draw a vfill bottom */
4181 pain.line(0, offset + y_bottom - 3 * DefaultHeight(),
4183 offset + y_bottom - 3 * DefaultHeight(),
4185 pain.line(0, offset + y_bottom - 2,
4187 offset + y_bottom - 2,
4189 pain.line(LYX_PAPER_MARGIN / 2,
4190 offset + y_bottom - 3 * DefaultHeight(),
4191 LYX_PAPER_MARGIN / 2,
4192 offset + y_bottom - 2,
4194 y_bottom -= 3* DefaultHeight();
4197 /* think about user added space */
4198 y_bottom -= int(firstpar->added_space_bottom.inPixels(owner_));
4200 if (firstpar->line_bottom) {
4201 /* draw a bottom line */
4202 y_bottom -= lyxfont::ascent('x', GetFont(par, par->Last() - 1));
4203 pain.line(0, offset + y_bottom,
4204 paperwidth, offset + y_bottom,
4205 LColor::topline, Painter::line_solid,
4206 Painter::line_thick);
4207 y_bottom -= lyxfont::ascent('x', GetFont(par, par->Last() - 1));
4211 int endlabel = row_ptr->par->GetEndLabel();
4214 case END_LABEL_FILLED_BOX:
4216 LyXFont font = GetFont(row_ptr->par, last);
4217 int size = int(0.75 * lyxfont::maxAscent(font));
4218 int y = (offset + row_ptr->baseline) - size;
4219 int x = is_rtl ? LYX_PAPER_MARGIN
4220 : paperwidth - LYX_PAPER_MARGIN - size;
4221 if (row_ptr->par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE)
4223 LyXFont font(LyXFont::ALL_SANE);
4224 font.setSize(LyXFont::SIZE_SMALL);
4225 x += lyxfont::width("Mwide-figM", font);
4227 x -= LYX_PAPER_MARGIN/2;
4228 if (row_ptr->fill <= size)
4229 x += (size - row_ptr->fill + 1) * (is_rtl ? -1 : 1);
4230 if (endlabel == END_LABEL_BOX) {
4231 pain.line(x, y, x, y + size,
4233 pain.line(x + size, y, x + size , y + size,
4235 pain.line(x, y, x + size, y,
4237 pain.line(x, y + size, x + size, y + size,
4240 pain.fillRectangle(x, y, size, size,
4244 case END_LABEL_STATIC:
4246 LyXTextClass::LayoutList::size_type layout = row_ptr->par->GetLayout();
4247 string tmpstring = textclasslist.Style(buffer->params.textclass,
4248 layout).endlabelstring();
4249 font = GetFont(row_ptr->par, -2);
4250 int tmpx = is_rtl ? int(x) - lyxfont::width(tmpstring, font)
4251 : paperwidth - RightMargin(row_ptr) - row_ptr->fill;
4252 pain.text( tmpx, offset + row_ptr->baseline, tmpstring, font);
4255 case END_LABEL_NO_LABEL:
4260 /* draw the text in the pixmap */
4262 vpos = row_ptr->pos;
4263 /* table stuff -- begin*/
4264 if (row_ptr->par->table) {
4266 int cell = NumberOfCell(row_ptr->par, row_ptr->pos);
4268 x += row_ptr->par->table->GetBeginningOfTextInCell(cell);
4270 while (vpos <= last) {
4271 pos = vis2log(vpos);
4272 if (row_ptr->par->IsNewline(pos)) {
4274 x = x_old + row_ptr->par->table->WidthOfColumn(cell);
4275 /* draw the table lines, still very simple */
4276 on_off = !row_ptr->par->table->TopLine(cell);
4278 !row_ptr->par->table->TopAlreadyDrawed(cell)) &&
4279 !row_ptr->par->table->IsContRow(cell))
4280 pain.line(int(x_old),
4281 offset + row_ptr->baseline - row_ptr->ascent_of_text,
4283 offset + row_ptr->baseline - row_ptr->ascent_of_text,
4285 on_off ? Painter::line_onoffdash : Painter::line_solid);
4287 on_off = !row_ptr->par->table->BottomLine(cell);
4288 if ((!on_off && !row_ptr->par->table->RowHasContRow(cell)) ||
4289 row_ptr->par->table->VeryLastRow(cell))
4291 pain.line(int(x_old),
4292 offset + y_bottom - 1,
4294 offset + y_bottom - 1,
4296 on_off ? Painter::line_onoffdash : Painter::line_solid);
4298 on_off = !row_ptr->par->table->LeftLine(cell);
4300 pain.line(int(x_old),
4301 offset + row_ptr->baseline - row_ptr->ascent_of_text,
4303 offset + y_bottom - 1,
4305 on_off ? Painter::line_onoffdash : Painter::line_solid);
4307 on_off = !row_ptr->par->table->RightLine(cell);
4309 pain.line(int(x) - row_ptr->par->table->AdditionalWidth(cell),
4310 offset + row_ptr->baseline - row_ptr->ascent_of_text,
4311 int(x) - row_ptr->par->table->AdditionalWidth(cell),
4312 offset + y_bottom - 1,
4314 on_off ? Painter::line_onoffdash : Painter::line_solid);
4317 /* take care about the alignment and other spaces */
4319 x += row_ptr->par->table->GetBeginningOfTextInCell(cell);
4320 if (row_ptr->par->table->IsFirstCell(cell))
4321 --cell; // little hack, sorry
4323 } else if (row_ptr->par->IsHfill(pos)) {
4327 offset + row_ptr->baseline - DefaultHeight() / 2,
4329 offset + row_ptr->baseline,
4334 } else if (row_ptr->par->IsSeparator(pos)) {
4336 x+= SingleWidth(row_ptr->par, pos);
4339 draw(row_ptr, vpos, offset, x);
4342 /* do not forget the very last cell. This has no NEWLINE so
4343 * ignored by the code above*/
4344 if (cell == row_ptr->par->table->GetNumberOfCells()-1){
4345 x = x_old + row_ptr->par->table->WidthOfColumn(cell);
4346 on_off = !row_ptr->par->table->TopLine(cell);
4348 !row_ptr->par->table->TopAlreadyDrawed(cell)) &&
4349 !row_ptr->par->table->IsContRow(cell))
4351 pain.line(int(x_old),
4352 offset + row_ptr->baseline - row_ptr->ascent_of_text,
4354 offset + row_ptr->baseline - row_ptr->ascent_of_text,
4356 on_off ? Painter::line_onoffdash : Painter::line_solid);
4357 on_off = !row_ptr->par->table->BottomLine(cell);
4358 if ((!on_off && !row_ptr->par->table->RowHasContRow(cell)) ||
4359 row_ptr->par->table->VeryLastRow(cell))
4361 pain.line(int(x_old),
4362 offset + y_bottom - 1,
4364 offset + y_bottom - 1,
4366 on_off ? Painter::line_onoffdash : Painter::line_solid);
4368 on_off = !row_ptr->par->table->LeftLine(cell);
4370 pain.line(int(x_old),
4371 offset + row_ptr->baseline - row_ptr->ascent_of_text,
4373 offset + y_bottom - 1,
4375 on_off ? Painter::line_onoffdash : Painter::line_solid);
4377 on_off = !row_ptr->par->table->RightLine(cell);
4379 pain.line(int(x) - row_ptr->par->table->AdditionalWidth(cell),
4380 offset + row_ptr->baseline - row_ptr->ascent_of_text,
4381 int(x) - row_ptr->par->table->AdditionalWidth(cell),
4382 offset + y_bottom - 1,
4384 on_off ? Painter::line_onoffdash : Painter::line_solid);
4387 /* table stuff -- end*/
4388 LyXParagraph::size_type main_body =
4389 BeginningOfMainBody(row_ptr->par);
4390 if (main_body > 0 &&
4391 (main_body-1 > last ||
4392 !row_ptr->par->IsLineSeparator(main_body-1)))
4395 while (vpos <= last) {
4396 pos = vis2log(vpos);
4397 if (main_body > 0 && pos == main_body-1) {
4398 x += fill_label_hfill
4399 + lyxfont::width(layout.labelsep, GetFont(row_ptr->par, -2))
4400 - SingleWidth(row_ptr->par, main_body-1);
4403 if (row_ptr->par->IsHfill(pos)) {
4406 offset + row_ptr->baseline - DefaultHeight() / 2,
4408 offset + row_ptr->baseline,
4411 if (HfillExpansion(row_ptr, pos)) {
4412 if (pos >= main_body) {
4414 offset + row_ptr->baseline - DefaultHeight() / 4,
4415 int(x + fill_hfill),
4416 offset + row_ptr->baseline - DefaultHeight() / 4,
4418 Painter::line_onoffdash);
4422 offset + row_ptr->baseline - DefaultHeight() / 4,
4423 int(x + fill_label_hfill),
4424 offset + row_ptr->baseline - DefaultHeight() / 4,
4426 Painter::line_onoffdash);
4428 x += fill_label_hfill;
4431 offset + row_ptr->baseline - DefaultHeight() / 2,
4433 offset + row_ptr->baseline,
4438 } else if (row_ptr->par->IsSeparator(pos)) {
4439 x += SingleWidth(row_ptr->par, pos);
4440 if (pos >= main_body)
4441 x += fill_separator;
4444 draw(row_ptr, vpos, offset, x);
4450 int LyXText::DefaultHeight() const
4452 LyXFont font(LyXFont::ALL_SANE);
4453 return int(lyxfont::maxAscent(font) + lyxfont::maxDescent(font) * 1.5);
4457 /* returns the column near the specified x-coordinate of the row
4458 * x is set to the real beginning of this column */
4459 int LyXText::GetColumnNearX(Row * row, int & x) const
4462 float fill_separator, fill_hfill, fill_label_hfill;
4464 PrepareToPrint(row, tmpx, fill_separator,
4465 fill_hfill, fill_label_hfill);
4467 LyXParagraph::size_type vc = row->pos;
4468 LyXParagraph::size_type last = RowLastPrintable(row);
4469 LyXParagraph::size_type c = 0;
4470 LyXLayout const & layout = textclasslist.Style(buffer->params.textclass,
4471 row->par->GetLayout());
4472 /* table stuff -- begin */
4473 if (row->par->table) {
4474 //the last row doesn't need a newline at the end
4475 if (row->next && row->next->par == row->par
4476 && row->par->IsNewline(last))
4478 int cell = NumberOfCell(row->par, row->pos);
4481 tmpx += row->par->table->GetBeginningOfTextInCell(cell);
4483 && (c = vis2log(vc)) >= 0
4484 && tmpx + (SingleWidth(row->par, c)/2) <= x
4486 if (row->par->IsNewline(c)) {
4487 if (x_old + row->par->table->WidthOfColumn(cell) <= x){
4488 tmpx = x_old + row->par->table->WidthOfColumn(cell);
4491 tmpx += row->par->table->GetBeginningOfTextInCell(cell);
4496 tmpx += SingleWidth(row->par, c);
4501 /* table stuff -- end*/
4502 LyXParagraph::size_type main_body = BeginningOfMainBody(row->par);
4503 float last_tmpx = tmpx;
4505 if (main_body > 0 &&
4506 (main_body-1 > last ||
4507 !row->par->IsLineSeparator(main_body-1)))
4510 while (vc <= last && tmpx <= x) {
4513 if (main_body > 0 && c == main_body-1) {
4514 tmpx += fill_label_hfill +
4515 lyxfont::width(layout.labelsep,
4516 GetFont(row->par, -2));
4517 if (row->par->IsLineSeparator(main_body-1))
4518 tmpx -= SingleWidth(row->par, main_body-1);
4521 if (HfillExpansion(row, c)) {
4522 x += SingleWidth(row->par, c);
4526 tmpx += fill_label_hfill;
4528 else if (row->par->IsSeparator(c)) {
4529 tmpx += SingleWidth(row->par, c);
4531 tmpx+= fill_separator;
4533 tmpx += SingleWidth(row->par, c);
4537 if (vc > row->pos && (tmpx+last_tmpx)/2 > x) {
4543 if (vc > last + 1) // This shouldn't happen.
4546 if (row->pos > last) // Row is empty?
4548 else if (vc > last ||
4549 (vc - 1 >= row->pos &&
4550 ( (row->par->IsSeparator(vis2log(vc)) && vis2log(vc) != last)
4551 || (row->par->table && row->par->IsNewline(vc) )
4553 c = vis2log(vc - 1);
4554 if (bidi_level(c) % 2 == 0)
4558 if (bidi_level(c) % 2 == 1)
4562 if (!row->par->table && row->pos <= last && c > last
4563 && row->par->IsNewline(last)) {
4564 if (bidi_level(last) % 2 == 0)
4565 tmpx -= SingleWidth(row->par, last);
4567 tmpx += SingleWidth(row->par, last);
4577 /* turn the selection into a new environment. If there is no selection,
4578 * create an empty environment */
4579 void LyXText::InsertFootnoteEnvironment(LyXParagraph::footnote_kind kind)
4581 /* no footnoteenvironment in a footnoteenvironment */
4582 if (cursor.par->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
4583 WriteAlert(_("Impossible operation"),
4584 _("You can't insert a float in a float!"),
4588 /* no marginpars in minipages */
4589 if (kind == LyXParagraph::MARGIN
4590 && cursor.par->pextra_type == LyXParagraph::PEXTRA_MINIPAGE) {
4591 WriteAlert(_("Impossible operation"),
4592 _("You can't insert a marginpar in a minipage!"),
4597 /* this doesnt make sense, if there is no selection */
4598 bool dummy_selection = false;
4600 sel_start_cursor = cursor; /* dummy selection */
4601 sel_end_cursor = cursor;
4602 dummy_selection = true;
4605 LyXParagraph *tmppar;
4607 if (sel_start_cursor.par->table || sel_end_cursor.par->table){
4608 WriteAlert(_("Impossible operation"), _("Cannot cut table."), _("Sorry."));
4612 /* a test to make sure there is not already a footnote
4613 * in the selection. */
4615 tmppar = sel_start_cursor.par->ParFromPos(sel_start_cursor.pos);
4617 while (tmppar != sel_end_cursor.par->ParFromPos(sel_end_cursor.pos) &&
4618 tmppar->footnoteflag == LyXParagraph::NO_FOOTNOTE)
4619 tmppar = tmppar->next;
4621 if (tmppar != sel_end_cursor.par->ParFromPos(sel_end_cursor.pos)
4622 || tmppar->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
4623 WriteAlert(_("Impossible operation"),
4624 _("Float would include float!"),
4629 /* ok we have a selection. This is always between sel_start_cursor
4630 * and sel_end cursor */
4632 SetUndo(Undo::FINISH,
4633 sel_start_cursor.par->ParFromPos(sel_start_cursor.pos)->previous,
4634 sel_end_cursor.par->ParFromPos(sel_end_cursor.pos)->next);
4636 if (sel_end_cursor.pos > 0
4637 && sel_end_cursor.par->IsLineSeparator(sel_end_cursor.pos - 1))
4638 sel_end_cursor.pos--; /* please break before a space at
4640 if (sel_start_cursor.par == sel_end_cursor.par
4641 && sel_start_cursor.pos > sel_end_cursor.pos)
4642 sel_start_cursor.pos--;
4644 sel_end_cursor.par->BreakParagraphConservative(sel_end_cursor.pos);
4646 sel_end_cursor.par = sel_end_cursor.par->Next();
4647 sel_end_cursor.pos = 0;
4649 // don't forget to insert a dummy layout paragraph if necessary
4650 if (sel_start_cursor.par->GetLayout() != sel_end_cursor.par->layout){
4651 sel_end_cursor.par->BreakParagraphConservative(0);
4652 sel_end_cursor.par->layout = LYX_DUMMY_LAYOUT;
4653 sel_end_cursor.par = sel_end_cursor.par->next;
4656 sel_end_cursor.par->layout = LYX_DUMMY_LAYOUT;
4658 cursor = sel_end_cursor;
4660 /* please break behind a space, if there is one. The space should
4662 if (sel_start_cursor.pos > 0
4663 && sel_start_cursor.par->IsLineSeparator(sel_start_cursor.pos - 1))
4664 sel_start_cursor.pos--;
4665 if (sel_start_cursor.par->IsLineSeparator(sel_start_cursor.pos)) {
4666 sel_start_cursor.par->Erase(sel_start_cursor.pos);
4669 sel_start_cursor.par->BreakParagraphConservative(sel_start_cursor.pos);
4670 tmppar = sel_start_cursor.par->Next();
4672 if (dummy_selection) {
4674 if (kind == LyXParagraph::TAB
4675 || kind == LyXParagraph::FIG
4676 || kind == LyXParagraph::WIDE_TAB
4677 || kind == LyXParagraph::WIDE_FIG
4678 || kind == LyXParagraph::ALGORITHM) {
4679 pair<bool, LyXTextClass::size_type> lres =
4680 textclasslist.NumberOfLayout(buffer->params.textclass,
4682 LyXTextClass::size_type lay;
4688 lay = 0; // use default layout "Standard" (0)
4690 tmppar->SetLayout(lay);
4694 if (sel_start_cursor.pos > 0) {
4695 /* the footnote-environment should begin with a standard layout.
4696 * Imagine you insert a footnote within an enumeration, you
4697 * certainly do not want an enumerated footnote! */
4701 /* this is a exception the user would sometimes expect, I hope */
4702 sel_start_cursor.par->Clear();
4706 while (tmppar != sel_end_cursor.par) {
4707 tmppar->footnoteflag = LyXParagraph::OPEN_FOOTNOTE;
4708 tmppar->footnotekind = kind;
4709 tmppar = tmppar->Next();
4712 RedoParagraphs(sel_start_cursor, sel_end_cursor.par->Next());
4714 SetCursor(sel_start_cursor.par->Next(), 0);
4720 // returns pointer to a specified row
4721 Row * LyXText::GetRow(LyXParagraph * par,
4722 LyXParagraph::size_type pos, long & y) const
4724 Row * tmprow = firstrow;
4727 // find the first row of the specified paragraph
4728 while (tmprow->next && tmprow->par != par) {
4729 y += tmprow->height;
4730 tmprow = tmprow->next;
4733 // now find the wanted row
4734 while (tmprow->pos < pos
4736 && tmprow->next->par == par
4737 && tmprow->next->pos <= pos) {
4738 y += tmprow->height;
4739 tmprow = tmprow->next;