//================================================================
// Xe UI toolkit
//
// Main public header file
//
// AUTHOR:  Gabor Nagy
// DATE:    2026-Aug-03 05:49:09
//
// Copyright (C) 1996-2026 by Gabor Nagy. All rights reserved.
//================================================================
#ifndef _Xe_h
#define _Xe_h

#include <stdarg.h>

#include <Xe/XeTypes.h>

#include <EInput.h>


#include <Xe/Composite.h>


#ifdef XeOpenGL
extern EBool  XeGL_SupportsGL(XeWidget PW);
extern EBool  XeGL_IsInGLShell(XeWidget PW);
#else
#define XeGL_SupportsGL(pW)   FALSE
#define XeGL_IsInGLShell(pW)  FALSE
#endif

// Compress_exposure options
#define XeExposeCompressSERIES    ((XeEnum)TRUE)
#define XeExposeCompressMULTIPLE  2
#define XeExposeCompressMAXIMAL   3

// Modifiers
#define XeExposeGraphicsExpose        0x10
#define XeExposeGraphicsExposeMerged  0x20
#define XeExposeNoExpose              0x40
#define XeExposeNoRegion              0x80

#define XePRESS    1
#define XeRELEASE  2

#define XeUNSPECIFIED_INT     (-1)


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

extern const int  _Xe_UnspecifiedInt;
#define XeUnspecifiedWindow       ((Window)2)
#define XeUnspecifiedWindowGroup  ((Window)3)

typedef struct
{
	EpImage*  Image;
	EIndex2   Size;  // These fields allow us to tell if the Image has changed
	EIndex    ID;
	EIndex    ChangeCount;
} XeImage;


#ifndef _XeP_h
struct _XeWidget_S
{
	ENamedCore;
};
#endif
EList_t(XeWidget);
EListA_t(XeWidget);

typedef struct _XeObjectRec*    XeObject;

#ifndef VMS
externalref XeWidgetClass*      XeObject_class;
#endif



// Built-in cursors
//
enum
{
	XeCursorLEFT_PTR = 1,
	XeCursorCENTER_PTR,
	XeCursorWATCH,
	XeCursorFOUR_ARROWS,
	XeCursorTOP_LEFT_CORNER,
	XeCursorTOP_RIGHT_CORNER,
	XeCursorBOTTOM_RIGHT_CORNER,
	XeCursorBOTTOM_LEFT_CORNER,
	XeCursorLEFT_SIDE,
	XeCursorRIGHT_SIDE,
	XeCursorTOP_SIDE,
	XeCursorBOTTOM_SIDE,
	XeCursorCROSSHAIR,
	XeCursorTARGET,
	XeCursorEXCHANGE,
	XeCursorSB_H_DOUBLE_ARROW
};


enum
{
	XeSelectionPRIMARY = 1,
	XeSelectionCLIPBOARD
};


enum
{
	XeSTRING8 = 1,
	XeWSTRING,
	XeUTF8_STRING
};


typedef struct XeStyle_S  XeStyle;

struct XeStyle_S
{
	void  (*Dummy)(void);
};

// Mouse pointer
//
typedef struct
{
	EpImage*  Image;

#ifdef EX11
	Display*  X_Display;
	Cursor    X_Cursor;
#endif
} XeCursor;


typedef struct
{
	uint32_t  Unused;  // Filler until we have other members, to makes sure the struct size is not 0
#ifdef EX11
	Drawable  X_Drawable;
#endif
} XeDrawContext;


typedef struct
{
	EcRGBAiColor   RGBAi;        // Platform-independent color. HDR, fixed-point  0..65535 -> 0.0..1.0
	EcRGBAiColor*  RGBAiP;       // Temporary pointer, used for passing resource values (we can't pass structs, like EcRGBAiColor by value, as a Resource).
	EcRGBAiColor*  RGBAiPtr;     // Pointer to Color. Using a pointer allows Xe for to support centralized settings and quick update of themes/colors.

	EBool          Set;          // This color was specified in a resource list (used in _WInit, etc.)
} XeColor;


//----------------------------------------------------------------
// Named colors For Xe_XPMToImageVa, XeX_XPMToPixmapVa etc.
//----------------------------------------------------------------
typedef struct
{
	char*         Name;
	EcRGBAiColor  RGBAi;
#ifdef EX11
	Pixel*        X_PixelPtr;
#endif
} XeNamedColor;



// XeExtension
// This object class is added to WMShell Widgets, to contain dialog box, file selection box, etc. data
//
typedef struct XeExtension_S           XeExtension;         // Forward-define XeExtension
typedef struct XeExtensionClassRec_S*  XeExtensionClass;    // Forward-define XeExtensionClass


#define XeCore(mType)\
	EEntityCore;\
	mType##Class  Class;\


#define XeExtensionCore\
	XeCore(XeExtension)

struct XeExtension_S
{
	XeExtensionCore;
};

/*
struct XeExtensionClass_S
{
	ECoreClassCore(XeExtension);
};
*/


extern XeExtension*  XeWMShellExtensionAdd(XeWidget PW, XeExtensionClass PClass);
extern XeExtension*  XeWMShellExtensionByClass(XeWidget PW, XeExtensionClass PClass);


typedef struct
{
	XeCallbackFunc  Callback;
	EPointer	      ClientData;
} XeCallbackRec, *XeCallbackList;

// Callbacks
//
typedef struct
{
	XeCallbackFunc  Func;
	EPointer        ClientData;
} EguiCallbackRec;

typedef struct EguiCallbackList
{
	EList(, EguiCallbackRec, Callbacks);
} EguiCallbackList;


typedef XeGeometryResult  (*XeGeometryHandler)(XeWidget PW, XeWidgetGeometry* PRequest, XeWidgetGeometry* PReply);


#include <Xe/XeStrDefs.h>


#define EXeStart         ArgCnt = 0
#define EXeSetArg(n, v)  Args[ArgCnt].Name = (n), Args[ArgCnt].Value = (EArgVal)(v), ArgCnt++
#define EXeSetArgColor(p) do\
{\
	if(p.RGBAiPtr) EXeSetArg(XeNBackgroundColorPtr, p.RGBAiPtr);\
	else if(p.RGBAiP) EXeSetArg(XeNBackgroundColor, p.RGBAiP);\
	else EXeSetArg(XeNBackgroundColor, &p.RGBAi);\
} while(0)

#define XeToolTip8(p) do\
{\
	EXeSetArg(XeNTipStringUTF8, (EArgVal)(p));\
} while(0)

#define XeToolTip(p) do\
{\
	EXeSetArg(XeNTipString, (EArgVal)(p));\
} while(0)

#define XeSetToolTipXeString(pXeStr, pString) do\
{\
	pXeStr = XeString_NewVa(TRUE,  XeFONT, XeFontDEFAULT,  XeALIGNMENT, XeStringAlignLEFT,  XeTEXT, pString,  0);if(pXeStr)\
	{\
		EXeSetArg(XeNTipString, pXeStr);\
	}\
} while(0)



#define EXeSetImages2(p)  do { EXeSetArg(XeNLabelImage, p);EXeSetArg(XeNActiveImage, p##_Active);EXeSetArg(XeNArmImage, p); } while(0)
#define EXeSetImages3(p)  do { EXeSetArg(XeNLabelImage, p);EXeSetArg(XeNActiveImage, p##_Active);EXeSetArg(XeNArmImage, p##_Armed); } while(0)


typedef struct _XeDCore*  XeDWidget;
EListA_t(XeDWidget);

typedef	int   XeIntervalId;

typedef void  (*XeTimerCallbackFuinc)(EPointer PClientData, XeIntervalId* PTID, const int PCount);


// XeNDeleteResponse values
//
enum
{
	XeDESTROY = 0, XeUNMAP_SHELL, XeDO_NOTHING
};


#define XeCallbackStructCore\
	ECallbackStructCore;\
	XeEvent*  Event


// Callback structures
//
typedef struct
{
	XeCallbackStructCore;
} XeCallbackStruct;


// Stretchable "themed" object (for scrollbar sliders, thermometers etc.)
//
typedef struct
{
	int       SizeX, SizeY,
	          StartCapLength,
	          MiddleTileLength,
	          EndCapLength;

	EpImage*  StartCap;
	EpImage*  MiddleTile;
	EpImage*  MiddleCache;
	EpImage*  EndCap;
} XeTile;



//------------------------------------------------------------------------
// Compound strings
//------------------------------------------------------------------------


//================================================================
// Defines for resources to be defaulted by vendors.
// String are initialized in Xeos.c
//================================================================
/*
#define XeDEFAULT_BACKGROUND            _XeSDEFAULT_BACKGROUND
#define XeDEFAULT_DARK_THRESHOLD        15
#define XeDEFAULT_LIGHT_THRESHOLD       77
#define XeDEFAULT_FOREGROUND_THRESHOLD  35
*/

typedef uint8_t  XeDirection;


#define XeDIRECTION_IGNORED            0x30

#define XeRIGHT_TO_LEFT_MASK           0x01  // 0x01 for bc
#define XeLEFT_TO_RIGHT_MASK           0x02
#define XeHORIZONTAL_MASK              0x03
#define XeTOP_TO_BOTTOM_MASK           0x04
#define XeBOTTOM_TO_TOP_MASK           0x08
#define XeVERTICAL_MASK                0x0c
#define XePRECEDENCE_HORIZ_MASK        0x40
#define XePRECEDENCE_VERT_MASK         0x80
#define XePRECEDENCE_MASK              0xc0

enum
{
	XeRIGHT_TO_LEFT_TOP_TO_BOTTOM = XeRIGHT_TO_LEFT_MASK | XeTOP_TO_BOTTOM_MASK | XePRECEDENCE_HORIZ_MASK,
	XeLEFT_TO_RIGHT_TOP_TO_BOTTOM = XeLEFT_TO_RIGHT_MASK | XeTOP_TO_BOTTOM_MASK | XePRECEDENCE_HORIZ_MASK,
	XeRIGHT_TO_LEFT_BOTTOM_TO_TOP = XeRIGHT_TO_LEFT_MASK | XeBOTTOM_TO_TOP_MASK | XePRECEDENCE_HORIZ_MASK,
	XeLEFT_TO_RIGHT_BOTTOM_TO_TOP = XeLEFT_TO_RIGHT_MASK | XeBOTTOM_TO_TOP_MASK | XePRECEDENCE_HORIZ_MASK,
	XeTOP_TO_BOTTOM_RIGHT_TO_LEFT = XeRIGHT_TO_LEFT_MASK | XeTOP_TO_BOTTOM_MASK | XePRECEDENCE_VERT_MASK,
	XeTOP_TO_BOTTOM_LEFT_TO_RIGHT = XeLEFT_TO_RIGHT_MASK | XeTOP_TO_BOTTOM_MASK | XePRECEDENCE_VERT_MASK,
	XeBOTTOM_TO_TOP_RIGHT_TO_LEFT = XeRIGHT_TO_LEFT_MASK | XeBOTTOM_TO_TOP_MASK | XePRECEDENCE_VERT_MASK,
	XeBOTTOM_TO_TOP_LEFT_TO_RIGHT = XeLEFT_TO_RIGHT_MASK | XeBOTTOM_TO_TOP_MASK | XePRECEDENCE_VERT_MASK,
	XeTOP_TO_BOTTOM = XeTOP_TO_BOTTOM_MASK | XeHORIZONTAL_MASK | XePRECEDENCE_MASK,
	XeBOTTOM_TO_TOP = XeBOTTOM_TO_TOP_MASK | XeHORIZONTAL_MASK | XePRECEDENCE_MASK,
	XeRIGHT_TO_LEFT = XeRIGHT_TO_LEFT_MASK | XeVERTICAL_MASK | XePRECEDENCE_MASK,
	XeLEFT_TO_RIGHT = XeLEFT_TO_RIGHT_MASK | XeVERTICAL_MASK | XePRECEDENCE_MASK,
	XeDEFAULT_DIRECTION = 0xff
};


extern EBool  XeDirectionMatch(XeDirection d1, XeDirection d2);

extern EBool  XeDirectionMatchPartial(XeDirection d1, XeDirection d2, XeDirection dmask);


typedef uint8_t          XeStringDirection;
typedef uint8_t          XeChar;                 // Opaque to outside
typedef uint8_t*         XeString;               // Opaque to outside
typedef XeString*        XeStringTable;          // Opaque to outside
typedef uint8_t          XeStringComponentType;  // Component tags

// Tag codes:
//
#define XeStringComponentMAGIC0  0xDF
#define XeStringComponentMAGIC1  0x80

enum
{
	XeStringComponentFONT = 1,
	XeStringComponentTEXT,
	XeStringComponentFTEXT,         // Formatted text, just like in printf, followed by optional values
	XeStringComponentDIRECTION,
	XeStringComponentSEPARATOR,
	XeStringComponentLINESPACING_PIXELS,
	XeStringComponentLOCALE_TEXT,
	XeStringComponentFULL_LENGTH,
	XeStringComponentCOLOR_NAME,
	XeStringComponentCOLOR_INDEX8,
	XeStringComponentCOLOR_INDEX16,
	XeStringComponentCOLOR_INDEX32,
	XeStringComponentCOLOR_RGB,
	XeStringComponentCOLOR_RGBA,
	XeStringComponentALIGNMENT,
	XeStringComponentANTI_ALIASING  // 2, 3, 4 etc. For example : 2 means that the font is 2x larger, so rendered text is scaled down by 2 with filtering
};

// Shorthands for the above
//
#define	XeFONT                XeStringComponentFONT
#define	XeTEXT                XeStringComponentTEXT
#define	XeFTEXT               XeStringComponentFTEXT
#define	XeDIRECTION           XeStringComponentDIRECTION
#define	XeSEPARATOR           XeStringComponentSEPARATOR
#define	XeLINESPACING_PIXELS  XeStringComponentLINESPACING_PIXELS
#define	XeLOCALE_TEXT	        XeStringComponentLOCALE_TEXT
#define	XeFULL_LENGTH	        XeStringComponentFULL_LENGTH
#define	XeCOLOR_NAME	        XeStringComponentCOLOR_NAME
#define	XeCOLOR_INDEX8        XeStringComponentCOLOR_INDEX8
#define	XeCOLOR_INDEX16       XeStringComponentCOLOR_INDEX16
#define	XeCOLOR_INDEX32       XeStringComponentCOLOR_INDEX32
#define	XeCOLOR_RGB           XeStringComponentCOLOR_RGB
#define	XeCOLOR_RGBA          XeStringComponentCOLOR_RGBA
#define	XeALIGNMENT           XeStringComponentALIGNMENT       // XeStringAlignLEFT etc.
#define	XeAA                  XeStringComponentANTI_ALIASING


// Codes 12-125 are reserved

#define XeStringComponentEND  ((XeStringComponentType)126)

// Codes 128-255 are user tags
#define XeStringComponentUSER_BEGIN  ((XeStringComponentType)128)
#define XeStringComponentUSER_END    ((XeStringComponentType)255)



#define XeStringDEFAULT_TAG  NULL

enum { XeStringDirectionDEFAULT = 0, XeStringDirectionL_TO_R = 1, XeStringDirectionR_TO_L };
enum { XeStringAlignDEFAULT = 0, XeStringAlignLEFT, XeStringAlignRIGHT, XeStringAlignCENTER, XeStringAlignJUSTIFY };



typedef struct
{
	EpImage*       Image;                   // Each binary segment may have an Image

	XeFontSize*    FontSize;                // The Font for rendering this string segment
// String to identify the Font within a FontList. These are compared as pointers and not duplicated or freed by Xe,
// so it's the application's responsibility to maintain them (e.g. global const variables, such as const char  HeaderTag = "header1")
// This system allows applications to quickly and efficiently change the Fonts in existing compound strings, for example when the user
// changes the application preferences.
//
	const char*    FontTag;

	EcRGBAiColor   TextColor;               // Text foreground color

	char*          String;                  // ASCII or UTF-8 string
	wchar_t*       WString;                 // Unicode string
	XeCoordinate*  CharacterPositionsX;     // Pixel position of each character in String / WString

	EInt32_2       Size;
	int32_t        AdvanceX;                // AnvanceY is just Size.Y.

// Position for drawing glyph bitmaps, vectors, etc. (e.g. the first glyph may stick out to the left of the pen position)
	int16_t        InkLeft, InkRight, InkTop;
	float          Ascent, Descent;

	uint32_t       AlignmentY : 2,          // To the image... XeStringAlignCENTER etc.
	               Direction : 2,           // XeStringDirectionL_TO_R etc.
	               Alignment : 3,           // XeStringAlignCENTER etc.
	               Separator : 2,           // New line after this segment. Number of new-line chars (1 on UNIX file: 0x0A, 2 on MS file: 0x0D,0x0A)
	               SetColor : 1,            // A color was defined for this segment
	               LineAdvanceType : 2;     // XePIXELS, XeTHOUSANDTHS (of the current font's ascent + descent)
	uint16_t       LineAdvance;

#ifdef EX11
	Pixel          X_TextColor;
	Pixmap         X_Pixmap;
#endif
} XeStringBinarySegment;
EListA_t(XeStringBinarySegment);
EList_t(XeStringBinarySegment);
typedef XeStringBinarySegmentListA  XeStringBinary;

static inline void XeStringBinarySegment_Init(XeStringBinarySegment* PSegment)
{
	PSegment->Image = NULL;
	PSegment->FontSize = NULL;
	PSegment->Direction = XeStringDirectionL_TO_R;
	PSegment->Alignment = XeStringAlignDEFAULT;
	PSegment->AlignmentY = XeStringAlignDEFAULT;

	PSegment->String = NULL;
	PSegment->WString = NULL;
	PSegment->CharacterPositionsX = NULL;

	PSegment->Size.X = PSegment->Size.Y = 0;
	PSegment->AdvanceX = 0;
	PSegment->InkLeft = PSegment->InkRight = PSegment->InkTop = 0;
	PSegment->Separator = 0;
	PSegment->LineAdvanceType = 0;PSegment->LineAdvance = 0;
	PSegment->SetColor = FALSE;

	EClr_s(PSegment->TextColor);

#ifdef EX11
	PSegment->X_TextColor = 0;
	PSegment->X_Pixmap = None;
#endif
}

typedef struct XeStringDrawContextGLData_S   XeStringDrawContextGLData;
typedef struct XeStringDrawContextX11Data_S  XeStringDrawContextX11Data;

typedef struct
{
	EList_s(, XeStringBinarySegment, Segments);  // These just point to Segments in the BinaryString of TextField / Text, so do not free this!
	int    Length,        // Number of characters in line, excluding new line character(s)
	       OrigLength;    // Number of characters in line, including new line character(s)
	int    SizeX, SizeY, YStart;
} XeStringLine;


typedef struct
{
	EcRGBAiColor    BackgroundColor, CurrentColor;
	EpImage         GlyphImage;          // Internal buffer image for rendering glyphs with FreeType
	EpPixelBuffer*  GlyphPixelBuffer;
	EpImage         AACacheImage;        // Lazily-grown Image for down-scaling based anti-aliasing
	int             AACacheImageMargin;  // Used for blur-type operations, to avoid overflow
	EIndex2         ClipMin, ClipSize;   // Clipping area (i.e. don't draw outside of this, even if the target is larger)

	int             DebugLevel;          // 0 : no debugging, 1 : save glyph images to /tmp/Xe

#ifdef XeOpenGL
	XeStringDrawContextGLData*   GL;
#endif

#ifdef EX11
	XeStringDrawContextX11Data*  X;
#endif
} XeStringDrawContext;


typedef struct
{
	int       NumOfTabStops;
	int*      TabStops;
	uint16_t  UniformTabStops;
	uint8_t   TabUnits;    // XePIXELS, XeCHARACTERS
} XeStringRenderingSettings;


typedef struct __XeStringContextRec*   XeStringContext;   // Opaque to outside
typedef struct __XeStringRec*          _XeString;         // Opaque to outside
typedef struct _XetStringContextRec*   _XeStringContext;  // Opaque to outside
typedef struct _XeFontListContextRec*  XeFontContext;     // Opaque to outside



// Keyboard focus policies for VendorShell
//
enum { XeEXPLICIT, XePOINTER };


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

// Text highlight modes
//
typedef enum
{
	XeHIGHLIGHT_NORMAL,
	XeHIGHLIGHT_SELECTED,
	XeHIGHLIGHT_SECONDARY_SELECTED
} XeHighlightMode;


typedef long  XeTextPosition;
typedef struct
{
	XeTextPosition  X, Y;
} XeTextPosition2;

#define XeCOPY_FAILED     0
#define XeCOPY_SUCCEEDED  1
#define XeCOPY_TRUNCATED  2

typedef struct
{
	XeWidget  W;
	int       SizeX;
} XeIndentRec;



enum
{
	XeSTATIC,
	XeDYNAMIC
};

// Cursor types for XeTextField and XeText
enum
{
	XeIBEAM,
	XeSOLID
};


typedef struct
{
	XeWidget  MatrixW;
	void*     IOModule;
} EIOModuleGUI;


// Used by XeWidgets_ChangeManage
//
typedef struct
{
	XeWidget  W;
	EBool     Manage;
} XeChangeManage;


typedef struct
{
	EpImage*  Image;
	EIndex2   CropPosition, CropSize;
	int       BlurRadius;
} XeImageVariant;
EListA_t(XeImageVariant);


typedef struct
{
	EpImageComponentCore;

	EpImage*  Image;
	EIndex2   Position;
	int       BlurRadius;
} XeImageComponentCropAndBlurVariant;

// Used for blending Widgets with the background
//
typedef struct
{
	EpImage*       Image;int  X, Y;
	EcRGBAiColor*  Color;
	int            AccumulatedBlurRadius;
} XeLayer;


typedef struct XeRectObj_S*  XeRectObj;
externalref XeWidgetClass*   XeRectObj_class;

externalref XeWidgetClass*   XeCoreWidget_class;


// Draw icon for menu PushButton or ToggleButton's sub-button
/*
#define _XeM_DrawSubButtonIcon(mWhat, pImage)\
{\
	int  lOptBX = LSizeX - LButton->SubButtonSizeX,\
	lOptBY = LEW->Base.MarginSizeY + LEW->Base.MarginTop,\
	lOptBSizeY = LSizeY - LEW->Base.MarginSizeY * 2 - LEW->Base.MarginTop - LEW->Base.MarginBottom;\
\
	EpImage*  lImage = _Xe_Theme->Image##mWhat;\
	EpImage*  lSImage = EpImage_New(0, 0,  EpRGBA, 0, EResUINT8,  FALSE);\
	EpImage_Scale(lImage, lSImage, LButton->SubButtonSizeX, lOptBSizeY, EpFilterGAUSSIAN, 0.75,  1,  NULL, NULL);\
	EpImage_Composite(lSImage, pImage, pImage,  0, 0,  lOptBX , lOptBY, lSImage->Size.X, lSImage->Size.Y,  EpSRCxALPHA_plus_DSTxONE_MINUS_ALPHA, EpADD);\
	E_Delete(lSImage);\
}
*/


#define _XeM_DrawSubButtonIcon(mWhat, pImage)\
{\
	const int  lOptBX = LSizeX - LButton->SubButtonSizeX,\
	           lOptBY = LEW->Base.MarginSize.Y + LEW->Base.MarginTop,\
	           lOptBSizeY = LSizeY - LEW->Base.MarginSize.Y * 2 - LEW->Base.MarginTop - LEW->Base.MarginBottom;\
\
	EpImage*        lImage = _Xe_Theme->Image_##mWhat;\
	const EpImage*  lScaledImage = Xe_ScaledImageFind(lImage, LButton->SubButtonSizeX, lOptBSizeY, TRUE);\
	EpImage_Composite(lScaledImage, pImage, pImage,  0, 0,  lOptBX , lOptBY, lScaledImage->Size.X, lScaledImage->Size.Y,\
	 EpSRCxALPHA_plus_DSTxONE_MINUS_ALPHA, EpADD);\
/*{ char  s[256];sprintf(s, "tmp/%s()_%s:%d-%s.sgi", __FUNCTION__, __FILE__, __LINE__, PW->Name); EpImage_SaveToFile(LCacheImage, NULL,  s, NULL, 0,  NULL); }*/\
}


//--------------------------------
// Theme.c
//--------------------------------
extern void  Xe_BuildHImageFromTheme(XeTile* PTile, EpImage* PImage,  const EIndex PSizeX, const EIndex PSizeY,  const EpCImage* PImage_Left, const EpCImage* PImage_Middle,
              const EpCImage* PImage_Right);

extern void  Xe_BuildVImageFromTheme(XeTile* PTile, EpImage* PImage,  const EIndex PSizeX, const EIndex PSizeY,  const EpCImage* PImage_Top, const EpCImage* PImage_Middle,
              const EpCImage* PImage_Bottom);


//--------------------------------
// Xe.c
//--------------------------------
extern int      Xe_Verbosity;
extern EBool    Xe_GUIFreeOnExit;
extern int      _XeX_RedrawVerbosity;

extern int      (*Xe_ItemAddFunction)(XeDWidget PW);

extern EpImageComponentClass*  Xe_ImageComponentCropAndBlurVariant_class;

extern EpImagePtr  Xe_ArrowDownImage, Xe_ArrowDownImage_Active;
extern EpImagePtr  Xe_ArrowLeftImage, Xe_ArrowLeftImage_Active;
extern EpImagePtr  Xe_ArrowRightImage, Xe_ArrowRightImage_Active;
extern EpImagePtr  _Image_Close, _Image_Close_Active, _Image_Close_Armed;
extern EpImagePtr  _Image_CloseButton, _Image_CloseButton_Active, _Image_CloseButton_Armed;

extern int      XeDrawer_LeftIndent;

extern int      XeMenu_ShadowOffset;
extern int      Xe_ScreenNumber;
extern int      Xe_DesktopIconSizeX, Xe_DesktopIconSizeY;
extern EIndex2  Xe_ScreenSize;

extern EBool    Xe_CommandKeyForCtrl;
extern EBool    Xe_ToolTips;
extern int      Xe_ToolTipShadowOffset;
extern int      Xe_ToolTipDelay;  // Milliseconds

extern XeCoordinate  Xe_LeftIndent, XeDrawer_LeftIndent;


extern void   Xe_TileInit(XeTile* PTile);
extern void   Xe_TileFreeContents(XeTile* PTile);
extern EBool  Xe_TileGrow(XeTile* PTile,  const int PSizeX, const int PSizeY);

extern void   Xe_Popup(XeWidget PW, XeGrabMode PGrabKind);
extern void   Xe_Popdown(XeWidget PW);

extern void   XeAddCallbackN(XeWidget PW, const char* PCallbackName, XeCallbackFunc PCallback, EPointer PClientData);

extern void  XeAddCallback(XeWidget PW, const char* PCallbackName, XeCallbackFunc PProc, EPointer PClientData);
extern void  XeRemoveCallback(XeWidget PW, const char* PCallbackName, XeCallbackFunc PProc, EPointer PClientData);
extern void  XeAddCallbacks(XeWidget PW, const char* PCallbackName, XeCallbackList PCallbacks);
extern void  XeRemoveCallbacks(XeWidget PW, const char* PCallbackName, XeCallbackList PCallbacks);
extern void  XeRemoveAllCallbacks(XeWidget PW, const char* PCallbackName);
extern void  XtCallCallbacks(XeWidget PW, const char* PCallbackName, EPointer PCallData);
extern void  XeCallCallbackList(XeWidget PW, XeCallbackList PCallbacks, EPointer PCallData);

extern void  XeWidget_Redraw(XeWidget PW);

extern const char* XeRedrawPolicy_ToString(const int PDrawPolicy);

extern int   XeWidget_Reparent(XeWidget PW, XeWidget PNewParent);

EList(extern, XeDisplay*, Xe_Displays);


//--------------------------------
// Function prototypes
//--------------------------------
extern XeApp        XeApp_New(const char* PName);
extern void         XeApp_Free(XeApp PApp);
extern XeDisplay*   XeApp_GetDisplay(XeApp PApp);
extern void         XeApp_CloseDisplays(XeApp PApp);

extern EObjectClass  XeVisual_class[];

extern XeDisplay*   XeDisplay_New(void);

extern void         _XeApp_CloseMarkedDisplays(XeApp PApp);

extern void         Xe_Init(void);
extern void         Xe_Free(void);

extern EArg*       Xe_MergeArgLists(const EArg* PArgs1, const int PNArgs1,  const EArg* PArgs2, const int PNArgs2);

extern XeDisplay*   Xe_OpenDisplay(XeApp PApp, const char* PDisplayName, const char* PAppName, const char* PClassName, int* PArgc, const char* const* PArgv);
extern XeVisual*    Xe_GetVisual(XeWidget PW);
extern XeDisplay*   Xe_GetDisplay(XeWidget PW);
extern XeVisual*    Xe_VisualGet(XeDisplay* PDisplay, const int PXScreenNumber, const uint16_t PTypeFlags, const uint16_t PDepth);

extern int          Xe_QueryPointer(XeWidget PW,  int* PRX, int* PRY,  int* PX, int* PY);

extern int          Xe_DisplaySizeX(XeDisplay* PDisplay);
extern int          Xe_DisplaySizeY(XeDisplay* PDisplay);

extern XeWidget    XeWidget_New(const char* PName, XeWidgetClass* PClass, XeWidget PParent,  EArg* PArgs, const int PArgs_Count);
extern XeWidget    XeWidget_NewInsert(const char* PName, XeWidgetClass* PClass,  XeWidget PParentW, const int PInsertAtIndex,  EArg* PArgs, const int PArgsN);
extern XeWidget    XeWidget_NewInsertAfter(const char* PName, XeWidgetClass* PClass,  XeWidget PParentW, XeWidget PInsertAfterW,  EArg* PArgs, const int PArgsN);

extern XeWidget    XeWidget_WindowlessNew(const char* PName, XeWidgetClass* PClass, XeWidget PParentW,  EArg* PArgs, const int PArgsN);
extern XeWidget    XeWidget_ManagedNew(const char* PName, XeWidgetClass* PClass, XeWidget PParent, EArg* PArgs, const int PArgs_Count);
extern XeWidget    XeWidget_ManagedWindowlessNew(const char* PName, XeWidgetClass* PClass, XeWidget PParentW,  EArg* PArgs, const int PArgsN);
extern XeWidget    XeWidget_NewVa(const char* PName, XeWidgetClass* PClass, XeWidget PParentW, ...);
extern XeWidget    XeWidget_ManagedNewVa(const char* PName, XeWidgetClass* PClass, XeWidget PParentW, ...);

extern void      XeWidget_Free(XeWidget PW);

extern int       XeWidget_SetCursor(XeWidget PW, const XeCursor* PCursor);


//
// Events
//

// This valid only for XeEventHandler_Remove and XeRawEventHandler_Remove. Don't use it to select events!

#define XeEventALL  ((XeEventMask) -1L)

extern void    XeEventHandler_Add(XeWidget PW, const XeEventMask PMask, const EBool PNonMaskable, XeEventHandler PFunc, EPointer PClosure);
extern void    XeEventHandler_Insert(XeWidget PW, const XeEventMask PEventMask, const EBool PNonMaskable,  XeEventHandler PFunc, EPointer PClientData,  XeListPosition PWhere);
extern void    XeEventHandler_Remove(XeWidget PW, const XeEventMask PEventMask, const EBool PNonMaskable,  XeEventHandler PFunc, EPointer PClientData);

extern void    XeRawEventHandler_Add(XeWidget PW, const XeEventMask PEventMask, const EBool PNonMaskable,  XeEventHandler PFunc, EPointer PClientData);
extern void    XeRawEventHandler_Insert(XeWidget PW, const XeEventMask PEventMask, const EBool PNonMaskable,  XeEventHandler PFunc, EPointer PClientData,  XeListPosition PWhere);
extern void    XeRawEventHandler_Remove(XeWidget PW, const XeEventMask PEventMask, const EBool PNonMaskable,  XeEventHandler PFunc, EPointer PClientData);

extern void    XeGrab_Add(XeWidget PW, const EBool PExclusive, const EBool PSpringLoaded);
extern void    XeGrab_Remove(XeWidget PW);
extern ETime   Xe_LastTimestampProcessed_ms(XeDisplay* PDisplay);

extern int     XeCursor_InitFromBuiltIn(XeDisplay* PDisplay, XeCursor* PCursor,  const int PID);
extern int     XeCursor_InitFromXPMs(XeDisplay* PDisplay, XeCursor* PCursor,  char** PXPMImage, const EcRGBAiColor* PColor0, const EcRGBAiColor* PColor1,  char** PMaskXPMImage);
extern void    XeCursor_FreeContents(XeCursor* PCursor);

extern float   XeActiveColorFactor, XeArmColorFactor, XeButtonColorFactor, XeSliderColorFactor, XeToggleButtonIndicatorColorFactor,
               XeTopShadowFactor, XeTopShadow2Factor, XeBottomShadowFactor, XeBottomShadow2Factor,
               XeOptionMenuPaneColorFactor,
               XeGradientBrightFactor, XeGradientDarkFactor,
               XeBottomShadow2Factor,
               XeGapColorFactor,
               XeTroughColorFactor;

extern void    Xe_SetMultiClickDelay_us(XeDisplay* PDisplay, const ETime PMicroseconds);
extern ETime   Xe_GetMultiClickDelay_us(XeDisplay* PDisplay);


//----------------------------------------
// Event Gathering
//----------------------------------------
typedef unsigned long  XeInputID;
typedef void      (*XeInputCallbackFunc)(EPointer PClientData, int* PSource, XeInputID* PId);
extern XeInputID  XeApp_AddInput(XeApp PApp, int PSource, EPointer PCondition, XeInputCallbackFunc PFunc, EPointer PClientData);
extern void       Xe_RemoveInput(XeInputID PId);


//----------------------------------------
// Selection.c
//----------------------------------------
// For now, this is in XeX.h because of the XeXSelectionCallbackFunc. We need to convert that to a platform-independent type
//void Xe_GetSelectionValue(XeWidget PW, const int PSelection, const int PTarget, XeXSelectionCallbackFunc PCallback, EPointer PClientData, const ETime PTime)


extern XeTheme*  _Xe_Theme;


extern XeStyle*  Xe_PushButtonStyle;
extern XeStyle*  Xe_ArrowButtonStyle;
extern XeStyle*  Xe_ToggleButtonStyle;
extern XeStyle*  Xe_ListHighlightStyle;
extern XeStyle*  Xe_ScrollBarStyle;

extern XeStyle*  XeStylePlain;
extern XeStyle*  XeStyleGradient;

extern int       Xe_ButtonGapWidth;
extern int       Xe_ButtonHighlightThickness;

extern int*      Xe_ClassDropDownPanelImageSizeXPtr;
extern int*      Xe_ClassDropDownPanelImageSizeYPtr;

#define XeCornerRadiusUNDEFINED  -9999

extern int       Xe_ButtonCornerRadius, Xe_DefeultButtonFrameSpacing;
extern int       Xe_IconSize;
extern int       Xe_FileSelectionBoxFolderIconSize, Xe_FileSelectionBoxIconSize;

extern void  XeImage_Draw1PixelShadows(EpImage* PImage, const int PX, const int PY, const int PSizeX, const int PSizeY, const int PGapWidth,
 const EcRGBAp8Color PGapColor, const EcRGBAp8Color PTopShadowColor, const EcRGBAp8Color PBottomShadowColor);

extern void  XeImage_DrawShadows(EpImage* PImage,  const int PX, const int PY,  const int PSizeX, const int PSizeY,  const int PGapWidth, const EcRGBAf32Color* PGapColor,
	const int16_t PTLRadius, EpBand* PTLBands,  const int16_t PTRRadius, EpBand* PTRBands, const int16_t PBLRadius, EpBand* PBLBands,  const int16_t PBRRadius, EpBand* PBRBands,
  const EBool PDrawCorners,
	const int PShadowThickness, const uint16_t PType, const uint8_t PGradient, const EcRGBAiColor* PBackgroundColor, const EcRGBAiColor* PParentBackgroundColor,
	const uint16_t PInsideBlendMode, const uint16_t PInsideAlphaOp,  const uint16_t POutsideBlendMode, const uint16_t POutsideAlphaOp,  const EBool PTransparentBGMode);


#define _Xe_DrawFocusShadows(pEW, pImage, pOffset) do\
{\
	XeImage_DrawShadows(pImage, (pOffset), (pOffset),  Xe_SizeX(pEW) - (pOffset) * 2, Xe_SizeY(pEW) - (pOffset) * 2,  0, NULL,  0, NULL, 0, NULL,  0, NULL, 0, NULL,  FALSE,\
	 pEW->Primitive.HighlightThickness, XeShadowIN|LShadowSpecFlags, 0,  LBackgroundColor, LBackgroundColor,\
	 EpSRCxALPHA_plus_DSTxONE_MINUS_ALPHA, EpACCUMULATEOPACITY,  EpSRC, EpACCUMULATEOPACITY,  FALSE);\
} while(0)


extern void  _XeImage_DrawArmedButtonShadows(EpImage* PImage,  const int PX, const int PY,  const int PSizeX, const int PSizeY,
              const int16_t PTLCornerRadius, const int16_t PTRCornerRadius, const int16_t PBLCornerRadius, const int16_t PBRCornerRadius,
              const EcRGBAiColor* PNormalBackgroundColor, const EcRGBAiColor* PParentBackgroundColor,
              const EcRGBAiColor* PGapColor, const int PGapWidth,  const int PShadowThickness,
              const int16_t PInsideBlendMode, const int16_t POutsideBlendMode, const uint16_t PAlphaOp,  const EBool PTransparentModeSupported,
              const char* PDebugName);

extern void		XeImage_DrawHighlight(EpImage* PImage, const XeStyle* PStyle, const int POrientation,  const int PX, const int PY,  const int PSizeX, const int PSizeY,
               const int PHighlightBorder, const int PCornerRadius,  EcRGBAf32Color* PColor, const float PGain, const float PContrast,  const EBool PTransparentBGMode);

extern void   Xe_Warning(XeWidget PW, char* PMessage);
extern int    Xe_ConvertUnits(XeWidget PW, const int POrientation, const int PFromUnitType, const int PFromValue, const int PToUnitType);


extern int    _XeNumOfManagedChildren(XeWidget PW);

extern void   Xe_CallChangeManaged(XeWidget PW);
extern void   XeWidget_Realize(XeWidget PW);
extern void   XeWidget_Unrealize(XeWidget PW);
extern EBool  XeWidget_Realized(XeWidget PW);

extern void   XeWidget_SetSensitive(XeWidget PW, const EBool PSensitive);


extern void                 XeColor_Init(XeColor* PXeColor);
extern void                 XeColor_Set(XeColor* PXeColor, const EcRGBAiColor* PColor);
extern void                 XeColor_SetPtr(XeColor* PXeColor, EcRGBAiColor* PColor);
extern const EcRGBAiColor   XeColor_GetRGBAi(const XeColor* PColor);
extern const EcRGBAiColor*  XeColor_GetRGBAiPtr(const XeColor* PColor);


//----------------------------------------
// FontList.c
//----------------------------------------
extern const float       XeFont_Sizes[];extern const int  XeFont_Sizes_Count;

extern float             XeFont_FontSizeMin;

extern XeFont*           Xe_WireFont_Sans;
extern XeFont*           Xe_WireFont_MonospaceSans;

extern int               XeFontDebug_GlyphDrawing;

extern char*             XeFont_LabelFontName;        extern XeFontSize*  Xe_LabelFontSize;
extern char*             XeFont_LabelBoldFontName;    extern XeFontSize*  Xe_LabelBoldFontSize;
extern char*             XeFont_FixedWidthFontName;   extern XeFontSize*  Xe_FixedWidthFontSize;
extern char*             XeFont_FinePrintFontName;    extern XeFontSize*  Xe_FinePrintFontSize;
extern char*             XeFont_Header1FontName;      extern XeFontSize*  Xe_Header1FontSize;
extern char*             XeFont_MenuFontName;         extern XeFontSize*  Xe_MenuFontSize;
extern char*             XeFont_DropDownPanelFontName;extern XeFontSize*  Xe_DropDownPanelFontSize;
extern char*             XeFont_ListFontName;         extern XeFontSize*  Xe_ListFontSize;
extern char*             XeFont_TextFieldFontName;    extern XeFontSize*  Xe_TextFieldFontSize;
extern char*             XeFont_NumbersFontName;      extern XeFontSize*  Xe_NumbersFontSize;

extern XeFontList        Xe_LabelFontList, Xe_Header1FontList, Xe_DropDownPanelFontList, XeMenu_FontList, Xe_ListFontList, Xe_TextFieldFontList,
                         Xe_FixedWidthFontList, Xe_NumbersFontList;


extern XeFontListEntry*  XeFont_ListEntryNew(const char* PTag, XeFontSize* PFontSize);
extern void              XeFont_ListEntryFree(XeFontListEntry* PEntry);
extern XeFontList        XeFont_ListNew(const char* PName, XeFontSize* PFontSize);
extern void              XeFont_ListFree(XeFontList PFontList);
extern void              XeFont_ListAppendEntry(XeFontList PFontList, XeFontListEntry* PEntry);
extern XeFontListEntry*  XeFont_ListAddEntry(XeFontList PFontList, const char* PName, XeFontSize* PFontSize);
extern int               XeFont_ListSetEntry(XeFontList PFontList, const char* PFontTag, XeFontSize* PFontSize);
extern XeFontListEntry*  XeFont_ListGetDefaultEntry(XeFontList PFontList);
extern XeFontListEntry*  XeFont_ListGetEntryWithTagS(XeFontList PFontList, const char* PFontTag);

extern XeFontListEntry*  XeFont_ListGetEntryWithTag(XeFontList PFontList, const char* PFontTag);

extern XeFont*           XeFont_FontFamilyGetStyle(XeFontFamily* PFontFamily, const char* PStyleName);
extern XeFont*           XeFont_FontFamilyAddStyle(XeFontFamily* PFontFamily, const char* PFileName, const char* PStyleName, const EBool PReference);

extern int               XeFont_SizeAdd(XeFont* PFont, const float PSizeInPixels, const EBool PAntiAliasing,  XeFontSize** PResult);
extern XeFontSize*       XeFont_FindClosestSize(const XeFont* PFont, const int PHeightInPixels, const int PMaxSizeDiff, const EBool PAntiAliasing, const EBool PSysBitmapOnly);

extern int               XeFont_Load(XeFont* PFont, XeFontSize* PFontSize);
extern EIndex            XeFont_Save(XeFont* PFont,  const char* PFileName,  uint8_t** PBuffer, EIndex* PBufferSize);
extern EIndex            XeFont_LoadF(const char* PFileName,  const uint8_t* PBuffer, const EIndex PBufferSize,  XeFont** PFont);

extern int               XeFont_ParseFontName(const char* PXFontName,  char* PFoundryName, char* PFamilyName, char* PStyleStr, XeFontParams* PParams);
extern void              XeFont_GetNameBySize(const XeFont* PFont, const int PSizeInPixels, const EBool PAA, char* PName, const int PMaxLen);

extern char*             XeFontSize_GetName(XeFontSize* PFontSize);

extern EIndex            Xe_SysFontsCollect(XeDisplay* PDisplay, const EBool PDetails, const EBool PForceReload, const int PMinSizeInPixels, const uint8_t PSpacing,
                          const char* PEncoding, const char* PCharset);

extern int               XeFont_CollectFonts(const char* PFontCacheFileName, const EBool PForceReload);
extern void              XeFont_SortFonts(XeFontFamilyPtrListA* PFontFamilies);
extern void              XeFont_SortSizes(XeFont* PFont);
extern void              XeFont_AddStandardSizes(XeFont* PFont);

extern int               XeFont_CollectSysFonts(XeDisplay* PDisplay, const EBool PCollectBitmapFonts, const EBool PForceReload);

extern EIndex            XeGlyph_ByIndex(XeFont* PFont, const EIndex PGlyphIndex, const float PScale, const EIndex PNumOfLinSteps,  XeGlyph** PGlyph);
extern EIndex            XeGlyph_FromFreeType(XeFont* PFont, const EIndex PGlyphIndex, const float PScale, const EIndex PNumOfLinSteps,  XeGlyph** PGlyph);

extern EIndex            XeGlyph_DrawIntoImage(const XeGlyph* PGlyph,  const EIndex PX, const EIndex PY,  const float PScale,  const EBool PClearImage,
                          EpImage* PImage, EIndex2* PGlyphSizeInPixel);

extern XeFontFamily*    _XeFont_GetFontFamily(XeFontFamilyPtrListA* PList, const char* PFoundryName, const char* PFamilyName,  const EBool PAddIfDoesNotExist);

extern void              XeFont_GetXName(const XeFont* PFont, const int PFontSizeInPixels, char* PStr);


typedef struct
{
	XeWidget  W;
	int       SizeX;
} XeWidthAlignRec;


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

extern const char  XeTab[2];  // Special fly-weight strings

extern EBool       XeString_Equal(const char* PString0, const char* PString1);

extern EBool       XeString_Dump(const XeString PXeStr, const EBool PRaw);
extern EBool       XeString_GetLtoR(XeString PXeStr, const char* PFontTag, char** PText);
extern EIndex      XeString_Length(XeString PXeStr);
extern XeString    XeString_Copy(const XeString PXeStr);
extern XeString    XeString_Concat(const XeString PXeStr1, const XeString PXeStr2);
extern XeString    XeString_SimpleNew(const char* PText);
extern XeString    XeString_New(const char* PString, const char* PTag);

extern XeString    XeString_NewV(const EBool PEntersToSeparators, va_list PVArgs);
extern XeString    XeString_NewVa(const int PEntersToSeparators, ...);
extern XeString    XeString_AppendVa(XeString PXeStr, const EBool PFreeOrig, const int PEntersToSeparators, ...);
extern void        XeStringBinary_NewVa(const int PEntersToSeparators, XeStringBinarySegmentListA* PDst, ...);

extern void        XeString_Free(XeString PXeStr);
extern void        XeString_Extents(const int PFormat, XeFontList PFontList, XeString PXeStr, int* PSizeX, int* PSizeY);

enum
{
	XeASCII = 1,
	XeUTF8
};

extern EIndex          XeString_ConvertToBinary(const XeVisual* PVisual,  const int PFormat, const XeFontList PFontList, const EcRGBAiColor* PForegroundColor,
                        const XeString PXeStr,  XeStringBinarySegmentListA* PSegmentsRet);

extern EIndex          XeString_8NumOfSegments(const char* PString);
extern EIndex          XeString_8ConvertToBinary(XeFontList PFontList, const char* PString,  XeStringBinarySegmentListA* PSegmentsRet);
extern EIndex          XeString_8ConvertToBinaryNoAlloc(XeFontList PFontList, const char* PString,  XeStringBinarySegment* PSegments, const EIndex PNSegments);
extern EIndex          XeString_UTF8ConvertToBinary(XeFontList PFontList, const char* PString,  XeStringBinarySegmentListA* PSegmentsRet);
extern EIndex          XeString_WConvertToBinary(XeFontList PFontList, const wchar_t* PString,  XeStringBinarySegmentListA* PSegmentsRet);

extern void            XeStringBinary_Dump(const XeStringBinarySegmentListA* PSegments);

extern XeTextPosition  XeStringBinary_Length(const XeStringBinarySegmentListA* PSegments);
extern char*           XeStringBinary_GetString8(const XeStringBinarySegmentListA* PSegments,  XeTextPosition* PLength);
extern char*           XeStringBinary_GetSubString8(const XeStringBinarySegmentListA* PSegments, const XeTextPosition PStart, const XeTextPosition PEnd);

extern char*           XeStringBinary_GetStringUTF8(const XeStringBinarySegmentListA* PSegments,  XeTextPosition* PLength);

extern void            XeStringBinarySegment_SetFont(XeStringBinarySegment* PSegment, XeFontListEntry* PFontListEntry);
extern void            XeStringBinarySegment_SetUTF8(XeStringBinarySegment* PSegment, XeVisual* PVisual, XeFontSize* PFontSize, const EcRGBAiColor* PColor, const char* PString);

extern void	           XeStringBinary_Free(XeStringBinarySegmentListA* PSegments);

extern int                     XeStringBinary_Copy(const XeStringBinarySegmentListA* PSrc, XeStringBinarySegmentListA* PDst);
extern XeStringBinarySegment*  XeStringBinary_InsertSegment(XeStringBinarySegmentListA* PSegments,  const EIndex PBefore);
extern XeStringBinarySegment*  XeStringBinary_InsertSegments(XeStringBinarySegmentListA* PSegments,  const EIndex PNumOfNewSegments, const EIndex PBefore);

extern void          XeStringBinary_UpdateFonts(XeStringBinarySegmentListA* PSegments,  XeFontList PFontList);

extern XeCoordinate  XeString_WCharPosToPixelX(XeFontSize* PFontSize,  const wchar_t* PWStr,  const EIndex PCharPos);
extern XeCoordinate  XeStringBinary_CharPosToPixelX(const XeStringBinarySegmentList* PSegments,  const XeStringRenderingSettings* PRenderSettings, const EIndex PCharPos);

extern int           XeString_WGetClosestCharToPositionX(XeFontSize* PFontSize,  const wchar_t* PWStr, const int PStringLen,  const EIndex PX,  EIndex* PTextXP);

extern EBool         XeString_WGetExtents(XeFontSize* PFontSize,  const wchar_t* PString, XeCoordinate* PCharacterPositionsX, const EIndex PStartChar, const EIndex PStringLen,
                      XeStringMetrics* PStringMetrics);

extern void          XeStringBinary_Extent(const XeStringBinarySegmentListA* PSegments,  const XeStringRenderingSettings* PRenderSettings, const EBool PComputeSegmentSizes,
                      XeStringMetrics* PResult);

extern void          XeStringBinary_ExtentExt(XeStringBinarySegment* PSegments, const EIndex PNumOfSegments, const XeStringRenderingSettings* PRenderSettings,
                      const EBool PComputeSegmentSizes,  XeStringMetrics* PResult);

extern int           XeStringBinary_PixelXToCharPos(const XeStringBinarySegmentList* PSegment,  const EIndex PXIn);
extern EBool         XeStringBinary_Equal(const XeStringBinarySegmentListA* PBinaryString0, const XeStringBinarySegmentListA* PBinaryString1);

extern void      XeStringDrawContext_Init(XeStringDrawContext* PContext);
extern void      XeStringDrawContext_FreeContents(XeDisplay* PDisplay, XeStringDrawContext* PContext);

extern void      XeStringBinary_UpdateColors(const XeVisual* PVisual, const EcRGBAiColor* PForegroundColor,  XeStringBinarySegmentListA* PSegments);

extern void      XeStringBinary_Draw(XeStringDrawContext* PContext, const XeVisual* PVisual,  EpImage* PTargetImage,  const uint16_t PBlendMode, const uint16_t PAlphaOp,
                  const int PDrawableSizeX, const int PDrawableSizeY, const float PBlurRadius,
                  const EcRGBAiColor* PBackgroundColor, const EcRGBAiColor* PForegroundXColor, const EBool PUseColorsInString,  const int PX, const int PY,  int PFullSizeX,
                  const XeStringBinarySegmentListA* PSegments,  const XeStringRenderingSettings* PRenderSettings, const int PAlignment);

extern EIndex    Xe_DrawString(XeStringDrawContext* PContext,  const XeVisual* PVisual,  const char* PStr, const wchar_t* PWStr, const EIndex PStartChar,
                  const EIndex PGlyphsToDraw,  const XeCoordinate PSizeX, const XeCoordinate PSizeY,  XeFontSize* PFontSize,  EpImage* PTargetImage,
                  const uint16_t PBlendMode, const uint16_t PAlphaOp,  const float PBlurRadius,  const XeCoordinate PDstX, const XeCoordinate PDstY,
                  const float PMaxAscent,  XeCoordinate* PAdvanceXP);

extern void      XeToolTipTimer(EPointer PClientData, XeIntervalId* PTID, const int PCount);
extern void      XeToolTipPopup(XeWidget PW);
extern void      XeToolTipPopdown(XeWidget PW);


extern EBool     Xe_SetFocus(XeWidget PW, const EBool PSendFocusLostEvent, const EBool PSendFocusGetEvent);
extern EBool     Xe_HasFocus(XeWidget PW);

extern void      XeTraversal_SetRing(XeWidget* PTWs, const int PTrN);


typedef struct
{
	XeWidget*  Widgets;int  Widgets_Count;
	uint8_t    Direction;  // XeLEFT only, for now
	int        Indent;
} XeIndentSet;


extern XeIndentSet*  XeWidgets_IndentFromLeft(XeWidget* PWidgets, const int PNumOfWidgets, const int POffset);
extern void          XeWidgets_IndentUpdate(void);
extern void          XeIndentSet_Remove(XeIndentSet* PSet);

extern int       Xe_GetPreferredSize(XeWidget PW,  const XeCoordinate PSuggestedSizeX, const XeCoordinate PSuggestedSizeY,  XeCoordinate* PSizeX, XeCoordinate* PSizeY);


extern void      Xe_GeometryUpdate(XeWidget PW, const uint8_t PUpdateFlags);
extern int       Xe_GeometryUpdateDisable(XeWidget PW);
extern void      Xe_GeometryUpdateEnable(XeWidget PW, const uint8_t PUpdateFlags);

extern void      Xe_BranchRedraw(XeWidget PW, const uint8_t PUpdateFlags, const EBool PUnmanaged, const EBool PClearDrawCaches);

extern void      Xe_CallExposeForWindowlessChildren(XeCompositeWidget PEW, const EBool PMarkedOnly, XeRedrawEvent* PEvent);

extern EBool     XeIsSubclass(const XeWidget PW, const XeWidgetClass* PClass);
extern int       XeWidget_GetIndex(XeWidget PW);



// Resources
//
extern const char*  XeVaNestedList;
extern const char*  XeVaTypedArg;

extern void      Xe_SetValues(XeWidget PW, EArg* PArgs, Cardinal PNumOfArgs);
extern void      Xe_SetValuesVa(XeWidget PW, ...);
extern void      Xe_GetValues(XeWidget PW, EArg* PArgs, const int PNArgs);
extern void      Xe_GetValuesVa(XeWidget PW, ...);

extern void      Xe_GetResourceList(XeWidgetClass* PWidgetClass, XeResource** PResourcesR, Cardinal* PNResourcesR);



extern void      XeApp_Message(XeApp PApp, const int PType, const char* PName, const char* PTypeName, const char* PFunction, const char* PFile, const int PLine,
                  const char* PMessage,  const char** PParams, const int PNParams);

#define XeAppWarning(pApp, pName, pTypeName, pMessage,  pParams, pNParams)\
 XeApp_Message(pApp, ELogWARNING, pName, pTypeName, __FUNCTION__, __FILE__, __LINE__, pMessage,  pParams, pNParams)

#define XeAppError(pApp, pName, pTypeName, pMessage,  pParams, pNParams)\
 XeApp_Message(pApp, ELogERROR, pName, pTypeName, __FUNCTION__, __FILE__, __LINE__, pMessage,  pParams, pNParams)


//
// Manage.c
//
extern void    XeWidget_Map(XeWidget PW);
extern void    XeWidget_Unmap(XeWidget PW);

extern void    XeWidget_Manage(XeWidget PW);
extern void    XeWidgets_Manage(XeWidget* PWidgets, const EIndex PNumOfChildren);
extern void    XeWidget_Unmanage(XeWidget PW);
extern void    XeWidgets_Unmanage(XeWidget* PWidgets, const EIndex PNumOfChildren);
extern void    XeWidgets_ChangeManage(const XeChangeManage* PChildren, const EIndex PNumOfChildren);

//
// Event.c
//
extern EBool   Xe_CallAcceptFocus(XeWidget PW, ETime* PTime);
extern void    XeApp_AddActions(XeApp PApp, XeActionList PActions, const int PNActions);
extern void    XeApp_MainLoop(XeApp PApp);
extern void    XeApp_SetExitFlag(XeApp PApp);

extern void    Xe_TranslateCoords(XeWidget PW,  const int PX, const int PY,  int* PRootXRet,  int* PRootYRet);

//
// X11/Event.c
//
extern XeIntervalId  XeTimeOut_Add(const ETime PMilliseconds, XeTimerCallbackFuinc PCallback, EPointer PClientData);
extern XeIntervalId  XeTimeOut_AddMicro(const ETime PMicroseconds, XeTimerCallbackFuinc PCallback, EPointer PClientData);
extern XeIntervalId  XeTimer_Add(const int PMilliseconds, XeTimerCallbackFuinc PCallback, EPointer PClientData);
extern XeIntervalId  XeTimer_AddMicro(const ETime PMicroseconds, XeTimerCallbackFuinc PCallback, EPointer PClientData);
extern void          XeTimer_Remove(XeIntervalId PID);
extern void          XeTimer_SetInterval(const XeIntervalId PID, const ETime PMilliseconds);
extern void          XeTimer_SetIntervalMicro(const XeIntervalId PID, const ETime PMicroseconds);

extern int           XeApp_Sync(XeApp PApp);


// Global variables
//
typedef struct       XeDropShadowGL_S  XeDropShadowGL;

extern XeDropShadowGL  XeShell_DropShadow;
extern XeCoordinate    XeShell_CornerRadius;

extern EBool         Xe_MultiThreaded;

extern EcRGBAiColor  Xe_BackgroundRGBAiColor,
                     Xe_ForegroundRGBAiColor,
                     Xe_LinkRGBAiColor,

                     Xe_BlackRGBAiColor,
                     Xe_TransparentBlackRGBAiColor,
                     Xe_WhiteRGBAiColor,
                     Xe_RedRGBAiColor,
                     Xe_GreenRGBAiColor,
                     Xe_BlueRGBAiColor,
                     Xe_CyanRGBAiColor,
                     Xe_MagentaRGBAiColor,
                     Xe_YellowRGBAiColor,

                     Xe_TroughRGBAiColor,

                     Xe_SliderRGBAiColor,

                     Xe_ListBackgroundRGBAiColor,
                     Xe_ListBackgroundAlternateRGBAiColor,
                     Xe_ListItemHighlightRGBAiColor,

                     XeMenu_BackgroundRGBAiColor,
                     XeMenu_ForegroundRGBAiColor,
                     XeMenu_ButtonArmRGBAiColor,

                     Xe_TextFieldBackgroundRGBAiColor,
                     Xe_TextFieldForegroundRGBAiColor,
                     Xe_TextFieldFocusRGBAiColor,
                     Xe_TextFieldTextSelectionRGBAiColor,

                     Xe_ButtonRGBAiColor,
                     Xe_ButtonActiveRGBAiColor,
                     Xe_ToolTipBackgroundRGBAiColor,
                     Xe_ScrollbarSliderRGBAiColor,

                     Xe_HighlightRGBAiColor,
                     Xe_ProgressBarRGBAiColor,
                     Xe_ToggleButtonLEDRGBAiColor;

extern EcRGBAiColor  Xe_TransparentRGBAiColor, Xe_TransparentRGBAiColorA1;

extern EcRGBAiColor  Xe_GapRGBAiColor,
                     XeListHeader_ButtonRGBAiColor, XeListHeader_ButtonArmedRGBAiColor,
                     XeOptionMenu_RGBAiColor;

extern uint32_t      Xe_ForegroundRGBA8pColor;

extern EcRGBAf32Color  Xe_TransparentBlackRGBAf32Color;


extern void          Xe_SetSensitive(XeWidget PW, const EBool PSensitive);

extern void          Xe_TranslateKeycode(XeDisplay* PDisplay, const XeKeyCode PKeyCode, XeModifiers PModifiers, XeModifiers* PModifiersR, XeKeySym* PKeysymR);
extern void          Xe_TranslateKey(XeDisplay* PDisplay, const XeKeyCode PKeyCode, XeModifiers PModifiers, XeModifiers* PModifiersRet, XeKeySym* PKeysymRet);

extern void          XeWidget_SetKeyboardFocus(XeWidget PSubtree, XeWidget PDescendant, const EBool PSendFocusLostEvent, const EBool PSendFocusGetEvent);
extern XeWidget      Xe_KeyboardFocusWidgetGet(XeWidget PW);

extern void          Xe_GrabKey(XeWidget PW, const XeKeyCode PKeyCode, const XeModifiers PModifiers, const EBool POwnerEents, int PPointerMode, int PKeyboardMode);
extern void          Xe_UngrabKey(XeWidget PW, const XeKeyCode PKeyCode, const XeModifiers PModifiers);
extern int           Xe_GrabKeyboard(XeWidget PWidget, const EBool POwnerEvents, const int PPointerMode, const int PKeyboardMode, const ETime PTime);
extern void          Xe_UngrabKeyboard(XeWidget PW, ETime PTime);

extern void          Xe_UngrabButton(XeWidget PW, const unsigned int PButton, const XeModifiers PModifiers);

extern void          Xe_UngrabPointer(XeWidget PW, const ETime PTime);


#define _Xe_GetRGBA8pColors()\
	const EBool     LDarkMode = XeColorTheme_IsDark();\
	const uint32_t  LGreenRGBA8p = LDarkMode ? EcRGBAp8Color_i(0x00C000FF) : EcRGBAp8Color_i(0x008000FF);(void)LGreenRGBA8p;\
	const uint32_t  LOrangeRGBA8p = LDarkMode ? EcRGBAp8Color_i(0xB08000FF) : EcRGBAp8Color_i(0xA06000FF);(void)LOrangeRGBA8p;\
	const uint32_t  LYellowRGBA8p = LDarkMode ? EcRGBAp8Color_i(0xE0E000FF) : EcRGBAp8Color_i(0x906000FF);(void)LYellowRGBA8p;\
	const uint32_t  LRed1RGBA8p = LDarkMode ? EcRGBAp8Color_i(0x200000FF) : EcRGBAp8Color_i(0x400000FF);(void)LRed1RGBA8p;\
	const uint32_t  LRed2RGBA8p = LDarkMode ? EcRGBAp8Color_i(0x401000FF) : EcRGBAp8Color_i(0x600000FF);(void)LRed2RGBA8p;\
	const uint32_t  LRed3RGBA8p = LDarkMode ? EcRGBAp8Color_i(0x602000FF) : EcRGBAp8Color_i(0x800000FF);(void)LRed3RGBA8p;\
	const uint32_t  LRed4RGBA8p = LDarkMode ? EcRGBAp8Color_i(0xE05030FF) : EcRGBAp8Color_i(0xC00000FF);(void)LRed4RGBA8p;\
	const uint32_t  LBlue1RGBA8p = LDarkMode ? EcRGBAp8Color_i(0x000020FF) : EcRGBAp8Color_i(0x000040FF);(void)LBlue1RGBA8p;\
	const uint32_t  LBlue2RGBA8p = LDarkMode ? EcRGBAp8Color_i(0x000040FF) : EcRGBAp8Color_i(0x000060FF);(void)LBlue2RGBA8p;\
	const uint32_t  LBlue3RGBA8p = LDarkMode ? EcRGBAp8Color_i(0x204060FF) : EcRGBAp8Color_i(0x000080FF);(void)LBlue3RGBA8p;\
	const uint32_t  LBlue4RGBA8p = LDarkMode ? EcRGBAp8Color_i(0x40A0FFFF) : EcRGBAp8Color_i(0x0000C0FF);(void)LBlue4RGBA8p


extern const EObjectClass  XeFontFamily_class[];
extern const EObjectClass  XeFont_class[];
extern const EObjectClass  XeFontSize_class[];

extern const float         XeFont_AAMult;
EListA_s(extern, XeFontFamilyPtr, XeFont_Families);
EList(extern, char*, XeFont_Paths);



extern void            XeGlyph_Size(const XeGlyph* PGlyph, const float PScale, EIndex2* PGlyphSizeInPixels);
extern int             XeGlyph_Kerning(const XeGlyph* PGlyph, const EIndex PPrevGlyphIndex, EFloat32_2* PKerning);


extern XeFontSize*     XeFontSize_New(XeDisplay* PDisplay, const char* PFontName, float* PAntiAliasing, const EBool PFallbackToDefault);
extern XeFontCharSet*  XeFontSize_CharSetAdd(XeFontSize* PFontSize, const char* PName);
extern void            XeFontSize_FreeContents(XeFontSize* PFontSize);
extern EBool           XeFontSize_HasLatin1(XeFontSize* PFontSize);

extern EBool           XeFont_HasKerning(const XeFont* PFont);
extern EIndex          XeFont_CharIndex(const XeFont* PFont, const EIndex PCharacter);
extern void            XeFont_FreeContents(XeFont* PFont);

extern XeFontSize*     XeDisplay_DefaultFontSize(XeDisplay* PDisplay);

extern int             XeFontSize_GlyphLoadAndRender(XeFontSize* PFontSize, const int PGlyphIndex, const EBool PAA, const EBool PSetResolution,  XeGlyphRaster** PResult);
extern int             XeFontSize_GlyphsLoadAndRender(XeFontSize* PFontSize, const int PEndCode, const EBool PAA);

extern void            XeFont_Init(void);
extern void            XeFont_Exit(void);

extern void        XeImage_DrawButton(EpImage* PImage,  const XeStyle* PStyle, const int POrientation,  const int PX, const int PY,  const int PSizeX, const int PSizeY,
 const int PGapWidth, const EcRGBAiColor* PGapColor, const int PShadowThickness,
 const int PTLCornerRadius, const int PTRCornerRadius, const int PBLCornerRadius, const int PBRCornerRadius,
 const EcRGBAiColor* PBackgroundColor, const EcRGBAiColor* PParentBackgroundColor, const float PGain, const float PContrast,  const EBool PTransparentMode);

extern void        XeImage_DrawButtonArmed(EpImage* PImage,  const XeStyle* LStyle, const int POrientation,
 const int PX, const int PY, const int PSizeX, const int PSizeY,
 const int PGapWidth, const EcRGBAiColor* PGapXColor, const int PShadowThickness,
 const int PTLCornerRadius, const int PTRCornerRadius, const int PBLCornerRadius, const int PBRCornerRadius,
 const EcRGBAiColor* PBackgroundColor, const EcRGBAiColor* PNormalBackgroundColor, const EcRGBAiColor* PParentBackgroundColor, 
 const uint16_t PInsideBlendMode, const uint16_t POutsideBlendMode, const uint16_t PAlphaOp,
 const EBool PTransparentBGMode,
 const char* PDebugName);


extern void		     XeImage_DrawLeftRightVLines(EpImage* PImage,  const int PSizeX, const int PSizeY,  const EcRGBAiColor* PLeftLineColor,  const EcRGBAiColor* PRightLineColor);
extern void        XeImage_DrawGrip(EpImage* PImage,  const int PX, const int PY,  const int PSizeX, const int PSizeY,  const EcRGBAiColor* PHColor, const EcRGBAiColor* PDColor);

extern EBool       Xe_IsRealized(XeWidget PW);
extern EBool       Xe_ChainIsManaged(XeWidget PW);

extern EBool       XeWidget_IsManaged(XeWidget PW);

#ifndef _XeP_h
extern XeWidget    Xe_Parent(XeWidget PW);
extern EBool       Xe_IsSensitive(XeWidget PW);
extern int         Xe_SizeX(XeWidget PW);
extern int         Xe_SizeY(XeWidget PW);
#endif

typedef struct
{
	XeWidget       FrameW, DrawW;
	EpImage*       Image;
	EpImage*       BackgroundImage;  // To show alpha channel (e.g. a checker-board)
	EcRGBAiColor   BackgroundColor;

	EIndex2        PreferredSize, Size;

	EpImage*       TmpImage;             // Temporary Image. E.g. for scaling down Image before alpha-compositing to DisplayImage
	EpImage*       DisplayImage;
	int            BlendFunction;        // EpSRCxALPHA_plus_DSTxONE_MINUS_ALPHA etc.

	EpToneMapper   ToneMapper;
} XeImageSwatch;


extern XeImageSwatch*  XeImageSwatch_New(const char* PName, XeWidget PParentW,  const EIndex PPreferredSizeX, const EIndex PPreferredSizeY,  ...);
extern void            XeImageSwatch_Free(XeImageSwatch* PSwatch, const EBool PFreeStruct);
extern void            XeImageSwatch_UpdateVa(XeImageSwatch* PSwatch,  const EIndex PPreferredSizeX, const EIndex PPreferredSizeY,  ...);

typedef void  (*XeErrorHandler)(const uint8_t PType, const char* PMessage);

extern XeErrorHandler  XeApp_SetErrorHandler(XeApp PApp, XeErrorHandler PFunc);

extern const EcRGBAiColor*  Xe_InheritBackgroundColor(XeWidget PW);
extern int                  Xe_InheritBackgroundImage(XeWidget PW, EpImage** PImage);
extern int                  Xe_CollectBackgroundLayers(XeWidget PW, XeLayer* PLayers, const int PMaxLayers);
extern EBool                Xe_HaveBackgroundBlending(XeWidget PW);


extern const char    _XePChars[];
extern EpImage*      Xe_XPMToImageVa(char** PXPM, ...);

extern XeApp         XeWidget_App(XeWidget PW);

extern XeWidget      Xe_ToolBarItemRealize(XeWidget PParentW, XeDWidget PItem, XeWidget PBeforeW,  int32_t* PImageSizeXPtr, int32_t* PImageSizeYPtr);

extern const EpImage*  Xe_ScaledImageFind(EpImage* PImage,  const EIndex PSizeX, const EIndex PSizeY, const EBool PAddIfDoesNotExist);


extern XeApp      XeDisplay_ToApp(XeDisplay* PDisplay);

typedef void      (*XeKeyFunc)(XeDisplay* PDisplay, const XeKeyCode PKeycode, XeModifiers PModifiers, XeModifiers* PModifiersR, XeKeySym* PKeysymR);
typedef void      (*XeCaseProc)(XeDisplay* PDisplay, XeKeySym PKeysym, XeKeySym* PLowerReturn, XeKeySym* PUpperReturn);
extern void       Xe_KeysymToKeycodeList(XeDisplay* PDisplay, XeKeySym PKeySym, XeKeyCode** PKeyCodesRet, Cardinal* PKeyCountRet);
extern XeKeySym*  Xe_GetKeysymTable(XeDisplay* PDisplay, XeKeyCode* PMinKeyCodeRet, int* PKeysymsPerKeycodeRet);
extern void       Xe_KeyTranslatorSet(XeDisplay* PDisplay, XeKeyFunc PFunc);

extern EBool      XeEvent_DispatchToWidget(XeEvent* PEvent, XeWidget PW);


//================================================================
// Translation Management
//================================================================

typedef struct _XeTranslationData_S*  XeTranslations;
typedef struct _XeTranslationData_S*  XeAccelerators;

extern XeTranslations  XeParseTranslationTable(const char* PStr);
extern XeAccelerators  XeParseAcceleratorTable(const char* PStr);
extern void            XeOverrideTranslations(XeWidget PW, XeTranslations PTrans);
extern void            XeAugmentTranslations(XeWidget PW, XeTranslations PTrans);

extern void   XeUninstallTranslations(XeWidget PW);

extern void   XeCallActionFunc(XeWidget PW, const char* PAction, XeEvent* PEvent,  String* PParams, Cardinal PNParams);



#ifdef __APPLE__
extern int  Xe_CocoaAppStart(void);
extern int  Xe_CocoaDialog(void);
#endif


#ifndef XeProgressIndicator_DEF
#define XeProgressIndicator_DEF
typedef struct XeProgressIndicator_S  XeProgressIndicator;
#endif

struct XeProgressIndicator_S
{
	XeWidget  MessageLabelW, ProgressW, AbortW;
	void      (*AbortCallback)(EPointer PClientData);
	EPointer  AbortClientData;
};

extern void  XeProgressIndicator_Init(XeProgressIndicator* PProgressIndicator, XeWidget PParentW,  const EArg* PArgs, const int PNArgs);

//
// Deferred Widgets
//

#define XeItemFree(p)  { if(p) XeDWidget_Free(p);p = NULL; }


typedef void*  XeDWidgetClass;

// Public global variables and functions
//
extern int   XeApp_InitDisplay(XeApp PApp, const int PArgs_Count, const char* const* PArgs,  const EArg* PArgList, const int PArgCnt);

extern void  XeDWidget_Free(XeDWidget PItem);

extern XeDWidget  Xe_AddCustomWidget(XeApp PApp, const char* PLocation, const char* PName, XeCallbackFunc PCallback);

extern void   XeColorTheme_SetDark(void);
extern void   XeColorTheme_SetLight(void);
extern EBool  XeColorTheme_IsDark(void);
extern void   Xe_UpdateColors(void);


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

#endif // _Xe_h
