X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/a23fd0e1d1329a4a258b4defb3e0b0001b700c6e..e8ac7bf221aec78b2a420660db711eed2e771670:/src/msw/listbox.cpp?ds=sidebyside diff --git a/src/msw/listbox.cpp b/src/msw/listbox.cpp index 4ca6a3955b..02d55e900d 100644 --- a/src/msw/listbox.cpp +++ b/src/msw/listbox.cpp @@ -20,35 +20,67 @@ #pragma hdrstop #endif +#include "wx/window.h" +#include "wx/msw/private.h" + #ifndef WX_PRECOMP - #include "wx/listbox.h" - #include "wx/settings.h" - #include "wx/brush.h" - #include "wx/font.h" - #include "wx/dc.h" +#include "wx/listbox.h" +#include "wx/settings.h" +#include "wx/brush.h" +#include "wx/font.h" +#include "wx/dc.h" +#include "wx/utils.h" #endif -#include "wx/msw/private.h" - -#include #include -#ifndef __TWIN32__ - #ifdef __GNUWIN32__ - #include - #endif +#ifdef __WXWINE__ + #if defined(GetWindowStyle) + #undef GetWindowStyle + #endif #endif -#ifdef GetCharWidth - #undef GetCharWidth -#endif +#include "wx/dynarray.h" +#include "wx/log.h" #if wxUSE_OWNER_DRAWN #include "wx/ownerdrw.h" #endif -#include "wx/dynarray.h" -#include "wx/log.h" +#ifndef __TWIN32__ + #if defined(__GNUWIN32__) + #ifndef wxUSE_NORLANDER_HEADERS + #include + #endif + #endif +#endif + +#ifdef __WXWINE__ + #ifndef ListBox_SetItemData + #define ListBox_SetItemData(hwndCtl, index, data) \ + ((int)(DWORD)SendMessage((hwndCtl), LB_SETITEMDATA, (WPARAM)(int)(index), (LPARAM)(data))) + #endif + #ifndef ListBox_GetHorizontalExtent + #define ListBox_GetHorizontalExtent(hwndCtl) \ + ((int)(DWORD)SendMessage((hwndCtl), LB_GETHORIZONTALEXTENT, 0L, 0L)) + #endif + #ifndef ListBox_GetSelCount + #define ListBox_GetSelCount(hwndCtl) \ + ((int)(DWORD)SendMessage((hwndCtl), LB_GETSELCOUNT, 0L, 0L)) + #endif + #ifndef ListBox_GetSelItems + #define ListBox_GetSelItems(hwndCtl, cItems, lpItems) \ + ((int)(DWORD)SendMessage((hwndCtl), LB_GETSELITEMS, (WPARAM)(int)(cItems), (LPARAM)(int *)(lpItems))) + #endif + #ifndef ListBox_GetTextLen + #define ListBox_GetTextLen(hwndCtl, index) \ + ((int)(DWORD)SendMessage((hwndCtl), LB_GETTEXTLEN, (WPARAM)(int)(index), 0L)) + #endif + #ifndef ListBox_GetText + #define ListBox_GetText(hwndCtl, index, lpszBuffer) \ + ((int)(DWORD)SendMessage((hwndCtl), LB_GETTEXT, (WPARAM)(int)(index), (LPARAM)(LPCTSTR)(lpszBuffer))) + #endif +#endif #if !USE_SHARED_LIBRARY IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl) @@ -101,7 +133,7 @@ bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) event.m_commandInt = aSelections[0] ; event.m_clientData = GetClientData(event.m_commandInt); wxString str(GetString(event.m_commandInt)); - if (str != "") + if (str != _T("")) { event.m_commandString = str; } @@ -165,7 +197,8 @@ bool wxListBox::Create(wxWindow *parent, int height = size.y; m_windowStyle = style; - DWORD wstyle = WS_VSCROLL | WS_TABSTOP | LBS_NOTIFY | LBS_HASSTRINGS; + DWORD wstyle = WS_VISIBLE | WS_VSCROLL | WS_TABSTOP | + LBS_NOTIFY | LBS_HASSTRINGS; if (m_windowStyle & wxLB_MULTIPLE) wstyle |= LBS_MULTIPLESEL; else if (m_windowStyle & wxLB_EXTENDED) @@ -199,13 +232,13 @@ bool wxListBox::Create(wxWindow *parent, wstyle |= WS_BORDER; } - m_hWnd = (WXHWND)::CreateWindowEx(exStyle, "LISTBOX", NULL, + m_hWnd = (WXHWND)::CreateWindowEx(exStyle, _T("LISTBOX"), NULL, wstyle | WS_CHILD, 0, 0, 0, 0, (HWND)parent->GetHWND(), (HMENU)m_windowId, wxGetInstance(), NULL); - wxCHECK_MSG( m_hWnd, FALSE, "Failed to create listbox" ); + wxCHECK_MSG( m_hWnd, FALSE, _T("Failed to create listbox") ); #if wxUSE_CTL3D if (want3D) @@ -254,7 +287,7 @@ void wxListBox::SetupColours() void wxListBox::SetFirstItem(int N) { wxCHECK_RET( N >= 0 && N < m_noItems, - "invalid index in wxListBox::SetFirstItem" ); + _T("invalid index in wxListBox::SetFirstItem") ); SendMessage(GetHwnd(),LB_SETTOPINDEX,(WPARAM)N,(LPARAM)0) ; } @@ -270,7 +303,7 @@ void wxListBox::SetFirstItem(const wxString& s) void wxListBox::Delete(int N) { wxCHECK_RET( N >= 0 && N < m_noItems, - "invalid index in wxListBox::Delete" ); + _T("invalid index in wxListBox::Delete") ); SendMessage(GetHwnd(), LB_DELETESTRING, N, 0); m_noItems--; @@ -295,7 +328,7 @@ void wxListBox::Append(const wxString& item) SetHorizontalExtent(item); } -void wxListBox::Append(const wxString& item, char *Client_data) +void wxListBox::Append(const wxString& item, void *Client_data) { int index = ListBox_AddString(GetHwnd(), item); m_noItems ++; @@ -304,7 +337,7 @@ void wxListBox::Append(const wxString& item, char *Client_data) if ( m_windowStyle & wxLB_OWNERDRAW ) { // client data must be pointer to wxOwnerDrawn, otherwise we would crash // in OnMeasure/OnDraw. - wxFAIL_MSG("Can't use client data with owner-drawn listboxes"); + wxFAIL_MSG(_T("Can't use client data with owner-drawn listboxes")); } else #endif @@ -314,7 +347,7 @@ void wxListBox::Append(const wxString& item, char *Client_data) SetHorizontalExtent(item); } -void wxListBox::Set(int n, const wxString *choices, char** clientData) +void wxListBox::Set(int n, const wxString *choices, void** clientData) { ShowWindow(GetHwnd(), SW_HIDE); ListBox_ResetContent(GetHwnd()); @@ -344,7 +377,7 @@ void wxListBox::Set(int n, const wxString *choices, char** clientData) ListBox_SetItemData(GetHwnd(), ui, pNewItem); wxASSERT_MSG(clientData[ui] == NULL, - "Can't use client data with owner-drawn listboxes"); + _T("Can't use client data with owner-drawn listboxes")); } } #endif @@ -382,7 +415,7 @@ void wxListBox::Clear() void wxListBox::SetSelection(int N, bool select) { wxCHECK_RET( N >= 0 && N < m_noItems, - "invalid index in wxListBox::SetSelection" ); + _T("invalid index in wxListBox::SetSelection") ); if ((m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED)) SendMessage(GetHwnd(), LB_SETSEL, select, N); @@ -398,7 +431,7 @@ void wxListBox::SetSelection(int N, bool select) bool wxListBox::Selected(int N) const { wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE, - "invalid index in wxListBox::Selected" ); + _T("invalid index in wxListBox::Selected") ); return SendMessage(GetHwnd(), LB_GETSEL, N, 0) == 0 ? FALSE : TRUE; } @@ -406,27 +439,27 @@ bool wxListBox::Selected(int N) const void wxListBox::Deselect(int N) { wxCHECK_RET( N >= 0 && N < m_noItems, - "invalid index in wxListBox::Deselect" ); + _T("invalid index in wxListBox::Deselect") ); if ((m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED)) SendMessage(GetHwnd(), LB_SETSEL, FALSE, N); } -char *wxListBox::GetClientData(int N) const +void *wxListBox::GetClientData(int N) const { wxCHECK_MSG( N >= 0 && N < m_noItems, NULL, - "invalid index in wxListBox::GetClientData" ); + _T("invalid index in wxListBox::GetClientData") ); - return (char *)SendMessage(GetHwnd(), LB_GETITEMDATA, N, 0); + return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA, N, 0); } -void wxListBox::SetClientData(int N, char *Client_data) +void wxListBox::SetClientData(int N, void *Client_data) { wxCHECK_RET( N >= 0 && N < m_noItems, - "invalid index in wxListBox::SetClientData" ); + _T("invalid index in wxListBox::SetClientData") ); if ( ListBox_SetItemData(GetHwnd(), N, Client_data) == LB_ERR ) - wxLogDebug("LB_SETITEMDATA failed"); + wxLogDebug(_T("LB_SETITEMDATA failed")); } // Return number of selections and an array of selected integers @@ -440,7 +473,7 @@ int wxListBox::GetSelections(wxArrayInt& aSelections) const if (no_sel != 0) { int *selections = new int[no_sel]; if ( ListBox_GetSelItems(GetHwnd(), no_sel, selections) == LB_ERR ) { - wxFAIL_MSG("This listbox can't have single-selection style!"); + wxFAIL_MSG(_T("This listbox can't have single-selection style!")); } aSelections.Alloc(no_sel); @@ -466,8 +499,8 @@ int wxListBox::GetSelection() const wxCHECK_MSG( !(m_windowStyle & wxLB_MULTIPLE) && !(m_windowStyle & wxLB_EXTENDED), -1, - "GetSelection() can't be used with multiple-selection " - "listboxes, use GetSelections() instead." ); + _T("GetSelection() can't be used with multiple-selection " + "listboxes, use GetSelections() instead.") ); return ListBox_GetCurSel(GetHwnd()); } @@ -476,7 +509,7 @@ int wxListBox::GetSelection() const wxString wxListBox::GetString(int N) const { wxCHECK_MSG( N >= 0 && N < m_noItems, "", - "invalid index in wxListBox::GetClientData" ); + _T("invalid index in wxListBox::GetClientData") ); int len = ListBox_GetTextLen(GetHwnd(), N); @@ -488,65 +521,8 @@ wxString wxListBox::GetString(int N) const return result; } -void wxListBox::DoSetSize(int x, int y, int width, int height, int sizeFlags) -{ - int currentX, currentY; - GetPosition(¤tX, ¤tY); - - int x1 = x; - int y1 = y; - int w1 = width; - int h1 = height; - - if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) - x1 = currentX; - if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) - y1 = currentY; - - AdjustForParentClientOrigin(x1, y1, sizeFlags); - - // If we're prepared to use the existing size, then... - if (width == -1 && height == -1 && ((sizeFlags & wxSIZE_AUTO) != wxSIZE_AUTO)) - { - GetSize(&w1, &h1); - } - - int cx; // button font dimensions - int cy; - - wxGetCharSize(GetHWND(), &cx, &cy, & this->GetFont()); - - float control_width, control_height, control_x, control_y; - - // Deal with default size (using -1 values) - if (w1<=0) - w1 = DEFAULT_ITEM_WIDTH; - - if (h1<=0) - h1 = DEFAULT_ITEM_HEIGHT; - - control_x = (float)x1; - control_y = (float)y1; - control_width = (float)w1; - control_height = (float)h1; - - // Calculations may have made size too small - if (control_height <= 0) - control_height = (float)DEFAULT_ITEM_HEIGHT; - - if (control_width <= 0) - control_width = (float)DEFAULT_ITEM_WIDTH; - - MoveWindow(GetHwnd(), - (int)control_x, (int)control_y, - (int)control_width, (int)control_height, - TRUE); - -} - -// Windows-specific code to set the horizontal extent of -// the listbox, if necessary. If s is non-NULL, it's -// used to calculate the horizontal extent. +// Windows-specific code to set the horizontal extent of the listbox, if +// necessary. If s is non-NULL, it's used to calculate the horizontal extent. // Otherwise, all strings are used. void wxListBox::SetHorizontalExtent(const wxString& s) { @@ -555,7 +531,7 @@ void wxListBox::SetHorizontalExtent(const wxString& s) return; TEXTMETRIC lpTextMetric; - if (s != "") + if (s != _T("")) { int existingExtent = (int)SendMessage(GetHwnd(), LB_GETHORIZONTALEXTENT, 0, 0L); HDC dc = GetWindowDC(GetHwnd()); @@ -565,7 +541,7 @@ void wxListBox::SetHorizontalExtent(const wxString& s) GetTextMetrics(dc, &lpTextMetric); SIZE extentXY; - ::GetTextExtentPoint(dc, (LPSTR) (const char *)s, s.Length(), &extentXY); + ::GetTextExtentPoint(dc, (LPTSTR) (const wxChar *)s, s.Length(), &extentXY); int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth); if (oldFont) @@ -591,7 +567,7 @@ void wxListBox::SetHorizontalExtent(const wxString& s) int len = (int)SendMessage(GetHwnd(), LB_GETTEXT, i, (LONG)wxBuffer); wxBuffer[len] = 0; SIZE extentXY; - ::GetTextExtentPoint(dc, (LPSTR)wxBuffer, len, &extentXY); + ::GetTextExtentPoint(dc, (LPTSTR)wxBuffer, len, &extentXY); int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth); if (extentX > largestExtent) largestExtent = extentX; @@ -608,26 +584,26 @@ void wxListBox::InsertItems(int nItems, const wxString items[], int pos) { wxCHECK_RET( pos >= 0 && pos <= m_noItems, - "invalid index in wxListBox::InsertItems" ); + _T("invalid index in wxListBox::InsertItems") ); int i; for (i = 0; i < nItems; i++) ListBox_InsertString(GetHwnd(), i + pos, items[i]); m_noItems += nItems; - SetHorizontalExtent(""); + SetHorizontalExtent(_T("")); } void wxListBox::SetString(int N, const wxString& s) { wxCHECK_RET( N >= 0 && N < m_noItems, - "invalid index in wxListBox::SetString" ); + _T("invalid index in wxListBox::SetString") ); int sel = -1; if (!(m_windowStyle & wxLB_MULTIPLE) && !(m_windowStyle & wxLB_EXTENDED)) sel = GetSelection(); - char *oldData = (char *)wxListBox::GetClientData(N); + void *oldData = wxListBox::GetClientData(N); SendMessage(GetHwnd(), LB_DELETESTRING, N, 0); @@ -635,7 +611,7 @@ void wxListBox::SetString(int N, const wxString& s) if (N == (m_noItems - 1)) newN = -1; - SendMessage(GetHwnd(), LB_INSERTSTRING, newN, (LPARAM) (const char *)s); + SendMessage(GetHwnd(), LB_INSERTSTRING, newN, (LPARAM) (const wxChar *)s); if (oldData) wxListBox::SetClientData(N, oldData); @@ -677,6 +653,35 @@ bool wxListBox::SetStringSelection (const wxString& s, bool flag) return FALSE; } +wxSize wxListBox::DoGetBestSize() +{ + // find the widest string + int wLine; + int wListbox = 0; + for ( int i = 0; i < m_noItems; i++ ) + { + wxString str(GetString(i)); + GetTextExtent(str, &wLine, NULL); + if ( wLine > wListbox ) + wListbox = wLine; + } + + // give it some reasonable default value if there are no strings in the + // list + if ( wListbox == 0 ) + wListbox = 100; + + // the listbox should be slightly larger than the widest string + int cx, cy; + wxGetCharSize(GetHWND(), &cx, &cy, &GetFont()); + + wListbox += 3*cx; + + int hListbox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*(wxMax(m_noItems, 7)); + + return wxSize(wListbox, hListbox); +} + // Is this the right thing? Won't setselection generate a command // event too? No! It'll just generate a setselection event. // But we still can't have this being called whenever a real command @@ -749,7 +754,7 @@ bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item) MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item; wxDC dc; - dc.SetHDC((WXHDC)CreateIC("DISPLAY", NULL, NULL, 0)); + dc.SetHDC((WXHDC)CreateIC(_T("DISPLAY"), NULL, NULL, 0)); dc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_ANSI_VAR_FONT)); pStruct->itemHeight = dc.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE;