X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/debe6624c1e9d4bf3243381153d1e173c849bcd8..fc8525d7b0364f358789122d1171e0fcf868a2bf:/src/msw/choice.cpp diff --git a/src/msw/choice.cpp b/src/msw/choice.cpp index bc0ab64e18..9859f91a06 100644 --- a/src/msw/choice.cpp +++ b/src/msw/choice.cpp @@ -6,7 +6,7 @@ // Created: 04/01/98 // RCS-ID: $Id$ // Copyright: (c) Julian Smart and Markus Holzem -// Licence: wxWindows license +// Licence: wxWindows license ///////////////////////////////////////////////////////////////////////////// #ifdef __GNUG__ @@ -22,6 +22,8 @@ #ifndef WX_PRECOMP #include "wx/choice.h" +#include "wx/utils.h" +#include "wx/log.h" #endif #include "wx/msw/private.h" @@ -34,38 +36,39 @@ bool wxChoice::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) { if (param == CBN_SELCHANGE) { - wxCommandEvent event(wxEVENT_TYPE_CHOICE_COMMAND, m_windowId); + wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId); event.SetInt(GetSelection()); event.SetEventObject(this); - event.SetString(copystring(GetStringSelection())); + event.SetString(GetStringSelection()); ProcessCommand(event); - delete[] event.GetString(); + return TRUE; } - else return FALSE; + else + return FALSE; } bool wxChoice::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, - int n, const wxString choices[], - long style, + int n, const wxString choices[], + long style, const wxValidator& validator, const wxString& name) { SetName(name); SetValidator(validator); if (parent) parent->AddChild(this); - SetBackgroundColour(parent->GetDefaultBackgroundColour()) ; - SetForegroundColour(parent->GetDefaultForegroundColour()) ; - no_strings = n; + SetBackgroundColour(parent->GetBackgroundColour()) ; + SetForegroundColour(parent->GetForegroundColour()) ; + m_noStrings = 0; m_windowStyle = style; if ( id == -1 ) - m_windowId = (int)NewControlId(); + m_windowId = (int)NewControlId(); else - m_windowId = id; + m_windowId = id; int x = pos.x; int y = pos.y; @@ -82,16 +85,18 @@ bool wxChoice::Create(wxWindow *parent, wxWindowID id, // Even with extended styles, need to combine with WS_BORDER // for them to look right. - if (want3D || (m_windowStyle & wxSIMPLE_BORDER) || (m_windowStyle & wxRAISED_BORDER) || - (m_windowStyle & wxSUNKEN_BORDER) || (m_windowStyle & wxDOUBLE_BORDER)) + if ( want3D || wxStyleHasBorder(m_windowStyle) ) msStyle |= WS_BORDER; - HWND wx_combo = CreateWindowEx(exStyle, "COMBOBOX", NULL, + m_hWnd = (WXHWND)::CreateWindowEx(exStyle, _T("COMBOBOX"), NULL, msStyle, 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId, wxGetInstance(), NULL); + + wxCHECK_MSG( m_hWnd, FALSE, _T("Failed to create combobox") ); + /* -#if CTL3D +#if wxUSE_CTL3D if (want3D) { m_useCtl3D = TRUE; @@ -100,17 +105,17 @@ bool wxChoice::Create(wxWindow *parent, wxWindowID id, #endif */ - m_hWnd = (WXHWND) wx_combo; - // Subclass again for purposes of dialog editing mode - SubclassWin((WXHWND) wx_combo); + SubclassWin(m_hWnd); - SetFont(* parent->GetFont()); + SetFont(parent->GetFont()); int i; for (i = 0; i < n; i++) - SendMessage(wx_combo, CB_INSERTSTRING, i, (LONG)(const char *)choices[i]); - SendMessage(wx_combo, CB_SETCURSEL, i, 0); + { + Append(choices[i]); + } + SetSelection(n); SetSize(x, y, width, height); @@ -119,32 +124,32 @@ bool wxChoice::Create(wxWindow *parent, wxWindowID id, void wxChoice::Append(const wxString& item) { - SendMessage((HWND) GetHWND(), CB_ADDSTRING, 0, (LONG)(const char *)item); + SendMessage(GetHwnd(), CB_ADDSTRING, 0, (LONG)(const wxChar *)item); - no_strings ++; + m_noStrings ++; } void wxChoice::Delete(int n) { - no_strings = (int)SendMessage((HWND) GetHWND(), CB_DELETESTRING, n, 0); + m_noStrings = (int)SendMessage(GetHwnd(), CB_DELETESTRING, n, 0); } void wxChoice::Clear(void) { - SendMessage((HWND) GetHWND(), CB_RESETCONTENT, 0, 0); + SendMessage(GetHwnd(), CB_RESETCONTENT, 0, 0); - no_strings = 0; + m_noStrings = 0; } int wxChoice::GetSelection(void) const { - return (int)SendMessage((HWND) GetHWND(), CB_GETCURSEL, 0, 0); + return (int)SendMessage(GetHwnd(), CB_GETCURSEL, 0, 0); } void wxChoice::SetSelection(int n) { - SendMessage((HWND) GetHWND(), CB_SETCURSEL, n, 0); + SendMessage(GetHwnd(), CB_SETCURSEL, n, 0); } int wxChoice::FindString(const wxString& s) const @@ -155,14 +160,14 @@ int wxChoice::FindString(const wxString& s) const char buf[512]; for (int i = 0; i < Number(); i++) { - int len = (int)SendMessage((HWND) GetHWND(), CB_GETLBTEXT, i, (LPARAM)(LPSTR)buf); + int len = (int)SendMessage(GetHwnd(), CB_GETLBTEXT, i, (LPARAM)(LPSTR)buf); buf[len] = 0; if (strcmp(buf, (const char *)s) == 0) return i; } return -1; #else - int pos = (int)SendMessage((HWND) GetHWND(), CB_FINDSTRINGEXACT, (WPARAM)-1, (LPARAM)(LPSTR)(const char *)s); + int pos = (int)SendMessage(GetHwnd(), CB_FINDSTRINGEXACT, (WPARAM)-1, (LPARAM)(LPSTR)(const wxChar *)s); if (pos == LB_ERR) return -1; else @@ -172,97 +177,66 @@ int wxChoice::FindString(const wxString& s) const wxString wxChoice::GetString(int n) const { - int len = (int)SendMessage((HWND) GetHWND(), CB_GETLBTEXT, n, (long)wxBuffer); - wxBuffer[len] = 0; - return wxString(wxBuffer); -} - -void wxChoice::SetSize(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; - - // If we're prepared to use the existing size, then... - if (width == -1 && height == -1 && ((sizeFlags & wxSIZE_AUTO) != wxSIZE_AUTO)) - { - GetSize(&w1, &h1); - } + size_t len = (size_t)::SendMessage(GetHwnd(), CB_GETLBTEXTLEN, n, 0); + wxString str; + if ( ::SendMessage(GetHwnd(), CB_GETLBTEXT, n, + (LPARAM)str.GetWriteBuf(len)) == CB_ERR ) + { + wxLogLastError("SendMessage(CB_GETLBTEXT)"); + } - int cx; // button font dimensions - int cy; - wxGetCharSize(GetHWND(), &cx, &cy, GetFont()); + str.UngetWriteBuf(); - float control_width, control_height; + return str; +} - // Ignore height parameter because height doesn't - // mean 'initially displayed' height, it refers to the - // drop-down menu as well. The wxWindows interpretation - // is different; also, getting the size returns the - // _displayed_ size (NOT the drop down menu size) - // so setting-getting-setting size would not work. - h1 = -1; +void wxChoice::DoSetSize(int x, int y, + int width, int height, + int sizeFlags) +{ + // Ignore height parameter because height doesn't mean 'initially + // displayed' height, it refers to the drop-down menu as well. The + // wxWindows interpretation is different; also, getting the size returns + // the _displayed_ size (NOT the drop down menu size) so + // setting-getting-setting size would not work. + wxControl::DoSetSize(x, y, width, -1, sizeFlags); +} - // Deal with default size (using -1 values) - if (width <= 0) - { - // Find the longest string - if (no_strings == 0) - control_width = (float)100.0; - else +wxSize wxChoice::DoGetBestSize() +{ + // find the widest string + int wLine; + int wChoice = 0; + for ( int i = 0; i < m_noStrings; i++ ) { - int len, ht; - float longest = (float)0.0; - int i; - for (i = 0; i < no_strings; i++) - { wxString str(GetString(i)); - GetTextExtent(str, &len, &ht, NULL, NULL,GetFont()); - if ( len > longest) longest = len; - } - - control_width = (float)(int)(longest + cx*5); + GetTextExtent(str, &wLine, NULL); + if ( wLine > wChoice ) + wChoice = wLine; } - } - - // Choice drop-down list depends on number of items (limited to 10) - if (h1 <= 0) - { - if (no_strings == 0) - h1 = (int)(EDIT_CONTROL_FACTOR*cy*10.0); - else h1 = (int)(EDIT_CONTROL_FACTOR*cy*(wxMin(10, no_strings) + 1)); - } - // If non-default width... - if (width >= 0) - control_width = (float)width; + // give it some reasonable default value if there are no strings in the + // list + if ( wChoice == 0 ) + wChoice = 100; - control_height = (float)h1; + // the combobox should be larger than the widest string + int cx, cy; + wxGetCharSize(GetHWND(), &cx, &cy, &GetFont()); - // Calculations may have made text size too small - if (control_height <= 0) - control_height = (float)(int)(cy*EDIT_CONTROL_FACTOR) ; + wChoice += 5*cx; - if (control_width <= 0) - control_width = (float)100.0; + // Choice drop-down list depends on number of items (limited to 10) + size_t nStrings = m_noStrings == 0 ? 10 : wxMin(10, m_noStrings) + 1; + int hChoice = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*nStrings; - MoveWindow((HWND) GetHWND(), x1, y1, - (int)control_width, (int)control_height, TRUE); + return wxSize(wChoice, hChoice); } WXHBRUSH wxChoice::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor, - WXUINT message, WXWPARAM wParam, WXLPARAM lParam) + WXUINT message, WXWPARAM wParam, WXLPARAM lParam) { - return 0; + return 0; } long wxChoice::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) @@ -301,7 +275,7 @@ long wxChoice::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) // with x = 65535 and y = 65535. // Filter out this nonsense. if (x == 65535 && y == 65535) - return Default(); + return 0; break; } } @@ -315,7 +289,7 @@ wxString wxChoice::GetStringSelection (void) const if (sel > -1) return wxString(this->GetString (sel)); else - return wxString(""); + return wxString(_T("")); } bool wxChoice::SetStringSelection (const wxString& s)