]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: radiobox.cpp | |
3 | // Purpose: wxRadioBox | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // =========================================================================== | |
13 | // declarations | |
14 | // =========================================================================== | |
15 | ||
16 | // --------------------------------------------------------------------------- | |
17 | // headers | |
18 | // --------------------------------------------------------------------------- | |
19 | ||
20 | #ifdef __GNUG__ | |
21 | #pragma implementation "radiobox.h" | |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
31 | #ifndef WX_PRECOMP | |
32 | #include <stdio.h> | |
33 | #include "wx/setup.h" | |
34 | #include "wx/bitmap.h" | |
35 | #include "wx/brush.h" | |
36 | #include "wx/radiobox.h" | |
37 | #endif | |
38 | ||
39 | #include "wx/msw/private.h" | |
40 | ||
41 | #if !USE_SHARED_LIBRARY | |
42 | IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl) | |
43 | #endif | |
44 | ||
45 | // --------------------------------------------------------------------------- | |
46 | // private functions | |
47 | // --------------------------------------------------------------------------- | |
48 | ||
49 | // get the id of the window | |
50 | #ifdef __WIN32__ | |
51 | #define GET_WIN_ID(hwnd) ::GetWindowLong((HWND)hwnd, GWL_ID) | |
52 | #else // Win16 | |
53 | #define GET_WIN_ID(hwnd) ::GetWindowWord((HWND)hwnd, GWW_ID) | |
54 | #endif // Win32/16 | |
55 | ||
56 | // wnd proc for radio buttons | |
57 | #ifdef __WIN32__ | |
58 | LRESULT APIENTRY _EXPORT wxRadioBtnWndProc(HWND hWnd, | |
59 | UINT message, | |
60 | WPARAM wParam, | |
61 | LPARAM lParam); | |
62 | #endif | |
63 | ||
64 | // --------------------------------------------------------------------------- | |
65 | // global vars | |
66 | // --------------------------------------------------------------------------- | |
67 | ||
68 | // the pointer to standard radio button wnd proc | |
69 | // static WNDPROC s_wndprocRadioBtn = (WNDPROC)NULL; | |
70 | static WXFARPROC s_wndprocRadioBtn = (WXFARPROC)NULL; | |
71 | ||
72 | // =========================================================================== | |
73 | // implementation | |
74 | // =========================================================================== | |
75 | ||
76 | // --------------------------------------------------------------------------- | |
77 | // wxRadioBox | |
78 | // --------------------------------------------------------------------------- | |
79 | ||
80 | int wxRadioBox::GetNumVer() const | |
81 | { | |
82 | if ( m_windowStyle & wxRA_SPECIFY_ROWS ) | |
83 | { | |
84 | return m_majorDim; | |
85 | } | |
86 | else | |
87 | { | |
88 | return (m_noItems + m_majorDim - 1)/m_majorDim; | |
89 | } | |
90 | } | |
91 | ||
92 | int wxRadioBox::GetNumHor() const | |
93 | { | |
94 | if ( m_windowStyle & wxRA_SPECIFY_ROWS ) | |
95 | { | |
96 | return (m_noItems + m_majorDim - 1)/m_majorDim; | |
97 | } | |
98 | else | |
99 | { | |
100 | return m_majorDim; | |
101 | } | |
102 | } | |
103 | ||
104 | bool wxRadioBox::MSWCommand(WXUINT param, WXWORD id) | |
105 | { | |
106 | if ( param == BN_CLICKED ) | |
107 | { | |
108 | int selectedButton = -1; | |
109 | ||
110 | for ( int i = 0; i < m_noItems; i++ ) | |
111 | { | |
112 | if ( id == GET_WIN_ID(m_radioButtons[i]) ) | |
113 | { | |
114 | selectedButton = i; | |
115 | ||
116 | break; | |
117 | } | |
118 | } | |
119 | ||
120 | wxASSERT_MSG( selectedButton != -1, "click from alien button?" ); | |
121 | ||
122 | if ( selectedButton != m_selectedButton ) | |
123 | { | |
124 | m_selectedButton = selectedButton; | |
125 | ||
126 | SendNotificationEvent(); | |
127 | } | |
128 | //else: don't generate events when the selection doesn't change | |
129 | ||
130 | return TRUE; | |
131 | } | |
132 | else | |
133 | return FALSE; | |
134 | } | |
135 | ||
136 | #if WXWIN_COMPATIBILITY | |
137 | wxRadioBox::wxRadioBox(wxWindow *parent, wxFunction func, const char *title, | |
138 | int x, int y, int width, int height, | |
139 | int n, char **choices, | |
140 | int majorDim, long style, const char *name) | |
141 | { | |
142 | wxString *choices2 = new wxString[n]; | |
143 | for ( int i = 0; i < n; i ++) choices2[i] = choices[i]; | |
144 | Create(parent, -1, title, wxPoint(x, y), wxSize(width, height), n, choices2, majorDim, style, | |
145 | wxDefaultValidator, name); | |
146 | Callback(func); | |
147 | delete choices2; | |
148 | } | |
149 | ||
150 | #endif | |
151 | ||
152 | // Radio box item | |
153 | wxRadioBox::wxRadioBox() | |
154 | { | |
155 | m_selectedButton = -1; | |
156 | m_noItems = 0; | |
157 | m_noRowsOrCols = 0; | |
158 | m_radioButtons = NULL; | |
159 | m_majorDim = 0; | |
160 | m_radioWidth = NULL; | |
161 | m_radioHeight = NULL; | |
162 | } | |
163 | ||
164 | bool wxRadioBox::Create(wxWindow *parent, wxWindowID id, const wxString& title, | |
165 | const wxPoint& pos, const wxSize& size, | |
166 | int n, const wxString choices[], | |
167 | int majorDim, long style, | |
168 | const wxValidator& val, const wxString& name) | |
169 | { | |
170 | m_selectedButton = -1; | |
171 | m_noItems = n; | |
172 | ||
173 | SetName(name); | |
174 | SetValidator(val); | |
175 | ||
176 | parent->AddChild(this); | |
177 | m_backgroundColour = parent->GetBackgroundColour(); | |
178 | m_foregroundColour = parent->GetForegroundColour(); | |
179 | ||
180 | m_windowStyle = (long&)style; | |
181 | ||
182 | int x = pos.x; | |
183 | int y = pos.y; | |
184 | int width = size.x; | |
185 | int height = size.y; | |
186 | ||
187 | if (id == -1) | |
188 | m_windowId = NewControlId(); | |
189 | else | |
190 | m_windowId = id; | |
191 | ||
192 | if ( majorDim == 0 ) | |
193 | m_majorDim = n; | |
194 | else | |
195 | m_majorDim = majorDim; | |
196 | m_noRowsOrCols = majorDim; | |
197 | ||
198 | long msStyle = GROUP_FLAGS; | |
199 | ||
200 | bool want3D; | |
201 | WXDWORD exStyle = Determine3DEffects(0, &want3D); | |
202 | // Even with extended styles, need to combine with WS_BORDER | |
203 | // for them to look right. | |
204 | /* | |
205 | if ( want3D || wxStyleHasBorder(m_windowStyle) ) | |
206 | msStyle |= WS_BORDER; | |
207 | */ | |
208 | ||
209 | HWND hwndParent = (HWND)parent->GetHWND(); | |
210 | ||
211 | m_hWnd = (WXHWND)::CreateWindowEx | |
212 | ( | |
213 | (DWORD)exStyle, | |
214 | GROUP_CLASS, | |
215 | title, | |
216 | msStyle, | |
217 | 0, 0, 0, 0, | |
218 | hwndParent, | |
219 | (HMENU)m_windowId, | |
220 | wxGetInstance(), | |
221 | NULL | |
222 | ); | |
223 | ||
224 | #if wxUSE_CTL3D | |
225 | if (want3D) | |
226 | { | |
227 | Ctl3dSubclassCtl((HWND)m_hWnd); | |
228 | m_useCtl3D = TRUE; | |
229 | } | |
230 | #endif // wxUSE_CTL3D | |
231 | ||
232 | SetFont(parent->GetFont()); | |
233 | ||
234 | SubclassWin(m_hWnd); | |
235 | ||
236 | // Some radio boxes test consecutive id. | |
237 | (void)NewControlId(); | |
238 | m_radioButtons = new WXHWND[n]; | |
239 | m_radioWidth = new int[n]; | |
240 | m_radioHeight = new int[n]; | |
241 | int i; | |
242 | for (i = 0; i < n; i++) | |
243 | { | |
244 | m_radioWidth[i] = m_radioHeight[i] = -1; | |
245 | long groupStyle = 0; | |
246 | if ( i == 0 && style == 0 ) | |
247 | groupStyle = WS_GROUP; | |
248 | long newId = NewControlId(); | |
249 | long msStyle = groupStyle | RADIO_FLAGS; | |
250 | ||
251 | HWND hwndBtn = CreateWindowEx(exStyle, RADIO_CLASS, | |
252 | choices[i], msStyle, | |
253 | 0,0,0,0, | |
254 | hwndParent, | |
255 | (HMENU)newId, wxGetInstance(), | |
256 | NULL); | |
257 | ||
258 | m_radioButtons[i] = (WXHWND)hwndBtn; | |
259 | SubclassRadioButton((WXHWND)hwndBtn); | |
260 | ||
261 | wxFont& font = GetFont(); | |
262 | if ( font.Ok() ) | |
263 | { | |
264 | SendMessage(hwndBtn, WM_SETFONT, | |
265 | (WPARAM)font.GetResourceHandle(), 0L); | |
266 | } | |
267 | ||
268 | m_subControls.Append((wxObject *)newId); | |
269 | } | |
270 | ||
271 | // Create a dummy radio control to end the group. | |
272 | (void)CreateWindowEx(0, RADIO_CLASS, "", WS_GROUP | RADIO_FLAGS, | |
273 | 0, 0, 0, 0, hwndParent, | |
274 | (HMENU)NewControlId(), wxGetInstance(), NULL); | |
275 | ||
276 | SetSelection(0); | |
277 | ||
278 | SetSize(x, y, width, height); | |
279 | ||
280 | return TRUE; | |
281 | } | |
282 | ||
283 | wxRadioBox::~wxRadioBox() | |
284 | { | |
285 | m_isBeingDeleted = TRUE; | |
286 | ||
287 | if (m_radioButtons) | |
288 | { | |
289 | int i; | |
290 | for (i = 0; i < m_noItems; i++) | |
291 | DestroyWindow((HWND) m_radioButtons[i]); | |
292 | delete[] m_radioButtons; | |
293 | } | |
294 | if (m_radioWidth) | |
295 | delete[] m_radioWidth; | |
296 | if (m_radioHeight) | |
297 | delete[] m_radioHeight; | |
298 | if (m_hWnd) | |
299 | ::DestroyWindow((HWND) m_hWnd); | |
300 | m_hWnd = 0; | |
301 | ||
302 | } | |
303 | ||
304 | wxString wxRadioBox::GetLabel(int item) const | |
305 | { | |
306 | GetWindowText((HWND)m_radioButtons[item], wxBuffer, 300); | |
307 | return wxString(wxBuffer); | |
308 | } | |
309 | ||
310 | void wxRadioBox::SetLabel(int item, const wxString& label) | |
311 | { | |
312 | m_radioWidth[item] = m_radioHeight[item] = -1; | |
313 | SetWindowText((HWND)m_radioButtons[item], (const char *)label); | |
314 | } | |
315 | ||
316 | void wxRadioBox::SetLabel(int item, wxBitmap *bitmap) | |
317 | { | |
318 | /* | |
319 | m_radioWidth[item] = bitmap->GetWidth() + FB_MARGIN; | |
320 | m_radioHeight[item] = bitmap->GetHeight() + FB_MARGIN; | |
321 | */ | |
322 | } | |
323 | ||
324 | int wxRadioBox::FindString(const wxString& s) const | |
325 | { | |
326 | int i; | |
327 | for (i = 0; i < m_noItems; i++) | |
328 | { | |
329 | GetWindowText((HWND) m_radioButtons[i], wxBuffer, 1000); | |
330 | if (s == wxBuffer) | |
331 | return i; | |
332 | } | |
333 | return -1; | |
334 | } | |
335 | ||
336 | void wxRadioBox::SetSelection(int N) | |
337 | { | |
338 | wxCHECK_RET( (N >= 0) && (N < m_noItems), "invalid radiobox index" ); | |
339 | ||
340 | // Following necessary for Win32s, because Win32s translate BM_SETCHECK | |
341 | if (m_selectedButton >= 0 && m_selectedButton < m_noItems) | |
342 | ::SendMessage((HWND) m_radioButtons[m_selectedButton], BM_SETCHECK, 0, 0L); | |
343 | ||
344 | ::SendMessage((HWND)m_radioButtons[N], BM_SETCHECK, 1, 0L); | |
345 | ::SetFocus((HWND)m_radioButtons[N]); | |
346 | ||
347 | m_selectedButton = N; | |
348 | } | |
349 | ||
350 | // Get single selection, for single choice list items | |
351 | int wxRadioBox::GetSelection() const | |
352 | { | |
353 | return m_selectedButton; | |
354 | } | |
355 | ||
356 | // Find string for position | |
357 | wxString wxRadioBox::GetString(int N) const | |
358 | { | |
359 | return wxGetWindowText(m_radioButtons[N]); | |
360 | } | |
361 | ||
362 | // Restored old code. | |
363 | void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags) | |
364 | { | |
365 | int currentX, currentY; | |
366 | GetPosition(¤tX, ¤tY); | |
367 | int xx = x; | |
368 | int yy = y; | |
369 | ||
370 | if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) | |
371 | xx = currentX; | |
372 | if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) | |
373 | yy = currentY; | |
374 | ||
375 | char buf[400]; | |
376 | ||
377 | int y_offset = yy; | |
378 | int x_offset = xx; | |
379 | int current_width, cyf; | |
380 | ||
381 | int cx1,cy1; | |
382 | wxGetCharSize(m_hWnd, &cx1, &cy1, & GetFont()); | |
383 | // Attempt to have a look coherent with other platforms: | |
384 | // We compute the biggest toggle dim, then we align all | |
385 | // items according this value. | |
386 | int maxWidth = -1; | |
387 | int maxHeight = -1; | |
388 | ||
389 | int i; | |
390 | for (i = 0 ; i < m_noItems; i++) | |
391 | { | |
392 | int eachWidth; | |
393 | int eachHeight; | |
394 | if (m_radioWidth[i]<0) | |
395 | { | |
396 | // It's a labelled toggle | |
397 | GetWindowText((HWND) m_radioButtons[i], buf, 300); | |
398 | GetTextExtent(buf, ¤t_width, &cyf,NULL,NULL, & GetFont()); | |
399 | eachWidth = (int)(current_width + RADIO_SIZE); | |
400 | eachHeight = (int)((3*cyf)/2); | |
401 | } | |
402 | else | |
403 | { | |
404 | eachWidth = m_radioWidth[i]; | |
405 | eachHeight = m_radioHeight[i]; | |
406 | } | |
407 | if (maxWidth<eachWidth) maxWidth = eachWidth; | |
408 | if (maxHeight<eachHeight) maxHeight = eachHeight; | |
409 | } | |
410 | ||
411 | if (m_hWnd) | |
412 | { | |
413 | int totWidth; | |
414 | int totHeight; | |
415 | ||
416 | int nbHor = GetNumHor(), | |
417 | nbVer = GetNumVer(); | |
418 | ||
419 | // this formula works, but I don't know why. | |
420 | // Please, be sure what you do if you modify it!! | |
421 | if (m_radioWidth[0]<0) | |
422 | totHeight = (nbVer * maxHeight) + cy1/2; | |
423 | else | |
424 | totHeight = nbVer * (maxHeight+cy1/2); | |
425 | totWidth = nbHor * (maxWidth+cx1); | |
426 | ||
427 | #if (!CTL3D) | |
428 | // Requires a bigger group box in plain Windows | |
429 | MoveWindow((HWND) m_hWnd,x_offset,y_offset,totWidth+cx1,totHeight+(3*cy1)/2,TRUE); | |
430 | #else | |
431 | MoveWindow((HWND) m_hWnd,x_offset,y_offset,totWidth+cx1,totHeight+cy1,TRUE); | |
432 | #endif | |
433 | x_offset += cx1; | |
434 | y_offset += cy1; | |
435 | } | |
436 | ||
437 | #if (!CTL3D) | |
438 | y_offset += (int)(cy1/2); // Fudge factor since buttons overlapped label | |
439 | // JACS 2/12/93. CTL3D draws group label quite high. | |
440 | #endif | |
441 | int startX = x_offset; | |
442 | int startY = y_offset; | |
443 | ||
444 | for ( i = 0 ; i < m_noItems; i++) | |
445 | { | |
446 | // Bidimensional radio adjustment | |
447 | if (i&&((i%m_majorDim)==0)) // Why is this omitted for i = 0? | |
448 | { | |
449 | if (m_windowStyle & wxRA_VERTICAL) | |
450 | { | |
451 | y_offset = startY; | |
452 | x_offset += maxWidth + cx1; | |
453 | } | |
454 | else | |
455 | { | |
456 | x_offset = startX; | |
457 | y_offset += maxHeight; | |
458 | if (m_radioWidth[0]>0) | |
459 | y_offset += cy1/2; | |
460 | } | |
461 | } | |
462 | int eachWidth; | |
463 | int eachHeight; | |
464 | if (m_radioWidth[i]<0) | |
465 | { | |
466 | // It's a labeled item | |
467 | GetWindowText((HWND) m_radioButtons[i], buf, 300); | |
468 | GetTextExtent(buf, ¤t_width, &cyf,NULL,NULL, & GetFont()); | |
469 | ||
470 | // How do we find out radio button bitmap size!! | |
471 | // By adjusting them carefully, manually :-) | |
472 | eachWidth = (int)(current_width + RADIO_SIZE); | |
473 | eachHeight = (int)((3*cyf)/2); | |
474 | } | |
475 | else | |
476 | { | |
477 | eachWidth = m_radioWidth[i]; | |
478 | eachHeight = m_radioHeight[i]; | |
479 | } | |
480 | ||
481 | MoveWindow((HWND) m_radioButtons[i],x_offset,y_offset,eachWidth,eachHeight,TRUE); | |
482 | if (m_windowStyle & wxRA_SPECIFY_ROWS) | |
483 | { | |
484 | y_offset += maxHeight; | |
485 | if (m_radioWidth[0]>0) | |
486 | y_offset += cy1/2; | |
487 | } | |
488 | else | |
489 | x_offset += maxWidth + cx1; | |
490 | } | |
491 | } | |
492 | ||
493 | ||
494 | void wxRadioBox::GetSize(int *width, int *height) const | |
495 | { | |
496 | RECT rect; | |
497 | rect.left = -1; rect.right = -1; rect.top = -1; rect.bottom = -1; | |
498 | ||
499 | if (m_hWnd) | |
500 | wxFindMaxSize(m_hWnd, &rect); | |
501 | ||
502 | int i; | |
503 | for (i = 0; i < m_noItems; i++) | |
504 | wxFindMaxSize(m_radioButtons[i], &rect); | |
505 | ||
506 | *width = rect.right - rect.left; | |
507 | *height = rect.bottom - rect.top; | |
508 | } | |
509 | ||
510 | void wxRadioBox::GetPosition(int *x, int *y) const | |
511 | { | |
512 | wxWindow *parent = GetParent(); | |
513 | RECT rect; | |
514 | rect.left = -1; rect.right = -1; rect.top = -1; rect.bottom = -1; | |
515 | ||
516 | int i; | |
517 | for (i = 0; i < m_noItems; i++) | |
518 | wxFindMaxSize(m_radioButtons[i], &rect); | |
519 | ||
520 | if (m_hWnd) | |
521 | wxFindMaxSize(m_hWnd, &rect); | |
522 | ||
523 | // Since we now have the absolute screen coords, | |
524 | // if there's a parent we must subtract its top left corner | |
525 | POINT point; | |
526 | point.x = rect.left; | |
527 | point.y = rect.top; | |
528 | if (parent) | |
529 | { | |
530 | ::ScreenToClient((HWND) parent->GetHWND(), &point); | |
531 | } | |
532 | // We may be faking the client origin. | |
533 | // So a window that's really at (0, 30) may appear | |
534 | // (to wxWin apps) to be at (0, 0). | |
535 | if (GetParent()) | |
536 | { | |
537 | wxPoint pt(GetParent()->GetClientAreaOrigin()); | |
538 | point.x -= pt.x; | |
539 | point.y -= pt.y; | |
540 | } | |
541 | ||
542 | *x = point.x; | |
543 | *y = point.y; | |
544 | } | |
545 | ||
546 | wxString wxRadioBox::GetLabel() const | |
547 | { | |
548 | if (m_hWnd) | |
549 | { | |
550 | GetWindowText((HWND) m_hWnd, wxBuffer, 300); | |
551 | return wxString(wxBuffer); | |
552 | } | |
553 | else return wxString(""); | |
554 | } | |
555 | ||
556 | void wxRadioBox::SetLabel(const wxString& label) | |
557 | { | |
558 | if (m_hWnd) | |
559 | SetWindowText((HWND) m_hWnd, label); | |
560 | } | |
561 | ||
562 | void wxRadioBox::SetFocus() | |
563 | { | |
564 | if (m_noItems > 0) | |
565 | { | |
566 | if (m_selectedButton == -1) | |
567 | ::SetFocus((HWND) m_radioButtons[0]); | |
568 | else | |
569 | ::SetFocus((HWND) m_radioButtons[m_selectedButton]); | |
570 | } | |
571 | ||
572 | } | |
573 | ||
574 | bool wxRadioBox::Show(bool show) | |
575 | { | |
576 | m_isShown = show; | |
577 | int cshow; | |
578 | if (show) | |
579 | cshow = SW_SHOW; | |
580 | else | |
581 | cshow = SW_HIDE; | |
582 | if (m_hWnd) | |
583 | ShowWindow((HWND) m_hWnd, cshow); | |
584 | int i; | |
585 | for (i = 0; i < m_noItems; i++) | |
586 | ShowWindow((HWND) m_radioButtons[i], cshow); | |
587 | return TRUE; | |
588 | } | |
589 | ||
590 | // Enable a specific button | |
591 | void wxRadioBox::Enable(int item, bool enable) | |
592 | { | |
593 | if (item<0) | |
594 | wxWindow::Enable(enable); | |
595 | else if (item < m_noItems) | |
596 | ::EnableWindow((HWND) m_radioButtons[item], enable); | |
597 | } | |
598 | ||
599 | // Enable all controls | |
600 | void wxRadioBox::Enable(bool enable) | |
601 | { | |
602 | wxControl::Enable(enable); | |
603 | ||
604 | int i; | |
605 | for (i = 0; i < m_noItems; i++) | |
606 | ::EnableWindow((HWND) m_radioButtons[i], enable); | |
607 | } | |
608 | ||
609 | // Show a specific button | |
610 | void wxRadioBox::Show(int item, bool show) | |
611 | { | |
612 | if (item<0) | |
613 | wxRadioBox::Show(show); | |
614 | else if (item < m_noItems) | |
615 | { | |
616 | int cshow; | |
617 | if (show) | |
618 | cshow = SW_SHOW; | |
619 | else | |
620 | cshow = SW_HIDE; | |
621 | ShowWindow((HWND) m_radioButtons[item], cshow); | |
622 | } | |
623 | } | |
624 | ||
625 | WXHBRUSH wxRadioBox::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor, | |
626 | WXUINT message, WXWPARAM wParam, WXLPARAM lParam) | |
627 | { | |
628 | #if wxUSE_CTL3D | |
629 | if ( m_useCtl3D ) | |
630 | { | |
631 | HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam); | |
632 | return (WXHBRUSH) hbrush; | |
633 | } | |
634 | #endif | |
635 | ||
636 | if (GetParent()->GetTransparentBackground()) | |
637 | SetBkMode((HDC) pDC, TRANSPARENT); | |
638 | else | |
639 | SetBkMode((HDC) pDC, OPAQUE); | |
640 | ||
641 | ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue())); | |
642 | ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue())); | |
643 | ||
644 | wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID); | |
645 | ||
646 | // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush | |
647 | // has a zero usage count. | |
648 | // backgroundBrush->RealizeResource(); | |
649 | return (WXHBRUSH) backgroundBrush->GetResourceHandle(); | |
650 | } | |
651 | ||
652 | // For single selection items only | |
653 | wxString wxRadioBox::GetStringSelection() const | |
654 | { | |
655 | wxString result; | |
656 | int sel = GetSelection(); | |
657 | if (sel > -1) | |
658 | result = GetString(sel); | |
659 | ||
660 | return result; | |
661 | } | |
662 | ||
663 | bool wxRadioBox::SetStringSelection(const wxString& s) | |
664 | { | |
665 | int sel = FindString (s); | |
666 | if (sel > -1) | |
667 | { | |
668 | SetSelection (sel); | |
669 | return TRUE; | |
670 | } | |
671 | else | |
672 | return FALSE; | |
673 | } | |
674 | ||
675 | bool wxRadioBox::ContainsHWND(WXHWND hWnd) const | |
676 | { | |
677 | int i; | |
678 | for (i = 0; i < Number(); i++) | |
679 | if (GetRadioButtons()[i] == hWnd) | |
680 | return TRUE; | |
681 | return FALSE; | |
682 | } | |
683 | ||
684 | void wxRadioBox::Command (wxCommandEvent & event) | |
685 | { | |
686 | SetSelection (event.m_commandInt); | |
687 | ProcessCommand (event); | |
688 | } | |
689 | ||
690 | long wxRadioBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) | |
691 | { | |
692 | if (nMsg == WM_NCHITTEST) | |
693 | { | |
694 | int xPos = LOWORD(lParam); // horizontal position of cursor | |
695 | int yPos = HIWORD(lParam); // vertical position of cursor | |
696 | ||
697 | ScreenToClient(&xPos, &yPos); | |
698 | ||
699 | // Make sure you can drag by the top of the groupbox, but let | |
700 | // other (enclosed) controls get mouse events also | |
701 | if (yPos < 10) | |
702 | return (long)HTCLIENT; | |
703 | } | |
704 | ||
705 | return wxControl::MSWWindowProc(nMsg, wParam, lParam); | |
706 | } | |
707 | ||
708 | void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn) | |
709 | { | |
710 | HWND hwndBtn = (HWND)hWndBtn; | |
711 | ||
712 | if ( !s_wndprocRadioBtn ) | |
713 | s_wndprocRadioBtn = (WXFARPROC)::GetWindowLong(hwndBtn, GWL_WNDPROC); | |
714 | ||
715 | // No GWL_USERDATA in Win16, so omit this subclassing. | |
716 | #ifdef __WIN32__ | |
717 | ::SetWindowLong(hwndBtn, GWL_WNDPROC, (long)wxRadioBtnWndProc); | |
718 | ::SetWindowLong(hwndBtn, GWL_USERDATA, (long)this); | |
719 | #endif | |
720 | } | |
721 | ||
722 | void wxRadioBox::SendNotificationEvent() | |
723 | { | |
724 | wxCommandEvent event(wxEVT_COMMAND_RADIOBOX_SELECTED, m_windowId); | |
725 | event.SetInt( m_selectedButton ); | |
726 | event.SetEventObject( this ); | |
727 | ProcessCommand(event); | |
728 | } | |
729 | ||
730 | // --------------------------------------------------------------------------- | |
731 | // window proc for radio buttons | |
732 | // --------------------------------------------------------------------------- | |
733 | ||
734 | #ifdef __WIN32__ | |
735 | ||
736 | LRESULT APIENTRY _EXPORT wxRadioBtnWndProc(HWND hwnd, | |
737 | UINT msg, | |
738 | WPARAM wParam, | |
739 | LPARAM lParam) | |
740 | { | |
741 | bool processed = TRUE; | |
742 | if ( msg != WM_KEYDOWN ) | |
743 | processed = FALSE; | |
744 | ||
745 | if ( processed ) | |
746 | { | |
747 | wxRadioBox *radiobox = (wxRadioBox *)::GetWindowLong(hwnd, GWL_USERDATA); | |
748 | ||
749 | wxCHECK_MSG( radiobox, 0, "radio button without radio box?" ); | |
750 | ||
751 | int sel = radiobox->GetSelection(); | |
752 | ||
753 | switch ( wParam ) | |
754 | { | |
755 | case VK_UP: | |
756 | sel--; | |
757 | break; | |
758 | ||
759 | case VK_LEFT: | |
760 | sel -= radiobox->GetNumVer(); | |
761 | break; | |
762 | ||
763 | case VK_DOWN: | |
764 | sel++; | |
765 | break; | |
766 | ||
767 | case VK_RIGHT: | |
768 | sel += radiobox->GetNumVer(); | |
769 | break; | |
770 | ||
771 | case VK_TAB: | |
772 | { | |
773 | wxNavigationKeyEvent event; | |
774 | event.SetDirection(!(::GetKeyState(VK_SHIFT) & 0x100)); | |
775 | event.SetWindowChange(FALSE); | |
776 | event.SetEventObject(radiobox); | |
777 | ||
778 | if ( radiobox->GetEventHandler()->ProcessEvent(event) ) | |
779 | return 0; | |
780 | } | |
781 | // fall through | |
782 | ||
783 | default: | |
784 | processed = FALSE; | |
785 | } | |
786 | ||
787 | if ( processed ) | |
788 | { | |
789 | if ( sel >= 0 && sel < radiobox->Number() ) | |
790 | { | |
791 | radiobox->SetSelection(sel); | |
792 | ||
793 | // emulate the button click | |
794 | radiobox->SendNotificationEvent(); | |
795 | } | |
796 | } | |
797 | } | |
798 | ||
799 | if ( !processed ) | |
800 | return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd, msg, wParam, lParam); | |
801 | else | |
802 | return 0; | |
803 | } | |
804 | #endif | |
805 |