1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Generic Drawer child window classes.
4 // Drawer windows appear under their parent window and
5 // behave like a drawer, opening and closing to reveal
6 // content that does not need to be visible at all times.
11 // Copyright: (c) Ryan Norton
12 // Licence: wxWindows licence
13 /////////////////////////////////////////////////////////////////////////////
16 #pragma implementation "drawerg.h"
20 #include "wx/generic/drawerg.h"
23 enum wxGenericDrawerConstants
25 wxGENERICDRAWER_INCREMENT
= 1,
26 wxGENERICDRAWER_INTERVAL
= 3
29 class wxGenericDrawerTimer
: public wxTimer
33 wxGenericDrawerTimer(wxGenericDrawerWindow
* pWin
, const int& nMult
) :
34 m_pWin(pWin
), m_nMult(nMult
) {}
38 if ((m_nMult
< 0 && !m_pWin
->m_nOpenOffset
) ||
41 (( (m_pWin
->m_nCurrentEdge
== wxLEFT
||
42 m_pWin
->m_nCurrentEdge
== wxRIGHT
) && m_pWin
->m_nOpenOffset
>= m_pWin
->GetSize().GetWidth())
46 ( (m_pWin
->m_nCurrentEdge
== wxTOP
||
47 m_pWin
->m_nCurrentEdge
== wxBOTTOM
) && m_pWin
->m_nOpenOffset
>= m_pWin
->GetSize().GetHeight())
52 wxFprintf(stderr, "shutdown - mult:%i off:%i tlr:%i ttb:%i\n", m_nMult, m_pWin->m_nOpenOffset,
54 (m_pWin->m_nCurrentEdge == wxLEFT ||
55 m_pWin->m_nCurrentEdge == wxRIGHT) && m_pWin->m_nOpenOffset >= m_pWin->GetSize().GetWidth(),
57 (m_pWin->m_nCurrentEdge == wxTOP ||
58 m_pWin->m_nCurrentEdge == wxBOTTOM) && m_pWin->m_nOpenOffset >= m_pWin->GetSize().GetHeight()
62 //clean up & shut down
64 m_pWin
->m_pTimer
= NULL
;
66 //reset current edge to preferred edge
69 m_pWin
->m_nCurrentEdge
= m_pWin
->m_nPreferredEdge
;
70 m_pWin
->DoDrawerPosition();
78 // wxFprintf(stderr, "continue\n");
79 wxPoint pos
= m_pWin
->GetPosition();
80 switch (m_pWin
->m_nCurrentEdge
)
83 pos
.x
-= wxGENERICDRAWER_INCREMENT
* m_nMult
;
87 pos
.x
+= wxGENERICDRAWER_INCREMENT
* m_nMult
;
91 pos
.y
-= wxGENERICDRAWER_INCREMENT
* m_nMult
;
96 pos
.y
+= wxGENERICDRAWER_INCREMENT
* m_nMult
;
99 m_pWin
->SetPosition(pos
);
100 //wxFprintf(stderr, "tpos:%i,%i\n", pos.x, pos.y);
101 m_pWin
->m_nOpenOffset
+= wxGENERICDRAWER_INCREMENT
* m_nMult
;
105 wxGenericDrawerWindow
* m_pWin
;
109 enum wxGenericDrawerOffsets
111 wxGENERICDRAWER_TOPOFFSET
= 20,
112 wxGENERICDRAWER_BOTTOMOFFSET
= 20
115 IMPLEMENT_DYNAMIC_CLASS(wxGenericDrawerWindow
, wxWindow
)
117 wxGenericDrawerWindow::wxGenericDrawerWindow() :
118 m_bOpen(false), m_nCurrentEdge(wxLEFT
), m_nPreferredEdge(wxLEFT
), m_nOpenOffset(0),
123 wxGenericDrawerWindow::~wxGenericDrawerWindow()
125 m_isBeingDeleted
= true;
134 bool wxGenericDrawerWindow::Create(wxWindow
*parent
,
135 wxWindowID id
, const wxString
& title
,
136 wxSize size
, wxDirection edge
, const wxString
& name
)
138 wxASSERT_MSG(NULL
!= parent
, wxT("wxDrawerWindows must be attached to a parent window."));
140 wxASSERT_MSG(m_pTimer
== NULL
, wxT("Currently opening!!"));
142 wxASSERT_MSG(edge
== wxLEFT
||
145 edge
== wxBOTTOM
, wxT("Invalid edge") );
147 // Create the drawer window.
148 const wxPoint
pos(0, 0);
149 const long style
= wxNO_BORDER
|| wxVSCROLL
| wxHSCROLL
;//wxFRAME_DRAWER;
151 bool success
= wxTopLevelWindow::Create(parent
, id
, wxT(""), pos
, size
, style
, name
);
155 // Set the drawers parent.
158 if (parent
->IsTopLevel())
160 wxTopLevelWindow
* tlwParent
= (wxTopLevelWindow
*) parent
;
162 //connect to parent's events
163 tlwParent
->Connect(tlwParent
->GetId(), wxEVT_MOVE
,
164 (wxObjectEventFunction
) (wxEventFunction
) (wxMoveEventFunction
) &wxGenericDrawerWindow::OnDrawerMove
,
168 tlwParent
->Connect(tlwParent
->GetId(), wxEVT_SIZE
,
169 (wxObjectEventFunction
) (wxEventFunction
) (wxSizeEventFunction
) &wxGenericDrawerWindow::OnDrawerSize
,
173 tlwParent
->Connect(tlwParent
->GetId(), wxEVT_KILL_FOCUS
,
174 (wxObjectEventFunction
) (wxEventFunction
) (wxFocusEventFunction
) &wxGenericDrawerWindow::OnDrawerFocus
,
178 m_LastParentSize
= parent
->GetSize();
186 m_nCurrentEdge
= m_nPreferredEdge
= edge
;
188 if (success
&& parent
->IsShown()) //bring parent on top
193 //wxFprintf(stderr,wxT("success==%i\n"), success);
197 wxDirection
wxGenericDrawerWindow::GetCurrentEdge() const
199 return m_nCurrentEdge
;
202 wxDirection
wxGenericDrawerWindow::GetPreferredEdge() const
204 return m_nPreferredEdge
;
207 bool wxGenericDrawerWindow::IsOpen() const
212 bool wxGenericDrawerWindow::Open(const bool& show
)
219 m_pTimer
= new wxGenericDrawerTimer(this, 1);
220 m_pTimer
->Start(wxGENERICDRAWER_INTERVAL
);
227 m_pTimer
= new wxGenericDrawerTimer(this, -1);
228 m_pTimer
->Start(wxGENERICDRAWER_INTERVAL
);
234 bool wxGenericDrawerWindow::SetPreferredEdge(const wxDirection
& edge
)
236 wxASSERT(edge
== wxLEFT
||
243 m_nCurrentEdge
= edge
;
247 m_nPreferredEdge
= edge
;
251 void wxGenericDrawerWindow::DoDrawerPosition()
253 const wxPoint
parentPosition(GetParent()->GetPosition());
256 (m_nCurrentEdge
== wxLEFT
?
257 -m_nOpenOffset
: m_nCurrentEdge
== wxRIGHT
? GetParent()->GetSize().GetWidth() + m_nOpenOffset
- GetSize().GetWidth(): wxGENERICDRAWER_TOPOFFSET
),
259 (m_nCurrentEdge
== wxTOP
?
260 -m_nOpenOffset
: m_nCurrentEdge
== wxBOTTOM
? GetParent()->GetSize().GetHeight() + m_nOpenOffset
- GetSize().GetHeight() : wxGENERICDRAWER_TOPOFFSET
)
262 //wxFprintf(stderr,wxT("parentposition:%i,%i\n"), parentPosition.x, parentPosition.y);
263 //wxFprintf(stderr,wxT("offset:%i\nposition:%i,%i\n"), m_nOpenOffset, GetPosition().x, GetPosition().y);
266 void wxGenericDrawerWindow::DoDrawerSize()
268 // Constrain the drawer size to the parent window.
269 const wxSize
parentSize(GetParent()->GetClientSize());
270 wxSize size
= GetSize();
272 if (wxLEFT
== m_nCurrentEdge
|| wxRIGHT
== m_nCurrentEdge
)
274 if (size
.GetHeight() > parentSize
.GetHeight())
275 size
.SetHeight(parentSize
.GetHeight() - (wxGENERICDRAWER_TOPOFFSET
+ wxGENERICDRAWER_BOTTOMOFFSET
));
277 size
.SetHeight(size
.GetHeight() + (parentSize
.GetHeight() - m_LastParentSize
.GetHeight()));
281 if (size
.GetWidth() > parentSize
.GetWidth())
282 size
.SetWidth(parentSize
.GetWidth() - (wxGENERICDRAWER_TOPOFFSET
+ wxGENERICDRAWER_BOTTOMOFFSET
));
284 size
.SetWidth(size
.GetWidth() + (parentSize
.GetWidth() - m_LastParentSize
.GetWidth()));
288 m_LastParentSize
= parentSize
;
290 //wxFprintf(stderr,wxT("size:%i,%i\n"), size.GetWidth(), size.GetHeight());
293 void wxGenericDrawerWindow::OnDrawerFocus(wxFocusEvent
& evt
)
295 // wxFprintf(stderr, wxT("focus\n"));
297 if (wxWindow::FindFocus() == this)
298 GetParent()->SetFocus();
301 void wxGenericDrawerWindow::OnDrawerMove(wxMoveEvent
& evt
)
306 void wxGenericDrawerWindow::OnDrawerSize(wxSizeEvent
& evt
)