]> git.lyx.org Git - lyx.git/blobdiff - src/text.C
next step...
[lyx.git] / src / text.C
index f087cb38160b07c7224e5b4e881c713c37abee8f..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();
@@ -138,7 +189,7 @@ int LyXText::singleWidth(BufferView * bview, Paragraph * par,
 
 
 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
-                        Paragraph::size_type pos, char c) const
+                         Paragraph::size_type pos, char c) const
 {
        LyXFont const font = getFont(bview->buffer(), par, pos);
 
@@ -253,7 +304,7 @@ void LyXText::computeBidiTables(Buffer const * buf, Row * row) const
                LyXFont font = row->par()->getFontSettings(buf->params, pos);
                if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
                    font.number() == LyXFont::ON &&
-                   row->par()->getFontSettings(buf->params, lpos-1).number()
+                   row->par()->getFontSettings(buf->params, lpos - 1).number()
                    == LyXFont::ON) {
                        font = row->par()->getFontSettings(buf->params, lpos);
                        is_space = false;
@@ -440,17 +491,19 @@ void LyXText::draw(BufferView * bview, Row const * row,
                        tmpinset->update(bview, font, false);
                        tmpinset->draw(bview, font, offset+row->baseline(), x,
                                       cleared);
-#ifdef SEEMS_TO_BE_NOT_NEEDED
-                       if (status_ == CHANGED_IN_DRAW) {
-                               UpdateInset(bview, tmpinset);
-                               status(bview, CHANGED_IN_DRAW);
+                       if (!need_break_row && !inset_owner &&
+                           bview->text->status() == CHANGED_IN_DRAW)
+                       {
+                               if (row->previous() && row->previous()->par() == row->par())
+                                       breakAgainOneRow(bview, row->previous());
+                               setCursor(bview, cursor.par(), cursor.pos());
+                               need_break_row = const_cast<Row *>(row);
                        }
-#endif
                }
                ++vpos;
 
                if (lyxrc.mark_foreign_language &&
-                       font.language() != ignore_language &&
+                       font.language() != latex_language &&
                    font.language() != bview->buffer()->params.language) {
                        int const y = offset + row->height() - 1;
                        pain.line(int(tmpx), y, int(x), y, LColor::language);
@@ -566,12 +619,15 @@ void LyXText::draw(BufferView * bview, Row const * row,
                x += lyxfont::width(textstring, font);
        }
 
+#ifdef INHERIT_LANGUAGE
 #ifdef WITH_WARNINGS
        if ((font.language() == inherit_language) ||
                (font.language() == ignore_language))
                lyxerr << "No this shouldn't happen!\n";
+#endif
 #endif
        if (lyxrc.mark_foreign_language &&
+           font.language() != latex_language &&
            font.language() != bview->buffer()->params.language) {
                int const y = offset + row->height() - 1;
                pain.line(int(tmpx), y, int(x), y,
@@ -590,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
@@ -615,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();
                        }
                }
@@ -630,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());
@@ -650,22 +697,17 @@ 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;
                }
                
        }
        
-       LyXFont const labelfont = getFont(bview->buffer(), row->par(), -2);
+       LyXFont const labelfont = getLabelFont(bview->buffer(), row->par());
        switch (layout.margintype) {
        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,
@@ -686,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:
@@ -738,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;
@@ -767,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;
@@ -785,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
@@ -814,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);
@@ -831,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;
 }
 
@@ -876,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) {
@@ -942,7 +969,7 @@ LyXText::nextBreakPoint(BufferView * bview, Row const * row, int width) const
                        ++i;
                        if (i == main_body) {
                                x += lyxfont::width(layout.labelsep,
-                                                   getFont(bview->buffer(), par, -2));
+                                                   getLabelFont(bview->buffer(), par));
                                if (par->isLineSeparator(i - 1))
                                        x-= singleWidth(bview, par, i - 1);
                                int left_margin = labelEnd(bview, row);
@@ -973,7 +1000,7 @@ LyXText::nextBreakPoint(BufferView * bview, Row const * row, int width) const
 int LyXText::fill(BufferView * bview, Row * row, int paper_width) const
 {
        if (paper_width < 0)
-               return 20;
+               return 0;
 
        int w;
        // get the pure distance
@@ -982,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);
@@ -998,7 +1026,7 @@ int LyXText::fill(BufferView * bview, Row * row, int paper_width) const
 
        while (i <= last) {
                if (main_body > 0 && i == main_body) {
-                       w += lyxfont::width(layout.labelsep, getFont(bview->buffer(), row->par(), -2));
+                       w += lyxfont::width(layout.labelsep, getLabelFont(bview->buffer(), row->par()));
                        if (row->par()->isLineSeparator(i - 1))
                                w -= singleWidth(bview, row->par(), i - 1);
                        int left_margin = labelEnd(bview, row);
@@ -1009,7 +1037,7 @@ int LyXText::fill(BufferView * bview, Row * row, int paper_width) const
                ++i;
        }
        if (main_body > 0 && main_body > last) {
-               w += lyxfont::width(layout.labelsep, getFont(bview->buffer(), row->par(), -2));
+               w += lyxfont::width(layout.labelsep, getLabelFont(bview->buffer(), row->par()));
                if (last >= 0 && row->par()->isLineSeparator(last))
                        w -= singleWidth(bview, row->par(), last);
                int const left_margin = labelEnd(bview, row);
@@ -1048,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;
@@ -1057,7 +1085,7 @@ int LyXText::labelFill(BufferView * bview, Row const * row) const
        int fill = 0;
        if (!row->par()->params().labelWidthString().empty()) {
                fill = max(lyxfont::width(row->par()->params().labelWidthString(),
-                                         getFont(bview->buffer(), row->par(), -2)) - w,
+                                         getLabelFont(bview->buffer(), row->par())) - w,
                           0);
        }
        
@@ -1097,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;
                }
@@ -1132,7 +1161,7 @@ int LyXText::numberOfLabelHfills(Buffer const * buf, Row const * row) const
 // returns true, if a expansion is needed.
 // Rules are given by LaTeX
 bool LyXText::hfillExpansion(Buffer const * buf, Row const * row_ptr,
-                            Paragraph::size_type pos) const
+                             Paragraph::size_type pos) const
 {
        // by the way, is it a hfill?
        if (!row_ptr->par()->isHfill(pos))
@@ -1208,11 +1237,11 @@ void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
        // do the assignment below too often.
        LyXFont font = getFont(bview->buffer(), par, row_ptr->pos());
        LyXFont::FONT_SIZE const tmpsize = font.size();
-       font = getFont(bview->buffer(), par, -1);
+       font = getLayoutFont(bview->buffer(), par);
        LyXFont::FONT_SIZE const size = font.size();
        font.setSize(tmpsize);
 
-       LyXFont labelfont = getFont(bview->buffer(), par, -2);
+       LyXFont labelfont = getLabelFont(bview->buffer(), par);
 
        float spacing_val = 1.0;
        if (!row_ptr->par()->params().spacing().isDefault()) {
@@ -1228,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);
@@ -1424,7 +1453,8 @@ void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
                if (firstpar->params().lineBottom())
                        maxdesc += 2 * lyxfont::ascent('x',
                                                       getFont(bview->buffer(),
-                                                      par, par->size() - 1));
+                                                              par,
+                                                              max(Paragraph::size_type(0), par->size() - 1)));
          
                // and now the pagebreaks
                if (firstpar->params().pagebreakBottom())
@@ -1476,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;
@@ -1500,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;
 
@@ -1743,12 +1775,12 @@ void LyXText::insertChar(BufferView * bview, char c)
                                      cursor.pos()).number() == LyXFont::ON &&
                              getFont(bview->buffer(),
                                      cursor.par(),
-                                     cursor.pos()-1).number() == LyXFont::ON)
+                                     cursor.pos() - 1).number() == LyXFont::ON)
                            )
-                               Number(bview); // Set current_font.number to OFF
+                               number(bview); // Set current_font.number to OFF
                } else if (isdigit(c) &&
                           real_current_font.isVisibleRightToLeft()) {
-                       Number(bview); // Set current_font.number to ON
+                       number(bview); // Set current_font.number to ON
 
                        if (cursor.pos() > 0) {
                                char const c = cursor.par()->getChar(cursor.pos() - 1);
@@ -1765,7 +1797,7 @@ void LyXText::insertChar(BufferView * bview, char c)
                                           cursor.pos() >= 2 &&
                                           getFont(bview->buffer(),
                                                   cursor.par(),
-                                                  cursor.pos()-2).number() == LyXFont::ON) {
+                                                  cursor.pos() - 2).number() == LyXFont::ON) {
                                        setCharFont(bview->buffer(),
                                                    cursor.par(),
                                                    cursor.pos() - 1,
@@ -1989,11 +2021,11 @@ void LyXText::charInserted()
 
 
 void LyXText::prepareToPrint(BufferView * bview,
-                            Row * row, float & x,
-                            float & fill_separator, 
-                            float & fill_hfill,
-                            float & fill_label_hfill,
-                            bool bidi) const
+                             Row * row, float & x,
+                             float & fill_separator, 
+                             float & fill_hfill,
+                             float & fill_label_hfill,
+                             bool bidi) const
 {
        float nlh;
        float ns;
@@ -2028,45 +2060,49 @@ void LyXText::prepareToPrint(BufferView * bview,
        // are there any hfills in the row?
        float const nh = numberOfHfills(bview->buffer(), row);
 
-       if (nh)
-         fill_hfill = w / nh;
-       else  {
+       if (nh) {
+               if (w > 0)
+                       fill_hfill = w / nh;
+       } else  {
                // is it block, flushleft or flushright? 
                // set x how you need it
-       int align;
-       if (row->par()->params().align() == LYX_ALIGN_LAYOUT)
-         align = textclasslist.Style(bview->buffer()->params.textclass, row->par()->getLayout()).align;
-       else
-         align = row->par()->params().align();
-          
-       // center displayed insets 
-       Inset * inset;
-          if (row->par()->getChar(row->pos()) == Paragraph::META_INSET
-              && (inset=row->par()->getInset(row->pos()))
-              && (inset->display())) // || (inset->scroll() < 0)))
-            align = (inset->lyxCode() == Inset::MATHMACRO_CODE)
-                    ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
-
-          switch (align) {
+               int align;
+               if (row->par()->params().align() == LYX_ALIGN_LAYOUT) {
+                       align = textclasslist.Style(bview->buffer()->params.textclass, row->par()->getLayout()).align;
+               } else {
+                       align = row->par()->params().align();
+               }
+               
+               // center displayed insets 
+               Inset * inset;
+               if (row->par()->getChar(row->pos()) == Paragraph::META_INSET
+                   && (inset=row->par()->getInset(row->pos()))
+                   && (inset->display())) // || (inset->scroll() < 0)))
+                   align = (inset->lyxCode() == Inset::MATHMACRO_CODE)
+                       ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
+               
+               switch (align) {
            case LYX_ALIGN_BLOCK:
-             ns = numberOfSeparators(bview->buffer(), row);
-             if (ns && row->next() && row->next()->par() == row->par() &&
-                 !(row->next()->par()->isNewline(row->next()->pos() - 1))
-                 && !(row->next()->par()->getChar(row->next()->pos()) == Paragraph::META_INSET
-                      && row->next()->par()->getInset(row->next()->pos())
-                      && row->next()->par()->getInset(row->next()->pos())->display())
-                 )
-               fill_separator = w / ns;
-             else if (is_rtl)
-               x += w;
-             break;
+                       ns = numberOfSeparators(bview->buffer(), row);
+                       if (ns && row->next() && row->next()->par() == row->par() &&
+                           !(row->next()->par()->isNewline(row->next()->pos() - 1))
+                           && !(row->next()->par()->getChar(row->next()->pos()) == Paragraph::META_INSET
+                                && row->next()->par()->getInset(row->next()->pos())
+                                && row->next()->par()->getInset(row->next()->pos())->display())
+                               )
+                       {
+                               fill_separator = w / ns;
+                       } else if (is_rtl) {
+                               x += w;
+                       }
+                       break;
            case LYX_ALIGN_RIGHT:
-             x += w;
-             break;
+                       x += w;
+                       break;
            case LYX_ALIGN_CENTER:
-             x += w / 2;
-             break;
-          }
+                       x += w / 2;
+                       break;
+               }
        }
        if (!bidi)
                return;
@@ -2084,7 +2120,7 @@ void LyXText::prepareToPrint(BufferView * bview,
                                textclasslist.Style(bview->buffer()->params.textclass,
                                                    row->par()->getLayout());
                        x += lyxfont::width(layout.labelsep,
-                                           getFont(bview->buffer(), row->par(), -2));
+                                           getLabelFont(bview->buffer(), row->par()));
                        if (main_body-1 <= last)
                                x += fill_label_hfill;
                }
@@ -2261,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())
@@ -2293,17 +2330,12 @@ string const LyXText::selectNextWord(BufferView * bview,
        
        // Now, skip until we have real text (will jump paragraphs)
        while ((cursor.par()->size() > cursor.pos()
-              && (!cursor.par()->isLetter(cursor.pos())
-#ifndef NO_LATEX
-                   || cursor.par()->getFont(bview->buffer()->params, cursor.pos())
-                   .latex() == LyXFont::ON
-#endif
-                       )
+              && (!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);
@@ -2313,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
@@ -2403,10 +2435,10 @@ void LyXText::deleteWordForward(BufferView * bview)
                setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
                selection.cursor = cursor;
                cursor = tmpcursor;
-               setSelection(bview); 
+               setSelection(bview);
                
                /* -----> Great, CutSelection() gets rid of multiple spaces. */
-               cutSelection(bview);
+               cutSelection(bview, true, false);
        }
 }
 
@@ -2425,7 +2457,7 @@ void LyXText::deleteWordBackward(BufferView * bview)
               selection.cursor = cursor;
               cursor = tmpcursor;
               setSelection(bview);
-              cutSelection(bview);
+              cutSelection(bview, true, false);
        }
 }
 
@@ -2451,7 +2483,7 @@ void LyXText::deleteLineForward(BufferView * bview)
                if (!selection.set()) {
                        deleteWordForward(bview);
                } else {
-                       cutSelection(bview);
+                       cutSelection(bview, true, false);
                }
        }
 }
@@ -2499,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;
                        }
                }
@@ -2579,7 +2611,9 @@ void LyXText::Delete(BufferView * bview)
        if ((cursor.par()->previous() ? cursor.par()->previous()->id() : 0)
            == old_cur_par_prev_id
            && cursor.par()->id() != old_cur_par_id)
+       {
                return; // delete-empty-paragraph-mechanism has done it
+       }
 
        // if you had success make a backspace
        if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
@@ -2642,8 +2676,7 @@ void LyXText::backspace(BufferView * bview)
 
                if (cursor.par()->previous()) {
                        setUndo(bview, Undo::DELETE,
-                               cursor.par()->previous(),
-                               cursor.par()->next());
+                               cursor.par()->previous(), cursor.par()->next());
                }
                
                Paragraph * tmppar = cursor.par();
@@ -2656,8 +2689,8 @@ void LyXText::backspace(BufferView * bview)
                if (cursor.par()->previous()) { 
                        // steps into the above paragraph.
                        setCursorIntern(bview, cursor.par()->previous(),
-                                       cursor.par()->previous()->size(),
-                                       false);
+                                       cursor.par()->previous()->size(),
+                                       false);
                }
 
                /* Pasting is not allowed, if the paragraphs have different
@@ -2673,8 +2706,8 @@ void LyXText::backspace(BufferView * bview)
                if (cursor.par() != tmppar
                    && (cursor.par()->getLayout() == tmppar->getLayout()
                        || tmppar->getLayout() == 0 /*standard*/)
-                   && cursor.par()->getAlign() == tmppar->getAlign()) {
-
+                   && cursor.par()->getAlign() == tmppar->getAlign())
+               {
                        removeParagraph(tmprow);
                        removeRow(tmprow);
                        cursor.par()->pasteParagraph(bview->buffer()->params);
@@ -2711,13 +2744,13 @@ void LyXText::backspace(BufferView * bview)
                /* this is the code for a normal backspace, not pasting
                 * any paragraphs */ 
                setUndo(bview, Undo::DELETE,
-                       cursor.par(), cursor.par()->next()); 
+                       cursor.par(), cursor.par()->next()); 
                // We used to do cursorLeftIntern() here, but it is
                // not a good idea since it triggers the auto-delete
                // mechanism. So we do a cursorLeftIntern()-lite,
                // without the dreaded mechanism. (JMarc)
                setCursorIntern(bview, cursor.par(), cursor.pos()- 1,
-                               false, cursor.boundary());
+                               false, cursor.boundary());
                
                // some insets are undeletable here
                if (cursor.par()->getChar(cursor.pos()) == Paragraph::META_INSET) {
@@ -2741,8 +2774,8 @@ void LyXText::backspace(BufferView * bview)
                if (cursor.pos() < rowLast(row) ||
                    !cursor.par()->isLineSeparator(cursor.pos())) {
                        row->fill(row->fill() + singleWidth(bview,
-                                                           cursor.par(),
-                                                           cursor.pos()));
+                                                           cursor.par(),
+                                                           cursor.pos()));
                }
                
                /* some special code when deleting a newline. This is similar
@@ -2962,52 +2995,47 @@ void LyXText::getVisibleRow(BufferView * bview, int y_offset, int x_offset,
        if (cleared) {
                clear_area = true;
        } else if (clear_area) {
-#ifdef WITH_WARNINGS
-#warning Should be fixed with a lyxinset::clear_width(bv, font) function! (Jug)
-#warning Should we not fix this in the Painter, please have a look Lars! (Jug)
-#endif
                int const y = y_offset < 0 ? 0 : y_offset;
                int const h = y_offset < 0 ?
                        row_ptr->height() + y_offset : row_ptr->height();
                int const w = inset_owner ?
-                       inset_owner->width(bview, font) - 2 : ww;
+                       inset_owner->textWidth(bview, true) : ww;
                int const x = x_offset;
                pain.fillRectangle(x, y, w, h, backgroundColor());
        } else if (inset != 0) {
                int h = row_ptr->baseline() - inset->ascent(bview, font);
                // first clear the whole row above the inset!
                if (h > 0) {
-#if 0
                        int const w = (inset_owner ?
-                                inset_owner->width(bview, font) : ww);
-#endif
-                       pain.fillRectangle(x_offset, y_offset, ww, h,
+                                inset_owner->textWidth(bview, true) : ww);
+                       pain.fillRectangle(x_offset, y_offset, w, h,
                                           backgroundColor());
                }
                h += inset->ascent(bview, font) + inset->descent(bview, font);
                // clear the space below the inset!
                if ((row_ptr->height() - h) > 0) {
-#if 0
                        int const w = (inset_owner ?
-                                inset_owner->width(bview, font) : ww);
-#endif
+                                      inset_owner->textWidth(bview, true) : ww);
                        pain.fillRectangle(x_offset, y_offset + h,
-                                          ww, row_ptr->height() - h,
+                                          w, row_ptr->height() - h,
                                           backgroundColor());
                }
                // clear the space behind the inset, if needed
-               if (!inset_owner && !inset->display() && !inset->needFullRow())
-               {
-                       int const w = inset->width(bview, font) + int(x);
-                       pain.fillRectangle(w, y_offset, 
-                                          ww - w, row_ptr->height(),
-                                          backgroundColor());
+               if (!inset->display() && !inset->needFullRow()) {
+                       int const w = (inset_owner ?
+                                      inset_owner->textWidth(bview, true) : ww);
+                       int const xp = int(x) + inset->width(bview, font);
+                       if (w-xp > 0) {
+                               pain.fillRectangle(xp, y_offset,
+                                                  w-xp, row_ptr->height(),
+                                                  backgroundColor());
+                       }
                }
        }
 
        if (selection.set()) {
                int const w = (inset_owner ?
-                              inset_owner->width(bview, font) : ww);
+                              inset_owner->textWidth(bview, true) : ww);
                // selection code
                if (bidi_same_direction) {
                        if (selection.start.row() == row_ptr &&
@@ -3081,7 +3109,7 @@ void LyXText::getVisibleRow(BufferView * bview, int y_offset, int x_offset,
                                        tmpx += fill_label_hfill +
                                                lyxfont::width(textclasslist.Style(bview->buffer()->params.textclass,
                                                                                   row_ptr->par()->getLayout()).labelsep,
-                                                              getFont(bview->buffer(),row_ptr->par(), -2));
+                                                              getLabelFont(bview->buffer(),row_ptr->par()));
                                        if (row_ptr->par()->isLineSeparator(main_body-1))
                                                tmpx -= singleWidth(bview, row_ptr->par(), main_body-1);
                                }
@@ -3269,7 +3297,7 @@ void LyXText::getVisibleRow(BufferView * bview, int y_offset, int x_offset,
                    && (layout.labeltype != LABEL_STATIC
                        || layout.latextype != LATEX_ENVIRONMENT
                        || row_ptr->par()->isFirstInSequence())) {
-                       font = getFont(bview->buffer(), row_ptr->par(), -2);
+                       font = getLabelFont(bview->buffer(), row_ptr->par());
                        if (!row_ptr->par()->getLabelstring().empty()) {
                                tmpx = x;
                                string const tmpstring =
@@ -3318,8 +3346,8 @@ void LyXText::getVisibleRow(BufferView * bview, int y_offset, int x_offset,
                           layout.labeltype == LABEL_BIBLIO ||
                           layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
                        if (row_ptr->par()->isFirstInSequence()) {
-                               font = getFont(bview->buffer(),
-                                              row_ptr->par(), -2);
+                               font = getLabelFont(bview->buffer(),
+                                                   row_ptr->par());
                                if (!row_ptr->par()->getLabelstring().empty()) {
                                        string const tmpstring =
                                                row_ptr->par()->getLabelstring();
@@ -3350,7 +3378,7 @@ void LyXText::getVisibleRow(BufferView * bview, int y_offset, int x_offset,
                        }
                }
                if (layout.labeltype == LABEL_BIBLIO && row_ptr->par()->bibkey) {
-                       font = getFont(bview->buffer(), row_ptr->par(), -1);
+                       font = getLayoutFont(bview->buffer(), row_ptr->par());
                        if (is_rtl)
                                tmpx = ww - leftMargin(bview, row_ptr)
                                        + lyxfont::width(layout.labelsep, font);
@@ -3423,7 +3451,7 @@ void LyXText::getVisibleRow(BufferView * bview, int y_offset, int x_offset,
                        y_bottom -= lyxfont::ascent('x',
                                                    getFont(bview->buffer(),
                                                            par,
-                                                           par->size() - 1));
+                                                           max(Paragraph::size_type(0), par->size() - 1)));
                        int const w = (inset_owner ?
                                       inset_owner->width(bview, font) : ww);
                        int const xp = static_cast<int>(inset_owner ? x : 0);
@@ -3434,7 +3462,7 @@ void LyXText::getVisibleRow(BufferView * bview, int y_offset, int x_offset,
                        y_bottom -= lyxfont::ascent('x',
                                                    getFont(bview->buffer(),
                                                            par,
-                                                           par->size() - 1));
+                                                           max(Paragraph::size_type(0), par->size() - 1)));
                }
 
                // draw an endlabel
@@ -3473,7 +3501,7 @@ void LyXText::getVisibleRow(BufferView * bview, int y_offset, int x_offset,
                        string const tmpstring = textclasslist.
                                Style(bview->buffer()->params.textclass,
                                      layout).endlabelstring();
-                       font = getFont(bview->buffer(), row_ptr->par(), -2);
+                       font = getLabelFont(bview->buffer(), row_ptr->par());
                        int const tmpx = is_rtl ?
                                int(x) - lyxfont::width(tmpstring, font)
                                : ww - rightMargin(bview->buffer(), row_ptr) - row_ptr->fill();
@@ -3501,8 +3529,8 @@ void LyXText::getVisibleRow(BufferView * bview, int y_offset, int x_offset,
                if (main_body > 0 && pos == main_body - 1) {
                        x += fill_label_hfill
                                + lyxfont::width(layout.labelsep,
-                                                getFont(bview->buffer(),
-                                                        row_ptr->par(), -2))
+                                                getLabelFont(bview->buffer(),
+                                                             row_ptr->par()))
                                - singleWidth(bview,
                                              row_ptr->par(),
                                              main_body - 1);
@@ -3565,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;
@@ -3599,7 +3628,7 @@ int LyXText::getColumnNearX(BufferView * bview, Row * row, int & x,
                if (main_body > 0 && c == main_body-1) {
                        tmpx += fill_label_hfill +
                                lyxfont::width(layout.labelsep,
-                                              getFont(bview->buffer(), row->par(), -2));
+                                              getLabelFont(bview->buffer(), row->par()));
                        if (row->par()->isLineSeparator(main_body - 1))
                                tmpx -= singleWidth(bview, row->par(), main_body-1);
                }