]>
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 "wx/bitmap.h" | |
33 | #include "wx/brush.h" | |
34 | #include "wx/radiobox.h" | |
35 | #include "wx/log.h" | |
36 | #endif | |
37 | ||
38 | #include "wx/msw/private.h" | |
39 | ||
40 | #if wxUSE_TOOLTIPS | |
41 | #ifndef __GNUWIN32_OLD__ | |
42 | #include <commctrl.h> | |
43 | #endif | |
44 | #include "wx/tooltip.h" | |
45 | #endif // wxUSE_TOOLTIPS | |
46 | ||
47 | IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl) | |
48 | ||
49 | // there are two possible ways to create the radio buttons: either as children | |
50 | // of the radiobox or as siblings of it - allow playing with both variants for | |
51 | // now, eventually we will choose the best one for our purposes | |
52 | // | |
53 | // two main problems are the keyboard navigation inside the radiobox (arrows | |
54 | // should switch between buttons, not pass focus to the next control) and the | |
55 | // tooltips - a tooltip is associated with the radiobox itself, not the | |
56 | // children... | |
57 | // | |
58 | // the problems with setting this to 1: | |
59 | // a) Alt-<mnemonic of radiobox> isn't handled properly by IsDialogMessage() | |
60 | // because it sets focus to the next control accepting it which is not a | |
61 | // radio button but a radiobox sibling in this case - the only solution to | |
62 | // this would be to handle Alt-<mnemonic> ourselves | |
63 | // b) the problems with setting radiobox colours under Win98/2K were reported | |
64 | // but I couldn't reproduce it so I have no idea about what causes it | |
65 | // | |
66 | // the problems with setting this to 0: | |
67 | // a) the tooltips are not shown for the radiobox - possible solution: make | |
68 | // TTM_WINDOWFROMPOS handling code in msw/tooltip.cpp work (easier said than | |
69 | // done because I don't know why it doesn't work) | |
70 | #define RADIOBTN_PARENT_IS_RADIOBOX 0 | |
71 | ||
72 | // --------------------------------------------------------------------------- | |
73 | // private functions | |
74 | // --------------------------------------------------------------------------- | |
75 | ||
76 | // wnd proc for radio buttons | |
77 | #ifdef __WIN32__ | |
78 | LRESULT APIENTRY _EXPORT wxRadioBtnWndProc(HWND hWnd, | |
79 | UINT message, | |
80 | WPARAM wParam, | |
81 | LPARAM lParam); | |
82 | ||
83 | // --------------------------------------------------------------------------- | |
84 | // global vars | |
85 | // --------------------------------------------------------------------------- | |
86 | ||
87 | // the pointer to standard radio button wnd proc | |
88 | static WXFARPROC s_wndprocRadioBtn = (WXFARPROC)NULL; | |
89 | ||
90 | #endif // __WIN32__ | |
91 | ||
92 | // =========================================================================== | |
93 | // implementation | |
94 | // =========================================================================== | |
95 | ||
96 | // --------------------------------------------------------------------------- | |
97 | // wxRadioBox | |
98 | // --------------------------------------------------------------------------- | |
99 | ||
100 | int wxRadioBox::GetNumVer() const | |
101 | { | |
102 | if ( m_windowStyle & wxRA_SPECIFY_ROWS ) | |
103 | { | |
104 | return m_majorDim; | |
105 | } | |
106 | else | |
107 | { | |
108 | return (m_noItems + m_majorDim - 1)/m_majorDim; | |
109 | } | |
110 | } | |
111 | ||
112 | int wxRadioBox::GetNumHor() const | |
113 | { | |
114 | if ( m_windowStyle & wxRA_SPECIFY_ROWS ) | |
115 | { | |
116 | return (m_noItems + m_majorDim - 1)/m_majorDim; | |
117 | } | |
118 | else | |
119 | { | |
120 | return m_majorDim; | |
121 | } | |
122 | } | |
123 | ||
124 | bool wxRadioBox::MSWCommand(WXUINT cmd, WXWORD id) | |
125 | { | |
126 | if ( cmd == BN_CLICKED ) | |
127 | { | |
128 | int selectedButton = -1; | |
129 | ||
130 | for ( int i = 0; i < m_noItems; i++ ) | |
131 | { | |
132 | if ( id == wxGetWindowId(m_radioButtons[i]) ) | |
133 | { | |
134 | selectedButton = i; | |
135 | ||
136 | break; | |
137 | } | |
138 | } | |
139 | ||
140 | wxASSERT_MSG( selectedButton != -1, wxT("click from alien button?") ); | |
141 | ||
142 | if ( selectedButton != m_selectedButton ) | |
143 | { | |
144 | m_selectedButton = selectedButton; | |
145 | ||
146 | SendNotificationEvent(); | |
147 | } | |
148 | //else: don't generate events when the selection doesn't change | |
149 | ||
150 | return TRUE; | |
151 | } | |
152 | else | |
153 | return FALSE; | |
154 | } | |
155 | ||
156 | #if WXWIN_COMPATIBILITY | |
157 | wxRadioBox::wxRadioBox(wxWindow *parent, wxFunction func, const char *title, | |
158 | int x, int y, int width, int height, | |
159 | int n, char **choices, | |
160 | int majorDim, long style, const char *name) | |
161 | { | |
162 | wxString *choices2 = new wxString[n]; | |
163 | for ( int i = 0; i < n; i ++) choices2[i] = choices[i]; | |
164 | Create(parent, -1, title, wxPoint(x, y), wxSize(width, height), n, choices2, majorDim, style, | |
165 | wxDefaultValidator, name); | |
166 | Callback(func); | |
167 | delete choices2; | |
168 | } | |
169 | ||
170 | #endif // WXWIN_COMPATIBILITY | |
171 | ||
172 | // Radio box item | |
173 | wxRadioBox::wxRadioBox() | |
174 | { | |
175 | m_selectedButton = -1; | |
176 | m_noItems = 0; | |
177 | m_noRowsOrCols = 0; | |
178 | m_radioButtons = NULL; | |
179 | m_majorDim = 0; | |
180 | m_radioWidth = NULL; | |
181 | m_radioHeight = NULL; | |
182 | } | |
183 | ||
184 | bool wxRadioBox::Create(wxWindow *parent, | |
185 | wxWindowID id, | |
186 | const wxString& title, | |
187 | const wxPoint& pos, | |
188 | const wxSize& size, | |
189 | int n, | |
190 | const wxString choices[], | |
191 | int majorDim, | |
192 | long style, | |
193 | const wxValidator& val, | |
194 | const wxString& name) | |
195 | { | |
196 | // initialize members | |
197 | m_selectedButton = -1; | |
198 | m_noItems = 0; | |
199 | ||
200 | m_majorDim = majorDim == 0 ? n : majorDim; | |
201 | m_noRowsOrCols = majorDim; | |
202 | ||
203 | // common initialization | |
204 | if ( !CreateControl(parent, id, pos, size, style, val, name) ) | |
205 | return FALSE; | |
206 | ||
207 | // create the static box | |
208 | if ( !MSWCreateControl(wxT("BUTTON"), BS_GROUPBOX | WS_GROUP, | |
209 | pos, size, title, 0) ) | |
210 | return FALSE; | |
211 | ||
212 | // and now create the buttons | |
213 | m_noItems = n; | |
214 | #if RADIOBTN_PARENT_IS_RADIOBOX | |
215 | HWND hwndParent = GetHwnd(); | |
216 | #else | |
217 | HWND hwndParent = GetHwndOf(parent); | |
218 | #endif | |
219 | ||
220 | // Some radio boxes test consecutive id. | |
221 | (void)NewControlId(); | |
222 | m_radioButtons = new WXHWND[n]; | |
223 | m_radioWidth = new int[n]; | |
224 | m_radioHeight = new int[n]; | |
225 | ||
226 | WXHFONT hfont = 0; | |
227 | wxFont& font = GetFont(); | |
228 | if ( font.Ok() ) | |
229 | { | |
230 | hfont = font.GetResourceHandle(); | |
231 | } | |
232 | ||
233 | for ( int i = 0; i < n; i++ ) | |
234 | { | |
235 | m_radioWidth[i] = | |
236 | m_radioHeight[i] = -1; | |
237 | long styleBtn = BS_AUTORADIOBUTTON | WS_TABSTOP | WS_CHILD | WS_VISIBLE; | |
238 | if ( i == 0 && style == 0 ) | |
239 | styleBtn |= WS_GROUP; | |
240 | ||
241 | long newId = NewControlId(); | |
242 | ||
243 | HWND hwndBtn = ::CreateWindow(_T("BUTTON"), | |
244 | choices[i], | |
245 | styleBtn, | |
246 | 0, 0, 0, 0, // will be set in SetSize() | |
247 | hwndParent, | |
248 | (HMENU)newId, | |
249 | wxGetInstance(), | |
250 | NULL); | |
251 | ||
252 | if ( !hwndBtn ) | |
253 | { | |
254 | wxLogLastError("CreateWindow(radio btn)"); | |
255 | ||
256 | return FALSE; | |
257 | } | |
258 | ||
259 | m_radioButtons[i] = (WXHWND)hwndBtn; | |
260 | ||
261 | SubclassRadioButton((WXHWND)hwndBtn); | |
262 | ||
263 | if ( hfont ) | |
264 | { | |
265 | ::SendMessage(hwndBtn, WM_SETFONT, (WPARAM)hfont, 0L); | |
266 | } | |
267 | ||
268 | m_subControls.Add(newId); | |
269 | } | |
270 | ||
271 | // Create a dummy radio control to end the group. | |
272 | (void)::CreateWindow(_T("BUTTON"), | |
273 | _T(""), | |
274 | WS_GROUP | BS_AUTORADIOBUTTON | WS_CHILD, | |
275 | 0, 0, 0, 0, hwndParent, | |
276 | (HMENU)NewControlId(), wxGetInstance(), NULL); | |
277 | ||
278 | SetSelection(0); | |
279 | ||
280 | SetSize(pos.x, pos.y, size.x, size.y); | |
281 | ||
282 | return TRUE; | |
283 | } | |
284 | ||
285 | wxRadioBox::~wxRadioBox() | |
286 | { | |
287 | m_isBeingDeleted = TRUE; | |
288 | ||
289 | if (m_radioButtons) | |
290 | { | |
291 | int i; | |
292 | for (i = 0; i < m_noItems; i++) | |
293 | ::DestroyWindow((HWND)m_radioButtons[i]); | |
294 | delete[] m_radioButtons; | |
295 | } | |
296 | ||
297 | if (m_radioWidth) | |
298 | delete[] m_radioWidth; | |
299 | if (m_radioHeight) | |
300 | delete[] m_radioHeight; | |
301 | ||
302 | } | |
303 | ||
304 | wxString wxRadioBox::GetLabel(int item) const | |
305 | { | |
306 | wxCHECK_MSG( item >= 0 && item < m_noItems, wxT(""), wxT("invalid radiobox index") ); | |
307 | ||
308 | return wxGetWindowText(m_radioButtons[item]); | |
309 | } | |
310 | ||
311 | void wxRadioBox::SetLabel(int item, const wxString& label) | |
312 | { | |
313 | wxCHECK_RET( item >= 0 && item < m_noItems, wxT("invalid radiobox index") ); | |
314 | ||
315 | m_radioWidth[item] = m_radioHeight[item] = -1; | |
316 | SetWindowText((HWND)m_radioButtons[item], label.c_str()); | |
317 | } | |
318 | ||
319 | void wxRadioBox::SetLabel(int item, wxBitmap *bitmap) | |
320 | { | |
321 | /* | |
322 | m_radioWidth[item] = bitmap->GetWidth() + FB_MARGIN; | |
323 | m_radioHeight[item] = bitmap->GetHeight() + FB_MARGIN; | |
324 | */ | |
325 | wxFAIL_MSG(wxT("not implemented")); | |
326 | } | |
327 | ||
328 | int wxRadioBox::FindString(const wxString& s) const | |
329 | { | |
330 | for (int i = 0; i < m_noItems; i++) | |
331 | { | |
332 | if ( s == wxGetWindowText(m_radioButtons[i]) ) | |
333 | return i; | |
334 | } | |
335 | ||
336 | return wxNOT_FOUND; | |
337 | } | |
338 | ||
339 | void wxRadioBox::SetSelection(int N) | |
340 | { | |
341 | wxCHECK_RET( (N >= 0) && (N < m_noItems), wxT("invalid radiobox index") ); | |
342 | ||
343 | // Following necessary for Win32s, because Win32s translate BM_SETCHECK | |
344 | if (m_selectedButton >= 0 && m_selectedButton < m_noItems) | |
345 | ::SendMessage((HWND) m_radioButtons[m_selectedButton], BM_SETCHECK, 0, 0L); | |
346 | ||
347 | ::SendMessage((HWND)m_radioButtons[N], BM_SETCHECK, 1, 0L); | |
348 | ::SetFocus((HWND)m_radioButtons[N]); | |
349 | ||
350 | m_selectedButton = N; | |
351 | } | |
352 | ||
353 | // Get single selection, for single choice list items | |
354 | int wxRadioBox::GetSelection() const | |
355 | { | |
356 | return m_selectedButton; | |
357 | } | |
358 | ||
359 | // Find string for position | |
360 | wxString wxRadioBox::GetString(int N) const | |
361 | { | |
362 | return wxGetWindowText(m_radioButtons[N]); | |
363 | } | |
364 | ||
365 | // Restored old code. | |
366 | void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags) | |
367 | { | |
368 | int currentX, currentY; | |
369 | GetPosition(¤tX, ¤tY); | |
370 | int widthOld, heightOld; | |
371 | GetSize(&widthOld, &heightOld); | |
372 | ||
373 | int xx = x; | |
374 | int yy = y; | |
375 | ||
376 | if (x == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) | |
377 | xx = currentX; | |
378 | if (y == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) | |
379 | yy = currentY; | |
380 | ||
381 | #if RADIOBTN_PARENT_IS_RADIOBOX | |
382 | int y_offset = 0; | |
383 | int x_offset = 0; | |
384 | #else | |
385 | int y_offset = yy; | |
386 | int x_offset = xx; | |
387 | #endif | |
388 | ||
389 | int current_width, cyf; | |
390 | ||
391 | int cx1,cy1; | |
392 | wxGetCharSize(m_hWnd, &cx1, &cy1, & GetFont()); | |
393 | ||
394 | // Attempt to have a look coherent with other platforms: We compute the | |
395 | // biggest toggle dim, then we align all items according this value. | |
396 | int maxWidth = -1; | |
397 | int maxHeight = -1; | |
398 | ||
399 | int i; | |
400 | for (i = 0 ; i < m_noItems; i++) | |
401 | { | |
402 | int eachWidth; | |
403 | int eachHeight; | |
404 | if (m_radioWidth[i]<0) | |
405 | { | |
406 | // It's a labelled toggle | |
407 | GetTextExtent(wxGetWindowText(m_radioButtons[i]), | |
408 | ¤t_width, &cyf); | |
409 | eachWidth = (int)(current_width + RADIO_SIZE); | |
410 | eachHeight = (int)((3*cyf)/2); | |
411 | } | |
412 | else | |
413 | { | |
414 | eachWidth = m_radioWidth[i]; | |
415 | eachHeight = m_radioHeight[i]; | |
416 | } | |
417 | ||
418 | if (maxWidth<eachWidth) | |
419 | maxWidth = eachWidth; | |
420 | if (maxHeight<eachHeight) | |
421 | maxHeight = eachHeight; | |
422 | } | |
423 | ||
424 | if (m_hWnd) | |
425 | { | |
426 | int totWidth; | |
427 | int totHeight; | |
428 | ||
429 | int nbHor = GetNumHor(), | |
430 | nbVer = GetNumVer(); | |
431 | ||
432 | // this formula works, but I don't know why. | |
433 | // Please, be sure what you do if you modify it!! | |
434 | if (m_radioWidth[0]<0) | |
435 | totHeight = (nbVer * maxHeight) + cy1/2; | |
436 | else | |
437 | totHeight = nbVer * (maxHeight+cy1/2); | |
438 | totWidth = nbHor * (maxWidth+cx1); | |
439 | ||
440 | int extraHeight = cy1; | |
441 | ||
442 | #if defined(CTL3D) && !CTL3D | |
443 | // Requires a bigger group box in plain Windows | |
444 | extraHeight *= 3; | |
445 | extraHeight /= 2; | |
446 | #endif | |
447 | ||
448 | // only change our width/height if asked for | |
449 | if ( width == -1 ) | |
450 | { | |
451 | if ( sizeFlags & wxSIZE_AUTO_WIDTH ) | |
452 | width = totWidth + cx1; | |
453 | else | |
454 | width = widthOld; | |
455 | } | |
456 | ||
457 | if ( height == -1 ) | |
458 | { | |
459 | if ( sizeFlags & wxSIZE_AUTO_HEIGHT ) | |
460 | height = totHeight + extraHeight; | |
461 | else | |
462 | height = heightOld; | |
463 | } | |
464 | ||
465 | ::MoveWindow(GetHwnd(), xx, yy, width, height, TRUE); | |
466 | ||
467 | x_offset += cx1; | |
468 | y_offset += cy1; | |
469 | } | |
470 | ||
471 | #if defined(CTL3D) && (!CTL3D) | |
472 | y_offset += (int)(cy1/2); // Fudge factor since buttons overlapped label | |
473 | // JACS 2/12/93. CTL3D draws group label quite high. | |
474 | #endif | |
475 | int startX = x_offset; | |
476 | int startY = y_offset; | |
477 | ||
478 | for ( i = 0 ; i < m_noItems; i++) | |
479 | { | |
480 | // Bidimensional radio adjustment | |
481 | if (i&&((i%m_majorDim)==0)) // Why is this omitted for i = 0? | |
482 | { | |
483 | if (m_windowStyle & wxRA_VERTICAL) | |
484 | { | |
485 | y_offset = startY; | |
486 | x_offset += maxWidth + cx1; | |
487 | } | |
488 | else | |
489 | { | |
490 | x_offset = startX; | |
491 | y_offset += maxHeight; | |
492 | if (m_radioWidth[0]>0) | |
493 | y_offset += cy1/2; | |
494 | } | |
495 | } | |
496 | int eachWidth; | |
497 | int eachHeight; | |
498 | if (m_radioWidth[i]<0) | |
499 | { | |
500 | // It's a labeled item | |
501 | GetTextExtent(wxGetWindowText(m_radioButtons[i]), | |
502 | ¤t_width, &cyf); | |
503 | ||
504 | // How do we find out radio button bitmap size!! | |
505 | // By adjusting them carefully, manually :-) | |
506 | eachWidth = (int)(current_width + RADIO_SIZE); | |
507 | eachHeight = (int)((3*cyf)/2); | |
508 | } | |
509 | else | |
510 | { | |
511 | eachWidth = m_radioWidth[i]; | |
512 | eachHeight = m_radioHeight[i]; | |
513 | } | |
514 | ||
515 | // VZ: make all buttons of the same, maximal size - like this they | |
516 | // cover the radiobox entirely and the radiobox tooltips are always | |
517 | // shown (otherwise they are not when the mouse pointer is in the | |
518 | // radiobox part not belonging to any radiobutton) | |
519 | ::MoveWindow((HWND)m_radioButtons[i], | |
520 | x_offset, y_offset, maxWidth, maxHeight, | |
521 | TRUE); | |
522 | ||
523 | if (m_windowStyle & wxRA_SPECIFY_ROWS) | |
524 | { | |
525 | y_offset += maxHeight; | |
526 | if (m_radioWidth[0]>0) | |
527 | y_offset += cy1/2; | |
528 | } | |
529 | else | |
530 | x_offset += maxWidth + cx1; | |
531 | } | |
532 | } | |
533 | ||
534 | void wxRadioBox::GetSize(int *width, int *height) const | |
535 | { | |
536 | RECT rect; | |
537 | rect.left = -1; rect.right = -1; rect.top = -1; rect.bottom = -1; | |
538 | ||
539 | if (m_hWnd) | |
540 | wxFindMaxSize(m_hWnd, &rect); | |
541 | ||
542 | int i; | |
543 | for (i = 0; i < m_noItems; i++) | |
544 | wxFindMaxSize(m_radioButtons[i], &rect); | |
545 | ||
546 | *width = rect.right - rect.left; | |
547 | *height = rect.bottom - rect.top; | |
548 | } | |
549 | ||
550 | void wxRadioBox::GetPosition(int *x, int *y) const | |
551 | { | |
552 | wxWindow *parent = GetParent(); | |
553 | RECT rect = { -1, -1, -1, -1 }; | |
554 | ||
555 | int i; | |
556 | for (i = 0; i < m_noItems; i++) | |
557 | wxFindMaxSize(m_radioButtons[i], &rect); | |
558 | ||
559 | if (m_hWnd) | |
560 | wxFindMaxSize(m_hWnd, &rect); | |
561 | ||
562 | // Since we now have the absolute screen coords, if there's a parent we | |
563 | // must subtract its top left corner | |
564 | POINT point; | |
565 | point.x = rect.left; | |
566 | point.y = rect.top; | |
567 | if (parent) | |
568 | { | |
569 | ::ScreenToClient((HWND) parent->GetHWND(), &point); | |
570 | } | |
571 | ||
572 | // We may be faking the client origin. So a window that's really at (0, 30) | |
573 | // may appear (to wxWin apps) to be at (0, 0). | |
574 | if (GetParent()) | |
575 | { | |
576 | wxPoint pt(GetParent()->GetClientAreaOrigin()); | |
577 | point.x -= pt.x; | |
578 | point.y -= pt.y; | |
579 | } | |
580 | ||
581 | *x = point.x; | |
582 | *y = point.y; | |
583 | } | |
584 | ||
585 | void wxRadioBox::SetFocus() | |
586 | { | |
587 | if (m_noItems > 0) | |
588 | { | |
589 | if (m_selectedButton == -1) | |
590 | ::SetFocus((HWND) m_radioButtons[0]); | |
591 | else | |
592 | ::SetFocus((HWND) m_radioButtons[m_selectedButton]); | |
593 | } | |
594 | ||
595 | } | |
596 | ||
597 | bool wxRadioBox::Show(bool show) | |
598 | { | |
599 | if ( !wxControl::Show(show) ) | |
600 | return FALSE; | |
601 | ||
602 | int nCmdShow = show ? SW_SHOW : SW_HIDE; | |
603 | for ( int i = 0; i < m_noItems; i++ ) | |
604 | { | |
605 | ::ShowWindow((HWND)m_radioButtons[i], nCmdShow); | |
606 | } | |
607 | ||
608 | return TRUE; | |
609 | } | |
610 | ||
611 | // Enable a specific button | |
612 | void wxRadioBox::Enable(int item, bool enable) | |
613 | { | |
614 | wxCHECK_RET( item >= 0 && item < m_noItems, | |
615 | wxT("invalid item in wxRadioBox::Enable()") ); | |
616 | ||
617 | ::EnableWindow((HWND) m_radioButtons[item], enable); | |
618 | } | |
619 | ||
620 | // Enable all controls | |
621 | bool wxRadioBox::Enable(bool enable) | |
622 | { | |
623 | if ( !wxControl::Enable(enable) ) | |
624 | return FALSE; | |
625 | ||
626 | for (int i = 0; i < m_noItems; i++) | |
627 | ::EnableWindow((HWND) m_radioButtons[i], enable); | |
628 | ||
629 | return TRUE; | |
630 | } | |
631 | ||
632 | // Show a specific button | |
633 | void wxRadioBox::Show(int item, bool show) | |
634 | { | |
635 | wxCHECK_RET( item >= 0 && item < m_noItems, | |
636 | wxT("invalid item in wxRadioBox::Show()") ); | |
637 | ||
638 | ::ShowWindow((HWND)m_radioButtons[item], show ? SW_SHOW : SW_HIDE); | |
639 | } | |
640 | ||
641 | // For single selection items only | |
642 | wxString wxRadioBox::GetStringSelection() const | |
643 | { | |
644 | wxString result; | |
645 | int sel = GetSelection(); | |
646 | if (sel > -1) | |
647 | result = GetString(sel); | |
648 | ||
649 | return result; | |
650 | } | |
651 | ||
652 | bool wxRadioBox::SetStringSelection(const wxString& s) | |
653 | { | |
654 | int sel = FindString (s); | |
655 | if (sel > -1) | |
656 | { | |
657 | SetSelection (sel); | |
658 | return TRUE; | |
659 | } | |
660 | else | |
661 | return FALSE; | |
662 | } | |
663 | ||
664 | bool wxRadioBox::ContainsHWND(WXHWND hWnd) const | |
665 | { | |
666 | int i; | |
667 | for (i = 0; i < Number(); i++) | |
668 | { | |
669 | if (GetRadioButtons()[i] == hWnd) | |
670 | return TRUE; | |
671 | } | |
672 | ||
673 | return FALSE; | |
674 | } | |
675 | ||
676 | void wxRadioBox::Command(wxCommandEvent & event) | |
677 | { | |
678 | SetSelection (event.m_commandInt); | |
679 | ProcessCommand (event); | |
680 | } | |
681 | ||
682 | // NB: if this code is changed, wxGetWindowForHWND() which relies on having the | |
683 | // radiobox pointer in GWL_USERDATA for radio buttons must be updated too! | |
684 | void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn) | |
685 | { | |
686 | // No GWL_USERDATA in Win16, so omit this subclassing. | |
687 | #ifdef __WIN32__ | |
688 | HWND hwndBtn = (HWND)hWndBtn; | |
689 | ||
690 | if ( !s_wndprocRadioBtn ) | |
691 | s_wndprocRadioBtn = (WXFARPROC)::GetWindowLong(hwndBtn, GWL_WNDPROC); | |
692 | ||
693 | ::SetWindowLong(hwndBtn, GWL_WNDPROC, (long)wxRadioBtnWndProc); | |
694 | ::SetWindowLong(hwndBtn, GWL_USERDATA, (long)this); | |
695 | #endif // __WIN32__ | |
696 | } | |
697 | ||
698 | void wxRadioBox::SendNotificationEvent() | |
699 | { | |
700 | wxCommandEvent event(wxEVT_COMMAND_RADIOBOX_SELECTED, m_windowId); | |
701 | event.SetInt( m_selectedButton ); | |
702 | event.SetString( GetString(m_selectedButton) ); | |
703 | event.SetEventObject( this ); | |
704 | ProcessCommand(event); | |
705 | } | |
706 | ||
707 | bool wxRadioBox::SetFont(const wxFont& font) | |
708 | { | |
709 | if ( !wxControl::SetFont(font) ) | |
710 | { | |
711 | // nothing to do | |
712 | return FALSE; | |
713 | } | |
714 | ||
715 | // also set the font of our radio buttons | |
716 | WXHFONT hfont = wxFont(font).GetResourceHandle(); | |
717 | for ( int n = 0; n < m_noItems; n++ ) | |
718 | { | |
719 | ::SendMessage((HWND)m_radioButtons[n], WM_SETFONT, (WPARAM)hfont, 0L); | |
720 | } | |
721 | ||
722 | // this is needed because otherwise the buttons are not redrawn correctly | |
723 | Refresh(); | |
724 | ||
725 | return TRUE; | |
726 | } | |
727 | ||
728 | // ---------------------------------------------------------------------------- | |
729 | // our window proc | |
730 | // ---------------------------------------------------------------------------- | |
731 | ||
732 | long wxRadioBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) | |
733 | { | |
734 | switch ( nMsg ) | |
735 | { | |
736 | #ifdef __WIN32__ | |
737 | case WM_CTLCOLORSTATIC: | |
738 | // set the colour of the radio buttons to be the same as ours | |
739 | { | |
740 | HDC hdc = (HDC)wParam; | |
741 | ||
742 | const wxColour& colBack = GetBackgroundColour(); | |
743 | ::SetBkColor(hdc, wxColourToRGB(colBack)); | |
744 | ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour())); | |
745 | ||
746 | wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID); | |
747 | ||
748 | return (WXHBRUSH)brush->GetResourceHandle(); | |
749 | } | |
750 | #endif // Win32 | |
751 | ||
752 | // This is required for the radiobox to be sensitive to mouse input, | |
753 | // e.g. for Dialog Editor. | |
754 | case WM_NCHITTEST: | |
755 | { | |
756 | int xPos = LOWORD(lParam); // horizontal position of cursor | |
757 | int yPos = HIWORD(lParam); // vertical position of cursor | |
758 | ||
759 | ScreenToClient(&xPos, &yPos); | |
760 | ||
761 | // Make sure you can drag by the top of the groupbox, but let | |
762 | // other (enclosed) controls get mouse events also | |
763 | if (yPos < 10) | |
764 | return (long)HTCLIENT; | |
765 | } | |
766 | break; | |
767 | } | |
768 | ||
769 | return wxControl::MSWWindowProc(nMsg, wParam, lParam); | |
770 | } | |
771 | ||
772 | // --------------------------------------------------------------------------- | |
773 | // window proc for radio buttons | |
774 | // --------------------------------------------------------------------------- | |
775 | ||
776 | #ifdef __WIN32__ | |
777 | ||
778 | LRESULT APIENTRY _EXPORT wxRadioBtnWndProc(HWND hwnd, | |
779 | UINT message, | |
780 | WPARAM wParam, | |
781 | LPARAM lParam) | |
782 | { | |
783 | switch ( message ) | |
784 | { | |
785 | case WM_GETDLGCODE: | |
786 | // we must tell IsDialogMessage()/our kbd processing code that we | |
787 | // want to process arrows ourselves because neither of them is | |
788 | // smart enough to handle arrows properly for us | |
789 | { | |
790 | long lDlgCode = ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd, | |
791 | message, wParam, lParam); | |
792 | ||
793 | return lDlgCode | DLGC_WANTARROWS; | |
794 | } | |
795 | ||
796 | #if wxUSE_TOOLTIPS | |
797 | case WM_NOTIFY: | |
798 | { | |
799 | NMHDR* hdr = (NMHDR *)lParam; | |
800 | if ( (int)hdr->code == TTN_NEEDTEXT ) | |
801 | { | |
802 | wxRadioBox *radiobox = (wxRadioBox *) | |
803 | ::GetWindowLong(hwnd, GWL_USERDATA); | |
804 | ||
805 | wxCHECK_MSG( radiobox, 0, | |
806 | wxT("radio button without radio box?") ); | |
807 | ||
808 | wxToolTip *tooltip = radiobox->GetToolTip(); | |
809 | if ( tooltip ) | |
810 | { | |
811 | TOOLTIPTEXT *ttt = (TOOLTIPTEXT *)lParam; | |
812 | ttt->lpszText = (wxChar *)tooltip->GetTip().c_str(); | |
813 | } | |
814 | ||
815 | // processed | |
816 | return 0; | |
817 | } | |
818 | } | |
819 | break; | |
820 | #endif // wxUSE_TOOLTIPS | |
821 | ||
822 | case WM_KEYDOWN: | |
823 | { | |
824 | wxRadioBox *radiobox = (wxRadioBox *) | |
825 | ::GetWindowLong(hwnd, GWL_USERDATA); | |
826 | ||
827 | wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") ); | |
828 | ||
829 | bool processed = TRUE; | |
830 | ||
831 | int selOld = radiobox->GetSelection(); | |
832 | int selNew = selOld; | |
833 | ||
834 | switch ( wParam ) | |
835 | { | |
836 | case VK_UP: | |
837 | selNew--; | |
838 | break; | |
839 | ||
840 | case VK_LEFT: | |
841 | selNew -= radiobox->GetNumVer(); | |
842 | break; | |
843 | ||
844 | case VK_DOWN: | |
845 | selNew++; | |
846 | break; | |
847 | ||
848 | case VK_RIGHT: | |
849 | selNew += radiobox->GetNumVer(); | |
850 | break; | |
851 | ||
852 | default: | |
853 | processed = FALSE; | |
854 | } | |
855 | ||
856 | if ( processed ) | |
857 | { | |
858 | // ensure that selNew is in range [0..num) | |
859 | int num = radiobox->Number(); | |
860 | selNew += num; | |
861 | selNew %= num; | |
862 | ||
863 | if ( selNew != selOld ) | |
864 | { | |
865 | radiobox->SetSelection(selNew); | |
866 | ||
867 | // emulate the button click | |
868 | radiobox->SendNotificationEvent(); | |
869 | ||
870 | return 0; | |
871 | } | |
872 | } | |
873 | } | |
874 | } | |
875 | ||
876 | return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd, message, wParam, lParam); | |
877 | } | |
878 | ||
879 | #endif // __WIN32__ | |
880 |