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