/* Simple BBC News ticker client
 * (c) Darren Salt
 * GPL applies
 * $Id: wimp.h,v 1.13 2004/08/01 16:35:35 ds Exp $
 */

#ifndef _Ticker_WIMP_H
#define _Ticker_WIMP_H

struct coordspair
{
  struct coords tl, br;
};

struct modeinfo_t
{
  struct coords min, max, size;
};

struct wimp_closewindow_block
{
  int window_handle;
};

union icon_data
{
  struct
  {
    char text[12];
  }
  t;

  struct
  {
    char *text;
    char *validation;
    int text_len;
  }
  it;

  struct
  {
    char *sprite;
    struct os_spriteop_area *sprite_area;
    int sprite_len;
  }
  is;
};

struct wimp_createicon_block
{
  int window_handle;
  struct coords min, max;
  int icon_flags;
  union icon_data contents;
};

struct wimp_createwindow_block
{
  struct coords min, max, scroll;
  int handle_behind;
  int window_flags;
  unsigned char title_fg, title_bg;
  unsigned char work_fg, work_bg;
  unsigned char scroll_outer_colour, scroll_inner_colour;
  unsigned char title_focus_colour;
  unsigned char flags;
  struct coords work_min, work_max;
  int icon_flags, workarea_flags;
  void *sprite_area;
  unsigned short min_width, min_height;
  union icon_data title;
  int number_of_icons;
  /* Icon definitions follow... */
};

struct wimp_deleteicon_block
{
  int window_handle, icon_handle;
};

struct wimp_deletewindow_block
{
  int window_handle;
};

struct wimp_dragbox_block
{
  int window_handle;
  int drag_type;
  struct coords min, max;
  struct coords parent_min, parent_max;
  int r12_value;

  /* Maybe... */
  int *drawbox_routine, *removebox_routine, *movebox_routine;
};

struct wimp_getcaretposition_block
{
  int window_handle, icon_handle;
  struct coords pos;
  int height_and_flags, index;
};

struct wimp_geticonstate_block
{
  int window_handle, icon_handle;
  struct coords min, max;
  int icon_flags;
  union icon_data contents;
};

struct wimp_getpointerinfo_block
{
  struct coords pos;
  int buttons;
  int window_handle, icon_handle;
};

struct wimp_getwindowinfo_block
{
  int window_handle;
  struct wimp_createwindow_block data;
  /* Icon definitions follow... */
};

struct wimp_getwindowstate_block
{
  int window_handle;
  struct coords min, max, scroll;
  int handle_behind;
  int window_flags;		/* only if you've used Wimp_GetWindowState :-) */
};

#define wimp_openwindow_block wimp_getwindowstate_block

union wimp_windowinfo_block
{
  int window_handle;
  struct wimp_getwindowinfo_block info;
  struct wimp_getwindowstate_block state;
  struct wimp_openwindow_block open;
};

struct wimp_getrectangle_block
{
  int window_handle;
  struct coords vismin, vismax, scroll;
  struct coords drawmin, drawmax;
};


struct wimp_reporterror_block
{
  int errnum;
  char errmess[252];
};

struct wimp_setextent_block
{
  struct coords min, max;
};

struct wimp_seticonstate_block
{
  int window_handle, icon_handle, eor, clear;
};

struct wimp_whichicon_block
{
  int icons[64];
};

struct wimp_ploticon_block
{
  struct coords min, max;
  int icon_flags;
  union icon_data contents;
};

struct wimp_extend_11_block
{
  int window_handle;
  struct
  {
    int left, bottom, right, top;
  }
  border;
  struct
  {
    int width;
  }
  back, close;
  int z0;
  struct
  {
    int width;
  }
  title;
  int z1;
  struct
  {
    int width;
  }
  iconise;
  struct
  {
    int width, height;
  }
  toggle;
  struct
  {
    struct
    {
      int height;
    }
    upper_gap, up, well, down, lower_gap;
  }
  vscroll;
  struct
  {
    int width, height;
  }
  adjust;
  struct
  {
    struct
    {
      int height;
    }
    right_gap, right, well, left, left_gap;
  }
  hscroll;
};


typedef struct user_message
{
  int length;
  int sender_ref;
  int my_ref;
  int your_ref;
  int msg_code;

  union
  {
    char bytes[236];

    struct
    {
      int window_handle;
      int icon_handle;
      struct coords pos;
      int size;
      int filetype;
      char file_name[212];
    }
    data_save, data_save_ack, data_load, data_load_ack;

    struct wimp_getpointerinfo_block help_request;

    char help_reply[236];

    struct
    {
      int submenu;
      struct coords pos;
      int selection[56];
    }
    menu_warning;

    struct
    {
      const int *handle;
      int slotsize;
      char taskname[228];
    }
    task_initialise;

    struct
    {
      int window_handle;
      int task_handle;
      char title[20];
    }
    window_iconise;

    struct
    {
      int window_handle;
      int reserved;
      char sprite[8];
      char title[220];
    }
    window_info;

    struct
    {
      int flags;
      int handle;
    }
    uri_mreturnresult;

  }
  contents;

}
user_message;


union wimp_poll_block
{
  struct wimp_getwindowstate_block open_window;
  /* for the window_flags field; content may not be valid */

  struct
  {
    int window_handle;
  }
  close_window, pointer_entering, pointer_leaving;

  struct wimp_getpointerinfo_block mouse_click;

  struct
  {
    struct coords min;
    struct coords max;
  }
  user_drag_box;

  struct
  {
    int window_handle;
    int icon_handle;
    struct coords pos;
    int height_and_flags;
    int index;
    int code;
  }
  key_pressed;

  struct
  {
    unsigned int menu[64];
  }
  menu_selection;

  user_message user;
};

enum ticker_state
{ is_CLOSED, is_CURRENT = 0, is_OPEN, is_MINIMISED };


#define erx(x) \
  { \
    _kernel_oserror *e_r_r = (x); \
    if (e_r_r) \
    { \
      report_oserror (e_r_r); \
      exit (2); \
    } \
  }

#define er(x) \
  { \
    _kernel_oserror *e_r_r = (x); \
    if (e_r_r) \
      report_oserror (e_r_r); \
  }

extern union wimp_poll_block poll_block;

extern int *sprites;
extern int spr22[3], spr11[3];

void wimp_init (void);
void wimp_quit (void);

void set_status_text (const char[], ...);
void set_status (enum state);

void start_drag (int window, int icon);

int poll (void);
int nullpoll (void);

void process_stories (void);

void set_ticker_align (struct wimp_openwindow_block *);
void set_ticker_width (struct wimp_openwindow_block *);
void set_ticker_top (void);
void open_ticker_window_minimised (struct wimp_openwindow_block *, int under);
void open_ticker_window (struct wimp_openwindow_block *, int under,
			 int reset);
void open_ticker_window_current (struct wimp_openwindow_block *, int under,
				 int reset);
void centre_window (struct wimp_openwindow_block *);
void open_sub (struct wimp_openwindow_block *,
	       struct wimp_openwindow_block *, int, int, int);
void close_window (int);
void open_menu (void *, int, int);

void status_errno (const char[]);
void status_error (const char[]);
void status_oserror (const char[], const _kernel_oserror[]);

void wimp_create_iconbar_icon (void);

/* In c.wimputil */

extern struct modeinfo_t mode, prevmode;

void free_indirected_icons (void);
int load_template (const char template_name[]);

void cache_mode_vars (void);

void wimp_extend_11 (struct wimp_extend_11_block *extend);

void set_menu_shade (void *menu, int entry, int available);

void centre_window (struct wimp_openwindow_block *block);
void open_sub (struct wimp_openwindow_block *main,
	       struct wimp_openwindow_block *pane, int xoff, int yoff,
	       int ext);

char *get_icon_text (int, int);
size_t get_icon_text_length (int, int);
void set_icon_text (int, int, const char *);
void set_icon_state (int, int, unsigned int, unsigned int);
void tick_icon (int, int, int);
void shade_icon (int, int, int);

int icon_ticked (int, int);

void set_caret_icon (int, int);

int get_text_width (const char text[]);

void report_errno (void);
void report_error (const char[]);
void report_oserror (const _kernel_oserror[]);
void report_oserrno (void);

void launch_url (const char *url, int allow_low);
void launch_url_ant (const char url[]);

void launch_url (const char *url, int allow_low);
void launch_url_ant (const char url[]);

int parse_url (int window, const char *file, int uri);

int is_window_open (int);
int is_save_window_open (void);

void pinboard_remove_ticker (void);


#define icn_TEXT_BIT		(1<<0)
#define icn_SPRITE_BIT		(1<<1)
#define icn_BORDER_BIT		(1<<2)
#define icn_HCENT_BIT		(1<<3)
#define icn_VCENT_BIT		(1<<4)
#define icn_FILLED_BIT		(1<<5)
#define icn_FONT_BIT		(1<<6)
#define icn_HELP_BIT		(1<<7)
#define icn_INDIRECTED_BIT	(1<<8)
#define icn_RJUSTIFY_BIT	(1<<9)
#define icn_ADJUST_BIT		(1<<10)
#define icn_HALFSIZE_BIT	(1<<11)
#define icn_TYPE(x)		((x)<<12)
#define icn_ESG(x)		((x)<<16)
#define icn_SELECTED_BIT	(1<<21)
#define icn_SHADED_BIT		(1<<22)
#define icn_DELETED_BIT		(1<<23)
#define icn_FGCOL(x)		((x)<<24)
#define icn_BGCOL(x)		((x)<<28)
#define icn_FHANDLE(x)		((x)<<24)

#define icn_TEXT_DEFAULTS \
  (icn_TEXT_BIT | icn_VCENT_BIT | icn_FGCOL(7) | icn_BGCOL(1))

#define WIMP_MESSAGE_QUIT		0
#define WIMP_MESSAGE_DATASAVE		1
#define WIMP_MESSAGE_DATASAVEACK	2
#define WIMP_MESSAGE_DATALOAD		3
#define WIMP_MESSAGE_DATALOADACK	4
#define WIMP_MESSAGE_PREQUIT		8
#define WIMP_MESSAGE_HELPREQUEST	0x502
#define WIMP_MESSAGE_HELPREPLY		0x503
#define WIMP_MESSAGE_MENUWARNING	0x400C0
#define WIMP_MESSAGE_MODECHANGE		0x400C1
#define WIMP_MESSAGE_TASKINITIALISE	0x400C2
#define WIMP_MESSAGE_MENUSDELETED	0x400C9
#define WIMP_MESSAGE_WINDOWCLOSED	0x400CB
#define WIMP_MESSAGE_WINDOWICONISE	0x400CC
#define WIMP_MESSAGE_LAUNCH_URL		0x4AF80
#define WIMP_MESSAGE_URI_MRETURNRESULT	0x4E383

#define URI_Dispatch		0x4E381
#define URI_RequestURI		0x4E382

/* In s.veneers */

_kernel_oserror *wimp_close_window (const int *);
_kernel_oserror *wimp_create_icon (struct wimp_createicon_block *, int, int *);
_kernel_oserror *wimp_create_icon_with_priority (struct wimp_createicon_block
						 *, int, int *, int);
_kernel_oserror *wimp_create_menu (const void *, int, int);
_kernel_oserror *wimp_delete_icon (int, int);
_kernel_oserror *wimp_get_icon_state (int, int,
				      struct wimp_geticonstate_block *);
_kernel_oserror *wimp_get_pointer_info (struct wimp_getpointerinfo_block *);
_kernel_oserror *wimp_get_window_state (struct wimp_getwindowstate_block *,
					int);
_kernel_oserror *wimp_get_window_info (struct wimp_getwindowinfo_block *,
				       int);
_kernel_oserror *wimp_open_window (struct wimp_openwindow_block *);
_kernel_oserror *wimp_open_window_ext (struct wimp_openwindow_block *,
				       int handle, int flags);
int wimp_report_error (const _kernel_oserror *, int, const char[]);
int wimp_report_error_ext (const _kernel_oserror *, int, const char[],
			   const char[], struct os_spriteop_area *,
			   const char[]);
_kernel_oserror *wimp_send_message (int, void *, int, int);
_kernel_oserror *wimp_reply_message (void *);
_kernel_oserror *wimp_set_icon_state (const struct wimp_seticonstate_block *);

#endif
