]> git.saurik.com Git - wxWidgets.git/blame - src/msw/radiobox.cpp
Corrected wxFormatConverter for strings which need
[wxWidgets.git] / src / msw / radiobox.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
3ca6a5f0
BP
2// Name: msw/radiobox.cpp
3// Purpose: wxRadioBox implementation
2bda0e17
KB
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
1e6feb95
VZ
31#if wxUSE_RADIOBOX
32
2bda0e17 33#ifndef WX_PRECOMP
da07e033
VZ
34 #include "wx/bitmap.h"
35 #include "wx/brush.h"
36 #include "wx/radiobox.h"
f6bcfd97 37 #include "wx/settings.h"
381dd4bf 38 #include "wx/log.h"
2bda0e17
KB
39#endif
40
41#include "wx/msw/private.h"
42
f048e32f 43#if wxUSE_TOOLTIPS
ae090fdb 44 #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
8614c467 45 #include <commctrl.h>
5ace0213 46 #endif
f048e32f
VZ
47 #include "wx/tooltip.h"
48#endif // wxUSE_TOOLTIPS
49
d3acd369 50IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl)
2bda0e17 51
8614c467
VZ
52// there are two possible ways to create the radio buttons: either as children
53// of the radiobox or as siblings of it - allow playing with both variants for
54// now, eventually we will choose the best one for our purposes
f048e32f 55//
8614c467
VZ
56// two main problems are the keyboard navigation inside the radiobox (arrows
57// should switch between buttons, not pass focus to the next control) and the
58// tooltips - a tooltip is associated with the radiobox itself, not the
59// children...
60//
61// the problems with setting this to 1:
62// a) Alt-<mnemonic of radiobox> isn't handled properly by IsDialogMessage()
63// because it sets focus to the next control accepting it which is not a
64// radio button but a radiobox sibling in this case - the only solution to
65// this would be to handle Alt-<mnemonic> ourselves
66// b) the problems with setting radiobox colours under Win98/2K were reported
67// but I couldn't reproduce it so I have no idea about what causes it
68//
69// the problems with setting this to 0:
70// a) the tooltips are not shown for the radiobox - possible solution: make
71// TTM_WINDOWFROMPOS handling code in msw/tooltip.cpp work (easier said than
72// done because I don't know why it doesn't work)
d1e418ea 73#define RADIOBTN_PARENT_IS_RADIOBOX 0
f048e32f 74
e373f51b
VZ
75// ---------------------------------------------------------------------------
76// private functions
77// ---------------------------------------------------------------------------
78
e373f51b 79// wnd proc for radio buttons
2a47d3c1 80#ifdef __WIN32__
e373f51b
VZ
81LRESULT APIENTRY _EXPORT wxRadioBtnWndProc(HWND hWnd,
82 UINT message,
83 WPARAM wParam,
84 LPARAM lParam);
85
86// ---------------------------------------------------------------------------
87// global vars
88// ---------------------------------------------------------------------------
89
90// the pointer to standard radio button wnd proc
457e6c54 91static WXFARPROC s_wndprocRadioBtn = (WXFARPROC)NULL;
e373f51b 92
42e69d6b
VZ
93#endif // __WIN32__
94
e373f51b
VZ
95// ===========================================================================
96// implementation
97// ===========================================================================
98
99// ---------------------------------------------------------------------------
100// wxRadioBox
101// ---------------------------------------------------------------------------
102
1e6feb95
VZ
103int wxRadioBox::GetCount() const
104{
105 return m_noItems;
106}
107
108int wxRadioBox::GetColumnCount() const
109{
110 return GetNumHor();
111}
112
113int wxRadioBox::GetRowCount() const
114{
115 return GetNumVer();
116}
117
3ca6a5f0 118// returns the number of rows
e373f51b
VZ
119int wxRadioBox::GetNumVer() const
120{
121 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
122 {
123 return m_majorDim;
124 }
125 else
126 {
127 return (m_noItems + m_majorDim - 1)/m_majorDim;
128 }
129}
130
3ca6a5f0 131// returns the number of columns
e373f51b
VZ
132int wxRadioBox::GetNumHor() const
133{
134 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
135 {
136 return (m_noItems + m_majorDim - 1)/m_majorDim;
137 }
138 else
139 {
140 return m_majorDim;
141 }
142}
143
42e69d6b 144bool wxRadioBox::MSWCommand(WXUINT cmd, WXWORD id)
2bda0e17 145{
42e69d6b 146 if ( cmd == BN_CLICKED )
da07e033 147 {
f6bcfd97
BP
148 if (id == GetId())
149 return TRUE;
150
9a5ccab4 151 int selectedButton = -1;
e373f51b
VZ
152
153 for ( int i = 0; i < m_noItems; i++ )
154 {
cc2b7472 155 if ( id == wxGetWindowId(m_radioButtons[i]) )
e373f51b 156 {
9a5ccab4 157 selectedButton = i;
e373f51b
VZ
158
159 break;
160 }
161 }
162
3ca6a5f0
BP
163 if ( selectedButton == -1 )
164 {
165 // just ignore it - due to a hack with WM_NCHITTEST handling in our
166 // wnd proc, we can receive dummy click messages when we click near
167 // the radiobox edge (this is ugly but Julian wouldn't let me get
168 // rid of this...)
169 return FALSE;
170 }
2bda0e17 171
9a5ccab4
VZ
172 if ( selectedButton != m_selectedButton )
173 {
174 m_selectedButton = selectedButton;
175
176 SendNotificationEvent();
177 }
178 //else: don't generate events when the selection doesn't change
e373f51b 179
da07e033
VZ
180 return TRUE;
181 }
e373f51b
VZ
182 else
183 return FALSE;
2bda0e17
KB
184}
185
186#if WXWIN_COMPATIBILITY
187wxRadioBox::wxRadioBox(wxWindow *parent, wxFunction func, const char *title,
da07e033
VZ
188 int x, int y, int width, int height,
189 int n, char **choices,
190 int majorDim, long style, const char *name)
2bda0e17
KB
191{
192 wxString *choices2 = new wxString[n];
193 for ( int i = 0; i < n; i ++) choices2[i] = choices[i];
194 Create(parent, -1, title, wxPoint(x, y), wxSize(width, height), n, choices2, majorDim, style,
da07e033 195 wxDefaultValidator, name);
2bda0e17
KB
196 Callback(func);
197 delete choices2;
198}
199
d9317fd4 200#endif // WXWIN_COMPATIBILITY
2bda0e17
KB
201
202// Radio box item
e373f51b 203wxRadioBox::wxRadioBox()
2bda0e17 204{
da07e033
VZ
205 m_selectedButton = -1;
206 m_noItems = 0;
207 m_noRowsOrCols = 0;
208 m_radioButtons = NULL;
e373f51b
VZ
209 m_majorDim = 0;
210 m_radioWidth = NULL;
211 m_radioHeight = NULL;
2bda0e17
KB
212}
213
f048e32f
VZ
214bool wxRadioBox::Create(wxWindow *parent,
215 wxWindowID id,
216 const wxString& title,
217 const wxPoint& pos,
218 const wxSize& size,
219 int n,
220 const wxString choices[],
221 int majorDim,
222 long style,
223 const wxValidator& val,
224 const wxString& name)
2bda0e17 225{
f048e32f 226 // initialize members
da07e033 227 m_selectedButton = -1;
4afd7529 228 m_noItems = 0;
2bda0e17 229
3ca6a5f0 230 m_majorDim = majorDim == 0 ? n : majorDim;
da07e033 231 m_noRowsOrCols = majorDim;
da07e033 232
f048e32f
VZ
233 // common initialization
234 if ( !CreateControl(parent, id, pos, size, style, val, name) )
235 return FALSE;
2bda0e17 236
f048e32f 237 // create the static box
d9317fd4
VZ
238 if ( !MSWCreateControl(wxT("BUTTON"), BS_GROUPBOX | WS_GROUP,
239 pos, size, title, 0) )
f048e32f 240 return FALSE;
da07e033 241
f048e32f 242 // and now create the buttons
4afd7529 243 m_noItems = n;
f048e32f
VZ
244#if RADIOBTN_PARENT_IS_RADIOBOX
245 HWND hwndParent = GetHwnd();
246#else
247 HWND hwndParent = GetHwndOf(parent);
248#endif
da07e033
VZ
249
250 // Some radio boxes test consecutive id.
e373f51b 251 (void)NewControlId();
da07e033 252 m_radioButtons = new WXHWND[n];
e373f51b
VZ
253 m_radioWidth = new int[n];
254 m_radioHeight = new int[n];
f048e32f
VZ
255
256 WXHFONT hfont = 0;
257 wxFont& font = GetFont();
258 if ( font.Ok() )
da07e033 259 {
f048e32f
VZ
260 hfont = font.GetResourceHandle();
261 }
262
263 for ( int i = 0; i < n; i++ )
264 {
265 m_radioWidth[i] =
266 m_radioHeight[i] = -1;
d3acd369 267 long styleBtn = BS_AUTORADIOBUTTON | WS_TABSTOP | WS_CHILD | WS_VISIBLE;
e373f51b 268 if ( i == 0 && style == 0 )
f048e32f
VZ
269 styleBtn |= WS_GROUP;
270
da07e033 271 long newId = NewControlId();
da07e033 272
f048e32f
VZ
273 HWND hwndBtn = ::CreateWindow(_T("BUTTON"),
274 choices[i],
275 styleBtn,
276 0, 0, 0, 0, // will be set in SetSize()
e373f51b 277 hwndParent,
f048e32f
VZ
278 (HMENU)newId,
279 wxGetInstance(),
e373f51b 280 NULL);
2bda0e17 281
f048e32f
VZ
282 if ( !hwndBtn )
283 {
f6bcfd97 284 wxLogLastError(wxT("CreateWindow(radio btn)"));
f048e32f
VZ
285
286 return FALSE;
287 }
288
e373f51b 289 m_radioButtons[i] = (WXHWND)hwndBtn;
42e69d6b 290
e373f51b 291 SubclassRadioButton((WXHWND)hwndBtn);
2bda0e17 292
f048e32f 293 if ( hfont )
da07e033 294 {
f048e32f 295 ::SendMessage(hwndBtn, WM_SETFONT, (WPARAM)hfont, 0L);
da07e033 296 }
e373f51b 297
f048e32f 298 m_subControls.Add(newId);
da07e033 299 }
e373f51b 300
da07e033 301 // Create a dummy radio control to end the group.
f048e32f
VZ
302 (void)::CreateWindow(_T("BUTTON"),
303 _T(""),
304 WS_GROUP | BS_AUTORADIOBUTTON | WS_CHILD,
e373f51b
VZ
305 0, 0, 0, 0, hwndParent,
306 (HMENU)NewControlId(), wxGetInstance(), NULL);
2bda0e17 307
da07e033 308 SetSelection(0);
2bda0e17 309
f048e32f 310 SetSize(pos.x, pos.y, size.x, size.y);
2bda0e17 311
da07e033 312 return TRUE;
2bda0e17 313}
2bda0e17 314
e373f51b 315wxRadioBox::~wxRadioBox()
2bda0e17 316{
da07e033 317 m_isBeingDeleted = TRUE;
1eb20d4a 318
da07e033
VZ
319 if (m_radioButtons)
320 {
321 int i;
322 for (i = 0; i < m_noItems; i++)
42e69d6b 323 ::DestroyWindow((HWND)m_radioButtons[i]);
da07e033
VZ
324 delete[] m_radioButtons;
325 }
42e69d6b 326
da07e033 327 if (m_radioWidth)
e373f51b 328 delete[] m_radioWidth;
da07e033 329 if (m_radioHeight)
e373f51b 330 delete[] m_radioHeight;
2bda0e17
KB
331
332}
333
1e6feb95 334void wxRadioBox::SetString(int item, const wxString& label)
2bda0e17 335{
223d09f6 336 wxCHECK_RET( item >= 0 && item < m_noItems, wxT("invalid radiobox index") );
d66a042c 337
e373f51b 338 m_radioWidth[item] = m_radioHeight[item] = -1;
42e69d6b 339 SetWindowText((HWND)m_radioButtons[item], label.c_str());
2bda0e17
KB
340}
341
debe6624 342void wxRadioBox::SetSelection(int N)
2bda0e17 343{
223d09f6 344 wxCHECK_RET( (N >= 0) && (N < m_noItems), wxT("invalid radiobox index") );
2bda0e17 345
da07e033
VZ
346 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
347 if (m_selectedButton >= 0 && m_selectedButton < m_noItems)
e373f51b
VZ
348 ::SendMessage((HWND) m_radioButtons[m_selectedButton], BM_SETCHECK, 0, 0L);
349
350 ::SendMessage((HWND)m_radioButtons[N], BM_SETCHECK, 1, 0L);
351 ::SetFocus((HWND)m_radioButtons[N]);
2bda0e17 352
da07e033 353 m_selectedButton = N;
2bda0e17
KB
354}
355
356// Get single selection, for single choice list items
e373f51b 357int wxRadioBox::GetSelection() const
2bda0e17 358{
da07e033 359 return m_selectedButton;
2bda0e17
KB
360}
361
362// Find string for position
1e6feb95 363wxString wxRadioBox::GetString(int item) const
2bda0e17 364{
1e6feb95
VZ
365 wxCHECK_MSG( item >= 0 && item < m_noItems, wxEmptyString,
366 wxT("invalid radiobox index") );
367
368 return wxGetWindowText(m_radioButtons[item]);
2bda0e17
KB
369}
370
3ca6a5f0
BP
371// ----------------------------------------------------------------------------
372// size calculations
373// ----------------------------------------------------------------------------
374
375wxSize wxRadioBox::GetMaxButtonSize() const
376{
377 // calculate the max button size
378 int widthMax = 0,
379 heightMax = 0;
380 for ( int i = 0 ; i < m_noItems; i++ )
381 {
382 int width, height;
383 if ( m_radioWidth[i] < 0 )
384 {
385 GetTextExtent(wxGetWindowText(m_radioButtons[i]), &width, &height);
386
387 // adjust the size to take into account the radio box itself
388 // FIXME this is totally bogus!
389 width += RADIO_SIZE;
390 height *= 3;
391 height /= 2;
392 }
393 else
394 {
395 width = m_radioWidth[i];
396 height = m_radioHeight[i];
397 }
398
399 if ( widthMax < width )
400 widthMax = width;
401 if ( heightMax < height )
402 heightMax = height;
403 }
404
405 return wxSize(widthMax, heightMax);
406}
407
408wxSize wxRadioBox::GetTotalButtonSize(const wxSize& sizeBtn) const
409{
410 // the radiobox should be big enough for its buttons
411 int cx1, cy1;
412 wxGetCharSize(m_hWnd, &cx1, &cy1, &GetFont());
413
414 int extraHeight = cy1;
415
c7fd6717 416 /* We'll assume the adjustments below are OK for Win 3.1 too
3ca6a5f0
BP
417#if defined(CTL3D) && !CTL3D
418 // Requires a bigger group box in plain Windows
419 extraHeight *= 3;
420 extraHeight /= 2;
421#endif
c7fd6717
JS
422 */
423
3ca6a5f0
BP
424 int height = GetNumVer() * sizeBtn.y + cy1/2 + extraHeight;
425 int width = GetNumHor() * (sizeBtn.x + cx1) + cx1;
426
c7fd6717
JS
427 // Add extra space under the label, if it exists.
428 if (!wxControl::GetLabel().IsEmpty())
429 height += cy1/2;
430
3ca6a5f0
BP
431 // and also wide enough for its label
432 int widthLabel;
433 GetTextExtent(GetTitle(), &widthLabel, NULL);
434 widthLabel += RADIO_SIZE; // FIXME this is bogus too
435 if ( widthLabel > width )
436 width = widthLabel;
437
438 return wxSize(width, height);
439}
440
441wxSize wxRadioBox::DoGetBestSize() const
442{
443 return GetTotalButtonSize(GetMaxButtonSize());
444}
445
1f916a19 446// Restored old code.
bfc6fde4 447void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
1f916a19 448{
4438caf4
VZ
449 int currentX, currentY;
450 GetPosition(&currentX, &currentY);
c219cecc
VZ
451 int widthOld, heightOld;
452 GetSize(&widthOld, &heightOld);
453
4438caf4
VZ
454 int xx = x;
455 int yy = y;
456
ce96b7a0 457 if (x == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
4438caf4 458 xx = currentX;
ce96b7a0 459 if (y == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
4438caf4
VZ
460 yy = currentY;
461
f048e32f
VZ
462#if RADIOBTN_PARENT_IS_RADIOBOX
463 int y_offset = 0;
464 int x_offset = 0;
465#else
4438caf4
VZ
466 int y_offset = yy;
467 int x_offset = xx;
f048e32f
VZ
468#endif
469
3ca6a5f0 470 int cx1, cy1;
4438caf4
VZ
471 wxGetCharSize(m_hWnd, &cx1, &cy1, & GetFont());
472
473 // Attempt to have a look coherent with other platforms: We compute the
474 // biggest toggle dim, then we align all items according this value.
3ca6a5f0
BP
475 wxSize maxSize = GetMaxButtonSize();
476 int maxWidth = maxSize.x,
477 maxHeight = maxSize.y;
4438caf4 478
3ca6a5f0
BP
479 wxSize totSize = GetTotalButtonSize(maxSize);
480 int totWidth = totSize.x,
481 totHeight = totSize.y;
482
483 // only change our width/height if asked for
484 if ( width == -1 )
1f916a19 485 {
3ca6a5f0
BP
486 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
487 width = totWidth;
4438caf4 488 else
3ca6a5f0 489 width = widthOld;
1f916a19 490 }
1f916a19 491
3ca6a5f0 492 if ( height == -1 )
4438caf4 493 {
3ca6a5f0
BP
494 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
495 height = totHeight;
4438caf4 496 else
3ca6a5f0
BP
497 height = heightOld;
498 }
4438caf4 499
3ca6a5f0 500 ::MoveWindow(GetHwnd(), xx, yy, width, height, TRUE);
c219cecc 501
3ca6a5f0
BP
502 // Now position all the buttons: the current button will be put at
503 // wxPoint(x_offset, y_offset) and the new row/column will start at
504 // startX/startY. The size of all buttons will be the same wxSize(maxWidth,
505 // maxHeight) except for the buttons in the last column which should extend
506 // to the right border of radiobox and thus can be wider than this.
c219cecc 507
3ca6a5f0
BP
508 // Also, remember that wxRA_SPECIFY_COLS means that we arrange buttons in
509 // left to right order and m_majorDim is the number of columns while
510 // wxRA_SPECIFY_ROWS means that the buttons are arranged top to bottom and
511 // m_majorDim is the number of rows.
4438caf4 512
3ca6a5f0
BP
513 x_offset += cx1;
514 y_offset += cy1;
1f916a19 515
c7fd6717
JS
516 // Add extra space under the label, if it exists.
517 if (!wxControl::GetLabel().IsEmpty())
518 y_offset += cy1/2;
3ca6a5f0 519
4438caf4
VZ
520 int startX = x_offset;
521 int startY = y_offset;
1f916a19 522
3ca6a5f0 523 for ( int i = 0; i < m_noItems; i++ )
1f916a19 524 {
3ca6a5f0
BP
525 // the last button in the row may be wider than the other ones as the
526 // radiobox may be wider than the sum of the button widths (as it
527 // happens, for example, when the radiobox label is very long)
528 bool isLastInTheRow;
529 if ( m_windowStyle & wxRA_SPECIFY_COLS )
530 {
531 // item is the last in its row if it is a multiple of the number of
532 // columns or if it is just the last item
533 int n = i + 1;
534 isLastInTheRow = ((n % m_majorDim) == 0) || (n == m_noItems);
535 }
536 else // wxRA_SPECIFY_ROWS
4438caf4 537 {
3ca6a5f0
BP
538 // item is the last in the row if it is in the last columns
539 isLastInTheRow = i >= (m_noItems/m_majorDim)*m_majorDim;
540 }
541
542 // is this the start of new row/column?
543 if ( i && (i % m_majorDim == 0) )
544 {
545 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
4438caf4 546 {
3ca6a5f0 547 // start of new column
4438caf4
VZ
548 y_offset = startY;
549 x_offset += maxWidth + cx1;
550 }
3ca6a5f0 551 else // start of new row
4438caf4
VZ
552 {
553 x_offset = startX;
554 y_offset += maxHeight;
555 if (m_radioWidth[0]>0)
556 y_offset += cy1/2;
557 }
558 }
3ca6a5f0
BP
559
560 int widthBtn;
561 if ( isLastInTheRow )
4438caf4 562 {
3ca6a5f0
BP
563 // make the button go to the end of radio box
564 widthBtn = startX + width - x_offset - 2*cx1;
565 if ( widthBtn < maxWidth )
566 widthBtn = maxWidth;
4438caf4
VZ
567 }
568 else
569 {
3ca6a5f0
BP
570 // normal button, always of the same size
571 widthBtn = maxWidth;
4438caf4 572 }
1f916a19 573
f048e32f
VZ
574 // VZ: make all buttons of the same, maximal size - like this they
575 // cover the radiobox entirely and the radiobox tooltips are always
576 // shown (otherwise they are not when the mouse pointer is in the
577 // radiobox part not belonging to any radiobutton)
578 ::MoveWindow((HWND)m_radioButtons[i],
3ca6a5f0 579 x_offset, y_offset, widthBtn, maxHeight,
f048e32f 580 TRUE);
4438caf4 581
3ca6a5f0
BP
582 // where do we put the next button?
583 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
4438caf4 584 {
3ca6a5f0 585 // below this one
4438caf4
VZ
586 y_offset += maxHeight;
587 if (m_radioWidth[0]>0)
588 y_offset += cy1/2;
589 }
590 else
3ca6a5f0
BP
591 {
592 // to the right of this one
593 x_offset += widthBtn + cx1;
594 }
1f916a19 595 }
1f916a19
JS
596}
597
2bda0e17
KB
598void wxRadioBox::GetSize(int *width, int *height) const
599{
da07e033
VZ
600 RECT rect;
601 rect.left = -1; rect.right = -1; rect.top = -1; rect.bottom = -1;
2bda0e17 602
da07e033
VZ
603 if (m_hWnd)
604 wxFindMaxSize(m_hWnd, &rect);
2bda0e17 605
da07e033
VZ
606 int i;
607 for (i = 0; i < m_noItems; i++)
608 wxFindMaxSize(m_radioButtons[i], &rect);
2bda0e17 609
da07e033
VZ
610 *width = rect.right - rect.left;
611 *height = rect.bottom - rect.top;
2bda0e17
KB
612}
613
614void wxRadioBox::GetPosition(int *x, int *y) const
615{
da07e033 616 wxWindow *parent = GetParent();
f048e32f 617 RECT rect = { -1, -1, -1, -1 };
da07e033
VZ
618
619 int i;
620 for (i = 0; i < m_noItems; i++)
621 wxFindMaxSize(m_radioButtons[i], &rect);
622
623 if (m_hWnd)
624 wxFindMaxSize(m_hWnd, &rect);
625
f048e32f
VZ
626 // Since we now have the absolute screen coords, if there's a parent we
627 // must subtract its top left corner
da07e033
VZ
628 POINT point;
629 point.x = rect.left;
630 point.y = rect.top;
631 if (parent)
632 {
633 ::ScreenToClient((HWND) parent->GetHWND(), &point);
634 }
f048e32f
VZ
635
636 // We may be faking the client origin. So a window that's really at (0, 30)
637 // may appear (to wxWin apps) to be at (0, 0).
da07e033
VZ
638 if (GetParent())
639 {
640 wxPoint pt(GetParent()->GetClientAreaOrigin());
641 point.x -= pt.x;
642 point.y -= pt.y;
643 }
644
645 *x = point.x;
646 *y = point.y;
2bda0e17
KB
647}
648
e373f51b 649void wxRadioBox::SetFocus()
2bda0e17 650{
da07e033
VZ
651 if (m_noItems > 0)
652 {
653 if (m_selectedButton == -1)
654 ::SetFocus((HWND) m_radioButtons[0]);
655 else
656 ::SetFocus((HWND) m_radioButtons[m_selectedButton]);
657 }
2bda0e17
KB
658
659}
660
debe6624 661bool wxRadioBox::Show(bool show)
2bda0e17 662{
42e69d6b
VZ
663 if ( !wxControl::Show(show) )
664 return FALSE;
665
666 int nCmdShow = show ? SW_SHOW : SW_HIDE;
667 for ( int i = 0; i < m_noItems; i++ )
668 {
669 ::ShowWindow((HWND)m_radioButtons[i], nCmdShow);
670 }
671
da07e033 672 return TRUE;
2bda0e17
KB
673}
674
675// Enable a specific button
debe6624 676void wxRadioBox::Enable(int item, bool enable)
2bda0e17 677{
42e69d6b 678 wxCHECK_RET( item >= 0 && item < m_noItems,
223d09f6 679 wxT("invalid item in wxRadioBox::Enable()") );
42e69d6b
VZ
680
681 ::EnableWindow((HWND) m_radioButtons[item], enable);
2bda0e17
KB
682}
683
684// Enable all controls
cc2b7472 685bool wxRadioBox::Enable(bool enable)
2bda0e17 686{
cc2b7472
VZ
687 if ( !wxControl::Enable(enable) )
688 return FALSE;
1eb20d4a 689
42e69d6b 690 for (int i = 0; i < m_noItems; i++)
da07e033 691 ::EnableWindow((HWND) m_radioButtons[i], enable);
cc2b7472
VZ
692
693 return TRUE;
2bda0e17
KB
694}
695
696// Show a specific button
debe6624 697void wxRadioBox::Show(int item, bool show)
2bda0e17 698{
42e69d6b 699 wxCHECK_RET( item >= 0 && item < m_noItems,
223d09f6 700 wxT("invalid item in wxRadioBox::Show()") );
42e69d6b
VZ
701
702 ::ShowWindow((HWND)m_radioButtons[item], show ? SW_SHOW : SW_HIDE);
2bda0e17
KB
703}
704
2bda0e17
KB
705bool wxRadioBox::ContainsHWND(WXHWND hWnd) const
706{
1e6feb95
VZ
707 size_t count = GetCount();
708 for ( size_t i = 0; i < count; i++ )
42e69d6b 709 {
1e6feb95 710 if ( GetRadioButtons()[i] == hWnd )
da07e033 711 return TRUE;
42e69d6b
VZ
712 }
713
da07e033 714 return FALSE;
2bda0e17
KB
715}
716
4afd7529 717void wxRadioBox::Command(wxCommandEvent & event)
2bda0e17 718{
da07e033
VZ
719 SetSelection (event.m_commandInt);
720 ProcessCommand (event);
2bda0e17
KB
721}
722
8614c467
VZ
723// NB: if this code is changed, wxGetWindowForHWND() which relies on having the
724// radiobox pointer in GWL_USERDATA for radio buttons must be updated too!
e373f51b
VZ
725void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn)
726{
8614c467 727 // No GWL_USERDATA in Win16, so omit this subclassing.
42e69d6b 728#ifdef __WIN32__
e373f51b
VZ
729 HWND hwndBtn = (HWND)hWndBtn;
730
731 if ( !s_wndprocRadioBtn )
457e6c54 732 s_wndprocRadioBtn = (WXFARPROC)::GetWindowLong(hwndBtn, GWL_WNDPROC);
e373f51b
VZ
733
734 ::SetWindowLong(hwndBtn, GWL_WNDPROC, (long)wxRadioBtnWndProc);
735 ::SetWindowLong(hwndBtn, GWL_USERDATA, (long)this);
42e69d6b 736#endif // __WIN32__
e373f51b
VZ
737}
738
9a5ccab4
VZ
739void wxRadioBox::SendNotificationEvent()
740{
741 wxCommandEvent event(wxEVT_COMMAND_RADIOBOX_SELECTED, m_windowId);
742 event.SetInt( m_selectedButton );
36dda0de 743 event.SetString( GetString(m_selectedButton) );
9a5ccab4
VZ
744 event.SetEventObject( this );
745 ProcessCommand(event);
746}
747
4afd7529
VZ
748bool wxRadioBox::SetFont(const wxFont& font)
749{
750 if ( !wxControl::SetFont(font) )
751 {
752 // nothing to do
753 return FALSE;
754 }
755
756 // also set the font of our radio buttons
757 WXHFONT hfont = wxFont(font).GetResourceHandle();
758 for ( int n = 0; n < m_noItems; n++ )
759 {
f6bcfd97
BP
760 HWND hwndBtn = (HWND)m_radioButtons[n];
761 ::SendMessage(hwndBtn, WM_SETFONT, (WPARAM)hfont, 0L);
4afd7529 762
f6bcfd97
BP
763 // otherwise the buttons are not redrawn correctly
764 ::InvalidateRect(hwndBtn, NULL, FALSE /* don't erase bg */);
765 }
8614c467 766
4afd7529
VZ
767 return TRUE;
768}
769
8614c467
VZ
770// ----------------------------------------------------------------------------
771// our window proc
772// ----------------------------------------------------------------------------
773
c92d798f
JS
774long wxRadioBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
775{
d3acd369 776 switch ( nMsg )
c92d798f 777 {
8614c467 778#ifdef __WIN32__
d3acd369
VZ
779 case WM_CTLCOLORSTATIC:
780 // set the colour of the radio buttons to be the same as ours
781 {
782 HDC hdc = (HDC)wParam;
c92d798f 783
d3acd369
VZ
784 const wxColour& colBack = GetBackgroundColour();
785 ::SetBkColor(hdc, wxColourToRGB(colBack));
786 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
c92d798f 787
d3acd369
VZ
788 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
789
790 return (WXHBRUSH)brush->GetResourceHandle();
791 }
8614c467 792#endif // Win32
c92d798f 793
73b26a1c
VZ
794 // VZ: this code breaks radiobox redrawing under Windows XP, don't use
795 // it. If you need to get messages from the static controls,
796 // create them as a child of another (non static) window
797#if 0
d3acd369
VZ
798 // This is required for the radiobox to be sensitive to mouse input,
799 // e.g. for Dialog Editor.
800 case WM_NCHITTEST:
801 {
802 int xPos = LOWORD(lParam); // horizontal position of cursor
803 int yPos = HIWORD(lParam); // vertical position of cursor
804
805 ScreenToClient(&xPos, &yPos);
806
807 // Make sure you can drag by the top of the groupbox, but let
808 // other (enclosed) controls get mouse events also
809 if (yPos < 10)
810 return (long)HTCLIENT;
811 }
8614c467 812 break;
73b26a1c 813#endif // 0
d3acd369 814 }
8614c467
VZ
815
816 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
c92d798f
JS
817}
818
33ac7e6f 819WXHBRUSH wxRadioBox::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
788722ac
JS
820#if wxUSE_CTL3D
821 WXUINT message,
822 WXWPARAM wParam,
823 WXLPARAM lParam
824#else
33ac7e6f
KB
825 WXUINT WXUNUSED(message),
826 WXWPARAM WXUNUSED(wParam),
788722ac
JS
827 WXLPARAM WXUNUSED(lParam)
828#endif
829 )
f6bcfd97
BP
830{
831#if wxUSE_CTL3D
832 if ( m_useCtl3D )
833 {
834 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
835 return (WXHBRUSH) hbrush;
836 }
837#endif // wxUSE_CTL3D
838
839 HDC hdc = (HDC)pDC;
840 if (GetParent()->GetTransparentBackground())
841 SetBkMode(hdc, TRANSPARENT);
842 else
843 SetBkMode(hdc, OPAQUE);
844
845 wxColour colBack = GetBackgroundColour();
846
847 if (!IsEnabled())
a756f210 848 colBack = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
f6bcfd97
BP
849
850 ::SetBkColor(hdc, wxColourToRGB(colBack));
851 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
852
853 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
854
855 return (WXHBRUSH)brush->GetResourceHandle();
856}
857
858
e373f51b
VZ
859// ---------------------------------------------------------------------------
860// window proc for radio buttons
861// ---------------------------------------------------------------------------
862
2a47d3c1
JS
863#ifdef __WIN32__
864
e373f51b 865LRESULT APIENTRY _EXPORT wxRadioBtnWndProc(HWND hwnd,
8614c467 866 UINT message,
e373f51b
VZ
867 WPARAM wParam,
868 LPARAM lParam)
869{
8614c467 870 switch ( message )
e373f51b 871 {
8614c467
VZ
872 case WM_GETDLGCODE:
873 // we must tell IsDialogMessage()/our kbd processing code that we
874 // want to process arrows ourselves because neither of them is
875 // smart enough to handle arrows properly for us
876 {
8ad6ad99 877 long lDlgCode = ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd,
8614c467 878 message, wParam, lParam);
8ad6ad99 879
8614c467
VZ
880 return lDlgCode | DLGC_WANTARROWS;
881 }
e373f51b 882
8614c467
VZ
883#if wxUSE_TOOLTIPS
884 case WM_NOTIFY:
f048e32f 885 {
8614c467
VZ
886 NMHDR* hdr = (NMHDR *)lParam;
887 if ( (int)hdr->code == TTN_NEEDTEXT )
e373f51b 888 {
8614c467
VZ
889 wxRadioBox *radiobox = (wxRadioBox *)
890 ::GetWindowLong(hwnd, GWL_USERDATA);
e373f51b 891
8614c467
VZ
892 wxCHECK_MSG( radiobox, 0,
893 wxT("radio button without radio box?") );
894
895 wxToolTip *tooltip = radiobox->GetToolTip();
896 if ( tooltip )
897 {
898 TOOLTIPTEXT *ttt = (TOOLTIPTEXT *)lParam;
899 ttt->lpszText = (wxChar *)tooltip->GetTip().c_str();
900 }
901
902 // processed
903 return 0;
e373f51b 904 }
f048e32f 905 }
8614c467 906 break;
f048e32f 907#endif // wxUSE_TOOLTIPS
f048e32f 908
8614c467 909 case WM_KEYDOWN:
9a5ccab4 910 {
8614c467
VZ
911 wxRadioBox *radiobox = (wxRadioBox *)
912 ::GetWindowLong(hwnd, GWL_USERDATA);
f048e32f 913
8614c467 914 wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") );
f048e32f 915
8614c467 916 bool processed = TRUE;
f048e32f 917
e421922f 918 wxDirection dir;
8614c467
VZ
919 switch ( wParam )
920 {
921 case VK_UP:
1e6feb95 922 dir = wxUP;
8614c467 923 break;
f048e32f 924
8614c467 925 case VK_LEFT:
1e6feb95 926 dir = wxLEFT;
8614c467 927 break;
f048e32f 928
8614c467 929 case VK_DOWN:
1e6feb95 930 dir = wxDOWN;
8614c467 931 break;
f048e32f 932
8614c467 933 case VK_RIGHT:
1e6feb95 934 dir = wxRIGHT;
8614c467
VZ
935 break;
936
937 default:
938 processed = FALSE;
1e6feb95
VZ
939
940 // just to suppress the compiler warning
941 dir = wxALL;
8614c467
VZ
942 }
943
944 if ( processed )
f048e32f 945 {
1e6feb95
VZ
946 int selOld = radiobox->GetSelection();
947 int selNew = radiobox->GetNextItem
948 (
949 selOld,
950 dir,
951 radiobox->GetWindowStyle()
952 );
3ca6a5f0 953
8614c467
VZ
954 if ( selNew != selOld )
955 {
956 radiobox->SetSelection(selNew);
957
958 // emulate the button click
959 radiobox->SendNotificationEvent();
9a5ccab4 960
8614c467
VZ
961 return 0;
962 }
f048e32f 963 }
9a5ccab4 964 }
21687069
VZ
965 break;
966
fac93396
JS
967#ifdef __WIN32__
968 case WM_HELP:
e421922f
VZ
969 {
970 wxRadioBox *radiobox = (wxRadioBox *)
971 ::GetWindowLong(hwnd, GWL_USERDATA);
fac93396 972
e421922f 973 wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") );
fac93396 974
e421922f 975 bool processed = TRUE;
fac93396 976
e421922f
VZ
977 HELPINFO* info = (HELPINFO*) lParam;
978 // Don't yet process menu help events, just windows
979 if (info->iContextType == HELPINFO_WINDOW)
fac93396 980 {
e421922f
VZ
981 wxWindow* subjectOfHelp = radiobox;
982 bool eventProcessed = FALSE;
983 while (subjectOfHelp && !eventProcessed)
984 {
985 wxHelpEvent helpEvent(wxEVT_HELP, subjectOfHelp->GetId(), wxPoint(info->MousePos.x, info->MousePos.y) ) ; // info->iCtrlId);
986 helpEvent.SetEventObject(radiobox);
987 eventProcessed = radiobox->GetEventHandler()->ProcessEvent(helpEvent);
fac93396 988
e421922f
VZ
989 // Go up the window hierarchy until the event is handled (or not)
990 subjectOfHelp = subjectOfHelp->GetParent();
991 }
992 processed = eventProcessed;
fac93396 993 }
e421922f
VZ
994 else if (info->iContextType == HELPINFO_MENUITEM)
995 {
996 wxHelpEvent helpEvent(wxEVT_HELP, info->iCtrlId) ;
997 helpEvent.SetEventObject(radiobox);
998 processed = radiobox->GetEventHandler()->ProcessEvent(helpEvent);
999 }
1000 else processed = FALSE;
fac93396 1001
e421922f
VZ
1002 if (processed)
1003 return 0;
fac93396 1004
e421922f
VZ
1005 break;
1006 }
1007#endif // __WIN32__
e373f51b
VZ
1008 }
1009
8ad6ad99 1010 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd, message, wParam, lParam);
e373f51b 1011}
42e69d6b
VZ
1012
1013#endif // __WIN32__
e373f51b 1014
1e6feb95 1015#endif // wxUSE_RADIOBOX