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