]> git.saurik.com Git - wxWidgets.git/blobdiff - src/msw/listctrl.cpp
Unicode build fix
[wxWidgets.git] / src / msw / listctrl.cpp
index 8336fc4af7d0c6c728dcfc313414be964475960b..a8316267c2b64b42c9f02736a773dfbbfe4276cd 100644 (file)
 // include <commctrl.h> "properly"
 #include "wx/msw/wrapcctl.h"
 
+// Currently gcc and watcom don't define NMLVFINDITEM, and DMC only defines
+// it by its old name NM_FINDTIEM.
+//
+#if defined(__VISUALC__) || defined(__BORLANDC__) || defined(NMLVFINDITEM)
+    #define HAVE_NMLVFINDITEM 1
+#elif defined(__DMC__) || defined(NM_FINDITEM)
+    #define HAVE_NMLVFINDITEM 1
+    #define NMLVFINDITEM NM_FINDITEM
+#endif
+
 // ----------------------------------------------------------------------------
 // private functions
 // ----------------------------------------------------------------------------
@@ -121,7 +131,7 @@ public:
 
         // memcpy() can't work if the struct sizes are different
         wxCOMPILE_TIME_ASSERT( sizeof(LV_ITEM_OTHER) == sizeof(LV_ITEM_NATIVE),
-                                CodeCantWorkIfDiffSizes);
+                               CodeCantWorkIfDiffSizes);
 
         memcpy(&m_item, &item, sizeof(LV_ITEM_NATIVE));
 
@@ -166,7 +176,7 @@ private:
 //
 // Solution:
 // Under MSW the only way to associate data with a List
-// item independant of its position in the list is to
+// item independent of its position in the list is to
 // store a pointer to it in its lParam attribute. However
 // user programs are already using this (via the
 // SetItemData() GetItemData() calls).
@@ -350,19 +360,19 @@ bool wxListCtrl::Create(wxWindow *parent,
     if ( !CreateControl(parent, id, pos, size, style, validator, name) )
         return false;
 
-    if ( !MSWCreateControl(WC_LISTVIEW, _T(""), pos, size) )
+    if ( !MSWCreateControl(WC_LISTVIEW, wxEmptyString, pos, size) )
         return false;
 
     // explicitly say that we want to use Unicode because otherwise we get ANSI
     // versions of _some_ messages (notably LVN_GETDISPINFOA) in MSLU build
     wxSetCCUnicodeFormat(GetHwnd());
 
-    // for comctl32.dll v 4.70+ we want to have this attribute because it's
-    // prettier (and also because wxGTK does it like this)
-    if ( InReportView() && wxTheApp->GetComCtl32Version() >= 470 )
+    // for comctl32.dll v 4.70+ we want to have some non default extended
+    // styles because it's prettier (and also because wxGTK does it like this)
+    if ( InReportView() && wxApp::GetComCtl32Version() >= 470 )
     {
         ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE,
-                      0, LVS_EX_FULLROWSELECT);
+                      0, LVS_EX_LABELTIP | LVS_EX_FULLROWSELECT);
     }
 
     return true;
@@ -428,7 +438,7 @@ WXDWORD wxListCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const
 #if !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
     if ( style & wxLC_VIRTUAL )
     {
-        int ver = wxTheApp->GetComCtl32Version();
+        int ver = wxApp::GetComCtl32Version();
         if ( ver < 470 )
         {
             wxLogWarning(_("Please install a newer version of comctl32.dll\n(at least version 4.70 is required but you have %d.%02d)\nor this program won't operate correctly."),
@@ -495,9 +505,12 @@ wxListCtrl::~wxListCtrl()
         m_textCtrl = NULL;
     }
 
-    if (m_ownsImageListNormal) delete m_imageListNormal;
-    if (m_ownsImageListSmall) delete m_imageListSmall;
-    if (m_ownsImageListState) delete m_imageListState;
+    if (m_ownsImageListNormal)
+        delete m_imageListNormal;
+    if (m_ownsImageListSmall)
+        delete m_imageListSmall;
+    if (m_ownsImageListState)
+        delete m_imageListState;
 }
 
 // ----------------------------------------------------------------------------
@@ -668,17 +681,15 @@ int wxListCtrl::GetColumnWidth(int col) const
 // Sets the column width
 bool wxListCtrl::SetColumnWidth(int col, int width)
 {
-    int col2 = col;
     if ( m_windowStyle & wxLC_LIST )
-        col2 = -1;
+        col = 0;
 
-    int width2 = width;
-    if ( width2 == wxLIST_AUTOSIZE)
-        width2 = LVSCW_AUTOSIZE;
-    else if ( width2 == wxLIST_AUTOSIZE_USEHEADER)
-        width2 = LVSCW_AUTOSIZE_USEHEADER;
+    if ( width == wxLIST_AUTOSIZE)
+        width = LVSCW_AUTOSIZE;
+    else if ( width == wxLIST_AUTOSIZE_USEHEADER)
+        width = LVSCW_AUTOSIZE_USEHEADER;
 
-    return ListView_SetColumnWidth(GetHwnd(), col2, width2) != 0;
+    return ListView_SetColumnWidth(GetHwnd(), col, width) != 0;
 }
 
 // Gets the number of items that can fit vertically in the
@@ -725,8 +736,7 @@ bool wxListCtrl::GetItem(wxListItem& info) const
     if ( info.m_mask & wxLIST_MASK_STATE )
     {
         lvItem.mask |= LVIF_STATE;
-        // the other bits are hardly interesting anyhow
-        lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
+        wxConvertToMSWFlags(0, info.m_stateMask, lvItem);
     }
 
     bool success = ListView_GetItem((HWND)GetHWND(), &lvItem) != 0;
@@ -1083,6 +1093,24 @@ wxColour wxListCtrl::GetItemBackgroundColour( long item ) const
     return col;
 }
 
+void wxListCtrl::SetItemFont( long item, const wxFont &f )
+{
+    wxListItem info;
+    info.m_itemId = item;
+    info.SetFont( f );
+    SetItem( info );
+}
+
+wxFont wxListCtrl::GetItemFont( long item ) const
+{
+    wxFont f;
+    wxListItemInternalData *data = wxGetInternalData(this, item);
+    if ( data && data->attr )
+        f = data->attr->GetFont();
+
+    return f;
+}
+
 // Gets the number of selected items in the list control
 int wxListCtrl::GetSelectedItemCount() const
 {
@@ -1230,7 +1258,7 @@ bool wxListCtrl::DeleteItem(long item)
         return false;
     }
 
-    m_count -= 1;
+    m_count--;
     wxASSERT_MSG( m_count == ListView_GetItemCount(GetHwnd()),
                   wxT("m_count should match ListView_GetItemCount"));
 
@@ -1816,7 +1844,8 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
         // ignored for efficiency.  It is done here because the internal data is in the
         // process of being deleted so we don't want to try and access it below.
         if ( m_ignoreChangeMessages &&
-             ( (nmLV->hdr.code == LVN_ITEMCHANGED) || (nmLV->hdr.code == LVN_ITEMCHANGING)))
+             ( (nmLV->hdr.code == LVN_ITEMCHANGED) ||
+               (nmLV->hdr.code == LVN_ITEMCHANGING)) )
         {
             return true;
         }
@@ -1833,7 +1862,7 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
                 event.m_item.m_data = internaldata->lParam;
         }
 
-
+        bool processed = true;
         switch ( nmhdr->code )
         {
             case LVN_BEGINRDRAG:
@@ -1917,16 +1946,17 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
                 break;
 
             case LVN_DELETEALLITEMS:
-                m_count = 0;
                 eventType = wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS;
                 event.m_itemIndex = -1;
                 break;
 
             case LVN_DELETEITEM:
-                if (m_count == 0)
-                    // this should be prevented by the post-processing code below,
-                    // but "just in case"
+                if ( m_count == 0 )
+                {
+                    // this should be prevented by the post-processing code
+                    // below, but "just in case"
                     return false;
+                }
 
                 eventType = wxEVT_COMMAND_LIST_DELETE_ITEM;
                 event.m_itemIndex = iItem;
@@ -2131,6 +2161,78 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
                 }
                 break;
 
+#ifdef HAVE_NMLVFINDITEM
+            case LVN_ODFINDITEM:
+                // this message is only used with the virtual list control but
+                // even there we don't want to always use it: in a control with
+                // sufficiently big number of items (defined as > 1000 here),
+                // accidentally pressing a key could result in hanging an
+                // application waiting while it performs linear search
+                if ( IsVirtual() && GetItemCount() <= 1000 )
+                {
+                    NMLVFINDITEM* pFindInfo = (NMLVFINDITEM*)lParam;
+
+                    // no match by default
+                    *result = -1;
+
+                    // we only handle string-based searches here
+                    //
+                    // TODO: what about LVFI_PARTIAL, should we handle this?
+                    if ( !(pFindInfo->lvfi.flags & LVFI_STRING) )
+                    {
+                        return false;
+                    }
+
+                    const wxChar * const searchstr = pFindInfo->lvfi.psz;
+                    const size_t len = wxStrlen(searchstr);
+
+                    // this is the first item we should examine, search from it
+                    // wrapping if necessary
+                    const int startPos = pFindInfo->iStart;
+                    const int maxPos = GetItemCount();
+                    wxCHECK_MSG( startPos <= maxPos, false,
+                                 _T("bad starting position in LVN_ODFINDITEM") );
+
+                    int currentPos = startPos;
+                    do
+                    {
+                        // wrap to the beginning if necessary
+                        if ( currentPos == maxPos )
+                        {
+                            // somewhat surprizingly, LVFI_WRAP isn't set in
+                            // flags but we still should wrap
+                            currentPos = 0;
+                        }
+
+                        // does this item begin with searchstr?
+                        if ( wxStrnicmp(searchstr,
+                                            GetItemText(currentPos), len) == 0 )
+                        {
+                            *result = currentPos;
+                            break;
+                        }
+                    }
+                    while ( ++currentPos != startPos );
+
+                    if ( *result == -1 )
+                    {
+                        // not found
+                        return false;
+                    }
+
+                    SetItemState(*result,
+                                 wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED,
+                                 wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED);
+                    EnsureVisible(*result);
+                    return true;
+                }
+                else
+                {
+                    processed = false;
+                }
+                break;
+#endif // HAVE_NMLVFINDITEM
+
             case LVN_GETDISPINFO:
                 if ( IsVirtual() )
                 {
@@ -2163,8 +2265,11 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
                 // fall through
 
             default:
-                return wxControl::MSWOnNotify(idCtrl, lParam, result);
+                processed = false;
         }
+
+        if ( !processed )
+            return wxControl::MSWOnNotify(idCtrl, lParam, result);
     }
     else
     {
@@ -2192,11 +2297,15 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
             // also, we may free all user data now (couldn't do it before as
             // the user should have access to it in OnDeleteAllItems() handler)
             FreeAllInternalData();
+
+            // the control is empty now, synchronize the cached number of items
+            // with the real one
+            m_count = 0;
             return true;
 
         case LVN_ENDLABELEDITA:
         case LVN_ENDLABELEDITW:
-            // logic here is inversed compared to all the other messages
+            // logic here is inverted compared to all the other messages
             *result = event.IsAllowed();
 
             // don't keep a stale wxTextCtrl around
@@ -2376,6 +2485,21 @@ void wxListCtrl::OnPaint(wxPaintEvent& event)
     }
 }
 
+WXLRESULT
+wxListCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
+{
+#ifdef WM_PRINT
+    if ( nMsg == WM_PRINT )
+    {
+        // we should bypass our own WM_PRINT handling as we don't handle
+        // PRF_CHILDREN flag, so leave it to the native control itself
+        return MSWDefWindowProc(nMsg, wParam, lParam);
+    }
+#endif // WM_PRINT
+
+    return wxControl::MSWWindowProc(nMsg, wParam, lParam);
+}
+
 // ----------------------------------------------------------------------------
 // virtual list controls
 // ----------------------------------------------------------------------------
@@ -2701,7 +2825,8 @@ static void wxConvertToMSWListCol(int WXUNUSED(col), const wxListItem& item,
             //
             // we don't use LVCFMT_COL_HAS_IMAGES because it doesn't seem to
             // make any difference in my tests -- but maybe we should?
-            lvCol.fmt |= LVCFMT_BITMAP_ON_RIGHT | LVCFMT_IMAGE;
+            if ( item.m_image != -1 )
+                lvCol.fmt |= LVCFMT_BITMAP_ON_RIGHT | LVCFMT_IMAGE;
 
             lvCol.iImage = item.m_image;
         }