//================================================================================
// Xe UI toolkit
//
// Strictly internal stuff
//
// AUTHOR:  Gabor Nagy
// DATE:    2026-Aug-03 01:28:23
//
// Copyright (C) 1996-2026 by Gabor Nagy. All rights reserved.
//===============================================================================
#ifndef _XeI_h
#define _XeI_h

#include <Xe/XeP.h>

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

#ifndef X_NOT_POSIX
#ifdef _POSIX_SOURCE
#include <limits.h>
#else
#define _POSIX_SOURCE
#include <limits.h>
#undef _POSIX_SOURCE
#endif
#endif
#ifndef PATH_MAX
#ifdef WIN32
#define PATH_MAX 512
#else
#include <sys/param.h>
#endif
#ifndef PATH_MAX
#ifdef MAXPATHLEN
#define PATH_MAX MAXPATHLEN
#else
#define PATH_MAX 1024
#endif
#endif
#endif


#ifdef CRAY
#define WORD64
#endif

#if defined (_LP64) || \
    defined(__alpha) || defined(__alpha__) || \
    defined(__ia64__) || defined(ia64) || \
    defined(__sparc64__) || \
    defined(__s390x__) || \
    (defined(__hppa__) && defined(__LP64__)) || \
    defined(__amd64__) || defined(amd64) || \
    defined(__powerpc64__) || \
    (defined(sgi) && (_MIPS_SZLONG == 64))
#define LONG64
#endif


#if defined(XTHREADS) && defined(XUSE_MTSAFE_API)

#define _XeDisplay_ToApp(mD)  XeApp  LApp = (mD)->App
#define _XeWidget_App(w)      XeApp  LApp = (w)->App
#define _XeAppLock(pApp)      XeAppLock(pApp)
#define _XeAppUnlock(pApp)    XeAppUnlock(pApp)
#define _XeProcessLock()      XeProcessLock()
#define _XeProcessUnlock()    XeProcessUnlock()

// If Xe provides an API to query whether it's multi-threaded, neutralize _XeProcessLock()
//
extern void  (*_XeProcessLock)(void);

#else

#define _XeDisplay_ToApp(pD)  do { } while(0)
#define _XeWidget_App(pW)     do { } while(0)
#define _XeAppLock(pApp)      do { } while(0)
#define _XeAppUnlock(pApp)    do { } while(0)
#define _XeProcessLock()      do { } while(0)
#define _XeProcessUnlock()    do { } while(0)

#endif  // XTHREADS && XUSE_MTSAFE_API


// Window-to-Widget mapping
//
typedef struct _WWPair
{
	struct _WWPair*  Next;
#ifdef EX11
	Window           X_Window;
#endif
	XeWidget         Widget;
} *WWPair;

typedef struct _XeW2WTable
{
	unsigned int  Mask;      // Size of hash table - 1
	unsigned int  Rehash;    // Mask - 2
	unsigned int  Occupied;  // Number of occupied entries
	unsigned int  Fakes;     // Number occupied by WWfake
	XeWidget*     Entries;   // The entries
	WWPair        Pairs;     // Bogus entries
} *XeW2WTable;


typedef enum
{
	XeNoServerGrab, 
	XePassiveServerGrab,
	XeActiveServerGrab,
	XePseudoPassiveServerGrab,
	XePseudoActiveServerGrab
} XeServerGrabType;


typedef struct
{
	XeServerGrab      Grab;  // Need a copy, in order to protect during grab.
	XeServerGrabType  GrabType;
} XeInputDevice;

typedef struct XeGrab_S  XeGrab;

typedef struct
{
	XeGrab*        GrabList;
	XeInputDevice  Keyboard, Pointer;
	XeKeyCode      ActivatingKey;
	XeWidget*      Trace;
	int            TraceDepth, TraceMax;
	XeWidget       FocusWidget;
} XePerDisplayInput;


typedef struct _XeEventRec
{
	XeEventTable    Next;
	XeEventMask     mask;      // Also select_data count for RecExt

	XeEventHandler	EventFunc;

	EPointer        ClientData;
	unsigned int    Select:1;
	unsigned int    HasTypeSpecifier:1;
	unsigned int    Async:1;   // Not used, here for Digital extension? 
} XeEventRec;

struct XeGrab_S
{
	XeGrab*       Next;
	XeWidget      Widget;
	unsigned int  Exclusive:1, SpringLoaded:1;
};

typedef struct
{
  Atom   Property;
  EBool  Available;
} XeSelectionPropRec;

typedef struct
{
  int                  PropCount;
  XeSelectionPropRec*  List;

#ifdef EX11
  Display*             X_Display;
  Atom                 X_IncrAtom, X_IndirectAtom, X_TimestampAtom;
#endif
} XePropListRec;


typedef struct
{
	XeModifiers  Mask;
	int          Count;
	int          Index;
} XeModToKeysymTable;


// Choose a number between 2 and 8
#define TMKEYCACHELOG2  6
#define TMKEYCACHESIZE  (1<<TMKEYCACHELOG2)

typedef struct
{
	unsigned char  ModifiersReturn[256];  // Constant per XeKeyCode, key proc
	XeKeyCode      KeyCode[TMKEYCACHESIZE];
	unsigned char  Modifiers[TMKEYCACHESIZE];
	XeKeySym       Keysym[TMKEYCACHESIZE];
} XeTMKeyCache;

typedef struct
{
	XeEvent*       Event;
	unsigned long  Serial;
	XeKeySym       Keysym;
	XeModifiers    Modifiers;
	XeTMKeyCache   KeyCache;  // Keep this last, to keep offsets to others small
} XeTMKeyContext;


// Internal version of XeDisplay.
// The fields after XeDisplayCore are always there, but not accessible to application code.
//
typedef struct
{
	XeDisplayCore;

	XePerDisplayInput    InputState;               // State for modal grabs and keyboard focus
	XeModToKeysymTable*  ModifiersToKeySyms;

	char*                 Language;
	ETime                 MultiClickDelay_us;
	XeExtSelectRec*       ExtSelect;int  ExtSelect_count;
	_XeInternalCallback*  MappingCallbacks;         // For the translation manager
	unsigned long         KeySymsSerial;            // For tracking MappingNotify events
	int                   KeySymsPerKeyCode;        // Number of KeySyms for each XeKeyCode
	uint8_t               IsModifier[32];           // 256-bit table that indicates for each key whether it's a modifier
	XeModifiers           ModeSwitch;	              // Keyboard group modifiers
	XeModifiers           NumLock;		              // Keyboard numlock modifiers
	XeTMKeyContext*       TMContext;                // For XeX_GetActionKeySym

#ifdef EX11
	XeKeySym           LockMeaning;              // Lock modifier meaning
	XeKeySym*          KeySyms;                  // XeKeyCode to XeKeySym table
	int                KeyCodeMin, KeyCodeMax;
	XeKeySym*          ModifierKeySyms;
	XeKeyFunc          DefaultKeycodeTranslator;
	Region             X_Region;
	struct _XeXGC*     X_GCs;                    // For reusing X graphics contexts within an X Display
	Drawable**         X_Pixmaps;
	Time               X_LastTimestamp;
	XePropListRec*     X_SArray;                 // X context
	struct _XeW2WTable*  XeW2WTable;	           // X Window to Widget table
#endif
} XeDisplayI;


#define KEYBOARD TRUE
#define POINTER  FALSE

#define _XeIsServerGrab(g) ((g == XePassiveServerGrab) || (g == XeActiveServerGrab))

#define XeIsEitherPassiveGrab(g) ((g == XePassiveServerGrab) || (g == XePseudoPassiveServerGrab))

extern XeServerGrab* _Xe_CheckServerGrabsOnWidget(XEvent* PXEvent, XeWidget PW, const EBool PIsKeyboard);

#define _XeGetGrabList(pdi) (&(pdi)->GrabList)

extern XeWidget  _XeProcessKeyboardEvent(XKeyEvent* PXKeyEvent, XeWidget PW, XePerDisplayInput* PDI);

typedef enum
{
	XeCallbackNoList,
	XeCallbackHasNone,
	XeCallbackHasSome
} XeCallbackStatus;

typedef XeResource**  _XeCallbackTable;

extern _XeInternalCallback*  _XeCompileCallbackList(XeCallbackList PCallbacks);



extern void _Xe_ShellGetPosition(XeWidget PW,  XeCoordinate* PX, XeCoordinate* PY);

#ifndef ERRORDB
#define ERRORDB "/usr/lib/X11/XtErrorDB"
#endif

#include <X11/Xalloca.h>  // For ALLOCATE_LOCAL


extern void     _XeAllocError(const char* PAllocType);
extern void     _Xe_ResourceListCompile(XeResource* PResources, const int PNResources);


typedef struct
{
	int                  RefCnt;                 // We need to reference-count these...
	XeIntervalId         ID;
	ETime                TriggerTime, Interval;  // Microseconds
	XeTimerCallbackFuinc  Callback;
	EPointer             ClientData;
	EBool                Active, Recurrent;
} XeTimeOut;


EListA(extern, XeTimeOut*, _XeTimeOut_s);  // Sorted by TriggerTime. Earliest first


#ifdef XTHREADS

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


#define INIT_APP_LOCK(pApp)  if(_XeInitAppLock) _XeInitAppLock(pApp)
#define FREE_APP_LOCK(pApp)  if(pApp && pApp->FreeLock) PApp->FreeLock(pApp)

#define YIELD_APP_LOCK(pApp, pPush, pPushed, pLevel)  if(pApp && pApp->YieldLock) pApp->YieldLock(pApp, pPush, pPushed, pLevel)
#define RESTORE_APP_LOCK(pApp, pLevel, pPushed)       if(pApp && pApp->RestoreLock) pApp->RestoreLock(pApp, pLevel, pPushed)

#define XeXDPY_TO_APP(pD)        XeApp  LApp = (_XeProcessLock ? XeX_DisplayToApp(pD): NULL)
#define XeDPY_TO_APP(pD)         XeApp  LApp = (_XeProcessLock ? XeDisplay_ToApp(pD): NULL)

#else  // defined(XTHREADS)

#define INIT_APP_LOCK(pApp)
#define FREE_APP_LOCK(pApp)

#define XeXDPY_TO_APP(pD)
#define XeDPY_TO_APP(pD)

#endif // !defined(XTHREADS)


typedef struct
{
	char*  Start;
	char*  Current;
	int    BytesRemaining;
} XeHeap;


extern void  _XeCacheFlushTag(XeApp PApp, EPointer PTag);
extern void  _XeFreeActions(_XeActionListRec* PActionTable);
extern void  _XtExtensionSelect(XeWidget PW);


extern void       _Xe_RemoveAllInputs(XeApp PApp);
extern int        _Xe_WaitForSomething(XeApp PApp, const EBool PIgnoreEvents, const EBool PIgnoreTimers, const EBool PIgnoreInputs, const EBool PIgnoreSignals, const EBool PBlock,
#ifdef XTHREADS
    const EBool PDropLock,
#endif
    unsigned long* PHowlong);

extern void       _XeWidgets_ManageI(XeWidget* PChildren, const int PNChildren,  XeWidget PParentW, const EBool PCallChangeManaged, const char* PCallerFunc);
extern void        Xe_SetMappedWhenManaged(XeWidget PW, const EBool PMappedWhenManaged);
extern EPointer   _XeActions_Compile(XeActionRec* PActions, const int PCount,  const EBool PInPlace);
extern void       _XeCompiledActions_Free(EPointer PActions);

extern ETime       _Xe_HandleTimers(ETime PCurrentTime);


extern void        _XeCountVaList(va_list PVar, int* PTotalCount, int* PTypedCount);
extern void        _XeVaToTypedArgList(va_list PVar, const int PMaxCount, XeTypedArg** PArgsR, Cardinal* PNArgsR);

extern XeDisplay*  _XeX_AppAddDisplay(XeApp PApp, Display* PXDisplay);

extern void        _XeDoPhase2Destroy(XeApp PApp, int PDispatchLevel);
extern void        _Xe_DestroyServerGrabs(XeWidget PW);


extern void    _XeX_SelectionFree(XeDisplayI* PDisplay);


// EventUtil.c
extern void    _Xe_AncestorListFill(XeWidget** PListP, int* PMaxElemsP, int* PNumElemsP, XeWidget PStartW, XeWidget PEndW);

extern void    _XeEvents_Init(void);
extern void    _XeEvents_Free(void);
extern void    _XeEventTable_Free(XeEventTable* PEventTable);
extern EBool   _Xe_InGrabList(XeWidget PW, XeGrab* PGrabList);
extern void    _Xe_SendFocusEvent(XeWidget PChild, const int PType);

extern void    _Xe_BuildKeysymTables(XeDisplayI* PDisplay);

#define _XeApp_IsSafeToDestroy(mApp)  ((mApp)->DispatchLevel == 0)

extern void  _Xe_AllocTMContext(XeDisplayI* PDisplay);

extern void     _XeGetLanguage(XeDisplayI* PDisplay);


extern XeCallbackStatus  XeHasCallbacks(XeWidget PW, const char* PCallbackName);


// Translation manager
typedef struct
{
	unsigned int    Knot:1, Pair:1;
	unsigned short  RefCount;  // Garbage collection
	XeKeySym        Keysym;
} XeLateBindings;

extern void      _XeTranslateEvent(XeWidget PW, XeEvent* PEvent);
extern EBool     _XeComputeLateBindings(XeDisplay* PDisplay, XeLateBindings* PLateModifiers, XeModifiers* PComputed, XeModifiers* PComputedMask);
extern void      _XeRemoveFromMappingCallbacks(XeWidget PW, EPointer PClientData /* target widget */, EPointer PCallData);



// Type converters

// Representation types

extern	XrmQuark  _XeQString;

// Resource conversions

extern void _XeAddDefaultConverters(XeConverterTable PTable);

extern void _XeSetDefaultConverterTable(XeConverterTable* PTable);

extern void _XeConverterTable_Free(XeConverterTable PTable);

extern void _XeTable_AddConverter(XeConverterTable PTable, XrmRepresentation PFromType, XrmRepresentation PToType,
    XeTypeConverter PConverter,  XeConvertArgList PConvertArgs, const int PNArgs,
    const EBool PNewStyle, XeCacheType PCache_type, XeDestructor PDestructor, const EBool PGlobal);

extern EBool _XeConvert(XeWidget PW, XrmRepresentation PFromType, XeSizeAddr* PFrom, XrmRepresentation PToType, XeSizeAddr* PTo, XeCacheRef* PCacheRefRet);

extern void  _XeConvertInitialize(void);



#ifdef EX11
extern void  _XeX_GCsFree(XeDisplayI* PDisplay);
#endif




//
// Translations
//
#define CACHE_TRANSLATIONS

#define TM_NO_MATCH (-2)

#define _XeRStateTablePair "_XeStateTablePair"

typedef unsigned char   TMByteCard;
typedef unsigned short  TMShortCard;
typedef unsigned long   TMLongCard;
typedef short           TMShortInt;


typedef struct
{
	XrmQuark      Signature;
	XeActionFunc  Func;
} XeCompiledAction;


struct _XeActionListRec_S
{
	_XeActionListRec*  Next;
	XeCompiledAction*  Table;
	TMShortCard        Count;
};


typedef struct XeTMTypeMatch_S      XeTMTypeMatch;
typedef struct XeTMModifierMatch_S  XeTMModifierMatch;
typedef struct XeTMEvent_S          XeTMEvent;

typedef EBool (*XeMatchFunc)(XeTMTypeMatch* PTypeMatch, XeTMModifierMatch* PModMatch, XeTMEvent* PEventSeq);


typedef short  ModifierMask;


typedef struct _XeAction_S  _XeAction;
struct _XeAction_S
{
	int         Index;      // Index into quarkTable to find proc
	String*     Params;     // Pointer to array of params
	Cardinal    NParams;    //  Number of params
	_XeAction*  Next;       //  ext action to perform
};

typedef struct _XeState_S  _XeState;
struct _XeState_S
{
	unsigned int  IsCycleStart:1, IsCycleEnd:1;
	TMShortCard   TypeIndex;
	TMShortCard   ModIndex;
	_XeAction*    Actions;    // rhs list of actions to perform
	_XeState*     NextLevel;
};


#define XeTableReplace   0
#define XeTableAugment   1
#define XeTableOverride  2
#define XeTableUnmerge   3

typedef unsigned int  _XeTranslateOp;

typedef struct _XeATranslationData_S  _XeATranslationData;


// New Definitions
struct XeTMModifierMatch_S
{
	TMLongCard       Modifiers, ModifierMask;
	XeLateBindings*  LateModifiers;
	EBool            Standard;
};

struct XeTMTypeMatch_S
{
	TMLongCard   EventType, EventCode, EventCodeMask;
	XeMatchFunc  MatchEvent;
};

typedef struct
{
	unsigned int  IsSimple:1, HasActions:1, HasCycles:1, More:13;
	TMShortCard   TypeIndex;
	TMShortCard   ModIndex;
} _XeTMBranchHead;

// NOTE: Elements of this structure must match those of _XeTMComplexStateNode and _XeTMParseStateNode.
//
typedef struct
{
	unsigned int      IsSimple:1;
	unsigned int      IsAccelerator:1;
	unsigned int      MappingNotifyInterest:1;
	unsigned int      RefCount:13;
	TMShortCard       NBranchHeads;
	TMShortCard       NQuarks;     // # of entries in quarkTbl

	TMShortCard       Unused;      // To ensure same alignment
	_XeTMBranchHead*  BranchHeadTbl;
	XrmQuark*         QuarkTbl;    // Table of quarkified rhs
} _XeTMSimpleStateNode;

// NOTE: elements of this structure must match those of XeTMSimpleStateNode and _XeTMParseStateNode.
//
typedef struct
{
	unsigned int      IsSimple:1, IsAccelerator:1, MappingNotifyInterest:1, RefCount:13;
	TMShortCard       NBranchHeads;
	TMShortCard       NQuarks;      // # of entries in quarkTbl

	TMShortCard       NComplexBranchHeads;
	_XeTMBranchHead*  BranchHeadTbl;
	XrmQuark*         QuarkTbl;       // table of quarkified rhs
	_XeState**        ComplexBranchHeadTbl;
} _XeTMComplexStateNode;


// NOTE: elements of this structure must match those of _XeTMSimpleStateNode and _XeTMComplexStateNode.
//
typedef struct
{
	unsigned int       IsSimple:1, IsAccelerator:1, MappingNotifyInterest:1, IsStackQuarks:1, IsStackBranchHeads:1, IsStackComplexBranchHeads:1;
	unsigned int       Unused:10;        // to ensure correct alignment
	TMShortCard        NBranchHeads;
	TMShortCard        NQuarks;        // # of entries in quarkTbl

	TMShortCard        NComplexBranchHeads;
	_XeTMBranchHead*   BranchHeadTbl;
	XrmQuark*          QuarkTbl;         // table of quarkified rhs
	_XeState**         ComplexBranchHeadTbl;
	TMShortCard        BranchHeadTblSize;
	TMShortCard        QuarkTblSize;     // total size of quarkTbl
	TMShortCard        ComplexBranchHeadTblSize;
	_XeState*          Head;
} _XeTMParseStateNode;


typedef union
{
	_XeTMSimpleStateNode   SimpleNode;
	_XeTMParseStateNode    ParseNode;
	_XeTMComplexStateNode  ComplexNode;
} _XeTMStateNode;

typedef struct
{
	XeActionFunc*  Funcs;
} XeTMSimpleBindFunc;

typedef struct
{
	XeWidget        Widget;    // Widget to pass to the action Func
	XeTranslations  Translations;
	XeActionFunc*   Funcs;
} XeTMComplexBindFunc;

typedef struct
{
	unsigned int        IsComplex:1;    // Must be first
	XeTMSimpleBindFunc  BindTbl[1];     // Variable length
} XeTMSimpleBindData;

typedef struct
{
	unsigned int          IsComplex:1;     // Must be first
	_XeATranslationData*  AccelContext;    // For GetValues
	XeTMComplexBindFunc   BindTbl[1];      // Variable length
} XeTMComplexBindData;

typedef union
{
	XeTMSimpleBindData   SimpleNode;
	XeTMComplexBindData  ComplexNode;
} XeTMBindData;

typedef struct  _XeTranslationData_S  _XeTranslationData;
struct _XeTranslationData_S
{
	unsigned char        HasBindings;      // Must be first
	unsigned char        Operation;        // Replace, augment, override
	TMShortCard          NStateTrees;
	_XeTranslationData*  Composers[2];
	XeEventMask          EventMask;
	_XeTMStateNode*      StateTreeTbl[1];  // Variable length
};


// _XeATranslationData is returned by GetValues for translations that contain accelerators. The TM can differentiate between this
// and TranslationData (that don't have a bindTbl) by looking at the first field (hasBindings) of either structure.
// All _XeATranslationData structures associated with a widget are chained off the BindData record of the widget.
//
struct _XeATranslationData_S
{
	unsigned char         HasBindings;   // Must be first
	unsigned char         Operation;
	_XeTranslationData*   Translations;  // Atual translations
	_XeATranslationData*  Next;          // Chain the contexts together
	XeTMComplexBindFunc   BindTbl[1];    // Accelerator bindings
};


typedef struct
{
	XeTMTypeMatch**           TypeMatchSegmentTbl;
	TMShortCard               NumTypeMatches;
	TMShortCard               NumTypeMatchSegments;
	TMShortCard               TypeMatchSegmentTblSize;
	XeTMModifierMatch**       ModMatchSegmentTbl;
	TMShortCard               NumModMatches;
	TMShortCard               NumModMatchSegments;
	TMShortCard               ModMatchSegmentTblSize;
	EBool                     NewMatchSemantics;
#ifdef XeTM_TRACE
	XeTranslations*           TMTable;
	TMShortCard               NTMs;
	TMShortCard               TmTableSize;
	struct _TMBindCacheRec**  BindCacheTbl;
	TMShortCard               NBindCache;
	TMShortCard               BindCacheTblSize;
	TMShortCard               NLateBindings;
	TMShortCard               NBranchHeads;
	TMShortCard               NComplexStates;
	TMShortCard               NComplexActions;
#endif
} _XeTMGlobal;

extern _XeTMGlobal  _XeGlobalTM;


extern void  _XeTMGlobal_FreeContents(void);


typedef struct
{
	XeTranslations	Old;  // Table to merge into
	XeTranslations	New;  // Table to merge from
} _XeTMConvert;

#define _XeEventTimerEventType ((TMLongCard)~0L)
#define KeysymModMask		(1L<<27)  // Private to TM
#define AnyButtonMask		(1L<<28)  // Private to TM

typedef struct
{
	TMLongCard       Modifiers, ModifierMask;
	XeLateBindings*  LateModifiers;
	TMLongCard       EventType, EventCode, EventCodeMask;
	XeMatchFunc      MatchEvent;
	EBool            Standard;
} XeTMTEvent;

typedef struct _XeEventSeq_S  _XeEventSeq;
struct _XeEventSeq_S
{
	XeTMTEvent    TMTEvent;  // Event description
	_XeState*     State;     // Private to state table builder
	_XeEventSeq*  Next;      // Next event on line
	_XeAction*    Actions;   // r.h.s.
};

typedef _XeEventSeq*  EventPtr;
struct XeTMEvent_S
{
	XeEvent*    Event;
	XeTMTEvent  TMTEvent;
};

typedef struct XeActionHookRec_S   XeActionHookRec;
typedef struct XeActionHookRec_S*  XeActionHook;

typedef void  (*XeActionHookFunc)(XeWidget PW, EPointer PClientData, const char* PActionName, XeEvent* PEvent,  String* PParams, Cardinal* PNParams);

struct XeActionHookRec_S
{
	XeActionHookRec*  Next;  // Must remain first
	XeApp             App;
	XeActionHookFunc  Func;
	EPointer          ClientData;
};



#define TM_MOD_SEGMENT_SIZE 	16
#define TM_TYPE_SEGMENT_SIZE 	16

#define TMGetTypeMatch(pIndex)      ((XeTMTypeMatch*) &((_XeGlobalTM.TypeMatchSegmentTbl[((pIndex) >> 4)])[(pIndex) & 15]))
#define TMGetModifierMatch(pIndex)  ((XeTMModifierMatch*)&((_XeGlobalTM.ModMatchSegmentTbl[(pIndex) >> 4])[(pIndex) & 15]))

// Useful Access Macros
#define TMNewMatchSemantics()  (_XeGlobalTM.NewMatchSemantics)
#define TMBranchMore(branch)   (branch->More)
#define TMComplexBranchHead(tree, br)  (((_XeTMComplexStateNode*)tree)->ComplexBranchHeadTbl[TMBranchMore(br)])

#define TMGetComplexBindEntry(bindData, pIndex)  ((XeTMComplexBindFunc*)&(((XeTMComplexBindData*)bindData)->BindTbl[pIndex]))

#define TMGetSimpleBindEntry(bindData, pIndex)   ((XeTMSimpleBindFunc*)&(((XeTMSimpleBindData*)bindData)->BindTbl[pIndex]))


#define _Xe_KeySymTablesInit(pDisplay)  { if((pDisplay)->KeySyms == NULL) _Xe_BuildKeysymTables(pDisplay); }


//
// Internal Functions
//
extern void      _XeRegisterGrabs(XeWidget PW);
extern EBool     _XeMatchUsingStandardMods(XeTMTypeMatch* PTypeMatch, XeTMModifierMatch* PModMatch, XeTMEvent* PEventSeq);
extern EBool     _XeMatchUsingDontCareMods(XeTMTypeMatch* PTypeMatch, XeTMModifierMatch* PModMatch, XeTMEvent* PEventSeq);


#define _XeCBCalling           1
#define _XeCBFreeAfterCalling  2

typedef EBool         (*_XeConditionFunc)(EPointer);

extern void             _XeAddCallback(_XeInternalCallback** PCBL, XeCallbackFunc, EPointer);
extern void             _XeAddCallbackOnce(_XeInternalCallback** PCBL, XeCallbackFunc, EPointer);
extern XeCallbackList   _XeGetCallbackList(_XeInternalCallback** PCBL);
extern void             _XeRemoveAllCallbacks(_XeInternalCallback** PCBL);
extern void             _XeRemoveCallback(_XeInternalCallback** PCBL, XeCallbackFunc, EPointer);
extern void             _XePeekCallback(XeWidget, XeCallbackList, XeCallbackFunc*, EPointer*);
extern void             _XeCallConditionalCallbackList(XeWidget, XeCallbackList, EPointer, _XeConditionFunc);

extern XeTranslations  _XeTranslations_New(_XeTMStateNode** PStateTrees, const TMShortCard PNStateTrees, const XeTranslations PFirst, const XeTranslations PSecond);

extern EBool  _XeCvtMergeTranslations(XeDisplay* PDisplay, XeSizeAddr* PArgs, Cardinal* PNArgs, XeSizeAddr* PFrom, XeSizeAddr* PTo, EPointer* PClosureRet);

extern void   _XeFreeTranslations(XeApp PApp, XeSizeAddr* PToVal, EPointer PClosure,  XeSizeAddr* PArgs, Cardinal* PNArgs);

extern TMShortCard     _XeGetQuarkIndex(_XeTMParseStateNode* PStateTreePtr, XrmQuark PQuark);
extern XeTranslations  _XeGetTranslationValue(XeWidget PW);
extern TMShortCard     _XeGetTypeIndex(XeTMTEvent* PTMTEvent);

extern void    _XeRemoveTranslations(XeWidget PW);
extern void    _XeTMDataFree(XeWidget PW);
extern void    _XeMergeTranslations(XeWidget PW, XeTranslations PNewTranslations, _XeTranslateOp POperation);

typedef EBool  (*_XeTraversalFunc)(_XeState* PState, EPointer PData);

extern void  _XeTraverseStateTree(_XeTMStateNode* PTree, _XeTraversalFunc PFunc, EPointer PData);
extern void  _XeTranslate_Init(void);
extern void  _XeAddTMConverters(XeConverterTable PTable);
extern void  _XeUnbindActions(XeWidget PW, XeTranslations PTranslations, XeTMBindData* PBindData);
extern void  _XeUnmergeTranslations(XeWidget PW, XeTranslations PTranslations);


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


#endif // _XeI_h
