/* 
 NSX11GraphicsContext.h
 
 X11 Backend Graphics Context class.  Conceptually, instances of 
 this subclass encapsulate a single X Window object
  
 Author:	H. N. Schaller <hns@computer.org>
 Date:	Jan 2006 - completely reworked

 This file is part of the mySTEP Library and is provided
 under the terms of the GNU Library General Public License.
 */ 

#ifndef _mySTEP_H_NSX11GraphicsContext
#define _mySTEP_H_NSX11GraphicsContext

#include <X11/keysym.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/cursorfont.h>
#define BOOL XWINDOWSBOOL							// prevent X windows BOOL
#include <X11/Xmd.h>								// warning
#undef BOOL
#include <X11/extensions/shape.h>

#include <ft2build.h>
#include FT_FREETYPE_H

#if 0 // RENDER
#include <X11/extensions/Xrender.h>
#include <X11/Xft.h>
#endif

#define Cursor QDCursor		// conflicting between QuickDraw and X11
#import <Foundation/Foundation.h>
#undef Cursor

#import "NSAppKitPrivate.h"
#import "NSBackendPrivate.h"
#import "NSColor.h"
#import "NSFont.h"
#import "NSScreen.h"

typedef struct _NSX11GraphicsState
{ // saved graphics state
	_NSGraphicsState _super;		// inherited
	NSAffineTransform *_ctm;		// current transformation matrix (transforms directly to X11)
	GC _gc;							// current context (line width, foreground color, fill rule, font etc.)
	Region _clip;					// current clipping path
	XFontStruct _font;				// current font
	// NSShadow *_shadow;			// current shadow
	// rendering intent;
	// default font name&size; (if not specified otherwise)
	// float _globalAlpha;
	// ?image interpolation quality
	// ?antialiasing
	// ?compositing operation
} _NSX11GraphicsState;

@interface _NSX11GraphicsContext : NSGraphicsContext
{ // describes one output window
	NSPoint _cursor;					// current text drawing position
	float _indent;						// position of left margin for newline
@public
	NSRect _windowRect;					// window in NSScreen coords (required to flip window coordinates and to clip composite operations)
#define _window ((Window) _graphicsPort)					// our X11 window
	Window _frontWindow;				// may be the same or different from _window for double buffered windows
	XRectangle _dirty;					// dirty area
#define _setDirty(rect)					// update dirty area
#define _isDoubleBuffered (_window != _frontWindow)
#define _state ((_NSX11GraphicsState *) _graphicsState)		// our graphics state
#define _screen _backendPrivate1							// the Screen *
	Screen *_backendPrivate1;			// screen pointer - NSScreen defines a macro to access this as _screen
	NSScreen *_nsscreen;
	float _fraction;					// compositing fraction
	int _windowNum;						// window number
	BOOL hasRender;						// screen supports render extension
}

- (id) _initWithGraphicsPort:(void *) port;
- (id) _initWithAttributes:(NSDictionary *) attributes;

@end

@interface NSScreen (_NSX11GraphicsContext)
#define _screen _backendPrivate1		// the Screen *
#define _screen2X11 _backendPrivate2	// screen transformation matrix (NSAffineTransform *)
- (NSAffineTransform *) _X112screen;
@end

@interface NSColor (_NSX11GraphicsContext)
#define _colorData _backendPrivate
- (unsigned long) _pixelForScreen:(Screen *) scr;
@end

@interface NSFont (_NSX11GraphicsContext)
#define _fontStruct _backendPrivate
- (XFontStruct *) _font;
@end

@interface NSCursor (_NSX11GraphicsContext)
#define _cursorData _backendPrivate
- (Cursor) _cursor;
@end

#endif /* _mySTEP_H_NSX11GraphicsContext */
