]> git.saurik.com Git - wxWidgets.git/blobdiff - src/generic/datavgen.cpp
If -1 is passed to InsertItem for the imageIndex then don't set wxLIST_MASK_IMAGE
[wxWidgets.git] / src / generic / datavgen.cpp
index d0260367993d5ecc40cf43ded60e35b8c60569d7..bdc389ad666390c374662c5c18d328f1bc55b2cc 100644 (file)
@@ -214,6 +214,8 @@ private:
             model->Resort();
 
         owner->OnColumnChange(idx);
             model->Resort();
 
         owner->OnColumnChange(idx);
+
+        SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, idx);
     }
 
     void OnRClick(wxHeaderCtrlEvent& event)
     }
 
     void OnRClick(wxHeaderCtrlEvent& event)
@@ -532,6 +534,8 @@ public:
     int GetLineHeight( unsigned int row ) const; // m_lineHeight in fixed mode
     int GetLineAt( unsigned int y ) const;       // y / m_lineHeight in fixed mode
 
     int GetLineHeight( unsigned int row ) const; // m_lineHeight in fixed mode
     int GetLineAt( unsigned int y ) const;       // y / m_lineHeight in fixed mode
 
+    void SetRowHeight( int lineHeight ) { m_lineHeight = lineHeight; }
+
     // Some useful functions for row and item mapping
     wxDataViewItem GetItemByRow( unsigned int row ) const;
     int GetRowByItem( const wxDataViewItem & item ) const;
     // Some useful functions for row and item mapping
     wxDataViewItem GetItemByRow( unsigned int row ) const;
     int GetRowByItem( const wxDataViewItem & item ) const;
@@ -732,7 +736,7 @@ bool wxDataViewTextRenderer::HasEditorCtrl() const
     return true;
 }
 
     return true;
 }
 
-wxControl* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow *parent,
+wxWindow* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow *parent,
         wxRect labelRect, const wxVariant &value )
 {
     wxTextCtrl* ctrl = new wxTextCtrl( parent, wxID_ANY, value,
         wxRect labelRect, const wxVariant &value )
 {
     wxTextCtrl* ctrl = new wxTextCtrl( parent, wxID_ANY, value,
@@ -746,7 +750,7 @@ wxControl* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow *parent,
     return ctrl;
 }
 
     return ctrl;
 }
 
-bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl *editor, wxVariant &value )
+bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxWindow *editor, wxVariant &value )
 {
     wxTextCtrl *text = (wxTextCtrl*) editor;
     value = text->GetValue();
 {
     wxTextCtrl *text = (wxTextCtrl*) editor;
     value = text->GetValue();
@@ -796,9 +800,9 @@ bool wxDataViewBitmapRenderer::GetValue( wxVariant& WXUNUSED(value) ) const
 
 bool wxDataViewBitmapRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
 {
 
 bool wxDataViewBitmapRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
 {
-    if (m_bitmap.Ok())
+    if (m_bitmap.IsOk())
         dc->DrawBitmap( m_bitmap, cell.x, cell.y );
         dc->DrawBitmap( m_bitmap, cell.x, cell.y );
-    else if (m_icon.Ok())
+    else if (m_icon.IsOk())
         dc->DrawIcon( m_icon, cell.x, cell.y );
 
     return true;
         dc->DrawIcon( m_icon, cell.x, cell.y );
 
     return true;
@@ -806,9 +810,9 @@ bool wxDataViewBitmapRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state
 
 wxSize wxDataViewBitmapRenderer::GetSize() const
 {
 
 wxSize wxDataViewBitmapRenderer::GetSize() const
 {
-    if (m_bitmap.Ok())
+    if (m_bitmap.IsOk())
         return wxSize( m_bitmap.GetWidth(), m_bitmap.GetHeight() );
         return wxSize( m_bitmap.GetWidth(), m_bitmap.GetHeight() );
-    else if (m_icon.Ok())
+    else if (m_icon.IsOk())
         return wxSize( m_icon.GetWidth(), m_icon.GetHeight() );
 
     return wxSize(wxDVC_DEFAULT_RENDERER_SIZE,wxDVC_DEFAULT_RENDERER_SIZE);
         return wxSize( m_icon.GetWidth(), m_icon.GetHeight() );
 
     return wxSize(wxDVC_DEFAULT_RENDERER_SIZE,wxDVC_DEFAULT_RENDERER_SIZE);
@@ -863,15 +867,23 @@ bool wxDataViewToggleRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state
     return true;
 }
 
     return true;
 }
 
-void wxDataViewToggleRenderer::WXOnActivate(wxDataViewModel *model,
-                                            const wxVariant& valueOld,
-                                            const wxDataViewItem & item,
-                                            unsigned int col)
+bool wxDataViewToggleRenderer::WXOnLeftClick(const wxPoint& cursor,
+                                             const wxRect& WXUNUSED(cell),
+                                             wxDataViewModel *model,
+                                             const wxDataViewItem& item,
+                                             unsigned int col)
 {
 {
+    // only react to clicks directly on the checkbox, not elsewhere in the same cell:
+    if (!wxRect(GetSize()).Contains(cursor))
+        return false;
+
     if (model->IsEnabled(item, col))
     {
     if (model->IsEnabled(item, col))
     {
-        model->ChangeValue(!valueOld.GetBool(), item, col);
+        model->ChangeValue(!m_toggle, item, col);
+        return true;
     }
     }
+
+    return false;
 }
 
 wxSize wxDataViewToggleRenderer::GetSize() const
 }
 
 wxSize wxDataViewToggleRenderer::GetSize() const
@@ -1023,12 +1035,12 @@ wxSize wxDataViewDateRenderer::GetSize() const
     return GetTextExtent(m_date.FormatDate());
 }
 
     return GetTextExtent(m_date.FormatDate());
 }
 
-void wxDataViewDateRenderer::WXOnActivate(wxDataViewModel *model,
-                                          const wxVariant& valueOld,
-                                          const wxDataViewItem & item,
-                                          unsigned int col )
+bool wxDataViewDateRenderer::WXOnActivate(const wxRect& WXUNUSED(cell),
+                                          wxDataViewModel *model,
+                                          const wxDataViewItem& item,
+                                          unsigned int col)
 {
 {
-    wxDateTime dtOld = valueOld.GetDateTime();
+    wxDateTime dtOld = m_date;
 
 #if wxUSE_DATE_RENDERER_POPUP
     wxDataViewDateRendererPopupTransient *popup = new wxDataViewDateRendererPopupTransient(
 
 #if wxUSE_DATE_RENDERER_POPUP
     wxDataViewDateRendererPopupTransient *popup = new wxDataViewDateRendererPopupTransient(
@@ -1040,6 +1052,8 @@ void wxDataViewDateRenderer::WXOnActivate(wxDataViewModel *model,
 #else // !wxUSE_DATE_RENDERER_POPUP
     wxMessageBox(dtOld.Format());
 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
 #else // !wxUSE_DATE_RENDERER_POPUP
     wxMessageBox(dtOld.Format());
 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
+
+    return true;
 }
 
 // ---------------------------------------------------------
 }
 
 // ---------------------------------------------------------
@@ -1096,7 +1110,7 @@ wxSize wxDataViewIconTextRenderer::GetSize() const
     return wxSize(80,20);
 }
 
     return wxSize(80,20);
 }
 
-wxControl* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow *parent, wxRect labelRect, const wxVariant& value)
+wxWindow* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow *parent, wxRect labelRect, const wxVariant& value)
 {
     wxDataViewIconText iconText;
     iconText << value;
 {
     wxDataViewIconText iconText;
     iconText << value;
@@ -1122,7 +1136,7 @@ wxControl* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow *parent, wxRect
     return ctrl;
 }
 
     return ctrl;
 }
 
-bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxControl *editor, wxVariant& value )
+bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxWindow *editor, wxVariant& value )
 {
     wxTextCtrl *text = (wxTextCtrl*) editor;
 
 {
     wxTextCtrl *text = (wxTextCtrl*) editor;
 
@@ -1587,6 +1601,12 @@ void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
     dc.DrawRectangle(GetClientSize());
 #endif
 
     dc.DrawRectangle(GetClientSize());
 #endif
 
+    if ( IsEmpty() )
+    {
+        // No items to draw.
+        return;
+    }
+
     // prepare the DC
     GetOwner()->PrepareDC( dc );
     dc.SetFont( GetFont() );
     // prepare the DC
     GetOwner()->PrepareDC( dc );
     dc.SetFont( GetFont() );
@@ -1600,10 +1620,11 @@ void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
         wxMin( (int)(  GetLineAt( wxMax(0,update.y+update.height) ) - item_start + 1),
             (int)(GetRowCount( ) - item_start));
     unsigned int item_last = item_start + item_count;
         wxMin( (int)(  GetLineAt( wxMax(0,update.y+update.height) ) - item_start + 1),
             (int)(GetRowCount( ) - item_start));
     unsigned int item_last = item_start + item_count;
-    // Get the parent of DataViewCtrl
-    wxWindow *parent = GetParent()->GetParent();
+
+    // Send the event to wxDataViewCtrl itself.
+    wxWindow * const parent = GetParent();
     wxDataViewEvent cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT, parent->GetId());
     wxDataViewEvent cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT, parent->GetId());
-    cache_event.SetEventObject(GetParent());
+    cache_event.SetEventObject(parent);
     cache_event.SetCache(item_start, item_last - 1);
     parent->ProcessWindowEvent(cache_event);
 
     cache_event.SetCache(item_start, item_last - 1);
     parent->ProcessWindowEvent(cache_event);
 
@@ -1664,22 +1685,23 @@ void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
         dc.SetPen(m_penRule);
         dc.SetBrush(*wxTRANSPARENT_BRUSH);
 
         dc.SetPen(m_penRule);
         dc.SetBrush(*wxTRANSPARENT_BRUSH);
 
-        int x = x_start;
+        // NB: Vertical rules are drawn in the last pixel of a column so that
+        //     they align perfectly with native MSW wxHeaderCtrl as well as for
+        //     consistency with MSW native list control. There's no vertical
+        //     rule at the most-left side of the control.
+
+        int x = x_start - 1;
         for (unsigned int i = col_start; i < col_last; i++)
         {
             wxDataViewColumn *col = GetOwner()->GetColumnAt(i);
             if (col->IsHidden())
                 continue;       // skip it
 
         for (unsigned int i = col_start; i < col_last; i++)
         {
             wxDataViewColumn *col = GetOwner()->GetColumnAt(i);
             if (col->IsHidden())
                 continue;       // skip it
 
+            x += col->GetWidth();
+
             dc.DrawLine(x, GetLineStart( item_start ),
                         x, GetLineStart( item_last ) );
             dc.DrawLine(x, GetLineStart( item_start ),
                         x, GetLineStart( item_last ) );
-
-            x += col->GetWidth();
         }
         }
-
-        // Draw last vertical rule
-        dc.DrawLine(x, GetLineStart( item_start ),
-                    x, GetLineStart( item_last ) );
     }
 
     // redraw the background for the items which are selected/current
     }
 
     // redraw the background for the items which are selected/current
@@ -1751,8 +1773,11 @@ void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
 
                 dataitem = node->GetItem();
 
 
                 dataitem = node->GetItem();
 
-                if ((i > 0) && model->IsContainer(dataitem) &&
-                    !model->HasContainerColumns(dataitem))
+                // Skip all columns of "container" rows except the expander
+                // column itself unless HasContainerColumns() overrides this.
+                if ( col != GetOwner()->GetExpanderColumn() &&
+                        model->IsContainer(dataitem) &&
+                            !model->HasContainerColumns(dataitem) )
                     continue;
             }
             else
                     continue;
             }
             else
@@ -3009,7 +3034,7 @@ wxDataViewTreeNode * wxDataViewMainWindow::FindNode( const wxDataViewItem & item
     // Find the item along the parent-chain.
     // This algorithm is designed to speed up the node-finding method
     wxDataViewTreeNode* node = m_root;
     // Find the item along the parent-chain.
     // This algorithm is designed to speed up the node-finding method
     wxDataViewTreeNode* node = m_root;
-    for( unsigned iter = parentChain.size()-1; iter>=0; --iter )
+    for( unsigned iter = parentChain.size()-1; ; --iter )
     {
         if( node->HasChildren() )
         {
     {
         if( node->HasChildren() )
         {
@@ -3040,6 +3065,9 @@ wxDataViewTreeNode * wxDataViewMainWindow::FindNode( const wxDataViewItem & item
         }
         else
             return NULL;
         }
         else
             return NULL;
+
+        if ( !iter )
+            break;
     }
     return NULL;
 }
     }
     return NULL;
 }
@@ -3351,7 +3379,7 @@ void wxDataViewMainWindow::OnChar( wxKeyEvent &event )
             break;
 
         case WXK_DOWN:
             break;
 
         case WXK_DOWN:
-            if ( m_currentRow < GetRowCount() - 1 )
+            if ( m_currentRow + 1 < GetRowCount() )
                 OnArrowChar( m_currentRow + 1, event );
             break;
         // Add the process for tree expanding/collapsing
                 OnArrowChar( m_currentRow + 1, event );
             break;
         // Add the process for tree expanding/collapsing
@@ -3629,19 +3657,11 @@ void wxDataViewMainWindow::OnMouse( wxMouseEvent &event )
             {
                 const unsigned colIdx = col->GetModelColumn();
 
             {
                 const unsigned colIdx = col->GetModelColumn();
 
-                wxVariant value;
-                model->GetValue( value, item, colIdx );
-
-                cell->WXOnActivate(model, value, item, colIdx);
-
-                if ( wxDataViewCustomRenderer *custom = cell->WXGetAsCustom() )
-                {
-                    cell->SetValue( value );
+                cell->PrepareForItem(model, item, colIdx);
 
 
-                    wxRect cell_rect( xpos, GetLineStart( current ),
-                                    col->GetWidth(), GetLineHeight( current ) );
-                    custom->Activate( cell_rect, model, item, colIdx );
-                }
+                wxRect cell_rect( xpos, GetLineStart( current ),
+                                col->GetWidth(), GetLineHeight( current ) );
+                cell->WXOnActivate( cell_rect, model, item, colIdx );
             }
             else
             {
             }
             else
             {
@@ -3708,8 +3728,10 @@ void wxDataViewMainWindow::OnMouse( wxMouseEvent &event )
         if (!IsRowSelected(current))
         {
             SelectAllRows(false);
         if (!IsRowSelected(current))
         {
             SelectAllRows(false);
+            const unsigned oldCurrent = m_currentRow;
             ChangeCurrentRow(current);
             SelectRow(m_currentRow,true);
             ChangeCurrentRow(current);
             SelectRow(m_currentRow,true);
+            RefreshRow(oldCurrent);
             SendSelectionChangedEvent(GetItemByRow( m_currentRow ) );
         }
     }
             SendSelectionChangedEvent(GetItemByRow( m_currentRow ) );
         }
     }
@@ -3812,53 +3834,50 @@ void wxDataViewMainWindow::OnMouse( wxMouseEvent &event )
         // Call LeftClick after everything else as under GTK+
         if (cell->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE)
         {
         // Call LeftClick after everything else as under GTK+
         if (cell->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE)
         {
-            if ( wxDataViewCustomRenderer *custom = cell->WXGetAsCustom() )
-            {
-                // notify cell about click
-                custom->PrepareForItem(model, item, col->GetModelColumn());
+            // notify cell about click
+            cell->PrepareForItem(model, item, col->GetModelColumn());
 
 
-                wxRect cell_rect( xpos, GetLineStart( current ),
-                                  col->GetWidth(), GetLineHeight( current ) );
+            wxRect cell_rect( xpos, GetLineStart( current ),
+                              col->GetWidth(), GetLineHeight( current ) );
 
 
-                // Report position relative to the cell's custom area, i.e.
-                // no the entire space as given by the control but the one
-                // used by the renderer after calculation of alignment etc.
+            // Report position relative to the cell's custom area, i.e.
+            // no the entire space as given by the control but the one
+            // used by the renderer after calculation of alignment etc.
 
 
-                // adjust the rectangle ourselves to account for the alignment
-                wxRect rectItem = cell_rect;
-                const int align = custom->GetAlignment();
-                if ( align != wxDVR_DEFAULT_ALIGNMENT )
-                {
-                    const wxSize size = custom->GetSize();
+            // adjust the rectangle ourselves to account for the alignment
+            wxRect rectItem = cell_rect;
+            const int align = cell->GetAlignment();
+            if ( align != wxDVR_DEFAULT_ALIGNMENT )
+            {
+                const wxSize size = cell->GetSize();
 
 
-                    if ( size.x >= 0 && size.x < cell_rect.width )
-                    {
-                        if ( align & wxALIGN_CENTER_HORIZONTAL )
-                            rectItem.x += (cell_rect.width - size.x)/2;
-                        else if ( align & wxALIGN_RIGHT )
-                            rectItem.x += cell_rect.width - size.x;
-                        // else: wxALIGN_LEFT is the default
-                    }
+                if ( size.x >= 0 && size.x < cell_rect.width )
+                {
+                    if ( align & wxALIGN_CENTER_HORIZONTAL )
+                        rectItem.x += (cell_rect.width - size.x)/2;
+                    else if ( align & wxALIGN_RIGHT )
+                        rectItem.x += cell_rect.width - size.x;
+                    // else: wxALIGN_LEFT is the default
+                }
 
 
-                    if ( size.y >= 0 && size.y < cell_rect.height )
-                    {
-                        if ( align & wxALIGN_CENTER_VERTICAL )
-                            rectItem.y += (cell_rect.height - size.y)/2;
-                        else if ( align & wxALIGN_BOTTOM )
-                            rectItem.y += cell_rect.height - size.y;
-                        // else: wxALIGN_TOP is the default
-                    }
+                if ( size.y >= 0 && size.y < cell_rect.height )
+                {
+                    if ( align & wxALIGN_CENTER_VERTICAL )
+                        rectItem.y += (cell_rect.height - size.y)/2;
+                    else if ( align & wxALIGN_BOTTOM )
+                        rectItem.y += cell_rect.height - size.y;
+                    // else: wxALIGN_TOP is the default
                 }
                 }
+            }
 
 
-                wxPoint pos( event.GetPosition() );
-                pos.x -= rectItem.x;
-                pos.y -= rectItem.y;
+            wxPoint pos( event.GetPosition() );
+            pos.x -= rectItem.x;
+            pos.y -= rectItem.y;
 
 
-                m_owner->CalcUnscrolledPosition( pos.x, pos.y, &pos.x, &pos.y );
+            m_owner->CalcUnscrolledPosition( pos.x, pos.y, &pos.x, &pos.y );
 
 
-                 /* ignore ret */ custom->LeftClick( pos, cell_rect,
-                                  model, item, col->GetModelColumn());
-            }
+             /* ignore ret */ cell->WXOnLeftClick( pos, cell_rect,
+                              model, item, col->GetModelColumn());
         }
     }
 }
         }
     }
 }
@@ -4013,6 +4032,16 @@ void wxDataViewCtrl::OnSize( wxSizeEvent &WXUNUSED(event) )
     Layout();
 
     AdjustScrollbars();
     Layout();
 
     AdjustScrollbars();
+
+    // We must redraw the headers if their height changed. Normally this
+    // shouldn't happen as the control shouldn't let itself be resized beneath
+    // its minimal height but avoid the display artefacts that appear if it
+    // does happen, e.g. because there is really not enough vertical space.
+    if ( !HasFlag(wxDV_NO_HEADER) && m_headerArea &&
+            m_headerArea->GetSize().y <= m_headerArea->GetBestSize(). y )
+    {
+        m_headerArea->Refresh();
+    }
 }
 
 void wxDataViewCtrl::SetFocus()
 }
 
 void wxDataViewCtrl::SetFocus()
@@ -4117,6 +4146,16 @@ unsigned int wxDataViewCtrl::GetColumnCount() const
     return m_cols.GetCount();
 }
 
     return m_cols.GetCount();
 }
 
+bool wxDataViewCtrl::SetRowHeight( int lineHeight )
+{
+    if ( !m_clientArea )
+        return false;
+
+    m_clientArea->SetRowHeight(lineHeight);
+
+    return true;
+}
+
 wxDataViewColumn* wxDataViewCtrl::GetColumn( unsigned int idx ) const
 {
     return m_cols[idx];
 wxDataViewColumn* wxDataViewCtrl::GetColumn( unsigned int idx ) const
 {
     return m_cols[idx];