]> git.saurik.com Git - wxWidgets.git/blame - src/common/containr.cpp
don't assert if neither wxFD_OPEN nor wxFD_SAVE is given; wxFD_OPEN is the default
[wxWidgets.git] / src / common / containr.cpp
CommitLineData
456bc6d9
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/common/containr.cpp
3// Purpose: implementation of wxControlContainer
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 06.08.01
7// RCS-ID: $Id$
8// Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
65571936 9// License: wxWindows licence
456bc6d9
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
456bc6d9
VZ
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
27#ifndef WX_PRECOMP
6285be72
VZ
28 #include "wx/log.h"
29 #include "wx/event.h"
30 #include "wx/window.h"
851dee09 31 #include "wx/scrolbar.h"
456bc6d9
VZ
32#endif //WX_PRECOMP
33
34#include "wx/containr.h"
35
c932709d
RR
36#ifdef __WXMSW__
37 #include "wx/radiobut.h"
38#endif
39
9f542367
VZ
40// trace mask for focus messages
41#define TRACE_FOCUS _T("focus")
42
456bc6d9
VZ
43// ============================================================================
44// implementation
45// ============================================================================
46
47wxControlContainer::wxControlContainer(wxWindow *winParent)
48{
49 m_winParent = winParent;
50
51 m_winLastFocused =
036da5e3 52 m_winTmpDefault =
456bc6d9 53 m_winDefault = NULL;
b33f7651 54 m_inSetFocus = false;
456bc6d9
VZ
55}
56
3251b834
VZ
57bool wxControlContainer::AcceptsFocus() const
58{
a7407834 59 // if we're not shown or disabled, we can't accept focus
3251b834
VZ
60 if ( m_winParent->IsShown() && m_winParent->IsEnabled() )
61 {
a7407834
VZ
62 // otherwise we can accept focus either if we have no children at all
63 // (in this case we're probably not used as a container) or only when
64 // at least one child will accept focus
222ed1d6 65 wxWindowList::compatibility_iterator node = m_winParent->GetChildren().GetFirst();
a7407834 66 if ( !node )
c9d59ee7 67 return true;
a7407834 68
2f64c3bb
SC
69#ifdef __WXMAC__
70 // wxMac has eventually the two scrollbars as children, they don't count
71 // as real children in the algorithm mentioned above
72 bool hasRealChildren = false ;
73#endif
c9d59ee7 74
3251b834
VZ
75 while ( node )
76 {
77 wxWindow *child = node->GetData();
78
79 if ( child->AcceptsFocus() )
80 {
c9d59ee7 81 return true;
3251b834
VZ
82 }
83
2f64c3bb
SC
84#ifdef __WXMAC__
85 wxScrollBar *sb = wxDynamicCast( child , wxScrollBar ) ;
86 if ( sb == NULL || !m_winParent->MacIsWindowScrollbar( sb ) )
87 hasRealChildren = true ;
88#endif
3251b834
VZ
89 node = node->GetNext();
90 }
c9d59ee7 91
2f64c3bb
SC
92#ifdef __WXMAC__
93 if ( !hasRealChildren )
c9d59ee7 94 return true ;
2f64c3bb 95#endif
3251b834
VZ
96 }
97
c9d59ee7 98 return false;
3251b834
VZ
99}
100
456bc6d9
VZ
101void wxControlContainer::SetLastFocus(wxWindow *win)
102{
6aeb6f2a
VZ
103 // the panel itself should never get the focus at all but if it does happen
104 // temporarily (as it seems to do under wxGTK), at the very least don't
105 // forget our previous m_winLastFocused
c25b5d1f 106 if ( win != m_winParent )
456bc6d9 107 {
c25b5d1f
VZ
108 // if we're setting the focus
109 if ( win )
83c865f5 110 {
c25b5d1f
VZ
111 // find the last _immediate_ child which got focus
112 wxWindow *winParent = win;
113 while ( winParent != m_winParent )
114 {
115 win = winParent;
116 winParent = win->GetParent();
83c865f5 117
c25b5d1f
VZ
118 // Yes, this can happen, though in a totally pathological case.
119 // like when detaching a menubar from a frame with a child
120 // which has pushed itself as an event handler for the menubar.
121 // (under wxGTK)
6f8239de 122
c25b5d1f
VZ
123 wxASSERT_MSG( winParent,
124 _T("Setting last focus for a window that is not our child?") );
125 }
6f8239de 126 }
456bc6d9 127
c25b5d1f 128 m_winLastFocused = win;
6aeb6f2a 129
c25b5d1f
VZ
130 if ( win )
131 {
9f542367 132 wxLogTrace(TRACE_FOCUS, _T("Set last focus to %s(%s)"),
c25b5d1f
VZ
133 win->GetClassInfo()->GetClassName(),
134 win->GetLabel().c_str());
135 }
136 else
137 {
9f542367 138 wxLogTrace(TRACE_FOCUS, _T("No more last focus"));
c25b5d1f 139 }
6aeb6f2a 140 }
c25b5d1f
VZ
141
142 // propagate the last focus upwards so that our parent can set focus back
143 // to us if it loses it now and regains later
144 wxWindow *parent = m_winParent->GetParent();
145 if ( parent )
6aeb6f2a 146 {
c25b5d1f
VZ
147 wxChildFocusEvent eventFocus(m_winParent);
148 parent->GetEventHandler()->ProcessEvent(eventFocus);
6aeb6f2a 149 }
456bc6d9
VZ
150}
151
7ff1b620 152// --------------------------------------------------------------------
b49f58fe 153// The following four functions are used to find other radio buttons
7ff1b620
VZ
154// within the same group. Used by wxSetFocusToChild on wxMSW
155// --------------------------------------------------------------------
156
157#ifdef __WXMSW__
158
159wxRadioButton* wxGetPreviousButtonInGroup(wxRadioButton *btn)
160{
161 if ( btn->HasFlag(wxRB_GROUP) || btn->HasFlag(wxRB_SINGLE) )
162 return NULL;
163
164 const wxWindowList& siblings = btn->GetParent()->GetChildren();
165 wxWindowList::compatibility_iterator nodeThis = siblings.Find(btn);
166 wxCHECK_MSG( nodeThis, NULL, _T("radio button not a child of its parent?") );
167
168 // Iterate over all previous siblings until we find the next radio button
169 wxWindowList::compatibility_iterator nodeBefore = nodeThis->GetPrevious();
170 wxRadioButton *prevBtn = 0;
171 while (nodeBefore)
172 {
173 prevBtn = wxDynamicCast(nodeBefore->GetData(), wxRadioButton);
174 if (prevBtn)
175 break;
176
177 nodeBefore = nodeBefore->GetPrevious();
178 }
b49f58fe 179
7ff1b620
VZ
180 if (!prevBtn || prevBtn->HasFlag(wxRB_SINGLE))
181 {
182 // no more buttons in group
183 return NULL;
184 }
3d3afaec
JS
185
186 return prevBtn;
7ff1b620
VZ
187}
188
189wxRadioButton* wxGetNextButtonInGroup(wxRadioButton *btn)
190{
191 if (btn->HasFlag(wxRB_SINGLE))
192 return NULL;
193
194 const wxWindowList& siblings = btn->GetParent()->GetChildren();
195 wxWindowList::compatibility_iterator nodeThis = siblings.Find(btn);
196 wxCHECK_MSG( nodeThis, NULL, _T("radio button not a child of its parent?") );
197
198 // Iterate over all previous siblings until we find the next radio button
199 wxWindowList::compatibility_iterator nodeNext = nodeThis->GetNext();
200 wxRadioButton *nextBtn = 0;
201 while (nodeNext)
202 {
203 nextBtn = wxDynamicCast(nodeNext->GetData(), wxRadioButton);
204 if (nextBtn)
205 break;
206
207 nodeNext = nodeNext->GetNext();
208 }
209
210 if ( !nextBtn || nextBtn->HasFlag(wxRB_GROUP) || nextBtn->HasFlag(wxRB_SINGLE) )
211 {
212 // no more buttons or the first button of the next group
213 return NULL;
214 }
3d3afaec
JS
215
216 return nextBtn;
7ff1b620
VZ
217}
218
219wxRadioButton* wxGetFirstButtonInGroup(wxRadioButton *btn)
220{
221 while (true)
222 {
223 wxRadioButton* prevBtn = wxGetPreviousButtonInGroup(btn);
224 if (!prevBtn)
225 return btn;
b49f58fe 226
7ff1b620
VZ
227 btn = prevBtn;
228 }
229}
230
3d3afaec
JS
231wxRadioButton* wxGetLastButtonInGroup(wxRadioButton *btn)
232{
233 while (true)
234 {
235 wxRadioButton* nextBtn = wxGetNextButtonInGroup(btn);
236 if (!nextBtn)
237 return btn;
238
239 btn = nextBtn;
240 }
241}
242
7ff1b620
VZ
243wxRadioButton* wxGetSelectedButtonInGroup(wxRadioButton *btn)
244{
245 // Find currently selected button
246 if (btn->GetValue())
247 return btn;
248
249 if (btn->HasFlag(wxRB_SINGLE))
250 return NULL;
251
252 wxRadioButton *selBtn;
253
254 // First check all previous buttons
255 for (selBtn = wxGetPreviousButtonInGroup(btn); selBtn; selBtn = wxGetPreviousButtonInGroup(selBtn))
256 if (selBtn->GetValue())
257 return selBtn;
258
259 // Now all following buttons
260 for (selBtn = wxGetNextButtonInGroup(btn); selBtn; selBtn = wxGetNextButtonInGroup(selBtn))
261 if (selBtn->GetValue())
262 return selBtn;
263
264 return NULL;
265}
266
b49f58fe 267#endif // __WXMSW__
7ff1b620 268
456bc6d9
VZ
269// ----------------------------------------------------------------------------
270// Keyboard handling - this is the place where the TAB traversal logic is
271// implemented. As this code is common to all ports, this ensures consistent
272// behaviour even if we don't specify how exactly the wxNavigationKeyEvent are
273// generated and this is done in platform specific code which also ensures that
274// we can follow the given platform standards.
275// ----------------------------------------------------------------------------
276
277void wxControlContainer::HandleOnNavigationKey( wxNavigationKeyEvent& event )
278{
279 wxWindow *parent = m_winParent->GetParent();
280
281 // the event is propagated downwards if the event emitter was our parent
282 bool goingDown = event.GetEventObject() == parent;
283
284 const wxWindowList& children = m_winParent->GetChildren();
285
286 // there is not much to do if we don't have children and we're not
287 // interested in "notebook page change" events here
288 if ( !children.GetCount() || event.IsWindowChange() )
289 {
290 // let the parent process it unless it already comes from our parent
291 // of we don't have any
292 if ( goingDown ||
293 !parent || !parent->GetEventHandler()->ProcessEvent(event) )
294 {
295 event.Skip();
296 }
297
298 return;
299 }
300
301 // where are we going?
edc0a395 302 const bool forward = event.GetDirection();
456bc6d9
VZ
303
304 // the node of the children list from which we should start looking for the
305 // next acceptable child
222ed1d6 306 wxWindowList::compatibility_iterator node, start_node;
456bc6d9
VZ
307
308 // we should start from the first/last control and not from the one which
309 // had focus the last time if we're propagating the event downwards because
310 // for our parent we look like a single control
311 if ( goingDown )
312 {
313 // just to be sure it's not used (normally this is not necessary, but
314 // doesn't hurt neither)
315 m_winLastFocused = (wxWindow *)NULL;
316
317 // start from first or last depending on where we're going
318 node = forward ? children.GetFirst() : children.GetLast();
456bc6d9 319 }
edc0a395 320 else // going up
456bc6d9
VZ
321 {
322 // try to find the child which has the focus currently
323
324 // the event emitter might have done this for us
325 wxWindow *winFocus = event.GetCurrentFocus();
326
327 // but if not, we might know where the focus was ourselves
328 if (!winFocus)
329 winFocus = m_winLastFocused;
330
331 // if still no luck, do it the hard way
332 if (!winFocus)
333 winFocus = wxWindow::FindFocus();
334
335 if ( winFocus )
336 {
7ff1b620 337#ifdef __WXMSW__
b49f58fe 338 // If we are in a radio button group, start from the first item in the
7ff1b620
VZ
339 // group
340 if ( event.IsFromTab() && wxIsKindOf(winFocus, wxRadioButton ) )
341 winFocus = wxGetFirstButtonInGroup((wxRadioButton*)winFocus);
342#endif
456bc6d9
VZ
343 // ok, we found the focus - now is it our child?
344 start_node = children.Find( winFocus );
345 }
456bc6d9
VZ
346
347 if ( !start_node && m_winLastFocused )
348 {
349 // window which has focus isn't our child, fall back to the one
350 // which had the focus the last time
351 start_node = children.Find( m_winLastFocused );
352 }
353
354 // if we still didn't find anything, we should start with the first one
355 if ( !start_node )
356 {
357 start_node = children.GetFirst();
358 }
359
360 // and the first child which we can try setting focus to is the next or
361 // the previous one
362 node = forward ? start_node->GetNext() : start_node->GetPrevious();
363 }
364
365 // we want to cycle over all elements passing by NULL
edc0a395 366 for ( ;; )
456bc6d9 367 {
edc0a395 368 // don't go into infinite loop
aa78d22e 369 if ( start_node && node && node == start_node )
edc0a395
VZ
370 break;
371
456bc6d9
VZ
372 // Have we come to the last or first item on the panel?
373 if ( !node )
374 {
e547f7a7
VZ
375 if ( !start_node )
376 {
377 // exit now as otherwise we'd loop forever
378 break;
379 }
380
456bc6d9
VZ
381 if ( !goingDown )
382 {
edc0a395 383 // Check if our (maybe grand) parent is another panel: if this
456bc6d9
VZ
384 // is the case, they will know what to do with this navigation
385 // key and so give them the chance to process it instead of
386 // looping inside this panel (normally, the focus will go to
387 // the next/previous item after this panel in the parent
388 // panel).
389 wxWindow *focussed_child_of_parent = m_winParent;
390 while ( parent )
391 {
392 // we don't want to tab into a different dialog or frame
393 if ( focussed_child_of_parent->IsTopLevel() )
394 break;
395
396 event.SetCurrentFocus( focussed_child_of_parent );
397 if ( parent->GetEventHandler()->ProcessEvent( event ) )
398 return;
399
400 focussed_child_of_parent = parent;
401
402 parent = parent->GetParent();
403 }
404 }
405 //else: as the focus came from our parent, we definitely don't want
406 // to send it back to it!
407
408 // no, we are not inside another panel so process this ourself
409 node = forward ? children.GetFirst() : children.GetLast();
410
411 continue;
412 }
413
414 wxWindow *child = node->GetData();
415
7ff1b620 416#ifdef __WXMSW__
3d3afaec 417 if ( event.IsFromTab() )
7ff1b620 418 {
3d3afaec 419 if ( wxIsKindOf(child, wxRadioButton) )
7ff1b620 420 {
3d3afaec
JS
421 // only radio buttons with either wxRB_GROUP or wxRB_SINGLE
422 // can be tabbed to
423 if ( child->HasFlag(wxRB_GROUP) )
7ff1b620 424 {
3d3afaec
JS
425 // need to tab into the active button within a group
426 wxRadioButton *rb = wxGetSelectedButtonInGroup((wxRadioButton*)child);
427 if ( rb )
428 child = rb;
429 }
430 else if ( !child->HasFlag(wxRB_SINGLE) )
431 {
432 node = forward ? node->GetNext() : node->GetPrevious();
433 continue;
7ff1b620
VZ
434 }
435 }
436 }
3d3afaec
JS
437 else if ( m_winLastFocused &&
438 wxIsKindOf(m_winLastFocused, wxRadioButton) &&
439 !m_winLastFocused->HasFlag(wxRB_SINGLE) )
7ff1b620 440 {
3d3afaec
JS
441 // cursor keys don't navigate out of a radio button group so
442 // find the correct radio button to focus
443 if ( forward )
7ff1b620 444 {
3d3afaec
JS
445 child = wxGetNextButtonInGroup((wxRadioButton*)m_winLastFocused);
446 if ( !child )
447 {
448 // no next button in group, set it to the first button
449 child = wxGetFirstButtonInGroup((wxRadioButton*)m_winLastFocused);
450 }
451 }
452 else
453 {
454 child = wxGetPreviousButtonInGroup((wxRadioButton*)m_winLastFocused);
455 if ( !child )
456 {
457 // no previous button in group, set it to the last button
458 child = wxGetLastButtonInGroup((wxRadioButton*)m_winLastFocused);
459 }
460 }
461
462 if ( child == m_winLastFocused )
463 {
464 // must be a group consisting of only one button therefore
465 // no need to send a navigation event
466 event.Skip(false);
467 return;
7ff1b620
VZ
468 }
469 }
3d3afaec 470#endif // __WXMSW__
c932709d 471
3d3afaec 472 if ( child->AcceptsFocusFromKeyboard() )
456bc6d9
VZ
473 {
474 // if we're setting the focus to a child panel we should prevent it
475 // from giving it to the child which had the focus the last time
476 // and instead give it to the first/last child depending from which
477 // direction we're coming
478 event.SetEventObject(m_winParent);
944e8709 479
aef35d0e
VZ
480 // disable propagation for this call as otherwise the event might
481 // bounce back to us.
482 wxPropagationDisabler disableProp(event);
456bc6d9
VZ
483 if ( !child->GetEventHandler()->ProcessEvent(event) )
484 {
2b5f62a0
VZ
485 // set it first in case SetFocusFromKbd() results in focus
486 // change too
487 m_winLastFocused = child;
488
456bc6d9 489 // everything is simple: just give focus to it
5463c0a4 490 child->SetFocusFromKbd();
456bc6d9
VZ
491 }
492 //else: the child manages its focus itself
493
c9d59ee7 494 event.Skip( false );
456bc6d9
VZ
495
496 return;
497 }
498
499 node = forward ? node->GetNext() : node->GetPrevious();
500 }
501
502 // we cycled through all of our children and none of them wanted to accept
503 // focus
504 event.Skip();
505}
506
507void wxControlContainer::HandleOnWindowDestroy(wxWindowBase *child)
508{
509 if ( child == m_winLastFocused )
510 m_winLastFocused = NULL;
511
512 if ( child == m_winDefault )
513 m_winDefault = NULL;
036da5e3
VZ
514
515 if ( child == m_winTmpDefault )
516 m_winTmpDefault = NULL;
456bc6d9
VZ
517}
518
519// ----------------------------------------------------------------------------
520// focus handling
521// ----------------------------------------------------------------------------
522
24a7a198 523bool wxControlContainer::DoSetFocus()
456bc6d9 524{
9f542367
VZ
525 wxLogTrace(TRACE_FOCUS, _T("SetFocus on wxPanel 0x%p."),
526 m_winParent->GetHandle());
456bc6d9 527
b33f7651
JS
528 if (m_inSetFocus)
529 return true;
c9d59ee7 530
2b5f62a0
VZ
531 // when the panel gets the focus we move the focus to either the last
532 // window that had the focus or the first one that can get it unless the
533 // focus had been already set to some other child
534
0dcdeee9
VZ
535 wxWindow *win = wxWindow::FindFocus();
536 while ( win )
537 {
538 if ( win == m_winParent )
2b5f62a0
VZ
539 {
540 // our child already has focus, don't take it away from it
1c9e321b 541 return true;
2b5f62a0 542 }
0dcdeee9
VZ
543
544 if ( win->IsTopLevel() )
545 {
546 // don't look beyond the first top level parent - useless and
547 // unnecessary
548 break;
549 }
550
551 win = win->GetParent();
552 }
c9d59ee7 553
2e07bdda
VS
554 // protect against infinite recursion:
555 m_inSetFocus = true;
0dcdeee9 556
b33f7651
JS
557 bool ret = SetFocusToChild();
558
559 m_inSetFocus = false;
560
561 return ret;
456bc6d9
VZ
562}
563
564void wxControlContainer::HandleOnFocus(wxFocusEvent& event)
565{
9f542367
VZ
566 wxLogTrace(TRACE_FOCUS, _T("OnFocus on wxPanel 0x%p, name: %s"),
567 m_winParent->GetHandle(),
456bc6d9
VZ
568 m_winParent->GetName().c_str() );
569
2b5f62a0 570 DoSetFocus();
456bc6d9
VZ
571
572 event.Skip();
573}
574
575bool wxControlContainer::SetFocusToChild()
576{
577 return wxSetFocusToChild(m_winParent, &m_winLastFocused);
578}
579
580// ----------------------------------------------------------------------------
581// SetFocusToChild(): this function is used by wxPanel but also by wxFrame in
582// wxMSW, this is why it is outside of wxControlContainer class
583// ----------------------------------------------------------------------------
584
585bool wxSetFocusToChild(wxWindow *win, wxWindow **childLastFocused)
586{
c9d59ee7
WS
587 wxCHECK_MSG( win, false, _T("wxSetFocusToChild(): invalid window") );
588 wxCHECK_MSG( childLastFocused, false,
c25b5d1f 589 _T("wxSetFocusToChild(): NULL child poonter") );
456bc6d9
VZ
590
591 if ( *childLastFocused )
592 {
c25b5d1f
VZ
593 // It might happen that the window got reparented
594 if ( (*childLastFocused)->GetParent() == win )
456bc6d9 595 {
9f542367
VZ
596 wxLogTrace(TRACE_FOCUS,
597 _T("SetFocusToChild() => last child (0x%p)."),
598 (*childLastFocused)->GetHandle());
456bc6d9 599
c25b5d1f
VZ
600 // not SetFocusFromKbd(): we're restoring focus back to the old
601 // window and not setting it as the result of a kbd action
602 (*childLastFocused)->SetFocus();
c9d59ee7 603 return true;
456bc6d9
VZ
604 }
605 else
606 {
607 // it doesn't count as such any more
608 *childLastFocused = (wxWindow *)NULL;
609 }
610 }
611
612 // set the focus to the first child who wants it
222ed1d6 613 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
456bc6d9
VZ
614 while ( node )
615 {
616 wxWindow *child = node->GetData();
617
618 if ( child->AcceptsFocusFromKeyboard() && !child->IsTopLevel() )
619 {
7ff1b620 620#ifdef __WXMSW__
b49f58fe 621 // If a radiobutton is the first focusable child, search for the
7ff1b620
VZ
622 // selected radiobutton in the same group
623 wxRadioButton* btn = wxDynamicCast(child, wxRadioButton);
624 if (btn)
625 {
626 wxRadioButton* selected = wxGetSelectedButtonInGroup(btn);
627 if (selected)
628 child = selected;
629 }
630#endif
631
9f542367
VZ
632 wxLogTrace(TRACE_FOCUS,
633 _T("SetFocusToChild() => first child (0x%p)."),
634 child->GetHandle());
456bc6d9 635
a7407834 636 *childLastFocused = child;
5463c0a4 637 child->SetFocusFromKbd();
c9d59ee7 638 return true;
456bc6d9
VZ
639 }
640
641 node = node->GetNext();
642 }
643
c9d59ee7 644 return false;
456bc6d9 645}