]> git.saurik.com Git - wxWidgets.git/blob - src/os2/tglbtn.cpp
Fix Seeking to end of wxStreamInputStream (patch [ 1178834 ] string stream bad m_len...
[wxWidgets.git] / src / os2 / tglbtn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/tglbtn.cpp
3 // Purpose: Definition of the wxToggleButton class, which implements a
4 // toggle button under wxMSW.
5 // Author: John Norris, minor changes by Axel Schlueter
6 // and William Gallafent.
7 // Modified by:
8 // Created: 08.02.01
9 // RCS-ID: $Id$
10 // Copyright: (c) 2000 Johnny C. Norris II
11 // License: wxWindows licence
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
47 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl)
48 DEFINE_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
60 bool wxToggleButton::OS2Command(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
70 bool 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 {
77 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
78 return false;
79
80 if ( !OS2CreateControl(wxT("BUTTON"), label, pos, size, 0) )
81 return false;
82
83 return true;
84 }
85
86 wxBorder wxToggleButton::GetDefaultBorder() const
87 {
88 return wxBORDER_NONE;
89 }
90
91 WXDWORD wxToggleButton::OS2GetStyle(long style, WXDWORD *exstyle) const
92 {
93 WXDWORD msStyle = wxControl::OS2GetStyle(style, exstyle);
94
95 #ifndef BS_PUSHLIKE
96 #define BS_PUSHLIKE 0x00001000L
97 #endif
98
99 msStyle |= BS_AUTOCHECKBOX | BS_PUSHLIKE | WS_TABSTOP;
100
101 return msStyle;
102 }
103
104 wxSize wxToggleButton::DoGetBestSize() const
105 {
106 wxString label = wxGetWindowText(GetHWND());
107 int wBtn;
108 wxFont vFont = GetFont();
109 int wChar;
110 int hChar;
111
112 GetTextExtent(label, &wBtn, NULL);
113
114
115 wxGetCharSize(GetHWND(), &wChar, &hChar, &vFont);
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
123 #if wxUSE_BUTTON
124 wxSize sz = wxButton::GetDefaultSize();
125 if (wBtn > sz.x)
126 sz.x = wBtn;
127 if (hBtn > sz.y)
128 sz.y = hBtn;
129 #else
130 wxSize sz(wBtn, hBtn);
131 #endif
132
133 return sz;
134 }
135
136 void wxToggleButton::SetValue(bool val)
137 {
138 ::WinSendMsg(GetHwnd(), BM_SETCHECK, MPFROMSHORT(val), (MPARAM)0);
139 }
140
141 #ifndef BST_CHECKED
142 #define BST_CHECKED 0x0001
143 #endif
144
145 bool wxToggleButton::GetValue() const
146 {
147 return (::WinSendMsg(GetHwnd(), BM_QUERYCHECK, 0, 0) == (MRESULT)BST_CHECKED);
148 }
149
150 void wxToggleButton::Command(wxCommandEvent & event)
151 {
152 SetValue((event.GetInt() != 0));
153 ProcessCommand(event);
154 }
155
156 #endif // wxUSE_TOGGLEBTN
157