]> git.saurik.com Git - wxWidgets.git/blame - src/os2/radiobox.cpp
Improved error handling.
[wxWidgets.git] / src / os2 / radiobox.cpp
CommitLineData
0e320a79 1/////////////////////////////////////////////////////////////////////////////
11e62fe6 2// Name: src/os2/radiobox.cpp
0e320a79 3// Purpose: wxRadioBox
cdf1e714 4// Author: David Webster
0e320a79 5// Modified by:
cdf1e714 6// Created: 10/12/99
0e320a79 7// RCS-ID: $Id$
cdf1e714 8// Copyright: (c) David Webster
65571936 9// Licence: wxWindows licence
0e320a79
DW
10/////////////////////////////////////////////////////////////////////////////
11
cdf1e714
DW
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
8228b893
WS
15#if wxUSE_RADIOBOX
16
cc11cc69
WS
17#include "wx/radiobox.h"
18
cdf1e714
DW
19#ifndef WX_PRECOMP
20 #include <stdio.h>
b59c98dd
SN
21 #include "wx/wxchar.h"
22 #include "wx/string.h"
cdf1e714
DW
23 #include "wx/bitmap.h"
24 #include "wx/brush.h"
0e320a79
DW
25#endif
26
cdf1e714 27#include "wx/os2/private.h"
0e320a79 28
0e320a79 29IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl)
0e320a79 30
cdf1e714
DW
31// ---------------------------------------------------------------------------
32// private functions
33// ---------------------------------------------------------------------------
34
35// wnd proc for radio buttons
3c299c3a
DW
36MRESULT EXPENTRY wxRadioBtnWndProc( HWND hWnd
37 ,UINT uMessage
38 ,MPARAM wParam
39 ,MPARAM lParam
40 );
f289196b
DW
41MRESULT EXPENTRY wxRadioBoxWndProc( HWND hWnd
42 ,UINT uMessage
43 ,MPARAM wParam
44 ,MPARAM lParam
45 );
cdf1e714
DW
46
47// ---------------------------------------------------------------------------
48// global vars
49// ---------------------------------------------------------------------------
50
f289196b
DW
51extern void wxAssociateWinWithHandle( HWND hWnd
52 ,wxWindowOS2* pWin
53 );
a4ebf7ba
SN
54extern void wxRemoveHandleAssociation( wxWindowOS2 *pWin );
55// the pointer to standard radio button & box wnd procs
3c299c3a 56static WXFARPROC fnWndProcRadioBtn = NULL;
f289196b 57static WXFARPROC fnWndProcRadioBox = NULL;
cdf1e714
DW
58
59// ===========================================================================
60// implementation
61// ===========================================================================
62
63// ---------------------------------------------------------------------------
64// wxRadioBox
65// ---------------------------------------------------------------------------
66
3c299c3a
DW
67// Radio box item
68wxRadioBox::wxRadioBox()
0367c1c0 69{
3c299c3a
DW
70 m_nSelectedButton = -1;
71 m_nNoItems = 0;
3c299c3a 72 m_ahRadioButtons = NULL;
3c299c3a
DW
73 m_pnRadioWidth = NULL;
74 m_pnRadioHeight = NULL;
75} // end of wxRadioBox::wxRadioBox
0367c1c0 76
3c299c3a 77wxRadioBox::~wxRadioBox()
0367c1c0 78{
1a87edf2 79 m_isBeingDeleted = true;
0367c1c0 80
a4ebf7ba
SN
81 if (m_hWnd)
82 wxRemoveHandleAssociation(this);
3c299c3a 83 if (m_ahRadioButtons)
cdf1e714 84 {
aa61d352 85 for (unsigned int i = 0; i < m_nNoItems; i++)
a4ebf7ba
SN
86 {
87 wxWindow* pWin = wxFindWinFromHandle((WXHWND)m_ahRadioButtons[i]);
88 wxRemoveHandleAssociation(pWin);
89
3c299c3a 90 ::WinDestroyWindow((HWND)m_ahRadioButtons[i]);
521bf4ff 91 }
3c299c3a 92 delete[] m_ahRadioButtons;
cdf1e714 93 }
3c299c3a
DW
94 if (m_pnRadioWidth)
95 delete[] m_pnRadioWidth;
96 if (m_pnRadioHeight)
97 delete[] m_pnRadioHeight;
98} // end of wxRadioBox::~wxRadioBox
99
8228b893 100void wxRadioBox::Command ( wxCommandEvent& rEvent )
3c299c3a
DW
101{
102 SetSelection (rEvent.GetInt());
103 ProcessCommand(rEvent);
104} // end of wxRadioBox::Command
cdf1e714 105
8228b893 106bool wxRadioBox::ContainsHWND( WXHWND hWnd ) const
0e320a79 107{
aa61d352
VZ
108 unsigned int nCount = GetCount();
109 unsigned int i;
3c299c3a
DW
110
111 for (i = 0; i < nCount; i++)
112 {
113 if (GetRadioButtons()[i] == hWnd)
1a87edf2 114 return true;
3c299c3a 115 }
1a87edf2 116 return false;
3c299c3a
DW
117} // end of wxRadioBox::ContainsHWND
118
8228b893
WS
119bool wxRadioBox::Create( wxWindow* pParent,
120 wxWindowID vId,
121 const wxString& rsTitle,
122 const wxPoint& rPos,
123 const wxSize& rSize,
124 const wxArrayString& asChoices,
125 int nMajorDim,
126 long lStyle,
127 const wxValidator& rVal,
128 const wxString& rsName )
584ad2a3 129{
41c54fac 130 wxCArrayString chs(asChoices);
584ad2a3
MB
131
132 return Create(pParent, vId, rsTitle, rPos, rSize, chs.GetCount(),
133 chs.GetStrings(), nMajorDim, lStyle, rVal, rsName);
134}
135
8228b893
WS
136bool wxRadioBox::Create( wxWindow* pParent,
137 wxWindowID vId,
138 const wxString& rsTitle,
139 const wxPoint& rPos,
140 const wxSize& rSize,
141 int nNum,
142 const wxString asChoices[],
143 int nMajorDim,
144 long lStyle,
145 const wxValidator& rVal,
146 const wxString& rsName )
147{
148 wxColour vColour;
149 LONG lColor;
150 HWND hWndParent = GetHwndOf(pParent);
3c299c3a 151
0fba44b4 152 vColour.Set(wxString(wxT("BLACK")));
b389a12d 153 m_backgroundColour = pParent->GetBackgroundColour();
3c299c3a 154 m_nSelectedButton = -1;
b389a12d 155 m_nNoItems = 0;
3c299c3a 156
3c299c3a
DW
157 //
158 // Common initialization
159 //
b9b1d6c8
DW
160 if (!CreateControl( pParent
161 ,vId
162 ,rPos
163 ,rSize
164 ,lStyle
b9b1d6c8 165 ,rVal
b9b1d6c8
DW
166 ,rsName
167 ))
1a87edf2 168 return false;
0fba44b4 169 if (!OS2CreateControl( wxT("STATIC")
f289196b 170 ,SS_GROUPBOX
3c299c3a
DW
171 ,rPos
172 ,rSize
173 ,rsTitle
174 ))
1a87edf2 175 return false;
cdf1e714 176
f289196b 177 wxAssociateWinWithHandle(m_hWnd, this);
cdf1e714 178
3c299c3a 179 //
4b2bdce6 180 // Now we can set m_nNoItems and let SetMajorDim set m_numCols/m_numRows
3c299c3a 181 //
aa61d352 182 m_nNoItems = (unsigned int)nNum;
4b2bdce6 183 SetMajorDim(nMajorDim == 0 ? nNum : nMajorDim, lStyle);
4b2bdce6 184
3c299c3a
DW
185 m_ahRadioButtons = new WXHWND[nNum];
186 m_pnRadioWidth = new int[nNum];
187 m_pnRadioHeight = new int[nNum];
188
3c299c3a 189 for (int i = 0; i < nNum; i++)
cdf1e714 190 {
3c299c3a 191 m_pnRadioWidth[i] = m_pnRadioHeight[i] = -1;
a4ebf7ba 192 long lStyleBtn = BS_AUTORADIOBUTTON | WS_VISIBLE;
3c299c3a
DW
193 int nNewId = NewControlId();
194
be9d9869 195 if (i == 0)
a4ebf7ba
SN
196 lStyleBtn |= WS_GROUP | WS_TABSTOP;
197
198 wxString sLabel = ::wxPMTextToLabel(asChoices[i]);
199
200 HWND hWndBtn = (WXHWND)::WinCreateWindow ( hWndParent,
201 WC_BUTTON,
202 sLabel.c_str(),
203 lStyleBtn,
204 0, 0, 0, 0,
205 hWndParent,
206 HWND_BOTTOM,
207 (HMENU)nNewId,
208 NULL,
209 NULL
3c299c3a 210 );
a4ebf7ba
SN
211 if (!hWndBtn)
212 {
213 return false;
214 }
5d44b24e 215 lColor = (LONG)vColour.GetPixel();
3c299c3a
DW
216 ::WinSetPresParam( hWndBtn
217 ,PP_FOREGROUNDCOLOR
218 ,sizeof(LONG)
219 ,(PVOID)&lColor
220 );
5d44b24e 221
a4ebf7ba 222 lColor = (LONG)m_backgroundColour.GetPixel();
5d44b24e
DW
223 ::WinSetPresParam( hWndBtn
224 ,PP_BACKGROUNDCOLOR
225 ,sizeof(LONG)
226 ,(PVOID)&lColor
227 );
3c299c3a
DW
228 m_ahRadioButtons[i] = (WXHWND)hWndBtn;
229 SubclassRadioButton((WXHWND)hWndBtn);
f289196b 230 wxAssociateWinWithHandle(hWndBtn, this);
3c299c3a 231 wxOS2SetFont( hWndBtn
f289196b 232 ,*wxSMALL_FONT
3c299c3a
DW
233 );
234 ::WinSetWindowULong(hWndBtn, QWL_USER, (ULONG)this);
235 m_aSubControls.Add(nNewId);
cdf1e714 236 }
0e320a79 237
3c299c3a 238 //
be9d9869 239 // Create a dummy control to end the group.
3c299c3a 240 //
a4ebf7ba 241 (void)::WinCreateWindow ( hWndParent,
743e24aa
WS
242 WC_BUTTON,
243 "",
be9d9869 244 WS_GROUP,
743e24aa 245 0, 0, 0, 0,
a4ebf7ba 246 hWndParent,
743e24aa
WS
247 HWND_TOP,
248 (HMENU)NewControlId(),
249 NULL,
250 NULL
3c299c3a 251 );
f289196b
DW
252 fnWndProcRadioBox = (WXFARPROC)::WinSubclassWindow( GetHwnd()
253 ,(PFNWP)wxRadioBoxWndProc
254 );
255 ::WinSetWindowULong(GetHwnd(), QWL_USER, (ULONG)this);
5d44b24e 256 lColor = (LONG)vColour.GetPixel();
3c299c3a
DW
257 ::WinSetPresParam( m_hWnd
258 ,PP_FOREGROUNDCOLOR
259 ,sizeof(LONG)
260 ,(PVOID)&lColor
261 );
5d44b24e 262
be9d9869 263 lColor = (LONG)m_backgroundColour.GetPixel();
5d44b24e
DW
264 ::WinSetPresParam( m_hWnd
265 ,PP_BACKGROUNDCOLOR
266 ,sizeof(LONG)
267 ,(PVOID)&lColor
268 );
b389a12d
DW
269 SetXComp(0);
270 SetYComp(0);
cdf1e714 271 SetSelection(0);
3c299c3a
DW
272 SetSize( rPos.x
273 ,rPos.y
274 ,rSize.x
275 ,rSize.y
276 );
1a87edf2 277 return true;
3c299c3a 278} // end of wxRadioBox::Create
0e320a79 279
3c299c3a 280wxSize wxRadioBox::DoGetBestSize() const
0e320a79 281{
3c299c3a 282 return (GetTotalButtonSize(GetMaxButtonSize()));
a4ebf7ba 283} // end of wxRadioBox::DoGetBestSize
3c299c3a
DW
284
285void wxRadioBox::DoSetSize(
286 int nX
287, int nY
288, int nWidth
289, int nHeight
290, int nSizeFlags
291)
292{
a4ebf7ba
SN
293 //
294 // Input parameters assume wxWidgets coordinate system
295 //
3c299c3a
DW
296 int nCurrentX;
297 int nCurrentY;
298 int nWidthOld;
299 int nHeightOld;
300 int nXx = nX;
301 int nYy = nY;
3c299c3a
DW
302 int nXOffset = nXx;
303 int nYOffset = nYy;
3c299c3a
DW
304 int nCx1;
305 int nCy1;
306 wxSize vMaxSize = GetMaxButtonSize();
307 int nMaxWidth;
308 int nMaxHeight;
309 wxSize vTotSize;
310 int nTotWidth;
311 int nTotHeight;
312 int nStartX;
313 int nStartY;
7804d121 314 wxFont vFont = GetFont();
3c299c3a
DW
315
316 m_nSizeFlags = nSizeFlags;
317 GetPosition( &nCurrentX
318 ,&nCurrentY
319 );
320 GetSize( &nWidthOld
321 ,&nHeightOld
322 );
323
fa50c0e3 324 if (nX == wxDefaultCoord && !(nSizeFlags & wxSIZE_ALLOW_MINUS_ONE))
3c299c3a 325 nXx = nCurrentX;
fa50c0e3 326 if (nY == wxDefaultCoord && !(nSizeFlags & wxSIZE_ALLOW_MINUS_ONE))
3c299c3a 327 nYy = nCurrentY;
b389a12d
DW
328 if (nYy < 0)
329 nYy = 0;
330 if (nXx < 0)
331 nXx = 0;
3c299c3a
DW
332
333 wxGetCharSize( m_hWnd
334 ,&nCx1
335 ,&nCy1
7804d121 336 ,&vFont
3c299c3a
DW
337 );
338
339 //
340 // Attempt to have a look coherent with other platforms: We compute the
341 // biggest toggle dim, then we align all items according this value.
342 //
343 vMaxSize = GetMaxButtonSize();
344 nMaxWidth = vMaxSize.x;
345 nMaxHeight = vMaxSize.y;
346
347 vTotSize = GetTotalButtonSize(vMaxSize);
348 nTotWidth = vTotSize.x;
349 nTotHeight = vTotSize.y;
350
351 //
352 // Only change our width/height if asked for
353 //
354 if (nWidth == -1)
355 {
356 if (nSizeFlags & wxSIZE_AUTO_WIDTH )
357 nWidth = nTotWidth;
358 else
359 nWidth = nWidthOld;
360 }
cdf1e714 361
3c299c3a 362 if (nHeight == -1)
cdf1e714 363 {
3c299c3a
DW
364 if (nSizeFlags & wxSIZE_AUTO_HEIGHT)
365 nHeight = nTotHeight;
366 else
367 nHeight = nHeightOld;
cdf1e714
DW
368 }
369
a4ebf7ba
SN
370 //
371 // Now convert to OS/2 coordinate system
372 //
3c299c3a 373 wxWindowOS2* pParent = (wxWindowOS2*)GetParent();
3c299c3a 374 if (pParent)
a4ebf7ba 375 nYy = GetOS2ParentHeight(pParent) - nYy - nHeight;
3c299c3a
DW
376 else
377 {
378 RECTL vRect;
3c299c3a 379 ::WinQueryWindowRect(HWND_DESKTOP, &vRect);
a4ebf7ba 380 nYy = vRect.yTop - nYy - nHeight;
3c299c3a 381 }
a4ebf7ba 382 nYOffset = nYy + nHeight;
3c299c3a
DW
383 ::WinSetWindowPos( GetHwnd()
384 ,HWND_TOP
385 ,(LONG)nXx
386 ,(LONG)nYy
387 ,(LONG)nWidth
388 ,(LONG)nHeight
389 ,SWP_ZORDER | SWP_SIZE | SWP_MOVE | SWP_SHOW
390 );
391
392 //
393 // Now position all the buttons: the current button will be put at
394 // wxPoint(x_offset, y_offset) and the new row/column will start at
395 // startX/startY. The size of all buttons will be the same wxSize(maxWidth,
396 // maxHeight) except for the buttons in the last column which should extend
397 // to the right border of radiobox and thus can be wider than this.
398 //
399 // Also, remember that wxRA_SPECIFY_COLS means that we arrange buttons in
400 // left to right order and m_majorDim is the number of columns while
401 // wxRA_SPECIFY_ROWS means that the buttons are arranged top to bottom and
402 // m_majorDim is the number of rows.
403 //
404 nXOffset += nCx1;
405 nYOffset -= (nMaxHeight + ((3*nCy1)/2));
406
407 nStartX = nXOffset;
408 nStartY = nYOffset;
409
aa61d352 410 for (unsigned int i = 0; i < m_nNoItems; i++)
3c299c3a
DW
411 {
412 //
413 // The last button in the row may be wider than the other ones as the
414 // radiobox may be wider than the sum of the button widths (as it
415 // happens, for example, when the radiobox label is very long)
416 //
8228b893 417 bool bIsLastInTheRow;
3c299c3a
DW
418
419 if (m_windowStyle & wxRA_SPECIFY_COLS)
420 {
421 //
422 // Item is the last in its row if it is a multiple of the number of
423 // columns or if it is just the last item
424 //
425 int n = i + 1;
0367c1c0 426
45abfa71 427 bIsLastInTheRow = ((n % GetMajorDim()) == 0) || (n == (int)m_nNoItems);
3c299c3a
DW
428 }
429 else // winRA_SPECIFY_ROWS
430 {
431 //
432 // Item is the last in the row if it is in the last columns
433 //
21e0a4d5 434 bIsLastInTheRow = i >= (m_nNoItems/GetMajorDim()) * GetMajorDim();
3c299c3a 435 }
0367c1c0 436
3c299c3a
DW
437 //
438 // Is this the start of new row/column?
439 //
21e0a4d5 440 if (i && (i % GetMajorDim() == 0))
3c299c3a
DW
441 {
442 if (m_windowStyle & wxRA_SPECIFY_ROWS)
443 {
3c299c3a
DW
444 //
445 // Start of new column
446 //
447 nYOffset = nStartY;
448 nXOffset += nMaxWidth + nCx1;
449 }
450 else // start of new row
451 {
452 nXOffset = nStartX;
453 nYOffset -= nMaxHeight;
454 if (m_pnRadioWidth[0] > 0L)
455 nYOffset -= nCy1/2;
456 }
457 }
458
459 int nWidthBtn;
460
461 if (bIsLastInTheRow)
462 {
463 //
464 // Make the button go to the end of radio box
465 //
466 nWidthBtn = nStartX + nWidth - nXOffset - (2 * nCx1);
467 if (nWidthBtn < nMaxWidth)
468 nWidthBtn = nMaxWidth;
469 }
470 else
471 {
472 //
473 // Normal button, always of the same size
474 //
475 nWidthBtn = nMaxWidth;
476 }
cdf1e714 477
3c299c3a
DW
478 //
479 // Make all buttons of the same, maximal size - like this they
480 // cover the radiobox entirely and the radiobox tooltips are always
481 // shown (otherwise they are not when the mouse pointer is in the
4b2bdce6 482 // radiobox part not belonging to any radiobutton)
3c299c3a
DW
483 //
484 ::WinSetWindowPos( (HWND)m_ahRadioButtons[i]
be9d9869 485 ,HWND_BOTTOM
3c299c3a
DW
486 ,(LONG)nXOffset
487 ,(LONG)nYOffset
488 ,(LONG)nWidthBtn
489 ,(LONG)nMaxHeight
490 ,SWP_ZORDER | SWP_SIZE | SWP_MOVE | SWP_SHOW
491 );
492 //
493 // Where do we put the next button?
494 //
495 if (m_windowStyle & wxRA_SPECIFY_ROWS)
496 {
497 //
498 // Below this one
499 //
500 nYOffset -= nMaxHeight;
501 if (m_pnRadioWidth[0] > 0)
502 nYOffset -= nCy1/2;
503 }
504 else
505 {
506 //
507 // To the right of this one
508 //
509 nXOffset += nWidthBtn + nCx1;
510 }
511 }
512} // end of wxRadioBox::DoSetSize
0e320a79 513
aa61d352 514bool wxRadioBox::Enable(unsigned int nItem, bool bEnable)
0e320a79 515{
fa50c0e3 516 wxCHECK_MSG( IsValid(nItem), false,
3c299c3a 517 wxT("invalid item in wxRadioBox::Enable()") );
cdf1e714 518
3c299c3a 519 ::WinEnableWindow((HWND) m_ahRadioButtons[nItem], bEnable);
1a87edf2 520 return true;
3c299c3a 521} // end of wxRadioBox::Enable
0e320a79 522
8228b893 523bool wxRadioBox::Enable(bool bEnable)
0e320a79 524{
3c299c3a 525 if ( !wxControl::Enable(bEnable) )
1a87edf2 526 return false;
aa61d352 527 for (unsigned int i = 0; i < m_nNoItems; i++)
3c299c3a 528 ::WinEnableWindow((HWND)m_ahRadioButtons[i], bEnable);
1a87edf2 529 return true;
3c299c3a 530} // end of wxRadioBox::Enable
0e320a79 531
aa61d352 532unsigned int wxRadioBox::GetCount() const
0e320a79 533{
3c299c3a
DW
534 return m_nNoItems;
535} // end of wxRadioBox::GetCount
0e320a79 536
11e62fe6 537wxString wxRadioBox::GetLabel(int nItem) const
0e320a79 538{
fa50c0e3 539 wxCHECK_MSG( IsValid(nItem), wxEmptyString, wxT("invalid radiobox index") );
cdf1e714 540
3c299c3a
DW
541 return wxGetWindowText(m_ahRadioButtons[nItem]);
542} // end of wxRadioBox::GetLabel
cdf1e714 543
3c299c3a
DW
544wxSize wxRadioBox::GetMaxButtonSize() const
545{
8228b893
WS
546 int nWidthMax = 0;
547 int nHeightMax = 0;
cdf1e714 548
aa61d352 549 for (unsigned int i = 0 ; i < m_nNoItems; i++)
cdf1e714 550 {
8228b893
WS
551 int nWidth;
552 int nHeight;
3c299c3a
DW
553
554 if (m_pnRadioWidth[i] < 0L)
cdf1e714 555 {
3c299c3a
DW
556 GetTextExtent( wxGetWindowText(m_ahRadioButtons[i])
557 ,&nWidth
558 ,&nHeight
559 );
560
561 //
562 // Adjust the size to take into account the radio box itself
563 // FIXME this is totally bogus!
564 //
565 nWidth += RADIO_SIZE;
566 nHeight *= 3;
567 nHeight /= 2;
cdf1e714
DW
568 }
569 else
570 {
3c299c3a
DW
571 nWidth = m_pnRadioWidth[i];
572 nHeight = m_pnRadioHeight[i];
cdf1e714 573 }
3c299c3a
DW
574 if (nWidthMax < nWidth )
575 nWidthMax = nWidth;
576 if (nHeightMax < nHeight )
577 nHeightMax = nHeight;
578 }
6670f564
WS
579 wxSize maxsize( nWidthMax, nHeightMax);
580 return maxsize;
3c299c3a 581} // end of wxRadioBox::GetMaxButtonSize
cdf1e714 582
3c299c3a
DW
583// Get single selection, for single choice list items
584int wxRadioBox::GetSelection() const
585{
586 return m_nSelectedButton;
587} // end of wxRadioBox::GetSelection
cdf1e714 588
743e24aa 589void wxRadioBox::GetSize( int* pnWidth, int* pnHeight ) const
3c299c3a 590{
8228b893 591 RECT vRect;
cdf1e714 592
3c299c3a
DW
593 vRect.xLeft = -1;
594 vRect.xRight = -1;
595 vRect.yTop = -1;
596 vRect.yBottom = -1;
597
598 if (m_hWnd)
8228b893 599 wxFindMaxSize( m_hWnd, &vRect );
3c299c3a 600
aa61d352 601 for (unsigned int i = 0; i < m_nNoItems; i++)
8228b893 602 wxFindMaxSize( m_ahRadioButtons[i], &vRect );
3c299c3a 603
a00ffdf1
SN
604 if (pnWidth)
605 *pnWidth = vRect.xRight - vRect.xLeft;
606 if (pnHeight)
607 *pnHeight = vRect.yTop - vRect.yBottom;
3c299c3a 608} // end of wxRadioBox::GetSize
cdf1e714 609
3c299c3a 610// Find string for position
aa61d352 611wxString wxRadioBox::GetString(unsigned int nNum) const
3c299c3a 612{
521bf4ff 613 wxCHECK_MSG( IsValid(nNum), wxEmptyString, wxT("invalid radiobox index") );
3c299c3a
DW
614 return wxGetWindowText(m_ahRadioButtons[nNum]);
615} // end of wxRadioBox::GetString
616
617// For single selection items only
618wxString wxRadioBox::GetStringSelection() const
619{
aa61d352
VZ
620 wxString sResult;
621 int nSel = GetSelection();
3c299c3a 622
aa61d352 623 if (nSel != wxNOT_FOUND)
3c299c3a 624 sResult = GetString(nSel);
aa61d352 625
3c299c3a
DW
626 return sResult;
627} // end of wxRadioBox::GetStringSelection
628
6670f564 629wxSize wxRadioBox::GetTotalButtonSize( const wxSize& rSizeBtn ) const
3c299c3a 630{
6670f564
WS
631 int nCx1;
632 int nCy1;
6670f564
WS
633 int nHeight;
634 int nWidth;
c5f975dd 635 int nWidthLabel = 0;
3c299c3a 636
c5f975dd
SN
637 nCx1 = GetCharWidth();
638 nCy1 = GetCharHeight();
21e0a4d5
VZ
639 nHeight = GetRowCount() * rSizeBtn.y + (2 * nCy1);
640 nWidth = GetColumnCount() * (rSizeBtn.x + nCx1) + nCx1;
3c299c3a
DW
641
642 //
643 // And also wide enough for its label
644 //
521bf4ff
WS
645 wxString sStr = wxGetWindowText(GetHwnd());
646 if (!sStr.empty())
c5f975dd
SN
647 {
648 GetTextExtent( sStr
649 ,&nWidthLabel
650 ,NULL
651 );
652 nWidthLabel += 2*nCx1;
653 }
3c299c3a
DW
654 if (nWidthLabel > nWidth)
655 nWidth = nWidthLabel;
656
6670f564
WS
657 wxSize total( nWidth, nHeight );
658 return total;
3c299c3a
DW
659} // end of wxRadioBox::GetTotalButtonSize
660
6670f564
WS
661WXHBRUSH wxRadioBox::OnCtlColor( WXHDC hwinDC,
662 WXHWND WXUNUSED(hWnd),
663 WXUINT WXUNUSED(uCtlColor),
664 WXUINT WXUNUSED(uMessage),
665 WXWPARAM WXUNUSED(wParam),
666 WXLPARAM WXUNUSED(lParam) )
3c299c3a 667{
6670f564 668 HPS hPS = (HPS)hwinDC; // pass in a PS handle in OS/2
3c299c3a
DW
669
670 if (GetParent()->GetTransparentBackground())
671 ::GpiSetBackMix(hPS, BM_LEAVEALONE);
672 else
673 ::GpiSetBackMix(hPS, BM_OVERPAINT);
674
6670f564 675 wxColour vColBack = GetBackgroundColour();
cdf1e714 676
3c299c3a
DW
677 ::GpiSetBackColor(hPS, vColBack.GetPixel());
678 ::GpiSetColor(hPS, vColBack.GetPixel());
679
6670f564 680 wxBrush* pBrush = wxTheBrushList->FindOrCreateBrush( vColBack, wxSOLID );
3c299c3a
DW
681 return ((WXHBRUSH)pBrush->GetResourceHandle());
682} // end of wxRadioBox::OnCtlColor
683
6670f564
WS
684bool wxRadioBox::OS2Command( WXUINT uCmd,
685 WXWORD wId)
3c299c3a 686{
6670f564 687 int nSelectedButton = -1;
3c299c3a
DW
688
689 if (uCmd == BN_CLICKED)
cdf1e714 690 {
3c299c3a 691 if (wId == GetId())
1a87edf2 692 return true;
3c299c3a 693
aa61d352 694 for (unsigned int i = 0; i < m_nNoItems; i++)
cdf1e714 695 {
3c299c3a 696 if (wId == wxGetWindowId(m_ahRadioButtons[i]))
cdf1e714 697 {
3c299c3a
DW
698 nSelectedButton = i;
699 break;
cdf1e714
DW
700 }
701 }
3c299c3a 702 if (nSelectedButton == -1)
cdf1e714 703 {
3c299c3a 704 //
f289196b
DW
705 // Just ignore it
706 //
1a87edf2 707 return false;
cdf1e714 708 }
3c299c3a 709 if (nSelectedButton != m_nSelectedButton)
cdf1e714 710 {
3c299c3a
DW
711 m_nSelectedButton = nSelectedButton;
712 SendNotificationEvent();
cdf1e714 713 }
1a87edf2 714 return true;
cdf1e714 715 }
3c299c3a 716 else
1a87edf2 717 return false;
3c299c3a 718} // end of wxRadioBox::OS2Command
0e320a79 719
3c299c3a 720void wxRadioBox::SendNotificationEvent()
0e320a79 721{
aa61d352
VZ
722 wxCommandEvent vEvent(
723 wxEVT_COMMAND_RADIOBOX_SELECTED,
724 m_windowId
725 );
cdf1e714 726
3c299c3a
DW
727 vEvent.SetInt( m_nSelectedButton );
728 vEvent.SetString( GetString(m_nSelectedButton) );
729 vEvent.SetEventObject(this);
730 ProcessCommand(vEvent);
731} // end of wxRadioBox::SendNotificationEvent
0e320a79
DW
732
733void wxRadioBox::SetFocus()
734{
3c299c3a 735 if (m_nNoItems > 0)
cdf1e714 736 {
3c299c3a
DW
737 if (m_nSelectedButton == -1)
738 ::WinSetFocus(HWND_DESKTOP, (HWND)m_ahRadioButtons[0]);
cdf1e714 739 else
3c299c3a 740 ::WinSetFocus(HWND_DESKTOP, (HWND)m_ahRadioButtons[m_nSelectedButton]);
cdf1e714 741 }
3c299c3a 742} // end of wxRadioBox::SetFocus
0e320a79 743
8228b893 744bool wxRadioBox::SetFont(const wxFont& rFont)
0e320a79 745{
3c299c3a
DW
746 if (!wxControl::SetFont(rFont))
747 {
748 //
749 // Nothing to do
750 //
1a87edf2 751 return false;
3c299c3a
DW
752 }
753 //
754 // Also set the font of our radio buttons
755 //
aa61d352 756 for (unsigned int n = 0; n < m_nNoItems; n++)
cdf1e714 757 {
8228b893 758 HWND hWndBtn = (HWND)m_ahRadioButtons[n];
cdf1e714 759
8228b893 760 wxOS2SetFont( hWndBtn, rFont );
3c299c3a
DW
761 ::WinInvalidateRect(hWndBtn, NULL, FALSE);
762 }
1a87edf2 763 return true;
3c299c3a 764} // end of wxRadioBox::SetFont
0e320a79 765
3c299c3a
DW
766void wxRadioBox::SetSelection(
767 int nNum
768)
0e320a79 769{
fa50c0e3 770 wxCHECK_RET( IsValid(nNum), wxT("invalid radiobox index") );
cdf1e714 771
fa50c0e3 772 if ( IsValid(m_nSelectedButton) )
3c299c3a 773 ::WinSendMsg((HWND)m_ahRadioButtons[m_nSelectedButton], BM_SETCHECK, (MPARAM)0, (MPARAM)0);
0e320a79 774
3c299c3a
DW
775 ::WinSendMsg((HWND)m_ahRadioButtons[nNum], BM_SETCHECK, (MPARAM)1, (MPARAM)0);
776 ::WinSetFocus(HWND_DESKTOP, (HWND)m_ahRadioButtons[nNum]);
777 m_nSelectedButton = nNum;
778} // end of wxRadioBox::SetSelection
0e320a79 779
aa61d352 780void wxRadioBox::SetString(unsigned int nItem, const wxString& rsLabel)
0e320a79 781{
fa50c0e3 782 wxCHECK_RET( IsValid(nItem), wxT("invalid radiobox index") );
cdf1e714 783
3c299c3a 784 m_pnRadioWidth[nItem] = m_pnRadioHeight[nItem] = -1;
0fba44b4 785 ::WinSetWindowText((HWND)m_ahRadioButtons[nItem], (PSZ)rsLabel.c_str());
3c299c3a 786} // end of wxRadioBox::SetString
cdf1e714 787
11e62fe6 788bool wxRadioBox::SetStringSelection(const wxString& rsStr)
cdf1e714 789{
11e62fe6 790 int nSel = FindString(rsStr);
0e320a79 791
3c299c3a 792 if (nSel > -1)
0e320a79 793 {
3c299c3a 794 SetSelection(nSel);
1a87edf2 795 return true;
0e320a79
DW
796 }
797 else
1a87edf2 798 return false;
3c299c3a 799} // end of wxRadioBox::SetStringSelection
0e320a79 800
8228b893 801bool wxRadioBox::Show(bool bShow)
cdf1e714 802{
3c299c3a 803 if (!wxControl::Show(bShow))
1a87edf2 804 return false;
3c299c3a 805
aa61d352 806 for (unsigned int i = 0; i < m_nNoItems; i++)
cdf1e714 807 {
3c299c3a 808 ::WinShowWindow((HWND)m_ahRadioButtons[i], (BOOL)bShow);
cdf1e714 809 }
1a87edf2 810 return true;
3c299c3a 811} // end of wxRadioBox::Show
cdf1e714 812
3c299c3a 813// Show a specific button
aa61d352 814bool wxRadioBox::Show(unsigned int nItem, bool bShow)
0e320a79 815{
fa50c0e3 816 wxCHECK_MSG( IsValid(nItem), false,
3c299c3a 817 wxT("invalid item in wxRadioBox::Show()") );
0e320a79 818
3c299c3a 819 ::WinShowWindow((HWND)m_ahRadioButtons[nItem], bShow);
fa50c0e3
WS
820
821 return true;
3c299c3a 822} // end of wxRadioBox::Show
cdf1e714 823
3c299c3a
DW
824void wxRadioBox::SubclassRadioButton(
825 WXHWND hWndBtn
826)
827{
3c299c3a
DW
828 fnWndProcRadioBtn = (WXFARPROC)::WinSubclassWindow(hWndBtn, (PFNWP)wxRadioBtnWndProc);
829} // end of wxRadioBox::SubclassRadioButton
cdf1e714 830
3c299c3a
DW
831MRESULT wxRadioBox::WindowProc(
832 WXUINT uMsg
833, WXWPARAM wParam
834, WXLPARAM lParam
835)
cdf1e714 836{
3c299c3a
DW
837 return (wxControl::OS2WindowProc( uMsg
838 ,wParam
839 ,lParam
840 ));
841} // end of wxRadioBox::WindowProc
cdf1e714
DW
842
843// ---------------------------------------------------------------------------
844// window proc for radio buttons
845// ---------------------------------------------------------------------------
846
3c299c3a
DW
847MRESULT wxRadioBtnWndProc(
848 HWND hWnd
849, UINT uMessage
850, MPARAM wParam
851, MPARAM lParam
852)
cdf1e714 853{
3c299c3a 854 switch (uMessage)
cdf1e714 855 {
3c299c3a
DW
856 case WM_CHAR:
857 {
858 USHORT uKeyFlags = SHORT1FROMMP((MPARAM)wParam);
cdf1e714 859
3c299c3a 860 if (!(uKeyFlags & KC_KEYUP)) // Key Down event
cdf1e714 861 {
3c299c3a
DW
862 if (uKeyFlags & KC_VIRTUALKEY)
863 {
864 wxRadioBox* pRadiobox = (wxRadioBox *)::WinQueryWindowULong( hWnd
865 ,QWL_USER
866 );
867 USHORT uVk = SHORT2FROMMP((MPARAM)lParam);
1a87edf2 868 bool bProcessed = true;
3c299c3a
DW
869 wxDirection eDir;
870
871 switch(uVk)
872 {
873 case VK_LEFT:
be9d9869 874 eDir = wxLEFT;
3c299c3a
DW
875 break;
876
877 case VK_RIGHT:
be9d9869 878 eDir = wxRIGHT;
3c299c3a
DW
879 break;
880
881 case VK_DOWN:
882 eDir = wxDOWN;
883 break;
884
885 case VK_UP:
886 eDir = wxUP;
887 break;
888
889 default:
1a87edf2 890 bProcessed = false;
3c299c3a
DW
891
892 //
893 // Just to suppress the compiler warning
894 //
895 eDir = wxALL;
896 }
897
898 if (bProcessed)
899 {
900 int nSelOld = pRadiobox->GetSelection();
901 int nSelNew = pRadiobox->GetNextItem( nSelOld
902 ,eDir
903 ,pRadiobox->GetWindowStyleFlag()
904 );
905
906 if (nSelNew != nSelOld)
907 {
908 pRadiobox->SetSelection(nSelNew);
909
910 //
911 // Emulate the button click
912 //
913 pRadiobox->SendNotificationEvent();
914 return 0;
915 }
916 }
917 }
cdf1e714 918 }
cdf1e714 919 }
3c299c3a 920 break;
cdf1e714
DW
921 }
922
3c299c3a
DW
923 return fnWndProcRadioBtn( hWnd
924 ,(ULONG)uMessage
925 ,(MPARAM)wParam
926 ,(MPARAM)lParam
927 );
928} // end of wxRadioBtnWndProc
0e320a79 929
743e24aa
WS
930MRESULT EXPENTRY wxRadioBoxWndProc( HWND hWnd,
931 UINT uMessage,
932 MPARAM wParam,
933 MPARAM lParam )
f289196b 934{
743e24aa
WS
935 return (fnWndProcRadioBox( hWnd,
936 (ULONG)uMessage,
937 (MPARAM)wParam,
938 (MPARAM)lParam )
f289196b
DW
939 );
940} // end of wxRadioBoxWndProc
8228b893
WS
941
942#endif // wxUSE_RADIOBOX