]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/drawer.cpp
wxDrawerWindow - [ 909351 ] Drawer window for OS X Carbon - somewhat heavily modified
[wxWidgets.git] / src / mac / carbon / drawer.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: drawer.cpp
3 // Purpose: 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.
7 // Author: Jason Bagley
8 // Modified by:
9 // Created: 2004-30-01
10 // RCS-ID: $Id$
11 // Copyright: (c) Jason Bagley; Art & Logic, Inc.
12 // Licence: wxWindows licence
13 /////////////////////////////////////////////////////////////////////////////
14
15 #ifdef __GNUG__
16 #pragma implementation "control.h"
17 #endif
18
19 #include "wx/defs.h"
20
21 #include "wx/mac/carbon/drawer.h"
22 #include "wx/mac/private.h"
23
24 #if defined( __WXMAC__ ) && TARGET_API_MAC_OSX && ( MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_2 )
25
26 // TODO:
27 // ¥ Contents do not draw on drawer opening. They do when a window is deactivated
28 // with the drawer open, and contents stay visible thereafter.
29
30 IMPLEMENT_DYNAMIC_CLASS(wxDrawerWindow, wxWindow)
31
32 // Use consants for now.
33 // These can be made into member variables and set dynamically.
34 const int kLeadingOffset = 20;
35 const int kTrailingOffset = 20;
36
37
38 // Converts Mac window edge constants to wxDirections, wxLEFT, wxRIGHT, etc.
39 static wxDirection WindowEdgeToDirection(OptionBits edge);
40
41 // Convert wxDirections to MAc window edge constants.
42 static OptionBits DirectionToWindowEdge(wxDirection direction);
43
44
45 wxDrawerWindow::wxDrawerWindow()
46 {
47 }
48
49 wxDrawerWindow::~wxDrawerWindow()
50 {
51 m_isBeingDeleted = TRUE;
52 this->Show(FALSE);
53 }
54
55 bool wxDrawerWindow::Create(wxWindow *parent,
56 wxWindowID id, const wxString& title,
57 wxSize size, wxDirection edge, const wxString& name)
58 {
59 wxASSERT_MSG(NULL != parent, "wxDrawerWindows must be attached to a parent window.");
60
61 // Constrain the drawer size to the parent window.
62 const wxSize parentSize(parent->GetClientSize());
63 if (wxLEFT == edge || wxRIGHT == edge)
64 {
65 if (size.GetHeight() > parentSize.GetHeight())
66 size.SetHeight(parentSize.GetHeight() - (kLeadingOffset + kTrailingOffset));
67 }
68 else
69 {
70 if (size.GetWidth() > parentSize.GetWidth())
71 size.SetWidth(parentSize.GetWidth() - (kLeadingOffset + kTrailingOffset));
72 }
73
74 // Create the drawer window.
75 const wxPoint pos(0, 0);
76 const wxSize dummySize(0,0);
77 const long style = wxFRAME_DRAWER;
78
79 bool success = wxWindow::Create(parent, id, pos, dummySize, style, name);
80 if (success)
81 {
82 this->MacCreateRealWindow(title, pos, size, style, name);
83 success = (m_macWindow != NULL);
84 }
85
86 if (success)
87 {
88 // Use drawer brush.
89 m_macBackgroundBrush.MacSetTheme(kThemeBrushDrawerBackground);
90 ::SetThemeWindowBackground((WindowRef)m_macWindow,
91 m_macBackgroundBrush.MacGetTheme(), false);
92
93 // Leading and trailing offset are gaps from parent window edges
94 // to where the drawer starts.
95 ::SetDrawerOffsets((WindowRef)m_macWindow, kLeadingOffset, kTrailingOffset);
96
97 // Set the drawers parent.
98 // Is there a better way to get the parent's WindowRef?
99 wxTopLevelWindow* tlwParent = wxDynamicCast(parent, wxTopLevelWindow);
100 if (NULL != tlwParent)
101 {
102 OSStatus status = ::SetDrawerParent((WindowRef)m_macWindow,
103 (WindowRef)tlwParent->MacGetWindowRef());
104 success = (noErr == status);
105 }
106 else
107 success = false;
108 }
109
110 return success && SetPreferredEdge(edge);
111 }
112
113 wxDirection wxDrawerWindow::GetCurrentEdge() const
114 {
115 const OptionBits edge = ::GetDrawerCurrentEdge((WindowRef)m_macWindow);
116 return WindowEdgeToDirection(edge);
117 }
118
119 wxDirection wxDrawerWindow::GetPreferredEdge() const
120 {
121 const OptionBits edge = ::GetDrawerPreferredEdge((WindowRef)m_macWindow);
122 return WindowEdgeToDirection(edge);
123 }
124
125 bool wxDrawerWindow::IsOpen() const
126 {
127 WindowDrawerState state = ::GetDrawerState((WindowRef)m_macWindow);
128 return (state == kWindowDrawerOpen || state == kWindowDrawerOpening);
129 }
130
131 bool wxDrawerWindow::Open(bool show)
132 {
133 static const Boolean kAsynchronous = true;
134 OSStatus status = noErr;
135
136 if (show)
137 {
138 const OptionBits preferredEdge = ::GetDrawerPreferredEdge((WindowRef)m_macWindow);
139 status = ::OpenDrawer((WindowRef)m_macWindow, preferredEdge, kAsynchronous);
140 }
141 else
142 status = ::CloseDrawer((WindowRef)m_macWindow, kAsynchronous);
143
144 return (noErr == status);
145 }
146
147 bool wxDrawerWindow::SetPreferredEdge(wxDirection edge)
148 {
149 const OSStatus status = ::SetDrawerPreferredEdge((WindowRef)m_macWindow,
150 DirectionToWindowEdge(edge));
151 return (noErr == status);
152 }
153
154
155 OptionBits DirectionToWindowEdge(wxDirection direction)
156 {
157 OptionBits edge;
158 switch (direction)
159 {
160 case wxTOP:
161 edge = kWindowEdgeTop;
162 break;
163
164 case wxBOTTOM:
165 edge = kWindowEdgeBottom;
166 break;
167
168 case wxRIGHT:
169 edge = kWindowEdgeRight;
170 break;
171
172 case wxLEFT:
173 default:
174 edge = kWindowEdgeLeft;
175 break;
176 }
177 return edge;
178 }
179
180 wxDirection WindowEdgeToDirection(OptionBits edge)
181 {
182 wxDirection direction;
183 switch (edge)
184 {
185 case kWindowEdgeTop:
186 direction = wxTOP;
187 break;
188
189 case kWindowEdgeBottom:
190 direction = wxBOTTOM;
191 break;
192
193 case kWindowEdgeRight:
194 direction = wxRIGHT;
195 break;
196
197 case kWindowEdgeDefault: // store current preferred and return that here?
198 case kWindowEdgeLeft:
199 default:
200 direction = wxLEFT;
201 break;
202 }
203
204 return direction;
205 }
206
207 #endif // defined( __WXMAC__ ) && TARGET_API_MAC_OSX && ( MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_2 )
208