2011-01-03 11:24:13 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: wx/compositewin.h
|
|
|
|
// Purpose: wxCompositeWindow<> declaration
|
|
|
|
// Author: Vadim Zeitlin
|
|
|
|
// Created: 2011-01-02
|
|
|
|
// Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef _WX_COMPOSITEWIN_H_
|
|
|
|
#define _WX_COMPOSITEWIN_H_
|
|
|
|
|
|
|
|
#include "wx/window.h"
|
2011-10-19 16:20:01 +00:00
|
|
|
#include "wx/containr.h"
|
2011-01-03 11:24:13 +00:00
|
|
|
|
2011-10-02 11:28:54 +00:00
|
|
|
class WXDLLIMPEXP_FWD_CORE wxToolTip;
|
|
|
|
|
2011-01-03 11:24:13 +00:00
|
|
|
// NB: This is an experimental and, as for now, undocumented class used only by
|
|
|
|
// wxWidgets itself internally. Don't use it in your code until its API is
|
|
|
|
// officially stabilized unless you are ready to change it with the next
|
|
|
|
// wxWidgets release.
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// wxCompositeWindow is a helper for implementing composite windows: to define
|
|
|
|
// a class using subwindows, simply inherit from it specialized with the real
|
|
|
|
// base class name and implement GetCompositeWindowParts() pure virtual method.
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2017-12-24 20:43:57 +00:00
|
|
|
// This is the base class of wxCompositeWindow which takes care of propagating
|
|
|
|
// colours, fonts etc changes to all the children, but doesn't bother with
|
|
|
|
// handling their events or focus. There should be rarely any need to use it
|
|
|
|
// rather than the full wxCompositeWindow.
|
|
|
|
|
2011-01-03 11:24:13 +00:00
|
|
|
// The template parameter W must be a wxWindow-derived class.
|
|
|
|
template <class W>
|
2017-12-24 20:43:57 +00:00
|
|
|
class wxCompositeWindowSettersOnly : public W
|
2011-01-03 11:24:13 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef W BaseWindowClass;
|
|
|
|
|
|
|
|
// Override all wxWindow methods which must be forwarded to the composite
|
|
|
|
// window parts.
|
|
|
|
|
|
|
|
// Attribute setters group.
|
|
|
|
//
|
|
|
|
// NB: Unfortunately we can't factor out the call for the setter itself
|
|
|
|
// into DoSetForAllParts() because we can't call the function passed to
|
|
|
|
// it non-virtually and we need to do this to avoid infinite recursion,
|
|
|
|
// so we work around this by calling the method of this object itself
|
|
|
|
// manually in each function.
|
2016-09-25 20:21:28 +00:00
|
|
|
virtual bool SetForegroundColour(const wxColour& colour) wxOVERRIDE
|
2011-01-03 11:24:13 +00:00
|
|
|
{
|
|
|
|
if ( !BaseWindowClass::SetForegroundColour(colour) )
|
|
|
|
return false;
|
|
|
|
|
2011-10-02 11:28:54 +00:00
|
|
|
SetForAllParts(&wxWindowBase::SetForegroundColour, colour);
|
2011-01-03 11:24:13 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-25 20:21:28 +00:00
|
|
|
virtual bool SetBackgroundColour(const wxColour& colour) wxOVERRIDE
|
2011-01-03 11:24:13 +00:00
|
|
|
{
|
|
|
|
if ( !BaseWindowClass::SetBackgroundColour(colour) )
|
|
|
|
return false;
|
|
|
|
|
2011-10-02 11:28:54 +00:00
|
|
|
SetForAllParts(&wxWindowBase::SetBackgroundColour, colour);
|
2011-01-03 11:24:13 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-25 20:21:28 +00:00
|
|
|
virtual bool SetFont(const wxFont& font) wxOVERRIDE
|
2011-01-03 11:24:13 +00:00
|
|
|
{
|
|
|
|
if ( !BaseWindowClass::SetFont(font) )
|
|
|
|
return false;
|
|
|
|
|
2011-10-02 11:28:54 +00:00
|
|
|
SetForAllParts(&wxWindowBase::SetFont, font);
|
2011-01-03 11:24:13 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-25 20:21:28 +00:00
|
|
|
virtual bool SetCursor(const wxCursor& cursor) wxOVERRIDE
|
2011-01-03 11:24:13 +00:00
|
|
|
{
|
|
|
|
if ( !BaseWindowClass::SetCursor(cursor) )
|
|
|
|
return false;
|
|
|
|
|
2011-10-02 11:28:54 +00:00
|
|
|
SetForAllParts(&wxWindowBase::SetCursor, cursor);
|
2011-01-03 11:24:13 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-25 20:21:28 +00:00
|
|
|
virtual void SetLayoutDirection(wxLayoutDirection dir) wxOVERRIDE
|
2014-09-10 16:51:51 +00:00
|
|
|
{
|
|
|
|
BaseWindowClass::SetLayoutDirection(dir);
|
|
|
|
|
|
|
|
SetForAllParts(&wxWindowBase::SetLayoutDirection, dir);
|
2014-09-21 01:41:27 +00:00
|
|
|
|
|
|
|
// The child layout almost invariably depends on the layout direction,
|
|
|
|
// so redo it when it changes.
|
2014-09-27 16:12:29 +00:00
|
|
|
//
|
|
|
|
// However avoid doing it when we're called from wxWindow::Create() in
|
|
|
|
// wxGTK as the derived window is not fully created yet and calling its
|
|
|
|
// SetSize() may be unexpected. This does mean that any future calls to
|
|
|
|
// SetLayoutDirection(wxLayout_Default) wouldn't result in a re-layout
|
|
|
|
// neither, but then we're not supposed to be called with it at all.
|
|
|
|
if ( dir != wxLayout_Default )
|
2017-12-21 12:46:32 +00:00
|
|
|
this->SetSize(-1, -1, -1, -1, wxSIZE_FORCE);
|
2014-09-10 16:51:51 +00:00
|
|
|
}
|
|
|
|
|
2011-10-02 11:28:54 +00:00
|
|
|
#if wxUSE_TOOLTIPS
|
2016-09-25 20:21:28 +00:00
|
|
|
virtual void DoSetToolTipText(const wxString &tip) wxOVERRIDE
|
2014-12-05 22:19:10 +00:00
|
|
|
{
|
|
|
|
BaseWindowClass::DoSetToolTipText(tip);
|
|
|
|
|
|
|
|
// Use a variable to disambiguate between SetToolTip() overloads.
|
|
|
|
void (wxWindowBase::*func)(const wxString&) = &wxWindowBase::SetToolTip;
|
|
|
|
|
|
|
|
SetForAllParts(func, tip);
|
|
|
|
}
|
|
|
|
|
2016-09-25 20:21:28 +00:00
|
|
|
virtual void DoSetToolTip(wxToolTip *tip) wxOVERRIDE
|
2011-10-02 11:28:54 +00:00
|
|
|
{
|
|
|
|
BaseWindowClass::DoSetToolTip(tip);
|
|
|
|
|
|
|
|
SetForAllParts(&wxWindowBase::CopyToolTip, tip);
|
|
|
|
}
|
|
|
|
#endif // wxUSE_TOOLTIPS
|
|
|
|
|
2017-12-24 20:43:57 +00:00
|
|
|
protected:
|
|
|
|
// Trivial but necessary default ctor.
|
|
|
|
wxCompositeWindowSettersOnly()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Must be implemented by the derived class to return all children to which
|
|
|
|
// the public methods we override should forward to.
|
|
|
|
virtual wxWindowList GetCompositeWindowParts() const = 0;
|
|
|
|
|
|
|
|
template <class T, class TArg, class R>
|
|
|
|
void SetForAllParts(R (wxWindowBase::*func)(TArg), T arg)
|
|
|
|
{
|
|
|
|
// Simply call the setters for all parts of this composite window.
|
|
|
|
const wxWindowList parts = GetCompositeWindowParts();
|
|
|
|
for ( wxWindowList::const_iterator i = parts.begin();
|
|
|
|
i != parts.end();
|
|
|
|
++i )
|
|
|
|
{
|
|
|
|
wxWindow * const child = *i;
|
|
|
|
|
|
|
|
// Allow NULL elements in the list, this makes the code of derived
|
|
|
|
// composite controls which may have optionally shown children
|
|
|
|
// simpler and it doesn't cost us much here.
|
|
|
|
if ( child )
|
|
|
|
(child->*func)(arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wxDECLARE_NO_COPY_TEMPLATE_CLASS(wxCompositeWindowSettersOnly, W);
|
|
|
|
};
|
|
|
|
|
|
|
|
// The real wxCompositeWindow itself, inheriting all the setters defined above.
|
|
|
|
template <class W>
|
|
|
|
class wxCompositeWindow : public wxCompositeWindowSettersOnly<W>
|
|
|
|
{
|
|
|
|
public:
|
2016-09-25 20:21:28 +00:00
|
|
|
virtual void SetFocus() wxOVERRIDE
|
2011-10-19 16:20:01 +00:00
|
|
|
{
|
|
|
|
wxSetFocusToChild(this, NULL);
|
|
|
|
}
|
|
|
|
|
2017-12-21 12:48:30 +00:00
|
|
|
protected:
|
|
|
|
// Default ctor sets things up for handling children events correctly.
|
|
|
|
wxCompositeWindow()
|
|
|
|
{
|
2018-05-28 20:10:56 +00:00
|
|
|
this->Bind(wxEVT_CREATE, &wxCompositeWindow::OnWindowCreate, this);
|
2017-12-21 12:48:30 +00:00
|
|
|
}
|
|
|
|
|
2011-01-03 11:24:13 +00:00
|
|
|
private:
|
2011-10-19 16:20:01 +00:00
|
|
|
void OnWindowCreate(wxWindowCreateEvent& event)
|
|
|
|
{
|
|
|
|
event.Skip();
|
|
|
|
|
|
|
|
// Attach a few event handlers to all parts of the composite window.
|
|
|
|
// This makes the composite window behave more like a simple control
|
|
|
|
// and allows other code (such as wxDataViewCtrl's inline editing
|
|
|
|
// support) to hook into its event processing.
|
|
|
|
|
|
|
|
wxWindow *child = event.GetWindow();
|
2018-07-22 21:41:53 +00:00
|
|
|
|
|
|
|
// Check that it's one of our children: it could also be this window
|
|
|
|
// itself (for which we don't need to handle focus at all) or one of
|
|
|
|
// its grandchildren and we don't want to bind to those as child
|
|
|
|
// controls are supposed to be well-behaved and get their own focus
|
|
|
|
// event if any of their children get focus anyhow, so binding to them
|
|
|
|
// would only result in duplicate events.
|
|
|
|
//
|
|
|
|
// Notice that we can't use GetCompositeWindowParts() here because the
|
|
|
|
// member variables that are typically used in its implementation in
|
|
|
|
// the derived classes would typically not be initialized yet, as this
|
|
|
|
// event is generated by "m_child = new wxChildControl(this, ...)" code
|
|
|
|
// before "m_child" is assigned.
|
|
|
|
if ( child->GetParent() != this )
|
|
|
|
return;
|
2011-10-19 16:20:01 +00:00
|
|
|
|
2018-05-28 20:10:56 +00:00
|
|
|
child->Bind(wxEVT_SET_FOCUS, &wxCompositeWindow::OnSetFocus, this);
|
2018-01-29 17:32:54 +00:00
|
|
|
|
2018-05-28 20:10:56 +00:00
|
|
|
child->Bind(wxEVT_KILL_FOCUS, &wxCompositeWindow::OnKillFocus, this);
|
2011-10-19 16:20:01 +00:00
|
|
|
|
|
|
|
// Some events should be only handled for non-toplevel children. For
|
|
|
|
// example, we want to close the control in wxDataViewCtrl when Enter
|
|
|
|
// is pressed in the inline editor, but not when it's pressed in a
|
|
|
|
// popup dialog it opens.
|
|
|
|
wxWindow *win = child;
|
|
|
|
while ( win && win != this )
|
|
|
|
{
|
|
|
|
if ( win->IsTopLevel() )
|
|
|
|
return;
|
|
|
|
win = win->GetParent();
|
|
|
|
}
|
|
|
|
|
2018-05-28 20:10:56 +00:00
|
|
|
child->Bind(wxEVT_CHAR, &wxCompositeWindow::OnChar, this);
|
2011-10-19 16:20:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OnChar(wxKeyEvent& event)
|
|
|
|
{
|
|
|
|
if ( !this->ProcessWindowEvent(event) )
|
|
|
|
event.Skip();
|
|
|
|
}
|
|
|
|
|
2018-01-29 17:32:54 +00:00
|
|
|
void OnSetFocus(wxFocusEvent& event)
|
|
|
|
{
|
|
|
|
event.Skip();
|
|
|
|
|
|
|
|
// When a child of a composite window gains focus, the entire composite
|
|
|
|
// focus gains focus as well -- unless it had it already.
|
|
|
|
//
|
|
|
|
// We suppose that we hadn't had focus if the event doesn't carry the
|
|
|
|
// previously focused window as it normally means that it comes from
|
|
|
|
// outside of this program.
|
|
|
|
wxWindow* const oldFocus = event.GetWindow();
|
|
|
|
if ( !oldFocus || oldFocus->GetMainWindowOfCompositeControl() != this )
|
|
|
|
{
|
|
|
|
wxFocusEvent eventThis(wxEVT_SET_FOCUS, this->GetId());
|
|
|
|
eventThis.SetEventObject(this);
|
|
|
|
eventThis.SetWindow(event.GetWindow());
|
|
|
|
|
|
|
|
this->ProcessWindowEvent(eventThis);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-19 16:20:01 +00:00
|
|
|
void OnKillFocus(wxFocusEvent& event)
|
|
|
|
{
|
|
|
|
// Ignore focus changes within the composite control:
|
|
|
|
wxWindow *win = event.GetWindow();
|
|
|
|
while ( win )
|
|
|
|
{
|
|
|
|
if ( win == this )
|
|
|
|
{
|
|
|
|
event.Skip();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that we don't use IsTopLevel() check here, because we do
|
|
|
|
// want to ignore focus changes going to toplevel window that have
|
|
|
|
// the composite control as its parent; these would typically be
|
|
|
|
// some kind of control's popup window.
|
|
|
|
win = win->GetParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
// The event shouldn't be ignored, forward it to the main control:
|
|
|
|
if ( !this->ProcessWindowEvent(event) )
|
|
|
|
event.Skip();
|
|
|
|
}
|
|
|
|
|
2011-01-03 11:24:13 +00:00
|
|
|
wxDECLARE_NO_COPY_TEMPLATE_CLASS(wxCompositeWindow, W);
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // _WX_COMPOSITEWIN_H_
|