/* Copyright 1997 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/***************************************************/
/* File   : FromROSLib.h                           */
/*                                                 */
/* Purpose: The original Customer browser sources */
/*          used RISC_OSLib heavily and some of    */
/*          the functions used are handy to have   */
/*          around. This source file contains      */
/*          a variety of functions from RISC_OSLib */
/*          with modifications where necessary     */
/*          both to fit into the browser more      */
/*          naturally, and to function better      */
/*          under C release 5.                     */
/*                                                 */
/* Authors: Various for original RISC_OSLib source */
/*          This source adapted by A.D.Hodgkinson  */
/*                                                 */
/* History: 06-Dec-96: Created.                    */
/***************************************************/

/* VDU commands */

#define BBC_CharToPrinter  1  /* Send next character to the printer      */
#define BBC_EnablePrinter  2  /* Enable printer                          */
#define BBC_DisablePrinter 3  /* Disable printer                         */
#define BBC_TextToText     4  /* Have separate text and graphics cursors */
#define BBC_TextToGraph    5  /* Join text cursor to graphics cursor     */
#define BBC_EnableVDU      6  /* Enable screen output                    */
#define BBC_Bell           7  /* Bell (beep)                             */
#define BBC_MoveOneBack    8  /* Back space / move left                  */
#define BBC_MoveOneOn      9  /* Horizontal tab / move right             */
#define BBC_MoveDownOne    10 /* Line feed / move down                   */
#define BBC_MoveUpOne      11 /* Vertical tab / move up                  */
#define BBC_ClearText      12 /* Form feed / clear screen                */
#define BBC_MoveToStart    13 /* Carriage return                         */
#define BBC_PageOn         14 /* Page mode on (wait before scrolling)    */
#define BBC_PageOff        15 /* Page mode off                           */
#define BBC_ClearGraph     16 /* Clear current graphics window           */
#define BBC_DefTextColour  17 /* Set text colour                         */
#define BBC_DefGraphColour 18 /* Set graphics colour and plotting action */
#define BBC_DefLogical     19 /* Set palette                             */
#define BBC_RestoreLogical 20 /* Restore default BBC colours             */
#define BBC_DisableVDU     21 /* Disable screen output                   */
#define BBC_ScreenMode     22 /* Change screen mode                      */
#define BBC_MultiPurpose   23 /* Multiple purpose command                */
#define BBC_DefGraphWindow 24 /* Define graphics window                  */
#define BBC_PlotCommand    25 /* Graphics plotting command               */
#define BBC_DefaultWindow  26 /* Give default text and graphics windows  */
                              /* 27 is undefined (it's a no-op)          */
#define BBC_DefTextWindow  28 /* Define text window                      */
#define BBC_DefGraphOrigin 29 /* Set graphics origin                     */
#define BBC_HomeText       30 /* Move cursors to Home (0,0)              */
#define BBC_MoveText       31 /* Position text cursor                    */

/* Graphics plotting commands */

#define BBC_SolidBoth        0x00 /* Solid line including both ends      */
#define BBC_SolidExFinal     0x08 /* Sold line excluding the end point   */
#define BBC_DottedBoth       0x10 /* Dotted line including both ends     */
#define BBC_DottedExFinal    0x18 /* Dotted line excluding the end point */
#define BBC_SolidExInit      0x20 /* Solid line excluding first point    */
#define BBC_SolidExBoth      0x28 /* Solid line excluding both ends      */
#define BBC_DottedExInit     0x30 /* Dotted line excluding first point   */
#define BBC_DottedExBoth     0x38 /* Dotted line excluding both ends     */
#define BBC_Point            0x40 /* Plot a single pixel                 */
#define BBC_HorizLineFillNB  0x48 /* Horizontal line fill (left and      */
                                  /* right) to non-background            */
#define BBC_TriangleFill     0x50 /* Filled triangle                     */
#define BBC_HorizLineFillB   0x58 /* Horizontal line fill (right only)   */
                                  /* to background                       */
#define BBC_RectangleFill    0x60 /* Filled rectangle                    */
#define BBC_HorizLineFillF   0x68 /* Horizontal line fill (left and      */
                                  /* right) to foreground                */
#define BBC_ParallelFill     0x70 /* Filled parallelogram                */
#define BBC_HorizLineFillNF  0x78 /* Horizontal line fill (right only)   */
                                  /* to non-foreground                   */
#define BBC_FloodToBack      0x80 /* Flood fill to non-background        */
#define BBC_FloodToFore      0x88 /* Flood fill to foreground            */
#define BBC_Circle           0x90 /* Circle outline                      */
#define BBC_CircleFill       0x98 /* Filled circle                       */
#define BBC_CircleArc        0xA0 /* Circle arc                          */
#define BBC_Segment          0xA8 /* Circle segment                      */
#define BBC_Sector           0xB0 /* Circle sector                       */

#define BBC_Block            0xB8 /* Block ops (special case, see below) */

#define BBC_Ellipse          0xC0 /* Ellipse outline                     */
#define BBC_EllipseFill      0xC8 /* Filled ellipse                      */
#define BBC_GraphicsChar     0xD0 /* Font printing                       */
#define BBC_SpritePlot       0xE8 /* Sprite plot                         */

/* Graphics plotting modifiers; within each block of eight above, */
/* the offset from the base number has the following meaning      */
/* (unless using the BBC_Block group):                            */

#define BBC_MoveCursorRel    0    /* Move cursor relative                */
#define BBC_DrawRelFore      1    /* Draw relative                       */
#define BBC_DrawRelInverse   2    /* Draw inverted relative              */
#define BBC_DrawRelBack      3    /* Draw background relativey           */
#define BBC_MoveCursorAbs    4    /* Move cursor absolute                */
#define BBC_DrawAbsFore      5    /* Draw absolute                       */
#define BBC_DrawAbsInverse   6    /* Draw inverted absolute              */
#define BBC_DrawAbsBack      7    /* Draw background absolute            */

/* When using the block operations, the modifers work as follows: */

#define BBC_BMoveRel         0    /* Move relative                       */
#define BBC_BMoveRectRel     1    /* Move retangle relative              */
#define BBC_BCopyRectRel     2    /* Copy rectangle relative             */
                                  /* 3 is undefined (same as code 2)     */
#define BBC_BMoveAbs         4    /* Move absolute                       */
#define BBC_BMoveRectAbs     5    /* Move rectangle absolute             */
#define BBC_BCopyRectAbs     6    /* Copy rectangle absolute             */
                                  /* 7 is undefined (same as code 6)     */

/* Local structures and enumerations */

/* VDU variables, as used by OS_ReadVduVariable */

typedef enum
{
  BBC_GWLCol = 128,    /* Graphics window left hand,                                 */
  BBC_GWBRow,          /* bottom,                                                    */
  BBC_GWRCol,          /* right hand, and                                            */
  BBC_GWTRow,          /* top bounding coordinates, all inclusive.                   */
  BBC_TWLCol,          /* Text window lefthand,                                      */
  BBC_TWBRow,          /* bottom,                                                    */
  BBC_TWRCol,          /* right hand, and                                            */
  BBC_TWTRow,          /* top bounding coordinates, all inclusive.                   */
  BBC_OrgX,            /* Graphics origin X,                                         */
  BBC_OrgY,            /* and Y coords (external coords)                             */
  BBC_GCsX,            /* Graphics cursor X,                                         */
  BBC_GCsY,            /* and Y coords (external coords)                             */
  BBC_OlderCsX,        /* Oldest graphics cursor X,                                  */
  BBC_OlderCsY,        /* and Y coords (internal coords)                             */
  BBC_OldCsX,          /* Previous graphics cursor X,                                */
  BBC_OldCsY,          /* and Y coords (internal coords)                             */
  BBC_GCsIX,           /* Graphics cursor X,                                         */
  BBC_GCsIY,           /* and Y coords (internal coords)                             */
  BBC_NewPtX,          /* New point X,                                               */
  BBC_NewPtY,          /* and Y coords (internal coords)                             */
  BBC_ScreenStart,     /* Start of screen memory, as used by VDU drivers             */
  BBC_DisplayStart,    /* Start of screen memory, as used by hardware                */
  BBC_TotalScreenSize, /* Memory currently allocated to screens                      */
  BBC_GPLFMD,          /* GCOL action for foreground colour                          */
  BBC_CPLBMD,          /* GCOL action for background colour                          */
  BBC_GFCOL,           /* Graphics foreground colour                                 */
  BBC_GBCOL,           /* Graphics background colour                                 */
  BBC_TForeCol,        /* Text foreground colour                                     */
  BBC_TBackCol,        /* Text background colour                                     */
  BBC_GFTint,          /* Graphics foreground tint                                   */
  BBC_GBTint,          /* Graphics background tint                                   */
  BBC_TFTint,          /* Text foreground tint                                       */
  BBC_TBTint,          /* Text background tint                                       */
  BBC_MaxMode,         /* Highest old-style (numbered) mode available                */
  BBC_GCharSizeX,      /* VDU 5 system font X size,                                  */
  BBC_GCharSizeY,      /* and Y size in pixels                                       */
  BBC_GCharSpaceX,     /* VDU 5 system font X spacing,                               */
  BBC_GCharSpaceY,     /* and Y spacing in pixels                                    */
  BBC_HLineAddr,       /* Address of horizontal line drawing routine                 */
  BBC_TCharSizeX,      /* VDU 4 system font X size,                                  */
  BBC_TCharSizeY,      /* and Y sized in pixels                                      */
  BBC_TCharSpaceX,     /* VDU 4 system font X spacing,                               */
  BBC_TCharSpaceY      /* and Y spacing in pixels                                    */

} bbc_vduvariable;

/* Mode variables, as used by OS_ReadModeVariable */

typedef enum
{
  BBC_ModeFlags = 0,   /* Bit 0: Non-graphics mode                                   */
                       /* Bit 1: Teletext mode                                       */
                       /* Bit 2: Gap mode (there is a gap between VDU 4 lines)       */
                       /* Bit 3: BBC gap mode (3 and 6)                              */
                       /* Bit 4: Hi-res mono mode                                    */
                       /* Bit 5: Double height VDU chars                             */
                       /* Bit 6: Hardware scroll never used                          */
  BBC_ScrRCol,         /* Maximum text col number                                    */
  BBC_ScrBCol,         /* Maximum text row number                                    */
  BBC_NColour,         /* Maximum logical colour: 1, 3, 15 or 63                     */
  BBC_XEigFactor,      /* OS-unit->pixel shift factor. 0 -> OS-units = pixels,       */
  BBC_YEigFactor,      /* 1 -> 2 OS-units per pixel, 2 -> 4 OS-units per pixel, etc. */
  BBC_LineLength,      /* Bytes per pixel row                                        */
  BBC_ScreenSize,      /* Bytes per screen                                           */
  BBC_YShftFactor,     /* DEPRECATED; Log(2) of LineLength/5                         */
  BBC_Log2BPP,         /* Log base 2 of bits per pixel                               */
  BBC_Log2BPC,         /* Log base 2 of bytes per character                          */
  BBC_XWindLimit,      /* Screen width in pixels, minus 1                            */
  BBC_YWindLimit       /* Screen height in pixels, minus 1                           */

} bbc_modevariable;

/* RISCOS_Lib derived Wimp structure, unions and types */

typedef union           /* The data field in an icon              */
{
  char text[12];        /* Up to 12 bytes of text                 */
  char sprite_name[12]; /* Up to 12 bytes of sprite name          */

  struct
  {
    char * name;
    void * spritearea;  /* 0->use the common sprite area          */
                        /* 1->use the wimp sprite area            */
    int    nameisname;  /* If 0, name is in fact a sprite pointer */

  } indirectsprite;

  struct
  {
    char * buffer;      /* Pointer to text buffer                 */
    char * validstring; /* Pointer to validation string           */
    int    bufflen;     /* Length of text buffer                  */

  } indirecttext;

} wimp_icondata;

typedef struct
{
  char title[12];       /* Menu title (optional)          */
  char tit_fcol;        /* Title foreground colour        */
  char tit_bcol;        /* Title background colour        */
  char work_fcol;       /* Workarea foreground colour     */
  char work_bcol;       /* Workarea background colour     */
  int  width;           /* Width of following menu items  */
  int  height;          /* Height of following menu items */
  int  gap;             /* Vertical gap between items     */

} wimp_menuhdr;

typedef struct
{
  wimp_menuhdr hdr;         /* Followed by 'n' wimp_menuitem structs    */

} wimp_menustr;

typedef struct wimp_menustr * wimp_menuptr;

typedef struct
{
  unsigned int   flags;     /* Menu entry flags                         */
  wimp_menuptr   submenu;   /* Pointer to submenu, or -1 for no submenu */
  unsigned int   iconflags; /* Icon flags for the entry                 */
  wimp_icondata  data;      /* Icon data for the entry                  */

} wimp_menuitem;

/* RISCOS_Lib derived Wimp enumerations */

typedef enum
{
  wimp_MTICK       = 1,
  wimp_MSEPARATE   = 2,
  wimp_MWRITABLE   = 4,
  wimp_MSUBLINKMSG = 8,    /* Show a => flag               */
  wimp_MLAST       = 0x80, /* Signal last item in the menu */
  wimp_MINDIRECTED = 0x100

} wimp_menuflags;

typedef enum
{
  wimp_ITEXT        = 0x00000001, /* Icon contains text                                                */
  wimp_ISPRITE      = 0x00000002, /* Icon is a sprite                                                  */
  wimp_IBORDER      = 0x00000004, /* Icon has a border                                                 */
  wimp_IHCENTRE     = 0x00000008, /* Text is horizontally centred                                      */
  wimp_IVCENTRE     = 0x00000010, /* Text is vertically centred                                        */
  wimp_IFILLED      = 0x00000020, /* Icon has a filled background                                      */
  wimp_IFONT        = 0x00000040, /* Text is an anti-aliased font                                      */
  wimp_IREDRAW      = 0x00000080, /* Redraw needs application's help                                   */
  wimp_INDIRECT     = 0x00000100, /* Icon data is 'indirected'                                         */
  wimp_IRJUST       = 0x00000200, /* Text right justified in box                                       */
  wimp_IESG_NOC     = 0x00000400, /* If selected by right button, don't cancel other icons in same ESG */
  wimp_IHALVESPRITE = 0x00000800, /* Plot sprites half-size                                            */
  wimp_IBTYPE       = 0x00001000, /* 4-bit field: button type                                          */
  wimp_ISELECTED    = 0x00200000, /* Icon selected by user (inverted)                                  */
  wimp_INOSELECT    = 0x00400000, /* Icon cannot be selected (shaded)                                  */
  wimp_IDELETED     = 0x00800000, /* Icon has been deleted                                             */
  wimp_IFORECOL     = 0x01000000, /* 4-bit field: foreground colour                                    */
  wimp_IBACKCOL     = 0x10000000  /* 4-bit field: background colour                                    */

} wimp_iconflags;

typedef enum
{
  wimp_BIGNORE,           /* Ignore all mouse ops                */
  wimp_BNOTIFY,
  wimp_BCLICKAUTO,
  wimp_BCLICKDEBOUNCE,
  wimp_BSELREL,
  wimp_BSELDOUBLE,
  wimp_BDEBOUNCEDRAG,
  wimp_BRELEASEDRAG,
  wimp_BDOUBLEDRAG,
  wimp_BSELNOTIFY,
  wimp_BCLICKDRAGDOUBLE,
  wimp_BCLICKSEL,         /* Useful for on/off and radio buttons */
  wimp_BWRITABLE = 15

} wimp_ibtype;

/* RISCOS_Lib derived key defintions */

#define akbd_Fn        (256 + 128)
#define akbd_Sh        (16)
#define akbd_Ctl       (32)
#define akbd_TabK      (akbd_Fn + 10)
#define akbd_CopyK     (akbd_Fn + 11)
#define akbd_LeftK     (akbd_Fn + 12)
#define akbd_RightK    (akbd_Fn + 13)
#define akbd_DownK     (akbd_Fn + 14)
#define akbd_UpK       (akbd_Fn + 15)
#define akbd_HomeK     (0x01E)
#define akbd_Fn10      (0x1CA)
#define akbd_Fn11      (0x1CB)
#define akbd_Fn12      (0x1CC)
#define akbd_InsertK   (0x1CD)
#define akbd_PrintK    (akbd_Fn + 0)
#define akbd_PageUpK   (akbd_Sh + akbd_UpK)
#define akbd_PageDownK (akbd_Sh + akbd_DownK)

/* Functions that are defined in FromROSLib.s */

_kernel_oserror * bbc_vdu(int);
_kernel_oserror * bbc_vduw(int);

/* Function prototypes */

int               bbc_modevar           (int mode, int varno);
int               bbc_vduvar            (int varno);
_kernel_oserror * bbc_vduvars           (int * vars, int * values);

_kernel_oserror * bbc_vduq              (int c,...);

_kernel_oserror * bbc_clg               (void);
_kernel_oserror * bbc_gwindow           (int xmin, int ymin, int xmax, int ymax);
_kernel_oserror * bbc_origin            (int x, int y);
_kernel_oserror * bbc_gcol              (int a, int b);

_kernel_oserror * bbc_plot              (int n, int x, int y);
_kernel_oserror * bbc_move              (int x, int y);
_kernel_oserror * bbc_moveby            (int x, int y);
_kernel_oserror * bbc_draw              (int x, int y);
_kernel_oserror * bbc_drawby            (int x, int y);
_kernel_oserror * bbc_rectangle         (int x, int y, int w, int h);
_kernel_oserror * bbc_rectanglefill     (int x, int y, int w, int h);
_kernel_oserror * bbc_circle            (int x, int y, int r);
_kernel_oserror * bbc_circlefill        (int x, int y, int r);
_kernel_oserror * bbc_trianglefill      (int x1, int y1, int x2, int y2, int x3, int y3);
_kernel_oserror * bbc_fill              (int x, int y);

void              wimpt_read            (void);
int               wimpt_dx              (void);
int               wimpt_dy              (void);

int               coords_x_toscreen     (int x, WimpRedrawWindowBlock * r);
int               coords_y_toscreen     (int y, WimpRedrawWindowBlock * r);
void              coords_box_toscreen   (BBox * b, WimpRedrawWindowBlock * r);
int               coords_x_toworkarea   (int x, WimpRedrawWindowBlock * r);
int               coords_y_toworkarea   (int x, WimpRedrawWindowBlock * r);
void              coords_box_toworkarea (BBox * b, WimpRedrawWindowBlock * r);