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