]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/window.mm
dfe05d763d1da2b6016876ed4a80cbbe840fd1c5
[wxWidgets.git] / src / cocoa / window.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/window.mm
3 // Purpose: wxWindowCocoa
4 // Author: David Elliott
5 // Modified by:
6 // Created: 2002/12/26
7 // RCS-ID: $Id:
8 // Copyright: (c) 2002 David Elliott
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/window.h"
13 #include "wx/log.h"
14
15 #import <Appkit/NSView.h>
16
17 // normally the base classes aren't included, but wxWindow is special
18 #ifdef __WXUNIVERSAL__
19 IMPLEMENT_ABSTRACT_CLASS(wxWindowCocoa, wxWindowBase)
20 #else
21 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase)
22 #endif
23
24 BEGIN_EVENT_TABLE(wxWindowCocoa, wxWindowBase)
25 END_EVENT_TABLE()
26
27 // Constructor
28 void wxWindowCocoa::Init()
29 {
30 InitBase();
31
32 m_cocoaNSView = NULL;
33 m_dummyNSView = NULL;
34 m_isBeingDeleted = FALSE;
35 }
36
37 // Constructor
38 bool wxWindow::Create(wxWindow *parent, wxWindowID winid,
39 const wxPoint& pos,
40 const wxSize& size,
41 long style,
42 const wxString& name)
43 {
44 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
45 return false;
46
47 // TODO: create the window
48 NSRect cocoaRect = NSMakeRect(10,10,20,20);
49 m_cocoaNSView = NULL;
50 SetNSView([[NSView alloc] initWithFrame: cocoaRect]);
51 [m_cocoaNSView release];
52
53 if (m_parent)
54 {
55 m_parent->AddChild(this);
56 m_parent->CocoaAddChild(this);
57 }
58
59 return TRUE;
60 }
61
62 // Destructor
63 wxWindow::~wxWindow()
64 {
65 DestroyChildren();
66
67 if(m_parent)
68 m_parent->RemoveChild(this);
69
70 CocoaRemoveFromParent();
71 SetNSView(NULL);
72 }
73
74 void wxWindowCocoa::CocoaAddChild(wxWindowCocoa *child)
75 {
76 [child->m_cocoaNSView retain];
77 // NOTE: addSubView takes ownership of, but does not retain the subview
78 // Upon a removeFromView or closing the super view, the child WILL be
79 // released!!! I think the idea here is that normally you would alloc
80 // the subview and add it to the superview and this way you don't have
81 // to release what you just alloced. Unfortunately, that doesn't
82 // make sense for wxCocoa, so we do this instead.
83 [m_cocoaNSView addSubview: child->m_cocoaNSView];
84 wxASSERT(!child->m_dummyNSView);
85 child->m_isShown = true;
86 }
87
88 void wxWindowCocoa::CocoaRemoveFromParent(void)
89 {
90 if(m_dummyNSView)
91 {
92 wxASSERT(m_cocoaNSView);
93 // balances the alloc
94 [m_dummyNSView removeFromSuperview];
95 // But since we also retained it ourselves
96 [m_dummyNSView release];
97 m_dummyNSView = nil;
98 // m_cocoaNSView has of course already been removed by virtue of
99 // replaceSubview: m_cocoaNSView with: m_dummyNSView
100 }
101 else
102 [m_cocoaNSView removeFromSuperview];
103 }
104
105 void wxWindowCocoa::SetNSView(WX_NSView cocoaNSView)
106 {
107 bool need_debug = cocoaNSView || m_cocoaNSView;
108 if(need_debug) wxLogDebug("wxWindowCocoa=%p::SetNSView [m_cocoaNSView=%p retainCount]=%d",this,m_cocoaNSView,[m_cocoaNSView retainCount]);
109 if(m_cocoaNSView)
110 DisassociateNSView(m_cocoaNSView);
111 [cocoaNSView retain];
112 [m_cocoaNSView release];
113 m_cocoaNSView = cocoaNSView;
114 if(m_cocoaNSView)
115 AssociateNSView(m_cocoaNSView);
116 if(need_debug) wxLogDebug("wxWindowCocoa=%p::SetNSView [cocoaNSView=%p retainCount]=%d",this,cocoaNSView,[cocoaNSView retainCount]);
117 }
118
119 void wxWindowCocoa::Cocoa_FrameChanged(void)
120 {
121 wxLogDebug("Cocoa_FrameChanged");
122 wxSizeEvent event(GetSize(), m_windowId);
123 event.SetEventObject(this);
124 GetEventHandler()->ProcessEvent(event);
125 }
126
127 bool wxWindow::Close(bool force)
128 {
129 return false;
130 }
131
132 bool wxWindow::Show(bool show)
133 {
134 // If the window is marked as visible, then it shouldn't have a dummy view
135 // If the window is marked hidden, then it should have a dummy view
136 wxASSERT_MSG( (m_isShown && !m_dummyNSView) || (!m_isShown && m_dummyNSView),"wxWindow: m_isShown does not agree with m_dummyNSView");
137 // Return false if there isn't a window to show or hide
138 if(!m_cocoaNSView)
139 return false;
140 // Return false if the state isn't changing
141 if( show == m_isShown )
142 return false;
143 if(show)
144 {
145 // replaceSubView releases m_dummyNSView, balancing the alloc
146 [m_cocoaNSView retain];
147 [[m_dummyNSView superview] replaceSubview:m_dummyNSView with:m_cocoaNSView];
148 // But since we also retained it ourselves
149 [m_dummyNSView release];
150 m_dummyNSView = nil;
151 return true;
152 }
153 else
154 {
155 m_dummyNSView = [[NSView alloc] initWithFrame: [m_cocoaNSView frame]];
156 [m_dummyNSView retain];
157 // NOTE: replaceSubView will cause m_cocaNSView to be released
158 [[m_cocoaNSView superview] replaceSubview:m_cocoaNSView with:m_dummyNSView];
159 // m_coocaNSView is now only retained by us
160 return true;
161 }
162 }
163
164 void wxWindowCocoa::DoSetSize(int x, int y, int width, int height, int sizeFlags)
165 {
166 wxLogDebug("wxWindow=%p::DoSetSizeWindow(%d,%d,%d,%d,Auto: %s%s)",this,x,y,width,height,(sizeFlags&wxSIZE_AUTO_WIDTH)?"W":".",sizeFlags&wxSIZE_AUTO_HEIGHT?"H":".");
167 int currentX, currentY;
168 int currentW, currentH;
169 DoGetPosition(&currentX, &currentY);
170 DoGetSize(&currentW, &currentH);
171 if((x==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
172 x=currentX;
173 if((y==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
174 y=currentY;
175
176 AdjustForParentClientOrigin(x,y,sizeFlags);
177
178 wxSize size(-1,-1);
179
180 if((width==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
181 {
182 if(sizeFlags&wxSIZE_AUTO_WIDTH)
183 {
184 size=DoGetBestSize();
185 width=size.x;
186 }
187 else
188 width=currentW;
189 }
190 if((height==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
191 {
192 if(sizeFlags&wxSIZE_AUTO_HEIGHT)
193 {
194 if(size.x==-1)
195 size=DoGetBestSize();
196 height=size.y;
197 }
198 else
199 height=currentH;
200 }
201 DoMoveWindow(x,y,width,height);
202 }
203
204 void wxWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
205 {
206 wxLogDebug("wxWindow=%p::DoMoveWindow(%d,%d,%d,%d)",this,x,y,width,height);
207
208 NSView *superview = [m_cocoaNSView superview];
209 wxCHECK_RET(superview,"NSView does not have a superview");
210 NSRect parentRect = [superview frame];
211
212 NSRect cocoaRect = NSMakeRect(x,parentRect.size.height-(y+height),width,height);
213 [m_cocoaNSView setFrame: cocoaRect];
214 // Also change the dummy's size
215 if(m_dummyNSView)
216 [m_dummyNSView setFrame: cocoaRect];
217 }
218
219 // Get total size
220 void wxWindow::DoGetSize(int *w, int *h) const
221 {
222 NSRect cocoaRect = [m_cocoaNSView frame];
223 if(w)
224 *w=(int)cocoaRect.size.width;
225 if(h)
226 *h=(int)cocoaRect.size.height;
227 wxLogDebug("wxWindow=%p::DoGetSize = (%d,%d)",this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
228 }
229
230 void wxWindow::DoGetPosition(int *x, int *y) const
231 {
232 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
233 NSView *superview = [nsview superview];
234 wxCHECK_RET(superview,"NSView does not have a superview");
235 NSRect parentRect = [superview frame];
236
237 NSRect cocoaRect = [nsview frame];
238 if(x)
239 *x=(int)cocoaRect.origin.x;
240 if(y)
241 *y=(int)(parentRect.size.height-(cocoaRect.origin.y+cocoaRect.size.height));
242 wxLogDebug("wxWindow=%p::DoGetPosition = (%d,%d)",this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
243 }
244
245 WXWidget wxWindow::GetHandle() const
246 {
247 return m_cocoaNSView;
248 }
249
250 void wxWindow::SetFocus()
251 {
252 // TODO
253 }
254
255 void wxWindow::DoCaptureMouse()
256 {
257 // TODO
258 }
259
260 void wxWindow::DoReleaseMouse()
261 {
262 // TODO
263 }
264
265 void wxWindow::DoScreenToClient(int *x, int *y) const
266 {
267 // TODO
268 }
269
270 void wxWindow::DoClientToScreen(int *x, int *y) const
271 {
272 // TODO
273 }
274
275 // Get size *available for subwindows* i.e. excluding menu bar etc.
276 void wxWindow::DoGetClientSize(int *x, int *y) const
277 {
278 wxLogDebug("DoGetClientSize:");
279 wxWindowCocoa::DoGetSize(x,y);
280 // TODO: Actually account for menubar, borders, etc...
281 }
282
283 void wxWindow::DoSetClientSize(int width, int height)
284 {
285 wxLogDebug("DoSetClientSize=(%d,%d)",width,height);
286 // TODO
287 }
288
289 int wxWindow::GetCharHeight() const
290 {
291 // TODO
292 return 0;
293 }
294
295 int wxWindow::GetCharWidth() const
296 {
297 // TODO
298 return 0;
299 }
300
301 void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
302 int *descent, int *externalLeading, const wxFont *theFont) const
303 {
304 // TODO
305 }
306
307 void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
308 {
309 // TODO
310 }
311
312 // Coordinates relative to the window
313 void wxWindow::WarpPointer (int x_pos, int y_pos)
314 {
315 // TODO
316 }
317
318 int wxWindow::GetScrollPos(int orient) const
319 {
320 // TODO
321 return 0;
322 }
323
324 // This now returns the whole range, not just the number
325 // of positions that we can scroll.
326 int wxWindow::GetScrollRange(int orient) const
327 {
328 // TODO
329 return 0;
330 }
331
332 int wxWindow::GetScrollThumb(int orient) const
333 {
334 // TODO
335 return 0;
336 }
337
338 void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
339 {
340 // TODO
341 }
342
343 // New function that will replace some of the above.
344 void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible,
345 int range, bool refresh)
346 {
347 // TODO
348 }
349
350 // Does a physical scroll
351 void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
352 {
353 // TODO
354 }
355
356 bool wxWindow::SetFont(const wxFont& font)
357 {
358 // TODO
359 return TRUE;
360 }
361
362 void wxWindow::Clear()
363 {
364 // TODO
365 }
366
367 // Raise the window to the top of the Z order
368 void wxWindow::Raise()
369 {
370 // TODO
371 }
372
373 // Lower the window to the bottom of the Z order
374 void wxWindow::Lower()
375 {
376 // TODO
377 }
378
379 bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
380 {
381 return FALSE;
382 }
383
384 // Get the window with the focus
385 wxWindow *wxWindowBase::FindFocus()
386 {
387 // TODO
388 return NULL;
389 }
390
391 /* static */ wxWindow *wxWindowBase::GetCapture()
392 {
393 // TODO
394 return NULL;
395 }
396
397 wxWindow *wxGetActiveWindow()
398 {
399 // TODO
400 return NULL;
401 }
402