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