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