]> git.saurik.com Git - wxWidgets.git/blobdiff - src/html/htmlcell.cpp
set string length to 0 in AllocBeforeWrite() just to be tidy (replaces patch 1123226)
[wxWidgets.git] / src / html / htmlcell.cpp
index 9e8f795042ac11c7973c4a00890d2c7bc108906a..c8be065e51c8161a28c463f70e2f99115748a274 100644 (file)
@@ -4,7 +4,7 @@
 // Author:      Vaclav Slavik
 // RCS-ID:      $Id$
 // Copyright:   (c) 1999 Vaclav Slavik
 // Author:      Vaclav Slavik
 // RCS-ID:      $Id$
 // Copyright:   (c) 1999 Vaclav Slavik
-// Licence:     wxWindows Licence
+// Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
 /////////////////////////////////////////////////////////////////////////////
 
 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
@@ -283,7 +283,7 @@ void wxHtmlWordCell::Split(wxDC& dc,
     wxPoint pt1 = (selFrom == wxDefaultPosition) ?
                    wxDefaultPosition : selFrom - GetAbsPos();
     wxPoint pt2 = (selTo == wxDefaultPosition) ?
     wxPoint pt1 = (selFrom == wxDefaultPosition) ?
                    wxDefaultPosition : selFrom - GetAbsPos();
     wxPoint pt2 = (selTo == wxDefaultPosition) ?
-                   wxPoint(m_Width, wxDefaultPosition.y) : selTo - GetAbsPos();
+                   wxPoint(m_Width, wxDefaultCoord) : selTo - GetAbsPos();
 
     wxCoord charW, charH;
     unsigned len = m_Word.length();
 
     wxCoord charW, charH;
     unsigned len = m_Word.length();
@@ -385,7 +385,7 @@ void wxHtmlWordCell::Draw(wxDC& dc, int x, int y,
 #endif
 
     bool drawSelectionAfterCell = false;
 #endif
 
     bool drawSelectionAfterCell = false;
-    
+
     if ( info.GetState().GetSelectionState() == wxHTML_SEL_CHANGING )
     {
         // Selection changing, we must draw the word piecewise:
     if ( info.GetState().GetSelectionState() == wxHTML_SEL_CHANGING )
     {
         // Selection changing, we must draw the word piecewise:
@@ -716,7 +716,7 @@ void wxHtmlContainerCell::Layout(int w)
             curLineWidth += cell->GetMaxTotalWidth();
 
         cell = cell->GetNext();
             curLineWidth += cell->GetMaxTotalWidth();
 
         cell = cell->GetNext();
-            
+
         // compute length of the next word that would be added:
         nextWordWidth = 0;
         if (cell)
         // compute length of the next word that would be added:
         nextWordWidth = 0;
         if (cell)
@@ -728,9 +728,9 @@ void wxHtmlContainerCell::Layout(int w)
                 nextCell = nextCell->GetNext();
             } while (nextCell && !nextCell->IsLinebreakAllowed());
         }
                 nextCell = nextCell->GetNext();
             } while (nextCell && !nextCell->IsLinebreakAllowed());
         }
-        
+
         // force new line if occured:
         // force new line if occured:
-        if ((cell == NULL) || 
+        if ((cell == NULL) ||
             (xpos + nextWordWidth > s_width && cell->IsLinebreakAllowed()))
         {
             if (xpos > MaxLineWidth) MaxLineWidth = xpos;
             (xpos + nextWordWidth > s_width && cell->IsLinebreakAllowed()))
         {
             if (xpos > MaxLineWidth) MaxLineWidth = xpos;
@@ -775,51 +775,54 @@ void wxHtmlContainerCell::Layout(int w)
                 // between two cells separated by, e.g. font change, cell which
                 // is wrong
 
                 // between two cells separated by, e.g. font change, cell which
                 // is wrong
 
-                const int step = s_width - xpos;
+                int step = s_width - xpos;
                 if ( step > 0 )
                 {
                     // first count the cells which will get extra space
                     int total = 0;
 
                 if ( step > 0 )
                 {
                     // first count the cells which will get extra space
                     int total = 0;
 
-                    const wxHtmlCell *c,
-                                     *prev = NULL,
-                                     *next = NULL;
-                    for ( c = line; c != cell; prev = c, c = next )
+                    const wxHtmlCell *c;
+                    if ( line != cell )
                     {
                     {
-                        next = c->GetNext();
-                        if ( c->IsLinebreakAllowed() &&
-                                (next == cell || next->IsLinebreakAllowed()) &&
-                                    (!prev || prev->IsLinebreakAllowed()) )
+                        for ( c = line->GetNext(); c != cell; c = c->GetNext() )
                         {
                         {
-                            total++;
+                            if ( c->IsLinebreakAllowed() )
+                            {
+                                total++;
+                            }
                         }
                     }
 
                     // and now extra space to those cells which merit it
                     if ( total )
                     {
                         }
                     }
 
                     // and now extra space to those cells which merit it
                     if ( total )
                     {
-                        prev =
-                        next = NULL;
+                        // first cell on line is not moved:
+                        line->SetPos(line->GetPosX() + s_indent,
+                                     line->GetPosY() + ypos);
+
+                        line = line->GetNext();
                         for ( int n = 0; line != cell; line = line->GetNext() )
                         {
                         for ( int n = 0; line != cell; line = line->GetNext() )
                         {
-                            line->SetPos(line->GetPosX() + s_indent +
-                                           ((n * step) / total),
-                                           line->GetPosY() + ypos);
-
-                            next = line->GetNext();
-                            if ( line->IsLinebreakAllowed() &&
-                                    (next == cell ||
-                                        next->IsLinebreakAllowed()) &&
-                                        (!prev || prev->IsLinebreakAllowed()) )
+                            if ( line->IsLinebreakAllowed() )
                             {
                                 // offset the next cell relative to this one
                                 // thus increasing our size
                                 n++;
                             }
                             {
                                 // offset the next cell relative to this one
                                 // thus increasing our size
                                 n++;
                             }
+
+                            line->SetPos(line->GetPosX() + s_indent +
+                                           ((n * step) / total),
+                                           line->GetPosY() + ypos);
                         }
                     }
                         }
                     }
+                    else
+                    {
+                        // this will cause the code to enter "else branch" below:
+                        step = 0;
+                    }
                 }
                 }
-                else // no extra space to distribute
+                // else branch:
+                if ( step <= 0 ) // no extra space to distribute
                 {
                     // just set the indent properly
                     while (line != cell)
                 {
                     // just set the indent properly
                     while (line != cell)
@@ -859,7 +862,7 @@ void wxHtmlContainerCell::Layout(int w)
 
     if (curLineWidth > m_MaxTotalWidth)
         m_MaxTotalWidth = curLineWidth;
 
     if (curLineWidth > m_MaxTotalWidth)
         m_MaxTotalWidth = curLineWidth;
-            
+
     m_MaxTotalWidth += s_indent + ((m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight);
     MaxLineWidth += s_indent + ((m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight);
     if (m_Width < MaxLineWidth) m_Width = MaxLineWidth;
     m_MaxTotalWidth += s_indent + ((m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight);
     MaxLineWidth += s_indent + ((m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight);
     if (m_Width < MaxLineWidth) m_Width = MaxLineWidth;
@@ -899,52 +902,60 @@ void wxHtmlContainerCell::Draw(wxDC& dc, int x, int y, int view_y1, int view_y2,
     dc.SetPen(*wxRED_PEN);
     dc.DrawRectangle(x+m_PosX,y+m_PosY,m_Width,m_Height);
 #endif
     dc.SetPen(*wxRED_PEN);
     dc.DrawRectangle(x+m_PosX,y+m_PosY,m_Width,m_Height);
 #endif
-    // container visible, draw it:
-    if ((y + m_PosY <= view_y2) && (y + m_PosY + m_Height > view_y1))
+            
+    int xlocal = x + m_PosX;
+    int ylocal = y + m_PosY;
+
+    if (m_UseBkColour)
     {
     {
-        if (m_UseBkColour)
-        {
-            wxBrush myb = wxBrush(m_BkColour, wxSOLID);
+        wxBrush myb = wxBrush(m_BkColour, wxSOLID);
 
 
-            int real_y1 = mMax(y + m_PosY, view_y1);
-            int real_y2 = mMin(y + m_PosY + m_Height - 1, view_y2);
+        int real_y1 = mMax(ylocal, view_y1);
+        int real_y2 = mMin(ylocal + m_Height - 1, view_y2);
 
 
-            dc.SetBrush(myb);
-            dc.SetPen(*wxTRANSPARENT_PEN);
-            dc.DrawRectangle(x + m_PosX, real_y1, m_Width, real_y2 - real_y1 + 1);
-        }
+        dc.SetBrush(myb);
+        dc.SetPen(*wxTRANSPARENT_PEN);
+        dc.DrawRectangle(xlocal, real_y1, m_Width, real_y2 - real_y1 + 1);
+    }
 
 
-        if (m_UseBorder)
-        {
-            wxPen mypen1(m_BorderColour1, 1, wxSOLID);
-            wxPen mypen2(m_BorderColour2, 1, wxSOLID);
-
-            dc.SetPen(mypen1);
-            dc.DrawLine(x + m_PosX, y + m_PosY, x + m_PosX, y + m_PosY + m_Height - 1);
-            dc.DrawLine(x + m_PosX, y + m_PosY, x + m_PosX + m_Width, y + m_PosY);
-            dc.SetPen(mypen2);
-            dc.DrawLine(x + m_PosX + m_Width - 1, y + m_PosY, x + m_PosX +  m_Width - 1, y + m_PosY + m_Height - 1);
-            dc.DrawLine(x + m_PosX, y + m_PosY + m_Height - 1, x + m_PosX + m_Width, y + m_PosY + m_Height - 1);
-        }
+    if (m_UseBorder)
+    {
+        wxPen mypen1(m_BorderColour1, 1, wxSOLID);
+        wxPen mypen2(m_BorderColour2, 1, wxSOLID);
+
+        dc.SetPen(mypen1);
+        dc.DrawLine(xlocal, ylocal, xlocal, ylocal + m_Height - 1);
+        dc.DrawLine(xlocal, ylocal, xlocal + m_Width, ylocal);
+        dc.SetPen(mypen2);
+        dc.DrawLine(xlocal + m_Width - 1, ylocal, xlocal +  m_Width - 1, ylocal + m_Height - 1);
+        dc.DrawLine(xlocal, ylocal + m_Height - 1, xlocal + m_Width, ylocal + m_Height - 1);
+    }
 
 
-        if (m_Cells)
+    if (m_Cells)
+    {
+        // draw container's contents:
+        for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext())
         {
         {
-            // draw container's contents:
-            for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext())
+            
+            // optimize drawing: don't render off-screen content:
+            if ((ylocal + cell->GetPosY() <= view_y2) &&
+                (ylocal + cell->GetPosY() + cell->GetHeight() > view_y1))
             {
             {
+                // the cell is visible, draw it:
                 UpdateRenderingStatePre(info, cell);
                 cell->Draw(dc,
                 UpdateRenderingStatePre(info, cell);
                 cell->Draw(dc,
-                           x + m_PosX, y + m_PosY, view_y1, view_y2,
+                           xlocal, ylocal, view_y1, view_y2,
                            info);
                 UpdateRenderingStatePost(info, cell);
             }
                            info);
                 UpdateRenderingStatePost(info, cell);
             }
+            else
+            {
+                // the cell is off-screen, proceed with font+color+etc.
+                // changes only:
+                cell->DrawInvisible(dc, xlocal, ylocal, info);
+            }
         }
     }
         }
     }
-    // container invisible, just proceed font+color changing:
-    else
-    {
-        DrawInvisible(dc, x, y, info);
-    }
 }
 
 
 }
 
 
@@ -1083,10 +1094,10 @@ wxHtmlCell *wxHtmlContainerCell::FindCellByPos(wxCoord x, wxCoord y,
             if ( cell->IsFormattingCell() )
                 continue;
             int cellY = cell->GetPosY();
             if ( cell->IsFormattingCell() )
                 continue;
             int cellY = cell->GetPosY();
-            if (!( y < cellY || (y < cellY + cell->GetHeight() && 
+            if (!( y < cellY || (y < cellY + cell->GetHeight() &&
                                  x < cell->GetPosX() + cell->GetWidth()) ))
                 continue;
                                  x < cell->GetPosX() + cell->GetWidth()) ))
                 continue;
-            
+
             c = cell->FindCellByPos(x - cell->GetPosX(), y - cellY, flags);
             if (c) return c;
         }
             c = cell->FindCellByPos(x - cell->GetPosX(), y - cellY, flags);
             if (c) return c;
         }
@@ -1172,7 +1183,7 @@ static bool IsEmptyContainer(wxHtmlContainerCell *cell)
 }
 
 void wxHtmlContainerCell::RemoveExtraSpacing(bool top, bool bottom)
 }
 
 void wxHtmlContainerCell::RemoveExtraSpacing(bool top, bool bottom)
-{   
+{
     if ( top )
         SetIndent(0, wxHTML_INDENT_TOP);
     if ( bottom )
     if ( top )
         SetIndent(0, wxHTML_INDENT_TOP);
     if ( bottom )
@@ -1206,13 +1217,13 @@ void wxHtmlContainerCell::RemoveExtraSpacing(bool top, bool bottom)
                 }
             }
         }
                 }
             }
         }
-        
+
         if ( bottom )
         {
             wxArrayPtrVoid arr;
             for ( c = m_Cells; c; c = c->GetNext() )
                 arr.Add((void*)c);
         if ( bottom )
         {
             wxArrayPtrVoid arr;
             for ( c = m_Cells; c; c = c->GetNext() )
                 arr.Add((void*)c);
-           
+
             for ( int i = arr.GetCount() - 1; i >= 0; i--)
             {
                 c = (wxHtmlCell*)arr[i];
             for ( int i = arr.GetCount() - 1; i >= 0; i--)
             {
                 c = (wxHtmlCell*)arr[i];
@@ -1226,7 +1237,8 @@ void wxHtmlContainerCell::RemoveExtraSpacing(bool top, bool bottom)
                     cont = (wxHtmlContainerCell*)c;
                     if ( IsEmptyContainer(cont) )
                     {
                     cont = (wxHtmlContainerCell*)c;
                     if ( IsEmptyContainer(cont) )
                     {
-                        cont->SetIndent(0, wxHTML_INDENT_VERTICAL);                                 }
+                        cont->SetIndent(0, wxHTML_INDENT_VERTICAL);
+                    }
                     else
                     {
                         cont->RemoveExtraSpacing(false, true);
                     else
                     {
                         cont->RemoveExtraSpacing(false, true);