]>
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 | |
1a75e76f | 16 | #include "wx/app.h" |
d88de032 DW |
17 | #include "wx/button.h" |
18 | #include "wx/brush.h" | |
19 | #include "wx/panel.h" | |
20 | #include "wx/bmpbuttn.h" | |
21 | #include "wx/settings.h" | |
22 | #include "wx/dcscreen.h" | |
a4a16252 | 23 | #include "wx/scrolwin.h" |
0e320a79 DW |
24 | #endif |
25 | ||
d88de032 | 26 | #include "wx/os2/private.h" |
0e320a79 | 27 | |
987da0d4 DW |
28 | #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10) |
29 | ||
30 | // | |
31 | // Should be at the very least less than winDEFAULT_BUTTON_MARGIN | |
32 | // | |
33 | #define FOCUS_MARGIN 3 | |
34 | ||
35 | #ifndef BST_CHECKED | |
36 | #define BST_CHECKED 0x0001 | |
37 | #endif | |
38 | ||
0e320a79 | 39 | IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl) |
0e320a79 DW |
40 | |
41 | // Button | |
42 | ||
987da0d4 DW |
43 | bool wxButton::Create( |
44 | wxWindow* pParent | |
45 | , wxWindowID vId | |
46 | , const wxString& rsLabel | |
47 | , const wxPoint& rPos | |
48 | , const wxSize& rSize | |
49 | , long lStyle | |
987da0d4 | 50 | , const wxValidator& rValidator |
987da0d4 DW |
51 | , const wxString& rsName |
52 | ) | |
0e320a79 | 53 | { |
987da0d4 | 54 | SetName(rsName); |
5d4b632b | 55 | #if wxUSE_VALIDATORS |
987da0d4 | 56 | SetValidator(rValidator); |
5d4b632b | 57 | #endif |
987da0d4 DW |
58 | m_windowStyle = lStyle; |
59 | pParent->AddChild((wxButton *)this); | |
60 | if (vId == -1) | |
0e320a79 DW |
61 | m_windowId = NewControlId(); |
62 | else | |
987da0d4 DW |
63 | m_windowId = vId; |
64 | lStyle = WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON; | |
65 | ||
66 | // | |
67 | // OS/2 PM does not have Right/Left/Top/Bottom styles. | |
68 | // We will have to define an additional style when we implement notebooks | |
69 | // for a notebook page button | |
70 | // | |
71 | if (m_windowStyle & wxCLIP_SIBLINGS ) | |
72 | lStyle |= WS_CLIPSIBLINGS; | |
5d44b24e | 73 | |
987da0d4 DW |
74 | m_hWnd = (WXHWND)::WinCreateWindow( GetHwndOf(pParent) // Parent handle |
75 | ,WC_BUTTON // A Button class window | |
76 | ,(PSZ)rsLabel.c_str() // Button text | |
77 | ,lStyle // Button style | |
78 | ,0, 0, 0, 0 // Location and size | |
79 | ,GetHwndOf(pParent) // Owner handle | |
80 | ,HWND_TOP // Top of Z-Order | |
81 | ,vId // Identifier | |
82 | ,NULL // No control data | |
83 | ,NULL // No Presentation parameters | |
84 | ); | |
85 | if (m_hWnd == 0) | |
86 | { | |
87 | return FALSE; | |
88 | } | |
0e320a79 | 89 | |
987da0d4 DW |
90 | // |
91 | // Subclass again for purposes of dialog editing mode | |
92 | // | |
93 | SubclassWin(m_hWnd); | |
2c1e8f2e DW |
94 | wxFont* pButtonFont = new wxFont( 8 |
95 | ,wxSWISS | |
96 | ,wxNORMAL | |
97 | ,wxNORMAL | |
98 | ); | |
99 | SetFont(*pButtonFont); | |
100 | SetXComp(0); | |
101 | SetYComp(0); | |
987da0d4 DW |
102 | SetSize( rPos.x |
103 | ,rPos.y | |
104 | ,rSize.x | |
105 | ,rSize.y | |
106 | ); | |
b3260bce | 107 | delete pButtonFont; |
987da0d4 DW |
108 | return TRUE; |
109 | } // end of wxButton::Create | |
0e320a79 | 110 | |
d88de032 | 111 | wxButton::~wxButton() |
0e320a79 | 112 | { |
987da0d4 DW |
113 | wxPanel* pPanel = wxDynamicCast(GetParent(), wxPanel); |
114 | ||
115 | if (pPanel) | |
d88de032 | 116 | { |
987da0d4 | 117 | if (pPanel->GetDefaultItem() == this) |
d88de032 | 118 | { |
987da0d4 DW |
119 | // |
120 | // Don't leave the panel with invalid default item | |
121 | // | |
122 | pPanel->SetDefaultItem(NULL); | |
d88de032 DW |
123 | } |
124 | } | |
987da0d4 | 125 | } // end of wxButton::~wxButton |
d88de032 DW |
126 | |
127 | // ---------------------------------------------------------------------------- | |
128 | // size management including autosizing | |
129 | // ---------------------------------------------------------------------------- | |
130 | ||
e78c4d50 | 131 | wxSize wxButton::DoGetBestSize() const |
d88de032 | 132 | { |
987da0d4 DW |
133 | wxString rsLabel = wxGetWindowText(GetHWND()); |
134 | int nWidthButton; | |
135 | int nWidthChar; | |
136 | int nHeightChar; | |
137 | ||
138 | GetTextExtent( rsLabel | |
139 | ,&nWidthButton | |
140 | ,NULL | |
141 | ); | |
142 | ||
143 | wxGetCharSize( GetHWND() | |
144 | ,&nWidthChar | |
145 | ,&nHeightChar | |
146 | ,(wxFont*)&GetFont() | |
147 | ); | |
148 | ||
149 | // | |
150 | // Add a margin - the button is wider than just its label | |
151 | // | |
152 | nWidthButton += 3 * nWidthChar; | |
153 | ||
154 | // | |
155 | // The button height is proportional to the height of the font used | |
156 | // | |
157 | int nHeightButton = BUTTON_HEIGHT_FROM_CHAR_HEIGHT(nHeightChar); | |
158 | ||
159 | // | |
160 | // Need a little extra to make it look right | |
161 | // | |
162 | nHeightButton += nHeightChar/1.5; | |
163 | ||
97d74dd2 DW |
164 | if (!HasFlag(wxBU_EXACTFIT)) |
165 | { | |
166 | wxSize vSize = GetDefaultSize(); | |
987da0d4 | 167 | |
97d74dd2 DW |
168 | if (nWidthButton > vSize.x) |
169 | vSize.x = nWidthButton; | |
170 | if (nHeightButton > vSize.y) | |
171 | vSize.y = nHeightButton; | |
172 | return vSize; | |
173 | } | |
174 | return wxSize( nWidthButton | |
175 | ,nHeightButton | |
176 | ); | |
987da0d4 | 177 | } // end of wxButton::DoGetBestSize |
d88de032 DW |
178 | |
179 | /* static */ | |
180 | wxSize wxButton::GetDefaultSize() | |
181 | { | |
987da0d4 | 182 | static wxSize vSizeBtn; |
d88de032 | 183 | |
987da0d4 | 184 | if (vSizeBtn.x == 0) |
d88de032 | 185 | { |
987da0d4 | 186 | wxScreenDC vDc; |
d88de032 | 187 | |
a756f210 | 188 | vDc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)); |
987da0d4 DW |
189 | |
190 | // | |
191 | // The size of a standard button in the dialog units is 50x14, | |
d88de032 DW |
192 | // translate this to pixels |
193 | // NB1: the multipliers come from the Windows convention | |
194 | // NB2: the extra +1/+2 were needed to get the size be the same as the | |
195 | // size of the buttons in the standard dialog - I don't know how | |
196 | // this happens, but on my system this size is 75x23 in pixels and | |
197 | // 23*8 isn't even divisible by 14... Would be nice to understand | |
198 | // why these constants are needed though! | |
987da0d4 DW |
199 | vSizeBtn.x = (50 * (vDc.GetCharWidth() + 1))/4; |
200 | vSizeBtn.y = ((14 * vDc.GetCharHeight()) + 2)/8; | |
d88de032 | 201 | } |
987da0d4 DW |
202 | return vSizeBtn; |
203 | } // end of wxButton::GetDefaultSize | |
d88de032 | 204 | |
987da0d4 DW |
205 | void wxButton::Command ( |
206 | wxCommandEvent& rEvent | |
207 | ) | |
d88de032 | 208 | { |
987da0d4 DW |
209 | ProcessCommand (rEvent); |
210 | } // end of wxButton::Command | |
d88de032 DW |
211 | |
212 | // ---------------------------------------------------------------------------- | |
213 | // helpers | |
214 | // ---------------------------------------------------------------------------- | |
215 | ||
216 | bool wxButton::SendClickEvent() | |
217 | { | |
987da0d4 DW |
218 | wxCommandEvent vEvent( wxEVT_COMMAND_BUTTON_CLICKED |
219 | ,GetId() | |
220 | ); | |
d88de032 | 221 | |
987da0d4 DW |
222 | vEvent.SetEventObject(this); |
223 | return ProcessCommand(vEvent); | |
224 | } // end of wxButton::SendClickEvent | |
0e320a79 DW |
225 | |
226 | void wxButton::SetDefault() | |
227 | { | |
987da0d4 | 228 | wxWindow* pParent = GetParent(); |
987da0d4 | 229 | |
430974f8 DW |
230 | wxCHECK_RET( pParent, _T("button without parent?") ); |
231 | ||
232 | // | |
233 | // Set this one as the default button both for wxWindows and Windows | |
234 | // | |
235 | wxWindow* pWinOldDefault = pParent->SetDefaultItem(this); | |
cfcebdb1 DW |
236 | |
237 | SetDefaultStyle( wxDynamicCast(pWinOldDefault, wxButton) | |
238 | ,FALSE | |
239 | ); | |
240 | SetDefaultStyle( this | |
241 | ,TRUE | |
242 | ); | |
430974f8 DW |
243 | } // end of wxButton::SetDefault |
244 | ||
245 | void wxButton::SetTmpDefault() | |
246 | { | |
247 | wxWindow* pParent = GetParent(); | |
248 | ||
249 | wxCHECK_RET( pParent, _T("button without parent?") ); | |
250 | ||
251 | wxWindow* pWinOldDefault = pParent->GetDefaultItem(); | |
cfcebdb1 | 252 | |
430974f8 | 253 | pParent->SetTmpDefaultItem(this); |
cfcebdb1 DW |
254 | SetDefaultStyle( wxDynamicCast(pWinOldDefault, wxButton) |
255 | ,FALSE | |
256 | ); | |
257 | SetDefaultStyle( this | |
258 | ,TRUE | |
259 | ); | |
430974f8 DW |
260 | } // end of wxButton::SetTmpDefault |
261 | ||
262 | void wxButton::UnsetTmpDefault() | |
263 | { | |
264 | wxWindow* pParent = GetParent(); | |
265 | ||
266 | wxCHECK_RET( pParent, _T("button without parent?") ); | |
987da0d4 | 267 | |
430974f8 DW |
268 | pParent->SetTmpDefaultItem(NULL); |
269 | ||
270 | wxWindow* pWinOldDefault = pParent->GetDefaultItem(); | |
271 | ||
cfcebdb1 DW |
272 | SetDefaultStyle( this |
273 | ,FALSE | |
274 | ); | |
275 | SetDefaultStyle( wxDynamicCast(pWinOldDefault, wxButton) | |
276 | ,TRUE | |
277 | ); | |
430974f8 DW |
278 | } // end of wxButton::UnsetTmpDefault |
279 | ||
cfcebdb1 DW |
280 | void wxButton::SetDefaultStyle( |
281 | wxButton* pBtn | |
282 | , bool bOn | |
283 | ) | |
430974f8 | 284 | { |
430974f8 | 285 | long lStyle; |
cfcebdb1 DW |
286 | // |
287 | // We may be called with NULL pointer -- simpler to do the check here than | |
288 | // in the caller which does wxDynamicCast() | |
289 | // | |
290 | if (!pBtn) | |
291 | return; | |
430974f8 | 292 | |
cfcebdb1 DW |
293 | // |
294 | // First, let DefDlgProc() know about the new default button | |
295 | // | |
296 | if (bOn) | |
297 | { | |
298 | if (!wxTheApp->IsActive()) | |
299 | return; | |
300 | ||
301 | // | |
302 | // In OS/2 the dialog/panel doesn't really know it has a default | |
303 | // button, the default button simply has that style. We'll just | |
304 | // simulate by setting focus to it | |
305 | // | |
306 | pBtn->SetFocus(); | |
307 | } | |
308 | lStyle = ::WinQueryWindowULong(GetHwndOf(pBtn), QWL_STYLE); | |
309 | if (!(lStyle & BS_DEFAULT) == bOn) | |
987da0d4 | 310 | { |
987da0d4 DW |
311 | if ((lStyle & BS_USERBUTTON) != BS_USERBUTTON) |
312 | { | |
cfcebdb1 DW |
313 | if (bOn) |
314 | lStyle | BS_DEFAULT; | |
315 | else | |
316 | lStyle &= ~BS_DEFAULT; | |
317 | ::WinSetWindowULong(GetHwndOf(pBtn), QWL_STYLE, lStyle); | |
987da0d4 DW |
318 | } |
319 | else | |
320 | { | |
cfcebdb1 DW |
321 | // |
322 | // Redraw the button - it will notice itself that it's not the | |
987da0d4 | 323 | // default one any longer |
cfcebdb1 DW |
324 | // |
325 | pBtn->Refresh(); | |
430974f8 | 326 | } |
987da0d4 | 327 | } |
430974f8 | 328 | } // end of wxButton::UpdateDefaultStyle |
0e320a79 | 329 | |
d88de032 DW |
330 | // ---------------------------------------------------------------------------- |
331 | // event/message handlers | |
332 | // ---------------------------------------------------------------------------- | |
0e320a79 | 333 | |
987da0d4 DW |
334 | bool wxButton::OS2Command( |
335 | WXUINT uParam | |
336 | , WXWORD wId | |
337 | ) | |
0e320a79 | 338 | { |
987da0d4 DW |
339 | bool bProcessed = FALSE; |
340 | ||
341 | switch (uParam) | |
d88de032 | 342 | { |
987da0d4 DW |
343 | case BN_CLICKED: // normal buttons send this |
344 | case BN_DBLCLICKED: // owner-drawn ones also send this | |
345 | bProcessed = SendClickEvent(); | |
d88de032 DW |
346 | break; |
347 | } | |
987da0d4 DW |
348 | return bProcessed; |
349 | } // end of wxButton::OS2Command | |
350 | ||
351 | WXHBRUSH wxButton::OnCtlColor( | |
352 | WXHDC pDC | |
353 | , WXHWND pWnd | |
354 | , WXUINT nCtlColor | |
355 | , WXUINT uMessage | |
356 | , WXWPARAM wParam | |
357 | , WXLPARAM lParam | |
358 | ) | |
359 | { | |
360 | wxBrush* pBackgroundBrush = wxTheBrushList->FindOrCreateBrush( GetBackgroundColour() | |
361 | ,wxSOLID | |
362 | ); | |
363 | ||
364 | return (WXHBRUSH)pBackgroundBrush->GetResourceHandle(); | |
365 | } // end of wxButton::OnCtlColor | |
366 | ||
367 | void wxButton::MakeOwnerDrawn() | |
0e320a79 | 368 | { |
987da0d4 | 369 | long lStyle = 0L; |
d88de032 | 370 | |
987da0d4 DW |
371 | lStyle = ::WinQueryWindowULong(GetHwnd(), QWL_STYLE); |
372 | if ((lStyle & BS_USERBUTTON) != BS_USERBUTTON) | |
373 | { | |
374 | // | |
375 | // Make it so | |
376 | // | |
377 | lStyle |= BS_USERBUTTON; | |
378 | ::WinSetWindowULong(GetHwnd(), QWL_STYLE, lStyle); | |
379 | } | |
bc5a847c DW |
380 | } // end of wxButton::MakeOwnerDrawn |
381 | ||
382 | WXDWORD wxButton::OS2GetStyle( | |
383 | long lStyle | |
384 | , WXDWORD* pdwExstyle | |
385 | ) const | |
386 | { | |
387 | // | |
388 | // Buttons never have an external border, they draw their own one | |
389 | // | |
390 | WXDWORD dwStyle = wxControl::OS2GetStyle( (lStyle & ~wxBORDER_MASK) | wxBORDER_NONE | |
391 | ,pdwExstyle | |
392 | ); | |
393 | ||
394 | // | |
395 | // We must use WS_CLIPSIBLINGS with the buttons or they would draw over | |
396 | // each other in any resizeable dialog which has more than one button in | |
397 | // the bottom | |
398 | // | |
399 | dwStyle |= WS_CLIPSIBLINGS; | |
400 | return dwStyle; | |
401 | } // end of wxButton::OS2GetStyle | |
987da0d4 DW |
402 | |
403 | MRESULT wxButton::WindowProc( | |
404 | WXUINT uMsg | |
405 | , WXWPARAM wParam | |
406 | , WXLPARAM lParam | |
407 | ) | |
408 | { | |
409 | // | |
430974f8 DW |
410 | // When we receive focus, we want to temporary become the default button in |
411 | // our parent panel so that pressing "Enter" would activate us -- and when | |
412 | // losing it we should restore the previous default button as well | |
987da0d4 DW |
413 | // |
414 | if (uMsg == WM_SETFOCUS) | |
415 | { | |
430974f8 DW |
416 | if (SHORT1FROMMP(lParam) == TRUE) |
417 | SetTmpDefault(); | |
418 | else | |
419 | UnsetTmpDefault(); | |
987da0d4 DW |
420 | |
421 | // | |
422 | // Let the default processign take place too | |
423 | // | |
424 | } | |
425 | ||
426 | else if (uMsg == WM_BUTTON1DBLCLK) | |
427 | { | |
428 | // | |
429 | // Emulate a click event to force an owner-drawn button to change its | |
430 | // appearance - without this, it won't do it | |
431 | // | |
432 | (void)wxControl::OS2WindowProc( WM_BUTTON1DOWN | |
433 | ,wParam | |
434 | ,lParam | |
435 | ); | |
436 | ||
437 | // | |
438 | // And conitnue with processing the message normally as well | |
439 | // | |
440 | } | |
0e320a79 | 441 | |
987da0d4 DW |
442 | // |
443 | // Let the base class do all real processing | |
444 | // | |
445 | return (wxControl::OS2WindowProc( uMsg | |
446 | ,wParam | |
447 | ,lParam | |
448 | )); | |
bc5a847c | 449 | } // end of wxWindowProc |
d88de032 | 450 |