]>
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 | |
16 | #include "wx/button.h" | |
17 | #include "wx/brush.h" | |
18 | #include "wx/panel.h" | |
19 | #include "wx/bmpbuttn.h" | |
20 | #include "wx/settings.h" | |
21 | #include "wx/dcscreen.h" | |
a4a16252 | 22 | #include "wx/scrolwin.h" |
0e320a79 DW |
23 | #endif |
24 | ||
d88de032 | 25 | #include "wx/os2/private.h" |
0e320a79 | 26 | |
987da0d4 DW |
27 | #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10) |
28 | ||
29 | // | |
30 | // Should be at the very least less than winDEFAULT_BUTTON_MARGIN | |
31 | // | |
32 | #define FOCUS_MARGIN 3 | |
33 | ||
34 | #ifndef BST_CHECKED | |
35 | #define BST_CHECKED 0x0001 | |
36 | #endif | |
37 | ||
0e320a79 | 38 | IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl) |
0e320a79 DW |
39 | |
40 | // Button | |
41 | ||
987da0d4 DW |
42 | bool wxButton::Create( |
43 | wxWindow* pParent | |
44 | , wxWindowID vId | |
45 | , const wxString& rsLabel | |
46 | , const wxPoint& rPos | |
47 | , const wxSize& rSize | |
48 | , long lStyle | |
5d4b632b | 49 | #if wxUSE_VALIDATORS |
987da0d4 | 50 | , const wxValidator& rValidator |
5d4b632b | 51 | #endif |
987da0d4 DW |
52 | , const wxString& rsName |
53 | ) | |
0e320a79 | 54 | { |
987da0d4 | 55 | SetName(rsName); |
5d4b632b | 56 | #if wxUSE_VALIDATORS |
987da0d4 | 57 | SetValidator(rValidator); |
5d4b632b | 58 | #endif |
987da0d4 DW |
59 | m_windowStyle = lStyle; |
60 | pParent->AddChild((wxButton *)this); | |
61 | if (vId == -1) | |
0e320a79 DW |
62 | m_windowId = NewControlId(); |
63 | else | |
987da0d4 DW |
64 | m_windowId = vId; |
65 | lStyle = WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON; | |
66 | ||
67 | // | |
68 | // OS/2 PM does not have Right/Left/Top/Bottom styles. | |
69 | // We will have to define an additional style when we implement notebooks | |
70 | // for a notebook page button | |
71 | // | |
72 | if (m_windowStyle & wxCLIP_SIBLINGS ) | |
73 | lStyle |= WS_CLIPSIBLINGS; | |
5d44b24e DW |
74 | // |
75 | // If the parent is a scrolled window the controls must | |
76 | // have this style or they will overlap the scrollbars | |
77 | // | |
78 | if (pParent) | |
79 | if (pParent->IsKindOf(CLASSINFO(wxScrolledWindow)) || | |
80 | pParent->IsKindOf(CLASSINFO(wxGenericScrolledWindow))) | |
81 | lStyle |= WS_CLIPSIBLINGS; | |
82 | ||
987da0d4 DW |
83 | m_hWnd = (WXHWND)::WinCreateWindow( GetHwndOf(pParent) // Parent handle |
84 | ,WC_BUTTON // A Button class window | |
85 | ,(PSZ)rsLabel.c_str() // Button text | |
86 | ,lStyle // Button style | |
87 | ,0, 0, 0, 0 // Location and size | |
88 | ,GetHwndOf(pParent) // Owner handle | |
89 | ,HWND_TOP // Top of Z-Order | |
90 | ,vId // Identifier | |
91 | ,NULL // No control data | |
92 | ,NULL // No Presentation parameters | |
93 | ); | |
94 | if (m_hWnd == 0) | |
95 | { | |
96 | return FALSE; | |
97 | } | |
0e320a79 | 98 | |
987da0d4 DW |
99 | // |
100 | // Subclass again for purposes of dialog editing mode | |
101 | // | |
102 | SubclassWin(m_hWnd); | |
2c1e8f2e DW |
103 | wxFont* pButtonFont = new wxFont( 8 |
104 | ,wxSWISS | |
105 | ,wxNORMAL | |
106 | ,wxNORMAL | |
107 | ); | |
108 | SetFont(*pButtonFont); | |
109 | SetXComp(0); | |
110 | SetYComp(0); | |
987da0d4 DW |
111 | SetSize( rPos.x |
112 | ,rPos.y | |
113 | ,rSize.x | |
114 | ,rSize.y | |
115 | ); | |
116 | return TRUE; | |
117 | } // end of wxButton::Create | |
0e320a79 | 118 | |
d88de032 | 119 | wxButton::~wxButton() |
0e320a79 | 120 | { |
987da0d4 DW |
121 | wxPanel* pPanel = wxDynamicCast(GetParent(), wxPanel); |
122 | ||
123 | if (pPanel) | |
d88de032 | 124 | { |
987da0d4 | 125 | if (pPanel->GetDefaultItem() == this) |
d88de032 | 126 | { |
987da0d4 DW |
127 | // |
128 | // Don't leave the panel with invalid default item | |
129 | // | |
130 | pPanel->SetDefaultItem(NULL); | |
d88de032 DW |
131 | } |
132 | } | |
987da0d4 | 133 | } // end of wxButton::~wxButton |
d88de032 DW |
134 | |
135 | // ---------------------------------------------------------------------------- | |
136 | // size management including autosizing | |
137 | // ---------------------------------------------------------------------------- | |
138 | ||
e78c4d50 | 139 | wxSize wxButton::DoGetBestSize() const |
d88de032 | 140 | { |
987da0d4 DW |
141 | wxString rsLabel = wxGetWindowText(GetHWND()); |
142 | int nWidthButton; | |
143 | int nWidthChar; | |
144 | int nHeightChar; | |
145 | ||
146 | GetTextExtent( rsLabel | |
147 | ,&nWidthButton | |
148 | ,NULL | |
149 | ); | |
150 | ||
151 | wxGetCharSize( GetHWND() | |
152 | ,&nWidthChar | |
153 | ,&nHeightChar | |
154 | ,(wxFont*)&GetFont() | |
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 | // | |
170 | nHeightButton += nHeightChar/1.5; | |
171 | ||
172 | wxSize vSize = GetDefaultSize(); | |
173 | ||
174 | if (nWidthButton > vSize.x) | |
175 | vSize.x = nWidthButton; | |
176 | if (nHeightButton > vSize.y) | |
177 | vSize.y = nHeightButton; | |
178 | return vSize; | |
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 DW |
230 | wxWindow* pParent = GetParent(); |
231 | wxButton* pBtnOldDefault = NULL; | |
232 | wxPanel* pPanel = wxDynamicCast(pParent, wxPanel); | |
233 | long lStyle = 0L; | |
234 | ||
235 | if (pParent) | |
236 | { | |
237 | wxWindow* pWinOldDefault = pParent->SetDefaultItem(this); | |
238 | ||
239 | pBtnOldDefault = wxDynamicCast(pWinOldDefault, wxButton); | |
240 | } | |
241 | if (pBtnOldDefault && pBtnOldDefault != this) | |
242 | { | |
243 | // | |
244 | // Remove the BS_DEFPUSHBUTTON style from the other button | |
245 | // | |
246 | lStyle = ::WinQueryWindowULong(GetHwndOf(pBtnOldDefault), QWL_STYLE); | |
247 | ||
248 | // | |
249 | // Don't do it with the owner drawn buttons because it will reset | |
250 | // BS_OWNERDRAW style bit too (BS_OWNERDRAW & BS_DEFPUSHBUTTON != 0)! | |
251 | // | |
252 | if ((lStyle & BS_USERBUTTON) != BS_USERBUTTON) | |
253 | { | |
254 | lStyle &= ~BS_DEFAULT; | |
255 | ::WinSetWindowULong(GetHwndOf(pBtnOldDefault), QWL_STYLE, lStyle); | |
256 | } | |
257 | else | |
258 | { | |
259 | // | |
260 | // Redraw the button - it will notice itself that it's not the | |
261 | // default one any longer | |
262 | // | |
263 | pBtnOldDefault->Refresh(); | |
264 | } | |
265 | } | |
0e320a79 | 266 | |
987da0d4 DW |
267 | // |
268 | // Set this button as the default | |
269 | // | |
270 | lStyle = ::WinQueryWindowULong(GetHwnd(), QWL_STYLE); | |
271 | if ((lStyle & BS_USERBUTTON) != BS_USERBUTTON) | |
272 | { | |
273 | lStyle != BS_DEFAULT; | |
274 | ::WinSetWindowULong(GetHwnd(), QWL_STYLE, lStyle); | |
275 | } | |
276 | } // end of wxButton::SetDefault | |
0e320a79 | 277 | |
d88de032 DW |
278 | // ---------------------------------------------------------------------------- |
279 | // event/message handlers | |
280 | // ---------------------------------------------------------------------------- | |
0e320a79 | 281 | |
987da0d4 DW |
282 | bool wxButton::OS2Command( |
283 | WXUINT uParam | |
284 | , WXWORD wId | |
285 | ) | |
0e320a79 | 286 | { |
987da0d4 DW |
287 | bool bProcessed = FALSE; |
288 | ||
289 | switch (uParam) | |
d88de032 | 290 | { |
987da0d4 DW |
291 | case BN_CLICKED: // normal buttons send this |
292 | case BN_DBLCLICKED: // owner-drawn ones also send this | |
293 | bProcessed = SendClickEvent(); | |
d88de032 DW |
294 | break; |
295 | } | |
987da0d4 DW |
296 | return bProcessed; |
297 | } // end of wxButton::OS2Command | |
298 | ||
299 | WXHBRUSH wxButton::OnCtlColor( | |
300 | WXHDC pDC | |
301 | , WXHWND pWnd | |
302 | , WXUINT nCtlColor | |
303 | , WXUINT uMessage | |
304 | , WXWPARAM wParam | |
305 | , WXLPARAM lParam | |
306 | ) | |
307 | { | |
308 | wxBrush* pBackgroundBrush = wxTheBrushList->FindOrCreateBrush( GetBackgroundColour() | |
309 | ,wxSOLID | |
310 | ); | |
311 | ||
312 | return (WXHBRUSH)pBackgroundBrush->GetResourceHandle(); | |
313 | } // end of wxButton::OnCtlColor | |
314 | ||
315 | void wxButton::MakeOwnerDrawn() | |
0e320a79 | 316 | { |
987da0d4 | 317 | long lStyle = 0L; |
d88de032 | 318 | |
987da0d4 DW |
319 | lStyle = ::WinQueryWindowULong(GetHwnd(), QWL_STYLE); |
320 | if ((lStyle & BS_USERBUTTON) != BS_USERBUTTON) | |
321 | { | |
322 | // | |
323 | // Make it so | |
324 | // | |
325 | lStyle |= BS_USERBUTTON; | |
326 | ::WinSetWindowULong(GetHwnd(), QWL_STYLE, lStyle); | |
327 | } | |
328 | } // end of wxCButton::MakeOwnerDrawn | |
329 | ||
330 | MRESULT wxButton::WindowProc( | |
331 | WXUINT uMsg | |
332 | , WXWPARAM wParam | |
333 | , WXLPARAM lParam | |
334 | ) | |
335 | { | |
336 | // | |
337 | // When we receive focus, we want to become the default button in our | |
338 | // parent panel | |
339 | // | |
340 | if (uMsg == WM_SETFOCUS) | |
341 | { | |
342 | SetDefault(); | |
343 | ||
344 | // | |
345 | // Let the default processign take place too | |
346 | // | |
347 | } | |
348 | ||
349 | else if (uMsg == WM_BUTTON1DBLCLK) | |
350 | { | |
351 | // | |
352 | // Emulate a click event to force an owner-drawn button to change its | |
353 | // appearance - without this, it won't do it | |
354 | // | |
355 | (void)wxControl::OS2WindowProc( WM_BUTTON1DOWN | |
356 | ,wParam | |
357 | ,lParam | |
358 | ); | |
359 | ||
360 | // | |
361 | // And conitnue with processing the message normally as well | |
362 | // | |
363 | } | |
0e320a79 | 364 | |
987da0d4 DW |
365 | // |
366 | // Let the base class do all real processing | |
367 | // | |
368 | return (wxControl::OS2WindowProc( uMsg | |
369 | ,wParam | |
370 | ,lParam | |
371 | )); | |
372 | } // end of wxW indowProc | |
d88de032 | 373 |