]> git.saurik.com Git - wxWidgets.git/blame - src/msw/tglbtn.cpp
document On{Open,Save}Document()
[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.
f1e01716
WS
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
706fb893 11// License: wxWindows licence
1db8dc4a
VZ
12/////////////////////////////////////////////////////////////////////////////
13
14// ============================================================================
8907154c 15// declarations
1db8dc4a
VZ
16// ============================================================================
17
18// ----------------------------------------------------------------------------
19// headers
20// ----------------------------------------------------------------------------
21
22#include "wx/wxprec.h"
23
24#ifdef __BORLANDC__
25 #pragma hdrstop
26#endif
27
1db8dc4a
VZ
28#if wxUSE_TOGGLEBTN
29
f1e01716
WS
30#include "wx/tglbtn.h"
31
1db8dc4a
VZ
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);
bfbb0b4c 66 return true;
1db8dc4a
VZ
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 77 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
bfbb0b4c
WS
78 return false;
79
b67e8d38 80 if ( !MSWCreateControl(wxT("BUTTON"), label, pos, size) )
bfbb0b4c
WS
81 return false;
82
83 return true;
b67e8d38 84}
1db8dc4a 85
b67e8d38
RD
86WXDWORD wxToggleButton::MSWGetStyle(long style, WXDWORD *exstyle) const
87{
88 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
1db8dc4a 89
788722ac
JS
90#ifndef BS_PUSHLIKE
91#define BS_PUSHLIKE 0x00001000L
92#endif
93
b67e8d38 94 msStyle |= BS_AUTOCHECKBOX | BS_PUSHLIKE | WS_TABSTOP;
b0766406 95
b67e8d38 96 if(style & wxBU_LEFT)
1db8dc4a 97 msStyle |= BS_LEFT;
b67e8d38 98 if(style & wxBU_RIGHT)
1db8dc4a 99 msStyle |= BS_RIGHT;
b67e8d38 100 if(style & wxBU_TOP)
1db8dc4a 101 msStyle |= BS_TOP;
b67e8d38 102 if(style & wxBU_BOTTOM)
1db8dc4a 103 msStyle |= BS_BOTTOM;
1db8dc4a 104
b67e8d38 105 return msStyle;
1db8dc4a
VZ
106}
107
1db8dc4a
VZ
108wxSize wxToggleButton::DoGetBestSize() const
109{
110 wxString label = wxGetWindowText(GetHWND());
111 int wBtn;
32cd189d 112 GetTextExtent(GetLabelText(label), &wBtn, NULL);
1db8dc4a
VZ
113
114 int wChar, hChar;
7a5e53ab 115 wxGetCharSize(GetHWND(), &wChar, &hChar, GetFont());
1db8dc4a
VZ
116
117 // add a margin - the button is wider than just its label
118 wBtn += 3*wChar;
119
120 // the button height is proportional to the height of the font used
121 int hBtn = BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar);
122
0b4f47a3 123#if wxUSE_BUTTON
f40ea601
VZ
124 // make all buttons of at least standard size unless wxBU_EXACTFIT is given
125 if ( !HasFlag(wxBU_EXACTFIT) )
126 {
127 const wxSize szMin = wxButton::GetDefaultSize();
128 if ( wBtn < szMin.x )
129 wBtn = szMin.x;
130 if ( hBtn < szMin.y )
131 hBtn = szMin.y;
132 }
133#endif // wxUSE_BUTTON
134
0b4f47a3 135 wxSize sz(wBtn, hBtn);
1db8dc4a 136
31582e4e 137 CacheBestSize(sz);
1db8dc4a
VZ
138 return sz;
139}
140
141void wxToggleButton::SetValue(bool val)
142{
bfbb0b4c 143 ::SendMessage(GetHwnd(), BM_SETCHECK, val, 0);
1db8dc4a
VZ
144}
145
146#ifndef BST_CHECKED
147#define BST_CHECKED 0x0001
148#endif
149
150bool wxToggleButton::GetValue() const
151{
152#ifdef __WIN32__
bfbb0b4c 153 return (::SendMessage(GetHwnd(), BM_GETCHECK, 0, 0) == BST_CHECKED);
1db8dc4a 154#else
bfbb0b4c 155 return ((0x001 & ::SendMessage(GetHwnd(), BM_GETCHECK, 0, 0)) == 0x001);
1db8dc4a
VZ
156#endif
157}
158
159void wxToggleButton::Command(wxCommandEvent & event)
160{
161 SetValue((event.GetInt() != 0));
162 ProcessCommand(event);
163}
164
165#endif // wxUSE_TOGGLEBTN