//================================================================================
// Xe UI toolkit
//
// Main private header file
//
// AUTHOR:  Gabor Nagy
// DATE:    2026-Jun-17 17:43:19
//
// Copyright (C) 1996-2026 by Gabor Nagy. All rights reserved.
//===============================================================================
#ifndef _XeP_h
#define _XeP_h

#include <Xe/XeStrDefs.h>

#include <Xe/Xe.h>

#if defined (__cplusplus) && (!(defined (__CPLUSLIBS)))
extern "C" {
#endif


#define XeTRANSPARENTBG_ALPHA_IS_TRANSPARENCY  1


#define XeObjectCore\
	EObjectCore(XeWidget);\
	XeWidget        Self;              /* Pointer to the Widget itself, for sanity checks */\
	XeWidget        Parent;            /* Parent Widget                             */\
	XeCallbackList  DestroyCallbacks;  /* Who to call when the Widget is destroyed  */\
	EPointer        Constraints;       /* Constraint record                         */\
	XeApp           App

#define XeObjectClassCore\
	ECoreClassCore(XeWidget);\
	XeInitFunc         Init;                              /* Constructor                         */\
	XeWidgetFunc       Free;                              /* Destructor                          */\
	XeObjectClassFunc  ClassPartInit;                     /* Dynamic Class initialization        */\
	XeObjectClassFunc  ClassPartFree;                     /* Dynamic Class freeing               */\
	uint32_t           ClassInitedFlags : 16,             /* 0, if class has not been initialized, XeShell_ClassFlag etc., when it has  */\
	                   ClassDepth : 8;                    /* How far this Class is from XeObjectClass */\
	XeResource*        WResources;Cardinal  WNResources;  /* Resources for subclass fields       */\
	EIndex             Instances_Count;                   /* Number of instances of this class and its sub-classes  */\
	XeSetValuesFunc    SetValues;                         /* Set subclass resource values        */\
	EArgsFuncB         SetValuesHook;                     /* Notify that SetValues called        */\
	EArgsFunc          GetValuesHook;                     /* Notify that get_values called       */\
	EPointer           CallbackPrivate                    /* List of callback offsets            */

typedef struct _XeEventRec*  XeEventTable;

typedef void    (*XeResourceDefaultFunc)(XeWidget PW, int POffset, XeSizeAddr* PValue);
typedef void    (*XeCreatePopupChildFunc)(XeWidget PW);

typedef struct
{
	String   Name;   // Resource name
	String   Type;   // Representation type name
	EArgVal  Value;  // Representation
	int      Size;   // Size of representation
} XeTypedArg;


typedef struct _XeState_S  _XeState;

typedef struct
{
	XeTranslations  Translations;      // Private to the Translation Manager
	XeActionFunc*   FuncTable;         // Procedure bindings for actions
	_XeState*       CurrentState;      // Translation Manager state
	ETime           LastEventTime_us;  // Microseconds
} XeTMData;


#ifdef DEBUG
#define Xe_CheckSubclass(pW, pWidgetClassPtr, pMessage) do\
{\
	if(!XeIsSubclass(((XeWidget)(pW)), pWidgetClass))\
	{\
		const char*  lParams[3];int  lNParams = 3;\
		lParams[0] = Xe_Class((XeWidget)(pW))->Name;\
		lParams[1] = (pWidgetClass)->Name;\
		lParams[2] = (pMessage);\
		XeAppError((pW)->App, "subclassMismatch", __FUNCTION__, "Widget class %s found when subclass of %s expected: %s",  lParams, lNParams);\
	}
} while(0)
#else
#define Xe_CheckSubclass(pW, pWidgetClass, pMessage) do {} while(0)
#endif


extern void  _XeH_HandleFocus(XeWidget PW, XeEvent* PEvent, EPointer PClientData, EBool* PContRet);


typedef struct _XeObjectRec
{
	XeObjectCore;
} XeObjectRec;


typedef struct XeObjectClass_S  XeObjectClass;
struct XeObjectClass_S
{
	XeObjectClassCore;
};


externalref XeObjectClass  _XeOject_class;
extern XeObjectClass*       XeWidget_class;

externalref int  _XeInheritTranslations;

#define XeInheritTranslations     ((String)&_XeInheritTranslations)


// Keyboard focus
//
#define XeMY_ANCESTOR    0
#define XeMY_DESCENDANT  1
#define XeMY_COUSIN      2
#define XeMYSELF         3
#define XeUNRELATED      4
typedef char  XeGeneology;

typedef struct XeServerGrab_S  XeServerGrab;

struct XeServerGrab_S
{
	XeServerGrab*  Next;
	XeWidget       Widget;
	uint32_t       OwnerEvents:1, PointerMode:1, KeyboardMode:1, HasExt:1, ConfineToIsWidgetWin:1;
	uint32_t       KeyBut;       // Key or button code
	uint32_t       Modifiers;    // Modifiers for key or button
	uint32_t       EventMask;
};



// OS-dependent file info for VirtKeys
//
#define XMBINDDIR "XMBINDDIR"
#ifndef XMBINDDIR_FALLBACK
#define XMBINDDIR_FALLBACK "/usr/lib/Xm/bindings"
#endif
#define XMBINDFILE "xmbind.alias"
#define MOTIFBIND ".motifbind"


typedef struct XeExtensionClassRec_S  XeExtensionClassRec;    // Forward-define XeExtensionClass

// Record for registering callbacks in the class record of an XeDWidget
//
typedef struct
{
	char*         Name;
	unsigned int  Offset;  // Offset in the Object's instance record
} XeCallbackI;


#define XeBaseClassCore(pType)\
	pType##Class  SuperClass;\
	char*         Name;\
	unsigned int  StructSize

extern void  XeCallCallbacks(XeWidget PW, EguiCallbackList* PCBList, EPointer PCallData);

typedef EPointer  XeCacheRef;

extern void XeCallbackReleaseCacheRef(XeWidget PW,
    EPointer PClientData,  /* XeCacheRef */
    EPointer PCallData);

extern void XeCallbackReleaseCacheRefList(XeWidget PW, EPointer PClientData,  /* XeCacheRef* */
    EPointer PCallData);


struct XeExtensionClassRec_S
{
	XeBaseClassCore(XeExtension);
	EIndex  Instances_Count;
	void  (*Init)(XeWidget PW, XeExtension* PExtension);
	int   (*Free)(XeWidget PW, XeExtension* PExtension);
	EList(, XeResource, WResources);        // Resources of the XeExtension object
	EList(, XeCallbackI, CallbackOffsets);  // Offsets of Callback lists, in the XeExtension object
};



#define XeRectObjCore\
	XeObjectCore;\
	XeCoord2        Position;               /* Rectangle position                          */\
	XeCoord2        Size;                   /* Rectangle dimensions                        */\
	XeCoordinate    BorderWidth;            /* Rectangle border width                      */\
	XeCoord2        PreferredSize;\
	XeCoord2        GivenSize;              /* Size specified by the app, via XeNSizeX.XeNSizeY*/\
	EBool           Managed;                /* Is widget geometry managed?                 */\
	EBool           Sensitive;              /* Is widget sensitive to user events          */\
	EBool           AncestorSensitive;      /* are all ancestors sensitive?                */\
/* The QueryGeometry method of this Widget takes a suggested geometry into consideration, when computing its preferred geometry (e.g.: XeScrolledWindow). */\
	EBool           IteratedQueryGeometry;\
	XeChangeFlags   ChangeFlags             /* Set by SetValues methods. XeChgREDRAW etc.  */


//
// RectObject
//
typedef struct XeRectObj_S
{
	XeRectObjCore;
} XeRectObjRec;


#ifdef XeOpenGL
#define XeRectObjectClassOpenGL\
  XeGL_RedrawFunc     GL_Redraw              /* Redraw the Widget with OpenGL         */
#else
#define XeRectObjectClassOpenGL
#endif



#define XeRectObjectClassCore\
	XeObjectClassCore;\
	XeWidgetFunc       Resize;                 /* Called when geometry management changed the Widget size  */\
	XeAlmostFunc       SetValuesAlmost;        /* Set values almost for geometry        */\
	XeGeometryHandler  QueryGeometry;          /* Return preferred geometry             */\
  XeRedrawFunc       Redraw                  /* Redraw method                         */


//
// RectObject class
//
typedef struct
{
	XeRectObjectClassCore;
} XeRectObjClass;

externalref XeRectObjClass  _XeRectObj_class;



#include <Xe/CoreP.h>

//--------------------------------------------------------------------------------------------------------
// "Deferred" Widgets, added from plugins etc. e.g. before the app's top-level UI is created
//--------------------------------------------------------------------------------------------------------

// Class record
//
#define XeDWidgetClassCore\
	char*           Name;\
	XeDWidgetClass  SuperClass;\
	unsigned int    InstanceSize;\
\
	XeCallbackI*    CallbackOffsets;EIndex  NumOfCallbacks;\
\
	void            (*Destroy)(XeDWidget PDW);\
\
	XeCallbackFunc  XeCBH  /* Callback handler for Xe */


typedef struct
{
	XeDWidgetClassCore;
} XeDWidgetClassRec;

externalref XeDWidgetClassRec  _XeDWidget_class;

// Core instance record
//
#define XeDeferredWidget_Core\
	EEntityCore;\
	XeDWidget           Parent;\
	char*               Location;  /* This is for the application to know where to put an item (on a menu, a panel etc.) */\
	XeDWidgetClassRec*  Class;\
	XeWidget            Widget;\
	EPointer            UserData

typedef struct _XeDCore
{
	XeDeferredWidget_Core;
} XeDCore;



//-----------------------------
// Class record
//-----------------------------
typedef struct
{
  void  (*AddChild)(XeDWidget PItem, const char* PName,  EArg* PArgs, const int PArgCnt);
} XeDCompositeClassPart;


typedef struct
{
	XeDWidgetClassCore;
	XeDCompositeClassPart  Composite;
} XeDCompositeClassRec;


//-----------------------------
// Instance
//-----------------------------
typedef struct
{
	EList(, XeDWidget, Children);
} XeDCompositePart;

typedef struct
{
	XeDWidgetClassCore;
	XeDCompositePart  Composite;
} XeDCompositeRec;




//========================================
// List with optional buttons
//========================================

typedef struct
{
	XeDWidgetClassCore;
	XeDCompositeClassPart  Composite;
// ListClassRec is empty
} XeDListClass;


typedef struct
{
	XeWidget          TopMatrixW, ListW, ButtonMatrixW;
	EguiCallbackList  SelectionCallback;
} XeDListPart;


typedef struct
{
	XeDeferredWidget_Core;
	XeDCompositePart  Composite;
	XeDListPart       List;
} XeDList;


extern XeDWidget  _XeDeferredWidget_New(const char* PName, XeDWidgetClass PClass, XeDWidget PParent, XeWidget PParentW);
extern void       _XeDeferredWidget_Register(XeApp PApp, XeDWidget PItem);

typedef struct
{
	const XeEvent*  Event;
} XeGlobalCallbackStruct;


typedef struct 
{
#ifndef USE_POLL
	fd_set  RMask;
	fd_set  WMask;
	fd_set  EMask;
#endif
	int     NFDs;
} _XeFDs;


typedef struct _XeConverter_S**  XeConverterTable;

typedef String  (*XeLanguageFunc)(XeDisplay* PDisplay, String PXNL, EPointer PClientData);

typedef struct _XeLangFuncRec
{
	XeLanguageFunc  Func;
	EPointer        ClientData;
} XeLangFunc;


typedef struct _ProcessContextRec
{
	XeApp             DefaultApp;
	EList(, XeApp, Apps);
	XeConverterTable  GlobalConverterTable;
	XeLangFunc        GlobalLangFunc;
} XeProcess;


typedef struct internalCallbackRec
{
	unsigned short  Count;
	char            IsPadded;     // Contains NULL padding for external form
	char            CallState;    // Combination of _XeCB{FreeAfter}Calling
#ifdef LONG64
	unsigned int    AlignPad;     // padding to align callback list
#endif
} _XeInternalCallback, *XeIntCallbackList;

typedef struct _XeSignalEvent
{
	struct _XeSignalEvent*  Next;
	XeApp                   App;
	EBool                   Notice;
} XeSignalEvent;

typedef unsigned long  XeInputMask;
#define XeInputNoneMask		0L
#define XeInputReadMask		(1L<<0)
#define XeInputWriteMask	(1L<<1)
#define XeInputExceptMask	(1L<<2)

typedef struct XeInputEvent_S  XeInputEvent;

struct XeInputEvent_S
{
	XeInputCallbackFunc  Func;
	EPointer             Closure;
	XeInputEvent*        Next;
	XeInputEvent*        OQ;
	XeApp                App;
	int                  Source;
	XeInputMask          Condition;
};


typedef struct XeActionHookRec_S   XeActionHookRec;
typedef struct _XeActionListRec_S  _XeActionListRec;

typedef struct _TMBindCache_S      _TMBindCache;

struct XeApp_S
{
	XeAppCore;

	uint16_t                  Alive : 1, BeingFreed : 1;

	XeProcess*                Process;

	EListA(, XeWidget, Shells);                               // All Shell Widgets created in this App (needed, for changing Fonts, Colors etc.)
	EListA(, XeDisplay*, Displays);
	EListA(, XeDisplay*, DisplayDestroyList);

	XeWidget                  WidgetUnderPhase2Free;
	int                       FreeCount;
	_XeFreeRec*               FreeList;int  FreeListSize;   // State data for 2-phase Widget freeing
	int                       DispatchLevel;

	unsigned long             SelectionTimeout;

	XeErrorMessageHandler     MessageFunc;
	XeErrorHandler            ErrorHandler;

	XeConverterTable          ConverterTable;
	_XeActionListRec*         ActionTable;struct _XeActionListRec*  ActionTableNext;
	XeActionHookRec*          ActionHookList;
	_TMBindCache*             FreeBindings;
	XeLangFunc                LangFunc;

	XeCallbackFunc            GlobalCallback;
	uint32_t                  GlobalCallbackEventMask;
	XeInputEvent**            InputList;short  InputMax;
	XeInputEvent*             OutstandingQueue;
	char*                     DisplayNameTried;

	XeSignalEvent*            SignalQueue;

	EListA_s(, XeDWidget, DeferredWidgets);

	ETime                     MultiClickDelay_us;  // In microseconds
#ifdef EX11
	_XeFDs                    FDs;                 // X11 server connection file descriptors
	EBool                     RebuildFDList;
#endif

#ifdef XTHREADS
	LockPtr                     LockInfo;
	XeThreadAppFunc             Lock;
	XeThreadAppFunc             Unlock;
	XeThreadAppYieldLockFunc    YieldLock;
	XeThreadAppRestoreLockFunc  RestoreLock;
	XeThreadAppFunc             FreeLock;
#endif
};


typedef void (*XeExtensionSelectProc)(XeWidget PW, int* PEventTypes, EPointer* PSelectData, int PCount, EPointer PClientData);

typedef struct _ExtensionSelectorRec
{
	XeExtensionSelectProc  Func;
	int                    Min, Max;
	EPointer               ClientData;
} XeExtSelectRec;


//================================================================================
// Reference-tracking correction for Images (Xe doesn't know about RefCnts etc.)
//================================================================================
#define _XeM_ImageChangeCheck(pStruct, pField)\
{\
	EpImage*  lOld = pStruct##Old->pField;\
	EpImage*  lNew = pStruct->pField;\
\
	if(lNew != lOld)\
	{\
/* A little trickery to make the reference-tracker work correctly	*/\
\
/* Restore pointer to old Object before Assign	*/\
		pStruct->pField = pStruct##Old->pField;\
		E_AssignToMember(lNew, pStruct,pField);\
\
		LChangeFlags |= XeChgREDRAW;\
		const EIndex  lOldSizeX = lOld ? lOld->Size.X : 0, lOldSizeY = lOld ? lOld->Size.Y : 0;\
		const EIndex  lNewSizeX = lNew ? lNew->Size.X : 0, lNewSizeY = lOld ? lNew->Size.Y : 0;\
		if(lOldSizeX != lNewSizeX || lNewSizeY != lOldSizeY) LChangeFlags |= XeChgSIZE;\
	}\
}


#define _DrawDefaultButtonShadow(pBackgroundColor) do\
{\
	if(LDefBShadowThickness > 0)\
	{\
		if(!LClearBackground)  /* If ClearBackground is TRUE, we don't need to draw the spacing and the corners were already rounded by _XeCore_DrawBackgroundAndGetBlendMode(). */\
		{\
/* Draw spacing between the default button shadow and the button */\
/*			Ep_DrawRectangleRGBA8(LCacheImage, Ec_RGBAiToRGBA8(pBackgroundColor),  LDefBS1, LDefBS1,  LSizeX - LDefBS1 * 2, LSizeY - LDefBS1 * 2,  LDefBSS);*/\
/*\
			const XeCoordinate  lRadius = LDefaultButtonFrameCornerRadius;\
			EpImage_RoundOffCorners(LCacheImage,  0, 0,  LSizeX, LSizeY,  lRadius, lRadius, lRadius, lRadius,  pBackgroundColor, pBackgroundColor,  EpRegionALL, 1, EpFROM_SOURCE0,  TRUE);\
*/\
		}\
\
		if(LButton->ShowAsDefault)\
		{\
			XeImage_DrawShadows(LCacheImage,  0, 0,  LSizeX, LSizeY,  0, NULL,\
			 LDefaultButtonFrameCornerRadius, NULL, LDefaultButtonFrameCornerRadius, NULL,  LDefaultButtonFrameCornerRadius, NULL, LDefaultButtonFrameCornerRadius, NULL,  TRUE,\
			 LDefBShadowThickness, XeShadowIN|LShadowSpecFlags, 0, pBackgroundColor, pBackgroundColor,  EpSRC, EpACCUMULATEOPACITY,  EpSRC, EpACCUMULATEOPACITY,  FALSE);\
		}\
	}\
} while(0)


//typedef int  privateint;

/*
#ifdef XeDEVELOPMENT
#define XeDEBUG(p)	p
#else
#define XeDEBUG(p)
#endif
*/


#define XeObject_ClassFlag            0x01
#define XeRectObj_ClassFlag           0x02
#define XeWidget_ClassFlag            0x04
#define XeCompositeWidget_ClassFlag   0x08
#define XeConstraintWidget_ClassFlag  0x10
#define XeShell_ClassFlag             0x20
#define XeWMShell_ClassFlag           0x40
#define XeTopLevelShell_ClassFlag     0x80


#define ASSIGN_MAX(x, y)	if ((y) > (x)) x = (y)


// XeScale and XeScrollbar pointer locations 
//
enum { XeOUTSIDE = 0, XeON_SCALE, XeON_SLIDER };

// NumObject and Scrollbar redraw flags
//
#define XeDO_VALUES           1   // Recompute pixel positions of slider/thermometer
#define XeDO_SCALE        (1<<1)  // Redraw scale background
#define XeDO_SLIDER       (1<<2)  // Redraw the slider
#define XeDO_THERMOMETER  (1<<3)  // Redraw the thermometer


// From Traversal.c
//
extern void	_Xe_NavigChangeManaged(XeWidget PW);


// Synthetic resource stuff
//

typedef enum
{ 
	XeSYNTHETIC_NONE, 
	XeSYNTHETIC_LOAD 
} XeImportOperator;

typedef void              (*XeExportFunc)(XeWidget PW, int, EArgVal*);
typedef XeImportOperator  (*XeImportFunc)(XeWidget PW, int, EArgVal*);


typedef struct _XeSyntheticResource
{
	String        Name;
	Cardinal      ResourceSize;
	Cardinal      ResourceOffset;
	XeExportFunc  ExportFunc;
	XeImportFunc  ImportFunc;
} XeSyntheticResource;


#define Xe_Parent(pW)           ((pW)->Parent)
#define Xe_Class(pW)            ((pW)->Class)
#define XeDestroyCallbacks(pW)  ((pW)->DestroyCallbacks)

#define Xe_SizeX(pW)  (((XeWidget)(pW))->Size.X)
#define Xe_SizeY(pW)  (((XeWidget)(pW))->Size.Y)

#define Xe_GetDisplay(pW)  ((pW)->DisplayInfo)
#define Xe_GetVisual(pW)   ((pW)->Visual)

#define XeFOCUS_RESET   1
#define XeFOCUS_IGNORE  2


//------------------------------------------------------------------------


extern char  _XeStrNormalColor[];
extern char  _XeStrRed[];
extern char  _XeStrGreen[];
extern char  _XeStrYellow[];
extern char  _XeStrPurple[];
extern char  _XeStrCyan[];

extern char*  _XeGetNormalColorCode(char* PPtr);
extern char*  _XeGetErrorColorCode(char* PPtr);
extern char*  _XeGetWarningColorCode(char* PPtr);
extern char*  _XeGetDirColorCode(char* PPtr);

extern XeTranslations  _XeScaleTextFieldTransTable;

extern void  _Xe_ImageGetScaledSize(const EIndex POrigSizeX, const EIndex POrigSizeY,  int32_t* PSizeXPtr, int32_t* PSizeYPtr,  XeCoord2* PSizeP);

extern void  XeImageDrawArrowRGBA8(EpImage* PCacheImage, const int PX, const int PY, const int PSizeX, const int PSizeY,  const int PDirection,  EcRGBAu8Color PColor);

#ifdef EX11
extern void  _XeCastShadow(XeWidget PW,  const int PRXPos, const int PRYPos,  const int PMSizeX, const int PMSizeY,
               Window* PShadowBXWindowPtr, Window* PShadowRXWindowPtr, Pixmap* PShadowBXMaskPtr,  Pixmap* PShadowXRMaskPtr, const int PShadowOffset);
#endif

extern void  _XeTraverse(XeWidget PW, XeEvent* PEvent, const char PActionCode, const EBool PSendFicysLostEvent, const EBool PSendFocusGetEvent);
extern void  _XeATraverse(XeWidget PW, XeEvent* PEvent,  String* PP, Cardinal* PNP);

extern void  _XeInstallTranslations(XeWidget PW);

extern EBool  XeToolkitThreadInitialize(void);
extern void   XeAppLock(XeApp PApp);
extern void   XeAppUnlock(XeApp PApp);

extern EBool  XeWidget_IsManagedP(XeWidget PObject);

extern void  (*_XeProcessLock)(void);
extern void  (*_XeProcessUnlock)(void);
extern void  (*_XeInitAppLock)(XeApp PApp);


extern uint32_t  XeString_Verbosity;


#ifdef XTHREADS

#define XeLOCK_PROCESS       if(_XeProcessLock) (*_XeProcessLock)()
#define XeUNLOCK_PROCESS     if(_XeProcessUnlock) (*_XeProcessUnlock)()
#define XeWIDGET_TO_APP(pW)  XeApp  LApp = (pW && _XeProcessLock ? (pW)->App : NULL)
#define XeLOCK_APP(pApp)     if(pApp && pApp->Lock)(*pApp->Lock)(pApp)
#define XeUNLOCK_APP(pApp)   if(pApp && pApp->Unlock)(*pApp->Unlock)(pApp)

#else  // ifdef XTHREADS

#define XeLOCK_PROCESS
#define XeUNLOCK_PROCESS
#define XeWIDGET_TO_APP(pW)
#define XeLOCK_APP(pApp)
#define XeUNLOCK_APP(pApp)

#endif

extern void  XeProcessLock(void);
extern void  XeProcessUnlock(void);



EListA_s(extern, EpScaledImagePtr, _Xe_ScaledImages);

extern const float  _Xe_GradientContrastDefault;

extern XeProcess  _Xe_Process;

extern int   _Xe_AppsToFree_Count;
extern void  _XeApps_Free(void);

extern void  _XePopupInitialize(XeApp PApp);
extern void  _XeDoFreeBindings(XeApp PApp);


//--------------------------------
// Menu.c
//--------------------------------
extern EBool  _XeIsInMenuPane(XeWidget PW);


//--------------------------------
// Geometry.c
//--------------------------------
extern XeGeometryResult  XeMakeGeometryRequest(XeWidget PW, XeWidgetGeometry* PDesired);
extern XeWidget          Xe_WindowedAncestorAndRelativePosition(XeWidget PW, const uint8_t PWindowTypeFlags,  int* PXP, int* PYP);

extern EBool             Xe_IsUpdateEnabled(XeWidget PW);

extern void              _Xe_CallResize(XeWidget PW,  const uint8_t PResizeFuncActions);
extern void              _Xe_SetGeometry(XeWidget PW,  const int PX, const int PY,  const int PSizeX, const int PSizeY,  const int PBorderWidth,  const uint8_t PResizeFuncActions);
extern void              Xe_SetGeometry(XeWidget PW,  const int PX, const int PY,  const int PSizeX, const int PSizeY,  const int PBorderWidth);
extern void              Xe_SetSize(XeWidget PW, const int PSizeX, const int PSizeY,  const int PBorderWidth);
extern void              Xe_SetPosition(XeWidget PW,  const int PX, const int PY);
extern XeGeometryResult  _Xe_RequestGeometry(XeWidget PW, XeWidgetGeometry* PRequest, XeWidgetGeometry* PReply, EBool* PClearRectObj);
extern char*             XeDBGeometryReplyToString(XeGeometryResult PA);


//--------------------------------
// Events.c
//--------------------------------
extern int   XeUnbufferButtonEvents(XeWidget PW, const int PButton);
extern void  _XeTimersFree(void);


extern void  XeTranslations_Free(XeTranslations PT);


typedef struct _XeEventRecExt
{
	int        type;
	EPointer  select_data[1];  // Actual dimension is [mask]
} XeEventRecExt;


extern char*  XeHomeDir(void);

extern EBool     _XeCheckSubclassFlag(XeWidget PW, const XeClassFlags PFlag);
extern EBool     _XeIsSubclassOf(XeWidget PW, XeWidgetClass* PWidgetClass, XeWidgetClass* PFlagClass, const XeClassFlags PFlag);
extern EBool     XeIsSubclass(XeWidget PW, const XeWidgetClass* PWidgetClass);
extern XeWidget  _XeWindowedAncestor(XeWidget PW);


#define Xe_SupportsWindow(p)              (XeIsWidget(p) && (p)->WindowTypeFlags != 0)
#define Xe_SupportsWindowType(p, pFlags)  (XeIsWidget(p) && ((p)->WindowTypeFlags & (pFlags)) == pFlags)
#define Xe_IsSensitive(p)                 (XeIsRectObj(p) ? ((p)->Sensitive && (p)->AncestorSensitive) : FALSE)


extern void           _XeImage_Init(XeImage* PImageRec);
extern void           _XeImage_Free(XeImage* PImageRec);
extern XeChangeFlags  _XeImage_ChangeCheck(XeImage* PImageRecOld, XeImage* PImageRec);


extern void           _Xe_SetValues(XeWidget PW,  const EArg* PArgs, const int PNArgs,  const EBool PGeoRequests, const EBool PRedraw);

// We were included from XeI.h - expose internal stuff
#ifdef _XeI_h

// These 4 macros a very handy internally, we can't expose them to applications, as they violate the rule that arguments are to be evaluated exactly once.

#define XeIsRectObj(pObject)     (_XeCheckSubclassFlag(pObject, XeRectObj_ClassFlag))
#define XeIsWidget(pObject)      (_XeCheckSubclassFlag(pObject, XeWidget_ClassFlag))
#define XeIsComposite(pW)        (_XeCheckSubclassFlag(pW, XeCompositeWidget_ClassFlag))
#define XeIsConstraint(pW)       (_XeCheckSubclassFlag(pW, XeConstraintWidget_ClassFlag))
#define XeIsShell(pW)            (_XeCheckSubclassFlag(pW, XeShell_ClassFlag))
#define XeIsOverrideShell(pW)    (_XeIsSubclassOf(pW, (XeWidgetClass*)XeOverrideShellWidget_class,    (XeWidgetClass*)XeShellWidget_class, XeShell_ClassFlag))
#define XeIsTopLevelShell(pW)    (_XeCheckSubclassFlag(pW, XeTopLevelShell_ClassFlag))
#define XeIsWMShell(pW)	         (_XeCheckSubclassFlag(pW, XeWMShell_ClassFlag))
#define XeIsVendorShell(pW)      (_XeIsSubclassOf(pW, (XeWidgetClass*)XeVendorShellWidget_class,      (XeWidgetClass*)XeWMShellWidget_class,       XeWMShell_ClassFlag))
#define XeIsTransientShell(pW)   (_XeIsSubclassOf(pW, (XeWidgetClass*)XeTransientShellWidget_class,   (XeWidgetClass*)XeWMShellWidget_class,       XeWMShell_ClassFlag))
#define XeIsApplicationShell(pW) (_XeIsSubclassOf(pW, (XeWidgetClass*)XeApplicationShellWidget_class, (XeWidgetClass*)XeTopLevelShellWidget_class, XeTopLevelShell_ClassFlag))

extern void             _Xe_ResourcesFlatten(XeObjectClass* PWC);
extern void             _XeConstraintResourcesFlatten(XeConstraintWidgetClass PWC);
extern XeCacheRef*      _XeGetResources(XeWidget PW,  EArg* PArgs, const int PNArgs);
extern void             _Xe_ResourceAddressFromParent(XeWidget PW, int POffset, XeSizeAddr* PValue);
extern void             _Xe_CopyToArg(void* PSrc, EArgVal* PDst, const unsigned int PVSize);
#else

// Non Xe-internal code should access internals through functions
//
extern EBool  XeIsRectObj(XeWidget PW);
extern EBool  XeIsWidget(XeWidget PW);
extern EBool  XeIsComposite(XeWidget PW);
extern EBool  XeIsConstraint(XeWidget PW);
extern EBool  XeIsShell(XeWidget PW);
extern EBool  XeIsOverrideShell(XeWidget PW);
extern EBool  XeIsTopLevelShell(XeWidget PW);
extern EBool  XeIsWMShell(XeWidget PW);
extern EBool  XeIsVendorShell(XeWidget PW);
extern EBool  XeIsTransientShell(XeWidget PW);
extern EBool  XeIsApplicationShell(XeWidget PW);

#endif

typedef void (*XeDestructor)(XeApp PApp, XeSizeAddr* PTo, EPointer PConverterData, XeSizeAddr* PArgs, Cardinal* PNArgs);

typedef int            XeCacheType;
#define XeCacheNone       0x001
#define XeCacheAll        0x002
#define XeCacheByDisplay  0x003
#define XeCacheRefCount   0x100


typedef enum
{
// Address mode       Meaning
//----------------------------------------
	XeAddress,          // Address
	XeBaseOffset,       // Offset
	XeImmediate,        // Constant
	XeResourceString,   // Resource name string
	XeResourceQuark,    // Resource name quark
	XeWidgetBaseOffset, // Offset from ancestor
	XeProcedureArg      // Procedure to invoke
} XeAddressMode;

typedef struct
{
	XeAddressMode  AddressMode;
	EPointer       AddressId;
	Cardinal       Size;
} XeConvertArgRec, *XeConvertArgList;


EListA_s(extern, XeObjectClassPtr, Xe_Classes);

extern ECoreClass  _XeCallbackStruct_class[];

extern const char  _XeStr_InvalidParent[];

//========================================================================
// Initialize an XeCallbackStruct with PReason, PClass and PEvent
//========================================================================
static inline void XeCallbackStruct_Init(XeCallbackStruct* PCBS, ECoreClass* PClass,  const int PReason, XeEvent* PEvent)
{
	if(PClass) ECallbackStruct_Init((ECallbackStruct*)PCBS, PClass,  PReason);
	else ECallbackStruct_Init((ECallbackStruct*)PCBS, _XeCallbackStruct_class,  PReason);
	PCBS->Event = PEvent;
}

typedef EBool (*XeTypeConverter)(XeDisplay* PDisplay, XeSizeAddr* PArgs, Cardinal* PNArgs, XeSizeAddr* PFrom, XeSizeAddr* PTo, EPointer* PConverterData);

extern void   XeSetTypeConverter(const char* PFromType, const char* PToType, XeTypeConverter PConverter,  XeConvertArgList PConvertArgs, const int PNumArgs,
               XeCacheType PCacheType, XeDestructor PDestructor);

extern EBool  XeCallConverter(XeDisplay* PDisplay, XeTypeConverter PConverter, XeSizeAddr* PArgs, Cardinal PNArgs, XeSizeAddr* PFrom, XeSizeAddr* PToInOut,
               XeCacheRef* PCacheRefRet);

extern void   XeDisplayStringConversionWarning(XeDisplay* PDisplay, const char* PFromValue, const char* PToType);

const EcRGBAf32Color* Xe_GetBackgroundColor(const EcRGBAf32Color* PColor, EcRGBAf32Color* PDstColor, const uint16_t PBlendFunction);

extern void   XeApp_SetTypeConverter(XeApp PApp, const char* PFromType, const char* PToType, XeTypeConverter PConverter,
               XeConvertArgList PConvertArgs, const int PNArgs,  XeCacheType PCacheType, XeDestructor PDestructor);


//
// Manage.c
//
extern void  _XeWidgets_Unmanage(XeWidget* PChildren, const EIndex PNChildren, const EBool PCallChangeManaged);
extern void  _XeWidget_Unmanage(XeWidget PW, const EBool PCallChangeManaged);

extern void  _XeWidgets_Manage(XeWidget* PChildren, const EIndex PNChildren, const EBool PCallChangeManaged);
extern void  _XeWidget_Manage(XeWidget PW, const EBool PCallChangeManaged);

extern void  _XeWidgets_ChangeManage(const XeChangeManage* PChildren, const EIndex PNChildren,  const EBool PCallChangeManaged);


//
// Strings
//
extern void  _XeStringMetrics_Init(XeStringMetrics* PMetrics);
extern void  _XeStringMetrics_ToSizeAndOffset(const XeStringMetrics* PMetrics, const uint8_t PGeometryMode,  XeCoord2* PTotalSizeP, XeCoord2* POffsetYP);


#if defined (__cplusplus) && (!(defined (__CPLUSLIBS)))
} // Close scope of 'extern "C"' declaration
#endif


#endif // _XeP_h
