]> git.saurik.com Git - wxWidgets.git/blobdiff - src/generic/listctrl.cpp
Fix to prevent Assert when there is an empty string in a combobox/choice.
[wxWidgets.git] / src / generic / listctrl.cpp
index d0a1bd2fcdcc63d1d5ecc9da76f17f5018b87505..8b9aaee667ffc1846959ae565dda12496743a3c6 100644 (file)
@@ -20,7 +20,7 @@
 
 #include "wx/dcscreen.h"
 #include "wx/app.h"
-#include "wx/generic/listctrl.h"
+#include "wx/listctrl.h"
 #include "wx/generic/imaglist.h"
 
 //-----------------------------------------------------------------------------
@@ -29,7 +29,7 @@
 
 IMPLEMENT_DYNAMIC_CLASS(wxListItemData,wxObject);
 
-wxListItemData::wxListItemData(void)
+wxListItemData::wxListItemData()
 {
     m_image = -1;
     m_data = 0;
@@ -92,12 +92,12 @@ void wxListItemData::SetColour( wxColour *col )
     m_colour = col;
 }
 
-bool wxListItemData::HasImage(void) const
+bool wxListItemData::HasImage() const
 {
     return (m_image >= 0);
 }
 
-bool wxListItemData::HasText(void) const
+bool wxListItemData::HasText() const
 {
     return (!m_text.IsNull());
 }
@@ -112,27 +112,27 @@ void wxListItemData::GetText( wxString &s )
     s = m_text;
 }
 
-int wxListItemData::GetX( void ) const
+int wxListItemData::GetX() const
 {
     return m_xpos;
 }
 
-int wxListItemData::GetY( void ) const
+int wxListItemData::GetY() const
 {
     return m_ypos;
 }
 
-int wxListItemData::GetWidth(void) const
+int wxListItemData::GetWidth() const
 {
     return m_width;
 }
 
-int wxListItemData::GetHeight(void) const
+int wxListItemData::GetHeight() const
 {
     return m_height;
 }
 
-int wxListItemData::GetImage(void) const
+int wxListItemData::GetImage() const
 {
     return m_image;
 }
@@ -144,7 +144,7 @@ void wxListItemData::GetItem( wxListItem &info )
     info.m_data = m_data;
 }
 
-wxColour *wxListItemData::GetColour(void)
+wxColour *wxListItemData::GetColour()
 {
     return m_colour;
 }
@@ -155,7 +155,7 @@ wxColour *wxListItemData::GetColour(void)
 
 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderData,wxObject);
 
-wxListHeaderData::wxListHeaderData(void)
+wxListHeaderData::wxListHeaderData()
 {
     m_mask = 0;
     m_image = 0;
@@ -208,12 +208,12 @@ void wxListHeaderData::SetFormat( int format )
     m_format = format;
 }
 
-bool wxListHeaderData::HasImage(void) const
+bool wxListHeaderData::HasImage() const
 {
     return (m_image != 0);
 }
 
-bool wxListHeaderData::HasText(void) const
+bool wxListHeaderData::HasText() const
 {
     return (m_text.Length() > 0);
 }
@@ -237,17 +237,17 @@ void wxListHeaderData::GetText( wxString &s )
     s =  m_text;
 }
 
-int wxListHeaderData::GetImage(void) const
+int wxListHeaderData::GetImage() const
 {
     return m_image;
 }
 
-int wxListHeaderData::GetWidth(void) const
+int wxListHeaderData::GetWidth() const
 {
     return m_width;
 }
 
-int wxListHeaderData::GetFormat(void) const
+int wxListHeaderData::GetFormat() const
 {
     return m_format;
 }
@@ -899,6 +899,9 @@ wxListTextCtrl::wxListTextCtrl( wxWindow *parent, const wxWindowID id,
     m_res = res;
     m_accept = accept;
     m_owner = owner;
+    (*m_accept) = FALSE;
+    (*m_res) = "";
+    m_startValue = value;
 }
 
 void wxListTextCtrl::OnChar( wxKeyEvent &event )
@@ -907,17 +910,14 @@ void wxListTextCtrl::OnChar( wxKeyEvent &event )
     {
         (*m_accept) = TRUE;
         (*m_res) = GetValue();
-        m_owner->OnRenameAccept();
-//      Show( FALSE );
-        Destroy();
+       m_owner->SetFocus();
         return;
     }
     if (event.m_keyCode == WXK_ESCAPE)
     {
         (*m_accept) = FALSE;
         (*m_res) = "";
-//      Show( FALSE );
-        Destroy();
+       m_owner->SetFocus();
         return;
     }
     event.Skip();
@@ -925,11 +925,12 @@ void wxListTextCtrl::OnChar( wxKeyEvent &event )
 
 void wxListTextCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
 {
-    (*m_accept) = FALSE;
-    (*m_res) = "";
-//  Show( FALSE );
-    Destroy();
-    return;
+    if (wxPendingDelete.Member(this)) return;
+
+    wxPendingDelete.Append(this);
+    
+    if ((*m_accept) && ((*m_res) != m_startValue))
+        m_owner->OnRenameAccept();
 }
 
 //-----------------------------------------------------------------------------
@@ -948,7 +949,7 @@ BEGIN_EVENT_TABLE(wxListMainWindow,wxScrolledWindow)
   EVT_KILL_FOCUS     (wxListMainWindow::OnKillFocus)
 END_EVENT_TABLE()
 
-wxListMainWindow::wxListMainWindow( void )
+wxListMainWindow::wxListMainWindow()
 {
     m_mode = 0;
     m_lines.DeleteContents( TRUE );
@@ -1013,7 +1014,7 @@ wxListMainWindow::wxListMainWindow( wxWindow *parent, wxWindowID id,
     SetBackgroundColour( *wxWHITE );
 }
 
-wxListMainWindow::~wxListMainWindow( void )
+wxListMainWindow::~wxListMainWindow()
 {
     if (m_hilightBrush) delete m_hilightBrush;
     
@@ -1140,31 +1141,36 @@ void wxListMainWindow::DeleteLine( wxListLineData *line )
     SendNotify( line, wxEVT_COMMAND_LIST_DELETE_ITEM );
 }
 
-void wxListMainWindow::StartLabelEdit( wxListLineData *line )
-{
-    SendNotify( line, wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT );
-}
+/* *** */
 
-void wxListMainWindow::RenameLine( wxListLineData *line, const wxString &newName )
+void wxListMainWindow::EditLabel( long item )
 {
-    wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() );
+    wxNode *node = m_lines.Nth( item );
+    wxCHECK_RET( node, _T("wrong index in wxListCtrl::Edit()") );
+    
+    m_currentEdit = (wxListLineData*) node->Data();
+
+    wxListEvent le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT, GetParent()->GetId() );
     le.SetEventObject( GetParent() );
-    le.m_itemIndex = GetIndexOfLine( line );
-    line->GetItem( 0, le.m_item );
-    le.m_item.m_text = newName;
+    le.m_itemIndex = GetIndexOfLine( m_currentEdit );
+    m_currentEdit->GetItem( 0, le.m_item );
     GetParent()->GetEventHandler()->ProcessEvent( le );
-}
+    
+    if (!le.IsAllowed())
+        return;
+    
+    // We have to call this here because the label in
+    // question might just have been added and no screen
+    // update taken place.
+    if (m_dirty) wxYield();
 
-void wxListMainWindow::OnRenameTimer()
-{
-    StartLabelEdit( m_current );
     wxString s;
-    m_current->GetText( 0, s );
+    m_currentEdit->GetText( 0, s );
     int x = 0;
     int y = 0;
     int w = 0;
     int h = 0;
-    m_current->GetLabelExtent( x, y, w, h );
+    m_currentEdit->GetLabelExtent( x, y, w, h );
 
     wxClientDC dc(this);
     PrepareDC( dc );
@@ -1176,9 +1182,29 @@ void wxListMainWindow::OnRenameTimer()
     text->SetFocus();
 }
 
+void wxListMainWindow::OnRenameTimer()
+{
+    wxCHECK_RET( m_current, _T("invalid m_current") );
+    
+    Edit( m_lines.IndexOf( m_current ) );
+}
+
 void wxListMainWindow::OnRenameAccept()
 {
-    RenameLine( m_current, m_renameRes );
+    wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() );
+    le.SetEventObject( GetParent() );
+    le.m_itemIndex = GetIndexOfLine( m_currentEdit );
+    m_currentEdit->GetItem( 0, le.m_item );
+    le.m_item.m_text = m_renameRes;
+    GetParent()->GetEventHandler()->ProcessEvent( le );
+    
+    if (!le.IsAllowed()) return;
+    
+    wxListItem info;
+    info.m_mask = wxLIST_MASK_TEXT;
+    info.m_itemId = le.m_itemIndex;
+    info.m_text = m_renameRes;
+    SetItem( info );
 }
 
 void wxListMainWindow::OnMouse( wxMouseEvent &event )
@@ -1187,6 +1213,7 @@ void wxListMainWindow::OnMouse( wxMouseEvent &event )
 
     if (!m_current) return;
     if (m_dirty) return;
+    if ( !(event.Dragging() || event.ButtonDown() || event.LeftUp()) ) return;
 
     wxClientDC dc(this);
     PrepareDC(dc);
@@ -1206,23 +1233,29 @@ void wxListMainWindow::OnMouse( wxMouseEvent &event )
         node = node->Next();
     }
 
-    if (!event.Dragging())
-        m_dragCount = 0;
-    else
-        m_dragCount++;
-
-    if (event.Dragging() && (m_dragCount > 3))
+    if (event.Dragging())
     {
-        m_dragCount = 0;
+        if (m_dragCount == 0)
+           m_dragStart = wxPoint(x,y);
+       
+        m_dragCount++;
+       
+       if (m_dragCount != 3) return;
        
-        wxListEvent le( wxEVT_COMMAND_LIST_BEGIN_DRAG, GetParent()->GetId() );
+       int command = wxEVT_COMMAND_LIST_BEGIN_DRAG;
+       if (event.RightIsDown()) command = wxEVT_COMMAND_LIST_BEGIN_RDRAG;
+       
+        wxListEvent le( command, GetParent()->GetId() );
         le.SetEventObject( GetParent() );
-       le.m_pointDrag.x = x;
-       le.m_pointDrag.y = y;
+       le.m_pointDrag = m_dragStart;
        GetParent()->GetEventHandler()->ProcessEvent( le );
        
        return;
     }
+    else
+    {
+        m_dragCount = 0;
+    }
 
     if (!line) return;
 
@@ -1340,7 +1373,7 @@ void wxListMainWindow::OnMouse( wxMouseEvent &event )
     }
 }
 
-void wxListMainWindow::MoveToFocus( void )
+void wxListMainWindow::MoveToFocus()
 {
     if (!m_current) return;
   
@@ -1424,6 +1457,14 @@ void wxListMainWindow::OnChar( wxKeyEvent &event )
     ke.SetEventObject( parent );
     if (parent->GetEventHandler()->ProcessEvent( ke )) return;
   
+    if (event.KeyCode() == WXK_TAB)
+    {
+        wxNavigationKeyEvent nevent;
+        nevent.SetDirection( !event.ShiftDown() );
+        nevent.SetCurrentFocus( m_parent );
+        if (m_parent->GetEventHandler()->ProcessEvent( nevent )) return;
+    }
+  
     /* no item -> nothing to do */
     if (!m_current)
     {
@@ -1687,7 +1728,44 @@ void wxListMainWindow::SetColumnWidth( int col, int width )
 
     m_dirty = TRUE;
 
-    wxNode *node = m_columns.Nth( col );
+    wxNode *node = (wxNode*) NULL;
+
+    if (width == wxLIST_AUTOSIZE_USEHEADER) width = 80;
+    if (width == wxLIST_AUTOSIZE)
+    {
+        wxClientDC dc(this);
+        dc.SetFont( GetFont() );
+        int max = 10;
+        node = m_lines.First();
+        while (node)
+        {
+            wxListLineData *line = (wxListLineData*)node->Data();
+            wxNode *n = line->m_items.Nth( col );
+            if (n)
+            {
+                wxListItemData *item = (wxListItemData*)n->Data();
+               int current = 0, ix = 0, iy = 0;
+               long lx = 0, ly = 0;
+               if (item->HasImage())
+               {
+                    GetImageSize( item->GetImage(), ix, iy );
+                   current = ix + 5;
+               }
+               if (item->HasText())
+               {
+                   wxString str;
+                   item->GetText( str );
+                   dc.GetTextExtent( str, &lx, &ly );
+                   current += lx;
+               }
+               if (current > max) max = current;
+            }
+            node = node->Next();
+        }
+       width = max+10;
+    }
+
+    node = m_columns.Nth( col );
     if (node)
     {
         wxListHeaderData *column = (wxListHeaderData*)node->Data();
@@ -1743,12 +1821,12 @@ int wxListMainWindow::GetColumnWidth( int col )
     }
 }
 
-int wxListMainWindow::GetColumnCount( void )
+int wxListMainWindow::GetColumnCount()
 {
     return m_columns.Number();
 }
 
-int wxListMainWindow::GetCountPerPage( void )
+int wxListMainWindow::GetCountPerPage()
 {
     return m_visibleLines;
 }
@@ -1781,7 +1859,7 @@ void wxListMainWindow::SetItemState( long item, long state, long stateMask )
             m_current = line;
             FocusLine( m_current );
             RefreshLine( m_current );
-            RefreshLine( oldCurrent );
+            if (oldCurrent) RefreshLine( oldCurrent );
         }
     }
 
@@ -1799,9 +1877,9 @@ void wxListMainWindow::SetItemState( long item, long state, long stateMask )
                 UnfocusLine( m_current );
                 m_current = line;
                 FocusLine( m_current );
-                oldCurrent->Hilight( FALSE );
+                if (oldCurrent) oldCurrent->Hilight( FALSE );
                 RefreshLine( m_current );
-                RefreshLine( oldCurrent );
+                if (oldCurrent) RefreshLine( oldCurrent );
             }
             bool on = state & wxLIST_STATE_SELECTED;
            if (on != line->IsHilighted())
@@ -1854,7 +1932,7 @@ void wxListMainWindow::GetItem( wxListItem &item )
     }
 }
 
-int wxListMainWindow::GetItemCount( void )
+int wxListMainWindow::GetItemCount()
 {
     return m_lines.Number();
 }
@@ -1895,7 +1973,7 @@ bool wxListMainWindow::GetItemPosition(long item, wxPoint& pos)
     return TRUE;
 }
 
-int wxListMainWindow::GetSelectedItemCount( void )
+int wxListMainWindow::GetSelectedItemCount()
 {
     int ret = 0;
     wxNode *node = m_lines.First();
@@ -1927,12 +2005,12 @@ void wxListMainWindow::SetMode( long mode )
     }
 }
 
-long wxListMainWindow::GetMode( void ) const
+long wxListMainWindow::GetMode() const
 {
     return m_mode;
 }
 
-void wxListMainWindow::CalculatePositions( void )
+void wxListMainWindow::CalculatePositions()
 {
     if (!m_lines.First()) return;
 
@@ -2123,6 +2201,11 @@ void wxListMainWindow::DeleteEverything( void )
 
 void wxListMainWindow::EnsureVisible( long index )
 {
+    // We have to call this here because the label in
+    // question might just have been added and no screen
+    // update taken place.
+    if (m_dirty) wxYield();
+
     wxListLineData *oldCurrent = m_current;
     m_current = (wxListLineData *) NULL;
     int i = index;
@@ -2267,7 +2350,7 @@ void wxListMainWindow::SortItems( wxListCtrlCompare fn, long data )
 
 IMPLEMENT_DYNAMIC_CLASS(wxListItem, wxObject)
 
-wxListItem::wxListItem(void)
+wxListItem::wxListItem()
 {
     m_mask = 0;
     m_itemId = 0;
@@ -2310,7 +2393,7 @@ BEGIN_EVENT_TABLE(wxListCtrl,wxControl)
   EVT_IDLE          (wxListCtrl::OnIdle)
 END_EVENT_TABLE()
 
-wxListCtrl::wxListCtrl(void)
+wxListCtrl::wxListCtrl()
 {
     m_imageListNormal = (wxImageList *) NULL;
     m_imageListSmall = (wxImageList *) NULL;
@@ -2319,7 +2402,7 @@ wxListCtrl::wxListCtrl(void)
     m_headerWin = (wxListHeaderWindow*) NULL;
 }
 
-wxListCtrl::~wxListCtrl(void)
+wxListCtrl::~wxListCtrl()
 {
 }
 
@@ -2345,7 +2428,9 @@ bool wxListCtrl::Create( wxWindow *parent, wxWindowID id,
 
     bool ret = wxControl::Create( parent, id, pos, size, s, name );
 
+#if wxUSE_VALIDATORS
     SetValidator( validator );
+#endif
   
     if (s & wxSUNKEN_BORDER) s -= wxSUNKEN_BORDER;
 
@@ -2453,17 +2538,11 @@ bool wxListCtrl::SetColumnWidth( int col, int width )
     return TRUE;
 }
 
-int wxListCtrl::GetCountPerPage(void) const
+int wxListCtrl::GetCountPerPage() const
 {
   return m_mainWin->GetCountPerPage();  // different from Windows ?
 }
 
-/*
-wxText& wxListCtrl::GetEditControl(void) const
-{
-}
-*/
-
 bool wxListCtrl::GetItem( wxListItem &info ) const
 {
     m_mainWin->GetItem( info );
@@ -2565,12 +2644,12 @@ bool wxListCtrl::SetItemPosition( long WXUNUSED(item), const wxPoint& WXUNUSED(p
     return 0;
 }
 
-int wxListCtrl::GetItemCount(void) const
+int wxListCtrl::GetItemCount() const
 {
     return m_mainWin->GetItemCount();
 }
 
-int wxListCtrl::GetColumnCount(void) const
+int wxListCtrl::GetColumnCount() const
 {
     return m_mainWin->GetColumnCount();
 }
@@ -2585,13 +2664,13 @@ int wxListCtrl::GetItemSpacing( bool isSmall ) const
     return m_mainWin->GetItemSpacing( isSmall );
 }
 
-int wxListCtrl::GetSelectedItemCount(void) const
+int wxListCtrl::GetSelectedItemCount() const
 {
     return m_mainWin->GetSelectedItemCount();
 }
 
 /*
-wxColour wxListCtrl::GetTextColour(void) const
+wxColour wxListCtrl::GetTextColour() const
 {
 }
 
@@ -2600,7 +2679,7 @@ void wxListCtrl::SetTextColour(const wxColour& WXUNUSED(col))
 }
 */
 
-long wxListCtrl::GetTopItem(void) const
+long wxListCtrl::GetTopItem() const
 {
     return 0;
 }
@@ -2643,7 +2722,7 @@ bool wxListCtrl::DeleteItem( long item )
     return TRUE;
 }
 
-bool wxListCtrl::DeleteAllItems(void)
+bool wxListCtrl::DeleteAllItems()
 {
     m_mainWin->DeleteAllItems();
     return TRUE;
@@ -2668,11 +2747,10 @@ bool wxListCtrl::DeleteColumn( int col )
     return TRUE;
 }
 
-/*
-wxText& wxListCtrl::Edit( long WXUNUSED(item ) )
+void wxListCtrl::Edit( long item )
 {
+    m_mainWin->Edit( item );
 }
-*/
 
 bool wxListCtrl::EnsureVisible( long item )
 {