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