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