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