]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/window.mm
don't return garbage from GetRawBitmap
[wxWidgets.git] / src / cocoa / window.mm
CommitLineData
fb896a32
DE
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__
19IMPLEMENT_ABSTRACT_CLASS(wxWindowCocoa, wxWindowBase)
20#else
21IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase)
22#endif
23
24BEGIN_EVENT_TABLE(wxWindowCocoa, wxWindowBase)
25END_EVENT_TABLE()
26
27// Constructor
28void wxWindowCocoa::Init()
29{
30 InitBase();
31
32 m_cocoaNSView = NULL;
33 m_dummyNSView = NULL;
34 m_isBeingDeleted = FALSE;
35}
36
37// Constructor
38bool 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
63wxWindow::~wxWindow()
64{
65 DestroyChildren();
66
67 if(m_parent)
68 m_parent->RemoveChild(this);
69
70 CocoaRemoveFromParent();
71 SetNSView(NULL);
72}
73
74void 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
88void wxWindowCocoa::CocoaRemoveFromParent(void)
89{
fb896a32
DE
90 if(m_dummyNSView)
91 {
c1f7d193 92 wxASSERT(m_cocoaNSView);
fb896a32
DE
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
105void 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
8ea5271e
DE
119bool wxWindowCocoa::Cocoa_drawRect(const NSRect &rect)
120{
121 wxLogDebug("Cocoa_drawRect");
122 //FIXME: should probably turn that rect into the update region
123 wxPaintEvent event(m_windowId);
124 event.SetEventObject(this);
125 return GetEventHandler()->ProcessEvent(event);
126}
127
fb896a32
DE
128void wxWindowCocoa::Cocoa_FrameChanged(void)
129{
130 wxLogDebug("Cocoa_FrameChanged");
131 wxSizeEvent event(GetSize(), m_windowId);
132 event.SetEventObject(this);
133 GetEventHandler()->ProcessEvent(event);
134}
135
136bool wxWindow::Close(bool force)
137{
138 return false;
139}
140
141bool wxWindow::Show(bool show)
142{
143 // If the window is marked as visible, then it shouldn't have a dummy view
144 // If the window is marked hidden, then it should have a dummy view
145 wxASSERT_MSG( (m_isShown && !m_dummyNSView) || (!m_isShown && m_dummyNSView),"wxWindow: m_isShown does not agree with m_dummyNSView");
146 // Return false if there isn't a window to show or hide
147 if(!m_cocoaNSView)
148 return false;
149 // Return false if the state isn't changing
150 if( show == m_isShown )
151 return false;
152 if(show)
153 {
154 // replaceSubView releases m_dummyNSView, balancing the alloc
155 [m_cocoaNSView retain];
156 [[m_dummyNSView superview] replaceSubview:m_dummyNSView with:m_cocoaNSView];
157 // But since we also retained it ourselves
158 [m_dummyNSView release];
159 m_dummyNSView = nil;
fb896a32
DE
160 }
161 else
162 {
163 m_dummyNSView = [[NSView alloc] initWithFrame: [m_cocoaNSView frame]];
164 [m_dummyNSView retain];
165 // NOTE: replaceSubView will cause m_cocaNSView to be released
166 [[m_cocoaNSView superview] replaceSubview:m_cocoaNSView with:m_dummyNSView];
167 // m_coocaNSView is now only retained by us
fb896a32 168 }
a6b4ff2e
DE
169 m_isShown = show;
170 return true;
fb896a32
DE
171}
172
173void wxWindowCocoa::DoSetSize(int x, int y, int width, int height, int sizeFlags)
174{
ddf7346a 175// 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":".");
fb896a32
DE
176 int currentX, currentY;
177 int currentW, currentH;
178 DoGetPosition(&currentX, &currentY);
179 DoGetSize(&currentW, &currentH);
180 if((x==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
181 x=currentX;
182 if((y==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
183 y=currentY;
184
185 AdjustForParentClientOrigin(x,y,sizeFlags);
186
187 wxSize size(-1,-1);
188
189 if((width==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
190 {
191 if(sizeFlags&wxSIZE_AUTO_WIDTH)
192 {
193 size=DoGetBestSize();
194 width=size.x;
195 }
196 else
197 width=currentW;
198 }
199 if((height==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
200 {
201 if(sizeFlags&wxSIZE_AUTO_HEIGHT)
202 {
203 if(size.x==-1)
204 size=DoGetBestSize();
205 height=size.y;
206 }
207 else
208 height=currentH;
209 }
210 DoMoveWindow(x,y,width,height);
211}
212
213void wxWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
214{
ddf7346a 215// wxLogDebug("wxWindow=%p::DoMoveWindow(%d,%d,%d,%d)",this,x,y,width,height);
fb896a32 216
d449cf47
DE
217 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
218 NSView *superview = [nsview superview];
fb896a32
DE
219 wxCHECK_RET(superview,"NSView does not have a superview");
220 NSRect parentRect = [superview frame];
221
222 NSRect cocoaRect = NSMakeRect(x,parentRect.size.height-(y+height),width,height);
223 [m_cocoaNSView setFrame: cocoaRect];
224 // Also change the dummy's size
225 if(m_dummyNSView)
226 [m_dummyNSView setFrame: cocoaRect];
227}
228
229// Get total size
230void wxWindow::DoGetSize(int *w, int *h) const
231{
232 NSRect cocoaRect = [m_cocoaNSView frame];
233 if(w)
234 *w=(int)cocoaRect.size.width;
235 if(h)
236 *h=(int)cocoaRect.size.height;
ddf7346a 237// wxLogDebug("wxWindow=%p::DoGetSize = (%d,%d)",this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
fb896a32
DE
238}
239
240void wxWindow::DoGetPosition(int *x, int *y) const
241{
576a1544
DE
242 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
243 NSView *superview = [nsview superview];
fb896a32
DE
244 wxCHECK_RET(superview,"NSView does not have a superview");
245 NSRect parentRect = [superview frame];
246
576a1544 247 NSRect cocoaRect = [nsview frame];
fb896a32
DE
248 if(x)
249 *x=(int)cocoaRect.origin.x;
250 if(y)
251 *y=(int)(parentRect.size.height-(cocoaRect.origin.y+cocoaRect.size.height));
ddf7346a 252// wxLogDebug("wxWindow=%p::DoGetPosition = (%d,%d)",this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
fb896a32
DE
253}
254
255WXWidget wxWindow::GetHandle() const
256{
257 return m_cocoaNSView;
258}
259
ddf7346a
DE
260void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
261{
262 [m_cocoaNSView setNeedsDisplay:YES];
263}
264
fb896a32
DE
265void wxWindow::SetFocus()
266{
267 // TODO
268}
269
270void wxWindow::DoCaptureMouse()
271{
272 // TODO
273}
274
275void wxWindow::DoReleaseMouse()
276{
277 // TODO
278}
279
280void wxWindow::DoScreenToClient(int *x, int *y) const
281{
282 // TODO
283}
284
285void wxWindow::DoClientToScreen(int *x, int *y) const
286{
287 // TODO
288}
289
290// Get size *available for subwindows* i.e. excluding menu bar etc.
291void wxWindow::DoGetClientSize(int *x, int *y) const
292{
293 wxLogDebug("DoGetClientSize:");
294 wxWindowCocoa::DoGetSize(x,y);
295 // TODO: Actually account for menubar, borders, etc...
296}
297
298void wxWindow::DoSetClientSize(int width, int height)
299{
300 wxLogDebug("DoSetClientSize=(%d,%d)",width,height);
301 // TODO
302}
303
304int wxWindow::GetCharHeight() const
305{
306 // TODO
307 return 0;
308}
309
310int wxWindow::GetCharWidth() const
311{
312 // TODO
313 return 0;
314}
315
316void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
317 int *descent, int *externalLeading, const wxFont *theFont) const
318{
319 // TODO
320}
321
fb896a32
DE
322// Coordinates relative to the window
323void wxWindow::WarpPointer (int x_pos, int y_pos)
324{
325 // TODO
326}
327
328int wxWindow::GetScrollPos(int orient) const
329{
330 // TODO
331 return 0;
332}
333
334// This now returns the whole range, not just the number
335// of positions that we can scroll.
336int wxWindow::GetScrollRange(int orient) const
337{
338 // TODO
339 return 0;
340}
341
342int wxWindow::GetScrollThumb(int orient) const
343{
344 // TODO
345 return 0;
346}
347
348void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
349{
350 // TODO
351}
352
353// New function that will replace some of the above.
354void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible,
355 int range, bool refresh)
356{
357 // TODO
358}
359
360// Does a physical scroll
361void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
362{
363 // TODO
364}
365
366bool wxWindow::SetFont(const wxFont& font)
367{
368 // TODO
369 return TRUE;
370}
371
372void wxWindow::Clear()
373{
374 // TODO
375}
376
377// Raise the window to the top of the Z order
378void wxWindow::Raise()
379{
d5ca6629
DE
380 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
381 NSView *superview = [nsview superview];
382 [nsview retain];
383 [nsview removeFromSuperview];
384 [superview addSubview:nsview];
fb896a32
DE
385}
386
387// Lower the window to the bottom of the Z order
388void wxWindow::Lower()
389{
390 // TODO
391}
392
393bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
394{
395 return FALSE;
396}
397
398// Get the window with the focus
399wxWindow *wxWindowBase::FindFocus()
400{
401 // TODO
402 return NULL;
403}
404
405/* static */ wxWindow *wxWindowBase::GetCapture()
406{
407 // TODO
408 return NULL;
409}
410
411wxWindow *wxGetActiveWindow()
412{
413 // TODO
414 return NULL;
415}
416