/*
 *  gui_riscos.h -- portable gui library, RISC OS header
 */

#include "oslib/colourtrans.h"
#include "oslib/help.h"
#include "oslib/messagetrans.h"
#include "oslib/osbyte.h"
#include "oslib/wimp.h"
#include "ufont.h"

typedef unsigned int gui_window_id;
#define gui_WINDOW_UNKNOWN ((gui_window_id) 0xffffffff)
typedef unsigned int gui_gadget_id;
typedef wimp_menu *gui_menu_id;

extern wimp_t gui_wimp_task;                          /* wimp task handle */
extern os_error *gui_error;                           /* last error block pointer */
extern const char *gui_error_message;                 /* last error message */
extern int gui_error_line;                            /* last error line */
extern jmp_buf gui_error_jump;                        /* longjmp buffer for error handling */

struct gui_dialog_icon
{
  enum { ICON_NONE, ICON_ACTION } type;
  void (*click_fn)(gui_window_id window_id, gui_gadget_id gadget_id);
  const char *help;
};

struct gui_window_data
{
  enum { UNUSED, WINDOW, DIALOG } used;
  wimp_w handle;
  char *title;
  void (*close_fn)(gui_window_id window_id);
  void (*redraw_fn)(gui_window_id window_id, unsigned int invalid[]);
  void (*click_fn)(gui_window_id window_id, unsigned int x, unsigned int y, unsigned int z);
  void (*resize_fn)(gui_window_id window_id, unsigned int width, unsigned int height);
  bool (*key_fn)(gui_window_id window_id, unsigned int key);
  void (*input_fn)(gui_window_id window_id, wchar_t key);
  void (*menu_fn)(gui_window_id window_id, unsigned int items[]);
  unsigned int width, height;
  wimp_menu *menu;
  wimp_w *bbar;
  struct gui_dialog_icon *icon;
  const char *help;
};
extern struct gui_window_data *gui_window_list;           /* array of window details */
extern unsigned int gui_window_list_items;                /* length of windows list */

extern gui_window_id gui_get_free_window_id(void);
extern gui_window_id gui_handle_to_window_id(wimp_w handle);
extern signed int gui_window_origin_x, gui_window_origin_y;

#define WINDOW_LIST_CHUNK 20


struct gui_ta_style
{
  unsigned int xsize;
  unsigned int ysize;
  unsigned int fcolour;
  unsigned int bcolour;
  unsigned int space;
  ufont_f handle;
  char font[1];
};

#define gui_SIZEOF_GUI_TA_STYLE(n) (offsetof(struct gui_ta_style, font) + (n) * sizeof(char))


#define KEY_BACKSPACE wimp_KEY_BACKSPACE
#define KEY_RETURN wimp_KEY_RETURN
#define KEY_ESCAPE wimp_KEY_ESCAPE
#define KEY_HOME wimp_KEY_HOME
#define KEY_DELETE wimp_KEY_DELETE
#define KEY_SHIFT wimp_KEY_SHIFT
#define KEY_CONTROL wimp_KEY_CONTROL
#define KEY_PRINT wimp_KEY_PRINT
#define KEY_F1 wimp_KEY_F1
#define KEY_F2 wimp_KEY_F2
#define KEY_F3 wimp_KEY_F3
#define KEY_F4 wimp_KEY_F4
#define KEY_F5 wimp_KEY_F5
#define KEY_F6 wimp_KEY_F6
#define KEY_F7 wimp_KEY_F7
#define KEY_F8 wimp_KEY_F8
#define KEY_F9 wimp_KEY_F9
#define KEY_TAB wimp_KEY_TAB
#define KEY_COPY wimp_KEY_COPY
#define KEY_LEFT wimp_KEY_LEFT
#define KEY_RIGHT wimp_KEY_RIGHT
#define KEY_LOGO wimp_KEY_LOGO
#define KEY_MENU wimp_KEY_MENU
#define KEY_F10 wimp_KEY_F10
#define KEY_F11 wimp_KEY_F11
#define KEY_F12 wimp_KEY_F12
#define KEY_INSERT wimp_KEY_INSERT
#define KEY_DOWN wimp_KEY_DOWN
#define KEY_UP wimp_KEY_UP
#define KEY_PAGE_DOWN wimp_KEY_PAGE_DOWN
#define KEY_PAGE_UP wimp_KEY_PAGE_UP




/*
 *  Error handling
 */

/*
 *  gui_check_error -- check for an error after a swi
 */

#define gui_check_error() if (gui_error) { gui_error_message = gui_error->errmess; gui_error_line = __LINE__; longjmp(gui_error_jump, 1); }

/*
 *  gui_raise_error -- generate an error
 */

#define gui_raise_error(message) { gui_error_message = (message); gui_error_line = __LINE__; longjmp(gui_error_jump, 1); }


/*
 *  assert
 */

#ifdef DEBUG
  #define assert(expression) ((void) ((expression) ? 0 : (printf("%s:%u: *** assert(" #expression ") failed ***\n", __FILE__, __LINE__), exit(EXIT_FAILURE), 0)))
#else
  #define assert(expression) ((void) 0)
#endif
