12 #define DEBUG_ROOT_COCOA
16 #ifdef DEBUG_ROOT_COCOA
23 #include <OpenGL/OpenGL.h>
24 #include <Cocoa/Cocoa.h>
37 CocoaPrivate::CocoaPrivate()
38 : fCurrentDrawableID(GetRootWindowID() + 1),
41 fApplicationDelegate([[ROOTApplicationDelegate alloc] init])
44 Util::AutoreleasePool pool;
45 [NSApplication sharedApplication];
49 CocoaPrivate::~CocoaPrivate()
54 Window_t CocoaPrivate::GetRootWindowID()
const
63 bool CocoaPrivate::IsRootWindow(Window_t windowID)
const
65 return windowID == GetRootWindowID();
69 Drawable_t CocoaPrivate::RegisterDrawable(NSObject *nsObj)
73 if (fCurrentDrawableID == 999)
76 Drawable_t newID = fCurrentDrawableID;
78 if (fFreeDrawableIDs.size()) {
79 newID = fFreeDrawableIDs.back();
80 fFreeDrawableIDs.pop_back();
84 assert(fDrawables.find(newID) == fDrawables.end() &&
"RegisterDrawable, id for new drawable is still in use");
85 fDrawables[newID] = nsObj;
91 NSObject<X11Drawable> *CocoaPrivate::GetDrawable(Drawable_t drawableID)
const
93 const_drawable_iterator drawableIter = fDrawables.find(drawableID);
95 #ifdef DEBUG_ROOT_COCOA
96 if (drawableIter == fDrawables.end()) {
97 NSLog(
@"Fatal error: requested non-existing drawable %lu", drawableID);
99 std::vector<Drawable_t>::const_iterator deletedDrawable = std::find(fFreeDrawableIDs.begin(), fFreeDrawableIDs.end(), drawableID);
100 if (deletedDrawable != fFreeDrawableIDs.end()) {
101 NSLog(
@"This drawable was deleted already");
103 NSLog(
@"This drawable not found among allocated/deleted drawables");
107 assert(drawableIter != fDrawables.end() &&
"GetDrawable, non-existing drawable requested");
108 return drawableIter->second.Get();
112 NSObject<X11Window> *CocoaPrivate::GetWindow(Window_t windowID)
const
114 const_drawable_iterator winIter = fDrawables.find(windowID);
115 #ifdef DEBUG_ROOT_COCOA
116 if (winIter == fDrawables.end()) {
117 NSLog(
@"Fatal error: requested non-existing drawable %lu", windowID);
119 std::vector<Drawable_t>::const_iterator deletedDrawable = std::find(fFreeDrawableIDs.begin(), fFreeDrawableIDs.end(), windowID);
120 if (deletedDrawable != fFreeDrawableIDs.end()) {
121 NSLog(
@"This window was deleted already");
123 NSLog(
@"This window not found among allocated/deleted drawables");
127 assert(winIter != fDrawables.end() &&
"GetWindow, non-existing window requested");
128 return (NSObject<X11Window> *)winIter->second.Get();
132 void CocoaPrivate::DeleteDrawable(Drawable_t drawableID)
134 drawable_iterator drawableIter = fDrawables.find(drawableID);
135 assert(drawableIter != fDrawables.end() &&
"DeleteDrawable, non existing drawableID");
137 NSObject<X11Drawable> *
const base = drawableIter->second.Get();
138 if ([base isKindOfClass : [QuartzView
class]]) {
139 [(QuartzView *)base removeFromSuperview];
140 ((QuartzView *)base).fParentView = nil;
141 }
else if ([base isKindOfClass : [QuartzWindow
class]]) {
142 QuartzWindow *qw = (QuartzWindow *)base;
143 qw.fContentView.fParentView = nil;
144 [qw.fContentView removeFromSuperview];
145 qw.contentView = nil;
148 if (qw.fMainWindow) {
149 [qw.fMainWindow removeChildWindow : qw];
150 qw.fMainWindow = nil;
156 fDrawables.erase(drawableIter);
160 Handle_t CocoaPrivate::RegisterGLContext(NSOpenGLContext *glContext)
162 assert(fGLContextToHandle.find(glContext) == fGLContextToHandle.end() &&
"RegisterGLContext, context was registered already");
166 bool contextInserted =
false;
168 fHandleToGLContext[fFreeGLContextID] = glContext;
169 contextInserted =
true;
170 fGLContextToHandle[glContext] = fFreeGLContextID;
171 }
catch (
const std::exception &) {
173 fHandleToGLContext.erase(fHandleToGLContext.find(fFreeGLContextID));
177 return fFreeGLContextID++;
181 void CocoaPrivate::DeleteGLContext(Handle_t contextID)
183 assert(fHandleToGLContext.find(contextID) != fHandleToGLContext.end() &&
"DeleteGLContext, bad context id");
185 handle2ctx_map::iterator h2cIt = fHandleToGLContext.find(contextID);
187 ctx2handle_map::iterator c2hIt = fGLContextToHandle.find(h2cIt->second.Get());
188 assert(c2hIt != fGLContextToHandle.end() &&
"DeleteGLContext, inconsistent context map");
190 fGLContextToHandle.erase(c2hIt);
191 fHandleToGLContext.erase(h2cIt);
195 NSOpenGLContext *CocoaPrivate::GetGLContextForHandle(Handle_t ctxID)
197 if (fHandleToGLContext.find(ctxID) == fHandleToGLContext.end())
200 return fHandleToGLContext[ctxID].Get();
204 Handle_t CocoaPrivate::GetHandleForGLContext(NSOpenGLContext *glContext)
206 if (fGLContextToHandle.find(glContext) == fGLContextToHandle.end())
209 return fGLContextToHandle[glContext];
213 void CocoaPrivate::SetFakeGLWindow(QuartzWindow *fakeWin)
215 fFakeGLWindow.Reset(fakeWin);
219 QuartzWindow *CocoaPrivate::GetFakeGLWindow()
221 return fFakeGLWindow.Get();
225 void CocoaPrivate::ReplaceDrawable(Drawable_t drawableID, NSObject *nsObj)
227 drawable_iterator drawableIter = fDrawables.find(drawableID);
228 assert(drawableIter != fDrawables.end() &&
"ReplaceDrawable, can not replace non existing drawable");
229 drawableIter->second.Reset(nsObj);