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