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