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