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