]> git.lyx.org Git - lyx.git/blobdiff - src/text.C
Added html export for LinuxDoc and DocBook. LinuxDoc import now available in file...
[lyx.git] / src / text.C
index 454592e3ea565fd8e691b82899327e8427e295f5..c81fae1f9b3af9e3315795c7f73349b769992667 100644 (file)
@@ -4,7 +4,7 @@
  *           LyX, The Document Processor
  *      
  *           Copyright 1995 Matthias Ettrich
- *           Copyright 1995-1999 The LyX Team.
+ *           Copyright 1995-2000 The LyX Team.
  *
  * ====================================================== */
 
 #include "LyXView.h"
 #include "lyxrow.h"
 #include "Painter.h"
+#include "tracer.h"
 
 using std::max;
 using std::min;
 
 static const int LYX_PAPER_MARGIN = 20;
 
-extern LyXRC * lyxrc;
 
 // ale070405
 extern int bibitemMaxWidth(Painter &, LyXFont const &);
@@ -172,7 +172,7 @@ int LyXText::SingleWidth(LyXParagraph * par,
 
        // The most common case is handled first (Asger)
        if (IsPrintable(c)) {
-               if (lyxrc->rtl_support && lyxrc->font_norm == "iso8859-6.8x")
+               if (lyxrc.rtl_support && lyxrc.font_norm == "iso8859-6.8x")
                        c = TransformChar(c, par, pos);
                return font.width(c);
 
@@ -247,7 +247,7 @@ LyXParagraph::size_type LyXText::RowLast(Row const * row) const
 void LyXText::ComputeBidiTables(Row * row) const
 {
 
-       if (!lyxrc->rtl_support) {
+       if (!lyxrc.rtl_support) {
                bidi_start = -1;
                return;
        }
@@ -321,10 +321,9 @@ void LyXText::ComputeBidiTablesFromTo(Row * row,
                } else {
                        if (level == 0 ||
                            (level == 1 && direction == LYX_DIR_RIGHT_TO_LEFT
-                            && row->par->getFont(lpos).direction() ==
-                            LyXFont::RTL_DIR
-                            && row->par->getFont(lpos).latex() ==
-                            LyXFont::ON ) ) {
+                            && row->par->getFont(lpos).isRightToLeft()
+                            && row->par->getFont(lpos).latex() == LyXFont::ON
+                            ) ) {
                                // The last check is needed when the
                                // char is a space
                                stack[level++] = lpos;
@@ -506,7 +505,7 @@ void LyXText::draw(Row const * row,
        // So IMHO we should go with the easier and clearer implementation.
        // And even if 1024 is a large number here it might overflow, string
        // will only overflow if the machine is out of memory...
-       bool do_transform = (lyxrc->rtl_support && lyxrc->font_norm == "iso8859-6.8x");
+       bool do_transform = (lyxrc.rtl_support && lyxrc.font_norm == "iso8859-6.8x");
        if (do_transform)
                c = TransformChar(c, row->par, pos);
        static string textstring;
@@ -1648,7 +1647,7 @@ void LyXText::BreakParagraph(char keep_layout)
    if (cursor.par->table) {
        int cell = NumberOfCell(cursor.par, cursor.pos);
        if (cursor.par->table->ShouldBeVeryLastCell(cell))
-           SetCursor(cursor.par, cursor.par->text.size());
+           SetCursor(cursor.par, cursor.par->size());
    }
    /* table stuff -- end */
    
@@ -1751,7 +1750,7 @@ void LyXText::OpenFootnote()
        cursor.par->ParFromPos(cursor.pos) != par; cursor.pos++);
    /* now the cursor is at the beginning of the physical par */
    SetCursor(cursor.par,
-            cursor.pos + cursor.par->ParFromPos(cursor.pos)->text.size());
+            cursor.pos + cursor.par->ParFromPos(cursor.pos)->size());
    
    /* the cursor must be exactly before the footnote */ 
    par = cursor.par->ParFromPos(cursor.pos);
@@ -1885,8 +1884,12 @@ void LyXText::TableFeatures(int feature) const
                
           /* insert the new cells */ 
           int number = cursor.par->table->NumberOfCellsInRow(cell_org);
-          for (int i = 0; i < number; ++i)
+         Language const * lang = cursor.par->getParLanguage();
+         LyXFont font(LyXFont::ALL_INHERIT,lang);
+          for (int i = 0; i < number; ++i) {
               cursor.par->InsertChar(pos, LyXParagraph::META_NEWLINE);
+             cursor.par->SetFont(pos, font);
+         }
                
           /* append the row into the table */
           cursor.par->table->AppendRow(cell_org);
@@ -1920,9 +1923,13 @@ void LyXText::TableFeatures(int feature) const
                
           /* insert the new cells */ 
           int number = cursor.par->table->NumberOfCellsInRow(cell_org);
-          for (int i = 0; i < number; ++i)
+         Language const * lang = cursor.par->getParLanguage();
+         LyXFont font(LyXFont::ALL_INHERIT,lang);
+          for (int i = 0; i < number; ++i) {
               cursor.par->InsertChar(pos, LyXParagraph::META_NEWLINE);
-               
+             cursor.par->SetFont(pos, font);
+         }
+
           /* append the row into the table */
           cursor.par->table->AppendContRow(cell_org);
           RedoParagraph();
@@ -1932,10 +1939,13 @@ void LyXText::TableFeatures(int feature) const
              LyXParagraph::size_type pos = 0;
           int cell_org = actCell;
           int cell = 0;
+         Language const * lang = cursor.par->getParLanguage();
+         LyXFont font(LyXFont::ALL_INHERIT,lang);
           do{
               if (pos && (cursor.par->IsNewline(pos-1))){
                   if (cursor.par->table->AppendCellAfterCell(cell_org, cell)) {
                       cursor.par->InsertChar(pos, LyXParagraph::META_NEWLINE);
+                     cursor.par->SetFont(pos, font);
                       if (pos <= cursor.pos)
                           cursor.pos++;
                       ++pos;
@@ -1946,8 +1956,11 @@ void LyXText::TableFeatures(int feature) const
           } while (pos <= cursor.par->Last());
           /* remember that the very last cell doesn't end with a newline.
              This saves one byte memory per table ;-) */
-          if (cursor.par->table->AppendCellAfterCell(cell_org, cell))
-              cursor.par->InsertChar(cursor.par->Last(), LyXParagraph::META_NEWLINE);
+          if (cursor.par->table->AppendCellAfterCell(cell_org, cell)) {
+                 LyXParagraph::size_type last = cursor.par->Last();
+                 cursor.par->InsertChar(last, LyXParagraph::META_NEWLINE);
+                 cursor.par->SetFont(last, font);
+         }
                
           /* append the column into the table */ 
           cursor.par->table->AppendColumn(cell_org);
@@ -2087,7 +2100,7 @@ void LyXText::TableFeatures(int feature) const
           // dummy-paragraph !! 
           // not necessar anymore with UNDO :)
           for (LyXParagraph::size_type i = 
-                      cursor.par->text.size() - 1; i >= 0; --i)
+                      cursor.par->size() - 1; i >= 0; --i)
              cursor.par->Erase(i);
           RedoParagraph();
           return;
@@ -3226,7 +3239,8 @@ void LyXText::ChangeWordCase(LyXText::TextCase action)
        LyXParagraph::size_type tmppos = 
                cursor.par->PositionInParFromPos(cursor.pos);
        while (tmppos < tmppar->size()) {
-               unsigned char c = tmppar->text[tmppos];
+               //unsigned char c = tmppar->text[tmppos];
+               unsigned char c = tmppar->GetChar(tmppos);
                if (IsKommaChar(c) || IsLineSeparatorChar(c))
                        break;
                if (c != LyXParagraph::META_INSET) {
@@ -3244,7 +3258,8 @@ void LyXText::ChangeWordCase(LyXText::TextCase action)
                        }
                }
                
-               tmppar->text[tmppos] = c;
+               //tmppar->text[tmppos] = c;
+               tmppar->SetChar(tmppos, c);
                ++tmppos;
        }
        CheckParagraph(tmppar, tmppos);
@@ -3293,13 +3308,10 @@ void LyXText::Delete()
 }
 
 
-void  LyXText::Backspace()
+void LyXText::Backspace()
 {
-       LyXParagraph * tmppar;
-       Row * tmprow, * row;
-       long y;
-       int tmpheight;
-
+       DebugTracer trc1("LyXText::Backspace");
+       
        /* table stuff -- begin */
        if (cursor.par->table) {
                BackspaceInTable();
@@ -3315,6 +3327,11 @@ void  LyXText::Backspace()
        LyXFont rawparfont = cursor.par->GetFontSettings(lastpos - 1);
 
        if (cursor.pos == 0) {
+               DebugTracer trc("LyXText::Backspace cursor.pos == 0");
+               
+               // The cursor is at the beginning of a paragraph, so the the backspace
+               // will collapse two paragraphs into one.
+               
                // we may paste some paragraphs
       
                // is it an empty paragraph?
@@ -3322,17 +3339,19 @@ void  LyXText::Backspace()
                if ((lastpos == 0
                     || (lastpos == 1 && cursor.par->IsSeparator(0)))
                    && !(cursor.par->Next() 
-                        && cursor.par->footnoteflag == 
-                        LyXParagraph::NO_FOOTNOTE
-                        && cursor.par->Next()->footnoteflag == 
-                        LyXParagraph::OPEN_FOOTNOTE)) {
+                        && cursor.par->footnoteflag == LyXParagraph::NO_FOOTNOTE
+                        && cursor.par->Next()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE)) {
+                       // This is an empty paragraph and we delete it just by moving the curosr one step
+                       // left and let the DeleteEmptyParagraphMechanism handle the actual deleteion
+                       // of the paragraph.
                        
                        if (cursor.par->previous) {
-                               tmppar = cursor.par->previous->FirstPhysicalPar();
+                               LyXParagraph * tmppar = cursor.par->previous->FirstPhysicalPar();
                                if (cursor.par->GetLayout() == tmppar->GetLayout()
                                    && cursor.par->footnoteflag == tmppar->footnoteflag
                                    && cursor.par->GetAlign() == tmppar->GetAlign()) {
-                                       
+                                       // Inherit botom DTD from the paragraph below.
+                                       // (the one we are deleting)
                                        tmppar->line_bottom = cursor.par->line_bottom;
                                        tmppar->added_space_bottom = cursor.par->added_space_bottom;
                                        tmppar->pagebreak_bottom = cursor.par->pagebreak_bottom;
@@ -3341,7 +3360,7 @@ void  LyXText::Backspace()
                                CursorLeftIntern();
                     
                                // the layout things can change the height of a row !
-                               tmpheight = cursor.row->height;
+                               int tmpheight = cursor.row->height;
                                SetHeightOfRow(cursor.row);
                                if (cursor.row->height != tmpheight) {
                                        refresh_y = cursor.y - cursor.row->baseline;
@@ -3351,13 +3370,15 @@ void  LyXText::Backspace()
                                return;
                        }
                }
+               
                if (cursor.par->ParFromPos(cursor.pos)->previous){
                        SetUndo(Undo::DELETE,
                                cursor.par->ParFromPos(cursor.pos)->previous->previous,
                                cursor.par->ParFromPos(cursor.pos)->next);
                }
-               tmppar = cursor.par;
-               tmprow = cursor.row;
+               
+               LyXParagraph * tmppar = cursor.par;
+               Row * tmprow = cursor.row;
                CursorLeftIntern();
 #warning See comment on top of text.C
                /* Pasting is not allowed, if the paragraphs have different
@@ -3372,18 +3393,22 @@ void  LyXText::Backspace()
                */
                if (cursor.par != tmppar
                    && (cursor.par->GetLayout() == tmppar->GetLayout()
-                       || !tmppar->GetLayout())
+                       || tmppar->GetLayout() == 0 /*standard*/)
                    && cursor.par->footnoteflag == tmppar->footnoteflag
                    /* table stuff -- begin*/
                    && !cursor.par->table /* no pasting of tables */ 
                    /* table stuff -- end*/
                    && cursor.par->GetAlign() == tmppar->GetAlign()) {
-                       
+
+                       RemoveParagraph(tmprow);
+                       RemoveRow(tmprow);
                        cursor.par->PasteParagraph();
                        
-                       if (!(cursor.pos &&
-                             cursor.par->IsSeparator(cursor.pos - 1)))
-                               cursor.par->InsertChar(cursor.pos, ' ');
+                       if (!cursor.pos || !cursor.par->IsSeparator(cursor.pos - 1))
+                               ; //cursor.par->InsertChar(cursor.pos, ' ');
+                       // strangely enough it seems that commenting out the line above removes
+                       // most or all of the segfaults. I will however also try to move the
+                       // two Remove... lines in front of the PasteParagraph too.
                        else
                                if (cursor.pos)
                                        cursor.pos--;
@@ -3393,16 +3418,22 @@ void  LyXText::Backspace()
                        refresh_y = cursor.y - cursor.row->baseline;
                        
                        // remove the lost paragraph
-                       RemoveParagraph(tmprow);
-                       RemoveRow(tmprow);  
+                       // This one is not safe, since the paragraph that the tmprow and the
+                       // following rows belong to has been deleted by the PasteParagraph
+                       // above. The question is... could this be moved in front of the
+                       // PasteParagraph?
+                       //RemoveParagraph(tmprow);
+                       //RemoveRow(tmprow);  
                        
-                       AppendParagraph(cursor.row);
+                       AppendParagraph(cursor.row); // This rebuilds the rows.
                        UpdateCounters(cursor.row);
                        
                        // the row may have changed, block, hfills etc.
                        SetCursor(cursor.par, cursor.pos);
                }
        } else {
+               DebugTracer trc("LyXText::Backspace normal backspace");
+               
                /* this is the code for a normal backspace, not pasting
                 * any paragraphs */ 
                SetUndo(Undo::DELETE, 
@@ -3423,8 +3454,8 @@ void  LyXText::Backspace()
                        }
                }
                
-               row = cursor.row;
-               y = cursor.y - row->baseline;
+               Row * row = cursor.row;
+               long y = cursor.y - row->baseline;
                LyXParagraph::size_type z;
                /* remember that a space at the end of a row doesnt count
                 * when calculating the fill */ 
@@ -3438,7 +3469,7 @@ void  LyXText::Backspace()
                if (cursor.pos && cursor.par->IsNewline(cursor.pos)) {
                        cursor.par->Erase(cursor.pos);
                        // refresh the positions
-                       tmprow = row;
+                       Row * tmprow = row;
                        while (tmprow->next && tmprow->next->par == row->par) {
                                tmprow = tmprow->next;
                                tmprow->pos--;
@@ -3459,7 +3490,7 @@ void  LyXText::Backspace()
                        cursor.par->Erase(cursor.pos);
                        
                        // refresh the positions
-                       tmprow = row;
+                       Row * tmprow = row;
                        while (tmprow->next && tmprow->next->par == row->par) {
                                tmprow = tmprow->next;
                                tmprow->pos--;
@@ -3508,7 +3539,7 @@ void  LyXText::Backspace()
                        if ( z >= row->pos) {
                                row->pos = z + 1;
                                
-                               tmprow = row->previous;
+                               Row * tmprow = row->previous;
                                
                                // maybe the current row is now empty
                                if (row->pos >= row->par->Last()) {
@@ -3587,7 +3618,8 @@ void  LyXText::Backspace()
                        SetCursor(cursor.par, cursor.pos, false);
                }
        }
-   
+       DebugTracer trc2("LyXText::Backspace wrap up");
+       
        // restore the current font
        // That is what a user expects!
        current_font = rawtmpfont; 
@@ -4444,10 +4476,15 @@ int LyXText::GetColumnNearX(Row * row, int & x) const
                        vc = row->pos+1;
                        tmpx += fill_separator+SingleWidth(row->par, vis2log(row->pos));
                }
-
        if (row->pos > last)  // Row is empty?
                c = row->pos;
-       else if (vc <= last) {
+       else if (vc > last ||
+                (row->par->table && vc > row->pos && row->par->IsNewline(vc)) ){
+               int pos = (vc > last+1) ? last : vc - 1; 
+               c = vis2log(pos);
+               if (row->par->getLetterDirection(c) == LYX_DIR_LEFT_TO_RIGHT)
+                       ++c;
+       } else {
                c = vis2log(vc);
                LyXDirection direction = row->par->getLetterDirection(c);
                if (vc > row->pos && row->par->IsLineSeparator(c)
@@ -4455,10 +4492,6 @@ int LyXText::GetColumnNearX(Row * row, int & x) const
                        c = vis2log(vc-1);
                if (direction == LYX_DIR_RIGHT_TO_LEFT)
                        ++c;
-       } else {
-               c = vis2log(last)+1;
-               if (row->par->getLetterDirection(c - 1) == LYX_DIR_RIGHT_TO_LEFT)
-                       --c;            
        }
 
        if (!row->par->table && row->pos <= last && c > last