]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
3ca6a5f0 BP |
2 | // Name: msw/radiobox.cpp |
3 | // Purpose: wxRadioBox implementation | |
2bda0e17 KB |
4 | // Author: Julian Smart |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
6c9a19aa | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
e373f51b VZ |
12 | // =========================================================================== |
13 | // declarations | |
14 | // =========================================================================== | |
15 | ||
16 | // --------------------------------------------------------------------------- | |
17 | // headers | |
18 | // --------------------------------------------------------------------------- | |
19 | ||
14f355c2 | 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
da07e033 | 21 | #pragma implementation "radiobox.h" |
2bda0e17 KB |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
da07e033 | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
1e6feb95 VZ |
31 | #if wxUSE_RADIOBOX |
32 | ||
2bda0e17 | 33 | #ifndef WX_PRECOMP |
da07e033 VZ |
34 | #include "wx/bitmap.h" |
35 | #include "wx/brush.h" | |
36 | #include "wx/radiobox.h" | |
f6bcfd97 | 37 | #include "wx/settings.h" |
381dd4bf | 38 | #include "wx/log.h" |
2bda0e17 KB |
39 | #endif |
40 | ||
bd0a76e2 | 41 | #include "wx/msw/subwin.h" |
2bda0e17 | 42 | |
f048e32f | 43 | #if wxUSE_TOOLTIPS |
ae090fdb | 44 | #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__) |
8614c467 | 45 | #include <commctrl.h> |
5ace0213 | 46 | #endif |
f048e32f VZ |
47 | #include "wx/tooltip.h" |
48 | #endif // wxUSE_TOOLTIPS | |
49 | ||
3ff066a4 | 50 | // TODO: wxCONSTRUCTOR |
bc9fb572 JS |
51 | #if 0 // wxUSE_EXTENDED_RTTI |
52 | WX_DEFINE_FLAGS( wxRadioBoxStyle ) | |
53 | ||
3ff066a4 | 54 | wxBEGIN_FLAGS( wxRadioBoxStyle ) |
bc9fb572 JS |
55 | // new style border flags, we put them first to |
56 | // use them for streaming out | |
3ff066a4 SC |
57 | wxFLAGS_MEMBER(wxBORDER_SIMPLE) |
58 | wxFLAGS_MEMBER(wxBORDER_SUNKEN) | |
59 | wxFLAGS_MEMBER(wxBORDER_DOUBLE) | |
60 | wxFLAGS_MEMBER(wxBORDER_RAISED) | |
61 | wxFLAGS_MEMBER(wxBORDER_STATIC) | |
62 | wxFLAGS_MEMBER(wxBORDER_NONE) | |
4d08943e | 63 | |
bc9fb572 | 64 | // old style border flags |
3ff066a4 SC |
65 | wxFLAGS_MEMBER(wxSIMPLE_BORDER) |
66 | wxFLAGS_MEMBER(wxSUNKEN_BORDER) | |
67 | wxFLAGS_MEMBER(wxDOUBLE_BORDER) | |
68 | wxFLAGS_MEMBER(wxRAISED_BORDER) | |
69 | wxFLAGS_MEMBER(wxSTATIC_BORDER) | |
cb0afb26 | 70 | wxFLAGS_MEMBER(wxBORDER) |
bc9fb572 JS |
71 | |
72 | // standard window styles | |
3ff066a4 SC |
73 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) |
74 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) | |
75 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) | |
76 | wxFLAGS_MEMBER(wxWANTS_CHARS) | |
cb0afb26 | 77 | wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE) |
3ff066a4 SC |
78 | wxFLAGS_MEMBER(wxALWAYS_SHOW_SB ) |
79 | wxFLAGS_MEMBER(wxVSCROLL) | |
80 | wxFLAGS_MEMBER(wxHSCROLL) | |
81 | ||
82 | wxFLAGS_MEMBER(wxRA_SPECIFY_COLS) | |
83 | wxFLAGS_MEMBER(wxRA_HORIZONTAL) | |
84 | wxFLAGS_MEMBER(wxRA_SPECIFY_ROWS) | |
85 | wxFLAGS_MEMBER(wxRA_VERTICAL) | |
86 | ||
87 | wxEND_FLAGS( wxRadioBoxStyle ) | |
bc9fb572 JS |
88 | |
89 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxRadioBox, wxControl,"wx/radiobox.h") | |
90 | ||
3ff066a4 SC |
91 | wxBEGIN_PROPERTIES_TABLE(wxRadioBox) |
92 | wxEVENT_PROPERTY( Select , wxEVT_COMMAND_RADIOBOX_SELECTED , wxCommandEvent ) | |
93 | wxPROPERTY_FLAGS( WindowStyle , wxRadioBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style | |
94 | wxEND_PROPERTIES_TABLE() | |
bc9fb572 JS |
95 | |
96 | #else | |
d3acd369 | 97 | IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl) |
bc9fb572 | 98 | #endif |
2bda0e17 | 99 | |
066f1b7a | 100 | /* |
4d08943e WS |
101 | selection |
102 | content | |
103 | label | |
104 | dimension | |
105 | item | |
066f1b7a SC |
106 | */ |
107 | ||
e373f51b VZ |
108 | // --------------------------------------------------------------------------- |
109 | // private functions | |
110 | // --------------------------------------------------------------------------- | |
111 | ||
e373f51b VZ |
112 | // wnd proc for radio buttons |
113 | LRESULT APIENTRY _EXPORT wxRadioBtnWndProc(HWND hWnd, | |
114 | UINT message, | |
115 | WPARAM wParam, | |
116 | LPARAM lParam); | |
117 | ||
118 | // --------------------------------------------------------------------------- | |
119 | // global vars | |
120 | // --------------------------------------------------------------------------- | |
121 | ||
122 | // the pointer to standard radio button wnd proc | |
457e6c54 | 123 | static WXFARPROC s_wndprocRadioBtn = (WXFARPROC)NULL; |
e373f51b VZ |
124 | |
125 | // =========================================================================== | |
126 | // implementation | |
127 | // =========================================================================== | |
128 | ||
129 | // --------------------------------------------------------------------------- | |
bd0a76e2 | 130 | // wxRadioBox creation |
e373f51b VZ |
131 | // --------------------------------------------------------------------------- |
132 | ||
2bda0e17 | 133 | // Radio box item |
bd0a76e2 | 134 | void wxRadioBox::Init() |
2bda0e17 | 135 | { |
bd0a76e2 | 136 | m_selectedButton = wxNOT_FOUND; |
da07e033 | 137 | m_radioButtons = NULL; |
e373f51b VZ |
138 | m_majorDim = 0; |
139 | m_radioWidth = NULL; | |
140 | m_radioHeight = NULL; | |
2bda0e17 KB |
141 | } |
142 | ||
f048e32f VZ |
143 | bool wxRadioBox::Create(wxWindow *parent, |
144 | wxWindowID id, | |
145 | const wxString& title, | |
146 | const wxPoint& pos, | |
147 | const wxSize& size, | |
148 | int n, | |
149 | const wxString choices[], | |
150 | int majorDim, | |
151 | long style, | |
152 | const wxValidator& val, | |
153 | const wxString& name) | |
2bda0e17 | 154 | { |
f048e32f | 155 | // initialize members |
3ca6a5f0 | 156 | m_majorDim = majorDim == 0 ? n : majorDim; |
da07e033 | 157 | |
f048e32f | 158 | // common initialization |
bd0a76e2 | 159 | if ( !wxStaticBox::Create(parent, id, title, pos, size, style, name) ) |
4d08943e | 160 | return false; |
2bda0e17 | 161 | |
bd0a76e2 VZ |
162 | #if wxUSE_VALIDATORS |
163 | SetValidator(val); | |
164 | #else | |
165 | wxUnusedVar(val); | |
166 | #endif // wxUSE_VALIDATORS/!wxUSE_VALIDATORS | |
da07e033 | 167 | |
bd0a76e2 | 168 | m_radioButtons = new wxSubwindows(n); |
e373f51b VZ |
169 | m_radioWidth = new int[n]; |
170 | m_radioHeight = new int[n]; | |
f048e32f | 171 | |
f048e32f VZ |
172 | for ( int i = 0; i < n; i++ ) |
173 | { | |
174 | m_radioWidth[i] = | |
4d08943e | 175 | m_radioHeight[i] = wxDefaultCoord; |
d3acd369 | 176 | long styleBtn = BS_AUTORADIOBUTTON | WS_TABSTOP | WS_CHILD | WS_VISIBLE; |
1f621c2f | 177 | if ( i == 0 ) |
f048e32f VZ |
178 | styleBtn |= WS_GROUP; |
179 | ||
da07e033 | 180 | long newId = NewControlId(); |
da07e033 | 181 | |
f048e32f VZ |
182 | HWND hwndBtn = ::CreateWindow(_T("BUTTON"), |
183 | choices[i], | |
184 | styleBtn, | |
185 | 0, 0, 0, 0, // will be set in SetSize() | |
eba99da4 | 186 | GetHwnd(), |
f048e32f VZ |
187 | (HMENU)newId, |
188 | wxGetInstance(), | |
e373f51b | 189 | NULL); |
2bda0e17 | 190 | |
f048e32f VZ |
191 | if ( !hwndBtn ) |
192 | { | |
f6bcfd97 | 193 | wxLogLastError(wxT("CreateWindow(radio btn)")); |
f048e32f | 194 | |
4d08943e | 195 | return false; |
f048e32f VZ |
196 | } |
197 | ||
bd0a76e2 | 198 | (*m_radioButtons)[i] = hwndBtn; |
42e69d6b | 199 | |
e373f51b | 200 | SubclassRadioButton((WXHWND)hwndBtn); |
2bda0e17 | 201 | |
f048e32f | 202 | m_subControls.Add(newId); |
da07e033 | 203 | } |
e373f51b | 204 | |
da07e033 | 205 | // Create a dummy radio control to end the group. |
f048e32f | 206 | (void)::CreateWindow(_T("BUTTON"), |
fda7962d | 207 | wxEmptyString, |
f048e32f | 208 | WS_GROUP | BS_AUTORADIOBUTTON | WS_CHILD, |
eba99da4 | 209 | 0, 0, 0, 0, GetHwnd(), |
e373f51b | 210 | (HMENU)NewControlId(), wxGetInstance(), NULL); |
2bda0e17 | 211 | |
bd0a76e2 VZ |
212 | m_radioButtons->SetFont(GetFont()); |
213 | ||
64133c38 JS |
214 | #ifdef __WXWINCE__ |
215 | // Set the z-order correctly | |
d26e1ab2 | 216 | SetWindowPos(GetHwnd(), HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE); |
64133c38 JS |
217 | #endif |
218 | ||
da07e033 | 219 | SetSelection(0); |
f048e32f | 220 | SetSize(pos.x, pos.y, size.x, size.y); |
2bda0e17 | 221 | |
7a5a5718 RD |
222 | // Now that we have items determine what is the best size and set it. |
223 | SetBestSize(size); | |
4d08943e WS |
224 | |
225 | return true; | |
2bda0e17 | 226 | } |
2bda0e17 | 227 | |
584ad2a3 MB |
228 | bool wxRadioBox::Create(wxWindow *parent, |
229 | wxWindowID id, | |
230 | const wxString& title, | |
231 | const wxPoint& pos, | |
232 | const wxSize& size, | |
233 | const wxArrayString& choices, | |
234 | int majorDim, | |
235 | long style, | |
236 | const wxValidator& val, | |
237 | const wxString& name) | |
238 | { | |
239 | wxCArrayString chs(choices); | |
240 | return Create(parent, id, title, pos, size, chs.GetCount(), | |
241 | chs.GetStrings(), majorDim, style, val, name); | |
242 | } | |
243 | ||
e373f51b | 244 | wxRadioBox::~wxRadioBox() |
2bda0e17 | 245 | { |
4d08943e | 246 | m_isBeingDeleted = true; |
1eb20d4a | 247 | |
bd0a76e2 VZ |
248 | delete m_radioButtons; |
249 | delete[] m_radioWidth; | |
250 | delete[] m_radioHeight; | |
251 | } | |
252 | ||
253 | // NB: if this code is changed, wxGetWindowForHWND() which relies on having the | |
254 | // radiobox pointer in GWL_USERDATA for radio buttons must be updated too! | |
255 | void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn) | |
256 | { | |
257 | HWND hwndBtn = (HWND)hWndBtn; | |
258 | ||
259 | if ( !s_wndprocRadioBtn ) | |
260 | s_wndprocRadioBtn = (WXFARPROC)wxGetWindowProc(hwndBtn); | |
261 | ||
262 | wxSetWindowProc(hwndBtn, wxRadioBtnWndProc); | |
263 | wxSetWindowUserData(hwndBtn, this); | |
264 | } | |
265 | ||
266 | // ---------------------------------------------------------------------------- | |
267 | // events generation | |
268 | // ---------------------------------------------------------------------------- | |
269 | ||
270 | bool wxRadioBox::MSWCommand(WXUINT cmd, WXWORD id) | |
271 | { | |
272 | if ( cmd == BN_CLICKED ) | |
da07e033 | 273 | { |
bd0a76e2 VZ |
274 | if (id == GetId()) |
275 | return true; | |
276 | ||
277 | int selectedButton = wxNOT_FOUND; | |
278 | ||
279 | int count = GetCount(); | |
280 | for ( int i = 0; i < count; i++ ) | |
281 | { | |
282 | if ( id == wxGetWindowId((*m_radioButtons)[i]) ) | |
283 | { | |
284 | selectedButton = i; | |
285 | ||
286 | break; | |
287 | } | |
288 | } | |
289 | ||
290 | if ( selectedButton == wxNOT_FOUND ) | |
291 | { | |
292 | // just ignore it - due to a hack with WM_NCHITTEST handling in our | |
293 | // wnd proc, we can receive dummy click messages when we click near | |
294 | // the radiobox edge (this is ugly but Julian wouldn't let me get | |
295 | // rid of this...) | |
296 | return false; | |
297 | } | |
298 | ||
299 | if ( selectedButton != m_selectedButton ) | |
300 | { | |
301 | m_selectedButton = selectedButton; | |
302 | ||
303 | SendNotificationEvent(); | |
304 | } | |
305 | //else: don't generate events when the selection doesn't change | |
306 | ||
307 | return true; | |
da07e033 | 308 | } |
bd0a76e2 VZ |
309 | else |
310 | return false; | |
311 | } | |
42e69d6b | 312 | |
bd0a76e2 VZ |
313 | void wxRadioBox::Command(wxCommandEvent & event) |
314 | { | |
687706f5 | 315 | SetSelection (event.GetInt()); |
bd0a76e2 VZ |
316 | SetFocus(); |
317 | ProcessCommand(event); | |
318 | } | |
2bda0e17 | 319 | |
bd0a76e2 VZ |
320 | void wxRadioBox::SendNotificationEvent() |
321 | { | |
322 | wxCommandEvent event(wxEVT_COMMAND_RADIOBOX_SELECTED, m_windowId); | |
323 | event.SetInt( m_selectedButton ); | |
324 | event.SetString( GetString(m_selectedButton) ); | |
325 | event.SetEventObject( this ); | |
326 | ProcessCommand(event); | |
2bda0e17 KB |
327 | } |
328 | ||
bd0a76e2 VZ |
329 | // ---------------------------------------------------------------------------- |
330 | // simple accessors | |
331 | // ---------------------------------------------------------------------------- | |
332 | ||
333 | int wxRadioBox::GetCount() const | |
2bda0e17 | 334 | { |
bd0a76e2 VZ |
335 | return m_radioButtons->GetCount(); |
336 | } | |
d66a042c | 337 | |
bd0a76e2 VZ |
338 | // returns the number of rows |
339 | int wxRadioBox::GetNumVer() const | |
340 | { | |
341 | if ( m_windowStyle & wxRA_SPECIFY_ROWS ) | |
342 | { | |
343 | return m_majorDim; | |
344 | } | |
345 | else | |
346 | { | |
347 | return (GetCount() + m_majorDim - 1)/m_majorDim; | |
348 | } | |
2bda0e17 KB |
349 | } |
350 | ||
bd0a76e2 VZ |
351 | // returns the number of columns |
352 | int wxRadioBox::GetNumHor() const | |
2bda0e17 | 353 | { |
bd0a76e2 VZ |
354 | if ( m_windowStyle & wxRA_SPECIFY_ROWS ) |
355 | { | |
356 | return (GetCount() + m_majorDim - 1)/m_majorDim; | |
357 | } | |
358 | else | |
359 | { | |
360 | return m_majorDim; | |
361 | } | |
362 | } | |
2bda0e17 | 363 | |
bd0a76e2 VZ |
364 | void wxRadioBox::SetString(int item, const wxString& label) |
365 | { | |
789f6795 | 366 | wxCHECK_RET( IsValid(item), wxT("invalid radiobox index") ); |
e373f51b | 367 | |
bd0a76e2 VZ |
368 | m_radioWidth[item] = |
369 | m_radioHeight[item] = wxDefaultCoord; | |
2bda0e17 | 370 | |
bd0a76e2 | 371 | ::SetWindowText((*m_radioButtons)[item], label.c_str()); |
2bda0e17 KB |
372 | } |
373 | ||
bd0a76e2 | 374 | void wxRadioBox::SetSelection(int N) |
2bda0e17 | 375 | { |
789f6795 | 376 | wxCHECK_RET( IsValid(N), wxT("invalid radiobox index") ); |
bd0a76e2 | 377 | |
27758ba7 VZ |
378 | // unselect the old button |
379 | if ( m_selectedButton != wxNOT_FOUND ) | |
380 | ::SendMessage((*m_radioButtons)[m_selectedButton], BM_SETCHECK, 0, 0L); | |
381 | ||
382 | // and select the new one | |
bd0a76e2 VZ |
383 | ::SendMessage((*m_radioButtons)[N], BM_SETCHECK, 1, 0L); |
384 | ||
385 | m_selectedButton = N; | |
2bda0e17 KB |
386 | } |
387 | ||
388 | // Find string for position | |
1e6feb95 | 389 | wxString wxRadioBox::GetString(int item) const |
2bda0e17 | 390 | { |
789f6795 | 391 | wxCHECK_MSG( IsValid(item), wxEmptyString, |
1e6feb95 VZ |
392 | wxT("invalid radiobox index") ); |
393 | ||
bd0a76e2 VZ |
394 | return wxGetWindowText((*m_radioButtons)[item]); |
395 | } | |
396 | ||
397 | void wxRadioBox::SetFocus() | |
398 | { | |
399 | if ( GetCount() > 0 ) | |
400 | { | |
401 | ::SetFocus((*m_radioButtons)[m_selectedButton == wxNOT_FOUND | |
402 | ? 0 | |
403 | : m_selectedButton]); | |
404 | } | |
405 | } | |
406 | ||
407 | // Enable a specific button | |
1a87edf2 | 408 | bool wxRadioBox::Enable(int item, bool enable) |
bd0a76e2 | 409 | { |
789f6795 | 410 | wxCHECK_MSG( IsValid(item), false, |
bd0a76e2 VZ |
411 | wxT("invalid item in wxRadioBox::Enable()") ); |
412 | ||
789f6795 WS |
413 | BOOL ret = ::EnableWindow((*m_radioButtons)[item], enable); |
414 | ||
415 | return (ret == 0) == enable; | |
2bda0e17 KB |
416 | } |
417 | ||
bd0a76e2 | 418 | // Show a specific button |
789f6795 | 419 | bool wxRadioBox::Show(int item, bool show) |
bd0a76e2 | 420 | { |
789f6795 | 421 | wxCHECK_MSG( IsValid(item), false, |
bd0a76e2 VZ |
422 | wxT("invalid item in wxRadioBox::Show()") ); |
423 | ||
789f6795 WS |
424 | BOOL ret = ::ShowWindow((*m_radioButtons)[item], show ? SW_SHOW : SW_HIDE); |
425 | ||
426 | return (ret != 0) == show; | |
bd0a76e2 VZ |
427 | } |
428 | ||
429 | WX_FORWARD_STD_METHODS_TO_SUBWINDOWS(wxRadioBox, wxStaticBox, m_radioButtons) | |
430 | ||
3ca6a5f0 BP |
431 | // ---------------------------------------------------------------------------- |
432 | // size calculations | |
433 | // ---------------------------------------------------------------------------- | |
434 | ||
435 | wxSize wxRadioBox::GetMaxButtonSize() const | |
436 | { | |
437 | // calculate the max button size | |
438 | int widthMax = 0, | |
439 | heightMax = 0; | |
bd0a76e2 VZ |
440 | const int count = GetCount(); |
441 | for ( int i = 0 ; i < count; i++ ) | |
3ca6a5f0 BP |
442 | { |
443 | int width, height; | |
444 | if ( m_radioWidth[i] < 0 ) | |
445 | { | |
bd0a76e2 | 446 | GetTextExtent(wxGetWindowText((*m_radioButtons)[i]), &width, &height); |
3ca6a5f0 BP |
447 | |
448 | // adjust the size to take into account the radio box itself | |
449 | // FIXME this is totally bogus! | |
450 | width += RADIO_SIZE; | |
451 | height *= 3; | |
452 | height /= 2; | |
453 | } | |
454 | else | |
455 | { | |
456 | width = m_radioWidth[i]; | |
457 | height = m_radioHeight[i]; | |
458 | } | |
459 | ||
460 | if ( widthMax < width ) | |
461 | widthMax = width; | |
462 | if ( heightMax < height ) | |
463 | heightMax = height; | |
464 | } | |
465 | ||
466 | return wxSize(widthMax, heightMax); | |
467 | } | |
468 | ||
469 | wxSize wxRadioBox::GetTotalButtonSize(const wxSize& sizeBtn) const | |
470 | { | |
471 | // the radiobox should be big enough for its buttons | |
472 | int cx1, cy1; | |
7a5e53ab | 473 | wxGetCharSize(m_hWnd, &cx1, &cy1, GetFont()); |
3ca6a5f0 BP |
474 | |
475 | int extraHeight = cy1; | |
476 | ||
3ca6a5f0 BP |
477 | int height = GetNumVer() * sizeBtn.y + cy1/2 + extraHeight; |
478 | int width = GetNumHor() * (sizeBtn.x + cx1) + cx1; | |
479 | ||
c7fd6717 | 480 | // Add extra space under the label, if it exists. |
1a87edf2 | 481 | if (!wxControl::GetLabel().empty()) |
c7fd6717 JS |
482 | height += cy1/2; |
483 | ||
3ca6a5f0 BP |
484 | // and also wide enough for its label |
485 | int widthLabel; | |
486 | GetTextExtent(GetTitle(), &widthLabel, NULL); | |
487 | widthLabel += RADIO_SIZE; // FIXME this is bogus too | |
488 | if ( widthLabel > width ) | |
489 | width = widthLabel; | |
490 | ||
491 | return wxSize(width, height); | |
492 | } | |
493 | ||
494 | wxSize wxRadioBox::DoGetBestSize() const | |
495 | { | |
496 | return GetTotalButtonSize(GetMaxButtonSize()); | |
497 | } | |
498 | ||
1f916a19 | 499 | // Restored old code. |
bfc6fde4 | 500 | void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags) |
1f916a19 | 501 | { |
4438caf4 VZ |
502 | int currentX, currentY; |
503 | GetPosition(¤tX, ¤tY); | |
c219cecc VZ |
504 | int widthOld, heightOld; |
505 | GetSize(&widthOld, &heightOld); | |
506 | ||
4438caf4 VZ |
507 | int xx = x; |
508 | int yy = y; | |
509 | ||
4d08943e | 510 | if (x == wxDefaultCoord && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) |
4438caf4 | 511 | xx = currentX; |
4d08943e | 512 | if (y == wxDefaultCoord && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) |
4438caf4 VZ |
513 | yy = currentY; |
514 | ||
eba99da4 JS |
515 | int y_offset = 0; |
516 | int x_offset = 0; | |
f048e32f | 517 | |
3ca6a5f0 | 518 | int cx1, cy1; |
7a5e53ab | 519 | wxGetCharSize(m_hWnd, &cx1, &cy1, GetFont()); |
4438caf4 VZ |
520 | |
521 | // Attempt to have a look coherent with other platforms: We compute the | |
522 | // biggest toggle dim, then we align all items according this value. | |
3ca6a5f0 BP |
523 | wxSize maxSize = GetMaxButtonSize(); |
524 | int maxWidth = maxSize.x, | |
525 | maxHeight = maxSize.y; | |
4438caf4 | 526 | |
3ca6a5f0 BP |
527 | wxSize totSize = GetTotalButtonSize(maxSize); |
528 | int totWidth = totSize.x, | |
529 | totHeight = totSize.y; | |
530 | ||
531 | // only change our width/height if asked for | |
4d08943e | 532 | if ( width == wxDefaultCoord ) |
1f916a19 | 533 | { |
3ca6a5f0 BP |
534 | if ( sizeFlags & wxSIZE_AUTO_WIDTH ) |
535 | width = totWidth; | |
4438caf4 | 536 | else |
3ca6a5f0 | 537 | width = widthOld; |
1f916a19 | 538 | } |
1f916a19 | 539 | |
4d08943e | 540 | if ( height == wxDefaultCoord ) |
4438caf4 | 541 | { |
3ca6a5f0 BP |
542 | if ( sizeFlags & wxSIZE_AUTO_HEIGHT ) |
543 | height = totHeight; | |
4438caf4 | 544 | else |
3ca6a5f0 BP |
545 | height = heightOld; |
546 | } | |
4438caf4 | 547 | |
3ca6a5f0 | 548 | ::MoveWindow(GetHwnd(), xx, yy, width, height, TRUE); |
c219cecc | 549 | |
3ca6a5f0 BP |
550 | // Now position all the buttons: the current button will be put at |
551 | // wxPoint(x_offset, y_offset) and the new row/column will start at | |
552 | // startX/startY. The size of all buttons will be the same wxSize(maxWidth, | |
553 | // maxHeight) except for the buttons in the last column which should extend | |
554 | // to the right border of radiobox and thus can be wider than this. | |
c219cecc | 555 | |
3ca6a5f0 BP |
556 | // Also, remember that wxRA_SPECIFY_COLS means that we arrange buttons in |
557 | // left to right order and m_majorDim is the number of columns while | |
558 | // wxRA_SPECIFY_ROWS means that the buttons are arranged top to bottom and | |
559 | // m_majorDim is the number of rows. | |
4438caf4 | 560 | |
3ca6a5f0 BP |
561 | x_offset += cx1; |
562 | y_offset += cy1; | |
1f916a19 | 563 | |
c7fd6717 | 564 | // Add extra space under the label, if it exists. |
1a87edf2 | 565 | if (!wxControl::GetLabel().empty()) |
c7fd6717 | 566 | y_offset += cy1/2; |
3ca6a5f0 | 567 | |
4438caf4 VZ |
568 | int startX = x_offset; |
569 | int startY = y_offset; | |
1f916a19 | 570 | |
bd0a76e2 VZ |
571 | const int count = GetCount(); |
572 | for ( int i = 0; i < count; i++ ) | |
1f916a19 | 573 | { |
3ca6a5f0 BP |
574 | // the last button in the row may be wider than the other ones as the |
575 | // radiobox may be wider than the sum of the button widths (as it | |
576 | // happens, for example, when the radiobox label is very long) | |
577 | bool isLastInTheRow; | |
578 | if ( m_windowStyle & wxRA_SPECIFY_COLS ) | |
579 | { | |
580 | // item is the last in its row if it is a multiple of the number of | |
581 | // columns or if it is just the last item | |
582 | int n = i + 1; | |
bd0a76e2 | 583 | isLastInTheRow = ((n % m_majorDim) == 0) || (n == count); |
3ca6a5f0 BP |
584 | } |
585 | else // wxRA_SPECIFY_ROWS | |
4438caf4 | 586 | { |
3ca6a5f0 | 587 | // item is the last in the row if it is in the last columns |
bd0a76e2 | 588 | isLastInTheRow = i >= (count/m_majorDim)*m_majorDim; |
3ca6a5f0 BP |
589 | } |
590 | ||
591 | // is this the start of new row/column? | |
592 | if ( i && (i % m_majorDim == 0) ) | |
593 | { | |
594 | if ( m_windowStyle & wxRA_SPECIFY_ROWS ) | |
4438caf4 | 595 | { |
3ca6a5f0 | 596 | // start of new column |
4438caf4 VZ |
597 | y_offset = startY; |
598 | x_offset += maxWidth + cx1; | |
599 | } | |
3ca6a5f0 | 600 | else // start of new row |
4438caf4 VZ |
601 | { |
602 | x_offset = startX; | |
603 | y_offset += maxHeight; | |
604 | if (m_radioWidth[0]>0) | |
605 | y_offset += cy1/2; | |
606 | } | |
607 | } | |
3ca6a5f0 BP |
608 | |
609 | int widthBtn; | |
610 | if ( isLastInTheRow ) | |
4438caf4 | 611 | { |
3ca6a5f0 BP |
612 | // make the button go to the end of radio box |
613 | widthBtn = startX + width - x_offset - 2*cx1; | |
614 | if ( widthBtn < maxWidth ) | |
615 | widthBtn = maxWidth; | |
4438caf4 VZ |
616 | } |
617 | else | |
618 | { | |
3ca6a5f0 BP |
619 | // normal button, always of the same size |
620 | widthBtn = maxWidth; | |
4438caf4 | 621 | } |
1f916a19 | 622 | |
bd0a76e2 VZ |
623 | // make all buttons of the same, maximal size - like this they cover |
624 | // the radiobox entirely and the radiobox tooltips are always shown | |
625 | // (otherwise they are not when the mouse pointer is in the radiobox | |
626 | // part not belonging to any radiobutton) | |
627 | ::MoveWindow((*m_radioButtons)[i], | |
3ca6a5f0 | 628 | x_offset, y_offset, widthBtn, maxHeight, |
f048e32f | 629 | TRUE); |
4438caf4 | 630 | |
3ca6a5f0 BP |
631 | // where do we put the next button? |
632 | if ( m_windowStyle & wxRA_SPECIFY_ROWS ) | |
4438caf4 | 633 | { |
3ca6a5f0 | 634 | // below this one |
4438caf4 VZ |
635 | y_offset += maxHeight; |
636 | if (m_radioWidth[0]>0) | |
637 | y_offset += cy1/2; | |
638 | } | |
639 | else | |
3ca6a5f0 BP |
640 | { |
641 | // to the right of this one | |
642 | x_offset += widthBtn + cx1; | |
643 | } | |
1f916a19 | 644 | } |
1f916a19 JS |
645 | } |
646 | ||
c3732409 VZ |
647 | // ---------------------------------------------------------------------------- |
648 | // radio box drawing | |
649 | // ---------------------------------------------------------------------------- | |
650 | ||
651 | #ifndef __WXWINCE__ | |
652 | ||
653 | WXHRGN wxRadioBox::MSWGetRegionWithoutChildren() | |
654 | { | |
655 | RECT rc; | |
656 | ::GetWindowRect(GetHwnd(), &rc); | |
657 | HRGN hrgn = ::CreateRectRgn(rc.left, rc.top, rc.right + 1, rc.bottom + 1); | |
658 | ||
659 | const size_t count = GetCount(); | |
660 | for ( size_t i = 0; i < count; ++i ) | |
661 | { | |
662 | ::GetWindowRect((*m_radioButtons)[i], &rc); | |
663 | AutoHRGN hrgnchild(::CreateRectRgnIndirect(&rc)); | |
664 | ::CombineRgn(hrgn, hrgn, hrgnchild, RGN_DIFF); | |
665 | } | |
666 | ||
667 | return (WXHRGN)hrgn; | |
668 | } | |
669 | ||
1a784dfc VZ |
670 | WXLRESULT |
671 | wxRadioBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) | |
672 | { | |
673 | if ( nMsg == WM_PRINTCLIENT ) | |
674 | { | |
675 | // we have to process WM_PRINTCLIENT ourselves as otherwise the radio | |
676 | // buttons background would never be drawn unless we have a parent with | |
677 | // non default background | |
678 | ||
679 | // so check first if we have one | |
680 | if ( !HandlePrintClient((WXHDC)wParam) ) | |
681 | { | |
682 | // no, we don't, erase the background ourselves (don't use our own | |
683 | // colour as with static box, see comments there) | |
1fef34b9 WS |
684 | wxBrush brush(GetParent()->GetBackgroundColour()); |
685 | wxFillRect(GetHwnd(), (HDC)wParam, GetHbrushOf(brush)); | |
1a784dfc VZ |
686 | } |
687 | ||
688 | return 0; | |
689 | } | |
690 | ||
691 | return wxStaticBox::MSWWindowProc(nMsg, wParam, lParam); | |
692 | } | |
693 | ||
c3732409 VZ |
694 | #endif // __WXWINCE__ |
695 | ||
e373f51b VZ |
696 | // --------------------------------------------------------------------------- |
697 | // window proc for radio buttons | |
698 | // --------------------------------------------------------------------------- | |
699 | ||
700 | LRESULT APIENTRY _EXPORT wxRadioBtnWndProc(HWND hwnd, | |
8614c467 | 701 | UINT message, |
e373f51b VZ |
702 | WPARAM wParam, |
703 | LPARAM lParam) | |
704 | { | |
8614c467 | 705 | switch ( message ) |
e373f51b | 706 | { |
8614c467 VZ |
707 | case WM_GETDLGCODE: |
708 | // we must tell IsDialogMessage()/our kbd processing code that we | |
709 | // want to process arrows ourselves because neither of them is | |
710 | // smart enough to handle arrows properly for us | |
711 | { | |
8ad6ad99 | 712 | long lDlgCode = ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd, |
8614c467 | 713 | message, wParam, lParam); |
8ad6ad99 | 714 | |
8614c467 VZ |
715 | return lDlgCode | DLGC_WANTARROWS; |
716 | } | |
e373f51b | 717 | |
8614c467 VZ |
718 | #if wxUSE_TOOLTIPS |
719 | case WM_NOTIFY: | |
f048e32f | 720 | { |
8614c467 | 721 | NMHDR* hdr = (NMHDR *)lParam; |
2b5f62a0 | 722 | if ( hdr->code == TTN_NEEDTEXT ) |
e373f51b | 723 | { |
975b6bcf VZ |
724 | wxRadioBox * |
725 | radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd); | |
e373f51b | 726 | |
8614c467 VZ |
727 | wxCHECK_MSG( radiobox, 0, |
728 | wxT("radio button without radio box?") ); | |
729 | ||
730 | wxToolTip *tooltip = radiobox->GetToolTip(); | |
731 | if ( tooltip ) | |
732 | { | |
733 | TOOLTIPTEXT *ttt = (TOOLTIPTEXT *)lParam; | |
734 | ttt->lpszText = (wxChar *)tooltip->GetTip().c_str(); | |
735 | } | |
736 | ||
737 | // processed | |
738 | return 0; | |
e373f51b | 739 | } |
f048e32f | 740 | } |
8614c467 | 741 | break; |
f048e32f | 742 | #endif // wxUSE_TOOLTIPS |
f048e32f | 743 | |
8614c467 | 744 | case WM_KEYDOWN: |
9a5ccab4 | 745 | { |
975b6bcf | 746 | wxRadioBox *radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd); |
f048e32f | 747 | |
8614c467 | 748 | wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") ); |
f048e32f | 749 | |
4d08943e | 750 | bool processed = true; |
f048e32f | 751 | |
e421922f | 752 | wxDirection dir; |
8614c467 VZ |
753 | switch ( wParam ) |
754 | { | |
755 | case VK_UP: | |
1e6feb95 | 756 | dir = wxUP; |
8614c467 | 757 | break; |
f048e32f | 758 | |
8614c467 | 759 | case VK_LEFT: |
1e6feb95 | 760 | dir = wxLEFT; |
8614c467 | 761 | break; |
f048e32f | 762 | |
8614c467 | 763 | case VK_DOWN: |
1e6feb95 | 764 | dir = wxDOWN; |
8614c467 | 765 | break; |
f048e32f | 766 | |
8614c467 | 767 | case VK_RIGHT: |
1e6feb95 | 768 | dir = wxRIGHT; |
8614c467 VZ |
769 | break; |
770 | ||
771 | default: | |
4d08943e | 772 | processed = false; |
1e6feb95 VZ |
773 | |
774 | // just to suppress the compiler warning | |
775 | dir = wxALL; | |
8614c467 VZ |
776 | } |
777 | ||
778 | if ( processed ) | |
f048e32f | 779 | { |
1e6feb95 VZ |
780 | int selOld = radiobox->GetSelection(); |
781 | int selNew = radiobox->GetNextItem | |
782 | ( | |
783 | selOld, | |
784 | dir, | |
785 | radiobox->GetWindowStyle() | |
786 | ); | |
3ca6a5f0 | 787 | |
8614c467 VZ |
788 | if ( selNew != selOld ) |
789 | { | |
790 | radiobox->SetSelection(selNew); | |
974931fe | 791 | radiobox->SetFocus(); |
8614c467 VZ |
792 | |
793 | // emulate the button click | |
794 | radiobox->SendNotificationEvent(); | |
9a5ccab4 | 795 | |
8614c467 VZ |
796 | return 0; |
797 | } | |
f048e32f | 798 | } |
9a5ccab4 | 799 | } |
21687069 VZ |
800 | break; |
801 | ||
1dab049c VZ |
802 | case WM_SETFOCUS: |
803 | case WM_KILLFOCUS: | |
804 | { | |
975b6bcf | 805 | wxRadioBox *radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd); |
1dab049c VZ |
806 | |
807 | wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") ); | |
808 | ||
809 | // if we don't do this, no focus events are generated for the | |
810 | // radiobox and, besides, we need to notify the parent about | |
811 | // the focus change, otherwise the focus handling logic in | |
812 | // wxControlContainer doesn't work | |
813 | if ( message == WM_SETFOCUS ) | |
814 | radiobox->HandleSetFocus((WXHWND)wParam); | |
815 | else | |
816 | radiobox->HandleKillFocus((WXHWND)wParam); | |
817 | } | |
818 | break; | |
819 | ||
bd0a76e2 | 820 | #ifndef __WXWINCE__ |
fac93396 | 821 | case WM_HELP: |
e421922f | 822 | { |
975b6bcf | 823 | wxRadioBox *radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd); |
fac93396 | 824 | |
e421922f | 825 | wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") ); |
fac93396 | 826 | |
bd0a76e2 VZ |
827 | bool processed = false; |
828 | ||
829 | wxEvtHandler * const handler = radiobox->GetEventHandler(); | |
fac93396 | 830 | |
e421922f | 831 | HELPINFO* info = (HELPINFO*) lParam; |
bd0a76e2 | 832 | if ( info->iContextType == HELPINFO_WINDOW ) |
fac93396 | 833 | { |
bd0a76e2 VZ |
834 | for ( wxWindow* subjectOfHelp = radiobox; |
835 | subjectOfHelp; | |
836 | subjectOfHelp = subjectOfHelp->GetParent() ) | |
e421922f | 837 | { |
bd0a76e2 VZ |
838 | wxHelpEvent helpEvent(wxEVT_HELP, |
839 | subjectOfHelp->GetId(), | |
840 | wxPoint(info->MousePos.x, | |
841 | info->MousePos.y)); | |
e421922f | 842 | helpEvent.SetEventObject(radiobox); |
bd0a76e2 VZ |
843 | if ( handler->ProcessEvent(helpEvent) ) |
844 | { | |
845 | processed = true; | |
846 | break; | |
847 | } | |
e421922f | 848 | } |
fac93396 | 849 | } |
e421922f VZ |
850 | else if (info->iContextType == HELPINFO_MENUITEM) |
851 | { | |
bd0a76e2 | 852 | wxHelpEvent helpEvent(wxEVT_HELP, info->iCtrlId); |
e421922f | 853 | helpEvent.SetEventObject(radiobox); |
bd0a76e2 | 854 | processed = handler->ProcessEvent(helpEvent); |
e421922f | 855 | } |
bd0a76e2 VZ |
856 | |
857 | if ( processed ) | |
e421922f | 858 | return 0; |
e421922f | 859 | } |
c3732409 | 860 | break; |
bd0a76e2 | 861 | #endif // !__WXWINCE__ |
e373f51b VZ |
862 | } |
863 | ||
8ad6ad99 | 864 | return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd, message, wParam, lParam); |
e373f51b | 865 | } |
42e69d6b | 866 | |
1e6feb95 | 867 | #endif // wxUSE_RADIOBOX |
bd0a76e2 | 868 |