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