Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
CocoaPrivate.mm
Go to the documentation of this file.
1// @(#)root/graf2d:$Id$
2// Author: Timur Pocheptsov 29/11/2011
3
4/*************************************************************************
5 * Copyright (C) 1995-2012, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#define DEBUG_ROOT_COCOA
13
14//#define NDEBUG
15
16#ifdef DEBUG_ROOT_COCOA
17#include <algorithm>
18#endif
19
20#include <stdexcept>
21#include <cassert>
22
23#include <OpenGL/OpenGL.h>
24#include <Cocoa/Cocoa.h>
25
27#include "ROOTOpenGLView.h"
28#include "CocoaPrivate.h"
29#include "QuartzWindow.h"
30#include "CocoaUtils.h"
31
32namespace ROOT {
33namespace MacOSX {
34namespace Details {
35
36//______________________________________________________________________________
38 : fCurrentDrawableID(GetRootWindowID() + 1), //Any real window has id > rootID.
39 //0 is also used by some X11 functions as None.
40 fFreeGLContextID(1),
41 fApplicationDelegate([[ROOTApplicationDelegate alloc] init])
42{
43 //Init NSApplication, if it was not done yet.
45 [NSApplication sharedApplication];
46}
47
48//______________________________________________________________________________
50{
51}
52
53//______________________________________________________________________________
55{
56 //First I had root ID == 0, but this is None in X11 and
57 //it can be used by ROOT, for example, I had trouble with
58 //gClient able to found TGWindow for None - crash!
59 return 1;
60}
61
62//______________________________________________________________________________
63bool CocoaPrivate::IsRootWindow(Window_t windowID)const
64{
65 return windowID == GetRootWindowID();
66}
67
68//______________________________________________________________________________
70{
71 //Return integer identifier for a new "drawable" (like in X11)
72
73 if (fCurrentDrawableID == 999)//I have to skip this, many thanks to ROOT who uses 999 as "all windows".
75
77
78 if (fFreeDrawableIDs.size()) {
79 newID = fFreeDrawableIDs.back();
80 fFreeDrawableIDs.pop_back();
81 } else
83
84 assert(fDrawables.find(newID) == fDrawables.end() && "RegisterDrawable, id for new drawable is still in use");
85 fDrawables[newID] = nsObj;
86
87 return newID;
88}
89
90//______________________________________________________________________________
91bool CocoaPrivate::HasDrawable(Drawable_t drawableID)const
92{
93 return fDrawables.find(drawableID) != fDrawables.end();
94}
95
96//______________________________________________________________________________
98{
99 const_drawable_iterator drawableIter = fDrawables.find(drawableID);
100
101 if (drawableIter == fDrawables.end()) {
102 NSLog(@"Fatal error: requested drawable %lu is not found among currently valid drawables.", drawableID);
103 /*
104 //We do not care about efficiency, ROOT's gonna die on assert :)
105 std::vector<Drawable_t>::const_iterator deletedDrawable = std::find(fFreeDrawableIDs.begin(), fFreeDrawableIDs.end(), drawableID);
106 if (deletedDrawable != fFreeDrawableIDs.end()) {
107 NSLog(@"This drawable was deleted already");
108 } else {
109 NSLog(@"This drawable not found among allocated/deleted drawables");
110 }
111 */
112 return nil;
113 }
114 return drawableIter->second.Get();
115}
116
117//______________________________________________________________________________
119{
120 const_drawable_iterator winIter = fDrawables.find(windowID);
121#ifdef DEBUG_ROOT_COCOA
122 if (winIter == fDrawables.end()) {
123 NSLog(@"Fatal error: requested non-existing drawable %lu", windowID);
124 //We do not care about efficiency, ROOT's gonna die on assert :)
125 std::vector<Drawable_t>::const_iterator deletedDrawable = std::find(fFreeDrawableIDs.begin(), fFreeDrawableIDs.end(), windowID);
126 if (deletedDrawable != fFreeDrawableIDs.end()) {
127 NSLog(@"This window was deleted already");
128 } else {
129 NSLog(@"This window not found among allocated/deleted drawables");
130 }
131 return 0;
132 }
133#endif
134 assert(winIter != fDrawables.end() && "GetWindow, non-existing window requested");
135 return (NSObject<X11Window> *)winIter->second.Get();
136}
137
138//______________________________________________________________________________
140{
141 drawable_iterator drawableIter = fDrawables.find(drawableID);
142 assert(drawableIter != fDrawables.end() && "DeleteDrawable, non existing drawableID");
143
144 NSObject<X11Drawable> * const base = drawableIter->second.Get();
145 if ([base isKindOfClass : [QuartzView class]]) {
146 [(QuartzView *)base removeFromSuperview];
147 ((QuartzView *)base).fParentView = nil;
148 } else if ([base isKindOfClass : [QuartzWindow class]]) {
149 QuartzWindow *qw = (QuartzWindow *)base;
150 qw.fContentView.fParentView = nil;
151 [qw.fContentView removeFromSuperview];
152 qw.contentView = nil;
153 qw.fIsDeleted = YES;
154
155 if (qw.fMainWindow) {
156 [qw.fMainWindow removeChildWindow : qw];
157 qw.fMainWindow = nil;
158 }
159
160 [qw orderOut:nil];
161 }
162
163 fDrawables.erase(drawableIter);//StrongReference should do work here.
164}
165
166//______________________________________________________________________________
167Handle_t CocoaPrivate::RegisterGLContext(NSOpenGLContext *glContext)
168{
169 assert(fGLContextToHandle.find(glContext) == fGLContextToHandle.end() && "RegisterGLContext, context was registered already");
170
171 //Strong es-guarantee guarantee - if we have an exception, everything is rolled-back.
172
173 bool contextInserted = false;
174 try {
176 contextInserted = true;
178 } catch (const std::exception &) {//bad alloc in one of two insertions.
179 if (contextInserted)
181 throw;
182 }
183
184 return fFreeGLContextID++;
185}
186
187//______________________________________________________________________________
189{
190 assert(fHandleToGLContext.find(contextID) != fHandleToGLContext.end() && "DeleteGLContext, bad context id");
191
192 handle2ctx_map::iterator h2cIt = fHandleToGLContext.find(contextID);
193
194 ctx2handle_map::iterator c2hIt = fGLContextToHandle.find(h2cIt->second.Get());
195 assert(c2hIt != fGLContextToHandle.end() && "DeleteGLContext, inconsistent context map");
196
197 fGLContextToHandle.erase(c2hIt);
198 fHandleToGLContext.erase(h2cIt);//RAII does work here.
199}
200
201//______________________________________________________________________________
202NSOpenGLContext *CocoaPrivate::GetGLContextForHandle(Handle_t ctxID)
203{
204 if (fHandleToGLContext.find(ctxID) == fHandleToGLContext.end())
205 return nil;
206
207 return fHandleToGLContext[ctxID].Get();
208}
209
210//______________________________________________________________________________
211Handle_t CocoaPrivate::GetHandleForGLContext(NSOpenGLContext *glContext)
212{
213 if (fGLContextToHandle.find(glContext) == fGLContextToHandle.end())
214 return Handle_t();
215
216 return fGLContextToHandle[glContext];
217}
218
219//______________________________________________________________________________
221{
222 fFakeGLWindow.Reset(fakeWin);
223}
224
225//______________________________________________________________________________
227{
228 return fFakeGLWindow.Get();
229}
230
231//______________________________________________________________________________
233{
234 drawable_iterator drawableIter = fDrawables.find(drawableID);
235 assert(drawableIter != fDrawables.end() && "ReplaceDrawable, can not replace non existing drawable");
236 drawableIter->second.Reset(nsObj);
237}
238
239}//Details
240}//MacOSX
241}//ROOT
Handle_t Window_t
Window handle.
Definition GuiTypes.h:29
Handle_t Drawable_t
Drawable handle.
Definition GuiTypes.h:32
ULongptr_t Handle_t
Generic resource handle.
Definition GuiTypes.h:26
void SetFakeGLWindow(QuartzWindow *fakeWin)
Drawable_t RegisterDrawable(NSObject *nsObj)
void DeleteDrawable(Drawable_t drawableID)
Handle_t GetHandleForGLContext(NSOpenGLContext *glContext)
std::map< unsigned, Util::NSStrongReference< NSObject< X11Drawable > > >::iterator drawable_iterator
Util::NSStrongReference< QuartzWindow > fFakeGLWindow
NSObject< X11Drawable > * GetDrawable(Drawable_t drawableD) const
std::map< unsigned, Util::NSStrongReference< NSObject< X11Drawable > > >::const_iterator const_drawable_iterator
void DeleteGLContext(Handle_t contextID)
std::vector< Drawable_t > fFreeDrawableIDs
Handle_t RegisterGLContext(NSOpenGLContext *glContext)
NSObject< X11Window > * GetWindow(Window_t windowID) const
std::map< unsigned, Util::NSStrongReference< NSObject< X11Drawable > > > fDrawables
void ReplaceDrawable(Drawable_t drawableID, NSObject *nsObj)
bool HasDrawable(Drawable_t drawableID) const
NSOpenGLContext * GetGLContextForHandle(Handle_t contextID)
bool IsRootWindow(Window_t windowID) const
QuartzView * fParentView
QuartzWindow * fMainWindow
QuartzView * fContentView