]> git.saurik.com Git - wxWidgets.git/blobdiff - src/msw/choice.cpp
compilation fix for wxUSE_VALIDATORS == 0
[wxWidgets.git] / src / msw / choice.cpp
index a73387523fdee1c8abb9853cc94c76d697dd3a0a..4d1df2c1a42f6313348740c7a820ca0d5ffd33ea 100644 (file)
 #include "wx/msw/private.h"
 
 #if wxUSE_EXTENDED_RTTI
-IMPLEMENT_DYNAMIC_CLASS_XTI(wxChoice, wxControl,"wx/checkbox.h")
-
-WX_BEGIN_PROPERTIES_TABLE(wxChoice)
-    // TODO DELEGATES
-       WX_PROPERTY( Font , wxFont , SetFont , GetFont  , )
-    WX_PROPERTY_COLLECTION( Choices , wxArrayString , wxString , AppendString , GetStrings )
-       WX_PROPERTY( Selection ,int, SetSelection, GetSelection, )
-WX_END_PROPERTIES_TABLE()
-
-WX_BEGIN_HANDLERS_TABLE(wxChoice)
-WX_END_HANDLERS_TABLE()
-
-WX_CONSTRUCTOR_4( wxChoice , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size ) 
+WX_DEFINE_FLAGS( wxChoiceStyle )
+
+wxBEGIN_FLAGS( wxChoiceStyle )
+    // new style border flags, we put them first to
+    // use them for streaming out
+    wxFLAGS_MEMBER(wxBORDER_SIMPLE)
+    wxFLAGS_MEMBER(wxBORDER_SUNKEN)
+    wxFLAGS_MEMBER(wxBORDER_DOUBLE)
+    wxFLAGS_MEMBER(wxBORDER_RAISED)
+    wxFLAGS_MEMBER(wxBORDER_STATIC)
+    wxFLAGS_MEMBER(wxBORDER_NONE)
+    
+    // old style border flags
+    wxFLAGS_MEMBER(wxSIMPLE_BORDER)
+    wxFLAGS_MEMBER(wxSUNKEN_BORDER)
+    wxFLAGS_MEMBER(wxDOUBLE_BORDER)
+    wxFLAGS_MEMBER(wxRAISED_BORDER)
+    wxFLAGS_MEMBER(wxSTATIC_BORDER)
+    wxFLAGS_MEMBER(wxBORDER)
+
+    // standard window styles
+    wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
+    wxFLAGS_MEMBER(wxCLIP_CHILDREN)
+    wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
+    wxFLAGS_MEMBER(wxWANTS_CHARS)
+    wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
+    wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
+    wxFLAGS_MEMBER(wxVSCROLL)
+    wxFLAGS_MEMBER(wxHSCROLL)
+
+wxEND_FLAGS( wxChoiceStyle )
+
+IMPLEMENT_DYNAMIC_CLASS_XTI(wxChoice, wxControl,"wx/choice.h")
+
+wxBEGIN_PROPERTIES_TABLE(wxChoice)
+       wxEVENT_PROPERTY( Select , wxEVT_COMMAND_CHOICE_SELECTED , wxCommandEvent )
+
+    wxPROPERTY( Font , wxFont , SetFont , GetFont  , , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
+    wxPROPERTY_COLLECTION( Choices , wxArrayString , wxString , AppendString , GetStrings , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
+       wxPROPERTY( Selection ,int, SetSelection, GetSelection, , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
+    wxPROPERTY_FLAGS( WindowStyle , wxChoiceStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
+wxEND_PROPERTIES_TABLE()
+
+wxBEGIN_HANDLERS_TABLE(wxChoice)
+wxEND_HANDLERS_TABLE()
+
+wxCONSTRUCTOR_4( wxChoice , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size ) 
 #else
 IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
 #endif
@@ -84,7 +118,7 @@ bool wxChoice::Create(wxWindow *parent,
     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 /* | WS_CLIPSIBLINGS */;
+    long msStyle = WS_CHILD | CBS_DROPDOWNLIST | WS_TABSTOP | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL;
     if ( style & wxCB_SORT )
         msStyle |= CBS_SORT;
 
@@ -117,6 +151,20 @@ bool wxChoice::Create(wxWindow *parent,
     return TRUE;
 }
 
+bool wxChoice::Create(wxWindow *parent,
+                      wxWindowID id,
+                      const wxPoint& pos,
+                      const wxSize& size,
+                      const wxArrayString& choices,
+                      long style,
+                      const wxValidator& validator,
+                      const wxString& name)
+{
+    wxCArrayString chs(choices);
+    return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
+                  style, validator, name);
+}
+
 wxChoice::~wxChoice()
 {
     Free();
@@ -319,6 +367,11 @@ wxClientData* wxChoice::DoGetItemClientObject( int n ) const
 // wxMSW specific helpers
 // ----------------------------------------------------------------------------
 
+int wxChoice::GetVisibleHeight() const
+{
+    return ::SendMessage(GetHwnd(), CB_GETITEMHEIGHT, (WPARAM)-1, 0);
+}
+
 void wxChoice::DoMoveWindow(int x, int y, int width, int height)
 {
     // here is why this is necessary: if the width is negative, the combobox
@@ -337,29 +390,65 @@ void wxChoice::DoMoveWindow(int x, int y, int width, int height)
     wxControl::DoMoveWindow(x, y, width, height);
 }
 
+void wxChoice::DoGetSize(int *w, int *h) const
+{
+    wxControl::DoGetSize(w, h);
+
+    // we need to return only the height of the visible part, not the entire
+    // height in the Windows sense which includes the height of the drop down
+    // list as well
+    if ( h )
+        *h = GetVisibleHeight();
+}
+
 void wxChoice::DoSetSize(int x, int y,
-                         int width, int WXUNUSED(height),
+                         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.
+    int heightOrig = height;
+
+    // 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 )
+    {
+        // 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;
+
+        // add space for the drop down list
+        const int hItem = SendMessage(GetHwnd(), CB_GETITEMHEIGHT, 0, 0);
+        height += hItem*(nItems + 1);
+    }
 
-    wxControl::DoSetSize(x, y, width, -1, sizeFlags);
+    wxControl::DoSetSize(x, y, width, height, sizeFlags);
+
+    // 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 )
+    {
+        const int hVisibleCurrent = GetVisibleHeight();
+        if ( hVisibleCurrent != heightOrig )
+        {
+            SendMessage(GetHwnd(), CB_SETITEMHEIGHT, (WPARAM)-1, heightOrig);
+        }
+    }
 }
 
 wxSize wxChoice::DoGetBestSize() const
 {
     // find the widest string
-    int wLine;
     int wChoice = 0;
-    int nItems = GetCount();
-    for ( int i = 0; i < nItems; i++ )
+    const size_t nItems = GetCount();
+    for ( size_t i = 0; i < nItems; i++ )
     {
-        wxString str(GetString(i));
-        GetTextExtent(str, &wLine, NULL);
+        int wLine;
+        GetTextExtent(GetString(i), &wLine, NULL);
         if ( wLine > wChoice )
             wChoice = wLine;
     }
@@ -369,17 +458,10 @@ wxSize wxChoice::DoGetBestSize() const
     if ( wChoice == 0 )
         wChoice = 100;
 
-    // the combobox should be larger than the widest string
-    int cx, cy;
-    wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
-
-    wChoice += 5*cx;
-
-    // Choice drop-down list depends on number of items (limited to 10)
-    size_t nStrings = nItems == 0 ? 10 : wxMin(10, nItems) + 1;
-    int hChoice = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*nStrings;
+    // the combobox should be slightly larger than the widest string
+    wChoice += 5*GetCharWidth();
 
-    return wxSize(wChoice, hChoice);
+    return wxSize(wChoice, GetVisibleHeight());
 }
 
 long wxChoice::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)