⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 choice.cpp

📁 A*算法 A*算法 A*算法 A*算法A*算法A*算法
💻 CPP
📖 第 1 页 / 共 2 页
字号:
   }
   else
   {
     int pos = (int)SendMessage(GetHwnd(), CB_FINDSTRINGEXACT,
                                (WPARAM)-1, (LPARAM)s.c_str());

     return pos == LB_ERR ? wxNOT_FOUND : pos;
   }
#endif // Watcom/!Watcom
}

void wxChoice::SetString(int n, const wxString& s)
{
    wxCHECK_RET( n >= 0 && n < GetCount(),
                 wxT("invalid item index in wxChoice::SetString") );

    // we have to delete and add back the string as there is no way to change a
    // string in place

    // we need to preserve the client data
    void *data;
    if ( m_clientDataItemsType != wxClientData_None )
    {
        data = DoGetItemClientData(n);
    }
    else // no client data
    {
        data = NULL;
    }

    ::SendMessage(GetHwnd(), CB_DELETESTRING, n, 0);
    ::SendMessage(GetHwnd(), CB_INSERTSTRING, n, (LPARAM)s.c_str() );

    if ( data )
    {
        DoSetItemClientData(n, data);
    }
    //else: it's already NULL by default

    InvalidateBestSize();
}

wxString wxChoice::GetString(int n) const
{
    int len = (int)::SendMessage(GetHwnd(), CB_GETLBTEXTLEN, n, 0);

    wxString str;
    if ( len != CB_ERR && len > 0 )
    {
        if ( ::SendMessage
               (
                GetHwnd(),
                CB_GETLBTEXT,
                n,
                (LPARAM)(wxChar *)wxStringBuffer(str, len)
               ) == CB_ERR )
        {
            wxLogLastError(wxT("SendMessage(CB_GETLBTEXT)"));
        }
    }

    return str;
}

// ----------------------------------------------------------------------------
// client data
// ----------------------------------------------------------------------------

void wxChoice::DoSetItemClientData( int n, void* clientData )
{
    if ( ::SendMessage(GetHwnd(), CB_SETITEMDATA,
                       n, (LPARAM)clientData) == CB_ERR )
    {
        wxLogLastError(wxT("CB_SETITEMDATA"));
    }
}

void* wxChoice::DoGetItemClientData( int n ) const
{
    LPARAM rc = SendMessage(GetHwnd(), CB_GETITEMDATA, n, 0);
    if ( rc == CB_ERR )
    {
        wxLogLastError(wxT("CB_GETITEMDATA"));

        // unfortunately, there is no way to return an error code to the user
        rc = (LPARAM) NULL;
    }

    return (void *)rc;
}

void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
{
    DoSetItemClientData(n, clientData);
}

wxClientData* wxChoice::DoGetItemClientObject( int n ) const
{
    return (wxClientData *)DoGetItemClientData(n);
}

// ----------------------------------------------------------------------------
// wxMSW specific helpers
// ----------------------------------------------------------------------------

void wxChoice::UpdateVisibleHeight()
{
    // be careful to not change the width here
    DoSetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, GetSize().y, wxSIZE_USE_EXISTING);
}

void wxChoice::DoMoveWindow(int x, int y, int width, int height)
{
    // here is why this is necessary: if the width is negative, the combobox
    // window proc makes the window of the size width*height instead of
    // interpreting height in the usual manner (meaning the height of the drop
    // down list - usually the height specified in the call to MoveWindow()
    // will not change the height of combo box per se)
    //
    // this behaviour is not documented anywhere, but this is just how it is
    // here (NT 4.4) and, anyhow, the check shouldn't hurt - however without
    // the check, constraints/sizers using combos may break the height
    // constraint will have not at all the same value as expected
    if ( width < 0 )
        return;

    wxControl::DoMoveWindow(x, y, width, height);
}

void wxChoice::DoGetSize(int *w, int *h) const
{
    // this is weird: sometimes, the height returned by Windows is clearly the
    // total height of the control including the drop down list -- but only
    // sometimes, and normally it isn't... I have no idea about what to do with
    // this
    wxControl::DoGetSize(w, h);
}

void wxChoice::DoSetSize(int x, int y,
                         int width, int height,
                         int sizeFlags)
{
    // 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 != wxDefaultCoord )
    {
        // 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 > 24 )
            nItems = 24;

        // add space for the drop down list
        const int hItem = SendMessage(GetHwnd(), CB_GETITEMHEIGHT, 0, 0);
        height += hItem*(nItems + 1);
    }
    else
    {
        // 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);

    // This solution works on XP, but causes choice/combobox lists to be
    // too short on W2K and earlier.
#if 0
    int widthCurrent, heightCurrent;
    DoGetSize(&widthCurrent, &heightCurrent);

    // 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 != wxDefaultCoord && height != heightCurrent )
    {
        // 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 > 24 )
            nItems = 24;

        // add space for the drop down list
        const int hItem = SendMessage(GetHwnd(), CB_GETITEMHEIGHT, 0, 0);
        height += hItem*(nItems + 1);
    }
    else // keep the same height as now
    {
        // normally wxWindow::DoSetSize() checks if we set the same size as the
        // window already has and does nothing in this case, but for us the
        // check fails as the size we pass to it includes the dropdown while
        // the size returned by our GetSize() does not, so test if the size
        // didn't really change ourselves here
        if ( width == wxDefaultCoord || width == widthCurrent )
        {
            // size doesn't change, what about position?
            int xCurrent, yCurrent;
            DoGetPosition(&xCurrent, &yCurrent);
            const bool defMeansUnchanged = !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE);
            if ( ((x == wxDefaultCoord && defMeansUnchanged) || x == xCurrent)
                    &&
                 ((y == wxDefaultCoord && defMeansUnchanged) || y == yCurrent) )
            {
                // nothing changes, nothing to do
                return;
            }
        }

        // 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.
        //
        // Also notice that sometimes CB_GETDROPPEDCONTROLRECT seems to return
        // wildly incorrect values (~32000) which looks like a bug in it, just
        // ignore them in this case
        RECT r;
        if ( ::SendMessage(GetHwnd(), CB_GETDROPPEDCONTROLRECT, 0, (LPARAM) &r)
                    && r.bottom < 30000 )
        {
            height = heightCurrent + r.bottom - r.top;
        }
    }

    wxControl::DoSetSize(x, y, width, height, sizeFlags);
#endif
}

wxSize wxChoice::DoGetBestSize() const
{
    // find the widest string
    int wChoice = 0;
    const size_t nItems = GetCount();
    for ( size_t i = 0; i < nItems; i++ )
    {
        int wLine;
        GetTextExtent(GetString(i), &wLine, NULL);
        if ( wLine > wChoice )
            wChoice = wLine;
    }

    // give it some reasonable default value if there are no strings in the
    // list
    if ( wChoice == 0 )
        wChoice = 100;

    // the combobox should be slightly larger than the widest string
    wChoice += 5*GetCharWidth();

    wxSize best(wChoice, EDIT_HEIGHT_FROM_CHAR_HEIGHT(GetCharHeight()));
    CacheBestSize(best);
    return best;
}

WXLRESULT wxChoice::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
{
    switch ( nMsg )
    {
        case WM_LBUTTONUP:
            {
                int x = (int)LOWORD(lParam);
                int y = (int)HIWORD(lParam);

                // Ok, this is truly weird, but if a panel with a wxChoice
                // loses the focus, then you get a *fake* WM_LBUTTONUP message
                // with x = 65535 and y = 65535. Filter out this nonsense.
                //
                // VZ: I'd like to know how to reproduce this please...
                if ( x == 65535 && y == 65535 )
                    return 0;
            }
            break;

            // we have to handle both: one for the normal case and the other
            // for readonly
        case WM_CTLCOLOREDIT:
        case WM_CTLCOLORLISTBOX:
        case WM_CTLCOLORSTATIC:
            {
                WXHDC hdc;
                WXHWND hwnd;
                UnpackCtlColor(wParam, lParam, &hdc, &hwnd);

                WXHBRUSH hbr = MSWControlColor((WXHDC)hdc, hwnd);
                if ( hbr )
                    return (WXLRESULT)hbr;
                //else: fall through to default window proc
            }
    }

    return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
}

bool wxChoice::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
{
    switch ( param )
    {
        case CBN_DROPDOWN:
            // we don't want to track selection using CB_GETCURSEL while the
            // dropdown is opened
            m_lastAcceptedSelection = GetCurrentSelection();
            break;

        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 false;
}

WXHBRUSH wxChoice::MSWControlColor(WXHDC hDC, WXHWND hWnd)
{
    if ( !IsEnabled() )
        return MSWControlColorDisabled(hDC);

    return wxChoiceBase::MSWControlColor(hDC, hWnd);
}

#endif // wxUSE_CHOICE && !(__SMARTPHONE__ && __WXWINCE__)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -