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