]> git.saurik.com Git - wxWidgets.git/blame - src/msw/radiobox.cpp
Minor corrections to native renderer. I also corrected
[wxWidgets.git] / src / msw / radiobox.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
faa49bfd 2// Name: src/msw/radiobox.cpp
3ca6a5f0 3// Purpose: wxRadioBox implementation
2bda0e17
KB
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6c9a19aa 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
e373f51b
VZ
12// ===========================================================================
13// declarations
14// ===========================================================================
15
16// ---------------------------------------------------------------------------
17// headers
18// ---------------------------------------------------------------------------
19
2bda0e17
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
da07e033 24 #pragma hdrstop
2bda0e17
KB
25#endif
26
1e6feb95
VZ
27#if wxUSE_RADIOBOX
28
2bda0e17 29#ifndef WX_PRECOMP
da07e033
VZ
30 #include "wx/bitmap.h"
31 #include "wx/brush.h"
32 #include "wx/radiobox.h"
f6bcfd97 33 #include "wx/settings.h"
381dd4bf 34 #include "wx/log.h"
2bda0e17
KB
35#endif
36
bd0a76e2 37#include "wx/msw/subwin.h"
2bda0e17 38
f048e32f 39#if wxUSE_TOOLTIPS
ae090fdb 40 #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
8614c467 41 #include <commctrl.h>
5ace0213 42 #endif
f048e32f
VZ
43 #include "wx/tooltip.h"
44#endif // wxUSE_TOOLTIPS
45
3ff066a4 46// TODO: wxCONSTRUCTOR
bc9fb572
JS
47#if 0 // wxUSE_EXTENDED_RTTI
48WX_DEFINE_FLAGS( wxRadioBoxStyle )
49
3ff066a4 50wxBEGIN_FLAGS( wxRadioBoxStyle )
bc9fb572
JS
51 // new style border flags, we put them first to
52 // use them for streaming out
3ff066a4
SC
53 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
54 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
55 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
56 wxFLAGS_MEMBER(wxBORDER_RAISED)
57 wxFLAGS_MEMBER(wxBORDER_STATIC)
58 wxFLAGS_MEMBER(wxBORDER_NONE)
4d08943e 59
bc9fb572 60 // old style border flags
3ff066a4
SC
61 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
62 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
63 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
64 wxFLAGS_MEMBER(wxRAISED_BORDER)
65 wxFLAGS_MEMBER(wxSTATIC_BORDER)
cb0afb26 66 wxFLAGS_MEMBER(wxBORDER)
bc9fb572
JS
67
68 // standard window styles
3ff066a4
SC
69 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
70 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
71 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
72 wxFLAGS_MEMBER(wxWANTS_CHARS)
cb0afb26 73 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
3ff066a4
SC
74 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
75 wxFLAGS_MEMBER(wxVSCROLL)
76 wxFLAGS_MEMBER(wxHSCROLL)
77
78 wxFLAGS_MEMBER(wxRA_SPECIFY_COLS)
79 wxFLAGS_MEMBER(wxRA_HORIZONTAL)
80 wxFLAGS_MEMBER(wxRA_SPECIFY_ROWS)
81 wxFLAGS_MEMBER(wxRA_VERTICAL)
82
83wxEND_FLAGS( wxRadioBoxStyle )
bc9fb572
JS
84
85IMPLEMENT_DYNAMIC_CLASS_XTI(wxRadioBox, wxControl,"wx/radiobox.h")
86
3ff066a4
SC
87wxBEGIN_PROPERTIES_TABLE(wxRadioBox)
88 wxEVENT_PROPERTY( Select , wxEVT_COMMAND_RADIOBOX_SELECTED , wxCommandEvent )
89 wxPROPERTY_FLAGS( WindowStyle , wxRadioBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
90wxEND_PROPERTIES_TABLE()
bc9fb572
JS
91
92#else
d3acd369 93IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl)
bc9fb572 94#endif
2bda0e17 95
066f1b7a 96/*
4d08943e
WS
97 selection
98 content
99 label
100 dimension
101 item
066f1b7a
SC
102*/
103
e373f51b
VZ
104// ---------------------------------------------------------------------------
105// private functions
106// ---------------------------------------------------------------------------
107
e373f51b
VZ
108// wnd proc for radio buttons
109LRESULT APIENTRY _EXPORT wxRadioBtnWndProc(HWND hWnd,
110 UINT message,
111 WPARAM wParam,
112 LPARAM lParam);
113
114// ---------------------------------------------------------------------------
115// global vars
116// ---------------------------------------------------------------------------
117
118// the pointer to standard radio button wnd proc
457e6c54 119static WXFARPROC s_wndprocRadioBtn = (WXFARPROC)NULL;
e373f51b
VZ
120
121// ===========================================================================
122// implementation
123// ===========================================================================
124
125// ---------------------------------------------------------------------------
bd0a76e2 126// wxRadioBox creation
e373f51b
VZ
127// ---------------------------------------------------------------------------
128
2bda0e17 129// Radio box item
bd0a76e2 130void wxRadioBox::Init()
2bda0e17 131{
bd0a76e2 132 m_selectedButton = wxNOT_FOUND;
da07e033 133 m_radioButtons = NULL;
e373f51b
VZ
134 m_radioWidth = NULL;
135 m_radioHeight = NULL;
2bda0e17
KB
136}
137
f048e32f
VZ
138bool wxRadioBox::Create(wxWindow *parent,
139 wxWindowID id,
140 const wxString& title,
141 const wxPoint& pos,
142 const wxSize& size,
143 int n,
144 const wxString choices[],
145 int majorDim,
146 long style,
147 const wxValidator& val,
148 const wxString& name)
2bda0e17 149{
f048e32f 150 // common initialization
bd0a76e2 151 if ( !wxStaticBox::Create(parent, id, title, pos, size, style, name) )
4d08943e 152 return false;
2bda0e17 153
bd0a76e2
VZ
154#if wxUSE_VALIDATORS
155 SetValidator(val);
156#else
157 wxUnusedVar(val);
158#endif // wxUSE_VALIDATORS/!wxUSE_VALIDATORS
da07e033 159
bd0a76e2 160 m_radioButtons = new wxSubwindows(n);
e373f51b
VZ
161 m_radioWidth = new int[n];
162 m_radioHeight = new int[n];
f048e32f 163
f048e32f
VZ
164 for ( int i = 0; i < n; i++ )
165 {
166 m_radioWidth[i] =
4d08943e 167 m_radioHeight[i] = wxDefaultCoord;
d3acd369 168 long styleBtn = BS_AUTORADIOBUTTON | WS_TABSTOP | WS_CHILD | WS_VISIBLE;
1f621c2f 169 if ( i == 0 )
f048e32f
VZ
170 styleBtn |= WS_GROUP;
171
da07e033 172 long newId = NewControlId();
da07e033 173
f048e32f
VZ
174 HWND hwndBtn = ::CreateWindow(_T("BUTTON"),
175 choices[i],
176 styleBtn,
177 0, 0, 0, 0, // will be set in SetSize()
99c613c5 178 GetHwndOf(parent),
f048e32f
VZ
179 (HMENU)newId,
180 wxGetInstance(),
e373f51b 181 NULL);
2bda0e17 182
f048e32f
VZ
183 if ( !hwndBtn )
184 {
f6bcfd97 185 wxLogLastError(wxT("CreateWindow(radio btn)"));
f048e32f 186
4d08943e 187 return false;
f048e32f
VZ
188 }
189
bd0a76e2 190 (*m_radioButtons)[i] = hwndBtn;
42e69d6b 191
e373f51b 192 SubclassRadioButton((WXHWND)hwndBtn);
2bda0e17 193
f048e32f 194 m_subControls.Add(newId);
da07e033 195 }
e373f51b 196
da07e033 197 // Create a dummy radio control to end the group.
f048e32f 198 (void)::CreateWindow(_T("BUTTON"),
fda7962d 199 wxEmptyString,
f048e32f 200 WS_GROUP | BS_AUTORADIOBUTTON | WS_CHILD,
99c613c5 201 0, 0, 0, 0, GetHwndOf(parent),
e373f51b 202 (HMENU)NewControlId(), wxGetInstance(), NULL);
2bda0e17 203
bd0a76e2
VZ
204 m_radioButtons->SetFont(GetFont());
205
64133c38
JS
206#ifdef __WXWINCE__
207 // Set the z-order correctly
d26e1ab2 208 SetWindowPos(GetHwnd(), HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
64133c38
JS
209#endif
210
f3984afa 211 SetMajorDim(majorDim == 0 ? n : majorDim, style);
da07e033 212 SetSelection(0);
f048e32f 213 SetSize(pos.x, pos.y, size.x, size.y);
2bda0e17 214
7a5a5718
RD
215 // Now that we have items determine what is the best size and set it.
216 SetBestSize(size);
4d08943e
WS
217
218 return true;
2bda0e17 219}
2bda0e17 220
584ad2a3
MB
221bool wxRadioBox::Create(wxWindow *parent,
222 wxWindowID id,
223 const wxString& title,
224 const wxPoint& pos,
225 const wxSize& size,
226 const wxArrayString& choices,
227 int majorDim,
228 long style,
229 const wxValidator& val,
230 const wxString& name)
231{
232 wxCArrayString chs(choices);
233 return Create(parent, id, title, pos, size, chs.GetCount(),
234 chs.GetStrings(), majorDim, style, val, name);
235}
236
e373f51b 237wxRadioBox::~wxRadioBox()
2bda0e17 238{
4d08943e 239 m_isBeingDeleted = true;
1eb20d4a 240
bd0a76e2
VZ
241 delete m_radioButtons;
242 delete[] m_radioWidth;
243 delete[] m_radioHeight;
244}
245
246// NB: if this code is changed, wxGetWindowForHWND() which relies on having the
247// radiobox pointer in GWL_USERDATA for radio buttons must be updated too!
248void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn)
249{
250 HWND hwndBtn = (HWND)hWndBtn;
251
252 if ( !s_wndprocRadioBtn )
253 s_wndprocRadioBtn = (WXFARPROC)wxGetWindowProc(hwndBtn);
254
255 wxSetWindowProc(hwndBtn, wxRadioBtnWndProc);
256 wxSetWindowUserData(hwndBtn, this);
257}
258
259// ----------------------------------------------------------------------------
260// events generation
261// ----------------------------------------------------------------------------
262
263bool wxRadioBox::MSWCommand(WXUINT cmd, WXWORD id)
264{
265 if ( cmd == BN_CLICKED )
da07e033 266 {
bd0a76e2
VZ
267 if (id == GetId())
268 return true;
269
270 int selectedButton = wxNOT_FOUND;
271
aa61d352
VZ
272 const unsigned int count = GetCount();
273 for ( unsigned int i = 0; i < count; i++ )
bd0a76e2
VZ
274 {
275 if ( id == wxGetWindowId((*m_radioButtons)[i]) )
276 {
277 selectedButton = i;
278
279 break;
280 }
281 }
282
283 if ( selectedButton == wxNOT_FOUND )
284 {
285 // just ignore it - due to a hack with WM_NCHITTEST handling in our
286 // wnd proc, we can receive dummy click messages when we click near
287 // the radiobox edge (this is ugly but Julian wouldn't let me get
288 // rid of this...)
289 return false;
290 }
291
292 if ( selectedButton != m_selectedButton )
293 {
294 m_selectedButton = selectedButton;
295
296 SendNotificationEvent();
297 }
298 //else: don't generate events when the selection doesn't change
299
300 return true;
da07e033 301 }
bd0a76e2
VZ
302 else
303 return false;
304}
42e69d6b 305
bd0a76e2
VZ
306void wxRadioBox::Command(wxCommandEvent & event)
307{
687706f5 308 SetSelection (event.GetInt());
bd0a76e2
VZ
309 SetFocus();
310 ProcessCommand(event);
311}
2bda0e17 312
bd0a76e2
VZ
313void wxRadioBox::SendNotificationEvent()
314{
315 wxCommandEvent event(wxEVT_COMMAND_RADIOBOX_SELECTED, m_windowId);
316 event.SetInt( m_selectedButton );
aa61d352 317 event.SetString(GetString(m_selectedButton));
bd0a76e2
VZ
318 event.SetEventObject( this );
319 ProcessCommand(event);
2bda0e17
KB
320}
321
bd0a76e2
VZ
322// ----------------------------------------------------------------------------
323// simple accessors
324// ----------------------------------------------------------------------------
325
aa61d352 326unsigned int wxRadioBox::GetCount() const
2bda0e17 327{
bd0a76e2
VZ
328 return m_radioButtons->GetCount();
329}
d66a042c 330
aa61d352 331void wxRadioBox::SetString(unsigned int item, const wxString& label)
bd0a76e2 332{
789f6795 333 wxCHECK_RET( IsValid(item), wxT("invalid radiobox index") );
e373f51b 334
bd0a76e2
VZ
335 m_radioWidth[item] =
336 m_radioHeight[item] = wxDefaultCoord;
2bda0e17 337
bd0a76e2 338 ::SetWindowText((*m_radioButtons)[item], label.c_str());
31582e4e
RD
339
340 InvalidateBestSize();
2bda0e17
KB
341}
342
bd0a76e2 343void wxRadioBox::SetSelection(int N)
2bda0e17 344{
789f6795 345 wxCHECK_RET( IsValid(N), wxT("invalid radiobox index") );
bd0a76e2 346
27758ba7
VZ
347 // unselect the old button
348 if ( m_selectedButton != wxNOT_FOUND )
349 ::SendMessage((*m_radioButtons)[m_selectedButton], BM_SETCHECK, 0, 0L);
350
351 // and select the new one
bd0a76e2
VZ
352 ::SendMessage((*m_radioButtons)[N], BM_SETCHECK, 1, 0L);
353
354 m_selectedButton = N;
2bda0e17
KB
355}
356
357// Find string for position
aa61d352 358wxString wxRadioBox::GetString(unsigned int item) const
2bda0e17 359{
789f6795 360 wxCHECK_MSG( IsValid(item), wxEmptyString,
1e6feb95
VZ
361 wxT("invalid radiobox index") );
362
bd0a76e2
VZ
363 return wxGetWindowText((*m_radioButtons)[item]);
364}
365
366void wxRadioBox::SetFocus()
367{
368 if ( GetCount() > 0 )
369 {
370 ::SetFocus((*m_radioButtons)[m_selectedButton == wxNOT_FOUND
371 ? 0
372 : m_selectedButton]);
373 }
374}
375
376// Enable a specific button
aa61d352 377bool wxRadioBox::Enable(unsigned int item, bool enable)
bd0a76e2 378{
789f6795 379 wxCHECK_MSG( IsValid(item), false,
bd0a76e2
VZ
380 wxT("invalid item in wxRadioBox::Enable()") );
381
789f6795
WS
382 BOOL ret = ::EnableWindow((*m_radioButtons)[item], enable);
383
3bfa7be9
VZ
384 return (ret == 0) != enable;
385}
386
aa61d352 387bool wxRadioBox::IsItemEnabled(unsigned int item) const
3bfa7be9
VZ
388{
389 wxCHECK_MSG( IsValid(item), false,
f2be5580 390 wxT("invalid item in wxRadioBox::IsItemEnabled()") );
3bfa7be9
VZ
391
392 return ::IsWindowEnabled((*m_radioButtons)[item]) != 0;
2bda0e17
KB
393}
394
bd0a76e2 395// Show a specific button
aa61d352 396bool wxRadioBox::Show(unsigned int item, bool show)
bd0a76e2 397{
789f6795 398 wxCHECK_MSG( IsValid(item), false,
bd0a76e2
VZ
399 wxT("invalid item in wxRadioBox::Show()") );
400
789f6795
WS
401 BOOL ret = ::ShowWindow((*m_radioButtons)[item], show ? SW_SHOW : SW_HIDE);
402
3bfa7be9
VZ
403 bool changed = (ret != 0) != show;
404 if ( changed )
405 {
31582e4e 406 InvalidateBestSize();
3bfa7be9
VZ
407 }
408
31582e4e 409 return changed;
bd0a76e2
VZ
410}
411
aa61d352 412bool wxRadioBox::IsItemShown(unsigned int item) const
3bfa7be9
VZ
413{
414 wxCHECK_MSG( IsValid(item), false,
f2be5580 415 wxT("invalid item in wxRadioBox::IsItemShown()") );
3bfa7be9
VZ
416
417 // don't use IsWindowVisible() here because it would return false if the
418 // radiobox itself is hidden while we want to only return false if this
419 // button specifically is hidden
420 return (::GetWindowLong((*m_radioButtons)[item],
421 GWL_STYLE) & WS_VISIBLE) != 0;
422}
423
bd0a76e2
VZ
424WX_FORWARD_STD_METHODS_TO_SUBWINDOWS(wxRadioBox, wxStaticBox, m_radioButtons)
425
3ca6a5f0
BP
426// ----------------------------------------------------------------------------
427// size calculations
428// ----------------------------------------------------------------------------
429
430wxSize wxRadioBox::GetMaxButtonSize() const
431{
432 // calculate the max button size
433 int widthMax = 0,
434 heightMax = 0;
aa61d352
VZ
435 const unsigned int count = GetCount();
436 for ( unsigned int i = 0 ; i < count; i++ )
3ca6a5f0
BP
437 {
438 int width, height;
439 if ( m_radioWidth[i] < 0 )
440 {
bd0a76e2 441 GetTextExtent(wxGetWindowText((*m_radioButtons)[i]), &width, &height);
3ca6a5f0
BP
442
443 // adjust the size to take into account the radio box itself
444 // FIXME this is totally bogus!
445 width += RADIO_SIZE;
446 height *= 3;
447 height /= 2;
448 }
449 else
450 {
451 width = m_radioWidth[i];
452 height = m_radioHeight[i];
453 }
454
455 if ( widthMax < width )
456 widthMax = width;
457 if ( heightMax < height )
458 heightMax = height;
459 }
460
461 return wxSize(widthMax, heightMax);
462}
463
464wxSize wxRadioBox::GetTotalButtonSize(const wxSize& sizeBtn) const
465{
466 // the radiobox should be big enough for its buttons
467 int cx1, cy1;
7a5e53ab 468 wxGetCharSize(m_hWnd, &cx1, &cy1, GetFont());
3ca6a5f0
BP
469
470 int extraHeight = cy1;
471
21e0a4d5
VZ
472 int height = GetRowCount() * sizeBtn.y + cy1/2 + extraHeight;
473 int width = GetColumnCount() * (sizeBtn.x + cx1) + cx1;
3ca6a5f0 474
c7fd6717 475 // Add extra space under the label, if it exists.
1a87edf2 476 if (!wxControl::GetLabel().empty())
c7fd6717
JS
477 height += cy1/2;
478
3ca6a5f0
BP
479 // and also wide enough for its label
480 int widthLabel;
9f8130d6 481 GetTextExtent(wxStripMenuCodes(GetLabel()), &widthLabel, NULL);
3ca6a5f0
BP
482 widthLabel += RADIO_SIZE; // FIXME this is bogus too
483 if ( widthLabel > width )
484 width = widthLabel;
485
486 return wxSize(width, height);
487}
488
489wxSize wxRadioBox::DoGetBestSize() const
490{
31582e4e
RD
491 wxSize best = GetTotalButtonSize(GetMaxButtonSize());
492 CacheBestSize(best);
493 return best;
3ca6a5f0
BP
494}
495
1f916a19 496// Restored old code.
bfc6fde4 497void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
1f916a19 498{
4438caf4
VZ
499 int currentX, currentY;
500 GetPosition(&currentX, &currentY);
c219cecc
VZ
501 int widthOld, heightOld;
502 GetSize(&widthOld, &heightOld);
503
4438caf4
VZ
504 int xx = x;
505 int yy = y;
506
4d08943e 507 if (x == wxDefaultCoord && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
4438caf4 508 xx = currentX;
4d08943e 509 if (y == wxDefaultCoord && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
4438caf4
VZ
510 yy = currentY;
511
99c613c5
JG
512 int y_offset = yy;
513 int x_offset = xx;
f048e32f 514
3ca6a5f0 515 int cx1, cy1;
7a5e53ab 516 wxGetCharSize(m_hWnd, &cx1, &cy1, GetFont());
4438caf4
VZ
517
518 // Attempt to have a look coherent with other platforms: We compute the
519 // biggest toggle dim, then we align all items according this value.
3ca6a5f0
BP
520 wxSize maxSize = GetMaxButtonSize();
521 int maxWidth = maxSize.x,
522 maxHeight = maxSize.y;
4438caf4 523
3ca6a5f0
BP
524 wxSize totSize = GetTotalButtonSize(maxSize);
525 int totWidth = totSize.x,
526 totHeight = totSize.y;
527
528 // only change our width/height if asked for
4d08943e 529 if ( width == wxDefaultCoord )
1f916a19 530 {
3ca6a5f0
BP
531 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
532 width = totWidth;
4438caf4 533 else
3ca6a5f0 534 width = widthOld;
1f916a19 535 }
1f916a19 536
4d08943e 537 if ( height == wxDefaultCoord )
4438caf4 538 {
3ca6a5f0
BP
539 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
540 height = totHeight;
4438caf4 541 else
3ca6a5f0
BP
542 height = heightOld;
543 }
4438caf4 544
7d86a2d4 545 DoMoveWindow(xx, yy, width, height);
c219cecc 546
3ca6a5f0
BP
547 // Now position all the buttons: the current button will be put at
548 // wxPoint(x_offset, y_offset) and the new row/column will start at
549 // startX/startY. The size of all buttons will be the same wxSize(maxWidth,
550 // maxHeight) except for the buttons in the last column which should extend
551 // to the right border of radiobox and thus can be wider than this.
c219cecc 552
3ca6a5f0 553 // Also, remember that wxRA_SPECIFY_COLS means that we arrange buttons in
21e0a4d5 554 // left to right order and GetMajorDim() is the number of columns while
3ca6a5f0 555 // wxRA_SPECIFY_ROWS means that the buttons are arranged top to bottom and
21e0a4d5 556 // GetMajorDim() is the number of rows.
4438caf4 557
3ca6a5f0
BP
558 x_offset += cx1;
559 y_offset += cy1;
1f916a19 560
c7fd6717 561 // Add extra space under the label, if it exists.
1a87edf2 562 if (!wxControl::GetLabel().empty())
c7fd6717 563 y_offset += cy1/2;
3ca6a5f0 564
4438caf4
VZ
565 int startX = x_offset;
566 int startY = y_offset;
1f916a19 567
aa61d352
VZ
568 const unsigned int count = GetCount();
569 for (unsigned int i = 0; i < count; i++)
1f916a19 570 {
3ca6a5f0
BP
571 // the last button in the row may be wider than the other ones as the
572 // radiobox may be wider than the sum of the button widths (as it
573 // happens, for example, when the radiobox label is very long)
574 bool isLastInTheRow;
575 if ( m_windowStyle & wxRA_SPECIFY_COLS )
576 {
577 // item is the last in its row if it is a multiple of the number of
578 // columns or if it is just the last item
aa61d352 579 unsigned int n = i + 1;
21e0a4d5 580 isLastInTheRow = ((n % GetMajorDim()) == 0) || (n == count);
3ca6a5f0
BP
581 }
582 else // wxRA_SPECIFY_ROWS
4438caf4 583 {
3ca6a5f0 584 // item is the last in the row if it is in the last columns
21e0a4d5 585 isLastInTheRow = i >= (count/GetMajorDim())*GetMajorDim();
3ca6a5f0
BP
586 }
587
588 // is this the start of new row/column?
21e0a4d5 589 if ( i && (i % GetMajorDim() == 0) )
3ca6a5f0
BP
590 {
591 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
4438caf4 592 {
3ca6a5f0 593 // start of new column
4438caf4
VZ
594 y_offset = startY;
595 x_offset += maxWidth + cx1;
596 }
3ca6a5f0 597 else // start of new row
4438caf4
VZ
598 {
599 x_offset = startX;
600 y_offset += maxHeight;
601 if (m_radioWidth[0]>0)
602 y_offset += cy1/2;
603 }
604 }
3ca6a5f0
BP
605
606 int widthBtn;
607 if ( isLastInTheRow )
4438caf4 608 {
3ca6a5f0
BP
609 // make the button go to the end of radio box
610 widthBtn = startX + width - x_offset - 2*cx1;
611 if ( widthBtn < maxWidth )
612 widthBtn = maxWidth;
4438caf4
VZ
613 }
614 else
615 {
3ca6a5f0
BP
616 // normal button, always of the same size
617 widthBtn = maxWidth;
4438caf4 618 }
1f916a19 619
bd0a76e2
VZ
620 // make all buttons of the same, maximal size - like this they cover
621 // the radiobox entirely and the radiobox tooltips are always shown
622 // (otherwise they are not when the mouse pointer is in the radiobox
623 // part not belonging to any radiobutton)
99c613c5 624 DoMoveSibling((*m_radioButtons)[i], x_offset, y_offset, widthBtn, maxHeight);
4438caf4 625
3ca6a5f0
BP
626 // where do we put the next button?
627 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
4438caf4 628 {
3ca6a5f0 629 // below this one
4438caf4
VZ
630 y_offset += maxHeight;
631 if (m_radioWidth[0]>0)
632 y_offset += cy1/2;
633 }
634 else
3ca6a5f0
BP
635 {
636 // to the right of this one
637 x_offset += widthBtn + cx1;
638 }
1f916a19 639 }
1f916a19
JS
640}
641
c3732409
VZ
642// ----------------------------------------------------------------------------
643// radio box drawing
644// ----------------------------------------------------------------------------
645
646#ifndef __WXWINCE__
647
648WXHRGN wxRadioBox::MSWGetRegionWithoutChildren()
649{
650 RECT rc;
651 ::GetWindowRect(GetHwnd(), &rc);
652 HRGN hrgn = ::CreateRectRgn(rc.left, rc.top, rc.right + 1, rc.bottom + 1);
653
aa61d352
VZ
654 const unsigned int count = GetCount();
655 for ( unsigned int i = 0; i < count; ++i )
c3732409 656 {
455c8f1d
VZ
657 // don't clip out hidden children
658 if ( !IsItemShown(i) )
659 continue;
660
c3732409
VZ
661 ::GetWindowRect((*m_radioButtons)[i], &rc);
662 AutoHRGN hrgnchild(::CreateRectRgnIndirect(&rc));
663 ::CombineRgn(hrgn, hrgn, hrgnchild, RGN_DIFF);
664 }
665
666 return (WXHRGN)hrgn;
667}
668
669#endif // __WXWINCE__
670
e373f51b
VZ
671// ---------------------------------------------------------------------------
672// window proc for radio buttons
673// ---------------------------------------------------------------------------
674
675LRESULT APIENTRY _EXPORT wxRadioBtnWndProc(HWND hwnd,
8614c467 676 UINT message,
e373f51b
VZ
677 WPARAM wParam,
678 LPARAM lParam)
679{
8614c467 680 switch ( message )
e373f51b 681 {
8614c467
VZ
682 case WM_GETDLGCODE:
683 // we must tell IsDialogMessage()/our kbd processing code that we
684 // want to process arrows ourselves because neither of them is
685 // smart enough to handle arrows properly for us
686 {
8ad6ad99 687 long lDlgCode = ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd,
8614c467 688 message, wParam, lParam);
8ad6ad99 689
8614c467
VZ
690 return lDlgCode | DLGC_WANTARROWS;
691 }
e373f51b 692
8614c467
VZ
693#if wxUSE_TOOLTIPS
694 case WM_NOTIFY:
f048e32f 695 {
8614c467 696 NMHDR* hdr = (NMHDR *)lParam;
2b5f62a0 697 if ( hdr->code == TTN_NEEDTEXT )
e373f51b 698 {
975b6bcf
VZ
699 wxRadioBox *
700 radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd);
e373f51b 701
8614c467
VZ
702 wxCHECK_MSG( radiobox, 0,
703 wxT("radio button without radio box?") );
704
705 wxToolTip *tooltip = radiobox->GetToolTip();
706 if ( tooltip )
707 {
708 TOOLTIPTEXT *ttt = (TOOLTIPTEXT *)lParam;
709 ttt->lpszText = (wxChar *)tooltip->GetTip().c_str();
710 }
711
712 // processed
713 return 0;
e373f51b 714 }
f048e32f 715 }
8614c467 716 break;
f048e32f 717#endif // wxUSE_TOOLTIPS
f048e32f 718
8614c467 719 case WM_KEYDOWN:
9a5ccab4 720 {
975b6bcf 721 wxRadioBox *radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd);
f048e32f 722
8614c467 723 wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") );
f048e32f 724
4d08943e 725 bool processed = true;
f048e32f 726
e421922f 727 wxDirection dir;
8614c467
VZ
728 switch ( wParam )
729 {
730 case VK_UP:
1e6feb95 731 dir = wxUP;
8614c467 732 break;
f048e32f 733
8614c467 734 case VK_LEFT:
1e6feb95 735 dir = wxLEFT;
8614c467 736 break;
f048e32f 737
8614c467 738 case VK_DOWN:
1e6feb95 739 dir = wxDOWN;
8614c467 740 break;
f048e32f 741
8614c467 742 case VK_RIGHT:
1e6feb95 743 dir = wxRIGHT;
8614c467
VZ
744 break;
745
746 default:
4d08943e 747 processed = false;
1e6feb95
VZ
748
749 // just to suppress the compiler warning
750 dir = wxALL;
8614c467
VZ
751 }
752
753 if ( processed )
f048e32f 754 {
1e6feb95
VZ
755 int selOld = radiobox->GetSelection();
756 int selNew = radiobox->GetNextItem
757 (
758 selOld,
759 dir,
760 radiobox->GetWindowStyle()
761 );
3ca6a5f0 762
8614c467
VZ
763 if ( selNew != selOld )
764 {
765 radiobox->SetSelection(selNew);
974931fe 766 radiobox->SetFocus();
8614c467
VZ
767
768 // emulate the button click
769 radiobox->SendNotificationEvent();
9a5ccab4 770
8614c467
VZ
771 return 0;
772 }
f048e32f 773 }
9a5ccab4 774 }
21687069
VZ
775 break;
776
1dab049c
VZ
777 case WM_SETFOCUS:
778 case WM_KILLFOCUS:
779 {
975b6bcf 780 wxRadioBox *radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd);
1dab049c
VZ
781
782 wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") );
783
784 // if we don't do this, no focus events are generated for the
785 // radiobox and, besides, we need to notify the parent about
786 // the focus change, otherwise the focus handling logic in
787 // wxControlContainer doesn't work
788 if ( message == WM_SETFOCUS )
789 radiobox->HandleSetFocus((WXHWND)wParam);
790 else
791 radiobox->HandleKillFocus((WXHWND)wParam);
792 }
793 break;
794
bd0a76e2 795#ifndef __WXWINCE__
fac93396 796 case WM_HELP:
e421922f 797 {
975b6bcf 798 wxRadioBox *radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd);
fac93396 799
e421922f 800 wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") );
fac93396 801
bd0a76e2
VZ
802 bool processed = false;
803
804 wxEvtHandler * const handler = radiobox->GetEventHandler();
fac93396 805
e421922f 806 HELPINFO* info = (HELPINFO*) lParam;
bd0a76e2 807 if ( info->iContextType == HELPINFO_WINDOW )
fac93396 808 {
bd0a76e2
VZ
809 for ( wxWindow* subjectOfHelp = radiobox;
810 subjectOfHelp;
811 subjectOfHelp = subjectOfHelp->GetParent() )
e421922f 812 {
bd0a76e2
VZ
813 wxHelpEvent helpEvent(wxEVT_HELP,
814 subjectOfHelp->GetId(),
815 wxPoint(info->MousePos.x,
816 info->MousePos.y));
e421922f 817 helpEvent.SetEventObject(radiobox);
bd0a76e2
VZ
818 if ( handler->ProcessEvent(helpEvent) )
819 {
820 processed = true;
821 break;
822 }
e421922f 823 }
fac93396 824 }
e421922f
VZ
825 else if (info->iContextType == HELPINFO_MENUITEM)
826 {
bd0a76e2 827 wxHelpEvent helpEvent(wxEVT_HELP, info->iCtrlId);
e421922f 828 helpEvent.SetEventObject(radiobox);
bd0a76e2 829 processed = handler->ProcessEvent(helpEvent);
e421922f 830 }
bd0a76e2
VZ
831
832 if ( processed )
e421922f 833 return 0;
e421922f 834 }
c3732409 835 break;
bd0a76e2 836#endif // !__WXWINCE__
e373f51b
VZ
837 }
838
8ad6ad99 839 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd, message, wParam, lParam);
e373f51b 840}
42e69d6b 841
1e6feb95 842#endif // wxUSE_RADIOBOX