]> git.saurik.com Git - wxWidgets.git/blame - src/msw/radiobox.cpp
reformat for readability
[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()
eba99da4 178 GetHwnd(),
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,
eba99da4 201 0, 0, 0, 0, GetHwnd(),
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
272 int count = GetCount();
273 for ( int i = 0; i < count; i++ )
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 );
317 event.SetString( GetString(m_selectedButton) );
318 event.SetEventObject( this );
319 ProcessCommand(event);
2bda0e17
KB
320}
321
bd0a76e2
VZ
322// ----------------------------------------------------------------------------
323// simple accessors
324// ----------------------------------------------------------------------------
325
326int wxRadioBox::GetCount() const
2bda0e17 327{
bd0a76e2
VZ
328 return m_radioButtons->GetCount();
329}
d66a042c 330
bd0a76e2
VZ
331void wxRadioBox::SetString(int item, const wxString& label)
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
1e6feb95 358wxString wxRadioBox::GetString(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
1a87edf2 377bool wxRadioBox::Enable(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
387bool wxRadioBox::IsItemEnabled(int item) const
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
789f6795 396bool wxRadioBox::Show(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
3bfa7be9
VZ
412bool wxRadioBox::IsItemShown(int item) const
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;
bd0a76e2
VZ
435 const int count = GetCount();
436 for ( 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;
faa49bfd 481 GetTextExtent(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
eba99da4
JS
512 int y_offset = 0;
513 int x_offset = 0;
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
bd0a76e2
VZ
568 const int count = GetCount();
569 for ( 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
579 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)
624 ::MoveWindow((*m_radioButtons)[i],
3ca6a5f0 625 x_offset, y_offset, widthBtn, maxHeight,
f048e32f 626 TRUE);
4438caf4 627
3ca6a5f0
BP
628 // where do we put the next button?
629 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
4438caf4 630 {
3ca6a5f0 631 // below this one
4438caf4
VZ
632 y_offset += maxHeight;
633 if (m_radioWidth[0]>0)
634 y_offset += cy1/2;
635 }
636 else
3ca6a5f0
BP
637 {
638 // to the right of this one
639 x_offset += widthBtn + cx1;
640 }
1f916a19 641 }
1f916a19
JS
642}
643
c3732409
VZ
644// ----------------------------------------------------------------------------
645// radio box drawing
646// ----------------------------------------------------------------------------
647
648#ifndef __WXWINCE__
649
650WXHRGN wxRadioBox::MSWGetRegionWithoutChildren()
651{
652 RECT rc;
653 ::GetWindowRect(GetHwnd(), &rc);
654 HRGN hrgn = ::CreateRectRgn(rc.left, rc.top, rc.right + 1, rc.bottom + 1);
655
656 const size_t count = GetCount();
657 for ( size_t i = 0; i < count; ++i )
658 {
455c8f1d
VZ
659 // don't clip out hidden children
660 if ( !IsItemShown(i) )
661 continue;
662
c3732409
VZ
663 ::GetWindowRect((*m_radioButtons)[i], &rc);
664 AutoHRGN hrgnchild(::CreateRectRgnIndirect(&rc));
665 ::CombineRgn(hrgn, hrgn, hrgnchild, RGN_DIFF);
666 }
667
668 return (WXHRGN)hrgn;
669}
670
1a784dfc
VZ
671WXLRESULT
672wxRadioBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
673{
8e44f3ca
JS
674 // FIXME: Without this, the radiobox corrupts other controls as it moves
675 // in a dynamic layout. Refreshing causes flicker, but it's better than
676 // leaving droppings. Note that for some reason, wxStaticBox doesn't need
677 // this (perhaps because it has no real children?)
9705fbe9 678 if ( nMsg == WM_MOVE )
8e44f3ca
JS
679 {
680 WXLRESULT res = wxControl::MSWWindowProc(nMsg, wParam, lParam);
681 wxRect rect = GetRect();
682 GetParent()->Refresh(true, & rect);
683 return res;
684 }
1a784dfc
VZ
685
686 return wxStaticBox::MSWWindowProc(nMsg, wParam, lParam);
687}
688
c3732409
VZ
689#endif // __WXWINCE__
690
e373f51b
VZ
691// ---------------------------------------------------------------------------
692// window proc for radio buttons
693// ---------------------------------------------------------------------------
694
695LRESULT APIENTRY _EXPORT wxRadioBtnWndProc(HWND hwnd,
8614c467 696 UINT message,
e373f51b
VZ
697 WPARAM wParam,
698 LPARAM lParam)
699{
8614c467 700 switch ( message )
e373f51b 701 {
8614c467
VZ
702 case WM_GETDLGCODE:
703 // we must tell IsDialogMessage()/our kbd processing code that we
704 // want to process arrows ourselves because neither of them is
705 // smart enough to handle arrows properly for us
706 {
8ad6ad99 707 long lDlgCode = ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd,
8614c467 708 message, wParam, lParam);
8ad6ad99 709
8614c467
VZ
710 return lDlgCode | DLGC_WANTARROWS;
711 }
e373f51b 712
8614c467
VZ
713#if wxUSE_TOOLTIPS
714 case WM_NOTIFY:
f048e32f 715 {
8614c467 716 NMHDR* hdr = (NMHDR *)lParam;
2b5f62a0 717 if ( hdr->code == TTN_NEEDTEXT )
e373f51b 718 {
975b6bcf
VZ
719 wxRadioBox *
720 radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd);
e373f51b 721
8614c467
VZ
722 wxCHECK_MSG( radiobox, 0,
723 wxT("radio button without radio box?") );
724
725 wxToolTip *tooltip = radiobox->GetToolTip();
726 if ( tooltip )
727 {
728 TOOLTIPTEXT *ttt = (TOOLTIPTEXT *)lParam;
729 ttt->lpszText = (wxChar *)tooltip->GetTip().c_str();
730 }
731
732 // processed
733 return 0;
e373f51b 734 }
f048e32f 735 }
8614c467 736 break;
f048e32f 737#endif // wxUSE_TOOLTIPS
f048e32f 738
8614c467 739 case WM_KEYDOWN:
9a5ccab4 740 {
975b6bcf 741 wxRadioBox *radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd);
f048e32f 742
8614c467 743 wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") );
f048e32f 744
4d08943e 745 bool processed = true;
f048e32f 746
e421922f 747 wxDirection dir;
8614c467
VZ
748 switch ( wParam )
749 {
750 case VK_UP:
1e6feb95 751 dir = wxUP;
8614c467 752 break;
f048e32f 753
8614c467 754 case VK_LEFT:
1e6feb95 755 dir = wxLEFT;
8614c467 756 break;
f048e32f 757
8614c467 758 case VK_DOWN:
1e6feb95 759 dir = wxDOWN;
8614c467 760 break;
f048e32f 761
8614c467 762 case VK_RIGHT:
1e6feb95 763 dir = wxRIGHT;
8614c467
VZ
764 break;
765
766 default:
4d08943e 767 processed = false;
1e6feb95
VZ
768
769 // just to suppress the compiler warning
770 dir = wxALL;
8614c467
VZ
771 }
772
773 if ( processed )
f048e32f 774 {
1e6feb95
VZ
775 int selOld = radiobox->GetSelection();
776 int selNew = radiobox->GetNextItem
777 (
778 selOld,
779 dir,
780 radiobox->GetWindowStyle()
781 );
3ca6a5f0 782
8614c467
VZ
783 if ( selNew != selOld )
784 {
785 radiobox->SetSelection(selNew);
974931fe 786 radiobox->SetFocus();
8614c467
VZ
787
788 // emulate the button click
789 radiobox->SendNotificationEvent();
9a5ccab4 790
8614c467
VZ
791 return 0;
792 }
f048e32f 793 }
9a5ccab4 794 }
21687069
VZ
795 break;
796
1dab049c
VZ
797 case WM_SETFOCUS:
798 case WM_KILLFOCUS:
799 {
975b6bcf 800 wxRadioBox *radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd);
1dab049c
VZ
801
802 wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") );
803
804 // if we don't do this, no focus events are generated for the
805 // radiobox and, besides, we need to notify the parent about
806 // the focus change, otherwise the focus handling logic in
807 // wxControlContainer doesn't work
808 if ( message == WM_SETFOCUS )
809 radiobox->HandleSetFocus((WXHWND)wParam);
810 else
811 radiobox->HandleKillFocus((WXHWND)wParam);
812 }
813 break;
814
bd0a76e2 815#ifndef __WXWINCE__
fac93396 816 case WM_HELP:
e421922f 817 {
975b6bcf 818 wxRadioBox *radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd);
fac93396 819
e421922f 820 wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") );
fac93396 821
bd0a76e2
VZ
822 bool processed = false;
823
824 wxEvtHandler * const handler = radiobox->GetEventHandler();
fac93396 825
e421922f 826 HELPINFO* info = (HELPINFO*) lParam;
bd0a76e2 827 if ( info->iContextType == HELPINFO_WINDOW )
fac93396 828 {
bd0a76e2
VZ
829 for ( wxWindow* subjectOfHelp = radiobox;
830 subjectOfHelp;
831 subjectOfHelp = subjectOfHelp->GetParent() )
e421922f 832 {
bd0a76e2
VZ
833 wxHelpEvent helpEvent(wxEVT_HELP,
834 subjectOfHelp->GetId(),
835 wxPoint(info->MousePos.x,
836 info->MousePos.y));
e421922f 837 helpEvent.SetEventObject(radiobox);
bd0a76e2
VZ
838 if ( handler->ProcessEvent(helpEvent) )
839 {
840 processed = true;
841 break;
842 }
e421922f 843 }
fac93396 844 }
e421922f
VZ
845 else if (info->iContextType == HELPINFO_MENUITEM)
846 {
bd0a76e2 847 wxHelpEvent helpEvent(wxEVT_HELP, info->iCtrlId);
e421922f 848 helpEvent.SetEventObject(radiobox);
bd0a76e2 849 processed = handler->ProcessEvent(helpEvent);
e421922f 850 }
bd0a76e2
VZ
851
852 if ( processed )
e421922f 853 return 0;
e421922f 854 }
c3732409 855 break;
bd0a76e2 856#endif // !__WXWINCE__
e373f51b
VZ
857 }
858
8ad6ad99 859 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd, message, wParam, lParam);
e373f51b 860}
42e69d6b 861
1e6feb95 862#endif // wxUSE_RADIOBOX