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