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