use new style creation (MSWCreateControl() and MSWGetStyle()); adjust the drop down list height after appending/inserting/deleting items

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@25578 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2004-02-07 19:26:22 +00:00
parent 7b28e0ede4
commit 71e57cd650
4 changed files with 119 additions and 60 deletions

View File

@@ -101,9 +101,24 @@ protected:
int width, int height, int width, int height,
int sizeFlags = wxSIZE_AUTO); int sizeFlags = wxSIZE_AUTO);
virtual WXDWORD MSWGetStyle(long style, WXDWORD *exstyle) const;
// get the real height of the control // get the real height of the control
int GetVisibleHeight() const; int GetVisibleHeight() const;
// update the height of the drop down list to fit the number of items we
// have (without changing the visible height)
void UpdateVisibleHeight();
// create and initialize the control
bool CreateAndInit(wxWindow *parent, wxWindowID id,
const wxPoint& pos,
const wxSize& size,
int n, const wxString choices[],
long style,
const wxValidator& validator,
const wxString& name);
// free all memory we have (used by Clear() and dtor) // free all memory we have (used by Clear() and dtor)
void Free(); void Free();

View File

@@ -100,6 +100,9 @@ public:
WXHWND GetEditHWND() const; WXHWND GetEditHWND() const;
protected:
virtual WXDWORD MSWGetStyle(long style, WXDWORD *exstyle) const;
private: private:
DECLARE_DYNAMIC_CLASS_NO_COPY(wxComboBox) DECLARE_DYNAMIC_CLASS_NO_COPY(wxComboBox)
}; };

View File

@@ -115,17 +115,6 @@ bool wxChoice::Create(wxWindow *parent,
const wxValidator& validator, const wxValidator& validator,
const wxString& name) const wxString& name)
{ {
if ( !CreateControl(parent, id, pos, size, style, validator, name) )
return FALSE;
long msStyle = WS_CHILD | CBS_DROPDOWNLIST | WS_TABSTOP | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL;
if ( style & wxCB_SORT )
msStyle |= CBS_SORT;
if ( style & wxCLIP_SIBLINGS )
msStyle |= WS_CLIPSIBLINGS;
// Experience shows that wxChoice vs. wxComboBox distinction confuses // Experience shows that wxChoice vs. wxComboBox distinction confuses
// quite a few people - try to help them // quite a few people - try to help them
wxASSERT_MSG( !(style & wxCB_DROPDOWN) && wxASSERT_MSG( !(style & wxCB_DROPDOWN) &&
@@ -134,20 +123,37 @@ bool wxChoice::Create(wxWindow *parent,
_T("this style flag is ignored by wxChoice, you ") _T("this style flag is ignored by wxChoice, you ")
_T("probably want to use a wxComboBox") ); _T("probably want to use a wxComboBox") );
if ( !MSWCreateControl(wxT("COMBOBOX"), msStyle) ) return CreateAndInit(parent, id, pos, size, n, choices, style,
validator, name);
}
bool wxChoice::CreateAndInit(wxWindow *parent, wxWindowID id,
const wxPoint& pos,
const wxSize& size,
int n, const wxString choices[],
long style,
const wxValidator& validator,
const wxString& name)
{
// initialize wxControl
if ( !CreateControl(parent, id, pos, size, style, validator, name) )
return FALSE; return FALSE;
// A choice/combobox normally has a white background (or other, depending // now create the real HWND
// on global settings) rather than inheriting the parent's background colour. if ( !MSWCreateControl(wxT("COMBOBOX"), _T(""), pos, size) )
return FALSE;
// choice/combobox normally has "white" (depends on colour scheme, of
// course) background rather than inheriting the parent's background
SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
// initialize
for ( int i = 0; i < n; i++ ) for ( int i = 0; i < n; i++ )
{ {
Append(choices[i]); Append(choices[i]);
} }
SetSize(pos.x, pos.y, size.x, size.y);
return TRUE; return TRUE;
} }
@@ -165,6 +171,26 @@ bool wxChoice::Create(wxWindow *parent,
style, validator, name); style, validator, name);
} }
WXDWORD wxChoice::MSWGetStyle(long style, WXDWORD *exstyle) const
{
// we never have an external border
WXDWORD msStyle = wxControl::MSWGetStyle
(
(style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle
);
// WS_CLIPSIBLINGS is useful with wxChoice and doesn't seem to result in
// any problems
msStyle |= WS_CLIPSIBLINGS;
// wxChoice-specific styles
msStyle |= CBS_DROPDOWNLIST | WS_HSCROLL | WS_VSCROLL;
if ( style & wxCB_SORT )
msStyle |= CBS_SORT;
return msStyle;
}
wxChoice::~wxChoice() wxChoice::~wxChoice()
{ {
Free(); Free();
@@ -181,6 +207,12 @@ int wxChoice::DoAppend(const wxString& item)
{ {
wxLogLastError(wxT("SendMessage(CB_ADDSTRING)")); wxLogLastError(wxT("SendMessage(CB_ADDSTRING)"));
} }
else // ok
{
// we need to refresh our size in order to have enough space for the
// newly added items
UpdateVisibleHeight();
}
return n; return n;
} }
@@ -195,6 +227,10 @@ int wxChoice::DoInsert(const wxString& item, int pos)
{ {
wxLogLastError(wxT("SendMessage(CB_INSERTSTRING)")); wxLogLastError(wxT("SendMessage(CB_INSERTSTRING)"));
} }
else // ok
{
UpdateVisibleHeight();
}
return n; return n;
} }
@@ -209,6 +245,8 @@ void wxChoice::Delete(int n)
} }
SendMessage(GetHwnd(), CB_DELETESTRING, n, 0); SendMessage(GetHwnd(), CB_DELETESTRING, n, 0);
UpdateVisibleHeight();
} }
void wxChoice::Clear() void wxChoice::Clear()
@@ -216,6 +254,8 @@ void wxChoice::Clear()
Free(); Free();
SendMessage(GetHwnd(), CB_RESETCONTENT, 0, 0); SendMessage(GetHwnd(), CB_RESETCONTENT, 0, 0);
UpdateVisibleHeight();
} }
void wxChoice::Free() void wxChoice::Free()
@@ -372,6 +412,11 @@ int wxChoice::GetVisibleHeight() const
return ::SendMessage(GetHwnd(), CB_GETITEMHEIGHT, (WPARAM)-1, 0); return ::SendMessage(GetHwnd(), CB_GETITEMHEIGHT, (WPARAM)-1, 0);
} }
void wxChoice::UpdateVisibleHeight()
{
DoSetSize(-1, -1, -1, GetVisibleHeight());
}
void wxChoice::DoMoveWindow(int x, int y, int width, int height) void wxChoice::DoMoveWindow(int x, int y, int width, int height)
{ {
// here is why this is necessary: if the width is negative, the combobox // here is why this is necessary: if the width is negative, the combobox

View File

@@ -355,6 +355,10 @@ WXHWND wxComboBox::GetEditHWND() const
return (WXHWND)hwndEdit; return (WXHWND)hwndEdit;
} }
// ----------------------------------------------------------------------------
// wxComboBox creation
// ----------------------------------------------------------------------------
bool wxComboBox::Create(wxWindow *parent, wxWindowID id, bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
const wxString& value, const wxString& value,
const wxPoint& pos, const wxPoint& pos,
@@ -369,50 +373,11 @@ bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
// some noticeable flicker while the control rearranges itself // some noticeable flicker while the control rearranges itself
m_isShown = FALSE; m_isShown = FALSE;
// first create wxWin object if ( !CreateAndInit(parent, id, pos, size, n, choices, style,
if ( !CreateControl(parent, id, pos, size, style, validator, name) ) validator, name) )
return FALSE; return FALSE;
// get the right style SetValue(value);
long msStyle = WS_TABSTOP | WS_VSCROLL | WS_HSCROLL |
CBS_AUTOHSCROLL | CBS_NOINTEGRALHEIGHT /* | WS_CLIPSIBLINGS */;
if ( style & wxCB_READONLY )
msStyle |= CBS_DROPDOWNLIST;
#ifndef __WXWINCE__
else if ( style & wxCB_SIMPLE )
msStyle |= CBS_SIMPLE; // A list (shown always) and edit control
#endif
else
msStyle |= CBS_DROPDOWN;
if ( style & wxCB_SORT )
msStyle |= CBS_SORT;
if ( style & wxCLIP_SIBLINGS )
msStyle |= WS_CLIPSIBLINGS;
// and now create the MSW control
if ( !MSWCreateControl(_T("COMBOBOX"), msStyle) )
return FALSE;
// A choice/combobox normally has a white background (or other, depending
// on global settings) rather than inheriting the parent's background colour.
SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
for ( int i = 0; i < n; i++ )
{
Append(choices[i]);
}
if ( !value.IsEmpty() )
{
SetValue(value);
}
// do this after appending the values to the combobox so that autosizing
// works correctly
SetSize(pos.x, pos.y, size.x, size.y);
// a (not read only) combobox is, in fact, 2 controls: the combobox itself // a (not read only) combobox is, in fact, 2 controls: the combobox itself
// and an edit control inside it and if we want to catch events from this // and an edit control inside it and if we want to catch events from this
@@ -447,6 +412,38 @@ bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
chs.GetStrings(), style, validator, name); chs.GetStrings(), style, validator, name);
} }
WXDWORD wxComboBox::MSWGetStyle(long style, WXDWORD *exstyle) const
{
// we never have an external border
WXDWORD msStyle = wxChoice::MSWGetStyle
(
(style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle
);
// remove the style always added by wxChoice
msStyle &= ~CBS_DROPDOWNLIST;
if ( style & wxCB_READONLY )
msStyle |= CBS_DROPDOWNLIST;
#ifndef __WXWINCE__
else if ( style & wxCB_SIMPLE )
msStyle |= CBS_SIMPLE; // A list (shown always) and edit control
#endif
else
msStyle |= CBS_DROPDOWN;
// there is no reason to not always use CBS_AUTOHSCROLL, so do use it
msStyle |= CBS_AUTOHSCROLL;
// NB: we used to also add CBS_NOINTEGRALHEIGHT here but why?
return msStyle;
}
// ----------------------------------------------------------------------------
// wxComboBox text control-like methods
// ----------------------------------------------------------------------------
void wxComboBox::SetValue(const wxString& value) void wxComboBox::SetValue(const wxString& value)
{ {
if ( HasFlag(wxCB_READONLY) ) if ( HasFlag(wxCB_READONLY) )
@@ -574,6 +571,5 @@ void wxComboBox::SetSelection(long from, long to)
} }
} }
#endif #endif // wxUSE_COMBOBOX
// wxUSE_COMBOBOX