1. wxMDIParentFrame::~wxMDIParentFrame() bug fixed
[wxWidgets.git] / src / msw / radiobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: radiobox.cpp
3 // Purpose: wxRadioBox
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ===========================================================================
13 // declarations
14 // ===========================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "radiobox.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/bitmap.h"
33 #include "wx/brush.h"
34 #include "wx/radiobox.h"
35 #endif
36
37 #include "wx/msw/private.h"
38
39 #if wxUSE_TOOLTIPS
40 #include <commctrl.h>
41
42 #include "wx/tooltip.h"
43 #endif // wxUSE_TOOLTIPS
44
45 #if !USE_SHARED_LIBRARY
46 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl)
47 #endif
48
49 // VZ: the new behaviour is to create the radio buttons as children of the
50 // radiobox instead of creating them as children of the radiobox' parent.
51 //
52 // This seems more logical, more consistent with what other frameworks do
53 // and allows tooltips to work with radioboxes, so there should be no
54 // reason to revert to the backward compatible behaviour - but I still
55 // leave this possibility just in case.
56 #define RADIOBTN_PARENT_IS_RADIOBOX 1
57
58 // ---------------------------------------------------------------------------
59 // private functions
60 // ---------------------------------------------------------------------------
61
62 // wnd proc for radio buttons
63 #ifdef __WIN32__
64 LRESULT APIENTRY _EXPORT wxRadioBtnWndProc(HWND hWnd,
65 UINT message,
66 WPARAM wParam,
67 LPARAM lParam);
68
69 // ---------------------------------------------------------------------------
70 // global vars
71 // ---------------------------------------------------------------------------
72
73 // the pointer to standard radio button wnd proc
74 static WXFARPROC s_wndprocRadioBtn = (WXFARPROC)NULL;
75
76 #endif // __WIN32__
77
78 // ===========================================================================
79 // implementation
80 // ===========================================================================
81
82 // ---------------------------------------------------------------------------
83 // wxRadioBox
84 // ---------------------------------------------------------------------------
85
86 int wxRadioBox::GetNumVer() const
87 {
88 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
89 {
90 return m_majorDim;
91 }
92 else
93 {
94 return (m_noItems + m_majorDim - 1)/m_majorDim;
95 }
96 }
97
98 int wxRadioBox::GetNumHor() const
99 {
100 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
101 {
102 return (m_noItems + m_majorDim - 1)/m_majorDim;
103 }
104 else
105 {
106 return m_majorDim;
107 }
108 }
109
110 bool wxRadioBox::MSWCommand(WXUINT cmd, WXWORD id)
111 {
112 if ( cmd == BN_CLICKED )
113 {
114 int selectedButton = -1;
115
116 for ( int i = 0; i < m_noItems; i++ )
117 {
118 if ( id == wxGetWindowId(m_radioButtons[i]) )
119 {
120 selectedButton = i;
121
122 break;
123 }
124 }
125
126 wxASSERT_MSG( selectedButton != -1, wxT("click from alien button?") );
127
128 if ( selectedButton != m_selectedButton )
129 {
130 m_selectedButton = selectedButton;
131
132 SendNotificationEvent();
133 }
134 //else: don't generate events when the selection doesn't change
135
136 return TRUE;
137 }
138 else
139 return FALSE;
140 }
141
142 #if WXWIN_COMPATIBILITY
143 wxRadioBox::wxRadioBox(wxWindow *parent, wxFunction func, const char *title,
144 int x, int y, int width, int height,
145 int n, char **choices,
146 int majorDim, long style, const char *name)
147 {
148 wxString *choices2 = new wxString[n];
149 for ( int i = 0; i < n; i ++) choices2[i] = choices[i];
150 Create(parent, -1, title, wxPoint(x, y), wxSize(width, height), n, choices2, majorDim, style,
151 wxDefaultValidator, name);
152 Callback(func);
153 delete choices2;
154 }
155
156 #endif
157
158 // Radio box item
159 wxRadioBox::wxRadioBox()
160 {
161 m_selectedButton = -1;
162 m_noItems = 0;
163 m_noRowsOrCols = 0;
164 m_radioButtons = NULL;
165 m_majorDim = 0;
166 m_radioWidth = NULL;
167 m_radioHeight = NULL;
168 }
169
170 bool wxRadioBox::Create(wxWindow *parent,
171 wxWindowID id,
172 const wxString& title,
173 const wxPoint& pos,
174 const wxSize& size,
175 int n,
176 const wxString choices[],
177 int majorDim,
178 long style,
179 const wxValidator& val,
180 const wxString& name)
181 {
182 // initialize members
183 m_selectedButton = -1;
184 m_noItems = n;
185
186 m_majorDim = majorDim == 0 ? n : majorDim;
187 m_noRowsOrCols = majorDim;
188
189 // common initialization
190 if ( !CreateControl(parent, id, pos, size, style, val, name) )
191 return FALSE;
192
193 // create the static box
194 if ( !MSWCreateControl(wxT("BUTTON"), BS_GROUPBOX, pos, size, title, 0) )
195 return FALSE;
196
197 // and now create the buttons
198 #if RADIOBTN_PARENT_IS_RADIOBOX
199 HWND hwndParent = GetHwnd();
200 #else
201 HWND hwndParent = GetHwndOf(parent);
202 #endif
203
204 // Some radio boxes test consecutive id.
205 (void)NewControlId();
206 m_radioButtons = new WXHWND[n];
207 m_radioWidth = new int[n];
208 m_radioHeight = new int[n];
209
210 WXHFONT hfont = 0;
211 wxFont& font = GetFont();
212 if ( font.Ok() )
213 {
214 hfont = font.GetResourceHandle();
215 }
216
217 for ( int i = 0; i < n; i++ )
218 {
219 m_radioWidth[i] =
220 m_radioHeight[i] = -1;
221 long styleBtn = BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE;
222 if ( i == 0 && style == 0 )
223 styleBtn |= WS_GROUP;
224
225 long newId = NewControlId();
226
227 HWND hwndBtn = ::CreateWindow(_T("BUTTON"),
228 choices[i],
229 styleBtn,
230 0, 0, 0, 0, // will be set in SetSize()
231 hwndParent,
232 (HMENU)newId,
233 wxGetInstance(),
234 NULL);
235
236 if ( !hwndBtn )
237 {
238 wxLogLastError("CreateWindow(radio btn)");
239
240 return FALSE;
241 }
242
243 m_radioButtons[i] = (WXHWND)hwndBtn;
244
245 SubclassRadioButton((WXHWND)hwndBtn);
246
247 if ( hfont )
248 {
249 ::SendMessage(hwndBtn, WM_SETFONT, (WPARAM)hfont, 0L);
250 }
251
252 m_subControls.Add(newId);
253 }
254
255 // Create a dummy radio control to end the group.
256 (void)::CreateWindow(_T("BUTTON"),
257 _T(""),
258 WS_GROUP | BS_AUTORADIOBUTTON | WS_CHILD,
259 0, 0, 0, 0, hwndParent,
260 (HMENU)NewControlId(), wxGetInstance(), NULL);
261
262 SetSelection(0);
263
264 SetSize(pos.x, pos.y, size.x, size.y);
265
266 return TRUE;
267 }
268
269 wxRadioBox::~wxRadioBox()
270 {
271 m_isBeingDeleted = TRUE;
272
273 if (m_radioButtons)
274 {
275 int i;
276 for (i = 0; i < m_noItems; i++)
277 ::DestroyWindow((HWND)m_radioButtons[i]);
278 delete[] m_radioButtons;
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 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 if (m_selectedButton >= 0 && m_selectedButton < m_noItems)
329 ::SendMessage((HWND) m_radioButtons[m_selectedButton], BM_SETCHECK, 0, 0L);
330
331 ::SendMessage((HWND)m_radioButtons[N], BM_SETCHECK, 1, 0L);
332 ::SetFocus((HWND)m_radioButtons[N]);
333
334 m_selectedButton = N;
335 }
336
337 // Get single selection, for single choice list items
338 int wxRadioBox::GetSelection() const
339 {
340 return m_selectedButton;
341 }
342
343 // Find string for position
344 wxString wxRadioBox::GetString(int N) const
345 {
346 return wxGetWindowText(m_radioButtons[N]);
347 }
348
349 // Restored old code.
350 void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
351 {
352 int currentX, currentY;
353 GetPosition(&currentX, &currentY);
354 int widthOld, heightOld;
355 GetSize(&widthOld, &heightOld);
356
357 int xx = x;
358 int yy = y;
359
360 if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
361 xx = currentX;
362 if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
363 yy = currentY;
364
365 #if RADIOBTN_PARENT_IS_RADIOBOX
366 int y_offset = 0;
367 int x_offset = 0;
368 #else
369 int y_offset = yy;
370 int x_offset = xx;
371 #endif
372
373 int current_width, cyf;
374
375 int cx1,cy1;
376 wxGetCharSize(m_hWnd, &cx1, &cy1, & GetFont());
377
378 // Attempt to have a look coherent with other platforms: We compute the
379 // biggest toggle dim, then we align all items according this value.
380 int maxWidth = -1;
381 int maxHeight = -1;
382
383 int i;
384 for (i = 0 ; i < m_noItems; i++)
385 {
386 int eachWidth;
387 int eachHeight;
388 if (m_radioWidth[i]<0)
389 {
390 // It's a labelled toggle
391 GetTextExtent(wxGetWindowText(m_radioButtons[i]),
392 &current_width, &cyf);
393 eachWidth = (int)(current_width + RADIO_SIZE);
394 eachHeight = (int)((3*cyf)/2);
395 }
396 else
397 {
398 eachWidth = m_radioWidth[i];
399 eachHeight = m_radioHeight[i];
400 }
401
402 if (maxWidth<eachWidth)
403 maxWidth = eachWidth;
404 if (maxHeight<eachHeight)
405 maxHeight = eachHeight;
406 }
407
408 if (m_hWnd)
409 {
410 int totWidth;
411 int totHeight;
412
413 int nbHor = GetNumHor(),
414 nbVer = GetNumVer();
415
416 // this formula works, but I don't know why.
417 // Please, be sure what you do if you modify it!!
418 if (m_radioWidth[0]<0)
419 totHeight = (nbVer * maxHeight) + cy1/2;
420 else
421 totHeight = nbVer * (maxHeight+cy1/2);
422 totWidth = nbHor * (maxWidth+cx1);
423
424 int extraHeight = cy1;
425
426 #if defined(CTL3D) && !CTL3D
427 // Requires a bigger group box in plain Windows
428 extraHeight *= 3;
429 extraHeight /= 2;
430 #endif
431
432 // only change our width/height if asked for
433 if ( width == -1 )
434 {
435 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
436 width = totWidth + cx1;
437 else
438 width = widthOld;
439 }
440
441 if ( height == -1 )
442 {
443 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
444 height = totHeight + extraHeight;
445 else
446 height = heightOld;
447 }
448
449 ::MoveWindow(GetHwnd(), xx, yy, width, height, TRUE);
450
451 x_offset += cx1;
452 y_offset += cy1;
453 }
454
455 #if defined(CTL3D) && (!CTL3D)
456 y_offset += (int)(cy1/2); // Fudge factor since buttons overlapped label
457 // JACS 2/12/93. CTL3D draws group label quite high.
458 #endif
459 int startX = x_offset;
460 int startY = y_offset;
461
462 for ( i = 0 ; i < m_noItems; i++)
463 {
464 // Bidimensional radio adjustment
465 if (i&&((i%m_majorDim)==0)) // Why is this omitted for i = 0?
466 {
467 if (m_windowStyle & wxRA_VERTICAL)
468 {
469 y_offset = startY;
470 x_offset += maxWidth + cx1;
471 }
472 else
473 {
474 x_offset = startX;
475 y_offset += maxHeight;
476 if (m_radioWidth[0]>0)
477 y_offset += cy1/2;
478 }
479 }
480 int eachWidth;
481 int eachHeight;
482 if (m_radioWidth[i]<0)
483 {
484 // It's a labeled item
485 GetTextExtent(wxGetWindowText(m_radioButtons[i]),
486 &current_width, &cyf);
487
488 // How do we find out radio button bitmap size!!
489 // By adjusting them carefully, manually :-)
490 eachWidth = (int)(current_width + RADIO_SIZE);
491 eachHeight = (int)((3*cyf)/2);
492 }
493 else
494 {
495 eachWidth = m_radioWidth[i];
496 eachHeight = m_radioHeight[i];
497 }
498
499 // VZ: make all buttons of the same, maximal size - like this they
500 // cover the radiobox entirely and the radiobox tooltips are always
501 // shown (otherwise they are not when the mouse pointer is in the
502 // radiobox part not belonging to any radiobutton)
503 ::MoveWindow((HWND)m_radioButtons[i],
504 x_offset, y_offset, maxWidth, maxHeight,
505 TRUE);
506
507 if (m_windowStyle & wxRA_SPECIFY_ROWS)
508 {
509 y_offset += maxHeight;
510 if (m_radioWidth[0]>0)
511 y_offset += cy1/2;
512 }
513 else
514 x_offset += maxWidth + cx1;
515 }
516 }
517
518 void wxRadioBox::GetSize(int *width, int *height) const
519 {
520 RECT rect;
521 rect.left = -1; rect.right = -1; rect.top = -1; rect.bottom = -1;
522
523 if (m_hWnd)
524 wxFindMaxSize(m_hWnd, &rect);
525
526 int i;
527 for (i = 0; i < m_noItems; i++)
528 wxFindMaxSize(m_radioButtons[i], &rect);
529
530 *width = rect.right - rect.left;
531 *height = rect.bottom - rect.top;
532 }
533
534 void wxRadioBox::GetPosition(int *x, int *y) const
535 {
536 wxWindow *parent = GetParent();
537 RECT rect = { -1, -1, -1, -1 };
538
539 int i;
540 for (i = 0; i < m_noItems; i++)
541 wxFindMaxSize(m_radioButtons[i], &rect);
542
543 if (m_hWnd)
544 wxFindMaxSize(m_hWnd, &rect);
545
546 // Since we now have the absolute screen coords, if there's a parent we
547 // must subtract its top left corner
548 POINT point;
549 point.x = rect.left;
550 point.y = rect.top;
551 if (parent)
552 {
553 ::ScreenToClient((HWND) parent->GetHWND(), &point);
554 }
555
556 // We may be faking the client origin. So a window that's really at (0, 30)
557 // may appear (to wxWin apps) to be at (0, 0).
558 if (GetParent())
559 {
560 wxPoint pt(GetParent()->GetClientAreaOrigin());
561 point.x -= pt.x;
562 point.y -= pt.y;
563 }
564
565 *x = point.x;
566 *y = point.y;
567 }
568
569 void wxRadioBox::SetFocus()
570 {
571 if (m_noItems > 0)
572 {
573 if (m_selectedButton == -1)
574 ::SetFocus((HWND) m_radioButtons[0]);
575 else
576 ::SetFocus((HWND) m_radioButtons[m_selectedButton]);
577 }
578
579 }
580
581 bool wxRadioBox::Show(bool show)
582 {
583 if ( !wxControl::Show(show) )
584 return FALSE;
585
586 int nCmdShow = show ? SW_SHOW : SW_HIDE;
587 for ( int i = 0; i < m_noItems; i++ )
588 {
589 ::ShowWindow((HWND)m_radioButtons[i], nCmdShow);
590 }
591
592 return TRUE;
593 }
594
595 // Enable a specific button
596 void wxRadioBox::Enable(int item, bool enable)
597 {
598 wxCHECK_RET( item >= 0 && item < m_noItems,
599 wxT("invalid item in wxRadioBox::Enable()") );
600
601 ::EnableWindow((HWND) m_radioButtons[item], enable);
602 }
603
604 // Enable all controls
605 bool wxRadioBox::Enable(bool enable)
606 {
607 if ( !wxControl::Enable(enable) )
608 return FALSE;
609
610 for (int i = 0; i < m_noItems; i++)
611 ::EnableWindow((HWND) m_radioButtons[i], enable);
612
613 return TRUE;
614 }
615
616 // Show a specific button
617 void wxRadioBox::Show(int item, bool show)
618 {
619 wxCHECK_RET( item >= 0 && item < m_noItems,
620 wxT("invalid item in wxRadioBox::Show()") );
621
622 ::ShowWindow((HWND)m_radioButtons[item], show ? SW_SHOW : SW_HIDE);
623 }
624
625 // For single selection items only
626 wxString wxRadioBox::GetStringSelection() const
627 {
628 wxString result;
629 int sel = GetSelection();
630 if (sel > -1)
631 result = GetString(sel);
632
633 return result;
634 }
635
636 bool wxRadioBox::SetStringSelection(const wxString& s)
637 {
638 int sel = FindString (s);
639 if (sel > -1)
640 {
641 SetSelection (sel);
642 return TRUE;
643 }
644 else
645 return FALSE;
646 }
647
648 bool wxRadioBox::ContainsHWND(WXHWND hWnd) const
649 {
650 int i;
651 for (i = 0; i < Number(); i++)
652 {
653 if (GetRadioButtons()[i] == hWnd)
654 return TRUE;
655 }
656
657 return FALSE;
658 }
659
660 void wxRadioBox::Command (wxCommandEvent & event)
661 {
662 SetSelection (event.m_commandInt);
663 ProcessCommand (event);
664 }
665
666 void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn)
667 {
668 #ifdef __WIN32__
669 HWND hwndBtn = (HWND)hWndBtn;
670
671 if ( !s_wndprocRadioBtn )
672 s_wndprocRadioBtn = (WXFARPROC)::GetWindowLong(hwndBtn, GWL_WNDPROC);
673
674 // No GWL_USERDATA in Win16, so omit this subclassing.
675 ::SetWindowLong(hwndBtn, GWL_WNDPROC, (long)wxRadioBtnWndProc);
676 ::SetWindowLong(hwndBtn, GWL_USERDATA, (long)this);
677 #endif // __WIN32__
678 }
679
680 void wxRadioBox::SendNotificationEvent()
681 {
682 wxCommandEvent event(wxEVT_COMMAND_RADIOBOX_SELECTED, m_windowId);
683 event.SetInt( m_selectedButton );
684 event.SetString( GetString(m_selectedButton) );
685 event.SetEventObject( this );
686 ProcessCommand(event);
687 }
688
689 // ---------------------------------------------------------------------------
690 // window proc for radio buttons
691 // ---------------------------------------------------------------------------
692
693 #ifdef __WIN32__
694
695 LRESULT APIENTRY _EXPORT wxRadioBtnWndProc(HWND hwnd,
696 UINT msg,
697 WPARAM wParam,
698 LPARAM lParam)
699 {
700 bool processed = FALSE;
701 if ( msg == WM_KEYDOWN
702 #if wxUSE_TOOLTIPS
703 || msg == WM_NOTIFY
704 #endif // wxUSE_TOOLTIPS
705 )
706 {
707 wxRadioBox *radiobox = (wxRadioBox *)::GetWindowLong(hwnd, GWL_USERDATA);
708
709 wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") );
710
711 #if wxUSE_TOOLTIPS
712 if ( msg == WM_NOTIFY )
713 {
714 NMHDR* hdr = (NMHDR *)lParam;
715 if ( (int)hdr->code == TTN_NEEDTEXT )
716 {
717 wxToolTip *tt = radiobox->GetToolTip();
718 if ( tt )
719 {
720 TOOLTIPTEXT *ttt = (TOOLTIPTEXT *)lParam;
721 ttt->lpszText = (wxChar *)tt->GetTip().c_str();
722
723 processed = TRUE;
724 }
725 }
726 }
727 else // msg == WM_KEYDOWN
728 #endif // wxUSE_TOOLTIPS
729 {
730 processed = TRUE;
731
732 int sel = radiobox->GetSelection();
733
734 switch ( wParam )
735 {
736 case VK_UP:
737 sel--;
738 break;
739
740 case VK_LEFT:
741 sel -= radiobox->GetNumVer();
742 break;
743
744 case VK_DOWN:
745 sel++;
746 break;
747
748 case VK_RIGHT:
749 sel += radiobox->GetNumVer();
750 break;
751
752 case VK_TAB:
753 {
754 wxNavigationKeyEvent event;
755 event.SetDirection(!(::GetKeyState(VK_SHIFT) & 0x100));
756 event.SetWindowChange(FALSE);
757 event.SetEventObject(radiobox);
758
759 if ( radiobox->GetEventHandler()->ProcessEvent(event) )
760 return 0;
761 }
762 // fall through
763
764 default:
765 processed = FALSE;
766 }
767
768 if ( processed )
769 {
770 if ( sel >= 0 && sel < radiobox->Number() )
771 {
772 radiobox->SetSelection(sel);
773
774 // emulate the button click
775 radiobox->SendNotificationEvent();
776 }
777 }
778 }
779 }
780
781 if ( processed )
782 return 0;
783
784 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd, msg, wParam, lParam);
785 }
786
787 #endif // __WIN32__
788