#ifndef __wind__H
#define __wind__H

#include "WimpLib:std.h"
#include <stdarg.h>

#include <stdbool.h>
#include "WimpLib:Coords.h"
#include "WimpLib:CIcon.h"
#include "WimpLib:EventSender.h"
#include "WimpLib:Sprites.h"

#ifdef __cplusplus
extern "C" {
#endif

#define EWind_Moveable          0x00000002
#define EWind_NoRedraw          0x00000010
#define EWind_Pane              0x00000020
#define EWind_NoLimits          0x00000040
#define EWind_Scroll_1          0x00000100
#define EWind_Scroll_2          0x00000200
#define EWind_StayonBackground  0x00000800
#define EWind_GrabHotKeys       0x00001000
#define EWind_ForceOnScreen     0x00002000
#define EWind_IgnoreRightExtent 0x00004000
#define EWind_IgnoreLeftExtent  0x00008000
#define EWind_IsOpen            0x00010000
#define EWind_IsOnTop           0x00020000
#define EWind_IsFullSize        0x00040000
#define EWind_ClickToggle       0x00080000
#define EWind_HasFocus          0x00100000
#define EWind_WillForceOnScreen 0x00200000
#define EWind_ForegroundWindow  0x00800000
#define EWind_BackIcon          0x01000000
#define EWind_CloseIcon         0x02000000
#define EWind_TitleBar          0x04000000
#define EWind_ToggleSize        0x08000000
#define EWind_VScroll           0x10000000
#define EWind_Resize            0x20000000
#define EWind_HScroll           0x40000000
#define EWind_NewFormat       (~0x7fffffff)

#define EWindX_FullTitleColor       0x01
#define EWindX_ExtendScrollRequests 0x02
#define EWindX_No3DBorder           0x04
#define EWindX_Always3DBorder       0x08
#define EWindX_ShadedPointerInfo    0x10

typedef struct
{
	HWind       w;
	CWindCvt    cvt;
	HWind       behind;
} CWindOpen;

typedef struct
{
	CWindOpen   o;
	uint32_t    flags;
} CWindState;

typedef struct
{
	CWindState    o;
	unsigned char title_fg;
	unsigned char title_bg;
	unsigned char work_fg;
	unsigned char work_bg;
	unsigned char scroll_inner;
	unsigned char scroll_outer;
	unsigned char highlight_bg;
	unsigned char extra_flags;
	CRect         ex;
	uint32_t      title_flags;
	uint32_t      work_flags;
	CSpriteArea*  sprite_area;
	short         min_width;
	short         min_height;
	CIconData     title_data;
	uint32_t      nicons;
} CWind;

typedef struct
{
	HWind       w;
	CWindCvt    cvt;
	CRect       bounds;
} CWindRedraw;

typedef struct CTemplate CTemplate;

HWind throw_Window_CreateCustom(CTemplate* t, CSpriteArea* pSpriteArea, const char* phelpid);
HWind throw_Window_Create(CTemplate* t, const char* phelpid);
HWind throw_Window_CreateFrom(const char* pid, const char* phelpid);
void Window_Delete(HWind id);

void throw_Window_RegisterEventHandler(HWind id, Listener_FOnEvent pEventHandler, void *pHandle, bool bFirst);
void Window_DeRegisterEventHandler(HWind id, Listener_FOnEvent pEventHandler, void *pHandle);
bool Window_ProcessEvent(HWind id, const Event* e);

void Window_WimpOpen(CWindOpen* o);
void Window_Open(HWind id);
void Window_OpenBehind(HWind id, HWind behind);
void Window_OpenAt(HWind id, CWindOpen* o);
bool Window_ScrollToRect(CWindOpen* o, const CRect* prct);
#define pane_fit_hgadgets 1
#define pane_fit_vgadgets 2
void Window_OpenPane(CWindOpen* o, const CWindOpen* maino, const CRect* pbox, unsigned int flags);
bool Window_IsOpen(HWind id);
void Window_WimpClose(HWind id);
void Window_Close(HWind id);

void throw_Window_AddPane(HWind id, HWind pane);
void Window_RemovePane(HWind id, HWind pane);
CWindOpen Window_GetOwnerInfo(HWind id);

CWindCvt Window_GetPosInfo(HWind id);
CWindState Window_GetState(HWind id);
CWind Window_GetInfo(HWind id);
CRect Window_GetOutline(HWind id);
CRect Window_GetExpectedOutline(CWindOpen* o);

void Window_FitInExtent(HWind id, CWindOpen* po);
void Window_SetExtent(HWind id, const CRect* box, bool fix_display);
#ifdef __CC_NORCROFT
#pragma -v1 // hint to the compiler to check f/s/printf format
#endif
void Window_SetTitle(HWind id, const char* pformat, ...);
#ifdef __CC_NORCROFT
#pragma -v0 // return to default
#endif
void Window_VSetTitle(HWind id, const char* pformat, va_list arg);

bool Window_ScrollIfMouseOnBorder(HWind id, int dx, int dy);

typedef void (*Window_Drawer)(void* handle, const CWindRedraw* pdraw);
void Window_Invalidate(HWind w, const CRect* pbox);
void Window_Update(HWind w, const CRect* pbox, Window_Drawer pdraw, void* handle);
void Window_Redraw(HWind w, Window_Drawer pdraw, void* handle);

HTask Window_GetTask(HWind w, HIcon i);
void Window_SetHelp(HWind w, const char* phelpid);

#ifdef __cplusplus
}
#endif

#endif
