]> git.saurik.com Git - wxWidgets.git/blob - src/os2/radiobox.cpp
fixed typo (IsOK instead of IsOk)
[wxWidgets.git] / src / os2 / radiobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 wxRadioBtnWndProc(HWND hWnd,
35 UINT message,
36 MPARAM wParam,
37 MPARAM lParam);
38
39 // ---------------------------------------------------------------------------
40 // global vars
41 // ---------------------------------------------------------------------------
42
43 // the pointer to standard radio button wnd proc
44 static s_wndprocRadioBtn = NULL;
45
46 // ===========================================================================
47 // implementation
48 // ===========================================================================
49
50 // ---------------------------------------------------------------------------
51 // wxRadioBox
52 // ---------------------------------------------------------------------------
53
54 int wxRadioBox::GetNumVer() const
55 {
56 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
57 {
58 return m_majorDim;
59 }
60 else
61 {
62 return (m_noItems + m_majorDim - 1)/m_majorDim;
63 }
64 }
65
66 int wxRadioBox::GetNumHor() const
67 {
68 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
69 {
70 return (m_noItems + m_majorDim - 1)/m_majorDim;
71 }
72 else
73 {
74 return m_majorDim;
75 }
76 }
77
78 bool wxRadioBox::OS2Command(WXUINT cmd, WXWORD id)
79 {
80 // TODO:
81 /*
82 if ( cmd == BN_CLICKED )
83 {
84 int selectedButton = -1;
85
86 for ( int i = 0; i < m_noItems; i++ )
87 {
88 if ( id == wxGetWindowId(m_radioButtons[i]) )
89 {
90 selectedButton = i;
91
92 break;
93 }
94 }
95
96 wxASSERT_MSG( selectedButton != -1, wxT("click from alien button?") );
97
98 if ( selectedButton != m_selectedButton )
99 {
100 m_selectedButton = selectedButton;
101
102 SendNotificationEvent();
103 }
104 //else: don't generate events when the selection doesn't change
105
106 return TRUE;
107 }
108 else
109 return FALSE;
110 */
111 return FALSE;
112 }
113
114 #if WXWIN_COMPATIBILITY
115 wxRadioBox::wxRadioBox(wxWindow *parent, wxFunction func, const char *title,
116 int x, int y, int width, int height,
117 int n, char **choices,
118 int majorDim, long style, const char *name)
119 {
120 wxString *choices2 = new wxString[n];
121 for ( int i = 0; i < n; i ++) choices2[i] = choices[i];
122 Create(parent, -1, title, wxPoint(x, y), wxSize(width, height), n, choices2, majorDim, style,
123 wxDefaultValidator, name);
124 Callback(func);
125 delete choices2;
126 }
127
128 #endif
129
130 // Radio box item
131 wxRadioBox::wxRadioBox()
132 {
133 m_selectedButton = -1;
134 m_noItems = 0;
135 m_noRowsOrCols = 0;
136 m_radioButtons = NULL;
137 m_majorDim = 0;
138 m_radioWidth = NULL;
139 m_radioHeight = NULL;
140 }
141
142 bool wxRadioBox::Create(wxWindow *parent, wxWindowID id, const wxString& title,
143 const wxPoint& pos, const wxSize& size,
144 int n, const wxString choices[],
145 int majorDim, long style,
146 #if wxUSE_VALIDATORS
147 const wxValidator& val, const wxString& name)
148 #endif
149 {
150 m_selectedButton = -1;
151 m_noItems = n;
152
153 SetName(name);
154 #if wxUSE_VALIDATORS
155 SetValidator(val);
156 #endif
157 parent->AddChild(this);
158 m_backgroundColour = parent->GetBackgroundColour();
159 m_foregroundColour = parent->GetForegroundColour();
160
161 m_windowStyle = (long&)style;
162
163 int x = pos.x;
164 int y = pos.y;
165 int width = size.x;
166 int height = size.y;
167
168 if (id == -1)
169 m_windowId = NewControlId();
170 else
171 m_windowId = id;
172
173 if ( majorDim == 0 )
174 m_majorDim = n;
175 else
176 m_majorDim = majorDim;
177 m_noRowsOrCols = majorDim;
178
179 long msStyle = 0; // TODO: GROUP_FLAGS;
180
181 bool want3D;
182 WXDWORD exStyle = Determine3DEffects(0, &want3D);
183
184 HWND hwndParent = (HWND)parent->GetHWND();
185 /*
186 m_hWnd = (WXHWND)::CreateWindowEx
187 (
188 (DWORD)exStyle,
189 GROUP_CLASS,
190 title,
191 msStyle,
192 0, 0, 0, 0,
193 hwndParent,
194 (HMENU)m_windowId,
195 wxGetInstance(),
196 NULL
197 );
198
199 #if wxUSE_CTL3D
200 if (want3D)
201 {
202 Ctl3dSubclassCtl((HWND)m_hWnd);
203 m_useCtl3D = TRUE;
204 }
205 #endif // wxUSE_CTL3D
206 */
207 SetFont(parent->GetFont());
208
209 SubclassWin(m_hWnd);
210
211 // Some radio boxes test consecutive id.
212 (void)NewControlId();
213 m_radioButtons = new WXHWND[n];
214 m_radioWidth = new int[n];
215 m_radioHeight = new int[n];
216 int i;
217 for (i = 0; i < n; i++)
218 {
219 // TODO:
220 /*
221 m_radioWidth[i] = m_radioHeight[i] = -1;
222 long groupStyle = 0;
223 if ( i == 0 && style == 0 )
224 groupStyle = WS_GROUP;
225 long newId = NewControlId();
226 long msStyle = groupStyle | RADIO_FLAGS;
227
228 HWND hwndBtn = CreateWindowEx(exStyle, RADIO_CLASS,
229 choices[i], msStyle,
230 0,0,0,0,
231 hwndParent,
232 (HMENU)newId, wxGetInstance(),
233 NULL);
234
235 m_radioButtons[i] = (WXHWND)hwndBtn;
236
237 SubclassRadioButton((WXHWND)hwndBtn);
238
239 wxFont& font = GetFont();
240 if ( font.Ok() )
241 {
242 SendMessage(hwndBtn, WM_SETFONT,
243 (WPARAM)font.GetResourceHandle(), 0L);
244 }
245
246 m_subControls.Append((wxObject *)(WXDWORD)(WXWORD)newId);
247 */
248 }
249
250 // Create a dummy radio control to end the group.
251 // (void)CreateWindowEx(0, RADIO_CLASS, wxT(""), WS_GROUP | RADIO_FLAGS,
252 // 0, 0, 0, 0, hwndParent,
253 // (HMENU)NewControlId(), wxGetInstance(), NULL);
254
255 SetSelection(0);
256
257 SetSize(x, y, width, height);
258
259 return TRUE;
260 }
261
262 wxRadioBox::~wxRadioBox()
263 {
264 m_isBeingDeleted = TRUE;
265
266 if (m_radioButtons)
267 {
268 int i;
269 // TODO:
270 /*
271 for (i = 0; i < m_noItems; i++)
272 ::DestroyWindow((HWND)m_radioButtons[i]);
273 delete[] m_radioButtons;
274 */
275 }
276
277 if (m_radioWidth)
278 delete[] m_radioWidth;
279 if (m_radioHeight)
280 delete[] m_radioHeight;
281
282 }
283
284 wxString wxRadioBox::GetLabel(int item) const
285 {
286 wxCHECK_MSG( item >= 0 && item < m_noItems, wxT(""), wxT("invalid radiobox index") );
287
288 return wxGetWindowText(m_radioButtons[item]);
289 }
290
291 void wxRadioBox::SetLabel(int item, const wxString& label)
292 {
293 wxCHECK_RET( item >= 0 && item < m_noItems, wxT("invalid radiobox index") );
294
295 m_radioWidth[item] = m_radioHeight[item] = -1;
296 // TODO: SetWindowText((HWND)m_radioButtons[item], label.c_str());
297 }
298
299 void wxRadioBox::SetLabel(int item, wxBitmap *bitmap)
300 {
301 /*
302 m_radioWidth[item] = bitmap->GetWidth() + FB_MARGIN;
303 m_radioHeight[item] = bitmap->GetHeight() + FB_MARGIN;
304 */
305 wxFAIL_MSG(wxT("not implemented"));
306 }
307
308 int wxRadioBox::FindString(const wxString& s) const
309 {
310 for (int i = 0; i < m_noItems; i++)
311 {
312 if ( s == wxGetWindowText(m_radioButtons[i]) )
313 return i;
314 }
315
316 return wxNOT_FOUND;
317 }
318
319 void wxRadioBox::SetSelection(int N)
320 {
321 wxCHECK_RET( (N >= 0) && (N < m_noItems), wxT("invalid radiobox index") );
322
323 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
324 // TODO:
325 /*
326 if (m_selectedButton >= 0 && m_selectedButton < m_noItems)
327 ::SendMessage((HWND) m_radioButtons[m_selectedButton], BM_SETCHECK, 0, 0L);
328
329 ::SendMessage((HWND)m_radioButtons[N], BM_SETCHECK, 1, 0L);
330 ::SetFocus((HWND)m_radioButtons[N]);
331 */
332 m_selectedButton = N;
333 }
334
335 // Get single selection, for single choice list items
336 int wxRadioBox::GetSelection() const
337 {
338 return m_selectedButton;
339 }
340
341 // Find string for position
342 wxString wxRadioBox::GetString(int N) const
343 {
344 return wxGetWindowText(m_radioButtons[N]);
345 }
346
347 // Restored old code.
348 void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
349 {
350 int currentX, currentY;
351 GetPosition(&currentX, &currentY);
352 int widthOld, heightOld;
353 GetSize(&widthOld, &heightOld);
354
355 int xx = x;
356 int yy = y;
357
358 if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
359 xx = currentX;
360 if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
361 yy = currentY;
362
363 wxString buf;
364
365 int y_offset = yy;
366 int x_offset = xx;
367 int current_width, cyf;
368
369 int cx1,cy1;
370 wxGetCharSize(m_hWnd, &cx1, &cy1, & GetFont());
371
372 // Attempt to have a look coherent with other platforms: We compute the
373 // biggest toggle dim, then we align all items according this value.
374 int maxWidth = -1;
375 int maxHeight = -1;
376
377 int i;
378 for (i = 0 ; i < m_noItems; i++)
379 {
380 int eachWidth;
381 int eachHeight;
382 if (m_radioWidth[i]<0)
383 {
384 // It's a labelled toggle
385 buf = wxGetWindowText(m_radioButtons[i]);
386 GetTextExtent(buf, &current_width, &cyf);
387 eachWidth = (int)(current_width + RADIO_SIZE);
388 eachHeight = (int)((3*cyf)/2);
389 }
390 else
391 {
392 eachWidth = m_radioWidth[i];
393 eachHeight = m_radioHeight[i];
394 }
395
396 if (maxWidth<eachWidth)
397 maxWidth = eachWidth;
398 if (maxHeight<eachHeight)
399 maxHeight = eachHeight;
400 }
401
402 if (m_hWnd)
403 {
404 int totWidth;
405 int totHeight;
406
407 int nbHor = GetNumHor(),
408 nbVer = GetNumVer();
409
410 // this formula works, but I don't know why.
411 // Please, be sure what you do if you modify it!!
412 if (m_radioWidth[0]<0)
413 totHeight = (nbVer * maxHeight) + cy1/2;
414 else
415 totHeight = nbVer * (maxHeight+cy1/2);
416 totWidth = nbHor * (maxWidth+cx1);
417
418 int extraHeight = cy1;
419
420 // only change our width/height if asked for
421 if ( width == -1 )
422 {
423 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
424 width = totWidth + cx1;
425 else
426 width = widthOld;
427 }
428
429 if ( height == -1 )
430 {
431 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
432 height = totHeight + extraHeight;
433 else
434 height = heightOld;
435 }
436
437 // TODO: MoveWindow(GetHwnd(), x_offset, y_offset, width, height, TRUE);
438
439 x_offset += cx1;
440 y_offset += cy1;
441 }
442
443 int startX = x_offset;
444 int startY = y_offset;
445
446 for ( i = 0 ; i < m_noItems; i++)
447 {
448 // Bidimensional radio adjustment
449 if (i&&((i%m_majorDim)==0)) // Why is this omitted for i = 0?
450 {
451 if (m_windowStyle & wxRA_VERTICAL)
452 {
453 y_offset = startY;
454 x_offset += maxWidth + cx1;
455 }
456 else
457 {
458 x_offset = startX;
459 y_offset += maxHeight;
460 if (m_radioWidth[0]>0)
461 y_offset += cy1/2;
462 }
463 }
464 int eachWidth;
465 int eachHeight;
466 if (m_radioWidth[i]<0)
467 {
468 // It's a labeled item
469 buf = wxGetWindowText(m_radioButtons[i]);
470 GetTextExtent(buf, &current_width, &cyf);
471
472 // How do we find out radio button bitmap size!!
473 // By adjusting them carefully, manually :-)
474 eachWidth = (int)(current_width + RADIO_SIZE);
475 eachHeight = (int)((3*cyf)/2);
476 }
477 else
478 {
479 eachWidth = m_radioWidth[i];
480 eachHeight = m_radioHeight[i];
481 }
482
483 // TODO:
484 /*
485 MoveWindow((HWND)m_radioButtons[i], x_offset, y_offset,
486 eachWidth, eachHeight,
487 TRUE);
488 */
489 if (m_windowStyle & wxRA_SPECIFY_ROWS)
490 {
491 y_offset += maxHeight;
492 if (m_radioWidth[0]>0)
493 y_offset += cy1/2;
494 }
495 else
496 x_offset += maxWidth + cx1;
497 }
498 }
499
500 void wxRadioBox::GetSize(int *width, int *height) const
501 {
502 RECT rect;
503 rect.xLeft = -1; rect.xRight = -1; rect.yTop = -1; rect.yBottom = -1;
504
505 if (m_hWnd)
506 wxFindMaxSize(m_hWnd, &rect);
507
508 int i;
509 for (i = 0; i < m_noItems; i++)
510 wxFindMaxSize(m_radioButtons[i], &rect);
511
512 *width = rect.xRight - rect.xLeft;
513 *height = rect.yBottom - rect.yTop;
514 }
515
516 void wxRadioBox::GetPosition(int *x, int *y) const
517 {
518 wxWindow *parent = GetParent();
519 RECT rect;
520 rect.xLeft = -1; rect.xRight = -1; rect.yTop = -1; rect.yBottom = -1;
521
522 int i;
523 for (i = 0; i < m_noItems; i++)
524 wxFindMaxSize(m_radioButtons[i], &rect);
525
526 if (m_hWnd)
527 wxFindMaxSize(m_hWnd, &rect);
528
529 // Since we now have the absolute screen coords,
530 // if there's a parent we must subtract its top left corner
531 POINTL point;
532 point.x = rect.xLeft;
533 point.y = rect.yTop;
534 // TODO:
535 /*
536 if (parent)
537 {
538 ::ScreenToClient((HWND) parent->GetHWND(), &point);
539 }
540 */
541 // We may be faking the client origin.
542 // So a window that's really at (0, 30) may appear
543 // (to wxWin apps) to be at (0, 0).
544 if (GetParent())
545 {
546 wxPoint pt(GetParent()->GetClientAreaOrigin());
547 point.x -= pt.x;
548 point.y -= pt.y;
549 }
550
551 *x = point.x;
552 *y = point.y;
553 }
554
555 void wxRadioBox::SetFocus()
556 {
557 // TODO:
558 /*
559 if (m_noItems > 0)
560 {
561 if (m_selectedButton == -1)
562 ::SetFocus((HWND) m_radioButtons[0]);
563 else
564 ::SetFocus((HWND) m_radioButtons[m_selectedButton]);
565 }
566 */
567 }
568
569 bool wxRadioBox::Show(bool show)
570 {
571 if ( !wxControl::Show(show) )
572 return FALSE;
573
574 int nCmdShow = 0; // TODO: show ? SW_SHOW : SW_HIDE;
575 for ( int i = 0; i < m_noItems; i++ )
576 {
577 // TODO: ::ShowWindow((HWND)m_radioButtons[i], nCmdShow);
578 }
579
580 return TRUE;
581 }
582
583 // Enable a specific button
584 void wxRadioBox::Enable(int item, bool enable)
585 {
586 wxCHECK_RET( item >= 0 && item < m_noItems,
587 wxT("invalid item in wxRadioBox::Enable()") );
588
589 // TODO: ::EnableWindow((HWND) m_radioButtons[item], enable);
590 }
591
592 // Enable all controls
593 bool wxRadioBox::Enable(bool enable)
594 {
595 if ( !wxControl::Enable(enable) )
596 return FALSE;
597
598 // TODO:
599 /*
600 for (int i = 0; i < m_noItems; i++)
601 ::EnableWindow((HWND) m_radioButtons[i], enable);
602 */
603 return TRUE;
604 }
605
606 // Show a specific button
607 void wxRadioBox::Show(int item, bool show)
608 {
609 wxCHECK_RET( item >= 0 && item < m_noItems,
610 wxT("invalid item in wxRadioBox::Show()") );
611
612 // TODO: ::ShowWindow((HWND)m_radioButtons[item], show ? SW_SHOW : SW_HIDE);
613 }
614
615 WXHBRUSH wxRadioBox::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
616 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
617 {
618 // TODO:
619 /*
620 if (GetParent()->GetTransparentBackground())
621 SetBkMode((HDC) pDC, TRANSPARENT);
622 else
623 SetBkMode((HDC) pDC, OPAQUE);
624
625 ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
626 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
627 */
628 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
629
630 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
631 }
632
633 // For single selection items only
634 wxString wxRadioBox::GetStringSelection() const
635 {
636 wxString result;
637 int sel = GetSelection();
638 if (sel > -1)
639 result = GetString(sel);
640
641 return result;
642 }
643
644 bool wxRadioBox::SetStringSelection(const wxString& s)
645 {
646 int sel = FindString (s);
647 if (sel > -1)
648 {
649 SetSelection (sel);
650 return TRUE;
651 }
652 else
653 return FALSE;
654 }
655
656 bool wxRadioBox::ContainsHWND(WXHWND hWnd) const
657 {
658 int i;
659 for (i = 0; i < Number(); i++)
660 {
661 if (GetRadioButtons()[i] == hWnd)
662 return TRUE;
663 }
664
665 return FALSE;
666 }
667
668 void wxRadioBox::Command (wxCommandEvent & event)
669 {
670 SetSelection (event.m_commandInt);
671 ProcessCommand (event);
672 }
673
674 void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn)
675 {
676 HWND hwndBtn = (HWND)hWndBtn;
677
678 // TODO:
679 /*
680 if ( !s_wndprocRadioBtn )
681 s_wndprocRadioBtn = (WXFARPROC)::GetWindowLong(hwndBtn, GWL_WNDPROC);
682
683 // No GWL_USERDATA in Win16, so omit this subclassing.
684 ::SetWindowLong(hwndBtn, GWL_WNDPROC, (long)wxRadioBtnWndProc);
685 ::SetWindowLong(hwndBtn, GWL_USERDATA, (long)this);
686 */
687 }
688
689 void wxRadioBox::SendNotificationEvent()
690 {
691 wxCommandEvent event(wxEVT_COMMAND_RADIOBOX_SELECTED, m_windowId);
692 event.SetInt( m_selectedButton );
693 event.SetString( GetString(m_selectedButton) );
694 event.SetEventObject( this );
695 ProcessCommand(event);
696 }
697
698 // ---------------------------------------------------------------------------
699 // window proc for radio buttons
700 // ---------------------------------------------------------------------------
701
702 MRESULT wxRadioBtnWndProc(HWND hwnd,
703 UINT msg,
704 MPARAM wParam,
705 MPARAM lParam)
706 {
707 bool processed = TRUE;
708 // if ( msg != WM_KEYDOWN )
709 // processed = FALSE;
710
711 if ( processed )
712 {
713 wxRadioBox *radiobox = NULL; // TODO: (wxRadioBox *)::GetWindowLong(hwnd, GWL_USERDATA);
714
715 wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") );
716
717 int sel = radiobox->GetSelection();
718
719 // TODO:
720 /*
721 switch ( wParam )
722 {
723 case VK_UP:
724 sel--;
725 break;
726
727 case VK_LEFT:
728 sel -= radiobox->GetNumVer();
729 break;
730
731 case VK_DOWN:
732 sel++;
733 break;
734
735 case VK_RIGHT:
736 sel += radiobox->GetNumVer();
737 break;
738
739 case VK_TAB:
740 {
741 wxNavigationKeyEvent event;
742 event.SetDirection(!(::GetKeyState(VK_SHIFT) & 0x100));
743 event.SetWindowChange(FALSE);
744 event.SetEventObject(radiobox);
745
746 if ( radiobox->GetEventHandler()->ProcessEvent(event) )
747 return 0;
748 }
749 // fall through
750
751 default:
752 processed = FALSE;
753 }
754 */
755 if ( processed )
756 {
757 if ( sel >= 0 && sel < radiobox->Number() )
758 {
759 radiobox->SetSelection(sel);
760
761 // emulate the button click
762 radiobox->SendNotificationEvent();
763 }
764 }
765 }
766
767 // TODO:
768 /*
769 if ( !processed )
770 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd, msg, wParam, lParam);
771 else
772 return 0;
773 */
774 return 0;
775 }
776