]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/glcanvas.mm
Readded event docs for wxListCtrl
[wxWidgets.git] / src / osx / cocoa / glcanvas.mm
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/glcanvas.mm
3 // Purpose: wxGLCanvas, for using OpenGL with wxWidgets under Macintosh
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id: glcanvas.cpp 54129 2008-06-11 19:30:52Z SC $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include "wx/wxprec.h"
21
22 #if defined(__BORLANDC__)
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_GLCANVAS
27
28 #include "wx/glcanvas.h"
29
30 #ifndef WX_PRECOMP
31 #include "wx/frame.h"
32 #include "wx/log.h"
33 #include "wx/settings.h"
34 #endif
35
36 #include "wx/osx/private.h"
37
38
39 WXGLContext WXGLCreateContext( WXGLPixelFormat pixelFormat, WXGLContext shareContext )
40 {
41 WXGLContext context = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext: shareContext];
42 if ( !context )
43 wxFAIL_MSG("NSOpenGLContext creation failed");
44 return context ;
45 }
46
47 void WXGLDestroyContext( WXGLContext context )
48 {
49 if ( context )
50 {
51 [context release];
52 }
53 }
54
55 void WXGLSwapBuffers( WXGLContext context )
56 {
57 [context flushBuffer];
58 }
59
60 WXGLContext WXGLGetCurrentContext()
61 {
62 return [NSOpenGLContext currentContext];
63 }
64
65 void WXGLDestroyPixelFormat( WXGLPixelFormat pixelFormat )
66 {
67 if ( pixelFormat )
68 {
69 [pixelFormat release];
70 }
71 }
72
73
74 WXGLPixelFormat WXGLChoosePixelFormat(const int *attribList)
75 {
76 NSOpenGLPixelFormatAttribute data[512];
77 const NSOpenGLPixelFormatAttribute defaultAttribs[] =
78 {
79 NSOpenGLPFADoubleBuffer,
80 (NSOpenGLPixelFormatAttribute)nil
81 };
82
83 const NSOpenGLPixelFormatAttribute *attribs;
84 if ( !attribList )
85 {
86 attribs = defaultAttribs;
87 }
88 else
89 {
90 unsigned p = 0;
91 data[p++] = NSOpenGLPFAMinimumPolicy; // make _SIZE tags behave more like GLX
92
93 for ( unsigned arg = 0; attribList[arg] !=0 && p < WXSIZEOF(data); )
94 {
95 switch ( attribList[arg++] )
96 {
97 case WX_GL_RGBA:
98 //data[p++] = AGL_RGBA;
99 break;
100
101 case WX_GL_BUFFER_SIZE:
102 //data[p++] = AGL_BUFFER_SIZE;
103 //data[p++] = attribList[arg++];
104 break;
105
106 case WX_GL_LEVEL:
107 //data[p++]=AGL_LEVEL;
108 //data[p++]=attribList[arg++];
109 break;
110
111 case WX_GL_DOUBLEBUFFER:
112 data[p++] = NSOpenGLPFADoubleBuffer;
113 break;
114
115 case WX_GL_STEREO:
116 data[p++] = NSOpenGLPFAStereo;
117 break;
118
119 case WX_GL_AUX_BUFFERS:
120 data[p++] = NSOpenGLPFAAuxBuffers;
121 data[p++] = attribList[arg++];
122 break;
123
124 case WX_GL_MIN_RED:
125 data[p++] = NSOpenGLPFAColorSize;
126 data[p++] = attribList[arg++];
127 break;
128
129 case WX_GL_MIN_GREEN:
130 //data[p++] = AGL_GREEN_SIZE;
131 //data[p++] = attribList[arg++];
132 break;
133
134 case WX_GL_MIN_BLUE:
135 //data[p++] = AGL_BLUE_SIZE;
136 //data[p++] = attribList[arg++];
137 break;
138
139 case WX_GL_MIN_ALPHA:
140 data[p++] = NSOpenGLPFAAlphaSize;
141 data[p++] = attribList[arg++];
142 break;
143
144 case WX_GL_DEPTH_SIZE:
145 data[p++] = NSOpenGLPFADepthSize;
146 data[p++] = attribList[arg++];
147 break;
148
149 case WX_GL_STENCIL_SIZE:
150 data[p++] = NSOpenGLPFAStencilSize;
151 data[p++] = attribList[arg++];
152 break;
153
154 case WX_GL_MIN_ACCUM_RED:
155 data[p++] = NSOpenGLPFAAccumSize;
156 data[p++] = attribList[arg++];
157 break;
158
159 case WX_GL_MIN_ACCUM_GREEN:
160 //data[p++] = AGL_ACCUM_GREEN_SIZE;
161 //data[p++] = attribList[arg++];
162 break;
163
164 case WX_GL_MIN_ACCUM_BLUE:
165 //data[p++] = AGL_ACCUM_BLUE_SIZE;
166 //data[p++] = attribList[arg++];
167 break;
168
169 case WX_GL_MIN_ACCUM_ALPHA:
170 //data[p++] = AGL_ACCUM_ALPHA_SIZE;
171 //data[p++] = attribList[arg++];
172 break;
173
174 case WX_GL_SAMPLE_BUFFERS:
175 if ( !wxGLCanvas::IsAGLMultiSampleAvailable() )
176 {
177 if ( !attribList[arg++] )
178 break;
179
180 return false;
181 }
182
183 data[p++] = NSOpenGLPFASampleBuffers;
184 if ( (data[p++] = attribList[arg++]) == true )
185 {
186 // don't use software fallback
187 data[p++] = NSOpenGLPFANoRecovery;
188 }
189 break;
190
191 case WX_GL_SAMPLES:
192 if ( !wxGLCanvas::IsAGLMultiSampleAvailable() )
193 {
194 if ( !attribList[arg++] )
195 break;
196
197 return false;
198 }
199
200 data[p++] = NSOpenGLPFASamples;
201 data[p++] = attribList[arg++];
202 break;
203 }
204 }
205
206 data[p] = (NSOpenGLPixelFormatAttribute)nil;
207
208 attribs = data;
209 }
210
211 return [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
212 }
213
214 bool wxGLContext::SetCurrent(const wxGLCanvas& win) const
215 {
216 if ( !m_glContext )
217 return false;
218
219 [m_glContext setView: win.GetHandle() ];
220
221 [m_glContext makeCurrentContext];
222
223 return true;
224 }
225
226 @interface wxNSCustomOpenGLView : NSView
227 {
228 wxWidgetImpl* impl;
229 NSOpenGLContext* context;
230 }
231
232 - (id)initWithFrame:(NSRect)frame;
233 - (void)setImplementation: (wxWidgetImpl *) theImplementation;
234 - (wxWidgetImpl*) implementation;
235 - (BOOL) isFlipped;
236
237 @end
238
239 @implementation wxNSCustomOpenGLView
240
241 - (id)initWithFrame:(NSRect)frame
242 {
243 [super initWithFrame:frame];
244 impl = NULL;
245 return self;
246 }
247
248 - (void)setImplementation: (wxWidgetImpl *) theImplementation
249 {
250 impl = theImplementation;
251 }
252
253 - (wxWidgetImpl*) implementation
254 {
255 return impl;
256 }
257
258 - (BOOL) isFlipped
259 {
260 return YES;
261 }
262
263 - (BOOL)isOpaque
264 {
265 return YES;
266 }
267
268 @end
269
270 bool wxGLCanvas::Create(wxWindow *parent,
271 wxWindowID id,
272 const wxPoint& pos,
273 const wxSize& size,
274 long style,
275 const wxString& name,
276 const int *attribList,
277 const wxPalette& WXUNUSED(palette))
278 {
279 m_glFormat = WXGLChoosePixelFormat(attribList);
280 if ( !m_glFormat )
281 return false;
282
283 m_macIsUserPane = false ;
284
285 if ( !wxWindow::Create(parent, id, pos, size, style, name) )
286 return false;
287
288
289 NSView* sv = (parent->GetHandle() );
290
291 NSRect r = wxOSXGetFrameForControl( this, pos , size ) ;
292 wxNSCustomOpenGLView* v = [[wxNSCustomOpenGLView alloc] initWithFrame:r];
293 [sv addSubview:v];
294 m_peer = new wxWidgetCocoaImpl( this, v );
295 [v setImplementation:m_peer];
296
297 MacPostControlCreate(pos, size) ;
298
299 return true;
300 }
301
302 #endif // wxUSE_GLCANVAS