]>
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; | |
144 | ||
145 | GetTextExtent( rsLabel | |
146 | ,&nWidthButton | |
147 | ,NULL | |
148 | ); | |
149 | ||
150 | wxGetCharSize( GetHWND() | |
151 | ,&nWidthChar | |
152 | ,&nHeightChar | |
153 | ,(wxFont*)&GetFont() | |
154 | ); | |
155 | ||
156 | // | |
157 | // Add a margin - the button is wider than just its label | |
158 | // | |
159 | nWidthButton += 3 * nWidthChar; | |
160 | ||
161 | // | |
162 | // The button height is proportional to the height of the font used | |
163 | // | |
164 | int nHeightButton = BUTTON_HEIGHT_FROM_CHAR_HEIGHT(nHeightChar); | |
165 | ||
166 | // | |
167 | // Need a little extra to make it look right | |
168 | // | |
9923c37d | 169 | nHeightButton += (int)(nHeightChar/1.5); |
987da0d4 | 170 | |
97d74dd2 DW |
171 | if (!HasFlag(wxBU_EXACTFIT)) |
172 | { | |
173 | wxSize vSize = GetDefaultSize(); | |
987da0d4 | 174 | |
97d74dd2 DW |
175 | if (nWidthButton > vSize.x) |
176 | vSize.x = nWidthButton; | |
177 | if (nHeightButton > vSize.y) | |
178 | vSize.y = nHeightButton; | |
179 | return vSize; | |
180 | } | |
181 | return wxSize( nWidthButton | |
182 | ,nHeightButton | |
183 | ); | |
987da0d4 | 184 | } // end of wxButton::DoGetBestSize |
d88de032 DW |
185 | |
186 | /* static */ | |
187 | wxSize wxButton::GetDefaultSize() | |
188 | { | |
987da0d4 | 189 | static wxSize vSizeBtn; |
d88de032 | 190 | |
987da0d4 | 191 | if (vSizeBtn.x == 0) |
d88de032 | 192 | { |
987da0d4 | 193 | wxScreenDC vDc; |
d88de032 | 194 | |
a756f210 | 195 | vDc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)); |
987da0d4 DW |
196 | |
197 | // | |
198 | // The size of a standard button in the dialog units is 50x14, | |
d88de032 DW |
199 | // translate this to pixels |
200 | // NB1: the multipliers come from the Windows convention | |
201 | // NB2: the extra +1/+2 were needed to get the size be the same as the | |
202 | // size of the buttons in the standard dialog - I don't know how | |
203 | // this happens, but on my system this size is 75x23 in pixels and | |
204 | // 23*8 isn't even divisible by 14... Would be nice to understand | |
205 | // why these constants are needed though! | |
987da0d4 DW |
206 | vSizeBtn.x = (50 * (vDc.GetCharWidth() + 1))/4; |
207 | vSizeBtn.y = ((14 * vDc.GetCharHeight()) + 2)/8; | |
d88de032 | 208 | } |
987da0d4 DW |
209 | return vSizeBtn; |
210 | } // end of wxButton::GetDefaultSize | |
d88de032 | 211 | |
987da0d4 DW |
212 | void wxButton::Command ( |
213 | wxCommandEvent& rEvent | |
214 | ) | |
d88de032 | 215 | { |
987da0d4 DW |
216 | ProcessCommand (rEvent); |
217 | } // end of wxButton::Command | |
d88de032 DW |
218 | |
219 | // ---------------------------------------------------------------------------- | |
220 | // helpers | |
221 | // ---------------------------------------------------------------------------- | |
222 | ||
223 | bool wxButton::SendClickEvent() | |
224 | { | |
987da0d4 DW |
225 | wxCommandEvent vEvent( wxEVT_COMMAND_BUTTON_CLICKED |
226 | ,GetId() | |
227 | ); | |
d88de032 | 228 | |
987da0d4 DW |
229 | vEvent.SetEventObject(this); |
230 | return ProcessCommand(vEvent); | |
231 | } // end of wxButton::SendClickEvent | |
0e320a79 DW |
232 | |
233 | void wxButton::SetDefault() | |
234 | { | |
987da0d4 | 235 | wxWindow* pParent = GetParent(); |
987da0d4 | 236 | |
430974f8 DW |
237 | wxCHECK_RET( pParent, _T("button without parent?") ); |
238 | ||
239 | // | |
77ffb593 | 240 | // Set this one as the default button both for wxWidgets and Windows |
430974f8 DW |
241 | // |
242 | wxWindow* pWinOldDefault = pParent->SetDefaultItem(this); | |
cfcebdb1 DW |
243 | |
244 | SetDefaultStyle( wxDynamicCast(pWinOldDefault, wxButton) | |
245 | ,FALSE | |
246 | ); | |
247 | SetDefaultStyle( this | |
248 | ,TRUE | |
249 | ); | |
430974f8 DW |
250 | } // end of wxButton::SetDefault |
251 | ||
252 | void wxButton::SetTmpDefault() | |
253 | { | |
254 | wxWindow* pParent = GetParent(); | |
255 | ||
256 | wxCHECK_RET( pParent, _T("button without parent?") ); | |
257 | ||
258 | wxWindow* pWinOldDefault = pParent->GetDefaultItem(); | |
cfcebdb1 | 259 | |
430974f8 | 260 | pParent->SetTmpDefaultItem(this); |
cfcebdb1 DW |
261 | SetDefaultStyle( wxDynamicCast(pWinOldDefault, wxButton) |
262 | ,FALSE | |
263 | ); | |
264 | SetDefaultStyle( this | |
265 | ,TRUE | |
266 | ); | |
430974f8 DW |
267 | } // end of wxButton::SetTmpDefault |
268 | ||
269 | void wxButton::UnsetTmpDefault() | |
270 | { | |
271 | wxWindow* pParent = GetParent(); | |
272 | ||
273 | wxCHECK_RET( pParent, _T("button without parent?") ); | |
987da0d4 | 274 | |
430974f8 DW |
275 | pParent->SetTmpDefaultItem(NULL); |
276 | ||
277 | wxWindow* pWinOldDefault = pParent->GetDefaultItem(); | |
278 | ||
cfcebdb1 DW |
279 | SetDefaultStyle( this |
280 | ,FALSE | |
281 | ); | |
282 | SetDefaultStyle( wxDynamicCast(pWinOldDefault, wxButton) | |
283 | ,TRUE | |
284 | ); | |
430974f8 DW |
285 | } // end of wxButton::UnsetTmpDefault |
286 | ||
cfcebdb1 DW |
287 | void wxButton::SetDefaultStyle( |
288 | wxButton* pBtn | |
289 | , bool bOn | |
290 | ) | |
430974f8 | 291 | { |
430974f8 | 292 | long lStyle; |
cfcebdb1 DW |
293 | // |
294 | // We may be called with NULL pointer -- simpler to do the check here than | |
295 | // in the caller which does wxDynamicCast() | |
296 | // | |
297 | if (!pBtn) | |
298 | return; | |
430974f8 | 299 | |
cfcebdb1 DW |
300 | // |
301 | // First, let DefDlgProc() know about the new default button | |
302 | // | |
303 | if (bOn) | |
304 | { | |
305 | if (!wxTheApp->IsActive()) | |
306 | return; | |
307 | ||
308 | // | |
309 | // In OS/2 the dialog/panel doesn't really know it has a default | |
310 | // button, the default button simply has that style. We'll just | |
311 | // simulate by setting focus to it | |
312 | // | |
313 | pBtn->SetFocus(); | |
314 | } | |
315 | lStyle = ::WinQueryWindowULong(GetHwndOf(pBtn), QWL_STYLE); | |
316 | if (!(lStyle & BS_DEFAULT) == bOn) | |
987da0d4 | 317 | { |
987da0d4 DW |
318 | if ((lStyle & BS_USERBUTTON) != BS_USERBUTTON) |
319 | { | |
cfcebdb1 | 320 | if (bOn) |
d00110f6 | 321 | lStyle |= BS_DEFAULT; |
cfcebdb1 DW |
322 | else |
323 | lStyle &= ~BS_DEFAULT; | |
324 | ::WinSetWindowULong(GetHwndOf(pBtn), QWL_STYLE, lStyle); | |
987da0d4 DW |
325 | } |
326 | else | |
327 | { | |
cfcebdb1 DW |
328 | // |
329 | // Redraw the button - it will notice itself that it's not the | |
987da0d4 | 330 | // default one any longer |
cfcebdb1 DW |
331 | // |
332 | pBtn->Refresh(); | |
430974f8 | 333 | } |
987da0d4 | 334 | } |
430974f8 | 335 | } // end of wxButton::UpdateDefaultStyle |
0e320a79 | 336 | |
d88de032 DW |
337 | // ---------------------------------------------------------------------------- |
338 | // event/message handlers | |
339 | // ---------------------------------------------------------------------------- | |
0e320a79 | 340 | |
987da0d4 DW |
341 | bool wxButton::OS2Command( |
342 | WXUINT uParam | |
343 | , WXWORD wId | |
344 | ) | |
0e320a79 | 345 | { |
987da0d4 DW |
346 | bool bProcessed = FALSE; |
347 | ||
348 | switch (uParam) | |
d88de032 | 349 | { |
987da0d4 DW |
350 | case BN_CLICKED: // normal buttons send this |
351 | case BN_DBLCLICKED: // owner-drawn ones also send this | |
352 | bProcessed = SendClickEvent(); | |
d88de032 DW |
353 | break; |
354 | } | |
987da0d4 DW |
355 | return bProcessed; |
356 | } // end of wxButton::OS2Command | |
357 | ||
358 | WXHBRUSH wxButton::OnCtlColor( | |
359 | WXHDC pDC | |
360 | , WXHWND pWnd | |
361 | , WXUINT nCtlColor | |
362 | , WXUINT uMessage | |
363 | , WXWPARAM wParam | |
364 | , WXLPARAM lParam | |
365 | ) | |
366 | { | |
367 | wxBrush* pBackgroundBrush = wxTheBrushList->FindOrCreateBrush( GetBackgroundColour() | |
368 | ,wxSOLID | |
369 | ); | |
370 | ||
371 | return (WXHBRUSH)pBackgroundBrush->GetResourceHandle(); | |
372 | } // end of wxButton::OnCtlColor | |
373 | ||
374 | void wxButton::MakeOwnerDrawn() | |
0e320a79 | 375 | { |
987da0d4 | 376 | long lStyle = 0L; |
d88de032 | 377 | |
987da0d4 DW |
378 | lStyle = ::WinQueryWindowULong(GetHwnd(), QWL_STYLE); |
379 | if ((lStyle & BS_USERBUTTON) != BS_USERBUTTON) | |
380 | { | |
381 | // | |
382 | // Make it so | |
383 | // | |
384 | lStyle |= BS_USERBUTTON; | |
385 | ::WinSetWindowULong(GetHwnd(), QWL_STYLE, lStyle); | |
386 | } | |
bc5a847c DW |
387 | } // end of wxButton::MakeOwnerDrawn |
388 | ||
389 | WXDWORD wxButton::OS2GetStyle( | |
390 | long lStyle | |
391 | , WXDWORD* pdwExstyle | |
392 | ) const | |
393 | { | |
394 | // | |
395 | // Buttons never have an external border, they draw their own one | |
396 | // | |
397 | WXDWORD dwStyle = wxControl::OS2GetStyle( (lStyle & ~wxBORDER_MASK) | wxBORDER_NONE | |
398 | ,pdwExstyle | |
399 | ); | |
400 | ||
401 | // | |
402 | // We must use WS_CLIPSIBLINGS with the buttons or they would draw over | |
403 | // each other in any resizeable dialog which has more than one button in | |
404 | // the bottom | |
405 | // | |
406 | dwStyle |= WS_CLIPSIBLINGS; | |
407 | return dwStyle; | |
408 | } // end of wxButton::OS2GetStyle | |
987da0d4 DW |
409 | |
410 | MRESULT wxButton::WindowProc( | |
411 | WXUINT uMsg | |
412 | , WXWPARAM wParam | |
413 | , WXLPARAM lParam | |
414 | ) | |
415 | { | |
416 | // | |
430974f8 DW |
417 | // When we receive focus, we want to temporary become the default button in |
418 | // our parent panel so that pressing "Enter" would activate us -- and when | |
419 | // losing it we should restore the previous default button as well | |
987da0d4 DW |
420 | // |
421 | if (uMsg == WM_SETFOCUS) | |
422 | { | |
430974f8 DW |
423 | if (SHORT1FROMMP(lParam) == TRUE) |
424 | SetTmpDefault(); | |
425 | else | |
426 | UnsetTmpDefault(); | |
987da0d4 DW |
427 | |
428 | // | |
429 | // Let the default processign take place too | |
430 | // | |
431 | } | |
432 | ||
433 | else if (uMsg == WM_BUTTON1DBLCLK) | |
434 | { | |
435 | // | |
436 | // Emulate a click event to force an owner-drawn button to change its | |
437 | // appearance - without this, it won't do it | |
438 | // | |
439 | (void)wxControl::OS2WindowProc( WM_BUTTON1DOWN | |
440 | ,wParam | |
441 | ,lParam | |
442 | ); | |
443 | ||
444 | // | |
445 | // And conitnue with processing the message normally as well | |
446 | // | |
447 | } | |
0e320a79 | 448 | |
987da0d4 DW |
449 | // |
450 | // Let the base class do all real processing | |
451 | // | |
452 | return (wxControl::OS2WindowProc( uMsg | |
453 | ,wParam | |
454 | ,lParam | |
455 | )); | |
bc5a847c | 456 | } // end of wxWindowProc |
d88de032 | 457 |