GHOST_WindowCarbon::GHOST_WindowCarbon(
	const STR_String& title,
	GHOST_TInt32 left,
	GHOST_TInt32 top,
	GHOST_TUns32 width,
	GHOST_TUns32 height,
	GHOST_TWindowState state,
	GHOST_TDrawingContextType type,
	const bool stereoVisual,
	const GHOST_TUns16 numOfAASamples
) :
	GHOST_Window(width, height, state, GHOST_kDrawingContextTypeNone),
	m_windowRef(0),
	m_grafPtr(0),
	m_aglCtx(0),
	m_customCursor(0),
	m_fullScreenDirty(false)
{
    Str255 title255;
	OSStatus err;
	
	//fprintf(stderr," main screen top %i left %i height %i width %i\n", top, left, height, width);
	
	if (state >= GHOST_kWindowState8Normal ) {
		if(state == GHOST_kWindowState8Normal) state= GHOST_kWindowStateNormal;
		else if(state == GHOST_kWindowState8Maximized) state= GHOST_kWindowStateMaximized;
		else if(state == GHOST_kWindowState8Minimized) state= GHOST_kWindowStateMinimized;
		else if(state == GHOST_kWindowState8FullScreen) state= GHOST_kWindowStateFullScreen;
		
		// state = state - 8;	this was the simple version of above code, doesnt work in gcc 4.0
		
		setMac_windowState(1);
	} else 
		setMac_windowState(0);

	if (state != GHOST_kWindowStateFullScreen) {
        Rect bnds = { top, left, top+height, left+width };
        // Boolean visible = (state == GHOST_kWindowStateNormal) || (state == GHOST_kWindowStateMaximized); /*unused*/
        gen2mac(title, title255);
        
		err =  ::CreateNewWindow( kDocumentWindowClass,
								 kWindowStandardDocumentAttributes+kWindowLiveResizeAttribute,
								 &bnds,
								 &m_windowRef);
		
		if ( err != noErr) {
			fprintf(stderr," error creating window %i \n",(int)err);
		} else {
			
			::SetWRefCon(m_windowRef,(SInt32)this);
			setTitle(title);
			err = InstallWindowEventHandler (m_windowRef, myWEventHandlerProc, GetEventTypeCount(kWEvents), kWEvents,NULL,NULL); 
			if ( err != noErr) {
				fprintf(stderr," error creating handler %i \n",(int)err);
			} else {
				//	::TransitionWindow (m_windowRef,kWindowZoomTransitionEffect,kWindowShowTransitionAction,NULL);
				::ShowWindow(m_windowRef);
				::MoveWindow (m_windowRef, left, top,true);
				
			}
		}
        if (m_windowRef) {
            m_grafPtr = ::GetWindowPort(m_windowRef);
            setDrawingContextType(type);
            updateDrawingContext();
            activateDrawingContext();
        }
		if(ugly_hack==NULL) {
			ugly_hack= m_windowRef;
			// when started from commandline, window remains in the back... also for play anim
			ProcessSerialNumber psn;
			GetCurrentProcess(&psn);
			SetFrontProcess(&psn);
		}
    }
    else {
    /*
        Rect bnds = { top, left, top+height, left+width };
        gen2mac("", title255);
        m_windowRef = ::NewCWindow(
            nil,							// Storage 
            &bnds,							// Bounding rectangle of the window
            title255,						// Title of the window
            0,								// Window initially visible
            plainDBox, 						// procID
            (WindowRef)-1L,					// Put window before all other windows
            0,								// Window has minimize box
            (SInt32)this);					// Store a pointer to the class in the refCon
    */
        //GHOST_PRINT("GHOST_WindowCarbon::GHOST_WindowCarbon(): creating full-screen OpenGL context\n");
        setDrawingContextType(GHOST_kDrawingContextTypeOpenGL);;installDrawingContext(GHOST_kDrawingContextTypeOpenGL);
        updateDrawingContext();
        activateDrawingContext();        

	m_tablet.Active = GHOST_kTabletModeNone;
    }
}
void ofxFenster::setActive() {
	ofxFensterManager::get()->setActiveWindow(this);
	activateDrawingContext();
}