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