From: Vadim Zeitlin Date: Sat, 7 Jun 2008 01:54:44 +0000 (+0000) Subject: support multiline labels in wxCheckBox (#9495) X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/533171c2873027b260f7107451ab22992d9e3926 support multiline labels in wxCheckBox (#9495) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@54008 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/docs/changes.txt b/docs/changes.txt index 3656438378..a16acebb9f 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -393,6 +393,7 @@ wxMSW: - Show resize gripper on resizeable dialogs (Kolya Kosenko) - Implement support for display enumeration under WinCE (Vince Harron) - Use different Win32 class names in different wx instances (Thomas Hauk) +- Support multiline labels for wxCheckBox. wxX11: diff --git a/include/wx/msw/checkbox.h b/include/wx/msw/checkbox.h index dfdcf42718..275fed537b 100644 --- a/include/wx/msw/checkbox.h +++ b/include/wx/msw/checkbox.h @@ -42,6 +42,8 @@ public: virtual bool GetValue() const; // override some base class virtuals + virtual void SetLabel(const wxString& label); + virtual bool MSWCommand(WXUINT param, WXWORD id); virtual void Command(wxCommandEvent& event); virtual bool SetForegroundColour(const wxColour& colour); diff --git a/include/wx/msw/private/button.h b/include/wx/msw/private/button.h new file mode 100644 index 0000000000..ac6393dc34 --- /dev/null +++ b/include/wx/msw/private/button.h @@ -0,0 +1,46 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: msw/private/button.h +// Purpose: helper functions used with native BUTTON control +// Author: Vadim Zeitlin +// Created: 2008-06-07 +// RCS-ID: $Id$ +// Copyright: (c) 2008 Vadim Zeitlin +// Licence: wxWindows licence +/////////////////////////////////////////////////////////////////////////////// + +#ifndef _WX_MSW_PRIVATE_BUTTON_H_ +#define _WX_MSW_PRIVATE_BUTTON_H_ + +namespace wxMSWButton +{ + +// returns BS_MULTILINE if the label contains new lines or 0 otherwise +inline int GetMultilineStyle(const wxString& label) +{ + return label.find(_T('\n')) == wxString::npos ? 0 : BS_MULTILINE; +} + +// update the style of the specified HWND to include or exclude BS_MULTILINE +// depending on whether the label contains the new lines +inline void UpdateMultilineStyle(HWND hwnd, const wxString& label) +{ + // update BS_MULTILINE style depending on the new label (resetting it + // doesn't seem to do anything very useful but it shouldn't hurt and we do + // have to set it whenever the label becomes multi line as otherwise it + // wouldn't be shown correctly as we don't use BS_MULTILINE when creating + // the control unless it already has new lines in its label) + long styleOld = ::GetWindowLong(hwnd, GWL_STYLE), + styleNew; + if ( label.find(_T('\n')) != wxString::npos ) + styleNew = styleOld | BS_MULTILINE; + else + styleNew = styleOld & ~BS_MULTILINE; + + if ( styleNew != styleOld ) + ::SetWindowLong(hwnd, GWL_STYLE, styleNew); +} + +} // namespace wxMSWButton + +#endif // _WX_MSW_PRIVATE_BUTTON_H_ + diff --git a/src/msw/button.cpp b/src/msw/button.cpp index 9ccd939cc1..b342fbc0b2 100644 --- a/src/msw/button.cpp +++ b/src/msw/button.cpp @@ -40,8 +40,8 @@ #endif #include "wx/stockitem.h" -#include "wx/tokenzr.h" #include "wx/msw/private.h" +#include "wx/msw/private/button.h" #if wxUSE_UXTHEME #include "wx/msw/uxtheme.h" @@ -183,10 +183,7 @@ bool wxButton::Create(wxWindow *parent, // // NB: we do it here and not in MSWGetStyle() because we need the label // value and the label is not set yet when MSWGetStyle() is called - if ( label.find(_T('\n')) != wxString::npos ) - { - msStyle |= BS_MULTILINE; - } + msStyle |= wxMSWButton::GetMultilineStyle(label); return MSWCreateControl(_T("BUTTON"), msStyle, pos, size, label, exstyle); } @@ -238,19 +235,7 @@ WXDWORD wxButton::MSWGetStyle(long style, WXDWORD *exstyle) const void wxButton::SetLabel(const wxString& label) { - // update BS_MULTILINE style depending on the new label (resetting it - // doesn't seem to do anything very useful but it shouldn't hurt and we do - // have to set it whenever the label becomes multi line as otherwise it - // wouldn't be shown correctly) - long styleOld = ::GetWindowLong(GetHwnd(), GWL_STYLE), - styleNew; - if ( label.find(_T('\n')) != wxString::npos ) - styleNew = styleOld | BS_MULTILINE; - else - styleNew = styleOld & ~BS_MULTILINE; - - if ( styleNew != styleOld ) - ::SetWindowLong(GetHwnd(), GWL_STYLE, styleNew); + wxMSWButton::UpdateMultilineStyle(GetHwnd(), label); wxButtonBase::SetLabel(label); } diff --git a/src/msw/checkbox.cpp b/src/msw/checkbox.cpp index 02a5a495c5..44645f3b72 100644 --- a/src/msw/checkbox.cpp +++ b/src/msw/checkbox.cpp @@ -35,8 +35,9 @@ #endif #include "wx/msw/dc.h" // for wxDCTemp -#include "wx/msw/uxtheme.h" #include "wx/renderer.h" +#include "wx/msw/uxtheme.h" +#include "wx/msw/private/button.h" // ---------------------------------------------------------------------------- // constants @@ -188,6 +189,8 @@ bool wxCheckBox::Create(wxWindow *parent, msStyle |= BS_LEFTTEXT | BS_RIGHT; } + msStyle |= wxMSWButton::GetMultilineStyle(label); + return MSWCreateControl(wxT("BUTTON"), msStyle, pos, size, label, 0); } @@ -212,7 +215,9 @@ wxSize wxCheckBox::DoGetBestSize() const int wCheckbox, hCheckbox; if ( !str.empty() ) { - GetTextExtent(GetLabelText(str), &wCheckbox, &hCheckbox); + wxClientDC dc(wx_const_cast(wxCheckBox *, this)); + dc.SetFont(GetFont()); + dc.GetMultiLineTextExtent(GetLabelText(str), &wCheckbox, &hCheckbox); wCheckbox += s_checkSize + GetCharWidth(); if ( hCheckbox < s_checkSize ) @@ -236,6 +241,13 @@ wxSize wxCheckBox::DoGetBestSize() const // wxCheckBox operations // ---------------------------------------------------------------------------- +void wxCheckBox::SetLabel(const wxString& label) +{ + wxMSWButton::UpdateMultilineStyle(GetHwnd(), label); + + wxCheckBoxBase::SetLabel(label); +} + void wxCheckBox::SetValue(bool val) { Set3StateValue(val ? wxCHK_CHECKED : wxCHK_UNCHECKED);