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