/////////////////////////////////////////////////////////////////////////////
-// Name: choice.cpp
+// Name: src/msw/choice.cpp
// Purpose: wxChoice
// Author: Julian Smart
// Modified by: Vadim Zeitlin to derive from wxChoiceBase
// 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"
return false;
// now create the real HWND
- if ( !MSWCreateControl(wxT("COMBOBOX"), _T(""), pos, size) )
+ if ( !MSWCreateControl(wxT("COMBOBOX"), wxEmptyString, pos, size) )
return false;
UpdateVisibleHeight();
}
+ InvalidateBestSize();
return n;
}
UpdateVisibleHeight();
}
+ InvalidateBestSize();
return n;
}
if ( !IsFrozen() )
UpdateVisibleHeight();
+
+ InvalidateBestSize();
}
void wxChoice::Clear()
if ( !IsFrozen() )
UpdateVisibleHeight();
+
+ InvalidateBestSize();
}
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);
}
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.
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;
}
#else // !Watcom
//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.size() == 0 )
+ if ( s.empty() )
{
- int count = GetCount();
- for ( int i = 0; i < count; i++ )
- {
- if ( GetString(i).size() == 0 )
- return i;
- }
-
- return wxNOT_FOUND;
+ 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;
+ int pos = (int)SendMessage(GetHwnd(), CB_FINDSTRINGEXACT,
+ (WPARAM)-1, (LPARAM)s.c_str());
+
+ return pos == LB_ERR ? wxNOT_FOUND : pos;
}
#endif // Watcom/!Watcom
}
DoSetItemClientData(n, data);
}
//else: it's already NULL by default
+
+ InvalidateBestSize();
}
wxString wxChoice::GetString(int n) const
const int hItem = SendMessage(GetHwnd(), CB_GETITEMHEIGHT, 0, 0);
height += hItem*(nItems + 1);
}
-
- // To work around a Windows bug (see "Bug in Windows Combobox" thread in Google Groups)
- // we have to reset the selection if it was accidentally selected in the size.
- DWORD oldSelStart = 0;
- DWORD oldSelEnd = 0;
- DWORD newSelStart = 0;
- DWORD newSelEnd = 0;
-
- ::SendMessage(GetHwnd(), CB_GETEDITSEL, (WPARAM) & oldSelStart, (LPARAM) & oldSelEnd);
-
- wxControl::DoSetSize(x, y, width, height, sizeFlags);
-
- ::SendMessage(GetHwnd(), CB_GETEDITSEL, (WPARAM) & newSelStart, (LPARAM) & newSelEnd);
-
- if (oldSelStart != newSelStart || oldSelEnd != newSelEnd)
+ else
{
- ::SendMessage(GetHwnd(), CB_SETEDITSEL, (WPARAM) 0, (LPARAM) MAKELPARAM(oldSelStart, oldSelEnd));
+ // 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);
+
// I'm commenting this out since the code appears to make choices
// and comboxes too high when they have associated sizers. I'm sure this
// is not the end of the story, which is why I'm leaving it #if'ed out for
// 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)
WXHWND hwnd;
UnpackCtlColor(wParam, lParam, &hdc, &hwnd);
- WXHBRUSH hbr = MSWControlColor((WXHDC)hdc);
+ WXHBRUSH hbr = MSWControlColor((WXHDC)hdc, hwnd);
if ( hbr )
return (WXLRESULT)hbr;
//else: fall through to default window proc
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;
- 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);
+ 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;
}
- return true;
+ return false;
}
-WXHBRUSH wxChoice::MSWControlColor(WXHDC hDC)
+WXHBRUSH wxChoice::MSWControlColor(WXHDC hDC, WXHWND hWnd)
{
if ( !IsEnabled() )
return MSWControlColorDisabled(hDC);
- return wxChoiceBase::MSWControlColorSolid(hDC);
+ return wxChoiceBase::MSWControlColor(hDC, hWnd);
}
#endif // wxUSE_CHOICE && !(__SMARTPHONE__ && __WXWINCE__)
-