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