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