]> git.lyx.org Git - lyx.git/blobdiff - src/text.C
add missing writeNormal() methods to some insets
[lyx.git] / src / text.C
index 3e8400867af817e380c358e1d875aa2d05b4a971..b859f46b550f56fc373f49ef2bdf854c6e4b4515 100644 (file)
@@ -9,8 +9,8 @@
  * ====================================================== */
 
 #include <config.h>
-#include <cstdlib>
-#include <cctype>
+//#include <cstdlib> //these two do not seem useful anymore (JMarc)
+//#include <cctype>
 #include <algorithm>
 
 #include "lyxtext.h"
@@ -64,6 +64,57 @@ int LyXText::workWidth(BufferView * bview) const
 }
 
 
+int LyXText::workWidth(BufferView * bview, Inset * inset) const
+{
+       Buffer::inset_iterator it;
+       Paragraph * par = 0;
+       Paragraph::size_type pos = 0;
+
+       for(it=bview->buffer()->inset_iterator_begin();
+           it != bview->buffer()->inset_iterator_end();
+           ++it)
+       {
+               if (*it == inset) {
+                       par = it.getPar();
+                       pos = it.getPos();
+                       break;
+               }
+       }
+       if (!par) {
+               return workWidth(bview);
+       }
+       
+       LyXLayout const & layout =
+               textclasslist.Style(bview->buffer()->params.textclass,
+                                   par->getLayout());
+
+       if (layout.margintype != MARGIN_RIGHT_ADDRESS_BOX) {
+               // Optimization here: in most cases, the real row is
+               // not needed, but only the par/pos values. So we just
+               // construct a dummy row for leftMargin. (JMarc)
+               Row dummyrow;
+               dummyrow.par(par);
+               dummyrow.pos(pos);
+               return workWidth(bview) - leftMargin(bview, &dummyrow);
+       } else {
+               int dummy_y;
+               Row * row = getRow(par, pos, dummy_y);
+               Row * frow = row;
+               while(frow->previous() && frow->par() == frow->previous()->par())
+                       frow = frow->previous();
+               int maxw = 0;
+               while(frow->next() && frow->par() == frow->next()->par()) {
+                       if ((frow != row) && (maxw < frow->width()))
+                               maxw = frow->width();
+                       frow = frow->next();
+               }
+               if (maxw)
+                       return maxw;
+       }
+       return workWidth(bview);
+}
+
+
 int LyXText::getRealCursorX(BufferView * bview) const
 {
        int x = cursor.x();
@@ -595,20 +646,15 @@ void LyXText::draw(BufferView * bview, Row const * row,
 // exactly the label-width.
 int LyXText::leftMargin(BufferView * bview, Row const * row) const
 {
-       LyXLayout const & layout =
-               textclasslist.Style(bview->buffer()->params.textclass,
-                                   row->par()->getLayout());
+       LyXTextClass const & tclass =
+               textclasslist.TextClass(bview->buffer()->params.textclass);
+       LyXLayout const & layout = tclass[row->par()->getLayout()];
        
        string parindent = layout.parindent; 
 
        int x = LYX_PAPER_MARGIN;
        
-       x += lyxfont::signedWidth(textclasslist
-                                 .TextClass(bview->buffer()->params.textclass)
-                                 .leftmargin(),
-                                 textclasslist
-                                 .TextClass(bview->buffer()->params.textclass)
-                                 .defaultfont());
+       x += lyxfont::signedWidth(tclass.leftmargin(), tclass.defaultfont());
 
        // this is the way, LyX handles the LaTeX-Environments.
        // I have had this idea very late, so it seems to be a
@@ -620,9 +666,7 @@ int LyXText::leftMargin(BufferView * bview, Row const * row) const
                                Paragraph * newpar = row->par()
                                        ->depthHook(row->par()->getDepth());
                                if (newpar &&
-                                   textclasslist.Style(bview->buffer()->params.textclass,
-                                                       newpar->getLayout())
-                                   .nextnoindent)
+                                   tclass[newpar->getLayout()].nextnoindent)
                                        parindent.erase();
                        }
                }
@@ -635,10 +679,8 @@ int LyXText::leftMargin(BufferView * bview, Row const * row) const
                // make a corresponding row. Needed to call LeftMargin()
                
                // check wether it is a sufficent paragraph 
-               if (newpar
-                   && textclasslist
-                       .Style(bview->buffer()->params.textclass, 
-                              newpar->getLayout()).isEnvironment()) {
+               if (newpar && tclass[newpar->getLayout()].isEnvironment())
+               {
                        Row dummyrow;
                        dummyrow.par(newpar);
                        dummyrow.pos(newpar->size());
@@ -655,9 +697,7 @@ int LyXText::leftMargin(BufferView * bview, Row const * row) const
                        if (newpar->params().noindent())
                                parindent.erase();
                        else
-                               parindent = textclasslist
-                                       .Style(bview->buffer()->params.textclass, 
-                                              newpar->getLayout()).parindent;
+                               parindent = tclass[newpar->getLayout()].parindent;
                }
                
        }
@@ -667,10 +707,7 @@ int LyXText::leftMargin(BufferView * bview, Row const * row) const
        case MARGIN_DYNAMIC:
                if (!layout.leftmargin.empty()) {
                        x += lyxfont::signedWidth(layout.leftmargin,
-                                                 textclasslist
-                                                 .TextClass(bview->buffer()->params.
-                                                            textclass)
-                                                 .defaultfont());
+                                                 tclass.defaultfont());
                }
                if (!row->par()->getLabelstring().empty()) {
                        x += lyxfont::signedWidth(layout.labelindent,
@@ -691,7 +728,7 @@ int LyXText::leftMargin(BufferView * bview, Row const * row) const
                }
                break;
        case MARGIN_STATIC:
-               x += lyxfont::signedWidth(layout.leftmargin, textclasslist.TextClass(bview->buffer()->params.textclass).defaultfont()) * 4
+               x += lyxfont::signedWidth(layout.leftmargin, tclass.defaultfont()) * 4
                        / (row->par()->getDepth() + 4);
                break;
        case MARGIN_FIRST_DYNAMIC:
@@ -743,15 +780,13 @@ int LyXText::leftMargin(BufferView * bview, Row const * row) const
                }
                
                x += lyxfont::signedWidth(layout.leftmargin,
-                                         textclasslist
-                                         .TextClass(bview->buffer()->params.textclass)
-                                         .defaultfont());
+                                         tclass.defaultfont());
                x += minfill;
        }
        break;
        }
        
-       int align; // wrong type
+       LyXAlignment align; // wrong type
 
        if (row->par()->params().align() == LYX_ALIGN_LAYOUT)
                align = layout.align;
@@ -772,16 +807,10 @@ int LyXText::leftMargin(BufferView * bview, Row const * row) const
                        bview->buffer()->params.paragraph_separation ==
                        BufferParams::PARSEP_INDENT))
                        x += lyxfont::signedWidth(parindent,
-                                                 textclasslist
-                                                 .TextClass(bview->buffer()->params
-                                                            .textclass)
-                                                 .defaultfont());
+                                                 tclass.defaultfont());
                else if (layout.labeltype == LABEL_BIBLIO) {
                        // ale970405 Right width for bibitems
-                       x += bibitemMaxWidth(bview,textclasslist
-                                            .TextClass(bview->buffer()->params
-                                                       .textclass)
-                                            .defaultfont());
+                       x += bibitemMaxWidth(bview, tclass.defaultfont());
                }
        }
        return x;
@@ -790,17 +819,13 @@ int LyXText::leftMargin(BufferView * bview, Row const * row) const
 
 int LyXText::rightMargin(Buffer const * buf, Row const * row) const
 {
-       LyXLayout const & layout =
-               textclasslist.Style(buf->params.textclass,
-                                   row->par()->getLayout());
-       
+       LyXTextClass const & tclass =
+               textclasslist.TextClass(buf->params.textclass);
+       LyXLayout const & layout = tclass[row->par()->getLayout()];
+               
        int x = LYX_PAPER_MARGIN
-               + lyxfont::signedWidth(textclasslist
-                                      .TextClass(buf->params.textclass)
-                                      .rightmargin(),
-                                      textclasslist
-                                      .TextClass(buf->params.textclass)
-                                      .defaultfont());
+               + lyxfont::signedWidth(tclass.rightmargin(),
+                                      tclass.defaultfont());
 
        // this is the way, LyX handles the LaTeX-Environments.
        // I have had this idea very late, so it seems to be a
@@ -819,9 +844,7 @@ int LyXText::rightMargin(Buffer const * buf, Row const * row) const
                
                // check wether it is a sufficent paragraph
                if (newpar
-                   && textclasslist.Style(buf->params.textclass,
-                                          newpar->getLayout())
-                      .isEnvironment()) {
+                   && tclass[newpar->getLayout()].isEnvironment()) {
                        Row dummyrow;
                        dummyrow.par(newpar);
                        dummyrow.pos(0);
@@ -836,10 +859,8 @@ int LyXText::rightMargin(Buffer const * buf, Row const * row) const
        }
        
        //lyxerr << "rightmargin: " << layout->rightmargin << endl;
-       x += lyxfont::signedWidth(layout.rightmargin, textclasslist
-                                 .TextClass(buf->params.textclass)
-                                 .defaultfont()) * 4 / (row->par()->getDepth()
-                                                        + 4);
+       x += lyxfont::signedWidth(layout.rightmargin, tclass.defaultfont())
+               * 4 / (row->par()->getDepth() + 4);
        return x;
 }
 
@@ -881,7 +902,8 @@ LyXText::nextBreakPoint(BufferView * bview, Row const * row, int width) const
        Paragraph::size_type const main_body =
                beginningOfMainBody(bview->buffer(), par);
        LyXLayout const & layout =
-               textclasslist.Style(bview->buffer()->params.textclass, par->getLayout());
+               textclasslist.Style(bview->buffer()->params.textclass,
+                                   par->getLayout());
        Paragraph::size_type i = pos;
 
        if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
@@ -987,7 +1009,8 @@ int LyXText::fill(BufferView * bview, Row * row, int paper_width) const
        // special handling of the right address boxes
        if (textclasslist.Style(bview->buffer()->params.textclass,
                                row->par()->getLayout()).margintype
-           == MARGIN_RIGHT_ADDRESS_BOX) {
+           == MARGIN_RIGHT_ADDRESS_BOX)
+       {
                int const tmpfill = row->fill();
                row->fill(0); // the minfill in MarginLeft()
                w = leftMargin(bview, row);
@@ -1053,7 +1076,7 @@ int LyXText::labelFill(BufferView * bview, Row const * row) const
                --last;
        
        int w = 0;
-       int i = row->pos();
+       Paragraph::size_type i = row->pos();
        while (i <= last) {
                w += singleWidth(bview, row->par(), i);
                ++i;
@@ -1102,7 +1125,8 @@ int LyXText::numberOfHfills(Buffer const * buf, Row const * row) const
 
        first = max(first, beginningOfMainBody(buf, row->par()));
        int n = 0;
-       for (int p = first; p <= last; ++p) { // last, because the end is ignored!
+       for (Paragraph::size_type p = first; p <= last; ++p) {
+       // last, because the end is ignored!
                if (row->par()->isHfill(p)) {
                        ++n;
                }
@@ -1233,12 +1257,12 @@ void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
        int maxdesc = int(lyxfont::maxDescent(font) *
                          layout.spacing.getValue() *
                          spacing_val);
-       int const pos_end = rowLast(row_ptr);
+       Paragraph::size_type const pos_end = rowLast(row_ptr);
        int labeladdon = 0;
        int maxwidth = 0;
 
        // Check if any insets are larger
-       for (int pos = row_ptr->pos(); pos <= pos_end; ++pos) {
+       for (Paragraph::size_type pos = row_ptr->pos(); pos <= pos_end; ++pos) {
                if (row_ptr->par()->getChar(pos) == Paragraph::META_INSET) {
                        tmpfont = getFont(bview->buffer(), row_ptr->par(), pos);
                        tmpinset = row_ptr->par()->getInset(pos);
@@ -1482,9 +1506,11 @@ void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
        row_ptr->baseline(maxasc + labeladdon);
        
        height += row_ptr->height();
-       float x;
-       float dummy;
-       prepareToPrint(bview, row_ptr, x, dummy, dummy, dummy, false);
+       float x = 0;
+       if (layout.margintype != MARGIN_RIGHT_ADDRESS_BOX) {
+               float dummy;
+               prepareToPrint(bview, row_ptr, x, dummy, dummy, dummy, false);
+       }
        row_ptr->width(int(maxwidth + x));
        if (inset_owner) {
                Row * r = firstrow;
@@ -1506,10 +1532,10 @@ void LyXText::appendParagraph(BufferView * bview, Row * row) const
    
    // The last character position of a paragraph is an invariant so we can 
    // safely get it here. (Asger)
-   int const lastposition = row->par()->size();
+   Paragraph::size_type const lastposition = row->par()->size();
    do {
       // Get the next breakpoint
-      int z = nextBreakPoint(bview, row, workWidth(bview));
+      Paragraph::size_type z = nextBreakPoint(bview, row, workWidth(bview));
       
       Row * tmprow = row;
 
@@ -2271,16 +2297,17 @@ bool LyXText::selectWordWhenUnderCursor(BufferView * bview,
 
 // This function is only used by the spellchecker for NextWord().
 // It doesn't handle LYX_ACCENTs and probably never will.
-string const LyXText::selectNextWord(BufferView * bview,
-                                     float & value) const
+string const LyXText::selectNextWordToSpellcheck(BufferView * bview,
+                                                 float & value) const
 {
        if (the_locking_inset) {
-               string str = the_locking_inset->selectNextWord(bview, value);
+               string str = the_locking_inset->selectNextWordToSpellcheck(bview, value);
                if (!str.empty()) {
                        value += float(cursor.y())/float(height);
                        return str;
                }
 #warning Dekel please have a look on this one RTL? (Jug)
+#warning DEKEL!
                // we have to go on checking so move cusor to the right
                if (cursor.pos() == cursor.par()->size()) {
                        if (!cursor.par()->next())
@@ -2305,10 +2332,10 @@ string const LyXText::selectNextWord(BufferView * bview,
        while ((cursor.par()->size() > cursor.pos()
               && (!cursor.par()->isLetter(cursor.pos()))
               && (!cursor.par()->isInset(cursor.pos()) ||
-                  !cursor.par()->getInset(cursor.pos())->isTextInset()))
+                          !cursor.par()->getInset(cursor.pos())->allowSpellcheck()))
               || (cursor.par()->size() == cursor.pos()
                   && cursor.par()->next()))
-       {
+       {      
                if (cursor.pos() == cursor.par()->size()) {
                        cursor.par(cursor.par()->next());
                        cursor.pos(0);
@@ -2318,13 +2345,13 @@ string const LyXText::selectNextWord(BufferView * bview,
 
        // now check if we hit an inset so it has to be a inset containing text!
        if (cursor.pos() < cursor.par()->size() &&
-               cursor.par()->isInset(cursor.pos()))
+           cursor.par()->isInset(cursor.pos()))
        {
                // lock the inset!
                cursor.par()->getInset(cursor.pos())->edit(bview);
                // now call us again to do the above trick
                // but obviously we have to start from down below ;)
-               return bview->text->selectNextWord(bview, value);
+               return bview->text->selectNextWordToSpellcheck(bview, value);
        }               
   
        // Update the value if we changed paragraphs
@@ -2504,14 +2531,14 @@ void LyXText::changeRegionCase(BufferView * bview,
                if (!IsInsetChar(c) && !IsHfillChar(c)) {
                        switch (action) {
                        case text_lowercase:
-                               c = tolower(c);
+                               c = lowercase(c);
                                break;
                        case text_capitalization:
-                               c = toupper(c);
+                               c = uppercase(c);
                                action = text_lowercase;
                                break;
                        case text_uppercase:
-                               c = toupper(c);
+                               c = uppercase(c);
                                break;
                        }
                }
@@ -3566,8 +3593,9 @@ int LyXText::defaultHeight() const
    
 /* returns the column near the specified x-coordinate of the row 
 * x is set to the real beginning of this column  */ 
-int LyXText::getColumnNearX(BufferView * bview, Row * row, int & x,
-                           bool & boundary) const
+Paragraph::size_type
+LyXText::getColumnNearX(BufferView * bview, Row * row, int & x,
+                       bool & boundary) const
 {
        float tmpx = 0.0;
        float fill_separator;