]>
Commit | Line | Data |
---|---|---|
0e320a79 DW |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: button.cpp | |
3 | // Purpose: wxButton | |
d88de032 | 4 | // Author: David Webster |
0e320a79 | 5 | // Modified by: |
d88de032 | 6 | // Created: 10/13/99 |
0e320a79 | 7 | // RCS-ID: $Id$ |
d88de032 DW |
8 | // Copyright: (c) David Webster |
9 | // Licence: wxWindows licence | |
0e320a79 DW |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
d88de032 DW |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifndef WX_PRECOMP | |
16 | #include "wx/button.h" | |
17 | #include "wx/brush.h" | |
18 | #include "wx/panel.h" | |
19 | #include "wx/bmpbuttn.h" | |
20 | #include "wx/settings.h" | |
21 | #include "wx/dcscreen.h" | |
a4a16252 | 22 | #include "wx/scrolwin.h" |
0e320a79 DW |
23 | #endif |
24 | ||
d88de032 | 25 | #include "wx/os2/private.h" |
0e320a79 | 26 | |
987da0d4 DW |
27 | #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10) |
28 | ||
29 | // | |
30 | // Should be at the very least less than winDEFAULT_BUTTON_MARGIN | |
31 | // | |
32 | #define FOCUS_MARGIN 3 | |
33 | ||
34 | #ifndef BST_CHECKED | |
35 | #define BST_CHECKED 0x0001 | |
36 | #endif | |
37 | ||
0e320a79 | 38 | IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl) |
0e320a79 DW |
39 | |
40 | // Button | |
41 | ||
987da0d4 DW |
42 | bool wxButton::Create( |
43 | wxWindow* pParent | |
44 | , wxWindowID vId | |
45 | , const wxString& rsLabel | |
46 | , const wxPoint& rPos | |
47 | , const wxSize& rSize | |
48 | , long lStyle | |
5d4b632b | 49 | #if wxUSE_VALIDATORS |
987da0d4 | 50 | , const wxValidator& rValidator |
5d4b632b | 51 | #endif |
987da0d4 DW |
52 | , const wxString& rsName |
53 | ) | |
0e320a79 | 54 | { |
987da0d4 | 55 | SetName(rsName); |
5d4b632b | 56 | #if wxUSE_VALIDATORS |
987da0d4 | 57 | SetValidator(rValidator); |
5d4b632b | 58 | #endif |
987da0d4 DW |
59 | m_windowStyle = lStyle; |
60 | pParent->AddChild((wxButton *)this); | |
61 | if (vId == -1) | |
0e320a79 DW |
62 | m_windowId = NewControlId(); |
63 | else | |
987da0d4 DW |
64 | m_windowId = vId; |
65 | lStyle = WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON; | |
66 | ||
67 | // | |
68 | // OS/2 PM does not have Right/Left/Top/Bottom styles. | |
69 | // We will have to define an additional style when we implement notebooks | |
70 | // for a notebook page button | |
71 | // | |
72 | if (m_windowStyle & wxCLIP_SIBLINGS ) | |
73 | lStyle |= WS_CLIPSIBLINGS; | |
5d44b24e DW |
74 | // |
75 | // If the parent is a scrolled window the controls must | |
76 | // have this style or they will overlap the scrollbars | |
77 | // | |
78 | if (pParent) | |
79 | if (pParent->IsKindOf(CLASSINFO(wxScrolledWindow)) || | |
80 | pParent->IsKindOf(CLASSINFO(wxGenericScrolledWindow))) | |
81 | lStyle |= WS_CLIPSIBLINGS; | |
82 | ||
987da0d4 DW |
83 | m_hWnd = (WXHWND)::WinCreateWindow( GetHwndOf(pParent) // Parent handle |
84 | ,WC_BUTTON // A Button class window | |
85 | ,(PSZ)rsLabel.c_str() // Button text | |
86 | ,lStyle // Button style | |
87 | ,0, 0, 0, 0 // Location and size | |
88 | ,GetHwndOf(pParent) // Owner handle | |
89 | ,HWND_TOP // Top of Z-Order | |
90 | ,vId // Identifier | |
91 | ,NULL // No control data | |
92 | ,NULL // No Presentation parameters | |
93 | ); | |
94 | if (m_hWnd == 0) | |
95 | { | |
96 | return FALSE; | |
97 | } | |
0e320a79 | 98 | |
987da0d4 DW |
99 | // |
100 | // Subclass again for purposes of dialog editing mode | |
101 | // | |
102 | SubclassWin(m_hWnd); | |
e58dab20 | 103 | SetFont(*wxSMALL_FONT); |
987da0d4 DW |
104 | SetSize( rPos.x |
105 | ,rPos.y | |
106 | ,rSize.x | |
107 | ,rSize.y | |
108 | ); | |
109 | return TRUE; | |
110 | } // end of wxButton::Create | |
0e320a79 | 111 | |
d88de032 | 112 | wxButton::~wxButton() |
0e320a79 | 113 | { |
987da0d4 DW |
114 | wxPanel* pPanel = wxDynamicCast(GetParent(), wxPanel); |
115 | ||
116 | if (pPanel) | |
d88de032 | 117 | { |
987da0d4 | 118 | if (pPanel->GetDefaultItem() == this) |
d88de032 | 119 | { |
987da0d4 DW |
120 | // |
121 | // Don't leave the panel with invalid default item | |
122 | // | |
123 | pPanel->SetDefaultItem(NULL); | |
d88de032 DW |
124 | } |
125 | } | |
987da0d4 | 126 | } // end of wxButton::~wxButton |
d88de032 DW |
127 | |
128 | // ---------------------------------------------------------------------------- | |
129 | // size management including autosizing | |
130 | // ---------------------------------------------------------------------------- | |
131 | ||
e78c4d50 | 132 | wxSize wxButton::DoGetBestSize() const |
d88de032 | 133 | { |
987da0d4 DW |
134 | wxString rsLabel = wxGetWindowText(GetHWND()); |
135 | int nWidthButton; | |
136 | int nWidthChar; | |
137 | int nHeightChar; | |
138 | ||
139 | GetTextExtent( rsLabel | |
140 | ,&nWidthButton | |
141 | ,NULL | |
142 | ); | |
143 | ||
144 | wxGetCharSize( GetHWND() | |
145 | ,&nWidthChar | |
146 | ,&nHeightChar | |
147 | ,(wxFont*)&GetFont() | |
148 | ); | |
149 | ||
150 | // | |
151 | // Add a margin - the button is wider than just its label | |
152 | // | |
153 | nWidthButton += 3 * nWidthChar; | |
154 | ||
155 | // | |
156 | // The button height is proportional to the height of the font used | |
157 | // | |
158 | int nHeightButton = BUTTON_HEIGHT_FROM_CHAR_HEIGHT(nHeightChar); | |
159 | ||
160 | // | |
161 | // Need a little extra to make it look right | |
162 | // | |
163 | nHeightButton += nHeightChar/1.5; | |
164 | ||
165 | wxSize vSize = GetDefaultSize(); | |
166 | ||
167 | if (nWidthButton > vSize.x) | |
168 | vSize.x = nWidthButton; | |
169 | if (nHeightButton > vSize.y) | |
170 | vSize.y = nHeightButton; | |
171 | return vSize; | |
172 | } // end of wxButton::DoGetBestSize | |
d88de032 DW |
173 | |
174 | /* static */ | |
175 | wxSize wxButton::GetDefaultSize() | |
176 | { | |
987da0d4 | 177 | static wxSize vSizeBtn; |
d88de032 | 178 | |
987da0d4 | 179 | if (vSizeBtn.x == 0) |
d88de032 | 180 | { |
987da0d4 | 181 | wxScreenDC vDc; |
d88de032 | 182 | |
a756f210 | 183 | vDc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)); |
987da0d4 DW |
184 | |
185 | // | |
186 | // The size of a standard button in the dialog units is 50x14, | |
d88de032 DW |
187 | // translate this to pixels |
188 | // NB1: the multipliers come from the Windows convention | |
189 | // NB2: the extra +1/+2 were needed to get the size be the same as the | |
190 | // size of the buttons in the standard dialog - I don't know how | |
191 | // this happens, but on my system this size is 75x23 in pixels and | |
192 | // 23*8 isn't even divisible by 14... Would be nice to understand | |
193 | // why these constants are needed though! | |
987da0d4 DW |
194 | vSizeBtn.x = (50 * (vDc.GetCharWidth() + 1))/4; |
195 | vSizeBtn.y = ((14 * vDc.GetCharHeight()) + 2)/8; | |
d88de032 | 196 | } |
987da0d4 DW |
197 | return vSizeBtn; |
198 | } // end of wxButton::GetDefaultSize | |
d88de032 | 199 | |
987da0d4 DW |
200 | void wxButton::Command ( |
201 | wxCommandEvent& rEvent | |
202 | ) | |
d88de032 | 203 | { |
987da0d4 DW |
204 | ProcessCommand (rEvent); |
205 | } // end of wxButton::Command | |
d88de032 DW |
206 | |
207 | // ---------------------------------------------------------------------------- | |
208 | // helpers | |
209 | // ---------------------------------------------------------------------------- | |
210 | ||
211 | bool wxButton::SendClickEvent() | |
212 | { | |
987da0d4 DW |
213 | wxCommandEvent vEvent( wxEVT_COMMAND_BUTTON_CLICKED |
214 | ,GetId() | |
215 | ); | |
d88de032 | 216 | |
987da0d4 DW |
217 | vEvent.SetEventObject(this); |
218 | return ProcessCommand(vEvent); | |
219 | } // end of wxButton::SendClickEvent | |
0e320a79 DW |
220 | |
221 | void wxButton::SetDefault() | |
222 | { | |
987da0d4 DW |
223 | wxWindow* pParent = GetParent(); |
224 | wxButton* pBtnOldDefault = NULL; | |
225 | wxPanel* pPanel = wxDynamicCast(pParent, wxPanel); | |
226 | long lStyle = 0L; | |
227 | ||
228 | if (pParent) | |
229 | { | |
230 | wxWindow* pWinOldDefault = pParent->SetDefaultItem(this); | |
231 | ||
232 | pBtnOldDefault = wxDynamicCast(pWinOldDefault, wxButton); | |
233 | } | |
234 | if (pBtnOldDefault && pBtnOldDefault != this) | |
235 | { | |
236 | // | |
237 | // Remove the BS_DEFPUSHBUTTON style from the other button | |
238 | // | |
239 | lStyle = ::WinQueryWindowULong(GetHwndOf(pBtnOldDefault), QWL_STYLE); | |
240 | ||
241 | // | |
242 | // Don't do it with the owner drawn buttons because it will reset | |
243 | // BS_OWNERDRAW style bit too (BS_OWNERDRAW & BS_DEFPUSHBUTTON != 0)! | |
244 | // | |
245 | if ((lStyle & BS_USERBUTTON) != BS_USERBUTTON) | |
246 | { | |
247 | lStyle &= ~BS_DEFAULT; | |
248 | ::WinSetWindowULong(GetHwndOf(pBtnOldDefault), QWL_STYLE, lStyle); | |
249 | } | |
250 | else | |
251 | { | |
252 | // | |
253 | // Redraw the button - it will notice itself that it's not the | |
254 | // default one any longer | |
255 | // | |
256 | pBtnOldDefault->Refresh(); | |
257 | } | |
258 | } | |
0e320a79 | 259 | |
987da0d4 DW |
260 | // |
261 | // Set this button as the default | |
262 | // | |
263 | lStyle = ::WinQueryWindowULong(GetHwnd(), QWL_STYLE); | |
264 | if ((lStyle & BS_USERBUTTON) != BS_USERBUTTON) | |
265 | { | |
266 | lStyle != BS_DEFAULT; | |
267 | ::WinSetWindowULong(GetHwnd(), QWL_STYLE, lStyle); | |
268 | } | |
269 | } // end of wxButton::SetDefault | |
0e320a79 | 270 | |
d88de032 DW |
271 | // ---------------------------------------------------------------------------- |
272 | // event/message handlers | |
273 | // ---------------------------------------------------------------------------- | |
0e320a79 | 274 | |
987da0d4 DW |
275 | bool wxButton::OS2Command( |
276 | WXUINT uParam | |
277 | , WXWORD wId | |
278 | ) | |
0e320a79 | 279 | { |
987da0d4 DW |
280 | bool bProcessed = FALSE; |
281 | ||
282 | switch (uParam) | |
d88de032 | 283 | { |
987da0d4 DW |
284 | case BN_CLICKED: // normal buttons send this |
285 | case BN_DBLCLICKED: // owner-drawn ones also send this | |
286 | bProcessed = SendClickEvent(); | |
d88de032 DW |
287 | break; |
288 | } | |
987da0d4 DW |
289 | return bProcessed; |
290 | } // end of wxButton::OS2Command | |
291 | ||
292 | WXHBRUSH wxButton::OnCtlColor( | |
293 | WXHDC pDC | |
294 | , WXHWND pWnd | |
295 | , WXUINT nCtlColor | |
296 | , WXUINT uMessage | |
297 | , WXWPARAM wParam | |
298 | , WXLPARAM lParam | |
299 | ) | |
300 | { | |
301 | wxBrush* pBackgroundBrush = wxTheBrushList->FindOrCreateBrush( GetBackgroundColour() | |
302 | ,wxSOLID | |
303 | ); | |
304 | ||
305 | return (WXHBRUSH)pBackgroundBrush->GetResourceHandle(); | |
306 | } // end of wxButton::OnCtlColor | |
307 | ||
308 | void wxButton::MakeOwnerDrawn() | |
0e320a79 | 309 | { |
987da0d4 | 310 | long lStyle = 0L; |
d88de032 | 311 | |
987da0d4 DW |
312 | lStyle = ::WinQueryWindowULong(GetHwnd(), QWL_STYLE); |
313 | if ((lStyle & BS_USERBUTTON) != BS_USERBUTTON) | |
314 | { | |
315 | // | |
316 | // Make it so | |
317 | // | |
318 | lStyle |= BS_USERBUTTON; | |
319 | ::WinSetWindowULong(GetHwnd(), QWL_STYLE, lStyle); | |
320 | } | |
321 | } // end of wxCButton::MakeOwnerDrawn | |
322 | ||
323 | MRESULT wxButton::WindowProc( | |
324 | WXUINT uMsg | |
325 | , WXWPARAM wParam | |
326 | , WXLPARAM lParam | |
327 | ) | |
328 | { | |
329 | // | |
330 | // When we receive focus, we want to become the default button in our | |
331 | // parent panel | |
332 | // | |
333 | if (uMsg == WM_SETFOCUS) | |
334 | { | |
335 | SetDefault(); | |
336 | ||
337 | // | |
338 | // Let the default processign take place too | |
339 | // | |
340 | } | |
341 | ||
342 | else if (uMsg == WM_BUTTON1DBLCLK) | |
343 | { | |
344 | // | |
345 | // Emulate a click event to force an owner-drawn button to change its | |
346 | // appearance - without this, it won't do it | |
347 | // | |
348 | (void)wxControl::OS2WindowProc( WM_BUTTON1DOWN | |
349 | ,wParam | |
350 | ,lParam | |
351 | ); | |
352 | ||
353 | // | |
354 | // And conitnue with processing the message normally as well | |
355 | // | |
356 | } | |
0e320a79 | 357 | |
987da0d4 DW |
358 | // |
359 | // Let the base class do all real processing | |
360 | // | |
361 | return (wxControl::OS2WindowProc( uMsg | |
362 | ,wParam | |
363 | ,lParam | |
364 | )); | |
365 | } // end of wxW indowProc | |
d88de032 | 366 |