]> git.saurik.com Git - wxWidgets.git/blame - src/msw/tglbtn.cpp
Fixed to work with the new wxMacControl system.
[wxWidgets.git] / src / msw / tglbtn.cpp
CommitLineData
1db8dc4a
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/msw/tglbtn.cpp
3// Purpose: Definition of the wxToggleButton class, which implements a
4// toggle button under wxMSW.
b67e8d38
RD
5// Author: John Norris, minor changes by Axel Schlueter
6// and William Gallafent.
1db8dc4a
VZ
7// Modified by:
8// Created: 08.02.01
9// RCS-ID: $Id$
10// Copyright: (c) 2000 Johnny C. Norris II
11// License: Rocketeer license
12/////////////////////////////////////////////////////////////////////////////
13
14// ============================================================================
15// declatations
16// ============================================================================
17
18// ----------------------------------------------------------------------------
19// headers
20// ----------------------------------------------------------------------------
21
22#include "wx/wxprec.h"
23
24#ifdef __BORLANDC__
25 #pragma hdrstop
26#endif
27
28#include "wx/tglbtn.h"
29
30#if wxUSE_TOGGLEBTN
31
32#ifndef WX_PRECOMP
33 #include "wx/button.h"
34 #include "wx/brush.h"
35 #include "wx/dcscreen.h"
36 #include "wx/settings.h"
37
38 #include "wx/log.h"
39#endif // WX_PRECOMP
40
41#include "wx/msw/private.h"
42
43// ----------------------------------------------------------------------------
44// macros
45// ----------------------------------------------------------------------------
46
47IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl)
48DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED)
49
50#define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
51
52// ============================================================================
53// implementation
54// ============================================================================
55
56// ----------------------------------------------------------------------------
57// wxToggleButton
58// ----------------------------------------------------------------------------
59
60bool wxToggleButton::MSWCommand(WXUINT WXUNUSED(param), WXWORD WXUNUSED(id))
61{
62 wxCommandEvent event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, m_windowId);
63 event.SetInt(GetValue());
64 event.SetEventObject(this);
65 ProcessCommand(event);
66 return TRUE;
67}
68
69// Single check box item
70bool wxToggleButton::Create(wxWindow *parent, wxWindowID id,
71 const wxString& label,
72 const wxPoint& pos,
73 const wxSize& size, long style,
74 const wxValidator& validator,
75 const wxString& name)
76{
b67e8d38
RD
77 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
78 return FALSE;
983d2e14 79
b67e8d38 80 if ( !MSWCreateControl(wxT("BUTTON"), label, pos, size) )
1db8dc4a 81 return FALSE;
b67e8d38
RD
82
83 return TRUE;
84}
1db8dc4a 85
b67e8d38
RD
86wxBorder wxToggleButton::GetDefaultBorder() const
87{
88 return wxBORDER_NONE;
89}
1db8dc4a 90
b67e8d38
RD
91WXDWORD wxToggleButton::MSWGetStyle(long style, WXDWORD *exstyle) const
92{
93 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
1db8dc4a 94
788722ac
JS
95#ifndef BS_PUSHLIKE
96#define BS_PUSHLIKE 0x00001000L
97#endif
98
b67e8d38 99 msStyle |= BS_AUTOCHECKBOX | BS_PUSHLIKE | WS_TABSTOP;
b0766406 100
b67e8d38 101 if(style & wxBU_LEFT)
1db8dc4a 102 msStyle |= BS_LEFT;
b67e8d38 103 if(style & wxBU_RIGHT)
1db8dc4a 104 msStyle |= BS_RIGHT;
b67e8d38 105 if(style & wxBU_TOP)
1db8dc4a 106 msStyle |= BS_TOP;
b67e8d38 107 if(style & wxBU_BOTTOM)
1db8dc4a 108 msStyle |= BS_BOTTOM;
1db8dc4a 109
b67e8d38 110 return msStyle;
1db8dc4a
VZ
111}
112
113void wxToggleButton::SetLabel(const wxString& label)
114{
115 SetWindowText(GetHwnd(), label);
116}
117
118wxSize wxToggleButton::DoGetBestSize() const
119{
120 wxString label = wxGetWindowText(GetHWND());
121 int wBtn;
122 GetTextExtent(label, &wBtn, NULL);
123
124 int wChar, hChar;
125 wxGetCharSize(GetHWND(), &wChar, &hChar, &GetFont());
126
127 // add a margin - the button is wider than just its label
128 wBtn += 3*wChar;
129
130 // the button height is proportional to the height of the font used
131 int hBtn = BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar);
132
0b4f47a3 133#if wxUSE_BUTTON
1db8dc4a
VZ
134 wxSize sz = wxButton::GetDefaultSize();
135 if (wBtn > sz.x)
136 sz.x = wBtn;
137 if (hBtn > sz.y)
138 sz.y = hBtn;
0b4f47a3
DS
139#else
140 wxSize sz(wBtn, hBtn);
141#endif
1db8dc4a
VZ
142
143 return sz;
144}
145
146void wxToggleButton::SetValue(bool val)
147{
148 SendMessage(GetHwnd(), BM_SETCHECK, val, 0);
149}
150
151#ifndef BST_CHECKED
152#define BST_CHECKED 0x0001
153#endif
154
155bool wxToggleButton::GetValue() const
156{
157#ifdef __WIN32__
158 return (SendMessage(GetHwnd(), BM_GETCHECK, 0, 0) == BST_CHECKED);
159#else
160 return ((0x001 & SendMessage(GetHwnd(), BM_GETCHECK, 0, 0)) == 0x001);
161#endif
162}
163
164void wxToggleButton::Command(wxCommandEvent & event)
165{
166 SetValue((event.GetInt() != 0));
167 ProcessCommand(event);
168}
169
170#endif // wxUSE_TOGGLEBTN
171