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