//****************************************************************
//* Part 0: My data types
//****************************************************************
#define  False 0
#define  True  !(False)

typedef struct {
	int MinX, MinY, MaxX, MaxY;
} t_BoxenLuder;

//****************************************************************
//* Part 1: RISC OS data types
//****************************************************************

// For some things I get so-called Handles
typedef int t_TaskHandle;
typedef int t_WindowHandle;
typedef int t_IconHandle;

//****************************************************************
// Icons

typedef int t_SpriteArea;

// Icon data
typedef union {
	char IconText[12];
	struct {
		char *Text;
		char *Validation;
		int Length;
	}	IconTextIndirect;
	struct {
		char *Sprite;
		t_SpriteArea *Area;
		int Length;
	}	IconSpriteIndirect;
} t_IconData;

// Icon
typedef struct {
	t_BoxenLuder BoundingBox;
	unsigned IconFlags;
	t_IconData IconData;
} t_Icon;

// Some useful constants
#define IconFlags_IconBar 0x3102 // Sprite, Indirect, Click
#define IconFlags_TextWhiteDirect 0x7000021 // Black on white, Text
#define IconFlags_TextWhiteIndirect 0x7000121
#define IconFlags_TextTransparentDirect 0x7000001 // Black on white, Text
#define IconFlags_TextTransparentIndirect 0x7000101
#define IconFlags_WindowTitleDirect 0x2700003d 
#define IconFlags_WindowTitleIndirect 0x2700013d 

#define IconBar_IconLeft (t_WindowHandle)(-2)
#define IconBar_IconRight (t_WindowHandle)(-1)

#define WimpSpriteArea (t_SpriteArea*)1

//****************************************************************
// Keys
#define KeyShift 0
#define KeyCtrl 1
#define KeyAlt 2
#define KeyOffsetLeft 3
#define KeyoffsetRight 6

//****************************************************************
// Windows

typedef struct {
	t_BoxenLuder Visible;
	int  ScrollX, ScrollY;
	t_WindowHandle WindowStack; // -1
	unsigned WindowFlags;
	char TitleForground; //7
	char TitleBackground; // 2
	char WorkareaForground; // 7
	char WorkareaBackground; // 1
	char ScrollbarBackground; //3
	char ScrollbarForeground; // 1
	char TitleFocusBackground; // 12
	char ExtraFlags;
	t_BoxenLuder Workarea;
	unsigned TitleIconFlags;
	int WorkareaButtonType;
	t_SpriteArea *ToolSpriteArea; // -1 (WimpSpriteArea)
	int MinArea; // Width & Height in 16 Bit each!
	t_IconData TitleIconData;
	int IconCount;
} t_WindowHeader;

typedef struct {
	t_WindowHeader WindowHeader;
	t_Icon WindowIcon[];
} t_Window;

#define Window_OpenInFront (t_WindowHandle)(-1)

// Flag bits
//  0	Old format flags I do not use
//  1	Moveable
//  2 	Old format flags I do not use
//  3 	Old format flags I do not use
//  4	Wimp can manage redraw alone
//  5	It is a pane
//  6	Window may be moved off-screen
//  7 	Old format flags I do not use
//  8	I want scroll request events with auto repeat
//  9	As above but not auto repeat
// 10	Window colors are in GCOL format
// 11	No window may be behind this one
// 12	I want hot keys events usind Wimp_ProcessKey
// 13	Windows must remain on-screen
// 14	Ignore horizontal resize
// 15	Ignore vertical resize
// The next 5 bits are filled by Wimp_GetWindowState
// 16	Window is open
// 17	Window is completely visible
// 18	Window has its max size
// 19	The Open_Window_Request has been triggered by resize click
// 20	Window has input focus
// 21 	Next Open_Window will force window on-screen (set by Wimp as needed)
// 22	reserved, must be 0
// 23	reserved, must be 0
// The next 8 bits replace the "old format flags" if bit 31 is set
// 24 	Window has put-to-back icon
// 25 	Window has close icon
// 26 	Window has title bar
// 27 	Window has toggle size icon
// 28 	Window has vertical scroll bar
// 29 	Window has resize icon
// 30 	Window has horizontal scroll bar
// 31 	If set the bits 24-30 are used instead of th eole bits 0, 2, 3, and 7

// Use bit 24-30, title bar, wimp can draw alone
// 1000 0100 0000 0000 0000 0000 0101 0000
#define WindowFlags_InfoWindow 0x84000050
// As above but with close tool and moveable
// 1000 0110 0000 0000 0000 0000 0101 0010
#define WindowFlags_SimpleWindow 0x86000052
// Window with scroll bars that is resizeable
// 1111 1111 0000 0000 0000 0000 0101 0000
#define WindowFlags_AllToolsWindow 0xFF000052
#define WindowFlags_AllToolsWindowRedraw 0xFF000042


//****************************************************************
// Menus

typedef struct {
	union {
		char Text[12];
		t_IconData IconData;
	} Title;
	char TitleForeground; // 7
	char TitleBackground; // 2
	char BodyForeground; // 7
	char BodyBackground; // 0
	int Width; // Automatic
	int Height; // 44
	int Gap; //0
} t_MenuHeader;

// Since MenuItem needs Menu and vice versa this is the trick
typedef struct t_Menu t_Menu;

typedef struct {
	unsigned MenuFlags;
	union {
		t_Menu *SubMenu;
		t_WindowHandle Window;
		int Warning;
	} MenuLink;
	unsigned IconFlags;
	t_IconData IconData;
} t_MenuItem;

struct t_Menu {
	t_MenuHeader MenuHeader;
	t_MenuItem MenuItem[];
};

#define MenuFlags_Standard 0
#define MenuFlags_Mask_Last (1<<7)
#define MenuFlags_Mask_Dashes (1<<1)
#define MenuFlags_Mask_Tick (1<<0)


//****************************************************************
//* Part 2: SWI Paramters
//****************************************************************

//****************************************************************
// Polling

#define TASK 0x4B534154
#define PollMask_Standard 0x3831

#define ReasonCode_RedrawWindow 1
#define ReasonCode_OpenWindow 2
#define ReasonCode_CloseWindow 3
#define ReasonCode_MouseClick 6
#define ReasonCode_MenuSelection 9
#define ReasonCode_UserMessage 17
#define ReasonCode_UserMessageRecorded 18

#define WindowBackground (t_WindowHandle)(-1)
#define WindowIconbar (t_WindowHandle)(-2)

#define UserMessage_Quit 0

#define MouseButtonRight  0x1
#define MouseButtonMiddle 0x2
#define MouseButtonLeft   0x4

// Wimp Poll Block as a buffer
typedef struct {
	char Data[256];
} t_Buffer256;

// Wimp Poll block for a user message
typedef struct {
	int MessageLength;
	int SenderHandle;
	int MyRef;
	int YourRef;
	int MessageCode;
	char MessageData[236];
} t_WimpUserMessage;

// Wimp Poll block for a mouse click
typedef struct {
	int MouseX, MouseY;
	int Buttons;
	t_WindowHandle Window;
	t_IconHandle Icon;
	char Filler[236];
} t_WimpMouseClick;

// Wimp Poll block for menu selection
typedef struct {
	int MenuItem[63];  // = 256 Bytes
} t_WimpMenuSelection;

// Wimp Poll block for open window
typedef struct {
	t_WindowHandle WindowHandle;
	t_BoxenLuder Visible;
	int  ScrollX, ScrollY;
	t_WindowHandle WindowStack; // -1
	char Filler[224];
} t_WimpOpenWindow;

// Block for redraw window's GetRectangle
typedef struct {
	t_WindowHandle WindowHandle;
	t_BoxenLuder Visible;
	int  ScrollX, ScrollY;
	t_BoxenLuder Rectangle;
	char Filler[212];
} t_WimpGetRectangle;

// Wimp Poll block for close window
typedef struct {
	t_WindowHandle WindowHandle;
	char Filler[252];
} t_WimpCloseWindow;


// Wimp Poll Block with multiple formats
typedef union {
	t_Buffer256 Block;
	t_WimpUserMessage UserMessage;
	t_WimpMouseClick MouseClick;
	t_WimpMenuSelection MenuSelection;
	t_WimpOpenWindow OpenWindow;
	t_WimpCloseWindow CloseWindow;
	t_WimpGetRectangle RedrawWindow;
} t_WimpPollBlock;

//****************************************************************
// Icon SWIs

// Block for SWI Wimp_CreateIcon
typedef struct {
	t_WindowHandle Window;
	t_Icon Icon;
} t_WimpCreateIconBlock;

// Block for SWI Wimp_DeleteIcon
typedef struct {
	t_WindowHandle Window;
	t_IconHandle Icon;
} t_WimpDeleteIconBlock;

//****************************************************************
// OS stuff
