X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/dc302518e6bf48326d202475bf78874fedcb2d9c..75e275af320f70d9bb57dd30494f6cc2c100dc25:/src/msw/choice.cpp diff --git a/src/msw/choice.cpp b/src/msw/choice.cpp index 6abcdaef9a..b643d1d1cb 100644 --- a/src/msw/choice.cpp +++ b/src/msw/choice.cpp @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: choice.cpp +// Name: src/msw/choice.cpp // Purpose: wxChoice // Author: Julian Smart // Modified by: Vadim Zeitlin to derive from wxChoiceBase @@ -17,10 +17,6 @@ // headers // ---------------------------------------------------------------------------- -#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) - #pragma implementation "choice.h" -#endif - // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" @@ -28,7 +24,7 @@ #pragma hdrstop #endif -#if wxUSE_CHOICE +#if wxUSE_CHOICE && !(defined(__SMARTPHONE__) && defined(__WXWINCE__)) #ifndef WX_PRECOMP #include "wx/choice.h" @@ -138,11 +134,11 @@ bool wxChoice::CreateAndInit(wxWindow *parent, { // initialize wxControl if ( !CreateControl(parent, id, pos, size, style, validator, name) ) - return FALSE; + return false; // now create the real HWND - if ( !MSWCreateControl(wxT("COMBOBOX"), _T(""), pos, size) ) - return FALSE; + if ( !MSWCreateControl(wxT("COMBOBOX"), wxEmptyString, pos, size) ) + return false; // choice/combobox normally has "white" (depends on colour scheme, of @@ -158,7 +154,7 @@ bool wxChoice::CreateAndInit(wxWindow *parent, // and now we may finally size the control properly (if needed) SetBestSize(size); - return TRUE; + return true; } bool wxChoice::Create(wxWindow *parent, @@ -232,9 +228,11 @@ int wxChoice::DoAppend(const wxString& item) { // we need to refresh our size in order to have enough space for the // newly added items - UpdateVisibleHeight(); + if ( !IsFrozen() ) + UpdateVisibleHeight(); } + InvalidateBestSize(); return n; } @@ -250,9 +248,11 @@ int wxChoice::DoInsert(const wxString& item, int pos) } else // ok { - UpdateVisibleHeight(); + if ( !IsFrozen() ) + UpdateVisibleHeight(); } + InvalidateBestSize(); return n; } @@ -267,7 +267,10 @@ void wxChoice::Delete(int n) SendMessage(GetHwnd(), CB_DELETESTRING, n, 0); - UpdateVisibleHeight(); + if ( !IsFrozen() ) + UpdateVisibleHeight(); + + InvalidateBestSize(); } void wxChoice::Clear() @@ -276,7 +279,10 @@ void wxChoice::Clear() SendMessage(GetHwnd(), CB_RESETCONTENT, 0, 0); - UpdateVisibleHeight(); + if ( !IsFrozen() ) + UpdateVisibleHeight(); + + InvalidateBestSize(); } void wxChoice::Free() @@ -296,6 +302,17 @@ void wxChoice::Free() // ---------------------------------------------------------------------------- int wxChoice::GetSelection() const +{ + // if m_lastAcceptedSelection is set, it means that the dropdown is + // currently shown and that we want to use the last "permanent" selection + // instead of whatever is under the mouse pointer currently + // + // otherwise, get the selection from the control + return m_lastAcceptedSelection == wxID_NONE ? GetCurrentSelection() + : m_lastAcceptedSelection; +} + +int wxChoice::GetCurrentSelection() const { return (int)SendMessage(GetHwnd(), CB_GETCURSEL, 0, 0); } @@ -314,7 +331,7 @@ int wxChoice::GetCount() const return (int)SendMessage(GetHwnd(), CB_GETCOUNT, 0, 0); } -int wxChoice::FindString(const wxString& s) const +int wxChoice::FindString(const wxString& s, bool bCase) const { #if defined(__WATCOMC__) && defined(__WIN386__) // For some reason, Watcom in WIN386 mode crashes in the CB_FINDSTRINGEXACT message. @@ -323,16 +340,37 @@ int wxChoice::FindString(const wxString& s) const for ( int i = 0; i < count; i++ ) { // as CB_FINDSTRINGEXACT is case insensitive, be case insensitive too - if ( GetString(i).IsSameAs(s, FALSE) ) + if ( GetString(i).IsSameAs(s, bCase) ) return i; } return wxNOT_FOUND; #else // !Watcom - int pos = (int)SendMessage(GetHwnd(), CB_FINDSTRINGEXACT, - (WPARAM)-1, (LPARAM)s.c_str()); - - return pos == LB_ERR ? wxNOT_FOUND : pos; + //TODO: Evidently some MSW versions (all?) don't like empty strings + //passed to SendMessage, so we have to do it ourselves in that case + if ( s.empty() ) + { + int count = GetCount(); + for ( int i = 0; i < count; i++ ) + { + if ( GetString(i).empty() ) + return i; + } + + return wxNOT_FOUND; + } + else if (bCase) + { + // back to base class search for not native search type + return wxItemContainerImmutable::FindString( s, bCase ); + } + else + { + int pos = (int)SendMessage(GetHwnd(), CB_FINDSTRINGEXACT, + (WPARAM)-1, (LPARAM)s.c_str()); + + return pos == LB_ERR ? wxNOT_FOUND : pos; + } #endif // Watcom/!Watcom } @@ -363,6 +401,8 @@ void wxChoice::SetString(int n, const wxString& s) DoSetItemClientData(n, data); } //else: it's already NULL by default + + InvalidateBestSize(); } wxString wxChoice::GetString(int n) const @@ -431,7 +471,7 @@ wxClientData* wxChoice::DoGetItemClientObject( int n ) const void wxChoice::UpdateVisibleHeight() { // be careful to not change the width here - DoSetSize(-1, -1, -1, GetSize().y, wxSIZE_USE_EXISTING); + DoSetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, GetSize().y, wxSIZE_USE_EXISTING); } void wxChoice::DoMoveWindow(int x, int y, int width, int height) @@ -458,7 +498,7 @@ void wxChoice::DoGetSize(int *w, int *h) const // total height of the control including the drop down list -- but only // sometimes, and normally it isn't... I have no idea about what to do with // this - wxControl::DoGetSize(w, h); + wxControl::DoGetSize(w, h); } void wxChoice::DoSetSize(int x, int y, @@ -470,20 +510,37 @@ void wxChoice::DoSetSize(int x, int y, // the height which we must pass to Windows should be the total height of // the control including the drop down list while the height given to us // is, of course, just the height of the permanently visible part of it - if ( height != -1 ) + if ( height != wxDefaultCoord ) { // don't make the drop down list too tall, arbitrarily limit it to 40 // items max and also don't leave it empty size_t nItems = GetCount(); if ( !nItems ) nItems = 9; - else if ( nItems > 39 ) - nItems = 39; + else if ( nItems > 24 ) + nItems = 24; // add space for the drop down list const int hItem = SendMessage(GetHwnd(), CB_GETITEMHEIGHT, 0, 0); height += hItem*(nItems + 1); } + else + { + // We cannot pass wxDefaultCoord as height to wxControl. wxControl uses + // wxGetWindowRect() to determine the current height of the combobox, + // and then again sets the combobox's height to that value. Unfortunately, + // wxGetWindowRect doesn't include the dropdown list's height (at least + // on Win2K), so this would result in a combobox with dropdown height of + // 1 pixel. We have to determine the default height ourselves and call + // wxControl with that value instead. + int w, h; + RECT r; + DoGetSize(&w, &h); + if (::SendMessage(GetHwnd(), CB_GETDROPPEDCONTROLRECT, 0, (LPARAM) &r) != 0) + { + height = h + r.bottom - r.top; + } + } wxControl::DoSetSize(x, y, width, height, sizeFlags); @@ -495,7 +552,7 @@ void wxChoice::DoSetSize(int x, int y, // if the height specified for the visible part of the control is // different from the current one, we need to change it separately // as it is not affected by normal WM_SETSIZE - if ( height != -1 ) + if ( height != wxDefaultCoord ) { const int delta = heightOrig - GetSize().y; if ( delta ) @@ -530,23 +587,45 @@ wxSize wxChoice::DoGetBestSize() const // the combobox should be slightly larger than the widest string wChoice += 5*GetCharWidth(); - return wxSize(wChoice, EDIT_HEIGHT_FROM_CHAR_HEIGHT(GetCharHeight())); + wxSize best(wChoice, EDIT_HEIGHT_FROM_CHAR_HEIGHT(GetCharHeight())); + CacheBestSize(best); + return best; } WXLRESULT wxChoice::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) { - if ( nMsg == WM_LBUTTONUP ) + switch ( nMsg ) { - int x = (int)LOWORD(lParam); - int y = (int)HIWORD(lParam); - - // Ok, this is truly weird, but if a panel with a wxChoice loses the - // focus, then you get a *fake* WM_LBUTTONUP message with x = 65535 and - // y = 65535. Filter out this nonsense. - // - // VZ: I'd like to know how to reproduce this please... - if ( x == 65535 && y == 65535 ) - return 0; + case WM_LBUTTONUP: + { + int x = (int)LOWORD(lParam); + int y = (int)HIWORD(lParam); + + // Ok, this is truly weird, but if a panel with a wxChoice + // loses the focus, then you get a *fake* WM_LBUTTONUP message + // with x = 65535 and y = 65535. Filter out this nonsense. + // + // VZ: I'd like to know how to reproduce this please... + if ( x == 65535 && y == 65535 ) + return 0; + } + break; + + // we have to handle both: one for the normal case and the other + // for readonly + case WM_CTLCOLOREDIT: + case WM_CTLCOLORLISTBOX: + case WM_CTLCOLORSTATIC: + { + WXHDC hdc; + WXHWND hwnd; + UnpackCtlColor(wParam, lParam, &hdc, &hwnd); + + WXHBRUSH hbr = MSWControlColor((WXHDC)hdc, hwnd); + if ( hbr ) + return (WXLRESULT)hbr; + //else: fall through to default window proc + } } return wxWindow::MSWWindowProc(nMsg, wParam, lParam); @@ -554,47 +633,50 @@ WXLRESULT wxChoice::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) bool wxChoice::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) { - if ( param != CBN_SELCHANGE) + switch ( param ) { - // "selection changed" is the only event we're after - return FALSE; + case CBN_DROPDOWN: + // we don't want to track selection using CB_GETCURSEL while the + // dropdown is opened + m_lastAcceptedSelection = GetCurrentSelection(); + break; + + case CBN_CLOSEUP: + // it should be safe to use CB_GETCURSEL again + m_lastAcceptedSelection = wxID_NONE; + break; + + case CBN_SELCHANGE: + { + const int n = GetSelection(); + + wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId); + event.SetInt(n); + event.SetEventObject(this); + + if ( n > -1 ) + { + event.SetString(GetStringSelection()); + if ( HasClientObjectData() ) + event.SetClientObject( GetClientObject(n) ); + else if ( HasClientUntypedData() ) + event.SetClientData( GetClientData(n) ); + } + + ProcessCommand(event); + } + return true; } - int n = GetSelection(); - if (n > -1) - { - wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId); - event.SetInt(n); - event.SetEventObject(this); - event.SetString(GetStringSelection()); - if ( HasClientObjectData() ) - event.SetClientObject( GetClientObject(n) ); - else if ( HasClientUntypedData() ) - event.SetClientData( GetClientData(n) ); - ProcessCommand(event); - } - - return TRUE; + return false; } -WXHBRUSH wxChoice::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor), - WXUINT WXUNUSED(message), - WXWPARAM WXUNUSED(wParam), - WXLPARAM WXUNUSED(lParam) - ) +WXHBRUSH wxChoice::MSWControlColor(WXHDC hDC, WXHWND hWnd) { - HDC hdc = (HDC)pDC; - wxColour colBack = GetBackgroundColour(); - - if (!IsEnabled()) - colBack = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE); - - ::SetBkColor(hdc, wxColourToRGB(colBack)); - ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour())); - - wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID); + if ( !IsEnabled() ) + return MSWControlColorDisabled(hDC); - return (WXHBRUSH)brush->GetResourceHandle(); + return wxChoiceBase::MSWControlColor(hDC, hWnd); } -#endif // wxUSE_CHOICE +#endif // wxUSE_CHOICE && !(__SMARTPHONE__ && __WXWINCE__)