]> git.saurik.com Git - wxWidgets.git/blobdiff - src/html/htmlcell.cpp
test popup menu help strings
[wxWidgets.git] / src / html / htmlcell.cpp
index b54d5d19538e932ed7ab439ff2b2e21c1b4ad884..c867b38366010622b3cfeb4a8adffce97d3276cb 100644 (file)
 #include "wx/settings.h"
 #include <stdlib.h>
 
+//-----------------------------------------------------------------------------
+// Helper classes
+//-----------------------------------------------------------------------------
+
+void wxHtmlSelection::Set(const wxPoint& fromPos, wxHtmlCell *fromCell,
+                          const wxPoint& toPos, wxHtmlCell *toCell)
+{
+    m_fromCell = fromCell;
+    m_toCell = toCell;
+    m_fromPos = fromPos;
+    m_toPos = toPos;
+}
+
+void wxHtmlSelection::Set(wxHtmlCell *fromCell, wxHtmlCell *toCell)
+{
+    wxPoint p1 = fromCell ? fromCell->GetAbsPos() : wxDefaultPosition;
+    wxPoint p2 = toCell ? toCell->GetAbsPos() : wxDefaultPosition;
+    if ( toCell )
+    {
+        p2.x += toCell->GetWidth()-1;
+        p2.y += toCell->GetHeight()-1;
+    }
+    Set(p1, fromCell, p2, toCell);
+}
 
 //-----------------------------------------------------------------------------
 // wxHtmlCell
@@ -147,6 +171,54 @@ wxPoint wxHtmlCell::GetAbsPos() const
     }
     return p;
 }
+    
+unsigned wxHtmlCell::GetDepth() const
+{
+    unsigned d = 0;
+    for (wxHtmlCell *p = m_Parent; p; p = p->m_Parent)
+        d++;
+    return d;
+}
+    
+bool wxHtmlCell::IsBefore(wxHtmlCell *cell) const
+{
+    const wxHtmlCell *c1 = this;
+    const wxHtmlCell *c2 = cell;
+    unsigned d1 = GetDepth();
+    unsigned d2 = cell->GetDepth();
+
+    if ( d1 > d2 )
+        for (; d1 != d2; d1-- )
+            c1 = c1->m_Parent;
+    else if ( d1 < d2 )
+        for (; d1 != d2; d2-- )
+            c2 = c2->m_Parent;
+
+    if ( cell == this )
+        return true;
+
+    while ( c1 && c2 )
+    {
+        if ( c1->m_Parent == c2->m_Parent )
+        {
+            while ( c1 )
+            {
+                if ( c1 == c2 )
+                    return true;
+                c1 = c1->GetNext();
+            }            
+            return false;
+        }
+        else
+        {
+            c1 = c1->m_Parent;
+            c2 = c2->m_Parent;
+        }
+    }
+
+    wxFAIL_MSG(_T("Cells are in different trees"));
+    return false;
+}
 
 
 //-----------------------------------------------------------------------------
@@ -183,8 +255,16 @@ void wxHtmlWordCell::Draw(wxDC& dc, int x, int y,
         dc.SetTextBackground(state.GetBgColour());
     }
 
+    // FIXME - use selection
     dc.DrawText(m_Word, x + m_PosX, y + m_PosY);
 }
+    
+
+wxString wxHtmlWordCell::ConvertToText(wxHtmlSelection *sel) const
+{
+    // FIXME - use selection
+    return m_Word;
+}
 
 
 
@@ -266,7 +346,7 @@ bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak, int* known_pagebreaks,
 
     else
     {
-        wxHtmlCell *c = GetFirstCell();
+        wxHtmlCell *c = GetFirstChild();
         bool rt = FALSE;
         int pbrk = *pagebreak - m_PosY;
 
@@ -637,7 +717,7 @@ const wxHtmlCell* wxHtmlContainerCell::Find(int condition, const void* param) co
 wxHtmlCell *wxHtmlContainerCell::FindCellByPos(wxCoord x, wxCoord y,
                                                unsigned flags) const
 {
-    if (flags & wxHTML_FIND_EXACT)
+    if ( flags & wxHTML_FIND_EXACT )
     {
         for ( const wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext() )
         {
@@ -650,10 +730,8 @@ wxHtmlCell *wxHtmlContainerCell::FindCellByPos(wxCoord x, wxCoord y,
                 return cell->FindCellByPos(x - cx, y - cy, flags);
             }
         }
-        return NULL;
     }
-
-    if ( flags & wxHTML_FIND_NEAREST_AFTER )
+    else if ( flags & wxHTML_FIND_NEAREST_AFTER )
     {
         wxHtmlCell *c;
         int y2;
@@ -666,24 +744,22 @@ wxHtmlCell *wxHtmlContainerCell::FindCellByPos(wxCoord x, wxCoord y,
                                     flags);
             if (c) return c;
         }
-        return NULL;
     }
-
-    if ( flags & wxHTML_FIND_NEAREST_BEFORE )
+    else if ( flags & wxHTML_FIND_NEAREST_BEFORE )
     {
-        wxHtmlCell *c = NULL;
-        wxHtmlCell *cx;
+        wxHtmlCell *c;
         for ( const wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext() )
         {
             if (cell->GetPosY() > y || 
                     (cell->GetPosY() == y && cell->GetPosX() > x))
                 break;
-            cx = cell->FindCellByPos(x - cell->GetPosX(), y - cell->GetPosY(),
-                                     flags);
-            if (cx) c = cx;
+            c = cell->FindCellByPos(x - cell->GetPosX(), y - cell->GetPosY(),
+                                    flags);
+            if (c) return c;
         }
-        return c;
     }
+
+    return NULL;
 }
 
 
@@ -722,19 +798,31 @@ void wxHtmlContainerCell::GetHorizontalConstraints(int *left, int *right) const
     
 wxHtmlCell *wxHtmlContainerCell::GetFirstTerminal() const
 {
-    if (m_Cells)
-        return m_Cells->GetFirstTerminal();
-    else
-        return NULL;
+    if ( m_Cells )
+    {
+        wxHtmlCell *c2;
+        for (wxHtmlCell *c = m_Cells; c; c = c->GetNext())
+        {
+            c2 = c->GetFirstTerminal();
+            if ( c2 )
+                return c2;
+        }
+    }
+    return NULL;
 }
 
 wxHtmlCell *wxHtmlContainerCell::GetLastTerminal() const
 {
-    if (m_Cells)
+    if ( m_Cells )
     {
-        wxHtmlCell *c;
-        for (c = m_Cells; c->GetNext(); c = c->GetNext()) {}
-        return c;
+        wxHtmlCell *c, *c2 = NULL;
+        // most common case first:
+        c = m_LastCell->GetLastTerminal();
+        if ( c )
+            return c;
+        for (wxHtmlCell *c = m_Cells; c; c = c->GetNext())
+            c2 = c->GetLastTerminal();
+        return c2;
     }
     else
         return NULL;
@@ -868,4 +956,44 @@ void wxHtmlWidgetCell::Layout(int w)
     wxHtmlCell::Layout(w);
 }
 
+
+
+// ----------------------------------------------------------------------------
+// wxHtmlTerminalCellsInterator
+// ----------------------------------------------------------------------------
+
+const wxHtmlCell* wxHtmlTerminalCellsInterator::operator++()
+{
+    if ( !m_pos )
+        return NULL;
+
+    do
+    {
+        if ( m_pos == m_to )
+        {
+            m_pos = NULL;
+            return NULL;
+        }
+
+        if ( m_pos->GetNext() )
+            m_pos = m_pos->GetNext();
+        else
+        {
+            // we must go up the hierarchy until we reach container where this
+            // is not the last child, and then go down to first terminal cell:
+            while ( m_pos->GetNext() == NULL )
+            {
+                m_pos = m_pos->GetParent();
+                if ( !m_pos )
+                    return NULL;
+            }
+            m_pos = m_pos->GetNext();
+        }
+        while ( m_pos->GetFirstChild() != NULL )
+            m_pos = m_pos->GetFirstChild();
+    } while ( !m_pos->IsTerminalCell() );
+    
+    return m_pos;
+}
+
 #endif